diff --git a/.core_files.yaml b/.core_files.yaml index 3f92ed87a84f04..4a11d5da27c5e4 100644 --- a/.core_files.yaml +++ b/.core_files.yaml @@ -61,6 +61,7 @@ components: &components - homeassistant/components/auth/** - homeassistant/components/automation/** - homeassistant/components/backup/** + - homeassistant/components/blueprint/** - homeassistant/components/bluetooth/** - homeassistant/components/cloud/** - homeassistant/components/config/** diff --git a/.github/workflows/builder.yml b/.github/workflows/builder.yml index 7f3c0b0e66e27c..ddb204ca42de67 100644 --- a/.github/workflows/builder.yml +++ b/.github/workflows/builder.yml @@ -32,7 +32,7 @@ jobs: fetch-depth: 0 - name: Set up Python ${{ env.DEFAULT_PYTHON }} - uses: actions/setup-python@v5.1.1 + uses: actions/setup-python@v5.2.0 with: python-version: ${{ env.DEFAULT_PYTHON }} @@ -69,7 +69,7 @@ jobs: run: find ./homeassistant/components/*/translations -name "*.json" | tar zcvf translations.tar.gz -T - - name: Upload translations - uses: actions/upload-artifact@v4.3.6 + uses: actions/upload-artifact@v4.4.0 with: name: translations path: translations.tar.gz @@ -116,7 +116,7 @@ jobs: - name: Set up Python ${{ env.DEFAULT_PYTHON }} if: needs.init.outputs.channel == 'dev' - uses: actions/setup-python@v5.1.1 + uses: actions/setup-python@v5.2.0 with: python-version: ${{ env.DEFAULT_PYTHON }} @@ -197,7 +197,7 @@ jobs: password: ${{ secrets.GITHUB_TOKEN }} - name: Build base image - uses: home-assistant/builder@2024.08.1 + uses: home-assistant/builder@2024.08.2 with: args: | $BUILD_ARGS \ @@ -263,7 +263,7 @@ jobs: password: ${{ secrets.GITHUB_TOKEN }} - name: Build base image - uses: home-assistant/builder@2024.08.1 + uses: home-assistant/builder@2024.08.2 with: args: | $BUILD_ARGS \ @@ -453,7 +453,7 @@ jobs: uses: actions/checkout@v4.1.7 - name: Set up Python ${{ env.DEFAULT_PYTHON }} - uses: actions/setup-python@v5.1.1 + uses: actions/setup-python@v5.2.0 with: python-version: ${{ env.DEFAULT_PYTHON }} @@ -482,3 +482,56 @@ jobs: export TWINE_PASSWORD="${{ secrets.TWINE_TOKEN }}" twine upload dist/* --skip-existing + + hassfest-image: + name: Build and test hassfest image + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + attestations: write + id-token: write + needs: ["init"] + if: github.repository_owner == 'home-assistant' + env: + HASSFEST_IMAGE_NAME: ghcr.io/home-assistant/hassfest + HASSFEST_IMAGE_TAG: ghcr.io/home-assistant/hassfest:${{ needs.init.outputs.version }} + steps: + - name: Checkout repository + uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 + + - name: Login to GitHub Container Registry + uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Build Docker image + uses: docker/build-push-action@5cd11c3a4ced054e52742c5fd54dca954e0edd85 # v6.7.0 + with: + context: . # So action will not pull the repository again + file: ./script/hassfest/docker/Dockerfile + load: true + tags: ${{ env.HASSFEST_IMAGE_TAG }} + + - name: Run hassfest against core + run: docker run --rm -v ${{ github.workspace }}/homeassistant:/github/workspace/homeassistant ${{ env.HASSFEST_IMAGE_TAG }} --core-integrations-path=/github/workspace/homeassistant/components + + - name: Push Docker image + if: needs.init.outputs.channel != 'dev' && needs.init.outputs.publish == 'true' + id: push + uses: docker/build-push-action@5cd11c3a4ced054e52742c5fd54dca954e0edd85 # v6.7.0 + with: + context: . # So action will not pull the repository again + file: ./script/hassfest/docker/Dockerfile + push: true + tags: ${{ env.HASSFEST_IMAGE_TAG }},${{ env.HASSFEST_IMAGE_NAME }}:latest + + - name: Generate artifact attestation + if: needs.init.outputs.channel != 'dev' && needs.init.outputs.publish == 'true' + uses: actions/attest-build-provenance@6149ea5740be74af77f260b9db67e633f6b0a9a1 # v1.4.2 + with: + subject-name: ${{ env.HASSFEST_IMAGE_NAME }} + subject-digest: ${{ steps.push.outputs.digest }} + push-to-registry: true diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 0f0850ade1af36..d35187a3c45ffd 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -40,7 +40,7 @@ env: CACHE_VERSION: 10 UV_CACHE_VERSION: 1 MYPY_CACHE_VERSION: 8 - HA_SHORT_VERSION: "2024.9" + HA_SHORT_VERSION: "2024.10" DEFAULT_PYTHON: "3.12" ALL_PYTHON_VERSIONS: "['3.12']" # 10.3 is the oldest supported version @@ -234,7 +234,7 @@ jobs: uses: actions/checkout@v4.1.7 - name: Set up Python ${{ env.DEFAULT_PYTHON }} id: python - uses: actions/setup-python@v5.1.1 + uses: actions/setup-python@v5.2.0 with: python-version: ${{ env.DEFAULT_PYTHON }} check-latest: true @@ -279,7 +279,7 @@ jobs: - name: Check out code from GitHub uses: actions/checkout@v4.1.7 - name: Set up Python ${{ env.DEFAULT_PYTHON }} - uses: actions/setup-python@v5.1.1 + uses: actions/setup-python@v5.2.0 id: python with: python-version: ${{ env.DEFAULT_PYTHON }} @@ -319,7 +319,7 @@ jobs: - name: Check out code from GitHub uses: actions/checkout@v4.1.7 - name: Set up Python ${{ env.DEFAULT_PYTHON }} - uses: actions/setup-python@v5.1.1 + uses: actions/setup-python@v5.2.0 id: python with: python-version: ${{ env.DEFAULT_PYTHON }} @@ -359,7 +359,7 @@ jobs: - name: Check out code from GitHub uses: actions/checkout@v4.1.7 - name: Set up Python ${{ env.DEFAULT_PYTHON }} - uses: actions/setup-python@v5.1.1 + uses: actions/setup-python@v5.2.0 id: python with: python-version: ${{ env.DEFAULT_PYTHON }} @@ -429,17 +429,28 @@ jobs: . venv/bin/activate pre-commit run --show-diff-on-failure --hook-stage manual codespell --all-files + lint-hadolint: + name: Check ${{ matrix.file }} + runs-on: ubuntu-24.04 + needs: + - info + - pre-commit + strategy: + fail-fast: false + matrix: + file: + - Dockerfile + - Dockerfile.dev + steps: + - name: Check out code from GitHub + uses: actions/checkout@v4.1.7 - name: Register hadolint problem matcher run: | echo "::add-matcher::.github/workflows/matchers/hadolint.json" - - name: Check Dockerfile - uses: docker://hadolint/hadolint:v1.18.2 - with: - args: hadolint Dockerfile - - name: Check Dockerfile.dev - uses: docker://hadolint/hadolint:v1.18.2 + - name: Check ${{ matrix.file }} + uses: docker://hadolint/hadolint:v2.12.0 with: - args: hadolint Dockerfile.dev + args: hadolint ${{ matrix.file }} base: name: Prepare dependencies @@ -454,7 +465,7 @@ jobs: uses: actions/checkout@v4.1.7 - name: Set up Python ${{ matrix.python-version }} id: python - uses: actions/setup-python@v5.1.1 + uses: actions/setup-python@v5.2.0 with: python-version: ${{ matrix.python-version }} check-latest: true @@ -538,7 +549,7 @@ jobs: uses: actions/checkout@v4.1.7 - name: Set up Python ${{ env.DEFAULT_PYTHON }} id: python - uses: actions/setup-python@v5.1.1 + uses: actions/setup-python@v5.2.0 with: python-version: ${{ env.DEFAULT_PYTHON }} check-latest: true @@ -571,7 +582,7 @@ jobs: uses: actions/checkout@v4.1.7 - name: Set up Python ${{ env.DEFAULT_PYTHON }} id: python - uses: actions/setup-python@v5.1.1 + uses: actions/setup-python@v5.2.0 with: python-version: ${{ env.DEFAULT_PYTHON }} check-latest: true @@ -605,7 +616,7 @@ jobs: uses: actions/checkout@v4.1.7 - name: Set up Python ${{ env.DEFAULT_PYTHON }} id: python - uses: actions/setup-python@v5.1.1 + uses: actions/setup-python@v5.2.0 with: python-version: ${{ env.DEFAULT_PYTHON }} check-latest: true @@ -623,7 +634,7 @@ jobs: . venv/bin/activate pip-licenses --format=json --output-file=licenses.json - name: Upload licenses - uses: actions/upload-artifact@v4.3.6 + uses: actions/upload-artifact@v4.4.0 with: name: licenses path: licenses.json @@ -648,7 +659,7 @@ jobs: uses: actions/checkout@v4.1.7 - name: Set up Python ${{ env.DEFAULT_PYTHON }} id: python - uses: actions/setup-python@v5.1.1 + uses: actions/setup-python@v5.2.0 with: python-version: ${{ env.DEFAULT_PYTHON }} check-latest: true @@ -695,7 +706,7 @@ jobs: uses: actions/checkout@v4.1.7 - name: Set up Python ${{ env.DEFAULT_PYTHON }} id: python - uses: actions/setup-python@v5.1.1 + uses: actions/setup-python@v5.2.0 with: python-version: ${{ env.DEFAULT_PYTHON }} check-latest: true @@ -716,14 +727,14 @@ jobs: run: | . venv/bin/activate python --version - pylint --ignore-missing-annotations=y tests + pylint tests - name: Run pylint (partially) if: needs.info.outputs.test_full_suite == 'false' shell: bash run: | . venv/bin/activate python --version - pylint --ignore-missing-annotations=y tests/components/${{ needs.info.outputs.tests_glob }} + pylint tests/components/${{ needs.info.outputs.tests_glob }} mypy: name: Check mypy @@ -740,7 +751,7 @@ jobs: uses: actions/checkout@v4.1.7 - name: Set up Python ${{ env.DEFAULT_PYTHON }} id: python - uses: actions/setup-python@v5.1.1 + uses: actions/setup-python@v5.2.0 with: python-version: ${{ env.DEFAULT_PYTHON }} check-latest: true @@ -815,7 +826,7 @@ jobs: uses: actions/checkout@v4.1.7 - name: Set up Python ${{ env.DEFAULT_PYTHON }} id: python - uses: actions/setup-python@v5.1.1 + uses: actions/setup-python@v5.2.0 with: python-version: ${{ env.DEFAULT_PYTHON }} check-latest: true @@ -833,7 +844,7 @@ jobs: . venv/bin/activate python -m script.split_tests ${{ needs.info.outputs.test_group_count }} tests - name: Upload pytest_buckets - uses: actions/upload-artifact@v4.3.6 + uses: actions/upload-artifact@v4.4.0 with: name: pytest_buckets path: pytest_buckets.txt @@ -879,7 +890,7 @@ jobs: uses: actions/checkout@v4.1.7 - name: Set up Python ${{ matrix.python-version }} id: python - uses: actions/setup-python@v5.1.1 + uses: actions/setup-python@v5.2.0 with: python-version: ${{ matrix.python-version }} check-latest: true @@ -934,14 +945,14 @@ jobs: 2>&1 | tee pytest-${{ matrix.python-version }}-${{ matrix.group }}.txt - name: Upload pytest output if: success() || failure() && steps.pytest-full.conclusion == 'failure' - uses: actions/upload-artifact@v4.3.6 + uses: actions/upload-artifact@v4.4.0 with: name: pytest-${{ github.run_number }}-${{ matrix.python-version }}-${{ matrix.group }} path: pytest-*.txt overwrite: true - name: Upload coverage artifact if: needs.info.outputs.skip_coverage != 'true' - uses: actions/upload-artifact@v4.3.6 + uses: actions/upload-artifact@v4.4.0 with: name: coverage-${{ matrix.python-version }}-${{ matrix.group }} path: coverage.xml @@ -999,7 +1010,7 @@ jobs: uses: actions/checkout@v4.1.7 - name: Set up Python ${{ matrix.python-version }} id: python - uses: actions/setup-python@v5.1.1 + uses: actions/setup-python@v5.2.0 with: python-version: ${{ matrix.python-version }} check-latest: true @@ -1060,7 +1071,7 @@ jobs: 2>&1 | tee pytest-${{ matrix.python-version }}-${mariadb}.txt - name: Upload pytest output if: success() || failure() && steps.pytest-partial.conclusion == 'failure' - uses: actions/upload-artifact@v4.3.6 + uses: actions/upload-artifact@v4.4.0 with: name: pytest-${{ github.run_number }}-${{ matrix.python-version }}-${{ steps.pytest-partial.outputs.mariadb }} @@ -1068,7 +1079,7 @@ jobs: overwrite: true - name: Upload coverage artifact if: needs.info.outputs.skip_coverage != 'true' - uses: actions/upload-artifact@v4.3.6 + uses: actions/upload-artifact@v4.4.0 with: name: coverage-${{ matrix.python-version }}-${{ steps.pytest-partial.outputs.mariadb }} @@ -1125,7 +1136,7 @@ jobs: uses: actions/checkout@v4.1.7 - name: Set up Python ${{ matrix.python-version }} id: python - uses: actions/setup-python@v5.1.1 + uses: actions/setup-python@v5.2.0 with: python-version: ${{ matrix.python-version }} check-latest: true @@ -1187,7 +1198,7 @@ jobs: 2>&1 | tee pytest-${{ matrix.python-version }}-${postgresql}.txt - name: Upload pytest output if: success() || failure() && steps.pytest-partial.conclusion == 'failure' - uses: actions/upload-artifact@v4.3.6 + uses: actions/upload-artifact@v4.4.0 with: name: pytest-${{ github.run_number }}-${{ matrix.python-version }}-${{ steps.pytest-partial.outputs.postgresql }} @@ -1195,7 +1206,7 @@ jobs: overwrite: true - name: Upload coverage artifact if: needs.info.outputs.skip_coverage != 'true' - uses: actions/upload-artifact@v4.3.6 + uses: actions/upload-artifact@v4.4.0 with: name: coverage-${{ matrix.python-version }}-${{ steps.pytest-partial.outputs.postgresql }} @@ -1271,7 +1282,7 @@ jobs: uses: actions/checkout@v4.1.7 - name: Set up Python ${{ matrix.python-version }} id: python - uses: actions/setup-python@v5.1.1 + uses: actions/setup-python@v5.2.0 with: python-version: ${{ matrix.python-version }} check-latest: true @@ -1329,14 +1340,14 @@ jobs: 2>&1 | tee pytest-${{ matrix.python-version }}-${{ matrix.group }}.txt - name: Upload pytest output if: success() || failure() && steps.pytest-partial.conclusion == 'failure' - uses: actions/upload-artifact@v4.3.6 + uses: actions/upload-artifact@v4.4.0 with: name: pytest-${{ github.run_number }}-${{ matrix.python-version }}-${{ matrix.group }} path: pytest-*.txt overwrite: true - name: Upload coverage artifact if: needs.info.outputs.skip_coverage != 'true' - uses: actions/upload-artifact@v4.3.6 + uses: actions/upload-artifact@v4.4.0 with: name: coverage-${{ matrix.python-version }}-${{ matrix.group }} path: coverage.xml diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 45c2b31d772f95..33c7d6a27116a5 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -24,11 +24,11 @@ jobs: uses: actions/checkout@v4.1.7 - name: Initialize CodeQL - uses: github/codeql-action/init@v3.26.2 + uses: github/codeql-action/init@v3.26.6 with: languages: python - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v3.26.2 + uses: github/codeql-action/analyze@v3.26.6 with: category: "/language:python" diff --git a/.github/workflows/translations.yml b/.github/workflows/translations.yml index 0ab955104802b4..4b3907e6cb939e 100644 --- a/.github/workflows/translations.yml +++ b/.github/workflows/translations.yml @@ -22,7 +22,7 @@ jobs: uses: actions/checkout@v4.1.7 - name: Set up Python ${{ env.DEFAULT_PYTHON }} - uses: actions/setup-python@v5.1.1 + uses: actions/setup-python@v5.2.0 with: python-version: ${{ env.DEFAULT_PYTHON }} diff --git a/.github/workflows/wheels.yml b/.github/workflows/wheels.yml index 694208d30ac5a9..fcd71cbec32b93 100644 --- a/.github/workflows/wheels.yml +++ b/.github/workflows/wheels.yml @@ -36,7 +36,7 @@ jobs: - name: Set up Python ${{ env.DEFAULT_PYTHON }} id: python - uses: actions/setup-python@v5.1.1 + uses: actions/setup-python@v5.2.0 with: python-version: ${{ env.DEFAULT_PYTHON }} check-latest: true @@ -82,14 +82,15 @@ jobs: ) > .env_file - name: Upload env_file - uses: actions/upload-artifact@v4.3.6 + uses: actions/upload-artifact@v4.4.0 with: name: env_file path: ./.env_file + include-hidden-files: true overwrite: true - name: Upload requirements_diff - uses: actions/upload-artifact@v4.3.6 + uses: actions/upload-artifact@v4.4.0 with: name: requirements_diff path: ./requirements_diff.txt @@ -101,7 +102,7 @@ jobs: python -m script.gen_requirements_all ci - name: Upload requirements_all_wheels - uses: actions/upload-artifact@v4.3.6 + uses: actions/upload-artifact@v4.4.0 with: name: requirements_all_wheels path: ./requirements_all_wheels_*.txt @@ -139,7 +140,7 @@ jobs: wheels-key: ${{ secrets.WHEELS_KEY }} env-file: true apk: "libffi-dev;openssl-dev;yaml-dev;nasm" - skip-binary: aiohttp + skip-binary: aiohttp;yarl constraints: "homeassistant/package_constraints.txt" requirements-diff: "requirements_diff.txt" requirements: "requirements.txt" @@ -211,7 +212,7 @@ jobs: wheels-key: ${{ secrets.WHEELS_KEY }} env-file: true apk: "bluez-dev;libffi-dev;openssl-dev;glib-dev;eudev-dev;libxml2-dev;libxslt-dev;libpng-dev;libjpeg-turbo-dev;tiff-dev;cups-dev;gmp-dev;mpfr-dev;mpc1-dev;ffmpeg-dev;gammu-dev;yaml-dev;openblas-dev;fftw-dev;lapack-dev;gfortran;blas-dev;eigen-dev;freetype-dev;glew-dev;harfbuzz-dev;hdf5-dev;libdc1394-dev;libtbb-dev;mesa-dev;openexr-dev;openjpeg-dev;uchardet-dev" - skip-binary: aiohttp;charset-normalizer;grpcio;SQLAlchemy;protobuf;pydantic;pymicro-vad + skip-binary: aiohttp;charset-normalizer;grpcio;SQLAlchemy;protobuf;pydantic;pymicro-vad;yarl constraints: "homeassistant/package_constraints.txt" requirements-diff: "requirements_diff.txt" requirements: "requirements_old-cython.txt" @@ -226,7 +227,7 @@ jobs: wheels-key: ${{ secrets.WHEELS_KEY }} env-file: true apk: "bluez-dev;libffi-dev;openssl-dev;glib-dev;eudev-dev;libxml2-dev;libxslt-dev;libpng-dev;libjpeg-turbo-dev;tiff-dev;cups-dev;gmp-dev;mpfr-dev;mpc1-dev;ffmpeg-dev;gammu-dev;yaml-dev;openblas-dev;fftw-dev;lapack-dev;gfortran;blas-dev;eigen-dev;freetype-dev;glew-dev;harfbuzz-dev;hdf5-dev;libdc1394-dev;libtbb-dev;mesa-dev;openexr-dev;openjpeg-dev;uchardet-dev;nasm" - skip-binary: aiohttp;charset-normalizer;grpcio;SQLAlchemy;protobuf;pydantic;pymicro-vad + skip-binary: aiohttp;charset-normalizer;grpcio;SQLAlchemy;protobuf;pydantic;pymicro-vad;yarl constraints: "homeassistant/package_constraints.txt" requirements-diff: "requirements_diff.txt" requirements: "requirements_all.txtaa" @@ -240,7 +241,7 @@ jobs: wheels-key: ${{ secrets.WHEELS_KEY }} env-file: true apk: "bluez-dev;libffi-dev;openssl-dev;glib-dev;eudev-dev;libxml2-dev;libxslt-dev;libpng-dev;libjpeg-turbo-dev;tiff-dev;cups-dev;gmp-dev;mpfr-dev;mpc1-dev;ffmpeg-dev;gammu-dev;yaml-dev;openblas-dev;fftw-dev;lapack-dev;gfortran;blas-dev;eigen-dev;freetype-dev;glew-dev;harfbuzz-dev;hdf5-dev;libdc1394-dev;libtbb-dev;mesa-dev;openexr-dev;openjpeg-dev;uchardet-dev;nasm" - skip-binary: aiohttp;charset-normalizer;grpcio;SQLAlchemy;protobuf;pydantic;pymicro-vad + skip-binary: aiohttp;charset-normalizer;grpcio;SQLAlchemy;protobuf;pydantic;pymicro-vad;yarl constraints: "homeassistant/package_constraints.txt" requirements-diff: "requirements_diff.txt" requirements: "requirements_all.txtab" @@ -254,7 +255,7 @@ jobs: wheels-key: ${{ secrets.WHEELS_KEY }} env-file: true apk: "bluez-dev;libffi-dev;openssl-dev;glib-dev;eudev-dev;libxml2-dev;libxslt-dev;libpng-dev;libjpeg-turbo-dev;tiff-dev;cups-dev;gmp-dev;mpfr-dev;mpc1-dev;ffmpeg-dev;gammu-dev;yaml-dev;openblas-dev;fftw-dev;lapack-dev;gfortran;blas-dev;eigen-dev;freetype-dev;glew-dev;harfbuzz-dev;hdf5-dev;libdc1394-dev;libtbb-dev;mesa-dev;openexr-dev;openjpeg-dev;uchardet-dev;nasm" - skip-binary: aiohttp;charset-normalizer;grpcio;SQLAlchemy;protobuf;pydantic;pymicro-vad + skip-binary: aiohttp;charset-normalizer;grpcio;SQLAlchemy;protobuf;pydantic;pymicro-vad;yarl constraints: "homeassistant/package_constraints.txt" requirements-diff: "requirements_diff.txt" requirements: "requirements_all.txtac" diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f29fd92a880ccd..ab5e59139cfd14 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.6.1 + rev: v0.6.2 hooks: - id: ruff args: diff --git a/.strict-typing b/.strict-typing index 07aed7b4ca12c7..e93f1589cc8b97 100644 --- a/.strict-typing +++ b/.strict-typing @@ -110,6 +110,7 @@ homeassistant.components.bitcoin.* homeassistant.components.blockchain.* homeassistant.components.blue_current.* homeassistant.components.blueprint.* +homeassistant.components.bluesound.* homeassistant.components.bluetooth.* homeassistant.components.bluetooth_adapters.* homeassistant.components.bluetooth_tracker.* @@ -139,6 +140,7 @@ homeassistant.components.cpuspeed.* homeassistant.components.crownstone.* homeassistant.components.date.* homeassistant.components.datetime.* +homeassistant.components.deako.* homeassistant.components.deconz.* homeassistant.components.default_config.* homeassistant.components.demo.* @@ -208,6 +210,8 @@ homeassistant.components.glances.* homeassistant.components.goalzero.* homeassistant.components.google.* homeassistant.components.google_assistant_sdk.* +homeassistant.components.google_cloud.* +homeassistant.components.google_photos.* homeassistant.components.google_sheets.* homeassistant.components.gpsd.* homeassistant.components.greeneye_monitor.* @@ -278,6 +282,7 @@ homeassistant.components.lawn_mower.* homeassistant.components.lcn.* homeassistant.components.ld2410_ble.* homeassistant.components.led_ble.* +homeassistant.components.lektrico.* homeassistant.components.lidarr.* homeassistant.components.lifx.* homeassistant.components.light.* @@ -294,7 +299,6 @@ homeassistant.components.london_underground.* homeassistant.components.lookin.* homeassistant.components.luftdaten.* homeassistant.components.madvr.* -homeassistant.components.mailbox.* homeassistant.components.manual.* homeassistant.components.map.* homeassistant.components.mastodon.* @@ -337,6 +341,7 @@ homeassistant.components.nut.* homeassistant.components.onboarding.* homeassistant.components.oncue.* homeassistant.components.onewire.* +homeassistant.components.onkyo.* homeassistant.components.open_meteo.* homeassistant.components.openexchangerates.* homeassistant.components.opensky.* @@ -408,9 +413,11 @@ homeassistant.components.slack.* homeassistant.components.sleepiq.* homeassistant.components.smhi.* homeassistant.components.snooz.* +homeassistant.components.solarlog.* homeassistant.components.sonarr.* homeassistant.components.speedtestdotnet.* homeassistant.components.sql.* +homeassistant.components.squeezebox.* homeassistant.components.ssdp.* homeassistant.components.starlink.* homeassistant.components.statistics.* diff --git a/CODEOWNERS b/CODEOWNERS index 1618b18a8be53a..42d96ceb9415dd 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -228,8 +228,8 @@ build.json @home-assistant/supervisor /homeassistant/components/bsblan/ @liudger /tests/components/bsblan/ @liudger /homeassistant/components/bt_smarthub/ @typhoon2099 -/homeassistant/components/bthome/ @Ernst79 -/tests/components/bthome/ @Ernst79 +/homeassistant/components/bthome/ @Ernst79 @thecode +/tests/components/bthome/ @Ernst79 @thecode /homeassistant/components/buienradar/ @mjj4791 @ties @Robbie1221 /tests/components/buienradar/ @mjj4791 @ties @Robbie1221 /homeassistant/components/button/ @home-assistant/core @@ -294,6 +294,8 @@ build.json @home-assistant/supervisor /tests/components/date/ @home-assistant/core /homeassistant/components/datetime/ @home-assistant/core /tests/components/datetime/ @home-assistant/core +/homeassistant/components/deako/ @sebirdman @balake @deakolights +/tests/components/deako/ @sebirdman @balake @deakolights /homeassistant/components/debugpy/ @frenck /tests/components/debugpy/ @frenck /homeassistant/components/deconz/ @Kane610 @@ -547,11 +549,14 @@ build.json @home-assistant/supervisor /tests/components/google_assistant/ @home-assistant/cloud /homeassistant/components/google_assistant_sdk/ @tronikos /tests/components/google_assistant_sdk/ @tronikos -/homeassistant/components/google_cloud/ @lufton +/homeassistant/components/google_cloud/ @lufton @tronikos +/tests/components/google_cloud/ @lufton @tronikos /homeassistant/components/google_generative_ai_conversation/ @tronikos /tests/components/google_generative_ai_conversation/ @tronikos /homeassistant/components/google_mail/ @tkdrob /tests/components/google_mail/ @tkdrob +/homeassistant/components/google_photos/ @allenporter +/tests/components/google_photos/ @allenporter /homeassistant/components/google_sheets/ @tkdrob /tests/components/google_sheets/ @tkdrob /homeassistant/components/google_tasks/ @allenporter @@ -629,6 +634,8 @@ build.json @home-assistant/supervisor /tests/components/homewizard/ @DCSBL /homeassistant/components/honeywell/ @rdfurman @mkmer /tests/components/honeywell/ @rdfurman @mkmer +/homeassistant/components/html5/ @alexyao2015 +/tests/components/html5/ @alexyao2015 /homeassistant/components/http/ @home-assistant/core /tests/components/http/ @home-assistant/core /homeassistant/components/huawei_lte/ @scop @fphammerle @@ -707,8 +714,8 @@ build.json @home-assistant/supervisor /tests/components/ios/ @robbiet480 /homeassistant/components/iotawatt/ @gtdiehl @jyavenard /tests/components/iotawatt/ @gtdiehl @jyavenard -/homeassistant/components/iotty/ @pburgio -/tests/components/iotty/ @pburgio +/homeassistant/components/iotty/ @pburgio @shapournemati-iotty +/tests/components/iotty/ @pburgio @shapournemati-iotty /homeassistant/components/iperf3/ @rohankapoorcom /homeassistant/components/ipma/ @dgomes /tests/components/ipma/ @dgomes @@ -721,6 +728,8 @@ build.json @home-assistant/supervisor /tests/components/iron_os/ @tr4nt0r /homeassistant/components/isal/ @bdraco /tests/components/isal/ @bdraco +/homeassistant/components/iskra/ @iskramis +/tests/components/iskra/ @iskramis /homeassistant/components/islamic_prayer_times/ @engrbm87 @cpfair /tests/components/islamic_prayer_times/ @engrbm87 @cpfair /homeassistant/components/israel_rail/ @shaiu @@ -797,8 +806,12 @@ build.json @home-assistant/supervisor /tests/components/leaone/ @bdraco /homeassistant/components/led_ble/ @bdraco /tests/components/led_ble/ @bdraco +/homeassistant/components/lektrico/ @lektrico +/tests/components/lektrico/ @lektrico /homeassistant/components/lg_netcast/ @Drafteed @splinter98 /tests/components/lg_netcast/ @Drafteed @splinter98 +/homeassistant/components/lg_thinq/ @LG-ThinQ-Integration +/tests/components/lg_thinq/ @LG-ThinQ-Integration /homeassistant/components/lidarr/ @tkdrob /tests/components/lidarr/ @tkdrob /homeassistant/components/lifx/ @Djelibeybi @@ -1493,6 +1506,8 @@ build.json @home-assistant/supervisor /tests/components/tomorrowio/ @raman325 @lymanepp /homeassistant/components/totalconnect/ @austinmroczek /tests/components/totalconnect/ @austinmroczek +/homeassistant/components/touchline_sl/ @jnsgruk +/tests/components/touchline_sl/ @jnsgruk /homeassistant/components/tplink/ @rytilahti @bdraco @sdb9696 /tests/components/tplink/ @rytilahti @bdraco @sdb9696 /homeassistant/components/tplink_omada/ @MarkGodwin @@ -1658,6 +1673,8 @@ build.json @home-assistant/supervisor /tests/components/xiaomi_miio/ @rytilahti @syssi @starkillerOG /homeassistant/components/xiaomi_tv/ @simse /homeassistant/components/xmpp/ @fabaff @flowolf +/homeassistant/components/yale/ @bdraco +/tests/components/yale/ @bdraco /homeassistant/components/yale_smart_alarm/ @gjohansson-ST /tests/components/yale_smart_alarm/ @gjohansson-ST /homeassistant/components/yalexs_ble/ @bdraco diff --git a/Dockerfile.dev b/Dockerfile.dev index d7a2f2b7bf99d6..d05c6df425cf5b 100644 --- a/Dockerfile.dev +++ b/Dockerfile.dev @@ -42,7 +42,8 @@ WORKDIR /usr/src # Setup hass-release RUN git clone --depth 1 https://github.com/home-assistant/hass-release \ - && uv pip install --system -e hass-release/ + && uv pip install --system -e hass-release/ \ + && chown -R vscode /usr/src/hass-release/data USER vscode ENV VIRTUAL_ENV="/home/vscode/.local/ha-venv" diff --git a/homeassistant/brands/google.json b/homeassistant/brands/google.json index 7c6ebc044e9cf8..460c92076d8315 100644 --- a/homeassistant/brands/google.json +++ b/homeassistant/brands/google.json @@ -9,6 +9,7 @@ "google_generative_ai_conversation", "google_mail", "google_maps", + "google_photos", "google_pubsub", "google_sheets", "google_tasks", diff --git a/homeassistant/brands/lg.json b/homeassistant/brands/lg.json index 350db80b5f3df5..6b706685f1f283 100644 --- a/homeassistant/brands/lg.json +++ b/homeassistant/brands/lg.json @@ -1,5 +1,5 @@ { "domain": "lg", "name": "LG", - "integrations": ["lg_netcast", "lg_soundbar", "webostv"] + "integrations": ["lg_netcast", "lg_thinq", "lg_soundbar", "webostv"] } diff --git a/homeassistant/brands/roth.json b/homeassistant/brands/roth.json new file mode 100644 index 00000000000000..21542b5b64151b --- /dev/null +++ b/homeassistant/brands/roth.json @@ -0,0 +1,5 @@ +{ + "domain": "roth", + "name": "Roth", + "integrations": ["touchline", "touchline_sl"] +} diff --git a/homeassistant/brands/yale.json b/homeassistant/brands/yale.json index 53dc9b43569746..a0e7c6bd453ac6 100644 --- a/homeassistant/brands/yale.json +++ b/homeassistant/brands/yale.json @@ -1,5 +1,11 @@ { "domain": "yale", "name": "Yale", - "integrations": ["august", "yale_smart_alarm", "yalexs_ble", "yale_home"] + "integrations": [ + "august", + "yale_smart_alarm", + "yalexs_ble", + "yale_home", + "yale" + ] } diff --git a/homeassistant/components/__init__.py b/homeassistant/components/__init__.py index 030e23628d6d30..d01f51c39512af 100644 --- a/homeassistant/components/__init__.py +++ b/homeassistant/components/__init__.py @@ -6,52 +6,3 @@ format ".". - Each component should publish services only under its own domain. """ - -from __future__ import annotations - -import logging - -from homeassistant.core import HomeAssistant, split_entity_id -from homeassistant.helpers.frame import report -from homeassistant.helpers.group import expand_entity_ids - -_LOGGER = logging.getLogger(__name__) - - -def is_on(hass: HomeAssistant, entity_id: str | None = None) -> bool: - """Load up the module to call the is_on method. - - If there is no entity id given we will check all. - """ - report( - ( - "uses homeassistant.components.is_on." - " This is deprecated and will stop working in Home Assistant 2024.9, it" - " should be updated to use the function of the platform directly." - ), - error_if_core=True, - ) - - if entity_id: - entity_ids = expand_entity_ids(hass, [entity_id]) - else: - entity_ids = hass.states.entity_ids() - - for ent_id in entity_ids: - domain = split_entity_id(ent_id)[0] - - try: - component = getattr(hass.components, domain) - - except ImportError: - _LOGGER.error("Failed to call %s.is_on: component not found", domain) - continue - - if not hasattr(component, "is_on"): - _LOGGER.warning("Integration %s has no is_on method", domain) - continue - - if component.is_on(ent_id): - return True - - return False diff --git a/homeassistant/components/abode/icons.json b/homeassistant/components/abode/icons.json index 00175628d9a0a4..4ce4e55cab6a92 100644 --- a/homeassistant/components/abode/icons.json +++ b/homeassistant/components/abode/icons.json @@ -7,8 +7,14 @@ } }, "services": { - "capture_image": "mdi:camera", - "change_setting": "mdi:cog", - "trigger_automation": "mdi:play" + "capture_image": { + "service": "mdi:camera" + }, + "change_setting": { + "service": "mdi:cog" + }, + "trigger_automation": { + "service": "mdi:play" + } } } diff --git a/homeassistant/components/accuweather/sensor.py b/homeassistant/components/accuweather/sensor.py index fac3a2a4ba34ab..2f6b10b296f2a4 100644 --- a/homeassistant/components/accuweather/sensor.py +++ b/homeassistant/components/accuweather/sensor.py @@ -18,6 +18,7 @@ UV_INDEX, UnitOfIrradiance, UnitOfLength, + UnitOfPressure, UnitOfSpeed, UnitOfTemperature, UnitOfTime, @@ -279,6 +280,15 @@ class AccuWeatherSensorDescription(SensorEntityDescription): value_fn=lambda data: cast(float, data[API_METRIC][ATTR_VALUE]), translation_key="realfeel_temperature_shade", ), + AccuWeatherSensorDescription( + key="RelativeHumidity", + device_class=SensorDeviceClass.HUMIDITY, + entity_registry_enabled_default=False, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=PERCENTAGE, + value_fn=lambda data: cast(int, data), + translation_key="humidity", + ), AccuWeatherSensorDescription( key="Precipitation", device_class=SensorDeviceClass.PRECIPITATION_INTENSITY, @@ -288,6 +298,16 @@ class AccuWeatherSensorDescription(SensorEntityDescription): attr_fn=lambda data: {"type": data["PrecipitationType"]}, translation_key="precipitation", ), + AccuWeatherSensorDescription( + key="Pressure", + device_class=SensorDeviceClass.PRESSURE, + entity_registry_enabled_default=False, + state_class=SensorStateClass.MEASUREMENT, + suggested_display_precision=0, + native_unit_of_measurement=UnitOfPressure.HPA, + value_fn=lambda data: cast(float, data[API_METRIC][ATTR_VALUE]), + translation_key="pressure", + ), AccuWeatherSensorDescription( key="PressureTendency", device_class=SensorDeviceClass.ENUM, @@ -295,9 +315,19 @@ class AccuWeatherSensorDescription(SensorEntityDescription): value_fn=lambda data: cast(str, data["LocalizedText"]).lower(), translation_key="pressure_tendency", ), + AccuWeatherSensorDescription( + key="Temperature", + device_class=SensorDeviceClass.TEMPERATURE, + entity_registry_enabled_default=False, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + value_fn=lambda data: cast(float, data[API_METRIC][ATTR_VALUE]), + translation_key="temperature", + ), AccuWeatherSensorDescription( key="UVIndex", state_class=SensorStateClass.MEASUREMENT, + entity_registry_enabled_default=False, native_unit_of_measurement=UV_INDEX, value_fn=lambda data: cast(int, data), attr_fn=lambda data: {ATTR_LEVEL: data["UVIndexText"]}, @@ -324,6 +354,7 @@ class AccuWeatherSensorDescription(SensorEntityDescription): AccuWeatherSensorDescription( key="Wind", device_class=SensorDeviceClass.WIND_SPEED, + entity_registry_enabled_default=False, state_class=SensorStateClass.MEASUREMENT, native_unit_of_measurement=UnitOfSpeed.KILOMETERS_PER_HOUR, value_fn=lambda data: cast(float, data[ATTR_SPEED][API_METRIC][ATTR_VALUE]), diff --git a/homeassistant/components/acmeda/manifest.json b/homeassistant/components/acmeda/manifest.json index a8b3c7c829fc62..0c35904cac699f 100644 --- a/homeassistant/components/acmeda/manifest.json +++ b/homeassistant/components/acmeda/manifest.json @@ -6,5 +6,5 @@ "documentation": "https://www.home-assistant.io/integrations/acmeda", "iot_class": "local_push", "loggers": ["aiopulse"], - "requirements": ["aiopulse==0.4.4"] + "requirements": ["aiopulse==0.4.6"] } diff --git a/homeassistant/components/adguard/icons.json b/homeassistant/components/adguard/icons.json index 9c5df8a4a450e9..18527c0ed9864a 100644 --- a/homeassistant/components/adguard/icons.json +++ b/homeassistant/components/adguard/icons.json @@ -66,10 +66,20 @@ } }, "services": { - "add_url": "mdi:link-plus", - "remove_url": "mdi:link-off", - "enable_url": "mdi:link-variant", - "disable_url": "mdi:link-variant-off", - "refresh": "mdi:refresh" + "add_url": { + "service": "mdi:link-plus" + }, + "remove_url": { + "service": "mdi:link-off" + }, + "enable_url": { + "service": "mdi:link-variant" + }, + "disable_url": { + "service": "mdi:link-variant-off" + }, + "refresh": { + "service": "mdi:refresh" + } } } diff --git a/homeassistant/components/ads/__init__.py b/homeassistant/components/ads/__init__.py index f5742718b12c6e..32d89b5b597ad4 100644 --- a/homeassistant/components/ads/__init__.py +++ b/homeassistant/components/ads/__init__.py @@ -29,18 +29,40 @@ # Supported Types ADSTYPE_BOOL = "bool" ADSTYPE_BYTE = "byte" -ADSTYPE_DINT = "dint" ADSTYPE_INT = "int" -ADSTYPE_UDINT = "udint" ADSTYPE_UINT = "uint" +ADSTYPE_SINT = "sint" +ADSTYPE_USINT = "usint" +ADSTYPE_DINT = "dint" +ADSTYPE_UDINT = "udint" +ADSTYPE_WORD = "word" +ADSTYPE_DWORD = "dword" +ADSTYPE_LREAL = "lreal" +ADSTYPE_REAL = "real" +ADSTYPE_STRING = "string" +ADSTYPE_TIME = "time" +ADSTYPE_DATE = "date" +ADSTYPE_DATE_AND_TIME = "dt" +ADSTYPE_TOD = "tod" ADS_TYPEMAP = { ADSTYPE_BOOL: pyads.PLCTYPE_BOOL, ADSTYPE_BYTE: pyads.PLCTYPE_BYTE, - ADSTYPE_DINT: pyads.PLCTYPE_DINT, ADSTYPE_INT: pyads.PLCTYPE_INT, - ADSTYPE_UDINT: pyads.PLCTYPE_UDINT, ADSTYPE_UINT: pyads.PLCTYPE_UINT, + ADSTYPE_SINT: pyads.PLCTYPE_SINT, + ADSTYPE_USINT: pyads.PLCTYPE_USINT, + ADSTYPE_DINT: pyads.PLCTYPE_DINT, + ADSTYPE_UDINT: pyads.PLCTYPE_UDINT, + ADSTYPE_WORD: pyads.PLCTYPE_WORD, + ADSTYPE_DWORD: pyads.PLCTYPE_DWORD, + ADSTYPE_REAL: pyads.PLCTYPE_REAL, + ADSTYPE_LREAL: pyads.PLCTYPE_LREAL, + ADSTYPE_STRING: pyads.PLCTYPE_STRING, + ADSTYPE_TIME: pyads.PLCTYPE_TIME, + ADSTYPE_DATE: pyads.PLCTYPE_DATE, + ADSTYPE_DATE_AND_TIME: pyads.PLCTYPE_DT, + ADSTYPE_TOD: pyads.PLCTYPE_TOD, } CONF_ADS_FACTOR = "factor" @@ -75,12 +97,23 @@ { vol.Required(CONF_ADS_TYPE): vol.In( [ + ADSTYPE_BOOL, + ADSTYPE_BYTE, ADSTYPE_INT, ADSTYPE_UINT, - ADSTYPE_BYTE, - ADSTYPE_BOOL, + ADSTYPE_SINT, + ADSTYPE_USINT, ADSTYPE_DINT, ADSTYPE_UDINT, + ADSTYPE_WORD, + ADSTYPE_DWORD, + ADSTYPE_REAL, + ADSTYPE_LREAL, + ADSTYPE_STRING, + ADSTYPE_TIME, + ADSTYPE_DATE, + ADSTYPE_DATE_AND_TIME, + ADSTYPE_TOD, ] ), vol.Required(CONF_ADS_VALUE): vol.Coerce(int), @@ -222,37 +255,53 @@ def add_device_notification(self, name, plc_datatype, callback): def _device_notification_callback(self, notification, name): """Handle device notifications.""" contents = notification.contents - hnotify = int(contents.hNotification) _LOGGER.debug("Received notification %d", hnotify) - # get dynamically sized data array + # Get dynamically sized data array data_size = contents.cbSampleSize - data = (ctypes.c_ubyte * data_size).from_address( + data_address = ( ctypes.addressof(contents) + pyads.structs.SAdsNotificationHeader.data.offset ) + data = (ctypes.c_ubyte * data_size).from_address(data_address) - try: - with self._lock: - notification_item = self._notification_items[hnotify] - except KeyError: + # Acquire notification item + with self._lock: + notification_item = self._notification_items.get(hnotify) + + if not notification_item: _LOGGER.error("Unknown device notification handle: %d", hnotify) return - # Parse data to desired datatype - if notification_item.plc_datatype == pyads.PLCTYPE_BOOL: + # Data parsing based on PLC data type + plc_datatype = notification_item.plc_datatype + unpack_formats = { + pyads.PLCTYPE_BYTE: " bool: @@ -43,27 +31,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: AirGradientConfigEntry) entry.data[CONF_HOST], session=async_get_clientsession(hass) ) - measurement_coordinator = AirGradientMeasurementCoordinator(hass, client) - config_coordinator = AirGradientConfigCoordinator(hass, client) - - await measurement_coordinator.async_config_entry_first_refresh() - await config_coordinator.async_config_entry_first_refresh() - - device_registry = dr.async_get(hass) - device_registry.async_get_or_create( - config_entry_id=entry.entry_id, - identifiers={(DOMAIN, measurement_coordinator.serial_number)}, - manufacturer="AirGradient", - model=get_model_name(measurement_coordinator.data.model), - model_id=measurement_coordinator.data.model, - serial_number=measurement_coordinator.data.serial_number, - sw_version=measurement_coordinator.data.firmware_version, - ) + coordinator = AirGradientCoordinator(hass, client) - entry.runtime_data = AirGradientData( - measurement=measurement_coordinator, - config=config_coordinator, - ) + await coordinator.async_config_entry_first_refresh() + + entry.runtime_data = coordinator await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) diff --git a/homeassistant/components/airgradient/button.py b/homeassistant/components/airgradient/button.py index b59188ebdd40c4..32a9b5adedf6ae 100644 --- a/homeassistant/components/airgradient/button.py +++ b/homeassistant/components/airgradient/button.py @@ -15,8 +15,9 @@ from homeassistant.helpers import entity_registry as er from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import DOMAIN, AirGradientConfigEntry -from .coordinator import AirGradientConfigCoordinator +from . import AirGradientConfigEntry +from .const import DOMAIN +from .coordinator import AirGradientCoordinator from .entity import AirGradientEntity @@ -47,8 +48,8 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """Set up AirGradient button entities based on a config entry.""" - model = entry.runtime_data.measurement.data.model - coordinator = entry.runtime_data.config + coordinator = entry.runtime_data + model = coordinator.data.measures.model added_entities = False @@ -57,7 +58,7 @@ def _check_entities() -> None: nonlocal added_entities if ( - coordinator.data.configuration_control is ConfigurationControl.LOCAL + coordinator.data.config.configuration_control is ConfigurationControl.LOCAL and not added_entities ): entities = [AirGradientButton(coordinator, CO2_CALIBRATION)] @@ -67,7 +68,8 @@ def _check_entities() -> None: async_add_entities(entities) added_entities = True elif ( - coordinator.data.configuration_control is not ConfigurationControl.LOCAL + coordinator.data.config.configuration_control + is not ConfigurationControl.LOCAL and added_entities ): entity_registry = er.async_get(hass) @@ -87,11 +89,10 @@ class AirGradientButton(AirGradientEntity, ButtonEntity): """Defines an AirGradient button.""" entity_description: AirGradientButtonEntityDescription - coordinator: AirGradientConfigCoordinator def __init__( self, - coordinator: AirGradientConfigCoordinator, + coordinator: AirGradientCoordinator, description: AirGradientButtonEntityDescription, ) -> None: """Initialize airgradient button.""" diff --git a/homeassistant/components/airgradient/config_flow.py b/homeassistant/components/airgradient/config_flow.py index 93cd0be61c45c6..70fa8a1755bf5d 100644 --- a/homeassistant/components/airgradient/config_flow.py +++ b/homeassistant/components/airgradient/config_flow.py @@ -92,7 +92,9 @@ async def async_step_user( except AirGradientError: errors["base"] = "cannot_connect" else: - await self.async_set_unique_id(current_measures.serial_number) + await self.async_set_unique_id( + current_measures.serial_number, raise_on_progress=False + ) self._abort_if_unique_id_configured() await self.set_configuration_source() return self.async_create_entry( diff --git a/homeassistant/components/airgradient/coordinator.py b/homeassistant/components/airgradient/coordinator.py index c3def0b1f33dc1..4e1c335019cd60 100644 --- a/homeassistant/components/airgradient/coordinator.py +++ b/homeassistant/components/airgradient/coordinator.py @@ -2,6 +2,7 @@ from __future__ import annotations +from dataclasses import dataclass from datetime import timedelta from typing import TYPE_CHECKING @@ -16,7 +17,15 @@ from . import AirGradientConfigEntry -class AirGradientCoordinator[_DataT](DataUpdateCoordinator[_DataT]): +@dataclass +class AirGradientData: + """Class for AirGradient data.""" + + measures: Measures + config: Config + + +class AirGradientCoordinator(DataUpdateCoordinator[AirGradientData]): """Class to manage fetching AirGradient data.""" config_entry: AirGradientConfigEntry @@ -33,25 +42,11 @@ def __init__(self, hass: HomeAssistant, client: AirGradientClient) -> None: assert self.config_entry.unique_id self.serial_number = self.config_entry.unique_id - async def _async_update_data(self) -> _DataT: + async def _async_update_data(self) -> AirGradientData: try: - return await self._update_data() + measures = await self.client.get_current_measures() + config = await self.client.get_config() except AirGradientError as error: raise UpdateFailed(error) from error - - async def _update_data(self) -> _DataT: - raise NotImplementedError - - -class AirGradientMeasurementCoordinator(AirGradientCoordinator[Measures]): - """Class to manage fetching AirGradient data.""" - - async def _update_data(self) -> Measures: - return await self.client.get_current_measures() - - -class AirGradientConfigCoordinator(AirGradientCoordinator[Config]): - """Class to manage fetching AirGradient data.""" - - async def _update_data(self) -> Config: - return await self.client.get_config() + else: + return AirGradientData(measures, config) diff --git a/homeassistant/components/airgradient/entity.py b/homeassistant/components/airgradient/entity.py index 4de07904bbae61..588a799610b8e4 100644 --- a/homeassistant/components/airgradient/entity.py +++ b/homeassistant/components/airgradient/entity.py @@ -1,5 +1,7 @@ """Base class for AirGradient entities.""" +from airgradient import get_model_name + from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.update_coordinator import CoordinatorEntity @@ -15,6 +17,12 @@ class AirGradientEntity(CoordinatorEntity[AirGradientCoordinator]): def __init__(self, coordinator: AirGradientCoordinator) -> None: """Initialize airgradient entity.""" super().__init__(coordinator) + measures = coordinator.data.measures self._attr_device_info = DeviceInfo( identifiers={(DOMAIN, coordinator.serial_number)}, + manufacturer="AirGradient", + model=get_model_name(measures.model), + model_id=measures.model, + serial_number=coordinator.serial_number, + sw_version=measures.firmware_version, ) diff --git a/homeassistant/components/airgradient/number.py b/homeassistant/components/airgradient/number.py index 139357f375312d..7fd282ddd8b501 100644 --- a/homeassistant/components/airgradient/number.py +++ b/homeassistant/components/airgradient/number.py @@ -18,7 +18,7 @@ from . import AirGradientConfigEntry from .const import DOMAIN -from .coordinator import AirGradientConfigCoordinator +from .coordinator import AirGradientCoordinator from .entity import AirGradientEntity @@ -62,8 +62,8 @@ async def async_setup_entry( ) -> None: """Set up AirGradient number entities based on a config entry.""" - model = entry.runtime_data.measurement.data.model - coordinator = entry.runtime_data.config + coordinator = entry.runtime_data + model = coordinator.data.measures.model added_entities = False @@ -72,7 +72,7 @@ def _async_check_entities() -> None: nonlocal added_entities if ( - coordinator.data.configuration_control is ConfigurationControl.LOCAL + coordinator.data.config.configuration_control is ConfigurationControl.LOCAL and not added_entities ): entities = [] @@ -84,7 +84,8 @@ def _async_check_entities() -> None: async_add_entities(entities) added_entities = True elif ( - coordinator.data.configuration_control is not ConfigurationControl.LOCAL + coordinator.data.config.configuration_control + is not ConfigurationControl.LOCAL and added_entities ): entity_registry = er.async_get(hass) @@ -104,11 +105,10 @@ class AirGradientNumber(AirGradientEntity, NumberEntity): """Defines an AirGradient number entity.""" entity_description: AirGradientNumberEntityDescription - coordinator: AirGradientConfigCoordinator def __init__( self, - coordinator: AirGradientConfigCoordinator, + coordinator: AirGradientCoordinator, description: AirGradientNumberEntityDescription, ) -> None: """Initialize AirGradient number.""" @@ -119,7 +119,7 @@ def __init__( @property def native_value(self) -> int | None: """Return the state of the number.""" - return self.entity_description.value_fn(self.coordinator.data) + return self.entity_description.value_fn(self.coordinator.data.config) async def async_set_native_value(self, value: float) -> None: """Set the selected value.""" diff --git a/homeassistant/components/airgradient/select.py b/homeassistant/components/airgradient/select.py index 532f7167dfff33..af56802d8426bc 100644 --- a/homeassistant/components/airgradient/select.py +++ b/homeassistant/components/airgradient/select.py @@ -18,7 +18,7 @@ from . import AirGradientConfigEntry from .const import DOMAIN, PM_STANDARD, PM_STANDARD_REVERSE -from .coordinator import AirGradientConfigCoordinator +from .coordinator import AirGradientCoordinator from .entity import AirGradientEntity @@ -144,13 +144,11 @@ async def async_setup_entry( ) -> None: """Set up AirGradient select entities based on a config entry.""" - coordinator = entry.runtime_data.config - measurement_coordinator = entry.runtime_data.measurement + coordinator = entry.runtime_data + model = coordinator.data.measures.model async_add_entities([AirGradientSelect(coordinator, CONFIG_CONTROL_ENTITY)]) - model = measurement_coordinator.data.model - added_entities = False @callback @@ -158,7 +156,7 @@ def _async_check_entities() -> None: nonlocal added_entities if ( - coordinator.data.configuration_control is ConfigurationControl.LOCAL + coordinator.data.config.configuration_control is ConfigurationControl.LOCAL and not added_entities ): entities: list[AirGradientSelect] = [ @@ -179,7 +177,8 @@ def _async_check_entities() -> None: async_add_entities(entities) added_entities = True elif ( - coordinator.data.configuration_control is not ConfigurationControl.LOCAL + coordinator.data.config.configuration_control + is not ConfigurationControl.LOCAL and added_entities ): entity_registry = er.async_get(hass) @@ -201,11 +200,10 @@ class AirGradientSelect(AirGradientEntity, SelectEntity): """Defines an AirGradient select entity.""" entity_description: AirGradientSelectEntityDescription - coordinator: AirGradientConfigCoordinator def __init__( self, - coordinator: AirGradientConfigCoordinator, + coordinator: AirGradientCoordinator, description: AirGradientSelectEntityDescription, ) -> None: """Initialize AirGradient select.""" @@ -216,7 +214,7 @@ def __init__( @property def current_option(self) -> str | None: """Return the state of the select.""" - return self.entity_description.value_fn(self.coordinator.data) + return self.entity_description.value_fn(self.coordinator.data.config) async def async_select_option(self, option: str) -> None: """Change the selected option.""" diff --git a/homeassistant/components/airgradient/sensor.py b/homeassistant/components/airgradient/sensor.py index f431c49ed2acb4..497d4cc0488255 100644 --- a/homeassistant/components/airgradient/sensor.py +++ b/homeassistant/components/airgradient/sensor.py @@ -32,7 +32,7 @@ from . import AirGradientConfigEntry from .const import PM_STANDARD, PM_STANDARD_REVERSE -from .coordinator import AirGradientConfigCoordinator, AirGradientMeasurementCoordinator +from .coordinator import AirGradientCoordinator from .entity import AirGradientEntity @@ -218,7 +218,7 @@ async def async_setup_entry( ) -> None: """Set up AirGradient sensor entities based on a config entry.""" - coordinator = entry.runtime_data.measurement + coordinator = entry.runtime_data listener: Callable[[], None] | None = None not_setup: set[AirGradientMeasurementSensorEntityDescription] = set( MEASUREMENT_SENSOR_TYPES @@ -232,7 +232,7 @@ def add_entities() -> None: not_setup = set() sensors = [] for description in sensor_descriptions: - if description.value_fn(coordinator.data) is None: + if description.value_fn(coordinator.data.measures) is None: not_setup.add(description) else: sensors.append(AirGradientMeasurementSensor(coordinator, description)) @@ -248,64 +248,65 @@ def add_entities() -> None: add_entities() entities = [ - AirGradientConfigSensor(entry.runtime_data.config, description) + AirGradientConfigSensor(coordinator, description) for description in CONFIG_SENSOR_TYPES ] - if "L" in coordinator.data.model: + if "L" in coordinator.data.measures.model: entities.extend( - AirGradientConfigSensor(entry.runtime_data.config, description) + AirGradientConfigSensor(coordinator, description) for description in CONFIG_LED_BAR_SENSOR_TYPES ) - if "I" in coordinator.data.model: + if "I" in coordinator.data.measures.model: entities.extend( - AirGradientConfigSensor(entry.runtime_data.config, description) + AirGradientConfigSensor(coordinator, description) for description in CONFIG_DISPLAY_SENSOR_TYPES ) async_add_entities(entities) -class AirGradientMeasurementSensor(AirGradientEntity, SensorEntity): +class AirGradientSensor(AirGradientEntity, SensorEntity): """Defines an AirGradient sensor.""" - entity_description: AirGradientMeasurementSensorEntityDescription - coordinator: AirGradientMeasurementCoordinator - def __init__( self, - coordinator: AirGradientMeasurementCoordinator, - description: AirGradientMeasurementSensorEntityDescription, + coordinator: AirGradientCoordinator, + description: SensorEntityDescription, ) -> None: """Initialize airgradient sensor.""" super().__init__(coordinator) self.entity_description = description self._attr_unique_id = f"{coordinator.serial_number}-{description.key}" + +class AirGradientMeasurementSensor(AirGradientSensor): + """Defines an AirGradient sensor.""" + + entity_description: AirGradientMeasurementSensorEntityDescription + @property def native_value(self) -> StateType: """Return the state of the sensor.""" - return self.entity_description.value_fn(self.coordinator.data) + return self.entity_description.value_fn(self.coordinator.data.measures) -class AirGradientConfigSensor(AirGradientEntity, SensorEntity): +class AirGradientConfigSensor(AirGradientSensor): """Defines an AirGradient sensor.""" entity_description: AirGradientConfigSensorEntityDescription - coordinator: AirGradientConfigCoordinator def __init__( self, - coordinator: AirGradientConfigCoordinator, + coordinator: AirGradientCoordinator, description: AirGradientConfigSensorEntityDescription, ) -> None: """Initialize airgradient sensor.""" - super().__init__(coordinator) - self.entity_description = description - self._attr_unique_id = f"{coordinator.serial_number}-{description.key}" + super().__init__(coordinator, description) self._attr_entity_registry_enabled_default = ( - coordinator.data.configuration_control is not ConfigurationControl.LOCAL + coordinator.data.config.configuration_control + is not ConfigurationControl.LOCAL ) @property def native_value(self) -> StateType: """Return the state of the sensor.""" - return self.entity_description.value_fn(self.coordinator.data) + return self.entity_description.value_fn(self.coordinator.data.config) diff --git a/homeassistant/components/airgradient/switch.py b/homeassistant/components/airgradient/switch.py index 60c3f83ae5e46d..329f704e755e96 100644 --- a/homeassistant/components/airgradient/switch.py +++ b/homeassistant/components/airgradient/switch.py @@ -19,7 +19,7 @@ from . import AirGradientConfigEntry from .const import DOMAIN -from .coordinator import AirGradientConfigCoordinator +from .coordinator import AirGradientCoordinator from .entity import AirGradientEntity @@ -46,7 +46,7 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """Set up AirGradient switch entities based on a config entry.""" - coordinator = entry.runtime_data.config + coordinator = entry.runtime_data added_entities = False @@ -55,7 +55,7 @@ def _async_check_entities() -> None: nonlocal added_entities if ( - coordinator.data.configuration_control is ConfigurationControl.LOCAL + coordinator.data.config.configuration_control is ConfigurationControl.LOCAL and not added_entities ): async_add_entities( @@ -63,7 +63,8 @@ def _async_check_entities() -> None: ) added_entities = True elif ( - coordinator.data.configuration_control is not ConfigurationControl.LOCAL + coordinator.data.config.configuration_control + is not ConfigurationControl.LOCAL and added_entities ): entity_registry = er.async_get(hass) @@ -82,11 +83,10 @@ class AirGradientSwitch(AirGradientEntity, SwitchEntity): """Defines an AirGradient switch entity.""" entity_description: AirGradientSwitchEntityDescription - coordinator: AirGradientConfigCoordinator def __init__( self, - coordinator: AirGradientConfigCoordinator, + coordinator: AirGradientCoordinator, description: AirGradientSwitchEntityDescription, ) -> None: """Initialize AirGradient switch.""" @@ -97,7 +97,7 @@ def __init__( @property def is_on(self) -> bool: """Return the state of the switch.""" - return self.entity_description.value_fn(self.coordinator.data) + return self.entity_description.value_fn(self.coordinator.data.config) async def async_turn_on(self, **kwargs: Any) -> None: """Turn the switch on.""" diff --git a/homeassistant/components/airgradient/update.py b/homeassistant/components/airgradient/update.py index 95e64930ea611c..eb6708afb67abb 100644 --- a/homeassistant/components/airgradient/update.py +++ b/homeassistant/components/airgradient/update.py @@ -7,7 +7,7 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import AirGradientConfigEntry, AirGradientMeasurementCoordinator +from . import AirGradientConfigEntry, AirGradientCoordinator from .entity import AirGradientEntity SCAN_INTERVAL = timedelta(hours=1) @@ -20,18 +20,17 @@ async def async_setup_entry( ) -> None: """Set up Airgradient update platform.""" - data = config_entry.runtime_data + coordinator = config_entry.runtime_data - async_add_entities([AirGradientUpdate(data.measurement)], True) + async_add_entities([AirGradientUpdate(coordinator)], True) class AirGradientUpdate(AirGradientEntity, UpdateEntity): """Representation of Airgradient Update.""" _attr_device_class = UpdateDeviceClass.FIRMWARE - coordinator: AirGradientMeasurementCoordinator - def __init__(self, coordinator: AirGradientMeasurementCoordinator) -> None: + def __init__(self, coordinator: AirGradientCoordinator) -> None: """Initialize the entity.""" super().__init__(coordinator) self._attr_unique_id = f"{coordinator.serial_number}-update" @@ -44,7 +43,7 @@ def should_poll(self) -> bool: @property def installed_version(self) -> str: """Return the installed version of the entity.""" - return self.coordinator.data.firmware_version + return self.coordinator.data.measures.firmware_version async def async_update(self) -> None: """Update the entity.""" diff --git a/homeassistant/components/airthings_ble/manifest.json b/homeassistant/components/airthings_ble/manifest.json index b86bc314819369..6c00fe79e7bd3a 100644 --- a/homeassistant/components/airthings_ble/manifest.json +++ b/homeassistant/components/airthings_ble/manifest.json @@ -24,5 +24,5 @@ "dependencies": ["bluetooth_adapters"], "documentation": "https://www.home-assistant.io/integrations/airthings_ble", "iot_class": "local_polling", - "requirements": ["airthings-ble==0.9.0"] + "requirements": ["airthings-ble==0.9.1"] } diff --git a/homeassistant/components/airtouch4/config_flow.py b/homeassistant/components/airtouch4/config_flow.py index 12e01ffde292aa..02bb5cc3ad0ea3 100644 --- a/homeassistant/components/airtouch4/config_flow.py +++ b/homeassistant/components/airtouch4/config_flow.py @@ -1,9 +1,11 @@ """Config flow for AirTouch4.""" +from typing import Any + from airtouch4pyapi import AirTouch, AirTouchStatus import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_HOST from .const import DOMAIN @@ -16,7 +18,9 @@ class AirtouchConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle a flow initialized by the user.""" if user_input is None: return self.async_show_form(step_id="user", data_schema=DATA_SCHEMA) diff --git a/homeassistant/components/airvisual_pro/config_flow.py b/homeassistant/components/airvisual_pro/config_flow.py index ebdbc807b18cbd..db83411b4a45f5 100644 --- a/homeassistant/components/airvisual_pro/config_flow.py +++ b/homeassistant/components/airvisual_pro/config_flow.py @@ -80,11 +80,9 @@ def __init__(self) -> None: """Initialize.""" self._reauth_entry: ConfigEntry | None = None - async def async_step_import( - self, import_config: dict[str, Any] - ) -> ConfigFlowResult: - """Import a config entry from configuration.yaml.""" - return await self.async_step_user(import_config) + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: + """Import a config entry from `airvisual` integration (see #83882).""" + return await self.async_step_user(import_data) async def async_step_reauth( self, entry_data: Mapping[str, Any] diff --git a/homeassistant/components/airzone/select.py b/homeassistant/components/airzone/select.py index 493150e5c6acc1..2bc11bc422880e 100644 --- a/homeassistant/components/airzone/select.py +++ b/homeassistant/components/airzone/select.py @@ -2,16 +2,21 @@ from __future__ import annotations +from collections.abc import Callable from dataclasses import dataclass from typing import Any, Final -from aioairzone.common import GrilleAngle, SleepTimeout +from aioairzone.common import GrilleAngle, OperationMode, SleepTimeout from aioairzone.const import ( API_COLD_ANGLE, API_HEAT_ANGLE, + API_MODE, API_SLEEP, AZD_COLD_ANGLE, AZD_HEAT_ANGLE, + AZD_MASTER, + AZD_MODE, + AZD_MODES, AZD_SLEEP, AZD_ZONES, ) @@ -33,6 +38,9 @@ class AirzoneSelectDescription(SelectEntityDescription): api_param: str options_dict: dict[str, int] + options_fn: Callable[[dict[str, Any], dict[str, int]], list[str]] = ( + lambda zone_data, value: list(value) + ) GRILLE_ANGLE_DICT: Final[dict[str, int]] = { @@ -42,6 +50,15 @@ class AirzoneSelectDescription(SelectEntityDescription): "40deg": GrilleAngle.DEG_40, } +MODE_DICT: Final[dict[str, int]] = { + "cool": OperationMode.COOLING, + "dry": OperationMode.DRY, + "fan": OperationMode.FAN, + "heat": OperationMode.HEATING, + "heat_cool": OperationMode.AUTO, + "stop": OperationMode.STOP, +} + SLEEP_DICT: Final[dict[str, int]] = { "off": SleepTimeout.SLEEP_OFF, "30m": SleepTimeout.SLEEP_30, @@ -50,6 +67,26 @@ class AirzoneSelectDescription(SelectEntityDescription): } +def main_zone_options( + zone_data: dict[str, Any], + options: dict[str, int], +) -> list[str]: + """Filter available modes.""" + modes = zone_data.get(AZD_MODES, []) + return [k for k, v in options.items() if v in modes] + + +MAIN_ZONE_SELECT_TYPES: Final[tuple[AirzoneSelectDescription, ...]] = ( + AirzoneSelectDescription( + api_param=API_MODE, + key=AZD_MODE, + options_dict=MODE_DICT, + options_fn=main_zone_options, + translation_key="modes", + ), +) + + ZONE_SELECT_TYPES: Final[tuple[AirzoneSelectDescription, ...]] = ( AirzoneSelectDescription( api_param=API_COLD_ANGLE, @@ -95,7 +132,20 @@ def _async_entity_listener() -> None: received_zones = set(zones_data) new_zones = received_zones - added_zones if new_zones: - async_add_entities( + entities: list[AirzoneZoneSelect] = [ + AirzoneZoneSelect( + coordinator, + description, + entry, + system_zone_id, + zones_data.get(system_zone_id), + ) + for system_zone_id in new_zones + for description in MAIN_ZONE_SELECT_TYPES + if description.key in zones_data.get(system_zone_id) + and zones_data.get(system_zone_id).get(AZD_MASTER) is True + ] + entities += [ AirzoneZoneSelect( coordinator, description, @@ -106,7 +156,8 @@ def _async_entity_listener() -> None: for system_zone_id in new_zones for description in ZONE_SELECT_TYPES if description.key in zones_data.get(system_zone_id) - ) + ] + async_add_entities(entities) added_zones.update(new_zones) entry.async_on_unload(coordinator.async_add_listener(_async_entity_listener)) @@ -153,6 +204,11 @@ def __init__( f"{self._attr_unique_id}_{system_zone_id}_{description.key}" ) self.entity_description = description + + self._attr_options = self.entity_description.options_fn( + zone_data, description.options_dict + ) + self.values_dict = {v: k for k, v in description.options_dict.items()} self._async_update_attrs() diff --git a/homeassistant/components/airzone/strings.json b/homeassistant/components/airzone/strings.json index 438304d7f417f0..cd313b821aa935 100644 --- a/homeassistant/components/airzone/strings.json +++ b/homeassistant/components/airzone/strings.json @@ -52,6 +52,17 @@ "40deg": "[%key:component::airzone::entity::select::grille_angles::state::40deg%]" } }, + "modes": { + "name": "Mode", + "state": { + "cool": "[%key:component::climate::entity_component::_::state::cool%]", + "dry": "[%key:component::climate::entity_component::_::state::dry%]", + "fan": "[%key:component::climate::entity_component::_::state::fan_only%]", + "heat": "[%key:component::climate::entity_component::_::state::heat%]", + "heat_cool": "[%key:component::climate::entity_component::_::state::heat_cool%]", + "stop": "Stop" + } + }, "sleep_times": { "name": "Sleep", "state": { diff --git a/homeassistant/components/airzone_cloud/manifest.json b/homeassistant/components/airzone_cloud/manifest.json index b691770e93495b..e0b0695655d0b7 100644 --- a/homeassistant/components/airzone_cloud/manifest.json +++ b/homeassistant/components/airzone_cloud/manifest.json @@ -6,5 +6,5 @@ "documentation": "https://www.home-assistant.io/integrations/airzone_cloud", "iot_class": "cloud_push", "loggers": ["aioairzone_cloud"], - "requirements": ["aioairzone-cloud==0.6.2"] + "requirements": ["aioairzone-cloud==0.6.5"] } diff --git a/homeassistant/components/airzone_cloud/sensor.py b/homeassistant/components/airzone_cloud/sensor.py index 9f0ee01aca212d..70d2fd079d40a7 100644 --- a/homeassistant/components/airzone_cloud/sensor.py +++ b/homeassistant/components/airzone_cloud/sensor.py @@ -12,7 +12,16 @@ AZD_AQ_PM_10, AZD_CPU_USAGE, AZD_HUMIDITY, + AZD_INDOOR_EXCHANGER_TEMP, + AZD_INDOOR_RETURN_TEMP, + AZD_INDOOR_WORK_TEMP, AZD_MEMORY_FREE, + AZD_OUTDOOR_CONDENSER_PRESS, + AZD_OUTDOOR_DISCHARGE_TEMP, + AZD_OUTDOOR_ELECTRIC_CURRENT, + AZD_OUTDOOR_EVAPORATOR_PRESS, + AZD_OUTDOOR_EXCHANGER_TEMP, + AZD_OUTDOOR_TEMP, AZD_TEMP, AZD_THERMOSTAT_BATTERY, AZD_THERMOSTAT_COVERAGE, @@ -32,7 +41,9 @@ PERCENTAGE, SIGNAL_STRENGTH_DECIBELS_MILLIWATT, EntityCategory, + UnitOfElectricCurrent, UnitOfInformation, + UnitOfPressure, UnitOfTemperature, ) from homeassistant.core import HomeAssistant, callback @@ -48,6 +59,78 @@ ) AIDOO_SENSOR_TYPES: Final[tuple[SensorEntityDescription, ...]] = ( + SensorEntityDescription( + entity_category=EntityCategory.DIAGNOSTIC, + entity_registry_enabled_default=False, + key=AZD_INDOOR_EXCHANGER_TEMP, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + state_class=SensorStateClass.MEASUREMENT, + translation_key="indoor_exchanger_temp", + ), + SensorEntityDescription( + entity_category=EntityCategory.DIAGNOSTIC, + entity_registry_enabled_default=False, + key=AZD_INDOOR_RETURN_TEMP, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + state_class=SensorStateClass.MEASUREMENT, + translation_key="indoor_return_temp", + ), + SensorEntityDescription( + entity_category=EntityCategory.DIAGNOSTIC, + entity_registry_enabled_default=False, + key=AZD_INDOOR_WORK_TEMP, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + state_class=SensorStateClass.MEASUREMENT, + translation_key="indoor_work_temp", + ), + SensorEntityDescription( + entity_category=EntityCategory.DIAGNOSTIC, + entity_registry_enabled_default=False, + key=AZD_OUTDOOR_CONDENSER_PRESS, + native_unit_of_measurement=UnitOfPressure.KPA, + state_class=SensorStateClass.MEASUREMENT, + translation_key="outdoor_condenser_press", + ), + SensorEntityDescription( + entity_category=EntityCategory.DIAGNOSTIC, + entity_registry_enabled_default=False, + key=AZD_OUTDOOR_DISCHARGE_TEMP, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + state_class=SensorStateClass.MEASUREMENT, + translation_key="outdoor_discharge_temp", + ), + SensorEntityDescription( + entity_category=EntityCategory.DIAGNOSTIC, + entity_registry_enabled_default=False, + key=AZD_OUTDOOR_ELECTRIC_CURRENT, + native_unit_of_measurement=UnitOfElectricCurrent.AMPERE, + state_class=SensorStateClass.MEASUREMENT, + translation_key="outdoor_electric_current", + ), + SensorEntityDescription( + entity_category=EntityCategory.DIAGNOSTIC, + entity_registry_enabled_default=False, + key=AZD_OUTDOOR_EVAPORATOR_PRESS, + native_unit_of_measurement=UnitOfPressure.KPA, + state_class=SensorStateClass.MEASUREMENT, + translation_key="outdoor_evaporator_press", + ), + SensorEntityDescription( + entity_category=EntityCategory.DIAGNOSTIC, + entity_registry_enabled_default=False, + key=AZD_OUTDOOR_EXCHANGER_TEMP, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + state_class=SensorStateClass.MEASUREMENT, + translation_key="outdoor_exchanger_temp", + ), + SensorEntityDescription( + entity_category=EntityCategory.DIAGNOSTIC, + entity_registry_enabled_default=False, + key=AZD_OUTDOOR_TEMP, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + state_class=SensorStateClass.MEASUREMENT, + translation_key="outdoor_temp", + ), SensorEntityDescription( device_class=SensorDeviceClass.TEMPERATURE, key=AZD_TEMP, diff --git a/homeassistant/components/airzone_cloud/strings.json b/homeassistant/components/airzone_cloud/strings.json index eb9529c7ca50c8..523c43f4955827 100644 --- a/homeassistant/components/airzone_cloud/strings.json +++ b/homeassistant/components/airzone_cloud/strings.json @@ -45,6 +45,33 @@ "free_memory": { "name": "Free memory" }, + "indoor_exchanger_temp": { + "name": "Indoor exchanger temperature" + }, + "indoor_return_temp": { + "name": "Indoor return temperature" + }, + "indoor_work_temp": { + "name": "Indoor working temperature" + }, + "outdoor_condenser_press": { + "name": "Outdoor condenser pressure" + }, + "outdoor_discharge_temp": { + "name": "Outdoor discharge temperature" + }, + "outdoor_electric_current": { + "name": "Outdoor electric current" + }, + "outdoor_evaporator_press": { + "name": "Outdoor evaporator pressure" + }, + "outdoor_exchanger_temp": { + "name": "Outdoor exchanger temperature" + }, + "outdoor_temp": { + "name": "Outdoor temperature" + }, "thermostat_coverage": { "name": "Signal percentage" } diff --git a/homeassistant/components/alarm_control_panel/icons.json b/homeassistant/components/alarm_control_panel/icons.json index 915448a996201b..0295699bae950a 100644 --- a/homeassistant/components/alarm_control_panel/icons.json +++ b/homeassistant/components/alarm_control_panel/icons.json @@ -15,12 +15,26 @@ } }, "services": { - "alarm_arm_away": "mdi:shield-lock", - "alarm_arm_home": "mdi:shield-home", - "alarm_arm_night": "mdi:shield-moon", - "alarm_arm_custom_bypass": "mdi:security", - "alarm_disarm": "mdi:shield-off", - "alarm_trigger": "mdi:bell-ring", - "alarm_arm_vacation": "mdi:shield-airplane" + "alarm_arm_away": { + "service": "mdi:shield-lock" + }, + "alarm_arm_home": { + "service": "mdi:shield-home" + }, + "alarm_arm_night": { + "service": "mdi:shield-moon" + }, + "alarm_arm_custom_bypass": { + "service": "mdi:security" + }, + "alarm_disarm": { + "service": "mdi:shield-off" + }, + "alarm_trigger": { + "service": "mdi:bell-ring" + }, + "alarm_arm_vacation": { + "service": "mdi:shield-airplane" + } } } diff --git a/homeassistant/components/alarmdecoder/icons.json b/homeassistant/components/alarmdecoder/icons.json index 80835a049c8064..ccb89749d2d840 100644 --- a/homeassistant/components/alarmdecoder/icons.json +++ b/homeassistant/components/alarmdecoder/icons.json @@ -7,7 +7,11 @@ } }, "services": { - "alarm_keypress": "mdi:dialpad", - "alarm_toggle_chime": "mdi:abc" + "alarm_keypress": { + "service": "mdi:dialpad" + }, + "alarm_toggle_chime": { + "service": "mdi:abc" + } } } diff --git a/homeassistant/components/alert/icons.json b/homeassistant/components/alert/icons.json index 7f5258706d2213..5d8613ec592a3b 100644 --- a/homeassistant/components/alert/icons.json +++ b/homeassistant/components/alert/icons.json @@ -1,7 +1,13 @@ { "services": { - "toggle": "mdi:bell-ring", - "turn_off": "mdi:bell-off", - "turn_on": "mdi:bell-alert" + "toggle": { + "service": "mdi:bell-ring" + }, + "turn_off": { + "service": "mdi:bell-off" + }, + "turn_on": { + "service": "mdi:bell-alert" + } } } diff --git a/homeassistant/components/alexa/entities.py b/homeassistant/components/alexa/entities.py index 8bba4ed2468969..ca7b389a0f1b03 100644 --- a/homeassistant/components/alexa/entities.py +++ b/homeassistant/components/alexa/entities.py @@ -661,9 +661,12 @@ def default_display_categories(self) -> list[str]: def interfaces(self) -> Generator[AlexaCapability]: """Yield the supported interfaces.""" yield AlexaPowerController(self.entity) - yield AlexaModeController( - self.entity, instance=f"{remote.DOMAIN}.{remote.ATTR_ACTIVITY}" - ) + supported = self.entity.attributes.get(ATTR_SUPPORTED_FEATURES, 0) + activities = self.entity.attributes.get(remote.ATTR_ACTIVITY_LIST) or [] + if activities and supported & remote.RemoteEntityFeature.ACTIVITY: + yield AlexaModeController( + self.entity, instance=f"{remote.DOMAIN}.{remote.ATTR_ACTIVITY}" + ) yield AlexaEndpointHealth(self.hass, self.entity) yield Alexa(self.entity) diff --git a/homeassistant/components/alexa/resources.py b/homeassistant/components/alexa/resources.py index 7782716798ae93..4541801d31f7e7 100644 --- a/homeassistant/components/alexa/resources.py +++ b/homeassistant/components/alexa/resources.py @@ -283,7 +283,7 @@ class AlexaPresetResource(AlexaCapabilityResource): """Implements Alexa PresetResources. Use presetResources with RangeController to provide a set of - friendlyNamesfor each RangeController preset. + friendlyNames for each RangeController preset. https://developer.amazon.com/docs/device-apis/resources-and-assets.html#presetresources """ diff --git a/homeassistant/components/amazon_polly/const.py b/homeassistant/components/amazon_polly/const.py index bb196544fc3d4f..40b1bba3dddddb 100644 --- a/homeassistant/components/amazon_polly/const.py +++ b/homeassistant/components/amazon_polly/const.py @@ -8,128 +8,23 @@ CONF_ACCESS_KEY_ID: Final = "aws_access_key_id" CONF_SECRET_ACCESS_KEY: Final = "aws_secret_access_key" -DEFAULT_REGION: Final = "us-east-1" -SUPPORTED_REGIONS: Final[list[str]] = [ - "us-east-1", - "us-east-2", - "us-west-1", - "us-west-2", - "ca-central-1", - "eu-west-1", - "eu-central-1", - "eu-west-2", - "eu-west-3", - "ap-southeast-1", - "ap-southeast-2", - "ap-northeast-2", - "ap-northeast-1", - "ap-south-1", - "sa-east-1", -] - CONF_ENGINE: Final = "engine" CONF_VOICE: Final = "voice" CONF_OUTPUT_FORMAT: Final = "output_format" CONF_SAMPLE_RATE: Final = "sample_rate" CONF_TEXT_TYPE: Final = "text_type" -SUPPORTED_VOICES: Final[list[str]] = [ - "Aditi", # Hindi - "Amy", # English (British) - "Aria", # English (New Zealand), Neural - "Arlet", # Catalan, Neural - "Arthur", # English, Neural - "Astrid", # Swedish - "Ayanda", # English (South African), Neural - "Bianca", # Italian - "Brian", # English (British) - "Camila", # Portuguese, Brazilian - "Carla", # Italian - "Carmen", # Romanian - "Celine", # French - "Chantal", # French Canadian - "Conchita", # Spanish (European) - "Cristiano", # Portuguese (European) - "Daniel", # German, Neural - "Dora", # Icelandic - "Elin", # Swedish, Neural - "Emma", # English - "Enrique", # Spanish (European) - "Ewa", # Polish - "Filiz", # Turkish - "Gabrielle", # French (Canadian) - "Geraint", # English Welsh - "Giorgio", # Italian - "Gwyneth", # Welsh - "Hala", # Arabic (Gulf), Neural - "Hannah", # German (Austrian), Neural - "Hans", # German - "Hiujin", # Chinese (Cantonese), Neural - "Ida", # Norwegian, Neural - "Ines", # Portuguese, European # codespell:ignore ines - "Ivy", # English - "Jacek", # Polish - "Jan", # Polish - "Joanna", # English - "Joey", # English - "Justin", # English - "Kajal", # English (Indian)/Hindi (Bilingual ), Neural - "Karl", # Icelandic - "Kendra", # English - "Kevin", # English, Neural - "Kimberly", # English - "Laura", # Dutch, Neural - "Lea", # French - "Liam", # Canadian French, Neural - "Liv", # Norwegian - "Lotte", # Dutch - "Lucia", # Spanish European - "Lupe", # Spanish US - "Mads", # Danish - "Maja", # Polish - "Marlene", # German - "Mathieu", # French - "Matthew", # English - "Maxim", # Russian - "Mia", # Spanish Mexican - "Miguel", # Spanish US - "Mizuki", # Japanese - "Naja", # Danish - "Nicole", # English Australian - "Ola", # Polish, Neural - "Olivia", # Female, Australian, Neural - "Penelope", # Spanish US - "Pedro", # Spanish US, Neural - "Raveena", # English, Indian - "Ricardo", # Portuguese (Brazilian) - "Ruben", # Dutch - "Russell", # English (Australian) - "Ruth", # English, Neural - "Salli", # English - "Seoyeon", # Korean - "Stephen", # English, Neural - "Suvi", # Finnish - "Takumi", # Japanese - "Tatyana", # Russian - "Vicki", # German - "Vitoria", # Portuguese, Brazilian - "Zeina", # Arabic - "Zhiyu", # Chinese -] - -SUPPORTED_OUTPUT_FORMATS: Final[list[str]] = ["mp3", "ogg_vorbis", "pcm"] +SUPPORTED_OUTPUT_FORMATS: Final[set[str]] = {"mp3", "ogg_vorbis", "pcm"} -SUPPORTED_ENGINES: Final[list[str]] = ["neural", "standard"] +SUPPORTED_SAMPLE_RATES: Final[set[str]] = {"8000", "16000", "22050", "24000"} -SUPPORTED_SAMPLE_RATES: Final[list[str]] = ["8000", "16000", "22050", "24000"] - -SUPPORTED_SAMPLE_RATES_MAP: Final[dict[str, list[str]]] = { - "mp3": ["8000", "16000", "22050", "24000"], - "ogg_vorbis": ["8000", "16000", "22050"], - "pcm": ["8000", "16000"], +SUPPORTED_SAMPLE_RATES_MAP: Final[dict[str, set[str]]] = { + "mp3": {"8000", "16000", "22050", "24000"}, + "ogg_vorbis": {"8000", "16000", "22050"}, + "pcm": {"8000", "16000"}, } -SUPPORTED_TEXT_TYPES: Final[list[str]] = ["text", "ssml"] +SUPPORTED_TEXT_TYPES: Final[set[str]] = {"text", "ssml"} CONTENT_TYPE_EXTENSIONS: Final[dict[str, str]] = { "audio/mpeg": "mp3", @@ -137,6 +32,8 @@ "audio/pcm": "pcm", } +DEFAULT_REGION: Final = "us-east-1" + DEFAULT_ENGINE: Final = "standard" DEFAULT_VOICE: Final = "Joanna" DEFAULT_OUTPUT_FORMAT: Final = "mp3" diff --git a/homeassistant/components/amazon_polly/tts.py b/homeassistant/components/amazon_polly/tts.py index d5cb7092fe3bc3..62852848a9ce83 100644 --- a/homeassistant/components/amazon_polly/tts.py +++ b/homeassistant/components/amazon_polly/tts.py @@ -2,6 +2,7 @@ from __future__ import annotations +from collections import defaultdict import logging from typing import Any, Final @@ -16,6 +17,11 @@ ) from homeassistant.const import ATTR_CREDENTIALS, CONF_PROFILE_NAME from homeassistant.core import HomeAssistant +from homeassistant.generated.amazon_polly import ( + SUPPORTED_ENGINES, + SUPPORTED_REGIONS, + SUPPORTED_VOICES, +) import homeassistant.helpers.config_validation as cv from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType @@ -38,13 +44,10 @@ DEFAULT_SAMPLE_RATES, DEFAULT_TEXT_TYPE, DEFAULT_VOICE, - SUPPORTED_ENGINES, SUPPORTED_OUTPUT_FORMATS, - SUPPORTED_REGIONS, SUPPORTED_SAMPLE_RATES, SUPPORTED_SAMPLE_RATES_MAP, SUPPORTED_TEXT_TYPES, - SUPPORTED_VOICES, ) _LOGGER: Final = logging.getLogger(__name__) @@ -112,6 +115,8 @@ def get_engine( all_voices: dict[str, dict[str, str]] = {} + all_engines: dict[str, set[str]] = defaultdict(set) + all_voices_req = polly_client.describe_voices() for voice in all_voices_req.get("Voices", []): @@ -122,8 +127,12 @@ def get_engine( language_code: str | None = voice.get("LanguageCode") if language_code is not None and language_code not in supported_languages: supported_languages.append(language_code) + for engine in voice.get("SupportedEngines"): + all_engines[engine].add(voice_id) - return AmazonPollyProvider(polly_client, config, supported_languages, all_voices) + return AmazonPollyProvider( + polly_client, config, supported_languages, all_voices, all_engines + ) class AmazonPollyProvider(Provider): @@ -135,13 +144,16 @@ def __init__( config: ConfigType, supported_languages: list[str], all_voices: dict[str, dict[str, str]], + all_engines: dict[str, set[str]], ) -> None: """Initialize Amazon Polly provider for TTS.""" self.client = polly_client self.config = config self.supported_langs = supported_languages self.all_voices = all_voices + self.all_engines = all_engines self.default_voice: str = self.config[CONF_VOICE] + self.default_engine: str = self.config[CONF_ENGINE] self.name = "Amazon Polly" @property @@ -157,12 +169,12 @@ def default_language(self) -> str | None: @property def default_options(self) -> dict[str, str]: """Return dict include default options.""" - return {CONF_VOICE: self.default_voice} + return {CONF_VOICE: self.default_voice, CONF_ENGINE: self.default_engine} @property def supported_options(self) -> list[str]: """Return a list of supported options.""" - return [CONF_VOICE] + return [CONF_VOICE, CONF_ENGINE] def get_tts_audio( self, @@ -177,9 +189,14 @@ def get_tts_audio( _LOGGER.error("%s does not support the %s language", voice_id, language) return None, None + engine = options.get(CONF_ENGINE, self.default_engine) + if voice_id not in self.all_engines[engine]: + _LOGGER.error("%s does not support the %s engine", voice_id, engine) + return None, None + _LOGGER.debug("Requesting TTS file for text: %s", message) resp = self.client.synthesize_speech( - Engine=self.config[CONF_ENGINE], + Engine=engine, OutputFormat=self.config[CONF_OUTPUT_FORMAT], SampleRate=self.config[CONF_SAMPLE_RATE], Text=message, diff --git a/homeassistant/components/ambient_network/manifest.json b/homeassistant/components/ambient_network/manifest.json index 553adb240b063a..4800ffcb29db01 100644 --- a/homeassistant/components/ambient_network/manifest.json +++ b/homeassistant/components/ambient_network/manifest.json @@ -7,5 +7,5 @@ "integration_type": "service", "iot_class": "cloud_polling", "loggers": ["aioambient"], - "requirements": ["aioambient==2024.01.0"] + "requirements": ["aioambient==2024.08.0"] } diff --git a/homeassistant/components/ambient_station/config_flow.py b/homeassistant/components/ambient_station/config_flow.py index 66e603ba2ff422..072ca68b86573f 100644 --- a/homeassistant/components/ambient_station/config_flow.py +++ b/homeassistant/components/ambient_station/config_flow.py @@ -2,6 +2,8 @@ from __future__ import annotations +from typing import Any + from aioambient import API from aioambient.errors import AmbientError import voluptuous as vol @@ -32,7 +34,9 @@ async def _show_form(self, errors: dict | None = None) -> ConfigFlowResult: errors=errors if errors else {}, ) - async def async_step_user(self, user_input: dict | None = None) -> ConfigFlowResult: + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the start of the config flow.""" if not user_input: return await self._show_form() diff --git a/homeassistant/components/ambient_station/manifest.json b/homeassistant/components/ambient_station/manifest.json index 046ab9f73e988d..a14de5f37c55d6 100644 --- a/homeassistant/components/ambient_station/manifest.json +++ b/homeassistant/components/ambient_station/manifest.json @@ -7,5 +7,5 @@ "integration_type": "hub", "iot_class": "cloud_push", "loggers": ["aioambient"], - "requirements": ["aioambient==2024.01.0"] + "requirements": ["aioambient==2024.08.0"] } diff --git a/homeassistant/components/amcrest/icons.json b/homeassistant/components/amcrest/icons.json index efba49d6b56d0e..e284bc152591ad 100644 --- a/homeassistant/components/amcrest/icons.json +++ b/homeassistant/components/amcrest/icons.json @@ -1,15 +1,37 @@ { "services": { - "enable_recording": "mdi:record-rec", - "disable_recording": "mdi:stop", - "enable_audio": "mdi:volume-high", - "disable_audio": "mdi:volume-off", - "enable_motion_recording": "mdi:motion-sensor", - "disable_motion_recording": "mdi:motion-sensor-off", - "goto_preset": "mdi:pan", - "set_color_bw": "mdi:palette", - "start_tour": "mdi:panorama", - "stop_tour": "mdi:panorama-outline", - "ptz_control": "mdi:pan" + "enable_recording": { + "service": "mdi:record-rec" + }, + "disable_recording": { + "service": "mdi:stop" + }, + "enable_audio": { + "service": "mdi:volume-high" + }, + "disable_audio": { + "service": "mdi:volume-off" + }, + "enable_motion_recording": { + "service": "mdi:motion-sensor" + }, + "disable_motion_recording": { + "service": "mdi:motion-sensor-off" + }, + "goto_preset": { + "service": "mdi:pan" + }, + "set_color_bw": { + "service": "mdi:palette" + }, + "start_tour": { + "service": "mdi:panorama" + }, + "stop_tour": { + "service": "mdi:panorama-outline" + }, + "ptz_control": { + "service": "mdi:pan" + } } } diff --git a/homeassistant/components/androidtv/icons.json b/homeassistant/components/androidtv/icons.json index 0127d60a72e528..d7c646dfdfc97d 100644 --- a/homeassistant/components/androidtv/icons.json +++ b/homeassistant/components/androidtv/icons.json @@ -1,8 +1,16 @@ { "services": { - "adb_command": "mdi:console", - "download": "mdi:download", - "upload": "mdi:upload", - "learn_sendevent": "mdi:remote" + "adb_command": { + "service": "mdi:console" + }, + "download": { + "service": "mdi:download" + }, + "upload": { + "service": "mdi:upload" + }, + "learn_sendevent": { + "service": "mdi:remote" + } } } diff --git a/homeassistant/components/androidtv_remote/manifest.json b/homeassistant/components/androidtv_remote/manifest.json index e24fcc5d653119..a06152fa57045e 100644 --- a/homeassistant/components/androidtv_remote/manifest.json +++ b/homeassistant/components/androidtv_remote/manifest.json @@ -8,6 +8,6 @@ "iot_class": "local_push", "loggers": ["androidtvremote2"], "quality_scale": "platinum", - "requirements": ["androidtvremote2==0.1.1"], + "requirements": ["androidtvremote2==0.1.2"], "zeroconf": ["_androidtvremote2._tcp.local."] } diff --git a/homeassistant/components/anthropic/const.py b/homeassistant/components/anthropic/const.py index 4ccf2c88faa2f4..0dbf9c51ac1bed 100644 --- a/homeassistant/components/anthropic/const.py +++ b/homeassistant/components/anthropic/const.py @@ -8,7 +8,7 @@ CONF_RECOMMENDED = "recommended" CONF_PROMPT = "prompt" CONF_CHAT_MODEL = "chat_model" -RECOMMENDED_CHAT_MODEL = "claude-3-5-sonnet-20240620" +RECOMMENDED_CHAT_MODEL = "claude-3-haiku-20240307" CONF_MAX_TOKENS = "max_tokens" RECOMMENDED_MAX_TOKENS = 1024 CONF_TEMPERATURE = "temperature" diff --git a/homeassistant/components/apcupsd/const.py b/homeassistant/components/apcupsd/const.py index 56bf229579dbb4..974c860afb81ab 100644 --- a/homeassistant/components/apcupsd/const.py +++ b/homeassistant/components/apcupsd/const.py @@ -6,4 +6,4 @@ CONNECTION_TIMEOUT: int = 10 # Field name of last self test retrieved from apcupsd. -LASTSTEST: Final = "laststest" +LAST_S_TEST: Final = "laststest" diff --git a/homeassistant/components/apcupsd/sensor.py b/homeassistant/components/apcupsd/sensor.py index ff72208e9ce4b1..d4bbfb148e59d2 100644 --- a/homeassistant/components/apcupsd/sensor.py +++ b/homeassistant/components/apcupsd/sensor.py @@ -13,7 +13,6 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( PERCENTAGE, - STATE_UNKNOWN, UnitOfApparentPower, UnitOfElectricCurrent, UnitOfElectricPotential, @@ -26,7 +25,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.update_coordinator import CoordinatorEntity -from .const import DOMAIN, LASTSTEST +from .const import DOMAIN, LAST_S_TEST from .coordinator import APCUPSdCoordinator PARALLEL_UPDATES = 0 @@ -157,8 +156,8 @@ device_class=SensorDeviceClass.TEMPERATURE, state_class=SensorStateClass.MEASUREMENT, ), - LASTSTEST: SensorEntityDescription( - key=LASTSTEST, + LAST_S_TEST: SensorEntityDescription( + key=LAST_S_TEST, translation_key="last_self_test", ), "lastxfer": SensorEntityDescription( @@ -423,7 +422,7 @@ async def async_setup_entry( # periodical (or manual) self test since last daemon restart. It might not be available # when we set up the integration, and we do not know if it would ever be available. Here we # add it anyway and mark it as unknown initially. - for resource in available_resources | {LASTSTEST}: + for resource in available_resources | {LAST_S_TEST}: if resource not in SENSORS: _LOGGER.warning("Invalid resource from APCUPSd: %s", resource.upper()) continue @@ -484,7 +483,7 @@ def _update_attrs(self) -> None: # performed) and may disappear again after certain event. So we mark the state as "unknown" # when it becomes unknown after such events. if key not in self.coordinator.data: - self._attr_native_value = STATE_UNKNOWN + self._attr_native_value = None return self._attr_native_value, inferred_unit = infer_unit(self.coordinator.data[key]) diff --git a/homeassistant/components/aquacell/config_flow.py b/homeassistant/components/aquacell/config_flow.py index 332cd16e749522..1ee89035d93d99 100644 --- a/homeassistant/components/aquacell/config_flow.py +++ b/homeassistant/components/aquacell/config_flow.py @@ -56,7 +56,7 @@ async def async_step_user( refresh_token = await api.authenticate( user_input[CONF_EMAIL], user_input[CONF_PASSWORD] ) - except ApiException: + except (ApiException, TimeoutError): errors["base"] = "cannot_connect" except AuthenticationFailed: errors["base"] = "invalid_auth" diff --git a/homeassistant/components/aquacell/coordinator.py b/homeassistant/components/aquacell/coordinator.py index dd5dfcd2d0d12e..ee4afb451b996c 100644 --- a/homeassistant/components/aquacell/coordinator.py +++ b/homeassistant/components/aquacell/coordinator.py @@ -56,7 +56,7 @@ async def _async_update_data(self) -> dict[str, Softener]: so entities can quickly look up their data. """ - async with asyncio.timeout(10): + async with asyncio.timeout(30): # Check if the refresh token is expired expiry_time = ( self.refresh_token_creation_time @@ -72,7 +72,7 @@ async def _async_update_data(self) -> dict[str, Softener]: softeners = await self.aquacell_api.get_all_softeners() except AuthenticationFailed as err: raise ConfigEntryError from err - except AquacellApiException as err: + except (AquacellApiException, TimeoutError) as err: raise UpdateFailed(f"Error communicating with API: {err}") from err return {softener.dsn: softener for softener in softeners} diff --git a/homeassistant/components/aranet/icons.json b/homeassistant/components/aranet/icons.json index 6d6e9a83b039c1..8e2b66c0150fd3 100644 --- a/homeassistant/components/aranet/icons.json +++ b/homeassistant/components/aranet/icons.json @@ -6,6 +6,9 @@ }, "radiation_rate": { "default": "mdi:radioactive" + }, + "radon_concentration": { + "default": "mdi:radioactive" } } } diff --git a/homeassistant/components/aranet/manifest.json b/homeassistant/components/aranet/manifest.json index 3f74d480c17878..6cce7554dd125b 100644 --- a/homeassistant/components/aranet/manifest.json +++ b/homeassistant/components/aranet/manifest.json @@ -19,5 +19,5 @@ "documentation": "https://www.home-assistant.io/integrations/aranet", "integration_type": "device", "iot_class": "local_push", - "requirements": ["aranet4==2.3.4"] + "requirements": ["aranet4==2.4.0"] } diff --git a/homeassistant/components/aranet/sensor.py b/homeassistant/components/aranet/sensor.py index c0fe194e87bdbe..1dc4b9f956ec93 100644 --- a/homeassistant/components/aranet/sensor.py +++ b/homeassistant/components/aranet/sensor.py @@ -99,6 +99,13 @@ class AranetSensorEntityDescription(SensorEntityDescription): suggested_display_precision=4, scale=0.000001, ), + "radon_concentration": AranetSensorEntityDescription( + key="radon_concentration", + translation_key="radon_concentration", + name="Radon Concentration", + native_unit_of_measurement="Bq/m³", + state_class=SensorStateClass.MEASUREMENT, + ), "battery": AranetSensorEntityDescription( key="battery", name="Battery", diff --git a/homeassistant/components/aseko_pool_live/config_flow.py b/homeassistant/components/aseko_pool_live/config_flow.py index cd2f0e4ac7ff4e..ce6de3683d5545 100644 --- a/homeassistant/components/aseko_pool_live/config_flow.py +++ b/homeassistant/components/aseko_pool_live/config_flow.py @@ -101,7 +101,7 @@ async def async_store_credentials(self, info: dict[str, Any]) -> ConfigFlowResul ) async def async_step_reauth( - self, user_input: Mapping[str, Any] + self, entry_data: Mapping[str, Any] ) -> ConfigFlowResult: """Perform reauth upon an API authentication error.""" @@ -109,10 +109,10 @@ async def async_step_reauth( self.context["entry_id"] ) - return await self.async_step_reauth_confirm(user_input) + return await self.async_step_reauth_confirm() async def async_step_reauth_confirm( - self, user_input: Mapping | None = None + self, user_input: dict[str, Any] | None = None ) -> ConfigFlowResult: """Dialog that informs the user that reauth is required.""" diff --git a/homeassistant/components/assist_pipeline/__init__.py b/homeassistant/components/assist_pipeline/__init__.py index 8ee053162b0663..0a03402105abd8 100644 --- a/homeassistant/components/assist_pipeline/__init__.py +++ b/homeassistant/components/assist_pipeline/__init__.py @@ -3,6 +3,7 @@ from __future__ import annotations from collections.abc import AsyncIterable +from typing import Any import voluptuous as vol @@ -99,7 +100,7 @@ async def async_pipeline_from_audio_stream( wake_word_phrase: str | None = None, pipeline_id: str | None = None, conversation_id: str | None = None, - tts_audio_output: str | None = None, + tts_audio_output: str | dict[str, Any] | None = None, wake_word_settings: WakeWordSettings | None = None, audio_settings: AudioSettings | None = None, device_id: str | None = None, diff --git a/homeassistant/components/assist_pipeline/audio_enhancer.py b/homeassistant/components/assist_pipeline/audio_enhancer.py index c9c60f421b1b86..ff2b122187a7ed 100644 --- a/homeassistant/components/assist_pipeline/audio_enhancer.py +++ b/homeassistant/components/assist_pipeline/audio_enhancer.py @@ -5,6 +5,7 @@ import logging from pymicro_vad import MicroVad +from pyspeex_noise import AudioProcessor from .const import BYTES_PER_CHUNK @@ -41,8 +42,8 @@ def enhance_chunk(self, audio: bytes, timestamp_ms: int) -> EnhancedAudioChunk: """Enhance chunk of PCM audio @ 16Khz with 16-bit mono samples.""" -class MicroVadEnhancer(AudioEnhancer): - """Audio enhancer that just runs microVAD.""" +class MicroVadSpeexEnhancer(AudioEnhancer): + """Audio enhancer that runs microVAD and speex.""" def __init__( self, auto_gain: int, noise_suppression: int, is_vad_enabled: bool @@ -50,6 +51,24 @@ def __init__( """Initialize audio enhancer.""" super().__init__(auto_gain, noise_suppression, is_vad_enabled) + self.audio_processor: AudioProcessor | None = None + + # Scale from 0-4 + self.noise_suppression = noise_suppression * -15 + + # Scale from 0-31 + self.auto_gain = auto_gain * 300 + + if (self.auto_gain != 0) or (self.noise_suppression != 0): + self.audio_processor = AudioProcessor( + self.auto_gain, self.noise_suppression + ) + _LOGGER.debug( + "Initialized speex with auto_gain=%s, noise_suppression=%s", + self.auto_gain, + self.noise_suppression, + ) + self.vad: MicroVad | None = None self.threshold = 0.5 @@ -61,12 +80,17 @@ def enhance_chunk(self, audio: bytes, timestamp_ms: int) -> EnhancedAudioChunk: """Enhance 10ms chunk of PCM audio @ 16Khz with 16-bit mono samples.""" is_speech: bool | None = None + assert len(audio) == BYTES_PER_CHUNK + if self.vad is not None: # Run VAD - assert len(audio) == BYTES_PER_CHUNK speech_prob = self.vad.Process10ms(audio) is_speech = speech_prob > self.threshold + if self.audio_processor is not None: + # Run noise suppression and auto gain + audio = self.audio_processor.Process10ms(audio).audio + return EnhancedAudioChunk( audio=audio, timestamp_ms=timestamp_ms, is_speech=is_speech ) diff --git a/homeassistant/components/assist_pipeline/manifest.json b/homeassistant/components/assist_pipeline/manifest.json index 00950b138fdb56..1b93ecd9eef37c 100644 --- a/homeassistant/components/assist_pipeline/manifest.json +++ b/homeassistant/components/assist_pipeline/manifest.json @@ -7,5 +7,5 @@ "integration_type": "system", "iot_class": "local_push", "quality_scale": "internal", - "requirements": ["pymicro-vad==1.0.1"] + "requirements": ["pymicro-vad==1.0.1", "pyspeex-noise==1.0.2"] } diff --git a/homeassistant/components/assist_pipeline/pipeline.py b/homeassistant/components/assist_pipeline/pipeline.py index 9fada934ca13bf..f6a6bc45b57eb2 100644 --- a/homeassistant/components/assist_pipeline/pipeline.py +++ b/homeassistant/components/assist_pipeline/pipeline.py @@ -49,7 +49,7 @@ ) from homeassistant.util.limited_size_dict import LimitedSizeDict -from .audio_enhancer import AudioEnhancer, EnhancedAudioChunk, MicroVadEnhancer +from .audio_enhancer import AudioEnhancer, EnhancedAudioChunk, MicroVadSpeexEnhancer from .const import ( BYTES_PER_CHUNK, CONF_DEBUG_RECORDING_DIR, @@ -538,7 +538,7 @@ class PipelineRun: language: str = None # type: ignore[assignment] runner_data: Any | None = None intent_agent: str | None = None - tts_audio_output: str | None = None + tts_audio_output: str | dict[str, Any] | None = None wake_word_settings: WakeWordSettings | None = None audio_settings: AudioSettings = field(default_factory=AudioSettings) @@ -589,7 +589,7 @@ def __post_init__(self) -> None: # Initialize with audio settings if self.audio_settings.needs_processor and (self.audio_enhancer is None): # Default audio enhancer - self.audio_enhancer = MicroVadEnhancer( + self.audio_enhancer = MicroVadSpeexEnhancer( self.audio_settings.auto_gain_dbfs, self.audio_settings.noise_suppression_level, self.audio_settings.is_vad_enabled, @@ -1052,12 +1052,15 @@ async def prepare_text_to_speech(self) -> None: if self.pipeline.tts_voice is not None: tts_options[tts.ATTR_VOICE] = self.pipeline.tts_voice - if self.tts_audio_output is not None: + if isinstance(self.tts_audio_output, dict): + tts_options.update(self.tts_audio_output) + elif isinstance(self.tts_audio_output, str): tts_options[tts.ATTR_PREFERRED_FORMAT] = self.tts_audio_output if self.tts_audio_output == "wav": # 16 Khz, 16-bit mono tts_options[tts.ATTR_PREFERRED_SAMPLE_RATE] = SAMPLE_RATE tts_options[tts.ATTR_PREFERRED_SAMPLE_CHANNELS] = SAMPLE_CHANNELS + tts_options[tts.ATTR_PREFERRED_SAMPLE_BYTES] = SAMPLE_WIDTH try: options_supported = await tts.async_support_options( diff --git a/homeassistant/components/assist_pipeline/vad.py b/homeassistant/components/assist_pipeline/vad.py index 8372dbc54c7650..4782d14dee47da 100644 --- a/homeassistant/components/assist_pipeline/vad.py +++ b/homeassistant/components/assist_pipeline/vad.py @@ -78,6 +78,9 @@ class VoiceCommandSegmenter: speech_seconds: float = 0.3 """Seconds of speech before voice command has started.""" + command_seconds: float = 1.0 + """Minimum number of seconds for a voice command.""" + silence_seconds: float = 0.7 """Seconds of silence after voice command has ended.""" @@ -96,6 +99,9 @@ class VoiceCommandSegmenter: _speech_seconds_left: float = 0.0 """Seconds left before considering voice command as started.""" + _command_seconds_left: float = 0.0 + """Seconds left before voice command could stop.""" + _silence_seconds_left: float = 0.0 """Seconds left before considering voice command as stopped.""" @@ -112,6 +118,7 @@ def __post_init__(self) -> None: def reset(self) -> None: """Reset all counters and state.""" self._speech_seconds_left = self.speech_seconds + self._command_seconds_left = self.command_seconds - self.speech_seconds self._silence_seconds_left = self.silence_seconds self._timeout_seconds_left = self.timeout_seconds self._reset_seconds_left = self.reset_seconds @@ -142,6 +149,9 @@ def process(self, chunk_seconds: float, is_speech: bool | None) -> bool: if self._speech_seconds_left <= 0: # Inside voice command self.in_command = True + self._command_seconds_left = ( + self.command_seconds - self.speech_seconds + ) self._silence_seconds_left = self.silence_seconds _LOGGER.debug("Voice command started") else: @@ -154,7 +164,8 @@ def process(self, chunk_seconds: float, is_speech: bool | None) -> bool: # Silence in command self._reset_seconds_left = self.reset_seconds self._silence_seconds_left -= chunk_seconds - if self._silence_seconds_left <= 0: + self._command_seconds_left -= chunk_seconds + if (self._silence_seconds_left <= 0) and (self._command_seconds_left <= 0): # Command finished successfully self.reset() _LOGGER.debug("Voice command finished") @@ -163,6 +174,7 @@ def process(self, chunk_seconds: float, is_speech: bool | None) -> bool: # Speech in command. # Reset silence counter if enough speech. self._reset_seconds_left -= chunk_seconds + self._command_seconds_left -= chunk_seconds if self._reset_seconds_left <= 0: self._silence_seconds_left = self.silence_seconds self._reset_seconds_left = self.reset_seconds diff --git a/homeassistant/components/asuswrt/bridge.py b/homeassistant/components/asuswrt/bridge.py index 4e928d63666b09..bc6f0fe6fd2258 100644 --- a/homeassistant/components/asuswrt/bridge.py +++ b/homeassistant/components/asuswrt/bridge.py @@ -5,6 +5,7 @@ from abc import ABC, abstractmethod from collections import namedtuple from collections.abc import Awaitable, Callable, Coroutine +from datetime import datetime import functools import logging from typing import Any, cast @@ -40,17 +41,23 @@ PROTOCOL_HTTPS, PROTOCOL_TELNET, SENSORS_BYTES, + SENSORS_CPU, SENSORS_LOAD_AVG, + SENSORS_MEMORY, SENSORS_RATES, SENSORS_TEMPERATURES, SENSORS_TEMPERATURES_LEGACY, + SENSORS_UPTIME, ) SENSORS_TYPE_BYTES = "sensors_bytes" SENSORS_TYPE_COUNT = "sensors_count" +SENSORS_TYPE_CPU = "sensors_cpu" SENSORS_TYPE_LOAD_AVG = "sensors_load_avg" +SENSORS_TYPE_MEMORY = "sensors_memory" SENSORS_TYPE_RATES = "sensors_rates" SENSORS_TYPE_TEMPERATURES = "sensors_temperatures" +SENSORS_TYPE_UPTIME = "sensors_uptime" WrtDevice = namedtuple("WrtDevice", ["ip", "name", "connected_to"]) # noqa: PYI024 @@ -346,6 +353,7 @@ async def async_get_connected_devices(self) -> dict[str, WrtDevice]: async def async_get_available_sensors(self) -> dict[str, dict[str, Any]]: """Return a dictionary of available sensors for this bridge.""" + sensors_cpu = await self._get_available_cpu_sensors() sensors_temperatures = await self._get_available_temperature_sensors() sensors_loadavg = await self._get_loadavg_sensors_availability() return { @@ -353,20 +361,49 @@ async def async_get_available_sensors(self) -> dict[str, dict[str, Any]]: KEY_SENSORS: SENSORS_BYTES, KEY_METHOD: self._get_bytes, }, + SENSORS_TYPE_CPU: { + KEY_SENSORS: sensors_cpu, + KEY_METHOD: self._get_cpu_usage, + }, SENSORS_TYPE_LOAD_AVG: { KEY_SENSORS: sensors_loadavg, KEY_METHOD: self._get_load_avg, }, + SENSORS_TYPE_MEMORY: { + KEY_SENSORS: SENSORS_MEMORY, + KEY_METHOD: self._get_memory_usage, + }, SENSORS_TYPE_RATES: { KEY_SENSORS: SENSORS_RATES, KEY_METHOD: self._get_rates, }, + SENSORS_TYPE_UPTIME: { + KEY_SENSORS: SENSORS_UPTIME, + KEY_METHOD: self._get_uptime, + }, SENSORS_TYPE_TEMPERATURES: { KEY_SENSORS: sensors_temperatures, KEY_METHOD: self._get_temperatures, }, } + async def _get_available_cpu_sensors(self) -> list[str]: + """Check which cpu information is available on the router.""" + try: + available_cpu = await self._api.async_get_cpu_usage() + available_sensors = [t for t in SENSORS_CPU if t in available_cpu] + except AsusWrtError as exc: + _LOGGER.warning( + ( + "Failed checking cpu sensor availability for ASUS router" + " %s. Exception: %s" + ), + self.host, + exc, + ) + return [] + return available_sensors + async def _get_available_temperature_sensors(self) -> list[str]: """Check which temperature information is available on the router.""" try: @@ -415,3 +452,25 @@ async def _get_load_avg(self) -> Any: async def _get_temperatures(self) -> Any: """Fetch temperatures information from the router.""" return await self._api.async_get_temperatures() + + @handle_errors_and_zip(AsusWrtError, None) + async def _get_cpu_usage(self) -> Any: + """Fetch cpu information from the router.""" + return await self._api.async_get_cpu_usage() + + @handle_errors_and_zip(AsusWrtError, None) + async def _get_memory_usage(self) -> Any: + """Fetch memory information from the router.""" + return await self._api.async_get_memory_usage() + + async def _get_uptime(self) -> dict[str, Any]: + """Fetch uptime from the router.""" + try: + uptimes = await self._api.async_get_uptime() + except AsusWrtError as exc: + raise UpdateFailed(exc) from exc + + last_boot = datetime.fromisoformat(uptimes["last_boot"]) + uptime = uptimes["uptime"] + + return dict(zip(SENSORS_UPTIME, [last_boot, uptime], strict=False)) diff --git a/homeassistant/components/asuswrt/const.py b/homeassistant/components/asuswrt/const.py index 5ce37207145ab0..7790750538e1aa 100644 --- a/homeassistant/components/asuswrt/const.py +++ b/homeassistant/components/asuswrt/const.py @@ -27,7 +27,20 @@ # Sensors SENSORS_BYTES = ["sensor_rx_bytes", "sensor_tx_bytes"] SENSORS_CONNECTED_DEVICE = ["sensor_connected_device"] +SENSORS_CPU = [ + "cpu_total_usage", + "cpu1_usage", + "cpu2_usage", + "cpu3_usage", + "cpu4_usage", + "cpu5_usage", + "cpu6_usage", + "cpu7_usage", + "cpu8_usage", +] SENSORS_LOAD_AVG = ["sensor_load_avg1", "sensor_load_avg5", "sensor_load_avg15"] +SENSORS_MEMORY = ["mem_usage_perc", "mem_free", "mem_used"] SENSORS_RATES = ["sensor_rx_rates", "sensor_tx_rates"] SENSORS_TEMPERATURES_LEGACY = ["2.4GHz", "5.0GHz", "CPU"] SENSORS_TEMPERATURES = [*SENSORS_TEMPERATURES_LEGACY, "5.0GHz_2", "6.0GHz"] +SENSORS_UPTIME = ["sensor_last_boot", "sensor_uptime"] diff --git a/homeassistant/components/asuswrt/icons.json b/homeassistant/components/asuswrt/icons.json index a4e44496a2fa3d..b5b2c35f742f6b 100644 --- a/homeassistant/components/asuswrt/icons.json +++ b/homeassistant/components/asuswrt/icons.json @@ -24,6 +24,21 @@ }, "load_avg_15m": { "default": "mdi:cpu-32-bit" + }, + "cpu_usage": { + "default": "mdi:cpu-32-bit" + }, + "cpu_core_usage": { + "default": "mdi:cpu-32-bit" + }, + "memory_usage": { + "default": "mdi:memory" + }, + "memory_free": { + "default": "mdi:memory" + }, + "memory_used": { + "default": "mdi:memory" } } } diff --git a/homeassistant/components/asuswrt/sensor.py b/homeassistant/components/asuswrt/sensor.py index 69470882153110..fb43e574379d3e 100644 --- a/homeassistant/components/asuswrt/sensor.py +++ b/homeassistant/components/asuswrt/sensor.py @@ -11,10 +11,12 @@ SensorStateClass, ) from homeassistant.const import ( + PERCENTAGE, EntityCategory, UnitOfDataRate, UnitOfInformation, UnitOfTemperature, + UnitOfTime, ) from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback @@ -30,9 +32,12 @@ KEY_SENSORS, SENSORS_BYTES, SENSORS_CONNECTED_DEVICE, + SENSORS_CPU, SENSORS_LOAD_AVG, + SENSORS_MEMORY, SENSORS_RATES, SENSORS_TEMPERATURES, + SENSORS_UPTIME, ) from .router import AsusWrtRouter @@ -46,6 +51,19 @@ class AsusWrtSensorEntityDescription(SensorEntityDescription): UNIT_DEVICES = "Devices" +CPU_CORE_SENSORS: tuple[AsusWrtSensorEntityDescription, ...] = tuple( + AsusWrtSensorEntityDescription( + key=sens_key, + translation_key="cpu_core_usage", + translation_placeholders={"core_id": str(core_id)}, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=PERCENTAGE, + entity_category=EntityCategory.DIAGNOSTIC, + entity_registry_enabled_default=False, + suggested_display_precision=1, + ) + for core_id, sens_key in enumerate(SENSORS_CPU[1:], start=1) +) CONNECTION_SENSORS: tuple[AsusWrtSensorEntityDescription, ...] = ( AsusWrtSensorEntityDescription( key=SENSORS_CONNECTED_DEVICE[0], @@ -167,6 +185,61 @@ class AsusWrtSensorEntityDescription(SensorEntityDescription): entity_registry_enabled_default=False, suggested_display_precision=1, ), + AsusWrtSensorEntityDescription( + key=SENSORS_MEMORY[0], + translation_key="memory_usage", + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=PERCENTAGE, + entity_category=EntityCategory.DIAGNOSTIC, + entity_registry_enabled_default=False, + suggested_display_precision=1, + ), + AsusWrtSensorEntityDescription( + key=SENSORS_MEMORY[1], + translation_key="memory_free", + state_class=SensorStateClass.MEASUREMENT, + device_class=SensorDeviceClass.DATA_SIZE, + native_unit_of_measurement=UnitOfInformation.MEGABYTES, + entity_category=EntityCategory.DIAGNOSTIC, + entity_registry_enabled_default=False, + suggested_display_precision=2, + factor=1024, + ), + AsusWrtSensorEntityDescription( + key=SENSORS_MEMORY[2], + translation_key="memory_used", + state_class=SensorStateClass.MEASUREMENT, + device_class=SensorDeviceClass.DATA_SIZE, + native_unit_of_measurement=UnitOfInformation.MEGABYTES, + entity_category=EntityCategory.DIAGNOSTIC, + entity_registry_enabled_default=False, + suggested_display_precision=2, + factor=1024, + ), + AsusWrtSensorEntityDescription( + key=SENSORS_UPTIME[0], + translation_key="last_boot", + device_class=SensorDeviceClass.TIMESTAMP, + ), + AsusWrtSensorEntityDescription( + key=SENSORS_UPTIME[1], + translation_key="uptime", + state_class=SensorStateClass.TOTAL, + device_class=SensorDeviceClass.DURATION, + native_unit_of_measurement=UnitOfTime.SECONDS, + entity_category=EntityCategory.DIAGNOSTIC, + entity_registry_enabled_default=False, + ), + AsusWrtSensorEntityDescription( + key=SENSORS_CPU[0], + translation_key="cpu_usage", + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=PERCENTAGE, + entity_category=EntityCategory.DIAGNOSTIC, + entity_registry_enabled_default=False, + suggested_display_precision=1, + ), + *CPU_CORE_SENSORS, ) diff --git a/homeassistant/components/asuswrt/strings.json b/homeassistant/components/asuswrt/strings.json index 4c8386dcd0042d..bab40f281f59b8 100644 --- a/homeassistant/components/asuswrt/strings.json +++ b/homeassistant/components/asuswrt/strings.json @@ -88,6 +88,27 @@ }, "6ghz_temperature": { "name": "6GHz Temperature" + }, + "cpu_usage": { + "name": "CPU usage" + }, + "cpu_core_usage": { + "name": "CPU core {core_id} usage" + }, + "memory_usage": { + "name": "Memory usage" + }, + "memory_free": { + "name": "Memory free" + }, + "memory_used": { + "name": "Memory used" + }, + "last_boot": { + "name": "Last boot" + }, + "uptime": { + "name": "Uptime" } } }, diff --git a/homeassistant/components/august/__init__.py b/homeassistant/components/august/__init__.py index 53aa3cdffd8cb2..434db46384baa3 100644 --- a/homeassistant/components/august/__init__.py +++ b/homeassistant/components/august/__init__.py @@ -6,15 +6,16 @@ from typing import cast from aiohttp import ClientResponseError +from yalexs.const import Brand from yalexs.exceptions import AugustApiAIOHTTPError from yalexs.manager.exceptions import CannotConnect, InvalidAuth, RequireValidation from yalexs.manager.gateway import Config as YaleXSConfig from homeassistant.config_entries import ConfigEntry from homeassistant.const import EVENT_HOMEASSISTANT_STOP -from homeassistant.core import HomeAssistant +from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady -from homeassistant.helpers import device_registry as dr +from homeassistant.helpers import device_registry as dr, issue_registry as ir from .const import DOMAIN, PLATFORMS from .data import AugustData @@ -24,7 +25,27 @@ type AugustConfigEntry = ConfigEntry[AugustData] -async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: +@callback +def _async_create_yale_brand_migration_issue( + hass: HomeAssistant, entry: AugustConfigEntry +) -> None: + """Create an issue for a brand migration.""" + ir.async_create_issue( + hass, + DOMAIN, + "yale_brand_migration", + breaks_in_ha_version="2024.9", + learn_more_url="https://www.home-assistant.io/integrations/yale", + translation_key="yale_brand_migration", + is_fixable=False, + severity=ir.IssueSeverity.CRITICAL, + translation_placeholders={ + "migrate_url": "https://my.home-assistant.io/redirect/config_flow_start?domain=yale" + }, + ) + + +async def async_setup_entry(hass: HomeAssistant, entry: AugustConfigEntry) -> bool: """Set up August from a config entry.""" session = async_create_august_clientsession(hass) august_gateway = AugustGateway(Path(hass.config.config_dir), session) @@ -40,6 +61,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: return True +async def async_remove_entry(hass: HomeAssistant, entry: AugustConfigEntry) -> None: + """Remove an August config entry.""" + ir.async_delete_issue(hass, DOMAIN, "yale_brand_migration") + + async def async_unload_entry(hass: HomeAssistant, entry: AugustConfigEntry) -> bool: """Unload a config entry.""" return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) @@ -51,6 +77,8 @@ async def async_setup_august( """Set up the August component.""" config = cast(YaleXSConfig, entry.data) await august_gateway.async_setup(config) + if august_gateway.api.brand == Brand.YALE_HOME: + _async_create_yale_brand_migration_issue(hass, entry) await august_gateway.async_authenticate() await august_gateway.async_refresh_access_token_if_needed() data = entry.runtime_data = AugustData(hass, august_gateway) diff --git a/homeassistant/components/august/binary_sensor.py b/homeassistant/components/august/binary_sensor.py index 6a56692bcd6f1c..fb877252010a71 100644 --- a/homeassistant/components/august/binary_sensor.py +++ b/homeassistant/components/august/binary_sensor.py @@ -109,12 +109,11 @@ async def async_setup_entry( for description in SENSOR_TYPES_DOORBELL ) - for doorbell in data.doorbells: - entities.extend( - AugustDoorbellBinarySensor(data, doorbell, description) - for description in SENSOR_TYPES_DOORBELL + SENSOR_TYPES_VIDEO_DOORBELL - ) - + entities.extend( + AugustDoorbellBinarySensor(data, doorbell, description) + for description in SENSOR_TYPES_DOORBELL + SENSOR_TYPES_VIDEO_DOORBELL + for doorbell in data.doorbells + ) async_add_entities(entities) diff --git a/homeassistant/components/august/button.py b/homeassistant/components/august/button.py index 406475db601bb8..79f2b67888afa1 100644 --- a/homeassistant/components/august/button.py +++ b/homeassistant/components/august/button.py @@ -5,7 +5,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback from . import AugustConfigEntry -from .entity import AugustEntityMixin +from .entity import AugustEntity async def async_setup_entry( @@ -18,7 +18,7 @@ async def async_setup_entry( async_add_entities(AugustWakeLockButton(data, lock, "wake") for lock in data.locks) -class AugustWakeLockButton(AugustEntityMixin, ButtonEntity): +class AugustWakeLockButton(AugustEntity, ButtonEntity): """Representation of an August lock wake button.""" _attr_translation_key = "wake" diff --git a/homeassistant/components/august/camera.py b/homeassistant/components/august/camera.py index 4e569e2a91ec42..f4398455256398 100644 --- a/homeassistant/components/august/camera.py +++ b/homeassistant/components/august/camera.py @@ -16,7 +16,7 @@ from . import AugustConfigEntry, AugustData from .const import DEFAULT_NAME, DEFAULT_TIMEOUT -from .entity import AugustEntityMixin +from .entity import AugustEntity _LOGGER = logging.getLogger(__name__) @@ -38,7 +38,7 @@ async def async_setup_entry( ) -class AugustCamera(AugustEntityMixin, Camera): +class AugustCamera(AugustEntity, Camera): """An implementation of an August security camera.""" _attr_translation_key = "camera" diff --git a/homeassistant/components/august/config_flow.py b/homeassistant/components/august/config_flow.py index 3523a4f7c3923c..58c3549fe4d1d8 100644 --- a/homeassistant/components/august/config_flow.py +++ b/homeassistant/components/august/config_flow.py @@ -9,7 +9,7 @@ import aiohttp import voluptuous as vol from yalexs.authenticator_common import ValidationResult -from yalexs.const import BRANDS, DEFAULT_BRAND +from yalexs.const import BRANDS_WITHOUT_OAUTH, DEFAULT_BRAND, Brand from yalexs.manager.exceptions import CannotConnect, InvalidAuth, RequireValidation from homeassistant.config_entries import ConfigFlow, ConfigFlowResult @@ -28,6 +28,12 @@ from .gateway import AugustGateway from .util import async_create_august_clientsession +# The Yale Home Brand is not supported by the August integration +# anymore and should migrate to the Yale integration +AVAILABLE_BRANDS = BRANDS_WITHOUT_OAUTH.copy() +del AVAILABLE_BRANDS[Brand.YALE_HOME] + + _LOGGER = logging.getLogger(__name__) @@ -118,7 +124,7 @@ async def async_step_user_validate( vol.Required( CONF_BRAND, default=self._user_auth_details.get(CONF_BRAND, DEFAULT_BRAND), - ): vol.In(BRANDS), + ): vol.In(AVAILABLE_BRANDS), vol.Required( CONF_LOGIN_METHOD, default=self._user_auth_details.get( @@ -208,7 +214,7 @@ async def async_step_reauth_validate( vol.Required( CONF_BRAND, default=self._user_auth_details.get(CONF_BRAND, DEFAULT_BRAND), - ): vol.In(BRANDS), + ): vol.In(BRANDS_WITHOUT_OAUTH), vol.Required(CONF_PASSWORD): str, } ), diff --git a/homeassistant/components/august/entity.py b/homeassistant/components/august/entity.py index babf5c587fb965..28c722354bae54 100644 --- a/homeassistant/components/august/entity.py +++ b/homeassistant/components/august/entity.py @@ -20,7 +20,7 @@ DEVICE_TYPES = ["keypad", "lock", "camera", "doorbell", "door", "bell"] -class AugustEntityMixin(Entity): +class AugustEntity(Entity): """Base implementation for August device.""" _attr_should_poll = False @@ -87,7 +87,7 @@ async def async_added_to_hass(self) -> None: self._update_from_data() -class AugustDescriptionEntity(AugustEntityMixin): +class AugustDescriptionEntity(AugustEntity): """An August entity with a description.""" def __init__( diff --git a/homeassistant/components/august/event.py b/homeassistant/components/august/event.py index b65f72272a3919..49b14630337411 100644 --- a/homeassistant/components/august/event.py +++ b/homeassistant/components/august/event.py @@ -63,22 +63,17 @@ async def async_setup_entry( ) -> None: """Set up the august event platform.""" data = config_entry.runtime_data - entities: list[AugustEventEntity] = [] - - for lock in data.locks: - detail = data.get_device_detail(lock.device_id) - if detail.doorbell: - entities.extend( - AugustEventEntity(data, lock, description) - for description in TYPES_DOORBELL - ) - - for doorbell in data.doorbells: - entities.extend( - AugustEventEntity(data, doorbell, description) - for description in TYPES_DOORBELL + TYPES_VIDEO_DOORBELL - ) - + entities: list[AugustEventEntity] = [ + AugustEventEntity(data, lock, description) + for description in TYPES_DOORBELL + for lock in data.locks + if (detail := data.get_device_detail(lock.device_id)) and detail.doorbell + ] + entities.extend( + AugustEventEntity(data, doorbell, description) + for description in TYPES_DOORBELL + TYPES_VIDEO_DOORBELL + for doorbell in data.doorbells + ) async_add_entities(entities) @@ -86,7 +81,6 @@ class AugustEventEntity(AugustDescriptionEntity, EventEntity): """An august event entity.""" entity_description: AugustEventEntityDescription - _attr_has_entity_name = True _last_activity: Activity | None = None @callback diff --git a/homeassistant/components/august/lock.py b/homeassistant/components/august/lock.py index 5382c710229950..fe5d90371adfef 100644 --- a/homeassistant/components/august/lock.py +++ b/homeassistant/components/august/lock.py @@ -19,7 +19,7 @@ import homeassistant.util.dt as dt_util from . import AugustConfigEntry, AugustData -from .entity import AugustEntityMixin +from .entity import AugustEntity _LOGGER = logging.getLogger(__name__) @@ -36,7 +36,7 @@ async def async_setup_entry( async_add_entities(AugustLock(data, lock) for lock in data.locks) -class AugustLock(AugustEntityMixin, RestoreEntity, LockEntity): +class AugustLock(AugustEntity, RestoreEntity, LockEntity): """Representation of an August lock.""" _attr_name = None diff --git a/homeassistant/components/august/manifest.json b/homeassistant/components/august/manifest.json index 5d7b253e95253f..6635a95f1cf9a6 100644 --- a/homeassistant/components/august/manifest.json +++ b/homeassistant/components/august/manifest.json @@ -4,10 +4,6 @@ "codeowners": ["@bdraco"], "config_flow": true, "dhcp": [ - { - "hostname": "yale-connect-plus", - "macaddress": "00177A*" - }, { "hostname": "connect", "macaddress": "D86162*" @@ -28,5 +24,5 @@ "documentation": "https://www.home-assistant.io/integrations/august", "iot_class": "cloud_push", "loggers": ["pubnub", "yalexs"], - "requirements": ["yalexs==8.1.2", "yalexs-ble==2.4.3"] + "requirements": ["yalexs==8.6.3", "yalexs-ble==2.4.3"] } diff --git a/homeassistant/components/august/sensor.py b/homeassistant/components/august/sensor.py index 7a4c1a9235840e..b7c0d6184925d4 100644 --- a/homeassistant/components/august/sensor.py +++ b/homeassistant/components/august/sensor.py @@ -4,7 +4,7 @@ from collections.abc import Callable from dataclasses import dataclass -from typing import Any, Generic, TypeVar, cast +from typing import Any, cast from yalexs.activity import ActivityType, LockOperationActivity from yalexs.doorbell import Doorbell @@ -42,7 +42,7 @@ OPERATION_METHOD_REMOTE, OPERATION_METHOD_TAG, ) -from .entity import AugustDescriptionEntity, AugustEntityMixin +from .entity import AugustDescriptionEntity, AugustEntity def _retrieve_device_battery_state(detail: LockDetail) -> int: @@ -55,14 +55,13 @@ def _retrieve_linked_keypad_battery_state(detail: KeypadDetail) -> int | None: return detail.battery_percentage -_T = TypeVar("_T", LockDetail, KeypadDetail) - - @dataclass(frozen=True, kw_only=True) -class AugustSensorEntityDescription(SensorEntityDescription, Generic[_T]): +class AugustSensorEntityDescription[T: LockDetail | KeypadDetail]( + SensorEntityDescription +): """Mixin for required keys.""" - value_fn: Callable[[_T], int | None] + value_fn: Callable[[T], int | None] SENSOR_TYPE_DEVICE_BATTERY = AugustSensorEntityDescription[LockDetail]( @@ -114,7 +113,7 @@ async def async_setup_entry( async_add_entities(entities) -class AugustOperatorSensor(AugustEntityMixin, RestoreSensor): +class AugustOperatorSensor(AugustEntity, RestoreSensor): """Representation of an August lock operation sensor.""" _attr_translation_key = "operator" @@ -198,10 +197,12 @@ async def async_added_to_hass(self) -> None: self._operated_autorelock = last_attrs[ATTR_OPERATION_AUTORELOCK] -class AugustBatterySensor(AugustDescriptionEntity, SensorEntity, Generic[_T]): +class AugustBatterySensor[T: LockDetail | KeypadDetail]( + AugustDescriptionEntity, SensorEntity +): """Representation of an August sensor.""" - entity_description: AugustSensorEntityDescription[_T] + entity_description: AugustSensorEntityDescription[T] _attr_device_class = SensorDeviceClass.BATTERY _attr_native_unit_of_measurement = PERCENTAGE diff --git a/homeassistant/components/august/strings.json b/homeassistant/components/august/strings.json index 772a8dca479073..589a494590b44e 100644 --- a/homeassistant/components/august/strings.json +++ b/homeassistant/components/august/strings.json @@ -1,4 +1,10 @@ { + "issues": { + "yale_brand_migration": { + "title": "Yale Home has a new integration", + "description": "Add the [Yale integration]({migrate_url}), and remove the August integration as soon as possible to avoid an interruption in service. The Yale Home brand will stop working with the August integration soon and will be removed in a future release." + } + }, "config": { "error": { "unhandled": "Unhandled error: {error}", diff --git a/homeassistant/components/august/util.py b/homeassistant/components/august/util.py index 6972913ba22142..5449d048613088 100644 --- a/homeassistant/components/august/util.py +++ b/homeassistant/components/august/util.py @@ -63,16 +63,11 @@ def _activity_time_based(latest: Activity) -> Activity | None: """Get the latest state of the sensor.""" start = latest.activity_start_time end = latest.activity_end_time + TIME_TO_DECLARE_DETECTION - if start <= _native_datetime() <= end: + if start <= datetime.now() <= end: return latest return None -def _native_datetime() -> datetime: - """Return time in the format august uses without timezone.""" - return datetime.now() - - def retrieve_online_state( data: AugustData, detail: DoorbellDetail | LockDetail ) -> bool: diff --git a/homeassistant/components/aurora_abb_powerone/config_flow.py b/homeassistant/components/aurora_abb_powerone/config_flow.py index f0093c626319e2..47c349ab48a95f 100644 --- a/homeassistant/components/aurora_abb_powerone/config_flow.py +++ b/homeassistant/components/aurora_abb_powerone/config_flow.py @@ -75,11 +75,10 @@ class AuroraABBConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - def __init__(self): + def __init__(self) -> None: """Initialise the config flow.""" - self.config = None self._com_ports_list: list[str] | None = None - self._default_com_port = None + self._default_com_port: str | None = None async def async_step_user( self, user_input: dict[str, Any] | None = None diff --git a/homeassistant/components/aussie_broadband/config_flow.py b/homeassistant/components/aussie_broadband/config_flow.py index 587c7df2b36c26..65507d57e8bbf3 100644 --- a/homeassistant/components/aussie_broadband/config_flow.py +++ b/homeassistant/components/aussie_broadband/config_flow.py @@ -22,11 +22,11 @@ class AussieBroadbandConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - def __init__(self): + def __init__(self) -> None: """Initialize the config flow.""" self.data: dict = {} self.options: dict = {CONF_SERVICES: []} - self.services: list[dict[str]] = [] + self.services: list[dict[str, Any]] = [] self.client: AussieBB | None = None self._reauth_username: str | None = None @@ -99,15 +99,11 @@ async def async_step_reauth_confirm( } if not (errors := await self.async_auth(data)): - entry = await self.async_set_unique_id(self._reauth_username.lower()) - if entry: - self.hass.config_entries.async_update_entry( - entry, - data=data, - ) - await self.hass.config_entries.async_reload(entry.entry_id) - return self.async_abort(reason="reauth_successful") - return self.async_create_entry(title=self._reauth_username, data=data) + entry = self.hass.config_entries.async_get_entry( + self.context["entry_id"] + ) + assert entry + return self.async_update_reload_and_abort(entry, data=data) return self.async_show_form( step_id="reauth_confirm", diff --git a/homeassistant/components/automation/__init__.py b/homeassistant/components/automation/__init__.py index 8ab9c478bc484d..2081ea938ae2ec 100644 --- a/homeassistant/components/automation/__init__.py +++ b/homeassistant/components/automation/__init__.py @@ -991,15 +991,15 @@ async def _create_automation_entities( # Add trigger variables to variables variables = None - if CONF_TRIGGER_VARIABLES in config_block: + if CONF_TRIGGER_VARIABLES in config_block and CONF_VARIABLES in config_block: variables = ScriptVariables( dict(config_block[CONF_TRIGGER_VARIABLES].as_dict()) ) - if CONF_VARIABLES in config_block: - if variables: - variables.variables.update(config_block[CONF_VARIABLES].as_dict()) - else: - variables = config_block[CONF_VARIABLES] + variables.variables.update(config_block[CONF_VARIABLES].as_dict()) + elif CONF_TRIGGER_VARIABLES in config_block: + variables = config_block[CONF_TRIGGER_VARIABLES] + elif CONF_VARIABLES in config_block: + variables = config_block[CONF_VARIABLES] entity = AutomationEntity( automation_id, diff --git a/homeassistant/components/automation/blueprints/motion_light.yaml b/homeassistant/components/automation/blueprints/motion_light.yaml index 8f5d3f957f990a..ad9c6f0286b551 100644 --- a/homeassistant/components/automation/blueprints/motion_light.yaml +++ b/homeassistant/components/automation/blueprints/motion_light.yaml @@ -10,8 +10,10 @@ blueprint: selector: entity: filter: - device_class: motion - domain: binary_sensor + - device_class: occupancy + domain: binary_sensor + - device_class: motion + domain: binary_sensor light_target: name: Light selector: diff --git a/homeassistant/components/automation/icons.json b/homeassistant/components/automation/icons.json index 9b68825ffd1629..f1e0f26ef65a9a 100644 --- a/homeassistant/components/automation/icons.json +++ b/homeassistant/components/automation/icons.json @@ -9,10 +9,20 @@ } }, "services": { - "turn_on": "mdi:robot", - "turn_off": "mdi:robot-off", - "toggle": "mdi:robot", - "trigger": "mdi:robot", - "reload": "mdi:reload" + "turn_on": { + "service": "mdi:robot" + }, + "turn_off": { + "service": "mdi:robot-off" + }, + "toggle": { + "service": "mdi:robot" + }, + "trigger": { + "service": "mdi:robot" + }, + "reload": { + "service": "mdi:reload" + } } } diff --git a/homeassistant/components/aws/config_flow.py b/homeassistant/components/aws/config_flow.py index 8c80b0d487d5ec..3175e6bc56c4c1 100644 --- a/homeassistant/components/aws/config_flow.py +++ b/homeassistant/components/aws/config_flow.py @@ -1,6 +1,5 @@ """Config flow for AWS component.""" -from collections.abc import Mapping from typing import Any from homeassistant.config_entries import ConfigFlow, ConfigFlowResult @@ -13,11 +12,9 @@ class AWSFlowHandler(ConfigFlow, domain=DOMAIN): VERSION = 1 - async def async_step_import( - self, user_input: Mapping[str, Any] - ) -> ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Import a config entry.""" if self._async_current_entries(): return self.async_abort(reason="single_instance_allowed") - return self.async_create_entry(title="configuration.yaml", data=user_input) + return self.async_create_entry(title="configuration.yaml", data=import_data) diff --git a/homeassistant/components/azure_devops/coordinator.py b/homeassistant/components/azure_devops/coordinator.py index 22dbe32c103d9d..21fb76560c3c7c 100644 --- a/homeassistant/components/azure_devops/coordinator.py +++ b/homeassistant/components/azure_devops/coordinator.py @@ -6,8 +6,14 @@ from typing import Final from aioazuredevops.client import DevOpsClient -from aioazuredevops.models.builds import Build +from aioazuredevops.helper import ( + WorkItemTypeAndState, + work_item_types_states_filter, + work_items_by_type_and_state, +) +from aioazuredevops.models.build import Build from aioazuredevops.models.core import Project +from aioazuredevops.models.work_item_type import Category import aiohttp from homeassistant.config_entries import ConfigEntry @@ -20,6 +26,7 @@ from .data import AzureDevOpsData BUILDS_QUERY: Final = "?queryOrder=queueTimeDescending&maxBuildsPerDefinition=1" +IGNORED_CATEGORIES: Final[list[Category]] = [Category.COMPLETED, Category.REMOVED] def ado_exception_none_handler(func: Callable) -> Callable: @@ -105,13 +112,60 @@ async def _get_builds(self, project_name: str) -> list[Build] | None: BUILDS_QUERY, ) + @ado_exception_none_handler + async def _get_work_items( + self, project_name: str + ) -> list[WorkItemTypeAndState] | None: + """Get the work items.""" + + if ( + work_item_types := await self.client.get_work_item_types( + self.organization, + project_name, + ) + ) is None: + # If no work item types are returned, return an empty list + return [] + + if ( + work_item_ids := await self.client.get_work_item_ids( + self.organization, + project_name, + # Filter out completed and removed work items so we only get active work items + states=work_item_types_states_filter( + work_item_types, + ignored_categories=IGNORED_CATEGORIES, + ), + ) + ) is None: + # If no work item ids are returned, return an empty list + return [] + + if ( + work_items := await self.client.get_work_items( + self.organization, + project_name, + work_item_ids, + ) + ) is None: + # If no work items are returned, return an empty list + return [] + + return work_items_by_type_and_state( + work_item_types, + work_items, + ignored_categories=IGNORED_CATEGORIES, + ) + async def _async_update_data(self) -> AzureDevOpsData: """Fetch data from Azure DevOps.""" # Get the builds from the project builds = await self._get_builds(self.project.name) + work_items = await self._get_work_items(self.project.name) return AzureDevOpsData( organization=self.organization, project=self.project, builds=builds, + work_items=work_items, ) diff --git a/homeassistant/components/azure_devops/data.py b/homeassistant/components/azure_devops/data.py index 6d9e2069b67b46..ff34bc90c24442 100644 --- a/homeassistant/components/azure_devops/data.py +++ b/homeassistant/components/azure_devops/data.py @@ -2,7 +2,8 @@ from dataclasses import dataclass -from aioazuredevops.models.builds import Build +from aioazuredevops.helper import WorkItemTypeAndState +from aioazuredevops.models.build import Build from aioazuredevops.models.core import Project @@ -13,3 +14,4 @@ class AzureDevOpsData: organization: str project: Project builds: list[Build] + work_items: list[WorkItemTypeAndState] diff --git a/homeassistant/components/azure_devops/icons.json b/homeassistant/components/azure_devops/icons.json index de720b46106760..ea6b4c632ea2a2 100644 --- a/homeassistant/components/azure_devops/icons.json +++ b/homeassistant/components/azure_devops/icons.json @@ -3,6 +3,9 @@ "sensor": { "latest_build": { "default": "mdi:pipe" + }, + "work_item_count": { + "default": "mdi:ticket" } } } diff --git a/homeassistant/components/azure_devops/manifest.json b/homeassistant/components/azure_devops/manifest.json index 48ceee5f9d84c8..5086e44ab0fff7 100644 --- a/homeassistant/components/azure_devops/manifest.json +++ b/homeassistant/components/azure_devops/manifest.json @@ -6,5 +6,5 @@ "documentation": "https://www.home-assistant.io/integrations/azure_devops", "iot_class": "cloud_polling", "loggers": ["aioazuredevops"], - "requirements": ["aioazuredevops==2.1.1"] + "requirements": ["aioazuredevops==2.2.1"] } diff --git a/homeassistant/components/azure_devops/sensor.py b/homeassistant/components/azure_devops/sensor.py index 029d3d875dca84..fd47115214a448 100644 --- a/homeassistant/components/azure_devops/sensor.py +++ b/homeassistant/components/azure_devops/sensor.py @@ -8,7 +8,8 @@ import logging from typing import Any -from aioazuredevops.models.builds import Build +from aioazuredevops.helper import WorkItemState, WorkItemTypeAndState +from aioazuredevops.models.build import Build from homeassistant.components.sensor import ( SensorDeviceClass, @@ -29,12 +30,19 @@ @dataclass(frozen=True, kw_only=True) class AzureDevOpsBuildSensorEntityDescription(SensorEntityDescription): - """Class describing Azure DevOps base build sensor entities.""" + """Class describing Azure DevOps build sensor entities.""" attr_fn: Callable[[Build], dict[str, Any] | None] = lambda _: None value_fn: Callable[[Build], datetime | StateType] +@dataclass(frozen=True, kw_only=True) +class AzureDevOpsWorkItemSensorEntityDescription(SensorEntityDescription): + """Class describing Azure DevOps work item sensor entities.""" + + value_fn: Callable[[WorkItemState], datetime | StateType] + + BASE_BUILD_SENSOR_DESCRIPTIONS: tuple[AzureDevOpsBuildSensorEntityDescription, ...] = ( # Attributes are deprecated in 2024.7 and can be removed in 2025.1 AzureDevOpsBuildSensorEntityDescription( @@ -116,6 +124,16 @@ class AzureDevOpsBuildSensorEntityDescription(SensorEntityDescription): ), ) +BASE_WORK_ITEM_SENSOR_DESCRIPTIONS: tuple[ + AzureDevOpsWorkItemSensorEntityDescription, ... +] = ( + AzureDevOpsWorkItemSensorEntityDescription( + key="work_item_count", + translation_key="work_item_count", + value_fn=lambda work_item_state: len(work_item_state.work_items), + ), +) + def parse_datetime(value: str | None) -> datetime | None: """Parse datetime string.""" @@ -134,7 +152,7 @@ async def async_setup_entry( coordinator = entry.runtime_data initial_builds: list[Build] = coordinator.data.builds - async_add_entities( + entities: list[SensorEntity] = [ AzureDevOpsBuildSensor( coordinator, description, @@ -143,8 +161,22 @@ async def async_setup_entry( for description in BASE_BUILD_SENSOR_DESCRIPTIONS for key, build in enumerate(initial_builds) if build.project and build.definition + ] + + entities.extend( + AzureDevOpsWorkItemSensor( + coordinator, + description, + key, + state_key, + ) + for description in BASE_WORK_ITEM_SENSOR_DESCRIPTIONS + for key, work_item_type_state in enumerate(coordinator.data.work_items) + for state_key, _ in enumerate(work_item_type_state.state_items) ) + async_add_entities(entities) + class AzureDevOpsBuildSensor(AzureDevOpsEntity, SensorEntity): """Define a Azure DevOps build sensor.""" @@ -162,8 +194,8 @@ def __init__( self.entity_description = description self.item_key = item_key self._attr_unique_id = ( - f"{self.coordinator.data.organization}_" - f"{self.build.project.id}_" + f"{coordinator.data.organization}_" + f"{coordinator.data.project.id}_" f"{self.build.definition.build_id}_" f"{description.key}" ) @@ -185,3 +217,48 @@ def native_value(self) -> datetime | StateType: def extra_state_attributes(self) -> Mapping[str, Any] | None: """Return the state attributes of the entity.""" return self.entity_description.attr_fn(self.build) + + +class AzureDevOpsWorkItemSensor(AzureDevOpsEntity, SensorEntity): + """Define a Azure DevOps work item sensor.""" + + entity_description: AzureDevOpsWorkItemSensorEntityDescription + + def __init__( + self, + coordinator: AzureDevOpsDataUpdateCoordinator, + description: AzureDevOpsWorkItemSensorEntityDescription, + wits_key: int, + state_key: int, + ) -> None: + """Initialize.""" + super().__init__(coordinator) + self.entity_description = description + self.wits_key = wits_key + self.state_key = state_key + self._attr_unique_id = ( + f"{coordinator.data.organization}_" + f"{coordinator.data.project.id}_" + f"{self.work_item_type.name}_" + f"{self.work_item_state.name}_" + f"{description.key}" + ) + self._attr_translation_placeholders = { + "item_type": self.work_item_type.name, + "item_state": self.work_item_state.name, + } + + @property + def work_item_type(self) -> WorkItemTypeAndState: + """Return the work item.""" + return self.coordinator.data.work_items[self.wits_key] + + @property + def work_item_state(self) -> WorkItemState: + """Return the work item state.""" + return self.work_item_type.state_items[self.state_key] + + @property + def native_value(self) -> datetime | StateType: + """Return the state.""" + return self.entity_description.value_fn(self.work_item_state) diff --git a/homeassistant/components/azure_devops/strings.json b/homeassistant/components/azure_devops/strings.json index 8a17169fb6b330..c530427039660a 100644 --- a/homeassistant/components/azure_devops/strings.json +++ b/homeassistant/components/azure_devops/strings.json @@ -60,6 +60,9 @@ }, "url": { "name": "{definition_name} latest build url" + }, + "work_item_count": { + "name": "{item_type} {item_state} work items" } } }, diff --git a/homeassistant/components/azure_event_hub/config_flow.py b/homeassistant/components/azure_event_hub/config_flow.py index 264daa683bc629..046851e6926bca 100644 --- a/homeassistant/components/azure_event_hub/config_flow.py +++ b/homeassistant/components/azure_event_hub/config_flow.py @@ -154,17 +154,15 @@ async def async_step_sas( options=self._options, ) - async def async_step_import( - self, import_config: dict[str, Any] - ) -> ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Import config from configuration.yaml.""" if self._async_current_entries(): return self.async_abort(reason="single_instance_allowed") - if CONF_SEND_INTERVAL in import_config: - self._options[CONF_SEND_INTERVAL] = import_config.pop(CONF_SEND_INTERVAL) - if CONF_MAX_DELAY in import_config: - self._options[CONF_MAX_DELAY] = import_config.pop(CONF_MAX_DELAY) - self._data = import_config + if CONF_SEND_INTERVAL in import_data: + self._options[CONF_SEND_INTERVAL] = import_data.pop(CONF_SEND_INTERVAL) + if CONF_MAX_DELAY in import_data: + self._options[CONF_MAX_DELAY] = import_data.pop(CONF_MAX_DELAY) + self._data = import_data errors = await validate_data(self._data) if errors: return self.async_abort(reason=errors["base"]) diff --git a/homeassistant/components/backup/icons.json b/homeassistant/components/backup/icons.json index cba4fb2283101d..bd5ff4a81eedb0 100644 --- a/homeassistant/components/backup/icons.json +++ b/homeassistant/components/backup/icons.json @@ -1,5 +1,7 @@ { "services": { - "create": "mdi:cloud-upload" + "create": { + "service": "mdi:cloud-upload" + } } } diff --git a/homeassistant/components/bayesian/icons.json b/homeassistant/components/bayesian/icons.json index a03163179cb5bd..a9829425570a0c 100644 --- a/homeassistant/components/bayesian/icons.json +++ b/homeassistant/components/bayesian/icons.json @@ -1,5 +1,7 @@ { "services": { - "reload": "mdi:reload" + "reload": { + "service": "mdi:reload" + } } } diff --git a/homeassistant/components/blackbird/icons.json b/homeassistant/components/blackbird/icons.json index f080fb5f857fa3..815a45ba17426c 100644 --- a/homeassistant/components/blackbird/icons.json +++ b/homeassistant/components/blackbird/icons.json @@ -1,5 +1,7 @@ { "services": { - "set_all_zones": "mdi:home-sound-in" + "set_all_zones": { + "service": "mdi:home-sound-in" + } } } diff --git a/homeassistant/components/blebox/config_flow.py b/homeassistant/components/blebox/config_flow.py index 1f04f06a05a15b..2221e35a81f81d 100644 --- a/homeassistant/components/blebox/config_flow.py +++ b/homeassistant/components/blebox/config_flow.py @@ -35,15 +35,11 @@ _LOGGER = logging.getLogger(__name__) -def host_port(data): - """Return a list with host and port.""" - return (data[CONF_HOST], data[CONF_PORT]) - - def create_schema(previous_input=None): """Create a schema with given values as default.""" if previous_input is not None: - host, port = host_port(previous_input) + host = previous_input[CONF_HOST] + port = previous_input[CONF_PORT] else: host = DEFAULT_HOST port = DEFAULT_PORT @@ -70,9 +66,9 @@ class BleBoxConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - def __init__(self): + def __init__(self) -> None: """Initialize the BleBox config flow.""" - self.device_config = {} + self.device_config: dict[str, Any] = {} def handle_step_exception( self, step, exception, schema, host, port, message_id, log_fn @@ -146,7 +142,9 @@ async def async_step_confirm_discovery( }, ) - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle initial user-triggered config step.""" hass = self.hass schema = create_schema(user_input) @@ -159,14 +157,14 @@ async def async_step_user(self, user_input=None): description_placeholders={}, ) - addr = host_port(user_input) + host = user_input[CONF_HOST] + port = user_input[CONF_PORT] username = user_input.get(CONF_USERNAME) password = user_input.get(CONF_PASSWORD) for entry in self._async_current_entries(): - if addr == host_port(entry.data): - host, port = addr + if host == entry.data[CONF_HOST] and port == entry.data[CONF_PORT]: return self.async_abort( reason=ADDRESS_ALREADY_CONFIGURED, description_placeholders={"address": f"{host}:{port}"}, @@ -174,27 +172,35 @@ async def async_step_user(self, user_input=None): websession = get_maybe_authenticated_session(hass, password, username) - api_host = ApiHost(*addr, DEFAULT_SETUP_TIMEOUT, websession, hass.loop, _LOGGER) + api_host = ApiHost( + host, port, DEFAULT_SETUP_TIMEOUT, websession, hass.loop, _LOGGER + ) try: product = await Box.async_from_host(api_host) except UnsupportedBoxVersion as ex: return self.handle_step_exception( - "user", ex, schema, *addr, UNSUPPORTED_VERSION, _LOGGER.debug + "user", + ex, + schema, + host, + port, + UNSUPPORTED_VERSION, + _LOGGER.debug, ) except UnauthorizedRequest as ex: return self.handle_step_exception( - "user", ex, schema, *addr, CANNOT_CONNECT, _LOGGER.error + "user", ex, schema, host, port, CANNOT_CONNECT, _LOGGER.error ) except Error as ex: return self.handle_step_exception( - "user", ex, schema, *addr, CANNOT_CONNECT, _LOGGER.warning + "user", ex, schema, host, port, CANNOT_CONNECT, _LOGGER.warning ) except RuntimeError as ex: return self.handle_step_exception( - "user", ex, schema, *addr, UNKNOWN, _LOGGER.error + "user", ex, schema, host, port, UNKNOWN, _LOGGER.error ) # Check if configured but IP changed since diff --git a/homeassistant/components/blink/icons.json b/homeassistant/components/blink/icons.json index 615a3c4c6dc8ec..bea67b25f6d672 100644 --- a/homeassistant/components/blink/icons.json +++ b/homeassistant/components/blink/icons.json @@ -12,10 +12,20 @@ } }, "services": { - "record": "mdi:video-box", - "trigger_camera": "mdi:image-refresh", - "save_video": "mdi:file-video", - "save_recent_clips": "mdi:file-video", - "send_pin": "mdi:two-factor-authentication" + "record": { + "service": "mdi:video-box" + }, + "trigger_camera": { + "service": "mdi:image-refresh" + }, + "save_video": { + "service": "mdi:file-video" + }, + "save_recent_clips": { + "service": "mdi:file-video" + }, + "send_pin": { + "service": "mdi:two-factor-authentication" + } } } diff --git a/homeassistant/components/bluesound/__init__.py b/homeassistant/components/bluesound/__init__.py index cbe95fc3abf8c9..da74ed042bef71 100644 --- a/homeassistant/components/bluesound/__init__.py +++ b/homeassistant/components/bluesound/__init__.py @@ -2,8 +2,8 @@ from dataclasses import dataclass -import aiohttp from pyblu import Player, SyncStatus +from pyblu.errors import PlayerUnreachableError from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_HOST, CONF_PORT, Platform @@ -22,14 +22,14 @@ @dataclass -class BluesoundData: +class BluesoundRuntimeData: """Bluesound data class.""" player: Player sync_status: SyncStatus -type BluesoundConfigEntry = ConfigEntry[BluesoundData] +type BluesoundConfigEntry = ConfigEntry[BluesoundRuntimeData] async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: @@ -51,14 +51,10 @@ async def async_setup_entry( async with Player(host, port, session=session, default_timeout=10) as player: try: sync_status = await player.sync_status(timeout=1) - except TimeoutError as ex: - raise ConfigEntryNotReady( - f"Timeout while connecting to {host}:{port}" - ) from ex - except aiohttp.ClientError as ex: + except PlayerUnreachableError as ex: raise ConfigEntryNotReady(f"Error connecting to {host}:{port}") from ex - config_entry.runtime_data = BluesoundData(player, sync_status) + config_entry.runtime_data = BluesoundRuntimeData(player, sync_status) await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS) diff --git a/homeassistant/components/bluesound/config_flow.py b/homeassistant/components/bluesound/config_flow.py index aae527187d2f3f..050b3ee4eacfbf 100644 --- a/homeassistant/components/bluesound/config_flow.py +++ b/homeassistant/components/bluesound/config_flow.py @@ -3,8 +3,8 @@ import logging from typing import Any -import aiohttp from pyblu import Player, SyncStatus +from pyblu.errors import PlayerUnreachableError import voluptuous as vol from homeassistant.components import zeroconf @@ -43,7 +43,7 @@ async def async_step_user( ) as player: try: sync_status = await player.sync_status(timeout=1) - except (TimeoutError, aiohttp.ClientError): + except PlayerUnreachableError: errors["base"] = "cannot_connect" else: await self.async_set_unique_id( @@ -79,7 +79,7 @@ async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResu ) as player: try: sync_status = await player.sync_status(timeout=1) - except (TimeoutError, aiohttp.ClientError): + except PlayerUnreachableError: return self.async_abort(reason="cannot_connect") await self.async_set_unique_id( @@ -105,7 +105,7 @@ async def async_step_zeroconf( discovery_info.host, self._port, session=session ) as player: sync_status = await player.sync_status(timeout=1) - except (TimeoutError, aiohttp.ClientError): + except PlayerUnreachableError: return self.async_abort(reason="cannot_connect") await self.async_set_unique_id(format_unique_id(sync_status.mac, self._port)) @@ -127,7 +127,9 @@ async def async_step_zeroconf( ) return await self.async_step_confirm() - async def async_step_confirm(self, user_input=None) -> ConfigFlowResult: + async def async_step_confirm( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Confirm the zeroconf setup.""" assert self._sync_status is not None assert self._host is not None diff --git a/homeassistant/components/bluesound/icons.json b/homeassistant/components/bluesound/icons.json index 8c886f12dfd295..2c5e95291c1b46 100644 --- a/homeassistant/components/bluesound/icons.json +++ b/homeassistant/components/bluesound/icons.json @@ -1,8 +1,16 @@ { "services": { - "join": "mdi:link-variant", - "unjoin": "mdi:link-variant-off", - "set_sleep_timer": "mdi:sleep", - "clear_sleep_timer": "mdi:sleep-off" + "join": { + "service": "mdi:link-variant" + }, + "unjoin": { + "service": "mdi:link-variant-off" + }, + "set_sleep_timer": { + "service": "mdi:sleep" + }, + "clear_sleep_timer": { + "service": "mdi:sleep-off" + } } } diff --git a/homeassistant/components/bluesound/manifest.json b/homeassistant/components/bluesound/manifest.json index 64b8e8abffc838..13514f528930b6 100644 --- a/homeassistant/components/bluesound/manifest.json +++ b/homeassistant/components/bluesound/manifest.json @@ -6,7 +6,7 @@ "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/bluesound", "iot_class": "local_polling", - "requirements": ["pyblu==0.4.0"], + "requirements": ["pyblu==1.0.1"], "zeroconf": [ { "type": "_musc._tcp.local." diff --git a/homeassistant/components/bluesound/media_player.py b/homeassistant/components/bluesound/media_player.py index 92f47977ee5600..cd1d9510eaae8b 100644 --- a/homeassistant/components/bluesound/media_player.py +++ b/homeassistant/components/bluesound/media_player.py @@ -9,8 +9,8 @@ import logging from typing import TYPE_CHECKING, Any, NamedTuple -from aiohttp.client_exceptions import ClientError from pyblu import Input, Player, Preset, Status, SyncStatus +from pyblu.errors import PlayerUnreachableError import voluptuous as vol from homeassistant.components import media_source @@ -239,7 +239,7 @@ def __init__( self.port = port self._polling_task: Task[None] | None = None # The actual polling task. self._id = sync_status.id - self._last_status_update = None + self._last_status_update: datetime | None = None self._sync_status = sync_status self._status: Status | None = None self._inputs: list[Input] = [] @@ -247,7 +247,7 @@ def __init__( self._muted = False self._master: BluesoundPlayer | None = None self._is_master = False - self._group_name = None + self._group_name: str | None = None self._group_list: list[str] = [] self._bluesound_device_name = sync_status.name self._player = player @@ -273,14 +273,6 @@ def __init__( via_device=(DOMAIN, format_mac(sync_status.mac)), ) - @staticmethod - def _try_get_index(string, search_string): - """Get the index.""" - try: - return string.index(search_string) - except ValueError: - return -1 - async def force_update_sync_status(self) -> bool: """Update the internal status.""" sync_status = await self._player.sync_status() @@ -309,12 +301,12 @@ async def force_update_sync_status(self) -> bool: return True - async def _start_poll_command(self): + async def _poll_loop(self) -> None: """Loop which polls the status of the player.""" while True: try: await self.async_update_status() - except (TimeoutError, ClientError): + except PlayerUnreachableError: _LOGGER.error( "Node %s:%s is offline, retrying later", self.host, self.port ) @@ -324,9 +316,9 @@ async def _start_poll_command(self): "Stopping the polling of node %s:%s", self.host, self.port ) return - except Exception: + except: # noqa: E722 - this loop should never stop _LOGGER.exception( - "Unexpected error in %s:%s, retrying later", self.host, self.port + "Unexpected error for %s:%s, retrying later", self.host, self.port ) await asyncio.sleep(NODE_OFFLINE_CHECK_TIMEOUT) @@ -335,7 +327,7 @@ async def async_added_to_hass(self) -> None: await super().async_added_to_hass() self._polling_task = self.hass.async_create_background_task( - self._start_poll_command(), + self._poll_loop(), name=f"bluesound.polling_{self.host}:{self.port}", ) @@ -345,7 +337,9 @@ async def async_will_remove_from_hass(self) -> None: assert self._polling_task is not None if self._polling_task.cancel(): - await self._polling_task + # the sleeps in _poll_loop will raise CancelledError + with suppress(CancelledError): + await self._polling_task self.hass.data[DATA_BLUESOUND].remove(self) @@ -354,12 +348,12 @@ async def async_update(self) -> None: if not self.available: return - with suppress(TimeoutError): + with suppress(PlayerUnreachableError): await self.async_update_sync_status() await self.async_update_presets() await self.async_update_captures() - async def async_update_status(self): + async def async_update_status(self) -> None: """Use the poll session to always get the status of the player.""" etag = None if self._status is not None: @@ -392,11 +386,11 @@ async def async_update_status(self): # the device is playing. This would solve a lot of # problems. This change will be done when the # communication is moved to a separate library - with suppress(TimeoutError): + with suppress(PlayerUnreachableError): await self.force_update_sync_status() self.async_write_ha_state() - except (TimeoutError, ClientError): + except PlayerUnreachableError: self._attr_available = False self._last_status_update = None self._status = None @@ -407,7 +401,7 @@ async def async_update_status(self): ) raise - async def async_trigger_sync_on_all(self): + async def async_trigger_sync_on_all(self) -> None: """Trigger sync status update on all devices.""" _LOGGER.debug("Trigger sync status on all devices") @@ -415,7 +409,7 @@ async def async_trigger_sync_on_all(self): await player.force_update_sync_status() @Throttle(SYNC_STATUS_INTERVAL) - async def async_update_sync_status(self): + async def async_update_sync_status(self) -> None: """Update sync status.""" await self.force_update_sync_status() @@ -504,8 +498,6 @@ def media_position(self) -> int | None: return None position = self._status.seconds - if position is None: - return None if mediastate == MediaPlayerState.PLAYING: position += (dt_util.utcnow() - self._last_status_update).total_seconds() @@ -522,7 +514,7 @@ def media_duration(self) -> int | None: if duration is None: return None - return duration + return int(duration) @property def media_position_updated_at(self) -> datetime | None: @@ -658,7 +650,7 @@ def shuffle(self) -> bool: return shuffle - async def async_join(self, master): + async def async_join(self, master: str) -> None: """Join the player to a group.""" master_device = [ device @@ -709,7 +701,7 @@ def rebuild_bluesound_group(self) -> list[str]: if entity.bluesound_device_name in device_group ] - async def async_unjoin(self): + async def async_unjoin(self) -> None: """Unjoin the player from a group.""" if self._master is None: return @@ -717,11 +709,11 @@ async def async_unjoin(self): _LOGGER.debug("Trying to unjoin player: %s", self.id) await self._master.async_remove_slave(self) - async def async_add_slave(self, slave_device: BluesoundPlayer): + async def async_add_slave(self, slave_device: BluesoundPlayer) -> None: """Add slave to master.""" await self._player.add_slave(slave_device.host, slave_device.port) - async def async_remove_slave(self, slave_device: BluesoundPlayer): + async def async_remove_slave(self, slave_device: BluesoundPlayer) -> None: """Remove slave to master.""" await self._player.remove_slave(slave_device.host, slave_device.port) @@ -729,7 +721,7 @@ async def async_increase_timer(self) -> int: """Increase sleep time on player.""" return await self._player.sleep_timer() - async def async_clear_timer(self): + async def async_clear_timer(self) -> None: """Clear sleep timer on player.""" sleep = 1 while sleep > 0: @@ -753,6 +745,9 @@ async def async_select_source(self, source: str) -> None: if preset.name == source: url = preset.url + if url is None: + raise ServiceValidationError(f"Source {source} not found") + await self._player.play_url(url) async def async_clear_playlist(self) -> None: @@ -824,20 +819,20 @@ async def async_play_media( async def async_volume_up(self) -> None: """Volume up the media player.""" if self.volume_level is None: - return None + return new_volume = self.volume_level + 0.01 new_volume = min(1, new_volume) - return await self.async_set_volume_level(new_volume) + await self.async_set_volume_level(new_volume) async def async_volume_down(self) -> None: """Volume down the media player.""" if self.volume_level is None: - return None + return new_volume = self.volume_level - 0.01 new_volume = max(0, new_volume) - return await self.async_set_volume_level(new_volume) + await self.async_set_volume_level(new_volume) async def async_set_volume_level(self, volume: float) -> None: """Send volume_up command to media player.""" diff --git a/homeassistant/components/bluetooth/manifest.json b/homeassistant/components/bluetooth/manifest.json index 0dbe1475b8d034..0d17be70e0b9cf 100644 --- a/homeassistant/components/bluetooth/manifest.json +++ b/homeassistant/components/bluetooth/manifest.json @@ -18,8 +18,8 @@ "bleak-retry-connector==3.5.0", "bluetooth-adapters==0.19.4", "bluetooth-auto-recovery==1.4.2", - "bluetooth-data-tools==1.19.4", - "dbus-fast==2.22.1", - "habluetooth==3.3.2" + "bluetooth-data-tools==1.20.0", + "dbus-fast==2.24.0", + "habluetooth==3.4.0" ] } diff --git a/homeassistant/components/bluetooth_tracker/icons.json b/homeassistant/components/bluetooth_tracker/icons.json index 650bf0b6d19f57..217f1240893477 100644 --- a/homeassistant/components/bluetooth_tracker/icons.json +++ b/homeassistant/components/bluetooth_tracker/icons.json @@ -1,5 +1,7 @@ { "services": { - "update": "mdi:update" + "update": { + "service": "mdi:update" + } } } diff --git a/homeassistant/components/bmw_connected_drive/coordinator.py b/homeassistant/components/bmw_connected_drive/coordinator.py index 6e0ed2ab6707f2..992e7dea6b263c 100644 --- a/homeassistant/components/bmw_connected_drive/coordinator.py +++ b/homeassistant/components/bmw_connected_drive/coordinator.py @@ -15,6 +15,7 @@ from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryAuthFailed from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed +from homeassistant.util.ssl import get_default_context from .const import CONF_GCID, CONF_READ_ONLY, CONF_REFRESH_TOKEN, DOMAIN, SCAN_INTERVALS @@ -33,6 +34,7 @@ def __init__(self, hass: HomeAssistant, *, entry: ConfigEntry) -> None: entry.data[CONF_PASSWORD], get_region_from_name(entry.data[CONF_REGION]), observer_position=GPSPosition(hass.config.latitude, hass.config.longitude), + verify=get_default_context(), ) self.read_only = entry.options[CONF_READ_ONLY] self._entry = entry diff --git a/homeassistant/components/bmw_connected_drive/manifest.json b/homeassistant/components/bmw_connected_drive/manifest.json index 304973b816f140..6bc9027ac19983 100644 --- a/homeassistant/components/bmw_connected_drive/manifest.json +++ b/homeassistant/components/bmw_connected_drive/manifest.json @@ -7,5 +7,5 @@ "iot_class": "cloud_polling", "loggers": ["bimmer_connected"], "quality_scale": "platinum", - "requirements": ["bimmer-connected[china]==0.16.1"] + "requirements": ["bimmer-connected[china]==0.16.3"] } diff --git a/homeassistant/components/bmw_connected_drive/strings.json b/homeassistant/components/bmw_connected_drive/strings.json index 8121ab6f65fa00..c59900ef4f9bb2 100644 --- a/homeassistant/components/bmw_connected_drive/strings.json +++ b/homeassistant/components/bmw_connected_drive/strings.json @@ -148,7 +148,8 @@ "cooling": "Cooling", "heating": "Heating", "inactive": "Inactive", - "standby": "Standby" + "standby": "Standby", + "ventilation": "Ventilation" } }, "front_left_current_pressure": { diff --git a/homeassistant/components/bond/icons.json b/homeassistant/components/bond/icons.json index 35743d20e654af..48b351b1c7600a 100644 --- a/homeassistant/components/bond/icons.json +++ b/homeassistant/components/bond/icons.json @@ -96,12 +96,26 @@ } }, "services": { - "set_fan_speed_tracked_state": "mdi:fan", - "set_switch_power_tracked_state": "mdi:toggle-switch-variant", - "set_light_power_tracked_state": "mdi:lightbulb", - "set_light_brightness_tracked_state": "mdi:lightbulb-on", - "start_increasing_brightness": "mdi:brightness-7", - "start_decreasing_brightness": "mdi:brightness-1", - "stop": "mdi:stop" + "set_fan_speed_tracked_state": { + "service": "mdi:fan" + }, + "set_switch_power_tracked_state": { + "service": "mdi:toggle-switch-variant" + }, + "set_light_power_tracked_state": { + "service": "mdi:lightbulb" + }, + "set_light_brightness_tracked_state": { + "service": "mdi:lightbulb-on" + }, + "start_increasing_brightness": { + "service": "mdi:brightness-7" + }, + "start_decreasing_brightness": { + "service": "mdi:brightness-1" + }, + "stop": { + "service": "mdi:stop" + } } } diff --git a/homeassistant/components/bring/icons.json b/homeassistant/components/bring/icons.json index 1c6c3bdeca06c7..6b79fab3c9453c 100644 --- a/homeassistant/components/bring/icons.json +++ b/homeassistant/components/bring/icons.json @@ -7,6 +7,8 @@ } }, "services": { - "send_message": "mdi:cellphone-message" + "send_message": { + "service": "mdi:cellphone-message" + } } } diff --git a/homeassistant/components/broadlink/config_flow.py b/homeassistant/components/broadlink/config_flow.py index 89d540a27fc2dd..c9b2fb46608535 100644 --- a/homeassistant/components/broadlink/config_flow.py +++ b/homeassistant/components/broadlink/config_flow.py @@ -37,11 +37,11 @@ class BroadlinkFlowHandler(ConfigFlow, domain=DOMAIN): VERSION = 1 - def __init__(self) -> None: - """Initialize the Broadlink flow.""" - self.device = None + device: blk.Device - async def async_set_device(self, device, raise_on_progress=True): + async def async_set_device( + self, device: blk.Device, raise_on_progress: bool = True + ) -> None: """Define a device for the config flow.""" if device.type not in DEVICE_TYPES: _LOGGER.error( @@ -90,7 +90,9 @@ async def async_step_dhcp( await self.async_set_device(device) return await self.async_step_auth() - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle a flow initiated by the user.""" errors = {} @@ -152,10 +154,10 @@ async def async_step_user(self, user_input=None): errors=errors, ) - async def async_step_auth(self): + async def async_step_auth(self) -> ConfigFlowResult: """Authenticate to the device.""" device = self.device - errors = {} + errors: dict[str, str] = {} try: await self.hass.async_add_executor_job(device.auth) @@ -205,7 +207,11 @@ async def async_step_auth(self): ) return self.async_show_form(step_id="auth", errors=errors) - async def async_step_reset(self, user_input=None, errors=None): + async def async_step_reset( + self, + user_input: dict[str, Any] | None = None, + errors: dict[str, str] | None = None, + ) -> ConfigFlowResult: """Guide the user to unlock the device manually. We are unable to authenticate because the device is locked. @@ -228,7 +234,9 @@ async def async_step_reset(self, user_input=None, errors=None): {CONF_HOST: device.host[0], CONF_TIMEOUT: device.timeout} ) - async def async_step_unlock(self, user_input=None): + async def async_step_unlock( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Unlock the device. The authentication succeeded, but the device is locked. @@ -282,10 +290,12 @@ async def async_step_unlock(self, user_input=None): }, ) - async def async_step_finish(self, user_input=None): + async def async_step_finish( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Choose a name for the device and create config entry.""" device = self.device - errors = {} + errors: dict[str, str] = {} # Abort reauthentication flow. self._abort_if_unique_id_configured( @@ -308,10 +318,10 @@ async def async_step_finish(self, user_input=None): step_id="finish", data_schema=vol.Schema(data_schema), errors=errors ) - async def async_step_import(self, import_info): + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Import a device.""" - self._async_abort_entries_match({CONF_HOST: import_info[CONF_HOST]}) - return await self.async_step_user(import_info) + self._async_abort_entries_match({CONF_HOST: import_data[CONF_HOST]}) + return await self.async_step_user(import_data) async def async_step_reauth( self, entry_data: Mapping[str, Any] diff --git a/homeassistant/components/browser/icons.json b/homeassistant/components/browser/icons.json index 7c971009fd7a87..680aaf14b86d82 100644 --- a/homeassistant/components/browser/icons.json +++ b/homeassistant/components/browser/icons.json @@ -1,5 +1,7 @@ { "services": { - "browse_url": "mdi:web" + "browse_url": { + "service": "mdi:web" + } } } diff --git a/homeassistant/components/bsblan/__init__.py b/homeassistant/components/bsblan/__init__.py index 113a582f403b6e..5ce90db5043693 100644 --- a/homeassistant/components/bsblan/__init__.py +++ b/homeassistant/components/bsblan/__init__.py @@ -22,7 +22,7 @@ @dataclasses.dataclass -class HomeAssistantBSBLANData: +class BSBLanData: """BSBLan data stored in the Home Assistant data object.""" coordinator: BSBLanUpdateCoordinator @@ -57,7 +57,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: info = await bsblan.info() static = await bsblan.static_values() - hass.data.setdefault(DOMAIN, {})[entry.entry_id] = HomeAssistantBSBLANData( + hass.data.setdefault(DOMAIN, {})[entry.entry_id] = BSBLanData( client=bsblan, coordinator=coordinator, device=device, diff --git a/homeassistant/components/bsblan/climate.py b/homeassistant/components/bsblan/climate.py index 4d6514251cbe44..ae7116143df9c3 100644 --- a/homeassistant/components/bsblan/climate.py +++ b/homeassistant/components/bsblan/climate.py @@ -4,7 +4,7 @@ from typing import Any -from bsblan import BSBLAN, BSBLANError, Device, Info, State, StaticState +from bsblan import BSBLANError from homeassistant.components.climate import ( ATTR_HVAC_MODE, @@ -21,15 +21,11 @@ from homeassistant.exceptions import HomeAssistantError, ServiceValidationError from homeassistant.helpers.device_registry import format_mac from homeassistant.helpers.entity_platform import AddEntitiesCallback -from homeassistant.helpers.update_coordinator import ( - CoordinatorEntity, - DataUpdateCoordinator, -) from homeassistant.util.enum import try_parse_enum -from . import HomeAssistantBSBLANData +from . import BSBLanData from .const import ATTR_TARGET_TEMPERATURE, DOMAIN -from .entity import BSBLANEntity +from .entity import BSBLanEntity PARALLEL_UPDATES = 1 @@ -51,24 +47,17 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """Set up BSBLAN device based on a config entry.""" - data: HomeAssistantBSBLANData = hass.data[DOMAIN][entry.entry_id] + data: BSBLanData = hass.data[DOMAIN][entry.entry_id] async_add_entities( [ BSBLANClimate( - data.coordinator, - data.client, - data.device, - data.info, - data.static, - entry, + data, ) ] ) -class BSBLANClimate( - BSBLANEntity, CoordinatorEntity[DataUpdateCoordinator[State]], ClimateEntity -): +class BSBLANClimate(BSBLanEntity, ClimateEntity): """Defines a BSBLAN climate device.""" _attr_has_entity_name = True @@ -80,30 +69,22 @@ class BSBLANClimate( | ClimateEntityFeature.TURN_OFF | ClimateEntityFeature.TURN_ON ) - _attr_preset_modes = PRESET_MODES - # Determine hvac modes + _attr_preset_modes = PRESET_MODES _attr_hvac_modes = HVAC_MODES _enable_turn_on_off_backwards_compatibility = False def __init__( self, - coordinator: DataUpdateCoordinator[State], - client: BSBLAN, - device: Device, - info: Info, - static: StaticState, - entry: ConfigEntry, + data: BSBLanData, ) -> None: """Initialize BSBLAN climate device.""" - super().__init__(client, device, info, static, entry) - CoordinatorEntity.__init__(self, coordinator) - self._attr_unique_id = f"{format_mac(device.MAC)}-climate" - - self._attr_min_temp = float(static.min_temp.value) - self._attr_max_temp = float(static.max_temp.value) - # check if self.coordinator.data.current_temperature.unit is "°C" or "°C" - if static.min_temp.unit in ("°C", "°C"): + super().__init__(data.coordinator, data) + self._attr_unique_id = f"{format_mac(data.device.MAC)}-climate" + + self._attr_min_temp = float(data.static.min_temp.value) + self._attr_max_temp = float(data.static.max_temp.value) + if data.static.min_temp.unit in ("°C", "°C"): self._attr_temperature_unit = UnitOfTemperature.CELSIUS else: self._attr_temperature_unit = UnitOfTemperature.FAHRENHEIT @@ -111,30 +92,30 @@ def __init__( @property def current_temperature(self) -> float | None: """Return the current temperature.""" - if self.coordinator.data.current_temperature.value == "---": + if self.coordinator.data.state.current_temperature.value == "---": # device returns no current temperature return None - return float(self.coordinator.data.current_temperature.value) + return float(self.coordinator.data.state.current_temperature.value) @property def target_temperature(self) -> float | None: """Return the temperature we try to reach.""" - return float(self.coordinator.data.target_temperature.value) + return float(self.coordinator.data.state.target_temperature.value) @property def hvac_mode(self) -> HVACMode | None: """Return hvac operation ie. heat, cool mode.""" - if self.coordinator.data.hvac_mode.value == PRESET_ECO: + if self.coordinator.data.state.hvac_mode.value == PRESET_ECO: return HVACMode.AUTO - return try_parse_enum(HVACMode, self.coordinator.data.hvac_mode.value) + return try_parse_enum(HVACMode, self.coordinator.data.state.hvac_mode.value) @property def preset_mode(self) -> str | None: """Return the current preset mode.""" if ( self.hvac_mode == HVACMode.AUTO - and self.coordinator.data.hvac_mode.value == PRESET_ECO + and self.coordinator.data.state.hvac_mode.value == PRESET_ECO ): return PRESET_ECO return PRESET_NONE @@ -173,7 +154,7 @@ async def async_set_data(self, **kwargs: Any) -> None: else: data[ATTR_HVAC_MODE] = kwargs[ATTR_PRESET_MODE] try: - await self.client.thermostat(**data) + await self.coordinator.client.thermostat(**data) except BSBLANError as err: raise HomeAssistantError( "An error occurred while updating the BSBLAN device", diff --git a/homeassistant/components/bsblan/coordinator.py b/homeassistant/components/bsblan/coordinator.py index 864daacc562474..3320c0f75007b2 100644 --- a/homeassistant/components/bsblan/coordinator.py +++ b/homeassistant/components/bsblan/coordinator.py @@ -1,12 +1,10 @@ """DataUpdateCoordinator for the BSB-Lan integration.""" -from __future__ import annotations - +from dataclasses import dataclass from datetime import timedelta from random import randint -from bsblan import BSBLAN, BSBLANConnectionError -from bsblan.models import State +from bsblan import BSBLAN, BSBLANConnectionError, State from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_HOST @@ -16,7 +14,14 @@ from .const import DOMAIN, LOGGER, SCAN_INTERVAL -class BSBLanUpdateCoordinator(DataUpdateCoordinator[State]): +@dataclass +class BSBLanCoordinatorData: + """BSBLan data stored in the Home Assistant data object.""" + + state: State + + +class BSBLanUpdateCoordinator(DataUpdateCoordinator[BSBLanCoordinatorData]): """The BSB-Lan update coordinator.""" config_entry: ConfigEntry @@ -28,30 +33,32 @@ def __init__( client: BSBLAN, ) -> None: """Initialize the BSB-Lan coordinator.""" - - self.client = client - super().__init__( hass, - LOGGER, + logger=LOGGER, name=f"{DOMAIN}_{config_entry.data[CONF_HOST]}", - # use the default scan interval and add a random number of seconds to avoid timeouts when - # the BSB-Lan device is already/still busy retrieving data, - # e.g. for MQTT or internal logging. - update_interval=SCAN_INTERVAL + timedelta(seconds=randint(1, 8)), + update_interval=self._get_update_interval(), ) + self.client = client - async def _async_update_data(self) -> State: - """Get state from BSB-Lan device.""" + def _get_update_interval(self) -> timedelta: + """Get the update interval with a random offset. - # use the default scan interval and add a random number of seconds to avoid timeouts when - # the BSB-Lan device is already/still busy retrieving data, e.g. for MQTT or internal logging. - self.update_interval = SCAN_INTERVAL + timedelta(seconds=randint(1, 8)) + Use the default scan interval and add a random number of seconds to avoid timeouts when + the BSB-Lan device is already/still busy retrieving data, + e.g. for MQTT or internal logging. + """ + return SCAN_INTERVAL + timedelta(seconds=randint(1, 8)) + async def _async_update_data(self) -> BSBLanCoordinatorData: + """Get state and sensor data from BSB-Lan device.""" try: - return await self.client.state() + state = await self.client.state() except BSBLANConnectionError as err: + host = self.config_entry.data[CONF_HOST] if self.config_entry else "unknown" raise UpdateFailed( - f"Error while establishing connection with " - f"BSB-Lan device at {self.config_entry.data[CONF_HOST]}" + f"Error while establishing connection with BSB-Lan device at {host}" ) from err + + self.update_interval = self._get_update_interval() + return BSBLanCoordinatorData(state=state) diff --git a/homeassistant/components/bsblan/diagnostics.py b/homeassistant/components/bsblan/diagnostics.py index a24082fd698a2e..b4ff67f4fbfe97 100644 --- a/homeassistant/components/bsblan/diagnostics.py +++ b/homeassistant/components/bsblan/diagnostics.py @@ -7,7 +7,7 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant -from . import HomeAssistantBSBLANData +from . import BSBLanData from .const import DOMAIN @@ -15,9 +15,13 @@ async def async_get_config_entry_diagnostics( hass: HomeAssistant, entry: ConfigEntry ) -> dict[str, Any]: """Return diagnostics for a config entry.""" - data: HomeAssistantBSBLANData = hass.data[DOMAIN][entry.entry_id] + data: BSBLanData = hass.data[DOMAIN][entry.entry_id] + return { "info": data.info.to_dict(), "device": data.device.to_dict(), - "state": data.coordinator.data.to_dict(), + "coordinator_data": { + "state": data.coordinator.data.state.to_dict(), + }, + "static": data.static.to_dict(), } diff --git a/homeassistant/components/bsblan/entity.py b/homeassistant/components/bsblan/entity.py index a69c4d2217ef66..252c397f4f2638 100644 --- a/homeassistant/components/bsblan/entity.py +++ b/homeassistant/components/bsblan/entity.py @@ -1,41 +1,35 @@ -"""Base entity for the BSBLAN integration.""" +"""BSBLan base entity.""" from __future__ import annotations -from bsblan import BSBLAN, Device, Info, StaticState - -from homeassistant.config_entries import ConfigEntry -from homeassistant.const import CONF_HOST from homeassistant.helpers.device_registry import ( CONNECTION_NETWORK_MAC, DeviceInfo, format_mac, ) -from homeassistant.helpers.entity import Entity +from homeassistant.helpers.update_coordinator import CoordinatorEntity +from . import BSBLanData from .const import DOMAIN +from .coordinator import BSBLanUpdateCoordinator -class BSBLANEntity(Entity): - """Defines a BSBLAN entity.""" +class BSBLanEntity(CoordinatorEntity[BSBLanUpdateCoordinator]): + """Defines a base BSBLan entity.""" - def __init__( - self, - client: BSBLAN, - device: Device, - info: Info, - static: StaticState, - entry: ConfigEntry, - ) -> None: - """Initialize an BSBLAN entity.""" - self.client = client + _attr_has_entity_name = True + def __init__(self, coordinator: BSBLanUpdateCoordinator, data: BSBLanData) -> None: + """Initialize BSBLan entity.""" + super().__init__(coordinator, data) + host = coordinator.config_entry.data["host"] + mac = data.device.MAC self._attr_device_info = DeviceInfo( - connections={(CONNECTION_NETWORK_MAC, format_mac(device.MAC))}, - identifiers={(DOMAIN, format_mac(device.MAC))}, + identifiers={(DOMAIN, mac)}, + connections={(CONNECTION_NETWORK_MAC, format_mac(mac))}, + name=data.device.name, manufacturer="BSBLAN Inc.", - model=info.device_identification.value, - name=device.name, - sw_version=f"{device.version})", - configuration_url=f"http://{entry.data[CONF_HOST]}", + model=data.info.device_identification.value, + sw_version=data.device.version, + configuration_url=f"http://{host}", ) diff --git a/homeassistant/components/bthome/device_trigger.py b/homeassistant/components/bthome/device_trigger.py index c49664b1146b84..4eca110e5819ec 100644 --- a/homeassistant/components/bthome/device_trigger.py +++ b/homeassistant/components/bthome/device_trigger.py @@ -2,11 +2,14 @@ from __future__ import annotations -from typing import Any +from typing import TYPE_CHECKING, Any import voluptuous as vol from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA +from homeassistant.components.device_automation.exceptions import ( + InvalidDeviceAutomationConfig, +) from homeassistant.components.homeassistant.triggers import event as event_trigger from homeassistant.const import ( CONF_DEVICE_ID, @@ -31,7 +34,7 @@ EVENT_TYPE, ) -TRIGGERS_BY_EVENT_CLASS = { +EVENT_TYPES_BY_EVENT_CLASS = { EVENT_CLASS_BUTTON: { "press", "double_press", @@ -43,54 +46,71 @@ EVENT_CLASS_DIMMER: {"rotate_left", "rotate_right"}, } -SCHEMA_BY_EVENT_CLASS = { - EVENT_CLASS_BUTTON: DEVICE_TRIGGER_BASE_SCHEMA.extend( - { - vol.Required(CONF_TYPE): vol.In([EVENT_CLASS_BUTTON]), - vol.Required(CONF_SUBTYPE): vol.In( - TRIGGERS_BY_EVENT_CLASS[EVENT_CLASS_BUTTON] - ), - } - ), - EVENT_CLASS_DIMMER: DEVICE_TRIGGER_BASE_SCHEMA.extend( - { - vol.Required(CONF_TYPE): vol.In([EVENT_CLASS_DIMMER]), - vol.Required(CONF_SUBTYPE): vol.In( - TRIGGERS_BY_EVENT_CLASS[EVENT_CLASS_DIMMER] - ), - } - ), -} +TRIGGER_SCHEMA = DEVICE_TRIGGER_BASE_SCHEMA.extend( + {vol.Required(CONF_TYPE): str, vol.Required(CONF_SUBTYPE): str} +) + + +def get_event_classes_by_device_id(hass: HomeAssistant, device_id: str) -> list[str]: + """Get the supported event classes for a device. + + Events for BTHome BLE devices are dynamically discovered + and stored in the device config entry when they are first seen. + """ + device_registry = dr.async_get(hass) + device = device_registry.async_get(device_id) + if TYPE_CHECKING: + assert device is not None + + config_entries = [ + hass.config_entries.async_get_entry(entry_id) + for entry_id in device.config_entries + ] + bthome_config_entry = next( + entry for entry in config_entries if entry and entry.domain == DOMAIN + ) + return bthome_config_entry.data.get(CONF_DISCOVERED_EVENT_CLASSES, []) + + +def get_event_types_by_event_class(event_class: str) -> set[str]: + """Get the supported event types for an event class. + + If the device has multiple buttons they will have + event classes like button_1 button_2, button_3, etc + but if there is only one button then it will be + button without a number postfix. + """ + return EVENT_TYPES_BY_EVENT_CLASS.get(event_class.split("_")[0], set()) async def async_validate_trigger_config( hass: HomeAssistant, config: ConfigType ) -> ConfigType: """Validate trigger config.""" - return SCHEMA_BY_EVENT_CLASS.get(config[CONF_TYPE], DEVICE_TRIGGER_BASE_SCHEMA)( # type: ignore[no-any-return] - config - ) + config = TRIGGER_SCHEMA(config) + event_class = config[CONF_TYPE] + event_type = config[CONF_SUBTYPE] + device_id = config[CONF_DEVICE_ID] + event_classes = get_event_classes_by_device_id(hass, device_id) + + if event_class not in event_classes: + raise InvalidDeviceAutomationConfig( + f"BTHome trigger {event_class} is not valid for device_id '{device_id}'" + ) + + if event_type not in get_event_types_by_event_class(event_class): + raise InvalidDeviceAutomationConfig( + f"BTHome trigger {event_type} is not valid for device_id '{device_id}'" + ) + + return config async def async_get_triggers( hass: HomeAssistant, device_id: str ) -> list[dict[str, Any]]: """Return a list of triggers for BTHome BLE devices.""" - device_registry = dr.async_get(hass) - device = device_registry.async_get(device_id) - assert device is not None - config_entries = [ - hass.config_entries.async_get_entry(entry_id) - for entry_id in device.config_entries - ] - bthome_config_entry = next( - iter(entry for entry in config_entries if entry and entry.domain == DOMAIN), - None, - ) - assert bthome_config_entry is not None - event_classes: list[str] = bthome_config_entry.data.get( - CONF_DISCOVERED_EVENT_CLASSES, [] - ) + event_classes = get_event_classes_by_device_id(hass, device_id) return [ { # Required fields of TRIGGER_BASE_SCHEMA @@ -102,14 +122,7 @@ async def async_get_triggers( CONF_SUBTYPE: event_type, } for event_class in event_classes - for event_type in TRIGGERS_BY_EVENT_CLASS.get( - event_class.split("_")[0], - # If the device has multiple buttons they will have - # event classes like button_1 button_2, button_3, etc - # but if there is only one button then it will be - # button without a number postfix. - (), - ) + for event_type in get_event_types_by_event_class(event_class) ] diff --git a/homeassistant/components/bthome/manifest.json b/homeassistant/components/bthome/manifest.json index 42fbe79491836e..ad06f648d144ae 100644 --- a/homeassistant/components/bthome/manifest.json +++ b/homeassistant/components/bthome/manifest.json @@ -15,7 +15,7 @@ "service_data_uuid": "0000fcd2-0000-1000-8000-00805f9b34fb" } ], - "codeowners": ["@Ernst79"], + "codeowners": ["@Ernst79", "@thecode"], "config_flow": true, "dependencies": ["bluetooth_adapters"], "documentation": "https://www.home-assistant.io/integrations/bthome", diff --git a/homeassistant/components/bthome/sensor.py b/homeassistant/components/bthome/sensor.py index 656addad620ab6..64e6d61cefb8a9 100644 --- a/homeassistant/components/bthome/sensor.py +++ b/homeassistant/components/bthome/sensor.py @@ -27,6 +27,7 @@ PERCENTAGE, SIGNAL_STRENGTH_DECIBELS_MILLIWATT, EntityCategory, + UnitOfConductivity, UnitOfElectricCurrent, UnitOfElectricPotential, UnitOfEnergy, @@ -356,6 +357,16 @@ native_unit_of_measurement=UnitOfVolume.LITERS, state_class=SensorStateClass.TOTAL, ), + # Conductivity (µS/cm) + ( + BTHomeSensorDeviceClass.CONDUCTIVITY, + Units.CONDUCTIVITY, + ): SensorEntityDescription( + key=f"{BTHomeSensorDeviceClass.CONDUCTIVITY}_{Units.CONDUCTIVITY}", + device_class=SensorDeviceClass.CONDUCTIVITY, + native_unit_of_measurement=UnitOfConductivity.MICROSIEMENS, + state_class=SensorStateClass.MEASUREMENT, + ), } diff --git a/homeassistant/components/buienradar/const.py b/homeassistant/components/buienradar/const.py index c82970ed318837..fd92afd59b0cf0 100644 --- a/homeassistant/components/buienradar/const.py +++ b/homeassistant/components/buienradar/const.py @@ -2,6 +2,7 @@ DOMAIN = "buienradar" +DEFAULT_TIMEOUT = 60 DEFAULT_TIMEFRAME = 60 DEFAULT_DIMENSION = 700 diff --git a/homeassistant/components/buienradar/util.py b/homeassistant/components/buienradar/util.py index b641644cebe624..f089fce89b7405 100644 --- a/homeassistant/components/buienradar/util.py +++ b/homeassistant/components/buienradar/util.py @@ -1,9 +1,9 @@ """Shared utilities for different supported platforms.""" -from asyncio import timeout from datetime import datetime, timedelta from http import HTTPStatus import logging +from typing import Any import aiohttp from buienradar.buienradar import parse_data @@ -27,12 +27,12 @@ from buienradar.urls import JSON_FEED_URL, json_precipitation_forecast_url from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE -from homeassistant.core import CALLBACK_TYPE, callback +from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.event import async_track_point_in_utc_time from homeassistant.util import dt as dt_util -from .const import SCHEDULE_NOK, SCHEDULE_OK +from .const import DEFAULT_TIMEOUT, SCHEDULE_NOK, SCHEDULE_OK __all__ = ["BrData"] _LOGGER = logging.getLogger(__name__) @@ -59,10 +59,10 @@ class BrData: load_error_count: int = WARN_THRESHOLD rain_error_count: int = WARN_THRESHOLD - def __init__(self, hass, coordinates, timeframe, devices): + def __init__(self, hass: HomeAssistant, coordinates, timeframe, devices) -> None: """Initialize the data object.""" self.devices = devices - self.data = {} + self.data: dict[str, Any] | None = {} self.hass = hass self.coordinates = coordinates self.timeframe = timeframe @@ -93,9 +93,9 @@ async def get_data(self, url): resp = None try: websession = async_get_clientsession(self.hass) - async with timeout(10): - resp = await websession.get(url) - + async with websession.get( + url, timeout=aiohttp.ClientTimeout(total=DEFAULT_TIMEOUT) + ) as resp: result[STATUS_CODE] = resp.status result[CONTENT] = await resp.text() if resp.status == HTTPStatus.OK: diff --git a/homeassistant/components/buienradar/weather.py b/homeassistant/components/buienradar/weather.py index 02e1f444c9c1ad..2af66982fab640 100644 --- a/homeassistant/components/buienradar/weather.py +++ b/homeassistant/components/buienradar/weather.py @@ -130,7 +130,7 @@ class BrWeather(WeatherEntity): _attr_should_poll = False _attr_supported_features = WeatherEntityFeature.FORECAST_DAILY - def __init__(self, config, coordinates): + def __init__(self, config, coordinates) -> None: """Initialize the platform with a data instance and station name.""" self._stationname = config.get(CONF_NAME, "Buienradar") self._attr_name = self._stationname or f"BR {'(unknown station)'}" diff --git a/homeassistant/components/button/icons.json b/homeassistant/components/button/icons.json index 71956124d7f2c2..1364fb2d056fd4 100644 --- a/homeassistant/components/button/icons.json +++ b/homeassistant/components/button/icons.json @@ -14,6 +14,8 @@ } }, "services": { - "press": "mdi:gesture-tap-button" + "press": { + "service": "mdi:gesture-tap-button" + } } } diff --git a/homeassistant/components/calendar/icons.json b/homeassistant/components/calendar/icons.json index e4e526fe75c4bd..9b8df3ec6d3f0a 100644 --- a/homeassistant/components/calendar/icons.json +++ b/homeassistant/components/calendar/icons.json @@ -9,8 +9,14 @@ } }, "services": { - "create_event": "mdi:calendar-plus", - "get_events": "mdi:calendar-month", - "list_events": "mdi:calendar-month" + "create_event": { + "service": "mdi:calendar-plus" + }, + "get_events": { + "service": "mdi:calendar-month" + }, + "list_events": { + "service": "mdi:calendar-month" + } } } diff --git a/homeassistant/components/camera/icons.json b/homeassistant/components/camera/icons.json index 37e71c80a674ef..982074cd55345c 100644 --- a/homeassistant/components/camera/icons.json +++ b/homeassistant/components/camera/icons.json @@ -8,12 +8,26 @@ } }, "services": { - "disable_motion_detection": "mdi:motion-sensor-off", - "enable_motion_detection": "mdi:motion-sensor", - "play_stream": "mdi:play", - "record": "mdi:record-rec", - "snapshot": "mdi:camera", - "turn_off": "mdi:video-off", - "turn_on": "mdi:video" + "disable_motion_detection": { + "service": "mdi:motion-sensor-off" + }, + "enable_motion_detection": { + "service": "mdi:motion-sensor" + }, + "play_stream": { + "service": "mdi:play" + }, + "record": { + "service": "mdi:record-rec" + }, + "snapshot": { + "service": "mdi:camera" + }, + "turn_off": { + "service": "mdi:video-off" + }, + "turn_on": { + "service": "mdi:video" + } } } diff --git a/homeassistant/components/camera/manifest.json b/homeassistant/components/camera/manifest.json index b1df158a260904..9c56d97f910f7d 100644 --- a/homeassistant/components/camera/manifest.json +++ b/homeassistant/components/camera/manifest.json @@ -7,5 +7,5 @@ "documentation": "https://www.home-assistant.io/integrations/camera", "integration_type": "entity", "quality_scale": "internal", - "requirements": ["PyTurboJPEG==1.7.1"] + "requirements": ["PyTurboJPEG==1.7.5"] } diff --git a/homeassistant/components/canary/config_flow.py b/homeassistant/components/canary/config_flow.py index 6ae7632a7e2359..5af7142af8fa2b 100644 --- a/homeassistant/components/canary/config_flow.py +++ b/homeassistant/components/canary/config_flow.py @@ -54,11 +54,9 @@ def async_get_options_flow(config_entry: ConfigEntry) -> OptionsFlow: """Get the options flow for this handler.""" return CanaryOptionsFlowHandler(config_entry) - async def async_step_import( - self, user_input: dict[str, Any] | None = None - ) -> ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Handle a flow initiated by configuration file.""" - return await self.async_step_user(user_input) + return await self.async_step_user(import_data) async def async_step_user( self, user_input: dict[str, Any] | None = None diff --git a/homeassistant/components/cast/config_flow.py b/homeassistant/components/cast/config_flow.py index 6ccd7be19c3926..4f7dd59e83ed88 100644 --- a/homeassistant/components/cast/config_flow.py +++ b/homeassistant/components/cast/config_flow.py @@ -29,11 +29,11 @@ class FlowHandler(ConfigFlow, domain=DOMAIN): VERSION = 1 - def __init__(self): + def __init__(self) -> None: """Initialize flow.""" - self._ignore_cec = set() - self._known_hosts = set() - self._wanted_uuid = set() + self._ignore_cec = set[str]() + self._known_hosts = set[str]() + self._wanted_uuid = set[str]() @staticmethod @callback @@ -43,7 +43,9 @@ def async_get_options_flow( """Get the options flow for this handler.""" return CastOptionsFlowHandler(config_entry) - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle a flow initialized by the user.""" if self._async_current_entries(): return self.async_abort(reason="single_instance_allowed") @@ -61,7 +63,9 @@ async def async_step_zeroconf( return await self.async_step_confirm() - async def async_step_config(self, user_input=None): + async def async_step_config( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Confirm the setup.""" errors = {} data = {CONF_KNOWN_HOSTS: self._known_hosts} @@ -88,7 +92,9 @@ async def async_step_config(self, user_input=None): step_id="config", data_schema=vol.Schema(fields), errors=errors ) - async def async_step_confirm(self, user_input=None): + async def async_step_confirm( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Confirm the setup.""" data = self._get_data() @@ -114,13 +120,15 @@ def __init__(self, config_entry: ConfigEntry) -> None: self.config_entry = config_entry self.updated_config: dict[str, Any] = {} - async def async_step_init(self, user_input=None): + async def async_step_init(self, user_input: None = None) -> ConfigFlowResult: """Manage the Google Cast options.""" return await self.async_step_basic_options() - async def async_step_basic_options(self, user_input=None): + async def async_step_basic_options( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Manage the Google Cast options.""" - errors = {} + errors: dict[str, str] = {} current_config = self.config_entry.data if user_input is not None: bad_hosts, known_hosts = _string_to_list( @@ -137,9 +145,9 @@ async def async_step_basic_options(self, user_input=None): self.hass.config_entries.async_update_entry( self.config_entry, data=self.updated_config ) - return self.async_create_entry(title="", data=None) + return self.async_create_entry(title="", data={}) - fields = {} + fields: dict[vol.Marker, type[str]] = {} suggested_value = _list_to_string(current_config.get(CONF_KNOWN_HOSTS)) _add_with_suggestion(fields, CONF_KNOWN_HOSTS, suggested_value) @@ -150,9 +158,11 @@ async def async_step_basic_options(self, user_input=None): last_step=not self.show_advanced_options, ) - async def async_step_advanced_options(self, user_input=None): + async def async_step_advanced_options( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Manage the Google Cast options.""" - errors = {} + errors: dict[str, str] = {} if user_input is not None: bad_cec, ignore_cec = _string_to_list( user_input.get(CONF_IGNORE_CEC, ""), IGNORE_CEC_SCHEMA @@ -167,9 +177,9 @@ async def async_step_advanced_options(self, user_input=None): self.hass.config_entries.async_update_entry( self.config_entry, data=self.updated_config ) - return self.async_create_entry(title="", data=None) + return self.async_create_entry(title="", data={}) - fields = {} + fields: dict[vol.Marker, type[str]] = {} current_config = self.config_entry.data suggested_value = _list_to_string(current_config.get(CONF_UUID)) _add_with_suggestion(fields, CONF_UUID, suggested_value) @@ -202,5 +212,7 @@ def _string_to_list(string, schema): return invalid, items -def _add_with_suggestion(fields, key, suggested_value): +def _add_with_suggestion( + fields: dict[vol.Marker, type[str]], key: str, suggested_value: str +) -> None: fields[vol.Optional(key, description={"suggested_value": suggested_value})] = str diff --git a/homeassistant/components/cast/icons.json b/homeassistant/components/cast/icons.json index e19ea0b07b23b8..a43411eaad31a2 100644 --- a/homeassistant/components/cast/icons.json +++ b/homeassistant/components/cast/icons.json @@ -1,5 +1,7 @@ { "services": { - "show_lovelace_view": "mdi:view-dashboard" + "show_lovelace_view": { + "service": "mdi:view-dashboard" + } } } diff --git a/homeassistant/components/cert_expiry/config_flow.py b/homeassistant/components/cert_expiry/config_flow.py index 8f937ef61ea71c..22d443c700dd64 100644 --- a/homeassistant/components/cert_expiry/config_flow.py +++ b/homeassistant/components/cert_expiry/config_flow.py @@ -95,12 +95,9 @@ async def async_step_user( errors=self._errors, ) - async def async_step_import( - self, - user_input: Mapping[str, Any] | None = None, - ) -> ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Import a config entry. Only host was required in the yaml file all other fields are optional """ - return await self.async_step_user(user_input) + return await self.async_step_user(import_data) diff --git a/homeassistant/components/channels/icons.json b/homeassistant/components/channels/icons.json index cbbda1ef6232c8..ad5504a5422ce7 100644 --- a/homeassistant/components/channels/icons.json +++ b/homeassistant/components/channels/icons.json @@ -1,7 +1,13 @@ { "services": { - "seek_forward": "mdi:skip-forward", - "seek_backward": "mdi:skip-backward", - "seek_by": "mdi:timer-check-outline" + "seek_forward": { + "service": "mdi:skip-forward" + }, + "seek_backward": { + "service": "mdi:skip-backward" + }, + "seek_by": { + "service": "mdi:timer-check-outline" + } } } diff --git a/homeassistant/components/climate/icons.json b/homeassistant/components/climate/icons.json index ea6c504ce2596a..c9a8d12d01be5f 100644 --- a/homeassistant/components/climate/icons.json +++ b/homeassistant/components/climate/icons.json @@ -56,15 +56,35 @@ } }, "services": { - "set_fan_mode": "mdi:fan", - "set_humidity": "mdi:water-percent", - "set_swing_mode": "mdi:arrow-oscillating", - "set_temperature": "mdi:thermometer", - "set_aux_heat": "mdi:radiator", - "set_preset_mode": "mdi:sofa", - "set_hvac_mode": "mdi:hvac", - "turn_on": "mdi:power-on", - "turn_off": "mdi:power-off", - "toggle": "mdi:toggle-switch" + "set_fan_mode": { + "service": "mdi:fan" + }, + "set_humidity": { + "service": "mdi:water-percent" + }, + "set_swing_mode": { + "service": "mdi:arrow-oscillating" + }, + "set_temperature": { + "service": "mdi:thermometer" + }, + "set_aux_heat": { + "service": "mdi:radiator" + }, + "set_preset_mode": { + "service": "mdi:sofa" + }, + "set_hvac_mode": { + "service": "mdi:hvac" + }, + "turn_on": { + "service": "mdi:power-on" + }, + "turn_off": { + "service": "mdi:power-off" + }, + "toggle": { + "service": "mdi:toggle-switch" + } } } diff --git a/homeassistant/components/cloud/icons.json b/homeassistant/components/cloud/icons.json index 06ee7eb2f197f4..32888fa75c74a9 100644 --- a/homeassistant/components/cloud/icons.json +++ b/homeassistant/components/cloud/icons.json @@ -1,6 +1,10 @@ { "services": { - "remote_connect": "mdi:cloud", - "remote_disconnect": "mdi:cloud-off" + "remote_connect": { + "service": "mdi:cloud" + }, + "remote_disconnect": { + "service": "mdi:cloud-off" + } } } diff --git a/homeassistant/components/cloud/tts.py b/homeassistant/components/cloud/tts.py index 8cf18c08314def..4dbee10fbaf5a5 100644 --- a/homeassistant/components/cloud/tts.py +++ b/homeassistant/components/cloud/tts.py @@ -221,7 +221,7 @@ class CloudProvider(Provider): def __init__(self, cloud: Cloud[CloudClient]) -> None: """Initialize cloud provider.""" self.cloud = cloud - self.name = "Cloud" + self.name = "Home Assistant Cloud" self._language, self._voice = cloud.client.prefs.tts_default_voice cloud.client.prefs.async_listen_updates(self._sync_prefs) diff --git a/homeassistant/components/cloudflare/icons.json b/homeassistant/components/cloudflare/icons.json index 6bf6d773fc3dc6..2d452716c94d92 100644 --- a/homeassistant/components/cloudflare/icons.json +++ b/homeassistant/components/cloudflare/icons.json @@ -1,5 +1,7 @@ { "services": { - "update_records": "mdi:dns" + "update_records": { + "service": "mdi:dns" + } } } diff --git a/homeassistant/components/co2signal/config_flow.py b/homeassistant/components/co2signal/config_flow.py index bf5d645638f3ba..3313d01be85eb8 100644 --- a/homeassistant/components/co2signal/config_flow.py +++ b/homeassistant/components/co2signal/config_flow.py @@ -131,16 +131,23 @@ async def async_step_reauth( self._reauth_entry = self.hass.config_entries.async_get_entry( self.context["entry_id"] ) + return await self.async_step_reauth_confirm() + async def async_step_reauth_confirm( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle the reauth step.""" data_schema = vol.Schema( { vol.Required(CONF_API_KEY): cv.string, } ) - return await self._validate_and_create("reauth", data_schema, entry_data) + return await self._validate_and_create( + "reauth_confirm", data_schema, user_input + ) async def _validate_and_create( - self, step_id: str, data_schema: vol.Schema, data: Mapping[str, Any] + self, step_id: str, data_schema: vol.Schema, data: Mapping[str, Any] | None ) -> ConfigFlowResult: """Validate data and show form if it is invalid.""" errors: dict[str, str] = {} diff --git a/homeassistant/components/co2signal/strings.json b/homeassistant/components/co2signal/strings.json index 7444cde73d759b..a4ec916bd42829 100644 --- a/homeassistant/components/co2signal/strings.json +++ b/homeassistant/components/co2signal/strings.json @@ -19,7 +19,7 @@ "country_code": "Country code" } }, - "reauth": { + "reauth_confirm": { "data": { "api_key": "[%key:common::config_flow::data::access_token%]" } diff --git a/homeassistant/components/color_extractor/icons.json b/homeassistant/components/color_extractor/icons.json index 07b449ffc5423b..9dab17a9f3bc08 100644 --- a/homeassistant/components/color_extractor/icons.json +++ b/homeassistant/components/color_extractor/icons.json @@ -1,5 +1,7 @@ { "services": { - "turn_on": "mdi:lightbulb-on" + "turn_on": { + "service": "mdi:lightbulb-on" + } } } diff --git a/homeassistant/components/command_line/icons.json b/homeassistant/components/command_line/icons.json index a03163179cb5bd..a9829425570a0c 100644 --- a/homeassistant/components/command_line/icons.json +++ b/homeassistant/components/command_line/icons.json @@ -1,5 +1,7 @@ { "services": { - "reload": "mdi:reload" + "reload": { + "service": "mdi:reload" + } } } diff --git a/homeassistant/components/control4/config_flow.py b/homeassistant/components/control4/config_flow.py index f6eb410cbf226c..77ae2c98c7d9d2 100644 --- a/homeassistant/components/control4/config_flow.py +++ b/homeassistant/components/control4/config_flow.py @@ -3,6 +3,7 @@ from __future__ import annotations import logging +from typing import TYPE_CHECKING, Any from aiohttp.client_exceptions import ClientError from pyControl4.account import C4Account @@ -10,14 +11,19 @@ from pyControl4.error_handling import NotFound, Unauthorized import voluptuous as vol -from homeassistant.config_entries import ConfigEntry, ConfigFlow, OptionsFlow +from homeassistant.config_entries import ( + ConfigEntry, + ConfigFlow, + ConfigFlowResult, + OptionsFlow, +) from homeassistant.const import ( CONF_HOST, CONF_PASSWORD, CONF_SCAN_INTERVAL, CONF_USERNAME, ) -from homeassistant.core import callback +from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import aiohttp_client, config_validation as cv from homeassistant.helpers.device_registry import format_mac @@ -43,7 +49,9 @@ class Control4Validator: """Validates that config details can be used to authenticate and communicate with Control4.""" - def __init__(self, host, username, password, hass): + def __init__( + self, host: str, username: str, password: str, hass: HomeAssistant + ) -> None: """Initialize.""" self.host = host self.username = username @@ -93,7 +101,9 @@ class Control4ConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" errors = {} if user_input is not None: @@ -118,6 +128,8 @@ async def async_step_user(self, user_input=None): if not errors: controller_unique_id = hub.controller_unique_id + if TYPE_CHECKING: + assert hub.controller_unique_id mac = (controller_unique_id.split("_", 3))[2] formatted_mac = format_mac(mac) await self.async_set_unique_id(formatted_mac) @@ -152,7 +164,9 @@ def __init__(self, config_entry: ConfigEntry) -> None: """Initialize options flow.""" self.config_entry = config_entry - async def async_step_init(self, user_input=None): + async def async_step_init( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle options flow.""" if user_input is not None: return self.async_create_entry(title="", data=user_input) diff --git a/homeassistant/components/control4/manifest.json b/homeassistant/components/control4/manifest.json index 765f0dce78ced4..3088ebf8672fe4 100644 --- a/homeassistant/components/control4/manifest.json +++ b/homeassistant/components/control4/manifest.json @@ -6,7 +6,7 @@ "documentation": "https://www.home-assistant.io/integrations/control4", "iot_class": "local_polling", "loggers": ["pyControl4"], - "requirements": ["pyControl4==1.1.0"], + "requirements": ["pyControl4==1.2.0"], "ssdp": [ { "st": "c4:director" diff --git a/homeassistant/components/conversation/icons.json b/homeassistant/components/conversation/icons.json index b39a1603b152ff..658783f9ae26f9 100644 --- a/homeassistant/components/conversation/icons.json +++ b/homeassistant/components/conversation/icons.json @@ -1,6 +1,10 @@ { "services": { - "process": "mdi:message-processing", - "reload": "mdi:reload" + "process": { + "service": "mdi:message-processing" + }, + "reload": { + "service": "mdi:reload" + } } } diff --git a/homeassistant/components/conversation/manifest.json b/homeassistant/components/conversation/manifest.json index d7a308b8b2bbfb..837ac9f9b1f2a1 100644 --- a/homeassistant/components/conversation/manifest.json +++ b/homeassistant/components/conversation/manifest.json @@ -6,5 +6,5 @@ "documentation": "https://www.home-assistant.io/integrations/conversation", "integration_type": "system", "quality_scale": "internal", - "requirements": ["hassil==1.7.4", "home-assistant-intents==2024.8.7"] + "requirements": ["hassil==1.7.4", "home-assistant-intents==2024.9.4"] } diff --git a/homeassistant/components/conversation/models.py b/homeassistant/components/conversation/models.py index 902b52483e0df1..724e520e6dfe56 100644 --- a/homeassistant/components/conversation/models.py +++ b/homeassistant/components/conversation/models.py @@ -23,11 +23,22 @@ class ConversationInput: """User input to be processed.""" text: str + """User spoken text.""" + context: Context + """Context of the request.""" + conversation_id: str | None + """Unique identifier for the conversation.""" + device_id: str | None + """Unique identifier for the device.""" + language: str + """Language of the request.""" + agent_id: str | None = None + """Agent to use for processing.""" @dataclass(slots=True) diff --git a/homeassistant/components/counter/icons.json b/homeassistant/components/counter/icons.json index 1e0ef54bbb782e..59cd0bb7121a3e 100644 --- a/homeassistant/components/counter/icons.json +++ b/homeassistant/components/counter/icons.json @@ -1,8 +1,16 @@ { "services": { - "decrement": "mdi:numeric-negative-1", - "increment": "mdi:numeric-positive-1", - "reset": "mdi:refresh", - "set_value": "mdi:counter" + "decrement": { + "service": "mdi:numeric-negative-1" + }, + "increment": { + "service": "mdi:numeric-positive-1" + }, + "reset": { + "service": "mdi:refresh" + }, + "set_value": { + "service": "mdi:counter" + } } } diff --git a/homeassistant/components/cover/icons.json b/homeassistant/components/cover/icons.json index f2edaaa0893b22..91775fe634dbec 100644 --- a/homeassistant/components/cover/icons.json +++ b/homeassistant/components/cover/icons.json @@ -78,15 +78,35 @@ } }, "services": { - "close_cover": "mdi:arrow-down-box", - "close_cover_tilt": "mdi:arrow-bottom-left", - "open_cover": "mdi:arrow-up-box", - "open_cover_tilt": "mdi:arrow-top-right", - "set_cover_position": "mdi:arrow-down-box", - "set_cover_tilt_position": "mdi:arrow-top-right", - "stop_cover": "mdi:stop", - "stop_cover_tilt": "mdi:stop", - "toggle": "mdi:arrow-up-down", - "toggle_cover_tilt": "mdi:arrow-top-right-bottom-left" + "close_cover": { + "service": "mdi:arrow-down-box" + }, + "close_cover_tilt": { + "service": "mdi:arrow-bottom-left" + }, + "open_cover": { + "service": "mdi:arrow-up-box" + }, + "open_cover_tilt": { + "service": "mdi:arrow-top-right" + }, + "set_cover_position": { + "service": "mdi:arrow-down-box" + }, + "set_cover_tilt_position": { + "service": "mdi:arrow-top-right" + }, + "stop_cover": { + "service": "mdi:stop" + }, + "stop_cover_tilt": { + "service": "mdi:stop" + }, + "toggle": { + "service": "mdi:arrow-up-down" + }, + "toggle_cover_tilt": { + "service": "mdi:arrow-top-right-bottom-left" + } } } diff --git a/homeassistant/components/daikin/manifest.json b/homeassistant/components/daikin/manifest.json index 0d93c0e25ad279..88c29a2043572e 100644 --- a/homeassistant/components/daikin/manifest.json +++ b/homeassistant/components/daikin/manifest.json @@ -6,6 +6,6 @@ "documentation": "https://www.home-assistant.io/integrations/daikin", "iot_class": "local_polling", "loggers": ["pydaikin"], - "requirements": ["pydaikin==2.13.4"], + "requirements": ["pydaikin==2.13.6"], "zeroconf": ["_dkapi._tcp.local."] } diff --git a/homeassistant/components/date/icons.json b/homeassistant/components/date/icons.json index 80ec26912854f9..b139b897210b22 100644 --- a/homeassistant/components/date/icons.json +++ b/homeassistant/components/date/icons.json @@ -5,6 +5,8 @@ } }, "services": { - "set_value": "mdi:calendar-edit" + "set_value": { + "service": "mdi:calendar-edit" + } } } diff --git a/homeassistant/components/datetime/icons.json b/homeassistant/components/datetime/icons.json index 563d03e2a8fdcc..d7e9fca8e5cf5c 100644 --- a/homeassistant/components/datetime/icons.json +++ b/homeassistant/components/datetime/icons.json @@ -5,6 +5,8 @@ } }, "services": { - "set_value": "mdi:calendar-edit" + "set_value": { + "service": "mdi:calendar-edit" + } } } diff --git a/homeassistant/components/deako/__init__.py b/homeassistant/components/deako/__init__.py new file mode 100644 index 00000000000000..fdcf09fad6074f --- /dev/null +++ b/homeassistant/components/deako/__init__.py @@ -0,0 +1,59 @@ +"""The deako integration.""" + +from __future__ import annotations + +import logging + +from pydeako.deako import Deako, DeviceListTimeout, FindDevicesTimeout +from pydeako.discover import DeakoDiscoverer + +from homeassistant.components import zeroconf +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import ConfigEntryNotReady + +_LOGGER: logging.Logger = logging.getLogger(__name__) + +PLATFORMS: list[Platform] = [Platform.LIGHT] + +type DeakoConfigEntry = ConfigEntry[Deako] + + +async def async_setup_entry(hass: HomeAssistant, entry: DeakoConfigEntry) -> bool: + """Set up deako.""" + _zc = await zeroconf.async_get_instance(hass) + discoverer = DeakoDiscoverer(_zc) + + connection = Deako(discoverer.get_address) + + await connection.connect() + try: + await connection.find_devices() + except DeviceListTimeout as exc: # device list never received + _LOGGER.warning("Device not responding to device list") + await connection.disconnect() + raise ConfigEntryNotReady(exc) from exc + except FindDevicesTimeout as exc: # total devices expected not received + _LOGGER.warning("Device not responding to device requests") + await connection.disconnect() + raise ConfigEntryNotReady(exc) from exc + + # If deako devices are advertising on mdns, we should be able to get at least one device + devices = connection.get_devices() + if len(devices) == 0: + await connection.disconnect() + raise ConfigEntryNotReady(devices) + + entry.runtime_data = connection + + await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) + + return True + + +async def async_unload_entry(hass: HomeAssistant, entry: DeakoConfigEntry) -> bool: + """Unload a config entry.""" + await entry.runtime_data.disconnect() + + return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) diff --git a/homeassistant/components/deako/config_flow.py b/homeassistant/components/deako/config_flow.py new file mode 100644 index 00000000000000..d0676fa81d93ab --- /dev/null +++ b/homeassistant/components/deako/config_flow.py @@ -0,0 +1,26 @@ +"""Config flow for deako.""" + +from pydeako.discover import DeakoDiscoverer, DevicesNotFoundException + +from homeassistant.components import zeroconf +from homeassistant.core import HomeAssistant +from homeassistant.helpers import config_entry_flow + +from .const import DOMAIN, NAME + + +async def _async_has_devices(hass: HomeAssistant) -> bool: + """Return if there are devices that can be discovered.""" + _zc = await zeroconf.async_get_instance(hass) + discoverer = DeakoDiscoverer(_zc) + + try: + await discoverer.get_address() + except DevicesNotFoundException: + return False + else: + # address exists, there's at least one device + return True + + +config_entry_flow.register_discovery_flow(DOMAIN, NAME, _async_has_devices) diff --git a/homeassistant/components/deako/const.py b/homeassistant/components/deako/const.py new file mode 100644 index 00000000000000..f6b688b9b07727 --- /dev/null +++ b/homeassistant/components/deako/const.py @@ -0,0 +1,5 @@ +"""Constants for Deako.""" + +# Base component constants +NAME = "Deako" +DOMAIN = "deako" diff --git a/homeassistant/components/deako/light.py b/homeassistant/components/deako/light.py new file mode 100644 index 00000000000000..c7ff8765402160 --- /dev/null +++ b/homeassistant/components/deako/light.py @@ -0,0 +1,96 @@ +"""Binary sensor platform for integration_blueprint.""" + +from typing import Any + +from pydeako.deako import Deako + +from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity +from homeassistant.core import HomeAssistant +from homeassistant.helpers.device_registry import DeviceInfo +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from . import DeakoConfigEntry +from .const import DOMAIN + +# Model names +MODEL_SMART = "smart" +MODEL_DIMMER = "dimmer" + + +async def async_setup_entry( + hass: HomeAssistant, + config: DeakoConfigEntry, + add_entities: AddEntitiesCallback, +) -> None: + """Configure the platform.""" + client = config.runtime_data + + add_entities([DeakoLightEntity(client, uuid) for uuid in client.get_devices()]) + + +class DeakoLightEntity(LightEntity): + """Deako LightEntity class.""" + + _attr_has_entity_name = True + _attr_name = None + _attr_is_on = False + _attr_available = True + + client: Deako + + def __init__(self, client: Deako, uuid: str) -> None: + """Save connection reference.""" + self.client = client + self._attr_unique_id = uuid + + dimmable = client.is_dimmable(uuid) + + model = MODEL_SMART + self._attr_color_mode = ColorMode.ONOFF + if dimmable: + model = MODEL_DIMMER + self._attr_color_mode = ColorMode.BRIGHTNESS + + self._attr_supported_color_modes = {self._attr_color_mode} + + self._attr_device_info = DeviceInfo( + identifiers={(DOMAIN, uuid)}, + name=client.get_name(uuid), + manufacturer="Deako", + model=model, + ) + + client.set_state_callback(uuid, self.on_update) + self.update() # set initial state + + def on_update(self) -> None: + """State update callback.""" + self.update() + self.schedule_update_ha_state() + + async def control_device(self, power: bool, dim: int | None = None) -> None: + """Control entity state via client.""" + assert self._attr_unique_id is not None + await self.client.control_device(self._attr_unique_id, power, dim) + + async def async_turn_on(self, **kwargs: Any) -> None: + """Turn on the light.""" + dim = None + if ATTR_BRIGHTNESS in kwargs: + dim = round(kwargs[ATTR_BRIGHTNESS] / 2.55, 0) + await self.control_device(True, dim) + + async def async_turn_off(self, **kwargs: Any) -> None: + """Turn off the device.""" + await self.control_device(False) + + def update(self) -> None: + """Call to update state.""" + assert self._attr_unique_id is not None + state = self.client.get_state(self._attr_unique_id) or {} + self._attr_is_on = bool(state.get("power", False)) + if ( + self._attr_supported_color_modes is not None + and ColorMode.BRIGHTNESS in self._attr_supported_color_modes + ): + self._attr_brightness = int(round(state.get("dim", 0) * 2.55)) diff --git a/homeassistant/components/deako/manifest.json b/homeassistant/components/deako/manifest.json new file mode 100644 index 00000000000000..e8f6f235107ec5 --- /dev/null +++ b/homeassistant/components/deako/manifest.json @@ -0,0 +1,13 @@ +{ + "domain": "deako", + "name": "Deako", + "codeowners": ["@sebirdman", "@balake", "@deakolights"], + "config_flow": true, + "dependencies": ["zeroconf"], + "documentation": "https://www.home-assistant.io/integrations/deako", + "iot_class": "local_polling", + "loggers": ["pydeako"], + "requirements": ["pydeako==0.4.0"], + "single_config_entry": true, + "zeroconf": ["_deako._tcp.local."] +} diff --git a/homeassistant/components/deako/strings.json b/homeassistant/components/deako/strings.json new file mode 100644 index 00000000000000..6bb292d74a9f3c --- /dev/null +++ b/homeassistant/components/deako/strings.json @@ -0,0 +1,13 @@ +{ + "config": { + "step": { + "confirm": { + "description": "Please confirm setting up the Deako integration" + } + }, + "abort": { + "single_instance_allowed": "[%key:common::config_flow::abort::single_instance_allowed%]", + "no_devices_found": "[%key:common::config_flow::abort::no_devices_found%]" + } + } +} diff --git a/homeassistant/components/debugpy/icons.json b/homeassistant/components/debugpy/icons.json index b3bb4dde23afd5..880863820596e8 100644 --- a/homeassistant/components/debugpy/icons.json +++ b/homeassistant/components/debugpy/icons.json @@ -1,5 +1,7 @@ { "services": { - "start": "mdi:play" + "start": { + "service": "mdi:play" + } } } diff --git a/homeassistant/components/deconz/icons.json b/homeassistant/components/deconz/icons.json index 5b22daee53f839..a7fb0859eec634 100644 --- a/homeassistant/components/deconz/icons.json +++ b/homeassistant/components/deconz/icons.json @@ -1,7 +1,13 @@ { "services": { - "configure": "mdi:cog", - "device_refresh": "mdi:refresh", - "remove_orphaned_entries": "mdi:bookmark-remove" + "configure": { + "service": "mdi:cog" + }, + "device_refresh": { + "service": "mdi:refresh" + }, + "remove_orphaned_entries": { + "service": "mdi:bookmark-remove" + } } } diff --git a/homeassistant/components/deconz/select.py b/homeassistant/components/deconz/select.py index dad3ba9d78d92e..7f3f8cca0609b2 100644 --- a/homeassistant/components/deconz/select.py +++ b/homeassistant/components/deconz/select.py @@ -3,6 +3,7 @@ from __future__ import annotations from pydeconz.models.event import EventType +from pydeconz.models.sensor.air_purifier import AirPurifier, AirPurifierFanMode from pydeconz.models.sensor.presence import ( Presence, PresenceConfigDeviceMode, @@ -36,6 +37,17 @@ async def async_setup_entry( hub = DeconzHub.get_hub(hass, config_entry) hub.entities[DOMAIN] = set() + @callback + def async_add_air_purifier_sensor(_: EventType, sensor_id: str) -> None: + """Add air purifier select entity from deCONZ.""" + sensor = hub.api.sensors.air_purifier[sensor_id] + async_add_entities([DeconzAirPurifierFanMode(sensor, hub)]) + + hub.register_platform_add_device_callback( + async_add_air_purifier_sensor, + hub.api.sensors.air_purifier, + ) + @callback def async_add_presence_sensor(_: EventType, sensor_id: str) -> None: """Add presence select entity from deCONZ.""" @@ -55,6 +67,39 @@ def async_add_presence_sensor(_: EventType, sensor_id: str) -> None: ) +class DeconzAirPurifierFanMode(DeconzDevice[AirPurifier], SelectEntity): + """Representation of a deCONZ air purifier fan mode entity.""" + + _name_suffix = "Fan Mode" + unique_id_suffix = "fan_mode" + _update_key = "mode" + + _attr_entity_category = EntityCategory.CONFIG + _attr_options = [ + AirPurifierFanMode.OFF.value, + AirPurifierFanMode.AUTO.value, + AirPurifierFanMode.SPEED_1.value, + AirPurifierFanMode.SPEED_2.value, + AirPurifierFanMode.SPEED_3.value, + AirPurifierFanMode.SPEED_4.value, + AirPurifierFanMode.SPEED_5.value, + ] + + TYPE = DOMAIN + + @property + def current_option(self) -> str: + """Return the selected entity option to represent the entity state.""" + return self._device.fan_mode.value + + async def async_select_option(self, option: str) -> None: + """Change the selected option.""" + await self.hub.api.sensors.air_purifier.set_config( + id=self._device.resource_id, + fan_mode=AirPurifierFanMode(option), + ) + + class DeconzPresenceDeviceModeSelect(DeconzDevice[Presence], SelectEntity): """Representation of a deCONZ presence device mode entity.""" diff --git a/homeassistant/components/demo/config_flow.py b/homeassistant/components/demo/config_flow.py index 468d9cb042b885..c866873732c5de 100644 --- a/homeassistant/components/demo/config_flow.py +++ b/homeassistant/components/demo/config_flow.py @@ -37,12 +37,12 @@ def async_get_options_flow( """Get the options flow for this handler.""" return OptionsFlowHandler(config_entry) - async def async_step_import(self, import_info: dict[str, Any]) -> ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Set the config entry up from yaml.""" if self._async_current_entries(): return self.async_abort(reason="single_instance_allowed") - return self.async_create_entry(title="Demo", data=import_info) + return self.async_create_entry(title="Demo", data=import_data) class OptionsFlowHandler(OptionsFlow): diff --git a/homeassistant/components/demo/icons.json b/homeassistant/components/demo/icons.json index d9e1d405490b6d..17425a6d11911f 100644 --- a/homeassistant/components/demo/icons.json +++ b/homeassistant/components/demo/icons.json @@ -75,6 +75,8 @@ } }, "services": { - "randomize_device_tracker_data": "mdi:dice-multiple" + "randomize_device_tracker_data": { + "service": "mdi:dice-multiple" + } } } diff --git a/homeassistant/components/denonavr/icons.json b/homeassistant/components/denonavr/icons.json index ec6bc0854f9008..33d7f1bd3d985b 100644 --- a/homeassistant/components/denonavr/icons.json +++ b/homeassistant/components/denonavr/icons.json @@ -1,7 +1,13 @@ { "services": { - "get_command": "mdi:console", - "set_dynamic_eq": "mdi:tune", - "update_audyssey": "mdi:waveform" + "get_command": { + "service": "mdi:console" + }, + "set_dynamic_eq": { + "service": "mdi:tune" + }, + "update_audyssey": { + "service": "mdi:waveform" + } } } diff --git a/homeassistant/components/device_tracker/icons.json b/homeassistant/components/device_tracker/icons.json index c89053701babe6..4e5b82576cf8b4 100644 --- a/homeassistant/components/device_tracker/icons.json +++ b/homeassistant/components/device_tracker/icons.json @@ -8,6 +8,8 @@ } }, "services": { - "see": "mdi:account-eye" + "see": { + "service": "mdi:account-eye" + } } } diff --git a/homeassistant/components/devolo_home_network/config_flow.py b/homeassistant/components/devolo_home_network/config_flow.py index 63d86d46e8a77e..fca724716938f2 100644 --- a/homeassistant/components/devolo_home_network/config_flow.py +++ b/homeassistant/components/devolo_home_network/config_flow.py @@ -112,10 +112,12 @@ async def async_step_zeroconf_confirm( description_placeholders={"host_name": title}, ) - async def async_step_reauth(self, data: Mapping[str, Any]) -> ConfigFlowResult: + async def async_step_reauth( + self, entry_data: Mapping[str, Any] + ) -> ConfigFlowResult: """Handle reauthentication.""" if entry := self.hass.config_entries.async_get_entry(self.context["entry_id"]): - self.context[CONF_HOST] = data[CONF_IP_ADDRESS] + self.context[CONF_HOST] = entry_data[CONF_IP_ADDRESS] self.context["title_placeholders"][PRODUCT] = ( entry.runtime_data.device.product ) diff --git a/homeassistant/components/dexcom/config_flow.py b/homeassistant/components/dexcom/config_flow.py index 19b35c2b03d1e2..c3ed43c8e9ad66 100644 --- a/homeassistant/components/dexcom/config_flow.py +++ b/homeassistant/components/dexcom/config_flow.py @@ -2,10 +2,17 @@ from __future__ import annotations +from typing import Any + from pydexcom import AccountError, Dexcom, SessionError import voluptuous as vol -from homeassistant.config_entries import ConfigEntry, ConfigFlow, OptionsFlow +from homeassistant.config_entries import ( + ConfigEntry, + ConfigFlow, + ConfigFlowResult, + OptionsFlow, +) from homeassistant.const import CONF_PASSWORD, CONF_UNIT_OF_MEASUREMENT, CONF_USERNAME from homeassistant.core import callback @@ -25,7 +32,9 @@ class DexcomConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" errors = {} if user_input is not None: @@ -70,7 +79,9 @@ def __init__(self, config_entry: ConfigEntry) -> None: """Initialize options flow.""" self.config_entry = config_entry - async def async_step_init(self, user_input=None): + async def async_step_init( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle options flow.""" if user_input is not None: return self.async_create_entry(title="", data=user_input) diff --git a/homeassistant/components/dhcp/__init__.py b/homeassistant/components/dhcp/__init__.py index e830de39f29241..0897729ec72b45 100644 --- a/homeassistant/components/dhcp/__init__.py +++ b/homeassistant/components/dhcp/__init__.py @@ -63,7 +63,6 @@ CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN) -FILTER = "udp and (port 67 or 68)" HOSTNAME: Final = "hostname" MAC_ADDRESS: Final = "macaddress" IP_ADDRESS: Final = "ip" diff --git a/homeassistant/components/dhcp/manifest.json b/homeassistant/components/dhcp/manifest.json index ff81540b0eab9a..6023e55faf31ce 100644 --- a/homeassistant/components/dhcp/manifest.json +++ b/homeassistant/components/dhcp/manifest.json @@ -16,6 +16,6 @@ "requirements": [ "aiodhcpwatcher==1.0.2", "aiodiscover==2.1.0", - "cached_ipaddress==0.3.0" + "cached-ipaddress==0.5.0" ] } diff --git a/homeassistant/components/directv/config_flow.py b/homeassistant/components/directv/config_flow.py index 7cdfd5c07c9e86..56d8f262d1c01b 100644 --- a/homeassistant/components/directv/config_flow.py +++ b/homeassistant/components/directv/config_flow.py @@ -40,9 +40,9 @@ class DirecTVConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - def __init__(self): + def __init__(self) -> None: """Set up the instance.""" - self.discovery_info = {} + self.discovery_info: dict[str, Any] = {} async def async_step_user( self, user_input: dict[str, Any] | None = None diff --git a/homeassistant/components/discovergy/config_flow.py b/homeassistant/components/discovergy/config_flow.py index 5e17f0764b732c..47a78ff430871c 100644 --- a/homeassistant/components/discovergy/config_flow.py +++ b/homeassistant/components/discovergy/config_flow.py @@ -70,8 +70,16 @@ async def async_step_reauth( self, entry_data: Mapping[str, Any] ) -> ConfigFlowResult: """Handle the initial step.""" - self._existing_entry = await self.async_set_unique_id(self.context["unique_id"]) - return await self._validate_and_save(entry_data, step_id="reauth") + self._existing_entry = self.hass.config_entries.async_get_entry( + self.context["entry_id"] + ) + return await self.async_step_reauth_confirm() + + async def async_step_reauth_confirm( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle the reauth step.""" + return await self._validate_and_save(user_input, step_id="reauth_confirm") async def _validate_and_save( self, user_input: Mapping[str, Any] | None = None, step_id: str = "user" diff --git a/homeassistant/components/discovergy/strings.json b/homeassistant/components/discovergy/strings.json index 34c21bc1cfecfd..9a91fa92dc44cc 100644 --- a/homeassistant/components/discovergy/strings.json +++ b/homeassistant/components/discovergy/strings.json @@ -7,7 +7,7 @@ "password": "[%key:common::config_flow::data::password%]" } }, - "reauth": { + "reauth_confirm": { "data": { "email": "[%key:common::config_flow::data::email%]", "password": "[%key:common::config_flow::data::password%]" diff --git a/homeassistant/components/dominos/icons.json b/homeassistant/components/dominos/icons.json index d88bfb2542f46f..ca33ac91dfd5e4 100644 --- a/homeassistant/components/dominos/icons.json +++ b/homeassistant/components/dominos/icons.json @@ -1,5 +1,7 @@ { "services": { - "order": "mdi:pizza" + "order": { + "service": "mdi:pizza" + } } } diff --git a/homeassistant/components/downloader/config_flow.py b/homeassistant/components/downloader/config_flow.py index e7191e055a6e00..61a7ba8fe52849 100644 --- a/homeassistant/components/downloader/config_flow.py +++ b/homeassistant/components/downloader/config_flow.py @@ -43,13 +43,13 @@ async def async_step_user( errors=errors, ) - async def async_step_import(self, user_input: dict[str, Any]) -> ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Handle a flow initiated by configuration file.""" try: - await self._validate_input(user_input) + await self._validate_input(import_data) except DirectoryDoesNotExist: return self.async_abort(reason="directory_does_not_exist") - return self.async_create_entry(title=DEFAULT_NAME, data=user_input) + return self.async_create_entry(title=DEFAULT_NAME, data=import_data) async def _validate_input(self, user_input: dict[str, Any]) -> None: """Validate the user input if the directory exists.""" diff --git a/homeassistant/components/downloader/icons.json b/homeassistant/components/downloader/icons.json index 2a78df93ca7af1..8f8b5bb2688d83 100644 --- a/homeassistant/components/downloader/icons.json +++ b/homeassistant/components/downloader/icons.json @@ -1,5 +1,7 @@ { "services": { - "download_file": "mdi:download" + "download_file": { + "service": "mdi:download" + } } } diff --git a/homeassistant/components/drop_connect/binary_sensor.py b/homeassistant/components/drop_connect/binary_sensor.py index 73e0e254607945..093c5bcbb8e135 100644 --- a/homeassistant/components/drop_connect/binary_sensor.py +++ b/homeassistant/components/drop_connect/binary_sensor.py @@ -17,6 +17,7 @@ from .const import ( CONF_DEVICE_TYPE, + DEV_ALERT, DEV_HUB, DEV_LEAK_DETECTOR, DEV_PROTECTION_VALVE, @@ -33,8 +34,10 @@ # Binary sensor type constants +ALERT_SENSOR = "alert_sensor" LEAK_DETECTED = "leak" PENDING_NOTIFICATION = "pending_notification" +POWER = "power" PUMP_STATUS = "pump" RESERVE_IN_USE = "reserve_in_use" SALT_LOW = "salt" @@ -74,10 +77,23 @@ class DROPBinarySensorEntityDescription(BinarySensorEntityDescription): translation_key=PUMP_STATUS, value_fn=lambda device: device.drop_api.pump_status(), ), + DROPBinarySensorEntityDescription( + key=ALERT_SENSOR, + translation_key=ALERT_SENSOR, + device_class=BinarySensorDeviceClass.PROBLEM, + value_fn=lambda device: device.drop_api.sensor_high(), + ), + DROPBinarySensorEntityDescription( + key=POWER, + translation_key=None, # Use name provided by binary sensor device class + device_class=BinarySensorDeviceClass.POWER, + value_fn=lambda device: device.drop_api.power(), + ), ] # Defines which binary sensors are used by each device type DEVICE_BINARY_SENSORS: dict[str, list[str]] = { + DEV_ALERT: [ALERT_SENSOR, POWER], DEV_HUB: [LEAK_DETECTED, PENDING_NOTIFICATION], DEV_LEAK_DETECTOR: [LEAK_DETECTED], DEV_PROTECTION_VALVE: [LEAK_DETECTED], diff --git a/homeassistant/components/drop_connect/const.py b/homeassistant/components/drop_connect/const.py index 38a8a57ea722eb..f1012f9652c1ef 100644 --- a/homeassistant/components/drop_connect/const.py +++ b/homeassistant/components/drop_connect/const.py @@ -11,6 +11,7 @@ CONF_DEVICE_OWNER_ID = "drop_device_owner_id" # Values for DROP device types +DEV_ALERT = "alrt" DEV_FILTER = "filt" DEV_HUB = "hub" DEV_LEAK_DETECTOR = "leak" diff --git a/homeassistant/components/drop_connect/sensor.py b/homeassistant/components/drop_connect/sensor.py index 0806737254e42d..ad123ee13c7db4 100644 --- a/homeassistant/components/drop_connect/sensor.py +++ b/homeassistant/components/drop_connect/sensor.py @@ -27,6 +27,7 @@ from .const import ( CONF_DEVICE_TYPE, + DEV_ALERT, DEV_FILTER, DEV_HUB, DEV_LEAK_DETECTOR, @@ -222,6 +223,7 @@ class DROPSensorEntityDescription(SensorEntityDescription): ], DEV_FILTER: [BATTERY, CURRENT_FLOW_RATE, CURRENT_SYSTEM_PRESSURE], DEV_LEAK_DETECTOR: [BATTERY, TEMPERATURE], + DEV_ALERT: [BATTERY, TEMPERATURE], DEV_PROTECTION_VALVE: [ BATTERY, CURRENT_FLOW_RATE, diff --git a/homeassistant/components/drop_connect/strings.json b/homeassistant/components/drop_connect/strings.json index 761d134bd184f5..93df4dc3310fc1 100644 --- a/homeassistant/components/drop_connect/strings.json +++ b/homeassistant/components/drop_connect/strings.json @@ -12,26 +12,27 @@ }, "entity": { "sensor": { - "current_flow_rate": { "name": "Water flow rate" }, - "peak_flow_rate": { "name": "Peak water flow rate today" }, - "water_used_today": { "name": "Total water used today" }, "average_water_used": { "name": "Average daily water usage" }, "capacity_remaining": { "name": "Capacity remaining" }, + "cart1": { "name": "Cartridge 1 life remaining" }, + "cart2": { "name": "Cartridge 2 life remaining" }, + "cart3": { "name": "Cartridge 3 life remaining" }, + "current_flow_rate": { "name": "Water flow rate" }, "current_system_pressure": { "name": "Current water pressure" }, "high_system_pressure": { "name": "High water pressure today" }, - "low_system_pressure": { "name": "Low water pressure today" }, "inlet_tds": { "name": "Inlet TDS" }, + "low_system_pressure": { "name": "Low water pressure today" }, "outlet_tds": { "name": "Outlet TDS" }, - "cart1": { "name": "Cartridge 1 life remaining" }, - "cart2": { "name": "Cartridge 2 life remaining" }, - "cart3": { "name": "Cartridge 3 life remaining" } + "peak_flow_rate": { "name": "Peak water flow rate today" }, + "water_used_today": { "name": "Total water used today" } }, "binary_sensor": { + "alert_sensor": { "name": "Sensor" }, "leak": { "name": "Leak detected" }, "pending_notification": { "name": "Notification unread" }, + "pump": { "name": "Pump status" }, "reserve_in_use": { "name": "Reserve capacity in use" }, - "salt": { "name": "Salt low" }, - "pump": { "name": "Pump status" } + "salt": { "name": "Salt low" } }, "select": { "protect_mode": { @@ -44,8 +45,8 @@ } }, "switch": { - "water": { "name": "Water supply" }, - "bypass": { "name": "Treatment bypass" } + "bypass": { "name": "Treatment bypass" }, + "water": { "name": "Water supply" } } } } diff --git a/homeassistant/components/duckdns/icons.json b/homeassistant/components/duckdns/icons.json index 79ec18d13ffaec..c5d0b5329dc814 100644 --- a/homeassistant/components/duckdns/icons.json +++ b/homeassistant/components/duckdns/icons.json @@ -1,5 +1,7 @@ { "services": { - "set_txt": "mdi:text-box-edit-outline" + "set_txt": { + "service": "mdi:text-box-edit-outline" + } } } diff --git a/homeassistant/components/dynalite/config_flow.py b/homeassistant/components/dynalite/config_flow.py index 3ae4828b668b15..928f7043a4985c 100644 --- a/homeassistant/components/dynalite/config_flow.py +++ b/homeassistant/components/dynalite/config_flow.py @@ -26,9 +26,9 @@ def __init__(self) -> None: """Initialize the Dynalite flow.""" self.host = None - async def async_step_import(self, import_info: dict[str, Any]) -> ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Import a new bridge as a config entry.""" - LOGGER.debug("Starting async_step_import (deprecated) - %s", import_info) + LOGGER.debug("Starting async_step_import (deprecated) - %s", import_data) # Raise an issue that this is deprecated and has been imported async_create_issue( self.hass, @@ -46,17 +46,17 @@ async def async_step_import(self, import_info: dict[str, Any]) -> ConfigFlowResu }, ) - host = import_info[CONF_HOST] + host = import_data[CONF_HOST] # Check if host already exists for entry in self._async_current_entries(): if entry.data[CONF_HOST] == host: self.hass.config_entries.async_update_entry( - entry, data=dict(import_info) + entry, data=dict(import_data) ) return self.async_abort(reason="already_configured") # New entry - return await self._try_create(import_info) + return await self._try_create(import_data) async def async_step_user( self, user_input: dict[str, Any] | None = None diff --git a/homeassistant/components/dynalite/icons.json b/homeassistant/components/dynalite/icons.json index dedbb1be3acc71..27949197b538d0 100644 --- a/homeassistant/components/dynalite/icons.json +++ b/homeassistant/components/dynalite/icons.json @@ -1,6 +1,10 @@ { "services": { - "request_area_preset": "mdi:texture-box", - "request_channel_level": "mdi:satellite-uplink" + "request_area_preset": { + "service": "mdi:texture-box" + }, + "request_channel_level": { + "service": "mdi:satellite-uplink" + } } } diff --git a/homeassistant/components/eafm/config_flow.py b/homeassistant/components/eafm/config_flow.py index 0345d2acf94674..6be1066575f25d 100644 --- a/homeassistant/components/eafm/config_flow.py +++ b/homeassistant/components/eafm/config_flow.py @@ -1,9 +1,11 @@ """Config flow to configure flood monitoring gauges.""" +from typing import Any + from aioeafm import get_stations import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.helpers.aiohttp_client import async_get_clientsession from .const import DOMAIN @@ -14,21 +16,23 @@ class UKFloodsFlowHandler(ConfigFlow, domain=DOMAIN): VERSION = 1 - def __init__(self): + def __init__(self) -> None: """Handle a UK Floods config flow.""" - self.stations = {} + self.stations: dict[str, str] = {} - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle a flow start.""" - errors = {} + errors: dict[str, str] = {} if user_input is not None: - station = self.stations[user_input["station"]] - await self.async_set_unique_id(station, raise_on_progress=False) + selected_station = self.stations[user_input["station"]] + await self.async_set_unique_id(selected_station, raise_on_progress=False) self._abort_if_unique_id_configured() return self.async_create_entry( title=user_input["station"], - data={"station": station}, + data={"station": selected_station}, ) session = async_get_clientsession(hass=self.hass) diff --git a/homeassistant/components/easyenergy/icons.json b/homeassistant/components/easyenergy/icons.json index 90cbec17a65830..501483eb932e2a 100644 --- a/homeassistant/components/easyenergy/icons.json +++ b/homeassistant/components/easyenergy/icons.json @@ -13,8 +13,14 @@ } }, "services": { - "get_gas_prices": "mdi:gas-station", - "get_energy_usage_prices": "mdi:transmission-tower-import", - "get_energy_return_prices": "mdi:transmission-tower-export" + "get_gas_prices": { + "service": "mdi:gas-station" + }, + "get_energy_usage_prices": { + "service": "mdi:transmission-tower-import" + }, + "get_energy_return_prices": { + "service": "mdi:transmission-tower-export" + } } } diff --git a/homeassistant/components/ebusd/icons.json b/homeassistant/components/ebusd/icons.json index 642be37a43b01a..ebfa3673a0c146 100644 --- a/homeassistant/components/ebusd/icons.json +++ b/homeassistant/components/ebusd/icons.json @@ -1,5 +1,7 @@ { "services": { - "write": "mdi:pencil" + "write": { + "service": "mdi:pencil" + } } } diff --git a/homeassistant/components/ecobee/config_flow.py b/homeassistant/components/ecobee/config_flow.py index dd5c2c62c85f77..f7709c68d915f8 100644 --- a/homeassistant/components/ecobee/config_flow.py +++ b/homeassistant/components/ecobee/config_flow.py @@ -1,5 +1,7 @@ """Config flow to configure ecobee.""" +from typing import Any + from pyecobee import ( ECOBEE_API_KEY, ECOBEE_CONFIG_FILENAME, @@ -8,7 +10,7 @@ ) import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_API_KEY from homeassistant.exceptions import HomeAssistantError from homeassistant.util.json import load_json_object @@ -21,11 +23,11 @@ class EcobeeFlowHandler(ConfigFlow, domain=DOMAIN): VERSION = 1 - def __init__(self) -> None: - """Initialize the ecobee flow.""" - self._ecobee = None + _ecobee: Ecobee - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle a flow initiated by the user.""" if self._async_current_entries(): # Config entry already exists, only one allowed. @@ -55,7 +57,9 @@ async def async_step_user(self, user_input=None): errors=errors, ) - async def async_step_authorize(self, user_input=None): + async def async_step_authorize( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Present the user with the PIN so that the app can be authorized on ecobee.com.""" errors = {} @@ -76,7 +80,7 @@ async def async_step_authorize(self, user_input=None): description_placeholders={"pin": self._ecobee.pin}, ) - async def async_step_import(self, import_data): + async def async_step_import(self, import_data: None) -> ConfigFlowResult: """Import ecobee config from configuration.yaml. Triggered by async_setup only if a config entry doesn't already exist. diff --git a/homeassistant/components/ecobee/icons.json b/homeassistant/components/ecobee/icons.json index 3e736d0dc68700..f24f1f7cfe5dc8 100644 --- a/homeassistant/components/ecobee/icons.json +++ b/homeassistant/components/ecobee/icons.json @@ -1,11 +1,25 @@ { "services": { - "create_vacation": "mdi:umbrella-beach", - "delete_vacation": "mdi:umbrella-beach-outline", - "resume_program": "mdi:play", - "set_fan_min_on_time": "mdi:fan-clock", - "set_dst_mode": "mdi:sun-clock", - "set_mic_mode": "mdi:microphone", - "set_occupancy_modes": "mdi:eye-settings" + "create_vacation": { + "service": "mdi:umbrella-beach" + }, + "delete_vacation": { + "service": "mdi:umbrella-beach-outline" + }, + "resume_program": { + "service": "mdi:play" + }, + "set_fan_min_on_time": { + "service": "mdi:fan-clock" + }, + "set_dst_mode": { + "service": "mdi:sun-clock" + }, + "set_mic_mode": { + "service": "mdi:microphone" + }, + "set_occupancy_modes": { + "service": "mdi:eye-settings" + } } } diff --git a/homeassistant/components/econet/config_flow.py b/homeassistant/components/econet/config_flow.py index 81a5fdf75f070b..145b9cf9f7de68 100644 --- a/homeassistant/components/econet/config_flow.py +++ b/homeassistant/components/econet/config_flow.py @@ -1,10 +1,12 @@ """Config flow to configure the EcoNet component.""" +from typing import Any + from pyeconet import EcoNetApiInterface from pyeconet.errors import InvalidCredentialsError, PyeconetError import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_EMAIL, CONF_PASSWORD from .const import DOMAIN @@ -24,7 +26,9 @@ def __init__(self) -> None: } ) - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the start of the config flow.""" if not user_input: return self.async_show_form( diff --git a/homeassistant/components/ecovacs/icons.json b/homeassistant/components/ecovacs/icons.json index 0c7178ced84e62..6097f43a4e44de 100644 --- a/homeassistant/components/ecovacs/icons.json +++ b/homeassistant/components/ecovacs/icons.json @@ -145,6 +145,8 @@ } }, "services": { - "raw_get_positions": "mdi:map-marker-radius-outline" + "raw_get_positions": { + "service": "mdi:map-marker-radius-outline" + } } } diff --git a/homeassistant/components/ecovacs/lawn_mower.py b/homeassistant/components/ecovacs/lawn_mower.py index a1dc8acf3a284f..bf773207dc5e9c 100644 --- a/homeassistant/components/ecovacs/lawn_mower.py +++ b/homeassistant/components/ecovacs/lawn_mower.py @@ -27,7 +27,7 @@ _STATE_TO_MOWER_STATE = { State.IDLE: LawnMowerActivity.PAUSED, State.CLEANING: LawnMowerActivity.MOWING, - State.RETURNING: LawnMowerActivity.MOWING, + State.RETURNING: LawnMowerActivity.RETURNING, State.DOCKED: LawnMowerActivity.DOCKED, State.ERROR: LawnMowerActivity.ERROR, State.PAUSED: LawnMowerActivity.PAUSED, diff --git a/homeassistant/components/ecovacs/manifest.json b/homeassistant/components/ecovacs/manifest.json index 560ee4d599c955..33977b3b0ded5e 100644 --- a/homeassistant/components/ecovacs/manifest.json +++ b/homeassistant/components/ecovacs/manifest.json @@ -6,5 +6,5 @@ "documentation": "https://www.home-assistant.io/integrations/ecovacs", "iot_class": "cloud_push", "loggers": ["sleekxmppfs", "sucks", "deebot_client"], - "requirements": ["py-sucks==0.9.10", "deebot-client==8.3.0"] + "requirements": ["py-sucks==0.9.10", "deebot-client==8.4.0"] } diff --git a/homeassistant/components/elgato/icons.json b/homeassistant/components/elgato/icons.json index 1b5eaf3763a1c7..d2c286594c7b12 100644 --- a/homeassistant/components/elgato/icons.json +++ b/homeassistant/components/elgato/icons.json @@ -10,6 +10,8 @@ } }, "services": { - "identify": "mdi:crosshairs-question" + "identify": { + "service": "mdi:crosshairs-question" + } } } diff --git a/homeassistant/components/elkm1/config_flow.py b/homeassistant/components/elkm1/config_flow.py index 4ab8d1fe1814bf..2f9d3338d7628f 100644 --- a/homeassistant/components/elkm1/config_flow.py +++ b/homeassistant/components/elkm1/config_flow.py @@ -335,10 +335,10 @@ async def async_step_manual_connection( errors=errors, ) - async def async_step_import(self, user_input: dict[str, Any]) -> ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Handle import.""" _LOGGER.debug("Elk is importing from yaml") - url = _make_url_from_data(user_input) + url = _make_url_from_data(import_data) if self._url_already_configured(url): return self.async_abort(reason="address_already_configured") @@ -357,7 +357,7 @@ async def async_step_import(self, user_input: dict[str, Any]) -> ConfigFlowResul ) self._abort_if_unique_id_configured() - errors, result = await self._async_create_or_error(user_input, True) + errors, result = await self._async_create_or_error(import_data, True) if errors: return self.async_abort(reason=list(errors.values())[0]) assert result is not None diff --git a/homeassistant/components/elkm1/icons.json b/homeassistant/components/elkm1/icons.json index 3bb9ea8c87d10b..54827e4b6ef36e 100644 --- a/homeassistant/components/elkm1/icons.json +++ b/homeassistant/components/elkm1/icons.json @@ -10,18 +10,44 @@ } }, "services": { - "alarm_bypass": "mdi:shield-off", - "alarm_clear_bypass": "mdi:shield", - "alarm_arm_home_instant": "mdi:shield-lock", - "alarm_arm_night_instant": "mdi:shield-moon", - "alarm_arm_vacation": "mdi:beach", - "alarm_display_message": "mdi:message-alert", - "set_time": "mdi:clock-edit", - "speak_phrase": "mdi:message-processing", - "speak_word": "mdi:message-minus", - "sensor_counter_refresh": "mdi:refresh", - "sensor_counter_set": "mdi:counter", - "sensor_zone_bypass": "mdi:shield-off", - "sensor_zone_trigger": "mdi:shield" + "alarm_bypass": { + "service": "mdi:shield-off" + }, + "alarm_clear_bypass": { + "service": "mdi:shield" + }, + "alarm_arm_home_instant": { + "service": "mdi:shield-lock" + }, + "alarm_arm_night_instant": { + "service": "mdi:shield-moon" + }, + "alarm_arm_vacation": { + "service": "mdi:beach" + }, + "alarm_display_message": { + "service": "mdi:message-alert" + }, + "set_time": { + "service": "mdi:clock-edit" + }, + "speak_phrase": { + "service": "mdi:message-processing" + }, + "speak_word": { + "service": "mdi:message-minus" + }, + "sensor_counter_refresh": { + "service": "mdi:refresh" + }, + "sensor_counter_set": { + "service": "mdi:counter" + }, + "sensor_zone_bypass": { + "service": "mdi:shield-off" + }, + "sensor_zone_trigger": { + "service": "mdi:shield" + } } } diff --git a/homeassistant/components/emby/manifest.json b/homeassistant/components/emby/manifest.json index f90dda7935233f..3f57f62eb0b94e 100644 --- a/homeassistant/components/emby/manifest.json +++ b/homeassistant/components/emby/manifest.json @@ -5,5 +5,5 @@ "documentation": "https://www.home-assistant.io/integrations/emby", "iot_class": "local_push", "loggers": ["pyemby"], - "requirements": ["pyEmby==1.9"] + "requirements": ["pyEmby==1.10"] } diff --git a/homeassistant/components/emoncms/__init__.py b/homeassistant/components/emoncms/__init__.py index 5e7adbcd6e772e..98ed6328578012 100644 --- a/homeassistant/components/emoncms/__init__.py +++ b/homeassistant/components/emoncms/__init__.py @@ -1 +1,40 @@ """The emoncms component.""" + +from pyemoncms import EmoncmsClient + +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import CONF_API_KEY, CONF_URL, Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers.aiohttp_client import async_get_clientsession + +from .coordinator import EmoncmsCoordinator + +PLATFORMS: list[Platform] = [Platform.SENSOR] + +type EmonCMSConfigEntry = ConfigEntry[EmoncmsCoordinator] + + +async def async_setup_entry(hass: HomeAssistant, entry: EmonCMSConfigEntry) -> bool: + """Load a config entry.""" + emoncms_client = EmoncmsClient( + entry.data[CONF_URL], + entry.data[CONF_API_KEY], + session=async_get_clientsession(hass), + ) + coordinator = EmoncmsCoordinator(hass, emoncms_client) + await coordinator.async_config_entry_first_refresh() + entry.runtime_data = coordinator + + entry.async_on_unload(entry.add_update_listener(update_listener)) + await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) + return True + + +async def update_listener(hass: HomeAssistant, entry: ConfigEntry): + """Handle options update.""" + await hass.config_entries.async_reload(entry.entry_id) + + +async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: + """Unload a config entry.""" + return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) diff --git a/homeassistant/components/emoncms/config_flow.py b/homeassistant/components/emoncms/config_flow.py new file mode 100644 index 00000000000000..fdd5d29788e1a6 --- /dev/null +++ b/homeassistant/components/emoncms/config_flow.py @@ -0,0 +1,210 @@ +"""Configflow for the emoncms integration.""" + +from typing import Any + +from pyemoncms import EmoncmsClient +import voluptuous as vol + +from homeassistant.config_entries import ( + ConfigEntry, + ConfigFlow, + ConfigFlowResult, + OptionsFlowWithConfigEntry, +) +from homeassistant.const import CONF_API_KEY, CONF_URL +from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers.aiohttp_client import async_get_clientsession +from homeassistant.helpers.selector import selector +from homeassistant.helpers.typing import ConfigType + +from .const import ( + CONF_MESSAGE, + CONF_ONLY_INCLUDE_FEEDID, + CONF_SUCCESS, + DOMAIN, + FEED_ID, + FEED_NAME, + FEED_TAG, + LOGGER, +) + + +def get_options(feeds: list[dict[str, Any]]) -> list[dict[str, Any]]: + """Build the selector options with the feed list.""" + return [ + { + "value": feed[FEED_ID], + "label": f"{feed[FEED_ID]}|{feed[FEED_TAG]}|{feed[FEED_NAME]}", + } + for feed in feeds + ] + + +def sensor_name(url: str) -> str: + """Return sensor name.""" + sensorip = url.rsplit("//", maxsplit=1)[-1] + return f"emoncms@{sensorip}" + + +async def get_feed_list(hass: HomeAssistant, url: str, api_key: str) -> dict[str, Any]: + """Check connection to emoncms and return feed list if successful.""" + emoncms_client = EmoncmsClient( + url, + api_key, + session=async_get_clientsession(hass), + ) + return await emoncms_client.async_request("/feed/list.json") + + +class EmoncmsConfigFlow(ConfigFlow, domain=DOMAIN): + """emoncms integration UI config flow.""" + + url: str + api_key: str + include_only_feeds: list | None = None + dropdown: dict = {} + + @staticmethod + @callback + def async_get_options_flow( + config_entry: ConfigEntry, + ) -> OptionsFlowWithConfigEntry: + """Get the options flow for this handler.""" + return EmoncmsOptionsFlow(config_entry) + + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Initiate a flow via the UI.""" + errors: dict[str, str] = {} + + if user_input is not None: + self._async_abort_entries_match( + { + CONF_API_KEY: user_input[CONF_API_KEY], + CONF_URL: user_input[CONF_URL], + } + ) + result = await get_feed_list( + self.hass, user_input[CONF_URL], user_input[CONF_API_KEY] + ) + if not result[CONF_SUCCESS]: + errors["base"] = result[CONF_MESSAGE] + else: + self.include_only_feeds = user_input.get(CONF_ONLY_INCLUDE_FEEDID) + self.url = user_input[CONF_URL] + self.api_key = user_input[CONF_API_KEY] + options = get_options(result[CONF_MESSAGE]) + self.dropdown = { + "options": options, + "mode": "dropdown", + "multiple": True, + } + return await self.async_step_choose_feeds() + return self.async_show_form( + step_id="user", + data_schema=self.add_suggested_values_to_schema( + vol.Schema( + { + vol.Required(CONF_URL): str, + vol.Required(CONF_API_KEY): str, + } + ), + user_input, + ), + errors=errors, + ) + + async def async_step_choose_feeds( + self, + user_input: dict[str, Any] | None = None, + ) -> ConfigFlowResult: + """Choose feeds to import.""" + errors: dict[str, str] = {} + include_only_feeds: list = [] + if user_input or self.include_only_feeds is not None: + if self.include_only_feeds is not None: + include_only_feeds = self.include_only_feeds + elif user_input: + include_only_feeds = user_input[CONF_ONLY_INCLUDE_FEEDID] + return self.async_create_entry( + title=sensor_name(self.url), + data={ + CONF_URL: self.url, + CONF_API_KEY: self.api_key, + CONF_ONLY_INCLUDE_FEEDID: include_only_feeds, + }, + ) + return self.async_show_form( + step_id="choose_feeds", + data_schema=vol.Schema( + { + vol.Required( + CONF_ONLY_INCLUDE_FEEDID, + default=include_only_feeds, + ): selector({"select": self.dropdown}), + } + ), + errors=errors, + ) + + async def async_step_import(self, import_info: ConfigType) -> ConfigFlowResult: + """Import config from yaml.""" + url = import_info[CONF_URL] + api_key = import_info[CONF_API_KEY] + include_only_feeds = None + if import_info.get(CONF_ONLY_INCLUDE_FEEDID) is not None: + include_only_feeds = list(map(str, import_info[CONF_ONLY_INCLUDE_FEEDID])) + config = { + CONF_API_KEY: api_key, + CONF_ONLY_INCLUDE_FEEDID: include_only_feeds, + CONF_URL: url, + } + LOGGER.debug(config) + result = await self.async_step_user(config) + if errors := result.get("errors"): + return self.async_abort(reason=errors["base"]) + return result + + +class EmoncmsOptionsFlow(OptionsFlowWithConfigEntry): + """Emoncms Options flow handler.""" + + async def async_step_init( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Manage the options.""" + errors: dict[str, str] = {} + data = self.options if self.options else self._config_entry.data + url = data[CONF_URL] + api_key = data[CONF_API_KEY] + include_only_feeds = data.get(CONF_ONLY_INCLUDE_FEEDID, []) + options: list = include_only_feeds + result = await get_feed_list(self.hass, url, api_key) + if not result[CONF_SUCCESS]: + errors["base"] = result[CONF_MESSAGE] + else: + options = get_options(result[CONF_MESSAGE]) + dropdown = {"options": options, "mode": "dropdown", "multiple": True} + if user_input: + include_only_feeds = user_input[CONF_ONLY_INCLUDE_FEEDID] + return self.async_create_entry( + title=sensor_name(url), + data={ + CONF_URL: url, + CONF_API_KEY: api_key, + CONF_ONLY_INCLUDE_FEEDID: include_only_feeds, + }, + ) + + return self.async_show_form( + step_id="init", + data_schema=vol.Schema( + { + vol.Required( + CONF_ONLY_INCLUDE_FEEDID, default=include_only_feeds + ): selector({"select": dropdown}), + } + ), + errors=errors, + ) diff --git a/homeassistant/components/emoncms/const.py b/homeassistant/components/emoncms/const.py index 9626921831667b..256db5726bbce0 100644 --- a/homeassistant/components/emoncms/const.py +++ b/homeassistant/components/emoncms/const.py @@ -7,6 +7,9 @@ CONF_MESSAGE = "message" CONF_SUCCESS = "success" DOMAIN = "emoncms" +FEED_ID = "id" +FEED_NAME = "name" +FEED_TAG = "tag" LOGGER = logging.getLogger(__package__) diff --git a/homeassistant/components/emoncms/coordinator.py b/homeassistant/components/emoncms/coordinator.py index d1f6a2858c7865..c6fda5ed7c8357 100644 --- a/homeassistant/components/emoncms/coordinator.py +++ b/homeassistant/components/emoncms/coordinator.py @@ -18,14 +18,13 @@ def __init__( self, hass: HomeAssistant, emoncms_client: EmoncmsClient, - scan_interval: timedelta, ) -> None: """Initialize the emoncms data coordinator.""" super().__init__( hass, LOGGER, name="emoncms_coordinator", - update_interval=scan_interval, + update_interval=timedelta(seconds=60), ) self.emoncms_client = emoncms_client diff --git a/homeassistant/components/emoncms/manifest.json b/homeassistant/components/emoncms/manifest.json index 09229d0419adc4..f8f0f2edb95370 100644 --- a/homeassistant/components/emoncms/manifest.json +++ b/homeassistant/components/emoncms/manifest.json @@ -2,6 +2,7 @@ "domain": "emoncms", "name": "Emoncms", "codeowners": ["@borpin", "@alexandrecuer"], + "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/emoncms", "iot_class": "local_polling", "requirements": ["pyemoncms==0.0.7"] diff --git a/homeassistant/components/emoncms/sensor.py b/homeassistant/components/emoncms/sensor.py index 3c44839197465e..4add7c9625d921 100644 --- a/homeassistant/components/emoncms/sensor.py +++ b/homeassistant/components/emoncms/sensor.py @@ -2,10 +2,8 @@ from __future__ import annotations -from datetime import timedelta from typing import Any -from pyemoncms import EmoncmsClient import voluptuous as vol from homeassistant.components.sensor import ( @@ -14,25 +12,33 @@ SensorEntity, SensorStateClass, ) +from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry from homeassistant.const import ( CONF_API_KEY, CONF_ID, - CONF_SCAN_INTERVAL, CONF_UNIT_OF_MEASUREMENT, CONF_URL, CONF_VALUE_TEMPLATE, - STATE_UNKNOWN, UnitOfPower, ) -from homeassistant.core import HomeAssistant, callback +from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant, callback +from homeassistant.data_entry_flow import FlowResultType from homeassistant.helpers import template -from homeassistant.helpers.aiohttp_client import async_get_clientsession import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.update_coordinator import CoordinatorEntity -from .const import CONF_EXCLUDE_FEEDID, CONF_ONLY_INCLUDE_FEEDID +from .config_flow import sensor_name +from .const import ( + CONF_EXCLUDE_FEEDID, + CONF_ONLY_INCLUDE_FEEDID, + DOMAIN, + FEED_ID, + FEED_NAME, + FEED_TAG, +) from .coordinator import EmoncmsCoordinator ATTR_FEEDID = "FeedId" @@ -42,9 +48,7 @@ ATTR_SIZE = "Size" ATTR_TAG = "Tag" ATTR_USERID = "UserId" - CONF_SENSOR_NAMES = "sensor_names" - DECIMALS = 2 DEFAULT_UNIT = UnitOfPower.WATT @@ -76,20 +80,73 @@ async def async_setup_platform( async_add_entities: AddEntitiesCallback, discovery_info: DiscoveryInfoType | None = None, ) -> None: - """Set up the Emoncms sensor.""" - apikey = config[CONF_API_KEY] - url = config[CONF_URL] - sensorid = config[CONF_ID] - value_template = config.get(CONF_VALUE_TEMPLATE) - config_unit = config.get(CONF_UNIT_OF_MEASUREMENT) + """Import config from yaml.""" + if CONF_VALUE_TEMPLATE in config: + async_create_issue( + hass, + DOMAIN, + f"remove_{CONF_VALUE_TEMPLATE}_{DOMAIN}", + is_fixable=False, + issue_domain=DOMAIN, + severity=IssueSeverity.ERROR, + translation_key=f"remove_{CONF_VALUE_TEMPLATE}", + translation_placeholders={ + "domain": DOMAIN, + "parameter": CONF_VALUE_TEMPLATE, + }, + ) + return + if CONF_ONLY_INCLUDE_FEEDID not in config: + async_create_issue( + hass, + DOMAIN, + f"missing_{CONF_ONLY_INCLUDE_FEEDID}_{DOMAIN}", + is_fixable=False, + issue_domain=DOMAIN, + severity=IssueSeverity.WARNING, + translation_key=f"missing_{CONF_ONLY_INCLUDE_FEEDID}", + translation_placeholders={ + "domain": DOMAIN, + }, + ) + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_IMPORT}, data=config + ) + if ( + result.get("type") == FlowResultType.CREATE_ENTRY + or result.get("reason") == "already_configured" + ): + async_create_issue( + hass, + HOMEASSISTANT_DOMAIN, + f"deprecated_yaml_{DOMAIN}", + is_fixable=False, + issue_domain=DOMAIN, + breaks_in_ha_version="2025.3.0", + severity=IssueSeverity.WARNING, + translation_key="deprecated_yaml", + translation_placeholders={ + "domain": DOMAIN, + "integration_title": "emoncms", + }, + ) + + +async def async_setup_entry( + hass: HomeAssistant, + entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up the emoncms sensors.""" + config = entry.options if entry.options else entry.data + name = sensor_name(config[CONF_URL]) exclude_feeds = config.get(CONF_EXCLUDE_FEEDID) include_only_feeds = config.get(CONF_ONLY_INCLUDE_FEEDID) - sensor_names = config.get(CONF_SENSOR_NAMES) - scan_interval = config.get(CONF_SCAN_INTERVAL, timedelta(seconds=30)) - emoncms_client = EmoncmsClient(url, apikey, session=async_get_clientsession(hass)) - coordinator = EmoncmsCoordinator(hass, emoncms_client, scan_interval) - await coordinator.async_refresh() + if exclude_feeds is None and include_only_feeds is None: + return + + coordinator = entry.runtime_data elems = coordinator.data if not elems: return @@ -97,28 +154,15 @@ async def async_setup_platform( sensors: list[EmonCmsSensor] = [] for idx, elem in enumerate(elems): - if exclude_feeds is not None and int(elem["id"]) in exclude_feeds: - continue - - if include_only_feeds is not None and int(elem["id"]) not in include_only_feeds: + if include_only_feeds is not None and elem[FEED_ID] not in include_only_feeds: continue - name = None - if sensor_names is not None: - name = sensor_names.get(int(elem["id"]), None) - - if unit := elem.get("unit"): - unit_of_measurement = unit - else: - unit_of_measurement = config_unit - sensors.append( EmonCmsSensor( coordinator, + entry.entry_id, + elem["unit"], name, - value_template, - unit_of_measurement, - str(sensorid), idx, ) ) @@ -131,10 +175,9 @@ class EmonCmsSensor(CoordinatorEntity[EmoncmsCoordinator], SensorEntity): def __init__( self, coordinator: EmoncmsCoordinator, - name: str | None, - value_template: template.Template | None, + entry_id: str, unit_of_measurement: str | None, - sensorid: str, + name: str, idx: int, ) -> None: """Initialize the sensor.""" @@ -143,20 +186,9 @@ def __init__( elem = {} if self.coordinator.data: elem = self.coordinator.data[self.idx] - if name is None: - # Suppress ID in sensor name if it's 1, since most people won't - # have more than one EmonCMS source and it's redundant to show the - # ID if there's only one. - id_for_name = "" if str(sensorid) == "1" else sensorid - # Use the feed name assigned in EmonCMS or fall back to the feed ID - feed_name = elem.get("name", f"Feed {elem.get('id')}") - self._attr_name = f"EmonCMS{id_for_name} {feed_name}" - else: - self._attr_name = name - self._value_template = value_template + self._attr_name = f"{name} {elem[FEED_NAME]}" self._attr_native_unit_of_measurement = unit_of_measurement - self._sensorid = sensorid - + self._attr_unique_id = f"{entry_id}-{elem[FEED_ID]}" if unit_of_measurement in ("kWh", "Wh"): self._attr_device_class = SensorDeviceClass.ENERGY self._attr_state_class = SensorStateClass.TOTAL_INCREASING @@ -186,9 +218,9 @@ def __init__( def _update_attributes(self, elem: dict[str, Any]) -> None: """Update entity attributes.""" self._attr_extra_state_attributes = { - ATTR_FEEDID: elem["id"], - ATTR_TAG: elem["tag"], - ATTR_FEEDNAME: elem["name"], + ATTR_FEEDID: elem[FEED_ID], + ATTR_TAG: elem[FEED_TAG], + ATTR_FEEDNAME: elem[FEED_NAME], } if elem["value"] is not None: self._attr_extra_state_attributes[ATTR_SIZE] = elem["size"] @@ -199,13 +231,7 @@ def _update_attributes(self, elem: dict[str, Any]) -> None: ) self._attr_native_value = None - if self._value_template is not None: - self._attr_native_value = ( - self._value_template.async_render_with_possible_json_value( - elem["value"], STATE_UNKNOWN - ) - ) - elif elem["value"] is not None: + if elem["value"] is not None: self._attr_native_value = round(float(elem["value"]), DECIMALS) @callback diff --git a/homeassistant/components/emoncms/strings.json b/homeassistant/components/emoncms/strings.json new file mode 100644 index 00000000000000..4a700cc8981e07 --- /dev/null +++ b/homeassistant/components/emoncms/strings.json @@ -0,0 +1,40 @@ +{ + "config": { + "step": { + "user": { + "data": { + "url": "[%key:common::config_flow::data::url%]", + "api_key": "[%key:common::config_flow::data::api_key%]" + }, + "data_description": { + "url": "Server url starting with the protocol (http or https)", + "api_key": "Your 32 bits api key" + } + }, + "choose_feeds": { + "data": { + "include_only_feed_id": "Choose feeds to include" + } + } + } + }, + "options": { + "step": { + "init": { + "data": { + "include_only_feed_id": "[%key:component::emoncms::config::step::choose_feeds::data::include_only_feed_id%]" + } + } + } + }, + "issues": { + "remove_value_template": { + "title": "The {domain} integration cannot start", + "description": "Configuring {domain} using YAML is being removed and the `{parameter}` parameter cannot be imported.\n\nPlease remove `{parameter}` from your `{domain}` yaml configuration and restart Home Assistant\n\nAlternatively, you may entirely remove the `{domain}` configuration from your configuration.yaml, restart Home Assistant, and add the {domain} integration manually." + }, + "missing_include_only_feed_id": { + "title": "No feed synchronized with the {domain} sensor", + "description": "Configuring {domain} using YAML is being removed.\n\nPlease add manually the feeds you want to synchronize with the `configure` button of the integration." + } + } +} diff --git a/homeassistant/components/emonitor/config_flow.py b/homeassistant/components/emonitor/config_flow.py index 9909ddff19c7a0..b924c7df52246d 100644 --- a/homeassistant/components/emonitor/config_flow.py +++ b/homeassistant/components/emonitor/config_flow.py @@ -1,6 +1,7 @@ """Config flow for SiteSage Emonitor integration.""" import logging +from typing import Any from aioemonitor import Emonitor import aiohttp @@ -33,12 +34,15 @@ class EmonitorConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - def __init__(self): + discovered_info: dict[str, str] + + def __init__(self) -> None: """Initialize Emonitor ConfigFlow.""" - self.discovered_ip = None - self.discovered_info = None + self.discovered_ip: str | None = None - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" errors = {} if user_input is not None: @@ -84,7 +88,9 @@ async def async_step_dhcp( return await self.async_step_user() return await self.async_step_confirm() - async def async_step_confirm(self, user_input=None): + async def async_step_confirm( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Attempt to confirm.""" if user_input is not None: return self.async_create_entry( diff --git a/homeassistant/components/emulated_roku/config_flow.py b/homeassistant/components/emulated_roku/config_flow.py index 1a3b2c0e2af8cd..725987418da412 100644 --- a/homeassistant/components/emulated_roku/config_flow.py +++ b/homeassistant/components/emulated_roku/config_flow.py @@ -1,16 +1,18 @@ """Config flow to configure emulated_roku component.""" +from typing import Any + import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_NAME -from homeassistant.core import callback +from homeassistant.core import HomeAssistant, callback from .const import CONF_LISTEN_PORT, DEFAULT_NAME, DEFAULT_PORT, DOMAIN @callback -def configured_servers(hass): +def configured_servers(hass: HomeAssistant) -> set[str]: """Return a set of the configured servers.""" return { entry.data[CONF_NAME] for entry in hass.config_entries.async_entries(DOMAIN) @@ -22,9 +24,11 @@ class EmulatedRokuFlowHandler(ConfigFlow, domain=DOMAIN): VERSION = 1 - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle a flow initialized by the user.""" - errors = {} + errors: dict[str, str] = {} if user_input is not None: self._async_abort_entries_match({CONF_NAME: user_input[CONF_NAME]}) @@ -52,6 +56,6 @@ async def async_step_user(self, user_input=None): errors=errors, ) - async def async_step_import(self, import_config): + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Handle a flow import.""" - return await self.async_step_user(import_config) + return await self.async_step_user(import_data) diff --git a/homeassistant/components/energyzero/icons.json b/homeassistant/components/energyzero/icons.json index bac061dd31886a..802f8ef69167cf 100644 --- a/homeassistant/components/energyzero/icons.json +++ b/homeassistant/components/energyzero/icons.json @@ -10,7 +10,11 @@ } }, "services": { - "get_gas_prices": "mdi:gas-station", - "get_energy_prices": "mdi:lightning-bolt" + "get_gas_prices": { + "service": "mdi:gas-station" + }, + "get_energy_prices": { + "service": "mdi:lightning-bolt" + } } } diff --git a/homeassistant/components/enigma2/config_flow.py b/homeassistant/components/enigma2/config_flow.py index 71c5830d5508f9..55c0f6fc6ae5a1 100644 --- a/homeassistant/components/enigma2/config_flow.py +++ b/homeassistant/components/enigma2/config_flow.py @@ -152,20 +152,20 @@ async def async_step_user( ) return self.async_create_entry(data=user_input, title=user_input[CONF_HOST]) - async def async_step_import(self, user_input: dict[str, Any]) -> ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Handle the import step.""" - if CONF_PORT not in user_input: - user_input[CONF_PORT] = DEFAULT_PORT - if CONF_SSL not in user_input: - user_input[CONF_SSL] = DEFAULT_SSL - user_input[CONF_VERIFY_SSL] = DEFAULT_VERIFY_SSL + if CONF_PORT not in import_data: + import_data[CONF_PORT] = DEFAULT_PORT + if CONF_SSL not in import_data: + import_data[CONF_SSL] = DEFAULT_SSL + import_data[CONF_VERIFY_SSL] = DEFAULT_VERIFY_SSL - data = {key: user_input[key] for key in user_input if key in self.DATA_KEYS} + data = {key: import_data[key] for key in import_data if key in self.DATA_KEYS} options = { - key: user_input[key] for key in user_input if key in self.OPTIONS_KEYS + key: import_data[key] for key in import_data if key in self.OPTIONS_KEYS } - if errors := await self.validate_user_input(user_input): + if errors := await self.validate_user_input(import_data): async_create_issue( self.hass, DOMAIN, diff --git a/homeassistant/components/enocean/config_flow.py b/homeassistant/components/enocean/config_flow.py index 157d58bbf23b45..fef633d94c33a5 100644 --- a/homeassistant/components/enocean/config_flow.py +++ b/homeassistant/components/enocean/config_flow.py @@ -1,8 +1,10 @@ """Config flows for the ENOcean integration.""" +from typing import Any + import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_DEVICE from . import dongle @@ -20,31 +22,35 @@ def __init__(self) -> None: self.dongle_path = None self.discovery_info = None - async def async_step_import(self, data=None): + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Import a yaml configuration.""" - if not await self.validate_enocean_conf(data): + if not await self.validate_enocean_conf(import_data): LOGGER.warning( "Cannot import yaml configuration: %s is not a valid dongle path", - data[CONF_DEVICE], + import_data[CONF_DEVICE], ) return self.async_abort(reason="invalid_dongle_path") - return self.create_enocean_entry(data) + return self.create_enocean_entry(import_data) - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle an EnOcean config flow start.""" if self._async_current_entries(): return self.async_abort(reason="single_instance_allowed") return await self.async_step_detect() - async def async_step_detect(self, user_input=None): + async def async_step_detect( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Propose a list of detected dongles.""" errors = {} if user_input is not None: if user_input[CONF_DEVICE] == self.MANUAL_PATH_VALUE: - return await self.async_step_manual(None) + return await self.async_step_manual() if await self.validate_enocean_conf(user_input): return self.create_enocean_entry(user_input) errors = {CONF_DEVICE: ERROR_INVALID_DONGLE_PATH} @@ -60,7 +66,9 @@ async def async_step_detect(self, user_input=None): errors=errors, ) - async def async_step_manual(self, user_input=None): + async def async_step_manual( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Request manual USB dongle path.""" default_value = None errors = {} diff --git a/homeassistant/components/environment_canada/config_flow.py b/homeassistant/components/environment_canada/config_flow.py index 79b37c64c1bc97..c4fd16f9522bb7 100644 --- a/homeassistant/components/environment_canada/config_flow.py +++ b/homeassistant/components/environment_canada/config_flow.py @@ -1,13 +1,14 @@ """Config flow for Environment Canada integration.""" import logging +from typing import Any import xml.etree.ElementTree as ET import aiohttp from env_canada import ECWeather, ec_exc import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_LANGUAGE, CONF_LATITUDE, CONF_LONGITUDE from homeassistant.helpers import config_validation as cv @@ -46,7 +47,9 @@ class EnvironmentCanadaConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" errors = {} if user_input is not None: diff --git a/homeassistant/components/environment_canada/icons.json b/homeassistant/components/environment_canada/icons.json index 5e23a96bcfbdff..c3562ce1840728 100644 --- a/homeassistant/components/environment_canada/icons.json +++ b/homeassistant/components/environment_canada/icons.json @@ -19,6 +19,8 @@ } }, "services": { - "set_radar_type": "mdi:radar" + "set_radar_type": { + "service": "mdi:radar" + } } } diff --git a/homeassistant/components/envisalink/alarm_control_panel.py b/homeassistant/components/envisalink/alarm_control_panel.py index d4bbe174f207e4..ea8b6390178833 100644 --- a/homeassistant/components/envisalink/alarm_control_panel.py +++ b/homeassistant/components/envisalink/alarm_control_panel.py @@ -119,7 +119,7 @@ def __init__( self._partition_number = partition_number self._panic_type = panic_type self._alarm_control_panel_option_default_code = code - self._attr_code_format = CodeFormat.NUMBER + self._attr_code_format = CodeFormat.NUMBER if not code else None _LOGGER.debug("Setting up alarm: %s", alarm_name) super().__init__(alarm_name, info, controller) diff --git a/homeassistant/components/envisalink/icons.json b/homeassistant/components/envisalink/icons.json index 20696067f76359..b25e988f478c2b 100644 --- a/homeassistant/components/envisalink/icons.json +++ b/homeassistant/components/envisalink/icons.json @@ -1,6 +1,10 @@ { "services": { - "alarm_keypress": "mdi:alarm-panel", - "invoke_custom_function": "mdi:console" + "alarm_keypress": { + "service": "mdi:alarm-panel" + }, + "invoke_custom_function": { + "service": "mdi:console" + } } } diff --git a/homeassistant/components/epson/__init__.py b/homeassistant/components/epson/__init__.py index 5171865594d3f4..715b55824b41d8 100644 --- a/homeassistant/components/epson/__init__.py +++ b/homeassistant/components/epson/__init__.py @@ -13,7 +13,7 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.aiohttp_client import async_get_clientsession -from .const import DOMAIN, HTTP +from .const import CONF_CONNECTION_TYPE, DOMAIN, HTTP from .exceptions import CannotConnect, PoweredOff PLATFORMS = [Platform.MEDIA_PLAYER] @@ -22,13 +22,17 @@ async def validate_projector( - hass: HomeAssistant, host, check_power=True, check_powered_on=True + hass: HomeAssistant, + host: str, + conn_type: str, + check_power: bool = True, + check_powered_on: bool = True, ): """Validate the given projector host allows us to connect.""" epson_proj = Projector( host=host, websession=async_get_clientsession(hass, verify_ssl=False), - type=HTTP, + type=conn_type, ) if check_power: _power = await epson_proj.get_power() @@ -46,6 +50,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: projector = await validate_projector( hass=hass, host=entry.data[CONF_HOST], + conn_type=entry.data[CONF_CONNECTION_TYPE], check_power=False, check_powered_on=False, ) @@ -60,5 +65,33 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Unload a config entry.""" unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) if unload_ok: - hass.data[DOMAIN].pop(entry.entry_id) + projector = hass.data[DOMAIN].pop(entry.entry_id) + projector.close() return unload_ok + + +async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: + """Migrate old entry.""" + _LOGGER.debug( + "Migrating configuration from version %s.%s", + config_entry.version, + config_entry.minor_version, + ) + + if config_entry.version > 1 or config_entry.minor_version > 1: + # This means the user has downgraded from a future version + return False + + if config_entry.version == 1 and config_entry.minor_version == 1: + new_data = {**config_entry.data} + new_data[CONF_CONNECTION_TYPE] = HTTP + + hass.config_entries.async_update_entry( + config_entry, data=new_data, version=1, minor_version=2 + ) + + _LOGGER.debug( + "Migration to configuration version %s successful", config_entry.version + ) + + return True diff --git a/homeassistant/components/epson/config_flow.py b/homeassistant/components/epson/config_flow.py index 4f038de9318223..c54bff2eea906c 100644 --- a/homeassistant/components/epson/config_flow.py +++ b/homeassistant/components/epson/config_flow.py @@ -1,18 +1,27 @@ """Config flow for epson integration.""" import logging +from typing import Any import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT +from homeassistant.helpers.selector import SelectSelector, SelectSelectorConfig from . import validate_projector -from .const import DOMAIN +from .const import CONF_CONNECTION_TYPE, DOMAIN, HTTP, SERIAL from .exceptions import CannotConnect, PoweredOff +ALLOWED_CONNECTION_TYPE = [HTTP, SERIAL] + DATA_SCHEMA = vol.Schema( { + vol.Required(CONF_CONNECTION_TYPE, default=HTTP): SelectSelector( + SelectSelectorConfig( + options=ALLOWED_CONNECTION_TYPE, translation_key="connection_type" + ) + ), vol.Required(CONF_HOST): str, vol.Required(CONF_NAME, default=DOMAIN): str, } @@ -25,17 +34,24 @@ class EpsonConfigFlow(ConfigFlow, domain=DOMAIN): """Handle a config flow for epson.""" VERSION = 1 + MINOR_VERSION = 2 - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" errors = {} if user_input is not None: + # Epson projector doesn't appear to need to be on for serial + check_power = user_input[CONF_CONNECTION_TYPE] != SERIAL + projector = None try: projector = await validate_projector( hass=self.hass, + conn_type=user_input[CONF_CONNECTION_TYPE], host=user_input[CONF_HOST], check_power=True, - check_powered_on=True, + check_powered_on=check_power, ) except CannotConnect: errors["base"] = "cannot_connect" @@ -52,6 +68,9 @@ async def async_step_user(self, user_input=None): return self.async_create_entry( title=user_input.pop(CONF_NAME), data=user_input ) + finally: + if projector: + projector.close() return self.async_show_form( step_id="user", data_schema=DATA_SCHEMA, errors=errors ) diff --git a/homeassistant/components/epson/const.py b/homeassistant/components/epson/const.py index 06ef9f25e350f2..5bc5f57cb3fd70 100644 --- a/homeassistant/components/epson/const.py +++ b/homeassistant/components/epson/const.py @@ -2,6 +2,8 @@ DOMAIN = "epson" SERVICE_SELECT_CMODE = "select_cmode" +CONF_CONNECTION_TYPE = "connection_type" ATTR_CMODE = "cmode" HTTP = "http" +SERIAL = "serial" diff --git a/homeassistant/components/epson/icons.json b/homeassistant/components/epson/icons.json index a9237edcfd17ae..d41ddebcdce91f 100644 --- a/homeassistant/components/epson/icons.json +++ b/homeassistant/components/epson/icons.json @@ -1,5 +1,7 @@ { "services": { - "select_cmode": "mdi:palette" + "select_cmode": { + "service": "mdi:palette" + } } } diff --git a/homeassistant/components/epson/strings.json b/homeassistant/components/epson/strings.json index 94544c32d1d60b..fb8d7ab5fddecc 100644 --- a/homeassistant/components/epson/strings.json +++ b/homeassistant/components/epson/strings.json @@ -3,11 +3,12 @@ "step": { "user": { "data": { + "connection_type": "Connection type", "host": "[%key:common::config_flow::data::host%]", "name": "[%key:common::config_flow::data::name%]" }, "data_description": { - "host": "The hostname or IP address of your Epson projector." + "host": "The hostname, IP address or serial port of your Epson projector." } } }, @@ -30,5 +31,13 @@ } } } + }, + "selector": { + "connection_type": { + "options": { + "http": "HTTP", + "serial": "Serial" + } + } } } diff --git a/homeassistant/components/esphome/climate.py b/homeassistant/components/esphome/climate.py index da1cdfb0eab22b..1b9b53f24cd4f9 100644 --- a/homeassistant/components/esphome/climate.py +++ b/homeassistant/components/esphome/climate.py @@ -58,6 +58,7 @@ from .entity import ( EsphomeEntity, convert_api_error_ha_error, + esphome_float_state_property, esphome_state_property, platform_async_setup_entry, ) @@ -227,7 +228,7 @@ def swing_mode(self) -> str | None: return _SWING_MODES.from_esphome(self._state.swing_mode) @property - @esphome_state_property + @esphome_float_state_property def current_temperature(self) -> float | None: """Return the current temperature.""" return self._state.current_temperature @@ -241,19 +242,19 @@ def current_humidity(self) -> int | None: return round(self._state.current_humidity) @property - @esphome_state_property + @esphome_float_state_property def target_temperature(self) -> float | None: """Return the temperature we try to reach.""" return self._state.target_temperature @property - @esphome_state_property + @esphome_float_state_property def target_temperature_low(self) -> float | None: """Return the lowbound target temperature we try to reach.""" return self._state.target_temperature_low @property - @esphome_state_property + @esphome_float_state_property def target_temperature_high(self) -> float | None: """Return the highbound target temperature we try to reach.""" return self._state.target_temperature_high diff --git a/homeassistant/components/esphome/cover.py b/homeassistant/components/esphome/cover.py index 19ce4cbf55a24c..83c749f89ca215 100644 --- a/homeassistant/components/esphome/cover.py +++ b/homeassistant/components/esphome/cover.py @@ -61,13 +61,13 @@ def is_closed(self) -> bool | None: @esphome_state_property def is_opening(self) -> bool: """Return if the cover is opening or not.""" - return self._state.current_operation == CoverOperation.IS_OPENING + return self._state.current_operation is CoverOperation.IS_OPENING @property @esphome_state_property def is_closing(self) -> bool: """Return if the cover is closing or not.""" - return self._state.current_operation == CoverOperation.IS_CLOSING + return self._state.current_operation is CoverOperation.IS_CLOSING @property @esphome_state_property diff --git a/homeassistant/components/esphome/entity.py b/homeassistant/components/esphome/entity.py index 764390c861b51a..455a3f8d1058ee 100644 --- a/homeassistant/components/esphome/entity.py +++ b/homeassistant/components/esphome/entity.py @@ -94,7 +94,6 @@ async def platform_async_setup_entry( """ entry_data = entry.runtime_data entry_data.info[info_type] = {} - entry_data.state.setdefault(state_type, {}) platform = entity_platform.async_get_current_platform() on_static_info_update = functools.partial( async_static_info_updated, @@ -119,20 +118,35 @@ def esphome_state_property[_R, _EntityT: EsphomeEntity[Any, Any]]( ) -> Callable[[_EntityT], _R | None]: """Wrap a state property of an esphome entity. - This checks if the state object in the entity is set, and - prevents writing NAN values to the Home Assistant state machine. + This checks if the state object in the entity is set + and returns None if it is not set. """ @functools.wraps(func) def _wrapper(self: _EntityT) -> _R | None: + return func(self) if self._has_state else None + + return _wrapper + + +def esphome_float_state_property[_EntityT: EsphomeEntity[Any, Any]]( + func: Callable[[_EntityT], float | None], +) -> Callable[[_EntityT], float | None]: + """Wrap a state property of an esphome entity that returns a float. + + This checks if the state object in the entity is set, and returns + None if its not set. If also prevents writing NAN values to the + Home Assistant state machine. + """ + + @functools.wraps(func) + def _wrapper(self: _EntityT) -> float | None: if not self._has_state: return None val = func(self) - if isinstance(val, float) and not math.isfinite(val): - # Home Assistant doesn't use NaN or inf values in state machine - # (not JSON serializable) - return None - return val + # Home Assistant doesn't use NaN or inf values in state machine + # (not JSON serializable) + return None if val is None or not math.isfinite(val) else val return _wrapper @@ -188,6 +202,7 @@ def __init__( ) -> None: """Initialize.""" self._entry_data = entry_data + self._states = cast(dict[int, _StateT], entry_data.state[state_type]) assert entry_data.device_info is not None device_info = entry_data.device_info self._device_info = device_info @@ -265,11 +280,9 @@ def _on_static_info_update(self, static_info: EntityInfo) -> None: @callback def _update_state_from_entry_data(self) -> None: """Update state from entry data.""" - state = self._entry_data.state key = self._key - state_type = self._state_type - if has_state := key in state[state_type]: - self._state = cast(_StateT, state[state_type][key]) + if has_state := key in self._states: + self._state = self._states[key] self._has_state = has_state @callback diff --git a/homeassistant/components/esphome/entry_data.py b/homeassistant/components/esphome/entry_data.py index ff6f048eba19f1..6fc40612c489f0 100644 --- a/homeassistant/components/esphome/entry_data.py +++ b/homeassistant/components/esphome/entry_data.py @@ -3,6 +3,7 @@ from __future__ import annotations import asyncio +from collections import defaultdict from collections.abc import Callable, Iterable from dataclasses import dataclass, field from functools import partial @@ -111,7 +112,9 @@ class RuntimeEntryData: title: str client: APIClient store: ESPHomeStorage - state: dict[type[EntityState], dict[int, EntityState]] = field(default_factory=dict) + state: defaultdict[type[EntityState], dict[int, EntityState]] = field( + default_factory=lambda: defaultdict(dict) + ) # When the disconnect callback is called, we mark all states # as stale so we will always dispatch a state update when the # device reconnects. This is the same format as state_subscriptions. diff --git a/homeassistant/components/esphome/lock.py b/homeassistant/components/esphome/lock.py index 4caa1f68612312..15a402ccb919fc 100644 --- a/homeassistant/components/esphome/lock.py +++ b/homeassistant/components/esphome/lock.py @@ -40,25 +40,25 @@ def _on_static_info_update(self, static_info: EntityInfo) -> None: @esphome_state_property def is_locked(self) -> bool | None: """Return true if the lock is locked.""" - return self._state.state == LockState.LOCKED + return self._state.state is LockState.LOCKED @property @esphome_state_property def is_locking(self) -> bool | None: """Return true if the lock is locking.""" - return self._state.state == LockState.LOCKING + return self._state.state is LockState.LOCKING @property @esphome_state_property def is_unlocking(self) -> bool | None: """Return true if the lock is unlocking.""" - return self._state.state == LockState.UNLOCKING + return self._state.state is LockState.UNLOCKING @property @esphome_state_property def is_jammed(self) -> bool | None: """Return true if the lock is jammed (incomplete locking).""" - return self._state.state == LockState.JAMMED + return self._state.state is LockState.JAMMED @convert_api_error_ha_error async def async_lock(self, **kwargs: Any) -> None: diff --git a/homeassistant/components/esphome/manager.py b/homeassistant/components/esphome/manager.py index 7629d1fa9cd57a..93e8d7b5bc2065 100644 --- a/homeassistant/components/esphome/manager.py +++ b/homeassistant/components/esphome/manager.py @@ -329,6 +329,15 @@ def async_on_state_subscription( entity_id, attribute, hass.states.get(entity_id) ) + @callback + def async_on_state_request( + self, entity_id: str, attribute: str | None = None + ) -> None: + """Forward state for requested entity.""" + self._send_home_assistant_state( + entity_id, attribute, self.hass.states.get(entity_id) + ) + def _handle_pipeline_finished(self) -> None: self.entry_data.async_set_assist_pipeline_state(False) @@ -526,7 +535,10 @@ async def _on_connnect(self) -> None: cli.subscribe_states(entry_data.async_update_state) cli.subscribe_service_calls(self.async_on_service_call) - cli.subscribe_home_assistant_states(self.async_on_state_subscription) + cli.subscribe_home_assistant_states( + self.async_on_state_subscription, + self.async_on_state_request, + ) entry_data.async_save_to_store() _async_check_firmware_version(hass, device_info, api_version) diff --git a/homeassistant/components/esphome/manifest.json b/homeassistant/components/esphome/manifest.json index b2647709e8e5be..233015b13bae8c 100644 --- a/homeassistant/components/esphome/manifest.json +++ b/homeassistant/components/esphome/manifest.json @@ -17,7 +17,7 @@ "mqtt": ["esphome/discover/#"], "quality_scale": "platinum", "requirements": [ - "aioesphomeapi==25.1.0", + "aioesphomeapi==25.3.2", "esphome-dashboard-api==1.2.3", "bleak-esphome==1.0.0" ], diff --git a/homeassistant/components/esphome/media_player.py b/homeassistant/components/esphome/media_player.py index ec9d61fb9e7c6f..f7c5d7011f87bb 100644 --- a/homeassistant/components/esphome/media_player.py +++ b/homeassistant/components/esphome/media_player.py @@ -29,6 +29,7 @@ from .entity import ( EsphomeEntity, convert_api_error_ha_error, + esphome_float_state_property, esphome_state_property, platform_async_setup_entry, ) @@ -79,7 +80,7 @@ def is_volume_muted(self) -> bool: return self._state.muted @property - @esphome_state_property + @esphome_float_state_property def volume_level(self) -> float | None: """Volume level of the media player (0..1).""" return self._state.volume diff --git a/homeassistant/components/esphome/number.py b/homeassistant/components/esphome/number.py index 1e588c8d35ea27..2d74dad1bcfa0f 100644 --- a/homeassistant/components/esphome/number.py +++ b/homeassistant/components/esphome/number.py @@ -3,7 +3,6 @@ from __future__ import annotations from functools import partial -import math from aioesphomeapi import ( EntityInfo, @@ -19,7 +18,7 @@ from .entity import ( EsphomeEntity, convert_api_error_ha_error, - esphome_state_property, + esphome_float_state_property, platform_async_setup_entry, ) from .enum_mapper import EsphomeEnumMapper @@ -57,13 +56,11 @@ def _on_static_info_update(self, static_info: EntityInfo) -> None: self._attr_mode = NumberMode.AUTO @property - @esphome_state_property + @esphome_float_state_property def native_value(self) -> float | None: """Return the state of the entity.""" state = self._state - if state.missing_state or not math.isfinite(state.state): - return None - return state.state + return None if state.missing_state else state.state @convert_api_error_ha_error async def async_set_native_value(self, value: float) -> None: diff --git a/homeassistant/components/esphome/sensor.py b/homeassistant/components/esphome/sensor.py index 0742bebed2893e..670c92d291e437 100644 --- a/homeassistant/components/esphome/sensor.py +++ b/homeassistant/components/esphome/sensor.py @@ -26,7 +26,7 @@ from homeassistant.util import dt as dt_util from homeassistant.util.enum import try_parse_enum -from .entity import EsphomeEntity, esphome_state_property, platform_async_setup_entry +from .entity import EsphomeEntity, platform_async_setup_entry from .enum_mapper import EsphomeEnumMapper @@ -93,15 +93,16 @@ def _on_static_info_update(self, static_info: EntityInfo) -> None: self._attr_state_class = _STATE_CLASSES.from_esphome(state_class) @property - @esphome_state_property def native_value(self) -> datetime | str | None: """Return the state of the entity.""" - state = self._state - if state.missing_state or not math.isfinite(state.state): + if not self._has_state or (state := self._state).missing_state: return None - if self._attr_device_class is SensorDeviceClass.TIMESTAMP: - return dt_util.utc_from_timestamp(state.state) - return f"{state.state:.{self._static_info.accuracy_decimals}f}" + state_float = state.state + if not math.isfinite(state_float): + return None + if self.device_class is SensorDeviceClass.TIMESTAMP: + return dt_util.utc_from_timestamp(state_float) + return f"{state_float:.{self._static_info.accuracy_decimals}f}" class EsphomeTextSensor(EsphomeEntity[TextSensorInfo, TextSensorState], SensorEntity): @@ -117,17 +118,17 @@ def _on_static_info_update(self, static_info: EntityInfo) -> None: ) @property - @esphome_state_property def native_value(self) -> str | datetime | date | None: """Return the state of the entity.""" - state = self._state - if state.missing_state: + if not self._has_state or (state := self._state).missing_state: return None - if self._attr_device_class is SensorDeviceClass.TIMESTAMP: - return dt_util.parse_datetime(state.state) + state_str = state.state + device_class = self.device_class + if device_class is SensorDeviceClass.TIMESTAMP: + return dt_util.parse_datetime(state_str) if ( - self._attr_device_class is SensorDeviceClass.DATE - and (value := dt_util.parse_datetime(state.state)) is not None + device_class is SensorDeviceClass.DATE + and (value := dt_util.parse_datetime(state_str)) is not None ): return value.date() - return state.state + return state_str diff --git a/homeassistant/components/esphome/text.py b/homeassistant/components/esphome/text.py index f9dbbbcd853f7c..36d77aac4a00d8 100644 --- a/homeassistant/components/esphome/text.py +++ b/homeassistant/components/esphome/text.py @@ -43,9 +43,7 @@ def _on_static_info_update(self, static_info: EntityInfo) -> None: def native_value(self) -> str | None: """Return the state of the entity.""" state = self._state - if state.missing_state: - return None - return state.state + return None if state.missing_state else state.state @convert_api_error_ha_error async def async_set_value(self, value: str) -> None: diff --git a/homeassistant/components/evohome/icons.json b/homeassistant/components/evohome/icons.json index cd0005e2546a1d..54488440e60c39 100644 --- a/homeassistant/components/evohome/icons.json +++ b/homeassistant/components/evohome/icons.json @@ -1,9 +1,19 @@ { "services": { - "set_system_mode": "mdi:pencil", - "reset_system": "mdi:refresh", - "refresh_system": "mdi:refresh", - "set_zone_override": "mdi:motion-sensor", - "clear_zone_override": "mdi:motion-sensor-off" + "set_system_mode": { + "service": "mdi:pencil" + }, + "reset_system": { + "service": "mdi:refresh" + }, + "refresh_system": { + "service": "mdi:refresh" + }, + "set_zone_override": { + "service": "mdi:motion-sensor" + }, + "clear_zone_override": { + "service": "mdi:motion-sensor-off" + } } } diff --git a/homeassistant/components/ezviz/config_flow.py b/homeassistant/components/ezviz/config_flow.py index 2b47b120cf8bf9..66425c675cc106 100644 --- a/homeassistant/components/ezviz/config_flow.py +++ b/homeassistant/components/ezviz/config_flow.py @@ -319,7 +319,7 @@ async def async_step_confirm( ) async def async_step_reauth( - self, user_input: Mapping[str, Any] + self, entry_data: Mapping[str, Any] ) -> ConfigFlowResult: """Handle a flow for reauthentication with password.""" diff --git a/homeassistant/components/ezviz/icons.json b/homeassistant/components/ezviz/icons.json index 89b4747ed69bd6..e4a2e49a22c71b 100644 --- a/homeassistant/components/ezviz/icons.json +++ b/homeassistant/components/ezviz/icons.json @@ -26,7 +26,11 @@ } }, "services": { - "set_alarm_detection_sensibility": "mdi:motion-sensor", - "wake_device": "mdi:sleep-off" + "set_alarm_detection_sensibility": { + "service": "mdi:motion-sensor" + }, + "wake_device": { + "service": "mdi:sleep-off" + } } } diff --git a/homeassistant/components/fan/icons.json b/homeassistant/components/fan/icons.json index 60edbce5f01986..caf80775f80245 100644 --- a/homeassistant/components/fan/icons.json +++ b/homeassistant/components/fan/icons.json @@ -20,14 +20,32 @@ } }, "services": { - "decrease_speed": "mdi:fan-minus", - "increase_speed": "mdi:fan-plus", - "oscillate": "mdi:arrow-oscillating", - "set_direction": "mdi:rotate-3d-variant", - "set_percentage": "mdi:fan", - "set_preset_mode": "mdi:fan-auto", - "toggle": "mdi:fan", - "turn_off": "mdi:fan-off", - "turn_on": "mdi:fan" + "decrease_speed": { + "service": "mdi:fan-minus" + }, + "increase_speed": { + "service": "mdi:fan-plus" + }, + "oscillate": { + "service": "mdi:arrow-oscillating" + }, + "set_direction": { + "service": "mdi:rotate-3d-variant" + }, + "set_percentage": { + "service": "mdi:fan" + }, + "set_preset_mode": { + "service": "mdi:fan-auto" + }, + "toggle": { + "service": "mdi:fan" + }, + "turn_off": { + "service": "mdi:fan-off" + }, + "turn_on": { + "service": "mdi:fan" + } } } diff --git a/homeassistant/components/feedreader/config_flow.py b/homeassistant/components/feedreader/config_flow.py index d367432ff8ccd7..4553978a47ecd3 100644 --- a/homeassistant/components/feedreader/config_flow.py +++ b/homeassistant/components/feedreader/config_flow.py @@ -115,10 +115,10 @@ async def async_step_user( options={CONF_MAX_ENTRIES: self._max_entries or DEFAULT_MAX_ENTRIES}, ) - async def async_step_import(self, user_input: dict[str, Any]) -> ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Handle an import flow.""" - self._max_entries = user_input[CONF_MAX_ENTRIES] - return await self.async_step_user({CONF_URL: user_input[CONF_URL]}) + self._max_entries = import_data[CONF_MAX_ENTRIES] + return await self.async_step_user({CONF_URL: import_data[CONF_URL]}) async def async_step_reconfigure( self, _: dict[str, Any] | None = None diff --git a/homeassistant/components/ffmpeg/icons.json b/homeassistant/components/ffmpeg/icons.json index a23f024599c7c7..780eb071af19d3 100644 --- a/homeassistant/components/ffmpeg/icons.json +++ b/homeassistant/components/ffmpeg/icons.json @@ -1,7 +1,13 @@ { "services": { - "restart": "mdi:restart", - "start": "mdi:play", - "stop": "mdi:stop" + "restart": { + "service": "mdi:restart" + }, + "start": { + "service": "mdi:play" + }, + "stop": { + "service": "mdi:stop" + } } } diff --git a/homeassistant/components/file/config_flow.py b/homeassistant/components/file/config_flow.py index 8cb58ec1f4701f..d74e36ce935e49 100644 --- a/homeassistant/components/file/config_flow.py +++ b/homeassistant/components/file/config_flow.py @@ -129,11 +129,8 @@ async def async_step_sensor( """Handle file sensor config flow.""" return await self._async_handle_step(Platform.SENSOR.value, user_input) - async def async_step_import( - self, import_data: dict[str, Any] | None = None - ) -> ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Import `file`` config from configuration.yaml.""" - assert import_data is not None self._async_abort_entries_match(import_data) platform = import_data[CONF_PLATFORM] name: str = import_data.get(CONF_NAME, DEFAULT_NAME) diff --git a/homeassistant/components/filter/icons.json b/homeassistant/components/filter/icons.json index a03163179cb5bd..a9829425570a0c 100644 --- a/homeassistant/components/filter/icons.json +++ b/homeassistant/components/filter/icons.json @@ -1,5 +1,7 @@ { "services": { - "reload": "mdi:reload" + "reload": { + "service": "mdi:reload" + } } } diff --git a/homeassistant/components/fireservicerota/config_flow.py b/homeassistant/components/fireservicerota/config_flow.py index afaef17c5a6b0a..7b7248d44a1893 100644 --- a/homeassistant/components/fireservicerota/config_flow.py +++ b/homeassistant/components/fireservicerota/config_flow.py @@ -27,18 +27,20 @@ class FireServiceRotaFlowHandler(ConfigFlow, domain=DOMAIN): VERSION = 1 - def __init__(self): + def __init__(self) -> None: """Initialize config flow.""" self.api = None self._base_url = None self._username = None self._password = None - self._existing_entry = None - self._description_placeholders = None + self._existing_entry: dict[str, Any] | None = None + self._description_placeholders: dict[str, str] | None = None - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle a flow initiated by the user.""" - errors = {} + errors: dict[str, str] = {} if user_input is None: return self._show_setup_form(user_input, errors) diff --git a/homeassistant/components/firmata/config_flow.py b/homeassistant/components/firmata/config_flow.py index 571df351b25362..4c0f800fff4262 100644 --- a/homeassistant/components/firmata/config_flow.py +++ b/homeassistant/components/firmata/config_flow.py @@ -19,9 +19,7 @@ class FirmataFlowHandler(ConfigFlow, domain=DOMAIN): VERSION = 1 - async def async_step_import( - self, import_config: dict[str, Any] - ) -> ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Import a firmata board as a config entry. This flow is triggered by `async_setup` for configured boards. @@ -30,14 +28,14 @@ async def async_step_import( config entry yet (based on entry_id). It validates a connection and then adds the entry. """ - name = f"serial-{import_config[CONF_SERIAL_PORT]}" - import_config[CONF_NAME] = name + name = f"serial-{import_data[CONF_SERIAL_PORT]}" + import_data[CONF_NAME] = name # Connect to the board to verify connection and then shutdown # If either fail then we cannot continue _LOGGER.debug("Connecting to Firmata board %s to test connection", name) try: - api = await get_board(import_config) + api = await get_board(import_data) await api.shutdown() except RuntimeError as err: _LOGGER.error("Error connecting to PyMata board %s: %s", name, err) @@ -54,6 +52,4 @@ async def async_step_import( return self.async_abort(reason="cannot_connect") _LOGGER.debug("Connection test to Firmata board %s successful", name) - return self.async_create_entry( - title=import_config[CONF_NAME], data=import_config - ) + return self.async_create_entry(title=import_data[CONF_NAME], data=import_data) diff --git a/homeassistant/components/fitbit/config_flow.py b/homeassistant/components/fitbit/config_flow.py index 0ae1973b5fb3a5..eff4ba37773afc 100644 --- a/homeassistant/components/fitbit/config_flow.py +++ b/homeassistant/components/fitbit/config_flow.py @@ -93,6 +93,6 @@ async def async_oauth_create_entry(self, data: dict[str, Any]) -> ConfigFlowResu self._abort_if_unique_id_configured() return self.async_create_entry(title=profile.display_name, data=data) - async def async_step_import(self, data: dict[str, Any]) -> ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Handle import from YAML.""" - return await self.async_oauth_create_entry(data) + return await self.async_oauth_create_entry(import_data) diff --git a/homeassistant/components/flick_electric/config_flow.py b/homeassistant/components/flick_electric/config_flow.py index 7fe5fda3f4ee6f..8a2455b9d14f23 100644 --- a/homeassistant/components/flick_electric/config_flow.py +++ b/homeassistant/components/flick_electric/config_flow.py @@ -2,12 +2,13 @@ import asyncio import logging +from typing import Any from pyflick.authentication import AuthException, SimpleFlickAuth from pyflick.const import DEFAULT_CLIENT_ID, DEFAULT_CLIENT_SECRET import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import ( CONF_CLIENT_ID, CONF_CLIENT_SECRET, @@ -55,7 +56,9 @@ async def _validate_input(self, user_input): return token is not None - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle gathering login info.""" errors = {} if user_input is not None: diff --git a/homeassistant/components/flipr/manifest.json b/homeassistant/components/flipr/manifest.json index 898cd640349b3a..cdd03770bab7f2 100644 --- a/homeassistant/components/flipr/manifest.json +++ b/homeassistant/components/flipr/manifest.json @@ -6,5 +6,5 @@ "documentation": "https://www.home-assistant.io/integrations/flipr", "iot_class": "cloud_polling", "loggers": ["flipr_api"], - "requirements": ["flipr-api==1.5.1"] + "requirements": ["flipr-api==1.6.1"] } diff --git a/homeassistant/components/flo/config_flow.py b/homeassistant/components/flo/config_flow.py index ec92b60c7406fb..bd524c590fad74 100644 --- a/homeassistant/components/flo/config_flow.py +++ b/homeassistant/components/flo/config_flow.py @@ -1,10 +1,12 @@ """Config flow for flo integration.""" +from typing import Any + from aioflo import async_get_api from aioflo.errors import RequestError import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError @@ -36,7 +38,9 @@ class FloConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" errors = {} if user_input is not None: diff --git a/homeassistant/components/flo/icons.json b/homeassistant/components/flo/icons.json index 3164781c1b4d0f..4bd0380c56caf6 100644 --- a/homeassistant/components/flo/icons.json +++ b/homeassistant/components/flo/icons.json @@ -10,9 +10,17 @@ } }, "services": { - "set_sleep_mode": "mdi:sleep", - "set_away_mode": "mdi:home-off", - "set_home_mode": "mdi:home", - "run_health_test": "mdi:heart-flash" + "set_sleep_mode": { + "service": "mdi:sleep" + }, + "set_away_mode": { + "service": "mdi:home-off" + }, + "set_home_mode": { + "service": "mdi:home" + }, + "run_health_test": { + "service": "mdi:heart-flash" + } } } diff --git a/homeassistant/components/flume/icons.json b/homeassistant/components/flume/icons.json index 631c0645ed3f7e..90830943689231 100644 --- a/homeassistant/components/flume/icons.json +++ b/homeassistant/components/flume/icons.json @@ -10,6 +10,8 @@ } }, "services": { - "list_notifications": "mdi:bell" + "list_notifications": { + "service": "mdi:bell" + } } } diff --git a/homeassistant/components/flux_led/icons.json b/homeassistant/components/flux_led/icons.json index 873fcd7c441033..07c27869ff7128 100644 --- a/homeassistant/components/flux_led/icons.json +++ b/homeassistant/components/flux_led/icons.json @@ -54,8 +54,14 @@ } }, "services": { - "set_custom_effect": "mdi:creation", - "set_zones": "mdi:texture-box", - "set_music_mode": "mdi:music" + "set_custom_effect": { + "service": "mdi:creation" + }, + "set_zones": { + "service": "mdi:texture-box" + }, + "set_music_mode": { + "service": "mdi:music" + } } } diff --git a/homeassistant/components/forked_daapd/config_flow.py b/homeassistant/components/forked_daapd/config_flow.py index 7edf25a25958df..5f061aa4be187e 100644 --- a/homeassistant/components/forked_daapd/config_flow.py +++ b/homeassistant/components/forked_daapd/config_flow.py @@ -2,6 +2,7 @@ from contextlib import suppress import logging +from typing import Any from pyforked_daapd import ForkedDaapdAPI import voluptuous as vol @@ -55,7 +56,9 @@ def __init__(self, config_entry: ConfigEntry) -> None: """Initialize.""" self.config_entry = config_entry - async def async_step_init(self, user_input=None): + async def async_step_init( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Manage the options.""" if user_input is not None: return self.async_create_entry(title="options", data=user_input) @@ -135,7 +138,9 @@ async def validate_input(self, user_input): ) return validate_result - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle a forked-daapd config flow start. Manage device specific parameters. diff --git a/homeassistant/components/foscam/config_flow.py b/homeassistant/components/foscam/config_flow.py index 8a005f19f0939f..19c19a1a5f5bcc 100644 --- a/homeassistant/components/foscam/config_flow.py +++ b/homeassistant/components/foscam/config_flow.py @@ -1,5 +1,7 @@ """Config flow for foscam integration.""" +from typing import Any + from libpyfoscam import FoscamCamera from libpyfoscam.foscam import ( ERROR_FOSCAM_AUTH, @@ -8,7 +10,7 @@ ) import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import ( CONF_HOST, CONF_NAME, @@ -90,7 +92,9 @@ async def _validate_and_create(self, data): return self.async_create_entry(title=name, data=data) - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" errors = {} diff --git a/homeassistant/components/foscam/icons.json b/homeassistant/components/foscam/icons.json index 0c7dba9a4dfb09..437575024d1708 100644 --- a/homeassistant/components/foscam/icons.json +++ b/homeassistant/components/foscam/icons.json @@ -1,6 +1,10 @@ { "services": { - "ptz": "mdi:pan", - "ptz_preset": "mdi:target-variant" + "ptz": { + "service": "mdi:pan" + }, + "ptz_preset": { + "service": "mdi:target-variant" + } } } diff --git a/homeassistant/components/foursquare/icons.json b/homeassistant/components/foursquare/icons.json index cf60ed9f247dda..8e2b4e91d5f7a3 100644 --- a/homeassistant/components/foursquare/icons.json +++ b/homeassistant/components/foursquare/icons.json @@ -1,5 +1,7 @@ { "services": { - "checkin": "mdi:map-marker" + "checkin": { + "service": "mdi:map-marker" + } } } diff --git a/homeassistant/components/freebox/icons.json b/homeassistant/components/freebox/icons.json index 81361d2c990482..f4184f0673ec06 100644 --- a/homeassistant/components/freebox/icons.json +++ b/homeassistant/components/freebox/icons.json @@ -1,5 +1,7 @@ { "services": { - "reboot": "mdi:restart" + "reboot": { + "service": "mdi:restart" + } } } diff --git a/homeassistant/components/freedompro/config_flow.py b/homeassistant/components/freedompro/config_flow.py index f1dd9dbbf14207..48d075f8a87e4b 100644 --- a/homeassistant/components/freedompro/config_flow.py +++ b/homeassistant/components/freedompro/config_flow.py @@ -1,9 +1,11 @@ """Config flow to configure Freedompro.""" +from typing import Any + from pyfreedompro import get_list import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_API_KEY from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError @@ -17,19 +19,19 @@ class Hub: """Freedompro Hub class.""" - def __init__(self, hass, api_key): + def __init__(self, hass: HomeAssistant, api_key: str) -> None: """Freedompro Hub class init.""" self._hass = hass self._api_key = api_key - async def authenticate(self): + async def authenticate(self) -> dict[str, Any]: """Freedompro Hub class authenticate.""" return await get_list( aiohttp_client.async_get_clientsession(self._hass), self._api_key ) -async def validate_input(hass: HomeAssistant, api_key): +async def validate_input(hass: HomeAssistant, api_key: str) -> None: """Validate api key.""" hub = Hub(hass, api_key) result = await hub.authenticate() @@ -45,7 +47,9 @@ class FreedomProConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Show the setup form to the user.""" if user_input is None: return self.async_show_form( diff --git a/homeassistant/components/fritz/icons.json b/homeassistant/components/fritz/icons.json index d2154dc7232f12..481568a4c2ce33 100644 --- a/homeassistant/components/fritz/icons.json +++ b/homeassistant/components/fritz/icons.json @@ -51,9 +51,17 @@ } }, "services": { - "reconnect": "mdi:connection", - "reboot": "mdi:refresh", - "cleanup": "mdi:broom", - "set_guest_wifi_password": "mdi:form-textbox-password" + "reconnect": { + "service": "mdi:connection" + }, + "reboot": { + "service": "mdi:refresh" + }, + "cleanup": { + "service": "mdi:broom" + }, + "set_guest_wifi_password": { + "service": "mdi:form-textbox-password" + } } } diff --git a/homeassistant/components/fritz/strings.json b/homeassistant/components/fritz/strings.json index 3b6c60ed48fc1b..6be393cc63601d 100644 --- a/homeassistant/components/fritz/strings.json +++ b/homeassistant/components/fritz/strings.json @@ -7,7 +7,8 @@ "description": "Discovered FRITZ!Box: {name}\n\nSet up FRITZ!Box Tools to control your {name}", "data": { "username": "[%key:common::config_flow::data::username%]", - "password": "[%key:common::config_flow::data::password%]" + "password": "[%key:common::config_flow::data::password%]", + "ssl": "[%key:common::config_flow::data::ssl%]" } }, "reauth_confirm": { diff --git a/homeassistant/components/frontend/icons.json b/homeassistant/components/frontend/icons.json index 9fbe4d5b9b0901..b4bcdef6194891 100644 --- a/homeassistant/components/frontend/icons.json +++ b/homeassistant/components/frontend/icons.json @@ -1,6 +1,10 @@ { "services": { - "set_theme": "mdi:palette-swatch", - "reload_themes": "mdi:reload" + "set_theme": { + "service": "mdi:palette-swatch" + }, + "reload_themes": { + "service": "mdi:reload" + } } } diff --git a/homeassistant/components/frontend/manifest.json b/homeassistant/components/frontend/manifest.json index 035b087e48124f..fbdafe6025d181 100644 --- a/homeassistant/components/frontend/manifest.json +++ b/homeassistant/components/frontend/manifest.json @@ -20,5 +20,5 @@ "documentation": "https://www.home-assistant.io/integrations/frontend", "integration_type": "system", "quality_scale": "internal", - "requirements": ["home-assistant-frontend==20240809.0"] + "requirements": ["home-assistant-frontend==20240904.0"] } diff --git a/homeassistant/components/frontier_silicon/config_flow.py b/homeassistant/components/frontier_silicon/config_flow.py index 103323ff5758dc..8a3c5fe086f884 100644 --- a/homeassistant/components/frontier_silicon/config_flow.py +++ b/homeassistant/components/frontier_silicon/config_flow.py @@ -172,9 +172,11 @@ async def async_step_confirm( step_id="confirm", description_placeholders={"name": self._name} ) - async def async_step_reauth(self, config: Mapping[str, Any]) -> ConfigFlowResult: + async def async_step_reauth( + self, entry_data: Mapping[str, Any] + ) -> ConfigFlowResult: """Perform reauth upon an API authentication error.""" - self._webfsapi_url = config[CONF_WEBFSAPI_URL] + self._webfsapi_url = entry_data[CONF_WEBFSAPI_URL] self._reauth_entry = self.hass.config_entries.async_get_entry( self.context["entry_id"] diff --git a/homeassistant/components/fujitsu_fglair/climate.py b/homeassistant/components/fujitsu_fglair/climate.py index 558f4b73a18998..726096eab1a9e4 100644 --- a/homeassistant/components/fujitsu_fglair/climate.py +++ b/homeassistant/components/fujitsu_fglair/climate.py @@ -2,9 +2,23 @@ from typing import Any -from ayla_iot_unofficial.fujitsu_hvac import Capability, FujitsuHVAC +from ayla_iot_unofficial.fujitsu_hvac import ( + Capability, + FanSpeed, + FujitsuHVAC, + OpMode, + SwingMode, +) from homeassistant.components.climate import ( + FAN_AUTO, + FAN_HIGH, + FAN_LOW, + FAN_MEDIUM, + SWING_BOTH, + SWING_HORIZONTAL, + SWING_OFF, + SWING_VERTICAL, ClimateEntity, ClimateEntityFeature, HVACMode, @@ -16,17 +30,35 @@ from homeassistant.helpers.update_coordinator import CoordinatorEntity from . import FGLairConfigEntry -from .const import ( - DOMAIN, - FUJI_TO_HA_FAN, - FUJI_TO_HA_HVAC, - FUJI_TO_HA_SWING, - HA_TO_FUJI_FAN, - HA_TO_FUJI_HVAC, - HA_TO_FUJI_SWING, -) +from .const import DOMAIN from .coordinator import FGLairCoordinator +HA_TO_FUJI_FAN = { + FAN_LOW: FanSpeed.LOW, + FAN_MEDIUM: FanSpeed.MEDIUM, + FAN_HIGH: FanSpeed.HIGH, + FAN_AUTO: FanSpeed.AUTO, +} +FUJI_TO_HA_FAN = {value: key for key, value in HA_TO_FUJI_FAN.items()} + +HA_TO_FUJI_HVAC = { + HVACMode.OFF: OpMode.OFF, + HVACMode.HEAT: OpMode.HEAT, + HVACMode.COOL: OpMode.COOL, + HVACMode.HEAT_COOL: OpMode.AUTO, + HVACMode.DRY: OpMode.DRY, + HVACMode.FAN_ONLY: OpMode.FAN, +} +FUJI_TO_HA_HVAC = {value: key for key, value in HA_TO_FUJI_HVAC.items()} + +HA_TO_FUJI_SWING = { + SWING_OFF: SwingMode.OFF, + SWING_VERTICAL: SwingMode.SWING_VERTICAL, + SWING_HORIZONTAL: SwingMode.SWING_HORIZONTAL, + SWING_BOTH: SwingMode.SWING_BOTH, +} +FUJI_TO_HA_SWING = {value: key for key, value in HA_TO_FUJI_SWING.items()} + async def async_setup_entry( hass: HomeAssistant, diff --git a/homeassistant/components/fujitsu_fglair/config_flow.py b/homeassistant/components/fujitsu_fglair/config_flow.py index db1975298a8273..5021e495656ef1 100644 --- a/homeassistant/components/fujitsu_fglair/config_flow.py +++ b/homeassistant/components/fujitsu_fglair/config_flow.py @@ -70,7 +70,7 @@ async def async_step_user( self._abort_if_unique_id_configured() errors = await self._async_validate_credentials(user_input) - if len(errors) == 0: + if not errors: return self.async_create_entry( title=f"FGLair ({user_input[CONF_USERNAME]})", data=user_input, diff --git a/homeassistant/components/fujitsu_fglair/const.py b/homeassistant/components/fujitsu_fglair/const.py index 0e93361f20b4de..a9d485281a351e 100644 --- a/homeassistant/components/fujitsu_fglair/const.py +++ b/homeassistant/components/fujitsu_fglair/const.py @@ -6,19 +6,6 @@ FGLAIR_APP_ID, FGLAIR_APP_SECRET, ) -from ayla_iot_unofficial.fujitsu_hvac import FanSpeed, OpMode, SwingMode - -from homeassistant.components.climate import ( - FAN_AUTO, - FAN_HIGH, - FAN_LOW, - FAN_MEDIUM, - SWING_BOTH, - SWING_HORIZONTAL, - SWING_OFF, - SWING_VERTICAL, - HVACMode, -) API_TIMEOUT = 10 API_REFRESH = timedelta(minutes=5) @@ -26,29 +13,3 @@ DOMAIN = "fujitsu_fglair" CONF_EUROPE = "is_europe" - -HA_TO_FUJI_FAN = { - FAN_LOW: FanSpeed.LOW, - FAN_MEDIUM: FanSpeed.MEDIUM, - FAN_HIGH: FanSpeed.HIGH, - FAN_AUTO: FanSpeed.AUTO, -} -FUJI_TO_HA_FAN = {value: key for key, value in HA_TO_FUJI_FAN.items()} - -HA_TO_FUJI_HVAC = { - HVACMode.OFF: OpMode.OFF, - HVACMode.HEAT: OpMode.HEAT, - HVACMode.COOL: OpMode.COOL, - HVACMode.HEAT_COOL: OpMode.AUTO, - HVACMode.DRY: OpMode.DRY, - HVACMode.FAN_ONLY: OpMode.FAN, -} -FUJI_TO_HA_HVAC = {value: key for key, value in HA_TO_FUJI_HVAC.items()} - -HA_TO_FUJI_SWING = { - SWING_OFF: SwingMode.OFF, - SWING_VERTICAL: SwingMode.SWING_VERTICAL, - SWING_HORIZONTAL: SwingMode.SWING_HORIZONTAL, - SWING_BOTH: SwingMode.SWING_BOTH, -} -FUJI_TO_HA_SWING = {value: key for key, value in HA_TO_FUJI_SWING.items()} diff --git a/homeassistant/components/fujitsu_fglair/coordinator.py b/homeassistant/components/fujitsu_fglair/coordinator.py index 902464bdd800fa..eac3cfd6ce5908 100644 --- a/homeassistant/components/fujitsu_fglair/coordinator.py +++ b/homeassistant/components/fujitsu_fglair/coordinator.py @@ -47,12 +47,12 @@ async def _async_update_data(self) -> dict[str, FujitsuHVAC]: except AylaAuthError as e: raise ConfigEntryAuthFailed("Credentials expired for Ayla IoT API") from e - if len(listening_entities) == 0: - devices = list(filter(lambda x: isinstance(x, FujitsuHVAC), devices)) + if not listening_entities: + devices = [dev for dev in devices if isinstance(dev, FujitsuHVAC)] else: - devices = list( - filter(lambda x: x.device_serial_number in listening_entities, devices) - ) + devices = [ + dev for dev in devices if dev.device_serial_number in listening_entities + ] try: for dev in devices: diff --git a/homeassistant/components/fully_kiosk/icons.json b/homeassistant/components/fully_kiosk/icons.json index 760698f7ac840f..0166679abe2848 100644 --- a/homeassistant/components/fully_kiosk/icons.json +++ b/homeassistant/components/fully_kiosk/icons.json @@ -1,7 +1,13 @@ { "services": { - "load_url": "mdi:link", - "set_config": "mdi:cog", - "start_application": "mdi:rocket-launch" + "load_url": { + "service": "mdi:link" + }, + "set_config": { + "service": "mdi:cog" + }, + "start_application": { + "service": "mdi:rocket-launch" + } } } diff --git a/homeassistant/components/fyta/manifest.json b/homeassistant/components/fyta/manifest.json index 07387f4ab0569e..dbd44ed34dc1db 100644 --- a/homeassistant/components/fyta/manifest.json +++ b/homeassistant/components/fyta/manifest.json @@ -7,5 +7,5 @@ "integration_type": "hub", "iot_class": "cloud_polling", "quality_scale": "platinum", - "requirements": ["fyta_cli==0.6.0"] + "requirements": ["fyta_cli==0.6.6"] } diff --git a/homeassistant/components/fyta/sensor.py b/homeassistant/components/fyta/sensor.py index 262d0b6d1f4f7a..a351d79dd8bbae 100644 --- a/homeassistant/components/fyta/sensor.py +++ b/homeassistant/components/fyta/sensor.py @@ -145,7 +145,7 @@ async def async_setup_entry( FytaPlantSensor(coordinator, entry, sensor, plant_id) for plant_id in coordinator.fyta.plant_list for sensor in SENSORS - if sensor.key in dir(coordinator.data[plant_id]) + if sensor.key in dir(coordinator.data.get(plant_id)) ] async_add_entities(plant_entities) diff --git a/homeassistant/components/gardena_bluetooth/manifest.json b/homeassistant/components/gardena_bluetooth/manifest.json index 4812def7dde4e2..6d7566b3edfd6e 100644 --- a/homeassistant/components/gardena_bluetooth/manifest.json +++ b/homeassistant/components/gardena_bluetooth/manifest.json @@ -14,5 +14,5 @@ "documentation": "https://www.home-assistant.io/integrations/gardena_bluetooth", "iot_class": "local_polling", "loggers": ["bleak", "bleak_esphome", "gardena_bluetooth"], - "requirements": ["gardena-bluetooth==1.4.2"] + "requirements": ["gardena-bluetooth==1.4.3"] } diff --git a/homeassistant/components/gdacs/diagnostics.py b/homeassistant/components/gdacs/diagnostics.py new file mode 100644 index 00000000000000..435e28ca1ae14b --- /dev/null +++ b/homeassistant/components/gdacs/diagnostics.py @@ -0,0 +1,39 @@ +"""Diagnostics support for GDACS integration.""" + +from __future__ import annotations + +from typing import Any + +from aio_georss_client.status_update import StatusUpdate + +from homeassistant.components.diagnostics import async_redact_data +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE +from homeassistant.core import HomeAssistant + +from . import GdacsFeedEntityManager +from .const import DOMAIN, FEED + +TO_REDACT = {CONF_LATITUDE, CONF_LONGITUDE} + + +async def async_get_config_entry_diagnostics( + hass: HomeAssistant, config_entry: ConfigEntry +) -> dict[str, Any]: + """Return diagnostics for a config entry.""" + data: dict[str, Any] = { + "info": async_redact_data(config_entry.data, TO_REDACT), + } + + manager: GdacsFeedEntityManager = hass.data[DOMAIN][FEED][config_entry.entry_id] + status_info: StatusUpdate = manager.status_info() + if status_info: + data["service"] = { + "status": status_info.status, + "total": status_info.total, + "last_update": status_info.last_update, + "last_update_successful": status_info.last_update_successful, + "last_timestamp": status_info.last_timestamp, + } + + return data diff --git a/homeassistant/components/gdacs/manifest.json b/homeassistant/components/gdacs/manifest.json index d743dd004247f3..fab47e0090431e 100644 --- a/homeassistant/components/gdacs/manifest.json +++ b/homeassistant/components/gdacs/manifest.json @@ -8,5 +8,5 @@ "iot_class": "cloud_polling", "loggers": ["aio_georss_gdacs", "aio_georss_client"], "quality_scale": "platinum", - "requirements": ["aio-georss-gdacs==0.9"] + "requirements": ["aio-georss-gdacs==0.10"] } diff --git a/homeassistant/components/generic/icons.json b/homeassistant/components/generic/icons.json index a03163179cb5bd..a9829425570a0c 100644 --- a/homeassistant/components/generic/icons.json +++ b/homeassistant/components/generic/icons.json @@ -1,5 +1,7 @@ { "services": { - "reload": "mdi:reload" + "reload": { + "service": "mdi:reload" + } } } diff --git a/homeassistant/components/generic_thermostat/icons.json b/homeassistant/components/generic_thermostat/icons.json index a03163179cb5bd..a9829425570a0c 100644 --- a/homeassistant/components/generic_thermostat/icons.json +++ b/homeassistant/components/generic_thermostat/icons.json @@ -1,5 +1,7 @@ { "services": { - "reload": "mdi:reload" + "reload": { + "service": "mdi:reload" + } } } diff --git a/homeassistant/components/geniushub/config_flow.py b/homeassistant/components/geniushub/config_flow.py index 5f026c91ee10c6..601eac6c2f2863 100644 --- a/homeassistant/components/geniushub/config_flow.py +++ b/homeassistant/components/geniushub/config_flow.py @@ -124,12 +124,12 @@ async def async_step_cloud_api( step_id="cloud_api", errors=errors, data_schema=CLOUD_API_SCHEMA ) - async def async_step_import(self, user_input: dict[str, Any]) -> ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Import the yaml config.""" - if CONF_HOST in user_input: - result = await self.async_step_local_api(user_input) + if CONF_HOST in import_data: + result = await self.async_step_local_api(import_data) else: - result = await self.async_step_cloud_api(user_input) + result = await self.async_step_cloud_api(import_data) if result["type"] is FlowResultType.FORM: assert result["errors"] return self.async_abort(reason=result["errors"]["base"]) diff --git a/homeassistant/components/geniushub/icons.json b/homeassistant/components/geniushub/icons.json index 41697b419a858e..c8a59dedbbdb8d 100644 --- a/homeassistant/components/geniushub/icons.json +++ b/homeassistant/components/geniushub/icons.json @@ -1,7 +1,13 @@ { "services": { - "set_zone_mode": "mdi:auto-mode", - "set_zone_override": "mdi:thermometer-lines", - "set_switch_override": "mdi:toggle-switch-variant" + "set_zone_mode": { + "service": "mdi:auto-mode" + }, + "set_zone_override": { + "service": "mdi:thermometer-lines" + }, + "set_switch_override": { + "service": "mdi:toggle-switch-variant" + } } } diff --git a/homeassistant/components/geonetnz_quakes/config_flow.py b/homeassistant/components/geonetnz_quakes/config_flow.py index 4367f820bd3681..083ac29b362624 100644 --- a/homeassistant/components/geonetnz_quakes/config_flow.py +++ b/homeassistant/components/geonetnz_quakes/config_flow.py @@ -1,10 +1,11 @@ """Config flow to configure the GeoNet NZ Quakes integration.""" import logging +from typing import Any import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import ( CONF_LATITUDE, CONF_LONGITUDE, @@ -44,11 +45,13 @@ async def _show_form(self, errors=None): step_id="user", data_schema=DATA_SCHEMA, errors=errors or {} ) - async def async_step_import(self, import_config): + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Import a config entry from configuration.yaml.""" - return await self.async_step_user(import_config) + return await self.async_step_user(import_data) - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the start of the config flow.""" _LOGGER.debug("User input: %s", user_input) if not user_input: diff --git a/homeassistant/components/geonetnz_quakes/diagnostics.py b/homeassistant/components/geonetnz_quakes/diagnostics.py new file mode 100644 index 00000000000000..fbe9bf511aa0d9 --- /dev/null +++ b/homeassistant/components/geonetnz_quakes/diagnostics.py @@ -0,0 +1,39 @@ +"""Diagnostics support for GeoNet NZ Quakes Feeds integration.""" + +from __future__ import annotations + +from typing import Any + +from homeassistant.components.diagnostics import async_redact_data +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE +from homeassistant.core import HomeAssistant + +from . import GeonetnzQuakesFeedEntityManager +from .const import DOMAIN, FEED + +TO_REDACT = {CONF_LATITUDE, CONF_LONGITUDE} + + +async def async_get_config_entry_diagnostics( + hass: HomeAssistant, config_entry: ConfigEntry +) -> dict[str, Any]: + """Return diagnostics for a config entry.""" + data: dict[str, Any] = { + "info": async_redact_data(config_entry.data, TO_REDACT), + } + + manager: GeonetnzQuakesFeedEntityManager = hass.data[DOMAIN][FEED][ + config_entry.entry_id + ] + status_info = manager.status_info() + if status_info: + data["service"] = { + "status": status_info.status, + "total": status_info.total, + "last_update": status_info.last_update, + "last_update_successful": status_info.last_update_successful, + "last_timestamp": status_info.last_timestamp, + } + + return data diff --git a/homeassistant/components/geonetnz_volcano/config_flow.py b/homeassistant/components/geonetnz_volcano/config_flow.py index 461da61ae1a9d9..cf3d5bc113926c 100644 --- a/homeassistant/components/geonetnz_volcano/config_flow.py +++ b/homeassistant/components/geonetnz_volcano/config_flow.py @@ -1,8 +1,10 @@ """Config flow to configure the GeoNet NZ Volcano integration.""" +from typing import Any + import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import ( CONF_LATITUDE, CONF_LONGITUDE, @@ -10,7 +12,7 @@ CONF_SCAN_INTERVAL, CONF_UNIT_SYSTEM, ) -from homeassistant.core import callback +from homeassistant.core import HomeAssistant, callback from homeassistant.helpers import config_validation as cv from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM @@ -24,7 +26,7 @@ @callback -def configured_instances(hass): +def configured_instances(hass: HomeAssistant) -> set[str]: """Return a set of configured GeoNet NZ Volcano instances.""" return { f"{entry.data[CONF_LATITUDE]}, {entry.data[CONF_LONGITUDE]}" @@ -45,11 +47,13 @@ async def _show_form(self, errors=None): step_id="user", data_schema=data_schema, errors=errors or {} ) - async def async_step_import(self, import_config): + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Import a config entry from configuration.yaml.""" - return await self.async_step_user(import_config) + return await self.async_step_user(import_data) - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the start of the config flow.""" if not user_input: return await self._show_form() diff --git a/homeassistant/components/goodwe/config_flow.py b/homeassistant/components/goodwe/config_flow.py index d6a3be7e56a057..354877e782f5f8 100644 --- a/homeassistant/components/goodwe/config_flow.py +++ b/homeassistant/components/goodwe/config_flow.py @@ -3,6 +3,7 @@ from __future__ import annotations import logging +from typing import Any from goodwe import InverterError, connect import voluptuous as vol @@ -26,7 +27,9 @@ class GoodweFlowHandler(ConfigFlow, domain=DOMAIN): VERSION = 1 - async def async_step_user(self, user_input: dict | None = None) -> ConfigFlowResult: + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle a flow initialized by the user.""" errors = {} if user_input is not None: diff --git a/homeassistant/components/google/config_flow.py b/homeassistant/components/google/config_flow.py index 6207303c8a60d2..98424ef24f55c1 100644 --- a/homeassistant/components/google/config_flow.py +++ b/homeassistant/components/google/config_flow.py @@ -94,18 +94,6 @@ def extra_authorize_data(self) -> dict[str, Any]: "prompt": "consent", } - async def async_step_import(self, info: dict[str, Any]) -> ConfigFlowResult: - """Import existing auth into a new config entry.""" - if self._async_current_entries(): - return self.async_abort(reason="single_instance_allowed") - implementations = await config_entry_oauth2_flow.async_get_implementations( - self.hass, self.DOMAIN - ) - assert len(implementations) == 1 - self.flow_impl = list(implementations.values())[0] - self.external_data = info - return await super().async_step_creation(info) - async def async_step_auth( self, user_input: dict[str, Any] | None = None ) -> ConfigFlowResult: diff --git a/homeassistant/components/google/icons.json b/homeassistant/components/google/icons.json index 6dbad61b43da04..e4f25442546c66 100644 --- a/homeassistant/components/google/icons.json +++ b/homeassistant/components/google/icons.json @@ -1,6 +1,10 @@ { "services": { - "add_event": "mdi:calendar-plus", - "create_event": "mdi:calendar-plus" + "add_event": { + "service": "mdi:calendar-plus" + }, + "create_event": { + "service": "mdi:calendar-plus" + } } } diff --git a/homeassistant/components/google_assistant/config_flow.py b/homeassistant/components/google_assistant/config_flow.py index 9504c623138da6..5934657f9ae120 100644 --- a/homeassistant/components/google_assistant/config_flow.py +++ b/homeassistant/components/google_assistant/config_flow.py @@ -1,6 +1,8 @@ """Config flow for google assistant component.""" -from homeassistant.config_entries import ConfigFlow +from typing import Any + +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from .const import CONF_PROJECT_ID, DOMAIN @@ -10,10 +12,10 @@ class GoogleAssistantHandler(ConfigFlow, domain=DOMAIN): VERSION = 1 - async def async_step_import(self, user_input): + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Import a config entry.""" - await self.async_set_unique_id(unique_id=user_input[CONF_PROJECT_ID]) + await self.async_set_unique_id(unique_id=import_data[CONF_PROJECT_ID]) self._abort_if_unique_id_configured() return self.async_create_entry( - title=user_input[CONF_PROJECT_ID], data=user_input + title=import_data[CONF_PROJECT_ID], data=import_data ) diff --git a/homeassistant/components/google_assistant/helpers.py b/homeassistant/components/google_assistant/helpers.py index 7f8f7a68ffa5c6..76869487ee3e05 100644 --- a/homeassistant/components/google_assistant/helpers.py +++ b/homeassistant/components/google_assistant/helpers.py @@ -521,7 +521,7 @@ def supported_traits_for_state(state: State) -> list[type[trait._Trait]]: class GoogleEntity: """Adaptation of Entity expressed in Google's terms.""" - __slots__ = ("hass", "config", "state", "_traits") + __slots__ = ("hass", "config", "state", "entity_id", "_traits") def __init__( self, hass: HomeAssistant, config: AbstractConfig, state: State @@ -530,17 +530,13 @@ def __init__( self.hass = hass self.config = config self.state = state + self.entity_id = state.entity_id self._traits: list[trait._Trait] | None = None def __repr__(self) -> str: """Return the representation.""" return f"" - @property - def entity_id(self): - """Return entity ID.""" - return self.state.entity_id - @callback def traits(self) -> list[trait._Trait]: """Return traits for entity.""" diff --git a/homeassistant/components/google_assistant/icons.json b/homeassistant/components/google_assistant/icons.json index 3bcab03d2c240b..a522103328a99b 100644 --- a/homeassistant/components/google_assistant/icons.json +++ b/homeassistant/components/google_assistant/icons.json @@ -1,5 +1,7 @@ { "services": { - "request_sync": "mdi:sync" + "request_sync": { + "service": "mdi:sync" + } } } diff --git a/homeassistant/components/google_assistant_sdk/icons.json b/homeassistant/components/google_assistant_sdk/icons.json index bf1420b2e3febf..75747c43f5b3a1 100644 --- a/homeassistant/components/google_assistant_sdk/icons.json +++ b/homeassistant/components/google_assistant_sdk/icons.json @@ -1,5 +1,7 @@ { "services": { - "send_text_command": "mdi:comment-text-outline" + "send_text_command": { + "service": "mdi:comment-text-outline" + } } } diff --git a/homeassistant/components/google_cloud/__init__.py b/homeassistant/components/google_cloud/__init__.py index 97b669245d2de8..9d1923fd87da81 100644 --- a/homeassistant/components/google_cloud/__init__.py +++ b/homeassistant/components/google_cloud/__init__.py @@ -1 +1,26 @@ """The google_cloud component.""" + +from __future__ import annotations + +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant + +PLATFORMS = [Platform.STT, Platform.TTS] + + +async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: + """Set up a config entry.""" + await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) + entry.async_on_unload(entry.add_update_listener(async_update_options)) + return True + + +async def async_update_options(hass: HomeAssistant, entry: ConfigEntry) -> None: + """Handle options update.""" + await hass.config_entries.async_reload(entry.entry_id) + + +async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: + """Unload a config entry.""" + return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) diff --git a/homeassistant/components/google_cloud/config_flow.py b/homeassistant/components/google_cloud/config_flow.py new file mode 100644 index 00000000000000..dec849de4e6ba3 --- /dev/null +++ b/homeassistant/components/google_cloud/config_flow.py @@ -0,0 +1,187 @@ +"""Config flow for the Google Cloud integration.""" + +from __future__ import annotations + +import json +import logging +from typing import TYPE_CHECKING, Any, cast + +from google.cloud import texttospeech +import voluptuous as vol + +from homeassistant.components.file_upload import process_uploaded_file +from homeassistant.components.tts import CONF_LANG +from homeassistant.config_entries import ( + ConfigEntry, + ConfigFlow, + ConfigFlowResult, + OptionsFlowWithConfigEntry, +) +from homeassistant.core import callback +from homeassistant.helpers.selector import ( + FileSelector, + FileSelectorConfig, + SelectSelector, + SelectSelectorConfig, + SelectSelectorMode, +) + +from .const import ( + CONF_KEY_FILE, + CONF_SERVICE_ACCOUNT_INFO, + CONF_STT_MODEL, + DEFAULT_LANG, + DEFAULT_STT_MODEL, + DOMAIN, + SUPPORTED_STT_MODELS, + TITLE, +) +from .helpers import ( + async_tts_voices, + tts_options_schema, + tts_platform_schema, + validate_service_account_info, +) + +_LOGGER = logging.getLogger(__name__) + +UPLOADED_KEY_FILE = "uploaded_key_file" + +STEP_USER_DATA_SCHEMA = vol.Schema( + { + vol.Required(UPLOADED_KEY_FILE): FileSelector( + FileSelectorConfig(accept=".json,application/json") + ) + } +) + + +class GoogleCloudConfigFlow(ConfigFlow, domain=DOMAIN): + """Handle a config flow for Google Cloud integration.""" + + VERSION = 1 + + _name: str | None = None + entry: ConfigEntry | None = None + abort_reason: str | None = None + + def _parse_uploaded_file(self, uploaded_file_id: str) -> dict[str, Any]: + """Read and parse an uploaded JSON file.""" + with process_uploaded_file(self.hass, uploaded_file_id) as file_path: + contents = file_path.read_text() + return cast(dict[str, Any], json.loads(contents)) + + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle the initial step.""" + errors: dict[str, Any] = {} + if user_input is not None: + try: + service_account_info = await self.hass.async_add_executor_job( + self._parse_uploaded_file, user_input[UPLOADED_KEY_FILE] + ) + validate_service_account_info(service_account_info) + except ValueError: + _LOGGER.exception("Reading uploaded JSON file failed") + errors["base"] = "invalid_file" + else: + data = {CONF_SERVICE_ACCOUNT_INFO: service_account_info} + if self.entry: + if TYPE_CHECKING: + assert self.abort_reason + return self.async_update_reload_and_abort( + self.entry, data=data, reason=self.abort_reason + ) + return self.async_create_entry(title=TITLE, data=data) + return self.async_show_form( + step_id="user", + data_schema=STEP_USER_DATA_SCHEMA, + errors=errors, + description_placeholders={ + "url": "https://console.cloud.google.com/apis/credentials/serviceaccountkey" + }, + ) + + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: + """Import Google Cloud configuration from YAML.""" + + def _read_key_file() -> dict[str, Any]: + with open( + self.hass.config.path(import_data[CONF_KEY_FILE]), encoding="utf8" + ) as f: + return cast(dict[str, Any], json.load(f)) + + service_account_info = await self.hass.async_add_executor_job(_read_key_file) + try: + validate_service_account_info(service_account_info) + except ValueError: + _LOGGER.exception("Reading credentials JSON file failed") + return self.async_abort(reason="invalid_file") + options = { + k: v for k, v in import_data.items() if k in tts_platform_schema().schema + } + options.pop(CONF_KEY_FILE) + _LOGGER.debug("Creating imported config entry with options: %s", options) + return self.async_create_entry( + title=TITLE, + data={CONF_SERVICE_ACCOUNT_INFO: service_account_info}, + options=options, + ) + + @staticmethod + @callback + def async_get_options_flow( + config_entry: ConfigEntry, + ) -> GoogleCloudOptionsFlowHandler: + """Create the options flow.""" + return GoogleCloudOptionsFlowHandler(config_entry) + + +class GoogleCloudOptionsFlowHandler(OptionsFlowWithConfigEntry): + """Google Cloud options flow.""" + + async def async_step_init( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Manage the options.""" + if user_input is not None: + return self.async_create_entry(data=user_input) + + service_account_info = self.config_entry.data[CONF_SERVICE_ACCOUNT_INFO] + client: texttospeech.TextToSpeechAsyncClient = ( + texttospeech.TextToSpeechAsyncClient.from_service_account_info( + service_account_info + ) + ) + voices = await async_tts_voices(client) + return self.async_show_form( + step_id="init", + data_schema=self.add_suggested_values_to_schema( + vol.Schema( + { + vol.Optional( + CONF_LANG, + default=DEFAULT_LANG, + ): SelectSelector( + SelectSelectorConfig( + mode=SelectSelectorMode.DROPDOWN, options=list(voices) + ) + ), + **tts_options_schema( + self.options, voices, from_config_flow=True + ).schema, + vol.Optional( + CONF_STT_MODEL, + default=DEFAULT_STT_MODEL, + ): SelectSelector( + SelectSelectorConfig( + mode=SelectSelectorMode.DROPDOWN, + options=SUPPORTED_STT_MODELS, + ) + ), + } + ), + self.options, + ), + ) diff --git a/homeassistant/components/google_cloud/const.py b/homeassistant/components/google_cloud/const.py index 0fbd5e782744fd..f416d36483ac3e 100644 --- a/homeassistant/components/google_cloud/const.py +++ b/homeassistant/components/google_cloud/const.py @@ -2,10 +2,15 @@ from __future__ import annotations +DOMAIN = "google_cloud" +TITLE = "Google Cloud" + +CONF_SERVICE_ACCOUNT_INFO = "service_account_info" CONF_KEY_FILE = "key_file" DEFAULT_LANG = "en-US" +# TTS constants CONF_GENDER = "gender" CONF_VOICE = "voice" CONF_ENCODING = "encoding" @@ -14,3 +19,166 @@ CONF_GAIN = "gain" CONF_PROFILES = "profiles" CONF_TEXT_TYPE = "text_type" + +# STT constants +CONF_STT_MODEL = "stt_model" + +DEFAULT_STT_MODEL = "latest_short" + +# https://cloud.google.com/speech-to-text/docs/transcription-model +SUPPORTED_STT_MODELS = [ + "latest_long", + "latest_short", + "telephony", + "telephony_short", + "medical_dictation", + "medical_conversation", + "command_and_search", + "default", + "phone_call", + "video", +] + +# https://cloud.google.com/speech-to-text/docs/speech-to-text-supported-languages +STT_LANGUAGES = [ + "af-ZA", + "am-ET", + "ar-AE", + "ar-BH", + "ar-DZ", + "ar-EG", + "ar-IL", + "ar-IQ", + "ar-JO", + "ar-KW", + "ar-LB", + "ar-MA", + "ar-MR", + "ar-OM", + "ar-PS", + "ar-QA", + "ar-SA", + "ar-SY", + "ar-TN", + "ar-YE", + "az-AZ", + "bg-BG", + "bn-BD", + "bn-IN", + "bs-BA", + "ca-ES", + "cmn-Hans-CN", + "cmn-Hans-HK", + "cmn-Hant-TW", + "cs-CZ", + "da-DK", + "de-AT", + "de-CH", + "de-DE", + "el-GR", + "en-AU", + "en-CA", + "en-GB", + "en-GH", + "en-HK", + "en-IE", + "en-IN", + "en-KE", + "en-NG", + "en-NZ", + "en-PH", + "en-PK", + "en-SG", + "en-TZ", + "en-US", + "en-ZA", + "es-AR", + "es-BO", + "es-CL", + "es-CO", + "es-CR", + "es-DO", + "es-EC", + "es-ES", + "es-GT", + "es-HN", + "es-MX", + "es-NI", + "es-PA", + "es-PE", + "es-PR", + "es-PY", + "es-SV", + "es-US", + "es-UY", + "es-VE", + "et-EE", + "eu-ES", + "fa-IR", + "fi-FI", + "fil-PH", + "fr-BE", + "fr-CA", + "fr-CH", + "fr-FR", + "gl-ES", + "gu-IN", + "hi-IN", + "hr-HR", + "hu-HU", + "hy-AM", + "id-ID", + "is-IS", + "it-CH", + "it-IT", + "iw-IL", + "ja-JP", + "jv-ID", + "ka-GE", + "kk-KZ", + "km-KH", + "kn-IN", + "ko-KR", + "lo-LA", + "lt-LT", + "lv-LV", + "mk-MK", + "ml-IN", + "mn-MN", + "mr-IN", + "ms-MY", + "my-MM", + "ne-NP", + "nl-BE", + "nl-NL", + "no-NO", + "pa-Guru-IN", + "pl-PL", + "pt-BR", + "pt-PT", + "ro-RO", + "ru-RU", + "si-LK", + "sk-SK", + "sl-SI", + "sq-AL", + "sr-RS", + "su-ID", + "sv-SE", + "sw-KE", + "sw-TZ", + "ta-IN", + "ta-LK", + "ta-MY", + "ta-SG", + "te-IN", + "th-TH", + "tr-TR", + "uk-UA", + "ur-IN", + "ur-PK", + "uz-UZ", + "vi-VN", + "yue-Hant-HK", + "zu-ZA", +] diff --git a/homeassistant/components/google_cloud/helpers.py b/homeassistant/components/google_cloud/helpers.py index 8ae6a456a4ff64..3c6141561323f8 100644 --- a/homeassistant/components/google_cloud/helpers.py +++ b/homeassistant/components/google_cloud/helpers.py @@ -2,12 +2,13 @@ from __future__ import annotations +from collections.abc import Mapping import functools import operator -from types import MappingProxyType from typing import Any from google.cloud import texttospeech +from google.oauth2.service_account import Credentials import voluptuous as vol from homeassistant.components.tts import CONF_LANG @@ -51,15 +52,20 @@ async def async_tts_voices( def tts_options_schema( - config_options: MappingProxyType[str, Any], voices: dict[str, list[str]] -): + config_options: dict[str, Any], + voices: dict[str, list[str]], + from_config_flow: bool = False, +) -> vol.Schema: """Return schema for TTS options with default values from config or constants.""" + # If we are called from the config flow we want the defaults to be from constants + # to allow clearing the current value (passed as suggested_value) in the UI. + # If we aren't called from the config flow we want the defaults to be from the config. + defaults = {} if from_config_flow else config_options return vol.Schema( { vol.Optional( CONF_GENDER, - description={"suggested_value": config_options.get(CONF_GENDER)}, - default=config_options.get( + default=defaults.get( CONF_GENDER, texttospeech.SsmlVoiceGender.NEUTRAL.name, # type: ignore[attr-defined] ), @@ -74,8 +80,7 @@ def tts_options_schema( ), vol.Optional( CONF_VOICE, - description={"suggested_value": config_options.get(CONF_VOICE)}, - default=config_options.get(CONF_VOICE, DEFAULT_VOICE), + default=defaults.get(CONF_VOICE, DEFAULT_VOICE), ): SelectSelector( SelectSelectorConfig( mode=SelectSelectorMode.DROPDOWN, @@ -84,8 +89,7 @@ def tts_options_schema( ), vol.Optional( CONF_ENCODING, - description={"suggested_value": config_options.get(CONF_ENCODING)}, - default=config_options.get( + default=defaults.get( CONF_ENCODING, texttospeech.AudioEncoding.MP3.name, # type: ignore[attr-defined] ), @@ -100,23 +104,19 @@ def tts_options_schema( ), vol.Optional( CONF_SPEED, - description={"suggested_value": config_options.get(CONF_SPEED)}, - default=config_options.get(CONF_SPEED, 1.0), + default=defaults.get(CONF_SPEED, 1.0), ): NumberSelector(NumberSelectorConfig(min=0.25, max=4.0, step=0.01)), vol.Optional( CONF_PITCH, - description={"suggested_value": config_options.get(CONF_PITCH)}, - default=config_options.get(CONF_PITCH, 0), + default=defaults.get(CONF_PITCH, 0), ): NumberSelector(NumberSelectorConfig(min=-20.0, max=20.0, step=0.1)), vol.Optional( CONF_GAIN, - description={"suggested_value": config_options.get(CONF_GAIN)}, - default=config_options.get(CONF_GAIN, 0), + default=defaults.get(CONF_GAIN, 0), ): NumberSelector(NumberSelectorConfig(min=-96.0, max=16.0, step=0.1)), vol.Optional( CONF_PROFILES, - description={"suggested_value": config_options.get(CONF_PROFILES)}, - default=config_options.get(CONF_PROFILES, []), + default=defaults.get(CONF_PROFILES, []), ): SelectSelector( SelectSelectorConfig( mode=SelectSelectorMode.DROPDOWN, @@ -137,8 +137,7 @@ def tts_options_schema( ), vol.Optional( CONF_TEXT_TYPE, - description={"suggested_value": config_options.get(CONF_TEXT_TYPE)}, - default=config_options.get(CONF_TEXT_TYPE, "text"), + default=defaults.get(CONF_TEXT_TYPE, "text"), ): vol.All( vol.Lower, SelectSelector( @@ -152,7 +151,7 @@ def tts_options_schema( ) -def tts_platform_schema(): +def tts_platform_schema() -> vol.Schema: """Return schema for TTS platform.""" return vol.Schema( { @@ -166,3 +165,16 @@ def tts_platform_schema(): ), } ) + + +def validate_service_account_info(info: Mapping[str, str]) -> None: + """Validate service account info. + + Args: + info: The service account info in Google format. + + Raises: + ValueError: If the info is not in the expected format. + + """ + Credentials.from_service_account_info(info) # type:ignore[no-untyped-call] diff --git a/homeassistant/components/google_cloud/manifest.json b/homeassistant/components/google_cloud/manifest.json index b4fc3f39b862be..3e08b6254dbc30 100644 --- a/homeassistant/components/google_cloud/manifest.json +++ b/homeassistant/components/google_cloud/manifest.json @@ -1,8 +1,14 @@ { "domain": "google_cloud", - "name": "Google Cloud Platform", - "codeowners": ["@lufton"], + "name": "Google Cloud", + "codeowners": ["@lufton", "@tronikos"], + "config_flow": true, + "dependencies": ["file_upload"], "documentation": "https://www.home-assistant.io/integrations/google_cloud", + "integration_type": "service", "iot_class": "cloud_push", - "requirements": ["google-cloud-texttospeech==2.16.3"] + "requirements": [ + "google-cloud-texttospeech==2.17.2", + "google-cloud-speech==2.27.0" + ] } diff --git a/homeassistant/components/google_cloud/strings.json b/homeassistant/components/google_cloud/strings.json new file mode 100644 index 00000000000000..3bf9d8c84898c1 --- /dev/null +++ b/homeassistant/components/google_cloud/strings.json @@ -0,0 +1,33 @@ +{ + "config": { + "step": { + "user": { + "description": "Upload your Google Cloud service account JSON file that you can create at {url}.", + "data": { + "uploaded_key_file": "Upload service account JSON file" + } + } + }, + "error": { + "invalid_file": "Invalid service account JSON file" + } + }, + "options": { + "step": { + "init": { + "data": { + "language": "Default language of the voice", + "gender": "Default gender of the voice", + "voice": "Default voice name (overrides language and gender)", + "encoding": "Default audio encoder", + "speed": "Default rate/speed of the voice", + "pitch": "Default pitch of the voice", + "gain": "Default volume gain (in dB) of the voice", + "profiles": "Default audio profiles", + "text_type": "Default text type", + "stt_model": "STT model" + } + } + } + } +} diff --git a/homeassistant/components/google_cloud/stt.py b/homeassistant/components/google_cloud/stt.py new file mode 100644 index 00000000000000..13715ae29f8b14 --- /dev/null +++ b/homeassistant/components/google_cloud/stt.py @@ -0,0 +1,147 @@ +"""Support for the Google Cloud STT service.""" + +from __future__ import annotations + +from collections.abc import AsyncGenerator, AsyncIterable +import logging + +from google.api_core.exceptions import GoogleAPIError, Unauthenticated +from google.cloud import speech_v1 + +from homeassistant.components.stt import ( + AudioBitRates, + AudioChannels, + AudioCodecs, + AudioFormats, + AudioSampleRates, + SpeechMetadata, + SpeechResult, + SpeechResultState, + SpeechToTextEntity, +) +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant +from homeassistant.helpers import device_registry as dr +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from .const import ( + CONF_SERVICE_ACCOUNT_INFO, + CONF_STT_MODEL, + DEFAULT_STT_MODEL, + DOMAIN, + STT_LANGUAGES, +) + +_LOGGER = logging.getLogger(__name__) + + +async def async_setup_entry( + hass: HomeAssistant, + config_entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up Google Cloud speech platform via config entry.""" + service_account_info = config_entry.data[CONF_SERVICE_ACCOUNT_INFO] + client = speech_v1.SpeechAsyncClient.from_service_account_info(service_account_info) + async_add_entities([GoogleCloudSpeechToTextEntity(config_entry, client)]) + + +class GoogleCloudSpeechToTextEntity(SpeechToTextEntity): + """Google Cloud STT entity.""" + + def __init__( + self, + entry: ConfigEntry, + client: speech_v1.SpeechAsyncClient, + ) -> None: + """Init Google Cloud STT entity.""" + self._attr_unique_id = f"{entry.entry_id}-stt" + self._attr_name = entry.title + self._attr_device_info = dr.DeviceInfo( + identifiers={(DOMAIN, entry.entry_id)}, + manufacturer="Google", + model="Cloud", + entry_type=dr.DeviceEntryType.SERVICE, + ) + self._entry = entry + self._client = client + self._model = entry.options.get(CONF_STT_MODEL, DEFAULT_STT_MODEL) + + @property + def supported_languages(self) -> list[str]: + """Return a list of supported languages.""" + return STT_LANGUAGES + + @property + def supported_formats(self) -> list[AudioFormats]: + """Return a list of supported formats.""" + return [AudioFormats.WAV, AudioFormats.OGG] + + @property + def supported_codecs(self) -> list[AudioCodecs]: + """Return a list of supported codecs.""" + return [AudioCodecs.PCM, AudioCodecs.OPUS] + + @property + def supported_bit_rates(self) -> list[AudioBitRates]: + """Return a list of supported bitrates.""" + return [AudioBitRates.BITRATE_16] + + @property + def supported_sample_rates(self) -> list[AudioSampleRates]: + """Return a list of supported samplerates.""" + return [AudioSampleRates.SAMPLERATE_16000] + + @property + def supported_channels(self) -> list[AudioChannels]: + """Return a list of supported channels.""" + return [AudioChannels.CHANNEL_MONO] + + async def async_process_audio_stream( + self, metadata: SpeechMetadata, stream: AsyncIterable[bytes] + ) -> SpeechResult: + """Process an audio stream to STT service.""" + streaming_config = speech_v1.StreamingRecognitionConfig( + config=speech_v1.RecognitionConfig( + encoding=( + speech_v1.RecognitionConfig.AudioEncoding.OGG_OPUS + if metadata.codec == AudioCodecs.OPUS + else speech_v1.RecognitionConfig.AudioEncoding.LINEAR16 + ), + sample_rate_hertz=metadata.sample_rate, + language_code=metadata.language, + model=self._model, + ) + ) + + async def request_generator() -> ( + AsyncGenerator[speech_v1.StreamingRecognizeRequest] + ): + # The first request must only contain a streaming_config + yield speech_v1.StreamingRecognizeRequest(streaming_config=streaming_config) + # All subsequent requests must only contain audio_content + async for audio_content in stream: + yield speech_v1.StreamingRecognizeRequest(audio_content=audio_content) + + try: + responses = await self._client.streaming_recognize( + requests=request_generator(), + timeout=10, + ) + + transcript = "" + async for response in responses: + _LOGGER.debug("response: %s", response) + if not response.results: + continue + result = response.results[0] + if not result.alternatives: + continue + transcript += response.results[0].alternatives[0].transcript + except GoogleAPIError as err: + _LOGGER.error("Error occurred during Google Cloud STT call: %s", err) + if isinstance(err, Unauthenticated): + self._entry.async_start_reauth(self.hass) + return SpeechResult(None, SpeechResultState.ERROR) + + return SpeechResult(transcript, SpeechResultState.SUCCESS) diff --git a/homeassistant/components/google_cloud/tts.py b/homeassistant/components/google_cloud/tts.py index ee9999fc4968b7..60cdfbee3abad7 100644 --- a/homeassistant/components/google_cloud/tts.py +++ b/homeassistant/components/google_cloud/tts.py @@ -1,9 +1,12 @@ """Support for the Google Cloud TTS service.""" +from __future__ import annotations + import logging -import os +from pathlib import Path +from typing import Any, cast -from google.api_core.exceptions import GoogleAPIError +from google.api_core.exceptions import GoogleAPIError, Unauthenticated from google.cloud import texttospeech import voluptuous as vol @@ -11,9 +14,15 @@ CONF_LANG, PLATFORM_SCHEMA as TTS_PLATFORM_SCHEMA, Provider, + TextToSpeechEntity, + TtsAudioType, Voice, ) +from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers import device_registry as dr +from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from .const import ( CONF_ENCODING, @@ -22,10 +31,12 @@ CONF_KEY_FILE, CONF_PITCH, CONF_PROFILES, + CONF_SERVICE_ACCOUNT_INFO, CONF_SPEED, CONF_TEXT_TYPE, CONF_VOICE, DEFAULT_LANG, + DOMAIN, ) from .helpers import async_tts_voices, tts_options_schema, tts_platform_schema @@ -34,17 +45,28 @@ PLATFORM_SCHEMA = TTS_PLATFORM_SCHEMA.extend(tts_platform_schema().schema) -async def async_get_engine(hass, config, discovery_info=None): +async def async_get_engine( + hass: HomeAssistant, + config: ConfigType, + discovery_info: DiscoveryInfoType | None = None, +) -> Provider | None: """Set up Google Cloud TTS component.""" if key_file := config.get(CONF_KEY_FILE): key_file = hass.config.path(key_file) - if not os.path.isfile(key_file): + if not Path(key_file).is_file(): _LOGGER.error("File %s doesn't exist", key_file) return None if key_file: - client = texttospeech.TextToSpeechAsyncClient.from_service_account_json( + client = texttospeech.TextToSpeechAsyncClient.from_service_account_file( key_file ) + if not hass.config_entries.async_entries(DOMAIN): + _LOGGER.debug("Creating config entry by importing: %s", config) + hass.async_create_task( + hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_IMPORT}, data=config + ) + ) else: client = texttospeech.TextToSpeechAsyncClient() try: @@ -53,7 +75,6 @@ async def async_get_engine(hass, config, discovery_info=None): _LOGGER.error("Error from calling list_voices: %s", err) return None return GoogleCloudTTSProvider( - hass, client, voices, config.get(CONF_LANG, DEFAULT_LANG), @@ -61,44 +82,75 @@ async def async_get_engine(hass, config, discovery_info=None): ) -class GoogleCloudTTSProvider(Provider): - """The Google Cloud TTS API provider.""" +async def async_setup_entry( + hass: HomeAssistant, + config_entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up Google Cloud text-to-speech.""" + service_account_info = config_entry.data[CONF_SERVICE_ACCOUNT_INFO] + client: texttospeech.TextToSpeechAsyncClient = ( + texttospeech.TextToSpeechAsyncClient.from_service_account_info( + service_account_info + ) + ) + try: + voices = await async_tts_voices(client) + except GoogleAPIError as err: + _LOGGER.error("Error from calling list_voices: %s", err) + if isinstance(err, Unauthenticated): + config_entry.async_start_reauth(hass) + return + options_schema = tts_options_schema(dict(config_entry.options), voices) + language = config_entry.options.get(CONF_LANG, DEFAULT_LANG) + async_add_entities( + [ + GoogleCloudTTSEntity( + config_entry, + client, + voices, + language, + options_schema, + ) + ] + ) + + +class BaseGoogleCloudProvider: + """The Google Cloud TTS base provider.""" def __init__( self, - hass: HomeAssistant, client: texttospeech.TextToSpeechAsyncClient, voices: dict[str, list[str]], - language, - options_schema, + language: str, + options_schema: vol.Schema, ) -> None: - """Init Google Cloud TTS service.""" - self.hass = hass - self.name = "Google Cloud TTS" + """Init Google Cloud TTS base provider.""" self._client = client self._voices = voices self._language = language self._options_schema = options_schema @property - def supported_languages(self): - """Return list of supported languages.""" + def supported_languages(self) -> list[str]: + """Return a list of supported languages.""" return list(self._voices) @property - def default_language(self): + def default_language(self) -> str: """Return the default language.""" return self._language @property - def supported_options(self): + def supported_options(self) -> list[str]: """Return a list of supported options.""" return [option.schema for option in self._options_schema.schema] @property - def default_options(self): + def default_options(self) -> dict[str, Any]: """Return a dict including default options.""" - return self._options_schema({}) + return cast(dict[str, Any], self._options_schema({})) @callback def async_get_supported_voices(self, language: str) -> list[Voice] | None: @@ -107,16 +159,23 @@ def async_get_supported_voices(self, language: str) -> list[Voice] | None: return None return [Voice(voice, voice) for voice in voices] - async def async_get_tts_audio(self, message, language, options): - """Load TTS from google.""" + async def _async_get_tts_audio( + self, + message: str, + language: str, + options: dict[str, Any], + ) -> TtsAudioType: + """Load TTS from Google Cloud.""" try: options = self._options_schema(options) except vol.Invalid as err: _LOGGER.error("Error: %s when validating options: %s", err, options) return None, None - encoding = texttospeech.AudioEncoding[options[CONF_ENCODING]] - gender = texttospeech.SsmlVoiceGender[options[CONF_GENDER]] + encoding = texttospeech.AudioEncoding(options[CONF_ENCODING]) + gender: texttospeech.SsmlVoiceGender | None = texttospeech.SsmlVoiceGender( + options[CONF_GENDER] + ) voice = options[CONF_VOICE] if voice: gender = None @@ -139,11 +198,7 @@ async def async_get_tts_audio(self, message, language, options): ), ) - try: - response = await self._client.synthesize_speech(request, timeout=10) - except GoogleAPIError as err: - _LOGGER.error("Error occurred during Google Cloud TTS call: %s", err) - return None, None + response = await self._client.synthesize_speech(request, timeout=10) if encoding == texttospeech.AudioEncoding.MP3: extension = "mp3" @@ -153,3 +208,64 @@ async def async_get_tts_audio(self, message, language, options): extension = "wav" return extension, response.audio_content + + +class GoogleCloudTTSEntity(BaseGoogleCloudProvider, TextToSpeechEntity): + """The Google Cloud TTS entity.""" + + def __init__( + self, + entry: ConfigEntry, + client: texttospeech.TextToSpeechAsyncClient, + voices: dict[str, list[str]], + language: str, + options_schema: vol.Schema, + ) -> None: + """Init Google Cloud TTS entity.""" + super().__init__(client, voices, language, options_schema) + self._attr_unique_id = f"{entry.entry_id}-tts" + self._attr_name = entry.title + self._attr_device_info = dr.DeviceInfo( + identifiers={(DOMAIN, entry.entry_id)}, + manufacturer="Google", + model="Cloud", + entry_type=dr.DeviceEntryType.SERVICE, + ) + self._entry = entry + + async def async_get_tts_audio( + self, message: str, language: str, options: dict[str, Any] + ) -> TtsAudioType: + """Load TTS from Google Cloud.""" + try: + return await self._async_get_tts_audio(message, language, options) + except GoogleAPIError as err: + _LOGGER.error("Error occurred during Google Cloud TTS call: %s", err) + if isinstance(err, Unauthenticated): + self._entry.async_start_reauth(self.hass) + return None, None + + +class GoogleCloudTTSProvider(BaseGoogleCloudProvider, Provider): + """The Google Cloud TTS API provider.""" + + def __init__( + self, + client: texttospeech.TextToSpeechAsyncClient, + voices: dict[str, list[str]], + language: str, + options_schema: vol.Schema, + ) -> None: + """Init Google Cloud TTS service.""" + super().__init__(client, voices, language, options_schema) + self.name = "Google Cloud TTS" + + async def async_get_tts_audio( + self, message: str, language: str, options: dict[str, Any] + ) -> TtsAudioType: + """Load TTS from Google Cloud.""" + try: + return await self._async_get_tts_audio(message, language, options) + except GoogleAPIError as err: + _LOGGER.error("Error occurred during Google Cloud TTS call: %s", err) + return None, None diff --git a/homeassistant/components/google_generative_ai_conversation/icons.json b/homeassistant/components/google_generative_ai_conversation/icons.json index 6544532783a053..6ac3cc3b21c57b 100644 --- a/homeassistant/components/google_generative_ai_conversation/icons.json +++ b/homeassistant/components/google_generative_ai_conversation/icons.json @@ -1,5 +1,7 @@ { "services": { - "generate_content": "mdi:receipt-text" + "generate_content": { + "service": "mdi:receipt-text" + } } } diff --git a/homeassistant/components/google_generative_ai_conversation/manifest.json b/homeassistant/components/google_generative_ai_conversation/manifest.json index 9e0dc1ddeab74d..a15da4906f8994 100644 --- a/homeassistant/components/google_generative_ai_conversation/manifest.json +++ b/homeassistant/components/google_generative_ai_conversation/manifest.json @@ -9,5 +9,5 @@ "integration_type": "service", "iot_class": "cloud_polling", "quality_scale": "platinum", - "requirements": ["google-generativeai==0.6.0"] + "requirements": ["google-generativeai==0.7.2"] } diff --git a/homeassistant/components/google_mail/icons.json b/homeassistant/components/google_mail/icons.json index 599ccffe3c71c7..d0a6eb33715f83 100644 --- a/homeassistant/components/google_mail/icons.json +++ b/homeassistant/components/google_mail/icons.json @@ -1,5 +1,7 @@ { "services": { - "set_vacation": "mdi:beach" + "set_vacation": { + "service": "mdi:beach" + } } } diff --git a/homeassistant/components/google_photos/__init__.py b/homeassistant/components/google_photos/__init__.py new file mode 100644 index 00000000000000..950995e72c0b5d --- /dev/null +++ b/homeassistant/components/google_photos/__init__.py @@ -0,0 +1,56 @@ +"""The Google Photos integration.""" + +from __future__ import annotations + +from aiohttp import ClientError, ClientResponseError +from google_photos_library_api.api import GooglePhotosLibraryApi + +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady +from homeassistant.helpers import config_entry_oauth2_flow +from homeassistant.helpers.aiohttp_client import async_get_clientsession + +from . import api +from .const import DOMAIN +from .services import async_register_services +from .types import GooglePhotosConfigEntry + +__all__ = [ + "DOMAIN", +] + + +async def async_setup_entry( + hass: HomeAssistant, entry: GooglePhotosConfigEntry +) -> bool: + """Set up Google Photos from a config entry.""" + implementation = ( + await config_entry_oauth2_flow.async_get_config_entry_implementation( + hass, entry + ) + ) + web_session = async_get_clientsession(hass) + oauth_session = config_entry_oauth2_flow.OAuth2Session(hass, entry, implementation) + auth = api.AsyncConfigEntryAuth(web_session, oauth_session) + try: + await auth.async_get_access_token() + except ClientResponseError as err: + if 400 <= err.status < 500: + raise ConfigEntryAuthFailed( + "OAuth session is not valid, reauth required" + ) from err + raise ConfigEntryNotReady from err + except ClientError as err: + raise ConfigEntryNotReady from err + entry.runtime_data = GooglePhotosLibraryApi(auth) + + async_register_services(hass) + + return True + + +async def async_unload_entry( + hass: HomeAssistant, entry: GooglePhotosConfigEntry +) -> bool: + """Unload a config entry.""" + return True diff --git a/homeassistant/components/google_photos/api.py b/homeassistant/components/google_photos/api.py new file mode 100644 index 00000000000000..35878efd7926f8 --- /dev/null +++ b/homeassistant/components/google_photos/api.py @@ -0,0 +1,44 @@ +"""API for Google Photos bound to Home Assistant OAuth.""" + +from typing import cast + +import aiohttp +from google_photos_library_api import api + +from homeassistant.const import CONF_ACCESS_TOKEN +from homeassistant.helpers import config_entry_oauth2_flow + + +class AsyncConfigEntryAuth(api.AbstractAuth): + """Provide Google Photos authentication tied to an OAuth2 based config entry.""" + + def __init__( + self, + websession: aiohttp.ClientSession, + oauth_session: config_entry_oauth2_flow.OAuth2Session, + ) -> None: + """Initialize AsyncConfigEntryAuth.""" + super().__init__(websession) + self._session = oauth_session + + async def async_get_access_token(self) -> str: + """Return a valid access token.""" + await self._session.async_ensure_token_valid() + return cast(str, self._session.token[CONF_ACCESS_TOKEN]) + + +class AsyncConfigFlowAuth(api.AbstractAuth): + """An API client used during the config flow with a fixed token.""" + + def __init__( + self, + websession: aiohttp.ClientSession, + token: str, + ) -> None: + """Initialize ConfigFlowAuth.""" + super().__init__(websession) + self._token = token + + async def async_get_access_token(self) -> str: + """Return a valid access token.""" + return self._token diff --git a/homeassistant/components/google_photos/application_credentials.py b/homeassistant/components/google_photos/application_credentials.py new file mode 100644 index 00000000000000..fc6cdbd272d9ad --- /dev/null +++ b/homeassistant/components/google_photos/application_credentials.py @@ -0,0 +1,23 @@ +"""application_credentials platform the Google Photos integration.""" + +from homeassistant.components.application_credentials import AuthorizationServer +from homeassistant.core import HomeAssistant + +from .const import OAUTH2_AUTHORIZE, OAUTH2_TOKEN + + +async def async_get_authorization_server(hass: HomeAssistant) -> AuthorizationServer: + """Return authorization server.""" + return AuthorizationServer( + authorize_url=OAUTH2_AUTHORIZE, + token_url=OAUTH2_TOKEN, + ) + + +async def async_get_description_placeholders(hass: HomeAssistant) -> dict[str, str]: + """Return description placeholders for the credentials dialog.""" + return { + "oauth_consent_url": "https://console.cloud.google.com/apis/credentials/consent", + "more_info_url": "https://www.home-assistant.io/integrations/google_photos/", + "oauth_creds_url": "https://console.cloud.google.com/apis/credentials", + } diff --git a/homeassistant/components/google_photos/config_flow.py b/homeassistant/components/google_photos/config_flow.py new file mode 100644 index 00000000000000..6b025cac6be695 --- /dev/null +++ b/homeassistant/components/google_photos/config_flow.py @@ -0,0 +1,87 @@ +"""Config flow for Google Photos.""" + +from collections.abc import Mapping +import logging +from typing import Any + +from google_photos_library_api.api import GooglePhotosLibraryApi +from google_photos_library_api.exceptions import GooglePhotosApiError + +from homeassistant.config_entries import ConfigFlowResult +from homeassistant.const import CONF_ACCESS_TOKEN, CONF_TOKEN +from homeassistant.helpers import aiohttp_client, config_entry_oauth2_flow + +from . import GooglePhotosConfigEntry, api +from .const import DOMAIN, OAUTH2_SCOPES + + +class OAuth2FlowHandler( + config_entry_oauth2_flow.AbstractOAuth2FlowHandler, domain=DOMAIN +): + """Config flow to handle Google Photos OAuth2 authentication.""" + + DOMAIN = DOMAIN + + reauth_entry: GooglePhotosConfigEntry | None = None + + @property + def logger(self) -> logging.Logger: + """Return logger.""" + return logging.getLogger(__name__) + + @property + def extra_authorize_data(self) -> dict[str, Any]: + """Extra data that needs to be appended to the authorize url.""" + return { + "scope": " ".join(OAUTH2_SCOPES), + # Add params to ensure we get back a refresh token + "access_type": "offline", + "prompt": "consent", + } + + async def async_oauth_create_entry(self, data: dict[str, Any]) -> ConfigFlowResult: + """Create an entry for the flow.""" + session = aiohttp_client.async_get_clientsession(self.hass) + auth = api.AsyncConfigFlowAuth(session, data[CONF_TOKEN][CONF_ACCESS_TOKEN]) + client = GooglePhotosLibraryApi(auth) + + try: + user_resource_info = await client.get_user_info() + await client.list_media_items(page_size=1) + except GooglePhotosApiError as ex: + return self.async_abort( + reason="access_not_configured", + description_placeholders={"message": str(ex)}, + ) + except Exception: + self.logger.exception("Unknown error occurred") + return self.async_abort(reason="unknown") + user_id = user_resource_info.id + + if self.reauth_entry: + if self.reauth_entry.unique_id == user_id: + return self.async_update_reload_and_abort( + self.reauth_entry, unique_id=user_id, data=data + ) + return self.async_abort(reason="wrong_account") + + await self.async_set_unique_id(user_id) + self._abort_if_unique_id_configured() + return self.async_create_entry(title=user_resource_info.name, data=data) + + async def async_step_reauth( + self, entry_data: Mapping[str, Any] + ) -> ConfigFlowResult: + """Perform reauth upon an API authentication error.""" + self.reauth_entry = self.hass.config_entries.async_get_entry( + self.context["entry_id"] + ) + return await self.async_step_reauth_confirm() + + async def async_step_reauth_confirm( + self, user_input: Mapping[str, Any] | None = None + ) -> ConfigFlowResult: + """Confirm reauth dialog.""" + if user_input is None: + return self.async_show_form(step_id="reauth_confirm") + return await self.async_step_user() diff --git a/homeassistant/components/google_photos/const.py b/homeassistant/components/google_photos/const.py new file mode 100644 index 00000000000000..c629e6feb271b8 --- /dev/null +++ b/homeassistant/components/google_photos/const.py @@ -0,0 +1,17 @@ +"""Constants for the Google Photos integration.""" + +DOMAIN = "google_photos" + +OAUTH2_AUTHORIZE = "https://accounts.google.com/o/oauth2/v2/auth" +OAUTH2_TOKEN = "https://oauth2.googleapis.com/token" + +UPLOAD_SCOPE = "https://www.googleapis.com/auth/photoslibrary.appendonly" +READ_SCOPES = [ + "https://www.googleapis.com/auth/photoslibrary.readonly", + "https://www.googleapis.com/auth/photoslibrary.readonly.appcreateddata", +] +OAUTH2_SCOPES = [ + *READ_SCOPES, + UPLOAD_SCOPE, + "https://www.googleapis.com/auth/userinfo.profile", +] diff --git a/homeassistant/components/google_photos/icons.json b/homeassistant/components/google_photos/icons.json new file mode 100644 index 00000000000000..5d51ed4370aa79 --- /dev/null +++ b/homeassistant/components/google_photos/icons.json @@ -0,0 +1,7 @@ +{ + "services": { + "upload": { + "service": "mdi:cloud-upload" + } + } +} diff --git a/homeassistant/components/google_photos/manifest.json b/homeassistant/components/google_photos/manifest.json new file mode 100644 index 00000000000000..5ff37135f9aa56 --- /dev/null +++ b/homeassistant/components/google_photos/manifest.json @@ -0,0 +1,11 @@ +{ + "domain": "google_photos", + "name": "Google Photos", + "codeowners": ["@allenporter"], + "config_flow": true, + "dependencies": ["application_credentials"], + "documentation": "https://www.home-assistant.io/integrations/google_photos", + "iot_class": "cloud_polling", + "loggers": ["google_photos_library_api"], + "requirements": ["google-photos-library-api==0.8.0"] +} diff --git a/homeassistant/components/google_photos/media_source.py b/homeassistant/components/google_photos/media_source.py new file mode 100644 index 00000000000000..63d66d5a82bd3c --- /dev/null +++ b/homeassistant/components/google_photos/media_source.py @@ -0,0 +1,359 @@ +"""Media source for Google Photos.""" + +from dataclasses import dataclass +from enum import Enum, StrEnum +import logging +from typing import Any, Self, cast + +from google_photos_library_api.exceptions import GooglePhotosApiError +from google_photos_library_api.model import Album, MediaItem + +from homeassistant.components.media_player import MediaClass, MediaType +from homeassistant.components.media_source import ( + BrowseError, + BrowseMediaSource, + MediaSource, + MediaSourceItem, + PlayMedia, +) +from homeassistant.core import HomeAssistant + +from . import GooglePhotosConfigEntry +from .const import DOMAIN, READ_SCOPES + +_LOGGER = logging.getLogger(__name__) + +MAX_RECENT_PHOTOS = 100 +MEDIA_ITEMS_PAGE_SIZE = 100 +ALBUM_PAGE_SIZE = 50 + +THUMBNAIL_SIZE = 256 +LARGE_IMAGE_SIZE = 2160 + + +@dataclass +class SpecialAlbumDetails: + """Details for a Special album.""" + + path: str + title: str + list_args: dict[str, Any] + max_photos: int | None + + +class SpecialAlbum(Enum): + """Special Album types.""" + + RECENT = SpecialAlbumDetails("recent", "Recent Photos", {}, MAX_RECENT_PHOTOS) + FAVORITE = SpecialAlbumDetails( + "favorites", "Favorite Photos", {"favorites": True}, None + ) + + @classmethod + def of(cls, path: str) -> Self | None: + """Parse a PhotosIdentifierType by string value.""" + for enum in cls: + if enum.value.path == path: + return enum + return None + + +# The PhotosIdentifier can be in the following forms: +# config-entry-id +# config-entry-id/a/album-media-id +# config-entry-id/p/photo-media-id +# +# The album-media-id can contain special reserved folder names for use by +# this integration for virtual folders like the `recent` album. + + +class PhotosIdentifierType(StrEnum): + """Type for a PhotosIdentifier.""" + + PHOTO = "p" + ALBUM = "a" + + @classmethod + def of(cls, name: str) -> "PhotosIdentifierType": + """Parse a PhotosIdentifierType by string value.""" + for enum in PhotosIdentifierType: + if enum.value == name: + return enum + raise ValueError(f"Invalid PhotosIdentifierType: {name}") + + +@dataclass +class PhotosIdentifier: + """Google Photos item identifier in a media source URL.""" + + config_entry_id: str + """Identifies the account for the media item.""" + + id_type: PhotosIdentifierType | None = None + """Type of identifier""" + + media_id: str | None = None + """Identifies the album or photo contents to show.""" + + def as_string(self) -> str: + """Serialize the identifier as a string.""" + if self.id_type is None: + return self.config_entry_id + return f"{self.config_entry_id}/{self.id_type}/{self.media_id}" + + @classmethod + def of(cls, identifier: str) -> Self: + """Parse a PhotosIdentifier form a string.""" + parts = identifier.split("/") + if len(parts) == 1: + return cls(parts[0]) + if len(parts) != 3: + raise BrowseError(f"Invalid identifier: {identifier}") + return cls(parts[0], PhotosIdentifierType.of(parts[1]), parts[2]) + + @classmethod + def album(cls, config_entry_id: str, media_id: str) -> Self: + """Create an album PhotosIdentifier.""" + return cls(config_entry_id, PhotosIdentifierType.ALBUM, media_id) + + @classmethod + def photo(cls, config_entry_id: str, media_id: str) -> Self: + """Create an album PhotosIdentifier.""" + return cls(config_entry_id, PhotosIdentifierType.PHOTO, media_id) + + +async def async_get_media_source(hass: HomeAssistant) -> MediaSource: + """Set up Google Photos media source.""" + return GooglePhotosMediaSource(hass) + + +class GooglePhotosMediaSource(MediaSource): + """Provide Google Photos as media sources.""" + + name = "Google Photos" + + def __init__(self, hass: HomeAssistant) -> None: + """Initialize Google Photos source.""" + super().__init__(DOMAIN) + self.hass = hass + + async def async_resolve_media(self, item: MediaSourceItem) -> PlayMedia: + """Resolve media identifier to a url. + + This will resolve a specific media item to a url for the full photo or video contents. + """ + try: + identifier = PhotosIdentifier.of(item.identifier) + except ValueError as err: + raise BrowseError(f"Could not parse identifier: {item.identifier}") from err + if ( + identifier.media_id is None + or identifier.id_type != PhotosIdentifierType.PHOTO + ): + raise BrowseError( + f"Could not resolve identiifer that is not a Photo: {identifier}" + ) + entry = self._async_config_entry(identifier.config_entry_id) + client = entry.runtime_data + media_item = await client.get_media_item(media_item_id=identifier.media_id) + if not media_item.mime_type: + raise BrowseError("Could not determine mime type of media item") + if media_item.media_metadata and (media_item.media_metadata.video is not None): + url = _video_url(media_item) + else: + url = _media_url(media_item, LARGE_IMAGE_SIZE) + return PlayMedia( + url=url, + mime_type=media_item.mime_type, + ) + + async def async_browse_media(self, item: MediaSourceItem) -> BrowseMediaSource: + """Return details about the media source. + + This renders the multi-level album structure for an account, its albums, + or the contents of an album. This will return a BrowseMediaSource with a + single level of children at the next level of the hierarchy. + """ + if not item.identifier: + # Top level view that lists all accounts. + return BrowseMediaSource( + domain=DOMAIN, + identifier=None, + media_class=MediaClass.DIRECTORY, + media_content_type=MediaClass.IMAGE, + title="Google Photos", + can_play=False, + can_expand=True, + children_media_class=MediaClass.DIRECTORY, + children=[ + _build_account(entry, PhotosIdentifier(cast(str, entry.unique_id))) + for entry in self._async_config_entries() + ], + ) + + # Determine the configuration entry for this item + identifier = PhotosIdentifier.of(item.identifier) + entry = self._async_config_entry(identifier.config_entry_id) + client = entry.runtime_data + + source = _build_account(entry, identifier) + if identifier.id_type is None: + source.children = [ + _build_album( + special_album.value.title, + PhotosIdentifier.album( + identifier.config_entry_id, special_album.value.path + ), + ) + for special_album in SpecialAlbum + ] + albums: list[Album] = [] + try: + async for album_result in await client.list_albums( + page_size=ALBUM_PAGE_SIZE + ): + albums.extend(album_result.albums) + except GooglePhotosApiError as err: + raise BrowseError(f"Error listing albums: {err}") from err + + source.children.extend( + _build_album( + album.title, + PhotosIdentifier.album( + identifier.config_entry_id, + album.id, + ), + _cover_photo_url(album, THUMBNAIL_SIZE), + ) + for album in albums + ) + return source + + if ( + identifier.id_type != PhotosIdentifierType.ALBUM + or identifier.media_id is None + ): + raise BrowseError(f"Unsupported identifier: {identifier}") + + list_args: dict[str, Any] + if special_album := SpecialAlbum.of(identifier.media_id): + list_args = special_album.value.list_args + else: + list_args = {"album_id": identifier.media_id} + + media_items: list[MediaItem] = [] + try: + async for media_item_result in await client.list_media_items( + **list_args, page_size=MEDIA_ITEMS_PAGE_SIZE + ): + media_items.extend(media_item_result.media_items) + if ( + special_album + and (max_photos := special_album.value.max_photos) + and len(media_items) > max_photos + ): + break + except GooglePhotosApiError as err: + raise BrowseError(f"Error listing media items: {err}") from err + + source.children = [ + _build_media_item( + PhotosIdentifier.photo(identifier.config_entry_id, media_item.id), + media_item, + ) + for media_item in media_items + ] + return source + + def _async_config_entries(self) -> list[GooglePhotosConfigEntry]: + """Return all config entries that support photo library reads.""" + entries = [] + for entry in self.hass.config_entries.async_loaded_entries(DOMAIN): + scopes = entry.data["token"]["scope"].split(" ") + if any(scope in scopes for scope in READ_SCOPES): + entries.append(entry) + return entries + + def _async_config_entry(self, config_entry_id: str) -> GooglePhotosConfigEntry: + """Return a config entry with the specified id.""" + entry = self.hass.config_entries.async_entry_for_domain_unique_id( + DOMAIN, config_entry_id + ) + if not entry: + raise BrowseError( + f"Could not find config entry for identifier: {config_entry_id}" + ) + return entry + + +def _build_account( + config_entry: GooglePhotosConfigEntry, + identifier: PhotosIdentifier, +) -> BrowseMediaSource: + """Build the root node for a Google Photos account for a config entry.""" + return BrowseMediaSource( + domain=DOMAIN, + identifier=identifier.as_string(), + media_class=MediaClass.DIRECTORY, + media_content_type=MediaClass.IMAGE, + title=config_entry.title, + can_play=False, + can_expand=True, + ) + + +def _build_album( + title: str, identifier: PhotosIdentifier, thumbnail_url: str | None = None +) -> BrowseMediaSource: + """Build an album node.""" + return BrowseMediaSource( + domain=DOMAIN, + identifier=identifier.as_string(), + media_class=MediaClass.ALBUM, + media_content_type=MediaClass.ALBUM, + title=title, + can_play=False, + can_expand=True, + thumbnail=thumbnail_url, + ) + + +def _build_media_item( + identifier: PhotosIdentifier, + media_item: MediaItem, +) -> BrowseMediaSource: + """Build the node for an individual photo or video.""" + is_video = media_item.media_metadata and ( + media_item.media_metadata.video is not None + ) + return BrowseMediaSource( + domain=DOMAIN, + identifier=identifier.as_string(), + media_class=MediaClass.IMAGE if not is_video else MediaClass.VIDEO, + media_content_type=MediaType.IMAGE if not is_video else MediaType.VIDEO, + title=media_item.filename, + can_play=is_video, + can_expand=False, + thumbnail=_media_url(media_item, THUMBNAIL_SIZE), + ) + + +def _media_url(media_item: MediaItem, max_size: int) -> str: + """Return a media item url with the specified max thumbnail size on the longest edge. + + See https://developers.google.com/photos/library/guides/access-media-items#base-urls + """ + return f"{media_item.base_url}=h{max_size}" + + +def _video_url(media_item: MediaItem) -> str: + """Return a video url for the item. + + See https://developers.google.com/photos/library/guides/access-media-items#base-urls + """ + return f"{media_item.base_url}=dv" + + +def _cover_photo_url(album: Album, max_size: int) -> str: + """Return a media item url for the cover photo of the album.""" + return f"{album.cover_photo_base_url}=h{max_size}" diff --git a/homeassistant/components/google_photos/services.py b/homeassistant/components/google_photos/services.py new file mode 100644 index 00000000000000..66aa61e23a4baa --- /dev/null +++ b/homeassistant/components/google_photos/services.py @@ -0,0 +1,138 @@ +"""Google Photos services.""" + +from __future__ import annotations + +import asyncio +import mimetypes +from pathlib import Path + +from google_photos_library_api.exceptions import GooglePhotosApiError +from google_photos_library_api.model import NewMediaItem, SimpleMediaItem +import voluptuous as vol + +from homeassistant.const import CONF_FILENAME +from homeassistant.core import ( + HomeAssistant, + ServiceCall, + ServiceResponse, + SupportsResponse, +) +from homeassistant.exceptions import HomeAssistantError, ServiceValidationError +from homeassistant.helpers import config_validation as cv + +from .const import DOMAIN, UPLOAD_SCOPE +from .types import GooglePhotosConfigEntry + +CONF_CONFIG_ENTRY_ID = "config_entry_id" + +UPLOAD_SERVICE = "upload" +UPLOAD_SERVICE_SCHEMA = vol.Schema( + { + vol.Required(CONF_CONFIG_ENTRY_ID): cv.string, + vol.Required(CONF_FILENAME): vol.All(cv.ensure_list, [cv.string]), + } +) + + +def _read_file_contents( + hass: HomeAssistant, filenames: list[str] +) -> list[tuple[str, bytes]]: + """Return the mime types and file contents for each file.""" + results = [] + for filename in filenames: + if not hass.config.is_allowed_path(filename): + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="no_access_to_path", + translation_placeholders={"filename": filename}, + ) + filename_path = Path(filename) + if not filename_path.exists(): + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="filename_does_not_exist", + translation_placeholders={"filename": filename}, + ) + mime_type, _ = mimetypes.guess_type(filename) + if mime_type is None or not (mime_type.startswith(("image", "video"))): + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="filename_is_not_image", + translation_placeholders={"filename": filename}, + ) + results.append((mime_type, filename_path.read_bytes())) + return results + + +def async_register_services(hass: HomeAssistant) -> None: + """Register Google Photos services.""" + + async def async_handle_upload(call: ServiceCall) -> ServiceResponse: + """Generate content from text and optionally images.""" + config_entry: GooglePhotosConfigEntry | None = ( + hass.config_entries.async_get_entry(call.data[CONF_CONFIG_ENTRY_ID]) + ) + if not config_entry: + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="integration_not_found", + translation_placeholders={"target": DOMAIN}, + ) + scopes = config_entry.data["token"]["scope"].split(" ") + if UPLOAD_SCOPE not in scopes: + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="missing_upload_permission", + translation_placeholders={"target": DOMAIN}, + ) + + client_api = config_entry.runtime_data + upload_tasks = [] + file_results = await hass.async_add_executor_job( + _read_file_contents, hass, call.data[CONF_FILENAME] + ) + for mime_type, content in file_results: + upload_tasks.append(client_api.upload_content(content, mime_type)) + try: + upload_results = await asyncio.gather(*upload_tasks) + except GooglePhotosApiError as err: + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="upload_error", + translation_placeholders={"message": str(err)}, + ) from err + try: + upload_result = await client_api.create_media_items( + [ + NewMediaItem( + SimpleMediaItem(upload_token=upload_result.upload_token) + ) + for upload_result in upload_results + ] + ) + except GooglePhotosApiError as err: + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="api_error", + translation_placeholders={"message": str(err)}, + ) from err + if call.return_response: + return { + "media_items": [ + { + "media_item_id": item_result.media_item.id + for item_result in upload_result.new_media_item_results + if item_result.media_item and item_result.media_item.id + } + ] + } + return None + + if not hass.services.has_service(DOMAIN, UPLOAD_SERVICE): + hass.services.async_register( + DOMAIN, + UPLOAD_SERVICE, + async_handle_upload, + schema=UPLOAD_SERVICE_SCHEMA, + supports_response=SupportsResponse.OPTIONAL, + ) diff --git a/homeassistant/components/google_photos/services.yaml b/homeassistant/components/google_photos/services.yaml new file mode 100644 index 00000000000000..047305c0bcaedb --- /dev/null +++ b/homeassistant/components/google_photos/services.yaml @@ -0,0 +1,11 @@ +upload: + fields: + config_entry_id: + required: true + selector: + config_entry: + integration: google_photos + filename: + required: false + selector: + object: diff --git a/homeassistant/components/google_photos/strings.json b/homeassistant/components/google_photos/strings.json new file mode 100644 index 00000000000000..bf2809f896fa77 --- /dev/null +++ b/homeassistant/components/google_photos/strings.json @@ -0,0 +1,73 @@ +{ + "application_credentials": { + "description": "Follow the [instructions]({more_info_url}) for [OAuth consent screen]({oauth_consent_url}) to give Home Assistant access to your Google Photos. You also need to create Application Credentials linked to your account:\n1. Go to [Credentials]({oauth_creds_url}) and click **Create Credentials**.\n1. From the drop-down list select **OAuth client ID**.\n1. Select **Web application** for the Application Type.\n\n" + }, + "config": { + "step": { + "pick_implementation": { + "title": "[%key:common::config_flow::title::oauth2_pick_implementation%]" + } + }, + "abort": { + "already_configured": "[%key:common::config_flow::abort::already_configured_account%]", + "already_in_progress": "[%key:common::config_flow::abort::already_in_progress%]", + "oauth_error": "[%key:common::config_flow::abort::oauth2_error%]", + "missing_configuration": "[%key:common::config_flow::abort::oauth2_missing_configuration%]", + "authorize_url_timeout": "[%key:common::config_flow::abort::oauth2_authorize_url_timeout%]", + "no_url_available": "[%key:common::config_flow::abort::oauth2_no_url_available%]", + "user_rejected_authorize": "[%key:common::config_flow::abort::oauth2_user_rejected_authorize%]", + "access_not_configured": "Unable to access the Google API:\n\n{message}", + "unknown": "[%key:common::config_flow::error::unknown%]", + "wrong_account": "Wrong account: Please authenticate with the right account.", + "oauth_timeout": "[%key:common::config_flow::abort::oauth2_timeout%]", + "oauth_unauthorized": "[%key:common::config_flow::abort::oauth2_unauthorized%]", + "oauth_failed": "[%key:common::config_flow::abort::oauth2_failed%]" + }, + "create_entry": { + "default": "[%key:common::config_flow::create_entry::authenticated%]" + } + }, + "exceptions": { + "integration_not_found": { + "message": "Integration \"{target}\" not found in registry." + }, + "not_loaded": { + "message": "{target} is not loaded." + }, + "no_access_to_path": { + "message": "Cannot read {filename}, no access to path; `allowlist_external_dirs` may need to be adjusted in `configuration.yaml`" + }, + "filename_does_not_exist": { + "message": "`{filename}` does not exist" + }, + "filename_is_not_image": { + "message": "`{filename}` is not an image" + }, + "missing_upload_permission": { + "message": "Home Assistnt was not granted permission to upload to Google Photos" + }, + "upload_error": { + "message": "Failed to upload content: {message}" + }, + "api_error": { + "message": "Google Photos API responded with error: {message}" + } + }, + "services": { + "upload": { + "name": "Upload media", + "description": "Upload images or videos to Google Photos.", + "fields": { + "config_entry_id": { + "name": "Integration Id", + "description": "The Google Photos integration id." + }, + "filename": { + "name": "Filename", + "description": "Path to the image or video to upload.", + "example": "/config/www/image.jpg" + } + } + } + } +} diff --git a/homeassistant/components/google_photos/types.py b/homeassistant/components/google_photos/types.py new file mode 100644 index 00000000000000..2fe57fe1d15e72 --- /dev/null +++ b/homeassistant/components/google_photos/types.py @@ -0,0 +1,7 @@ +"""Google Photos types.""" + +from google_photos_library_api.api import GooglePhotosLibraryApi + +from homeassistant.config_entries import ConfigEntry + +type GooglePhotosConfigEntry = ConfigEntry[GooglePhotosLibraryApi] diff --git a/homeassistant/components/google_pubsub/manifest.json b/homeassistant/components/google_pubsub/manifest.json index f22317404ab159..aa13f1808c42fd 100644 --- a/homeassistant/components/google_pubsub/manifest.json +++ b/homeassistant/components/google_pubsub/manifest.json @@ -4,5 +4,5 @@ "codeowners": [], "documentation": "https://www.home-assistant.io/integrations/google_pubsub", "iot_class": "cloud_push", - "requirements": ["google-cloud-pubsub==2.13.11"] + "requirements": ["google-cloud-pubsub==2.23.0"] } diff --git a/homeassistant/components/google_sheets/icons.json b/homeassistant/components/google_sheets/icons.json index c8010a690bec04..e2b6ed57579f35 100644 --- a/homeassistant/components/google_sheets/icons.json +++ b/homeassistant/components/google_sheets/icons.json @@ -1,5 +1,7 @@ { "services": { - "append_sheet": "mdi:google-spreadsheet" + "append_sheet": { + "service": "mdi:google-spreadsheet" + } } } diff --git a/homeassistant/components/google_translate/tts.py b/homeassistant/components/google_translate/tts.py index 221c99e7c20d72..13e0ca4c2738ba 100644 --- a/homeassistant/components/google_translate/tts.py +++ b/homeassistant/components/google_translate/tts.py @@ -74,7 +74,7 @@ def __init__(self, config_entry: ConfigEntry, lang: str, tld: str) -> None: else: self._lang = lang self._tld = tld - self._attr_name = f"Google {self._lang} {self._tld}" + self._attr_name = f"Google Translate {self._lang} {self._tld}" self._attr_unique_id = config_entry.entry_id @property @@ -130,7 +130,7 @@ def __init__(self, hass: HomeAssistant, lang: str, tld: str) -> None: else: self._lang = lang self._tld = tld - self.name = "Google" + self.name = "Google Translate" @property def default_language(self) -> str: diff --git a/homeassistant/components/group/icons.json b/homeassistant/components/group/icons.json index 8cca94e08e1a94..577d1effac04c5 100644 --- a/homeassistant/components/group/icons.json +++ b/homeassistant/components/group/icons.json @@ -1,7 +1,13 @@ { "services": { - "reload": "mdi:reload", - "set": "mdi:home-group-plus", - "remove": "mdi:home-group-remove" + "reload": { + "service": "mdi:reload" + }, + "set": { + "service": "mdi:home-group-plus" + }, + "remove": { + "service": "mdi:home-group-remove" + } } } diff --git a/homeassistant/components/growatt_server/config_flow.py b/homeassistant/components/growatt_server/config_flow.py index 95002a70a9587c..e676d8fae3268b 100644 --- a/homeassistant/components/growatt_server/config_flow.py +++ b/homeassistant/components/growatt_server/config_flow.py @@ -1,9 +1,11 @@ """Config flow for growatt server integration.""" +from typing import Any + import growattServer import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_NAME, CONF_PASSWORD, CONF_URL, CONF_USERNAME from homeassistant.core import callback @@ -21,11 +23,12 @@ class GrowattServerConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - def __init__(self): + api: growattServer.GrowattApi + + def __init__(self) -> None: """Initialise growatt server flow.""" - self.api = None self.user_id = None - self.data = {} + self.data: dict[str, Any] = {} @callback def _async_show_user_form(self, errors=None): @@ -42,7 +45,9 @@ def _async_show_user_form(self, errors=None): step_id="user", data_schema=data_schema, errors=errors ) - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the start of the config flow.""" if not user_input: return self._async_show_user_form() @@ -66,7 +71,9 @@ async def async_step_user(self, user_input=None): self.data = user_input return await self.async_step_plant() - async def async_step_plant(self, user_input=None): + async def async_step_plant( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle adding a "plant" to Home Assistant.""" plant_info = await self.hass.async_add_executor_job( self.api.plant_list, self.user_id @@ -82,7 +89,8 @@ async def async_step_plant(self, user_input=None): return self.async_show_form(step_id="plant", data_schema=data_schema) - if user_input is None and len(plant_info["data"]) == 1: + if user_input is None: + # single plant => mark it as selected user_input = {CONF_PLANT_ID: plant_info["data"][0]["plantId"]} user_input[CONF_NAME] = plants[user_input[CONF_PLANT_ID]] diff --git a/homeassistant/components/guardian/icons.json b/homeassistant/components/guardian/icons.json index 4740366e993000..fe44eb0460be13 100644 --- a/homeassistant/components/guardian/icons.json +++ b/homeassistant/components/guardian/icons.json @@ -18,8 +18,14 @@ } }, "services": { - "pair_sensor": "mdi:link-variant", - "unpair_sensor": "mdi:link-variant-remove", - "upgrade_firmware": "mdi:update" + "pair_sensor": { + "service": "mdi:link-variant" + }, + "unpair_sensor": { + "service": "mdi:link-variant-remove" + }, + "upgrade_firmware": { + "service": "mdi:update" + } } } diff --git a/homeassistant/components/habitica/__init__.py b/homeassistant/components/habitica/__init__.py index 468db8fbc4251e..bcf8713f9b1001 100644 --- a/homeassistant/components/habitica/__init__.py +++ b/homeassistant/components/habitica/__init__.py @@ -15,6 +15,7 @@ CONF_NAME, CONF_SENSORS, CONF_URL, + CONF_VERIFY_SSL, Platform, ) from homeassistant.core import HomeAssistant, ServiceCall @@ -125,6 +126,7 @@ async def handle_api_call(call: ServiceCall) -> None: name = call.data[ATTR_NAME] path = call.data[ATTR_PATH] entries = hass.config_entries.async_entries(DOMAIN) + api = None for entry in entries: if entry.data[CONF_NAME] == name: @@ -147,18 +149,16 @@ async def handle_api_call(call: ServiceCall) -> None: EVENT_API_CALL_SUCCESS, {ATTR_NAME: name, ATTR_PATH: path, ATTR_DATA: data} ) - websession = async_get_clientsession(hass) - - url = config_entry.data[CONF_URL] - username = config_entry.data[CONF_API_USER] - password = config_entry.data[CONF_API_KEY] + websession = async_get_clientsession( + hass, verify_ssl=config_entry.data.get(CONF_VERIFY_SSL, True) + ) api = await hass.async_add_executor_job( HAHabitipyAsync, { - "url": url, - "login": username, - "password": password, + "url": config_entry.data[CONF_URL], + "login": config_entry.data[CONF_API_USER], + "password": config_entry.data[CONF_API_KEY], }, ) try: diff --git a/homeassistant/components/habitica/config_flow.py b/homeassistant/components/habitica/config_flow.py index 5dd9fb2aa22d45..2947032c41ef76 100644 --- a/homeassistant/components/habitica/config_flow.py +++ b/homeassistant/components/habitica/config_flow.py @@ -2,53 +2,60 @@ from __future__ import annotations +from http import HTTPStatus import logging +from typing import Any from aiohttp import ClientResponseError from habitipy.aio import HabitipyAsync import voluptuous as vol -from homeassistant.config_entries import ConfigFlow -from homeassistant.const import CONF_API_KEY, CONF_NAME, CONF_URL -from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant -from homeassistant.exceptions import HomeAssistantError +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult +from homeassistant.const import ( + CONF_API_KEY, + CONF_PASSWORD, + CONF_URL, + CONF_USERNAME, + CONF_VERIFY_SSL, +) +from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue +from homeassistant.helpers.selector import ( + TextSelector, + TextSelectorConfig, + TextSelectorType, +) from .const import CONF_API_USER, DEFAULT_URL, DOMAIN -DATA_SCHEMA = vol.Schema( +STEP_ADVANCED_DATA_SCHEMA = vol.Schema( { vol.Required(CONF_API_USER): str, vol.Required(CONF_API_KEY): str, - vol.Optional(CONF_NAME): str, vol.Optional(CONF_URL, default=DEFAULT_URL): str, + vol.Required(CONF_VERIFY_SSL, default=True): bool, } ) -_LOGGER = logging.getLogger(__name__) - +STEP_LOGIN_DATA_SCHEMA = vol.Schema( + { + vol.Required(CONF_USERNAME): TextSelector( + TextSelectorConfig( + type=TextSelectorType.EMAIL, + autocomplete="email", + ) + ), + vol.Required(CONF_PASSWORD): TextSelector( + TextSelectorConfig( + type=TextSelectorType.PASSWORD, + autocomplete="current-password", + ) + ), + } +) -async def validate_input(hass: HomeAssistant, data: dict[str, str]) -> dict[str, str]: - """Validate the user input allows us to connect.""" - - websession = async_get_clientsession(hass) - api = await hass.async_add_executor_job( - HabitipyAsync, - { - "login": data[CONF_API_USER], - "password": data[CONF_API_KEY], - "url": data[CONF_URL] or DEFAULT_URL, - }, - ) - try: - await api.user.get(session=websession) - return { - "title": f"{data.get('name', 'Default username')}", - CONF_API_USER: data[CONF_API_USER], - } - except ClientResponseError as ex: - raise InvalidAuth from ex +_LOGGER = logging.getLogger(__name__) class HabiticaConfigFlow(ConfigFlow, domain=DOMAIN): @@ -56,30 +63,123 @@ class HabiticaConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" - errors = {} + return self.async_show_menu( + step_id="user", + menu_options=["login", "advanced"], + ) + + async def async_step_login( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Config flow with username/password. + + Simplified configuration setup that retrieves API credentials + from Habitica.com by authenticating with login and password. + """ + errors: dict[str, str] = {} if user_input is not None: try: - info = await validate_input(self.hass, user_input) - except InvalidAuth: - errors = {"base": "invalid_credentials"} + session = async_get_clientsession(self.hass) + api = await self.hass.async_add_executor_job( + HabitipyAsync, + { + "login": "", + "password": "", + "url": DEFAULT_URL, + }, + ) + login_response = await api.user.auth.local.login.post( + session=session, + username=user_input[CONF_USERNAME], + password=user_input[CONF_PASSWORD], + ) + + except ClientResponseError as ex: + if ex.status == HTTPStatus.UNAUTHORIZED: + errors["base"] = "invalid_auth" + else: + errors["base"] = "cannot_connect" except Exception: _LOGGER.exception("Unexpected exception") - errors = {"base": "unknown"} + errors["base"] = "unknown" else: - await self.async_set_unique_id(info[CONF_API_USER]) + await self.async_set_unique_id(login_response["id"]) self._abort_if_unique_id_configured() - return self.async_create_entry(title=info["title"], data=user_input) + return self.async_create_entry( + title=login_response["username"], + data={ + CONF_API_USER: login_response["id"], + CONF_API_KEY: login_response["apiToken"], + CONF_USERNAME: login_response["username"], + CONF_URL: DEFAULT_URL, + CONF_VERIFY_SSL: True, + }, + ) + return self.async_show_form( - step_id="user", - data_schema=DATA_SCHEMA, + step_id="login", + data_schema=self.add_suggested_values_to_schema( + data_schema=STEP_LOGIN_DATA_SCHEMA, suggested_values=user_input + ), errors=errors, - description_placeholders={}, ) - async def async_step_import(self, import_data): + async def async_step_advanced( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Advanced configuration with User Id and API Token. + + Advanced configuration allows connecting to Habitica instances + hosted on different domains or to self-hosted instances. + """ + errors: dict[str, str] = {} + if user_input is not None: + try: + session = async_get_clientsession( + self.hass, verify_ssl=user_input.get(CONF_VERIFY_SSL, True) + ) + api = await self.hass.async_add_executor_job( + HabitipyAsync, + { + "login": user_input[CONF_API_USER], + "password": user_input[CONF_API_KEY], + "url": user_input.get(CONF_URL, DEFAULT_URL), + }, + ) + api_response = await api.user.get( + session=session, + userFields="auth", + ) + except ClientResponseError as ex: + if ex.status == HTTPStatus.UNAUTHORIZED: + errors["base"] = "invalid_auth" + else: + errors["base"] = "cannot_connect" + except Exception: + _LOGGER.exception("Unexpected exception") + errors["base"] = "unknown" + else: + await self.async_set_unique_id(user_input[CONF_API_USER]) + self._abort_if_unique_id_configured() + user_input[CONF_USERNAME] = api_response["auth"]["local"]["username"] + return self.async_create_entry( + title=user_input[CONF_USERNAME], data=user_input + ) + + return self.async_show_form( + step_id="advanced", + data_schema=self.add_suggested_values_to_schema( + data_schema=STEP_ADVANCED_DATA_SCHEMA, suggested_values=user_input + ), + errors=errors, + ) + + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Import habitica config from configuration.yaml.""" async_create_issue( @@ -95,8 +195,4 @@ async def async_step_import(self, import_data): "integration_title": "Habitica", }, ) - return await self.async_step_user(import_data) - - -class InvalidAuth(HomeAssistantError): - """Error to indicate there is invalid auth.""" + return await self.async_step_advanced(import_data) diff --git a/homeassistant/components/habitica/coordinator.py b/homeassistant/components/habitica/coordinator.py index 4e949b703fb3e7..357643593e4249 100644 --- a/homeassistant/components/habitica/coordinator.py +++ b/homeassistant/components/habitica/coordinator.py @@ -56,7 +56,14 @@ async def _async_update_data(self) -> HabiticaData: try: user_response = await self.api.user.get() tasks_response = await self.api.tasks.user.get() - tasks_response.extend(await self.api.tasks.user.get(type="completedTodos")) + tasks_response.extend( + [ + {"id": task["_id"], **task} + for task in await self.api.tasks.user.get(type="completedTodos") + if task.get("_id") + ] + ) + except ClientResponseError as error: if error.status == HTTPStatus.TOO_MANY_REQUESTS: _LOGGER.debug("Currently rate limited, skipping update") diff --git a/homeassistant/components/habitica/icons.json b/homeassistant/components/habitica/icons.json index 710b8c9d25b928..662cf1d84a55ff 100644 --- a/homeassistant/components/habitica/icons.json +++ b/homeassistant/components/habitica/icons.json @@ -88,6 +88,8 @@ } }, "services": { - "api_call": "mdi:console" + "api_call": { + "service": "mdi:console" + } } } diff --git a/homeassistant/components/habitica/strings.json b/homeassistant/components/habitica/strings.json index 21d2622245c837..c5a54d254cc232 100644 --- a/homeassistant/components/habitica/strings.json +++ b/homeassistant/components/habitica/strings.json @@ -4,18 +4,32 @@ "already_configured": "[%key:common::config_flow::abort::already_configured_account%]" }, "error": { - "invalid_credentials": "[%key:common::config_flow::error::invalid_auth%]", + "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", + "invalid_auth": "[%key:common::config_flow::error::invalid_auth%]", "unknown": "[%key:common::config_flow::error::unknown%]" }, "step": { "user": { + "menu_options": { + "login": "Login to Habitica", + "advanced": "Login to other instances" + }, + "description": "Connect your Habitica profile to allow monitoring of your user's profile and tasks." + }, + "login": { + "data": { + "username": "Email or username (case-sensitive)", + "password": "[%key:common::config_flow::data::password%]" + } + }, + "advanced": { "data": { "url": "[%key:common::config_flow::data::url%]", - "name": "Override for Habitica’s username. Will be used for actions", - "api_user": "Habitica’s API user ID", - "api_key": "[%key:common::config_flow::data::api_key%]" + "api_user": "User ID", + "api_key": "API Token", + "verify_ssl": "[%key:common::config_flow::data::verify_ssl%]" }, - "description": "Connect your Habitica profile to allow monitoring of your user's profile and tasks. Note that api_id and api_key must be gotten from https://habitica.com/user/settings/api" + "description": "You can retrieve your `User ID` and `API Token` from **Settings -> Site Data** on Habitica or the instance you want to connect to" } } }, diff --git a/homeassistant/components/harmony/icons.json b/homeassistant/components/harmony/icons.json index f96fd985323ded..b6fe0d8c42e770 100644 --- a/homeassistant/components/harmony/icons.json +++ b/homeassistant/components/harmony/icons.json @@ -10,7 +10,11 @@ } }, "services": { - "sync": "mdi:sync", - "change_channel": "mdi:remote-tv" + "sync": { + "service": "mdi:sync" + }, + "change_channel": { + "service": "mdi:remote-tv" + } } } diff --git a/homeassistant/components/hassio/handler.py b/homeassistant/components/hassio/handler.py index 305b9d4961b3ff..7c8d5c61a22c0f 100644 --- a/homeassistant/components/hassio/handler.py +++ b/homeassistant/components/hassio/handler.py @@ -568,14 +568,13 @@ async def send_command( This method is a coroutine. """ - url = f"http://{self._ip}{command}" - joined_url = self._base_url.join(URL(command)) + joined_url = self._base_url.with_path(command) # This check is to make sure the normalized URL string # is the same as the URL string that was passed in. If # they are different, then the passed in command URL # contained characters that were removed by the normalization # such as ../../../../etc/passwd - if url != str(joined_url): + if joined_url.raw_path != command: _LOGGER.error("Invalid request %s", command) raise HassioAPIError diff --git a/homeassistant/components/hassio/icons.json b/homeassistant/components/hassio/icons.json index c55820b58f2aab..64f032d9f806ea 100644 --- a/homeassistant/components/hassio/icons.json +++ b/homeassistant/components/hassio/icons.json @@ -10,16 +10,38 @@ } }, "services": { - "addon_start": "mdi:play", - "addon_restart": "mdi:restart", - "addon_stdin": "mdi:console", - "addon_stop": "mdi:stop", - "addon_update": "mdi:update", - "host_reboot": "mdi:restart", - "host_shutdown": "mdi:power", - "backup_full": "mdi:content-save", - "backup_partial": "mdi:content-save", - "restore_full": "mdi:backup-restore", - "restore_partial": "mdi:backup-restore" + "addon_start": { + "service": "mdi:play" + }, + "addon_restart": { + "service": "mdi:restart" + }, + "addon_stdin": { + "service": "mdi:console" + }, + "addon_stop": { + "service": "mdi:stop" + }, + "addon_update": { + "service": "mdi:update" + }, + "host_reboot": { + "service": "mdi:restart" + }, + "host_shutdown": { + "service": "mdi:power" + }, + "backup_full": { + "service": "mdi:content-save" + }, + "backup_partial": { + "service": "mdi:content-save" + }, + "restore_full": { + "service": "mdi:backup-restore" + }, + "restore_partial": { + "service": "mdi:backup-restore" + } } } diff --git a/homeassistant/components/hdmi_cec/icons.json b/homeassistant/components/hdmi_cec/icons.json index 0bfcb98eea2c9f..93647a6bb12e2e 100644 --- a/homeassistant/components/hdmi_cec/icons.json +++ b/homeassistant/components/hdmi_cec/icons.json @@ -1,10 +1,22 @@ { "services": { - "power_on": "mdi:power", - "select_device": "mdi:television", - "send_command": "mdi:console", - "standby": "mdi:power-standby", - "update": "mdi:update", - "volume": "mdi:volume-high" + "power_on": { + "service": "mdi:power" + }, + "select_device": { + "service": "mdi:television" + }, + "send_command": { + "service": "mdi:console" + }, + "standby": { + "service": "mdi:power-standby" + }, + "update": { + "service": "mdi:update" + }, + "volume": { + "service": "mdi:volume-high" + } } } diff --git a/homeassistant/components/heos/config_flow.py b/homeassistant/components/heos/config_flow.py index b68d7d16717051..57ed51a3c05c47 100644 --- a/homeassistant/components/heos/config_flow.py +++ b/homeassistant/components/heos/config_flow.py @@ -1,6 +1,6 @@ """Config flow to configure Heos.""" -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Any from urllib.parse import urlparse from pyheos import Heos, HeosError @@ -43,15 +43,17 @@ async def async_step_ssdp( # Show selection form return self.async_show_form(step_id="user") - async def async_step_import(self, user_input=None): + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Occurs when an entry is setup through config.""" - host = user_input[CONF_HOST] + host = import_data[CONF_HOST] # raise_on_progress is False here in case ssdp discovers # heos first which would block the import await self.async_set_unique_id(DOMAIN, raise_on_progress=False) return self.async_create_entry(title=format_title(host), data={CONF_HOST: host}) - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Obtain host and validate connection.""" self.hass.data.setdefault(DATA_DISCOVERED_HOSTS, {}) # Only a single entry is needed for all devices diff --git a/homeassistant/components/heos/icons.json b/homeassistant/components/heos/icons.json index 69c434c8287b93..23c2c8faeafe3c 100644 --- a/homeassistant/components/heos/icons.json +++ b/homeassistant/components/heos/icons.json @@ -1,6 +1,10 @@ { "services": { - "sign_in": "mdi:login", - "sign_out": "mdi:logout" + "sign_in": { + "service": "mdi:login" + }, + "sign_out": { + "service": "mdi:logout" + } } } diff --git a/homeassistant/components/history_stats/icons.json b/homeassistant/components/history_stats/icons.json index a03163179cb5bd..a9829425570a0c 100644 --- a/homeassistant/components/history_stats/icons.json +++ b/homeassistant/components/history_stats/icons.json @@ -1,5 +1,7 @@ { "services": { - "reload": "mdi:reload" + "reload": { + "service": "mdi:reload" + } } } diff --git a/homeassistant/components/hive/config_flow.py b/homeassistant/components/hive/config_flow.py index f8cb089834a413..d6be2d1efabd58 100644 --- a/homeassistant/components/hive/config_flow.py +++ b/homeassistant/components/hive/config_flow.py @@ -163,11 +163,9 @@ async def async_step_reauth( } return await self.async_step_user(data) - async def async_step_import( - self, user_input: dict[str, Any] | None = None - ) -> ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Import user.""" - return await self.async_step_user(user_input) + return await self.async_step_user(import_data) @staticmethod @callback diff --git a/homeassistant/components/hive/icons.json b/homeassistant/components/hive/icons.json index 2704317779cbd2..e4c06556906fd0 100644 --- a/homeassistant/components/hive/icons.json +++ b/homeassistant/components/hive/icons.json @@ -18,8 +18,14 @@ } }, "services": { - "boost_heating_on": "mdi:radiator", - "boost_heating_off": "mdi:radiator-off", - "boost_hot_water": "mdi:water-boiler" + "boost_heating_on": { + "service": "mdi:radiator" + }, + "boost_heating_off": { + "service": "mdi:radiator-off" + }, + "boost_hot_water": { + "service": "mdi:water-boiler" + } } } diff --git a/homeassistant/components/hlk_sw16/config_flow.py b/homeassistant/components/hlk_sw16/config_flow.py index b315d0daa78221..34ee1ebd0e7988 100644 --- a/homeassistant/components/hlk_sw16/config_flow.py +++ b/homeassistant/components/hlk_sw16/config_flow.py @@ -1,11 +1,13 @@ """Config flow for HLK-SW16.""" import asyncio +from typing import Any from hlk_sw16 import create_hlk_sw16_connection +from hlk_sw16.protocol import SW16Client import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_HOST, CONF_PORT from homeassistant.core import HomeAssistant @@ -26,7 +28,7 @@ ) -async def connect_client(hass, user_input): +async def connect_client(hass: HomeAssistant, user_input: dict[str, Any]) -> SW16Client: """Connect the HLK-SW16 client.""" client_aw = create_hlk_sw16_connection( host=user_input[CONF_HOST], @@ -40,7 +42,7 @@ async def connect_client(hass, user_input): return await client_aw -async def validate_input(hass: HomeAssistant, user_input): +async def validate_input(hass: HomeAssistant, user_input: dict[str, Any]) -> None: """Validate the user input allows us to connect.""" try: client = await connect_client(hass, user_input) @@ -69,11 +71,13 @@ class SW16FlowHandler(ConfigFlow, domain=DOMAIN): VERSION = 1 - async def async_step_import(self, user_input): + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Handle import.""" - return await self.async_step_user(user_input) + return await self.async_step_user(import_data) - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" errors = {} if user_input is not None: diff --git a/homeassistant/components/holiday/manifest.json b/homeassistant/components/holiday/manifest.json index 0a714815ae3e5b..0a2d98e71c529b 100644 --- a/homeassistant/components/holiday/manifest.json +++ b/homeassistant/components/holiday/manifest.json @@ -5,5 +5,5 @@ "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/holiday", "iot_class": "local_polling", - "requirements": ["holidays==0.54", "babel==2.15.0"] + "requirements": ["holidays==0.56", "babel==2.15.0"] } diff --git a/homeassistant/components/home_connect/icons.json b/homeassistant/components/home_connect/icons.json index 48965cc554ae19..33617f5472ebd9 100644 --- a/homeassistant/components/home_connect/icons.json +++ b/homeassistant/components/home_connect/icons.json @@ -1,11 +1,25 @@ { "services": { - "start_program": "mdi:play", - "select_program": "mdi:form-select", - "pause_program": "mdi:pause", - "resume_program": "mdi:play-pause", - "set_option_active": "mdi:gesture-tap", - "set_option_selected": "mdi:gesture-tap", - "change_setting": "mdi:cog" + "start_program": { + "service": "mdi:play" + }, + "select_program": { + "service": "mdi:form-select" + }, + "pause_program": { + "service": "mdi:pause" + }, + "resume_program": { + "service": "mdi:play-pause" + }, + "set_option_active": { + "service": "mdi:gesture-tap" + }, + "set_option_selected": { + "service": "mdi:gesture-tap" + }, + "change_setting": { + "service": "mdi:cog" + } } } diff --git a/homeassistant/components/homeassistant/icons.json b/homeassistant/components/homeassistant/icons.json index ec4d572991846f..f08fa8d969ba03 100644 --- a/homeassistant/components/homeassistant/icons.json +++ b/homeassistant/components/homeassistant/icons.json @@ -1,17 +1,43 @@ { "services": { - "check_config": "mdi:receipt-text-check", - "reload_core_config": "mdi:receipt-text-send", - "restart": "mdi:restart", - "set_location": "mdi:map-marker", - "stop": "mdi:stop", - "toggle": "mdi:toggle-switch", - "turn_on": "mdi:power-on", - "turn_off": "mdi:power-off", - "update_entity": "mdi:update", - "reload_custom_templates": "mdi:palette-swatch", - "reload_config_entry": "mdi:reload", - "save_persistent_states": "mdi:content-save", - "reload_all": "mdi:reload" + "check_config": { + "service": "mdi:receipt-text-check" + }, + "reload_core_config": { + "service": "mdi:receipt-text-send" + }, + "restart": { + "service": "mdi:restart" + }, + "set_location": { + "service": "mdi:map-marker" + }, + "stop": { + "service": "mdi:stop" + }, + "toggle": { + "service": "mdi:toggle-switch" + }, + "turn_on": { + "service": "mdi:power-on" + }, + "turn_off": { + "service": "mdi:power-off" + }, + "update_entity": { + "service": "mdi:update" + }, + "reload_custom_templates": { + "service": "mdi:palette-swatch" + }, + "reload_config_entry": { + "service": "mdi:reload" + }, + "save_persistent_states": { + "service": "mdi:content-save" + }, + "reload_all": { + "service": "mdi:reload" + } } } diff --git a/homeassistant/components/homeassistant_yellow/__init__.py b/homeassistant/components/homeassistant_yellow/__init__.py index 14c2de2c9a1fb3..04abe5a1dcae2d 100644 --- a/homeassistant/components/homeassistant_yellow/__init__.py +++ b/homeassistant/components/homeassistant_yellow/__init__.py @@ -2,18 +2,24 @@ from __future__ import annotations +import logging + from homeassistant.components.hassio import get_os_info, is_hassio from homeassistant.components.homeassistant_hardware.silabs_multiprotocol_addon import ( check_multi_pan_addon, - get_zigbee_socket, - multi_pan_addon_using_device, +) +from homeassistant.components.homeassistant_hardware.util import ( + ApplicationType, + guess_firmware_type, ) from homeassistant.config_entries import SOURCE_HARDWARE, ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady, HomeAssistantError from homeassistant.helpers import discovery_flow -from .const import RADIO_DEVICE, ZHA_HW_DISCOVERY_DATA +from .const import FIRMWARE, RADIO_DEVICE, ZHA_HW_DISCOVERY_DATA + +_LOGGER = logging.getLogger(__name__) async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: @@ -27,34 +33,26 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: # The hassio integration has not yet fetched data from the supervisor raise ConfigEntryNotReady - board: str | None - if (board := os_info.get("board")) is None or board != "yellow": + if os_info.get("board") != "yellow": # Not running on a Home Assistant Yellow, Home Assistant may have been migrated hass.async_create_task(hass.config_entries.async_remove(entry.entry_id)) return False - try: - await check_multi_pan_addon(hass) - except HomeAssistantError as err: - raise ConfigEntryNotReady from err - - if not await multi_pan_addon_using_device(hass, RADIO_DEVICE): - hw_discovery_data = ZHA_HW_DISCOVERY_DATA - else: - hw_discovery_data = { - "name": "Yellow Multiprotocol", - "port": { - "path": get_zigbee_socket(), - }, - "radio_type": "ezsp", - } - - discovery_flow.async_create_flow( - hass, - "zha", - context={"source": SOURCE_HARDWARE}, - data=hw_discovery_data, - ) + firmware = ApplicationType(entry.data[FIRMWARE]) + + if firmware is ApplicationType.CPC: + try: + await check_multi_pan_addon(hass) + except HomeAssistantError as err: + raise ConfigEntryNotReady from err + + if firmware is ApplicationType.EZSP: + discovery_flow.async_create_flow( + hass, + "zha", + context={"source": SOURCE_HARDWARE}, + data=ZHA_HW_DISCOVERY_DATA, + ) return True @@ -62,3 +60,39 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Unload a config entry.""" return True + + +async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: + """Migrate old entry.""" + + _LOGGER.debug( + "Migrating from version %s.%s", config_entry.version, config_entry.minor_version + ) + + if config_entry.version == 1: + if config_entry.minor_version == 1: + # Add-on startup with type service get started before Core, always (e.g. the + # Multi-Protocol add-on). Probing the firmware would interfere with the add-on, + # so we can't safely probe here. Instead, we must make an educated guess! + firmware_guess = await guess_firmware_type(hass, RADIO_DEVICE) + + new_data = {**config_entry.data} + new_data[FIRMWARE] = firmware_guess.firmware_type.value + + hass.config_entries.async_update_entry( + config_entry, + data=new_data, + version=1, + minor_version=2, + ) + + _LOGGER.debug( + "Migration to version %s.%s successful", + config_entry.version, + config_entry.minor_version, + ) + + return True + + # This means the user has downgraded from a future version + return False diff --git a/homeassistant/components/homeassistant_yellow/config_flow.py b/homeassistant/components/homeassistant_yellow/config_flow.py index d2212a968db123..1f4d150e49b556 100644 --- a/homeassistant/components/homeassistant_yellow/config_flow.py +++ b/homeassistant/components/homeassistant_yellow/config_flow.py @@ -2,11 +2,13 @@ from __future__ import annotations +from abc import ABC, abstractmethod import asyncio import logging -from typing import Any +from typing import Any, final import aiohttp +from universal_silabs_flasher.const import ApplicationType import voluptuous as vol from homeassistant.components.hassio import ( @@ -15,12 +17,25 @@ async_reboot_host, async_set_yellow_settings, ) -from homeassistant.components.homeassistant_hardware import silabs_multiprotocol_addon -from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResult +from homeassistant.components.homeassistant_hardware.firmware_config_flow import ( + BaseFirmwareConfigFlow, + BaseFirmwareOptionsFlow, +) +from homeassistant.components.homeassistant_hardware.silabs_multiprotocol_addon import ( + OptionsFlowHandler as MultiprotocolOptionsFlowHandler, + SerialPortSettings as MultiprotocolSerialPortSettings, +) +from homeassistant.config_entries import ( + SOURCE_HARDWARE, + ConfigEntry, + ConfigFlowResult, + OptionsFlow, +) from homeassistant.core import callback -from homeassistant.helpers import selector +from homeassistant.helpers import discovery_flow, selector -from .const import DOMAIN, ZHA_HW_DISCOVERY_DATA +from .const import DOMAIN, FIRMWARE, RADIO_DEVICE, ZHA_DOMAIN, ZHA_HW_DISCOVERY_DATA +from .hardware import BOARD_NAME _LOGGER = logging.getLogger(__name__) @@ -33,18 +48,30 @@ ) -class HomeAssistantYellowConfigFlow(ConfigFlow, domain=DOMAIN): +class HomeAssistantYellowConfigFlow(BaseFirmwareConfigFlow, domain=DOMAIN): """Handle a config flow for Home Assistant Yellow.""" VERSION = 1 + MINOR_VERSION = 2 + + def __init__(self, *args: Any, **kwargs: Any) -> None: + """Instantiate config flow.""" + super().__init__(*args, **kwargs) + + self._device = RADIO_DEVICE @staticmethod @callback def async_get_options_flow( config_entry: ConfigEntry, - ) -> HomeAssistantYellowOptionsFlow: + ) -> OptionsFlow: """Return the options flow.""" - return HomeAssistantYellowOptionsFlow(config_entry) + firmware_type = ApplicationType(config_entry.data[FIRMWARE]) + + if firmware_type is ApplicationType.CPC: + return HomeAssistantYellowMultiPanOptionsFlowHandler(config_entry) + + return HomeAssistantYellowOptionsFlowHandler(config_entry) async def async_step_system( self, data: dict[str, Any] | None = None @@ -53,30 +80,54 @@ async def async_step_system( if self._async_current_entries(): return self.async_abort(reason="single_instance_allowed") - return self.async_create_entry(title="Home Assistant Yellow", data={}) + # We do not actually use any portion of `BaseFirmwareConfigFlow` beyond this + await self._probe_firmware_type() + + # Kick off ZHA hardware discovery automatically if Zigbee firmware is running + if self._probed_firmware_type is ApplicationType.EZSP: + discovery_flow.async_create_flow( + self.hass, + ZHA_DOMAIN, + context={"source": SOURCE_HARDWARE}, + data=ZHA_HW_DISCOVERY_DATA, + ) + + return self._async_flow_finished() + + def _async_flow_finished(self) -> ConfigFlowResult: + """Create the config entry.""" + return self.async_create_entry( + title=BOARD_NAME, + data={ + # Assume the firmware type is EZSP if we cannot probe it + FIRMWARE: (self._probed_firmware_type or ApplicationType.EZSP).value, + }, + ) -class HomeAssistantYellowOptionsFlow(silabs_multiprotocol_addon.OptionsFlowHandler): - """Handle an option flow for Home Assistant Yellow.""" +class BaseHomeAssistantYellowOptionsFlow(OptionsFlow, ABC): + """Base Home Assistant Yellow options flow shared between firmware and multi-PAN.""" _hw_settings: dict[str, bool] | None = None + @abstractmethod + async def async_step_main_menu(self, _: None = None) -> ConfigFlowResult: + """Show the main menu.""" + + @final + async def async_step_init( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Manage the options flow.""" + return await self.async_step_main_menu() + + @final async def async_step_on_supervisor( self, user_input: dict[str, Any] | None = None ) -> ConfigFlowResult: """Handle logic when on Supervisor host.""" return await self.async_step_main_menu() - async def async_step_main_menu(self, _: None = None) -> ConfigFlowResult: - """Show the main menu.""" - return self.async_show_menu( - step_id="main_menu", - menu_options=[ - "hardware_settings", - "multipan_settings", - ], - ) - async def async_step_hardware_settings( self, user_input: dict[str, Any] | None = None ) -> ConfigFlowResult: @@ -133,18 +184,36 @@ async def async_step_reboot_later( """Reboot later.""" return self.async_create_entry(data={}) + +class HomeAssistantYellowMultiPanOptionsFlowHandler( + BaseHomeAssistantYellowOptionsFlow, MultiprotocolOptionsFlowHandler +): + """Handle a multi-PAN options flow for Home Assistant Yellow.""" + + async def async_step_main_menu(self, _: None = None) -> ConfigFlowResult: + """Show the main menu.""" + return self.async_show_menu( + step_id="main_menu", + menu_options=[ + "hardware_settings", + "multipan_settings", + ], + ) + async def async_step_multipan_settings( self, user_input: dict[str, Any] | None = None ) -> ConfigFlowResult: """Handle multipan settings.""" - return await super().async_step_on_supervisor(user_input) + return await MultiprotocolOptionsFlowHandler.async_step_on_supervisor( + self, user_input + ) async def _async_serial_port_settings( self, - ) -> silabs_multiprotocol_addon.SerialPortSettings: + ) -> MultiprotocolSerialPortSettings: """Return the radio serial port settings.""" - return silabs_multiprotocol_addon.SerialPortSettings( - device="/dev/ttyAMA1", + return MultiprotocolSerialPortSettings( + device=RADIO_DEVICE, baudrate="115200", flow_control=True, ) @@ -163,4 +232,64 @@ def _zha_name(self) -> str: def _hardware_name(self) -> str: """Return the name of the hardware.""" - return "Home Assistant Yellow" + return BOARD_NAME + + async def async_step_flashing_complete( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Finish flashing and update the config entry.""" + self.hass.config_entries.async_update_entry( + entry=self.config_entry, + data={ + **self.config_entry.data, + FIRMWARE: ApplicationType.EZSP.value, + }, + ) + + return await super().async_step_flashing_complete(user_input) + + +class HomeAssistantYellowOptionsFlowHandler( + BaseHomeAssistantYellowOptionsFlow, BaseFirmwareOptionsFlow +): + """Handle a firmware options flow for Home Assistant Yellow.""" + + def __init__(self, *args: Any, **kwargs: Any) -> None: + """Instantiate options flow.""" + super().__init__(*args, **kwargs) + + self._hardware_name = BOARD_NAME + self._device = RADIO_DEVICE + + # Regenerate the translation placeholders + self._get_translation_placeholders() + + async def async_step_main_menu(self, _: None = None) -> ConfigFlowResult: + """Show the main menu.""" + return self.async_show_menu( + step_id="main_menu", + menu_options=[ + "hardware_settings", + "firmware_settings", + ], + ) + + async def async_step_firmware_settings( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle firmware configuration settings.""" + return await super().async_step_pick_firmware() + + def _async_flow_finished(self) -> ConfigFlowResult: + """Create the config entry.""" + assert self._probed_firmware_type is not None + + self.hass.config_entries.async_update_entry( + entry=self.config_entry, + data={ + **self.config_entry.data, + FIRMWARE: self._probed_firmware_type.value, + }, + ) + + return self.async_create_entry(title="", data={}) diff --git a/homeassistant/components/homeassistant_yellow/const.py b/homeassistant/components/homeassistant_yellow/const.py index 8f1f9a4c2b8135..79753ae9b9ec75 100644 --- a/homeassistant/components/homeassistant_yellow/const.py +++ b/homeassistant/components/homeassistant_yellow/const.py @@ -12,3 +12,6 @@ }, "radio_type": "efr32", } + +FIRMWARE = "firmware" +ZHA_DOMAIN = "zha" diff --git a/homeassistant/components/homeassistant_yellow/strings.json b/homeassistant/components/homeassistant_yellow/strings.json index 95442d315003ac..fd3be3586b1b59 100644 --- a/homeassistant/components/homeassistant_yellow/strings.json +++ b/homeassistant/components/homeassistant_yellow/strings.json @@ -42,6 +42,7 @@ "main_menu": { "menu_options": { "hardware_settings": "[%key:component::homeassistant_yellow::options::step::hardware_settings::title%]", + "firmware_settings": "Switch between Zigbee or Thread firmware.", "multipan_settings": "Configure IEEE 802.15.4 radio multiprotocol support" } }, @@ -79,6 +80,46 @@ "start_flasher_addon": { "title": "[%key:component::homeassistant_hardware::silabs_multiprotocol_hardware::options::step::start_flasher_addon::title%]", "description": "[%key:component::homeassistant_hardware::silabs_multiprotocol_hardware::options::step::start_flasher_addon::description%]" + }, + "pick_firmware": { + "title": "[%key:component::homeassistant_hardware::firmware_picker::options::step::pick_firmware::title%]", + "description": "[%key:component::homeassistant_hardware::firmware_picker::options::step::pick_firmware::description%]", + "menu_options": { + "pick_firmware_thread": "[%key:component::homeassistant_hardware::firmware_picker::options::step::pick_firmware::menu_options::pick_firmware_thread%]", + "pick_firmware_zigbee": "[%key:component::homeassistant_hardware::firmware_picker::options::step::pick_firmware::menu_options::pick_firmware_zigbee%]" + } + }, + "install_zigbee_flasher_addon": { + "title": "[%key:component::homeassistant_hardware::firmware_picker::options::step::install_zigbee_flasher_addon::title%]", + "description": "[%key:component::homeassistant_hardware::firmware_picker::options::step::install_zigbee_flasher_addon::description%]" + }, + "run_zigbee_flasher_addon": { + "title": "[%key:component::homeassistant_hardware::firmware_picker::options::step::run_zigbee_flasher_addon::title%]", + "description": "[%key:component::homeassistant_hardware::firmware_picker::options::step::run_zigbee_flasher_addon::description%]" + }, + "zigbee_flasher_failed": { + "title": "[%key:component::homeassistant_hardware::firmware_picker::options::step::zigbee_flasher_failed::title%]", + "description": "[%key:component::homeassistant_hardware::firmware_picker::options::step::zigbee_flasher_failed::description%]" + }, + "confirm_zigbee": { + "title": "[%key:component::homeassistant_hardware::firmware_picker::options::step::confirm_zigbee::title%]", + "description": "[%key:component::homeassistant_hardware::firmware_picker::options::step::confirm_zigbee::description%]" + }, + "install_otbr_addon": { + "title": "[%key:component::homeassistant_hardware::firmware_picker::options::step::install_otbr_addon::title%]", + "description": "[%key:component::homeassistant_hardware::firmware_picker::options::step::install_otbr_addon::description%]" + }, + "start_otbr_addon": { + "title": "[%key:component::homeassistant_hardware::firmware_picker::options::step::start_otbr_addon::title%]", + "description": "[%key:component::homeassistant_hardware::firmware_picker::options::step::start_otbr_addon::description%]" + }, + "otbr_failed": { + "title": "[%key:component::homeassistant_hardware::firmware_picker::options::step::otbr_failed::title%]", + "description": "[%key:component::homeassistant_hardware::firmware_picker::options::step::otbr_failed::description%]" + }, + "confirm_otbr": { + "title": "[%key:component::homeassistant_hardware::firmware_picker::options::step::confirm_otbr::title%]", + "description": "[%key:component::homeassistant_hardware::firmware_picker::options::step::confirm_otbr::description%]" } }, "error": { @@ -93,11 +134,19 @@ "not_hassio": "[%key:component::homeassistant_hardware::silabs_multiprotocol_hardware::options::abort::not_hassio%]", "read_hw_settings_error": "Failed to read hardware settings", "write_hw_settings_error": "Failed to write hardware settings", - "zha_migration_failed": "[%key:component::homeassistant_hardware::silabs_multiprotocol_hardware::options::abort::zha_migration_failed%]" + "zha_migration_failed": "[%key:component::homeassistant_hardware::silabs_multiprotocol_hardware::options::abort::zha_migration_failed%]", + "not_hassio_thread": "[%key:component::homeassistant_hardware::firmware_picker::options::abort::not_hassio_thread%]", + "otbr_addon_already_running": "[%key:component::homeassistant_hardware::firmware_picker::options::abort::otbr_addon_already_running%]", + "zha_still_using_stick": "[%key:component::homeassistant_hardware::firmware_picker::options::abort::zha_still_using_stick%]", + "otbr_still_using_stick": "[%key:component::homeassistant_hardware::firmware_picker::options::abort::otbr_still_using_stick%]" }, "progress": { "install_addon": "[%key:component::homeassistant_hardware::silabs_multiprotocol_hardware::options::progress::install_addon%]", - "start_addon": "[%key:component::homeassistant_hardware::silabs_multiprotocol_hardware::options::progress::start_addon%]" + "start_addon": "[%key:component::homeassistant_hardware::silabs_multiprotocol_hardware::options::progress::start_addon%]", + "start_otbr_addon": "[%key:component::homeassistant_hardware::silabs_multiprotocol_hardware::options::progress::start_addon%]", + "install_zigbee_flasher_addon": "[%key:component::homeassistant_hardware::firmware_picker::options::progress::install_zigbee_flasher_addon%]", + "run_zigbee_flasher_addon": "[%key:component::homeassistant_hardware::firmware_picker::options::progress::run_zigbee_flasher_addon%]", + "uninstall_zigbee_flasher_addon": "[%key:component::homeassistant_hardware::firmware_picker::options::progress::uninstall_zigbee_flasher_addon%]" } } } diff --git a/homeassistant/components/homekit/config_flow.py b/homeassistant/components/homekit/config_flow.py index 78979f73490d09..f88aa646f0450f 100644 --- a/homeassistant/components/homekit/config_flow.py +++ b/homeassistant/components/homekit/config_flow.py @@ -311,12 +311,12 @@ async def async_step_accessory( title=f"{name}:{entry_data[CONF_PORT]}", data=entry_data ) - async def async_step_import(self, user_input: dict[str, Any]) -> ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Handle import from yaml.""" - if not self._async_is_unique_name_port(user_input): + if not self._async_is_unique_name_port(import_data): return self.async_abort(reason="port_name_in_use") return self.async_create_entry( - title=f"{user_input[CONF_NAME]}:{user_input[CONF_PORT]}", data=user_input + title=f"{import_data[CONF_NAME]}:{import_data[CONF_PORT]}", data=import_data ) @callback diff --git a/homeassistant/components/homekit/icons.json b/homeassistant/components/homekit/icons.json index fb0461eb5d8415..7d8ddf131efeb7 100644 --- a/homeassistant/components/homekit/icons.json +++ b/homeassistant/components/homekit/icons.json @@ -1,7 +1,13 @@ { "services": { - "reload": "mdi:reload", - "reset_accessory": "mdi:cog-refresh", - "unpair": "mdi:link-variant-off" + "reload": { + "service": "mdi:reload" + }, + "reset_accessory": { + "service": "mdi:cog-refresh" + }, + "unpair": { + "service": "mdi:link-variant-off" + } } } diff --git a/homeassistant/components/homekit/manifest.json b/homeassistant/components/homekit/manifest.json index 17d1237e579ef7..eebdc0026fd738 100644 --- a/homeassistant/components/homekit/manifest.json +++ b/homeassistant/components/homekit/manifest.json @@ -10,7 +10,7 @@ "loggers": ["pyhap"], "requirements": [ "HAP-python==4.9.1", - "fnv-hash-fast==0.5.0", + "fnv-hash-fast==1.0.2", "PyQRCode==1.2.1", "base36==0.1.1" ], diff --git a/homeassistant/components/homekit_controller/connection.py b/homeassistant/components/homekit_controller/connection.py index 4da907daf3e7af..934e7e883aef5d 100644 --- a/homeassistant/components/homekit_controller/connection.py +++ b/homeassistant/components/homekit_controller/connection.py @@ -154,6 +154,7 @@ def __init__( self._pending_subscribes: set[tuple[int, int]] = set() self._subscribe_timer: CALLBACK_TYPE | None = None self._load_platforms_lock = asyncio.Lock() + self._full_update_requested: bool = False @property def entity_map(self) -> Accessories: @@ -841,6 +842,7 @@ def async_update_available_state(self, *_: Any) -> None: async def async_request_update(self, now: datetime | None = None) -> None: """Request an debounced update from the accessory.""" + self._full_update_requested = True await self._debounced_update.async_call() async def async_update(self, now: datetime | None = None) -> None: @@ -849,7 +851,8 @@ async def async_update(self, now: datetime | None = None) -> None: accessories = self.entity_map.accessories if ( - len(accessories) == 1 + not self._full_update_requested + and len(accessories) == 1 and self.available and not (to_poll - self.watchable_characteristics) and self.pairing.is_available @@ -879,6 +882,8 @@ async def async_update(self, now: datetime | None = None) -> None: firmware_iid = accessory_info[CharacteristicsTypes.FIRMWARE_REVISION].iid to_poll = {(first_accessory.aid, firmware_iid)} + self._full_update_requested = False + if not to_poll: self.async_update_available_state() _LOGGER.debug( diff --git a/homeassistant/components/homekit_controller/cover.py b/homeassistant/components/homekit_controller/cover.py index d0944db38f8466..0eebb72c988a5f 100644 --- a/homeassistant/components/homekit_controller/cover.py +++ b/homeassistant/components/homekit_controller/cover.py @@ -214,34 +214,32 @@ def is_vertical_tilt(self) -> bool: @property def current_cover_tilt_position(self) -> int | None: """Return current position of cover tilt.""" - tilt_position = self.service.value(CharacteristicsTypes.VERTICAL_TILT_CURRENT) - if not tilt_position: - tilt_position = self.service.value( - CharacteristicsTypes.HORIZONTAL_TILT_CURRENT - ) - if tilt_position is None: - return None - # Recalculate to convert from arcdegree scale to percentage scale. if self.is_vertical_tilt: - scale = 0.9 - if ( - self.service[CharacteristicsTypes.VERTICAL_TILT_CURRENT].minValue == -90 - and self.service[CharacteristicsTypes.VERTICAL_TILT_CURRENT].maxValue - == 0 - ): - scale = -0.9 - tilt_position = int(tilt_position / scale) + char = self.service[CharacteristicsTypes.VERTICAL_TILT_CURRENT] elif self.is_horizontal_tilt: - scale = 0.9 - if ( - self.service[CharacteristicsTypes.HORIZONTAL_TILT_TARGET].minValue - == -90 - and self.service[CharacteristicsTypes.HORIZONTAL_TILT_TARGET].maxValue - == 0 - ): - scale = -0.9 - tilt_position = int(tilt_position / scale) - return tilt_position + char = self.service[CharacteristicsTypes.HORIZONTAL_TILT_CURRENT] + else: + return None + + # Recalculate tilt_position. Convert arc to percent scale based on min/max values. + tilt_position = char.value + min_value = char.minValue + max_value = char.maxValue + total_range = int(max_value or 0) - int(min_value or 0) + + if ( + tilt_position is None + or min_value is None + or max_value is None + or total_range <= 0 + ): + return None + + # inverted scale + if min_value == -90 and max_value == 0: + return abs(int(100 / total_range * (tilt_position - max_value))) + # normal scale + return abs(int(100 / total_range * (tilt_position - min_value))) async def async_stop_cover(self, **kwargs: Any) -> None: """Send hold command.""" @@ -265,34 +263,32 @@ async def async_set_cover_position(self, **kwargs: Any) -> None: async def async_set_cover_tilt_position(self, **kwargs: Any) -> None: """Move the cover tilt to a specific position.""" tilt_position = kwargs[ATTR_TILT_POSITION] + if self.is_vertical_tilt: - # Recalculate to convert from percentage scale to arcdegree scale. - scale = 0.9 - if ( - self.service[CharacteristicsTypes.VERTICAL_TILT_TARGET].minValue == -90 - and self.service[CharacteristicsTypes.VERTICAL_TILT_TARGET].maxValue - == 0 - ): - scale = -0.9 - tilt_position = int(tilt_position * scale) - await self.async_put_characteristics( - {CharacteristicsTypes.VERTICAL_TILT_TARGET: tilt_position} - ) + char = self.service[CharacteristicsTypes.VERTICAL_TILT_TARGET] elif self.is_horizontal_tilt: - # Recalculate to convert from percentage scale to arcdegree scale. - scale = 0.9 - if ( - self.service[CharacteristicsTypes.HORIZONTAL_TILT_TARGET].minValue - == -90 - and self.service[CharacteristicsTypes.HORIZONTAL_TILT_TARGET].maxValue - == 0 - ): - scale = -0.9 - tilt_position = int(tilt_position * scale) - await self.async_put_characteristics( - {CharacteristicsTypes.HORIZONTAL_TILT_TARGET: tilt_position} + char = self.service[CharacteristicsTypes.HORIZONTAL_TILT_TARGET] + + # Calculate tilt_position. Convert from 1-100 scale to arc degree scale respecting possible min/max Values. + min_value = char.minValue + max_value = char.maxValue + if min_value is None or max_value is None: + raise ValueError( + "Entity does not provide minValue and maxValue for the tilt" + ) + + # inverted scale + if min_value == -90 and max_value == 0: + tilt_position = int( + tilt_position / 100 * (min_value - max_value) + max_value + ) + else: + tilt_position = int( + tilt_position / 100 * (max_value - min_value) + min_value ) + await self.async_put_characteristics({char.type: tilt_position}) + @property def extra_state_attributes(self) -> dict[str, Any]: """Return the optional state attributes.""" diff --git a/homeassistant/components/homematic/icons.json b/homeassistant/components/homematic/icons.json index 998c9a385bac15..9e58bbe3a9095b 100644 --- a/homeassistant/components/homematic/icons.json +++ b/homeassistant/components/homematic/icons.json @@ -1,10 +1,22 @@ { "services": { - "virtualkey": "mdi:keyboard", - "set_variable_value": "mdi:console", - "set_device_value": "mdi:television", - "reconnect": "mdi:wifi-refresh", - "set_install_mode": "mdi:cog", - "put_paramset": "mdi:cog" + "virtualkey": { + "service": "mdi:keyboard" + }, + "set_variable_value": { + "service": "mdi:console" + }, + "set_device_value": { + "service": "mdi:television" + }, + "reconnect": { + "service": "mdi:wifi-refresh" + }, + "set_install_mode": { + "service": "mdi:cog" + }, + "put_paramset": { + "service": "mdi:cog" + } } } diff --git a/homeassistant/components/homematicip_cloud/config_flow.py b/homeassistant/components/homematicip_cloud/config_flow.py index c2277e16c7998e..a8b17a80aff048 100644 --- a/homeassistant/components/homematicip_cloud/config_flow.py +++ b/homeassistant/components/homematicip_cloud/config_flow.py @@ -83,11 +83,11 @@ async def async_step_link(self, user_input: None = None) -> ConfigFlowResult: return self.async_show_form(step_id="link", errors=errors) - async def async_step_import(self, import_info: dict[str, str]) -> ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, str]) -> ConfigFlowResult: """Import a new access point as a config entry.""" - hapid = import_info[HMIPC_HAPID].replace("-", "").upper() - authtoken = import_info[HMIPC_AUTHTOKEN] - name = import_info[HMIPC_NAME] + hapid = import_data[HMIPC_HAPID].replace("-", "").upper() + authtoken = import_data[HMIPC_AUTHTOKEN] + name = import_data[HMIPC_NAME] await self.async_set_unique_id(hapid) self._abort_if_unique_id_configured() diff --git a/homeassistant/components/homematicip_cloud/icons.json b/homeassistant/components/homematicip_cloud/icons.json index 73c60ea8cddc8b..53a39d8213c50e 100644 --- a/homeassistant/components/homematicip_cloud/icons.json +++ b/homeassistant/components/homematicip_cloud/icons.json @@ -1,13 +1,31 @@ { "services": { - "activate_eco_mode_with_duration": "mdi:leaf", - "activate_eco_mode_with_period": "mdi:leaf", - "activate_vacation": "mdi:compass", - "deactivate_eco_mode": "mdi:leaf-off", - "deactivate_vacation": "mdi:compass-off", - "set_active_climate_profile": "mdi:home-thermometer", - "dump_hap_config": "mdi:database-export", - "reset_energy_counter": "mdi:reload", - "set_home_cooling_mode": "mdi:snowflake" + "activate_eco_mode_with_duration": { + "service": "mdi:leaf" + }, + "activate_eco_mode_with_period": { + "service": "mdi:leaf" + }, + "activate_vacation": { + "service": "mdi:compass" + }, + "deactivate_eco_mode": { + "service": "mdi:leaf-off" + }, + "deactivate_vacation": { + "service": "mdi:compass-off" + }, + "set_active_climate_profile": { + "service": "mdi:home-thermometer" + }, + "dump_hap_config": { + "service": "mdi:database-export" + }, + "reset_energy_counter": { + "service": "mdi:reload" + }, + "set_home_cooling_mode": { + "service": "mdi:snowflake" + } } } diff --git a/homeassistant/components/homewizard/sensor.py b/homeassistant/components/homewizard/sensor.py index c5cf0bc64c7fba..9bb61a467cb207 100644 --- a/homeassistant/components/homewizard/sensor.py +++ b/homeassistant/components/homewizard/sensor.py @@ -625,26 +625,8 @@ async def async_setup_entry( ) -> None: """Initialize sensors.""" - # Migrate original gas meter sensor to ExternalDevice - # This is sensor that was directly linked to the P1 Meter - # Migration can be removed after 2024.8.0 ent_reg = er.async_get(hass) data = entry.runtime_data.data.data - if ( - entity_id := ent_reg.async_get_entity_id( - Platform.SENSOR, DOMAIN, f"{entry.unique_id}_total_gas_m3" - ) - ) and data.gas_unique_id is not None: - ent_reg.async_update_entity( - entity_id, - new_unique_id=f"{DOMAIN}_gas_meter_{data.gas_unique_id}", - ) - - # Remove old gas_unique_id sensor - if entity_id := ent_reg.async_get_entity_id( - Platform.SENSOR, DOMAIN, f"{entry.unique_id}_gas_unique_id" - ): - ent_reg.async_remove(entity_id) # Initialize default sensors entities: list = [ diff --git a/homeassistant/components/homeworks/icons.json b/homeassistant/components/homeworks/icons.json index f53b447d96e936..fc39b2ef455905 100644 --- a/homeassistant/components/homeworks/icons.json +++ b/homeassistant/components/homeworks/icons.json @@ -1,5 +1,7 @@ { "services": { - "send_command": "mdi:console" + "send_command": { + "service": "mdi:console" + } } } diff --git a/homeassistant/components/honeywell/climate.py b/homeassistant/components/honeywell/climate.py index 141cb87f1174f1..934d41b238e271 100644 --- a/homeassistant/components/honeywell/climate.py +++ b/homeassistant/components/honeywell/climate.py @@ -35,11 +35,7 @@ from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError, ServiceValidationError -from homeassistant.helpers import ( - device_registry as dr, - entity_registry as er, - issue_registry as ir, -) +from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.util.unit_conversion import TemperatureConverter @@ -218,9 +214,6 @@ def __init__( if device._data.get("canControlHumidification"): # noqa: SLF001 self._attr_supported_features |= ClimateEntityFeature.TARGET_HUMIDITY - if device.raw_ui_data.get("SwitchEmergencyHeatAllowed"): - self._attr_supported_features |= ClimateEntityFeature.AUX_HEAT - if not device._data.get("hasFan"): # noqa: SLF001 return @@ -337,11 +330,6 @@ def preset_mode(self) -> str | None: return PRESET_NONE - @property - def is_aux_heat(self) -> bool | None: - """Return true if aux heater.""" - return self._device.system_mode == "emheat" - @property def fan_mode(self) -> str | None: """Return the fan setting.""" @@ -538,53 +526,6 @@ async def async_set_preset_mode(self, preset_mode: str) -> None: else: await self._turn_away_mode_off() - async def async_turn_aux_heat_on(self) -> None: - """Turn auxiliary heater on.""" - ir.async_create_issue( - self.hass, - DOMAIN, - "service_deprecation", - breaks_in_ha_version="2024.10.0", - is_fixable=True, - is_persistent=True, - severity=ir.IssueSeverity.WARNING, - translation_key="service_deprecation", - ) - try: - await self._device.set_system_mode("emheat") - - except SomeComfortError as err: - raise HomeAssistantError( - translation_domain=DOMAIN, - translation_key="set_aux_failed", - ) from err - - async def async_turn_aux_heat_off(self) -> None: - """Turn auxiliary heater off.""" - - ir.async_create_issue( - self.hass, - DOMAIN, - "service_deprecation", - breaks_in_ha_version="2024.10.0", - is_fixable=True, - is_persistent=True, - severity=ir.IssueSeverity.WARNING, - translation_key="service_deprecation", - ) - - try: - if HVACMode.HEAT in self.hvac_modes: - await self.async_set_hvac_mode(HVACMode.HEAT) - else: - await self.async_set_hvac_mode(HVACMode.OFF) - - except HomeAssistantError as err: - raise HomeAssistantError( - translation_domain=DOMAIN, - translation_key="disable_aux_failed", - ) from err - async def async_update(self) -> None: """Get the latest state from the service.""" diff --git a/homeassistant/components/honeywell/strings.json b/homeassistant/components/honeywell/strings.json index d3bc1924e28931..aa6e53620a55b2 100644 --- a/homeassistant/components/honeywell/strings.json +++ b/homeassistant/components/honeywell/strings.json @@ -88,30 +88,11 @@ "stop_hold_failed": { "message": "Honeywell could not stop hold mode" }, - "set_aux_failed": { - "message": "Honeywell could not set system mode to aux heat" - }, - "disable_aux_failed": { - "message": "Honeywell could turn off aux heat mode" - }, "switch_failed_off": { "message": "Honeywell could turn off emergency heat mode." }, "switch_failed_on": { "message": "Honeywell could not set system mode to emergency heat mode." } - }, - "issues": { - "service_deprecation": { - "title": "Honeywell aux heat is being removed", - "fix_flow": { - "step": { - "confirm": { - "title": "[%key:component::honeywell::issues::service_deprecation::title%]", - "description": "Use `switch.{name}_emergency_heat` instead to change mode.\n\nPlease adjust your automations and scripts and select **submit** to fix this issue." - } - } - } - } } } diff --git a/homeassistant/components/html5/__init__.py b/homeassistant/components/html5/__init__.py index 88e437ef5666d4..4b85bf8ab8cd32 100644 --- a/homeassistant/components/html5/__init__.py +++ b/homeassistant/components/html5/__init__.py @@ -1 +1,16 @@ """The html5 component.""" + +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers import discovery + +from .const import DOMAIN + + +async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: + """Set up HTML5 from a config entry.""" + await discovery.async_load_platform( + hass, Platform.NOTIFY, DOMAIN, dict(entry.data), {} + ) + return True diff --git a/homeassistant/components/html5/config_flow.py b/homeassistant/components/html5/config_flow.py new file mode 100644 index 00000000000000..1dae0102d05c9c --- /dev/null +++ b/homeassistant/components/html5/config_flow.py @@ -0,0 +1,103 @@ +"""Config flow for the html5 component.""" + +import binascii +from typing import Any, cast + +from cryptography.hazmat.backends import default_backend +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric import ec +from py_vapid import Vapid +from py_vapid.utils import b64urlencode +import voluptuous as vol + +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult +from homeassistant.const import CONF_NAME +from homeassistant.core import callback + +from .const import ATTR_VAPID_EMAIL, ATTR_VAPID_PRV_KEY, ATTR_VAPID_PUB_KEY, DOMAIN +from .issues import async_create_html5_issue + + +def vapid_generate_private_key() -> str: + """Generate a VAPID private key.""" + private_key = ec.generate_private_key(ec.SECP256R1(), default_backend()) + return b64urlencode( + binascii.unhexlify(f"{private_key.private_numbers().private_value:x}".zfill(64)) + ) + + +def vapid_get_public_key(private_key: str) -> str: + """Get the VAPID public key from a private key.""" + vapid = Vapid.from_string(private_key) + public_key = cast(ec.EllipticCurvePublicKey, vapid.public_key) + return b64urlencode( + public_key.public_bytes( + serialization.Encoding.X962, serialization.PublicFormat.UncompressedPoint + ) + ) + + +class HTML5ConfigFlow(ConfigFlow, domain=DOMAIN): + """Handle a config flow for HTML5.""" + + @callback + def _async_create_html5_entry( + self: "HTML5ConfigFlow", data: dict[str, str] + ) -> tuple[dict[str, str], ConfigFlowResult | None]: + """Create an HTML5 entry.""" + errors = {} + flow_result = None + + if not data.get(ATTR_VAPID_PRV_KEY): + data[ATTR_VAPID_PRV_KEY] = vapid_generate_private_key() + + # we will always generate the corresponding public key + try: + data[ATTR_VAPID_PUB_KEY] = vapid_get_public_key(data[ATTR_VAPID_PRV_KEY]) + except (ValueError, binascii.Error): + errors[ATTR_VAPID_PRV_KEY] = "invalid_prv_key" + + if not errors: + config = { + ATTR_VAPID_EMAIL: data[ATTR_VAPID_EMAIL], + ATTR_VAPID_PRV_KEY: data[ATTR_VAPID_PRV_KEY], + ATTR_VAPID_PUB_KEY: data[ATTR_VAPID_PUB_KEY], + CONF_NAME: DOMAIN, + } + flow_result = self.async_create_entry(title="HTML5", data=config) + return errors, flow_result + + async def async_step_user( + self: "HTML5ConfigFlow", user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle a flow initialized by the user.""" + errors: dict[str, str] = {} + if user_input: + errors, flow_result = self._async_create_html5_entry(user_input) + if flow_result: + return flow_result + else: + user_input = {} + + return self.async_show_form( + data_schema=vol.Schema( + { + vol.Required( + ATTR_VAPID_EMAIL, default=user_input.get(ATTR_VAPID_EMAIL, "") + ): str, + vol.Optional(ATTR_VAPID_PRV_KEY): str, + } + ), + errors=errors, + ) + + async def async_step_import( + self: "HTML5ConfigFlow", import_config: dict + ) -> ConfigFlowResult: + """Handle config import from yaml.""" + _, flow_result = self._async_create_html5_entry(import_config) + if not flow_result: + async_create_html5_issue(self.hass, False) + return self.async_abort(reason="invalid_config") + async_create_html5_issue(self.hass, True) + return flow_result diff --git a/homeassistant/components/html5/const.py b/homeassistant/components/html5/const.py index bf7eaca7e24f73..75826ab90c91d8 100644 --- a/homeassistant/components/html5/const.py +++ b/homeassistant/components/html5/const.py @@ -1,4 +1,9 @@ """Constants for the HTML5 component.""" DOMAIN = "html5" +DATA_HASS_CONFIG = "html5_hass_config" SERVICE_DISMISS = "dismiss" + +ATTR_VAPID_PUB_KEY = "vapid_pub_key" +ATTR_VAPID_PRV_KEY = "vapid_prv_key" +ATTR_VAPID_EMAIL = "vapid_email" diff --git a/homeassistant/components/html5/icons.json b/homeassistant/components/html5/icons.json index c3d6e27efda376..d0a6013dd12524 100644 --- a/homeassistant/components/html5/icons.json +++ b/homeassistant/components/html5/icons.json @@ -1,5 +1,7 @@ { "services": { - "dismiss": "mdi:bell-off" + "dismiss": { + "service": "mdi:bell-off" + } } } diff --git a/homeassistant/components/html5/issues.py b/homeassistant/components/html5/issues.py new file mode 100644 index 00000000000000..8892562d347e5e --- /dev/null +++ b/homeassistant/components/html5/issues.py @@ -0,0 +1,50 @@ +"""Issues utility for HTML5.""" + +import logging + +from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant, callback +from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue + +from .const import DOMAIN + +_LOGGER = logging.getLogger(__name__) + +SUCCESSFUL_IMPORT_TRANSLATION_KEY = "deprecated_yaml" +FAILED_IMPORT_TRANSLATION_KEY = "deprecated_yaml_import_issue" + +INTEGRATION_TITLE = "HTML5 Push Notifications" + + +@callback +def async_create_html5_issue(hass: HomeAssistant, import_success: bool) -> None: + """Create issues for HTML5.""" + if import_success: + async_create_issue( + hass, + HOMEASSISTANT_DOMAIN, + f"deprecated_yaml_{DOMAIN}", + breaks_in_ha_version="2025.4.0", + is_fixable=False, + issue_domain=DOMAIN, + severity=IssueSeverity.WARNING, + translation_key="deprecated_yaml", + translation_placeholders={ + "domain": DOMAIN, + "integration_title": INTEGRATION_TITLE, + }, + ) + else: + async_create_issue( + hass, + DOMAIN, + f"deprecated_yaml_{DOMAIN}", + breaks_in_ha_version="2025.4.0", + is_fixable=False, + issue_domain=DOMAIN, + severity=IssueSeverity.WARNING, + translation_key="deprecated_yaml_import_issue", + translation_placeholders={ + "domain": DOMAIN, + "integration_title": INTEGRATION_TITLE, + }, + ) diff --git a/homeassistant/components/html5/manifest.json b/homeassistant/components/html5/manifest.json index f480086d153baa..c6cbd826544b96 100644 --- a/homeassistant/components/html5/manifest.json +++ b/homeassistant/components/html5/manifest.json @@ -1,10 +1,12 @@ { "domain": "html5", "name": "HTML5 Push Notifications", - "codeowners": [], + "codeowners": ["@alexyao2015"], + "config_flow": true, "dependencies": ["http"], "documentation": "https://www.home-assistant.io/integrations/html5", "iot_class": "cloud_push", "loggers": ["http_ece", "py_vapid", "pywebpush"], - "requirements": ["pywebpush==1.14.1"] + "requirements": ["pywebpush==1.14.1"], + "single_config_entry": true } diff --git a/homeassistant/components/html5/notify.py b/homeassistant/components/html5/notify.py index 8082ca37aa3820..48cc05984790cf 100644 --- a/homeassistant/components/html5/notify.py +++ b/homeassistant/components/html5/notify.py @@ -29,6 +29,7 @@ PLATFORM_SCHEMA as NOTIFY_PLATFORM_SCHEMA, BaseNotificationService, ) +from homeassistant.config_entries import SOURCE_IMPORT from homeassistant.const import ATTR_NAME, URL_ROOT from homeassistant.core import HomeAssistant, ServiceCall from homeassistant.exceptions import HomeAssistantError @@ -38,32 +39,23 @@ from homeassistant.util import ensure_unique_string from homeassistant.util.json import JsonObjectType, load_json_object -from .const import DOMAIN, SERVICE_DISMISS +from .const import ( + ATTR_VAPID_EMAIL, + ATTR_VAPID_PRV_KEY, + ATTR_VAPID_PUB_KEY, + DOMAIN, + SERVICE_DISMISS, +) +from .issues import async_create_html5_issue _LOGGER = logging.getLogger(__name__) REGISTRATIONS_FILE = "html5_push_registrations.conf" -ATTR_VAPID_PUB_KEY = "vapid_pub_key" -ATTR_VAPID_PRV_KEY = "vapid_prv_key" -ATTR_VAPID_EMAIL = "vapid_email" - - -def gcm_api_deprecated(value): - """Warn user that GCM API config is deprecated.""" - if value: - _LOGGER.warning( - "Configuring html5_push_notifications via the GCM api" - " has been deprecated and stopped working since May 29," - " 2019. Use the VAPID configuration instead. For instructions," - " see https://www.home-assistant.io/integrations/html5/" - ) - return value - PLATFORM_SCHEMA = NOTIFY_PLATFORM_SCHEMA.extend( { - vol.Optional("gcm_sender_id"): vol.All(cv.string, gcm_api_deprecated), + vol.Optional("gcm_sender_id"): cv.string, vol.Optional("gcm_api_key"): cv.string, vol.Required(ATTR_VAPID_PUB_KEY): cv.string, vol.Required(ATTR_VAPID_PRV_KEY): cv.string, @@ -171,15 +163,30 @@ async def async_get_service( discovery_info: DiscoveryInfoType | None = None, ) -> HTML5NotificationService | None: """Get the HTML5 push notification service.""" + if config: + existing_config_entry = hass.config_entries.async_entries(DOMAIN) + if existing_config_entry: + async_create_html5_issue(hass, True) + return None + hass.async_create_task( + hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_IMPORT}, data=config + ) + ) + return None + + if discovery_info is None: + return None + json_path = hass.config.path(REGISTRATIONS_FILE) registrations = await hass.async_add_executor_job(_load_config, json_path) - vapid_pub_key = config[ATTR_VAPID_PUB_KEY] - vapid_prv_key = config[ATTR_VAPID_PRV_KEY] - vapid_email = config[ATTR_VAPID_EMAIL] + vapid_pub_key = discovery_info[ATTR_VAPID_PUB_KEY] + vapid_prv_key = discovery_info[ATTR_VAPID_PRV_KEY] + vapid_email = discovery_info[ATTR_VAPID_EMAIL] - def websocket_appkey(hass, connection, msg): + def websocket_appkey(_hass, connection, msg): connection.send_message(websocket_api.result_message(msg["id"], vapid_pub_key)) websocket_api.async_register_command( diff --git a/homeassistant/components/html5/strings.json b/homeassistant/components/html5/strings.json index fa69025c43c1b8..40bdbb3626112f 100644 --- a/homeassistant/components/html5/strings.json +++ b/homeassistant/components/html5/strings.json @@ -1,4 +1,31 @@ { + "config": { + "step": { + "user": { + "data": { + "vapid_email": "[%key:common::config_flow::data::email%]", + "vapid_prv_key": "VAPID private key" + }, + "data_description": { + "vapid_email": "Email to use for html5 push notifications.", + "vapid_prv_key": "If not specified, one will be automatically generated." + } + } + }, + "error": { + "unknown": "Unknown error", + "invalid_prv_key": "Invalid private key" + }, + "abort": { + "invalid_config": "Invalid configuration" + } + }, + "issues": { + "deprecated_yaml_import_issue": { + "title": "HTML5 YAML configuration import failed", + "description": "Configuring HTML5 push notification using YAML has been deprecated. An automatic import of your existing configuration was attempted, but it failed.\n\nPlease remove the HTML5 push notification YAML configuration from your configuration.yaml file and reconfigure HTML5 push notification again manually." + } + }, "services": { "dismiss": { "name": "Dismiss", diff --git a/homeassistant/components/huawei_lte/icons.json b/homeassistant/components/huawei_lte/icons.json index d105702bf51821..a338cc65ed4e56 100644 --- a/homeassistant/components/huawei_lte/icons.json +++ b/homeassistant/components/huawei_lte/icons.json @@ -53,7 +53,11 @@ } }, "services": { - "resume_integration": "mdi:play-pause", - "suspend_integration": "mdi:pause" + "resume_integration": { + "service": "mdi:play-pause" + }, + "suspend_integration": { + "service": "mdi:pause" + } } } diff --git a/homeassistant/components/hue/config_flow.py b/homeassistant/components/hue/config_flow.py index fb32f568ee137b..e73ae8fe11ddd4 100644 --- a/homeassistant/components/hue/config_flow.py +++ b/homeassistant/components/hue/config_flow.py @@ -258,7 +258,7 @@ async def async_step_homekit( await self._async_handle_discovery_without_unique_id() return await self.async_step_link() - async def async_step_import(self, import_info: dict[str, Any]) -> ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Import a new bridge as a config entry. This flow is triggered by `async_setup` for both configured and @@ -268,9 +268,9 @@ async def async_step_import(self, import_info: dict[str, Any]) -> ConfigFlowResu This flow is also triggered by `async_step_discovery`. """ # Check if host exists, abort if so. - self._async_abort_entries_match({"host": import_info["host"]}) + self._async_abort_entries_match({"host": import_data["host"]}) - bridge = await self._get_bridge(import_info["host"]) + bridge = await self._get_bridge(import_data["host"]) if bridge is None: return self.async_abort(reason="cannot_connect") self.bridge = bridge diff --git a/homeassistant/components/hue/icons.json b/homeassistant/components/hue/icons.json index 9371ae5843e32a..31464308b0a3a2 100644 --- a/homeassistant/components/hue/icons.json +++ b/homeassistant/components/hue/icons.json @@ -1,6 +1,10 @@ { "services": { - "hue_activate_scene": "mdi:palette", - "activate_scene": "mdi:palette" + "hue_activate_scene": { + "service": "mdi:palette" + }, + "activate_scene": { + "service": "mdi:palette" + } } } diff --git a/homeassistant/components/hue/manifest.json b/homeassistant/components/hue/manifest.json index 71aabd4c2043a1..dbd9b5119771e2 100644 --- a/homeassistant/components/hue/manifest.json +++ b/homeassistant/components/hue/manifest.json @@ -11,6 +11,6 @@ "iot_class": "local_push", "loggers": ["aiohue"], "quality_scale": "platinum", - "requirements": ["aiohue==4.7.2"], + "requirements": ["aiohue==4.7.3"], "zeroconf": ["_hue._tcp.local."] } diff --git a/homeassistant/components/hue/v2/hue_event.py b/homeassistant/components/hue/v2/hue_event.py index b286a11aade2c5..2eace5139afbfc 100644 --- a/homeassistant/components/hue/v2/hue_event.py +++ b/homeassistant/components/hue/v2/hue_event.py @@ -80,9 +80,9 @@ def handle_rotary_event(evt_type: EventType, hue_resource: RelativeRotary) -> No CONF_DEVICE_ID: device.id, # type: ignore[union-attr] CONF_UNIQUE_ID: hue_resource.id, CONF_TYPE: hue_resource.relative_rotary.rotary_report.action.value, - CONF_SUBTYPE: hue_resource.relative_rotary.last_event.rotation.direction.value, - CONF_DURATION: hue_resource.relative_rotary.last_event.rotation.duration, - CONF_STEPS: hue_resource.relative_rotary.last_event.rotation.steps, + CONF_SUBTYPE: hue_resource.relative_rotary.rotary_report.rotation.direction.value, + CONF_DURATION: hue_resource.relative_rotary.rotary_report.rotation.duration, + CONF_STEPS: hue_resource.relative_rotary.rotary_report.rotation.steps, } hass.bus.async_fire(ATTR_HUE_EVENT, data) diff --git a/homeassistant/components/huisbaasje/config_flow.py b/homeassistant/components/huisbaasje/config_flow.py index ecf8cdbe43154a..43fbe839fa6a50 100644 --- a/homeassistant/components/huisbaasje/config_flow.py +++ b/homeassistant/components/huisbaasje/config_flow.py @@ -1,11 +1,12 @@ """Config flow for EnergyFlip integration.""" import logging +from typing import Any from energyflip import EnergyFlip, EnergyFlipConnectionException, EnergyFlipException import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_ID, CONF_PASSWORD, CONF_USERNAME from homeassistant.data_entry_flow import AbortFlow @@ -23,7 +24,9 @@ class EnergyFlipConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle a flow initiated by the user.""" if user_input is None: return await self._show_setup_form(user_input) diff --git a/homeassistant/components/humidifier/icons.json b/homeassistant/components/humidifier/icons.json index 2c67f759195236..15951df432db81 100644 --- a/homeassistant/components/humidifier/icons.json +++ b/homeassistant/components/humidifier/icons.json @@ -33,10 +33,20 @@ } }, "services": { - "set_humidity": "mdi:water-percent", - "set_mode": "mdi:air-humidifier", - "toggle": "mdi:air-humidifier", - "turn_off": "mdi:air-humidifier-off", - "turn_on": "mdi:air-humidifier" + "set_humidity": { + "service": "mdi:water-percent" + }, + "set_mode": { + "service": "mdi:air-humidifier" + }, + "toggle": { + "service": "mdi:air-humidifier" + }, + "turn_off": { + "service": "mdi:air-humidifier-off" + }, + "turn_on": { + "service": "mdi:air-humidifier" + } } } diff --git a/homeassistant/components/husqvarna_automower/icons.json b/homeassistant/components/husqvarna_automower/icons.json index 9dc1cbeb667645..bcaf1826260b57 100644 --- a/homeassistant/components/husqvarna_automower/icons.json +++ b/homeassistant/components/husqvarna_automower/icons.json @@ -34,7 +34,11 @@ } }, "services": { - "override_schedule": "mdi:debug-step-over", - "override_schedule_work_area": "mdi:land-fields" + "override_schedule": { + "service": "mdi:debug-step-over" + }, + "override_schedule_work_area": { + "service": "mdi:land-fields" + } } } diff --git a/homeassistant/components/husqvarna_automower/lawn_mower.py b/homeassistant/components/husqvarna_automower/lawn_mower.py index ac0f1fd6af25ad..eeabaa09f79839 100644 --- a/homeassistant/components/husqvarna_automower/lawn_mower.py +++ b/homeassistant/components/husqvarna_automower/lawn_mower.py @@ -26,7 +26,6 @@ MOWING_ACTIVITIES = ( MowerActivities.MOWING, MowerActivities.LEAVING, - MowerActivities.GOING_HOME, ) PAUSED_STATES = [ MowerStates.PAUSED, @@ -107,6 +106,8 @@ def activity(self) -> LawnMowerActivity: return LawnMowerActivity.PAUSED if mower_attributes.mower.activity in MOWING_ACTIVITIES: return LawnMowerActivity.MOWING + if mower_attributes.mower.activity == MowerActivities.GOING_HOME: + return LawnMowerActivity.RETURNING if (mower_attributes.mower.state == "RESTRICTED") or ( mower_attributes.mower.activity in DOCKED_ACTIVITIES ): diff --git a/homeassistant/components/hvv_departures/config_flow.py b/homeassistant/components/hvv_departures/config_flow.py index 0c909e2d8c1b89..3e1b98d9a3827c 100644 --- a/homeassistant/components/hvv_departures/config_flow.py +++ b/homeassistant/components/hvv_departures/config_flow.py @@ -9,7 +9,12 @@ from pygti.exceptions import CannotConnect, InvalidAuth import voluptuous as vol -from homeassistant.config_entries import ConfigEntry, ConfigFlow, OptionsFlow +from homeassistant.config_entries import ( + ConfigEntry, + ConfigFlow, + ConfigFlowResult, + OptionsFlow, +) from homeassistant.const import CONF_HOST, CONF_OFFSET, CONF_PASSWORD, CONF_USERNAME from homeassistant.core import callback from homeassistant.helpers import aiohttp_client @@ -44,13 +49,16 @@ class HVVDeparturesConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - def __init__(self): + hub: GTIHub + data: dict[str, Any] + + def __init__(self) -> None: """Initialize component.""" - self.hub = None - self.data = None - self.stations = {} + self.stations: dict[str, Any] = {} - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" errors = {} @@ -79,7 +87,9 @@ async def async_step_user(self, user_input=None): step_id="user", data_schema=SCHEMA_STEP_USER, errors=errors ) - async def async_step_station(self, user_input=None): + async def async_step_station( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the step where the user inputs his/her station.""" if user_input is not None: errors = {} @@ -109,7 +119,9 @@ async def async_step_station(self, user_input=None): return self.async_show_form(step_id="station", data_schema=SCHEMA_STEP_STATION) - async def async_step_station_select(self, user_input=None): + async def async_step_station_select( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the step where the user inputs his/her station.""" schema = vol.Schema({vol.Required(CONF_STATION): vol.In(list(self.stations))}) @@ -141,7 +153,9 @@ def __init__(self, config_entry: ConfigEntry) -> None: self.options = dict(config_entry.options) self.departure_filters: dict[str, Any] = {} - async def async_step_init(self, user_input=None): + async def async_step_init( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Manage the options.""" errors = {} if not self.departure_filters: @@ -170,7 +184,7 @@ async def async_step_init(self, user_input=None): if not errors: self.departure_filters = { str(i): departure_filter - for i, departure_filter in enumerate(departure_list.get("filter")) + for i, departure_filter in enumerate(departure_list["filter"]) } if user_input is not None and not errors: @@ -188,7 +202,7 @@ async def async_step_init(self, user_input=None): old_filter = [ i for (i, f) in self.departure_filters.items() - if f in self.config_entry.options.get(CONF_FILTER) + if f in self.config_entry.options[CONF_FILTER] ] else: old_filter = [] diff --git a/homeassistant/components/hydrawise/config_flow.py b/homeassistant/components/hydrawise/config_flow.py index ab9ebbb065d6c6..a5e7d616fcf1ae 100644 --- a/homeassistant/components/hydrawise/config_flow.py +++ b/homeassistant/components/hydrawise/config_flow.py @@ -90,7 +90,7 @@ def _show_form(self, error_type: str | None = None) -> ConfigFlowResult: ) async def async_step_reauth( - self, user_input: Mapping[str, Any] + self, entry_data: Mapping[str, Any] ) -> ConfigFlowResult: """Perform reauth after updating config to username/password.""" self.reauth_entry = self.hass.config_entries.async_get_entry( diff --git a/homeassistant/components/hydrawise/icons.json b/homeassistant/components/hydrawise/icons.json index 1d1d349dbf9776..5baf76454b79c0 100644 --- a/homeassistant/components/hydrawise/icons.json +++ b/homeassistant/components/hydrawise/icons.json @@ -31,8 +31,14 @@ } }, "services": { - "start_watering": "mdi:sprinkler-variant", - "suspend": "mdi:pause-circle-outline", - "resume": "mdi:play" + "start_watering": { + "service": "mdi:sprinkler-variant" + }, + "suspend": { + "service": "mdi:pause-circle-outline" + }, + "resume": { + "service": "mdi:play" + } } } diff --git a/homeassistant/components/ialarm/config_flow.py b/homeassistant/components/ialarm/config_flow.py index 08cb98683578cc..6df1b0f8290642 100644 --- a/homeassistant/components/ialarm/config_flow.py +++ b/homeassistant/components/ialarm/config_flow.py @@ -1,11 +1,12 @@ """Config flow for Antifurto365 iAlarm integration.""" import logging +from typing import Any from pyialarm import IAlarm import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_HOST, CONF_PORT from homeassistant.core import HomeAssistant @@ -31,7 +32,9 @@ class IAlarmConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" errors = {} mac = None diff --git a/homeassistant/components/icloud/config_flow.py b/homeassistant/components/icloud/config_flow.py index 30942ce6727b9a..efcef15b4d038a 100644 --- a/homeassistant/components/icloud/config_flow.py +++ b/homeassistant/components/icloud/config_flow.py @@ -5,7 +5,7 @@ from collections.abc import Mapping import logging import os -from typing import Any +from typing import TYPE_CHECKING, Any from pyicloud import PyiCloudService from pyicloud.exceptions import ( @@ -43,7 +43,7 @@ class IcloudFlowHandler(ConfigFlow, domain=DOMAIN): VERSION = 1 - def __init__(self): + def __init__(self) -> None: """Initialize iCloud config flow.""" self.api = None self._username = None @@ -55,8 +55,8 @@ def __init__(self): self._trusted_device = None self._verification_code = None - self._existing_entry_data = None - self._description_placeholders = None + self._existing_entry_data: dict[str, Any] | None = None + self._description_placeholders: dict[str, str] | None = None def _show_setup_form(self, user_input=None, errors=None, step_id="user"): """Show the setup form to the user.""" @@ -164,11 +164,13 @@ async def _validate_and_create_entry(self, user_input, step_id): await self.hass.config_entries.async_reload(entry.entry_id) return self.async_abort(reason="reauth_successful") - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle a flow initiated by the user.""" - errors = {} + errors: dict[str, str] = {} - icloud_dir = Store(self.hass, STORAGE_VERSION, STORAGE_KEY) + icloud_dir = Store[Any](self.hass, STORAGE_VERSION, STORAGE_KEY) if not os.path.exists(icloud_dir.path): await self.hass.async_add_executor_job(os.makedirs, icloud_dir.path) @@ -198,11 +200,17 @@ async def async_step_reauth_confirm( return await self._validate_and_create_entry(user_input, "reauth_confirm") - async def async_step_trusted_device(self, user_input=None, errors=None): + async def async_step_trusted_device( + self, + user_input: dict[str, Any] | None = None, + errors: dict[str, str] | None = None, + ) -> ConfigFlowResult: """We need a trusted device.""" if errors is None: errors = {} + if TYPE_CHECKING: + assert self.api is not None trusted_devices = await self.hass.async_add_executor_job( getattr, self.api, "trusted_devices" ) @@ -214,7 +222,7 @@ async def async_step_trusted_device(self, user_input=None, errors=None): if user_input is None: return await self._show_trusted_device_form( - trusted_devices_for_form, user_input, errors + trusted_devices_for_form, errors ) self._trusted_device = trusted_devices[int(user_input[CONF_TRUSTED_DEVICE])] @@ -227,18 +235,18 @@ async def async_step_trusted_device(self, user_input=None, errors=None): errors[CONF_TRUSTED_DEVICE] = "send_verification_code" return await self._show_trusted_device_form( - trusted_devices_for_form, user_input, errors + trusted_devices_for_form, errors ) return await self.async_step_verification_code() async def _show_trusted_device_form( - self, trusted_devices, user_input=None, errors=None - ): + self, trusted_devices, errors: dict[str, str] | None = None + ) -> ConfigFlowResult: """Show the trusted_device form to the user.""" return self.async_show_form( - step_id=CONF_TRUSTED_DEVICE, + step_id="trusted_device", data_schema=vol.Schema( { vol.Required(CONF_TRUSTED_DEVICE): vol.All( @@ -249,13 +257,20 @@ async def _show_trusted_device_form( errors=errors or {}, ) - async def async_step_verification_code(self, user_input=None, errors=None): + async def async_step_verification_code( + self, + user_input: dict[str, Any] | None = None, + errors: dict[str, str] | None = None, + ) -> ConfigFlowResult: """Ask the verification code to the user.""" if errors is None: errors = {} if user_input is None: - return await self._show_verification_code_form(user_input, errors) + return await self._show_verification_code_form(errors) + + if TYPE_CHECKING: + assert self.api is not None self._verification_code = user_input[CONF_VERIFICATION_CODE] @@ -308,11 +323,13 @@ async def async_step_verification_code(self, user_input=None, errors=None): } ) - async def _show_verification_code_form(self, user_input=None, errors=None): + async def _show_verification_code_form( + self, errors: dict[str, str] | None = None + ) -> ConfigFlowResult: """Show the verification_code form to the user.""" return self.async_show_form( - step_id=CONF_VERIFICATION_CODE, + step_id="verification_code", data_schema=vol.Schema({vol.Required(CONF_VERIFICATION_CODE): str}), - errors=errors or {}, + errors=errors, ) diff --git a/homeassistant/components/icloud/icons.json b/homeassistant/components/icloud/icons.json index 4ed856aabc1465..16280a063e3dab 100644 --- a/homeassistant/components/icloud/icons.json +++ b/homeassistant/components/icloud/icons.json @@ -1,8 +1,16 @@ { "services": { - "update": "mdi:update", - "play_sound": "mdi:speaker-wireless", - "display_message": "mdi:message-alert", - "lost_device": "mdi:devices" + "update": { + "service": "mdi:update" + }, + "play_sound": { + "service": "mdi:speaker-wireless" + }, + "display_message": { + "service": "mdi:message-alert" + }, + "lost_device": { + "service": "mdi:devices" + } } } diff --git a/homeassistant/components/ifttt/icons.json b/homeassistant/components/ifttt/icons.json index b943478a70bfab..a90d76f664aec9 100644 --- a/homeassistant/components/ifttt/icons.json +++ b/homeassistant/components/ifttt/icons.json @@ -1,6 +1,10 @@ { "services": { - "push_alarm_state": "mdi:security", - "trigger": "mdi:play" + "push_alarm_state": { + "service": "mdi:security" + }, + "trigger": { + "service": "mdi:play" + } } } diff --git a/homeassistant/components/ihc/icons.json b/homeassistant/components/ihc/icons.json index 73aab5f80d899c..3842d1a48a6d35 100644 --- a/homeassistant/components/ihc/icons.json +++ b/homeassistant/components/ihc/icons.json @@ -1,8 +1,16 @@ { "services": { - "set_runtime_value_bool": "mdi:toggle-switch", - "set_runtime_value_int": "mdi:numeric", - "set_runtime_value_float": "mdi:numeric", - "pulse": "mdi:pulse" + "set_runtime_value_bool": { + "service": "mdi:toggle-switch" + }, + "set_runtime_value_int": { + "service": "mdi:numeric" + }, + "set_runtime_value_float": { + "service": "mdi:numeric" + }, + "pulse": { + "service": "mdi:pulse" + } } } diff --git a/homeassistant/components/image_processing/icons.json b/homeassistant/components/image_processing/icons.json index b19d29c186dc43..ae95718e381977 100644 --- a/homeassistant/components/image_processing/icons.json +++ b/homeassistant/components/image_processing/icons.json @@ -1,5 +1,7 @@ { "services": { - "scan": "mdi:qrcode-scan" + "scan": { + "service": "mdi:qrcode-scan" + } } } diff --git a/homeassistant/components/imap/icons.json b/homeassistant/components/imap/icons.json index 6672f9a4a7fce3..17a11d0fe22d28 100644 --- a/homeassistant/components/imap/icons.json +++ b/homeassistant/components/imap/icons.json @@ -10,9 +10,17 @@ } }, "services": { - "seen": "mdi:email-open-outline", - "move": "mdi:email-arrow-right-outline", - "delete": "mdi:trash-can-outline", - "fetch": "mdi:email-sync-outline" + "seen": { + "service": "mdi:email-open-outline" + }, + "move": { + "service": "mdi:email-arrow-right-outline" + }, + "delete": { + "service": "mdi:trash-can-outline" + }, + "fetch": { + "service": "mdi:email-sync-outline" + } } } diff --git a/homeassistant/components/input_boolean/icons.json b/homeassistant/components/input_boolean/icons.json index dc595a60fba4d7..088c9094b3f556 100644 --- a/homeassistant/components/input_boolean/icons.json +++ b/homeassistant/components/input_boolean/icons.json @@ -8,9 +8,17 @@ } }, "services": { - "toggle": "mdi:toggle-switch", - "turn_off": "mdi:toggle-switch-off", - "turn_on": "mdi:toggle-switch", - "reload": "mdi:reload" + "toggle": { + "service": "mdi:toggle-switch" + }, + "turn_off": { + "service": "mdi:toggle-switch-off" + }, + "turn_on": { + "service": "mdi:toggle-switch" + }, + "reload": { + "service": "mdi:reload" + } } } diff --git a/homeassistant/components/input_button/icons.json b/homeassistant/components/input_button/icons.json index 226b8ede1101f8..20d41b4934a236 100644 --- a/homeassistant/components/input_button/icons.json +++ b/homeassistant/components/input_button/icons.json @@ -1,6 +1,10 @@ { "services": { - "press": "mdi:gesture-tap-button", - "reload": "mdi:reload" + "press": { + "service": "mdi:gesture-tap-button" + }, + "reload": { + "service": "mdi:reload" + } } } diff --git a/homeassistant/components/input_datetime/icons.json b/homeassistant/components/input_datetime/icons.json index de899023cf272f..f3676f022208da 100644 --- a/homeassistant/components/input_datetime/icons.json +++ b/homeassistant/components/input_datetime/icons.json @@ -1,6 +1,10 @@ { "services": { - "set_datetime": "mdi:calendar-clock", - "reload": "mdi:reload" + "set_datetime": { + "service": "mdi:calendar-clock" + }, + "reload": { + "service": "mdi:reload" + } } } diff --git a/homeassistant/components/input_number/icons.json b/homeassistant/components/input_number/icons.json index d1423838491251..9f90582308bb90 100644 --- a/homeassistant/components/input_number/icons.json +++ b/homeassistant/components/input_number/icons.json @@ -1,8 +1,16 @@ { "services": { - "decrement": "mdi:minus", - "increment": "mdi:plus", - "set_value": "mdi:numeric", - "reload": "mdi:reload" + "decrement": { + "service": "mdi:minus" + }, + "increment": { + "service": "mdi:plus" + }, + "set_value": { + "service": "mdi:numeric" + }, + "reload": { + "service": "mdi:reload" + } } } diff --git a/homeassistant/components/input_select/icons.json b/homeassistant/components/input_select/icons.json index 03b477ddb36e73..6ef5cfaf96a8c2 100644 --- a/homeassistant/components/input_select/icons.json +++ b/homeassistant/components/input_select/icons.json @@ -1,11 +1,25 @@ { "services": { - "select_next": "mdi:skip-next", - "select_option": "mdi:check", - "select_previous": "mdi:skip-previous", - "select_first": "mdi:skip-backward", - "select_last": "mdi:skip-forward", - "set_options": "mdi:cog", - "reload": "mdi:reload" + "select_next": { + "service": "mdi:skip-next" + }, + "select_option": { + "service": "mdi:check" + }, + "select_previous": { + "service": "mdi:skip-previous" + }, + "select_first": { + "service": "mdi:skip-backward" + }, + "select_last": { + "service": "mdi:skip-forward" + }, + "set_options": { + "service": "mdi:cog" + }, + "reload": { + "service": "mdi:reload" + } } } diff --git a/homeassistant/components/input_text/icons.json b/homeassistant/components/input_text/icons.json index 0190e4ffba254b..8fca66668bc4bb 100644 --- a/homeassistant/components/input_text/icons.json +++ b/homeassistant/components/input_text/icons.json @@ -1,6 +1,10 @@ { "services": { - "set_value": "mdi:form-textbox", - "reload": "mdi:reload" + "set_value": { + "service": "mdi:form-textbox" + }, + "reload": { + "service": "mdi:reload" + } } } diff --git a/homeassistant/components/insteon/api/config.py b/homeassistant/components/insteon/api/config.py index 8a617911d1ec06..88c062c3271a40 100644 --- a/homeassistant/components/insteon/api/config.py +++ b/homeassistant/components/insteon/api/config.py @@ -211,7 +211,7 @@ async def websocket_update_modem_config( """Get the schema for the modem configuration.""" config = msg["config"] config_entry = get_insteon_config_entry(hass) - is_connected = devices.modem.connected + is_connected = devices.modem is not None and devices.modem.connected if not await _async_connect(**config): connection.send_error( diff --git a/homeassistant/components/insteon/config_flow.py b/homeassistant/components/insteon/config_flow.py index baf06b138608a0..6b048004ba17e0 100644 --- a/homeassistant/components/insteon/config_flow.py +++ b/homeassistant/components/insteon/config_flow.py @@ -3,6 +3,7 @@ from __future__ import annotations import logging +from typing import Any from pyinsteon import async_connect @@ -54,14 +55,18 @@ class InsteonFlowHandler(ConfigFlow, domain=DOMAIN): _device_name: str | None = None discovered_conf: dict[str, str] = {} - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Init the config flow.""" if self._async_current_entries(): return self.async_abort(reason="single_instance_allowed") modem_types = [STEP_PLM, STEP_HUB_V1, STEP_HUB_V2] return self.async_show_menu(step_id="user", menu_options=modem_types) - async def async_step_plm(self, user_input=None): + async def async_step_plm( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Set up the PLM modem type.""" errors = {} if user_input is not None: @@ -80,7 +85,9 @@ async def async_step_plm(self, user_input=None): step_id=STEP_PLM, data_schema=data_schema, errors=errors ) - async def async_step_plm_manually(self, user_input=None): + async def async_step_plm_manually( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Set up the PLM modem type manually.""" errors = {} schema_defaults = {} @@ -94,15 +101,21 @@ async def async_step_plm_manually(self, user_input=None): step_id=STEP_PLM_MANUALLY, data_schema=data_schema, errors=errors ) - async def async_step_hubv1(self, user_input=None): + async def async_step_hubv1( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Set up the Hub v1 modem type.""" return await self._async_setup_hub(hub_version=1, user_input=user_input) - async def async_step_hubv2(self, user_input=None): + async def async_step_hubv2( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Set up the Hub v2 modem type.""" return await self._async_setup_hub(hub_version=2, user_input=user_input) - async def _async_setup_hub(self, hub_version, user_input): + async def _async_setup_hub( + self, hub_version: int, user_input: dict[str, Any] | None + ) -> ConfigFlowResult: """Set up the Hub versions 1 and 2.""" errors = {} if user_input is not None: @@ -141,7 +154,9 @@ async def async_step_usb( await self.async_set_unique_id(DEFAULT_DISCOVERY_UNIQUE_ID) return await self.async_step_confirm_usb() - async def async_step_confirm_usb(self, user_input=None) -> ConfigFlowResult: + async def async_step_confirm_usb( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Confirm a USB discovery.""" if user_input is not None: return await self.async_step_plm({CONF_DEVICE: self._device_path}) diff --git a/homeassistant/components/insteon/icons.json b/homeassistant/components/insteon/icons.json index 4d015e13b0dc43..530006ca7d466e 100644 --- a/homeassistant/components/insteon/icons.json +++ b/homeassistant/components/insteon/icons.json @@ -1,15 +1,37 @@ { "services": { - "add_all_link": "mdi:link-variant", - "delete_all_link": "mdi:link-variant-remove", - "load_all_link_database": "mdi:database", - "print_all_link_database": "mdi:database-export", - "print_im_all_link_database": "mdi:database-export", - "x10_all_units_off": "mdi:power-off", - "x10_all_lights_on": "mdi:lightbulb-on", - "x10_all_lights_off": "mdi:lightbulb-off", - "scene_on": "mdi:palette", - "scene_off": "mdi:palette-outline", - "add_default_links": "mdi:link-variant-plus" + "add_all_link": { + "service": "mdi:link-variant" + }, + "delete_all_link": { + "service": "mdi:link-variant-remove" + }, + "load_all_link_database": { + "service": "mdi:database" + }, + "print_all_link_database": { + "service": "mdi:database-export" + }, + "print_im_all_link_database": { + "service": "mdi:database-export" + }, + "x10_all_units_off": { + "service": "mdi:power-off" + }, + "x10_all_lights_on": { + "service": "mdi:lightbulb-on" + }, + "x10_all_lights_off": { + "service": "mdi:lightbulb-off" + }, + "scene_on": { + "service": "mdi:palette" + }, + "scene_off": { + "service": "mdi:palette-outline" + }, + "add_default_links": { + "service": "mdi:link-variant-plus" + } } } diff --git a/homeassistant/components/intellifire/__init__.py b/homeassistant/components/intellifire/__init__.py index 7af472c8745284..7609398673b38f 100644 --- a/homeassistant/components/intellifire/__init__.py +++ b/homeassistant/components/intellifire/__init__.py @@ -2,15 +2,17 @@ from __future__ import annotations -from aiohttp import ClientConnectionError -from intellifire4py import IntellifireControlAsync -from intellifire4py.exceptions import LoginException -from intellifire4py.intellifire import IntellifireAPICloud, IntellifireAPILocal +import asyncio + +from intellifire4py import UnifiedFireplace +from intellifire4py.cloud_interface import IntelliFireCloudInterface +from intellifire4py.model import IntelliFireCommonFireplaceData from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( CONF_API_KEY, CONF_HOST, + CONF_IP_ADDRESS, CONF_PASSWORD, CONF_USERNAME, Platform, @@ -18,7 +20,18 @@ from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady -from .const import CONF_USER_ID, DOMAIN, LOGGER +from .const import ( + CONF_AUTH_COOKIE, + CONF_CONTROL_MODE, + CONF_READ_MODE, + CONF_SERIAL, + CONF_USER_ID, + CONF_WEB_CLIENT_ID, + DOMAIN, + INIT_WAIT_TIME_SECONDS, + LOGGER, + STARTUP_TIMEOUT, +) from .coordinator import IntellifireDataUpdateCoordinator PLATFORMS = [ @@ -32,79 +45,114 @@ ] +def _construct_common_data(entry: ConfigEntry) -> IntelliFireCommonFireplaceData: + """Convert config entry data into IntelliFireCommonFireplaceData.""" + + return IntelliFireCommonFireplaceData( + auth_cookie=entry.data[CONF_AUTH_COOKIE], + user_id=entry.data[CONF_USER_ID], + web_client_id=entry.data[CONF_WEB_CLIENT_ID], + serial=entry.data[CONF_SERIAL], + api_key=entry.data[CONF_API_KEY], + ip_address=entry.data[CONF_IP_ADDRESS], + read_mode=entry.options[CONF_READ_MODE], + control_mode=entry.options[CONF_CONTROL_MODE], + ) + + +async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: + """Migrate entries.""" + LOGGER.debug( + "Migrating configuration from version %s.%s", + config_entry.version, + config_entry.minor_version, + ) + + if config_entry.version == 1: + new = {**config_entry.data} + + if config_entry.minor_version < 2: + username = config_entry.data[CONF_USERNAME] + password = config_entry.data[CONF_PASSWORD] + + # Create a Cloud Interface + async with IntelliFireCloudInterface() as cloud_interface: + await cloud_interface.login_with_credentials( + username=username, password=password + ) + + new_data = cloud_interface.user_data.get_data_for_ip(new[CONF_HOST]) + + if not new_data: + raise ConfigEntryAuthFailed + new[CONF_API_KEY] = new_data.api_key + new[CONF_WEB_CLIENT_ID] = new_data.web_client_id + new[CONF_AUTH_COOKIE] = new_data.auth_cookie + + new[CONF_IP_ADDRESS] = new_data.ip_address + new[CONF_SERIAL] = new_data.serial + + hass.config_entries.async_update_entry( + config_entry, + data=new, + options={CONF_READ_MODE: "local", CONF_CONTROL_MODE: "local"}, + unique_id=new[CONF_SERIAL], + version=1, + minor_version=2, + ) + LOGGER.debug("Pseudo Migration %s successful", config_entry.version) + + return True + + async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up IntelliFire from a config entry.""" - LOGGER.debug("Setting up config entry: %s", entry.unique_id) if CONF_USERNAME not in entry.data: - LOGGER.debug("Old config entry format detected: %s", entry.unique_id) + LOGGER.debug("Config entry without username detected: %s", entry.unique_id) raise ConfigEntryAuthFailed - ift_control = IntellifireControlAsync( - fireplace_ip=entry.data[CONF_HOST], - ) try: - await ift_control.login( - username=entry.data[CONF_USERNAME], - password=entry.data[CONF_PASSWORD], - ) - except (ConnectionError, ClientConnectionError) as err: - raise ConfigEntryNotReady from err - except LoginException as err: - raise ConfigEntryAuthFailed(err) from err - - finally: - await ift_control.close() - - # Extract API Key and User_ID from ift_control - # Eventually this will migrate to using IntellifireAPICloud - - if CONF_USER_ID not in entry.data or CONF_API_KEY not in entry.data: - LOGGER.info( - "Updating intellifire config entry for %s with api information", - entry.unique_id, + fireplace: UnifiedFireplace = ( + await UnifiedFireplace.build_fireplace_from_common( + _construct_common_data(entry) + ) ) - cloud_api = IntellifireAPICloud() - await cloud_api.login( - username=entry.data[CONF_USERNAME], - password=entry.data[CONF_PASSWORD], + LOGGER.debug("Waiting for Fireplace to Initialize") + await asyncio.wait_for( + _async_wait_for_initialization(fireplace), timeout=STARTUP_TIMEOUT ) - api_key = cloud_api.get_fireplace_api_key() - user_id = cloud_api.get_user_id() - # Update data entry - hass.config_entries.async_update_entry( - entry, - data={ - **entry.data, - CONF_API_KEY: api_key, - CONF_USER_ID: user_id, - }, - ) - - else: - api_key = entry.data[CONF_API_KEY] - user_id = entry.data[CONF_USER_ID] - - # Instantiate local control - api = IntellifireAPILocal( - fireplace_ip=entry.data[CONF_HOST], - api_key=api_key, - user_id=user_id, + except TimeoutError as err: + raise ConfigEntryNotReady( + "Initialization of fireplace timed out after 10 minutes" + ) from err + + # Construct coordinator + data_update_coordinator = IntellifireDataUpdateCoordinator( + hass=hass, fireplace=fireplace ) - # Define the update coordinator - coordinator = IntellifireDataUpdateCoordinator( - hass=hass, - api=api, - ) + LOGGER.debug("Fireplace to Initialized - Awaiting first refresh") + await data_update_coordinator.async_config_entry_first_refresh() + + hass.data.setdefault(DOMAIN, {})[entry.entry_id] = data_update_coordinator - await coordinator.async_config_entry_first_refresh() - hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) return True +async def _async_wait_for_initialization( + fireplace: UnifiedFireplace, timeout=STARTUP_TIMEOUT +): + """Wait for a fireplace to be initialized.""" + while ( + fireplace.data.ipv4_address == "127.0.0.1" and fireplace.data.serial == "unset" + ): + LOGGER.debug(f"Waiting for fireplace to initialize [{fireplace.read_mode}]") + await asyncio.sleep(INIT_WAIT_TIME_SECONDS) + + async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Unload a config entry.""" if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): diff --git a/homeassistant/components/intellifire/binary_sensor.py b/homeassistant/components/intellifire/binary_sensor.py index a1b8865c876113..f0a5d84fa62dca 100644 --- a/homeassistant/components/intellifire/binary_sensor.py +++ b/homeassistant/components/intellifire/binary_sensor.py @@ -5,7 +5,7 @@ from collections.abc import Callable from dataclasses import dataclass -from intellifire4py import IntellifirePollData +from intellifire4py.model import IntelliFirePollData from homeassistant.components.binary_sensor import ( BinarySensorDeviceClass, @@ -26,7 +26,7 @@ class IntellifireBinarySensorRequiredKeysMixin: """Mixin for required keys.""" - value_fn: Callable[[IntellifirePollData], bool] + value_fn: Callable[[IntelliFirePollData], bool] @dataclass(frozen=True) diff --git a/homeassistant/components/intellifire/climate.py b/homeassistant/components/intellifire/climate.py index ed4facffc67407..4eddde5ff10be0 100644 --- a/homeassistant/components/intellifire/climate.py +++ b/homeassistant/components/intellifire/climate.py @@ -69,7 +69,7 @@ def __init__( super().__init__(coordinator, description) if coordinator.data.thermostat_on: - self.last_temp = coordinator.data.thermostat_setpoint_c + self.last_temp = int(coordinator.data.thermostat_setpoint_c) @property def hvac_mode(self) -> HVACMode: diff --git a/homeassistant/components/intellifire/config_flow.py b/homeassistant/components/intellifire/config_flow.py index 268fc6623d3543..56f0d5ca6a5857 100644 --- a/homeassistant/components/intellifire/config_flow.py +++ b/homeassistant/components/intellifire/config_flow.py @@ -7,16 +7,33 @@ from typing import Any from aiohttp import ClientConnectionError -from intellifire4py import AsyncUDPFireplaceFinder -from intellifire4py.exceptions import LoginException -from intellifire4py.intellifire import IntellifireAPICloud, IntellifireAPILocal +from intellifire4py.cloud_interface import IntelliFireCloudInterface +from intellifire4py.exceptions import LoginError +from intellifire4py.local_api import IntelliFireAPILocal +from intellifire4py.model import IntelliFireCommonFireplaceData import voluptuous as vol from homeassistant.components.dhcp import DhcpServiceInfo -from homeassistant.config_entries import ConfigFlow, ConfigFlowResult -from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_PASSWORD, CONF_USERNAME - -from .const import CONF_USER_ID, DOMAIN, LOGGER +from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResult +from homeassistant.const import ( + CONF_API_KEY, + CONF_HOST, + CONF_IP_ADDRESS, + CONF_PASSWORD, + CONF_USERNAME, +) + +from .const import ( + API_MODE_LOCAL, + CONF_AUTH_COOKIE, + CONF_CONTROL_MODE, + CONF_READ_MODE, + CONF_SERIAL, + CONF_USER_ID, + CONF_WEB_CLIENT_ID, + DOMAIN, + LOGGER, +) STEP_USER_DATA_SCHEMA = vol.Schema({vol.Required(CONF_HOST): str}) @@ -31,17 +48,20 @@ class DiscoveredHostInfo: serial: str | None -async def validate_host_input(host: str, dhcp_mode: bool = False) -> str: +async def _async_poll_local_fireplace_for_serial( + host: str, dhcp_mode: bool = False +) -> str: """Validate the user input allows us to connect. Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user. """ LOGGER.debug("Instantiating IntellifireAPI with host: [%s]", host) - api = IntellifireAPILocal(fireplace_ip=host) + api = IntelliFireAPILocal(fireplace_ip=host) await api.poll(suppress_warnings=dhcp_mode) serial = api.data.serial LOGGER.debug("Found a fireplace: %s", serial) + # Return the serial number which will be used to calculate a unique ID for the device/sensors return serial @@ -50,239 +70,206 @@ class IntelliFireConfigFlow(ConfigFlow, domain=DOMAIN): """Handle a config flow for IntelliFire.""" VERSION = 1 + MINOR_VERSION = 2 def __init__(self) -> None: """Initialize the Config Flow Handler.""" - self._host: str = "" - self._serial: str = "" - self._not_configured_hosts: list[DiscoveredHostInfo] = [] + + # DHCP Variables + self._dhcp_discovered_serial: str = "" # used only in discovery mode self._discovered_host: DiscoveredHostInfo - self._reauth_needed: DiscoveredHostInfo + self._dhcp_mode = False + self._is_reauth = False - async def _find_fireplaces(self): - """Perform UDP discovery.""" - fireplace_finder = AsyncUDPFireplaceFinder() - discovered_hosts = await fireplace_finder.search_fireplace(timeout=12) - configured_hosts = { - entry.data[CONF_HOST] - for entry in self._async_current_entries(include_ignore=False) - if CONF_HOST in entry.data # CONF_HOST will be missing for ignored entries - } + self._not_configured_hosts: list[DiscoveredHostInfo] = [] + self._reauth_needed: DiscoveredHostInfo - self._not_configured_hosts = [ - DiscoveredHostInfo(ip, None) - for ip in discovered_hosts - if ip not in configured_hosts - ] - LOGGER.debug("Discovered Hosts: %s", discovered_hosts) - LOGGER.debug("Configured Hosts: %s", configured_hosts) - LOGGER.debug("Not Configured Hosts: %s", self._not_configured_hosts) + self._configured_serials: list[str] = [] - async def validate_api_access_and_create_or_update( - self, *, host: str, username: str, password: str, serial: str - ): - """Validate username/password against api.""" - LOGGER.debug("Attempting login to iftapi with: %s", username) + # Define a cloud api interface we can use + self.cloud_api_interface = IntelliFireCloudInterface() - ift_cloud = IntellifireAPICloud() - await ift_cloud.login(username=username, password=password) - api_key = ift_cloud.get_fireplace_api_key() - user_id = ift_cloud.get_user_id() + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Start the user flow.""" - data = { - CONF_HOST: host, - CONF_PASSWORD: password, - CONF_USERNAME: username, - CONF_API_KEY: api_key, - CONF_USER_ID: user_id, - } + current_entries = self._async_current_entries(include_ignore=False) + self._configured_serials = [ + entry.data[CONF_SERIAL] for entry in current_entries + ] - # Update or Create - existing_entry = await self.async_set_unique_id(serial) - if existing_entry: - self.hass.config_entries.async_update_entry(existing_entry, data=data) - await self.hass.config_entries.async_reload(existing_entry.entry_id) - return self.async_abort(reason="reauth_successful") - return self.async_create_entry(title=f"Fireplace {serial}", data=data) + return await self.async_step_cloud_api() - async def async_step_api_config( + async def async_step_cloud_api( self, user_input: dict[str, Any] | None = None ) -> ConfigFlowResult: - """Configure API access.""" - - errors = {} - control_schema = vol.Schema( - { - vol.Required(CONF_USERNAME): str, - vol.Required(CONF_PASSWORD): str, - } - ) + """Authenticate against IFTAPI Cloud in order to see configured devices. - if user_input is not None: - control_schema = vol.Schema( - { - vol.Required( - CONF_USERNAME, default=user_input.get(CONF_USERNAME, "") - ): str, - vol.Required( - CONF_PASSWORD, default=user_input.get(CONF_PASSWORD, "") - ): str, - } - ) - - try: - return await self.validate_api_access_and_create_or_update( - host=self._host, - username=user_input[CONF_USERNAME], - password=user_input[CONF_PASSWORD], - serial=self._serial, - ) - - except (ConnectionError, ClientConnectionError): - errors["base"] = "iftapi_connect" - LOGGER.error( - "Could not connect to iftapi.net over https - verify connectivity" - ) - except LoginException: - errors["base"] = "api_error" - LOGGER.error("Invalid credentials for iftapi.net") + Local control of IntelliFire devices requires that the user download the correct API KEY which is only available on the cloud. Cloud control of the devices requires the user has at least once authenticated against the cloud and a set of cookie variables have been stored locally. - return self.async_show_form( - step_id="api_config", errors=errors, data_schema=control_schema - ) + """ + errors: dict[str, str] = {} + LOGGER.debug("STEP: cloud_api") - async def _async_validate_ip_and_continue(self, host: str) -> ConfigFlowResult: - """Validate local config and continue.""" - self._async_abort_entries_match({CONF_HOST: host}) - self._serial = await validate_host_input(host) - await self.async_set_unique_id(self._serial, raise_on_progress=False) - self._abort_if_unique_id_configured(updates={CONF_HOST: host}) - # Store current data and jump to next stage - self._host = host - - return await self.async_step_api_config() - - async def async_step_manual_device_entry(self, user_input=None): - """Handle manual input of local IP configuration.""" - LOGGER.debug("STEP: manual_device_entry") - errors = {} - self._host = user_input.get(CONF_HOST) if user_input else None if user_input is not None: try: - return await self._async_validate_ip_and_continue(self._host) - except (ConnectionError, ClientConnectionError): - errors["base"] = "cannot_connect" + async with self.cloud_api_interface as cloud_interface: + await cloud_interface.login_with_credentials( + username=user_input[CONF_USERNAME], + password=user_input[CONF_PASSWORD], + ) + + # If login was successful pass username/password to next step + return await self.async_step_pick_cloud_device() + except LoginError: + errors["base"] = "api_error" return self.async_show_form( - step_id="manual_device_entry", + step_id="cloud_api", errors=errors, - data_schema=vol.Schema({vol.Required(CONF_HOST, default=self._host): str}), + data_schema=vol.Schema( + { + vol.Required(CONF_USERNAME): str, + vol.Required(CONF_PASSWORD): str, + } + ), ) - async def async_step_pick_device( + async def async_step_pick_cloud_device( self, user_input: dict[str, Any] | None = None ) -> ConfigFlowResult: - """Pick which device to configure.""" - errors = {} - LOGGER.debug("STEP: pick_device") + """Step to select a device from the cloud. + + We can only get here if we have logged in. If there is only one device available it will be auto-configured, + else the user will be given a choice to pick a device. + """ + errors: dict[str, str] = {} + LOGGER.debug( + f"STEP: pick_cloud_device: {user_input} - DHCP_MODE[{self._dhcp_mode}" + ) - if user_input is not None: - if user_input[CONF_HOST] == MANUAL_ENTRY_STRING: - return await self.async_step_manual_device_entry() + if self._dhcp_mode or user_input is not None: + if self._dhcp_mode: + serial = self._dhcp_discovered_serial + LOGGER.debug(f"DHCP Mode detected for serial [{serial}]") + if user_input is not None: + serial = user_input[CONF_SERIAL] + + # Run a unique ID Check prior to anything else + await self.async_set_unique_id(serial) + self._abort_if_unique_id_configured(updates={CONF_SERIAL: serial}) + + # If Serial is Good obtain fireplace and configure + fireplace = self.cloud_api_interface.user_data.get_data_for_serial(serial) + if fireplace: + return await self._async_create_config_entry_from_common_data( + fireplace=fireplace + ) - try: - return await self._async_validate_ip_and_continue(user_input[CONF_HOST]) - except (ConnectionError, ClientConnectionError): - errors["base"] = "cannot_connect" + # Parse User Data to see if we auto-configure or prompt for selection: + user_data = self.cloud_api_interface.user_data + + available_fireplaces: list[IntelliFireCommonFireplaceData] = [ + fp + for fp in user_data.fireplaces + if fp.serial not in self._configured_serials + ] + + # Abort if all devices have been configured + if not available_fireplaces: + return self.async_abort(reason="no_available_devices") + + # If there is a single fireplace configure it + if len(available_fireplaces) == 1: + if self._is_reauth: + reauth_entry = self.hass.config_entries.async_get_entry( + self.context["entry_id"] + ) + return await self._async_create_config_entry_from_common_data( + fireplace=available_fireplaces[0], existing_entry=reauth_entry + ) + + return await self._async_create_config_entry_from_common_data( + fireplace=available_fireplaces[0] + ) return self.async_show_form( - step_id="pick_device", + step_id="pick_cloud_device", errors=errors, data_schema=vol.Schema( { - vol.Required(CONF_HOST): vol.In( - [host.ip for host in self._not_configured_hosts] - + [MANUAL_ENTRY_STRING] + vol.Required(CONF_SERIAL): vol.In( + [fp.serial for fp in available_fireplaces] ) } ), ) - async def async_step_user( - self, user_input: dict[str, Any] | None = None + async def _async_create_config_entry_from_common_data( + self, + fireplace: IntelliFireCommonFireplaceData, + existing_entry: ConfigEntry | None = None, ) -> ConfigFlowResult: - """Start the user flow.""" + """Construct a config entry based on an object of IntelliFireCommonFireplaceData.""" + + data = { + CONF_IP_ADDRESS: fireplace.ip_address, + CONF_API_KEY: fireplace.api_key, + CONF_SERIAL: fireplace.serial, + CONF_AUTH_COOKIE: fireplace.auth_cookie, + CONF_WEB_CLIENT_ID: fireplace.web_client_id, + CONF_USER_ID: fireplace.user_id, + CONF_USERNAME: self.cloud_api_interface.user_data.username, + CONF_PASSWORD: self.cloud_api_interface.user_data.password, + } - # Launch fireplaces discovery - await self._find_fireplaces() - LOGGER.debug("STEP: user") - if self._not_configured_hosts: - LOGGER.debug("Running Step: pick_device") - return await self.async_step_pick_device() - LOGGER.debug("Running Step: manual_device_entry") - return await self.async_step_manual_device_entry() + options = {CONF_READ_MODE: API_MODE_LOCAL, CONF_CONTROL_MODE: API_MODE_LOCAL} + + if existing_entry: + return self.async_update_reload_and_abort( + existing_entry, data=data, options=options + ) + return self.async_create_entry( + title=f"Fireplace {fireplace.serial}", data=data, options=options + ) async def async_step_reauth( self, entry_data: Mapping[str, Any] ) -> ConfigFlowResult: """Perform reauth upon an API authentication error.""" LOGGER.debug("STEP: reauth") + self._is_reauth = True entry = self.hass.config_entries.async_get_entry(self.context["entry_id"]) - assert entry - assert entry.unique_id # populate the expected vars - self._serial = entry.unique_id - self._host = entry.data[CONF_HOST] + self._dhcp_discovered_serial = entry.data[CONF_SERIAL] # type: ignore[union-attr] - placeholders = {CONF_HOST: self._host, "serial": self._serial} + placeholders = {"serial": self._dhcp_discovered_serial} self.context["title_placeholders"] = placeholders - return await self.async_step_api_config() + + return await self.async_step_cloud_api() async def async_step_dhcp( self, discovery_info: DhcpServiceInfo ) -> ConfigFlowResult: """Handle DHCP Discovery.""" + self._dhcp_mode = True # Run validation logic on ip - host = discovery_info.ip - LOGGER.debug("STEP: dhcp for host %s", host) + ip_address = discovery_info.ip + LOGGER.debug("STEP: dhcp for ip_address %s", ip_address) - self._async_abort_entries_match({CONF_HOST: host}) + self._async_abort_entries_match({CONF_IP_ADDRESS: ip_address}) try: - self._serial = await validate_host_input(host, dhcp_mode=True) + self._dhcp_discovered_serial = await _async_poll_local_fireplace_for_serial( + ip_address, dhcp_mode=True + ) except (ConnectionError, ClientConnectionError): LOGGER.debug( - "DHCP Discovery has determined %s is not an IntelliFire device", host + "DHCP Discovery has determined %s is not an IntelliFire device", + ip_address, ) return self.async_abort(reason="not_intellifire_device") - await self.async_set_unique_id(self._serial) - self._abort_if_unique_id_configured(updates={CONF_HOST: host}) - self._discovered_host = DiscoveredHostInfo(ip=host, serial=self._serial) - - placeholders = {CONF_HOST: host, "serial": self._serial} - self.context["title_placeholders"] = placeholders - self._set_confirm_only() - - return await self.async_step_dhcp_confirm() - - async def async_step_dhcp_confirm(self, user_input=None): - """Attempt to confirm.""" - - LOGGER.debug("STEP: dhcp_confirm") - # Add the hosts one by one - host = self._discovered_host.ip - serial = self._discovered_host.serial - - if user_input is None: - # Show the confirmation dialog - return self.async_show_form( - step_id="dhcp_confirm", - description_placeholders={CONF_HOST: host, "serial": serial}, - ) - - return self.async_create_entry( - title=f"Fireplace {serial}", - data={CONF_HOST: host}, - ) + return await self.async_step_cloud_api() diff --git a/homeassistant/components/intellifire/const.py b/homeassistant/components/intellifire/const.py index 5c8af1eefe9e9f..f194eeaf4e2d7d 100644 --- a/homeassistant/components/intellifire/const.py +++ b/homeassistant/components/intellifire/const.py @@ -5,11 +5,22 @@ import logging DOMAIN = "intellifire" - -CONF_USER_ID = "user_id" - LOGGER = logging.getLogger(__package__) +DEFAULT_THERMOSTAT_TEMP = 21 + +CONF_USER_ID = "user_id" # part of the cloud cookie +CONF_WEB_CLIENT_ID = "web_client_id" # part of the cloud cookie +CONF_AUTH_COOKIE = "auth_cookie" # part of the cloud cookie CONF_SERIAL = "serial" +CONF_READ_MODE = "cloud_read" +CONF_CONTROL_MODE = "cloud_control" -DEFAULT_THERMOSTAT_TEMP = 21 + +API_MODE_LOCAL = "local" +API_MODE_CLOUD = "cloud" + + +STARTUP_TIMEOUT = 600 + +INIT_WAIT_TIME_SECONDS = 10 diff --git a/homeassistant/components/intellifire/coordinator.py b/homeassistant/components/intellifire/coordinator.py index 0a46ff61435768..b4f03f4b5c87d9 100644 --- a/homeassistant/components/intellifire/coordinator.py +++ b/homeassistant/components/intellifire/coordinator.py @@ -2,27 +2,27 @@ from __future__ import annotations -import asyncio from datetime import timedelta -from aiohttp import ClientConnectionError -from intellifire4py import IntellifirePollData -from intellifire4py.intellifire import IntellifireAPILocal +from intellifire4py import UnifiedFireplace +from intellifire4py.control import IntelliFireController +from intellifire4py.model import IntelliFirePollData +from intellifire4py.read import IntelliFireDataProvider from homeassistant.core import HomeAssistant from homeassistant.helpers.device_registry import DeviceInfo -from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed +from homeassistant.helpers.update_coordinator import DataUpdateCoordinator from .const import DOMAIN, LOGGER -class IntellifireDataUpdateCoordinator(DataUpdateCoordinator[IntellifirePollData]): +class IntellifireDataUpdateCoordinator(DataUpdateCoordinator[IntelliFirePollData]): """Class to manage the polling of the fireplace API.""" def __init__( self, hass: HomeAssistant, - api: IntellifireAPILocal, + fireplace: UnifiedFireplace, ) -> None: """Initialize the Coordinator.""" super().__init__( @@ -31,36 +31,21 @@ def __init__( name=DOMAIN, update_interval=timedelta(seconds=15), ) - self._api = api - async def _async_update_data(self) -> IntellifirePollData: - if not self._api.is_polling_in_background: - LOGGER.info("Starting Intellifire Background Polling Loop") - await self._api.start_background_polling() - - # Don't return uninitialized poll data - async with asyncio.timeout(15): - try: - await self._api.poll() - except (ConnectionError, ClientConnectionError) as exception: - raise UpdateFailed from exception - - LOGGER.debug("Failure Count %d", self._api.failed_poll_attempts) - if self._api.failed_poll_attempts > 10: - LOGGER.debug("Too many polling errors - raising exception") - raise UpdateFailed - - return self._api.data + self.fireplace = fireplace @property - def read_api(self) -> IntellifireAPILocal: + def read_api(self) -> IntelliFireDataProvider: """Return the Status API pointer.""" - return self._api + return self.fireplace.read_api @property - def control_api(self) -> IntellifireAPILocal: + def control_api(self) -> IntelliFireController: """Return the control API.""" - return self._api + return self.fireplace.control_api + + async def _async_update_data(self) -> IntelliFirePollData: + return self.fireplace.data @property def device_info(self) -> DeviceInfo: @@ -69,7 +54,6 @@ def device_info(self) -> DeviceInfo: manufacturer="Hearth and Home", model="IFT-WFM", name="IntelliFire", - identifiers={("IntelliFire", f"{self.read_api.data.serial}]")}, - sw_version=self.read_api.data.fw_ver_str, - configuration_url=f"http://{self._api.fireplace_ip}/poll", + identifiers={("IntelliFire", str(self.fireplace.serial))}, + configuration_url=f"http://{self.fireplace.ip_address}/poll", ) diff --git a/homeassistant/components/intellifire/entity.py b/homeassistant/components/intellifire/entity.py index 3b35c9dabd836d..571c4717ac2df8 100644 --- a/homeassistant/components/intellifire/entity.py +++ b/homeassistant/components/intellifire/entity.py @@ -9,7 +9,7 @@ class IntellifireEntity(CoordinatorEntity[IntellifireDataUpdateCoordinator]): - """Define a generic class for Intellifire entities.""" + """Define a generic class for IntelliFire entities.""" _attr_attribution = "Data provided by unpublished Intellifire API" _attr_has_entity_name = True @@ -22,6 +22,8 @@ def __init__( """Class initializer.""" super().__init__(coordinator=coordinator) self.entity_description = description - self._attr_unique_id = f"{description.key}_{coordinator.read_api.data.serial}" + self._attr_unique_id = f"{description.key}_{coordinator.fireplace.serial}" + self.identifiers = ({("IntelliFire", f"{coordinator.fireplace.serial}]")},) + # Configure the Device Info self._attr_device_info = self.coordinator.device_info diff --git a/homeassistant/components/intellifire/fan.py b/homeassistant/components/intellifire/fan.py index f68827b0a56882..dc2fc279a5db0d 100644 --- a/homeassistant/components/intellifire/fan.py +++ b/homeassistant/components/intellifire/fan.py @@ -7,7 +7,8 @@ import math from typing import Any -from intellifire4py import IntellifireControlAsync, IntellifirePollData +from intellifire4py.control import IntelliFireController +from intellifire4py.model import IntelliFirePollData from homeassistant.components.fan import ( FanEntity, @@ -31,8 +32,8 @@ class IntellifireFanRequiredKeysMixin: """Required keys for fan entity.""" - set_fn: Callable[[IntellifireControlAsync, int], Awaitable] - value_fn: Callable[[IntellifirePollData], bool] + set_fn: Callable[[IntelliFireController, int], Awaitable] + value_fn: Callable[[IntelliFirePollData], int] speed_range: tuple[int, int] @@ -91,7 +92,8 @@ def is_on(self) -> bool: def percentage(self) -> int | None: """Return fan percentage.""" return ranged_value_to_percentage( - self.entity_description.speed_range, self.coordinator.read_api.data.fanspeed + self.entity_description.speed_range, + self.coordinator.read_api.data.fanspeed, ) @property diff --git a/homeassistant/components/intellifire/light.py b/homeassistant/components/intellifire/light.py index a7f2befaf33c60..5f25b5de823898 100644 --- a/homeassistant/components/intellifire/light.py +++ b/homeassistant/components/intellifire/light.py @@ -6,7 +6,8 @@ from dataclasses import dataclass from typing import Any -from intellifire4py import IntellifireControlAsync, IntellifirePollData +from intellifire4py.control import IntelliFireController +from intellifire4py.model import IntelliFirePollData from homeassistant.components.light import ( ATTR_BRIGHTNESS, @@ -27,8 +28,8 @@ class IntellifireLightRequiredKeysMixin: """Required keys for fan entity.""" - set_fn: Callable[[IntellifireControlAsync, int], Awaitable] - value_fn: Callable[[IntellifirePollData], bool] + set_fn: Callable[[IntelliFireController, int], Awaitable] + value_fn: Callable[[IntelliFirePollData], int] @dataclass(frozen=True) @@ -56,7 +57,7 @@ class IntellifireLight(IntellifireEntity, LightEntity): _attr_supported_color_modes = {ColorMode.BRIGHTNESS} @property - def brightness(self): + def brightness(self) -> int: """Return the current brightness 0-255.""" return 85 * self.entity_description.value_fn(self.coordinator.read_api.data) diff --git a/homeassistant/components/intellifire/manifest.json b/homeassistant/components/intellifire/manifest.json index 90d41fcffe7541..e3ee663e8fecfc 100644 --- a/homeassistant/components/intellifire/manifest.json +++ b/homeassistant/components/intellifire/manifest.json @@ -11,5 +11,5 @@ "documentation": "https://www.home-assistant.io/integrations/intellifire", "iot_class": "local_polling", "loggers": ["intellifire4py"], - "requirements": ["intellifire4py==2.2.2"] + "requirements": ["intellifire4py==4.1.9"] } diff --git a/homeassistant/components/intellifire/sensor.py b/homeassistant/components/intellifire/sensor.py index dd3eef9c9b47b1..eaff89d08e70a0 100644 --- a/homeassistant/components/intellifire/sensor.py +++ b/homeassistant/components/intellifire/sensor.py @@ -6,8 +6,6 @@ from dataclasses import dataclass from datetime import datetime, timedelta -from intellifire4py import IntellifirePollData - from homeassistant.components.sensor import ( SensorDeviceClass, SensorEntity, @@ -29,7 +27,9 @@ class IntellifireSensorRequiredKeysMixin: """Mixin for required keys.""" - value_fn: Callable[[IntellifirePollData], int | str | datetime | None] + value_fn: Callable[ + [IntellifireDataUpdateCoordinator], int | str | datetime | float | None + ] @dataclass(frozen=True) @@ -40,16 +40,29 @@ class IntellifireSensorEntityDescription( """Describes a sensor entity.""" -def _time_remaining_to_timestamp(data: IntellifirePollData) -> datetime | None: +def _time_remaining_to_timestamp( + coordinator: IntellifireDataUpdateCoordinator, +) -> datetime | None: """Define a sensor that takes into account timezone.""" - if not (seconds_offset := data.timeremaining_s): + if not (seconds_offset := coordinator.data.timeremaining_s): return None return utcnow() + timedelta(seconds=seconds_offset) -def _downtime_to_timestamp(data: IntellifirePollData) -> datetime | None: +def _downtime_to_timestamp( + coordinator: IntellifireDataUpdateCoordinator, +) -> datetime | None: """Define a sensor that takes into account a timezone.""" - if not (seconds_offset := data.downtime): + if not (seconds_offset := coordinator.data.downtime): + return None + return utcnow() - timedelta(seconds=seconds_offset) + + +def _uptime_to_timestamp( + coordinator: IntellifireDataUpdateCoordinator, +) -> datetime | None: + """Return a timestamp of how long the sensor has been up.""" + if not (seconds_offset := coordinator.data.uptime): return None return utcnow() - timedelta(seconds=seconds_offset) @@ -60,14 +73,14 @@ def _downtime_to_timestamp(data: IntellifirePollData) -> datetime | None: translation_key="flame_height", state_class=SensorStateClass.MEASUREMENT, # UI uses 1-5 for flame height, backing lib uses 0-4 - value_fn=lambda data: (data.flameheight + 1), + value_fn=lambda coordinator: (coordinator.data.flameheight + 1), ), IntellifireSensorEntityDescription( key="temperature", state_class=SensorStateClass.MEASUREMENT, device_class=SensorDeviceClass.TEMPERATURE, native_unit_of_measurement=UnitOfTemperature.CELSIUS, - value_fn=lambda data: data.temperature_c, + value_fn=lambda coordinator: coordinator.data.temperature_c, ), IntellifireSensorEntityDescription( key="target_temp", @@ -75,13 +88,13 @@ def _downtime_to_timestamp(data: IntellifirePollData) -> datetime | None: state_class=SensorStateClass.MEASUREMENT, device_class=SensorDeviceClass.TEMPERATURE, native_unit_of_measurement=UnitOfTemperature.CELSIUS, - value_fn=lambda data: data.thermostat_setpoint_c, + value_fn=lambda coordinator: coordinator.data.thermostat_setpoint_c, ), IntellifireSensorEntityDescription( key="fan_speed", translation_key="fan_speed", state_class=SensorStateClass.MEASUREMENT, - value_fn=lambda data: data.fanspeed, + value_fn=lambda coordinator: coordinator.data.fanspeed, ), IntellifireSensorEntityDescription( key="timer_end_timestamp", @@ -102,27 +115,27 @@ def _downtime_to_timestamp(data: IntellifirePollData) -> datetime | None: translation_key="uptime", entity_category=EntityCategory.DIAGNOSTIC, device_class=SensorDeviceClass.TIMESTAMP, - value_fn=lambda data: utcnow() - timedelta(seconds=data.uptime), + value_fn=_uptime_to_timestamp, ), IntellifireSensorEntityDescription( key="connection_quality", translation_key="connection_quality", entity_category=EntityCategory.DIAGNOSTIC, - value_fn=lambda data: data.connection_quality, + value_fn=lambda coordinator: coordinator.data.connection_quality, entity_registry_enabled_default=False, ), IntellifireSensorEntityDescription( key="ecm_latency", translation_key="ecm_latency", entity_category=EntityCategory.DIAGNOSTIC, - value_fn=lambda data: data.ecm_latency, + value_fn=lambda coordinator: coordinator.data.ecm_latency, entity_registry_enabled_default=False, ), IntellifireSensorEntityDescription( key="ipv4_address", translation_key="ipv4_address", entity_category=EntityCategory.DIAGNOSTIC, - value_fn=lambda data: data.ipv4_address, + value_fn=lambda coordinator: coordinator.data.ipv4_address, ), ) @@ -134,17 +147,17 @@ async def async_setup_entry( coordinator: IntellifireDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] async_add_entities( - IntellifireSensor(coordinator=coordinator, description=description) + IntelliFireSensor(coordinator=coordinator, description=description) for description in INTELLIFIRE_SENSORS ) -class IntellifireSensor(IntellifireEntity, SensorEntity): - """Extends IntellifireEntity with Sensor specific logic.""" +class IntelliFireSensor(IntellifireEntity, SensorEntity): + """Extends IntelliFireEntity with Sensor specific logic.""" entity_description: IntellifireSensorEntityDescription @property - def native_value(self) -> int | str | datetime | None: + def native_value(self) -> int | str | datetime | float | None: """Return the state.""" - return self.entity_description.value_fn(self.coordinator.read_api.data) + return self.entity_description.value_fn(self.coordinator) diff --git a/homeassistant/components/intellifire/strings.json b/homeassistant/components/intellifire/strings.json index 6393a4e070d16f..2eeb2b50b93f68 100644 --- a/homeassistant/components/intellifire/strings.json +++ b/homeassistant/components/intellifire/strings.json @@ -1,39 +1,30 @@ { "config": { - "flow_title": "{serial} ({host})", + "flow_title": "{serial}", "step": { - "manual_device_entry": { - "description": "Local Configuration", - "data": { - "host": "Host (IP Address)" - } - }, - "api_config": { + "pick_cloud_device": { + "title": "Configure fireplace", + "description": "Select fireplace by serial number:" + }, + "cloud_api": { + "description": "Authenticate against IntelliFire Cloud", + "data_description": { + "username": "Your IntelliFire app username", + "password": "Your IntelliFire app password" + }, "data": { "username": "[%key:common::config_flow::data::email%]", "password": "[%key:common::config_flow::data::password%]" } - }, - "dhcp_confirm": { - "description": "Do you want to set up {host}\nSerial: {serial}?" - }, - "pick_device": { - "title": "Device Selection", - "description": "The following IntelliFire devices were discovered. Please select which you wish to configure.", - "data": { - "host": "[%key:common::config_flow::data::host%]" - } } }, "error": { - "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", - "api_error": "Login failed", - "iftapi_connect": "Error conecting to iftapi.net" + "api_error": "Login failed" }, "abort": { - "already_configured": "[%key:common::config_flow::abort::already_configured_device%]", "reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]", - "not_intellifire_device": "Not an IntelliFire Device." + "not_intellifire_device": "Not an IntelliFire device.", + "no_available_devices": "All available devices have already been configured." } }, "entity": { diff --git a/homeassistant/components/intellifire/switch.py b/homeassistant/components/intellifire/switch.py index 00de6d74a9cbb0..ac6096497b6435 100644 --- a/homeassistant/components/intellifire/switch.py +++ b/homeassistant/components/intellifire/switch.py @@ -6,16 +6,13 @@ from dataclasses import dataclass from typing import Any -from intellifire4py import IntellifirePollData -from intellifire4py.intellifire import IntellifireAPILocal - from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback +from . import IntellifireDataUpdateCoordinator from .const import DOMAIN -from .coordinator import IntellifireDataUpdateCoordinator from .entity import IntellifireEntity @@ -23,9 +20,9 @@ class IntellifireSwitchRequiredKeysMixin: """Mixin for required keys.""" - on_fn: Callable[[IntellifireAPILocal], Awaitable] - off_fn: Callable[[IntellifireAPILocal], Awaitable] - value_fn: Callable[[IntellifirePollData], bool] + on_fn: Callable[[IntellifireDataUpdateCoordinator], Awaitable] + off_fn: Callable[[IntellifireDataUpdateCoordinator], Awaitable] + value_fn: Callable[[IntellifireDataUpdateCoordinator], bool] @dataclass(frozen=True) @@ -39,16 +36,16 @@ class IntellifireSwitchEntityDescription( IntellifireSwitchEntityDescription( key="on_off", translation_key="flame", - on_fn=lambda control_api: control_api.flame_on(), - off_fn=lambda control_api: control_api.flame_off(), - value_fn=lambda data: data.is_on, + on_fn=lambda coordinator: coordinator.control_api.flame_on(), + off_fn=lambda coordinator: coordinator.control_api.flame_off(), + value_fn=lambda coordinator: coordinator.read_api.data.is_on, ), IntellifireSwitchEntityDescription( key="pilot", translation_key="pilot_light", - on_fn=lambda control_api: control_api.pilot_on(), - off_fn=lambda control_api: control_api.pilot_off(), - value_fn=lambda data: data.pilot_on, + on_fn=lambda coordinator: coordinator.control_api.pilot_on(), + off_fn=lambda coordinator: coordinator.control_api.pilot_off(), + value_fn=lambda coordinator: coordinator.read_api.data.pilot_on, ), ) @@ -74,15 +71,15 @@ class IntellifireSwitch(IntellifireEntity, SwitchEntity): async def async_turn_on(self, **kwargs: Any) -> None: """Turn on the switch.""" - await self.entity_description.on_fn(self.coordinator.control_api) + await self.entity_description.on_fn(self.coordinator) await self.async_update_ha_state(force_refresh=True) async def async_turn_off(self, **kwargs: Any) -> None: """Turn off the switch.""" - await self.entity_description.off_fn(self.coordinator.control_api) + await self.entity_description.off_fn(self.coordinator) await self.async_update_ha_state(force_refresh=True) @property def is_on(self) -> bool | None: """Return the on state.""" - return self.entity_description.value_fn(self.coordinator.read_api.data) + return self.entity_description.value_fn(self.coordinator) diff --git a/homeassistant/components/intent_script/icons.json b/homeassistant/components/intent_script/icons.json index a03163179cb5bd..a9829425570a0c 100644 --- a/homeassistant/components/intent_script/icons.json +++ b/homeassistant/components/intent_script/icons.json @@ -1,5 +1,7 @@ { "services": { - "reload": "mdi:reload" + "reload": { + "service": "mdi:reload" + } } } diff --git a/homeassistant/components/iotawatt/config_flow.py b/homeassistant/components/iotawatt/config_flow.py index f8821784a1d998..668844a1c5c4bd 100644 --- a/homeassistant/components/iotawatt/config_flow.py +++ b/homeassistant/components/iotawatt/config_flow.py @@ -3,11 +3,12 @@ from __future__ import annotations import logging +from typing import Any from iotawattpy.iotawatt import Iotawatt import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError @@ -46,11 +47,13 @@ class IOTaWattConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - def __init__(self): + def __init__(self) -> None: """Initialize.""" - self._data = {} + self._data: dict[str, Any] = {} - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" if user_input is None: user_input = {} @@ -72,7 +75,9 @@ async def async_step_user(self, user_input=None): return self.async_show_form(step_id="user", data_schema=schema, errors=errors) - async def async_step_auth(self, user_input=None): + async def async_step_auth( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Authenticate user if authentication is enabled on the IoTaWatt device.""" if user_input is None: user_input = {} diff --git a/homeassistant/components/iotawatt/const.py b/homeassistant/components/iotawatt/const.py index 278030d4f103cd..de008388f6220e 100644 --- a/homeassistant/components/iotawatt/const.py +++ b/homeassistant/components/iotawatt/const.py @@ -7,6 +7,7 @@ import httpx DOMAIN = "iotawatt" +VOLT_AMPERE_REACTIVE = "VAR" VOLT_AMPERE_REACTIVE_HOURS = "VARh" CONNECTION_ERRORS = (KeyError, json.JSONDecodeError, httpx.HTTPError) diff --git a/homeassistant/components/iotawatt/sensor.py b/homeassistant/components/iotawatt/sensor.py index e0328ea9d58ef2..c9af588c1606af 100644 --- a/homeassistant/components/iotawatt/sensor.py +++ b/homeassistant/components/iotawatt/sensor.py @@ -23,7 +23,6 @@ UnitOfEnergy, UnitOfFrequency, UnitOfPower, - UnitOfReactivePower, ) from homeassistant.core import HomeAssistant, callback from homeassistant.helpers import device_registry as dr, entity_registry as er @@ -32,7 +31,7 @@ from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.util import dt as dt_util -from .const import DOMAIN, VOLT_AMPERE_REACTIVE_HOURS +from .const import DOMAIN, VOLT_AMPERE_REACTIVE, VOLT_AMPERE_REACTIVE_HOURS from .coordinator import IotawattUpdater _LOGGER = logging.getLogger(__name__) @@ -90,7 +89,7 @@ class IotaWattSensorEntityDescription(SensorEntityDescription): ), "VAR": IotaWattSensorEntityDescription( key="VAR", - native_unit_of_measurement=UnitOfReactivePower.VOLT_AMPERE_REACTIVE, + native_unit_of_measurement=VOLT_AMPERE_REACTIVE, state_class=SensorStateClass.MEASUREMENT, icon="mdi:flash", entity_registry_enabled_default=False, diff --git a/homeassistant/components/iotty/manifest.json b/homeassistant/components/iotty/manifest.json index 87aa49799b2fea..66baddc6b47de7 100644 --- a/homeassistant/components/iotty/manifest.json +++ b/homeassistant/components/iotty/manifest.json @@ -1,7 +1,7 @@ { "domain": "iotty", "name": "iotty", - "codeowners": ["@pburgio"], + "codeowners": ["@pburgio", "@shapournemati-iotty"], "config_flow": true, "dependencies": ["application_credentials"], "documentation": "https://www.home-assistant.io/integrations/iotty", diff --git a/homeassistant/components/iperf3/icons.json b/homeassistant/components/iperf3/icons.json index 3ef7e301ed65d6..f6ebe1aee2f555 100644 --- a/homeassistant/components/iperf3/icons.json +++ b/homeassistant/components/iperf3/icons.json @@ -1,5 +1,7 @@ { "services": { - "speedtest": "mdi:speedometer" + "speedtest": { + "service": "mdi:speedometer" + } } } diff --git a/homeassistant/components/iqvia/sensor.py b/homeassistant/components/iqvia/sensor.py index ba3c288b702322..af351e0d543172 100644 --- a/homeassistant/components/iqvia/sensor.py +++ b/homeassistant/components/iqvia/sensor.py @@ -244,8 +244,8 @@ def update_from_latest_data(self) -> None: key = self.entity_description.key.split("_")[-1].title() try: - [period] = [p for p in data["periods"] if p["Type"] == key] # type: ignore[index] - except TypeError: + period = next(p for p in data["periods"] if p["Type"] == key) # type: ignore[index] + except StopIteration: return data = cast(dict[str, Any], data) diff --git a/homeassistant/components/iskra/__init__.py b/homeassistant/components/iskra/__init__.py new file mode 100644 index 00000000000000..b841da9df26de2 --- /dev/null +++ b/homeassistant/components/iskra/__init__.py @@ -0,0 +1,100 @@ +"""The iskra integration.""" + +from __future__ import annotations + +from pyiskra.adapters import Modbus, RestAPI +from pyiskra.devices import Device +from pyiskra.exceptions import DeviceConnectionError, DeviceNotSupported, NotAuthorised + +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import ( + CONF_ADDRESS, + CONF_HOST, + CONF_PASSWORD, + CONF_PORT, + CONF_PROTOCOL, + CONF_USERNAME, + Platform, +) +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import ConfigEntryNotReady +from homeassistant.helpers import device_registry as dr + +from .const import DOMAIN, MANUFACTURER +from .coordinator import IskraDataUpdateCoordinator + +PLATFORMS: list[Platform] = [Platform.SENSOR] + + +type IskraConfigEntry = ConfigEntry[list[IskraDataUpdateCoordinator]] + + +async def async_setup_entry(hass: HomeAssistant, entry: IskraConfigEntry) -> bool: + """Set up iskra device from a config entry.""" + conf = entry.data + adapter = None + + if conf[CONF_PROTOCOL] == "modbus_tcp": + adapter = Modbus( + ip_address=conf[CONF_HOST], + protocol="tcp", + port=conf[CONF_PORT], + modbus_address=conf[CONF_ADDRESS], + ) + elif conf[CONF_PROTOCOL] == "rest_api": + authentication = None + if (username := conf.get(CONF_USERNAME)) is not None and ( + password := conf.get(CONF_PASSWORD) + ) is not None: + authentication = { + "username": username, + "password": password, + } + adapter = RestAPI(ip_address=conf[CONF_HOST], authentication=authentication) + + # Try connecting to the device and create pyiskra device object + try: + base_device = await Device.create_device(adapter) + except DeviceConnectionError as e: + raise ConfigEntryNotReady("Cannot connect to the device") from e + except NotAuthorised as e: + raise ConfigEntryNotReady("Not authorised to connect to the device") from e + except DeviceNotSupported as e: + raise ConfigEntryNotReady("Device not supported") from e + + # Initialize the device + await base_device.init() + + # if the device is a gateway, add all child devices, otherwise add the device itself. + if base_device.is_gateway: + # Add the gateway device to the device registry + device_registry = dr.async_get(hass) + device_registry.async_get_or_create( + config_entry_id=entry.entry_id, + identifiers={(DOMAIN, base_device.serial)}, + manufacturer=MANUFACTURER, + name=base_device.model, + model=base_device.model, + sw_version=base_device.fw_version, + ) + + coordinators = [ + IskraDataUpdateCoordinator(hass, child_device) + for child_device in base_device.get_child_devices() + ] + else: + coordinators = [IskraDataUpdateCoordinator(hass, base_device)] + + for coordinator in coordinators: + await coordinator.async_config_entry_first_refresh() + + entry.runtime_data = coordinators + + await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) + + return True + + +async def async_unload_entry(hass: HomeAssistant, entry: IskraConfigEntry) -> bool: + """Unload a config entry.""" + return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) diff --git a/homeassistant/components/iskra/config_flow.py b/homeassistant/components/iskra/config_flow.py new file mode 100644 index 00000000000000..b67b9ba3839566 --- /dev/null +++ b/homeassistant/components/iskra/config_flow.py @@ -0,0 +1,253 @@ +"""Config flow for iskra integration.""" + +from __future__ import annotations + +import logging +from typing import Any + +from pyiskra.adapters import Modbus, RestAPI +from pyiskra.exceptions import ( + DeviceConnectionError, + DeviceTimeoutError, + InvalidResponseCode, + NotAuthorised, +) +from pyiskra.helper import BasicInfo +import voluptuous as vol + +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult +from homeassistant.const import ( + CONF_ADDRESS, + CONF_HOST, + CONF_PASSWORD, + CONF_PORT, + CONF_PROTOCOL, + CONF_USERNAME, +) +from homeassistant.exceptions import HomeAssistantError +from homeassistant.helpers.selector import ( + NumberSelector, + NumberSelectorConfig, + NumberSelectorMode, + SelectSelector, + SelectSelectorConfig, + SelectSelectorMode, +) + +from .const import DOMAIN + +_LOGGER = logging.getLogger(__name__) + + +STEP_USER_DATA_SCHEMA = vol.Schema( + { + vol.Required(CONF_HOST): str, + vol.Required(CONF_PROTOCOL, default="rest_api"): SelectSelector( + SelectSelectorConfig( + options=["rest_api", "modbus_tcp"], + mode=SelectSelectorMode.LIST, + translation_key="protocol", + ), + ), + } +) + +STEP_AUTHENTICATION_DATA_SCHEMA = vol.Schema( + { + vol.Required(CONF_USERNAME): str, + vol.Required(CONF_PASSWORD): str, + } +) + +# CONF_ADDRESS validation is done later in code, as if ranges are set in voluptuous it turns into a slider +STEP_MODBUS_TCP_DATA_SCHEMA = vol.Schema( + { + vol.Required(CONF_PORT, default=10001): vol.All( + vol.Coerce(int), vol.Range(min=0, max=65535) + ), + vol.Required(CONF_ADDRESS, default=33): NumberSelector( + NumberSelectorConfig(min=1, max=255, mode=NumberSelectorMode.BOX) + ), + } +) + + +async def test_rest_api_connection(host: str, user_input: dict[str, Any]) -> BasicInfo: + """Check if the RestAPI requires authentication.""" + + rest_api = RestAPI(ip_address=host, authentication=user_input) + try: + basic_info = await rest_api.get_basic_info() + except NotAuthorised as e: + raise NotAuthorised from e + except (DeviceConnectionError, DeviceTimeoutError, InvalidResponseCode) as e: + raise CannotConnect from e + except Exception as e: + _LOGGER.error("Unexpected exception: %s", e) + raise UnknownException from e + + return basic_info + + +async def test_modbus_connection(host: str, user_input: dict[str, Any]) -> BasicInfo: + """Test the Modbus connection.""" + modbus_api = Modbus( + ip_address=host, + protocol="tcp", + port=user_input[CONF_PORT], + modbus_address=user_input[CONF_ADDRESS], + ) + + try: + basic_info = await modbus_api.get_basic_info() + except NotAuthorised as e: + raise NotAuthorised from e + except (DeviceConnectionError, DeviceTimeoutError, InvalidResponseCode) as e: + raise CannotConnect from e + except Exception as e: + _LOGGER.error("Unexpected exception: %s", e) + raise UnknownException from e + + return basic_info + + +class IskraConfigFlowFlow(ConfigFlow, domain=DOMAIN): + """Handle a config flow for iskra.""" + + VERSION = 1 + host: str + protocol: str + + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle a flow initiated by the user.""" + errors: dict[str, str] = {} + if user_input is not None: + self.host = user_input[CONF_HOST] + self.protocol = user_input[CONF_PROTOCOL] + if self.protocol == "rest_api": + # Check if authentication is required. + try: + device_info = await test_rest_api_connection(self.host, user_input) + except CannotConnect: + errors["base"] = "cannot_connect" + except NotAuthorised: + # Proceed to authentication step. + return await self.async_step_authentication() + except UnknownException: + errors["base"] = "unknown" + # If the connection was not successful, show an error. + + # If the connection was successful, create the device. + if not errors: + return await self._create_entry( + host=self.host, + protocol=self.protocol, + device_info=device_info, + user_input=user_input, + ) + + if self.protocol == "modbus_tcp": + # Proceed to modbus step. + return await self.async_step_modbus_tcp() + + return self.async_show_form( + step_id="user", + data_schema=STEP_USER_DATA_SCHEMA, + errors=errors, + ) + + async def async_step_authentication( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle the authentication step.""" + errors: dict[str, str] = {} + if user_input is not None: + try: + device_info = await test_rest_api_connection(self.host, user_input) + # If the connection failed, abort. + except CannotConnect: + errors["base"] = "cannot_connect" + # If the authentication failed, show an error and authentication form again. + except NotAuthorised: + errors["base"] = "invalid_auth" + except UnknownException: + errors["base"] = "unknown" + + # if the connection was successful, create the device. + if not errors: + return await self._create_entry( + self.host, + self.protocol, + device_info=device_info, + user_input=user_input, + ) + + # If there's no user_input or there was an error, show the authentication form again. + return self.async_show_form( + step_id="authentication", + data_schema=STEP_AUTHENTICATION_DATA_SCHEMA, + errors=errors, + ) + + async def async_step_modbus_tcp( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle the Modbus TCP step.""" + errors: dict[str, str] = {} + + # If there's user_input, check the connection. + if user_input is not None: + # convert to integer + user_input[CONF_ADDRESS] = int(user_input[CONF_ADDRESS]) + + try: + device_info = await test_modbus_connection(self.host, user_input) + + # If the connection failed, show an error. + except CannotConnect: + errors["base"] = "cannot_connect" + except UnknownException: + errors["base"] = "unknown" + + # If the connection was successful, create the device. + if not errors: + return await self._create_entry( + host=self.host, + protocol=self.protocol, + device_info=device_info, + user_input=user_input, + ) + + # If there's no user_input or there was an error, show the modbus form again. + return self.async_show_form( + step_id="modbus_tcp", + data_schema=STEP_MODBUS_TCP_DATA_SCHEMA, + errors=errors, + ) + + async def _create_entry( + self, + host: str, + protocol: str, + device_info: BasicInfo, + user_input: dict[str, Any], + ) -> ConfigFlowResult: + """Create the config entry.""" + + await self.async_set_unique_id(device_info.serial) + self._abort_if_unique_id_configured() + + return self.async_create_entry( + title=device_info.model, + data={CONF_HOST: host, CONF_PROTOCOL: protocol, **user_input}, + ) + + +class CannotConnect(HomeAssistantError): + """Error to indicate we cannot connect.""" + + +class UnknownException(HomeAssistantError): + """Error to indicate an unknown exception occurred.""" diff --git a/homeassistant/components/iskra/const.py b/homeassistant/components/iskra/const.py new file mode 100644 index 00000000000000..5fc3b501962ebb --- /dev/null +++ b/homeassistant/components/iskra/const.py @@ -0,0 +1,25 @@ +"""Constants for the iskra integration.""" + +DOMAIN = "iskra" +MANUFACTURER = "Iskra d.o.o" + +# POWER +ATTR_TOTAL_APPARENT_POWER = "total_apparent_power" +ATTR_TOTAL_REACTIVE_POWER = "total_reactive_power" +ATTR_TOTAL_ACTIVE_POWER = "total_active_power" +ATTR_PHASE1_POWER = "phase1_power" +ATTR_PHASE2_POWER = "phase2_power" +ATTR_PHASE3_POWER = "phase3_power" + +# Voltage +ATTR_PHASE1_VOLTAGE = "phase1_voltage" +ATTR_PHASE2_VOLTAGE = "phase2_voltage" +ATTR_PHASE3_VOLTAGE = "phase3_voltage" + +# Current +ATTR_PHASE1_CURRENT = "phase1_current" +ATTR_PHASE2_CURRENT = "phase2_current" +ATTR_PHASE3_CURRENT = "phase3_current" + +# Frequency +ATTR_FREQUENCY = "frequency" diff --git a/homeassistant/components/iskra/coordinator.py b/homeassistant/components/iskra/coordinator.py new file mode 100644 index 00000000000000..175d8ed4c86a60 --- /dev/null +++ b/homeassistant/components/iskra/coordinator.py @@ -0,0 +1,57 @@ +"""Coordinator for Iskra integration.""" + +from datetime import timedelta +import logging + +from pyiskra.devices import Device +from pyiskra.exceptions import ( + DeviceConnectionError, + DeviceTimeoutError, + InvalidResponseCode, + NotAuthorised, +) + +from homeassistant.core import HomeAssistant +from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed + +from .const import DOMAIN + +_LOGGER = logging.getLogger(__name__) + + +class IskraDataUpdateCoordinator(DataUpdateCoordinator[None]): + """Class to manage fetching Iskra data.""" + + def __init__(self, hass: HomeAssistant, device: Device) -> None: + """Initialize.""" + self.device = device + + update_interval = timedelta(seconds=60) + + super().__init__( + hass, + _LOGGER, + name=DOMAIN, + update_interval=update_interval, + ) + + async def _async_update_data(self) -> None: + """Fetch data from Iskra device.""" + try: + await self.device.update_status() + except DeviceTimeoutError as e: + raise UpdateFailed( + f"Timeout error occurred while updating data for device {self.device.serial}" + ) from e + except DeviceConnectionError as e: + raise UpdateFailed( + f"Connection error occurred while updating data for device {self.device.serial}" + ) from e + except NotAuthorised as e: + raise UpdateFailed( + f"Not authorised to fetch data from device {self.device.serial}" + ) from e + except InvalidResponseCode as e: + raise UpdateFailed( + f"Invalid response code from device {self.device.serial}" + ) from e diff --git a/homeassistant/components/iskra/entity.py b/homeassistant/components/iskra/entity.py new file mode 100644 index 00000000000000..f1c01d3eaa4030 --- /dev/null +++ b/homeassistant/components/iskra/entity.py @@ -0,0 +1,38 @@ +"""Base entity for Iskra devices.""" + +from homeassistant.helpers.device_registry import DeviceInfo +from homeassistant.helpers.update_coordinator import CoordinatorEntity + +from .const import DOMAIN, MANUFACTURER +from .coordinator import IskraDataUpdateCoordinator + + +class IskraEntity(CoordinatorEntity[IskraDataUpdateCoordinator]): + """Representation a base Iskra device.""" + + _attr_has_entity_name = True + + def __init__(self, coordinator: IskraDataUpdateCoordinator) -> None: + """Initialize the Iskra device.""" + super().__init__(coordinator) + self.device = coordinator.device + gateway = self.device.parent_device + + if gateway is not None: + self._attr_device_info = DeviceInfo( + identifiers={(DOMAIN, self.device.serial)}, + manufacturer=MANUFACTURER, + model=self.device.model, + name=self.device.model, + sw_version=self.device.fw_version, + serial_number=self.device.serial, + via_device=(DOMAIN, gateway.serial), + ) + else: + self._attr_device_info = DeviceInfo( + identifiers={(DOMAIN, self.device.serial)}, + manufacturer=MANUFACTURER, + model=self.device.model, + sw_version=self.device.fw_version, + serial_number=self.device.serial, + ) diff --git a/homeassistant/components/iskra/manifest.json b/homeassistant/components/iskra/manifest.json new file mode 100644 index 00000000000000..7bda12ab61598f --- /dev/null +++ b/homeassistant/components/iskra/manifest.json @@ -0,0 +1,11 @@ +{ + "domain": "iskra", + "name": "iskra", + "codeowners": ["@iskramis"], + "config_flow": true, + "documentation": "https://www.home-assistant.io/integrations/iskra", + "integration_type": "hub", + "iot_class": "local_polling", + "loggers": ["pyiskra"], + "requirements": ["pyiskra==0.1.8"] +} diff --git a/homeassistant/components/iskra/sensor.py b/homeassistant/components/iskra/sensor.py new file mode 100644 index 00000000000000..9e9976749a15be --- /dev/null +++ b/homeassistant/components/iskra/sensor.py @@ -0,0 +1,229 @@ +"""Support for Iskra.""" + +from __future__ import annotations + +from collections.abc import Callable +from dataclasses import dataclass + +from pyiskra.devices import Device + +from homeassistant.components.sensor import ( + SensorDeviceClass, + SensorEntity, + SensorEntityDescription, + SensorStateClass, +) +from homeassistant.const import ( + UnitOfApparentPower, + UnitOfElectricCurrent, + UnitOfElectricPotential, + UnitOfFrequency, + UnitOfPower, + UnitOfReactivePower, +) +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from . import IskraConfigEntry +from .const import ( + ATTR_FREQUENCY, + ATTR_PHASE1_CURRENT, + ATTR_PHASE1_POWER, + ATTR_PHASE1_VOLTAGE, + ATTR_PHASE2_CURRENT, + ATTR_PHASE2_POWER, + ATTR_PHASE2_VOLTAGE, + ATTR_PHASE3_CURRENT, + ATTR_PHASE3_POWER, + ATTR_PHASE3_VOLTAGE, + ATTR_TOTAL_ACTIVE_POWER, + ATTR_TOTAL_APPARENT_POWER, + ATTR_TOTAL_REACTIVE_POWER, +) +from .coordinator import IskraDataUpdateCoordinator +from .entity import IskraEntity + + +@dataclass(frozen=True, kw_only=True) +class IskraSensorEntityDescription(SensorEntityDescription): + """Describes Iskra sensor entity.""" + + value_func: Callable[[Device], float | None] + + +SENSOR_TYPES: tuple[IskraSensorEntityDescription, ...] = ( + # Power + IskraSensorEntityDescription( + key=ATTR_TOTAL_ACTIVE_POWER, + translation_key="total_active_power", + device_class=SensorDeviceClass.POWER, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfPower.WATT, + value_func=lambda device: device.measurements.total.active_power.value, + ), + IskraSensorEntityDescription( + key=ATTR_TOTAL_REACTIVE_POWER, + translation_key="total_reactive_power", + device_class=SensorDeviceClass.REACTIVE_POWER, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfReactivePower.VOLT_AMPERE_REACTIVE, + value_func=lambda device: device.measurements.total.reactive_power.value, + ), + IskraSensorEntityDescription( + key=ATTR_TOTAL_APPARENT_POWER, + translation_key="total_apparent_power", + device_class=SensorDeviceClass.APPARENT_POWER, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfApparentPower.VOLT_AMPERE, + value_func=lambda device: device.measurements.total.apparent_power.value, + ), + IskraSensorEntityDescription( + key=ATTR_PHASE1_POWER, + translation_key="phase1_power", + device_class=SensorDeviceClass.POWER, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfPower.WATT, + value_func=lambda device: device.measurements.phases[0].active_power.value, + ), + IskraSensorEntityDescription( + key=ATTR_PHASE2_POWER, + translation_key="phase2_power", + device_class=SensorDeviceClass.POWER, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfPower.WATT, + value_func=lambda device: device.measurements.phases[1].active_power.value, + ), + IskraSensorEntityDescription( + key=ATTR_PHASE3_POWER, + translation_key="phase3_power", + device_class=SensorDeviceClass.POWER, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfPower.WATT, + value_func=lambda device: device.measurements.phases[2].active_power.value, + ), + # Voltage + IskraSensorEntityDescription( + key=ATTR_PHASE1_VOLTAGE, + translation_key="phase1_voltage", + device_class=SensorDeviceClass.VOLTAGE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfElectricPotential.VOLT, + value_func=lambda device: device.measurements.phases[0].voltage.value, + ), + IskraSensorEntityDescription( + key=ATTR_PHASE2_VOLTAGE, + translation_key="phase2_voltage", + device_class=SensorDeviceClass.VOLTAGE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfElectricPotential.VOLT, + value_func=lambda device: device.measurements.phases[1].voltage.value, + ), + IskraSensorEntityDescription( + key=ATTR_PHASE3_VOLTAGE, + translation_key="phase3_voltage", + device_class=SensorDeviceClass.VOLTAGE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfElectricPotential.VOLT, + value_func=lambda device: device.measurements.phases[2].voltage.value, + ), + # Current + IskraSensorEntityDescription( + key=ATTR_PHASE1_CURRENT, + translation_key="phase1_current", + device_class=SensorDeviceClass.CURRENT, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfElectricCurrent.AMPERE, + value_func=lambda device: device.measurements.phases[0].current.value, + ), + IskraSensorEntityDescription( + key=ATTR_PHASE2_CURRENT, + translation_key="phase2_current", + device_class=SensorDeviceClass.CURRENT, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfElectricCurrent.AMPERE, + value_func=lambda device: device.measurements.phases[1].current.value, + ), + IskraSensorEntityDescription( + key=ATTR_PHASE3_CURRENT, + translation_key="phase3_current", + device_class=SensorDeviceClass.CURRENT, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfElectricCurrent.AMPERE, + value_func=lambda device: device.measurements.phases[2].current.value, + ), + # Frequency + IskraSensorEntityDescription( + key=ATTR_FREQUENCY, + translation_key="frequency", + device_class=SensorDeviceClass.FREQUENCY, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfFrequency.HERTZ, + value_func=lambda device: device.measurements.frequency.value, + ), +) + + +async def async_setup_entry( + hass: HomeAssistant, + entry: IskraConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up Iskra sensors based on config_entry.""" + + # Device that uses the config entry. + coordinators = entry.runtime_data + + entities: list[IskraSensor] = [] + + # Add sensors for each device. + for coordinator in coordinators: + device = coordinator.device + sensors = [] + + # Add measurement sensors. + if device.supports_measurements: + sensors.append(ATTR_FREQUENCY) + sensors.append(ATTR_TOTAL_APPARENT_POWER) + sensors.append(ATTR_TOTAL_ACTIVE_POWER) + sensors.append(ATTR_TOTAL_REACTIVE_POWER) + if device.phases >= 1: + sensors.append(ATTR_PHASE1_VOLTAGE) + sensors.append(ATTR_PHASE1_POWER) + sensors.append(ATTR_PHASE1_CURRENT) + if device.phases >= 2: + sensors.append(ATTR_PHASE2_VOLTAGE) + sensors.append(ATTR_PHASE2_POWER) + sensors.append(ATTR_PHASE2_CURRENT) + if device.phases >= 3: + sensors.append(ATTR_PHASE3_VOLTAGE) + sensors.append(ATTR_PHASE3_POWER) + sensors.append(ATTR_PHASE3_CURRENT) + + entities.extend( + IskraSensor(coordinator, description) + for description in SENSOR_TYPES + if description.key in sensors + ) + + async_add_entities(entities) + + +class IskraSensor(IskraEntity, SensorEntity): + """Representation of a Sensor.""" + + entity_description: IskraSensorEntityDescription + + def __init__( + self, + coordinator: IskraDataUpdateCoordinator, + description: IskraSensorEntityDescription, + ) -> None: + """Initialize the sensor.""" + super().__init__(coordinator) + self.entity_description = description + self._attr_unique_id = f"{coordinator.device.serial}_{description.key}" + + @property + def native_value(self) -> float | None: + """Return the state of the sensor.""" + return self.entity_description.value_func(self.device) diff --git a/homeassistant/components/iskra/strings.json b/homeassistant/components/iskra/strings.json new file mode 100644 index 00000000000000..bd70336f637a12 --- /dev/null +++ b/homeassistant/components/iskra/strings.json @@ -0,0 +1,92 @@ +{ + "config": { + "step": { + "user": { + "title": "Configure Iskra Device", + "description": "Enter the IP address of your Iskra Device and select protocol.", + "data": { + "host": "[%key:common::config_flow::data::host%]" + }, + "data_description": { + "host": "Hostname or IP address of your Iskra device." + } + }, + "authentication": { + "title": "Configure Rest API Credentials", + "description": "Enter username and password", + "data": { + "username": "[%key:common::config_flow::data::username%]", + "password": "[%key:common::config_flow::data::password%]" + } + }, + "modbus_tcp": { + "title": "Configure Modbus TCP", + "description": "Enter Modbus TCP port and device's Modbus address.", + "data": { + "port": "[%key:common::config_flow::data::port%]", + "address": "Modbus address" + }, + "data_description": { + "port": "Port number can be found in the device's settings menu.", + "address": "Modbus address can be found in the device's settings menu." + } + } + }, + "error": { + "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", + "invalid_auth": "[%key:common::config_flow::error::invalid_auth%]", + "unknown": "[%key:common::config_flow::error::unknown%]" + }, + "abort": { + "already_configured": "[%key:common::config_flow::abort::already_configured_device%]" + } + }, + "selector": { + "protocol": { + "options": { + "rest_api": "Rest API", + "modbus_tcp": "Modbus TCP" + } + } + }, + "entity": { + "sensor": { + "total_active_power": { + "name": "Total active power" + }, + "total_apparent_power": { + "name": "Total apparent power" + }, + "total_reactive_power": { + "name": "Total reactive power" + }, + "phase1_power": { + "name": "Phase 1 power" + }, + "phase2_power": { + "name": "Phase 2 power" + }, + "phase3_power": { + "name": "Phase 3 power" + }, + "phase1_voltage": { + "name": "Phase 1 voltage" + }, + "phase2_voltage": { + "name": "Phase 2 voltage" + }, + "phase3_voltage": { + "name": "Phase 3 voltage" + }, + "phase1_current": { + "name": "Phase 1 current" + }, + "phase2_current": { + "name": "Phase 2 current" + }, + "phase3_current": { + "name": "Phase 3 current" + } + } + } +} diff --git a/homeassistant/components/isy994/icons.json b/homeassistant/components/isy994/icons.json index 27b2ea6954ed4a..9c6e7fa78df23f 100644 --- a/homeassistant/components/isy994/icons.json +++ b/homeassistant/components/isy994/icons.json @@ -1,12 +1,28 @@ { "services": { - "send_raw_node_command": "mdi:console-line", - "send_node_command": "mdi:console", - "get_zwave_parameter": "mdi:download", - "set_zwave_parameter": "mdi:upload", - "set_zwave_lock_user_code": "mdi:upload-lock", - "delete_zwave_lock_user_code": "mdi:lock-remove", - "rename_node": "mdi:pencil", - "send_program_command": "mdi:console" + "send_raw_node_command": { + "service": "mdi:console-line" + }, + "send_node_command": { + "service": "mdi:console" + }, + "get_zwave_parameter": { + "service": "mdi:download" + }, + "set_zwave_parameter": { + "service": "mdi:upload" + }, + "set_zwave_lock_user_code": { + "service": "mdi:upload-lock" + }, + "delete_zwave_lock_user_code": { + "service": "mdi:lock-remove" + }, + "rename_node": { + "service": "mdi:pencil" + }, + "send_program_command": { + "service": "mdi:console" + } } } diff --git a/homeassistant/components/izone/climate.py b/homeassistant/components/izone/climate.py index 3a1279a9bd43e4..617cdc730cc08c 100644 --- a/homeassistant/components/izone/climate.py +++ b/homeassistant/components/izone/climate.py @@ -441,6 +441,9 @@ class ZoneDevice(ClimateEntity): _attr_name = None _attr_temperature_unit = UnitOfTemperature.CELSIUS _attr_target_temperature_step = 0.5 + _attr_supported_features = ( + ClimateEntityFeature.TURN_OFF | ClimateEntityFeature.TURN_ON + ) def __init__(self, controller: ControllerDevice, zone: Zone) -> None: """Initialise ZoneDevice.""" diff --git a/homeassistant/components/izone/icons.json b/homeassistant/components/izone/icons.json index e02cd57c141270..bb38db27839ba9 100644 --- a/homeassistant/components/izone/icons.json +++ b/homeassistant/components/izone/icons.json @@ -1,6 +1,10 @@ { "services": { - "airflow_min": "mdi:fan-minus", - "airflow_max": "mdi:fan-plus" + "airflow_min": { + "service": "mdi:fan-minus" + }, + "airflow_max": { + "service": "mdi:fan-plus" + } } } diff --git a/homeassistant/components/jewish_calendar/config_flow.py b/homeassistant/components/jewish_calendar/config_flow.py index 8f04d73915fc1e..518db38b3bbbe6 100644 --- a/homeassistant/components/jewish_calendar/config_flow.py +++ b/homeassistant/components/jewish_calendar/config_flow.py @@ -30,7 +30,6 @@ SelectSelector, SelectSelectorConfig, ) -from homeassistant.helpers.typing import ConfigType from .const import ( CONF_CANDLE_LIGHT_MINUTES, @@ -125,11 +124,9 @@ async def async_step_user( ), ) - async def async_step_import( - self, import_config: ConfigType | None - ) -> ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Import a config entry from configuration.yaml.""" - return await self.async_step_user(import_config) + return await self.async_step_user(import_data) class JewishCalendarOptionsFlowHandler(OptionsFlowWithConfigEntry): diff --git a/homeassistant/components/juicenet/config_flow.py b/homeassistant/components/juicenet/config_flow.py index 607ffb6ffe2e36..8bcee5677e69b1 100644 --- a/homeassistant/components/juicenet/config_flow.py +++ b/homeassistant/components/juicenet/config_flow.py @@ -1,13 +1,14 @@ """Config flow for JuiceNet integration.""" import logging +from typing import Any import aiohttp from pyjuicenet import Api, TokenError import voluptuous as vol from homeassistant import core, exceptions -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_ACCESS_TOKEN from homeassistant.helpers.aiohttp_client import async_get_clientsession @@ -44,7 +45,9 @@ class JuiceNetConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" errors = {} if user_input is not None: @@ -66,9 +69,9 @@ async def async_step_user(self, user_input=None): step_id="user", data_schema=DATA_SCHEMA, errors=errors ) - async def async_step_import(self, user_input): + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Handle import.""" - return await self.async_step_user(user_input) + return await self.async_step_user(import_data) class CannotConnect(exceptions.HomeAssistantError): diff --git a/homeassistant/components/justnimbus/config_flow.py b/homeassistant/components/justnimbus/config_flow.py index 0520c558266629..8c816c1ac1b1ec 100644 --- a/homeassistant/components/justnimbus/config_flow.py +++ b/homeassistant/components/justnimbus/config_flow.py @@ -77,7 +77,7 @@ async def async_step_user( ) async def async_step_reauth( - self, user_input: Mapping[str, Any] + self, entry_data: Mapping[str, Any] ) -> ConfigFlowResult: """Perform reauth upon an API authentication error.""" self.reauth_entry = self.hass.config_entries.async_get_entry( diff --git a/homeassistant/components/jvc_projector/__init__.py b/homeassistant/components/jvc_projector/__init__.py index 8ce1fb46e3d1af..09e93127e40d5c 100644 --- a/homeassistant/components/jvc_projector/__init__.py +++ b/homeassistant/components/jvc_projector/__init__.py @@ -15,13 +15,14 @@ from homeassistant.core import Event, HomeAssistant from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady -from .const import DOMAIN from .coordinator import JvcProjectorDataUpdateCoordinator +type JVCConfigEntry = ConfigEntry[JvcProjectorDataUpdateCoordinator] + PLATFORMS = [Platform.BINARY_SENSOR, Platform.REMOTE, Platform.SELECT, Platform.SENSOR] -async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: +async def async_setup_entry(hass: HomeAssistant, entry: JVCConfigEntry) -> bool: """Set up integration from a config entry.""" device = JvcProjector( host=entry.data[CONF_HOST], @@ -43,7 +44,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: coordinator = JvcProjectorDataUpdateCoordinator(hass, device) await coordinator.async_config_entry_first_refresh() - hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator + entry.runtime_data = coordinator async def disconnect(event: Event) -> None: await device.disconnect() @@ -57,9 +58,8 @@ async def disconnect(event: Event) -> None: return True -async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: +async def async_unload_entry(hass: HomeAssistant, entry: JVCConfigEntry) -> bool: """Unload config entry.""" if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): - await hass.data[DOMAIN][entry.entry_id].device.disconnect() - hass.data[DOMAIN].pop(entry.entry_id) + await entry.runtime_data.device.disconnect() return unload_ok diff --git a/homeassistant/components/jvc_projector/binary_sensor.py b/homeassistant/components/jvc_projector/binary_sensor.py index 7e8788aa0a63ab..6dfac63892bec0 100644 --- a/homeassistant/components/jvc_projector/binary_sensor.py +++ b/homeassistant/components/jvc_projector/binary_sensor.py @@ -5,22 +5,20 @@ from jvcprojector import const from homeassistant.components.binary_sensor import BinarySensorEntity -from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import JvcProjectorDataUpdateCoordinator -from .const import DOMAIN +from . import JVCConfigEntry, JvcProjectorDataUpdateCoordinator from .entity import JvcProjectorEntity ON_STATUS = (const.ON, const.WARMING) async def async_setup_entry( - hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback + hass: HomeAssistant, entry: JVCConfigEntry, async_add_entities: AddEntitiesCallback ) -> None: """Set up the JVC Projector platform from a config entry.""" - coordinator: JvcProjectorDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] + coordinator = entry.runtime_data async_add_entities([JvcBinarySensor(coordinator)]) diff --git a/homeassistant/components/jvc_projector/config_flow.py b/homeassistant/components/jvc_projector/config_flow.py index 7fbfb17a976281..253aa640f718c8 100644 --- a/homeassistant/components/jvc_projector/config_flow.py +++ b/homeassistant/components/jvc_projector/config_flow.py @@ -74,7 +74,7 @@ async def async_step_user( ) async def async_step_reauth( - self, user_input: Mapping[str, Any] + self, entry_data: Mapping[str, Any] ) -> ConfigFlowResult: """Perform reauth on password authentication error.""" self._reauth_entry = self.hass.config_entries.async_get_entry( diff --git a/homeassistant/components/jvc_projector/remote.py b/homeassistant/components/jvc_projector/remote.py index b69d3b0118b3f3..f90a2816363d8a 100644 --- a/homeassistant/components/jvc_projector/remote.py +++ b/homeassistant/components/jvc_projector/remote.py @@ -10,12 +10,11 @@ from jvcprojector import const from homeassistant.components.remote import RemoteEntity -from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.entity_platform import AddEntitiesCallback -from .const import DOMAIN +from . import JVCConfigEntry from .entity import JvcProjectorEntity COMMANDS = { @@ -55,10 +54,10 @@ async def async_setup_entry( - hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback + hass: HomeAssistant, entry: JVCConfigEntry, async_add_entities: AddEntitiesCallback ) -> None: """Set up the JVC Projector platform from a config entry.""" - coordinator = hass.data[DOMAIN][entry.entry_id] + coordinator = entry.runtime_data async_add_entities([JvcProjectorRemote(coordinator)], True) diff --git a/homeassistant/components/jvc_projector/select.py b/homeassistant/components/jvc_projector/select.py index 1395637fad1df4..60c80f98fc0702 100644 --- a/homeassistant/components/jvc_projector/select.py +++ b/homeassistant/components/jvc_projector/select.py @@ -9,12 +9,10 @@ from jvcprojector import JvcProjector, const from homeassistant.components.select import SelectEntity, SelectEntityDescription -from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import JvcProjectorDataUpdateCoordinator -from .const import DOMAIN +from . import JVCConfigEntry, JvcProjectorDataUpdateCoordinator from .entity import JvcProjectorEntity @@ -41,11 +39,11 @@ class JvcProjectorSelectDescription(SelectEntityDescription): async def async_setup_entry( hass: HomeAssistant, - entry: ConfigEntry, + entry: JVCConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: """Set up the JVC Projector platform from a config entry.""" - coordinator: JvcProjectorDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] + coordinator = entry.runtime_data async_add_entities( JvcProjectorSelectEntity(coordinator, description) for description in SELECTS diff --git a/homeassistant/components/jvc_projector/sensor.py b/homeassistant/components/jvc_projector/sensor.py index 9be04b367e64e9..3edf51e4316cbd 100644 --- a/homeassistant/components/jvc_projector/sensor.py +++ b/homeassistant/components/jvc_projector/sensor.py @@ -9,13 +9,11 @@ SensorEntity, SensorEntityDescription, ) -from homeassistant.config_entries import ConfigEntry from homeassistant.const import EntityCategory from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import JvcProjectorDataUpdateCoordinator -from .const import DOMAIN +from . import JVCConfigEntry, JvcProjectorDataUpdateCoordinator from .entity import JvcProjectorEntity JVC_SENSORS = ( @@ -36,10 +34,10 @@ async def async_setup_entry( - hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback + hass: HomeAssistant, entry: JVCConfigEntry, async_add_entities: AddEntitiesCallback ) -> None: """Set up the JVC Projector platform from a config entry.""" - coordinator: JvcProjectorDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] + coordinator = entry.runtime_data async_add_entities( JvcSensor(coordinator, description) for description in JVC_SENSORS diff --git a/homeassistant/components/keba/icons.json b/homeassistant/components/keba/icons.json index 7f64bf7fb346a3..6de43a84cf6b40 100644 --- a/homeassistant/components/keba/icons.json +++ b/homeassistant/components/keba/icons.json @@ -1,12 +1,28 @@ { "services": { - "request_data": "mdi:database-arrow-down", - "authorize": "mdi:lock", - "deauthorize": "mdi:lock-open", - "set_energy": "mdi:flash", - "set_current": "mdi:flash", - "enable": "mdi:flash", - "disable": "mdi:fash-off", - "set_failsafe": "mdi:message-alert" + "request_data": { + "service": "mdi:database-arrow-down" + }, + "authorize": { + "service": "mdi:lock" + }, + "deauthorize": { + "service": "mdi:lock-open" + }, + "set_energy": { + "service": "mdi:flash" + }, + "set_current": { + "service": "mdi:flash" + }, + "enable": { + "service": "mdi:flash" + }, + "disable": { + "service": "mdi:fash-off" + }, + "set_failsafe": { + "service": "mdi:message-alert" + } } } diff --git a/homeassistant/components/kef/icons.json b/homeassistant/components/kef/icons.json index eeb6dd099cec41..e259e91eb1b0e4 100644 --- a/homeassistant/components/kef/icons.json +++ b/homeassistant/components/kef/icons.json @@ -1,12 +1,28 @@ { "services": { - "update_dsp": "mdi:update", - "set_mode": "mdi:cog", - "set_desk_db": "mdi:volume-high", - "set_wall_db": "mdi:volume-high", - "set_treble_db": "mdi:volume-high", - "set_high_hz": "mdi:sine-wave", - "set_low_hz": "mdi:cosine-wave", - "set_sub_db": "mdi:volume-high" + "update_dsp": { + "service": "mdi:update" + }, + "set_mode": { + "service": "mdi:cog" + }, + "set_desk_db": { + "service": "mdi:volume-high" + }, + "set_wall_db": { + "service": "mdi:volume-high" + }, + "set_treble_db": { + "service": "mdi:volume-high" + }, + "set_high_hz": { + "service": "mdi:sine-wave" + }, + "set_low_hz": { + "service": "mdi:cosine-wave" + }, + "set_sub_db": { + "service": "mdi:volume-high" + } } } diff --git a/homeassistant/components/keyboard/icons.json b/homeassistant/components/keyboard/icons.json index 8186b2684ddd09..03b6210bf41ad8 100644 --- a/homeassistant/components/keyboard/icons.json +++ b/homeassistant/components/keyboard/icons.json @@ -1,10 +1,22 @@ { "services": { - "volume_up": "mdi:volume-high", - "volume_down": "mdi:volume-low", - "volume_mute": "mdi:volume-off", - "media_play_pause": "mdi:play-pause", - "media_next_track": "mdi:skip-next", - "media_prev_track": "mdi:skip-previous" + "volume_up": { + "service": "mdi:volume-high" + }, + "volume_down": { + "service": "mdi:volume-low" + }, + "volume_mute": { + "service": "mdi:volume-off" + }, + "media_play_pause": { + "service": "mdi:play-pause" + }, + "media_next_track": { + "service": "mdi:skip-next" + }, + "media_prev_track": { + "service": "mdi:skip-previous" + } } } diff --git a/homeassistant/components/keymitt_ble/config_flow.py b/homeassistant/components/keymitt_ble/config_flow.py index 589798a281aa22..217ce3cc923616 100644 --- a/homeassistant/components/keymitt_ble/config_flow.py +++ b/homeassistant/components/keymitt_ble/config_flow.py @@ -42,9 +42,9 @@ class MicroBotConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - def __init__(self): + def __init__(self) -> None: """Initialize.""" - self._errors = {} + self._errors: dict[str, str] = {} self._discovered_adv: MicroBotAdvertisement | None = None self._discovered_advs: dict[str, MicroBotAdvertisement] = {} self._client: MicroBotApiClient | None = None diff --git a/homeassistant/components/keymitt_ble/icons.json b/homeassistant/components/keymitt_ble/icons.json index 77450fbf02647f..d265d96b395f91 100644 --- a/homeassistant/components/keymitt_ble/icons.json +++ b/homeassistant/components/keymitt_ble/icons.json @@ -1,5 +1,7 @@ { "services": { - "calibrate": "mdi:wrench" + "calibrate": { + "service": "mdi:wrench" + } } } diff --git a/homeassistant/components/kitchen_sink/__init__.py b/homeassistant/components/kitchen_sink/__init__.py index 94dfca77410533..2c3887bb3837f2 100644 --- a/homeassistant/components/kitchen_sink/__init__.py +++ b/homeassistant/components/kitchen_sink/__init__.py @@ -9,6 +9,8 @@ import datetime from random import random +import voluptuous as vol + from homeassistant.components.recorder import DOMAIN as RECORDER_DOMAIN, get_instance from homeassistant.components.recorder.models import StatisticData, StatisticMetaData from homeassistant.components.recorder.statistics import ( @@ -18,7 +20,7 @@ ) from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry from homeassistant.const import Platform, UnitOfEnergy, UnitOfTemperature, UnitOfVolume -from homeassistant.core import HomeAssistant +from homeassistant.core import HomeAssistant, ServiceCall, callback from homeassistant.helpers import config_validation as cv from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue from homeassistant.helpers.typing import ConfigType @@ -40,6 +42,15 @@ CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN) +SCHEMA_SERVICE_TEST_SERVICE_1 = vol.Schema( + { + vol.Required("field_1"): vol.Coerce(int), + vol.Required("field_2"): vol.In(["off", "auto", "cool"]), + vol.Optional("field_3"): vol.Coerce(int), + vol.Optional("field_4"): vol.In(["forwards", "reverse"]), + } +) + async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up the demo environment.""" @@ -48,6 +59,15 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: DOMAIN, context={"source": SOURCE_IMPORT}, data={} ) ) + + @callback + def service_handler(call: ServiceCall | None = None) -> None: + """Do nothing.""" + + hass.services.async_register( + DOMAIN, "test_service_1", service_handler, SCHEMA_SERVICE_TEST_SERVICE_1 + ) + return True diff --git a/homeassistant/components/kitchen_sink/config_flow.py b/homeassistant/components/kitchen_sink/config_flow.py index c561ca29b8a974..8cff9321729a4d 100644 --- a/homeassistant/components/kitchen_sink/config_flow.py +++ b/homeassistant/components/kitchen_sink/config_flow.py @@ -2,6 +2,7 @@ from __future__ import annotations +from collections.abc import Mapping from typing import Any import voluptuous as vol @@ -34,18 +35,22 @@ def async_get_options_flow( """Get the options flow for this handler.""" return OptionsFlowHandler(config_entry) - async def async_step_import(self, import_info: dict[str, Any]) -> ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Set the config entry up from yaml.""" if self._async_current_entries(): return self.async_abort(reason="single_instance_allowed") - return self.async_create_entry(title="Kitchen Sink", data=import_info) + return self.async_create_entry(title="Kitchen Sink", data=import_data) - async def async_step_reauth(self, data): + async def async_step_reauth( + self, entry_data: Mapping[str, Any] + ) -> ConfigFlowResult: """Reauth step.""" return await self.async_step_reauth_confirm() - async def async_step_reauth_confirm(self, user_input=None): + async def async_step_reauth_confirm( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Reauth confirm step.""" if user_input is None: return self.async_show_form(step_id="reauth_confirm") diff --git a/homeassistant/components/kitchen_sink/icons.json b/homeassistant/components/kitchen_sink/icons.json index 85472996819b3e..565d595d9c7aa7 100644 --- a/homeassistant/components/kitchen_sink/icons.json +++ b/homeassistant/components/kitchen_sink/icons.json @@ -2,10 +2,18 @@ "options": { "step": { "options_1": { - "section": { + "sections": { "section_1": "mdi:robot" } } } + }, + "services": { + "test_service_1": { + "service": "mdi:flask", + "sections": { + "advanced_fields": "mdi:test-tube" + } + } } } diff --git a/homeassistant/components/kitchen_sink/lawn_mower.py b/homeassistant/components/kitchen_sink/lawn_mower.py index 50ec70f6759376..51814fb262dde6 100644 --- a/homeassistant/components/kitchen_sink/lawn_mower.py +++ b/homeassistant/components/kitchen_sink/lawn_mower.py @@ -30,18 +30,26 @@ async def async_setup_platform( ), DemoLawnMower( "kitchen_sink_mower_002", + "Mower can return", + LawnMowerActivity.RETURNING, + LawnMowerEntityFeature.DOCK + | LawnMowerEntityFeature.PAUSE + | LawnMowerEntityFeature.START_MOWING, + ), + DemoLawnMower( + "kitchen_sink_mower_003", "Mower can dock", LawnMowerActivity.MOWING, LawnMowerEntityFeature.DOCK | LawnMowerEntityFeature.START_MOWING, ), DemoLawnMower( - "kitchen_sink_mower_003", + "kitchen_sink_mower_004", "Mower can pause", LawnMowerActivity.DOCKED, LawnMowerEntityFeature.PAUSE | LawnMowerEntityFeature.START_MOWING, ), DemoLawnMower( - "kitchen_sink_mower_004", + "kitchen_sink_mower_005", "Mower can do all", LawnMowerActivity.DOCKED, LawnMowerEntityFeature.DOCK @@ -49,7 +57,7 @@ async def async_setup_platform( | LawnMowerEntityFeature.START_MOWING, ), DemoLawnMower( - "kitchen_sink_mower_005", + "kitchen_sink_mower_006", "Mower is paused", LawnMowerActivity.PAUSED, LawnMowerEntityFeature.DOCK diff --git a/homeassistant/components/kitchen_sink/services.yaml b/homeassistant/components/kitchen_sink/services.yaml new file mode 100644 index 00000000000000..c65495095dc7ba --- /dev/null +++ b/homeassistant/components/kitchen_sink/services.yaml @@ -0,0 +1,32 @@ +test_service_1: + fields: + field_1: + required: true + selector: + number: + min: 0 + max: 60 + unit_of_measurement: seconds + field_2: + required: true + selector: + select: + options: + - "off" + - "auto" + - "cool" + advanced_fields: + collapsed: true + fields: + field_3: + selector: + number: + min: 0 + max: 24 + unit_of_measurement: hours + field_4: + selector: + select: + options: + - "forward" + - "reverse" diff --git a/homeassistant/components/kitchen_sink/strings.json b/homeassistant/components/kitchen_sink/strings.json index e67527d846828f..b10534eac00a08 100644 --- a/homeassistant/components/kitchen_sink/strings.json +++ b/homeassistant/components/kitchen_sink/strings.json @@ -12,7 +12,7 @@ "data": {} }, "options_1": { - "section": { + "sections": { "section_1": { "data": { "bool": "Optional boolean", @@ -71,5 +71,35 @@ "title": "This is not a fixable problem", "description": "This issue is never going to give up." } + }, + "services": { + "test_service_1": { + "name": "Test service 1", + "description": "Fake service for testing", + "fields": { + "field_1": { + "name": "Field 1", + "description": "Number of seconds" + }, + "field_2": { + "name": "Field 2", + "description": "Mode" + }, + "field_3": { + "name": "Field 3", + "description": "Number of hours" + }, + "field_4": { + "name": "Field 4", + "description": "Direction" + } + }, + "sections": { + "advanced_fields": { + "name": "Advanced options", + "description": "Some very advanced things" + } + } + } } } diff --git a/homeassistant/components/kmtronic/config_flow.py b/homeassistant/components/kmtronic/config_flow.py index 746b075789f74c..6bf0b878f72917 100644 --- a/homeassistant/components/kmtronic/config_flow.py +++ b/homeassistant/components/kmtronic/config_flow.py @@ -3,13 +3,19 @@ from __future__ import annotations import logging +from typing import Any import aiohttp from pykmtronic.auth import Auth from pykmtronic.hub import KMTronicHubAPI import voluptuous as vol -from homeassistant.config_entries import ConfigEntry, ConfigFlow, OptionsFlow +from homeassistant.config_entries import ( + ConfigEntry, + ConfigFlow, + ConfigFlowResult, + OptionsFlow, +) from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import HomeAssistantError @@ -62,7 +68,9 @@ def async_get_options_flow( """Get the options flow for this handler.""" return KMTronicOptionsFlow(config_entry) - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" errors = {} if user_input is not None: @@ -98,7 +106,9 @@ def __init__(self, config_entry: ConfigEntry) -> None: """Initialize options flow.""" self.config_entry = config_entry - async def async_step_init(self, user_input=None): + async def async_step_init( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Manage the options.""" if user_input is not None: return self.async_create_entry(title="", data=user_input) diff --git a/homeassistant/components/knocki/strings.json b/homeassistant/components/knocki/strings.json index b7a7daad1fcaad..8f5d01611664d4 100644 --- a/homeassistant/components/knocki/strings.json +++ b/homeassistant/components/knocki/strings.json @@ -11,6 +11,9 @@ "error": { "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", "unknown": "[%key:common::config_flow::error::unknown%]" + }, + "abort": { + "already_configured": "[%key:common::config_flow::abort::already_configured_service%]" } }, "entity": { diff --git a/homeassistant/components/knx/__init__.py b/homeassistant/components/knx/__init__.py index a401ee2ccac8d0..01d5294639c91c 100644 --- a/homeassistant/components/knx/__init__.py +++ b/homeassistant/components/knx/__init__.py @@ -297,6 +297,7 @@ def __init__( self.config_store = KNXConfigStore(hass=hass, config_entry=entry) self.xknx = XKNX( + address_format=self.project.get_address_format(), connection_config=self.connection_config(), rate_limit=self.entry.data[CONF_KNX_RATE_LIMIT], state_updater=self.entry.data[CONF_KNX_STATE_UPDATER], diff --git a/homeassistant/components/knx/expose.py b/homeassistant/components/knx/expose.py index 921af6ba4a91d1..82bee48ba69a56 100644 --- a/homeassistant/components/knx/expose.py +++ b/homeassistant/components/knx/expose.py @@ -125,6 +125,8 @@ def async_remove(self) -> None: def _get_expose_value(self, state: State | None) -> bool | int | float | str | None: """Extract value from state.""" if state is None or state.state in (STATE_UNKNOWN, STATE_UNAVAILABLE): + if self.expose_default is None: + return None value = self.expose_default elif self.expose_attribute is not None: _attr = state.attributes.get(self.expose_attribute) @@ -154,12 +156,22 @@ def _get_expose_value(self, state: State | None) -> bool | int | float | str | N if value is not None and ( isinstance(self.device.sensor_value, RemoteValueSensor) ): - if issubclass(self.device.sensor_value.dpt_class, DPTNumeric): - return float(value) - if issubclass(self.device.sensor_value.dpt_class, DPTString): - # DPT 16.000 only allows up to 14 Bytes - return str(value)[:14] - return value + try: + if issubclass(self.device.sensor_value.dpt_class, DPTNumeric): + return float(value) + if issubclass(self.device.sensor_value.dpt_class, DPTString): + # DPT 16.000 only allows up to 14 Bytes + return str(value)[:14] + except (ValueError, TypeError) as err: + _LOGGER.warning( + 'Could not expose %s %s value "%s" to KNX: Conversion failed: %s', + self.entity_id, + self.expose_attribute or "state", + value, + err, + ) + return None + return value # type: ignore[no-any-return] async def _async_entity_changed(self, event: Event[EventStateChangedData]) -> None: """Handle entity change.""" diff --git a/homeassistant/components/knx/icons.json b/homeassistant/components/knx/icons.json index 2aee34219f66f4..756b6ab9f9e4e0 100644 --- a/homeassistant/components/knx/icons.json +++ b/homeassistant/components/knx/icons.json @@ -36,10 +36,20 @@ } }, "services": { - "send": "mdi:email-arrow-right", - "read": "mdi:email-search", - "event_register": "mdi:home-import-outline", - "exposure_register": "mdi:home-export-outline", - "reload": "mdi:reload" + "send": { + "service": "mdi:email-arrow-right" + }, + "read": { + "service": "mdi:email-search" + }, + "event_register": { + "service": "mdi:home-import-outline" + }, + "exposure_register": { + "service": "mdi:home-export-outline" + }, + "reload": { + "service": "mdi:reload" + } } } diff --git a/homeassistant/components/knx/manifest.json b/homeassistant/components/knx/manifest.json index 9ecf687d6b9d7a..181dca6f4b8e6d 100644 --- a/homeassistant/components/knx/manifest.json +++ b/homeassistant/components/knx/manifest.json @@ -11,9 +11,9 @@ "loggers": ["xknx", "xknxproject"], "quality_scale": "platinum", "requirements": [ - "xknx==3.1.0", + "xknx==3.1.1", "xknxproject==3.7.1", - "knx-frontend==2024.8.9.225351" + "knx-frontend==2024.9.4.64538" ], "single_config_entry": true } diff --git a/homeassistant/components/knx/project.py b/homeassistant/components/knx/project.py index b5bafe0072489f..04cac68aab076b 100644 --- a/homeassistant/components/knx/project.py +++ b/homeassistant/components/knx/project.py @@ -8,12 +8,13 @@ from xknx import XKNX from xknx.dpt import DPTBase -from xknx.telegram.address import DeviceAddressableType +from xknx.telegram.address import DeviceAddressableType, GroupAddress, GroupAddressType from xknxproject import XKNXProj from xknxproject.models import ( Device, DPTType, GroupAddress as GroupAddressModel, + GroupAddressStyle as XknxProjectGroupAddressStyle, KNXProject as KNXProjectModel, ProjectInfo, ) @@ -90,6 +91,7 @@ async def load_project( if project := data or await self._store.async_load(): self.devices = project["devices"] self.info = project["info"] + GroupAddress.address_format = self.get_address_format() xknx.group_address_dpt.clear() xknx_ga_dict: dict[DeviceAddressableType, DPTType] = {} @@ -133,3 +135,13 @@ async def remove_project_file(self) -> None: async def get_knxproject(self) -> KNXProjectModel | None: """Load the project file from local storage.""" return await self._store.async_load() + + def get_address_format(self) -> GroupAddressType: + """Return the address format for group addresses used in the project.""" + if self.info: + match self.info["group_address_style"]: + case XknxProjectGroupAddressStyle.TWOLEVEL.value: + return GroupAddressType.SHORT + case XknxProjectGroupAddressStyle.FREE.value: + return GroupAddressType.FREE + return GroupAddressType.LONG diff --git a/homeassistant/components/knx/telegrams.py b/homeassistant/components/knx/telegrams.py index a96d841a07d8f5..f4b31fd11f950c 100644 --- a/homeassistant/components/knx/telegrams.py +++ b/homeassistant/components/knx/telegrams.py @@ -9,7 +9,7 @@ from xknx.dpt import DPTArray, DPTBase, DPTBinary from xknx.dpt.dpt import DPTComplexData, DPTEnumData from xknx.exceptions import XKNXException -from xknx.telegram import Telegram +from xknx.telegram import Telegram, TelegramDirection from xknx.telegram.apci import GroupValueResponse, GroupValueWrite from homeassistant.core import HomeAssistant @@ -119,6 +119,8 @@ def telegram_to_dict(self, telegram: Telegram) -> TelegramDict: device := self.project.devices.get(f"{telegram.source_address}") ) is not None: src_name = f"{device['manufacturer_name']} {device['name']}" + elif telegram.direction is TelegramDirection.OUTGOING: + src_name = "Home Assistant" if isinstance(telegram.payload, (GroupValueWrite, GroupValueResponse)): payload_data = telegram.payload.value.value diff --git a/homeassistant/components/knx/websocket.py b/homeassistant/components/knx/websocket.py index 4af3012741a89d..5c21a941484755 100644 --- a/homeassistant/components/knx/websocket.py +++ b/homeassistant/components/knx/websocket.py @@ -2,7 +2,10 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Final +import asyncio +from collections.abc import Awaitable, Callable +from functools import wraps +from typing import TYPE_CHECKING, Any, Final, overload import knx_frontend as knx_panel import voluptuous as vol @@ -77,21 +80,92 @@ async def register_panel(hass: HomeAssistant) -> None: ) +type KnxWebSocketCommandHandler = Callable[ + [HomeAssistant, KNXModule, websocket_api.ActiveConnection, dict[str, Any]], None +] +type KnxAsyncWebSocketCommandHandler = Callable[ + [HomeAssistant, KNXModule, websocket_api.ActiveConnection, dict[str, Any]], + Awaitable[None], +] + + +@overload +def provide_knx( + func: KnxAsyncWebSocketCommandHandler, +) -> websocket_api.const.AsyncWebSocketCommandHandler: ... +@overload +def provide_knx( + func: KnxWebSocketCommandHandler, +) -> websocket_api.const.WebSocketCommandHandler: ... + + +def provide_knx( + func: KnxAsyncWebSocketCommandHandler | KnxWebSocketCommandHandler, +) -> ( + websocket_api.const.AsyncWebSocketCommandHandler + | websocket_api.const.WebSocketCommandHandler +): + """Websocket decorator to provide a KNXModule instance.""" + + def _send_not_loaded_error( + connection: websocket_api.ActiveConnection, msg_id: int + ) -> None: + connection.send_error( + msg_id, + websocket_api.const.ERR_HOME_ASSISTANT_ERROR, + "KNX integration not loaded.", + ) + + if asyncio.iscoroutinefunction(func): + + @wraps(func) + async def with_knx( + hass: HomeAssistant, + connection: websocket_api.ActiveConnection, + msg: dict[str, Any], + ) -> None: + """Add KNX Module to call function.""" + try: + knx: KNXModule = hass.data[DOMAIN] + except KeyError: + _send_not_loaded_error(connection, msg["id"]) + return + await func(hass, knx, connection, msg) + + else: + + @wraps(func) + def with_knx( + hass: HomeAssistant, + connection: websocket_api.ActiveConnection, + msg: dict[str, Any], + ) -> None: + """Add KNX Module to call function.""" + try: + knx: KNXModule = hass.data[DOMAIN] + except KeyError: + _send_not_loaded_error(connection, msg["id"]) + return + func(hass, knx, connection, msg) + + return with_knx + + @websocket_api.require_admin @websocket_api.websocket_command( { vol.Required("type"): "knx/info", } ) +@provide_knx @callback def ws_info( hass: HomeAssistant, + knx: KNXModule, connection: websocket_api.ActiveConnection, msg: dict, ) -> None: """Handle get info command.""" - knx: KNXModule = hass.data[DOMAIN] - _project_info = None if project_info := knx.project.info: _project_info = { @@ -119,13 +193,14 @@ def ws_info( } ) @websocket_api.async_response +@provide_knx async def ws_get_knx_project( hass: HomeAssistant, + knx: KNXModule, connection: websocket_api.ActiveConnection, msg: dict, ) -> None: """Handle get KNX project.""" - knx: KNXModule = hass.data[DOMAIN] knxproject = await knx.project.get_knxproject() connection.send_result( msg["id"], @@ -145,13 +220,14 @@ async def ws_get_knx_project( } ) @websocket_api.async_response +@provide_knx async def ws_project_file_process( hass: HomeAssistant, + knx: KNXModule, connection: websocket_api.ActiveConnection, msg: dict, ) -> None: """Handle get info command.""" - knx: KNXModule = hass.data[DOMAIN] try: await knx.project.process_project_file( xknx=knx.xknx, @@ -175,13 +251,14 @@ async def ws_project_file_process( } ) @websocket_api.async_response +@provide_knx async def ws_project_file_remove( hass: HomeAssistant, + knx: KNXModule, connection: websocket_api.ActiveConnection, msg: dict, ) -> None: """Handle get info command.""" - knx: KNXModule = hass.data[DOMAIN] await knx.project.remove_project_file() connection.send_result(msg["id"]) @@ -192,14 +269,15 @@ async def ws_project_file_remove( vol.Required("type"): "knx/group_monitor_info", } ) +@provide_knx @callback def ws_group_monitor_info( hass: HomeAssistant, + knx: KNXModule, connection: websocket_api.ActiveConnection, msg: dict, ) -> None: """Handle get info command of group monitor.""" - knx: KNXModule = hass.data[DOMAIN] recent_telegrams = [*knx.telegrams.recent_telegrams] connection.send_result( msg["id"], @@ -272,8 +350,10 @@ def ws_validate_entity( } ) @websocket_api.async_response +@provide_knx async def ws_create_entity( hass: HomeAssistant, + knx: KNXModule, connection: websocket_api.ActiveConnection, msg: dict, ) -> None: @@ -283,7 +363,6 @@ async def ws_create_entity( except EntityStoreValidationException as exc: connection.send_result(msg["id"], exc.validation_error) return - knx: KNXModule = hass.data[DOMAIN] try: entity_id = await knx.config_store.create_entity( # use validation result so defaults are applied @@ -308,8 +387,10 @@ async def ws_create_entity( } ) @websocket_api.async_response +@provide_knx async def ws_update_entity( hass: HomeAssistant, + knx: KNXModule, connection: websocket_api.ActiveConnection, msg: dict, ) -> None: @@ -319,7 +400,6 @@ async def ws_update_entity( except EntityStoreValidationException as exc: connection.send_result(msg["id"], exc.validation_error) return - knx: KNXModule = hass.data[DOMAIN] try: await knx.config_store.update_entity( validated_data[CONF_PLATFORM], @@ -344,13 +424,14 @@ async def ws_update_entity( } ) @websocket_api.async_response +@provide_knx async def ws_delete_entity( hass: HomeAssistant, + knx: KNXModule, connection: websocket_api.ActiveConnection, msg: dict, ) -> None: """Delete entity from entity store and remove it.""" - knx: KNXModule = hass.data[DOMAIN] try: await knx.config_store.delete_entity(msg[CONF_ENTITY_ID]) except ConfigStoreException as err: @@ -367,14 +448,15 @@ async def ws_delete_entity( vol.Required("type"): "knx/get_entity_entries", } ) +@provide_knx @callback def ws_get_entity_entries( hass: HomeAssistant, + knx: KNXModule, connection: websocket_api.ActiveConnection, msg: dict, ) -> None: """Get entities configured from entity store.""" - knx: KNXModule = hass.data[DOMAIN] entity_entries = [ entry.extended_dict for entry in knx.config_store.get_entity_entries() ] @@ -388,14 +470,15 @@ def ws_get_entity_entries( vol.Required(CONF_ENTITY_ID): str, } ) +@provide_knx @callback def ws_get_entity_config( hass: HomeAssistant, + knx: KNXModule, connection: websocket_api.ActiveConnection, msg: dict, ) -> None: """Get entity configuration from entity store.""" - knx: KNXModule = hass.data[DOMAIN] try: config_info = knx.config_store.get_entity_config(msg[CONF_ENTITY_ID]) except ConfigStoreException as err: @@ -414,14 +497,15 @@ def ws_get_entity_config( vol.Optional("area_id"): str, } ) +@provide_knx @callback def ws_create_device( hass: HomeAssistant, + knx: KNXModule, connection: websocket_api.ActiveConnection, msg: dict, ) -> None: """Create a new KNX device.""" - knx: KNXModule = hass.data[DOMAIN] identifier = f"knx_vdev_{ulid_now()}" device_registry = dr.async_get(hass) _device = device_registry.async_get_or_create( diff --git a/homeassistant/components/kodi/config_flow.py b/homeassistant/components/kodi/config_flow.py index e431c72d21e156..ef0798220ddee2 100644 --- a/homeassistant/components/kodi/config_flow.py +++ b/homeassistant/components/kodi/config_flow.py @@ -3,6 +3,7 @@ from __future__ import annotations import logging +from typing import Any from pykodi import CannotConnectError, InvalidAuthError, Kodi, get_kodi_connection import voluptuous as vol @@ -139,7 +140,9 @@ async def async_step_zeroconf( return await self.async_step_discovery_confirm() - async def async_step_discovery_confirm(self, user_input=None): + async def async_step_discovery_confirm( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle user-confirmation of discovered node.""" if user_input is None: return self.async_show_form( @@ -149,7 +152,9 @@ async def async_step_discovery_confirm(self, user_input=None): return self._create_entry() - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" errors = {} @@ -175,7 +180,9 @@ async def async_step_user(self, user_input=None): return self._show_user_form(errors) - async def async_step_credentials(self, user_input=None): + async def async_step_credentials( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle username and password input.""" errors = {} @@ -200,7 +207,9 @@ async def async_step_credentials(self, user_input=None): return self._show_credentials_form(errors) - async def async_step_ws_port(self, user_input=None): + async def async_step_ws_port( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle websocket port of discovered node.""" errors = {} @@ -223,12 +232,12 @@ async def async_step_ws_port(self, user_input=None): return self._show_ws_port_form(errors) - async def async_step_import(self, data): + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Handle import from YAML.""" reason = None try: - await validate_http(self.hass, data) - await validate_ws(self.hass, data) + await validate_http(self.hass, import_data) + await validate_ws(self.hass, import_data) except InvalidAuth: _LOGGER.exception("Invalid Kodi credentials") reason = "invalid_auth" @@ -239,12 +248,16 @@ async def async_step_import(self, data): _LOGGER.exception("Unexpected exception") reason = "unknown" else: - return self.async_create_entry(title=data[CONF_NAME], data=data) + return self.async_create_entry( + title=import_data[CONF_NAME], data=import_data + ) return self.async_abort(reason=reason) @callback - def _show_credentials_form(self, errors=None): + def _show_credentials_form( + self, errors: dict[str, str] | None = None + ) -> ConfigFlowResult: schema = vol.Schema( { vol.Optional( @@ -257,7 +270,7 @@ def _show_credentials_form(self, errors=None): ) return self.async_show_form( - step_id="credentials", data_schema=schema, errors=errors or {} + step_id="credentials", data_schema=schema, errors=errors ) @callback @@ -299,7 +312,7 @@ def _create_entry(self): ) @callback - def _get_data(self): + def _get_data(self) -> dict[str, Any]: return { CONF_NAME: self._name, CONF_HOST: self._host, diff --git a/homeassistant/components/kodi/icons.json b/homeassistant/components/kodi/icons.json index 07bd246e92d6fe..d9c32630961b88 100644 --- a/homeassistant/components/kodi/icons.json +++ b/homeassistant/components/kodi/icons.json @@ -1,6 +1,10 @@ { "services": { - "add_to_playlist": "mdi:playlist-plus", - "call_method": "mdi:console" + "add_to_playlist": { + "service": "mdi:playlist-plus" + }, + "call_method": { + "service": "mdi:console" + } } } diff --git a/homeassistant/components/konnected/config_flow.py b/homeassistant/components/konnected/config_flow.py index 29f4fbe2a491ea..18e113e146b246 100644 --- a/homeassistant/components/konnected/config_flow.py +++ b/homeassistant/components/konnected/config_flow.py @@ -7,7 +7,7 @@ import logging import random import string -from typing import Any +from typing import TYPE_CHECKING, Any from urllib.parse import urlparse import voluptuous as vol @@ -202,24 +202,24 @@ async def async_gen_config(self, host, port): random.choices(f"{string.ascii_uppercase}{string.digits}", k=20) ) - async def async_step_import(self, device_config): + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Import a configuration.yaml config. This flow is triggered by `async_setup` for configured panels. """ - _LOGGER.debug(device_config) + _LOGGER.debug(import_data) # save the data and confirm connection via user step - await self.async_set_unique_id(device_config["id"]) - self.options = device_config[CONF_DEFAULT_OPTIONS] + await self.async_set_unique_id(import_data["id"]) + self.options = import_data[CONF_DEFAULT_OPTIONS] # config schema ensures we have port if we have host - if device_config.get(CONF_HOST): + if import_data.get(CONF_HOST): # automatically connect if we have host info return await self.async_step_user( user_input={ - CONF_HOST: device_config[CONF_HOST], - CONF_PORT: device_config[CONF_PORT], + CONF_HOST: import_data[CONF_HOST], + CONF_PORT: import_data[CONF_PORT], } ) @@ -227,8 +227,12 @@ async def async_step_import(self, device_config): self._abort_if_unique_id_configured() return await self.async_step_import_confirm() - async def async_step_import_confirm(self, user_input=None): + async def async_step_import_confirm( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Confirm the user wants to import the config entry.""" + if TYPE_CHECKING: + assert self.unique_id is not None if user_input is None: return self.async_show_form( step_id="import_confirm", @@ -303,7 +307,9 @@ async def async_step_ssdp( return self.async_abort(reason="unknown") - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Connect to panel and get config.""" errors = {} if user_input: @@ -347,7 +353,9 @@ async def async_step_user(self, user_input=None): errors=errors, ) - async def async_step_confirm(self, user_input=None): + async def async_step_confirm( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Attempt to link with the Konnected panel. Given a configured host, will ask the user to confirm and finalize @@ -399,8 +407,8 @@ def __init__(self, config_entry: ConfigEntry) -> None: self.current_opt = self.entry.options or self.entry.data[CONF_DEFAULT_OPTIONS] # as config proceeds we'll build up new options and then replace what's in the config entry - self.new_opt: dict[str, dict[str, Any]] = {CONF_IO: {}} - self.active_cfg = None + self.new_opt: dict[str, Any] = {CONF_IO: {}} + self.active_cfg: str | None = None self.io_cfg: dict[str, Any] = {} self.current_states: list[dict[str, Any]] = [] self.current_state = 1 @@ -417,13 +425,17 @@ def get_current_cfg(self, io_type, zone): {}, ) - async def async_step_init(self, user_input=None): + async def async_step_init( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle options flow.""" return await self.async_step_options_io() - async def async_step_options_io(self, user_input=None): + async def async_step_options_io( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Configure legacy panel IO or first half of pro IO.""" - errors = {} + errors: dict[str, str] = {} current_io = self.current_opt.get(CONF_IO, {}) if user_input is not None: @@ -506,9 +518,11 @@ async def async_step_options_io(self, user_input=None): return self.async_abort(reason="not_konn_panel") - async def async_step_options_io_ext(self, user_input=None): + async def async_step_options_io_ext( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Allow the user to configure the extended IO for pro.""" - errors = {} + errors: dict[str, str] = {} current_io = self.current_opt.get(CONF_IO, {}) if user_input is not None: @@ -564,10 +578,12 @@ async def async_step_options_io_ext(self, user_input=None): return self.async_abort(reason="not_konn_panel") - async def async_step_options_binary(self, user_input=None): + async def async_step_options_binary( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Allow the user to configure the IO options for binary sensors.""" - errors = {} - if user_input is not None: + errors: dict[str, str] = {} + if user_input is not None and self.active_cfg is not None: zone = {"zone": self.active_cfg} zone.update(user_input) self.new_opt[CONF_BINARY_SENSORS] = [ @@ -600,7 +616,7 @@ async def async_step_options_binary(self, user_input=None): description_placeholders={ "zone": f"Zone {self.active_cfg}" if len(self.active_cfg) < 3 - else self.active_cfg.upper + else self.active_cfg.upper() }, errors=errors, ) @@ -633,17 +649,19 @@ async def async_step_options_binary(self, user_input=None): description_placeholders={ "zone": f"Zone {self.active_cfg}" if len(self.active_cfg) < 3 - else self.active_cfg.upper + else self.active_cfg.upper() }, errors=errors, ) return await self.async_step_options_digital() - async def async_step_options_digital(self, user_input=None): + async def async_step_options_digital( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Allow the user to configure the IO options for digital sensors.""" - errors = {} - if user_input is not None: + errors: dict[str, str] = {} + if user_input is not None and self.active_cfg is not None: zone = {"zone": self.active_cfg} zone.update(user_input) self.new_opt[CONF_SENSORS] = [*self.new_opt.get(CONF_SENSORS, []), zone] @@ -708,10 +726,12 @@ async def async_step_options_digital(self, user_input=None): return await self.async_step_options_switch() - async def async_step_options_switch(self, user_input=None): + async def async_step_options_switch( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Allow the user to configure the IO options for switches.""" - errors = {} - if user_input is not None: + errors: dict[str, str] = {} + if user_input is not None and self.active_cfg is not None: zone = {"zone": self.active_cfg} zone.update(user_input) del zone[CONF_MORE_STATES] @@ -823,7 +843,9 @@ async def async_step_options_switch(self, user_input=None): return await self.async_step_options_misc() - async def async_step_options_misc(self, user_input=None): + async def async_step_options_misc( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Allow the user to configure the LED behavior.""" errors = {} if user_input is not None: diff --git a/homeassistant/components/kostal_plenticore/config_flow.py b/homeassistant/components/kostal_plenticore/config_flow.py index 547afa9d71ba8b..59c737a08746fc 100644 --- a/homeassistant/components/kostal_plenticore/config_flow.py +++ b/homeassistant/components/kostal_plenticore/config_flow.py @@ -1,12 +1,13 @@ """Config flow for Kostal Plenticore Solar Inverter integration.""" import logging +from typing import Any from aiohttp.client_exceptions import ClientError from pykoplenti import ApiClient, AuthenticationException import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_BASE, CONF_HOST, CONF_PASSWORD from homeassistant.core import HomeAssistant from homeassistant.helpers.aiohttp_client import async_get_clientsession @@ -44,10 +45,11 @@ class KostalPlenticoreConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" errors = {} - hostname = None if user_input is not None: self._async_abort_entries_match({CONF_HOST: user_input[CONF_HOST]}) @@ -62,8 +64,7 @@ async def async_step_user(self, user_input=None): except Exception: _LOGGER.exception("Unexpected exception") errors[CONF_BASE] = "unknown" - - if not errors: + else: return self.async_create_entry(title=hostname, data=user_input) return self.async_show_form( diff --git a/homeassistant/components/lamarzocco/__init__.py b/homeassistant/components/lamarzocco/__init__.py index dfcaa54047df7d..02e47ecd78e70a 100644 --- a/homeassistant/components/lamarzocco/__init__.py +++ b/homeassistant/components/lamarzocco/__init__.py @@ -53,6 +53,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: LaMarzoccoConfigEntry) - cloud_client = LaMarzoccoCloudClient( username=entry.data[CONF_USERNAME], password=entry.data[CONF_PASSWORD], + client=get_async_client(hass), ) # initialize local API diff --git a/homeassistant/components/lamarzocco/manifest.json b/homeassistant/components/lamarzocco/manifest.json index 73d14250525754..181a2b9ab9bdab 100644 --- a/homeassistant/components/lamarzocco/manifest.json +++ b/homeassistant/components/lamarzocco/manifest.json @@ -22,5 +22,5 @@ "integration_type": "device", "iot_class": "cloud_polling", "loggers": ["lmcloud"], - "requirements": ["lmcloud==1.1.13"] + "requirements": ["lmcloud==1.2.2"] } diff --git a/homeassistant/components/lametric/icons.json b/homeassistant/components/lametric/icons.json index 7e1841272cfab7..229770c96dc85d 100644 --- a/homeassistant/components/lametric/icons.json +++ b/homeassistant/components/lametric/icons.json @@ -39,7 +39,11 @@ } }, "services": { - "chart": "mdi:chart-areaspline-variant", - "message": "mdi:message" + "chart": { + "service": "mdi:chart-areaspline-variant" + }, + "message": { + "service": "mdi:message" + } } } diff --git a/homeassistant/components/lawn_mower/const.py b/homeassistant/components/lawn_mower/const.py index e060abe642340c..231be83ed887ca 100644 --- a/homeassistant/components/lawn_mower/const.py +++ b/homeassistant/components/lawn_mower/const.py @@ -18,6 +18,9 @@ class LawnMowerActivity(StrEnum): DOCKED = "docked" """Device is docked.""" + RETURNING = "returning" + """Device is returning.""" + class LawnMowerEntityFeature(IntFlag): """Supported features of the lawn mower entity.""" diff --git a/homeassistant/components/lawn_mower/icons.json b/homeassistant/components/lawn_mower/icons.json index b25bf927fcdc9e..2fa1f79efa1182 100644 --- a/homeassistant/components/lawn_mower/icons.json +++ b/homeassistant/components/lawn_mower/icons.json @@ -5,8 +5,14 @@ } }, "services": { - "dock": "mdi:home-import-outline", - "pause": "mdi:pause", - "start_mowing": "mdi:play" + "dock": { + "service": "mdi:home-import-outline" + }, + "pause": { + "service": "mdi:pause" + }, + "start_mowing": { + "service": "mdi:play" + } } } diff --git a/homeassistant/components/lawn_mower/strings.json b/homeassistant/components/lawn_mower/strings.json index 15ed50ca6c5cd3..ebaea4ffd6a43d 100644 --- a/homeassistant/components/lawn_mower/strings.json +++ b/homeassistant/components/lawn_mower/strings.json @@ -7,7 +7,8 @@ "error": "Error", "paused": "[%key:common::state::paused%]", "mowing": "Mowing", - "docked": "Docked" + "docked": "Docked", + "returning": "Returning" } } }, diff --git a/homeassistant/components/lcn/binary_sensor.py b/homeassistant/components/lcn/binary_sensor.py index d7e5798bb912fc..a0f8e1cf360746 100644 --- a/homeassistant/components/lcn/binary_sensor.py +++ b/homeassistant/components/lcn/binary_sensor.py @@ -1,6 +1,7 @@ """Support for LCN binary sensors.""" -from __future__ import annotations +from collections.abc import Iterable +from functools import partial import pypck @@ -25,22 +26,37 @@ from .helpers import DeviceConnectionType, InputType, get_device_connection -def create_lcn_binary_sensor_entity( - hass: HomeAssistant, entity_config: ConfigType, config_entry: ConfigEntry -) -> LcnEntity: - """Set up an entity for this domain.""" - device_connection = get_device_connection( - hass, entity_config[CONF_ADDRESS], config_entry - ) - - if entity_config[CONF_DOMAIN_DATA][CONF_SOURCE] in SETPOINTS: - return LcnRegulatorLockSensor( - entity_config, config_entry.entry_id, device_connection +def add_lcn_entities( + hass: HomeAssistant, + config_entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, + entity_configs: Iterable[ConfigType], +) -> None: + """Add entities for this domain.""" + entities: list[LcnRegulatorLockSensor | LcnBinarySensor | LcnLockKeysSensor] = [] + for entity_config in entity_configs: + device_connection = get_device_connection( + hass, entity_config[CONF_ADDRESS], config_entry ) - if entity_config[CONF_DOMAIN_DATA][CONF_SOURCE] in BINSENSOR_PORTS: - return LcnBinarySensor(entity_config, config_entry.entry_id, device_connection) - # in KEY - return LcnLockKeysSensor(entity_config, config_entry.entry_id, device_connection) + + if entity_config[CONF_DOMAIN_DATA][CONF_SOURCE] in SETPOINTS: + entities.append( + LcnRegulatorLockSensor( + entity_config, config_entry.entry_id, device_connection + ) + ) + elif entity_config[CONF_DOMAIN_DATA][CONF_SOURCE] in BINSENSOR_PORTS: + entities.append( + LcnBinarySensor(entity_config, config_entry.entry_id, device_connection) + ) + else: # in KEY + entities.append( + LcnLockKeysSensor( + entity_config, config_entry.entry_id, device_connection + ) + ) + + async_add_entities(entities) async def async_setup_entry( @@ -49,14 +65,23 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """Set up LCN switch entities from a config entry.""" + add_entities = partial( + add_lcn_entities, + hass, + config_entry, + async_add_entities, + ) + hass.data[DOMAIN][config_entry.entry_id][ADD_ENTITIES_CALLBACKS].update( - {DOMAIN_BINARY_SENSOR: (async_add_entities, create_lcn_binary_sensor_entity)} + {DOMAIN_BINARY_SENSOR: add_entities} ) - async_add_entities( - create_lcn_binary_sensor_entity(hass, entity_config, config_entry) - for entity_config in config_entry.data[CONF_ENTITIES] - if entity_config[CONF_DOMAIN] == DOMAIN_BINARY_SENSOR + add_entities( + ( + entity_config + for entity_config in config_entry.data[CONF_ENTITIES] + if entity_config[CONF_DOMAIN] == DOMAIN_BINARY_SENSOR + ), ) diff --git a/homeassistant/components/lcn/climate.py b/homeassistant/components/lcn/climate.py index d34a872d867338..0142894a16bd7f 100644 --- a/homeassistant/components/lcn/climate.py +++ b/homeassistant/components/lcn/climate.py @@ -1,7 +1,7 @@ """Support for LCN climate control.""" -from __future__ import annotations - +from collections.abc import Iterable +from functools import partial from typing import Any, cast import pypck @@ -41,15 +41,24 @@ PARALLEL_UPDATES = 0 -def create_lcn_climate_entity( - hass: HomeAssistant, entity_config: ConfigType, config_entry: ConfigEntry -) -> LcnEntity: - """Set up an entity for this domain.""" - device_connection = get_device_connection( - hass, entity_config[CONF_ADDRESS], config_entry - ) +def add_lcn_entities( + hass: HomeAssistant, + config_entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, + entity_configs: Iterable[ConfigType], +) -> None: + """Add entities for this domain.""" + entities: list[LcnClimate] = [] + for entity_config in entity_configs: + device_connection = get_device_connection( + hass, entity_config[CONF_ADDRESS], config_entry + ) + + entities.append( + LcnClimate(entity_config, config_entry.entry_id, device_connection) + ) - return LcnClimate(entity_config, config_entry.entry_id, device_connection) + async_add_entities(entities) async def async_setup_entry( @@ -58,14 +67,23 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """Set up LCN switch entities from a config entry.""" + add_entities = partial( + add_lcn_entities, + hass, + config_entry, + async_add_entities, + ) + hass.data[DOMAIN][config_entry.entry_id][ADD_ENTITIES_CALLBACKS].update( - {DOMAIN_CLIMATE: (async_add_entities, create_lcn_climate_entity)} + {DOMAIN_CLIMATE: add_entities} ) - async_add_entities( - create_lcn_climate_entity(hass, entity_config, config_entry) - for entity_config in config_entry.data[CONF_ENTITIES] - if entity_config[CONF_DOMAIN] == DOMAIN_CLIMATE + add_entities( + ( + entity_config + for entity_config in config_entry.data[CONF_ENTITIES] + if entity_config[CONF_DOMAIN] == DOMAIN_CLIMATE + ), ) diff --git a/homeassistant/components/lcn/config_flow.py b/homeassistant/components/lcn/config_flow.py index 664f32e55856cd..c38a16cc21eb1d 100644 --- a/homeassistant/components/lcn/config_flow.py +++ b/homeassistant/components/lcn/config_flow.py @@ -9,6 +9,7 @@ import voluptuous as vol from homeassistant import config_entries +from homeassistant.config_entries import ConfigFlowResult from homeassistant.const import ( CONF_BASE, CONF_DEVICES, @@ -107,12 +108,10 @@ class LcnFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): VERSION = 1 - async def async_step_import( - self, data: ConfigType - ) -> config_entries.ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Import existing configuration from LCN.""" # validate the imported connection parameters - if error := await validate_connection(data): + if error := await validate_connection(import_data): async_create_issue( self.hass, DOMAIN, @@ -144,17 +143,19 @@ async def async_step_import( ) # check if we already have a host with the same address configured - if entry := get_config_entry(self.hass, data): + if entry := get_config_entry(self.hass, import_data): entry.source = config_entries.SOURCE_IMPORT # Cleanup entity and device registry, if we imported from configuration.yaml to # remove orphans when entities were removed from configuration - purge_entity_registry(self.hass, entry.entry_id, data) - purge_device_registry(self.hass, entry.entry_id, data) + purge_entity_registry(self.hass, entry.entry_id, import_data) + purge_device_registry(self.hass, entry.entry_id, import_data) - self.hass.config_entries.async_update_entry(entry, data=data) + self.hass.config_entries.async_update_entry(entry, data=import_data) return self.async_abort(reason="existing_configuration_updated") - return self.async_create_entry(title=f"{data[CONF_HOST]}", data=data) + return self.async_create_entry( + title=f"{import_data[CONF_HOST]}", data=import_data + ) async def async_step_user( self, user_input: dict[str, Any] | None = None diff --git a/homeassistant/components/lcn/cover.py b/homeassistant/components/lcn/cover.py index a2f508cee97262..1e428a350d6a59 100644 --- a/homeassistant/components/lcn/cover.py +++ b/homeassistant/components/lcn/cover.py @@ -1,7 +1,7 @@ """Support for LCN covers.""" -from __future__ import annotations - +from collections.abc import Iterable +from functools import partial from typing import Any import pypck @@ -26,18 +26,29 @@ PARALLEL_UPDATES = 0 -def create_lcn_cover_entity( - hass: HomeAssistant, entity_config: ConfigType, config_entry: ConfigEntry -) -> LcnEntity: - """Set up an entity for this domain.""" - device_connection = get_device_connection( - hass, entity_config[CONF_ADDRESS], config_entry - ) +def add_lcn_entities( + hass: HomeAssistant, + config_entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, + entity_configs: Iterable[ConfigType], +) -> None: + """Add entities for this domain.""" + entities: list[LcnOutputsCover | LcnRelayCover] = [] + for entity_config in entity_configs: + device_connection = get_device_connection( + hass, entity_config[CONF_ADDRESS], config_entry + ) + + if entity_config[CONF_DOMAIN_DATA][CONF_MOTOR] in "OUTPUTS": + entities.append( + LcnOutputsCover(entity_config, config_entry.entry_id, device_connection) + ) + else: # in RELAYS + entities.append( + LcnRelayCover(entity_config, config_entry.entry_id, device_connection) + ) - if entity_config[CONF_DOMAIN_DATA][CONF_MOTOR] in "OUTPUTS": - return LcnOutputsCover(entity_config, config_entry.entry_id, device_connection) - # in RELAYS - return LcnRelayCover(entity_config, config_entry.entry_id, device_connection) + async_add_entities(entities) async def async_setup_entry( @@ -46,14 +57,23 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """Set up LCN cover entities from a config entry.""" + add_entities = partial( + add_lcn_entities, + hass, + config_entry, + async_add_entities, + ) + hass.data[DOMAIN][config_entry.entry_id][ADD_ENTITIES_CALLBACKS].update( - {DOMAIN_COVER: (async_add_entities, create_lcn_cover_entity)} + {DOMAIN_COVER: add_entities} ) - async_add_entities( - create_lcn_cover_entity(hass, entity_config, config_entry) - for entity_config in config_entry.data[CONF_ENTITIES] - if entity_config[CONF_DOMAIN] == DOMAIN_COVER + add_entities( + ( + entity_config + for entity_config in config_entry.data[CONF_ENTITIES] + if entity_config[CONF_DOMAIN] == DOMAIN_COVER + ), ) diff --git a/homeassistant/components/lcn/icons.json b/homeassistant/components/lcn/icons.json index c8b451a79ea3b7..944c3938a92112 100644 --- a/homeassistant/components/lcn/icons.json +++ b/homeassistant/components/lcn/icons.json @@ -1,17 +1,43 @@ { "services": { - "output_abs": "mdi:brightness-auto", - "output_rel": "mdi:brightness-7", - "output_toggle": "mdi:toggle-switch", - "relays": "mdi:light-switch-off", - "led": "mdi:led-on", - "var_abs": "mdi:wrench", - "var_reset": "mdi:reload", - "var_rel": "mdi:wrench", - "lock_regulator": "mdi:lock", - "send_keys": "mdi:alarm-panel", - "lock_keys": "mdi:lock", - "dyn_text": "mdi:form-textbox", - "pck": "mdi:package-variant-closed" + "output_abs": { + "service": "mdi:brightness-auto" + }, + "output_rel": { + "service": "mdi:brightness-7" + }, + "output_toggle": { + "service": "mdi:toggle-switch" + }, + "relays": { + "service": "mdi:light-switch-off" + }, + "led": { + "service": "mdi:led-on" + }, + "var_abs": { + "service": "mdi:wrench" + }, + "var_reset": { + "service": "mdi:reload" + }, + "var_rel": { + "service": "mdi:wrench" + }, + "lock_regulator": { + "service": "mdi:lock" + }, + "send_keys": { + "service": "mdi:alarm-panel" + }, + "lock_keys": { + "service": "mdi:lock" + }, + "dyn_text": { + "service": "mdi:form-textbox" + }, + "pck": { + "service": "mdi:package-variant-closed" + } } } diff --git a/homeassistant/components/lcn/light.py b/homeassistant/components/lcn/light.py index b896462c8a1fb6..799ed0036d8933 100644 --- a/homeassistant/components/lcn/light.py +++ b/homeassistant/components/lcn/light.py @@ -1,7 +1,7 @@ """Support for LCN lights.""" -from __future__ import annotations - +from collections.abc import Iterable +from functools import partial from typing import Any import pypck @@ -35,18 +35,29 @@ PARALLEL_UPDATES = 0 -def create_lcn_light_entity( - hass: HomeAssistant, entity_config: ConfigType, config_entry: ConfigEntry -) -> LcnEntity: - """Set up an entity for this domain.""" - device_connection = get_device_connection( - hass, entity_config[CONF_ADDRESS], config_entry - ) +def add_lcn_entities( + hass: HomeAssistant, + config_entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, + entity_configs: Iterable[ConfigType], +) -> None: + """Add entities for this domain.""" + entities: list[LcnOutputLight | LcnRelayLight] = [] + for entity_config in entity_configs: + device_connection = get_device_connection( + hass, entity_config[CONF_ADDRESS], config_entry + ) + + if entity_config[CONF_DOMAIN_DATA][CONF_OUTPUT] in OUTPUT_PORTS: + entities.append( + LcnOutputLight(entity_config, config_entry.entry_id, device_connection) + ) + else: # in RELAY_PORTS + entities.append( + LcnRelayLight(entity_config, config_entry.entry_id, device_connection) + ) - if entity_config[CONF_DOMAIN_DATA][CONF_OUTPUT] in OUTPUT_PORTS: - return LcnOutputLight(entity_config, config_entry.entry_id, device_connection) - # in RELAY_PORTS - return LcnRelayLight(entity_config, config_entry.entry_id, device_connection) + async_add_entities(entities) async def async_setup_entry( @@ -55,14 +66,23 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """Set up LCN light entities from a config entry.""" + add_entities = partial( + add_lcn_entities, + hass, + config_entry, + async_add_entities, + ) + hass.data[DOMAIN][config_entry.entry_id][ADD_ENTITIES_CALLBACKS].update( - {DOMAIN_LIGHT: (async_add_entities, create_lcn_light_entity)} + {DOMAIN_LIGHT: add_entities} ) - async_add_entities( - create_lcn_light_entity(hass, entity_config, config_entry) - for entity_config in config_entry.data[CONF_ENTITIES] - if entity_config[CONF_DOMAIN] == DOMAIN_LIGHT + add_entities( + ( + entity_config + for entity_config in config_entry.data[CONF_ENTITIES] + if entity_config[CONF_DOMAIN] == DOMAIN_LIGHT + ), ) diff --git a/homeassistant/components/lcn/manifest.json b/homeassistant/components/lcn/manifest.json index 44aae34e9e6ab1..f8b7d02b103acb 100644 --- a/homeassistant/components/lcn/manifest.json +++ b/homeassistant/components/lcn/manifest.json @@ -1,10 +1,12 @@ { "domain": "lcn", "name": "LCN", + "after_dependencies": ["panel_custom"], "codeowners": ["@alengwenus"], "config_flow": true, + "dependencies": ["http", "websocket_api"], "documentation": "https://www.home-assistant.io/integrations/lcn", "iot_class": "local_push", "loggers": ["pypck"], - "requirements": ["pypck==0.7.21", "lcn-frontend==0.1.5"] + "requirements": ["pypck==0.7.21", "lcn-frontend==0.1.6"] } diff --git a/homeassistant/components/lcn/scene.py b/homeassistant/components/lcn/scene.py index f9220f676d644b..52ec0262b55d7e 100644 --- a/homeassistant/components/lcn/scene.py +++ b/homeassistant/components/lcn/scene.py @@ -1,7 +1,7 @@ """Support for LCN scenes.""" -from __future__ import annotations - +from collections.abc import Iterable +from functools import partial from typing import Any import pypck @@ -28,15 +28,24 @@ PARALLEL_UPDATES = 0 -def create_lcn_scene_entity( - hass: HomeAssistant, entity_config: ConfigType, config_entry: ConfigEntry -) -> LcnEntity: - """Set up an entity for this domain.""" - device_connection = get_device_connection( - hass, entity_config[CONF_ADDRESS], config_entry - ) +def add_lcn_entities( + hass: HomeAssistant, + config_entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, + entity_configs: Iterable[ConfigType], +) -> None: + """Add entities for this domain.""" + entities: list[LcnScene] = [] + for entity_config in entity_configs: + device_connection = get_device_connection( + hass, entity_config[CONF_ADDRESS], config_entry + ) + + entities.append( + LcnScene(entity_config, config_entry.entry_id, device_connection) + ) - return LcnScene(entity_config, config_entry.entry_id, device_connection) + async_add_entities(entities) async def async_setup_entry( @@ -45,14 +54,23 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """Set up LCN switch entities from a config entry.""" + add_entities = partial( + add_lcn_entities, + hass, + config_entry, + async_add_entities, + ) + hass.data[DOMAIN][config_entry.entry_id][ADD_ENTITIES_CALLBACKS].update( - {DOMAIN_SCENE: (async_add_entities, create_lcn_scene_entity)} + {DOMAIN_SCENE: add_entities} ) - async_add_entities( - create_lcn_scene_entity(hass, entity_config, config_entry) - for entity_config in config_entry.data[CONF_ENTITIES] - if entity_config[CONF_DOMAIN] == DOMAIN_SCENE + add_entities( + ( + entity_config + for entity_config in config_entry.data[CONF_ENTITIES] + if entity_config[CONF_DOMAIN] == DOMAIN_SCENE + ), ) diff --git a/homeassistant/components/lcn/sensor.py b/homeassistant/components/lcn/sensor.py index b63ddbef8ad90e..7e8941a0bf9080 100644 --- a/homeassistant/components/lcn/sensor.py +++ b/homeassistant/components/lcn/sensor.py @@ -1,7 +1,7 @@ """Support for LCN sensors.""" -from __future__ import annotations - +from collections.abc import Iterable +from functools import partial from itertools import chain from typing import cast @@ -34,22 +34,35 @@ from .helpers import DeviceConnectionType, InputType, get_device_connection -def create_lcn_sensor_entity( - hass: HomeAssistant, entity_config: ConfigType, config_entry: ConfigEntry -) -> LcnEntity: - """Set up an entity for this domain.""" - device_connection = get_device_connection( - hass, entity_config[CONF_ADDRESS], config_entry - ) - - if entity_config[CONF_DOMAIN_DATA][CONF_SOURCE] in chain( - VARIABLES, SETPOINTS, THRESHOLDS, S0_INPUTS - ): - return LcnVariableSensor( - entity_config, config_entry.entry_id, device_connection +def add_lcn_entities( + hass: HomeAssistant, + config_entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, + entity_configs: Iterable[ConfigType], +) -> None: + """Add entities for this domain.""" + entities: list[LcnVariableSensor | LcnLedLogicSensor] = [] + for entity_config in entity_configs: + device_connection = get_device_connection( + hass, entity_config[CONF_ADDRESS], config_entry ) - # in LED_PORTS + LOGICOP_PORTS - return LcnLedLogicSensor(entity_config, config_entry.entry_id, device_connection) + + if entity_config[CONF_DOMAIN_DATA][CONF_SOURCE] in chain( + VARIABLES, SETPOINTS, THRESHOLDS, S0_INPUTS + ): + entities.append( + LcnVariableSensor( + entity_config, config_entry.entry_id, device_connection + ) + ) + else: # in LED_PORTS + LOGICOP_PORTS + entities.append( + LcnLedLogicSensor( + entity_config, config_entry.entry_id, device_connection + ) + ) + + async_add_entities(entities) async def async_setup_entry( @@ -58,14 +71,23 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """Set up LCN switch entities from a config entry.""" + add_entities = partial( + add_lcn_entities, + hass, + config_entry, + async_add_entities, + ) + hass.data[DOMAIN][config_entry.entry_id][ADD_ENTITIES_CALLBACKS].update( - {DOMAIN_SENSOR: (async_add_entities, create_lcn_sensor_entity)} + {DOMAIN_SENSOR: add_entities} ) - async_add_entities( - create_lcn_sensor_entity(hass, entity_config, config_entry) - for entity_config in config_entry.data[CONF_ENTITIES] - if entity_config[CONF_DOMAIN] == DOMAIN_SENSOR + add_entities( + ( + entity_config + for entity_config in config_entry.data[CONF_ENTITIES] + if entity_config[CONF_DOMAIN] == DOMAIN_SENSOR + ), ) diff --git a/homeassistant/components/lcn/switch.py b/homeassistant/components/lcn/switch.py index 1136e4c27a1214..4c316cef547c22 100644 --- a/homeassistant/components/lcn/switch.py +++ b/homeassistant/components/lcn/switch.py @@ -1,7 +1,7 @@ """Support for LCN switches.""" -from __future__ import annotations - +from collections.abc import Iterable +from functools import partial from typing import Any import pypck @@ -26,18 +26,29 @@ PARALLEL_UPDATES = 0 -def create_lcn_switch_entity( - hass: HomeAssistant, entity_config: ConfigType, config_entry: ConfigEntry -) -> LcnEntity: - """Set up an entity for this domain.""" - device_connection = get_device_connection( - hass, entity_config[CONF_ADDRESS], config_entry - ) +def add_lcn_switch_entities( + hass: HomeAssistant, + config_entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, + entity_configs: Iterable[ConfigType], +) -> None: + """Add entities for this domain.""" + entities: list[LcnOutputSwitch | LcnRelaySwitch] = [] + for entity_config in entity_configs: + device_connection = get_device_connection( + hass, entity_config[CONF_ADDRESS], config_entry + ) + + if entity_config[CONF_DOMAIN_DATA][CONF_OUTPUT] in OUTPUT_PORTS: + entities.append( + LcnOutputSwitch(entity_config, config_entry.entry_id, device_connection) + ) + else: # in RELAY_PORTS + entities.append( + LcnRelaySwitch(entity_config, config_entry.entry_id, device_connection) + ) - if entity_config[CONF_DOMAIN_DATA][CONF_OUTPUT] in OUTPUT_PORTS: - return LcnOutputSwitch(entity_config, config_entry.entry_id, device_connection) - # in RELAY_PORTS - return LcnRelaySwitch(entity_config, config_entry.entry_id, device_connection) + async_add_entities(entities) async def async_setup_entry( @@ -46,14 +57,23 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """Set up LCN switch entities from a config entry.""" + add_entities = partial( + add_lcn_switch_entities, + hass, + config_entry, + async_add_entities, + ) + hass.data[DOMAIN][config_entry.entry_id][ADD_ENTITIES_CALLBACKS].update( - {DOMAIN_SWITCH: (async_add_entities, create_lcn_switch_entity)} + {DOMAIN_SWITCH: add_entities} ) - async_add_entities( - create_lcn_switch_entity(hass, entity_config, config_entry) - for entity_config in config_entry.data[CONF_ENTITIES] - if entity_config[CONF_DOMAIN] == DOMAIN_SWITCH + add_entities( + ( + entity_config + for entity_config in config_entry.data[CONF_ENTITIES] + if entity_config[CONF_DOMAIN] == DOMAIN_SWITCH + ), ) diff --git a/homeassistant/components/lcn/websocket.py b/homeassistant/components/lcn/websocket.py index 5317bc86763016..65896cc78d12c8 100644 --- a/homeassistant/components/lcn/websocket.py +++ b/homeassistant/components/lcn/websocket.py @@ -10,6 +10,7 @@ import voluptuous as vol from homeassistant.components import panel_custom, websocket_api +from homeassistant.components.http import StaticPathConfig from homeassistant.components.websocket_api import AsyncWebSocketCommandHandler from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( @@ -17,7 +18,6 @@ CONF_DEVICES, CONF_DOMAIN, CONF_ENTITIES, - CONF_ENTITY_ID, CONF_NAME, CONF_RESOURCE, ) @@ -77,10 +77,14 @@ async def register_panel_and_ws_api(hass: HomeAssistant) -> None: websocket_api.async_register_command(hass, websocket_delete_entity) if DOMAIN not in hass.data.get("frontend_panels", {}): - hass.http.register_static_path( - URL_BASE, - path=lcn_panel.locate_dir(), - cache_headers=lcn_panel.is_prod_build, + await hass.http.async_register_static_paths( + [ + StaticPathConfig( + URL_BASE, + path=lcn_panel.locate_dir(), + cache_headers=lcn_panel.is_prod_build, + ) + ] ) await panel_custom.async_register_panel( hass=hass, @@ -154,20 +158,13 @@ async def websocket_get_entity_configs( else: entity_configs = config_entry.data[CONF_ENTITIES] - entity_registry = er.async_get(hass) - for entity_config in entity_configs: - entity_unique_id = generate_unique_id( - config_entry.entry_id, - entity_config[CONF_ADDRESS], - entity_config[CONF_RESOURCE], - ) - entity_id = entity_registry.async_get_entity_id( - entity_config[CONF_DOMAIN], DOMAIN, entity_unique_id - ) - - entity_config[CONF_ENTITY_ID] = entity_id + result_entity_configs = [ + {**entity_config, CONF_NAME: entity.name or entity.original_name} + for entity_config in entity_configs[:] + if (entity := get_entity_entry(hass, entity_config, config_entry)) is not None + ] - connection.send_result(msg["id"], entity_configs) + connection.send_result(msg["id"], result_entity_configs) @websocket_api.require_admin @@ -350,11 +347,10 @@ async def websocket_add_entity( } # Create new entity and add to corresponding component - callbacks = hass.data[DOMAIN][msg["entry_id"]][ADD_ENTITIES_CALLBACKS] - async_add_entities, create_lcn_entity = callbacks[msg[CONF_DOMAIN]] - - entity = create_lcn_entity(hass, entity_config, config_entry) - async_add_entities([entity]) + add_entities = hass.data[DOMAIN][msg["entry_id"]][ADD_ENTITIES_CALLBACKS][ + msg[CONF_DOMAIN] + ] + add_entities([entity_config]) # Add entity config to config_entry entity_configs = [*config_entry.data[CONF_ENTITIES], entity_config] @@ -448,3 +444,23 @@ async def async_create_or_update_device_in_config_entry( await async_update_device_config(device_connection, device_config) hass.config_entries.async_update_entry(config_entry, data=data) + + +def get_entity_entry( + hass: HomeAssistant, entity_config: dict, config_entry: ConfigEntry +) -> er.RegistryEntry | None: + """Get entity RegistryEntry from entity_config.""" + entity_registry = er.async_get(hass) + domain_name = entity_config[CONF_DOMAIN] + domain_data = entity_config[CONF_DOMAIN_DATA] + resource = get_resource(domain_name, domain_data).lower() + unique_id = generate_unique_id( + config_entry.entry_id, + entity_config[CONF_ADDRESS], + resource, + ) + if ( + entity_id := entity_registry.async_get_entity_id(domain_name, DOMAIN, unique_id) + ) is None: + return None + return entity_registry.async_get(entity_id) diff --git a/homeassistant/components/ld2410_ble/manifest.json b/homeassistant/components/ld2410_ble/manifest.json index a1b0e9a13987dc..d3e21eeae9017c 100644 --- a/homeassistant/components/ld2410_ble/manifest.json +++ b/homeassistant/components/ld2410_ble/manifest.json @@ -20,5 +20,5 @@ "documentation": "https://www.home-assistant.io/integrations/ld2410_ble", "integration_type": "device", "iot_class": "local_push", - "requirements": ["bluetooth-data-tools==1.19.4", "ld2410-ble==0.1.1"] + "requirements": ["bluetooth-data-tools==1.20.0", "ld2410-ble==0.1.1"] } diff --git a/homeassistant/components/led_ble/manifest.json b/homeassistant/components/led_ble/manifest.json index e22d23fb9712a4..1d12e355a0d2a9 100644 --- a/homeassistant/components/led_ble/manifest.json +++ b/homeassistant/components/led_ble/manifest.json @@ -35,5 +35,5 @@ "dependencies": ["bluetooth_adapters"], "documentation": "https://www.home-assistant.io/integrations/led_ble", "iot_class": "local_polling", - "requirements": ["bluetooth-data-tools==1.19.4", "led-ble==1.0.2"] + "requirements": ["bluetooth-data-tools==1.20.0", "led-ble==1.0.2"] } diff --git a/homeassistant/components/lektrico/__init__.py b/homeassistant/components/lektrico/__init__.py new file mode 100644 index 00000000000000..70dbecca77a90d --- /dev/null +++ b/homeassistant/components/lektrico/__init__.py @@ -0,0 +1,51 @@ +"""The Lektrico Charging Station integration.""" + +from __future__ import annotations + +from lektricowifi import Device + +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import ATTR_SERIAL_NUMBER, CONF_TYPE, Platform +from homeassistant.core import HomeAssistant + +from .coordinator import LektricoDeviceDataUpdateCoordinator + +# List the platforms that charger supports. +CHARGERS_PLATFORMS = [Platform.SENSOR] + +# List the platforms that load balancer device supports. +LB_DEVICES_PLATFORMS = [Platform.SENSOR] + +type LektricoConfigEntry = ConfigEntry[LektricoDeviceDataUpdateCoordinator] + + +async def async_setup_entry(hass: HomeAssistant, entry: LektricoConfigEntry) -> bool: + """Set up Lektrico Charging Station from a config entry.""" + coordinator = LektricoDeviceDataUpdateCoordinator( + hass, + f"{entry.data[CONF_TYPE]}_{entry.data[ATTR_SERIAL_NUMBER]}", + ) + + await coordinator.async_config_entry_first_refresh() + + entry.runtime_data = coordinator + + await hass.config_entries.async_forward_entry_setups(entry, _get_platforms(entry)) + + return True + + +async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: + """Unload a config entry.""" + + return await hass.config_entries.async_unload_platforms( + entry, _get_platforms(entry) + ) + + +def _get_platforms(entry: ConfigEntry) -> list[Platform]: + """Return the platforms for this type of device.""" + _device_type: str = entry.data[CONF_TYPE] + if _device_type in (Device.TYPE_1P7K, Device.TYPE_3P22K): + return CHARGERS_PLATFORMS + return LB_DEVICES_PLATFORMS diff --git a/homeassistant/components/lektrico/config_flow.py b/homeassistant/components/lektrico/config_flow.py new file mode 100644 index 00000000000000..7091856f4fdcfd --- /dev/null +++ b/homeassistant/components/lektrico/config_flow.py @@ -0,0 +1,138 @@ +"""Config flow for Lektrico Charging Station.""" + +from __future__ import annotations + +from typing import Any + +from lektricowifi import Device, DeviceConnectionError +import voluptuous as vol + +from homeassistant.components.zeroconf import ZeroconfServiceInfo +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult +from homeassistant.const import ( + ATTR_HW_VERSION, + ATTR_SERIAL_NUMBER, + CONF_HOST, + CONF_TYPE, +) +from homeassistant.core import callback +from homeassistant.helpers.httpx_client import get_async_client + +from .const import DOMAIN + +STEP_USER_DATA_SCHEMA = vol.Schema( + { + vol.Required(CONF_HOST): str, + } +) + + +class LektricoFlowHandler(ConfigFlow, domain=DOMAIN): + """Handle a Lektrico config flow.""" + + VERSION = 1 + + _host: str + _name: str + _serial_number: str + _board_revision: str + _device_type: str + + async def async_step_user( + self, user_input: dict[str, str] | None = None + ) -> ConfigFlowResult: + """Handle a flow initiated by the user.""" + errors = None + + if user_input is not None: + self._host = user_input[CONF_HOST] + + # obtain serial number + try: + await self._get_lektrico_device_settings_and_treat_unique_id() + return self._async_create_entry() + except DeviceConnectionError: + errors = {CONF_HOST: "cannot_connect"} + + return self._async_show_setup_form(user_input=user_input, errors=errors) + + @callback + def _async_show_setup_form( + self, + user_input: dict[str, Any] | None = None, + errors: dict[str, str] | None = None, + ) -> ConfigFlowResult: + """Show the setup form to the user.""" + if user_input is None: + user_input = {} + + schema = self.add_suggested_values_to_schema(STEP_USER_DATA_SCHEMA, user_input) + + return self.async_show_form( + step_id="user", + data_schema=schema, + errors=errors or {}, + ) + + @callback + def _async_create_entry(self) -> ConfigFlowResult: + return self.async_create_entry( + title=self._name, + data={ + CONF_HOST: self._host, + ATTR_SERIAL_NUMBER: self._serial_number, + CONF_TYPE: self._device_type, + ATTR_HW_VERSION: self._board_revision, + }, + ) + + async def async_step_zeroconf( + self, discovery_info: ZeroconfServiceInfo + ) -> ConfigFlowResult: + """Handle zeroconf discovery.""" + self._host = discovery_info.host # 192.168.100.11 + + # read settings from the device + try: + await self._get_lektrico_device_settings_and_treat_unique_id() + except DeviceConnectionError: + return self.async_abort(reason="cannot_connect") + + self.context["title_placeholders"] = { + "serial_number": self._serial_number, + "name": self._name, + } + + return await self.async_step_confirm() + + async def _get_lektrico_device_settings_and_treat_unique_id(self) -> None: + """Get device's serial number from a Lektrico device.""" + device = Device( + _host=self._host, + asyncClient=get_async_client(self.hass), + ) + + settings = await device.device_config() + self._serial_number = str(settings["serial_number"]) + self._device_type = settings["type"] + self._board_revision = settings["board_revision"] + self._name = f"{settings["type"]}_{self._serial_number}" + + # Check if already configured + # Set unique id + await self.async_set_unique_id(self._serial_number, raise_on_progress=True) + # Abort if already configured, but update the last-known host + self._abort_if_unique_id_configured( + updates={CONF_HOST: self._host}, reload_on_update=True + ) + + async def async_step_confirm( + self, user_input: dict[str, str] | None = None + ) -> ConfigFlowResult: + """Allow the user to confirm adding the device.""" + + if user_input is not None: + return self._async_create_entry() + + self._set_confirm_only() + return self.async_show_form(step_id="confirm") diff --git a/homeassistant/components/lektrico/const.py b/homeassistant/components/lektrico/const.py new file mode 100644 index 00000000000000..d3fc52f61be39f --- /dev/null +++ b/homeassistant/components/lektrico/const.py @@ -0,0 +1,9 @@ +"""Constants for the Lektrico Charging Station integration.""" + +from logging import Logger, getLogger + +# Integration domain +DOMAIN = "lektrico" + +# Logger +LOGGER: Logger = getLogger(__package__) diff --git a/homeassistant/components/lektrico/coordinator.py b/homeassistant/components/lektrico/coordinator.py new file mode 100644 index 00000000000000..7c72a00e2d36b8 --- /dev/null +++ b/homeassistant/components/lektrico/coordinator.py @@ -0,0 +1,52 @@ +"""Coordinator for the Lektrico Charging Station integration.""" + +from __future__ import annotations + +from datetime import timedelta +from typing import Any + +from lektricowifi import Device, DeviceConnectionError + +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import ( + ATTR_HW_VERSION, + ATTR_SERIAL_NUMBER, + CONF_HOST, + CONF_TYPE, +) +from homeassistant.core import HomeAssistant +from homeassistant.helpers.httpx_client import get_async_client +from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed + +from .const import LOGGER + +SCAN_INTERVAL = timedelta(seconds=10) + + +class LektricoDeviceDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]): + """Data update coordinator for Lektrico device.""" + + config_entry: ConfigEntry + + def __init__(self, hass: HomeAssistant, device_name: str) -> None: + """Initialize a Lektrico Device.""" + super().__init__( + hass, + LOGGER, + name=device_name, + update_interval=SCAN_INTERVAL, + ) + self.device = Device( + self.config_entry.data[CONF_HOST], + asyncClient=get_async_client(hass), + ) + self.serial_number: str = self.config_entry.data[ATTR_SERIAL_NUMBER] + self.board_revision: str = self.config_entry.data[ATTR_HW_VERSION] + self.device_type: str = self.config_entry.data[CONF_TYPE] + + async def _async_update_data(self) -> dict[str, Any]: + """Async Update device state.""" + try: + return await self.device.device_info(self.device_type) + except DeviceConnectionError as lek_ex: + raise UpdateFailed(lek_ex) from lek_ex diff --git a/homeassistant/components/lektrico/entity.py b/homeassistant/components/lektrico/entity.py new file mode 100644 index 00000000000000..1a5e08febe3677 --- /dev/null +++ b/homeassistant/components/lektrico/entity.py @@ -0,0 +1,33 @@ +"""Entity classes for the Lektrico integration.""" + +from __future__ import annotations + +from homeassistant.helpers.device_registry import DeviceInfo +from homeassistant.helpers.update_coordinator import CoordinatorEntity + +from . import LektricoDeviceDataUpdateCoordinator +from .const import DOMAIN + + +class LektricoEntity(CoordinatorEntity[LektricoDeviceDataUpdateCoordinator]): + """Define an Lektrico entity.""" + + _attr_has_entity_name = True + + def __init__( + self, + coordinator: LektricoDeviceDataUpdateCoordinator, + device_name: str, + ) -> None: + """Initialize.""" + super().__init__(coordinator) + + self._attr_device_info = DeviceInfo( + identifiers={(DOMAIN, coordinator.serial_number)}, + model=coordinator.device_type.upper(), + name=device_name, + manufacturer="Lektrico", + sw_version=coordinator.data["fw_version"], + hw_version=coordinator.board_revision, + serial_number=coordinator.serial_number, + ) diff --git a/homeassistant/components/lektrico/manifest.json b/homeassistant/components/lektrico/manifest.json new file mode 100644 index 00000000000000..5aef09f3845f15 --- /dev/null +++ b/homeassistant/components/lektrico/manifest.json @@ -0,0 +1,16 @@ +{ + "domain": "lektrico", + "name": "Lektrico Charging Station", + "codeowners": ["@lektrico"], + "config_flow": true, + "documentation": "https://www.home-assistant.io/integrations/lektrico", + "integration_type": "device", + "iot_class": "local_polling", + "requirements": ["lektricowifi==0.0.41"], + "zeroconf": [ + { + "type": "_http._tcp.local.", + "name": "lektrico*" + } + ] +} diff --git a/homeassistant/components/lektrico/sensor.py b/homeassistant/components/lektrico/sensor.py new file mode 100644 index 00000000000000..a8a929d974f448 --- /dev/null +++ b/homeassistant/components/lektrico/sensor.py @@ -0,0 +1,324 @@ +"""Support for Lektrico charging station sensors.""" + +from __future__ import annotations + +from collections.abc import Callable +from dataclasses import dataclass +from typing import Any + +from lektricowifi import Device + +from homeassistant.components.sensor import ( + SensorDeviceClass, + SensorEntity, + SensorEntityDescription, + SensorStateClass, +) +from homeassistant.const import ( + ATTR_SERIAL_NUMBER, + CONF_TYPE, + PERCENTAGE, + UnitOfElectricCurrent, + UnitOfElectricPotential, + UnitOfEnergy, + UnitOfPower, + UnitOfTemperature, + UnitOfTime, +) +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import IntegrationError +from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.typing import StateType + +from . import LektricoConfigEntry, LektricoDeviceDataUpdateCoordinator +from .entity import LektricoEntity + + +@dataclass(frozen=True, kw_only=True) +class LektricoSensorEntityDescription(SensorEntityDescription): + """A class that describes the Lektrico sensor entities.""" + + value_fn: Callable[[dict[str, Any]], StateType] + + +SENSORS_FOR_CHARGERS: tuple[LektricoSensorEntityDescription, ...] = ( + LektricoSensorEntityDescription( + key="state", + device_class=SensorDeviceClass.ENUM, + options=[ + "available", + "connected", + "need_auth", + "paused", + "charging", + "error", + "updating_firmware", + ], + translation_key="state", + value_fn=lambda data: str(data["charger_state"]), + ), + LektricoSensorEntityDescription( + key="charging_time", + translation_key="charging_time", + device_class=SensorDeviceClass.DURATION, + native_unit_of_measurement=UnitOfTime.SECONDS, + value_fn=lambda data: int(data["charging_time"]), + ), + LektricoSensorEntityDescription( + key="power", + device_class=SensorDeviceClass.POWER, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfPower.WATT, + suggested_unit_of_measurement=UnitOfPower.KILO_WATT, + value_fn=lambda data: float(data["instant_power"]), + ), + LektricoSensorEntityDescription( + key="energy", + device_class=SensorDeviceClass.ENERGY, + native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + value_fn=lambda data: float(data["session_energy"]) / 1000, + ), + LektricoSensorEntityDescription( + key="temperature", + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + value_fn=lambda data: float(data["temperature"]), + ), + LektricoSensorEntityDescription( + key="lifetime_energy", + translation_key="lifetime_energy", + state_class=SensorStateClass.TOTAL_INCREASING, + device_class=SensorDeviceClass.ENERGY, + native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + value_fn=lambda data: int(data["total_charged_energy"]), + ), + LektricoSensorEntityDescription( + key="installation_current", + translation_key="installation_current", + device_class=SensorDeviceClass.CURRENT, + native_unit_of_measurement=UnitOfElectricCurrent.AMPERE, + value_fn=lambda data: int(data["install_current"]), + ), + LektricoSensorEntityDescription( + key="limit_reason", + translation_key="limit_reason", + device_class=SensorDeviceClass.ENUM, + options=[ + "no_limit", + "installation_current", + "user_limit", + "dynamic_limit", + "schedule", + "em_offline", + "em", + "ocpp", + ], + value_fn=lambda data: str(data["current_limit_reason"]), + ), +) + +SENSORS_FOR_LB_DEVICES: tuple[LektricoSensorEntityDescription, ...] = ( + LektricoSensorEntityDescription( + key="breaker_current", + translation_key="breaker_current", + device_class=SensorDeviceClass.CURRENT, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfElectricCurrent.AMPERE, + value_fn=lambda data: int(data["breaker_curent"]), + ), +) + +SENSORS_FOR_1_PHASE: tuple[LektricoSensorEntityDescription, ...] = ( + LektricoSensorEntityDescription( + key="voltage", + device_class=SensorDeviceClass.VOLTAGE, + native_unit_of_measurement=UnitOfElectricPotential.VOLT, + value_fn=lambda data: float(data["voltage_l1"]), + ), + LektricoSensorEntityDescription( + key="current", + device_class=SensorDeviceClass.CURRENT, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfElectricCurrent.AMPERE, + value_fn=lambda data: float(data["current_l1"]), + ), +) + +SENSORS_FOR_3_PHASE: tuple[LektricoSensorEntityDescription, ...] = ( + LektricoSensorEntityDescription( + key="voltage_l1", + translation_key="voltage_l1", + device_class=SensorDeviceClass.VOLTAGE, + native_unit_of_measurement=UnitOfElectricPotential.VOLT, + value_fn=lambda data: float(data["voltage_l1"]), + ), + LektricoSensorEntityDescription( + key="voltage_l2", + translation_key="voltage_l2", + device_class=SensorDeviceClass.VOLTAGE, + native_unit_of_measurement=UnitOfElectricPotential.VOLT, + value_fn=lambda data: float(data["voltage_l2"]), + ), + LektricoSensorEntityDescription( + key="voltage_l3", + translation_key="voltage_l3", + device_class=SensorDeviceClass.VOLTAGE, + native_unit_of_measurement=UnitOfElectricPotential.VOLT, + value_fn=lambda data: float(data["voltage_l3"]), + ), + LektricoSensorEntityDescription( + key="current_l1", + translation_key="current_l1", + device_class=SensorDeviceClass.CURRENT, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfElectricCurrent.AMPERE, + value_fn=lambda data: float(data["current_l1"]), + ), + LektricoSensorEntityDescription( + key="current_l2", + translation_key="current_l2", + device_class=SensorDeviceClass.CURRENT, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfElectricCurrent.AMPERE, + value_fn=lambda data: float(data["current_l2"]), + ), + LektricoSensorEntityDescription( + key="current_l3", + translation_key="current_l3", + device_class=SensorDeviceClass.CURRENT, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfElectricCurrent.AMPERE, + value_fn=lambda data: float(data["current_l3"]), + ), +) + + +SENSORS_FOR_LB_1_PHASE: tuple[LektricoSensorEntityDescription, ...] = ( + LektricoSensorEntityDescription( + key="power", + device_class=SensorDeviceClass.POWER, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfPower.WATT, + suggested_unit_of_measurement=UnitOfPower.KILO_WATT, + value_fn=lambda data: float(data["power_l1"]), + ), + LektricoSensorEntityDescription( + key="pf", + device_class=SensorDeviceClass.POWER_FACTOR, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=PERCENTAGE, + value_fn=lambda data: float(data["power_factor_l1"]) * 100, + ), +) + + +SENSORS_FOR_LB_3_PHASE: tuple[LektricoSensorEntityDescription, ...] = ( + LektricoSensorEntityDescription( + key="power_l1", + translation_key="power_l1", + device_class=SensorDeviceClass.POWER, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfPower.WATT, + suggested_unit_of_measurement=UnitOfPower.KILO_WATT, + value_fn=lambda data: float(data["power_l1"]), + ), + LektricoSensorEntityDescription( + key="power_l2", + translation_key="power_l2", + device_class=SensorDeviceClass.POWER, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfPower.WATT, + suggested_unit_of_measurement=UnitOfPower.KILO_WATT, + value_fn=lambda data: float(data["power_l2"]), + ), + LektricoSensorEntityDescription( + key="power_l3", + translation_key="power_l3", + device_class=SensorDeviceClass.POWER, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfPower.WATT, + suggested_unit_of_measurement=UnitOfPower.KILO_WATT, + value_fn=lambda data: float(data["power_l3"]), + ), + LektricoSensorEntityDescription( + key="pf_l1", + translation_key="pf_l1", + device_class=SensorDeviceClass.POWER_FACTOR, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=PERCENTAGE, + value_fn=lambda data: float(data["power_factor_l1"]) * 100, + ), + LektricoSensorEntityDescription( + key="pf_l2", + translation_key="pf_l2", + device_class=SensorDeviceClass.POWER_FACTOR, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=PERCENTAGE, + value_fn=lambda data: float(data["power_factor_l2"]) * 100, + ), + LektricoSensorEntityDescription( + key="pf_l3", + translation_key="pf_l3", + device_class=SensorDeviceClass.POWER_FACTOR, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=PERCENTAGE, + value_fn=lambda data: float(data["power_factor_l3"]) * 100, + ), +) + + +async def async_setup_entry( + hass: HomeAssistant, + entry: LektricoConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up Lektrico charger based on a config entry.""" + coordinator = entry.runtime_data + + sensors_to_be_used: tuple[LektricoSensorEntityDescription, ...] + if coordinator.device_type == Device.TYPE_1P7K: + sensors_to_be_used = SENSORS_FOR_CHARGERS + SENSORS_FOR_1_PHASE + elif coordinator.device_type == Device.TYPE_3P22K: + sensors_to_be_used = SENSORS_FOR_CHARGERS + SENSORS_FOR_3_PHASE + elif coordinator.device_type == Device.TYPE_EM: + sensors_to_be_used = ( + SENSORS_FOR_LB_DEVICES + SENSORS_FOR_1_PHASE + SENSORS_FOR_LB_1_PHASE + ) + elif coordinator.device_type == Device.TYPE_3EM: + sensors_to_be_used = ( + SENSORS_FOR_LB_DEVICES + SENSORS_FOR_3_PHASE + SENSORS_FOR_LB_3_PHASE + ) + else: + raise IntegrationError + + async_add_entities( + LektricoSensor( + description, + coordinator, + f"{entry.data[CONF_TYPE]}_{entry.data[ATTR_SERIAL_NUMBER]}", + ) + for description in sensors_to_be_used + ) + + +class LektricoSensor(LektricoEntity, SensorEntity): + """The entity class for Lektrico charging stations sensors.""" + + entity_description: LektricoSensorEntityDescription + + def __init__( + self, + description: LektricoSensorEntityDescription, + coordinator: LektricoDeviceDataUpdateCoordinator, + device_name: str, + ) -> None: + """Initialize Lektrico charger.""" + super().__init__(coordinator, device_name) + self.entity_description = description + self._attr_unique_id = f"{coordinator.serial_number}_{description.key}" + + @property + def native_value(self) -> StateType: + """Return the state of the sensor.""" + return self.entity_description.value_fn(self.coordinator.data) diff --git a/homeassistant/components/lektrico/strings.json b/homeassistant/components/lektrico/strings.json new file mode 100644 index 00000000000000..767987e7e64d10 --- /dev/null +++ b/homeassistant/components/lektrico/strings.json @@ -0,0 +1,101 @@ +{ + "config": { + "step": { + "user": { + "description": "Set required parameters to connect to your device", + "data": { + "host": "[%key:common::config_flow::data::host%]", + "device_name": "[%key:common::config_flow::data::name%]" + } + }, + "zeroconf_confirm": { + "description": "Do you want to add the Lektrico Charger with serial number `{serial_number}` to Home Assistant?", + "title": "Discovered Lektrico Charger device" + } + }, + "error": { + "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]" + }, + "abort": { + "already_configured": "[%key:common::config_flow::abort::already_configured_device%]", + "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]" + } + }, + "entity": { + "sensor": { + "state": { + "name": "State", + "state": { + "available": "Available", + "connected": "Connected", + "need_auth": "Waiting for authentication", + "paused": "Paused", + "charging": "Charging", + "error": "Error", + "updating_firmware": "Updating firmware" + } + }, + "charging_time": { + "name": "Charging time" + }, + "lifetime_energy": { + "name": "Lifetime energy" + }, + "installation_current": { + "name": "Installation current" + }, + "limit_reason": { + "name": "Limit reason", + "state": { + "no_limit": "No limit", + "installation_current": "Installation current", + "user_limit": "User limit", + "dynamic_limit": "Dynamic limit", + "schedule": "Schedule", + "em_offline": "EM offline", + "em": "EM", + "ocpp": "OCPP" + } + }, + "breaker_current": { + "name": "Breaker current" + }, + "voltage_l1": { + "name": "Voltage L1" + }, + "voltage_l2": { + "name": "Voltage L2" + }, + "voltage_l3": { + "name": "Voltage L3" + }, + "current_l1": { + "name": "Current L1" + }, + "current_l2": { + "name": "Current L2" + }, + "current_l3": { + "name": "Current L3" + }, + "power_l1": { + "name": "Power L1" + }, + "power_l2": { + "name": "Power L2" + }, + "power_l3": { + "name": "Power L3" + }, + "pf_l1": { + "name": "Power factor L1" + }, + "pf_l2": { + "name": "Power factor L2" + }, + "pf_l3": { + "name": "Power factor L3" + } + } + } +} diff --git a/homeassistant/components/lg_netcast/config_flow.py b/homeassistant/components/lg_netcast/config_flow.py index c4e6c75edea0b7..4b1780d41ae63d 100644 --- a/homeassistant/components/lg_netcast/config_flow.py +++ b/homeassistant/components/lg_netcast/config_flow.py @@ -68,11 +68,11 @@ async def async_step_user( errors=errors, ) - async def async_step_import(self, config: dict[str, Any]) -> ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Import configuration from yaml.""" self.device_config = { - CONF_HOST: config[CONF_HOST], - CONF_NAME: config[CONF_NAME], + CONF_HOST: import_data[CONF_HOST], + CONF_NAME: import_data[CONF_NAME], } def _create_issue(): @@ -92,7 +92,7 @@ def _create_issue(): ) try: - result: ConfigFlowResult = await self.async_step_authorize(config) + result: ConfigFlowResult = await self.async_step_authorize(import_data) except AbortFlow as err: if err.reason != "already_configured": async_create_issue( diff --git a/homeassistant/components/lg_thinq/__init__.py b/homeassistant/components/lg_thinq/__init__.py new file mode 100644 index 00000000000000..625938564a849c --- /dev/null +++ b/homeassistant/components/lg_thinq/__init__.py @@ -0,0 +1,101 @@ +"""Support for LG ThinQ Connect device.""" + +from __future__ import annotations + +import asyncio +import logging + +from thinqconnect import ThinQApi, ThinQAPIException +from thinqconnect.integration import async_get_ha_bridge_list + +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import CONF_ACCESS_TOKEN, CONF_COUNTRY, Platform +from homeassistant.core import HomeAssistant, callback +from homeassistant.exceptions import ConfigEntryNotReady +from homeassistant.helpers import device_registry as dr +from homeassistant.helpers.aiohttp_client import async_get_clientsession + +from .const import CONF_CONNECT_CLIENT_ID +from .coordinator import DeviceDataUpdateCoordinator, async_setup_device_coordinator + +type ThinqConfigEntry = ConfigEntry[dict[str, DeviceDataUpdateCoordinator]] + +PLATFORMS = [Platform.BINARY_SENSOR, Platform.SWITCH] + +_LOGGER = logging.getLogger(__name__) + + +async def async_setup_entry(hass: HomeAssistant, entry: ThinqConfigEntry) -> bool: + """Set up an entry.""" + entry.runtime_data = {} + + access_token = entry.data[CONF_ACCESS_TOKEN] + client_id = entry.data[CONF_CONNECT_CLIENT_ID] + country_code = entry.data[CONF_COUNTRY] + + thinq_api = ThinQApi( + session=async_get_clientsession(hass), + access_token=access_token, + country_code=country_code, + client_id=client_id, + ) + + # Setup coordinators and register devices. + await async_setup_coordinators(hass, entry, thinq_api) + + # Set up all platforms for this device/entry. + await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) + + # Clean up devices they are no longer in use. + async_cleanup_device_registry(hass, entry) + + return True + + +async def async_setup_coordinators( + hass: HomeAssistant, + entry: ThinqConfigEntry, + thinq_api: ThinQApi, +) -> None: + """Set up coordinators and register devices.""" + # Get a list of ha bridge. + try: + bridge_list = await async_get_ha_bridge_list(thinq_api) + except ThinQAPIException as exc: + raise ConfigEntryNotReady(exc.message) from exc + + if not bridge_list: + return + + # Setup coordinator per device. + task_list = [ + hass.async_create_task(async_setup_device_coordinator(hass, bridge)) + for bridge in bridge_list + ] + task_result = await asyncio.gather(*task_list) + for coordinator in task_result: + entry.runtime_data[coordinator.unique_id] = coordinator + + +@callback +def async_cleanup_device_registry(hass: HomeAssistant, entry: ThinqConfigEntry) -> None: + """Clean up device registry.""" + new_device_unique_ids = [ + coordinator.unique_id for coordinator in entry.runtime_data.values() + ] + device_registry = dr.async_get(hass) + existing_entries = dr.async_entries_for_config_entry( + device_registry, entry.entry_id + ) + + # Remove devices that are no longer exist. + for old_entry in existing_entries: + old_unique_id = next(iter(old_entry.identifiers))[1] + if old_unique_id not in new_device_unique_ids: + device_registry.async_remove_device(old_entry.id) + _LOGGER.debug("Remove device_registry: device_id=%s", old_entry.id) + + +async def async_unload_entry(hass: HomeAssistant, entry: ThinqConfigEntry) -> bool: + """Unload the entry.""" + return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) diff --git a/homeassistant/components/lg_thinq/binary_sensor.py b/homeassistant/components/lg_thinq/binary_sensor.py new file mode 100644 index 00000000000000..6f856c3055f1ea --- /dev/null +++ b/homeassistant/components/lg_thinq/binary_sensor.py @@ -0,0 +1,114 @@ +"""Support for binary sensor entities.""" + +from __future__ import annotations + +import logging + +from thinqconnect import DeviceType +from thinqconnect.devices.const import Property as ThinQProperty + +from homeassistant.components.binary_sensor import ( + BinarySensorEntity, + BinarySensorEntityDescription, +) +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from . import ThinqConfigEntry +from .entity import ThinQEntity + +BINARY_SENSOR_DESC: dict[ThinQProperty, BinarySensorEntityDescription] = { + ThinQProperty.RINSE_REFILL: BinarySensorEntityDescription( + key=ThinQProperty.RINSE_REFILL, + translation_key=ThinQProperty.RINSE_REFILL, + ), + ThinQProperty.ECO_FRIENDLY_MODE: BinarySensorEntityDescription( + key=ThinQProperty.ECO_FRIENDLY_MODE, + translation_key=ThinQProperty.ECO_FRIENDLY_MODE, + ), + ThinQProperty.POWER_SAVE_ENABLED: BinarySensorEntityDescription( + key=ThinQProperty.POWER_SAVE_ENABLED, + translation_key=ThinQProperty.POWER_SAVE_ENABLED, + ), + ThinQProperty.REMOTE_CONTROL_ENABLED: BinarySensorEntityDescription( + key=ThinQProperty.REMOTE_CONTROL_ENABLED, + translation_key=ThinQProperty.REMOTE_CONTROL_ENABLED, + ), + ThinQProperty.SABBATH_MODE: BinarySensorEntityDescription( + key=ThinQProperty.SABBATH_MODE, + translation_key=ThinQProperty.SABBATH_MODE, + ), +} + +DEVICE_TYPE_BINARY_SENSOR_MAP: dict[ + DeviceType, tuple[BinarySensorEntityDescription, ...] +] = { + DeviceType.COOKTOP: (BINARY_SENSOR_DESC[ThinQProperty.REMOTE_CONTROL_ENABLED],), + DeviceType.DISH_WASHER: ( + BINARY_SENSOR_DESC[ThinQProperty.RINSE_REFILL], + BINARY_SENSOR_DESC[ThinQProperty.REMOTE_CONTROL_ENABLED], + ), + DeviceType.DRYER: (BINARY_SENSOR_DESC[ThinQProperty.REMOTE_CONTROL_ENABLED],), + DeviceType.OVEN: (BINARY_SENSOR_DESC[ThinQProperty.REMOTE_CONTROL_ENABLED],), + DeviceType.REFRIGERATOR: ( + BINARY_SENSOR_DESC[ThinQProperty.ECO_FRIENDLY_MODE], + BINARY_SENSOR_DESC[ThinQProperty.POWER_SAVE_ENABLED], + BINARY_SENSOR_DESC[ThinQProperty.SABBATH_MODE], + ), + DeviceType.STYLER: (BINARY_SENSOR_DESC[ThinQProperty.REMOTE_CONTROL_ENABLED],), + DeviceType.WASHCOMBO_MAIN: ( + BINARY_SENSOR_DESC[ThinQProperty.REMOTE_CONTROL_ENABLED], + ), + DeviceType.WASHCOMBO_MINI: ( + BINARY_SENSOR_DESC[ThinQProperty.REMOTE_CONTROL_ENABLED], + ), + DeviceType.WASHER: (BINARY_SENSOR_DESC[ThinQProperty.REMOTE_CONTROL_ENABLED],), + DeviceType.WASHTOWER_DRYER: ( + BINARY_SENSOR_DESC[ThinQProperty.REMOTE_CONTROL_ENABLED], + ), + DeviceType.WASHTOWER: (BINARY_SENSOR_DESC[ThinQProperty.REMOTE_CONTROL_ENABLED],), + DeviceType.WASHTOWER_WASHER: ( + BINARY_SENSOR_DESC[ThinQProperty.REMOTE_CONTROL_ENABLED], + ), + DeviceType.WINE_CELLAR: (BINARY_SENSOR_DESC[ThinQProperty.SABBATH_MODE],), +} +_LOGGER = logging.getLogger(__name__) + + +async def async_setup_entry( + hass: HomeAssistant, + entry: ThinqConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up an entry for binary sensor platform.""" + entities: list[ThinQBinarySensorEntity] = [] + for coordinator in entry.runtime_data.values(): + if ( + descriptions := DEVICE_TYPE_BINARY_SENSOR_MAP.get( + coordinator.api.device.device_type + ) + ) is not None: + for description in descriptions: + entities.extend( + ThinQBinarySensorEntity(coordinator, description, property_id) + for property_id in coordinator.api.get_active_idx(description.key) + ) + + if entities: + async_add_entities(entities) + + +class ThinQBinarySensorEntity(ThinQEntity, BinarySensorEntity): + """Represent a thinq binary sensor platform.""" + + def _update_status(self) -> None: + """Update status itself.""" + super()._update_status() + + _LOGGER.debug( + "[%s:%s] update status: %s", + self.coordinator.device_name, + self.property_id, + self.data.is_on, + ) + self._attr_is_on = self.data.is_on diff --git a/homeassistant/components/lg_thinq/config_flow.py b/homeassistant/components/lg_thinq/config_flow.py new file mode 100644 index 00000000000000..cdb419166880da --- /dev/null +++ b/homeassistant/components/lg_thinq/config_flow.py @@ -0,0 +1,103 @@ +"""Config flow for LG ThinQ.""" + +from __future__ import annotations + +import logging +from typing import Any +import uuid + +from thinqconnect import ThinQApi, ThinQAPIException +from thinqconnect.country import Country +import voluptuous as vol + +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult +from homeassistant.const import CONF_ACCESS_TOKEN, CONF_COUNTRY +from homeassistant.helpers import config_validation as cv +from homeassistant.helpers.aiohttp_client import async_get_clientsession +from homeassistant.helpers.selector import CountrySelector, CountrySelectorConfig + +from .const import ( + CLIENT_PREFIX, + CONF_CONNECT_CLIENT_ID, + DEFAULT_COUNTRY, + DOMAIN, + THINQ_DEFAULT_NAME, + THINQ_PAT_URL, +) + +SUPPORTED_COUNTRIES = [country.value for country in Country] + +_LOGGER = logging.getLogger(__name__) + + +class ThinQFlowHandler(ConfigFlow, domain=DOMAIN): + """Handle a config flow.""" + + VERSION = 1 + + def _get_default_country_code(self) -> str: + """Get the default country code based on config.""" + country = self.hass.config.country + if country is not None and country in SUPPORTED_COUNTRIES: + return country + + return DEFAULT_COUNTRY + + async def _validate_and_create_entry( + self, access_token: str, country_code: str + ) -> ConfigFlowResult: + """Create an entry for the flow.""" + connect_client_id = f"{CLIENT_PREFIX}-{uuid.uuid4()!s}" + + # To verify PAT, create an api to retrieve the device list. + await ThinQApi( + session=async_get_clientsession(self.hass), + access_token=access_token, + country_code=country_code, + client_id=connect_client_id, + ).async_get_device_list() + + # If verification is success, create entry. + return self.async_create_entry( + title=THINQ_DEFAULT_NAME, + data={ + CONF_ACCESS_TOKEN: access_token, + CONF_CONNECT_CLIENT_ID: connect_client_id, + CONF_COUNTRY: country_code, + }, + ) + + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle a flow initiated by the user.""" + errors: dict[str, str] = {} + + if user_input is not None: + access_token = user_input[CONF_ACCESS_TOKEN] + country_code = user_input[CONF_COUNTRY] + + # Check if PAT is already configured. + await self.async_set_unique_id(access_token) + self._abort_if_unique_id_configured() + + try: + return await self._validate_and_create_entry(access_token, country_code) + except ThinQAPIException: + errors["base"] = "token_unauthorized" + + return self.async_show_form( + step_id="user", + data_schema=vol.Schema( + { + vol.Required(CONF_ACCESS_TOKEN): cv.string, + vol.Required( + CONF_COUNTRY, default=self._get_default_country_code() + ): CountrySelector( + CountrySelectorConfig(countries=SUPPORTED_COUNTRIES) + ), + } + ), + description_placeholders={"pat_url": THINQ_PAT_URL}, + errors=errors, + ) diff --git a/homeassistant/components/lg_thinq/const.py b/homeassistant/components/lg_thinq/const.py new file mode 100644 index 00000000000000..09f8c0833dfd5e --- /dev/null +++ b/homeassistant/components/lg_thinq/const.py @@ -0,0 +1,12 @@ +"""Constants for LG ThinQ.""" + +from typing import Final + +# Config flow +DOMAIN = "lg_thinq" +COMPANY = "LGE" +DEFAULT_COUNTRY: Final = "US" +THINQ_DEFAULT_NAME: Final = "LG ThinQ" +THINQ_PAT_URL: Final = "https://connect-pat.lgthinq.com" +CLIENT_PREFIX: Final = "home-assistant" +CONF_CONNECT_CLIENT_ID: Final = "connect_client_id" diff --git a/homeassistant/components/lg_thinq/coordinator.py b/homeassistant/components/lg_thinq/coordinator.py new file mode 100644 index 00000000000000..5ba77c648a84fc --- /dev/null +++ b/homeassistant/components/lg_thinq/coordinator.py @@ -0,0 +1,69 @@ +"""DataUpdateCoordinator for the LG ThinQ device.""" + +from __future__ import annotations + +import logging +from typing import Any + +from thinqconnect import ThinQAPIException +from thinqconnect.integration import HABridge + +from homeassistant.core import HomeAssistant +from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed + +from .const import DOMAIN + +_LOGGER = logging.getLogger(__name__) + + +class DeviceDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]): + """LG Device's Data Update Coordinator.""" + + def __init__(self, hass: HomeAssistant, ha_bridge: HABridge) -> None: + """Initialize data coordinator.""" + super().__init__( + hass, + _LOGGER, + name=f"{DOMAIN}_{ha_bridge.device.device_id}", + ) + + self.data = {} + self.api = ha_bridge + self.device_id = ha_bridge.device.device_id + self.sub_id = ha_bridge.sub_id + + alias = ha_bridge.device.alias + + # The device name is usually set to 'alias'. + # But, if the sub_id exists, it will be set to 'alias {sub_id}'. + # e.g. alias='MyWashTower', sub_id='dryer' then 'MyWashTower dryer'. + self.device_name = f"{alias} {self.sub_id}" if self.sub_id else alias + + # The unique id is usually set to 'device_id'. + # But, if the sub_id exists, it will be set to 'device_id_{sub_id}'. + # e.g. device_id='TQSXXXX', sub_id='dryer' then 'TQSXXXX_dryer'. + self.unique_id = ( + f"{self.device_id}_{self.sub_id}" if self.sub_id else self.device_id + ) + + async def _async_update_data(self) -> dict[str, Any]: + """Request to the server to update the status from full response data.""" + try: + return await self.api.fetch_data() + except ThinQAPIException as e: + raise UpdateFailed(e) from e + + def refresh_status(self) -> None: + """Refresh current status.""" + self.async_set_updated_data(self.data) + + +async def async_setup_device_coordinator( + hass: HomeAssistant, ha_bridge: HABridge +) -> DeviceDataUpdateCoordinator: + """Create DeviceDataUpdateCoordinator and device_api per device.""" + coordinator = DeviceDataUpdateCoordinator(hass, ha_bridge) + await coordinator.async_refresh() + + _LOGGER.debug("Setup device's coordinator: %s", coordinator.device_name) + return coordinator diff --git a/homeassistant/components/lg_thinq/entity.py b/homeassistant/components/lg_thinq/entity.py new file mode 100644 index 00000000000000..09ff8662efb055 --- /dev/null +++ b/homeassistant/components/lg_thinq/entity.py @@ -0,0 +1,95 @@ +"""Base class for ThinQ entities.""" + +from __future__ import annotations + +from collections.abc import Coroutine +import logging +from typing import Any + +from thinqconnect import ThinQAPIException +from thinqconnect.devices.const import Location +from thinqconnect.integration import PropertyState + +from homeassistant.core import callback +from homeassistant.exceptions import ServiceValidationError +from homeassistant.helpers import device_registry as dr +from homeassistant.helpers.entity import EntityDescription +from homeassistant.helpers.update_coordinator import CoordinatorEntity + +from .const import COMPANY, DOMAIN +from .coordinator import DeviceDataUpdateCoordinator + +_LOGGER = logging.getLogger(__name__) + +EMPTY_STATE = PropertyState() + + +class ThinQEntity(CoordinatorEntity[DeviceDataUpdateCoordinator]): + """The base implementation of all lg thinq entities.""" + + _attr_has_entity_name = True + + def __init__( + self, + coordinator: DeviceDataUpdateCoordinator, + entity_description: EntityDescription, + property_id: str, + ) -> None: + """Initialize an entity.""" + super().__init__(coordinator) + + self.entity_description = entity_description + self.property_id = property_id + self.location = self.coordinator.api.get_location_for_idx(self.property_id) + + self._attr_device_info = dr.DeviceInfo( + identifiers={(DOMAIN, coordinator.unique_id)}, + manufacturer=COMPANY, + model=coordinator.api.device.model_name, + name=coordinator.device_name, + ) + self._attr_unique_id = f"{coordinator.unique_id}_{self.property_id}" + if self.location is not None and self.location not in ( + Location.MAIN, + Location.OVEN, + coordinator.sub_id, + ): + self._attr_translation_placeholders = {"location": self.location} + self._attr_translation_key = ( + f"{entity_description.translation_key}_for_location" + ) + + @property + def data(self) -> PropertyState: + """Return the state data of entity.""" + return self.coordinator.data.get(self.property_id, EMPTY_STATE) + + def _update_status(self) -> None: + """Update status itself. + + All inherited classes can update their own status in here. + """ + + @callback + def _handle_coordinator_update(self) -> None: + """Handle updated data from the coordinator.""" + self._update_status() + self.async_write_ha_state() + + async def async_added_to_hass(self) -> None: + """Call when entity is added to hass.""" + await super().async_added_to_hass() + self._handle_coordinator_update() + + async def async_call_api(self, target: Coroutine[Any, Any, Any]) -> None: + """Call the given api and handle exception.""" + try: + await target + except ThinQAPIException as exc: + raise ServiceValidationError( + exc.message, + translation_domain=DOMAIN, + translation_key=exc.code, + ) from exc + finally: + await self.coordinator.async_request_refresh() diff --git a/homeassistant/components/lg_thinq/icons.json b/homeassistant/components/lg_thinq/icons.json new file mode 100644 index 00000000000000..3cc4ab784c2ca1 --- /dev/null +++ b/homeassistant/components/lg_thinq/icons.json @@ -0,0 +1,29 @@ +{ + "entity": { + "switch": { + "operation_power": { + "default": "mdi:power" + } + }, + "binary_sensor": { + "eco_friendly_mode": { + "default": "mdi:sprout" + }, + "power_save_enabled": { + "default": "mdi:meter-electric" + }, + "remote_control_enabled": { + "default": "mdi:remote" + }, + "remote_control_enabled_for_location": { + "default": "mdi:remote" + }, + "rinse_refill": { + "default": "mdi:tune-vertical-variant" + }, + "sabbath_mode": { + "default": "mdi:food-off-outline" + } + } + } +} diff --git a/homeassistant/components/lg_thinq/manifest.json b/homeassistant/components/lg_thinq/manifest.json new file mode 100644 index 00000000000000..9a594f70f95c39 --- /dev/null +++ b/homeassistant/components/lg_thinq/manifest.json @@ -0,0 +1,11 @@ +{ + "domain": "lg_thinq", + "name": "LG ThinQ", + "codeowners": ["@LG-ThinQ-Integration"], + "config_flow": true, + "dependencies": [], + "documentation": "https://www.home-assistant.io/integrations/lg_thinq/", + "iot_class": "cloud_push", + "loggers": ["thinqconnect"], + "requirements": ["thinqconnect==0.9.6"] +} diff --git a/homeassistant/components/lg_thinq/strings.json b/homeassistant/components/lg_thinq/strings.json new file mode 100644 index 00000000000000..6649c6b0c13afc --- /dev/null +++ b/homeassistant/components/lg_thinq/strings.json @@ -0,0 +1,48 @@ +{ + "config": { + "abort": { + "already_configured": "[%key:common::config_flow::abort::already_configured_device%]", + "already_in_progress": "[%key:common::config_flow::abort::already_in_progress%]" + }, + "error": { + "token_unauthorized": "The token is invalid or unauthorized." + }, + "step": { + "user": { + "title": "Connect to ThinQ", + "description": "Please enter a ThinQ [PAT(Personal Access Token)]({pat_url}) created with your LG ThinQ account.", + "data": { + "access_token": "Personal Access Token", + "country": "Country" + } + } + } + }, + "entity": { + "switch": { + "operation_power": { + "name": "Power" + } + }, + "binary_sensor": { + "eco_friendly_mode": { + "name": "Eco friendly" + }, + "power_save_enabled": { + "name": "Power saving mode" + }, + "remote_control_enabled": { + "name": "Remote start" + }, + "remote_control_enabled_for_location": { + "name": "{location} remote start" + }, + "rinse_refill": { + "name": "Rinse refill needed" + }, + "sabbath_mode": { + "name": "Sabbath" + } + } + } +} diff --git a/homeassistant/components/lg_thinq/switch.py b/homeassistant/components/lg_thinq/switch.py new file mode 100644 index 00000000000000..ef85c8ad50e1dc --- /dev/null +++ b/homeassistant/components/lg_thinq/switch.py @@ -0,0 +1,104 @@ +"""Support for switch entities.""" + +from __future__ import annotations + +import logging +from typing import Any + +from thinqconnect import DeviceType +from thinqconnect.devices.const import Property as ThinQProperty + +from homeassistant.components.switch import ( + SwitchDeviceClass, + SwitchEntity, + SwitchEntityDescription, +) +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from . import ThinqConfigEntry +from .entity import ThinQEntity + +DEVICE_TYPE_SWITCH_MAP: dict[DeviceType, tuple[SwitchEntityDescription, ...]] = { + DeviceType.AIR_PURIFIER_FAN: ( + SwitchEntityDescription( + key=ThinQProperty.AIR_FAN_OPERATION_MODE, translation_key="operation_power" + ), + ), + DeviceType.AIR_PURIFIER: ( + SwitchEntityDescription( + key=ThinQProperty.AIR_PURIFIER_OPERATION_MODE, + translation_key="operation_power", + ), + ), + DeviceType.DEHUMIDIFIER: ( + SwitchEntityDescription( + key=ThinQProperty.DEHUMIDIFIER_OPERATION_MODE, + translation_key="operation_power", + ), + ), + DeviceType.HUMIDIFIER: ( + SwitchEntityDescription( + key=ThinQProperty.HUMIDIFIER_OPERATION_MODE, + translation_key="operation_power", + ), + ), + DeviceType.SYSTEM_BOILER: ( + SwitchEntityDescription( + key=ThinQProperty.BOILER_OPERATION_MODE, translation_key="operation_power" + ), + ), +} + +_LOGGER = logging.getLogger(__name__) + + +async def async_setup_entry( + hass: HomeAssistant, + entry: ThinqConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up an entry for switch platform.""" + entities: list[ThinQSwitchEntity] = [] + for coordinator in entry.runtime_data.values(): + if ( + descriptions := DEVICE_TYPE_SWITCH_MAP.get( + coordinator.api.device.device_type + ) + ) is not None: + for description in descriptions: + entities.extend( + ThinQSwitchEntity(coordinator, description, property_id) + for property_id in coordinator.api.get_active_idx(description.key) + ) + + if entities: + async_add_entities(entities) + + +class ThinQSwitchEntity(ThinQEntity, SwitchEntity): + """Represent a thinq switch platform.""" + + _attr_device_class = SwitchDeviceClass.SWITCH + + def _update_status(self) -> None: + """Update status itself.""" + super()._update_status() + + _LOGGER.debug( + "[%s:%s] update status: %s", + self.coordinator.device_name, + self.property_id, + self.data.is_on, + ) + self._attr_is_on = self.data.is_on + + async def async_turn_on(self, **kwargs: Any) -> None: + """Turn on the switch.""" + _LOGGER.debug("[%s] async_turn_on", self.name) + await self.async_call_api(self.coordinator.api.async_turn_on(self.property_id)) + + async def async_turn_off(self, **kwargs: Any) -> None: + """Turn off the switch.""" + _LOGGER.debug("[%s] async_turn_off", self.name) + await self.async_call_api(self.coordinator.api.async_turn_off(self.property_id)) diff --git a/homeassistant/components/lidarr/config_flow.py b/homeassistant/components/lidarr/config_flow.py index 05d6900bb41063..bc7a40c976ee7a 100644 --- a/homeassistant/components/lidarr/config_flow.py +++ b/homeassistant/components/lidarr/config_flow.py @@ -29,7 +29,7 @@ def __init__(self) -> None: self.entry: LidarrConfigEntry | None = None async def async_step_reauth( - self, user_input: Mapping[str, Any] + self, entry_data: Mapping[str, Any] ) -> ConfigFlowResult: """Handle configuration by re-auth.""" self.entry = self.hass.config_entries.async_get_entry(self.context["entry_id"]) diff --git a/homeassistant/components/lifx/icons.json b/homeassistant/components/lifx/icons.json index e32fdb5e06bf3a..58a7c89e266264 100644 --- a/homeassistant/components/lifx/icons.json +++ b/homeassistant/components/lifx/icons.json @@ -1,13 +1,31 @@ { "services": { - "set_hev_cycle_state": "mdi:led-on", - "set_state": "mdi:led-on", - "effect_pulse": "mdi:pulse", - "effect_colorloop": "mdi:looks", - "effect_move": "mdi:cube-send", - "effect_flame": "mdi:fire", - "effect_morph": "mdi:shape-outline", - "effect_sky": "mdi:clouds", - "effect_stop": "mdi:stop" + "set_hev_cycle_state": { + "service": "mdi:led-on" + }, + "set_state": { + "service": "mdi:led-on" + }, + "effect_pulse": { + "service": "mdi:pulse" + }, + "effect_colorloop": { + "service": "mdi:looks" + }, + "effect_move": { + "service": "mdi:cube-send" + }, + "effect_flame": { + "service": "mdi:fire" + }, + "effect_morph": { + "service": "mdi:shape-outline" + }, + "effect_sky": { + "service": "mdi:clouds" + }, + "effect_stop": { + "service": "mdi:stop" + } } } diff --git a/homeassistant/components/lifx/manifest.json b/homeassistant/components/lifx/manifest.json index 54cff7d6e1fdc9..3ef70f1646705e 100644 --- a/homeassistant/components/lifx/manifest.json +++ b/homeassistant/components/lifx/manifest.json @@ -48,7 +48,7 @@ "iot_class": "local_polling", "loggers": ["aiolifx", "aiolifx_effects", "bitstring"], "requirements": [ - "aiolifx==1.0.6", + "aiolifx==1.0.9", "aiolifx-effects==0.3.2", "aiolifx-themes==0.5.0" ] diff --git a/homeassistant/components/light/icons.json b/homeassistant/components/light/icons.json index 5113834e575a55..df98def090e480 100644 --- a/homeassistant/components/light/icons.json +++ b/homeassistant/components/light/icons.json @@ -5,8 +5,14 @@ } }, "services": { - "toggle": "mdi:lightbulb", - "turn_off": "mdi:lightbulb-off", - "turn_on": "mdi:lightbulb-on" + "toggle": { + "service": "mdi:lightbulb" + }, + "turn_off": { + "service": "mdi:lightbulb-off" + }, + "turn_on": { + "service": "mdi:lightbulb-on" + } } } diff --git a/homeassistant/components/linkplay/__init__.py b/homeassistant/components/linkplay/__init__.py index c0fe711a61b2fb..808f2f93ce2a7d 100644 --- a/homeassistant/components/linkplay/__init__.py +++ b/homeassistant/components/linkplay/__init__.py @@ -1,17 +1,22 @@ """Support for LinkPlay devices.""" +from dataclasses import dataclass + +from aiohttp import ClientSession from linkplay.bridge import LinkPlayBridge -from linkplay.discovery import linkplay_factory_bridge +from linkplay.discovery import linkplay_factory_httpapi_bridge +from linkplay.exceptions import LinkPlayRequestException from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_HOST from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady -from homeassistant.helpers.aiohttp_client import async_get_clientsession from .const import PLATFORMS +from .utils import async_get_client_session +@dataclass class LinkPlayData: """Data for LinkPlay.""" @@ -24,16 +29,17 @@ class LinkPlayData: async def async_setup_entry(hass: HomeAssistant, entry: LinkPlayConfigEntry) -> bool: """Async setup hass config entry. Called when an entry has been setup.""" - session = async_get_clientsession(hass) - if ( - bridge := await linkplay_factory_bridge(entry.data[CONF_HOST], session) - ) is None: + session: ClientSession = await async_get_client_session(hass) + bridge: LinkPlayBridge | None = None + + try: + bridge = await linkplay_factory_httpapi_bridge(entry.data[CONF_HOST], session) + except LinkPlayRequestException as exception: raise ConfigEntryNotReady( f"Failed to connect to LinkPlay device at {entry.data[CONF_HOST]}" - ) + ) from exception - entry.runtime_data = LinkPlayData() - entry.runtime_data.bridge = bridge + entry.runtime_data = LinkPlayData(bridge=bridge) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) return True diff --git a/homeassistant/components/linkplay/config_flow.py b/homeassistant/components/linkplay/config_flow.py index 0f9c40d0fd4be5..7dfdce238ff20f 100644 --- a/homeassistant/components/linkplay/config_flow.py +++ b/homeassistant/components/linkplay/config_flow.py @@ -1,16 +1,22 @@ """Config flow to configure LinkPlay component.""" +import logging from typing import Any -from linkplay.discovery import linkplay_factory_bridge +from aiohttp import ClientSession +from linkplay.bridge import LinkPlayBridge +from linkplay.discovery import linkplay_factory_httpapi_bridge +from linkplay.exceptions import LinkPlayRequestException import voluptuous as vol from homeassistant.components import zeroconf from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_HOST, CONF_MODEL -from homeassistant.helpers.aiohttp_client import async_get_clientsession from .const import DOMAIN +from .utils import async_get_client_session + +_LOGGER = logging.getLogger(__name__) class LinkPlayConfigFlow(ConfigFlow, domain=DOMAIN): @@ -25,10 +31,15 @@ async def async_step_zeroconf( ) -> ConfigFlowResult: """Handle Zeroconf discovery.""" - session = async_get_clientsession(self.hass) - bridge = await linkplay_factory_bridge(discovery_info.host, session) + session: ClientSession = await async_get_client_session(self.hass) + bridge: LinkPlayBridge | None = None - if bridge is None: + try: + bridge = await linkplay_factory_httpapi_bridge(discovery_info.host, session) + except LinkPlayRequestException: + _LOGGER.exception( + "Failed to connect to LinkPlay device at %s", discovery_info.host + ) return self.async_abort(reason="cannot_connect") self.data[CONF_HOST] = discovery_info.host @@ -66,14 +77,26 @@ async def async_step_user( """Handle a flow initialized by the user.""" errors: dict[str, str] = {} if user_input: - session = async_get_clientsession(self.hass) - bridge = await linkplay_factory_bridge(user_input[CONF_HOST], session) + session: ClientSession = await async_get_client_session(self.hass) + bridge: LinkPlayBridge | None = None + + try: + bridge = await linkplay_factory_httpapi_bridge( + user_input[CONF_HOST], session + ) + except LinkPlayRequestException: + _LOGGER.exception( + "Failed to connect to LinkPlay device at %s", user_input[CONF_HOST] + ) + errors["base"] = "cannot_connect" if bridge is not None: self.data[CONF_HOST] = user_input[CONF_HOST] self.data[CONF_MODEL] = bridge.device.name - await self.async_set_unique_id(bridge.device.uuid) + await self.async_set_unique_id( + bridge.device.uuid, raise_on_progress=False + ) self._abort_if_unique_id_configured( updates={CONF_HOST: self.data[CONF_HOST]} ) @@ -83,7 +106,6 @@ async def async_step_user( data={CONF_HOST: self.data[CONF_HOST]}, ) - errors["base"] = "cannot_connect" return self.async_show_form( step_id="user", data_schema=vol.Schema({vol.Required(CONF_HOST): str}), diff --git a/homeassistant/components/linkplay/const.py b/homeassistant/components/linkplay/const.py index 48ae225dd98e24..91a427d5eb8151 100644 --- a/homeassistant/components/linkplay/const.py +++ b/homeassistant/components/linkplay/const.py @@ -4,3 +4,4 @@ DOMAIN = "linkplay" PLATFORMS = [Platform.MEDIA_PLAYER] +CONF_SESSION = "session" diff --git a/homeassistant/components/linkplay/manifest.json b/homeassistant/components/linkplay/manifest.json index 5212f3f99b8e52..66a719c640ea31 100644 --- a/homeassistant/components/linkplay/manifest.json +++ b/homeassistant/components/linkplay/manifest.json @@ -6,6 +6,6 @@ "documentation": "https://www.home-assistant.io/integrations/linkplay", "integration_type": "hub", "iot_class": "local_polling", - "requirements": ["python-linkplay==0.0.8"], + "requirements": ["python-linkplay==0.0.9"], "zeroconf": ["_linkplay._tcp.local."] } diff --git a/homeassistant/components/linkplay/media_player.py b/homeassistant/components/linkplay/media_player.py index 398add235bd789..8b2fcf5d52f863 100644 --- a/homeassistant/components/linkplay/media_player.py +++ b/homeassistant/components/linkplay/media_player.py @@ -48,6 +48,18 @@ PlayingMode.XLR: "XLR", PlayingMode.HDMI: "HDMI", PlayingMode.OPTICAL_2: "Optical 2", + PlayingMode.EXTERN_BLUETOOTH: "External Bluetooth", + PlayingMode.PHONO: "Phono", + PlayingMode.ARC: "ARC", + PlayingMode.COAXIAL_2: "Coaxial 2", + PlayingMode.TF_CARD_1: "SD Card 1", + PlayingMode.TF_CARD_2: "SD Card 2", + PlayingMode.CD: "CD", + PlayingMode.DAB: "DAB Radio", + PlayingMode.FM: "FM Radio", + PlayingMode.RCA: "RCA", + PlayingMode.UDISK: "USB", + PlayingMode.FOLLOWER: "Follower", } SOURCE_MAP_INV: dict[str, PlayingMode] = {v: k for k, v in SOURCE_MAP.items()} diff --git a/homeassistant/components/linkplay/utils.py b/homeassistant/components/linkplay/utils.py index 7532c9b354a774..7f15e2971456c1 100644 --- a/homeassistant/components/linkplay/utils.py +++ b/homeassistant/components/linkplay/utils.py @@ -2,6 +2,14 @@ from typing import Final +from aiohttp import ClientSession +from linkplay.utils import async_create_unverified_client_session + +from homeassistant.const import EVENT_HOMEASSISTANT_CLOSE +from homeassistant.core import Event, HomeAssistant, callback + +from .const import CONF_SESSION, DOMAIN + MANUFACTURER_ARTSOUND: Final[str] = "ArtSound" MANUFACTURER_ARYLIC: Final[str] = "Arylic" MANUFACTURER_IEAST: Final[str] = "iEAST" @@ -44,3 +52,22 @@ def get_info_from_project(project: str) -> tuple[str, str]: return MANUFACTURER_IEAST, MODELS_IEAST_AUDIOCAST_M5 case _: return MANUFACTURER_GENERIC, MODELS_GENERIC + + +async def async_get_client_session(hass: HomeAssistant) -> ClientSession: + """Get a ClientSession that can be used with LinkPlay devices.""" + hass.data.setdefault(DOMAIN, {}) + if CONF_SESSION not in hass.data[DOMAIN]: + clientsession: ClientSession = await async_create_unverified_client_session() + + @callback + def _async_close_websession(event: Event) -> None: + """Close websession.""" + clientsession.detach() + + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_CLOSE, _async_close_websession) + hass.data[DOMAIN][CONF_SESSION] = clientsession + return clientsession + + session: ClientSession = hass.data[DOMAIN][CONF_SESSION] + return session diff --git a/homeassistant/components/litterrobot/icons.json b/homeassistant/components/litterrobot/icons.json index 333f309e9e8a31..482031f8424fdc 100644 --- a/homeassistant/components/litterrobot/icons.json +++ b/homeassistant/components/litterrobot/icons.json @@ -40,6 +40,8 @@ } }, "services": { - "set_sleep_mode": "mdi:sleep" + "set_sleep_mode": { + "service": "mdi:sleep" + } } } diff --git a/homeassistant/components/local_file/icons.json b/homeassistant/components/local_file/icons.json index c9c92fa86c8d70..7b0067c6a44e3f 100644 --- a/homeassistant/components/local_file/icons.json +++ b/homeassistant/components/local_file/icons.json @@ -1,5 +1,7 @@ { "services": { - "update_file_path": "mdi:cog" + "update_file_path": { + "service": "mdi:cog" + } } } diff --git a/homeassistant/components/lock/icons.json b/homeassistant/components/lock/icons.json index 009bd84a372903..0b1befde9ff409 100644 --- a/homeassistant/components/lock/icons.json +++ b/homeassistant/components/lock/icons.json @@ -13,8 +13,14 @@ } }, "services": { - "lock": "mdi:lock", - "open": "mdi:door-open", - "unlock": "mdi:lock-open-variant" + "lock": { + "service": "mdi:lock" + }, + "open": { + "service": "mdi:door-open" + }, + "unlock": { + "service": "mdi:lock-open-variant" + } } } diff --git a/homeassistant/components/logbook/icons.json b/homeassistant/components/logbook/icons.json index cd2cde8600c65c..a8af6427b8c7a2 100644 --- a/homeassistant/components/logbook/icons.json +++ b/homeassistant/components/logbook/icons.json @@ -1,5 +1,7 @@ { "services": { - "log": "mdi:file-document" + "log": { + "service": "mdi:file-document" + } } } diff --git a/homeassistant/components/logger/icons.json b/homeassistant/components/logger/icons.json index 305dd3ece915dd..1542e1e5ad36e6 100644 --- a/homeassistant/components/logger/icons.json +++ b/homeassistant/components/logger/icons.json @@ -1,6 +1,10 @@ { "services": { - "set_default_level": "mdi:cog-outline", - "set_level": "mdi:cog-outline" + "set_default_level": { + "service": "mdi:cog-outline" + }, + "set_level": { + "service": "mdi:cog-outline" + } } } diff --git a/homeassistant/components/lovelace/icons.json b/homeassistant/components/lovelace/icons.json index fe0a0e114aeecd..8261dc2d0c9da9 100644 --- a/homeassistant/components/lovelace/icons.json +++ b/homeassistant/components/lovelace/icons.json @@ -1,5 +1,7 @@ { "services": { - "reload_resources": "mdi:reload" + "reload_resources": { + "service": "mdi:reload" + } } } diff --git a/homeassistant/components/lutron_caseta/config_flow.py b/homeassistant/components/lutron_caseta/config_flow.py index d7b47aebc7e3cd..cd566b767fb0e4 100644 --- a/homeassistant/components/lutron_caseta/config_flow.py +++ b/homeassistant/components/lutron_caseta/config_flow.py @@ -6,6 +6,7 @@ import logging import os import ssl +from typing import Any from pylutron_caseta.pairing import PAIR_CA, PAIR_CERT, PAIR_KEY, async_pair from pylutron_caseta.smartbridge import Smartbridge @@ -50,14 +51,16 @@ class LutronCasetaFlowHandler(ConfigFlow, domain=DOMAIN): VERSION = 1 - def __init__(self): + def __init__(self) -> None: """Initialize a Lutron Caseta flow.""" - self.data = {} - self.lutron_id = None + self.data: dict[str, Any] = {} + self.lutron_id: str | None = None self.tls_assets_validated = False self.attempted_tls_validation = False - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle a flow initialized by the user.""" if user_input is not None: self.data[CONF_HOST] = user_input[CONF_HOST] @@ -92,7 +95,9 @@ async def async_step_homekit( """Handle a flow initialized by homekit discovery.""" return await self.async_step_zeroconf(discovery_info) - async def async_step_link(self, user_input=None): + async def async_step_link( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle pairing with the hub.""" errors = {} # Abort if existing entry with matching host exists. @@ -163,21 +168,21 @@ def _configure_tls_assets(self): for asset_key, conf_key in FILE_MAPPING.items(): self.data[conf_key] = TLS_ASSET_TEMPLATE.format(self.bridge_id, asset_key) - async def async_step_import(self, import_info): + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Import a new Caseta bridge as a config entry. This flow is triggered by `async_setup`. """ - host = import_info[CONF_HOST] + host = import_data[CONF_HOST] # Store the imported config for other steps in this flow to access. self.data[CONF_HOST] = host # Abort if existing entry with matching host exists. self._async_abort_entries_match({CONF_HOST: self.data[CONF_HOST]}) - self.data[CONF_KEYFILE] = import_info[CONF_KEYFILE] - self.data[CONF_CERTFILE] = import_info[CONF_CERTFILE] - self.data[CONF_CA_CERTS] = import_info[CONF_CA_CERTS] + self.data[CONF_KEYFILE] = import_data[CONF_KEYFILE] + self.data[CONF_CERTFILE] = import_data[CONF_CERTFILE] + self.data[CONF_CA_CERTS] = import_data[CONF_CA_CERTS] if not (lutron_id := await self.async_get_lutron_id()): # Ultimately we won't have a dedicated step for import failure, but @@ -195,7 +200,9 @@ async def async_step_import(self, import_info): self._abort_if_unique_id_configured() return self.async_create_entry(title=ENTRY_DEFAULT_TITLE, data=self.data) - async def async_step_import_failed(self, user_input=None): + async def async_step_import_failed( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Make failed import surfaced to user.""" self.context["title_placeholders"] = {CONF_NAME: self.data[CONF_HOST]} diff --git a/homeassistant/components/lyric/icons.json b/homeassistant/components/lyric/icons.json index 555215f8685edb..edb61c3f8e2bd3 100644 --- a/homeassistant/components/lyric/icons.json +++ b/homeassistant/components/lyric/icons.json @@ -7,6 +7,8 @@ } }, "services": { - "set_hold_time": "mdi:timer-pause" + "set_hold_time": { + "service": "mdi:timer-pause" + } } } diff --git a/homeassistant/components/madvr/diagnostics.py b/homeassistant/components/madvr/diagnostics.py new file mode 100644 index 00000000000000..f6261d273059e8 --- /dev/null +++ b/homeassistant/components/madvr/diagnostics.py @@ -0,0 +1,25 @@ +"""Provides diagnostics for madVR.""" + +from __future__ import annotations + +from typing import Any + +from homeassistant.components.diagnostics import async_redact_data +from homeassistant.const import CONF_HOST +from homeassistant.core import HomeAssistant + +from . import MadVRConfigEntry + +TO_REDACT = [CONF_HOST] + + +async def async_get_config_entry_diagnostics( + hass: HomeAssistant, config_entry: MadVRConfigEntry +) -> dict[str, Any]: + """Return diagnostics for a config entry.""" + data = config_entry.runtime_data.data + + return { + "config_entry": async_redact_data(config_entry.as_dict(), TO_REDACT), + "madvr_data": data, + } diff --git a/homeassistant/components/madvr/manifest.json b/homeassistant/components/madvr/manifest.json index ce6336acabc7a8..0ac906fdbefa48 100644 --- a/homeassistant/components/madvr/manifest.json +++ b/homeassistant/components/madvr/manifest.json @@ -6,5 +6,5 @@ "documentation": "https://www.home-assistant.io/integrations/madvr", "integration_type": "device", "iot_class": "local_push", - "requirements": ["py-madvr2==1.6.29"] + "requirements": ["py-madvr2==1.6.32"] } diff --git a/homeassistant/components/mailbox/__init__.py b/homeassistant/components/mailbox/__init__.py deleted file mode 100644 index e0438342a547c1..00000000000000 --- a/homeassistant/components/mailbox/__init__.py +++ /dev/null @@ -1,291 +0,0 @@ -"""Support for Voice mailboxes.""" - -from __future__ import annotations - -import asyncio -from contextlib import suppress -from datetime import timedelta -from http import HTTPStatus -import logging -from typing import Any, Final - -from aiohttp import web -from aiohttp.web_exceptions import HTTPNotFound - -from homeassistant.components import frontend -from homeassistant.components.http import HomeAssistantView -from homeassistant.config import config_per_platform -from homeassistant.core import Event, HomeAssistant, callback -from homeassistant.exceptions import HomeAssistantError -from homeassistant.helpers import config_validation as cv, discovery -from homeassistant.helpers.entity import Entity -from homeassistant.helpers.entity_component import EntityComponent -from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue -from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType -from homeassistant.setup import async_prepare_setup_platform - -_LOGGER = logging.getLogger(__name__) - -DOMAIN: Final = "mailbox" - -EVENT: Final = "mailbox_updated" -CONTENT_TYPE_MPEG: Final = "audio/mpeg" -CONTENT_TYPE_NONE: Final = "none" - -SCAN_INTERVAL = timedelta(seconds=30) - -CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN) - - -async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: - """Track states and offer events for mailboxes.""" - mailboxes: list[Mailbox] = [] - frontend.async_register_built_in_panel(hass, "mailbox", "mailbox", "mdi:mailbox") - hass.http.register_view(MailboxPlatformsView(mailboxes)) - hass.http.register_view(MailboxMessageView(mailboxes)) - hass.http.register_view(MailboxMediaView(mailboxes)) - hass.http.register_view(MailboxDeleteView(mailboxes)) - - async def async_setup_platform( - p_type: str, - p_config: ConfigType | None = None, - discovery_info: DiscoveryInfoType | None = None, - ) -> None: - """Set up a mailbox platform.""" - if p_config is None: - p_config = {} - if discovery_info is None: - discovery_info = {} - - platform = await async_prepare_setup_platform(hass, config, DOMAIN, p_type) - - if platform is None: - _LOGGER.error("Unknown mailbox platform specified") - return - - if p_type not in ["asterisk_cdr", "asterisk_mbox", "demo"]: - # Asterisk integration will raise a repair issue themselves - # For demo we don't create one - async_create_issue( - hass, - DOMAIN, - f"deprecated_mailbox_{p_type}", - breaks_in_ha_version="2024.9.0", - is_fixable=False, - issue_domain=DOMAIN, - severity=IssueSeverity.WARNING, - translation_key="deprecated_mailbox_integration", - translation_placeholders={ - "integration_domain": p_type, - }, - ) - - _LOGGER.info("Setting up %s.%s", DOMAIN, p_type) - mailbox = None - try: - if hasattr(platform, "async_get_handler"): - mailbox = await platform.async_get_handler( - hass, p_config, discovery_info - ) - elif hasattr(platform, "get_handler"): - mailbox = await hass.async_add_executor_job( - platform.get_handler, hass, p_config, discovery_info - ) - else: - raise HomeAssistantError("Invalid mailbox platform.") # noqa: TRY301 - - if mailbox is None: - _LOGGER.error("Failed to initialize mailbox platform %s", p_type) - return - - except Exception: - _LOGGER.exception("Error setting up platform %s", p_type) - return - - mailboxes.append(mailbox) - mailbox_entity = MailboxEntity(mailbox) - component = EntityComponent[MailboxEntity]( - logging.getLogger(__name__), DOMAIN, hass, SCAN_INTERVAL - ) - component.register_shutdown() - await component.async_add_entities([mailbox_entity]) - - for p_type, p_config in config_per_platform(config, DOMAIN): - if p_type is not None: - hass.async_create_task( - async_setup_platform(p_type, p_config), eager_start=True - ) - - async def async_platform_discovered( - platform: str, info: DiscoveryInfoType | None - ) -> None: - """Handle for discovered platform.""" - await async_setup_platform(platform, discovery_info=info) - - discovery.async_listen_platform(hass, DOMAIN, async_platform_discovered) - - return True - - -class MailboxEntity(Entity): - """Entity for each mailbox platform to provide a badge display.""" - - def __init__(self, mailbox: Mailbox) -> None: - """Initialize mailbox entity.""" - self.mailbox = mailbox - self.message_count = 0 - - async def async_added_to_hass(self) -> None: - """Complete entity initialization.""" - - @callback - def _mailbox_updated(event: Event) -> None: - self.async_schedule_update_ha_state(True) - - self.hass.bus.async_listen(EVENT, _mailbox_updated) - self.async_schedule_update_ha_state(True) - - @property - def state(self) -> str: - """Return the state of the binary sensor.""" - return str(self.message_count) - - @property - def name(self) -> str: - """Return the name of the entity.""" - return self.mailbox.name - - async def async_update(self) -> None: - """Retrieve messages from platform.""" - messages = await self.mailbox.async_get_messages() - self.message_count = len(messages) - - -class Mailbox: - """Represent a mailbox device.""" - - def __init__(self, hass: HomeAssistant, name: str) -> None: - """Initialize mailbox object.""" - self.hass = hass - self.name = name - - @callback - def async_update(self) -> None: - """Send event notification of updated mailbox.""" - self.hass.bus.async_fire(EVENT) - - @property - def media_type(self) -> str: - """Return the supported media type.""" - raise NotImplementedError - - @property - def can_delete(self) -> bool: - """Return if messages can be deleted.""" - return False - - @property - def has_media(self) -> bool: - """Return if messages have attached media files.""" - return False - - async def async_get_media(self, msgid: str) -> bytes: - """Return the media blob for the msgid.""" - raise NotImplementedError - - async def async_get_messages(self) -> list[dict[str, Any]]: - """Return a list of the current messages.""" - raise NotImplementedError - - async def async_delete(self, msgid: str) -> bool: - """Delete the specified messages.""" - raise NotImplementedError - - -class StreamError(Exception): - """Media streaming exception.""" - - -class MailboxView(HomeAssistantView): - """Base mailbox view.""" - - def __init__(self, mailboxes: list[Mailbox]) -> None: - """Initialize a basic mailbox view.""" - self.mailboxes = mailboxes - - def get_mailbox(self, platform: str) -> Mailbox: - """Retrieve the specified mailbox.""" - for mailbox in self.mailboxes: - if mailbox.name == platform: - return mailbox - raise HTTPNotFound - - -class MailboxPlatformsView(MailboxView): - """View to return the list of mailbox platforms.""" - - url = "/api/mailbox/platforms" - name = "api:mailbox:platforms" - - async def get(self, request: web.Request) -> web.Response: - """Retrieve list of platforms.""" - return self.json( - [ - { - "name": mailbox.name, - "has_media": mailbox.has_media, - "can_delete": mailbox.can_delete, - } - for mailbox in self.mailboxes - ] - ) - - -class MailboxMessageView(MailboxView): - """View to return the list of messages.""" - - url = "/api/mailbox/messages/{platform}" - name = "api:mailbox:messages" - - async def get(self, request: web.Request, platform: str) -> web.Response: - """Retrieve messages.""" - mailbox = self.get_mailbox(platform) - messages = await mailbox.async_get_messages() - return self.json(messages) - - -class MailboxDeleteView(MailboxView): - """View to delete selected messages.""" - - url = "/api/mailbox/delete/{platform}/{msgid}" - name = "api:mailbox:delete" - - async def delete(self, request: web.Request, platform: str, msgid: str) -> None: - """Delete items.""" - mailbox = self.get_mailbox(platform) - await mailbox.async_delete(msgid) - - -class MailboxMediaView(MailboxView): - """View to return a media file.""" - - url = r"/api/mailbox/media/{platform}/{msgid}" - name = "api:asteriskmbox:media" - - async def get( - self, request: web.Request, platform: str, msgid: str - ) -> web.Response: - """Retrieve media.""" - mailbox = self.get_mailbox(platform) - - with suppress(asyncio.CancelledError, TimeoutError): - async with asyncio.timeout(10): - try: - stream = await mailbox.async_get_media(msgid) - except StreamError as err: - _LOGGER.error("Error getting media: %s", err) - return web.Response(status=HTTPStatus.INTERNAL_SERVER_ERROR) - if stream: - return web.Response(body=stream, content_type=mailbox.media_type) - - return web.Response(status=HTTPStatus.INTERNAL_SERVER_ERROR) diff --git a/homeassistant/components/mailbox/manifest.json b/homeassistant/components/mailbox/manifest.json deleted file mode 100644 index 43dd133654c50b..00000000000000 --- a/homeassistant/components/mailbox/manifest.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "domain": "mailbox", - "name": "Mailbox", - "codeowners": [], - "dependencies": ["http"], - "documentation": "https://www.home-assistant.io/integrations/mailbox", - "integration_type": "entity", - "quality_scale": "internal" -} diff --git a/homeassistant/components/mailbox/strings.json b/homeassistant/components/mailbox/strings.json deleted file mode 100644 index 01746e3e98d117..00000000000000 --- a/homeassistant/components/mailbox/strings.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "title": "Mailbox", - "issues": { - "deprecated_mailbox": { - "title": "The mailbox platform is being removed", - "description": "The mailbox platform is being removed. Please report it to the author of the \"{integration_domain}\" custom integration." - } - } -} diff --git a/homeassistant/components/mastodon/__init__.py b/homeassistant/components/mastodon/__init__.py index 77e66b6e45ca9c..e8d2343424846d 100644 --- a/homeassistant/components/mastodon/__init__.py +++ b/homeassistant/components/mastodon/__init__.py @@ -97,14 +97,13 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: LOGGER.error("Migration failed with error %s", ex) return False - entry.minor_version = 2 - hass.config_entries.async_update_entry( entry, + minor_version=2, unique_id=slugify(construct_mastodon_username(instance, account)), ) - LOGGER.info( + LOGGER.debug( "Entry %s successfully migrated to version %s.%s", entry.entry_id, entry.version, diff --git a/homeassistant/components/mastodon/config_flow.py b/homeassistant/components/mastodon/config_flow.py index 4e856275736671..5c9419cd12df13 100644 --- a/homeassistant/components/mastodon/config_flow.py +++ b/homeassistant/components/mastodon/config_flow.py @@ -19,7 +19,6 @@ TextSelectorConfig, TextSelectorType, ) -from homeassistant.helpers.typing import ConfigType from homeassistant.util import slugify from .const import CONF_BASE_URL, DEFAULT_URL, DOMAIN, LOGGER @@ -126,17 +125,17 @@ async def async_step_user( return self.show_user_form(user_input, errors) - async def async_step_import(self, import_config: ConfigType) -> ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Import a config entry from configuration.yaml.""" errors: dict[str, str] | None = None LOGGER.debug("Importing Mastodon from configuration.yaml") - base_url = str(import_config.get(CONF_BASE_URL, DEFAULT_URL)) - client_id = str(import_config.get(CONF_CLIENT_ID)) - client_secret = str(import_config.get(CONF_CLIENT_SECRET)) - access_token = str(import_config.get(CONF_ACCESS_TOKEN)) - name = import_config.get(CONF_NAME, None) + base_url = str(import_data.get(CONF_BASE_URL, DEFAULT_URL)) + client_id = str(import_data.get(CONF_CLIENT_ID)) + client_secret = str(import_data.get(CONF_CLIENT_SECRET)) + access_token = str(import_data.get(CONF_ACCESS_TOKEN)) + name = import_data.get(CONF_NAME) instance, account, errors = await self.hass.async_add_executor_job( self.check_connection, diff --git a/homeassistant/components/matrix/icons.json b/homeassistant/components/matrix/icons.json index 4fc56ebe0ff0b7..a8b83e67303fb7 100644 --- a/homeassistant/components/matrix/icons.json +++ b/homeassistant/components/matrix/icons.json @@ -1,5 +1,7 @@ { "services": { - "send_message": "mdi:matrix" + "send_message": { + "service": "mdi:matrix" + } } } diff --git a/homeassistant/components/matter/light.py b/homeassistant/components/matter/light.py index 6e9019c46fa8d9..bcac945562adf4 100644 --- a/homeassistant/components/matter/light.py +++ b/homeassistant/components/matter/light.py @@ -60,10 +60,13 @@ (4456, 1011, "1.0.0", "2.00.00"), (4488, 260, "1.0", "1.0.0"), (4488, 514, "1.0", "1.0.0"), + (4921, 42, "1.0", "1.01.060"), + (4921, 43, "1.0", "1.01.060"), (4999, 24875, "1.0", "27.0"), (4999, 25057, "1.0", "27.0"), (5009, 514, "1.0", "1.0.0"), (5010, 769, "3.0", "1.0.0"), + (5130, 544, "v0.4", "6.7.196e9d4e08-14"), ) diff --git a/homeassistant/components/matter/select.py b/homeassistant/components/matter/select.py index 4a9ef3780d1cc1..b46cad53123f58 100644 --- a/homeassistant/components/matter/select.py +++ b/homeassistant/components/matter/select.py @@ -229,12 +229,12 @@ def _update_from_device(self) -> None: entity_category=EntityCategory.CONFIG, translation_key="startup_on_off", options=["On", "Off", "Toggle", "Previous"], - measurement_to_ha=lambda x: { + measurement_to_ha=lambda x: { # pylint: disable=unnecessary-lambda 0: "Off", 1: "On", 2: "Toggle", None: "Previous", - }[x], + }.get(x), ha_to_native_value=lambda x: { "Off": 0, "On": 1, diff --git a/homeassistant/components/matter/sensor.py b/homeassistant/components/matter/sensor.py index c3ab18072f089b..5d4ad900d8ece8 100644 --- a/homeassistant/components/matter/sensor.py +++ b/homeassistant/components/matter/sensor.py @@ -384,7 +384,7 @@ def _update_from_device(self) -> None: key="ThirdRealityEnergySensorWattAccumulated", device_class=SensorDeviceClass.ENERGY, entity_category=EntityCategory.DIAGNOSTIC, - native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, suggested_display_precision=3, state_class=SensorStateClass.TOTAL_INCREASING, measurement_to_ha=lambda x: x / 1000, diff --git a/homeassistant/components/mealie/__init__.py b/homeassistant/components/mealie/__init__.py index 5c9c91729c0082..bf0fbcac406bf7 100644 --- a/homeassistant/components/mealie/__init__.py +++ b/homeassistant/components/mealie/__init__.py @@ -48,6 +48,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: MealieConfigEntry) -> bo ), ) try: + await client.define_household_support() about = await client.get_about() version = create_version(about.version) except MealieAuthenticationError as error: diff --git a/homeassistant/components/mealie/icons.json b/homeassistant/components/mealie/icons.json index 16176391701c25..d7e29cc8bbec06 100644 --- a/homeassistant/components/mealie/icons.json +++ b/homeassistant/components/mealie/icons.json @@ -24,10 +24,20 @@ } }, "services": { - "get_mealplan": "mdi:food", - "get_recipe": "mdi:map", - "import_recipe": "mdi:map-search", - "set_random_mealplan": "mdi:dice-multiple", - "set_mealplan": "mdi:food" + "get_mealplan": { + "service": "mdi:food" + }, + "get_recipe": { + "service": "mdi:map" + }, + "import_recipe": { + "service": "mdi:map-search" + }, + "set_random_mealplan": { + "service": "mdi:dice-multiple" + }, + "set_mealplan": { + "service": "mdi:food" + } } } diff --git a/homeassistant/components/mealie/manifest.json b/homeassistant/components/mealie/manifest.json index 75093577b0fb7d..4fabdffadc4356 100644 --- a/homeassistant/components/mealie/manifest.json +++ b/homeassistant/components/mealie/manifest.json @@ -6,5 +6,5 @@ "documentation": "https://www.home-assistant.io/integrations/mealie", "integration_type": "service", "iot_class": "local_polling", - "requirements": ["aiomealie==0.8.1"] + "requirements": ["aiomealie==0.9.2"] } diff --git a/homeassistant/components/media_extractor/config_flow.py b/homeassistant/components/media_extractor/config_flow.py index 4343d0551e09de..b91942d7b13dca 100644 --- a/homeassistant/components/media_extractor/config_flow.py +++ b/homeassistant/components/media_extractor/config_flow.py @@ -25,8 +25,6 @@ async def async_step_user( return self.async_show_form(step_id="user", data_schema=vol.Schema({})) - async def async_step_import( - self, import_config: dict[str, Any] - ) -> ConfigFlowResult: + async def async_step_import(self, import_data: None) -> ConfigFlowResult: """Handle import.""" return self.async_create_entry(title="Media extractor", data={}) diff --git a/homeassistant/components/media_extractor/icons.json b/homeassistant/components/media_extractor/icons.json index 7abc4410b1956e..611db7c944cfd0 100644 --- a/homeassistant/components/media_extractor/icons.json +++ b/homeassistant/components/media_extractor/icons.json @@ -1,6 +1,10 @@ { "services": { - "play_media": "mdi:play", - "extract_media_url": "mdi:link" + "play_media": { + "service": "mdi:play" + }, + "extract_media_url": { + "service": "mdi:link" + } } } diff --git a/homeassistant/components/media_player/icons.json b/homeassistant/components/media_player/icons.json index 847ce5989d6442..c11211c38ec384 100644 --- a/homeassistant/components/media_player/icons.json +++ b/homeassistant/components/media_player/icons.json @@ -32,27 +32,71 @@ } }, "services": { - "clear_playlist": "mdi:playlist-remove", - "join": "mdi:group", - "media_next_track": "mdi:skip-next", - "media_pause": "mdi:pause", - "media_play": "mdi:play", - "media_play_pause": "mdi:play-pause", - "media_previous_track": "mdi:skip-previous", - "media_seek": "mdi:fast-forward", - "media_stop": "mdi:stop", - "play_media": "mdi:play", - "repeat_set": "mdi:repeat", - "select_sound_mode": "mdi:surround-sound", - "select_source": "mdi:import", - "shuffle_set": "mdi:shuffle", - "toggle": "mdi:play-pause", - "turn_off": "mdi:power", - "turn_on": "mdi:power", - "unjoin": "mdi:ungroup", - "volume_down": "mdi:volume-minus", - "volume_mute": "mdi:volume-mute", - "volume_set": "mdi:volume-medium", - "volume_up": "mdi:volume-plus" + "clear_playlist": { + "service": "mdi:playlist-remove" + }, + "join": { + "service": "mdi:group" + }, + "media_next_track": { + "service": "mdi:skip-next" + }, + "media_pause": { + "service": "mdi:pause" + }, + "media_play": { + "service": "mdi:play" + }, + "media_play_pause": { + "service": "mdi:play-pause" + }, + "media_previous_track": { + "service": "mdi:skip-previous" + }, + "media_seek": { + "service": "mdi:fast-forward" + }, + "media_stop": { + "service": "mdi:stop" + }, + "play_media": { + "service": "mdi:play" + }, + "repeat_set": { + "service": "mdi:repeat" + }, + "select_sound_mode": { + "service": "mdi:surround-sound" + }, + "select_source": { + "service": "mdi:import" + }, + "shuffle_set": { + "service": "mdi:shuffle" + }, + "toggle": { + "service": "mdi:play-pause" + }, + "turn_off": { + "service": "mdi:power" + }, + "turn_on": { + "service": "mdi:power" + }, + "unjoin": { + "service": "mdi:ungroup" + }, + "volume_down": { + "service": "mdi:volume-minus" + }, + "volume_mute": { + "service": "mdi:volume-mute" + }, + "volume_set": { + "service": "mdi:volume-medium" + }, + "volume_up": { + "service": "mdi:volume-plus" + } } } diff --git a/homeassistant/components/media_source/__init__.py b/homeassistant/components/media_source/__init__.py index 928e46ab528680..732a1d834f0d75 100644 --- a/homeassistant/components/media_source/__init__.py +++ b/homeassistant/components/media_source/__init__.py @@ -160,7 +160,7 @@ async def async_resolve_media( if target_media_player is UNDEFINED: report( "calls media_source.async_resolve_media without passing an entity_id", - {DOMAIN}, + exclude_integrations={DOMAIN}, ) target_media_player = None diff --git a/homeassistant/components/melcloud/icons.json b/homeassistant/components/melcloud/icons.json index de3eb3c0ba2c97..b91696b5b357ea 100644 --- a/homeassistant/components/melcloud/icons.json +++ b/homeassistant/components/melcloud/icons.json @@ -7,7 +7,11 @@ } }, "services": { - "set_vane_horizontal": "mdi:arrow-left-right", - "set_vane_vertical": "mdi:arrow-up-down" + "set_vane_horizontal": { + "service": "mdi:arrow-left-right" + }, + "set_vane_vertical": { + "service": "mdi:arrow-up-down" + } } } diff --git a/homeassistant/components/met/const.py b/homeassistant/components/met/const.py index c513e98504efbf..ccc0662b3c34bf 100644 --- a/homeassistant/components/met/const.py +++ b/homeassistant/components/met/const.py @@ -21,12 +21,14 @@ ATTR_FORECAST_NATIVE_WIND_SPEED, ATTR_FORECAST_PRECIPITATION_PROBABILITY, ATTR_FORECAST_TIME, + ATTR_FORECAST_UV_INDEX, ATTR_FORECAST_WIND_BEARING, ATTR_WEATHER_CLOUD_COVERAGE, ATTR_WEATHER_DEW_POINT, ATTR_WEATHER_HUMIDITY, ATTR_WEATHER_PRESSURE, ATTR_WEATHER_TEMPERATURE, + ATTR_WEATHER_UV_INDEX, ATTR_WEATHER_VISIBILITY, ATTR_WEATHER_WIND_BEARING, ATTR_WEATHER_WIND_GUST_SPEED, @@ -190,6 +192,7 @@ ATTR_FORECAST_NATIVE_WIND_GUST_SPEED: "wind_gust", ATTR_FORECAST_CLOUD_COVERAGE: "cloudiness", ATTR_FORECAST_HUMIDITY: "humidity", + ATTR_FORECAST_UV_INDEX: "uv_index", } ATTR_MAP = { @@ -202,4 +205,5 @@ ATTR_WEATHER_WIND_GUST_SPEED: "wind_gust", ATTR_WEATHER_CLOUD_COVERAGE: "cloudiness", ATTR_WEATHER_DEW_POINT: "dew_point", + ATTR_WEATHER_UV_INDEX: "uv_index", } diff --git a/homeassistant/components/met/manifest.json b/homeassistant/components/met/manifest.json index e900c5a012a65f..1a145589a6837a 100644 --- a/homeassistant/components/met/manifest.json +++ b/homeassistant/components/met/manifest.json @@ -6,5 +6,5 @@ "documentation": "https://www.home-assistant.io/integrations/met", "iot_class": "cloud_polling", "loggers": ["metno"], - "requirements": ["PyMetno==0.12.0"] + "requirements": ["PyMetno==0.13.0"] } diff --git a/homeassistant/components/met/weather.py b/homeassistant/components/met/weather.py index 809bb792b2c5ae..7b95567366b591 100644 --- a/homeassistant/components/met/weather.py +++ b/homeassistant/components/met/weather.py @@ -13,6 +13,7 @@ ATTR_WEATHER_HUMIDITY, ATTR_WEATHER_PRESSURE, ATTR_WEATHER_TEMPERATURE, + ATTR_WEATHER_UV_INDEX, ATTR_WEATHER_WIND_BEARING, ATTR_WEATHER_WIND_GUST_SPEED, ATTR_WEATHER_WIND_SPEED, @@ -208,6 +209,13 @@ def native_dew_point(self) -> float | None: ATTR_MAP[ATTR_WEATHER_DEW_POINT] ) + @property + def uv_index(self) -> float | None: + """Return the uv index.""" + return self.coordinator.data.current_weather_data.get( + ATTR_MAP[ATTR_WEATHER_UV_INDEX] + ) + def _forecast(self, hourly: bool) -> list[Forecast] | None: """Return the forecast array.""" if hourly: diff --git a/homeassistant/components/meteoclimatic/config_flow.py b/homeassistant/components/meteoclimatic/config_flow.py index d772a6c9d62aff..59877941fee3e2 100644 --- a/homeassistant/components/meteoclimatic/config_flow.py +++ b/homeassistant/components/meteoclimatic/config_flow.py @@ -1,12 +1,13 @@ """Config flow to configure the Meteoclimatic integration.""" import logging +from typing import Any from meteoclimatic import MeteoclimaticClient from meteoclimatic.exceptions import MeteoclimaticError, StationNotFound import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from .const import CONF_STATION_CODE, DOMAIN @@ -35,9 +36,11 @@ def _show_setup_form(self, user_input=None, errors=None): errors=errors or {}, ) - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle a flow initiated by the user.""" - errors = {} + errors: dict[str, str] = {} if user_input is None: return self._show_setup_form(user_input, errors) diff --git a/homeassistant/components/microsoft_face/icons.json b/homeassistant/components/microsoft_face/icons.json index 826e390197a3a7..6e61676224d2bc 100644 --- a/homeassistant/components/microsoft_face/icons.json +++ b/homeassistant/components/microsoft_face/icons.json @@ -1,10 +1,22 @@ { "services": { - "create_group": "mdi:account-multiple-plus", - "create_person": "mdi:account-plus", - "delete_group": "mdi:account-multiple-remove", - "delete_person": "mdi:account-remove", - "face_person": "mdi:face-man", - "train_group": "mdi:account-multiple-check" + "create_group": { + "service": "mdi:account-multiple-plus" + }, + "create_person": { + "service": "mdi:account-plus" + }, + "delete_group": { + "service": "mdi:account-multiple-remove" + }, + "delete_person": { + "service": "mdi:account-remove" + }, + "face_person": { + "service": "mdi:face-man" + }, + "train_group": { + "service": "mdi:account-multiple-check" + } } } diff --git a/homeassistant/components/mikrotik/config_flow.py b/homeassistant/components/mikrotik/config_flow.py index fe0d020d373795..6035565acf1d34 100644 --- a/homeassistant/components/mikrotik/config_flow.py +++ b/homeassistant/components/mikrotik/config_flow.py @@ -83,7 +83,9 @@ async def async_step_user( errors=errors, ) - async def async_step_reauth(self, data: Mapping[str, Any]) -> ConfigFlowResult: + async def async_step_reauth( + self, entry_data: Mapping[str, Any] + ) -> ConfigFlowResult: """Perform reauth upon an API authentication error.""" self._reauth_entry = self.hass.config_entries.async_get_entry( self.context["entry_id"] diff --git a/homeassistant/components/mill/config_flow.py b/homeassistant/components/mill/config_flow.py index 58660d6358e619..7b2e5c3c4d5efa 100644 --- a/homeassistant/components/mill/config_flow.py +++ b/homeassistant/components/mill/config_flow.py @@ -1,10 +1,12 @@ """Adds config flow for Mill integration.""" +from typing import Any + from mill import Mill from mill_local import Mill as MillLocal import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_IP_ADDRESS, CONF_PASSWORD, CONF_USERNAME from homeassistant.helpers.aiohttp_client import async_get_clientsession @@ -16,7 +18,9 @@ class MillConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" data_schema = vol.Schema( { @@ -39,7 +43,9 @@ async def async_step_user(self, user_input=None): return await self.async_step_local() return await self.async_step_cloud() - async def async_step_local(self, user_input=None): + async def async_step_local( + self, user_input: dict[str, str] | None = None + ) -> ConfigFlowResult: """Handle the local step.""" data_schema = vol.Schema({vol.Required(CONF_IP_ADDRESS): str}) if user_input is None: @@ -71,7 +77,9 @@ async def async_step_local(self, user_input=None): }, ) - async def async_step_cloud(self, user_input=None): + async def async_step_cloud( + self, user_input: dict[str, str] | None = None + ) -> ConfigFlowResult: """Handle the cloud step.""" data_schema = vol.Schema( {vol.Required(CONF_USERNAME): str, vol.Required(CONF_PASSWORD): str} diff --git a/homeassistant/components/mill/icons.json b/homeassistant/components/mill/icons.json index 13d6bb650c112a..f2595f28057386 100644 --- a/homeassistant/components/mill/icons.json +++ b/homeassistant/components/mill/icons.json @@ -1,5 +1,7 @@ { "services": { - "set_room_temperature": "mdi:thermometer" + "set_room_temperature": { + "service": "mdi:thermometer" + } } } diff --git a/homeassistant/components/min_max/icons.json b/homeassistant/components/min_max/icons.json index a03163179cb5bd..a9829425570a0c 100644 --- a/homeassistant/components/min_max/icons.json +++ b/homeassistant/components/min_max/icons.json @@ -1,5 +1,7 @@ { "services": { - "reload": "mdi:reload" + "reload": { + "service": "mdi:reload" + } } } diff --git a/homeassistant/components/minio/icons.json b/homeassistant/components/minio/icons.json index 16deb1a168d50f..dce148a23de663 100644 --- a/homeassistant/components/minio/icons.json +++ b/homeassistant/components/minio/icons.json @@ -1,7 +1,13 @@ { "services": { - "get": "mdi:cloud-download", - "put": "mdi:cloud-upload", - "remove": "mdi:delete" + "get": { + "service": "mdi:cloud-download" + }, + "put": { + "service": "mdi:cloud-upload" + }, + "remove": { + "service": "mdi:delete" + } } } diff --git a/homeassistant/components/mobile_app/config_flow.py b/homeassistant/components/mobile_app/config_flow.py index 66035733c33ea3..33c0442b529c5c 100644 --- a/homeassistant/components/mobile_app/config_flow.py +++ b/homeassistant/components/mobile_app/config_flow.py @@ -1,9 +1,10 @@ """Config flow for Mobile App.""" +from typing import Any import uuid from homeassistant.components import person -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import ATTR_DEVICE_ID from homeassistant.helpers import entity_registry as er @@ -15,7 +16,9 @@ class MobileAppFlowHandler(ConfigFlow, domain=DOMAIN): VERSION = 1 - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle a flow initialized by the user.""" placeholders = { "apps_url": "https://www.home-assistant.io/integrations/mobile_app/#apps" @@ -25,7 +28,9 @@ async def async_step_user(self, user_input=None): reason="install_app", description_placeholders=placeholders ) - async def async_step_registration(self, user_input=None): + async def async_step_registration( + self, user_input: dict[str, Any] + ) -> ConfigFlowResult: """Handle a flow initialized during registration.""" if ATTR_DEVICE_ID in user_input: # Unique ID is combi of app + device ID. diff --git a/homeassistant/components/modbus/icons.json b/homeassistant/components/modbus/icons.json index eeaeff6403b5dd..05ee76fd44ed96 100644 --- a/homeassistant/components/modbus/icons.json +++ b/homeassistant/components/modbus/icons.json @@ -1,9 +1,19 @@ { "services": { - "reload": "mdi:reload", - "write_coil": "mdi:pencil", - "write_register": "mdi:database-edit", - "stop": "mdi:stop", - "restart": "mdi:restart" + "reload": { + "service": "mdi:reload" + }, + "write_coil": { + "service": "mdi:pencil" + }, + "write_register": { + "service": "mdi:database-edit" + }, + "stop": { + "service": "mdi:stop" + }, + "restart": { + "service": "mdi:restart" + } } } diff --git a/homeassistant/components/modern_forms/diagnostics.py b/homeassistant/components/modern_forms/diagnostics.py new file mode 100644 index 00000000000000..0011a7c3bab00c --- /dev/null +++ b/homeassistant/components/modern_forms/diagnostics.py @@ -0,0 +1,36 @@ +"""Diagnostics support for Modern Forms.""" + +from __future__ import annotations + +from dataclasses import asdict +from typing import TYPE_CHECKING, Any + +from homeassistant.components.diagnostics import async_redact_data +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import CONF_MAC +from homeassistant.core import HomeAssistant + +from .const import DOMAIN +from .coordinator import ModernFormsDataUpdateCoordinator + +REDACT_CONFIG = {CONF_MAC} +REDACT_DEVICE_INFO = {"mac_address", "owner"} + + +async def async_get_config_entry_diagnostics( + hass: HomeAssistant, entry: ConfigEntry +) -> dict[str, Any]: + """Return diagnostics for a config entry.""" + coordinator: ModernFormsDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] + if TYPE_CHECKING: + assert coordinator is not None + + return { + "config_entry": async_redact_data(entry.as_dict(), REDACT_CONFIG), + "device": { + "info": async_redact_data( + asdict(coordinator.modern_forms.info), REDACT_DEVICE_INFO + ), + "status": asdict(coordinator.modern_forms.status), + }, + } diff --git a/homeassistant/components/modern_forms/icons.json b/homeassistant/components/modern_forms/icons.json index e5df55dc15e5fe..544e48e17f1f9f 100644 --- a/homeassistant/components/modern_forms/icons.json +++ b/homeassistant/components/modern_forms/icons.json @@ -26,9 +26,17 @@ } }, "services": { - "set_light_sleep_timer": "mdi:timer", - "clear_light_sleep_timer": "mdi:timer-cancel", - "set_fan_sleep_timer": "mdi:timer", - "clear_fan_sleep_timer": "mdi:timer-cancel" + "set_light_sleep_timer": { + "service": "mdi:timer" + }, + "clear_light_sleep_timer": { + "service": "mdi:timer-cancel" + }, + "set_fan_sleep_timer": { + "service": "mdi:timer" + }, + "clear_fan_sleep_timer": { + "service": "mdi:timer-cancel" + } } } diff --git a/homeassistant/components/monoprice/config_flow.py b/homeassistant/components/monoprice/config_flow.py index 2c7163123b65e7..cac673e38c1914 100644 --- a/homeassistant/components/monoprice/config_flow.py +++ b/homeassistant/components/monoprice/config_flow.py @@ -3,12 +3,18 @@ from __future__ import annotations import logging +from typing import Any from pymonoprice import get_monoprice from serial import SerialException import voluptuous as vol -from homeassistant.config_entries import ConfigEntry, ConfigFlow, OptionsFlow +from homeassistant.config_entries import ( + ConfigEntry, + ConfigFlow, + ConfigFlowResult, + OptionsFlow, +) from homeassistant.const import CONF_PORT from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import HomeAssistantError @@ -76,7 +82,9 @@ class MonoPriceConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" errors = {} if user_input is not None: @@ -131,7 +139,9 @@ def _previous_sources(self): return previous - async def async_step_init(self, user_input=None): + async def async_step_init( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Manage the options.""" if user_input is not None: return self.async_create_entry( diff --git a/homeassistant/components/monoprice/icons.json b/homeassistant/components/monoprice/icons.json index 22610cc2a470f2..d560c7bcfa8265 100644 --- a/homeassistant/components/monoprice/icons.json +++ b/homeassistant/components/monoprice/icons.json @@ -1,6 +1,10 @@ { "services": { - "snapshot": "mdi:content-copy", - "restore": "mdi:content-paste" + "snapshot": { + "service": "mdi:content-copy" + }, + "restore": { + "service": "mdi:content-paste" + } } } diff --git a/homeassistant/components/mopeka/__init__.py b/homeassistant/components/mopeka/__init__.py index 17a87efd6e6d6c..d73ece581d74d5 100644 --- a/homeassistant/components/mopeka/__init__.py +++ b/homeassistant/components/mopeka/__init__.py @@ -14,7 +14,7 @@ from homeassistant.const import Platform from homeassistant.core import HomeAssistant -from .const import CONF_MEDIUM_TYPE +from .const import CONF_MEDIUM_TYPE, DEFAULT_MEDIUM_TYPE PLATFORMS: list[Platform] = [Platform.SENSOR] @@ -29,8 +29,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: MopekaConfigEntry) -> bo address = entry.unique_id assert address is not None - # Default sensors configured prior to the intorudction of MediumType - medium_type_str = entry.data.get(CONF_MEDIUM_TYPE, MediumType.PROPANE.value) + # Default sensors configured prior to the introduction of MediumType + medium_type_str = entry.data.get(CONF_MEDIUM_TYPE, DEFAULT_MEDIUM_TYPE) data = MopekaIOTBluetoothDeviceData(MediumType(medium_type_str)) coordinator = entry.runtime_data = PassiveBluetoothProcessorCoordinator( hass, diff --git a/homeassistant/components/motion_blinds/cover.py b/homeassistant/components/motion_blinds/cover.py index 72b78915badffb..e60e7fa0ae8653 100644 --- a/homeassistant/components/motion_blinds/cover.py +++ b/homeassistant/components/motion_blinds/cover.py @@ -330,23 +330,63 @@ def current_cover_position(self) -> None: """Return current position of cover.""" return None + @property + def current_cover_tilt_position(self) -> int | None: + """Return current angle of cover. + + None is unknown, 0 is closed/minimum tilt, 100 is fully open/maximum tilt. + """ + if self._blind.position is None: + if self._blind.angle is None: + return None + return self._blind.angle * 100 / 180 + + return self._blind.position + @property def is_closed(self) -> bool | None: """Return if the cover is closed or not.""" - if self._blind.angle is None: - return None - return self._blind.angle == 0 + if self._blind.position is None: + if self._blind.angle is None: + return None + return self._blind.angle == 0 + + return self._blind.position == 0 + + async def async_open_cover_tilt(self, **kwargs: Any) -> None: + """Open the cover tilt.""" + async with self._api_lock: + await self.hass.async_add_executor_job(self._blind.Open) + + async def async_close_cover_tilt(self, **kwargs: Any) -> None: + """Close the cover tilt.""" + async with self._api_lock: + await self.hass.async_add_executor_job(self._blind.Close) + + async def async_set_cover_tilt_position(self, **kwargs: Any) -> None: + """Move the cover tilt to a specific position.""" + angle = kwargs[ATTR_TILT_POSITION] + if self._blind.position is None: + angle = angle * 180 / 100 + async with self._api_lock: + await self.hass.async_add_executor_job(self._blind.Set_angle, angle) + else: + async with self._api_lock: + await self.hass.async_add_executor_job(self._blind.Set_position, angle) async def async_set_absolute_position(self, **kwargs): """Move the cover to a specific absolute position (see TDBU).""" angle = kwargs.get(ATTR_TILT_POSITION) - if angle is not None: + if angle is None: + return + + if self._blind.position is None: angle = angle * 180 / 100 async with self._api_lock: - await self.hass.async_add_executor_job( - self._blind.Set_angle, - angle, - ) + await self.hass.async_add_executor_job(self._blind.Set_angle, angle) + else: + async with self._api_lock: + await self.hass.async_add_executor_job(self._blind.Set_position, angle) class MotionTDBUDevice(MotionBaseDevice): diff --git a/homeassistant/components/motion_blinds/icons.json b/homeassistant/components/motion_blinds/icons.json index 9e1cd613e5b53d..e50e50130f7ab6 100644 --- a/homeassistant/components/motion_blinds/icons.json +++ b/homeassistant/components/motion_blinds/icons.json @@ -10,6 +10,8 @@ } }, "services": { - "set_absolute_position": "mdi:set-square" + "set_absolute_position": { + "service": "mdi:set-square" + } } } diff --git a/homeassistant/components/motionblinds_ble/diagnostics.py b/homeassistant/components/motionblinds_ble/diagnostics.py new file mode 100644 index 00000000000000..c76bef7c2f88df --- /dev/null +++ b/homeassistant/components/motionblinds_ble/diagnostics.py @@ -0,0 +1,53 @@ +"""Diagnostics support for Motionblinds Bluetooth.""" + +from __future__ import annotations + +from collections.abc import Iterable +from typing import Any + +from motionblindsble.device import MotionDevice + +from homeassistant.components.diagnostics import async_redact_data +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import CONF_UNIQUE_ID +from homeassistant.core import HomeAssistant + +from .const import DOMAIN + +CONF_TITLE = "title" + +TO_REDACT: Iterable[Any] = { + # Config entry title and unique ID may contain sensitive data: + CONF_TITLE, + CONF_UNIQUE_ID, +} + + +async def async_get_config_entry_diagnostics( + hass: HomeAssistant, entry: ConfigEntry +) -> dict[str, Any]: + """Return diagnostics for a config entry.""" + device: MotionDevice = hass.data[DOMAIN][entry.entry_id] + + return async_redact_data( + { + "entry": entry.as_dict(), + "device": { + "blind_type": device.blind_type.value, + "timezone": device.timezone, + "position": device._position, # noqa: SLF001 + "tilt": device._tilt, # noqa: SLF001 + "calibration_type": device._calibration_type.value # noqa: SLF001 + if device._calibration_type # noqa: SLF001 + else None, + "connection_type": device._connection_type.value, # noqa: SLF001 + "end_position_info": None + if not device._end_position_info # noqa: SLF001 + else { + "end_positions": device._end_position_info.end_positions.value, # noqa: SLF001 + "favorite": device._end_position_info.favorite_position, # noqa: SLF001 + }, + }, + }, + TO_REDACT, + ) diff --git a/homeassistant/components/motionblinds_ble/manifest.json b/homeassistant/components/motionblinds_ble/manifest.json index 454c873dfa2654..d9968cfde4cf3a 100644 --- a/homeassistant/components/motionblinds_ble/manifest.json +++ b/homeassistant/components/motionblinds_ble/manifest.json @@ -14,5 +14,5 @@ "integration_type": "device", "iot_class": "assumed_state", "loggers": ["motionblindsble"], - "requirements": ["motionblindsble==0.1.0"] + "requirements": ["motionblindsble==0.1.1"] } diff --git a/homeassistant/components/motioneye/icons.json b/homeassistant/components/motioneye/icons.json index b0a4ea8dcb149d..7cc93d528e88aa 100644 --- a/homeassistant/components/motioneye/icons.json +++ b/homeassistant/components/motioneye/icons.json @@ -1,7 +1,13 @@ { "services": { - "set_text_overlay": "mdi:text-box-outline", - "action": "mdi:gesture-tap-button", - "snapshot": "mdi:camera" + "set_text_overlay": { + "service": "mdi:text-box-outline" + }, + "action": { + "service": "mdi:gesture-tap-button" + }, + "snapshot": { + "service": "mdi:camera" + } } } diff --git a/homeassistant/components/mpd/config_flow.py b/homeassistant/components/mpd/config_flow.py index f37ebe5e5e809a..36777a205f9c4d 100644 --- a/homeassistant/components/mpd/config_flow.py +++ b/homeassistant/components/mpd/config_flow.py @@ -67,19 +67,17 @@ async def async_step_user( errors=errors, ) - async def async_step_import( - self, import_config: dict[str, Any] - ) -> ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Attempt to import the existing configuration.""" - self._async_abort_entries_match({CONF_HOST: import_config[CONF_HOST]}) + self._async_abort_entries_match({CONF_HOST: import_data[CONF_HOST]}) client = MPDClient() client.timeout = 30 client.idletimeout = 10 try: async with timeout(35): - await client.connect(import_config[CONF_HOST], import_config[CONF_PORT]) - if CONF_PASSWORD in import_config: - await client.password(import_config[CONF_PASSWORD]) + await client.connect(import_data[CONF_HOST], import_data[CONF_PORT]) + if CONF_PASSWORD in import_data: + await client.password(import_data[CONF_PASSWORD]) with suppress(mpd.ConnectionError): client.disconnect() except ( @@ -94,10 +92,10 @@ async def async_step_import( return self.async_abort(reason="unknown") return self.async_create_entry( - title=import_config.get(CONF_NAME, "Music Player Daemon"), + title=import_data.get(CONF_NAME, "Music Player Daemon"), data={ - CONF_HOST: import_config[CONF_HOST], - CONF_PORT: import_config[CONF_PORT], - CONF_PASSWORD: import_config.get(CONF_PASSWORD), + CONF_HOST: import_data[CONF_HOST], + CONF_PORT: import_data[CONF_PORT], + CONF_PASSWORD: import_data.get(CONF_PASSWORD), }, ) diff --git a/homeassistant/components/mqtt/__init__.py b/homeassistant/components/mqtt/__init__.py index b2adb7665fc07d..86eeca2017c0a9 100644 --- a/homeassistant/components/mqtt/__init__.py +++ b/homeassistant/components/mqtt/__init__.py @@ -89,7 +89,6 @@ PayloadSentinel, PublishPayloadType, ReceiveMessage, - ReceivePayloadType, convert_outgoing_mqtt_payload, ) from .subscription import ( # noqa: F401 diff --git a/homeassistant/components/mqtt/abbreviations.py b/homeassistant/components/mqtt/abbreviations.py index f4a32bbdf9d163..3c1d0abdb667b8 100644 --- a/homeassistant/components/mqtt/abbreviations.py +++ b/homeassistant/components/mqtt/abbreviations.py @@ -6,9 +6,6 @@ "act_stat_t": "activity_state_topic", "act_val_tpl": "activity_value_template", "atype": "automation_type", - "aux_cmd_t": "aux_command_topic", - "aux_stat_tpl": "aux_state_template", - "aux_stat_t": "aux_state_topic", "av_tones": "available_tones", "avty": "availability", "avty_mode": "availability_mode", @@ -157,8 +154,6 @@ "pos_open": "position_open", "pow_cmd_t": "power_command_topic", "pow_cmd_tpl": "power_command_template", - "pow_stat_t": "power_state_topic", - "pow_stat_tpl": "power_state_template", "pr_mode_cmd_t": "preset_mode_command_topic", "pr_mode_cmd_tpl": "preset_mode_command_template", "pr_mode_stat_t": "preset_mode_state_topic", diff --git a/homeassistant/components/mqtt/addon.py b/homeassistant/components/mqtt/addon.py new file mode 100644 index 00000000000000..3ac6748033f114 --- /dev/null +++ b/homeassistant/components/mqtt/addon.py @@ -0,0 +1,22 @@ +"""Provide MQTT add-on management. + +Currently only supports the official mosquitto add-on. +""" + +from __future__ import annotations + +from homeassistant.components.hassio import AddonManager +from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers.singleton import singleton + +from .const import DOMAIN, LOGGER + +ADDON_SLUG = "core_mosquitto" +DATA_ADDON_MANAGER = f"{DOMAIN}_addon_manager" + + +@singleton(DATA_ADDON_MANAGER) +@callback +def get_addon_manager(hass: HomeAssistant) -> AddonManager: + """Get the add-on manager.""" + return AddonManager(hass, LOGGER, "Mosquitto Mqtt Broker", ADDON_SLUG) diff --git a/homeassistant/components/mqtt/climate.py b/homeassistant/components/mqtt/climate.py index 7873b056889884..ac276c37d716b4 100644 --- a/homeassistant/components/mqtt/climate.py +++ b/homeassistant/components/mqtt/climate.py @@ -46,6 +46,7 @@ from homeassistant.core import HomeAssistant, callback import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.service_info.mqtt import ReceivePayloadType from homeassistant.helpers.template import Template from homeassistant.helpers.typing import ConfigType, VolSchemaType from homeassistant.util.unit_conversion import TemperatureConverter @@ -84,7 +85,6 @@ MqttValueTemplate, PublishPayloadType, ReceiveMessage, - ReceivePayloadType, ) from .schemas import MQTT_ENTITY_COMMON_SCHEMA from .util import valid_publish_topic, valid_subscribe_topic @@ -93,13 +93,6 @@ DEFAULT_NAME = "MQTT HVAC" -# Options CONF_AUX_COMMAND_TOPIC, CONF_AUX_STATE_TOPIC -# and CONF_AUX_STATE_TEMPLATE were deprecated in HA Core 2023.9 -# Support was removed in HA Core 2024.3 -CONF_AUX_COMMAND_TOPIC = "aux_command_topic" -CONF_AUX_STATE_TEMPLATE = "aux_state_template" -CONF_AUX_STATE_TOPIC = "aux_state_topic" - CONF_FAN_MODE_COMMAND_TEMPLATE = "fan_mode_command_template" CONF_FAN_MODE_COMMAND_TOPIC = "fan_mode_command_topic" CONF_FAN_MODE_LIST = "fan_modes" @@ -113,10 +106,6 @@ CONF_HUMIDITY_MAX = "max_humidity" CONF_HUMIDITY_MIN = "min_humidity" -# Support for CONF_POWER_STATE_TOPIC and CONF_POWER_STATE_TEMPLATE -# was removed in HA Core 2023.8 -CONF_POWER_STATE_TEMPLATE = "power_state_template" -CONF_POWER_STATE_TOPIC = "power_state_topic" CONF_PRESET_MODE_STATE_TOPIC = "preset_mode_state_topic" CONF_PRESET_MODE_COMMAND_TOPIC = "preset_mode_command_topic" CONF_PRESET_MODE_VALUE_TEMPLATE = "preset_mode_value_template" @@ -201,7 +190,6 @@ CONF_MODE_COMMAND_TOPIC, CONF_MODE_STATE_TOPIC, CONF_POWER_COMMAND_TOPIC, - CONF_POWER_STATE_TOPIC, CONF_PRESET_MODE_COMMAND_TOPIC, CONF_PRESET_MODE_STATE_TOPIC, CONF_SWING_MODE_COMMAND_TOPIC, @@ -295,8 +283,6 @@ def valid_humidity_state_configuration(config: ConfigType) -> ConfigType: vol.Optional(CONF_PAYLOAD_OFF, default="OFF"): cv.string, vol.Optional(CONF_POWER_COMMAND_TOPIC): valid_publish_topic, vol.Optional(CONF_POWER_COMMAND_TEMPLATE): cv.template, - vol.Optional(CONF_POWER_STATE_TEMPLATE): cv.template, - vol.Optional(CONF_POWER_STATE_TOPIC): valid_subscribe_topic, vol.Optional(CONF_PRECISION): vol.In( [PRECISION_TENTHS, PRECISION_HALVES, PRECISION_WHOLE] ), @@ -343,16 +329,6 @@ def valid_humidity_state_configuration(config: ConfigType) -> ConfigType: ).extend(MQTT_ENTITY_COMMON_SCHEMA.schema) PLATFORM_SCHEMA_MODERN = vol.All( - # Support for CONF_POWER_STATE_TOPIC and CONF_POWER_STATE_TEMPLATE - # was removed in HA Core 2023.8 - cv.removed(CONF_POWER_STATE_TEMPLATE), - cv.removed(CONF_POWER_STATE_TOPIC), - # Options CONF_AUX_COMMAND_TOPIC, CONF_AUX_STATE_TOPIC - # and CONF_AUX_STATE_TEMPLATE were deprecated in HA Core 2023.9 - # Support was removed in HA Core 2024.3 - cv.removed(CONF_AUX_COMMAND_TOPIC), - cv.removed(CONF_AUX_STATE_TEMPLATE), - cv.removed(CONF_AUX_STATE_TOPIC), _PLATFORM_SCHEMA_BASE, valid_preset_mode_configuration, valid_humidity_range_configuration, @@ -363,10 +339,6 @@ def valid_humidity_state_configuration(config: ConfigType) -> ConfigType: DISCOVERY_SCHEMA = vol.All( _DISCOVERY_SCHEMA_BASE, - # Support for CONF_POWER_STATE_TOPIC and CONF_POWER_STATE_TEMPLATE - # was removed in HA Core 2023.8 - cv.removed(CONF_POWER_STATE_TEMPLATE), - cv.removed(CONF_POWER_STATE_TOPIC), valid_preset_mode_configuration, valid_humidity_range_configuration, valid_humidity_state_configuration, diff --git a/homeassistant/components/mqtt/config_flow.py b/homeassistant/components/mqtt/config_flow.py index 17dfc6512b3076..ca799ff3653c5d 100644 --- a/homeassistant/components/mqtt/config_flow.py +++ b/homeassistant/components/mqtt/config_flow.py @@ -2,8 +2,10 @@ from __future__ import annotations +import asyncio from collections import OrderedDict from collections.abc import Callable, Mapping +import logging import queue from ssl import PROTOCOL_TLS_CLIENT, SSLContext, SSLError from types import MappingProxyType @@ -14,7 +16,12 @@ import voluptuous as vol from homeassistant.components.file_upload import process_uploaded_file -from homeassistant.components.hassio import HassioServiceInfo +from homeassistant.components.hassio import HassioServiceInfo, is_hassio +from homeassistant.components.hassio.addon_manager import ( + AddonError, + AddonManager, + AddonState, +) from homeassistant.config_entries import ( ConfigEntry, ConfigFlow, @@ -32,6 +39,7 @@ CONF_USERNAME, ) from homeassistant.core import callback +from homeassistant.data_entry_flow import AbortFlow from homeassistant.helpers import config_validation as cv from homeassistant.helpers.json import json_dumps from homeassistant.helpers.selector import ( @@ -51,6 +59,7 @@ ) from homeassistant.util.json import JSON_DECODE_EXCEPTIONS, json_loads +from .addon import get_addon_manager from .client import MqttClientSetup from .const import ( ATTR_PAYLOAD, @@ -91,6 +100,11 @@ valid_publish_topic, ) +_LOGGER = logging.getLogger(__name__) + +ADDON_SETUP_TIMEOUT = 5 +ADDON_SETUP_TIMEOUT_ROUNDS = 5 + MQTT_TIMEOUT = 5 ADVANCED_OPTIONS = "advanced_options" @@ -197,6 +211,12 @@ class FlowHandler(ConfigFlow, domain=DOMAIN): entry: ConfigEntry | None _hassio_discovery: dict[str, Any] | None = None + _addon_manager: AddonManager + + def __init__(self) -> None: + """Set up flow instance.""" + self.install_task: asyncio.Task | None = None + self.start_task: asyncio.Task | None = None @staticmethod @callback @@ -206,6 +226,118 @@ def async_get_options_flow( """Get the options flow for this handler.""" return MQTTOptionsFlowHandler(config_entry) + async def _async_install_addon(self) -> None: + """Install the Mosquitto Mqtt broker add-on.""" + addon_manager: AddonManager = get_addon_manager(self.hass) + await addon_manager.async_schedule_install_addon() + + async def async_step_install_failed( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Add-on installation failed.""" + return self.async_abort( + reason="addon_install_failed", + description_placeholders={"addon": self._addon_manager.addon_name}, + ) + + async def async_step_install_addon( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Install Mosquitto Broker add-on.""" + if self.install_task is None: + self.install_task = self.hass.async_create_task(self._async_install_addon()) + + if not self.install_task.done(): + return self.async_show_progress( + step_id="install_addon", + progress_action="install_addon", + progress_task=self.install_task, + ) + + try: + await self.install_task + except AddonError as err: + _LOGGER.error(err) + return self.async_show_progress_done(next_step_id="install_failed") + finally: + self.install_task = None + + return self.async_show_progress_done(next_step_id="start_addon") + + async def async_step_start_failed( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Add-on start failed.""" + return self.async_abort( + reason="addon_start_failed", + description_placeholders={"addon": self._addon_manager.addon_name}, + ) + + async def async_step_start_addon( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Start Mosquitto Broker add-on.""" + if not self.start_task: + self.start_task = self.hass.async_create_task(self._async_start_addon()) + if not self.start_task.done(): + return self.async_show_progress( + step_id="start_addon", + progress_action="start_addon", + progress_task=self.start_task, + ) + try: + await self.start_task + except AddonError as err: + _LOGGER.error(err) + return self.async_show_progress_done(next_step_id="start_failed") + finally: + self.start_task = None + + return self.async_show_progress_done(next_step_id="setup_entry_from_discovery") + + async def _async_get_config_and_try(self) -> dict[str, Any] | None: + """Get the MQTT add-on discovery info and try the connection.""" + if self._hassio_discovery is not None: + return self._hassio_discovery + addon_manager: AddonManager = get_addon_manager(self.hass) + try: + addon_discovery_config = ( + await addon_manager.async_get_addon_discovery_info() + ) + config: dict[str, Any] = { + CONF_BROKER: addon_discovery_config[CONF_HOST], + CONF_PORT: addon_discovery_config[CONF_PORT], + CONF_USERNAME: addon_discovery_config.get(CONF_USERNAME), + CONF_PASSWORD: addon_discovery_config.get(CONF_PASSWORD), + CONF_DISCOVERY: DEFAULT_DISCOVERY, + } + except AddonError: + # We do not have discovery information yet + return None + if await self.hass.async_add_executor_job( + try_connection, + config, + ): + self._hassio_discovery = config + return config + return None + + async def _async_start_addon(self) -> None: + """Start the Mosquitto Broker add-on.""" + addon_manager: AddonManager = get_addon_manager(self.hass) + await addon_manager.async_schedule_start_addon() + + # Sleep some seconds to let the add-on start properly before connecting. + for _ in range(ADDON_SETUP_TIMEOUT_ROUNDS): + await asyncio.sleep(ADDON_SETUP_TIMEOUT) + # Finish setup using discovery info to test the connection + if await self._async_get_config_and_try(): + break + else: + raise AddonError( + f"Failed to correctly start {addon_manager.addon_name} add-on" + ) + async def async_step_user( self, user_input: dict[str, Any] | None = None ) -> ConfigFlowResult: @@ -213,13 +345,92 @@ async def async_step_user( if self._async_current_entries(): return self.async_abort(reason="single_instance_allowed") + if is_hassio(self.hass): + # Offer to set up broker add-on if supervisor is available + self._addon_manager = get_addon_manager(self.hass) + return self.async_show_menu( + step_id="user", + menu_options=["addon", "broker"], + description_placeholders={"addon": self._addon_manager.addon_name}, + ) + + # Start up a flow for manual setup return await self.async_step_broker() + async def async_step_setup_entry_from_discovery( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Set up mqtt entry from discovery info.""" + if (config := await self._async_get_config_and_try()) is not None: + return self.async_create_entry( + title=self._addon_manager.addon_name, + data=config, + ) + + raise AbortFlow( + "addon_connection_failed", + description_placeholders={"addon": self._addon_manager.addon_name}, + ) + + async def async_step_addon( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Install and start MQTT broker add-on.""" + addon_manager = self._addon_manager + + try: + addon_info = await addon_manager.async_get_addon_info() + except AddonError as err: + raise AbortFlow( + "addon_info_failed", + description_placeholders={"addon": self._addon_manager.addon_name}, + ) from err + + if addon_info.state == AddonState.RUNNING: + # Finish setup using discovery info + return await self.async_step_setup_entry_from_discovery() + + if addon_info.state == AddonState.NOT_RUNNING: + return await self.async_step_start_addon() + + # Install the add-on and start it + return await self.async_step_install_addon() + async def async_step_reauth( self, entry_data: Mapping[str, Any] ) -> ConfigFlowResult: """Handle re-authentication with MQTT broker.""" self.entry = self.hass.config_entries.async_get_entry(self.context["entry_id"]) + if is_hassio(self.hass): + # Check if entry setup matches the add-on discovery config + addon_manager = get_addon_manager(self.hass) + try: + addon_discovery_config = ( + await addon_manager.async_get_addon_discovery_info() + ) + except AddonError: + # Follow manual flow if we have an error + pass + else: + # Check if the addon secrets need to be renewed. + # This will repair the config entry, + # in case the official Mosquitto Broker addon was re-installed. + if ( + entry_data[CONF_BROKER] == addon_discovery_config[CONF_HOST] + and entry_data[CONF_PORT] == addon_discovery_config[CONF_PORT] + and entry_data.get(CONF_USERNAME) + == (username := addon_discovery_config.get(CONF_USERNAME)) + and entry_data.get(CONF_PASSWORD) + != (password := addon_discovery_config.get(CONF_PASSWORD)) + ): + _LOGGER.info( + "Executing autorecovery %s add-on secrets", + addon_manager.addon_name, + ) + return await self.async_step_reauth_confirm( + user_input={CONF_USERNAME: username, CONF_PASSWORD: password} + ) + return await self.async_step_reauth_confirm() async def async_step_reauth_confirm( @@ -293,7 +504,7 @@ async def async_step_broker( async def async_step_hassio( self, discovery_info: HassioServiceInfo ) -> ConfigFlowResult: - """Receive a Hass.io discovery.""" + """Receive a Hass.io discovery or process setup after addon install.""" await self._async_handle_discovery_without_unique_id() self._hassio_discovery = discovery_info.config diff --git a/homeassistant/components/mqtt/const.py b/homeassistant/components/mqtt/const.py index 9a8e6ae22df666..1e1011cc38121a 100644 --- a/homeassistant/components/mqtt/const.py +++ b/homeassistant/components/mqtt/const.py @@ -1,5 +1,7 @@ """Constants used by multiple MQTT modules.""" +import logging + import jinja2 from homeassistant.const import CONF_PAYLOAD, Platform @@ -37,6 +39,7 @@ CONF_JSON_ATTRS_TOPIC = "json_attributes_topic" CONF_JSON_ATTRS_TEMPLATE = "json_attributes_template" CONF_KEEPALIVE = "keepalive" +CONF_OPTIONS = "options" CONF_ORIGIN = "origin" CONF_QOS = ATTR_QOS CONF_RETAIN = ATTR_RETAIN @@ -148,6 +151,7 @@ } DOMAIN = "mqtt" +LOGGER = logging.getLogger(__package__) MQTT_CONNECTION_STATE = "mqtt_connection_state" diff --git a/homeassistant/components/mqtt/device_tracker.py b/homeassistant/components/mqtt/device_tracker.py index b2aeb4c0fc1882..57614106d4ec6e 100644 --- a/homeassistant/components/mqtt/device_tracker.py +++ b/homeassistant/components/mqtt/device_tracker.py @@ -27,13 +27,14 @@ from homeassistant.core import HomeAssistant, callback import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.service_info.mqtt import ReceivePayloadType from homeassistant.helpers.typing import ConfigType, VolSchemaType from . import subscription from .config import MQTT_BASE_SCHEMA from .const import CONF_PAYLOAD_RESET, CONF_STATE_TOPIC from .mixins import CONF_JSON_ATTRS_TOPIC, MqttEntity, async_setup_entity_entry_helper -from .models import MqttValueTemplate, ReceiveMessage, ReceivePayloadType +from .models import MqttValueTemplate, ReceiveMessage from .schemas import MQTT_ENTITY_COMMON_SCHEMA from .util import valid_subscribe_topic diff --git a/homeassistant/components/mqtt/event.py b/homeassistant/components/mqtt/event.py index 5e801fda54b017..0dc267f80f964c 100644 --- a/homeassistant/components/mqtt/event.py +++ b/homeassistant/components/mqtt/event.py @@ -19,6 +19,7 @@ from homeassistant.core import HomeAssistant, callback import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.service_info.mqtt import ReceivePayloadType from homeassistant.helpers.typing import ConfigType, VolSchemaType from homeassistant.util.json import JSON_DECODE_EXCEPTIONS, json_loads_object @@ -32,7 +33,6 @@ MqttValueTemplateException, PayloadSentinel, ReceiveMessage, - ReceivePayloadType, ) from .schemas import MQTT_ENTITY_COMMON_SCHEMA diff --git a/homeassistant/components/mqtt/fan.py b/homeassistant/components/mqtt/fan.py index 1838ce20e4d97f..a22dba4ae936cd 100644 --- a/homeassistant/components/mqtt/fan.py +++ b/homeassistant/components/mqtt/fan.py @@ -29,6 +29,7 @@ from homeassistant.core import HomeAssistant, callback import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.service_info.mqtt import ReceivePayloadType from homeassistant.helpers.template import Template from homeassistant.helpers.typing import ConfigType, VolSchemaType from homeassistant.util.percentage import ( @@ -52,7 +53,6 @@ MqttValueTemplate, PublishPayloadType, ReceiveMessage, - ReceivePayloadType, ) from .schemas import MQTT_ENTITY_COMMON_SCHEMA from .util import valid_publish_topic, valid_subscribe_topic diff --git a/homeassistant/components/mqtt/humidifier.py b/homeassistant/components/mqtt/humidifier.py index a4510ee5951f37..d55c1d3cebf4ef 100644 --- a/homeassistant/components/mqtt/humidifier.py +++ b/homeassistant/components/mqtt/humidifier.py @@ -32,6 +32,7 @@ from homeassistant.core import HomeAssistant, callback import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.service_info.mqtt import ReceivePayloadType from homeassistant.helpers.template import Template from homeassistant.helpers.typing import ConfigType, VolSchemaType @@ -54,7 +55,6 @@ MqttValueTemplate, PublishPayloadType, ReceiveMessage, - ReceivePayloadType, ) from .schemas import MQTT_ENTITY_COMMON_SCHEMA from .util import valid_publish_topic, valid_subscribe_topic diff --git a/homeassistant/components/mqtt/icons.json b/homeassistant/components/mqtt/icons.json index 1979359c5a1261..73cbf22b629b62 100644 --- a/homeassistant/components/mqtt/icons.json +++ b/homeassistant/components/mqtt/icons.json @@ -1,7 +1,13 @@ { "services": { - "publish": "mdi:publish", - "dump": "mdi:database-export", - "reload": "mdi:reload" + "publish": { + "service": "mdi:publish" + }, + "dump": { + "service": "mdi:database-export" + }, + "reload": { + "service": "mdi:reload" + } } } diff --git a/homeassistant/components/mqtt/lawn_mower.py b/homeassistant/components/mqtt/lawn_mower.py index a74d278401cce5..f4aa248929ef78 100644 --- a/homeassistant/components/mqtt/lawn_mower.py +++ b/homeassistant/components/mqtt/lawn_mower.py @@ -20,6 +20,7 @@ from homeassistant.helpers import config_validation as cv from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.restore_state import RestoreEntity +from homeassistant.helpers.service_info.mqtt import ReceivePayloadType from homeassistant.helpers.typing import ConfigType, VolSchemaType from . import subscription @@ -31,7 +32,6 @@ MqttValueTemplate, PublishPayloadType, ReceiveMessage, - ReceivePayloadType, ) from .schemas import MQTT_ENTITY_COMMON_SCHEMA from .util import valid_publish_topic, valid_subscribe_topic diff --git a/homeassistant/components/mqtt/light/schema_basic.py b/homeassistant/components/mqtt/light/schema_basic.py index b0ffae4e328f8d..1a64b1eecb48d3 100644 --- a/homeassistant/components/mqtt/light/schema_basic.py +++ b/homeassistant/components/mqtt/light/schema_basic.py @@ -39,6 +39,7 @@ from homeassistant.core import callback import homeassistant.helpers.config_validation as cv from homeassistant.helpers.restore_state import RestoreEntity +from homeassistant.helpers.service_info.mqtt import ReceivePayloadType from homeassistant.helpers.typing import ConfigType, VolSchemaType import homeassistant.util.color as color_util @@ -57,7 +58,6 @@ PayloadSentinel, PublishPayloadType, ReceiveMessage, - ReceivePayloadType, TemplateVarsType, ) from ..schemas import MQTT_ENTITY_COMMON_SCHEMA diff --git a/homeassistant/components/mqtt/light/schema_template.py b/homeassistant/components/mqtt/light/schema_template.py index c35b0e6ced9b8d..a1f4ea2e81a225 100644 --- a/homeassistant/components/mqtt/light/schema_template.py +++ b/homeassistant/components/mqtt/light/schema_template.py @@ -31,6 +31,7 @@ from homeassistant.core import callback import homeassistant.helpers.config_validation as cv from homeassistant.helpers.restore_state import RestoreEntity +from homeassistant.helpers.service_info.mqtt import ReceivePayloadType from homeassistant.helpers.typing import ConfigType, TemplateVarsType, VolSchemaType import homeassistant.util.color as color_util @@ -43,7 +44,6 @@ MqttValueTemplate, PublishPayloadType, ReceiveMessage, - ReceivePayloadType, ) from ..schemas import MQTT_ENTITY_COMMON_SCHEMA from .schema import MQTT_LIGHT_SCHEMA_SCHEMA diff --git a/homeassistant/components/mqtt/lock.py b/homeassistant/components/mqtt/lock.py index 22b0e24b3c6665..c72dcd8dc21bfd 100644 --- a/homeassistant/components/mqtt/lock.py +++ b/homeassistant/components/mqtt/lock.py @@ -21,6 +21,7 @@ from homeassistant.core import HomeAssistant, callback import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.service_info.mqtt import ReceivePayloadType from homeassistant.helpers.typing import ConfigType, TemplateVarsType from . import subscription @@ -39,7 +40,6 @@ MqttValueTemplate, PublishPayloadType, ReceiveMessage, - ReceivePayloadType, ) from .schemas import MQTT_ENTITY_COMMON_SCHEMA diff --git a/homeassistant/components/mqtt/number.py b/homeassistant/components/mqtt/number.py index e8f2cf0cfe4043..ce441a2de6ecfe 100644 --- a/homeassistant/components/mqtt/number.py +++ b/homeassistant/components/mqtt/number.py @@ -28,6 +28,7 @@ from homeassistant.core import HomeAssistant, callback from homeassistant.helpers import config_validation as cv from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.service_info.mqtt import ReceivePayloadType from homeassistant.helpers.typing import ConfigType, VolSchemaType from . import subscription @@ -44,7 +45,6 @@ MqttValueTemplate, PublishPayloadType, ReceiveMessage, - ReceivePayloadType, ) from .schemas import MQTT_ENTITY_COMMON_SCHEMA diff --git a/homeassistant/components/mqtt/select.py b/homeassistant/components/mqtt/select.py index 5cc7a586c712ee..5f9c4a11c23163 100644 --- a/homeassistant/components/mqtt/select.py +++ b/homeassistant/components/mqtt/select.py @@ -15,25 +15,28 @@ from homeassistant.helpers import config_validation as cv from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.restore_state import RestoreEntity +from homeassistant.helpers.service_info.mqtt import ReceivePayloadType from homeassistant.helpers.typing import ConfigType, VolSchemaType from . import subscription from .config import MQTT_RW_SCHEMA -from .const import CONF_COMMAND_TEMPLATE, CONF_COMMAND_TOPIC, CONF_STATE_TOPIC +from .const import ( + CONF_COMMAND_TEMPLATE, + CONF_COMMAND_TOPIC, + CONF_OPTIONS, + CONF_STATE_TOPIC, +) from .mixins import MqttEntity, async_setup_entity_entry_helper from .models import ( MqttCommandTemplate, MqttValueTemplate, PublishPayloadType, ReceiveMessage, - ReceivePayloadType, ) from .schemas import MQTT_ENTITY_COMMON_SCHEMA _LOGGER = logging.getLogger(__name__) -CONF_OPTIONS = "options" - DEFAULT_NAME = "MQTT Select" MQTT_SELECT_ATTRIBUTES_BLOCKED = frozenset( diff --git a/homeassistant/components/mqtt/sensor.py b/homeassistant/components/mqtt/sensor.py index e983f1b66f3910..fc95807b8a5d96 100644 --- a/homeassistant/components/mqtt/sensor.py +++ b/homeassistant/components/mqtt/sensor.py @@ -33,19 +33,15 @@ import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.event import async_call_later +from homeassistant.helpers.service_info.mqtt import ReceivePayloadType from homeassistant.helpers.typing import ConfigType, VolSchemaType from homeassistant.util import dt as dt_util from . import subscription from .config import MQTT_RO_SCHEMA -from .const import CONF_STATE_TOPIC, PAYLOAD_NONE +from .const import CONF_OPTIONS, CONF_STATE_TOPIC, PAYLOAD_NONE from .mixins import MqttAvailabilityMixin, MqttEntity, async_setup_entity_entry_helper -from .models import ( - MqttValueTemplate, - PayloadSentinel, - ReceiveMessage, - ReceivePayloadType, -) +from .models import MqttValueTemplate, PayloadSentinel, ReceiveMessage from .schemas import MQTT_ENTITY_COMMON_SCHEMA from .util import check_state_too_long @@ -72,6 +68,7 @@ vol.Optional(CONF_FORCE_UPDATE, default=DEFAULT_FORCE_UPDATE): cv.boolean, vol.Optional(CONF_LAST_RESET_VALUE_TEMPLATE): cv.template, vol.Optional(CONF_NAME): vol.Any(cv.string, None), + vol.Optional(CONF_OPTIONS): cv.ensure_list, vol.Optional(CONF_SUGGESTED_DISPLAY_PRECISION): cv.positive_int, vol.Optional(CONF_STATE_CLASS): vol.Any(STATE_CLASSES_SCHEMA, None), vol.Optional(CONF_UNIT_OF_MEASUREMENT): vol.Any(cv.string, None), @@ -79,8 +76,8 @@ ).extend(MQTT_ENTITY_COMMON_SCHEMA.schema) -def validate_sensor_state_class_config(config: ConfigType) -> ConfigType: - """Validate the sensor state class config.""" +def validate_sensor_state_and_device_class_config(config: ConfigType) -> ConfigType: + """Validate the sensor options, state and device class config.""" if ( CONF_LAST_RESET_VALUE_TEMPLATE in config and (state_class := config.get(CONF_STATE_CLASS)) != SensorStateClass.TOTAL @@ -90,17 +87,35 @@ def validate_sensor_state_class_config(config: ConfigType) -> ConfigType: f"together with state class `{state_class}`" ) + # Only allow `options` to be set for `enum` sensors + # to limit the possible sensor values + if (options := config.get(CONF_OPTIONS)) is not None: + if not options: + raise vol.Invalid("An empty options list is not allowed") + if config.get(CONF_STATE_CLASS) or config.get(CONF_UNIT_OF_MEASUREMENT): + raise vol.Invalid( + f"Specifying `{CONF_OPTIONS}` is not allowed together with " + f"the `{CONF_STATE_CLASS}` or `{CONF_UNIT_OF_MEASUREMENT}` option" + ) + + if (device_class := config.get(CONF_DEVICE_CLASS)) != SensorDeviceClass.ENUM: + raise vol.Invalid( + f"The option `{CONF_OPTIONS}` can only be used " + f"together with device class `{SensorDeviceClass.ENUM}`, " + f"got `{CONF_DEVICE_CLASS}` '{device_class}'" + ) + return config PLATFORM_SCHEMA_MODERN = vol.All( _PLATFORM_SCHEMA_BASE, - validate_sensor_state_class_config, + validate_sensor_state_and_device_class_config, ) DISCOVERY_SCHEMA = vol.All( _PLATFORM_SCHEMA_BASE.extend({}, extra=vol.REMOVE_EXTRA), - validate_sensor_state_class_config, + validate_sensor_state_and_device_class_config, ) @@ -197,6 +212,7 @@ def _setup_from_config(self, config: ConfigType) -> None: CONF_SUGGESTED_DISPLAY_PRECISION ) self._attr_native_unit_of_measurement = config.get(CONF_UNIT_OF_MEASUREMENT) + self._attr_options = config.get(CONF_OPTIONS) self._attr_state_class = config.get(CONF_STATE_CLASS) self._expire_after = config.get(CONF_EXPIRE_AFTER) @@ -252,6 +268,15 @@ def _update_state(self, msg: ReceiveMessage) -> None: else: self._attr_native_value = payload return + if self.options and payload not in self.options: + _LOGGER.warning( + "Ignoring invalid option received on topic '%s', got '%s', allowed: %s", + msg.topic, + payload, + ", ".join(self.options), + ) + return + if self.device_class in { None, SensorDeviceClass.ENUM, diff --git a/homeassistant/components/mqtt/siren.py b/homeassistant/components/mqtt/siren.py index 9f1466dd95d42a..e7cf9e270bdeda 100644 --- a/homeassistant/components/mqtt/siren.py +++ b/homeassistant/components/mqtt/siren.py @@ -31,6 +31,7 @@ import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.json import json_dumps +from homeassistant.helpers.service_info.mqtt import ReceivePayloadType from homeassistant.helpers.template import Template from homeassistant.helpers.typing import ConfigType, TemplateVarsType, VolSchemaType from homeassistant.util.json import JSON_DECODE_EXCEPTIONS, json_loads_object @@ -51,7 +52,6 @@ MqttValueTemplate, PublishPayloadType, ReceiveMessage, - ReceivePayloadType, ) from .schemas import MQTT_ENTITY_COMMON_SCHEMA diff --git a/homeassistant/components/mqtt/strings.json b/homeassistant/components/mqtt/strings.json index c786d7e08a157c..75855f6d9f36b8 100644 --- a/homeassistant/components/mqtt/strings.json +++ b/homeassistant/components/mqtt/strings.json @@ -23,6 +23,13 @@ }, "config": { "step": { + "user": { + "description": "Please choose how you want to connect to the MQTT broker:", + "menu_options": { + "addon": "Use the official {addon} add-on.", + "broker": "Manually enter the MQTT broker connection details" + } + }, "broker": { "description": "Please enter the connection information of your MQTT broker.", "data": { @@ -63,15 +70,15 @@ "ws_path": "The WebSocket path to be used for the connection to your MQTT broker." } }, + "install_addon": { + "title": "Installing add-on" + }, + "start_addon": { + "title": "Starting add-on" + }, "hassio_confirm": { "title": "MQTT Broker via Home Assistant add-on", - "description": "Do you want to configure Home Assistant to connect to the MQTT broker provided by the add-on {addon}?", - "data": { - "discovery": "Enable discovery" - }, - "data_description": { - "discovery": "Option to enable MQTT automatic discovery." - } + "description": "Do you want to configure Home Assistant to connect to the MQTT broker provided by the add-on {addon}?" }, "reauth_confirm": { "title": "Re-authentication required with the MQTT broker", @@ -87,6 +94,10 @@ } }, "abort": { + "addon_info_failed": "Failed get info for the {addon} add-on.", + "addon_install_failed": "Failed to install the {addon} add-on.", + "addon_start_failed": "Failed to start the {addon} add-on.", + "addon_connection_failed": "Failed to connect to the {addon} add-on. Check the add-on status and try again later.", "already_configured": "[%key:common::config_flow::abort::already_configured_service%]", "reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]", "single_instance_allowed": "[%key:common::config_flow::abort::single_instance_allowed%]" @@ -174,7 +185,7 @@ "title": "MQTT options", "description": "Discovery - If discovery is enabled (recommended), Home Assistant will automatically discover devices and entities which publish their configuration on the MQTT broker. If discovery is disabled, all configuration must be done manually.\nDiscovery prefix - The prefix a configuration topic for automatic discovery must start with.\nBirth message - The birth message will be sent each time Home Assistant (re)connects to the MQTT broker.\nWill message - The will message will be sent each time Home Assistant loses its connection to the broker, both in case of a clean (e.g. Home Assistant shutting down) and in case of an unclean (e.g. Home Assistant crashing or losing its network connection) disconnect.", "data": { - "discovery": "[%key:component::mqtt::config::step::hassio_confirm::data::discovery%]", + "discovery": "Enable discovery", "discovery_prefix": "Discovery prefix", "birth_enable": "Enable birth message", "birth_topic": "Birth message topic", diff --git a/homeassistant/components/mqtt/tag.py b/homeassistant/components/mqtt/tag.py index d5f5371c357a53..031c620af4a232 100644 --- a/homeassistant/components/mqtt/tag.py +++ b/homeassistant/components/mqtt/tag.py @@ -13,6 +13,7 @@ from homeassistant.const import CONF_DEVICE, CONF_VALUE_TEMPLATE from homeassistant.core import HassJobType, HomeAssistant, callback import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.service_info.mqtt import ReceivePayloadType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from . import subscription @@ -31,7 +32,6 @@ MqttValueTemplate, MqttValueTemplateException, ReceiveMessage, - ReceivePayloadType, ) from .schemas import MQTT_ENTITY_DEVICE_INFO_SCHEMA from .subscription import EntitySubscription diff --git a/homeassistant/components/mqtt/text.py b/homeassistant/components/mqtt/text.py index 0b122dec7b5dda..0db711cc4567de 100644 --- a/homeassistant/components/mqtt/text.py +++ b/homeassistant/components/mqtt/text.py @@ -22,6 +22,7 @@ from homeassistant.core import HomeAssistant, callback from homeassistant.helpers import config_validation as cv from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.service_info.mqtt import ReceivePayloadType from homeassistant.helpers.typing import ConfigType, VolSchemaType from . import subscription @@ -33,7 +34,6 @@ MqttValueTemplate, PublishPayloadType, ReceiveMessage, - ReceivePayloadType, ) from .schemas import MQTT_ENTITY_COMMON_SCHEMA from .util import check_state_too_long diff --git a/homeassistant/components/mqtt/trigger.py b/homeassistant/components/mqtt/trigger.py index 3f7f03d7f19881..b901176cf88052 100644 --- a/homeassistant/components/mqtt/trigger.py +++ b/homeassistant/components/mqtt/trigger.py @@ -18,6 +18,7 @@ callback, ) from homeassistant.helpers import config_validation as cv +from homeassistant.helpers.service_info.mqtt import ReceivePayloadType from homeassistant.helpers.template import Template from homeassistant.helpers.trigger import TriggerActionType, TriggerData, TriggerInfo from homeassistant.helpers.typing import ConfigType, TemplateVarsType @@ -31,7 +32,6 @@ PayloadSentinel, PublishPayloadType, ReceiveMessage, - ReceivePayloadType, ) TRIGGER_SCHEMA = cv.TRIGGER_BASE_SCHEMA.extend( diff --git a/homeassistant/components/mqtt/vacuum.py b/homeassistant/components/mqtt/vacuum.py index c9898465184963..87d6c9dd7446e6 100644 --- a/homeassistant/components/mqtt/vacuum.py +++ b/homeassistant/components/mqtt/vacuum.py @@ -1,10 +1,5 @@ """Support for MQTT vacuums.""" -# The legacy schema for MQTT vacuum was deprecated with HA Core 2023.8.0 -# and was removed with HA Core 2024.2.0 -# The use of the schema attribute with MQTT vacuum was deprecated with HA Core 2024.2 -# the attribute will be remove with HA Core 2024.8 - from __future__ import annotations import logging @@ -38,15 +33,12 @@ from . import subscription from .config import MQTT_BASE_SCHEMA -from .const import CONF_COMMAND_TOPIC, CONF_RETAIN, CONF_SCHEMA, CONF_STATE_TOPIC +from .const import CONF_COMMAND_TOPIC, CONF_RETAIN, CONF_STATE_TOPIC from .mixins import MqttEntity, async_setup_entity_entry_helper from .models import ReceiveMessage from .schemas import MQTT_ENTITY_COMMON_SCHEMA from .util import valid_publish_topic -LEGACY = "legacy" -STATE = "state" - BATTERY = "battery_level" FAN_SPEED = "fan_speed" STATE = "state" @@ -149,7 +141,7 @@ def services_to_strings( MQTT_VACUUM_DOCS_URL = "https://www.home-assistant.io/integrations/vacuum.mqtt/" -VACUUM_BASE_SCHEMA = MQTT_BASE_SCHEMA.extend( +PLATFORM_SCHEMA_MODERN = MQTT_BASE_SCHEMA.extend( { vol.Optional(CONF_FAN_SPEED_LIST, default=[]): vol.All( cv.ensure_list, [cv.string] @@ -173,26 +165,10 @@ def services_to_strings( ), vol.Optional(CONF_COMMAND_TOPIC): valid_publish_topic, vol.Optional(CONF_RETAIN, default=DEFAULT_RETAIN): cv.boolean, - vol.Optional(CONF_SCHEMA): vol.All(vol.Lower, vol.Any(LEGACY, STATE)), } ).extend(MQTT_ENTITY_COMMON_SCHEMA.schema) -DISCOVERY_SCHEMA = vol.All( - VACUUM_BASE_SCHEMA.extend({}, extra=vol.ALLOW_EXTRA), - # Do not fail a config is the schema option is still present, - # De option was deprecated with HA Core 2024.2 and removed with HA Core 2024.8. - # As we allow extra options, and we will remove this check silently - # with HA Core 2025.8.0, we will only warn, - # if a adiscovery config still uses this option. - cv.removed(CONF_SCHEMA, raise_if_present=False), -) - -PLATFORM_SCHEMA_MODERN = vol.All( - VACUUM_BASE_SCHEMA, - # The schema options was removed with HA Core 2024.8, - # the cleanup is planned for HA Core 2025.8. - cv.removed(CONF_SCHEMA, raise_if_present=True), -) +DISCOVERY_SCHEMA = PLATFORM_SCHEMA_MODERN.extend({}, extra=vol.ALLOW_EXTRA) async def async_setup_entry( diff --git a/homeassistant/components/mysensors/gateway.py b/homeassistant/components/mysensors/gateway.py index 11f27f8a1083e3..00c8d5eecfb874 100644 --- a/homeassistant/components/mysensors/gateway.py +++ b/homeassistant/components/mysensors/gateway.py @@ -16,7 +16,6 @@ from homeassistant.components.mqtt import ( DOMAIN as MQTT_DOMAIN, ReceiveMessage as MQTTReceiveMessage, - ReceivePayloadType, async_publish, async_subscribe, ) @@ -24,6 +23,7 @@ from homeassistant.const import CONF_DEVICE, EVENT_HOMEASSISTANT_STOP from homeassistant.core import Event, HomeAssistant, callback import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.service_info.mqtt import ReceivePayloadType from homeassistant.setup import SetupPhases, async_pause_setup from homeassistant.util.unit_system import METRIC_SYSTEM diff --git a/homeassistant/components/neato/icons.json b/homeassistant/components/neato/icons.json index ca50d5a9bc7064..eb18a7e3196dc6 100644 --- a/homeassistant/components/neato/icons.json +++ b/homeassistant/components/neato/icons.json @@ -1,5 +1,7 @@ { "services": { - "custom_cleaning": "mdi:broom" + "custom_cleaning": { + "service": "mdi:broom" + } } } diff --git a/homeassistant/components/ness_alarm/icons.json b/homeassistant/components/ness_alarm/icons.json index ea17fd2b299618..29d8ae1c8f5aa0 100644 --- a/homeassistant/components/ness_alarm/icons.json +++ b/homeassistant/components/ness_alarm/icons.json @@ -1,6 +1,10 @@ { "services": { - "aux": "mdi:audio-input-stereo-minijack", - "panic": "mdi:fire" + "aux": { + "service": "mdi:audio-input-stereo-minijack" + }, + "panic": { + "service": "mdi:fire" + } } } diff --git a/homeassistant/components/nest/__init__.py b/homeassistant/components/nest/__init__.py index bdec44a3c8599d..8a1719a9bd575c 100644 --- a/homeassistant/components/nest/__init__.py +++ b/homeassistant/components/nest/__init__.py @@ -20,6 +20,7 @@ DecodeException, SubscriberException, ) +from google_nest_sdm.traits import TraitType import voluptuous as vol from homeassistant.auth.permissions.const import POLICY_READ @@ -65,6 +66,8 @@ ) from .events import EVENT_NAME_MAP, NEST_EVENT from .media_source import ( + EVENT_MEDIA_API_URL_FORMAT, + EVENT_THUMBNAIL_URL_FORMAT, async_get_media_event_store, async_get_media_source_devices, async_get_transcoder, @@ -97,7 +100,7 @@ ) # Platforms for SDM API -PLATFORMS = [Platform.CAMERA, Platform.CLIMATE, Platform.SENSOR] +PLATFORMS = [Platform.CAMERA, Platform.CLIMATE, Platform.EVENT, Platform.SENSOR] # Fetch media events with a disk backed cache, with a limit for each camera # device. The largest media items are mp4 clips at ~120kb each, and we target @@ -136,11 +139,15 @@ class SignalUpdateCallback: """An EventCallback invoked when new events arrive from subscriber.""" def __init__( - self, hass: HomeAssistant, config_reload_cb: Callable[[], Awaitable[None]] + self, + hass: HomeAssistant, + config_reload_cb: Callable[[], Awaitable[None]], + config_entry_id: str, ) -> None: """Initialize EventCallback.""" self._hass = hass self._config_reload_cb = config_reload_cb + self._config_entry_id = config_entry_id async def async_handle_event(self, event_message: EventMessage) -> None: """Process an incoming EventMessage.""" @@ -159,19 +166,44 @@ async def async_handle_event(self, event_message: EventMessage) -> None: ) if not device_entry: return + supported_traits = self._supported_traits(device_id) for api_event_type, image_event in events.items(): if not (event_type := EVENT_NAME_MAP.get(api_event_type)): continue + nest_event_id = image_event.event_token message = { "device_id": device_entry.id, "type": event_type, "timestamp": event_message.timestamp, - "nest_event_id": image_event.event_token, + "nest_event_id": nest_event_id, } + if ( + TraitType.CAMERA_EVENT_IMAGE in supported_traits + or TraitType.CAMERA_CLIP_PREVIEW in supported_traits + ): + attachment = { + "image": EVENT_THUMBNAIL_URL_FORMAT.format( + device_id=device_entry.id, event_token=image_event.event_token + ) + } + if TraitType.CAMERA_CLIP_PREVIEW in supported_traits: + attachment["video"] = EVENT_MEDIA_API_URL_FORMAT.format( + device_id=device_entry.id, event_token=image_event.event_token + ) + message["attachment"] = attachment if image_event.zones: message["zones"] = image_event.zones self._hass.bus.async_fire(NEST_EVENT, message) + def _supported_traits(self, device_id: str) -> list[TraitType]: + if not ( + device_manager := self._hass.data[DOMAIN] + .get(self._config_entry_id, {}) + .get(DATA_DEVICE_MANAGER) + ) or not (device := device_manager.devices.get(device_id)): + return [] + return list(device.traits) + async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Nest from a config entry with dispatch between old/new flows.""" @@ -197,7 +229,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_config_reload() -> None: await hass.config_entries.async_reload(entry.entry_id) - update_callback = SignalUpdateCallback(hass, async_config_reload) + update_callback = SignalUpdateCallback(hass, async_config_reload, entry.entry_id) subscriber.set_update_callback(update_callback.async_handle_event) try: await subscriber.start_async() diff --git a/homeassistant/components/nest/event.py b/homeassistant/components/nest/event.py new file mode 100644 index 00000000000000..a6d70fe86d56c4 --- /dev/null +++ b/homeassistant/components/nest/event.py @@ -0,0 +1,129 @@ +"""Event platform for Google Nest.""" + +from dataclasses import dataclass +import logging + +from google_nest_sdm.device import Device +from google_nest_sdm.device_manager import DeviceManager +from google_nest_sdm.event import EventMessage, EventType +from google_nest_sdm.traits import TraitType + +from homeassistant.components.event import ( + EventDeviceClass, + EventEntity, + EventEntityDescription, +) +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from .const import DATA_DEVICE_MANAGER, DOMAIN +from .device_info import NestDeviceInfo +from .events import ( + EVENT_CAMERA_MOTION, + EVENT_CAMERA_PERSON, + EVENT_CAMERA_SOUND, + EVENT_DOORBELL_CHIME, + EVENT_NAME_MAP, +) + +_LOGGER = logging.getLogger(__name__) + + +@dataclass(kw_only=True, frozen=True) +class NestEventEntityDescription(EventEntityDescription): + """Entity description for nest event entities.""" + + trait_types: list[TraitType] + api_event_types: list[EventType] + event_types: list[str] + + +ENTITY_DESCRIPTIONS = [ + NestEventEntityDescription( + key=EVENT_DOORBELL_CHIME, + translation_key="chime", + device_class=EventDeviceClass.DOORBELL, + event_types=[EVENT_DOORBELL_CHIME], + trait_types=[TraitType.DOORBELL_CHIME], + api_event_types=[EventType.DOORBELL_CHIME], + ), + NestEventEntityDescription( + key=EVENT_CAMERA_MOTION, + translation_key="motion", + device_class=EventDeviceClass.MOTION, + event_types=[EVENT_CAMERA_MOTION, EVENT_CAMERA_PERSON, EVENT_CAMERA_SOUND], + trait_types=[ + TraitType.CAMERA_MOTION, + TraitType.CAMERA_PERSON, + TraitType.CAMERA_SOUND, + ], + api_event_types=[ + EventType.CAMERA_MOTION, + EventType.CAMERA_PERSON, + EventType.CAMERA_SOUND, + ], + ), +] + + +async def async_setup_entry( + hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback +) -> None: + """Set up the sensors.""" + + device_manager: DeviceManager = hass.data[DOMAIN][entry.entry_id][ + DATA_DEVICE_MANAGER + ] + async_add_entities( + NestTraitEventEntity(desc, device) + for device in device_manager.devices.values() + for desc in ENTITY_DESCRIPTIONS + if any(trait in device.traits for trait in desc.trait_types) + ) + + +class NestTraitEventEntity(EventEntity): + """Nest doorbell event entity.""" + + entity_description: NestEventEntityDescription + _attr_has_entity_name = True + + def __init__( + self, entity_description: NestEventEntityDescription, device: Device + ) -> None: + """Initialize the event entity.""" + self.entity_description = entity_description + self._device = device + self._attr_unique_id = f"{device.name}-{entity_description.key}" + self._attr_device_info = NestDeviceInfo(device).device_info + + async def _async_handle_event(self, event_message: EventMessage) -> None: + """Handle a device event.""" + if ( + event_message.relation_update + or not event_message.resource_update_name + or not (events := event_message.resource_update_events) + ): + return + last_nest_event_id = self.state_attributes.get("nest_event_id") + for api_event_type, nest_event in events.items(): + if api_event_type not in self.entity_description.api_event_types: + continue + + event_type = EVENT_NAME_MAP[api_event_type] + nest_event_id = nest_event.event_token + if last_nest_event_id is not None and last_nest_event_id == nest_event_id: + # This event is a duplicate message in the same thread + return + + self._trigger_event( + event_type, + {"nest_event_id": nest_event_id}, + ) + self.async_write_ha_state() + return + + async def async_added_to_hass(self) -> None: + """Run when entity is added to attach an event listener.""" + self.async_on_remove(self._device.add_event_callback(self._async_handle_event)) diff --git a/homeassistant/components/nest/manifest.json b/homeassistant/components/nest/manifest.json index 3472fa64e8fc2f..1b0697f7602cd0 100644 --- a/homeassistant/components/nest/manifest.json +++ b/homeassistant/components/nest/manifest.json @@ -20,5 +20,5 @@ "iot_class": "cloud_push", "loggers": ["google_nest_sdm"], "quality_scale": "platinum", - "requirements": ["google-nest-sdm==4.0.7"] + "requirements": ["google-nest-sdm==5.0.0"] } diff --git a/homeassistant/components/nest/strings.json b/homeassistant/components/nest/strings.json index 35e1cc68165bfd..cd915acfbe5be9 100644 --- a/homeassistant/components/nest/strings.json +++ b/homeassistant/components/nest/strings.json @@ -72,5 +72,31 @@ "title": "Legacy Works With Nest has been removed", "description": "Legacy Works With Nest has been removed from Home Assistant, and the API shuts down as of September 2023.\n\nYou must take action to use the SDM API. Remove all `nest` configuration from `configuration.yaml` and restart Home Assistant, then see the Nest [integration instructions]({documentation_url}) for set up instructions and supported devices." } + }, + "entity": { + "event": { + "chime": { + "name": "Chime", + "state_attributes": { + "event_type": { + "state": { + "doorbell_chime": "[%key:component::nest::entity::event::chime::name%]" + } + } + } + }, + "motion": { + "name": "[%key:component::event::entity_component::motion::name%]", + "state_attributes": { + "event_type": { + "state": { + "camera_motion": "[%key:component::event::entity_component::motion::name%]", + "camera_person": "Person", + "camera_sound": "Sound" + } + } + } + } + } } } diff --git a/homeassistant/components/netatmo/icons.json b/homeassistant/components/netatmo/icons.json index 31b1740ab21fb3..70a51542126252 100644 --- a/homeassistant/components/netatmo/icons.json +++ b/homeassistant/components/netatmo/icons.json @@ -34,15 +34,35 @@ } }, "services": { - "set_camera_light": "mdi:led-on", - "set_schedule": "mdi:calendar-clock", - "set_preset_mode_with_end_datetime": "mdi:calendar-clock", - "set_temperature_with_end_datetime": "mdi:thermometer", - "set_temperature_with_time_period": "mdi:thermometer", - "clear_temperature_setting": "mdi:thermometer", - "set_persons_home": "mdi:home", - "set_person_away": "mdi:walk", - "register_webhook": "mdi:link-variant", - "unregister_webhook": "mdi:link-variant-off" + "set_camera_light": { + "service": "mdi:led-on" + }, + "set_schedule": { + "service": "mdi:calendar-clock" + }, + "set_preset_mode_with_end_datetime": { + "service": "mdi:calendar-clock" + }, + "set_temperature_with_end_datetime": { + "service": "mdi:thermometer" + }, + "set_temperature_with_time_period": { + "service": "mdi:thermometer" + }, + "clear_temperature_setting": { + "service": "mdi:thermometer" + }, + "set_persons_home": { + "service": "mdi:home" + }, + "set_person_away": { + "service": "mdi:walk" + }, + "register_webhook": { + "service": "mdi:link-variant" + }, + "unregister_webhook": { + "service": "mdi:link-variant-off" + } } } diff --git a/homeassistant/components/netatmo/manifest.json b/homeassistant/components/netatmo/manifest.json index 98734bcb74253d..0a32777b527104 100644 --- a/homeassistant/components/netatmo/manifest.json +++ b/homeassistant/components/netatmo/manifest.json @@ -12,5 +12,5 @@ "integration_type": "hub", "iot_class": "cloud_polling", "loggers": ["pyatmo"], - "requirements": ["pyatmo==8.0.3"] + "requirements": ["pyatmo==8.1.0"] } diff --git a/homeassistant/components/netatmo/select.py b/homeassistant/components/netatmo/select.py index 3fe098a75a97a0..92568b73e809c7 100644 --- a/homeassistant/components/netatmo/select.py +++ b/homeassistant/components/netatmo/select.py @@ -72,7 +72,7 @@ def __init__(self, netatmo_home: NetatmoHome) -> None: self._attr_current_option = getattr(self.home.get_selected_schedule(), "name") self._attr_options = [ - schedule.name for schedule in self.home.schedules.values() + schedule.name for schedule in self.home.schedules.values() if schedule.name ] async def async_added_to_hass(self) -> None: @@ -128,5 +128,5 @@ def async_update_callback(self) -> None: self.home.schedules ) self._attr_options = [ - schedule.name for schedule in self.home.schedules.values() + schedule.name for schedule in self.home.schedules.values() if schedule.name ] diff --git a/homeassistant/components/netgear/config_flow.py b/homeassistant/components/netgear/config_flow.py index a872e9fb4ac17f..fba934af38d1fa 100644 --- a/homeassistant/components/netgear/config_flow.py +++ b/homeassistant/components/netgear/config_flow.py @@ -3,7 +3,7 @@ from __future__ import annotations import logging -from typing import cast +from typing import Any, cast from urllib.parse import urlparse from pynetgear import DEFAULT_HOST, DEFAULT_PORT, DEFAULT_USER @@ -67,7 +67,9 @@ def __init__(self, config_entry: ConfigEntry) -> None: """Init object.""" self.config_entry = config_entry - async def async_step_init(self, user_input=None): + async def async_step_init( + self, user_input: dict[str, int] | None = None + ) -> ConfigFlowResult: """Manage the options.""" if user_input is not None: return self.async_create_entry(title="", data=user_input) @@ -109,7 +111,11 @@ def async_get_options_flow( """Get the options flow.""" return OptionsFlowHandler(config_entry) - async def _show_setup_form(self, user_input=None, errors=None): + async def _show_setup_form( + self, + user_input: dict[str, Any] | None = None, + errors: dict[str, str] | None = None, + ) -> ConfigFlowResult: """Show the setup form to the user.""" if not user_input: user_input = {} @@ -175,7 +181,9 @@ async def async_step_ssdp( return await self.async_step_user() - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle a flow initiated by the user.""" errors = {} diff --git a/homeassistant/components/netgear_lte/icons.json b/homeassistant/components/netgear_lte/icons.json index 543d9bf46903a7..703d330512bf93 100644 --- a/homeassistant/components/netgear_lte/icons.json +++ b/homeassistant/components/netgear_lte/icons.json @@ -31,9 +31,17 @@ } }, "services": { - "delete_sms": "mdi:delete", - "set_option": "mdi:cog", - "connect_lte": "mdi:wifi", - "disconnect_lte": "mdi:wifi-off" + "delete_sms": { + "service": "mdi:delete" + }, + "set_option": { + "service": "mdi:cog" + }, + "connect_lte": { + "service": "mdi:wifi" + }, + "disconnect_lte": { + "service": "mdi:wifi-off" + } } } diff --git a/homeassistant/components/nexia/config_flow.py b/homeassistant/components/nexia/config_flow.py index 6d1f4af043bd23..592ebde61c3384 100644 --- a/homeassistant/components/nexia/config_flow.py +++ b/homeassistant/components/nexia/config_flow.py @@ -1,13 +1,14 @@ """Config flow for Nexia integration.""" import logging +from typing import Any import aiohttp from nexia.const import BRAND_ASAIR, BRAND_NEXIA, BRAND_TRANE from nexia.home import NexiaHome import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError @@ -81,7 +82,9 @@ class NexiaConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" errors = {} if user_input is not None: diff --git a/homeassistant/components/nexia/icons.json b/homeassistant/components/nexia/icons.json index 620d1a42c03218..a2157f5c035179 100644 --- a/homeassistant/components/nexia/icons.json +++ b/homeassistant/components/nexia/icons.json @@ -20,8 +20,14 @@ } }, "services": { - "set_aircleaner_mode": "mdi:air-filter", - "set_humidify_setpoint": "mdi:water-percent", - "set_hvac_run_mode": "mdi:hvac" + "set_aircleaner_mode": { + "service": "mdi:air-filter" + }, + "set_humidify_setpoint": { + "service": "mdi:water-percent" + }, + "set_hvac_run_mode": { + "service": "mdi:hvac" + } } } diff --git a/homeassistant/components/nextbus/config_flow.py b/homeassistant/components/nextbus/config_flow.py index 05290733bd9edc..90a6a4fc912f65 100644 --- a/homeassistant/components/nextbus/config_flow.py +++ b/homeassistant/components/nextbus/config_flow.py @@ -79,7 +79,7 @@ class NextBusFlowHandler(ConfigFlow, domain=DOMAIN): _route_tags: dict[str, str] _stop_tags: dict[str, str] - def __init__(self): + def __init__(self) -> None: """Initialize NextBus config flow.""" self.data: dict[str, str] = {} self._client = NextBusClient() diff --git a/homeassistant/components/nextcloud/update.py b/homeassistant/components/nextcloud/update.py index 8c292e1bba288c..5b9de52ad1d51a 100644 --- a/homeassistant/components/nextcloud/update.py +++ b/homeassistant/components/nextcloud/update.py @@ -32,12 +32,12 @@ class NextcloudUpdateSensor(NextcloudEntity, UpdateEntity): """Represents a Nextcloud update entity.""" @property - def installed_version(self) -> str | None: + def installed_version(self) -> str: """Version installed and in use.""" - return self.coordinator.data.get("system_version") + return self.coordinator.data["system_version"] @property - def latest_version(self) -> str | None: + def latest_version(self) -> str: """Latest version available for install.""" return self.coordinator.data.get( "update_available_version", self.installed_version @@ -46,7 +46,5 @@ def latest_version(self) -> str | None: @property def release_url(self) -> str | None: """URL to the full release notes of the latest version available.""" - if self.latest_version: - ver = "-".join(self.latest_version.split(".")[:3]) - return f"https://nextcloud.com/changelog/#{ver}" - return None + ver = "-".join(self.latest_version.split(".")[:3]) + return f"https://nextcloud.com/changelog/#{ver}" diff --git a/homeassistant/components/nextdns/__init__.py b/homeassistant/components/nextdns/__init__.py index 4256126b3c71ae..7f0729bca1e134 100644 --- a/homeassistant/components/nextdns/__init__.py +++ b/homeassistant/components/nextdns/__init__.py @@ -15,6 +15,7 @@ AnalyticsStatus, ApiError, ConnectionStatus, + InvalidApiKeyError, NextDns, Settings, ) @@ -23,7 +24,7 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_API_KEY, Platform from homeassistant.core import HomeAssistant -from homeassistant.exceptions import ConfigEntryNotReady +from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady from homeassistant.helpers.aiohttp_client import async_get_clientsession from .const import ( @@ -88,6 +89,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: NextDnsConfigEntry) -> b nextdns = await NextDns.create(websession, api_key) except (ApiError, ClientConnectorError, RetryError, TimeoutError) as err: raise ConfigEntryNotReady from err + except InvalidApiKeyError as err: + raise ConfigEntryAuthFailed from err tasks = [] coordinators = {} diff --git a/homeassistant/components/nextdns/config_flow.py b/homeassistant/components/nextdns/config_flow.py index bd79112b1f9db6..80caba6ec7ed5b 100644 --- a/homeassistant/components/nextdns/config_flow.py +++ b/homeassistant/components/nextdns/config_flow.py @@ -2,19 +2,30 @@ from __future__ import annotations -from typing import Any +from collections.abc import Mapping +from typing import TYPE_CHECKING, Any from aiohttp.client_exceptions import ClientConnectorError from nextdns import ApiError, InvalidApiKeyError, NextDns from tenacity import RetryError import voluptuous as vol -from homeassistant.config_entries import ConfigFlow, ConfigFlowResult +from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_API_KEY, CONF_PROFILE_NAME +from homeassistant.core import HomeAssistant from homeassistant.helpers.aiohttp_client import async_get_clientsession from .const import CONF_PROFILE_ID, DOMAIN +AUTH_SCHEMA = vol.Schema({vol.Required(CONF_API_KEY): str}) + + +async def async_init_nextdns(hass: HomeAssistant, api_key: str) -> NextDns: + """Check if credentials are valid.""" + websession = async_get_clientsession(hass) + + return await NextDns.create(websession, api_key) + class NextDnsFlowHandler(ConfigFlow, domain=DOMAIN): """Config flow for NextDNS.""" @@ -23,8 +34,9 @@ class NextDnsFlowHandler(ConfigFlow, domain=DOMAIN): def __init__(self) -> None: """Initialize the config flow.""" - self.nextdns: NextDns | None = None - self.api_key: str | None = None + self.nextdns: NextDns + self.api_key: str + self.entry: ConfigEntry | None async def async_step_user( self, user_input: dict[str, Any] | None = None @@ -32,14 +44,10 @@ async def async_step_user( """Handle a flow initialized by the user.""" errors: dict[str, str] = {} - websession = async_get_clientsession(self.hass) - if user_input is not None: self.api_key = user_input[CONF_API_KEY] try: - self.nextdns = await NextDns.create( - websession, user_input[CONF_API_KEY] - ) + self.nextdns = await async_init_nextdns(self.hass, self.api_key) except InvalidApiKeyError: errors["base"] = "invalid_api_key" except (ApiError, ClientConnectorError, RetryError, TimeoutError): @@ -51,7 +59,7 @@ async def async_step_user( return self.async_show_form( step_id="user", - data_schema=vol.Schema({vol.Required(CONF_API_KEY): str}), + data_schema=AUTH_SCHEMA, errors=errors, ) @@ -61,8 +69,6 @@ async def async_step_profiles( """Handle the profiles step.""" errors: dict[str, str] = {} - assert self.nextdns is not None - if user_input is not None: profile_name = user_input[CONF_PROFILE_NAME] profile_id = self.nextdns.get_profile_id(profile_name) @@ -86,3 +92,39 @@ async def async_step_profiles( ), errors=errors, ) + + async def async_step_reauth( + self, entry_data: Mapping[str, Any] + ) -> ConfigFlowResult: + """Handle configuration by re-auth.""" + self.entry = self.hass.config_entries.async_get_entry(self.context["entry_id"]) + return await self.async_step_reauth_confirm() + + async def async_step_reauth_confirm( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Dialog that informs the user that reauth is required.""" + errors: dict[str, str] = {} + + if user_input is not None: + try: + await async_init_nextdns(self.hass, user_input[CONF_API_KEY]) + except InvalidApiKeyError: + errors["base"] = "invalid_api_key" + except (ApiError, ClientConnectorError, RetryError, TimeoutError): + errors["base"] = "cannot_connect" + except Exception: # noqa: BLE001 + errors["base"] = "unknown" + else: + if TYPE_CHECKING: + assert self.entry is not None + + return self.async_update_reload_and_abort( + self.entry, data={**self.entry.data, **user_input} + ) + + return self.async_show_form( + step_id="reauth_confirm", + data_schema=AUTH_SCHEMA, + errors=errors, + ) diff --git a/homeassistant/components/nextdns/coordinator.py b/homeassistant/components/nextdns/coordinator.py index 5210807bd3cfc9..6b35e35a027cfb 100644 --- a/homeassistant/components/nextdns/coordinator.py +++ b/homeassistant/components/nextdns/coordinator.py @@ -21,6 +21,7 @@ from tenacity import RetryError from homeassistant.core import HomeAssistant +from homeassistant.exceptions import ConfigEntryAuthFailed from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed @@ -62,10 +63,11 @@ async def _async_update_data(self) -> CoordinatorDataT: except ( ApiError, ClientConnectorError, - InvalidApiKeyError, RetryError, ) as err: raise UpdateFailed(err) from err + except InvalidApiKeyError as err: + raise ConfigEntryAuthFailed from err async def _async_update_data_internal(self) -> CoordinatorDataT: """Update data via library.""" diff --git a/homeassistant/components/nextdns/manifest.json b/homeassistant/components/nextdns/manifest.json index b65706ef1ceac2..be9eee5049ce47 100644 --- a/homeassistant/components/nextdns/manifest.json +++ b/homeassistant/components/nextdns/manifest.json @@ -8,5 +8,5 @@ "iot_class": "cloud_polling", "loggers": ["nextdns"], "quality_scale": "platinum", - "requirements": ["nextdns==3.1.0"] + "requirements": ["nextdns==3.2.0"] } diff --git a/homeassistant/components/nextdns/strings.json b/homeassistant/components/nextdns/strings.json index e0a37aad03b0bc..9dbc80618497d1 100644 --- a/homeassistant/components/nextdns/strings.json +++ b/homeassistant/components/nextdns/strings.json @@ -10,6 +10,11 @@ "data": { "profile": "Profile" } + }, + "reauth_confirm": { + "data": { + "api_key": "[%key:common::config_flow::data::api_key%]" + } } }, "error": { @@ -18,7 +23,8 @@ "unknown": "[%key:common::config_flow::error::unknown%]" }, "abort": { - "already_configured": "This NextDNS profile is already configured." + "already_configured": "This NextDNS profile is already configured.", + "reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]" } }, "system_health": { diff --git a/homeassistant/components/nice_go/cover.py b/homeassistant/components/nice_go/cover.py index 70bd4b136a5c2c..4098d9ef426f00 100644 --- a/homeassistant/components/nice_go/cover.py +++ b/homeassistant/components/nice_go/cover.py @@ -25,7 +25,7 @@ async def async_setup_entry( coordinator = config_entry.runtime_data async_add_entities( - NiceGOCoverEntity(coordinator, device_id, device_data.name, "cover") + NiceGOCoverEntity(coordinator, device_id, device_data.name) for device_id, device_data in coordinator.data.items() ) diff --git a/homeassistant/components/nice_go/entity.py b/homeassistant/components/nice_go/entity.py index 5af4b9c8731ecb..266ad72add3cd4 100644 --- a/homeassistant/components/nice_go/entity.py +++ b/homeassistant/components/nice_go/entity.py @@ -17,12 +17,11 @@ def __init__( coordinator: NiceGOUpdateCoordinator, device_id: str, device_name: str, - sub_device_id: str, ) -> None: """Initialize the entity.""" super().__init__(coordinator) - self._attr_unique_id = f"{device_id}-{sub_device_id}" + self._attr_unique_id = device_id self._device_id = device_id self._attr_device_info = DeviceInfo( identifiers={(DOMAIN, device_id)}, diff --git a/homeassistant/components/nice_go/event.py b/homeassistant/components/nice_go/event.py index caf984f824f89b..a19511b0b115c5 100644 --- a/homeassistant/components/nice_go/event.py +++ b/homeassistant/components/nice_go/event.py @@ -23,7 +23,7 @@ async def async_setup_entry( coordinator = config_entry.runtime_data async_add_entities( - NiceGOEventEntity(coordinator, device_id, device_data.name, "event") + NiceGOEventEntity(coordinator, device_id, device_data.name) for device_id, device_data in coordinator.data.items() ) @@ -48,4 +48,4 @@ async def on_barrier_obstructed(self, data: dict[str, Any]) -> None: if data["deviceId"] == self.data.id: _LOGGER.debug("Barrier obstructed event for %s, triggering", self.data.name) self._trigger_event(EVENT_BARRIER_OBSTRUCTED) - self.schedule_update_ha_state() + self.async_write_ha_state() diff --git a/homeassistant/components/nice_go/light.py b/homeassistant/components/nice_go/light.py index 76730ea822f94b..4a08364688e5ed 100644 --- a/homeassistant/components/nice_go/light.py +++ b/homeassistant/components/nice_go/light.py @@ -20,7 +20,7 @@ async def async_setup_entry( coordinator = config_entry.runtime_data async_add_entities( - NiceGOLightEntity(coordinator, device_id, device_data.name, "light") + NiceGOLightEntity(coordinator, device_id, device_data.name) for device_id, device_data in coordinator.data.items() ) diff --git a/homeassistant/components/nice_go/manifest.json b/homeassistant/components/nice_go/manifest.json index c2ff8370e2a1b7..884f2eb7b18ee4 100644 --- a/homeassistant/components/nice_go/manifest.json +++ b/homeassistant/components/nice_go/manifest.json @@ -6,5 +6,5 @@ "documentation": "https://www.home-assistant.io/integrations/nice_go", "iot_class": "cloud_push", "loggers": ["nice-go"], - "requirements": ["nice-go==0.3.0"] + "requirements": ["nice-go==0.3.8"] } diff --git a/homeassistant/components/nice_go/switch.py b/homeassistant/components/nice_go/switch.py index e7290aabdd6a3e..26d42dab124091 100644 --- a/homeassistant/components/nice_go/switch.py +++ b/homeassistant/components/nice_go/switch.py @@ -24,7 +24,7 @@ async def async_setup_entry( coordinator = config_entry.runtime_data async_add_entities( - NiceGOSwitchEntity(coordinator, device_id, device_data.name, "switch") + NiceGOSwitchEntity(coordinator, device_id, device_data.name) for device_id, device_data in coordinator.data.items() ) diff --git a/homeassistant/components/nina/config_flow.py b/homeassistant/components/nina/config_flow.py index 1fee6430ffc4ed..e048ce81be3f2f 100644 --- a/homeassistant/components/nina/config_flow.py +++ b/homeassistant/components/nina/config_flow.py @@ -182,9 +182,11 @@ def __init__(self, config_entry: ConfigEntry) -> None: if name not in self.data: self.data[name] = [] - async def async_step_init(self, user_input=None): + async def async_step_init( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle options flow.""" - errors: dict[str, Any] = {} + errors: dict[str, str] = {} if not self._all_region_codes_sorted: nina: Nina = Nina(async_get_clientsession(self.hass)) @@ -244,33 +246,33 @@ async def async_step_init(self, user_input=None): self.config_entry, data=user_input ) - return self.async_create_entry(title="", data=None) + return self.async_create_entry(title="", data={}) errors["base"] = "no_selection" + schema: VolDictType = { + **{ + vol.Optional(region, default=self.data[region]): cv.multi_select( + self.regions[region] + ) + for region in CONST_REGIONS + }, + vol.Required( + CONF_MESSAGE_SLOTS, + default=self.data[CONF_MESSAGE_SLOTS], + ): vol.All(int, vol.Range(min=1, max=20)), + vol.Optional( + CONF_HEADLINE_FILTER, + default=self.data[CONF_HEADLINE_FILTER], + ): cv.string, + vol.Optional( + CONF_AREA_FILTER, + default=self.data[CONF_AREA_FILTER], + ): cv.string, + } + return self.async_show_form( step_id="init", - data_schema=vol.Schema( - { - **{ - vol.Optional( - region, default=self.data[region] - ): cv.multi_select(self.regions[region]) - for region in CONST_REGIONS - }, - vol.Required( - CONF_MESSAGE_SLOTS, - default=self.data[CONF_MESSAGE_SLOTS], - ): vol.All(int, vol.Range(min=1, max=20)), - vol.Optional( - CONF_HEADLINE_FILTER, - default=self.data[CONF_HEADLINE_FILTER], - ): cv.string, - vol.Optional( - CONF_AREA_FILTER, - default=self.data[CONF_AREA_FILTER], - ): cv.string, - } - ), + data_schema=vol.Schema(schema), errors=errors, ) diff --git a/homeassistant/components/nissan_leaf/icons.json b/homeassistant/components/nissan_leaf/icons.json index 5da03ed5f1a4ec..832fce90c086fb 100644 --- a/homeassistant/components/nissan_leaf/icons.json +++ b/homeassistant/components/nissan_leaf/icons.json @@ -1,6 +1,10 @@ { "services": { - "start_charge": "mdi:flash", - "update": "mdi:update" + "start_charge": { + "service": "mdi:flash" + }, + "update": { + "service": "mdi:update" + } } } diff --git a/homeassistant/components/nobo_hub/config_flow.py b/homeassistant/components/nobo_hub/config_flow.py index 6fc5bba2c1be49..8aed520f21e7a7 100644 --- a/homeassistant/components/nobo_hub/config_flow.py +++ b/homeassistant/components/nobo_hub/config_flow.py @@ -3,7 +3,7 @@ from __future__ import annotations import socket -from typing import Any +from typing import TYPE_CHECKING, Any from pynobo import nobo import voluptuous as vol @@ -36,10 +36,10 @@ class NoboHubConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - def __init__(self): + def __init__(self) -> None: """Initialize the config flow.""" - self._discovered_hubs = None - self._hub = None + self._discovered_hubs: dict[str, Any] | None = None + self._hub: str | None = None async def async_step_user( self, user_input: dict[str, Any] | None = None @@ -75,6 +75,9 @@ async def async_step_selected( ) -> ConfigFlowResult: """Handle configuration of a selected discovered device.""" errors = {} + if TYPE_CHECKING: + assert self._discovered_hubs + assert self._hub if user_input is not None: serial_prefix = self._discovered_hubs[self._hub] serial_suffix = user_input["serial_suffix"] diff --git a/homeassistant/components/norway_air/manifest.json b/homeassistant/components/norway_air/manifest.json index f787f647db852b..0c8f15b9b789a4 100644 --- a/homeassistant/components/norway_air/manifest.json +++ b/homeassistant/components/norway_air/manifest.json @@ -5,5 +5,5 @@ "documentation": "https://www.home-assistant.io/integrations/norway_air", "iot_class": "cloud_polling", "loggers": ["metno"], - "requirements": ["PyMetno==0.12.0"] + "requirements": ["PyMetno==0.13.0"] } diff --git a/homeassistant/components/notify/icons.json b/homeassistant/components/notify/icons.json index ace8ee0c96b47c..e5ab34031f752a 100644 --- a/homeassistant/components/notify/icons.json +++ b/homeassistant/components/notify/icons.json @@ -5,8 +5,14 @@ } }, "services": { - "notify": "mdi:bell-ring", - "persistent_notification": "mdi:bell-badge", - "send_message": "mdi:message-arrow-right" + "notify": { + "service": "mdi:bell-ring" + }, + "persistent_notification": { + "service": "mdi:bell-badge" + }, + "send_message": { + "service": "mdi:message-arrow-right" + } } } diff --git a/homeassistant/components/nuheat/config_flow.py b/homeassistant/components/nuheat/config_flow.py index a5d34f7ae6c9a5..0e090eeab3ed15 100644 --- a/homeassistant/components/nuheat/config_flow.py +++ b/homeassistant/components/nuheat/config_flow.py @@ -2,12 +2,13 @@ from http import HTTPStatus import logging +from typing import Any import nuheat import requests.exceptions import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError @@ -64,7 +65,9 @@ class NuHeatConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" errors = {} if user_input is not None: diff --git a/homeassistant/components/nuki/config_flow.py b/homeassistant/components/nuki/config_flow.py index 286395e1ff3622..4a9789c7e5134e 100644 --- a/homeassistant/components/nuki/config_flow.py +++ b/homeassistant/components/nuki/config_flow.py @@ -12,6 +12,7 @@ from homeassistant.components import dhcp from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_HOST, CONF_PORT, CONF_TOKEN +from homeassistant.core import HomeAssistant from .const import CONF_ENCRYPT_TOKEN, DEFAULT_PORT, DEFAULT_TIMEOUT, DOMAIN from .helpers import CannotConnect, InvalidAuth, parse_id @@ -34,7 +35,7 @@ ) -async def validate_input(hass, data): +async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str, Any]: """Validate the user input allows us to connect. Data has the keys from USER_SCHEMA with values provided by the user. @@ -62,12 +63,14 @@ async def validate_input(hass, data): class NukiConfigFlow(ConfigFlow, domain=DOMAIN): """Nuki config flow.""" - def __init__(self): + def __init__(self) -> None: """Initialize the Nuki config flow.""" - self.discovery_schema = {} - self._data = {} + self.discovery_schema: vol.Schema | None = None + self._data: Mapping[str, Any] = {} - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle a flow initiated by the user.""" return await self.async_step_validate(user_input) @@ -97,7 +100,9 @@ async def async_step_reauth( return await self.async_step_reauth_confirm() - async def async_step_reauth_confirm(self, user_input=None): + async def async_step_reauth_confirm( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Dialog that inform the user that reauth is required.""" errors = {} if user_input is None: @@ -138,7 +143,9 @@ async def async_step_reauth_confirm(self, user_input=None): step_id="reauth_confirm", data_schema=REAUTH_SCHEMA, errors=errors ) - async def async_step_validate(self, user_input=None): + async def async_step_validate( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle init step of a flow.""" data_schema = self.discovery_schema or USER_SCHEMA diff --git a/homeassistant/components/nuki/icons.json b/homeassistant/components/nuki/icons.json index f74603cb9dc14a..ea1ff9c4fedee4 100644 --- a/homeassistant/components/nuki/icons.json +++ b/homeassistant/components/nuki/icons.json @@ -7,7 +7,11 @@ } }, "services": { - "lock_n_go": "mdi:lock-clock", - "set_continuous_mode": "mdi:bell-cog" + "lock_n_go": { + "service": "mdi:lock-clock" + }, + "set_continuous_mode": { + "service": "mdi:bell-cog" + } } } diff --git a/homeassistant/components/number/icons.json b/homeassistant/components/number/icons.json index 2ce22fcaa4aadf..a122aaecb09465 100644 --- a/homeassistant/components/number/icons.json +++ b/homeassistant/components/number/icons.json @@ -21,6 +21,9 @@ "carbon_monoxide": { "default": "mdi:molecule-co" }, + "conductivity": { + "default": "mdi:sprout-outline" + }, "current": { "default": "mdi:current-ac" }, @@ -146,6 +149,8 @@ } }, "services": { - "set_value": "mdi:numeric" + "set_value": { + "service": "mdi:numeric" + } } } diff --git a/homeassistant/components/number/strings.json b/homeassistant/components/number/strings.json index d6932286469e82..580385172e38a3 100644 --- a/homeassistant/components/number/strings.json +++ b/homeassistant/components/number/strings.json @@ -49,6 +49,9 @@ "carbon_monoxide": { "name": "[%key:component::sensor::entity_component::carbon_monoxide::name%]" }, + "conductivity": { + "name": "[%key:component::sensor::entity_component::conductivity::name%]" + }, "current": { "name": "[%key:component::sensor::entity_component::current::name%]" }, diff --git a/homeassistant/components/nut/icons.json b/homeassistant/components/nut/icons.json index a4125d8633f009..e0f78d6400b55b 100644 --- a/homeassistant/components/nut/icons.json +++ b/homeassistant/components/nut/icons.json @@ -1,118 +1,118 @@ { "entity": { "sensor": { - "ups_status_display": { - "default": "mdi:information-outline" - }, - "ups_status": { + "battery_alarm_threshold": { "default": "mdi:information-outline" }, - "ups_alarm": { - "default": "mdi:alarm" + "battery_capacity": { + "default": "mdi:flash" }, - "ups_load": { + "battery_charge_low": { "default": "mdi:gauge" }, - "ups_load_high": { + "battery_charge_restart": { "default": "mdi:gauge" }, - "ups_id": { - "default": "mdi:information-outline" + "battery_charge_warning": { + "default": "mdi:gauge" }, - "ups_test_result": { + "battery_charger_status": { "default": "mdi:information-outline" }, - "ups_test_date": { + "battery_date": { "default": "mdi:calendar" }, - "ups_display_language": { - "default": "mdi:information-outline" + "battery_mfr_date": { + "default": "mdi:calendar" }, - "ups_contacts": { + "battery_packs": { "default": "mdi:information-outline" }, - "ups_efficiency": { - "default": "mdi:gauge" - }, - "ups_beeper_status": { + "battery_packs_bad": { "default": "mdi:information-outline" }, - "ups_type": { + "battery_type": { "default": "mdi:information-outline" }, - "ups_watchdog_status": { + "input_bypass_phases": { "default": "mdi:information-outline" }, - "ups_start_auto": { + "input_frequency_status": { "default": "mdi:information-outline" }, - "ups_start_battery": { + "input_phases": { "default": "mdi:information-outline" }, - "ups_start_reboot": { + "input_sensitivity": { "default": "mdi:information-outline" }, - "ups_shutdown": { + "input_transfer_reason": { "default": "mdi:information-outline" }, - "battery_charge_low": { + "output_l1_power_percent": { "default": "mdi:gauge" }, - "battery_charge_restart": { + "output_l2_power_percent": { "default": "mdi:gauge" }, - "battery_charge_warning": { + "output_l3_power_percent": { "default": "mdi:gauge" }, - "battery_charger_status": { + "output_phases": { "default": "mdi:information-outline" }, - "battery_capacity": { - "default": "mdi:flash" + "ups_alarm": { + "default": "mdi:alarm" }, - "battery_alarm_threshold": { + "ups_beeper_status": { "default": "mdi:information-outline" }, - "battery_date": { - "default": "mdi:calendar" - }, - "battery_mfr_date": { - "default": "mdi:calendar" + "ups_contacts": { + "default": "mdi:information-outline" }, - "battery_packs": { + "ups_display_language": { "default": "mdi:information-outline" }, - "battery_packs_bad": { + "ups_efficiency": { + "default": "mdi:gauge" + }, + "ups_id": { "default": "mdi:information-outline" }, - "battery_type": { + "ups_load": { + "default": "mdi:gauge" + }, + "ups_load_high": { + "default": "mdi:gauge" + }, + "ups_shutdown": { "default": "mdi:information-outline" }, - "input_sensitivity": { + "ups_start_auto": { "default": "mdi:information-outline" }, - "input_transfer_reason": { + "ups_start_battery": { "default": "mdi:information-outline" }, - "input_frequency_status": { + "ups_start_reboot": { "default": "mdi:information-outline" }, - "input_bypass_phases": { + "ups_status": { "default": "mdi:information-outline" }, - "input_phases": { + "ups_status_display": { "default": "mdi:information-outline" }, - "output_l1_power_percent": { - "default": "mdi:gauge" + "ups_test_date": { + "default": "mdi:calendar" }, - "output_l2_power_percent": { - "default": "mdi:gauge" + "ups_test_result": { + "default": "mdi:information-outline" }, - "output_l3_power_percent": { - "default": "mdi:gauge" + "ups_type": { + "default": "mdi:information-outline" }, - "output_phases": { + "ups_watchdog_status": { "default": "mdi:information-outline" } } diff --git a/homeassistant/components/nws/icons.json b/homeassistant/components/nws/icons.json index 8f91388a3ef611..2aef3a2e614047 100644 --- a/homeassistant/components/nws/icons.json +++ b/homeassistant/components/nws/icons.json @@ -1,5 +1,7 @@ { "services": { - "get_forecasts_extra": "mdi:weather-cloudy-clock" + "get_forecasts_extra": { + "service": "mdi:weather-cloudy-clock" + } } } diff --git a/homeassistant/components/nx584/icons.json b/homeassistant/components/nx584/icons.json index 76e5ae82e0984a..3bd8e485bfd127 100644 --- a/homeassistant/components/nx584/icons.json +++ b/homeassistant/components/nx584/icons.json @@ -1,6 +1,10 @@ { "services": { - "bypass_zone": "mdi:wrench", - "unbypass_zone": "mdi:wrench" + "bypass_zone": { + "service": "mdi:wrench" + }, + "unbypass_zone": { + "service": "mdi:wrench" + } } } diff --git a/homeassistant/components/nzbget/icons.json b/homeassistant/components/nzbget/icons.json index a693e9fec86d89..ca4f4d584ae1fb 100644 --- a/homeassistant/components/nzbget/icons.json +++ b/homeassistant/components/nzbget/icons.json @@ -1,7 +1,13 @@ { "services": { - "pause": "mdi:pause", - "resume": "mdi:play", - "set_speed": "mdi:speedometer" + "pause": { + "service": "mdi:pause" + }, + "resume": { + "service": "mdi:play" + }, + "set_speed": { + "service": "mdi:speedometer" + } } } diff --git a/homeassistant/components/octoprint/config_flow.py b/homeassistant/components/octoprint/config_flow.py index 32f5fa88fff1cc..cd8706f235098e 100644 --- a/homeassistant/components/octoprint/config_flow.py +++ b/homeassistant/components/octoprint/config_flow.py @@ -5,7 +5,7 @@ import asyncio from collections.abc import Mapping import logging -from typing import Any +from typing import TYPE_CHECKING, Any import aiohttp from pyoctoprintapi import ApiError, OctoprintClient, OctoprintException @@ -63,7 +63,9 @@ def __init__(self) -> None: """Handle a config flow for OctoPrint.""" self._sessions: list[aiohttp.ClientSession] = [] - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" # When coming back from the progress steps, the user_input is stored in the # instance variable instead of being passed in @@ -102,7 +104,9 @@ async def async_step_user(self, user_input=None): self._user_input = user_input return await self.async_step_get_api_key() - async def async_step_get_api_key(self, user_input=None): + async def async_step_get_api_key( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Get an Application Api Key.""" if not self.api_key_task: self.api_key_task = self.hass.async_create_task( @@ -128,7 +132,7 @@ async def async_step_get_api_key(self, user_input=None): return self.async_show_progress_done(next_step_id="user") - async def _finish_config(self, user_input: dict): + async def _finish_config(self, user_input: dict[str, Any]) -> ConfigFlowResult: """Finish the configuration setup.""" existing_entry = await self.async_set_unique_id(self.unique_id) if existing_entry is not None: @@ -154,13 +158,13 @@ async def _finish_config(self, user_input: dict): return self.async_create_entry(title=user_input[CONF_HOST], data=user_input) - async def async_step_auth_failed(self, user_input): + async def async_step_auth_failed(self, user_input: None) -> ConfigFlowResult: """Handle api fetch failure.""" return self.async_abort(reason="auth_failed") - async def async_step_import(self, user_input): + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Handle import.""" - return await self.async_step_user(user_input) + return await self.async_step_user(import_data) async def async_step_zeroconf( self, discovery_info: zeroconf.ZeroconfServiceInfo @@ -213,13 +217,15 @@ async def async_step_ssdp( return await self.async_step_user() - async def async_step_reauth(self, config: Mapping[str, Any]) -> ConfigFlowResult: + async def async_step_reauth( + self, entry_data: Mapping[str, Any] + ) -> ConfigFlowResult: """Handle reauthorization request from Octoprint.""" - self._reauth_data = dict(config) + self._reauth_data = dict(entry_data) self.context.update( { - "title_placeholders": {CONF_HOST: config[CONF_HOST]}, + "title_placeholders": {CONF_HOST: entry_data[CONF_HOST]}, } ) @@ -248,15 +254,17 @@ async def async_step_reauth_confirm( self._user_input = self._reauth_data return await self.async_step_get_api_key() - async def _async_get_auth_key(self): + async def _async_get_auth_key(self) -> None: """Get application api key.""" + if TYPE_CHECKING: + assert self._user_input is not None octoprint = self._get_octoprint_client(self._user_input) self._user_input[CONF_API_KEY] = await octoprint.request_app_key( "Home Assistant", self._user_input[CONF_USERNAME], 300 ) - def _get_octoprint_client(self, user_input: dict) -> OctoprintClient: + def _get_octoprint_client(self, user_input: dict[str, Any]) -> OctoprintClient: """Build an octoprint client from the user_input.""" verify_ssl = user_input.get(CONF_VERIFY_SSL, True) @@ -277,7 +285,7 @@ def _get_octoprint_client(self, user_input: dict) -> OctoprintClient: path=user_input[CONF_PATH], ) - def async_remove(self): + def async_remove(self) -> None: """Detach the session.""" for session in self._sessions: session.detach() diff --git a/homeassistant/components/octoprint/icons.json b/homeassistant/components/octoprint/icons.json index 972ecabb765b09..720718fcedefe5 100644 --- a/homeassistant/components/octoprint/icons.json +++ b/homeassistant/components/octoprint/icons.json @@ -1,5 +1,7 @@ { "services": { - "printer_connect": "mdi:lan-connect" + "printer_connect": { + "service": "mdi:lan-connect" + } } } diff --git a/homeassistant/components/ollama/__init__.py b/homeassistant/components/ollama/__init__.py index 2286a2c7b7587c..3bcba567803a0b 100644 --- a/homeassistant/components/ollama/__init__.py +++ b/homeassistant/components/ollama/__init__.py @@ -13,11 +13,13 @@ from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.helpers import config_validation as cv +from homeassistant.util.ssl import get_default_context from .const import ( CONF_KEEP_ALIVE, CONF_MAX_HISTORY, CONF_MODEL, + CONF_NUM_CTX, CONF_PROMPT, DEFAULT_TIMEOUT, DOMAIN, @@ -30,6 +32,7 @@ "CONF_PROMPT", "CONF_MODEL", "CONF_MAX_HISTORY", + "CONF_NUM_CTX", "CONF_KEEP_ALIVE", "DOMAIN", ] @@ -41,7 +44,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Ollama from a config entry.""" settings = {**entry.data, **entry.options} - client = ollama.AsyncClient(host=settings[CONF_URL]) + client = ollama.AsyncClient(host=settings[CONF_URL], verify=get_default_context()) try: async with asyncio.timeout(DEFAULT_TIMEOUT): await client.list() diff --git a/homeassistant/components/ollama/config_flow.py b/homeassistant/components/ollama/config_flow.py index bcdd6e06f48eab..65b8efaf525c63 100644 --- a/homeassistant/components/ollama/config_flow.py +++ b/homeassistant/components/ollama/config_flow.py @@ -33,17 +33,22 @@ TextSelectorConfig, TextSelectorType, ) +from homeassistant.util.ssl import get_default_context from .const import ( CONF_KEEP_ALIVE, CONF_MAX_HISTORY, CONF_MODEL, + CONF_NUM_CTX, CONF_PROMPT, DEFAULT_KEEP_ALIVE, DEFAULT_MAX_HISTORY, DEFAULT_MODEL, + DEFAULT_NUM_CTX, DEFAULT_TIMEOUT, DOMAIN, + MAX_NUM_CTX, + MIN_NUM_CTX, MODEL_NAMES, ) @@ -87,7 +92,9 @@ async def async_step_user( errors = {} try: - self.client = ollama.AsyncClient(host=self.url) + self.client = ollama.AsyncClient( + host=self.url, verify=get_default_context() + ) async with asyncio.timeout(DEFAULT_TIMEOUT): response = await self.client.list() @@ -255,6 +262,14 @@ def ollama_config_option_schema( description={"suggested_value": options.get(CONF_LLM_HASS_API)}, default="none", ): SelectSelector(SelectSelectorConfig(options=hass_apis)), + vol.Optional( + CONF_NUM_CTX, + description={"suggested_value": options.get(CONF_NUM_CTX, DEFAULT_NUM_CTX)}, + ): NumberSelector( + NumberSelectorConfig( + min=MIN_NUM_CTX, max=MAX_NUM_CTX, step=1, mode=NumberSelectorMode.BOX + ) + ), vol.Optional( CONF_MAX_HISTORY, description={ diff --git a/homeassistant/components/ollama/const.py b/homeassistant/components/ollama/const.py index 97c4f1186fcccd..6152b223d6d2ba 100644 --- a/homeassistant/components/ollama/const.py +++ b/homeassistant/components/ollama/const.py @@ -11,6 +11,11 @@ KEEP_ALIVE_FOREVER = -1 DEFAULT_TIMEOUT = 5.0 # seconds +CONF_NUM_CTX = "num_ctx" +DEFAULT_NUM_CTX = 8192 +MIN_NUM_CTX = 2048 +MAX_NUM_CTX = 131072 + CONF_MAX_HISTORY = "max_history" DEFAULT_MAX_HISTORY = 20 diff --git a/homeassistant/components/ollama/conversation.py b/homeassistant/components/ollama/conversation.py index c0423b258f0455..1a91c790d2703b 100644 --- a/homeassistant/components/ollama/conversation.py +++ b/homeassistant/components/ollama/conversation.py @@ -26,9 +26,11 @@ CONF_KEEP_ALIVE, CONF_MAX_HISTORY, CONF_MODEL, + CONF_NUM_CTX, CONF_PROMPT, DEFAULT_KEEP_ALIVE, DEFAULT_MAX_HISTORY, + DEFAULT_NUM_CTX, DOMAIN, MAX_HISTORY_SECONDS, ) @@ -263,6 +265,7 @@ async def async_process( stream=False, # keep_alive requires specifying unit. In this case, seconds keep_alive=f"{settings.get(CONF_KEEP_ALIVE, DEFAULT_KEEP_ALIVE)}s", + options={CONF_NUM_CTX: settings.get(CONF_NUM_CTX, DEFAULT_NUM_CTX)}, ) except (ollama.RequestError, ollama.ResponseError) as err: _LOGGER.error("Unexpected error talking to Ollama server: %s", err) diff --git a/homeassistant/components/ollama/strings.json b/homeassistant/components/ollama/strings.json index 2366ecd084869a..c307f160228d47 100644 --- a/homeassistant/components/ollama/strings.json +++ b/homeassistant/components/ollama/strings.json @@ -27,11 +27,13 @@ "prompt": "Instructions", "llm_hass_api": "[%key:common::config_flow::data::llm_hass_api%]", "max_history": "Max history messages", + "num_ctx": "Context window size", "keep_alive": "Keep alive" }, "data_description": { "prompt": "Instruct how the LLM should respond. This can be a template.", - "keep_alive": "Duration in seconds for Ollama to keep model in memory. -1 = indefinite, 0 = never." + "keep_alive": "Duration in seconds for Ollama to keep model in memory. -1 = indefinite, 0 = never.", + "num_ctx": "Maximum number of text tokens the model can process. Lower to reduce Ollama RAM, or increase for a large number of exposed entities." } } } diff --git a/homeassistant/components/ombi/icons.json b/homeassistant/components/ombi/icons.json index 4b3e32a1e1301d..15b8af56188a7f 100644 --- a/homeassistant/components/ombi/icons.json +++ b/homeassistant/components/ombi/icons.json @@ -1,7 +1,13 @@ { "services": { - "submit_movie_request": "mdi:movie-roll", - "submit_tv_request": "mdi:television-classic", - "submit_music_request": "mdi:music" + "submit_movie_request": { + "service": "mdi:movie-roll" + }, + "submit_tv_request": { + "service": "mdi:television-classic" + }, + "submit_music_request": { + "service": "mdi:music" + } } } diff --git a/homeassistant/components/omnilogic/config_flow.py b/homeassistant/components/omnilogic/config_flow.py index 229f458ceb4d9f..77bca0039a9633 100644 --- a/homeassistant/components/omnilogic/config_flow.py +++ b/homeassistant/components/omnilogic/config_flow.py @@ -3,11 +3,17 @@ from __future__ import annotations import logging +from typing import Any from omnilogic import LoginException, OmniLogic, OmniLogicException import voluptuous as vol -from homeassistant.config_entries import ConfigEntry, ConfigFlow, OptionsFlow +from homeassistant.config_entries import ( + ConfigEntry, + ConfigFlow, + ConfigFlowResult, + OptionsFlow, +) from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.core import callback from homeassistant.helpers import aiohttp_client @@ -30,9 +36,11 @@ def async_get_options_flow( """Get the options flow for this handler.""" return OptionsFlowHandler(config_entry) - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" - errors = {} + errors: dict[str, str] = {} config_entry = self._async_current_entries() if config_entry: @@ -80,7 +88,9 @@ def __init__(self, config_entry: ConfigEntry) -> None: """Initialize options flow.""" self.config_entry = config_entry - async def async_step_init(self, user_input=None): + async def async_step_init( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Manage options.""" if user_input is not None: diff --git a/homeassistant/components/omnilogic/icons.json b/homeassistant/components/omnilogic/icons.json index ee5b51021779bb..8f0f13fe6522d8 100644 --- a/homeassistant/components/omnilogic/icons.json +++ b/homeassistant/components/omnilogic/icons.json @@ -1,5 +1,7 @@ { "services": { - "set_pump_speed": "mdi:water-pump" + "set_pump_speed": { + "service": "mdi:water-pump" + } } } diff --git a/homeassistant/components/onkyo/media_player.py b/homeassistant/components/onkyo/media_player.py index 8107c62e4a1e83..1718ecb36be91c 100644 --- a/homeassistant/components/onkyo/media_player.py +++ b/homeassistant/components/onkyo/media_player.py @@ -3,15 +3,14 @@ from __future__ import annotations import asyncio -from dataclasses import dataclass import logging -from typing import Any +from typing import Any, Literal import pyeiscp import voluptuous as vol from homeassistant.components.media_player import ( - DOMAIN, + DOMAIN as MEDIA_PLAYER_DOMAIN, PLATFORM_SCHEMA as MEDIA_PLAYER_PLATFORM_SCHEMA, MediaPlayerEntity, MediaPlayerEntityFeature, @@ -24,13 +23,20 @@ CONF_NAME, EVENT_HOMEASSISTANT_STOP, ) -from homeassistant.core import HomeAssistant, ServiceCall, callback +from homeassistant.core import Event, HomeAssistant, ServiceCall, callback from homeassistant.helpers import config_validation as cv from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType +from homeassistant.util.hass_dict import HassKey + +from .receiver import Receiver, ReceiverInfo _LOGGER = logging.getLogger(__name__) +DOMAIN = "onkyo" + +DATA_MP_ENTITIES: HassKey[list[dict[str, OnkyoMediaPlayer]]] = HassKey(DOMAIN) + CONF_SOURCES = "sources" CONF_MAX_VOLUME = "max_volume" CONF_RECEIVER_MAX_VOLUME = "receiver_max_volume" @@ -138,47 +144,45 @@ SERVICE_SELECT_HDMI_OUTPUT = "onkyo_select_hdmi_output" -@dataclass -class ReceiverInfo: - """Onkyo Receiver information.""" - - host: str - port: int - model_name: str - identifier: str - - -async def async_setup_platform( - hass: HomeAssistant, - config: ConfigType, - async_add_entities: AddEntitiesCallback, - discovery_info: DiscoveryInfoType | None = None, -) -> None: - """Set up the Onkyo platform.""" - receivers: dict[str, pyeiscp.Connection] = {} # indexed by host - entities: dict[str, dict[str, OnkyoMediaPlayer]] = {} # indexed by host and zone +async def async_register_services(hass: HomeAssistant) -> None: + """Register Onkyo services.""" async def async_service_handle(service: ServiceCall) -> None: """Handle for services.""" entity_ids = service.data[ATTR_ENTITY_ID] - targets = [ - entity - for h in entities.values() - for entity in h.values() - if entity.entity_id in entity_ids - ] + + targets: list[OnkyoMediaPlayer] = [] + for receiver_entities in hass.data[DATA_MP_ENTITIES]: + targets.extend( + entity + for entity in receiver_entities.values() + if entity.entity_id in entity_ids + ) for target in targets: if service.service == SERVICE_SELECT_HDMI_OUTPUT: await target.async_select_output(service.data[ATTR_HDMI_OUTPUT]) hass.services.async_register( - DOMAIN, + MEDIA_PLAYER_DOMAIN, SERVICE_SELECT_HDMI_OUTPUT, async_service_handle, schema=ONKYO_SELECT_OUTPUT_SCHEMA, ) + +async def async_setup_platform( + hass: HomeAssistant, + config: ConfigType, + async_add_entities: AddEntitiesCallback, + discovery_info: DiscoveryInfoType | None = None, +) -> None: + """Set up the Onkyo platform.""" + await async_register_services(hass) + + receivers: dict[str, Receiver] = {} # indexed by host + all_entities = hass.data.setdefault(DATA_MP_ENTITIES, []) + host = config.get(CONF_HOST) name = config.get(CONF_NAME) max_volume = config[CONF_MAX_VOLUME] @@ -188,6 +192,9 @@ async def async_service_handle(service: ServiceCall) -> None: async def async_setup_receiver( info: ReceiverInfo, discovered: bool, name: str | None ) -> None: + entities: dict[str, OnkyoMediaPlayer] = {} + all_entities.append(entities) + @callback def async_onkyo_update_callback( message: tuple[str, str, Any], origin: str @@ -199,7 +206,7 @@ def async_onkyo_update_callback( ) zone, _, value = message - entity = entities[origin].get(zone) + entity = entities.get(zone) if entity is not None: if entity.enabled: entity.process_update(message) @@ -210,7 +217,7 @@ def async_onkyo_update_callback( zone_entity = OnkyoMediaPlayer( receiver, sources, zone, max_volume, receiver_max_volume ) - entities[origin][zone] = zone_entity + entities[zone] = zone_entity async_add_entities([zone_entity]) @callback @@ -218,40 +225,41 @@ def async_onkyo_connect_callback(origin: str) -> None: """Receiver (re)connected.""" receiver = receivers[origin] _LOGGER.debug( - "Receiver (re)connected: %s (%s)", receiver.name, receiver.host + "Receiver (re)connected: %s (%s)", receiver.name, receiver.conn.host ) - for entity in entities[origin].values(): + for entity in entities.values(): entity.backfill_state() _LOGGER.debug("Creating receiver: %s (%s)", info.model_name, info.host) - receiver = await pyeiscp.Connection.create( + connection = await pyeiscp.Connection.create( host=info.host, port=info.port, update_callback=async_onkyo_update_callback, connect_callback=async_onkyo_connect_callback, ) - receiver.model_name = info.model_name - receiver.identifier = info.identifier - receiver.name = name or info.model_name - receiver.discovered = discovered + receiver = Receiver( + conn=connection, + model_name=info.model_name, + identifier=info.identifier, + name=name or info.model_name, + discovered=discovered, + ) - # Store the receiver object and create a dictionary to store its entities. - receivers[receiver.host] = receiver - entities[receiver.host] = {} + receivers[connection.host] = receiver # Discover what zones are available for the receiver by querying the power. # If we get a response for the specific zone, it means it is available. for zone in ZONES: - receiver.query_property(zone, "power") + receiver.conn.query_property(zone, "power") # Add the main zone to entities, since it is always active. _LOGGER.debug("Adding Main Zone on %s", receiver.name) main_entity = OnkyoMediaPlayer( receiver, sources, "main", max_volume, receiver_max_volume ) - entities[receiver.host]["main"] = main_entity + entities["main"] = main_entity async_add_entities([main_entity]) if host is not None: @@ -261,7 +269,7 @@ def async_onkyo_connect_callback(origin: str) -> None: _LOGGER.debug("Manually creating receiver: %s (%s)", name, host) @callback - async def async_onkyo_interview_callback(conn: pyeiscp.Connection): + async def async_onkyo_interview_callback(conn: pyeiscp.Connection) -> None: """Receiver interviewed, connection not yet active.""" info = ReceiverInfo(conn.host, conn.port, conn.name, conn.identifier) _LOGGER.debug("Receiver interviewed: %s (%s)", info.model_name, info.host) @@ -277,7 +285,7 @@ async def async_onkyo_interview_callback(conn: pyeiscp.Connection): _LOGGER.debug("Discovering receivers") @callback - async def async_onkyo_discovery_callback(conn: pyeiscp.Connection): + async def async_onkyo_discovery_callback(conn: pyeiscp.Connection) -> None: """Receiver discovered, connection not yet active.""" info = ReceiverInfo(conn.host, conn.port, conn.name, conn.identifier) _LOGGER.debug("Receiver discovered: %s (%s)", info.model_name, info.host) @@ -290,9 +298,9 @@ async def async_onkyo_discovery_callback(conn: pyeiscp.Connection): ) @callback - def close_receiver(_event): + def close_receiver(_event: Event) -> None: for receiver in receivers.values(): - receiver.close() + receiver.conn.close() hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, close_receiver) @@ -309,7 +317,7 @@ class OnkyoMediaPlayer(MediaPlayerEntity): def __init__( self, - receiver: pyeiscp.Connection, + receiver: Receiver, sources: dict[str, str], zone: str, max_volume: int, @@ -320,14 +328,11 @@ def __init__( name = receiver.name identifier = receiver.identifier self._attr_name = f"{name}{' ' + ZONES[zone] if zone != 'main' else ''}" - if receiver.discovered: - if zone == "main": - # keep legacy unique_id - self._attr_unique_id = f"{name}_{identifier}" - else: - self._attr_unique_id = f"{identifier}_{zone}" + if receiver.discovered and zone == "main": + # keep legacy unique_id + self._attr_unique_id = f"{name}_{identifier}" else: - self._attr_unique_id = None + self._attr_unique_id = f"{identifier}_{zone}" self._zone = zone self._source_mapping = sources @@ -358,12 +363,12 @@ def supported_features(self) -> MediaPlayerEntityFeature: @callback def _update_receiver(self, propname: str, value: Any) -> None: """Update a property in the receiver.""" - self._receiver.update_property(self._zone, propname, value) + self._receiver.conn.update_property(self._zone, propname, value) @callback def _query_receiver(self, propname: str) -> None: """Cause the receiver to send an update about a property.""" - self._receiver.query_property(self._zone, propname) + self._receiver.conn.query_property(self._zone, propname) async def async_turn_on(self) -> None: """Turn the media player on.""" @@ -490,19 +495,23 @@ def process_update(self, update: tuple[str, str, Any]) -> None: self.async_write_ha_state() @callback - def _parse_source(self, source): + def _parse_source(self, source_raw: str | int | tuple[str]) -> None: # source is either a tuple of values or a single value, # so we convert to a tuple, when it is a single value. - if not isinstance(source, tuple): - source = (source,) + if isinstance(source_raw, str | int): + source = (str(source_raw),) + else: + source = source_raw for value in source: if value in self._source_mapping: self._attr_source = self._source_mapping[value] - break - self._attr_source = "_".join(source) + return + self._attr_source = "_".join(source) @callback - def _parse_audio_information(self, audio_information): + def _parse_audio_information( + self, audio_information: tuple[str] | Literal["N/A"] + ) -> None: # If audio information is not available, N/A is returned, # so only update the audio information, when it is not N/A. if audio_information == "N/A": @@ -518,7 +527,9 @@ def _parse_audio_information(self, audio_information): } @callback - def _parse_video_information(self, video_information): + def _parse_video_information( + self, video_information: tuple[str] | Literal["N/A"] + ) -> None: # If video information is not available, N/A is returned, # so only update the video information, when it is not N/A. if video_information == "N/A": @@ -533,11 +544,11 @@ def _parse_video_information(self, video_information): if len(value) > 0 } - def _query_av_info_delayed(self): + def _query_av_info_delayed(self) -> None: if self._zone == "main" and not self._query_timer: @callback - def _query_av_info(): + def _query_av_info() -> None: if self._supports_audio_info: self._query_receiver("audio-information") if self._supports_video_info: diff --git a/homeassistant/components/onkyo/receiver.py b/homeassistant/components/onkyo/receiver.py new file mode 100644 index 00000000000000..eb20f327b6937f --- /dev/null +++ b/homeassistant/components/onkyo/receiver.py @@ -0,0 +1,28 @@ +"""Onkyo receiver.""" + +from __future__ import annotations + +from dataclasses import dataclass + +import pyeiscp + + +@dataclass +class Receiver: + """Onkyo receiver.""" + + conn: pyeiscp.Connection + model_name: str + identifier: str + name: str + discovered: bool + + +@dataclass +class ReceiverInfo: + """Onkyo receiver information.""" + + host: str + port: int + model_name: str + identifier: str diff --git a/homeassistant/components/onvif/config_flow.py b/homeassistant/components/onvif/config_flow.py index 36ae0e1bf1831d..f4e3f11d0b72c8 100644 --- a/homeassistant/components/onvif/config_flow.py +++ b/homeassistant/components/onvif/config_flow.py @@ -112,13 +112,15 @@ def async_get_options_flow( """Get the options flow for this handler.""" return OnvifOptionsFlowHandler(config_entry) - def __init__(self): + def __init__(self) -> None: """Initialize the ONVIF config flow.""" self.device_id = None - self.devices = [] - self.onvif_config = {} + self.devices: list[dict[str, Any]] = [] + self.onvif_config: dict[str, Any] = {} - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle user flow.""" if user_input: if user_input["auto"]: @@ -196,7 +198,9 @@ async def async_step_dhcp( hass.async_create_task(self.hass.config_entries.async_reload(entry_id)) return self.async_abort(reason="already_configured") - async def async_step_device(self, user_input=None): + async def async_step_device( + self, user_input: dict[str, str] | None = None + ) -> ConfigFlowResult: """Handle WS-Discovery. Let user choose between discovered devices and manual configuration. @@ -393,11 +397,13 @@ def __init__(self, config_entry: ConfigEntry) -> None: self.config_entry = config_entry self.options = dict(config_entry.options) - async def async_step_init(self, user_input=None): + async def async_step_init(self, user_input: None = None) -> ConfigFlowResult: """Manage the ONVIF options.""" return await self.async_step_onvif_devices() - async def async_step_onvif_devices(self, user_input=None): + async def async_step_onvif_devices( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Manage the ONVIF devices options.""" if user_input is not None: self.options[CONF_EXTRA_ARGUMENTS] = user_input[CONF_EXTRA_ARGUMENTS] diff --git a/homeassistant/components/onvif/icons.json b/homeassistant/components/onvif/icons.json index 4db9a9f9e49b2c..d42985d34e8209 100644 --- a/homeassistant/components/onvif/icons.json +++ b/homeassistant/components/onvif/icons.json @@ -13,6 +13,8 @@ } }, "services": { - "ptz": "mdi:pan" + "ptz": { + "service": "mdi:pan" + } } } diff --git a/homeassistant/components/openai_conversation/__init__.py b/homeassistant/components/openai_conversation/__init__.py index 75b5db230940e7..0fbda9b7f4a0f2 100644 --- a/homeassistant/components/openai_conversation/__init__.py +++ b/homeassistant/components/openai_conversation/__init__.py @@ -19,6 +19,7 @@ ServiceValidationError, ) from homeassistant.helpers import config_validation as cv, selector +from homeassistant.helpers.httpx_client import get_async_client from homeassistant.helpers.typing import ConfigType from .const import DOMAIN, LOGGER @@ -88,7 +89,14 @@ async def render_image(call: ServiceCall) -> ServiceResponse: async def async_setup_entry(hass: HomeAssistant, entry: OpenAIConfigEntry) -> bool: """Set up OpenAI Conversation from a config entry.""" - client = openai.AsyncOpenAI(api_key=entry.data[CONF_API_KEY]) + client = openai.AsyncOpenAI( + api_key=entry.data[CONF_API_KEY], + http_client=get_async_client(hass), + ) + + # Cache current platform data which gets added to each request (caching done by library) + _ = await hass.async_add_executor_job(client.platform_headers) + try: await hass.async_add_executor_job(client.with_options(timeout=10.0).models.list) except openai.AuthenticationError as err: diff --git a/homeassistant/components/openai_conversation/icons.json b/homeassistant/components/openai_conversation/icons.json index 7f736a5ff3b2f1..3abecd640d18ee 100644 --- a/homeassistant/components/openai_conversation/icons.json +++ b/homeassistant/components/openai_conversation/icons.json @@ -1,5 +1,7 @@ { "services": { - "generate_image": "mdi:image-sync" + "generate_image": { + "service": "mdi:image-sync" + } } } diff --git a/homeassistant/components/openhome/icons.json b/homeassistant/components/openhome/icons.json index 081e97c3489ccf..d75659f17daa3c 100644 --- a/homeassistant/components/openhome/icons.json +++ b/homeassistant/components/openhome/icons.json @@ -1,5 +1,7 @@ { "services": { - "invoke_pin": "mdi:alarm-panel" + "invoke_pin": { + "service": "mdi:alarm-panel" + } } } diff --git a/homeassistant/components/opentherm_gw/__init__.py b/homeassistant/components/opentherm_gw/__init__.py index 30410f73c2d2a1..dfce2206df764c 100644 --- a/homeassistant/components/opentherm_gw/__init__.py +++ b/homeassistant/components/opentherm_gw/__init__.py @@ -4,11 +4,12 @@ from datetime import date, datetime import logging -import pyotgw +from pyotgw import OpenThermGateway import pyotgw.vars as gw_vars from serial import SerialException import voluptuous as vol +from homeassistant.components.climate import DOMAIN as CLIMATE_DOMAIN from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry from homeassistant.const import ( ATTR_DATE, @@ -27,7 +28,11 @@ ) from homeassistant.core import HomeAssistant, ServiceCall from homeassistant.exceptions import ConfigEntryNotReady -from homeassistant.helpers import config_validation as cv, device_registry as dr +from homeassistant.helpers import ( + config_validation as cv, + device_registry as dr, + entity_registry as er, +) from homeassistant.helpers.dispatcher import async_dispatcher_send from homeassistant.helpers.typing import ConfigType @@ -41,8 +46,6 @@ CONF_CLIMATE, CONF_FLOOR_TEMP, CONF_PRECISION, - CONF_READ_PRECISION, - CONF_SET_PRECISION, CONNECTION_TIMEOUT, DATA_GATEWAYS, DATA_OPENTHERM_GW, @@ -59,6 +62,8 @@ SERVICE_SET_MAX_MOD, SERVICE_SET_OAT, SERVICE_SET_SB_TEMP, + OpenThermDataSource, + OpenThermDeviceIdentifier, ) _LOGGER = logging.getLogger(__name__) @@ -85,7 +90,7 @@ extra=vol.ALLOW_EXTRA, ) -PLATFORMS = [Platform.BINARY_SENSOR, Platform.CLIMATE, Platform.SENSOR] +PLATFORMS = [Platform.BINARY_SENSOR, Platform.BUTTON, Platform.CLIMATE, Platform.SENSOR] async def options_updated(hass: HomeAssistant, entry: ConfigEntry) -> None: @@ -102,16 +107,34 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b gateway = OpenThermGatewayHub(hass, config_entry) hass.data[DATA_OPENTHERM_GW][DATA_GATEWAYS][config_entry.data[CONF_ID]] = gateway - if config_entry.options.get(CONF_PRECISION): - migrate_options = dict(config_entry.options) - migrate_options.update( - { - CONF_READ_PRECISION: config_entry.options[CONF_PRECISION], - CONF_SET_PRECISION: config_entry.options[CONF_PRECISION], - } + # Migration can be removed in 2025.4.0 + dev_reg = dr.async_get(hass) + if ( + migrate_device := dev_reg.async_get_device( + {(DOMAIN, config_entry.data[CONF_ID])} + ) + ) is not None: + dev_reg.async_update_device( + migrate_device.id, + new_identifiers={ + ( + DOMAIN, + f"{config_entry.data[CONF_ID]}-{OpenThermDeviceIdentifier.GATEWAY}", + ) + }, + ) + + # Migration can be removed in 2025.4.0 + ent_reg = er.async_get(hass) + if ( + entity_id := ent_reg.async_get_entity_id( + CLIMATE_DOMAIN, DOMAIN, config_entry.data[CONF_ID] + ) + ) is not None: + ent_reg.async_update_entity( + entity_id, + new_unique_id=f"{config_entry.data[CONF_ID]}-{OpenThermDeviceIdentifier.THERMOSTAT}-thermostat_entity", ) - del migrate_options[CONF_PRECISION] - hass.config_entries.async_update_entry(config_entry, options=migrate_options) config_entry.add_update_listener(options_updated) @@ -427,10 +450,9 @@ def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) -> None: self.name = config_entry.data[CONF_NAME] self.climate_config = config_entry.options self.config_entry_id = config_entry.entry_id - self.status = gw_vars.DEFAULT_STATUS self.update_signal = f"{DATA_OPENTHERM_GW}_{self.hub_id}_update" self.options_update_signal = f"{DATA_OPENTHERM_GW}_{self.hub_id}_options_update" - self.gateway = pyotgw.OpenThermGateway() + self.gateway = OpenThermGateway() self.gw_version = None async def cleanup(self, event=None) -> None: @@ -441,11 +463,11 @@ async def cleanup(self, event=None) -> None: async def connect_and_subscribe(self) -> None: """Connect to serial device and subscribe report handler.""" - self.status = await self.gateway.connect(self.device_path) - if not self.status: + status = await self.gateway.connect(self.device_path) + if not status: await self.cleanup() raise ConnectionError - version_string = self.status[gw_vars.OTGW].get(gw_vars.OTGW_ABOUT) + version_string = status[OpenThermDataSource.GATEWAY].get(gw_vars.OTGW_ABOUT) self.gw_version = version_string[18:] if version_string else None _LOGGER.debug( "Connected to OpenTherm Gateway %s at %s", self.gw_version, self.device_path @@ -453,22 +475,69 @@ async def connect_and_subscribe(self) -> None: dev_reg = dr.async_get(self.hass) gw_dev = dev_reg.async_get_or_create( config_entry_id=self.config_entry_id, - identifiers={(DOMAIN, self.hub_id)}, - name=self.name, + identifiers={ + (DOMAIN, f"{self.hub_id}-{OpenThermDeviceIdentifier.GATEWAY}") + }, manufacturer="Schelte Bron", model="OpenTherm Gateway", + translation_key="gateway_device", sw_version=self.gw_version, ) if gw_dev.sw_version != self.gw_version: dev_reg.async_update_device(gw_dev.id, sw_version=self.gw_version) + + boiler_device = dev_reg.async_get_or_create( + config_entry_id=self.config_entry_id, + identifiers={(DOMAIN, f"{self.hub_id}-{OpenThermDeviceIdentifier.BOILER}")}, + translation_key="boiler_device", + ) + thermostat_device = dev_reg.async_get_or_create( + config_entry_id=self.config_entry_id, + identifiers={ + (DOMAIN, f"{self.hub_id}-{OpenThermDeviceIdentifier.THERMOSTAT}") + }, + translation_key="thermostat_device", + ) + self.hass.bus.async_listen(EVENT_HOMEASSISTANT_STOP, self.cleanup) async def handle_report(status): """Handle reports from the OpenTherm Gateway.""" _LOGGER.debug("Received report: %s", status) - self.status = status async_dispatcher_send(self.hass, self.update_signal, status) + dev_reg.async_update_device( + boiler_device.id, + manufacturer=status[OpenThermDataSource.BOILER].get( + gw_vars.DATA_SLAVE_MEMBERID + ), + model_id=status[OpenThermDataSource.BOILER].get( + gw_vars.DATA_SLAVE_PRODUCT_TYPE + ), + hw_version=status[OpenThermDataSource.BOILER].get( + gw_vars.DATA_SLAVE_PRODUCT_VERSION + ), + sw_version=status[OpenThermDataSource.BOILER].get( + gw_vars.DATA_SLAVE_OT_VERSION + ), + ) + + dev_reg.async_update_device( + thermostat_device.id, + manufacturer=status[OpenThermDataSource.THERMOSTAT].get( + gw_vars.DATA_MASTER_MEMBERID + ), + model_id=status[OpenThermDataSource.THERMOSTAT].get( + gw_vars.DATA_MASTER_PRODUCT_TYPE + ), + hw_version=status[OpenThermDataSource.THERMOSTAT].get( + gw_vars.DATA_MASTER_PRODUCT_VERSION + ), + sw_version=status[OpenThermDataSource.THERMOSTAT].get( + gw_vars.DATA_MASTER_OT_VERSION + ), + ) + self.gateway.subscribe(handle_report) @property diff --git a/homeassistant/components/opentherm_gw/binary_sensor.py b/homeassistant/components/opentherm_gw/binary_sensor.py index f978a2695d76bf..5d542bedc074e2 100644 --- a/homeassistant/components/opentherm_gw/binary_sensor.py +++ b/homeassistant/components/opentherm_gw/binary_sensor.py @@ -5,281 +5,387 @@ from pyotgw import vars as gw_vars from homeassistant.components.binary_sensor import ( - ENTITY_ID_FORMAT, BinarySensorDeviceClass, BinarySensorEntity, BinarySensorEntityDescription, ) from homeassistant.config_entries import ConfigEntry -from homeassistant.const import CONF_ID +from homeassistant.const import CONF_ID, EntityCategory from homeassistant.core import HomeAssistant, callback -from homeassistant.helpers.entity import async_generate_entity_id from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import OpenThermGatewayHub -from .const import DATA_GATEWAYS, DATA_OPENTHERM_GW -from .entity import OpenThermEntity, OpenThermEntityDescription +from .const import ( + BOILER_DEVICE_DESCRIPTION, + DATA_GATEWAYS, + DATA_OPENTHERM_GW, + GATEWAY_DEVICE_DESCRIPTION, + THERMOSTAT_DEVICE_DESCRIPTION, + OpenThermDataSource, +) +from .entity import OpenThermEntityDescription, OpenThermStatusEntity @dataclass(frozen=True, kw_only=True) class OpenThermBinarySensorEntityDescription( - BinarySensorEntityDescription, OpenThermEntityDescription + OpenThermEntityDescription, BinarySensorEntityDescription ): """Describes opentherm_gw binary sensor entity.""" -BINARY_SENSOR_INFO: tuple[ - tuple[list[str], OpenThermBinarySensorEntityDescription], ... -] = ( - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermBinarySensorEntityDescription( - key=gw_vars.DATA_MASTER_CH_ENABLED, - friendly_name_format="Thermostat Central Heating {}", - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermBinarySensorEntityDescription( - key=gw_vars.DATA_MASTER_DHW_ENABLED, - friendly_name_format="Thermostat Hot Water {}", - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermBinarySensorEntityDescription( - key=gw_vars.DATA_MASTER_COOLING_ENABLED, - friendly_name_format="Thermostat Cooling {}", - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermBinarySensorEntityDescription( - key=gw_vars.DATA_MASTER_OTC_ENABLED, - friendly_name_format="Thermostat Outside Temperature Correction {}", - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermBinarySensorEntityDescription( - key=gw_vars.DATA_MASTER_CH2_ENABLED, - friendly_name_format="Thermostat Central Heating 2 {}", - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermBinarySensorEntityDescription( - key=gw_vars.DATA_SLAVE_FAULT_IND, - friendly_name_format="Boiler Fault {}", - device_class=BinarySensorDeviceClass.PROBLEM, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermBinarySensorEntityDescription( - key=gw_vars.DATA_SLAVE_CH_ACTIVE, - friendly_name_format="Boiler Central Heating {}", - device_class=BinarySensorDeviceClass.HEAT, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermBinarySensorEntityDescription( - key=gw_vars.DATA_SLAVE_DHW_ACTIVE, - friendly_name_format="Boiler Hot Water {}", - device_class=BinarySensorDeviceClass.HEAT, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermBinarySensorEntityDescription( - key=gw_vars.DATA_SLAVE_FLAME_ON, - friendly_name_format="Boiler Flame {}", - device_class=BinarySensorDeviceClass.HEAT, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermBinarySensorEntityDescription( - key=gw_vars.DATA_SLAVE_COOLING_ACTIVE, - friendly_name_format="Boiler Cooling {}", - device_class=BinarySensorDeviceClass.COLD, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermBinarySensorEntityDescription( - key=gw_vars.DATA_SLAVE_CH2_ACTIVE, - friendly_name_format="Boiler Central Heating 2 {}", - device_class=BinarySensorDeviceClass.HEAT, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermBinarySensorEntityDescription( - key=gw_vars.DATA_SLAVE_DIAG_IND, - friendly_name_format="Boiler Diagnostics {}", - device_class=BinarySensorDeviceClass.PROBLEM, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermBinarySensorEntityDescription( - key=gw_vars.DATA_SLAVE_DHW_PRESENT, - friendly_name_format="Boiler Hot Water Present {}", - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermBinarySensorEntityDescription( - key=gw_vars.DATA_SLAVE_CONTROL_TYPE, - friendly_name_format="Boiler Control Type {}", - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermBinarySensorEntityDescription( - key=gw_vars.DATA_SLAVE_COOLING_SUPPORTED, - friendly_name_format="Boiler Cooling Support {}", - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermBinarySensorEntityDescription( - key=gw_vars.DATA_SLAVE_DHW_CONFIG, - friendly_name_format="Boiler Hot Water Configuration {}", - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermBinarySensorEntityDescription( - key=gw_vars.DATA_SLAVE_MASTER_LOW_OFF_PUMP, - friendly_name_format="Boiler Pump Commands Support {}", - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermBinarySensorEntityDescription( - key=gw_vars.DATA_SLAVE_CH2_PRESENT, - friendly_name_format="Boiler Central Heating 2 Present {}", - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermBinarySensorEntityDescription( - key=gw_vars.DATA_SLAVE_SERVICE_REQ, - friendly_name_format="Boiler Service Required {}", - device_class=BinarySensorDeviceClass.PROBLEM, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermBinarySensorEntityDescription( - key=gw_vars.DATA_SLAVE_REMOTE_RESET, - friendly_name_format="Boiler Remote Reset Support {}", - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermBinarySensorEntityDescription( - key=gw_vars.DATA_SLAVE_LOW_WATER_PRESS, - friendly_name_format="Boiler Low Water Pressure {}", - device_class=BinarySensorDeviceClass.PROBLEM, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermBinarySensorEntityDescription( - key=gw_vars.DATA_SLAVE_GAS_FAULT, - friendly_name_format="Boiler Gas Fault {}", - device_class=BinarySensorDeviceClass.PROBLEM, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermBinarySensorEntityDescription( - key=gw_vars.DATA_SLAVE_AIR_PRESS_FAULT, - friendly_name_format="Boiler Air Pressure Fault {}", - device_class=BinarySensorDeviceClass.PROBLEM, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermBinarySensorEntityDescription( - key=gw_vars.DATA_SLAVE_WATER_OVERTEMP, - friendly_name_format="Boiler Water Overtemperature {}", - device_class=BinarySensorDeviceClass.PROBLEM, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermBinarySensorEntityDescription( - key=gw_vars.DATA_REMOTE_TRANSFER_DHW, - friendly_name_format="Remote Hot Water Setpoint Transfer Support {}", - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermBinarySensorEntityDescription( - key=gw_vars.DATA_REMOTE_TRANSFER_MAX_CH, - friendly_name_format="Remote Maximum Central Heating Setpoint Write Support {}", - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermBinarySensorEntityDescription( - key=gw_vars.DATA_REMOTE_RW_DHW, - friendly_name_format="Remote Hot Water Setpoint Write Support {}", - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermBinarySensorEntityDescription( - key=gw_vars.DATA_REMOTE_RW_MAX_CH, - friendly_name_format="Remote Central Heating Setpoint Write Support {}", - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermBinarySensorEntityDescription( - key=gw_vars.DATA_ROVRD_MAN_PRIO, - friendly_name_format="Remote Override Manual Change Priority {}", - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermBinarySensorEntityDescription( - key=gw_vars.DATA_ROVRD_AUTO_PRIO, - friendly_name_format="Remote Override Program Change Priority {}", - ), - ), - ( - [gw_vars.OTGW], - OpenThermBinarySensorEntityDescription( - key=gw_vars.OTGW_GPIO_A_STATE, - friendly_name_format="Gateway GPIO A {}", - ), - ), - ( - [gw_vars.OTGW], - OpenThermBinarySensorEntityDescription( - key=gw_vars.OTGW_GPIO_B_STATE, - friendly_name_format="Gateway GPIO B {}", - ), - ), - ( - [gw_vars.OTGW], - OpenThermBinarySensorEntityDescription( - key=gw_vars.OTGW_IGNORE_TRANSITIONS, - friendly_name_format="Gateway Ignore Transitions {}", - ), - ), - ( - [gw_vars.OTGW], - OpenThermBinarySensorEntityDescription( - key=gw_vars.OTGW_OVRD_HB, - friendly_name_format="Gateway Override High Byte {}", - ), +BINARY_SENSOR_DESCRIPTIONS: tuple[OpenThermBinarySensorEntityDescription, ...] = ( + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_FAULT_IND, + translation_key="fault_indication", + device_class=BinarySensorDeviceClass.PROBLEM, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_CH_ACTIVE, + translation_key="central_heating_n", + translation_placeholders={"circuit_number": "1"}, + device_class=BinarySensorDeviceClass.RUNNING, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_CH2_ACTIVE, + translation_key="central_heating_n", + translation_placeholders={"circuit_number": "2"}, + device_class=BinarySensorDeviceClass.RUNNING, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_DHW_ACTIVE, + translation_key="hot_water", + device_class=BinarySensorDeviceClass.RUNNING, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_FLAME_ON, + translation_key="flame", + device_class=BinarySensorDeviceClass.HEAT, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_COOLING_ACTIVE, + translation_key="cooling", + device_class=BinarySensorDeviceClass.RUNNING, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_DIAG_IND, + translation_key="diagnostic_indication", + device_class=BinarySensorDeviceClass.PROBLEM, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_DHW_PRESENT, + translation_key="supports_hot_water", + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_CONTROL_TYPE, + translation_key="control_type", + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_COOLING_SUPPORTED, + translation_key="supports_cooling", + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_DHW_CONFIG, + translation_key="hot_water_config", + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_MASTER_LOW_OFF_PUMP, + translation_key="supports_pump_control", + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_CH2_PRESENT, + translation_key="supports_ch_2", + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_SERVICE_REQ, + translation_key="service_required", + device_class=BinarySensorDeviceClass.PROBLEM, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_REMOTE_RESET, + translation_key="supports_remote_reset", + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_LOW_WATER_PRESS, + translation_key="low_water_pressure", + device_class=BinarySensorDeviceClass.PROBLEM, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_GAS_FAULT, + translation_key="gas_fault", + device_class=BinarySensorDeviceClass.PROBLEM, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_AIR_PRESS_FAULT, + translation_key="air_pressure_fault", + device_class=BinarySensorDeviceClass.PROBLEM, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_WATER_OVERTEMP, + translation_key="water_overtemperature", + device_class=BinarySensorDeviceClass.PROBLEM, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_REMOTE_TRANSFER_MAX_CH, + translation_key="supports_central_heating_setpoint_transfer", + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_REMOTE_RW_MAX_CH, + translation_key="supports_central_heating_setpoint_writing", + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_REMOTE_TRANSFER_DHW, + translation_key="supports_hot_water_setpoint_transfer", + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_REMOTE_RW_DHW, + translation_key="supports_hot_water_setpoint_writing", + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.OTGW_GPIO_A_STATE, + translation_key="gpio_state_n", + translation_placeholders={"gpio_id": "A"}, + device_description=GATEWAY_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.OTGW_GPIO_B_STATE, + translation_key="gpio_state_n", + translation_placeholders={"gpio_id": "B"}, + device_description=GATEWAY_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.OTGW_IGNORE_TRANSITIONS, + translation_key="ignore_transitions", + device_description=GATEWAY_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.OTGW_OVRD_HB, + translation_key="override_high_byte", + device_description=GATEWAY_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_MASTER_CH_ENABLED, + translation_key="central_heating_n", + translation_placeholders={"circuit_number": "1"}, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_MASTER_CH2_ENABLED, + translation_key="central_heating_n", + translation_placeholders={"circuit_number": "2"}, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_MASTER_DHW_ENABLED, + translation_key="hot_water", + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_MASTER_COOLING_ENABLED, + translation_key="cooling", + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_MASTER_OTC_ENABLED, + translation_key="outside_temp_correction", + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_ROVRD_MAN_PRIO, + translation_key="override_manual_change_prio", + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_ROVRD_AUTO_PRIO, + translation_key="override_program_change_prio", + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_FAULT_IND, + translation_key="fault_indication", + device_class=BinarySensorDeviceClass.PROBLEM, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_CH_ACTIVE, + translation_key="central_heating_n", + translation_placeholders={"circuit_number": "1"}, + device_class=BinarySensorDeviceClass.RUNNING, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_CH2_ACTIVE, + translation_key="central_heating_n", + translation_placeholders={"circuit_number": "2"}, + device_class=BinarySensorDeviceClass.RUNNING, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_DHW_ACTIVE, + translation_key="hot_water", + device_class=BinarySensorDeviceClass.RUNNING, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_FLAME_ON, + translation_key="flame", + device_class=BinarySensorDeviceClass.HEAT, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_COOLING_ACTIVE, + translation_key="cooling", + device_class=BinarySensorDeviceClass.RUNNING, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_DIAG_IND, + translation_key="diagnostic_indication", + device_class=BinarySensorDeviceClass.PROBLEM, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_DHW_PRESENT, + translation_key="supports_hot_water", + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_CONTROL_TYPE, + translation_key="control_type", + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_COOLING_SUPPORTED, + translation_key="supports_cooling", + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_DHW_CONFIG, + translation_key="hot_water_config", + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_MASTER_LOW_OFF_PUMP, + translation_key="supports_pump_control", + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_CH2_PRESENT, + translation_key="supports_ch_2", + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_SERVICE_REQ, + translation_key="service_required", + device_class=BinarySensorDeviceClass.PROBLEM, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_REMOTE_RESET, + translation_key="supports_remote_reset", + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_LOW_WATER_PRESS, + translation_key="low_water_pressure", + device_class=BinarySensorDeviceClass.PROBLEM, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_GAS_FAULT, + translation_key="gas_fault", + device_class=BinarySensorDeviceClass.PROBLEM, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_AIR_PRESS_FAULT, + translation_key="air_pressure_fault", + device_class=BinarySensorDeviceClass.PROBLEM, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_SLAVE_WATER_OVERTEMP, + translation_key="water_overtemperature", + device_class=BinarySensorDeviceClass.PROBLEM, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_REMOTE_TRANSFER_MAX_CH, + translation_key="supports_central_heating_setpoint_transfer", + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_REMOTE_RW_MAX_CH, + translation_key="supports_central_heating_setpoint_writing", + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_REMOTE_TRANSFER_DHW, + translation_key="supports_hot_water_setpoint_transfer", + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_REMOTE_RW_DHW, + translation_key="supports_hot_water_setpoint_writing", + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_MASTER_CH_ENABLED, + translation_key="central_heating_n", + translation_placeholders={"circuit_number": "1"}, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_MASTER_CH2_ENABLED, + translation_key="central_heating_n", + translation_placeholders={"circuit_number": "2"}, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_MASTER_DHW_ENABLED, + translation_key="hot_water", + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_MASTER_COOLING_ENABLED, + translation_key="cooling", + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_MASTER_OTC_ENABLED, + translation_key="outside_temp_correction", + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_ROVRD_MAN_PRIO, + translation_key="override_manual_change_prio", + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermBinarySensorEntityDescription( + key=gw_vars.DATA_ROVRD_AUTO_PRIO, + translation_key="override_program_change_prio", + device_description=BOILER_DEVICE_DESCRIPTION, ), ) @@ -293,35 +399,22 @@ async def async_setup_entry( gw_hub = hass.data[DATA_OPENTHERM_GW][DATA_GATEWAYS][config_entry.data[CONF_ID]] async_add_entities( - OpenThermBinarySensor(gw_hub, source, description) - for sources, description in BINARY_SENSOR_INFO - for source in sources + OpenThermBinarySensor(gw_hub, description) + for description in BINARY_SENSOR_DESCRIPTIONS ) -class OpenThermBinarySensor(OpenThermEntity, BinarySensorEntity): +class OpenThermBinarySensor(OpenThermStatusEntity, BinarySensorEntity): """Represent an OpenTherm Gateway binary sensor.""" + _attr_entity_category = EntityCategory.DIAGNOSTIC entity_description: OpenThermBinarySensorEntityDescription - def __init__( - self, - gw_hub: OpenThermGatewayHub, - source: str, - description: OpenThermBinarySensorEntityDescription, - ) -> None: - """Initialize the binary sensor.""" - self.entity_id = async_generate_entity_id( - ENTITY_ID_FORMAT, - f"{description.key}_{source}_{gw_hub.hub_id}", - hass=gw_hub.hass, - ) - super().__init__(gw_hub, source, description) - @callback - def receive_report(self, status: dict[str, dict]) -> None: + def receive_report(self, status: dict[OpenThermDataSource, dict]) -> None: """Handle status updates from the component.""" - self._attr_available = self._gateway.connected - state = status[self._source].get(self.entity_description.key) + state = status[self.entity_description.device_description.data_source].get( + self.entity_description.key + ) self._attr_is_on = None if state is None else bool(state) self.async_write_ha_state() diff --git a/homeassistant/components/opentherm_gw/button.py b/homeassistant/components/opentherm_gw/button.py new file mode 100644 index 00000000000000..bac50295199ab1 --- /dev/null +++ b/homeassistant/components/opentherm_gw/button.py @@ -0,0 +1,63 @@ +"""Support for OpenTherm Gateway buttons.""" + +from collections.abc import Awaitable, Callable +from dataclasses import dataclass + +import pyotgw.vars as gw_vars + +from homeassistant.components.button import ( + ButtonDeviceClass, + ButtonEntity, + ButtonEntityDescription, +) +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import CONF_ID, EntityCategory +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from . import OpenThermGatewayHub +from .const import DATA_GATEWAYS, DATA_OPENTHERM_GW, GATEWAY_DEVICE_DESCRIPTION +from .entity import OpenThermEntity, OpenThermEntityDescription + + +@dataclass(frozen=True, kw_only=True) +class OpenThermButtonEntityDescription( + ButtonEntityDescription, OpenThermEntityDescription +): + """Describes an opentherm_gw button entity.""" + + action: Callable[[OpenThermGatewayHub], Awaitable] + + +BUTTON_DESCRIPTIONS: tuple[OpenThermButtonEntityDescription, ...] = ( + OpenThermButtonEntityDescription( + key="restart_button", + device_class=ButtonDeviceClass.RESTART, + device_description=GATEWAY_DEVICE_DESCRIPTION, + action=lambda hub: hub.gateway.set_mode(gw_vars.OTGW_MODE_RESET), + ), +) + + +async def async_setup_entry( + hass: HomeAssistant, + config_entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up the OpenTherm Gateway buttons.""" + gw_hub = hass.data[DATA_OPENTHERM_GW][DATA_GATEWAYS][config_entry.data[CONF_ID]] + + async_add_entities( + OpenThermButton(gw_hub, description) for description in BUTTON_DESCRIPTIONS + ) + + +class OpenThermButton(OpenThermEntity, ButtonEntity): + """Representation of an OpenTherm button.""" + + _attr_entity_category = EntityCategory.CONFIG + entity_description: OpenThermButtonEntityDescription + + async def async_press(self) -> None: + """Perform button action.""" + await self.entity_description.action(self._gateway) diff --git a/homeassistant/components/opentherm_gw/climate.py b/homeassistant/components/opentherm_gw/climate.py index bf295fb1fb7aec..6edfeb35ec3fc4 100644 --- a/homeassistant/components/opentherm_gw/climate.py +++ b/homeassistant/components/opentherm_gw/climate.py @@ -2,50 +2,52 @@ from __future__ import annotations +from dataclasses import dataclass import logging +from types import MappingProxyType from typing import Any from pyotgw import vars as gw_vars from homeassistant.components.climate import ( - ENTITY_ID_FORMAT, PRESET_AWAY, PRESET_NONE, ClimateEntity, + ClimateEntityDescription, ClimateEntityFeature, HVACAction, HVACMode, ) from homeassistant.config_entries import ConfigEntry -from homeassistant.const import ( - ATTR_TEMPERATURE, - CONF_ID, - PRECISION_HALVES, - PRECISION_TENTHS, - PRECISION_WHOLE, - UnitOfTemperature, -) +from homeassistant.const import ATTR_TEMPERATURE, CONF_ID, UnitOfTemperature from homeassistant.core import HomeAssistant, callback -from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.dispatcher import async_dispatcher_connect -from homeassistant.helpers.entity import async_generate_entity_id from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import DOMAIN +from . import OpenThermGatewayHub from .const import ( - CONF_FLOOR_TEMP, CONF_READ_PRECISION, CONF_SET_PRECISION, CONF_TEMPORARY_OVRD_MODE, DATA_GATEWAYS, DATA_OPENTHERM_GW, + THERMOSTAT_DEVICE_DESCRIPTION, + OpenThermDataSource, ) +from .entity import OpenThermEntityDescription, OpenThermStatusEntity _LOGGER = logging.getLogger(__name__) DEFAULT_FLOOR_TEMP = False +@dataclass(frozen=True, kw_only=True) +class OpenThermClimateEntityDescription( + ClimateEntityDescription, OpenThermEntityDescription +): + """Describes an opentherm_gw climate entity.""" + + async def async_setup_entry( hass: HomeAssistant, config_entry: ConfigEntry, @@ -56,6 +58,10 @@ async def async_setup_entry( ents.append( OpenThermClimate( hass.data[DATA_OPENTHERM_GW][DATA_GATEWAYS][config_entry.data[CONF_ID]], + OpenThermClimateEntityDescription( + key="thermostat_entity", + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), config_entry.options, ) ) @@ -63,98 +69,81 @@ async def async_setup_entry( async_add_entities(ents) -class OpenThermClimate(ClimateEntity): +class OpenThermClimate(OpenThermStatusEntity, ClimateEntity): """Representation of a climate device.""" - _attr_should_poll = False _attr_supported_features = ( ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.PRESET_MODE ) _attr_temperature_unit = UnitOfTemperature.CELSIUS - _attr_available = False _attr_hvac_modes = [] + _attr_name = None _attr_preset_modes = [] _attr_min_temp = 1 _attr_max_temp = 30 - _hvac_mode = HVACMode.HEAT - _current_temperature: float | None = None - _new_target_temperature: float | None = None - _target_temperature: float | None = None + _attr_hvac_mode = HVACMode.HEAT _away_mode_a: int | None = None _away_mode_b: int | None = None _away_state_a = False _away_state_b = False - _current_operation: HVACAction | None = None _enable_turn_on_off_backwards_compatibility = False - - def __init__(self, gw_hub, options): - """Initialize the device.""" - self._gateway = gw_hub - self.entity_id = async_generate_entity_id( - ENTITY_ID_FORMAT, gw_hub.hub_id, hass=gw_hub.hass - ) - self.friendly_name = gw_hub.name - self._attr_name = self.friendly_name - self.floor_temp = options.get(CONF_FLOOR_TEMP, DEFAULT_FLOOR_TEMP) - self.temp_read_precision = options.get(CONF_READ_PRECISION) - self.temp_set_precision = options.get(CONF_SET_PRECISION) + _target_temperature: float | None = None + _new_target_temperature: float | None = None + entity_description: OpenThermClimateEntityDescription + + def __init__( + self, + gw_hub: OpenThermGatewayHub, + description: OpenThermClimateEntityDescription, + options: MappingProxyType[str, Any], + ) -> None: + """Initialize the entity.""" + super().__init__(gw_hub, description) + if CONF_READ_PRECISION in options: + self._attr_precision = options[CONF_READ_PRECISION] + self._attr_target_temperature_step = options.get(CONF_SET_PRECISION) self.temporary_ovrd_mode = options.get(CONF_TEMPORARY_OVRD_MODE, True) - self._unsub_options = None - self._unsub_updates = None - self._attr_device_info = DeviceInfo( - identifiers={(DOMAIN, gw_hub.hub_id)}, - manufacturer="Schelte Bron", - model="OpenTherm Gateway", - name=gw_hub.name, - sw_version=gw_hub.gw_version, - ) - self._attr_unique_id = gw_hub.hub_id @callback def update_options(self, entry): """Update climate entity options.""" - self.floor_temp = entry.options[CONF_FLOOR_TEMP] - self.temp_read_precision = entry.options[CONF_READ_PRECISION] - self.temp_set_precision = entry.options[CONF_SET_PRECISION] + self._attr_precision = entry.options[CONF_READ_PRECISION] + self._attr_target_temperature_step = entry.options[CONF_SET_PRECISION] self.temporary_ovrd_mode = entry.options[CONF_TEMPORARY_OVRD_MODE] self.async_write_ha_state() async def async_added_to_hass(self) -> None: """Connect to the OpenTherm Gateway device.""" - _LOGGER.debug("Added OpenTherm Gateway climate device %s", self.friendly_name) - self._unsub_updates = async_dispatcher_connect( - self.hass, self._gateway.update_signal, self.receive_report - ) - self._unsub_options = async_dispatcher_connect( - self.hass, self._gateway.options_update_signal, self.update_options + await super().async_added_to_hass() + self.async_on_remove( + async_dispatcher_connect( + self.hass, self._gateway.options_update_signal, self.update_options + ) ) - async def async_will_remove_from_hass(self) -> None: - """Unsubscribe from updates from the component.""" - _LOGGER.debug("Removing OpenTherm Gateway climate %s", self.friendly_name) - self._unsub_options() - self._unsub_updates() - @callback - def receive_report(self, status): + def receive_report(self, status: dict[OpenThermDataSource, dict]): """Receive and handle a new report from the Gateway.""" - self._attr_available = self._gateway.connected - ch_active = status[gw_vars.BOILER].get(gw_vars.DATA_SLAVE_CH_ACTIVE) - flame_on = status[gw_vars.BOILER].get(gw_vars.DATA_SLAVE_FLAME_ON) - cooling_active = status[gw_vars.BOILER].get(gw_vars.DATA_SLAVE_COOLING_ACTIVE) + ch_active = status[OpenThermDataSource.BOILER].get(gw_vars.DATA_SLAVE_CH_ACTIVE) + flame_on = status[OpenThermDataSource.BOILER].get(gw_vars.DATA_SLAVE_FLAME_ON) + cooling_active = status[OpenThermDataSource.BOILER].get( + gw_vars.DATA_SLAVE_COOLING_ACTIVE + ) if ch_active and flame_on: - self._current_operation = HVACAction.HEATING - self._hvac_mode = HVACMode.HEAT + self._attr_hvac_action = HVACAction.HEATING + self._attr_hvac_mode = HVACMode.HEAT elif cooling_active: - self._current_operation = HVACAction.COOLING - self._hvac_mode = HVACMode.COOL + self._attr_hvac_action = HVACAction.COOLING + self._attr_hvac_mode = HVACMode.COOL else: - self._current_operation = HVACAction.IDLE + self._attr_hvac_action = HVACAction.IDLE - self._current_temperature = status[gw_vars.THERMOSTAT].get( + self._attr_current_temperature = status[OpenThermDataSource.THERMOSTAT].get( gw_vars.DATA_ROOM_TEMP ) - temp_upd = status[gw_vars.THERMOSTAT].get(gw_vars.DATA_ROOM_SETPOINT) + temp_upd = status[OpenThermDataSource.THERMOSTAT].get( + gw_vars.DATA_ROOM_SETPOINT + ) if self._target_temperature != temp_upd: self._new_target_temperature = None @@ -162,82 +151,35 @@ def receive_report(self, status): # GPIO mode 5: 0 == Away # GPIO mode 6: 1 == Away - gpio_a_state = status[gw_vars.OTGW].get(gw_vars.OTGW_GPIO_A) - if gpio_a_state == 5: - self._away_mode_a = 0 - elif gpio_a_state == 6: - self._away_mode_a = 1 - else: - self._away_mode_a = None - gpio_b_state = status[gw_vars.OTGW].get(gw_vars.OTGW_GPIO_B) - if gpio_b_state == 5: - self._away_mode_b = 0 - elif gpio_b_state == 6: - self._away_mode_b = 1 - else: - self._away_mode_b = None - if self._away_mode_a is not None: - self._away_state_a = ( - status[gw_vars.OTGW].get(gw_vars.OTGW_GPIO_A_STATE) == self._away_mode_a + gpio_a_state = status[OpenThermDataSource.GATEWAY].get(gw_vars.OTGW_GPIO_A) + gpio_b_state = status[OpenThermDataSource.GATEWAY].get(gw_vars.OTGW_GPIO_B) + self._away_mode_a = gpio_a_state - 5 if gpio_a_state in (5, 6) else None + self._away_mode_b = gpio_b_state - 5 if gpio_b_state in (5, 6) else None + self._away_state_a = ( + ( + status[OpenThermDataSource.GATEWAY].get(gw_vars.OTGW_GPIO_A_STATE) + == self._away_mode_a ) - if self._away_mode_b is not None: - self._away_state_b = ( - status[gw_vars.OTGW].get(gw_vars.OTGW_GPIO_B_STATE) == self._away_mode_b + if self._away_mode_a is not None + else False + ) + self._away_state_b = ( + ( + status[OpenThermDataSource.GATEWAY].get(gw_vars.OTGW_GPIO_B_STATE) + == self._away_mode_b ) + if self._away_mode_b is not None + else False + ) self.async_write_ha_state() @property - def precision(self): - """Return the precision of the system.""" - if self.temp_read_precision: - return self.temp_read_precision - if self.hass.config.units.temperature_unit == UnitOfTemperature.CELSIUS: - return PRECISION_HALVES - return PRECISION_WHOLE - - @property - def hvac_action(self) -> HVACAction | None: - """Return current HVAC operation.""" - return self._current_operation - - @property - def hvac_mode(self) -> HVACMode: - """Return current HVAC mode.""" - return self._hvac_mode - - def set_hvac_mode(self, hvac_mode: HVACMode) -> None: - """Set the HVAC mode.""" - _LOGGER.warning("Changing HVAC mode is not supported") - - @property - def current_temperature(self): - """Return the current temperature.""" - if self._current_temperature is None: - return None - if self.floor_temp is True: - if self.precision == PRECISION_HALVES: - return int(2 * self._current_temperature) / 2 - if self.precision == PRECISION_TENTHS: - return int(10 * self._current_temperature) / 10 - return int(self._current_temperature) - return self._current_temperature - - @property - def target_temperature(self): + def target_temperature(self) -> float | None: """Return the temperature we try to reach.""" return self._new_target_temperature or self._target_temperature @property - def target_temperature_step(self): - """Return the supported step of target temperature.""" - if self.temp_set_precision: - return self.temp_set_precision - if self.hass.config.units.temperature_unit == UnitOfTemperature.CELSIUS: - return PRECISION_HALVES - return PRECISION_WHOLE - - @property - def preset_mode(self): + def preset_mode(self) -> str: """Return current preset mode.""" if self._away_state_a or self._away_state_b: return PRESET_AWAY diff --git a/homeassistant/components/opentherm_gw/config_flow.py b/homeassistant/components/opentherm_gw/config_flow.py index 19906689b57fbf..3cf8a1c4594ddf 100644 --- a/homeassistant/components/opentherm_gw/config_flow.py +++ b/homeassistant/components/opentherm_gw/config_flow.py @@ -3,13 +3,19 @@ from __future__ import annotations import asyncio +from typing import Any import pyotgw from pyotgw import vars as gw_vars from serial import SerialException import voluptuous as vol -from homeassistant.config_entries import ConfigEntry, ConfigFlow, OptionsFlow +from homeassistant.config_entries import ( + ConfigEntry, + ConfigFlow, + ConfigFlowResult, + OptionsFlow, +) from homeassistant.const import ( CONF_DEVICE, CONF_ID, @@ -28,6 +34,7 @@ CONF_SET_PRECISION, CONF_TEMPORARY_OVRD_MODE, CONNECTION_TIMEOUT, + OpenThermDataSource, ) @@ -44,7 +51,9 @@ def async_get_options_flow( """Get the options flow for this handler.""" return OpenThermGwOptionsFlow(config_entry) - async def async_step_init(self, info=None): + async def async_step_init( + self, info: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle config flow initiation.""" if info: name = info[CONF_NAME] @@ -66,7 +75,7 @@ async def test_connection(): await otgw.disconnect() if not status: raise ConnectionError - return status[gw_vars.OTGW].get(gw_vars.OTGW_ABOUT) + return status[OpenThermDataSource.GATEWAY].get(gw_vars.OTGW_ABOUT) try: async with asyncio.timeout(CONNECTION_TIMEOUT): @@ -80,23 +89,25 @@ async def test_connection(): return self._show_form() - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle manual initiation of the config flow.""" return await self.async_step_init(user_input) - async def async_step_import(self, import_config): + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Import an OpenTherm Gateway device as a config entry. This flow is triggered by `async_setup` for configured devices. """ formatted_config = { - CONF_NAME: import_config.get(CONF_NAME, import_config[CONF_ID]), - CONF_DEVICE: import_config[CONF_DEVICE], - CONF_ID: import_config[CONF_ID], + CONF_NAME: import_data.get(CONF_NAME, import_data[CONF_ID]), + CONF_DEVICE: import_data[CONF_DEVICE], + CONF_ID: import_data[CONF_ID], } return await self.async_step_init(info=formatted_config) - def _show_form(self, errors=None): + def _show_form(self, errors: dict[str, str] | None = None) -> ConfigFlowResult: """Show the config flow form with possible errors.""" return self.async_show_form( step_id="init", @@ -124,7 +135,9 @@ def __init__(self, config_entry: ConfigEntry) -> None: """Initialize the options flow.""" self.config_entry = config_entry - async def async_step_init(self, user_input=None): + async def async_step_init( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Manage the opentherm_gw options.""" if user_input is not None: return self.async_create_entry(title="", data=user_input) diff --git a/homeassistant/components/opentherm_gw/const.py b/homeassistant/components/opentherm_gw/const.py index c1932c7b2bd834..c842ff568ae2cf 100644 --- a/homeassistant/components/opentherm_gw/const.py +++ b/homeassistant/components/opentherm_gw/const.py @@ -1,5 +1,10 @@ """Constants for the opentherm_gw integration.""" +from dataclasses import dataclass +from enum import StrEnum + +from pyotgw import vars as gw_vars + ATTR_GW_ID = "gateway_id" ATTR_LEVEL = "level" ATTR_DHW_OVRD = "dhw_override" @@ -33,3 +38,41 @@ SERVICE_SET_OAT = "set_outside_temperature" SERVICE_SET_SB_TEMP = "set_setback_temperature" SERVICE_SEND_TRANSP_CMD = "send_transparent_command" + + +class OpenThermDataSource(StrEnum): + """List valid OpenTherm data sources.""" + + BOILER = gw_vars.BOILER + GATEWAY = gw_vars.OTGW + THERMOSTAT = gw_vars.THERMOSTAT + + +class OpenThermDeviceIdentifier(StrEnum): + """List valid OpenTherm device identifiers.""" + + BOILER = "boiler" + GATEWAY = "gateway" + THERMOSTAT = "thermostat" + + +@dataclass(frozen=True, kw_only=True) +class OpenThermDeviceDescription: + """Describe OpenTherm device properties.""" + + data_source: OpenThermDataSource + device_identifier: OpenThermDeviceIdentifier + + +BOILER_DEVICE_DESCRIPTION = OpenThermDeviceDescription( + data_source=OpenThermDataSource.BOILER, + device_identifier=OpenThermDeviceIdentifier.BOILER, +) +GATEWAY_DEVICE_DESCRIPTION = OpenThermDeviceDescription( + data_source=OpenThermDataSource.GATEWAY, + device_identifier=OpenThermDeviceIdentifier.GATEWAY, +) +THERMOSTAT_DEVICE_DESCRIPTION = OpenThermDeviceDescription( + data_source=OpenThermDataSource.THERMOSTAT, + device_identifier=OpenThermDeviceIdentifier.THERMOSTAT, +) diff --git a/homeassistant/components/opentherm_gw/entity.py b/homeassistant/components/opentherm_gw/entity.py index a1035b946c2e48..e87a6c182aa131 100644 --- a/homeassistant/components/opentherm_gw/entity.py +++ b/homeassistant/components/opentherm_gw/entity.py @@ -10,7 +10,7 @@ from homeassistant.helpers.entity import Entity, EntityDescription from . import OpenThermGatewayHub -from .const import DOMAIN +from .const import DOMAIN, OpenThermDataSource, OpenThermDeviceDescription _LOGGER = logging.getLogger(__name__) @@ -24,45 +24,45 @@ class OpenThermEntityDescription(EntityDescription): """Describe common opentherm_gw entity properties.""" - friendly_name_format: str + device_description: OpenThermDeviceDescription class OpenThermEntity(Entity): - """Represent an OpenTherm Gateway entity.""" + """Represent an OpenTherm entity.""" + _attr_has_entity_name = True _attr_should_poll = False - _attr_entity_registry_enabled_default = False - _attr_available = False entity_description: OpenThermEntityDescription def __init__( self, gw_hub: OpenThermGatewayHub, - source: str, description: OpenThermEntityDescription, ) -> None: """Initialize the entity.""" self.entity_description = description self._gateway = gw_hub - self._source = source - friendly_name_format = ( - f"{description.friendly_name_format} ({TRANSLATE_SOURCE[source]})" - if TRANSLATE_SOURCE[source] is not None - else description.friendly_name_format - ) - self._attr_name = friendly_name_format.format(gw_hub.name) - self._attr_unique_id = f"{gw_hub.hub_id}-{source}-{description.key}" + self._attr_unique_id = f"{gw_hub.hub_id}-{description.device_description.device_identifier}-{description.key}" self._attr_device_info = DeviceInfo( - identifiers={(DOMAIN, gw_hub.hub_id)}, - manufacturer="Schelte Bron", - model="OpenTherm Gateway", - name=gw_hub.name, - sw_version=gw_hub.gw_version, + identifiers={ + ( + DOMAIN, + f"{gw_hub.hub_id}-{description.device_description.device_identifier}", + ) + }, ) + @property + def available(self) -> bool: + """Return connection status of the hub to indicate availability.""" + return self._gateway.connected + + +class OpenThermStatusEntity(OpenThermEntity): + """Represent an OpenTherm entity that receives status updates.""" + async def async_added_to_hass(self) -> None: """Subscribe to updates from the component.""" - _LOGGER.debug("Added OpenTherm Gateway entity %s", self._attr_name) self.async_on_remove( async_dispatcher_connect( self.hass, self._gateway.update_signal, self.receive_report @@ -70,7 +70,7 @@ async def async_added_to_hass(self) -> None: ) @callback - def receive_report(self, status: dict[str, dict]) -> None: + def receive_report(self, status: dict[OpenThermDataSource, dict]) -> None: """Handle status updates from the component.""" # Must be implemented at the platform level. raise NotImplementedError diff --git a/homeassistant/components/opentherm_gw/icons.json b/homeassistant/components/opentherm_gw/icons.json index 13dbe0a70a1792..37942aa0e63d0a 100644 --- a/homeassistant/components/opentherm_gw/icons.json +++ b/homeassistant/components/opentherm_gw/icons.json @@ -1,16 +1,40 @@ { "services": { - "reset_gateway": "mdi:reload", - "set_central_heating_ovrd": "mdi:heat-wave", - "set_clock": "mdi:clock", - "set_control_setpoint": "mdi:thermometer-lines", - "set_hot_water_ovrd": "mdi:thermometer-lines", - "set_hot_water_setpoint": "mdi:thermometer-lines", - "set_gpio_mode": "mdi:cable-data", - "set_led_mode": "mdi:led-on", - "set_max_modulation": "mdi:thermometer-lines", - "set_outside_temperature": "mdi:thermometer-lines", - "set_setback_temperature": "mdi:thermometer-lines", - "send_transparent_command": "mdi:console" + "reset_gateway": { + "service": "mdi:reload" + }, + "set_central_heating_ovrd": { + "service": "mdi:heat-wave" + }, + "set_clock": { + "service": "mdi:clock" + }, + "set_control_setpoint": { + "service": "mdi:thermometer-lines" + }, + "set_hot_water_ovrd": { + "service": "mdi:thermometer-lines" + }, + "set_hot_water_setpoint": { + "service": "mdi:thermometer-lines" + }, + "set_gpio_mode": { + "service": "mdi:cable-data" + }, + "set_led_mode": { + "service": "mdi:led-on" + }, + "set_max_modulation": { + "service": "mdi:thermometer-lines" + }, + "set_outside_temperature": { + "service": "mdi:thermometer-lines" + }, + "set_setback_temperature": { + "service": "mdi:thermometer-lines" + }, + "send_transparent_command": { + "service": "mdi:console" + } } } diff --git a/homeassistant/components/opentherm_gw/sensor.py b/homeassistant/components/opentherm_gw/sensor.py index fb30b2ce35cde1..5ccb4166665da4 100644 --- a/homeassistant/components/opentherm_gw/sensor.py +++ b/homeassistant/components/opentherm_gw/sensor.py @@ -5,7 +5,6 @@ import pyotgw.vars as gw_vars from homeassistant.components.sensor import ( - ENTITY_ID_FORMAT, SensorDeviceClass, SensorEntity, SensorEntityDescription, @@ -15,6 +14,7 @@ from homeassistant.const import ( CONF_ID, PERCENTAGE, + EntityCategory, UnitOfPower, UnitOfPressure, UnitOfTemperature, @@ -22,12 +22,17 @@ UnitOfVolumeFlowRate, ) from homeassistant.core import HomeAssistant, callback -from homeassistant.helpers.entity import async_generate_entity_id from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import OpenThermGatewayHub -from .const import DATA_GATEWAYS, DATA_OPENTHERM_GW -from .entity import OpenThermEntity, OpenThermEntityDescription +from .const import ( + BOILER_DEVICE_DESCRIPTION, + DATA_GATEWAYS, + DATA_OPENTHERM_GW, + GATEWAY_DEVICE_DESCRIPTION, + THERMOSTAT_DEVICE_DESCRIPTION, + OpenThermDataSource, +) +from .entity import OpenThermEntityDescription, OpenThermStatusEntity SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION = 1 @@ -36,584 +41,833 @@ class OpenThermSensorEntityDescription( SensorEntityDescription, OpenThermEntityDescription ): - """Describes opentherm_gw sensor entity.""" + """Describes an opentherm_gw sensor entity.""" -SENSOR_INFO: tuple[tuple[list[str], OpenThermSensorEntityDescription], ...] = ( - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_CONTROL_SETPOINT, - friendly_name_format="Control Setpoint {}", - device_class=SensorDeviceClass.TEMPERATURE, - state_class=SensorStateClass.MEASUREMENT, - native_unit_of_measurement=UnitOfTemperature.CELSIUS, - suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_MASTER_MEMBERID, - friendly_name_format="Thermostat Member ID {}", - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_SLAVE_MEMBERID, - friendly_name_format="Boiler Member ID {}", - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_SLAVE_OEM_FAULT, - friendly_name_format="Boiler OEM Fault Code {}", - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_COOLING_CONTROL, - friendly_name_format="Cooling Control Signal {}", - state_class=SensorStateClass.MEASUREMENT, - native_unit_of_measurement=PERCENTAGE, - suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_CONTROL_SETPOINT_2, - friendly_name_format="Control Setpoint 2 {}", - device_class=SensorDeviceClass.TEMPERATURE, - state_class=SensorStateClass.MEASUREMENT, - native_unit_of_measurement=UnitOfTemperature.CELSIUS, - suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_ROOM_SETPOINT_OVRD, - friendly_name_format="Room Setpoint Override {}", - device_class=SensorDeviceClass.TEMPERATURE, - state_class=SensorStateClass.MEASUREMENT, - native_unit_of_measurement=UnitOfTemperature.CELSIUS, - suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_SLAVE_MAX_RELATIVE_MOD, - friendly_name_format="Boiler Maximum Relative Modulation {}", - state_class=SensorStateClass.MEASUREMENT, - native_unit_of_measurement=PERCENTAGE, - suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_SLAVE_MAX_CAPACITY, - friendly_name_format="Boiler Maximum Capacity {}", - state_class=SensorStateClass.MEASUREMENT, - device_class=SensorDeviceClass.POWER, - native_unit_of_measurement=UnitOfPower.KILO_WATT, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_SLAVE_MIN_MOD_LEVEL, - friendly_name_format="Boiler Minimum Modulation Level {}", - state_class=SensorStateClass.MEASUREMENT, - native_unit_of_measurement=PERCENTAGE, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_ROOM_SETPOINT, - friendly_name_format="Room Setpoint {}", - device_class=SensorDeviceClass.TEMPERATURE, - state_class=SensorStateClass.MEASUREMENT, - native_unit_of_measurement=UnitOfTemperature.CELSIUS, - suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_REL_MOD_LEVEL, - friendly_name_format="Relative Modulation Level {}", - state_class=SensorStateClass.MEASUREMENT, - native_unit_of_measurement=PERCENTAGE, - suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_CH_WATER_PRESS, - friendly_name_format="Central Heating Water Pressure {}", - device_class=SensorDeviceClass.PRESSURE, - state_class=SensorStateClass.MEASUREMENT, - native_unit_of_measurement=UnitOfPressure.BAR, - suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_DHW_FLOW_RATE, - friendly_name_format="Hot Water Flow Rate {}", - device_class=SensorDeviceClass.VOLUME_FLOW_RATE, - state_class=SensorStateClass.MEASUREMENT, - native_unit_of_measurement=UnitOfVolumeFlowRate.LITERS_PER_MINUTE, - suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_ROOM_SETPOINT_2, - friendly_name_format="Room Setpoint 2 {}", - device_class=SensorDeviceClass.TEMPERATURE, - state_class=SensorStateClass.MEASUREMENT, - native_unit_of_measurement=UnitOfTemperature.CELSIUS, - suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_ROOM_TEMP, - friendly_name_format="Room Temperature {}", - device_class=SensorDeviceClass.TEMPERATURE, - state_class=SensorStateClass.MEASUREMENT, - native_unit_of_measurement=UnitOfTemperature.CELSIUS, - suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_CH_WATER_TEMP, - friendly_name_format="Central Heating Water Temperature {}", - device_class=SensorDeviceClass.TEMPERATURE, - state_class=SensorStateClass.MEASUREMENT, - native_unit_of_measurement=UnitOfTemperature.CELSIUS, - suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_DHW_TEMP, - friendly_name_format="Hot Water Temperature {}", - device_class=SensorDeviceClass.TEMPERATURE, - state_class=SensorStateClass.MEASUREMENT, - native_unit_of_measurement=UnitOfTemperature.CELSIUS, - suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_OUTSIDE_TEMP, - friendly_name_format="Outside Temperature {}", - device_class=SensorDeviceClass.TEMPERATURE, - state_class=SensorStateClass.MEASUREMENT, - native_unit_of_measurement=UnitOfTemperature.CELSIUS, - suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_RETURN_WATER_TEMP, - friendly_name_format="Return Water Temperature {}", - device_class=SensorDeviceClass.TEMPERATURE, - state_class=SensorStateClass.MEASUREMENT, - native_unit_of_measurement=UnitOfTemperature.CELSIUS, - suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_SOLAR_STORAGE_TEMP, - friendly_name_format="Solar Storage Temperature {}", - device_class=SensorDeviceClass.TEMPERATURE, - state_class=SensorStateClass.MEASUREMENT, - native_unit_of_measurement=UnitOfTemperature.CELSIUS, - suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_SOLAR_COLL_TEMP, - friendly_name_format="Solar Collector Temperature {}", - device_class=SensorDeviceClass.TEMPERATURE, - state_class=SensorStateClass.MEASUREMENT, - native_unit_of_measurement=UnitOfTemperature.CELSIUS, - suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_CH_WATER_TEMP_2, - friendly_name_format="Central Heating 2 Water Temperature {}", - device_class=SensorDeviceClass.TEMPERATURE, - state_class=SensorStateClass.MEASUREMENT, - native_unit_of_measurement=UnitOfTemperature.CELSIUS, - suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_DHW_TEMP_2, - friendly_name_format="Hot Water 2 Temperature {}", - device_class=SensorDeviceClass.TEMPERATURE, - state_class=SensorStateClass.MEASUREMENT, - native_unit_of_measurement=UnitOfTemperature.CELSIUS, - suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_EXHAUST_TEMP, - friendly_name_format="Exhaust Temperature {}", - device_class=SensorDeviceClass.TEMPERATURE, - state_class=SensorStateClass.MEASUREMENT, - native_unit_of_measurement=UnitOfTemperature.CELSIUS, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_SLAVE_DHW_MAX_SETP, - friendly_name_format="Hot Water Maximum Setpoint {}", - device_class=SensorDeviceClass.TEMPERATURE, - state_class=SensorStateClass.MEASUREMENT, - native_unit_of_measurement=UnitOfTemperature.CELSIUS, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_SLAVE_DHW_MIN_SETP, - friendly_name_format="Hot Water Minimum Setpoint {}", - device_class=SensorDeviceClass.TEMPERATURE, - state_class=SensorStateClass.MEASUREMENT, - native_unit_of_measurement=UnitOfTemperature.CELSIUS, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_SLAVE_CH_MAX_SETP, - friendly_name_format="Boiler Maximum Central Heating Setpoint {}", - device_class=SensorDeviceClass.TEMPERATURE, - state_class=SensorStateClass.MEASUREMENT, - native_unit_of_measurement=UnitOfTemperature.CELSIUS, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_SLAVE_CH_MIN_SETP, - friendly_name_format="Boiler Minimum Central Heating Setpoint {}", - device_class=SensorDeviceClass.TEMPERATURE, - state_class=SensorStateClass.MEASUREMENT, - native_unit_of_measurement=UnitOfTemperature.CELSIUS, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_DHW_SETPOINT, - friendly_name_format="Hot Water Setpoint {}", - device_class=SensorDeviceClass.TEMPERATURE, - state_class=SensorStateClass.MEASUREMENT, - native_unit_of_measurement=UnitOfTemperature.CELSIUS, - suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_MAX_CH_SETPOINT, - friendly_name_format="Maximum Central Heating Setpoint {}", - device_class=SensorDeviceClass.TEMPERATURE, - state_class=SensorStateClass.MEASUREMENT, - native_unit_of_measurement=UnitOfTemperature.CELSIUS, - suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_OEM_DIAG, - friendly_name_format="OEM Diagnostic Code {}", - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_TOTAL_BURNER_STARTS, - friendly_name_format="Total Burner Starts {}", - state_class=SensorStateClass.TOTAL, - native_unit_of_measurement="starts", - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_CH_PUMP_STARTS, - friendly_name_format="Central Heating Pump Starts {}", - state_class=SensorStateClass.TOTAL, - native_unit_of_measurement="starts", - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_DHW_PUMP_STARTS, - friendly_name_format="Hot Water Pump Starts {}", - state_class=SensorStateClass.TOTAL, - native_unit_of_measurement="starts", - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_DHW_BURNER_STARTS, - friendly_name_format="Hot Water Burner Starts {}", - state_class=SensorStateClass.TOTAL, - native_unit_of_measurement="starts", - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_TOTAL_BURNER_HOURS, - friendly_name_format="Total Burner Hours {}", - device_class=SensorDeviceClass.DURATION, - state_class=SensorStateClass.TOTAL, - native_unit_of_measurement=UnitOfTime.HOURS, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_CH_PUMP_HOURS, - friendly_name_format="Central Heating Pump Hours {}", - device_class=SensorDeviceClass.DURATION, - state_class=SensorStateClass.TOTAL, - native_unit_of_measurement=UnitOfTime.HOURS, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_DHW_PUMP_HOURS, - friendly_name_format="Hot Water Pump Hours {}", - device_class=SensorDeviceClass.DURATION, - state_class=SensorStateClass.TOTAL, - native_unit_of_measurement=UnitOfTime.HOURS, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_DHW_BURNER_HOURS, - friendly_name_format="Hot Water Burner Hours {}", - device_class=SensorDeviceClass.DURATION, - state_class=SensorStateClass.TOTAL, - native_unit_of_measurement=UnitOfTime.HOURS, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_MASTER_OT_VERSION, - friendly_name_format="Thermostat OpenTherm Version {}", - suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_SLAVE_OT_VERSION, - friendly_name_format="Boiler OpenTherm Version {}", - suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_MASTER_PRODUCT_TYPE, - friendly_name_format="Thermostat Product Type {}", - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_MASTER_PRODUCT_VERSION, - friendly_name_format="Thermostat Product Version {}", - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_SLAVE_PRODUCT_TYPE, - friendly_name_format="Boiler Product Type {}", - ), - ), - ( - [gw_vars.BOILER, gw_vars.THERMOSTAT], - OpenThermSensorEntityDescription( - key=gw_vars.DATA_SLAVE_PRODUCT_VERSION, - friendly_name_format="Boiler Product Version {}", - ), - ), - ( - [gw_vars.OTGW], - OpenThermSensorEntityDescription( - key=gw_vars.OTGW_MODE, - friendly_name_format="Gateway/Monitor Mode {}", - ), - ), - ( - [gw_vars.OTGW], - OpenThermSensorEntityDescription( - key=gw_vars.OTGW_DHW_OVRD, - friendly_name_format="Gateway Hot Water Override Mode {}", - ), - ), - ( - [gw_vars.OTGW], - OpenThermSensorEntityDescription( - key=gw_vars.OTGW_ABOUT, - friendly_name_format="Gateway Firmware Version {}", - ), - ), - ( - [gw_vars.OTGW], - OpenThermSensorEntityDescription( - key=gw_vars.OTGW_BUILD, - friendly_name_format="Gateway Firmware Build {}", - ), - ), - ( - [gw_vars.OTGW], - OpenThermSensorEntityDescription( - key=gw_vars.OTGW_CLOCKMHZ, - friendly_name_format="Gateway Clock Speed {}", - ), - ), - ( - [gw_vars.OTGW], - OpenThermSensorEntityDescription( - key=gw_vars.OTGW_LED_A, - friendly_name_format="Gateway LED A Mode {}", - ), - ), - ( - [gw_vars.OTGW], - OpenThermSensorEntityDescription( - key=gw_vars.OTGW_LED_B, - friendly_name_format="Gateway LED B Mode {}", - ), - ), - ( - [gw_vars.OTGW], - OpenThermSensorEntityDescription( - key=gw_vars.OTGW_LED_C, - friendly_name_format="Gateway LED C Mode {}", - ), - ), - ( - [gw_vars.OTGW], - OpenThermSensorEntityDescription( - key=gw_vars.OTGW_LED_D, - friendly_name_format="Gateway LED D Mode {}", - ), - ), - ( - [gw_vars.OTGW], - OpenThermSensorEntityDescription( - key=gw_vars.OTGW_LED_E, - friendly_name_format="Gateway LED E Mode {}", - ), - ), - ( - [gw_vars.OTGW], - OpenThermSensorEntityDescription( - key=gw_vars.OTGW_LED_F, - friendly_name_format="Gateway LED F Mode {}", - ), - ), - ( - [gw_vars.OTGW], - OpenThermSensorEntityDescription( - key=gw_vars.OTGW_GPIO_A, - friendly_name_format="Gateway GPIO A Mode {}", - ), - ), - ( - [gw_vars.OTGW], - OpenThermSensorEntityDescription( - key=gw_vars.OTGW_GPIO_B, - friendly_name_format="Gateway GPIO B Mode {}", - ), - ), - ( - [gw_vars.OTGW], - OpenThermSensorEntityDescription( - key=gw_vars.OTGW_SB_TEMP, - friendly_name_format="Gateway Setback Temperature {}", - device_class=SensorDeviceClass.TEMPERATURE, - state_class=SensorStateClass.MEASUREMENT, - native_unit_of_measurement=UnitOfTemperature.CELSIUS, - suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, - ), - ), - ( - [gw_vars.OTGW], - OpenThermSensorEntityDescription( - key=gw_vars.OTGW_SETP_OVRD_MODE, - friendly_name_format="Gateway Room Setpoint Override Mode {}", - ), - ), - ( - [gw_vars.OTGW], - OpenThermSensorEntityDescription( - key=gw_vars.OTGW_SMART_PWR, - friendly_name_format="Gateway Smart Power Mode {}", - ), - ), - ( - [gw_vars.OTGW], - OpenThermSensorEntityDescription( - key=gw_vars.OTGW_THRM_DETECT, - friendly_name_format="Gateway Thermostat Detection {}", - ), - ), - ( - [gw_vars.OTGW], - OpenThermSensorEntityDescription( - key=gw_vars.OTGW_VREF, - friendly_name_format="Gateway Reference Voltage Setting {}", - ), +SENSOR_DESCRIPTIONS: tuple[OpenThermSensorEntityDescription, ...] = ( + OpenThermSensorEntityDescription( + key=gw_vars.DATA_CONTROL_SETPOINT, + translation_key="control_setpoint_n", + translation_placeholders={"circuit_number": "1"}, + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_CONTROL_SETPOINT_2, + translation_key="control_setpoint_n", + translation_placeholders={"circuit_number": "2"}, + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_SLAVE_MEMBERID, + translation_key="manufacturer_id", + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_SLAVE_OEM_FAULT, + translation_key="oem_fault_code", + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_COOLING_CONTROL, + translation_key="cooling_control", + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=PERCENTAGE, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_SLAVE_MAX_RELATIVE_MOD, + translation_key="max_relative_mod_level", + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=PERCENTAGE, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_SLAVE_MAX_CAPACITY, + translation_key="max_capacity", + state_class=SensorStateClass.MEASUREMENT, + device_class=SensorDeviceClass.POWER, + native_unit_of_measurement=UnitOfPower.KILO_WATT, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_SLAVE_MIN_MOD_LEVEL, + translation_key="min_mod_level", + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=PERCENTAGE, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_REL_MOD_LEVEL, + translation_key="relative_mod_level", + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=PERCENTAGE, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_CH_WATER_PRESS, + translation_key="central_heating_pressure", + device_class=SensorDeviceClass.PRESSURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfPressure.BAR, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_DHW_FLOW_RATE, + translation_key="hot_water_flow_rate", + device_class=SensorDeviceClass.VOLUME_FLOW_RATE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfVolumeFlowRate.LITERS_PER_MINUTE, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_CH_WATER_TEMP, + translation_key="central_heating_temperature_n", + translation_placeholders={"circuit_number": "1"}, + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_CH_WATER_TEMP_2, + translation_key="central_heating_temperature_n", + translation_placeholders={"circuit_number": "2"}, + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_DHW_TEMP, + translation_key="hot_water_temperature_n", + translation_placeholders={"circuit_number": "1"}, + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_DHW_TEMP_2, + translation_key="hot_water_temperature_n", + translation_placeholders={"circuit_number": "2"}, + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_RETURN_WATER_TEMP, + translation_key="return_water_temperature", + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_SOLAR_STORAGE_TEMP, + translation_key="solar_storage_temperature", + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_SOLAR_COLL_TEMP, + translation_key="solar_collector_temperature", + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_EXHAUST_TEMP, + translation_key="exhaust_temperature", + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_SLAVE_DHW_MAX_SETP, + translation_key="max_hot_water_setpoint_upper", + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_SLAVE_DHW_MIN_SETP, + translation_key="max_hot_water_setpoint_lower", + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_SLAVE_CH_MAX_SETP, + translation_key="max_central_heating_setpoint_upper", + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_SLAVE_CH_MIN_SETP, + translation_key="max_central_heating_setpoint_lower", + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_DHW_SETPOINT, + translation_key="hot_water_setpoint", + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_MAX_CH_SETPOINT, + translation_key="max_central_heating_setpoint", + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_OEM_DIAG, + translation_key="oem_diagnostic_code", + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_TOTAL_BURNER_STARTS, + translation_key="total_burner_starts", + state_class=SensorStateClass.TOTAL, + native_unit_of_measurement="starts", + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_CH_PUMP_STARTS, + translation_key="central_heating_pump_starts", + state_class=SensorStateClass.TOTAL, + native_unit_of_measurement="starts", + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_DHW_PUMP_STARTS, + translation_key="hot_water_pump_starts", + state_class=SensorStateClass.TOTAL, + native_unit_of_measurement="starts", + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_DHW_BURNER_STARTS, + translation_key="hot_water_burner_starts", + state_class=SensorStateClass.TOTAL, + native_unit_of_measurement="starts", + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_TOTAL_BURNER_HOURS, + translation_key="total_burner_hours", + device_class=SensorDeviceClass.DURATION, + state_class=SensorStateClass.TOTAL, + native_unit_of_measurement=UnitOfTime.HOURS, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_CH_PUMP_HOURS, + translation_key="central_heating_pump_hours", + device_class=SensorDeviceClass.DURATION, + state_class=SensorStateClass.TOTAL, + native_unit_of_measurement=UnitOfTime.HOURS, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_DHW_PUMP_HOURS, + translation_key="hot_water_pump_hours", + device_class=SensorDeviceClass.DURATION, + state_class=SensorStateClass.TOTAL, + native_unit_of_measurement=UnitOfTime.HOURS, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_DHW_BURNER_HOURS, + translation_key="hot_water_burner_hours", + device_class=SensorDeviceClass.DURATION, + state_class=SensorStateClass.TOTAL, + native_unit_of_measurement=UnitOfTime.HOURS, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_SLAVE_OT_VERSION, + translation_key="opentherm_version", + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_SLAVE_PRODUCT_TYPE, + translation_key="product_type", + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_SLAVE_PRODUCT_VERSION, + translation_key="product_version", + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.OTGW_MODE, + translation_key="operating_mode", + device_description=GATEWAY_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.OTGW_DHW_OVRD, + translation_key="hot_water_override_mode", + device_description=GATEWAY_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.OTGW_ABOUT, + translation_key="firmware_version", + device_description=GATEWAY_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.OTGW_BUILD, + translation_key="firmware_build", + device_description=GATEWAY_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.OTGW_CLOCKMHZ, + translation_key="clock_speed", + device_description=GATEWAY_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.OTGW_LED_A, + translation_key="led_mode_n", + translation_placeholders={"led_id": "A"}, + device_description=GATEWAY_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.OTGW_LED_B, + translation_key="led_mode_n", + translation_placeholders={"led_id": "B"}, + device_description=GATEWAY_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.OTGW_LED_C, + translation_key="led_mode_n", + translation_placeholders={"led_id": "C"}, + device_description=GATEWAY_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.OTGW_LED_D, + translation_key="led_mode_n", + translation_placeholders={"led_id": "D"}, + device_description=GATEWAY_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.OTGW_LED_E, + translation_key="led_mode_n", + translation_placeholders={"led_id": "E"}, + device_description=GATEWAY_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.OTGW_LED_F, + translation_key="led_mode_n", + translation_placeholders={"led_id": "F"}, + device_description=GATEWAY_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.OTGW_GPIO_A, + translation_key="gpio_mode_n", + translation_placeholders={"gpio_id": "A"}, + device_description=GATEWAY_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.OTGW_GPIO_B, + translation_key="gpio_mode_n", + translation_placeholders={"gpio_id": "B"}, + device_description=GATEWAY_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.OTGW_SB_TEMP, + translation_key="setback_temperature", + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=GATEWAY_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.OTGW_SETP_OVRD_MODE, + translation_key="room_setpoint_override_mode", + device_description=GATEWAY_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.OTGW_SMART_PWR, + translation_key="smart_power_mode", + device_description=GATEWAY_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.OTGW_THRM_DETECT, + translation_key="thermostat_detection_mode", + device_description=GATEWAY_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.OTGW_VREF, + translation_key="reference_voltage", + device_description=GATEWAY_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_MASTER_MEMBERID, + translation_key="manufacturer_id", + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_ROOM_SETPOINT_OVRD, + translation_key="room_setpoint_override", + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_ROOM_SETPOINT, + translation_key="room_setpoint_n", + translation_placeholders={"setpoint_id": "1"}, + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_ROOM_SETPOINT_2, + translation_key="room_setpoint_n", + translation_placeholders={"setpoint_id": "2"}, + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_ROOM_TEMP, + translation_key="room_temperature", + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_OUTSIDE_TEMP, + translation_key="outside_temperature", + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_MASTER_OT_VERSION, + translation_key="opentherm_version", + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_MASTER_PRODUCT_TYPE, + translation_key="product_type", + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_MASTER_PRODUCT_VERSION, + translation_key="product_version", + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_CONTROL_SETPOINT, + translation_key="control_setpoint_n", + translation_placeholders={"circuit_number": "1"}, + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_CONTROL_SETPOINT_2, + translation_key="control_setpoint_n", + translation_placeholders={"circuit_number": "2"}, + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_SLAVE_MEMBERID, + translation_key="manufacturer_id", + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_SLAVE_OEM_FAULT, + translation_key="oem_fault_code", + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_COOLING_CONTROL, + translation_key="cooling_control", + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=PERCENTAGE, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_SLAVE_MAX_RELATIVE_MOD, + translation_key="max_relative_mod_level", + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=PERCENTAGE, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_SLAVE_MAX_CAPACITY, + translation_key="max_capacity", + state_class=SensorStateClass.MEASUREMENT, + device_class=SensorDeviceClass.POWER, + native_unit_of_measurement=UnitOfPower.KILO_WATT, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_SLAVE_MIN_MOD_LEVEL, + translation_key="min_mod_level", + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=PERCENTAGE, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_REL_MOD_LEVEL, + translation_key="relative_mod_level", + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=PERCENTAGE, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_CH_WATER_PRESS, + translation_key="central_heating_pressure", + device_class=SensorDeviceClass.PRESSURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfPressure.BAR, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_DHW_FLOW_RATE, + translation_key="hot_water_flow_rate", + device_class=SensorDeviceClass.VOLUME_FLOW_RATE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfVolumeFlowRate.LITERS_PER_MINUTE, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_CH_WATER_TEMP, + translation_key="central_heating_temperature_n", + translation_placeholders={"circuit_number": "1"}, + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_CH_WATER_TEMP_2, + translation_key="central_heating_temperature_n", + translation_placeholders={"circuit_number": "2"}, + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_DHW_TEMP, + translation_key="hot_water_temperature_n", + translation_placeholders={"circuit_number": "1"}, + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_DHW_TEMP_2, + translation_key="hot_water_temperature_n", + translation_placeholders={"circuit_number": "2"}, + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_RETURN_WATER_TEMP, + translation_key="return_water_temperature", + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_SOLAR_STORAGE_TEMP, + translation_key="solar_storage_temperature", + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_SOLAR_COLL_TEMP, + translation_key="solar_collector_temperature", + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_EXHAUST_TEMP, + translation_key="exhaust_temperature", + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_SLAVE_DHW_MAX_SETP, + translation_key="max_hot_water_setpoint_upper", + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_SLAVE_DHW_MIN_SETP, + translation_key="max_hot_water_setpoint_lower", + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_SLAVE_CH_MAX_SETP, + translation_key="max_central_heating_setpoint_upper", + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_SLAVE_CH_MIN_SETP, + translation_key="max_central_heating_setpoint_lower", + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_DHW_SETPOINT, + translation_key="hot_water_setpoint", + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_MAX_CH_SETPOINT, + translation_key="max_central_heating_setpoint", + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_OEM_DIAG, + translation_key="oem_diagnostic_code", + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_TOTAL_BURNER_STARTS, + translation_key="total_burner_starts", + state_class=SensorStateClass.TOTAL, + native_unit_of_measurement="starts", + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_CH_PUMP_STARTS, + translation_key="central_heating_pump_starts", + state_class=SensorStateClass.TOTAL, + native_unit_of_measurement="starts", + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_DHW_PUMP_STARTS, + translation_key="hot_water_pump_starts", + state_class=SensorStateClass.TOTAL, + native_unit_of_measurement="starts", + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_DHW_BURNER_STARTS, + translation_key="hot_water_burner_starts", + state_class=SensorStateClass.TOTAL, + native_unit_of_measurement="starts", + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_TOTAL_BURNER_HOURS, + translation_key="total_burner_hours", + device_class=SensorDeviceClass.DURATION, + state_class=SensorStateClass.TOTAL, + native_unit_of_measurement=UnitOfTime.HOURS, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_CH_PUMP_HOURS, + translation_key="central_heating_pump_hours", + device_class=SensorDeviceClass.DURATION, + state_class=SensorStateClass.TOTAL, + native_unit_of_measurement=UnitOfTime.HOURS, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_DHW_PUMP_HOURS, + translation_key="hot_water_pump_hours", + device_class=SensorDeviceClass.DURATION, + state_class=SensorStateClass.TOTAL, + native_unit_of_measurement=UnitOfTime.HOURS, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_DHW_BURNER_HOURS, + translation_key="hot_water_burner_hours", + device_class=SensorDeviceClass.DURATION, + state_class=SensorStateClass.TOTAL, + native_unit_of_measurement=UnitOfTime.HOURS, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_SLAVE_OT_VERSION, + translation_key="opentherm_version", + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_SLAVE_PRODUCT_TYPE, + translation_key="product_type", + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_SLAVE_PRODUCT_VERSION, + translation_key="product_version", + device_description=THERMOSTAT_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_MASTER_MEMBERID, + translation_key="manufacturer_id", + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_ROOM_SETPOINT_OVRD, + translation_key="room_setpoint_override", + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_ROOM_SETPOINT, + translation_key="room_setpoint_n", + translation_placeholders={"setpoint_id": "1"}, + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_ROOM_SETPOINT_2, + translation_key="room_setpoint_n", + translation_placeholders={"setpoint_id": "2"}, + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_ROOM_TEMP, + translation_key="room_temperature", + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_OUTSIDE_TEMP, + translation_key="outside_temperature", + device_class=SensorDeviceClass.TEMPERATURE, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_MASTER_OT_VERSION, + translation_key="opentherm_version", + suggested_display_precision=SENSOR_FLOAT_SUGGESTED_DISPLAY_PRECISION, + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_MASTER_PRODUCT_TYPE, + translation_key="product_type", + device_description=BOILER_DEVICE_DESCRIPTION, + ), + OpenThermSensorEntityDescription( + key=gw_vars.DATA_MASTER_PRODUCT_VERSION, + translation_key="product_version", + device_description=BOILER_DEVICE_DESCRIPTION, ), ) @@ -629,37 +883,22 @@ async def async_setup_entry( async_add_entities( OpenThermSensor( gw_hub, - source, description, ) - for sources, description in SENSOR_INFO - for source in sources + for description in SENSOR_DESCRIPTIONS ) -class OpenThermSensor(OpenThermEntity, SensorEntity): - """Representation of an OpenTherm Gateway sensor.""" +class OpenThermSensor(OpenThermStatusEntity, SensorEntity): + """Representation of an OpenTherm sensor.""" + _attr_entity_category = EntityCategory.DIAGNOSTIC entity_description: OpenThermSensorEntityDescription - def __init__( - self, - gw_hub: OpenThermGatewayHub, - source: str, - description: OpenThermSensorEntityDescription, - ) -> None: - """Initialize the OpenTherm Gateway sensor.""" - self.entity_id = async_generate_entity_id( - ENTITY_ID_FORMAT, - f"{description.key}_{source}_{gw_hub.hub_id}", - hass=gw_hub.hass, - ) - super().__init__(gw_hub, source, description) - @callback - def receive_report(self, status: dict[str, dict]) -> None: + def receive_report(self, status: dict[OpenThermDataSource, dict]) -> None: """Handle status updates from the component.""" - self._attr_available = self._gateway.connected - value = status[self._source].get(self.entity_description.key) - self._attr_native_value = value + self._attr_native_value = status[ + self.entity_description.device_description.data_source + ].get(self.entity_description.key) self.async_write_ha_state() diff --git a/homeassistant/components/opentherm_gw/strings.json b/homeassistant/components/opentherm_gw/strings.json index 9eb97539df98c0..006ccd1909bfac 100644 --- a/homeassistant/components/opentherm_gw/strings.json +++ b/homeassistant/components/opentherm_gw/strings.json @@ -1,4 +1,8 @@ { + "common": { + "state_not_supported": "Not supported", + "state_supported": "Supported" + }, "config": { "step": { "init": { @@ -16,6 +20,297 @@ "timeout_connect": "[%key:common::config_flow::error::timeout_connect%]" } }, + "device": { + "boiler_device": { + "name": "OpenTherm Boiler" + }, + "gateway_device": { + "name": "OpenTherm Gateway" + }, + "thermostat_device": { + "name": "OpenTherm Thermostat" + } + }, + "entity": { + "binary_sensor": { + "fault_indication": { + "name": "Fault indication" + }, + "central_heating_n": { + "name": "Central heating {circuit_number}" + }, + "cooling": { + "name": "Cooling" + }, + "flame": { + "name": "Flame" + }, + "hot_water": { + "name": "Hot water" + }, + "diagnostic_indication": { + "name": "Diagnostic indication" + }, + "supports_hot_water": { + "name": "Hot water support", + "state": { + "off": "[%key:component::opentherm_gw::common::state_not_supported%]", + "on": "[%key:component::opentherm_gw::common::state_supported%]" + } + }, + "control_type": { + "name": "Control type" + }, + "supports_cooling": { + "name": "Cooling support", + "state": { + "off": "[%key:component::opentherm_gw::common::state_not_supported%]", + "on": "[%key:component::opentherm_gw::common::state_supported%]" + } + }, + "hot_water_config": { + "name": "Hot water system type", + "state": { + "off": "Instantaneous or unspecified", + "on": "Storage tank" + } + }, + "supports_pump_control": { + "name": "Pump control support", + "state": { + "off": "[%key:component::opentherm_gw::common::state_not_supported%]", + "on": "[%key:component::opentherm_gw::common::state_supported%]" + } + }, + "supports_ch_2": { + "name": "Central heating 2 support", + "state": { + "off": "[%key:component::opentherm_gw::common::state_not_supported%]", + "on": "[%key:component::opentherm_gw::common::state_supported%]" + } + }, + "service_required": { + "name": "Service required" + }, + "supports_remote_reset": { + "name": "Remote reset support", + "state": { + "off": "[%key:component::opentherm_gw::common::state_not_supported%]", + "on": "[%key:component::opentherm_gw::common::state_supported%]" + } + }, + "low_water_pressure": { + "name": "Low water pressure" + }, + "gas_fault": { + "name": "Gas fault" + }, + "air_pressure_fault": { + "name": "Air pressure fault" + }, + "water_overtemperature": { + "name": "Water overtemperature" + }, + "supports_central_heating_setpoint_transfer": { + "name": "Central heating setpoint transfer support", + "state": { + "off": "[%key:component::opentherm_gw::common::state_not_supported%]", + "on": "[%key:component::opentherm_gw::common::state_supported%]" + } + }, + "supports_central_heating_setpoint_writing": { + "name": "Central heating setpoint write support", + "state": { + "off": "[%key:component::opentherm_gw::common::state_not_supported%]", + "on": "[%key:component::opentherm_gw::common::state_supported%]" + } + }, + "supports_hot_water_setpoint_transfer": { + "name": "Hot water setpoint transfer support", + "state": { + "off": "[%key:component::opentherm_gw::common::state_not_supported%]", + "on": "[%key:component::opentherm_gw::common::state_supported%]" + } + }, + "supports_hot_water_setpoint_writing": { + "name": "Hot water setpoint write support", + "state": { + "off": "[%key:component::opentherm_gw::common::state_not_supported%]", + "on": "[%key:component::opentherm_gw::common::state_supported%]" + } + }, + "gpio_state_n": { + "name": "GPIO {gpio_id} state" + }, + "ignore_transitions": { + "name": "Ignore transitions" + }, + "override_high_byte": { + "name": "Override high byte" + }, + "outside_temp_correction": { + "name": "Outside temperature correction" + }, + "override_manual_change_prio": { + "name": "Manual change has priority over override" + }, + "override_program_change_prio": { + "name": "Programmed change has priority over override" + } + }, + "sensor": { + "control_setpoint_n": { + "name": "Control setpoint {circuit_number}" + }, + "manufacturer_id": { + "name": "Manufacturer ID" + }, + "oem_fault_code": { + "name": "Manufacturer-specific fault code" + }, + "cooling_control": { + "name": "Cooling control signal" + }, + "max_relative_mod_level": { + "name": "Maximum relative modulation level" + }, + "max_capacity": { + "name": "Maximum capacity" + }, + "min_mod_level": { + "name": "Minimum modulation level" + }, + "relative_mod_level": { + "name": "Relative modulation level" + }, + "central_heating_pressure": { + "name": "Central heating water pressure" + }, + "hot_water_flow_rate": { + "name": "Hot water flow rate" + }, + "central_heating_temperature_n": { + "name": "Central heating {circuit_number} water temperature" + }, + "hot_water_temperature_n": { + "name": "Hot water {circuit_number} temperature" + }, + "return_water_temperature": { + "name": "Return water temperature" + }, + "solar_storage_temperature": { + "name": "Solar storage temperature" + }, + "solar_collector_temperature": { + "name": "Solar collector temperature" + }, + "exhaust_temperature": { + "name": "Exhaust temperature" + }, + "max_hot_water_setpoint_upper": { + "name": "Maximum hot water setpoint upper bound" + }, + "max_hot_water_setpoint_lower": { + "name": "Maximum hot water setpoint lower bound" + }, + "max_central_heating_setpoint_upper": { + "name": "Maximum central heating setpoint upper bound" + }, + "max_central_heating_setpoint_lower": { + "name": "Maximum central heating setpoint lower bound" + }, + "hot_water_setpoint": { + "name": "Hot water setpoint" + }, + "max_central_heating_setpoint": { + "name": "Maximum central heating setpoint" + }, + "oem_diagnostic_code": { + "name": "Manufacturer-specific diagnostic code" + }, + "total_burner_starts": { + "name": "Burner start count" + }, + "central_heating_pump_starts": { + "name": "Central heating pump start count" + }, + "hot_water_pump_starts": { + "name": "Hot water pump start count" + }, + "hot_water_burner_starts": { + "name": "Hot water burner start count" + }, + "total_burner_hours": { + "name": "Burner running time" + }, + "central_heating_pump_hours": { + "name": "Central heating pump running time" + }, + "hot_water_pump_hours": { + "name": "Hot water pump running time" + }, + "hot_water_burner_hours": { + "name": "Hot water burner running time" + }, + "opentherm_version": { + "name": "OpenTherm protocol version" + }, + "product_type": { + "name": "Product type" + }, + "product_version": { + "name": "Product version" + }, + "operating_mode": { + "name": "Operating mode" + }, + "hot_water_override_mode": { + "name": "Hot water override mode" + }, + "firmware_version": { + "name": "Firmware version" + }, + "firmware_build": { + "name": "Firmware build" + }, + "clock_speed": { + "name": "Clock speed" + }, + "led_mode_n": { + "name": "LED {led_id} mode" + }, + "gpio_mode_n": { + "name": "GPIO {gpio_id} mode" + }, + "setback_temperature": { + "name": "Setback temperature" + }, + "room_setpoint_override_mode": { + "name": "Room setpoint override mode" + }, + "smart_power_mode": { + "name": "Smart power mode" + }, + "thermostat_detection_mode": { + "name": "Thermostat detection mode" + }, + "reference_voltage": { + "name": "Reference voltage setting" + }, + "room_setpoint_override": { + "name": "Room setpoint override" + }, + "room_setpoint_n": { + "name": "Room setpoint {setpoint_id}" + }, + "room_temperature": { + "name": "Room temperature" + }, + "outside_temperature": { + "name": "Outside temperature" + } + } + }, "options": { "step": { "init": { diff --git a/homeassistant/components/opower/config_flow.py b/homeassistant/components/opower/config_flow.py index 574062aca523d2..a9162b060a2ed4 100644 --- a/homeassistant/components/opower/config_flow.py +++ b/homeassistant/components/opower/config_flow.py @@ -4,7 +4,6 @@ from collections.abc import Mapping import logging -import socket from typing import Any from opower import ( @@ -40,7 +39,7 @@ async def _validate_login( ) -> dict[str, str]: """Validate login data and return any errors.""" api = Opower( - async_create_clientsession(hass, family=socket.AF_INET), + async_create_clientsession(hass), login_data[CONF_UTILITY], login_data[CONF_USERNAME], login_data[CONF_PASSWORD], diff --git a/homeassistant/components/opower/coordinator.py b/homeassistant/components/opower/coordinator.py index d0795ae4e15e01..1e00243f6573f0 100644 --- a/homeassistant/components/opower/coordinator.py +++ b/homeassistant/components/opower/coordinator.py @@ -2,7 +2,6 @@ from datetime import datetime, timedelta import logging -import socket from types import MappingProxyType from typing import Any, cast @@ -54,7 +53,7 @@ def __init__( update_interval=timedelta(hours=12), ) self.api = Opower( - aiohttp_client.async_get_clientsession(hass, family=socket.AF_INET), + aiohttp_client.async_get_clientsession(hass), entry_data[CONF_UTILITY], entry_data[CONF_USERNAME], entry_data[CONF_PASSWORD], @@ -98,7 +97,7 @@ async def _insert_statistics(self) -> None: account.meter_type.name.lower(), # Some utilities like AEP have "-" in their account id. # Replace it with "_" to avoid "Invalid statistic_id" - account.utility_account_id.replace("-", "_"), + account.utility_account_id.replace("-", "_").lower(), ) ) cost_statistic_id = f"{DOMAIN}:{id_prefix}_energy_cost" @@ -110,7 +109,7 @@ async def _insert_statistics(self) -> None: ) last_stat = await get_instance(self.hass).async_add_executor_job( - get_last_statistics, self.hass, 1, cost_statistic_id, True, set() + get_last_statistics, self.hass, 1, consumption_statistic_id, True, set() ) if not last_stat: _LOGGER.debug("Updating statistic for the first time") @@ -124,7 +123,7 @@ async def _insert_statistics(self) -> None: cost_reads = await self._async_get_cost_reads( account, self.api.utility.timezone(), - last_stat[cost_statistic_id][0]["start"], + last_stat[consumption_statistic_id][0]["start"], ) if not cost_reads: _LOGGER.debug("No recent usage/cost data. Skipping update") @@ -141,7 +140,7 @@ async def _insert_statistics(self) -> None: ) cost_sum = cast(float, stats[cost_statistic_id][0]["sum"]) consumption_sum = cast(float, stats[consumption_statistic_id][0]["sum"]) - last_stats_time = stats[cost_statistic_id][0]["start"] + last_stats_time = stats[consumption_statistic_id][0]["start"] cost_statistics = [] consumption_statistics = [] @@ -187,7 +186,17 @@ async def _insert_statistics(self) -> None: else UnitOfVolume.CENTUM_CUBIC_FEET, ) + _LOGGER.debug( + "Adding %s statistics for %s", + len(cost_statistics), + cost_statistic_id, + ) async_add_external_statistics(self.hass, cost_metadata, cost_statistics) + _LOGGER.debug( + "Adding %s statistics for %s", + len(consumption_statistics), + consumption_statistic_id, + ) async_add_external_statistics( self.hass, consumption_metadata, consumption_statistics ) @@ -227,9 +236,11 @@ def _update_with_finer_cost_reads( else: start = datetime.fromtimestamp(start_time, tz=tz) - timedelta(days=30) end = dt_util.now(tz) + _LOGGER.debug("Getting monthly cost reads: %s - %s", start, end) cost_reads = await self.api.async_get_cost_reads( account, AggregateType.BILL, start, end ) + _LOGGER.debug("Got %s monthly cost reads", len(cost_reads)) if account.read_resolution == ReadResolution.BILLING: return cost_reads @@ -240,9 +251,11 @@ def _update_with_finer_cost_reads( start = cost_reads[0].start_time assert start start = max(start, end - timedelta(days=3 * 365)) + _LOGGER.debug("Getting daily cost reads: %s - %s", start, end) daily_cost_reads = await self.api.async_get_cost_reads( account, AggregateType.DAY, start, end ) + _LOGGER.debug("Got %s daily cost reads", len(daily_cost_reads)) _update_with_finer_cost_reads(cost_reads, daily_cost_reads) if account.read_resolution == ReadResolution.DAY: return cost_reads @@ -252,8 +265,11 @@ def _update_with_finer_cost_reads( else: assert start start = max(start, end - timedelta(days=2 * 30)) + _LOGGER.debug("Getting hourly cost reads: %s - %s", start, end) hourly_cost_reads = await self.api.async_get_cost_reads( account, AggregateType.HOUR, start, end ) + _LOGGER.debug("Got %s hourly cost reads", len(hourly_cost_reads)) _update_with_finer_cost_reads(cost_reads, hourly_cost_reads) + _LOGGER.debug("Got %s cost reads", len(cost_reads)) return cost_reads diff --git a/homeassistant/components/opower/manifest.json b/homeassistant/components/opower/manifest.json index b869356cdf987d..02b98cfaf00fb2 100644 --- a/homeassistant/components/opower/manifest.json +++ b/homeassistant/components/opower/manifest.json @@ -7,5 +7,5 @@ "documentation": "https://www.home-assistant.io/integrations/opower", "iot_class": "cloud_polling", "loggers": ["opower"], - "requirements": ["opower==0.6.0"] + "requirements": ["opower==0.7.0"] } diff --git a/homeassistant/components/osoenergy/config_flow.py b/homeassistant/components/osoenergy/config_flow.py index e0afc5292aee9a..0642250e9ed8f7 100644 --- a/homeassistant/components/osoenergy/config_flow.py +++ b/homeassistant/components/osoenergy/config_flow.py @@ -69,9 +69,9 @@ async def get_user_email(self, subscription_key: str) -> str | None: return None async def async_step_reauth( - self, user_input: Mapping[str, Any] + self, entry_data: Mapping[str, Any] ) -> ConfigFlowResult: """Re Authenticate a user.""" self.entry = self.hass.config_entries.async_get_entry(self.context["entry_id"]) - data = {CONF_API_KEY: user_input[CONF_API_KEY]} + data = {CONF_API_KEY: entry_data[CONF_API_KEY]} return await self.async_step_user(data) diff --git a/homeassistant/components/otbr/__init__.py b/homeassistant/components/otbr/__init__.py index 3e53358a16255d..4b95be1d40d678 100644 --- a/homeassistant/components/otbr/__init__.py +++ b/homeassistant/components/otbr/__init__.py @@ -2,6 +2,8 @@ from __future__ import annotations +import logging + import aiohttp import python_otbr_api @@ -14,22 +16,28 @@ from homeassistant.helpers.typing import ConfigType from . import websocket_api -from .const import DATA_OTBR, DOMAIN -from .util import OTBRData, update_issues +from .const import DOMAIN +from .util import ( + GetBorderAgentIdNotSupported, + OTBRData, + update_issues, + update_unique_id, +) + +_LOGGER = logging.getLogger(__name__) CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN) +type OTBRConfigEntry = ConfigEntry[OTBRData] + async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up the Open Thread Border Router component.""" websocket_api.async_setup(hass) - if len(config_entries := hass.config_entries.async_entries(DOMAIN)): - for config_entry in config_entries[1:]: - await hass.config_entries.async_remove(config_entry.entry_id) return True -async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: +async def async_setup_entry(hass: HomeAssistant, entry: OTBRConfigEntry) -> bool: """Set up an Open Thread Border Router config entry.""" api = python_otbr_api.OTBR(entry.data["url"], async_get_clientsession(hass), 10) @@ -38,13 +46,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: border_agent_id = await otbrdata.get_border_agent_id() dataset_tlvs = await otbrdata.get_active_dataset_tlvs() extended_address = await otbrdata.get_extended_address() - except ( - HomeAssistantError, - aiohttp.ClientError, - TimeoutError, - ) as err: - raise ConfigEntryNotReady("Unable to connect") from err - if border_agent_id is None: + except GetBorderAgentIdNotSupported: ir.async_create_issue( hass, DOMAIN, @@ -55,6 +57,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: translation_key="get_get_border_agent_id_unsupported", ) return False + except ( + HomeAssistantError, + aiohttp.ClientError, + TimeoutError, + ) as err: + raise ConfigEntryNotReady("Unable to connect") from err + await update_unique_id(hass, entry, border_agent_id) if dataset_tlvs: await update_issues(hass, otbrdata, dataset_tlvs) await async_add_dataset( @@ -66,18 +75,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: ) entry.async_on_unload(entry.add_update_listener(async_reload_entry)) - - hass.data[DATA_OTBR] = otbrdata + entry.runtime_data = otbrdata return True -async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: +async def async_unload_entry(hass: HomeAssistant, entry: OTBRConfigEntry) -> bool: """Unload a config entry.""" - hass.data.pop(DATA_OTBR) return True -async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None: +async def async_reload_entry(hass: HomeAssistant, entry: OTBRConfigEntry) -> None: """Handle an options update.""" await hass.config_entries.async_reload(entry.entry_id) diff --git a/homeassistant/components/otbr/config_flow.py b/homeassistant/components/otbr/config_flow.py index 8cffc0a99e6df4..c1747981b07deb 100644 --- a/homeassistant/components/otbr/config_flow.py +++ b/homeassistant/components/otbr/config_flow.py @@ -4,7 +4,7 @@ from contextlib import suppress import logging -from typing import cast +from typing import TYPE_CHECKING, cast import aiohttp import python_otbr_api @@ -33,9 +33,16 @@ get_allowed_channel, ) +if TYPE_CHECKING: + from . import OTBRConfigEntry + _LOGGER = logging.getLogger(__name__) +class AlreadyConfigured(HomeAssistantError): + """Raised when the router is already configured.""" + + def _is_yellow(hass: HomeAssistant) -> bool: """Return True if Home Assistant is running on a Home Assistant Yellow.""" try: @@ -70,9 +77,8 @@ class OTBRConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - async def _connect_and_set_dataset(self, otbr_url: str) -> None: + async def _set_dataset(self, api: python_otbr_api.OTBR, otbr_url: str) -> None: """Connect to the OTBR and create or apply a dataset if it doesn't have one.""" - api = python_otbr_api.OTBR(otbr_url, async_get_clientsession(self.hass), 10) if await api.get_active_dataset_tlvs() is None: allowed_channel = await get_allowed_channel(self.hass, otbr_url) @@ -89,7 +95,9 @@ async def _connect_and_set_dataset(self, otbr_url: str) -> None: await api.set_active_dataset_tlvs(bytes.fromhex(thread_dataset_tlv)) else: _LOGGER.debug( - "not importing TLV with channel %s", thread_dataset_channel + "not importing TLV with channel %s for %s", + thread_dataset_channel, + otbr_url, ) pan_id = generate_random_pan_id() await api.create_active_dataset( @@ -101,27 +109,65 @@ async def _connect_and_set_dataset(self, otbr_url: str) -> None: ) await api.set_enabled(True) + async def _is_border_agent_id_configured(self, border_agent_id: bytes) -> bool: + """Return True if another config entry's OTBR has the same border agent id.""" + config_entry: OTBRConfigEntry + for config_entry in self.hass.config_entries.async_loaded_entries(DOMAIN): + data = config_entry.runtime_data + try: + other_border_agent_id = await data.get_border_agent_id() + except HomeAssistantError: + _LOGGER.debug( + "Could not read border agent id from %s", data.url, exc_info=True + ) + continue + _LOGGER.debug( + "border agent id for existing url %s: %s", + data.url, + other_border_agent_id.hex(), + ) + if border_agent_id == other_border_agent_id: + return True + return False + + async def _connect_and_configure_router(self, otbr_url: str) -> bytes: + """Connect to the router and configure it if needed. + + Will raise if the router's border agent id is in use by another config entry. + Returns the router's border agent id. + """ + api = python_otbr_api.OTBR(otbr_url, async_get_clientsession(self.hass), 10) + border_agent_id = await api.get_border_agent_id() + _LOGGER.debug("border agent id for url %s: %s", otbr_url, border_agent_id.hex()) + + if await self._is_border_agent_id_configured(border_agent_id): + raise AlreadyConfigured + + await self._set_dataset(api, otbr_url) + + return border_agent_id + async def async_step_user( self, user_input: dict[str, str] | None = None ) -> ConfigFlowResult: """Set up by user.""" - if self._async_current_entries(): - return self.async_abort(reason="single_instance_allowed") - errors = {} if user_input is not None: url = user_input[CONF_URL].rstrip("/") try: - await self._connect_and_set_dataset(url) + border_agent_id = await self._connect_and_configure_router(url) + except AlreadyConfigured: + errors["base"] = "already_configured" except ( python_otbr_api.OTBRError, aiohttp.ClientError, TimeoutError, - ): + ) as exc: + _LOGGER.debug("Failed to communicate with OTBR@%s: %s", url, exc) errors["base"] = "cannot_connect" else: - await self.async_set_unique_id(DOMAIN) + await self.async_set_unique_id(border_agent_id.hex()) return self.async_create_entry( title="Open Thread Border Router", data={CONF_URL: url}, @@ -140,34 +186,35 @@ async def async_step_hassio( url = f"http://{config['host']}:{config['port']}" config_entry_data = {"url": url} - if self._async_in_progress(include_uninitialized=True): - # We currently don't handle multiple config entries, abort if hassio - # discovers multiple addons with otbr support - return self.async_abort(reason="single_instance_allowed") - if current_entries := self._async_current_entries(): for current_entry in current_entries: if current_entry.source != SOURCE_HASSIO: continue current_url = yarl.URL(current_entry.data["url"]) - if ( + if not (unique_id := current_entry.unique_id): # The first version did not set a unique_id # so if the entry does not have a unique_id # we have to assume it's the first version - current_entry.unique_id - and (current_entry.unique_id != discovery_info.uuid) + # This check can be removed in HA Core 2025.9 + unique_id = discovery_info.uuid + if ( + unique_id != discovery_info.uuid or current_url.host != config["host"] or current_url.port == config["port"] ): continue # Update URL with the new port self.hass.config_entries.async_update_entry( - current_entry, data=config_entry_data + current_entry, + data=config_entry_data, + unique_id=unique_id, # Remove in HA Core 2025.9 ) - return self.async_abort(reason="single_instance_allowed") + return self.async_abort(reason="already_configured") try: - await self._connect_and_set_dataset(url) + await self._connect_and_configure_router(url) + except AlreadyConfigured: + return self.async_abort(reason="already_configured") except ( python_otbr_api.OTBRError, aiohttp.ClientError, diff --git a/homeassistant/components/otbr/const.py b/homeassistant/components/otbr/const.py index cf1678466a44af..c38b3cc125097c 100644 --- a/homeassistant/components/otbr/const.py +++ b/homeassistant/components/otbr/const.py @@ -2,14 +2,6 @@ from __future__ import annotations -from typing import TYPE_CHECKING - -from homeassistant.util.hass_dict import HassKey - -if TYPE_CHECKING: - from .util import OTBRData - DOMAIN = "otbr" -DATA_OTBR: HassKey[OTBRData] = HassKey(DOMAIN) DEFAULT_CHANNEL = 15 diff --git a/homeassistant/components/otbr/silabs_multiprotocol.py b/homeassistant/components/otbr/silabs_multiprotocol.py index b3a711968fda9d..d97e6811e6d19e 100644 --- a/homeassistant/components/otbr/silabs_multiprotocol.py +++ b/homeassistant/components/otbr/silabs_multiprotocol.py @@ -5,7 +5,7 @@ from collections.abc import Callable, Coroutine from functools import wraps import logging -from typing import Any, Concatenate +from typing import TYPE_CHECKING, Any, Concatenate import aiohttp from python_otbr_api import tlv_parser @@ -18,9 +18,12 @@ from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError -from .const import DATA_OTBR, DOMAIN +from .const import DOMAIN from .util import OTBRData +if TYPE_CHECKING: + from . import OTBRConfigEntry + _LOGGER = logging.getLogger(__name__) @@ -45,15 +48,13 @@ async def async_get_otbr_data_wrapper( hass: HomeAssistant, *args: _P.args, **kwargs: _P.kwargs ) -> _R | _R_Def: """Fetch OTBR data and pass to orig_func.""" - if DATA_OTBR not in hass.data: - return retval - - data = hass.data[DATA_OTBR] - - if not is_multiprotocol_url(data.url): - return retval + config_entry: OTBRConfigEntry + for config_entry in hass.config_entries.async_loaded_entries(DOMAIN): + data = config_entry.runtime_data + if is_multiprotocol_url(data.url): + return await orig_func(hass, data, *args, **kwargs) - return await orig_func(hass, data, *args, **kwargs) + return retval return async_get_otbr_data_wrapper diff --git a/homeassistant/components/otbr/strings.json b/homeassistant/components/otbr/strings.json index 838ebeb5b8cb73..bc7812c1db777a 100644 --- a/homeassistant/components/otbr/strings.json +++ b/homeassistant/components/otbr/strings.json @@ -9,6 +9,7 @@ } }, "error": { + "already_configured": "The Thread border router is already configured", "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]" }, "abort": { diff --git a/homeassistant/components/otbr/util.py b/homeassistant/components/otbr/util.py index d426ca9ba17aa7..351e23c7736a08 100644 --- a/homeassistant/components/otbr/util.py +++ b/homeassistant/components/otbr/util.py @@ -7,7 +7,7 @@ from functools import wraps import logging import random -from typing import Any, Concatenate, cast +from typing import TYPE_CHECKING, Any, Concatenate, cast import aiohttp import python_otbr_api @@ -22,12 +22,16 @@ multi_pan_addon_using_device, ) from homeassistant.components.homeassistant_yellow import RADIO_DEVICE as YELLOW_RADIO +from homeassistant.config_entries import SOURCE_USER from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import issue_registry as ir from .const import DOMAIN +if TYPE_CHECKING: + from . import OTBRConfigEntry + _LOGGER = logging.getLogger(__name__) INFO_URL_SKY_CONNECT = ( @@ -48,6 +52,10 @@ ) +class GetBorderAgentIdNotSupported(HomeAssistantError): + """Raised from python_otbr_api.GetBorderAgentIdNotSupportedError.""" + + def compose_default_network_name(pan_id: int) -> str: """Generate a default network name.""" return f"ha-thread-{pan_id:04x}" @@ -83,7 +91,7 @@ class OTBRData: entry_id: str @_handle_otbr_error - async def factory_reset(self) -> None: + async def factory_reset(self, hass: HomeAssistant) -> None: """Reset the router.""" try: await self.api.factory_reset() @@ -92,14 +100,19 @@ async def factory_reset(self) -> None: "OTBR does not support factory reset, attempting to delete dataset" ) await self.delete_active_dataset() + await update_unique_id( + hass, + hass.config_entries.async_get_entry(self.entry_id), + await self.get_border_agent_id(), + ) @_handle_otbr_error - async def get_border_agent_id(self) -> bytes | None: + async def get_border_agent_id(self) -> bytes: """Get the border agent ID or None if not supported by the router.""" try: return await self.api.get_border_agent_id() - except python_otbr_api.GetBorderAgentIdNotSupportedError: - return None + except python_otbr_api.GetBorderAgentIdNotSupportedError as exc: + raise GetBorderAgentIdNotSupported from exc @_handle_otbr_error async def set_enabled(self, enabled: bool) -> None: @@ -258,3 +271,18 @@ async def update_issues( """Raise or clear repair issues related to network settings.""" await _warn_on_channel_collision(hass, otbrdata, dataset_tlvs) _warn_on_default_network_settings(hass, otbrdata, dataset_tlvs) + + +async def update_unique_id( + hass: HomeAssistant, entry: OTBRConfigEntry | None, border_agent_id: bytes +) -> None: + """Update the config entry's unique_id if not matching.""" + border_agent_id_hex = border_agent_id.hex() + if entry and entry.source == SOURCE_USER and entry.unique_id != border_agent_id_hex: + _LOGGER.debug( + "Updating unique_id of entry %s from %s to %s", + entry.entry_id, + entry.unique_id, + border_agent_id_hex, + ) + hass.config_entries.async_update_entry(entry, unique_id=border_agent_id_hex) diff --git a/homeassistant/components/otbr/websocket_api.py b/homeassistant/components/otbr/websocket_api.py index 577f9cc381d811..2bcd0da8f16c50 100644 --- a/homeassistant/components/otbr/websocket_api.py +++ b/homeassistant/components/otbr/websocket_api.py @@ -2,7 +2,7 @@ from collections.abc import Callable, Coroutine from functools import wraps -from typing import Any, cast +from typing import TYPE_CHECKING, Any, cast import python_otbr_api from python_otbr_api import PENDING_DATASET_DELAY_TIMER, tlv_parser @@ -17,7 +17,7 @@ from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import HomeAssistantError -from .const import DATA_OTBR, DEFAULT_CHANNEL, DOMAIN +from .const import DEFAULT_CHANNEL, DOMAIN from .util import ( OTBRData, compose_default_network_name, @@ -26,6 +26,9 @@ update_issues, ) +if TYPE_CHECKING: + from . import OTBRConfigEntry + @callback def async_setup(hass: HomeAssistant) -> None: @@ -47,41 +50,45 @@ async def websocket_info( hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict ) -> None: """Get OTBR info.""" - if DATA_OTBR not in hass.data: + config_entries: list[OTBRConfigEntry] + config_entries = hass.config_entries.async_loaded_entries(DOMAIN) + + if not config_entries: connection.send_error(msg["id"], "not_loaded", "No OTBR API loaded") return - data = hass.data[DATA_OTBR] + response: dict[str, dict[str, Any]] = {} - try: - border_agent_id = await data.get_border_agent_id() - dataset = await data.get_active_dataset() - dataset_tlvs = await data.get_active_dataset_tlvs() - extended_address = (await data.get_extended_address()).hex() - except HomeAssistantError as exc: - connection.send_error(msg["id"], "otbr_info_failed", str(exc)) - return + for config_entry in config_entries: + data = config_entry.runtime_data + try: + border_agent_id = await data.get_border_agent_id() + dataset = await data.get_active_dataset() + dataset_tlvs = await data.get_active_dataset_tlvs() + extended_address = (await data.get_extended_address()).hex() + except HomeAssistantError as exc: + connection.send_error(msg["id"], "otbr_info_failed", str(exc)) + return - # The border agent ID is checked when the OTBR config entry is setup, - # we can assert it's not None - assert border_agent_id is not None - - extended_pan_id = ( - dataset.extended_pan_id.lower() if dataset and dataset.extended_pan_id else None - ) - connection.send_result( - msg["id"], - { - extended_address: { - "active_dataset_tlvs": dataset_tlvs.hex() if dataset_tlvs else None, - "border_agent_id": border_agent_id.hex(), - "channel": dataset.channel if dataset else None, - "extended_address": extended_address, - "extended_pan_id": extended_pan_id, - "url": data.url, - } - }, - ) + # The border agent ID is checked when the OTBR config entry is setup, + # we can assert it's not None + assert border_agent_id is not None + + extended_pan_id = ( + dataset.extended_pan_id.lower() + if dataset and dataset.extended_pan_id + else None + ) + response[extended_address] = { + "active_dataset_tlvs": dataset_tlvs.hex() if dataset_tlvs else None, + "border_agent_id": border_agent_id.hex(), + "channel": dataset.channel if dataset else None, + "extended_address": extended_address, + "extended_pan_id": extended_pan_id, + "url": data.url, + } + + connection.send_result(msg["id"], response) def async_get_otbr_data( @@ -99,22 +106,29 @@ async def async_check_extended_address_func( hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict ) -> None: """Fetch OTBR data and pass to orig_func.""" - if DATA_OTBR not in hass.data: + config_entries: list[OTBRConfigEntry] + config_entries = hass.config_entries.async_loaded_entries(DOMAIN) + + if not config_entries: connection.send_error(msg["id"], "not_loaded", "No OTBR API loaded") return - data = hass.data[DATA_OTBR] - - try: - extended_address = await data.get_extended_address() - except HomeAssistantError as exc: - connection.send_error(msg["id"], "get_extended_address_failed", str(exc)) - return - if extended_address.hex() != msg["extended_address"]: - connection.send_error(msg["id"], "unknown_router", "") + for config_entry in config_entries: + data = config_entry.runtime_data + try: + extended_address = await data.get_extended_address() + except HomeAssistantError as exc: + connection.send_error( + msg["id"], "get_extended_address_failed", str(exc) + ) + return + if extended_address.hex() != msg["extended_address"]: + continue + + await orig_func(hass, connection, msg, data) return - await orig_func(hass, connection, msg, data) + connection.send_error(msg["id"], "unknown_router", "") return async_check_extended_address_func @@ -144,7 +158,7 @@ async def websocket_create_network( return try: - await data.factory_reset() + await data.factory_reset(hass) except HomeAssistantError as exc: connection.send_error(msg["id"], "factory_reset_failed", str(exc)) return diff --git a/homeassistant/components/otp/config_flow.py b/homeassistant/components/otp/config_flow.py index 6aa4532683a61f..33f63a04d68940 100644 --- a/homeassistant/components/otp/config_flow.py +++ b/homeassistant/components/otp/config_flow.py @@ -82,15 +82,15 @@ async def async_step_user( errors=errors, ) - async def async_step_import(self, import_info: dict[str, Any]) -> ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Import config from yaml.""" - await self.async_set_unique_id(import_info[CONF_TOKEN]) + await self.async_set_unique_id(import_data[CONF_TOKEN]) self._abort_if_unique_id_configured() return self.async_create_entry( - title=import_info.get(CONF_NAME, DEFAULT_NAME), - data=import_info, + title=import_data.get(CONF_NAME, DEFAULT_NAME), + data=import_data, ) async def async_step_confirm( diff --git a/homeassistant/components/overkiz/binary_sensor.py b/homeassistant/components/overkiz/binary_sensor.py index 8ea86e03e8cccd..57df3cd4e09e11 100644 --- a/homeassistant/components/overkiz/binary_sensor.py +++ b/homeassistant/components/overkiz/binary_sensor.py @@ -115,14 +115,24 @@ class OverkizBinarySensorDescription(BinarySensorEntityDescription): OverkizBinarySensorDescription( key=OverkizState.MODBUSLINK_DHW_ABSENCE_MODE, name="Absence mode", - value_fn=lambda state: state - in (OverkizCommandParam.ON, OverkizCommandParam.PROG), + value_fn=( + lambda state: state in (OverkizCommandParam.ON, OverkizCommandParam.PROG) + ), ), OverkizBinarySensorDescription( key=OverkizState.MODBUSLINK_DHW_BOOST_MODE, name="Boost mode", - value_fn=lambda state: state - in (OverkizCommandParam.ON, OverkizCommandParam.PROG), + value_fn=( + lambda state: state in (OverkizCommandParam.ON, OverkizCommandParam.PROG) + ), + ), + OverkizBinarySensorDescription( + key=OverkizState.MODBUSLINK_DHW_MODE, + name="Manual mode", + value_fn=( + lambda state: state + in (OverkizCommandParam.MANUAL, OverkizCommandParam.MANUAL_ECO_INACTIVE) + ), ), ] diff --git a/homeassistant/components/overkiz/water_heater_entities/atlantic_domestic_hot_water_production_mlb_component.py b/homeassistant/components/overkiz/water_heater_entities/atlantic_domestic_hot_water_production_mlb_component.py index de995a2bd1af93..0f57d13433b902 100644 --- a/homeassistant/components/overkiz/water_heater_entities/atlantic_domestic_hot_water_production_mlb_component.py +++ b/homeassistant/components/overkiz/water_heater_entities/atlantic_domestic_hot_water_production_mlb_component.py @@ -6,6 +6,7 @@ from homeassistant.components.water_heater import ( STATE_ECO, + STATE_ELECTRIC, STATE_OFF, STATE_PERFORMANCE, WaterHeaterEntity, @@ -28,9 +29,10 @@ class AtlanticDomesticHotWaterProductionMBLComponent(OverkizEntity, WaterHeaterE | WaterHeaterEntityFeature.ON_OFF ) _attr_operation_list = [ - OverkizCommandParam.PERFORMANCE, - OverkizCommandParam.ECO, - OverkizCommandParam.MANUAL, + STATE_ECO, + STATE_OFF, + STATE_PERFORMANCE, + STATE_ELECTRIC, ] def __init__( @@ -116,20 +118,20 @@ def current_operation(self) -> str: cast(str, self.executor.select_state(OverkizState.MODBUSLINK_DHW_MODE)) == OverkizCommandParam.MANUAL_ECO_INACTIVE ): - return OverkizCommandParam.MANUAL + # STATE_ELECTRIC is a substitution for OverkizCommandParam.MANUAL + # to keep up with the conventional state usage only + # https://developers.home-assistant.io/docs/core/entity/water-heater/#states + return STATE_ELECTRIC return STATE_OFF async def async_set_operation_mode(self, operation_mode: str) -> None: """Set new operation mode.""" - if operation_mode in (STATE_PERFORMANCE, OverkizCommandParam.BOOST): + if operation_mode == STATE_PERFORMANCE: if self.is_away_mode_on: await self.async_turn_away_mode_off() await self.async_turn_boost_mode_on() - elif operation_mode in ( - OverkizCommandParam.ECO, - OverkizCommandParam.MANUAL_ECO_ACTIVE, - ): + elif operation_mode == STATE_ECO: if self.is_away_mode_on: await self.async_turn_away_mode_off() if self.is_boost_mode_on: @@ -137,10 +139,7 @@ async def async_set_operation_mode(self, operation_mode: str) -> None: await self.executor.async_execute_command( OverkizCommand.SET_DHW_MODE, OverkizCommandParam.AUTO_MODE ) - elif operation_mode in ( - OverkizCommandParam.MANUAL, - OverkizCommandParam.MANUAL_ECO_INACTIVE, - ): + elif operation_mode == STATE_ELECTRIC: if self.is_away_mode_on: await self.async_turn_away_mode_off() if self.is_boost_mode_on: @@ -148,14 +147,8 @@ async def async_set_operation_mode(self, operation_mode: str) -> None: await self.executor.async_execute_command( OverkizCommand.SET_DHW_MODE, OverkizCommandParam.MANUAL_ECO_INACTIVE ) - else: - if self.is_away_mode_on: - await self.async_turn_away_mode_off() - if self.is_boost_mode_on: - await self.async_turn_boost_mode_off() - await self.executor.async_execute_command( - OverkizCommand.SET_DHW_MODE, operation_mode - ) + elif operation_mode == STATE_OFF: + await self.async_turn_away_mode_on() async def async_turn_away_mode_on(self) -> None: """Turn away mode on.""" diff --git a/homeassistant/components/owntracks/config_flow.py b/homeassistant/components/owntracks/config_flow.py index 29fe4f0cf65af6..390cc880c1ec9e 100644 --- a/homeassistant/components/owntracks/config_flow.py +++ b/homeassistant/components/owntracks/config_flow.py @@ -1,9 +1,10 @@ """Config flow for OwnTracks.""" import secrets +from typing import Any from homeassistant.components import cloud, webhook -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_WEBHOOK_ID from .const import DOMAIN @@ -18,7 +19,9 @@ class OwnTracksFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle a user initiated set up flow to create OwnTracks webhook.""" if self._async_current_entries(): return self.async_abort(reason="single_instance_allowed") diff --git a/homeassistant/components/panasonic_viera/config_flow.py b/homeassistant/components/panasonic_viera/config_flow.py index 0226fb33c9eb91..b00fee513a63e7 100644 --- a/homeassistant/components/panasonic_viera/config_flow.py +++ b/homeassistant/components/panasonic_viera/config_flow.py @@ -157,11 +157,9 @@ async def async_step_pairing( errors=errors, ) - async def async_step_import( - self, import_config: dict[str, Any] - ) -> ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Import a config entry from configuration.yaml.""" - return await self.async_step_user(user_input=import_config) + return await self.async_step_user(user_input=import_data) async def async_load_data(self, config: dict[str, Any]) -> None: """Load the data.""" diff --git a/homeassistant/components/permobil/config_flow.py b/homeassistant/components/permobil/config_flow.py index cb47640e55f7f5..f7f247a412eeae 100644 --- a/homeassistant/components/permobil/config_flow.py +++ b/homeassistant/components/permobil/config_flow.py @@ -161,17 +161,12 @@ async def async_step_email_code( return self.async_create_entry(title=self.data[CONF_EMAIL], data=self.data) async def async_step_reauth( - self, user_input: Mapping[str, Any] + self, entry_data: Mapping[str, Any] ) -> ConfigFlowResult: """Perform reauth upon an API authentication error.""" - reauth_entry = self.hass.config_entries.async_get_entry( - self.context["entry_id"] - ) - assert reauth_entry - try: - email: str = reauth_entry.data[CONF_EMAIL] - region: str = reauth_entry.data[CONF_REGION] + email: str = entry_data[CONF_EMAIL] + region: str = entry_data[CONF_REGION] self.p_api.set_email(email) self.p_api.set_region(region) self.data = { diff --git a/homeassistant/components/persistent_notification/icons.json b/homeassistant/components/persistent_notification/icons.json index 9c782bd7b21b0f..30847357a47e78 100644 --- a/homeassistant/components/persistent_notification/icons.json +++ b/homeassistant/components/persistent_notification/icons.json @@ -1,7 +1,13 @@ { "services": { - "create": "mdi:message-badge", - "dismiss": "mdi:bell-off", - "dismiss_all": "mdi:notification-clear-all" + "create": { + "service": "mdi:message-badge" + }, + "dismiss": { + "service": "mdi:bell-off" + }, + "dismiss_all": { + "service": "mdi:notification-clear-all" + } } } diff --git a/homeassistant/components/person/icons.json b/homeassistant/components/person/icons.json index fbfd5be75d2e59..f645d9c20905fc 100644 --- a/homeassistant/components/person/icons.json +++ b/homeassistant/components/person/icons.json @@ -8,6 +8,8 @@ } }, "services": { - "reload": "mdi:reload" + "reload": { + "service": "mdi:reload" + } } } diff --git a/homeassistant/components/pi_hole/icons.json b/homeassistant/components/pi_hole/icons.json index 58f20da5a2d9cd..3a45f8ab4544be 100644 --- a/homeassistant/components/pi_hole/icons.json +++ b/homeassistant/components/pi_hole/icons.json @@ -36,6 +36,8 @@ } }, "services": { - "disable": "mdi:server-off" + "disable": { + "service": "mdi:server-off" + } } } diff --git a/homeassistant/components/picnic/config_flow.py b/homeassistant/components/picnic/config_flow.py index 3023b5309dedf5..9548029209b0f1 100644 --- a/homeassistant/components/picnic/config_flow.py +++ b/homeassistant/components/picnic/config_flow.py @@ -87,7 +87,9 @@ async def async_step_reauth( """Perform the re-auth step upon an API authentication error.""" return await self.async_step_user() - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the authentication step, this is the generic step for both `step_user` and `step_reauth`.""" if user_input is None: return self.async_show_form( diff --git a/homeassistant/components/picnic/icons.json b/homeassistant/components/picnic/icons.json index d8f99153f330a8..78803b6d263b50 100644 --- a/homeassistant/components/picnic/icons.json +++ b/homeassistant/components/picnic/icons.json @@ -57,6 +57,8 @@ } }, "services": { - "add_product": "mdi:cart-plus" + "add_product": { + "service": "mdi:cart-plus" + } } } diff --git a/homeassistant/components/pilight/icons.json b/homeassistant/components/pilight/icons.json index c1b8e741e4536c..cbc48cf2105355 100644 --- a/homeassistant/components/pilight/icons.json +++ b/homeassistant/components/pilight/icons.json @@ -1,5 +1,7 @@ { "services": { - "send": "mdi:send" + "send": { + "service": "mdi:send" + } } } diff --git a/homeassistant/components/plaato/config_flow.py b/homeassistant/components/plaato/config_flow.py index 1240abc5e8145a..74967c417a4672 100644 --- a/homeassistant/components/plaato/config_flow.py +++ b/homeassistant/components/plaato/config_flow.py @@ -2,11 +2,18 @@ from __future__ import annotations +from typing import Any + from pyplaato.plaato import PlaatoDeviceType import voluptuous as vol from homeassistant.components import cloud, webhook -from homeassistant.config_entries import ConfigEntry, ConfigFlow, OptionsFlow +from homeassistant.config_entries import ( + ConfigEntry, + ConfigFlow, + ConfigFlowResult, + OptionsFlow, +) from homeassistant.const import CONF_SCAN_INTERVAL, CONF_TOKEN, CONF_WEBHOOK_ID from homeassistant.core import callback import homeassistant.helpers.config_validation as cv @@ -31,11 +38,13 @@ class PlaatoConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - def __init__(self): + def __init__(self) -> None: """Initialize.""" - self._init_info = {} + self._init_info: dict[str, Any] = {} - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle user step.""" if user_input is not None: @@ -62,7 +71,9 @@ async def async_step_user(self, user_input=None): ), ) - async def async_step_api_method(self, user_input=None): + async def async_step_api_method( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle device type step.""" device_type = self._init_info[CONF_DEVICE_TYPE] @@ -81,7 +92,9 @@ async def async_step_api_method(self, user_input=None): return await self._show_api_method_form(device_type) - async def async_step_webhook(self, user_input=None): + async def async_step_webhook( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Validate config step.""" use_webhook = self._init_info[CONF_USE_WEBHOOK] @@ -127,8 +140,8 @@ async def _async_create_entry(self): ) async def _show_api_method_form( - self, device_type: PlaatoDeviceType, errors: dict | None = None - ): + self, device_type: PlaatoDeviceType, errors: dict[str, str] | None = None + ) -> ConfigFlowResult: data_schema = vol.Schema({vol.Optional(CONF_TOKEN, default=""): str}) if device_type == PlaatoDeviceType.Airlock: @@ -177,7 +190,7 @@ def __init__(self, config_entry: ConfigEntry) -> None: self._config_entry = config_entry - async def async_step_init(self, user_input=None): + async def async_step_init(self, user_input: None = None) -> ConfigFlowResult: """Manage the options.""" use_webhook = self._config_entry.data.get(CONF_USE_WEBHOOK, False) if use_webhook: @@ -185,7 +198,9 @@ async def async_step_init(self, user_input=None): return await self.async_step_user() - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Manage the options.""" if user_input is not None: return self.async_create_entry(title="", data=user_input) @@ -204,7 +219,9 @@ async def async_step_user(self, user_input=None): ), ) - async def async_step_webhook(self, user_input=None): + async def async_step_webhook( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Manage the options for webhook device.""" if user_input is not None: return self.async_create_entry(title="", data=user_input) diff --git a/homeassistant/components/plex/config_flow.py b/homeassistant/components/plex/config_flow.py index 374067c94cd407..fcd5751effb352 100644 --- a/homeassistant/components/plex/config_flow.py +++ b/homeassistant/components/plex/config_flow.py @@ -5,7 +5,7 @@ from collections.abc import Mapping import copy import logging -from typing import Any +from typing import TYPE_CHECKING, Any from aiohttp import web_response import plexapi.exceptions @@ -35,7 +35,7 @@ CONF_URL, CONF_VERIFY_SSL, ) -from homeassistant.core import callback +from homeassistant.core import HomeAssistant, callback from homeassistant.helpers import discovery_flow from homeassistant.helpers.aiohttp_client import async_get_clientsession import homeassistant.helpers.config_validation as cv @@ -71,7 +71,7 @@ @callback -def configured_servers(hass): +def configured_servers(hass: HomeAssistant) -> set[str]: """Return a set of the configured Plex servers.""" return { entry.data[CONF_SERVER_IDENTIFIER] @@ -79,7 +79,7 @@ def configured_servers(hass): } -async def async_discover(hass): +async def async_discover(hass: HomeAssistant) -> None: """Scan for available Plex servers.""" gdm = GDM() await hass.async_add_executor_job(gdm.scan) @@ -97,6 +97,9 @@ class PlexFlowHandler(ConfigFlow, domain=DOMAIN): VERSION = 1 + available_servers: list[tuple[str, str, str]] + plexauth: PlexAuth + @staticmethod @callback def async_get_options_flow( @@ -105,31 +108,37 @@ def async_get_options_flow( """Get the options flow for this handler.""" return PlexOptionsFlowHandler(config_entry) - def __init__(self): + def __init__(self) -> None: """Initialize the Plex flow.""" - self.current_login = {} - self.available_servers = None - self.plexauth = None + self.current_login: dict[str, Any] = {} self.token = None self.client_id = None self._manual = False - self._reauth_config = None + self._reauth_config: dict[str, Any] | None = None - async def async_step_user(self, user_input=None, errors=None): + async def async_step_user( + self, + user_input: dict[str, Any] | None = None, + errors: dict[str, str] | None = None, + ) -> ConfigFlowResult: """Handle a flow initialized by the user.""" if user_input is not None: - return await self.async_step_plex_website_auth() + return await self._async_step_plex_website_auth() if self.show_advanced_options: return await self.async_step_user_advanced(errors=errors) return self.async_show_form(step_id="user", errors=errors) - async def async_step_user_advanced(self, user_input=None, errors=None): + async def async_step_user_advanced( + self, + user_input: dict[str, str] | None = None, + errors: dict[str, str] | None = None, + ) -> ConfigFlowResult: """Handle an advanced mode flow initialized by the user.""" if user_input is not None: if user_input.get("setup_method") == MANUAL_SETUP_STRING: self._manual = True return await self.async_step_manual_setup() - return await self.async_step_plex_website_auth() + return await self._async_step_plex_website_auth() data_schema = vol.Schema( { @@ -142,7 +151,11 @@ async def async_step_user_advanced(self, user_input=None, errors=None): step_id="user_advanced", data_schema=data_schema, errors=errors ) - async def async_step_manual_setup(self, user_input=None, errors=None): + async def async_step_manual_setup( + self, + user_input: dict[str, Any] | None = None, + errors: dict[str, str] | None = None, + ) -> ConfigFlowResult: """Begin manual configuration.""" if user_input is not None and errors is None: user_input.pop(CONF_URL, None) @@ -184,7 +197,9 @@ async def async_step_manual_setup(self, user_input=None, errors=None): step_id="manual_setup", data_schema=data_schema, errors=errors ) - async def async_step_server_validate(self, server_config): + async def async_step_server_validate( + self, server_config: dict[str, Any] + ) -> ConfigFlowResult: """Validate a provided configuration.""" if self._reauth_config: server_config = {**self._reauth_config, **server_config} @@ -249,6 +264,8 @@ async def async_step_server_validate(self, server_config): entry = await self.async_set_unique_id(server_id) if self.context[CONF_SOURCE] == SOURCE_REAUTH: + if TYPE_CHECKING: + assert entry self.hass.config_entries.async_update_entry(entry, data=data) _LOGGER.debug("Updated config entry for %s", plex_server.friendly_name) await self.hass.config_entries.async_reload(entry.entry_id) @@ -260,7 +277,9 @@ async def async_step_server_validate(self, server_config): return self.async_create_entry(title=url, data=data) - async def async_step_select_server(self, user_input=None): + async def async_step_select_server( + self, user_input: dict[str, str] | None = None + ) -> ConfigFlowResult: """Use selected Plex server.""" config = dict(self.current_login) if user_input is not None: @@ -288,7 +307,9 @@ async def async_step_select_server(self, user_input=None): errors={}, ) - async def async_step_integration_discovery(self, discovery_info): + async def async_step_integration_discovery( + self, discovery_info: dict[str, Any] + ) -> ConfigFlowResult: """Handle GDM discovery.""" machine_identifier = discovery_info["data"]["Resource-Identifier"] await self.async_set_unique_id(machine_identifier) @@ -301,7 +322,7 @@ async def async_step_integration_discovery(self, discovery_info): } return await self.async_step_user() - async def async_step_plex_website_auth(self): + async def _async_step_plex_website_auth(self) -> ConfigFlowResult: """Begin external auth flow on Plex website.""" self.hass.http.register_view(PlexAuthorizationCallbackView) if (req := http.current_request.get()) is None: @@ -325,7 +346,9 @@ async def async_step_plex_website_auth(self): auth_url = self.plexauth.auth_url(forward_url) return self.async_external_step(step_id="obtain_token", url=auth_url) - async def async_step_obtain_token(self, user_input=None): + async def async_step_obtain_token( + self, user_input: None = None + ) -> ConfigFlowResult: """Obtain token after external auth completed.""" token = await self.plexauth.token(10) @@ -336,11 +359,13 @@ async def async_step_obtain_token(self, user_input=None): self.client_id = self.plexauth.client_identifier return self.async_external_step_done(next_step_id="use_external_token") - async def async_step_timed_out(self, user_input=None): + async def async_step_timed_out(self, user_input: None = None) -> ConfigFlowResult: """Abort flow when time expires.""" return self.async_abort(reason="token_request_timeout") - async def async_step_use_external_token(self, user_input=None): + async def async_step_use_external_token( + self, user_input: None = None + ) -> ConfigFlowResult: """Continue server validation with external token.""" server_config = {CONF_TOKEN: self.token} return await self.async_step_server_validate(server_config) @@ -363,11 +388,13 @@ def __init__(self, config_entry: ConfigEntry) -> None: self.options = copy.deepcopy(dict(config_entry.options)) self.server_id = config_entry.data[CONF_SERVER_IDENTIFIER] - async def async_step_init(self, user_input=None): + async def async_step_init(self, user_input: None = None) -> ConfigFlowResult: """Manage the Plex options.""" return await self.async_step_plex_mp_settings() - async def async_step_plex_mp_settings(self, user_input=None): + async def async_step_plex_mp_settings( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Manage the Plex media_player options.""" plex_server = get_plex_server(self.hass, self.server_id) diff --git a/homeassistant/components/plex/icons.json b/homeassistant/components/plex/icons.json index 03bc835d2f60a2..2d3a7342ad2882 100644 --- a/homeassistant/components/plex/icons.json +++ b/homeassistant/components/plex/icons.json @@ -7,7 +7,11 @@ } }, "services": { - "refresh_library": "mdi:refresh", - "scan_for_clients": "mdi:database-refresh" + "refresh_library": { + "service": "mdi:refresh" + }, + "scan_for_clients": { + "service": "mdi:database-refresh" + } } } diff --git a/homeassistant/components/plex/manifest.json b/homeassistant/components/plex/manifest.json index 323bca0477a155..6270a6d34963af 100644 --- a/homeassistant/components/plex/manifest.json +++ b/homeassistant/components/plex/manifest.json @@ -8,7 +8,7 @@ "iot_class": "local_push", "loggers": ["plexapi", "plexwebsocket"], "requirements": [ - "PlexAPI==4.15.14", + "PlexAPI==4.15.16", "plexauth==0.0.6", "plexwebsocket==0.0.14" ], diff --git a/homeassistant/components/point/config_flow.py b/homeassistant/components/point/config_flow.py index 279561b4e2b1b3..b2455438208163 100644 --- a/homeassistant/components/point/config_flow.py +++ b/homeassistant/components/point/config_flow.py @@ -3,12 +3,13 @@ import asyncio from collections import OrderedDict import logging +from typing import Any from pypoint import PointSession import voluptuous as vol from homeassistant.components.http import KEY_HASS, HomeAssistantView -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_CLIENT_ID, CONF_CLIENT_SECRET from homeassistant.core import callback from homeassistant.helpers.aiohttp_client import async_get_clientsession @@ -59,7 +60,9 @@ async def async_step_import(self, user_input=None): return await self.async_step_auth() - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle a flow start.""" flows = self.hass.data.get(DATA_FLOW_IMPL, {}) diff --git a/homeassistant/components/private_ble_device/manifest.json b/homeassistant/components/private_ble_device/manifest.json index 8b072361d34099..6759cdda0f067c 100644 --- a/homeassistant/components/private_ble_device/manifest.json +++ b/homeassistant/components/private_ble_device/manifest.json @@ -6,5 +6,5 @@ "dependencies": ["bluetooth_adapters"], "documentation": "https://www.home-assistant.io/integrations/private_ble_device", "iot_class": "local_push", - "requirements": ["bluetooth-data-tools==1.19.4"] + "requirements": ["bluetooth-data-tools==1.20.0"] } diff --git a/homeassistant/components/profiler/config_flow.py b/homeassistant/components/profiler/config_flow.py index 4acce51e25f655..19995cf79aaa65 100644 --- a/homeassistant/components/profiler/config_flow.py +++ b/homeassistant/components/profiler/config_flow.py @@ -1,6 +1,8 @@ """Config flow for Profiler integration.""" -from homeassistant.config_entries import ConfigFlow +from typing import Any + +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from .const import DEFAULT_NAME, DOMAIN @@ -10,7 +12,9 @@ class ProfilerConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" if self._async_current_entries(): return self.async_abort(reason="single_instance_allowed") diff --git a/homeassistant/components/profiler/icons.json b/homeassistant/components/profiler/icons.json index 4dda003c186db3..c1f996b6eb15ce 100644 --- a/homeassistant/components/profiler/icons.json +++ b/homeassistant/components/profiler/icons.json @@ -1,16 +1,40 @@ { "services": { - "start": "mdi:play", - "memory": "mdi:memory", - "start_log_objects": "mdi:invoice-text-plus", - "stop_log_objects": "mdi:invoice-text-remove", - "dump_log_objects": "mdi:invoice-export-outline", - "start_log_object_sources": "mdi:play", - "stop_log_object_sources": "mdi:stop", - "lru_stats": "mdi:chart-areaspline", - "log_current_tasks": "mdi:format-list-bulleted", - "log_thread_frames": "mdi:format-list-bulleted", - "log_event_loop_scheduled": "mdi:calendar-clock", - "set_asyncio_debug": "mdi:bug-check" + "start": { + "service": "mdi:play" + }, + "memory": { + "service": "mdi:memory" + }, + "start_log_objects": { + "service": "mdi:invoice-text-plus" + }, + "stop_log_objects": { + "service": "mdi:invoice-text-remove" + }, + "dump_log_objects": { + "service": "mdi:invoice-export-outline" + }, + "start_log_object_sources": { + "service": "mdi:play" + }, + "stop_log_object_sources": { + "service": "mdi:stop" + }, + "lru_stats": { + "service": "mdi:chart-areaspline" + }, + "log_current_tasks": { + "service": "mdi:format-list-bulleted" + }, + "log_thread_frames": { + "service": "mdi:format-list-bulleted" + }, + "log_event_loop_scheduled": { + "service": "mdi:calendar-clock" + }, + "set_asyncio_debug": { + "service": "mdi:bug-check" + } } } diff --git a/homeassistant/components/progettihwsw/config_flow.py b/homeassistant/components/progettihwsw/config_flow.py index dbe12184a1050a..2202678da9b5d5 100644 --- a/homeassistant/components/progettihwsw/config_flow.py +++ b/homeassistant/components/progettihwsw/config_flow.py @@ -1,9 +1,11 @@ """Config flow for ProgettiHWSW Automation integration.""" +from typing import TYPE_CHECKING, Any + from ProgettiHWSW.ProgettiHWSWAPI import ProgettiHWSWAPI import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError @@ -38,11 +40,15 @@ class ProgettiHWSWConfigFlow(ConfigFlow, domain=DOMAIN): def __init__(self) -> None: """Initialize class variables.""" - self.s1_in = None + self.s1_in: dict[str, Any] | None = None - async def async_step_relay_modes(self, user_input=None): + async def async_step_relay_modes( + self, user_input: dict[str, str] | None = None + ) -> ConfigFlowResult: """Manage relay modes step.""" - errors = {} + errors: dict[str, str] = {} + if TYPE_CHECKING: + assert self.s1_in is not None if user_input is not None: whole_data = user_input whole_data.update(self.s1_in) @@ -66,7 +72,9 @@ async def async_step_relay_modes(self, user_input=None): errors=errors, ) - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" errors = {} if user_input is not None: diff --git a/homeassistant/components/prosegur/config_flow.py b/homeassistant/components/prosegur/config_flow.py index 82cf1d424c75c9..7bd87e405ef392 100644 --- a/homeassistant/components/prosegur/config_flow.py +++ b/homeassistant/components/prosegur/config_flow.py @@ -51,7 +51,9 @@ class ProsegurConfigFlow(ConfigFlow, domain=DOMAIN): user_input: dict contracts: list[dict[str, str]] - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" errors = {} @@ -114,9 +116,11 @@ async def async_step_reauth( ) return await self.async_step_reauth_confirm() - async def async_step_reauth_confirm(self, user_input=None): + async def async_step_reauth_confirm( + self, user_input: dict[str, str] | None = None + ) -> ConfigFlowResult: """Handle re-authentication with Prosegur.""" - errors = {} + errors: dict[str, str] = {} if user_input: try: diff --git a/homeassistant/components/prosegur/icons.json b/homeassistant/components/prosegur/icons.json index 33cddefdaea341..8f175ab905678f 100644 --- a/homeassistant/components/prosegur/icons.json +++ b/homeassistant/components/prosegur/icons.json @@ -1,5 +1,7 @@ { "services": { - "request_image": "mdi:image-sync" + "request_image": { + "service": "mdi:image-sync" + } } } diff --git a/homeassistant/components/proximity/config_flow.py b/homeassistant/components/proximity/config_flow.py index d133b14cb6ac96..1758b182ad75a0 100644 --- a/homeassistant/components/proximity/config_flow.py +++ b/homeassistant/components/proximity/config_flow.py @@ -117,12 +117,6 @@ async def async_step_user( data_schema=self._user_form_schema(user_input), ) - async def async_step_import( - self, user_input: dict[str, Any] | None = None - ) -> ConfigFlowResult: - """Import a yaml config entry.""" - return await self.async_step_user(user_input) - class ProximityOptionsFlow(OptionsFlow): """Handle a option flow.""" diff --git a/homeassistant/components/ps4/config_flow.py b/homeassistant/components/ps4/config_flow.py index b842c2f7cfbc26..877fb595fc02e8 100644 --- a/homeassistant/components/ps4/config_flow.py +++ b/homeassistant/components/ps4/config_flow.py @@ -1,13 +1,14 @@ """Config Flow for PlayStation 4.""" from collections import OrderedDict +from typing import Any from pyps4_2ndscreen.errors import CredentialTimeout from pyps4_2ndscreen.helpers import Helper from pyps4_2ndscreen.media_art import COUNTRIES import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import ( CONF_CODE, CONF_HOST, @@ -44,19 +45,21 @@ class PlayStation4FlowHandler(ConfigFlow, domain=DOMAIN): VERSION = CONFIG_ENTRY_VERSION - def __init__(self): + def __init__(self) -> None: """Initialize the config flow.""" self.helper = Helper() - self.creds = None + self.creds: str | None = None self.name = None self.host = None self.region = None - self.pin = None + self.pin: str | None = None self.m_device = None - self.location = None - self.device_list = [] + self.location: location.LocationInfo | None = None + self.device_list: list[str] = [] - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle a user config flow.""" # Check if able to bind to ports: UDP 987, TCP 997. ports = PORT_MSG.keys() @@ -66,7 +69,9 @@ async def async_step_user(self, user_input=None): return self.async_abort(reason=reason) return await self.async_step_creds() - async def async_step_creds(self, user_input=None): + async def async_step_creds( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Return PS4 credentials from 2nd Screen App.""" errors = {} if user_input is not None: @@ -82,7 +87,9 @@ async def async_step_creds(self, user_input=None): return self.async_show_form(step_id="creds", errors=errors) - async def async_step_mode(self, user_input=None): + async def async_step_mode( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Prompt for mode.""" errors = {} mode = [CONF_AUTO, CONF_MANUAL] @@ -97,7 +104,7 @@ async def async_step_mode(self, user_input=None): if not errors: return await self.async_step_link() - mode_schema = OrderedDict() + mode_schema = OrderedDict[vol.Marker, Any]() mode_schema[vol.Required(CONF_MODE, default=CONF_AUTO)] = vol.In(list(mode)) mode_schema[vol.Optional(CONF_IP_ADDRESS)] = str @@ -105,7 +112,9 @@ async def async_step_mode(self, user_input=None): step_id="mode", data_schema=vol.Schema(mode_schema), errors=errors ) - async def async_step_link(self, user_input=None): + async def async_step_link( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Prompt user input. Create or edit entry.""" regions = sorted(COUNTRIES.keys()) default_region = None @@ -190,7 +199,7 @@ async def async_step_link(self, user_input=None): default_region = country # Show User Input form. - link_schema = OrderedDict() + link_schema = OrderedDict[vol.Marker, Any]() link_schema[vol.Required(CONF_IP_ADDRESS)] = vol.In(list(self.device_list)) link_schema[vol.Required(CONF_REGION, default=default_region)] = vol.In( list(regions) diff --git a/homeassistant/components/ps4/icons.json b/homeassistant/components/ps4/icons.json index 8da5909213b0fd..21f8405f8161fb 100644 --- a/homeassistant/components/ps4/icons.json +++ b/homeassistant/components/ps4/icons.json @@ -7,6 +7,8 @@ } }, "services": { - "send_command": "mdi:console" + "send_command": { + "service": "mdi:console" + } } } diff --git a/homeassistant/components/pyload/config_flow.py b/homeassistant/components/pyload/config_flow.py index 2f4f9519d30bbd..79b90ff917d0fe 100644 --- a/homeassistant/components/pyload/config_flow.py +++ b/homeassistant/components/pyload/config_flow.py @@ -133,16 +133,16 @@ async def async_step_user( errors=errors, ) - async def async_step_import(self, import_info: dict[str, Any]) -> ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Import config from yaml.""" config = { - CONF_NAME: import_info.get(CONF_NAME), - CONF_HOST: import_info.get(CONF_HOST, DEFAULT_HOST), - CONF_PASSWORD: import_info.get(CONF_PASSWORD, ""), - CONF_PORT: import_info.get(CONF_PORT, DEFAULT_PORT), - CONF_SSL: import_info.get(CONF_SSL, False), - CONF_USERNAME: import_info.get(CONF_USERNAME, ""), + CONF_NAME: import_data.get(CONF_NAME), + CONF_HOST: import_data.get(CONF_HOST, DEFAULT_HOST), + CONF_PASSWORD: import_data.get(CONF_PASSWORD, ""), + CONF_PORT: import_data.get(CONF_PORT, DEFAULT_PORT), + CONF_SSL: import_data.get(CONF_SSL, False), + CONF_USERNAME: import_data.get(CONF_USERNAME, ""), CONF_VERIFY_SSL: False, } diff --git a/homeassistant/components/python_script/icons.json b/homeassistant/components/python_script/icons.json index a03163179cb5bd..a9829425570a0c 100644 --- a/homeassistant/components/python_script/icons.json +++ b/homeassistant/components/python_script/icons.json @@ -1,5 +1,7 @@ { "services": { - "reload": "mdi:reload" + "reload": { + "service": "mdi:reload" + } } } diff --git a/homeassistant/components/qbittorrent/icons.json b/homeassistant/components/qbittorrent/icons.json index 68fc1020daec74..cede127ebe8b20 100644 --- a/homeassistant/components/qbittorrent/icons.json +++ b/homeassistant/components/qbittorrent/icons.json @@ -10,7 +10,11 @@ } }, "services": { - "get_torrents": "mdi:file-arrow-up-down-outline", - "get_all_torrents": "mdi:file-arrow-up-down-outline" + "get_torrents": { + "service": "mdi:file-arrow-up-down-outline" + }, + "get_all_torrents": { + "service": "mdi:file-arrow-up-down-outline" + } } } diff --git a/homeassistant/components/qvr_pro/icons.json b/homeassistant/components/qvr_pro/icons.json index 556a8d40752bb2..3b57387d2517d2 100644 --- a/homeassistant/components/qvr_pro/icons.json +++ b/homeassistant/components/qvr_pro/icons.json @@ -1,6 +1,10 @@ { "services": { - "start_record": "mdi:record-rec", - "stop_record": "mdi:stop" + "start_record": { + "service": "mdi:record-rec" + }, + "stop_record": { + "service": "mdi:stop" + } } } diff --git a/homeassistant/components/rachio/config_flow.py b/homeassistant/components/rachio/config_flow.py index 77fe20946b40e1..66811091820886 100644 --- a/homeassistant/components/rachio/config_flow.py +++ b/homeassistant/components/rachio/config_flow.py @@ -4,6 +4,7 @@ from http import HTTPStatus import logging +from typing import Any from rachiopy import Rachio from requests.exceptions import ConnectTimeout @@ -67,7 +68,9 @@ class RachioConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" errors = {} if user_input is not None: @@ -115,7 +118,9 @@ def __init__(self, config_entry: ConfigEntry) -> None: """Initialize options flow.""" self.config_entry = config_entry - async def async_step_init(self, user_input=None): + async def async_step_init( + self, user_input: dict[str, int] | None = None + ) -> ConfigFlowResult: """Handle options flow.""" if user_input is not None: return self.async_create_entry(title="", data=user_input) diff --git a/homeassistant/components/rachio/icons.json b/homeassistant/components/rachio/icons.json index dfab8788fc8b00..df30929ab4c2a4 100644 --- a/homeassistant/components/rachio/icons.json +++ b/homeassistant/components/rachio/icons.json @@ -10,11 +10,23 @@ } }, "services": { - "set_zone_moisture_percent": "mdi:water-percent", - "start_multiple_zone_schedule": "mdi:play", - "pause_watering": "mdi:pause", - "resume_watering": "mdi:play", - "stop_watering": "mdi:stop", - "start_watering": "mdi:water" + "set_zone_moisture_percent": { + "service": "mdi:water-percent" + }, + "start_multiple_zone_schedule": { + "service": "mdi:play" + }, + "pause_watering": { + "service": "mdi:pause" + }, + "resume_watering": { + "service": "mdi:play" + }, + "stop_watering": { + "service": "mdi:stop" + }, + "start_watering": { + "service": "mdi:water" + } } } diff --git a/homeassistant/components/radarr/config_flow.py b/homeassistant/components/radarr/config_flow.py index 3bf0796a9a8b8f..c748c63e992d35 100644 --- a/homeassistant/components/radarr/config_flow.py +++ b/homeassistant/components/radarr/config_flow.py @@ -26,7 +26,9 @@ class RadarrConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 entry: RadarrConfigEntry | None = None - async def async_step_reauth(self, _: Mapping[str, Any]) -> ConfigFlowResult: + async def async_step_reauth( + self, entry_data: Mapping[str, Any] + ) -> ConfigFlowResult: """Handle configuration by re-auth.""" self.entry = self.hass.config_entries.async_get_entry(self.context["entry_id"]) diff --git a/homeassistant/components/radiotherm/config_flow.py b/homeassistant/components/radiotherm/config_flow.py index e9904318ae9462..6bcbe11872d7ff 100644 --- a/homeassistant/components/radiotherm/config_flow.py +++ b/homeassistant/components/radiotherm/config_flow.py @@ -60,7 +60,9 @@ async def async_step_dhcp( self.discovered_ip = discovery_info.ip return await self.async_step_confirm() - async def async_step_confirm(self, user_input=None): + async def async_step_confirm( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Attempt to confirm.""" ip_address = self.discovered_ip init_data = self.discovered_init_data diff --git a/homeassistant/components/rainbird/icons.json b/homeassistant/components/rainbird/icons.json index 79d2256f184994..61c09f74e88cae 100644 --- a/homeassistant/components/rainbird/icons.json +++ b/homeassistant/components/rainbird/icons.json @@ -22,7 +22,11 @@ } }, "services": { - "start_irrigation": "mdi:water", - "set_rain_delay": "mdi:water-sync" + "start_irrigation": { + "service": "mdi:water" + }, + "set_rain_delay": { + "service": "mdi:water-sync" + } } } diff --git a/homeassistant/components/rainmachine/icons.json b/homeassistant/components/rainmachine/icons.json index 32988081a18edb..ca85d81346efc1 100644 --- a/homeassistant/components/rainmachine/icons.json +++ b/homeassistant/components/rainmachine/icons.json @@ -70,16 +70,38 @@ } }, "services": { - "pause_watering": "mdi:pause", - "restrict_watering": "mdi:cancel", - "start_program": "mdi:play", - "start_zone": "mdi:play", - "stop_all": "mdi:stop", - "stop_program": "mdi:stop", - "stop_zone": "mdi:stop", - "unpause_watering": "mdi:play-pause", - "push_flow_meter_data": "mdi:database-arrow-up", - "push_weather_data": "mdi:database-arrow-up", - "unrestrict_watering": "mdi:check" + "pause_watering": { + "service": "mdi:pause" + }, + "restrict_watering": { + "service": "mdi:cancel" + }, + "start_program": { + "service": "mdi:play" + }, + "start_zone": { + "service": "mdi:play" + }, + "stop_all": { + "service": "mdi:stop" + }, + "stop_program": { + "service": "mdi:stop" + }, + "stop_zone": { + "service": "mdi:stop" + }, + "unpause_watering": { + "service": "mdi:play-pause" + }, + "push_flow_meter_data": { + "service": "mdi:database-arrow-up" + }, + "push_weather_data": { + "service": "mdi:database-arrow-up" + }, + "unrestrict_watering": { + "service": "mdi:check" + } } } diff --git a/homeassistant/components/recorder/core.py b/homeassistant/components/recorder/core.py index 31c36be9c888cf..002d8937e3abe3 100644 --- a/homeassistant/components/recorder/core.py +++ b/homeassistant/components/recorder/core.py @@ -225,7 +225,6 @@ def __init__( self.event_session: Session | None = None self._get_session: Callable[[], Session] | None = None self._completed_first_database_setup: bool | None = None - self.async_migration_event = asyncio.Event() self.migration_in_progress = False self.migration_is_live = False self.use_legacy_events_index = False @@ -367,13 +366,6 @@ def async_add_executor_job[_T]( """Add an executor job from within the event loop.""" return self.hass.loop.run_in_executor(self._db_executor, target, *args) - def _stop_executor(self) -> None: - """Stop the executor.""" - if self._db_executor is None: - return - self._db_executor.shutdown() - self._db_executor = None - @callback def _async_check_queue(self, *_: Any) -> None: """Periodic check of the queue size to ensure we do not exhaust memory. @@ -941,11 +933,6 @@ def _setup_recorder(self) -> bool: return False - @callback - def _async_migration_started(self) -> None: - """Set the migration started event.""" - self.async_migration_event.set() - def _migrate_schema_offline( self, schema_status: migration.SchemaValidationStatus ) -> tuple[bool, migration.SchemaValidationStatus]: @@ -970,7 +957,6 @@ def _migrate_schema_live( "Database upgrade in progress", "recorder_database_migration", ) - self.hass.add_job(self._async_migration_started) return self._migrate_schema(schema_status, True) def _migrate_schema( @@ -1297,14 +1283,6 @@ def _open_event_session(self) -> None: self.event_session = self.get_session() self.event_session.expire_on_commit = False - def _post_schema_migration(self, old_version: int, new_version: int) -> None: - """Run post schema migration tasks.""" - migration.post_schema_migration(self, old_version, new_version) - - def _post_migrate_entity_ids(self) -> bool: - """Post migrate entity_ids if needed.""" - return migration.post_migrate_entity_ids(self) - def _send_keep_alive(self) -> None: """Send a keep alive to keep the db connection open.""" assert self.event_session is not None @@ -1501,5 +1479,13 @@ def _shutdown(self) -> None: try: self._end_session() finally: - self._stop_executor() + if self._db_executor: + # We shutdown the executor without forcefully + # joining the threads until after we have tried + # to cleanly close the connection. + self._db_executor.shutdown(join_threads_or_timeout=False) self._close_connection() + if self._db_executor: + # After the connection is closed, we can join the threads + # or forcefully shutdown the threads if they take too long. + self._db_executor.join_threads_or_timeout() diff --git a/homeassistant/components/recorder/db_schema.py b/homeassistant/components/recorder/db_schema.py index f84459675ae059..6ba9d971f2c331 100644 --- a/homeassistant/components/recorder/db_schema.py +++ b/homeassistant/components/recorder/db_schema.py @@ -693,13 +693,13 @@ class StatisticsBase: @classmethod def from_stats(cls, metadata_id: int, stats: StatisticData) -> Self: - """Create object from a statistics with datatime objects.""" + """Create object from a statistics with datetime objects.""" return cls( # type: ignore[call-arg] metadata_id=metadata_id, created=None, created_ts=time.time(), start=None, - start_ts=dt_util.utc_to_timestamp(stats["start"]), + start_ts=stats["start"].timestamp(), mean=stats.get("mean"), min=stats.get("min"), max=stats.get("max"), diff --git a/homeassistant/components/recorder/icons.json b/homeassistant/components/recorder/icons.json index 1090401abd598e..9e41637184a2b2 100644 --- a/homeassistant/components/recorder/icons.json +++ b/homeassistant/components/recorder/icons.json @@ -1,8 +1,16 @@ { "services": { - "purge": "mdi:database-sync", - "purge_entities": "mdi:database-sync", - "disable": "mdi:database-off", - "enable": "mdi:database" + "purge": { + "service": "mdi:database-sync" + }, + "purge_entities": { + "service": "mdi:database-sync" + }, + "disable": { + "service": "mdi:database-off" + }, + "enable": { + "service": "mdi:database" + } } } diff --git a/homeassistant/components/recorder/manifest.json b/homeassistant/components/recorder/manifest.json index 7d5576e46720e6..2be4b6862bafba 100644 --- a/homeassistant/components/recorder/manifest.json +++ b/homeassistant/components/recorder/manifest.json @@ -8,7 +8,7 @@ "quality_scale": "internal", "requirements": [ "SQLAlchemy==2.0.31", - "fnv-hash-fast==0.5.0", + "fnv-hash-fast==1.0.2", "psutil-home-assistant==0.0.1" ] } diff --git a/homeassistant/components/recorder/migration.py b/homeassistant/components/recorder/migration.py index a4a5fa874668eb..4d9978c641b170 100644 --- a/homeassistant/components/recorder/migration.py +++ b/homeassistant/components/recorder/migration.py @@ -99,19 +99,14 @@ migrate_single_short_term_statistics_row_to_timestamp, migrate_single_statistics_row_to_timestamp, ) -from .statistics import get_start_time -from .tasks import ( - CommitTask, - EntityIDPostMigrationTask, - PostSchemaMigrationTask, - RecorderTask, - StatisticsTimestampMigrationCleanupTask, -) +from .statistics import cleanup_statistics_timestamp_migration, get_start_time +from .tasks import RecorderTask from .util import ( database_job_retry_wrapper, execute_stmt_lambda_element, get_index_by_name, retryable_database_job, + retryable_database_job_method, session_scope, ) @@ -128,6 +123,11 @@ "Home Assistant will not start until the upgrade is completed. Please be patient " "and do not turn off or restart Home Assistant while the upgrade is in progress!" ) +MIGRATION_NOTE_MINUTES = ( + "Note: this may take several minutes on large databases and slow machines. " + "Please be patient!" +) +MIGRATION_NOTE_WHILE = "This will take a while; please be patient!" _EMPTY_ENTITY_ID = "missing.entity_id" _EMPTY_EVENT_TYPE = "missing_event_type" @@ -345,13 +345,6 @@ def migrate_schema_live( states_correct_db_schema(instance, schema_errors) events_correct_db_schema(instance, schema_errors) - start_version = schema_status.start_version - if start_version != SCHEMA_VERSION: - instance.queue_task(PostSchemaMigrationTask(start_version, SCHEMA_VERSION)) - # Make sure the post schema migration task is committed in case - # the next task does not have commit_before = True - instance.queue_task(CommitTask()) - return schema_status @@ -373,11 +366,10 @@ def _create_index( index = index_list[0] _LOGGER.debug("Creating %s index", index_name) _LOGGER.warning( - "Adding index `%s` to table `%s`. Note: this can take several " - "minutes on large databases and slow machines. Please " - "be patient!", + "Adding index `%s` to table `%s`. %s", index_name, table_name, + MIGRATION_NOTE_MINUTES, ) with session_scope(session=session_maker()) as session: try: @@ -422,11 +414,10 @@ def _drop_index( DO NOT USE THIS FUNCTION IN ANY OPERATION THAT TAKES USER INPUT. """ _LOGGER.warning( - "Dropping index `%s` from table `%s`. Note: this can take several " - "minutes on large databases and slow machines. Please " - "be patient!", + "Dropping index `%s` from table `%s`. %s", index_name, table_name, + MIGRATION_NOTE_MINUTES, ) index_to_drop: str | None = None with session_scope(session=session_maker()) as session: @@ -472,13 +463,10 @@ def _add_columns( ) -> None: """Add columns to a table.""" _LOGGER.warning( - ( - "Adding columns %s to table %s. Note: this can take several " - "minutes on large databases and slow machines. Please " - "be patient!" - ), + "Adding columns %s to table %s. %s", ", ".join(column.split(" ")[0] for column in columns_def), table_name, + MIGRATION_NOTE_MINUTES, ) columns_def = [f"ADD {col_def}" for col_def in columns_def] @@ -534,13 +522,10 @@ def _modify_columns( return _LOGGER.warning( - ( - "Modifying columns %s in table %s. Note: this can take several " - "minutes on large databases and slow machines. Please " - "be patient!" - ), + "Modifying columns %s in table %s. %s", ", ".join(column.split(" ")[0] for column in columns_def), table_name, + MIGRATION_NOTE_MINUTES, ) if engine.dialect.name == SupportedDialect.POSTGRESQL: @@ -695,6 +680,18 @@ def _restore_foreign_key_constraints( _LOGGER.info("Did not find a matching constraint for %s.%s", table, column) continue + inspector = sqlalchemy.inspect(engine) + if any( + foreign_key["name"] and foreign_key["constrained_columns"] == [column] + for foreign_key in inspector.get_foreign_keys(table) + ): + _LOGGER.info( + "The database already has a matching constraint for %s.%s", + table, + column, + ) + continue + if TYPE_CHECKING: assert foreign_table is not None assert foreign_column is not None @@ -1405,6 +1402,12 @@ def _apply_update(self) -> None: _drop_index(self.session_maker, "events", "ix_events_event_type_time_fired") _drop_index(self.session_maker, "states", "ix_states_last_updated") _drop_index(self.session_maker, "events", "ix_events_time_fired") + with session_scope(session=self.session_maker()) as session: + # In version 31 we migrated all the time_fired, last_updated, and last_changed + # columns to be timestamps. In version 32 we need to wipe the old columns + # since they are no longer used and take up a significant amount of space. + assert self.instance.engine is not None, "engine should never be None" + _wipe_old_string_time_columns(self.instance, self.instance.engine, session) class _SchemaVersion33Migrator(_SchemaVersionMigrator, target_version=33): @@ -1483,6 +1486,12 @@ def _apply_update(self) -> None: # ix_statistics_start and ix_statistics_statistic_id_start are still used # for the post migration cleanup and can be removed in a future version. + # In version 34 we migrated all the created, start, and last_reset + # columns to be timestamps. In version 35 we need to wipe the old columns + # since they are no longer used and take up a significant amount of space. + while not cleanup_statistics_timestamp_migration(self.instance): + pass + class _SchemaVersion36Migrator(_SchemaVersionMigrator, target_version=36): def _apply_update(self) -> None: @@ -1769,10 +1778,9 @@ def _migrate_statistics_columns_to_timestamp_removing_duplicates( except IntegrityError as ex: _LOGGER.error( "Statistics table contains duplicate entries: %s; " - "Cleaning up duplicates and trying again; " - "This will take a while; " - "Please be patient!", + "Cleaning up duplicates and trying again; %s", ex, + MIGRATION_NOTE_WHILE, ) # There may be duplicated statistics entries, delete duplicates # and try again @@ -1800,10 +1808,9 @@ def _correct_table_character_set_and_collation( """Correct issues detected by validate_db_schema.""" # Attempt to convert the table to utf8mb4 _LOGGER.warning( - "Updating character set and collation of table %s to utf8mb4. " - "Note: this can take several minutes on large databases and slow " - "machines. Please be patient!", + "Updating character set and collation of table %s to utf8mb4. %s", table, + MIGRATION_NOTE_MINUTES, ) with ( contextlib.suppress(SQLAlchemyError), @@ -1821,40 +1828,6 @@ def _correct_table_character_set_and_collation( ) -def post_schema_migration( - instance: Recorder, - old_version: int, - new_version: int, -) -> None: - """Post schema migration. - - Run any housekeeping tasks after the schema migration has completed. - - Post schema migration is run after the schema migration has completed - and the queue has been processed to ensure that we reduce the memory - pressure since events are held in memory until the queue is processed - which is blocked from being processed until the schema migration is - complete. - """ - if old_version < 32 <= new_version: - # In version 31 we migrated all the time_fired, last_updated, and last_changed - # columns to be timestamps. In version 32 we need to wipe the old columns - # since they are no longer used and take up a significant amount of space. - assert instance.event_session is not None - assert instance.engine is not None - _wipe_old_string_time_columns(instance, instance.engine, instance.event_session) - if old_version < 35 <= new_version: - # In version 34 we migrated all the created, start, and last_reset - # columns to be timestamps. In version 35 we need to wipe the old columns - # since they are no longer used and take up a significant amount of space. - _wipe_old_string_statistics_columns(instance) - - -def _wipe_old_string_statistics_columns(instance: Recorder) -> None: - """Wipe old string statistics columns to save space.""" - instance.queue_task(StatisticsTimestampMigrationCleanupTask()) - - @database_job_retry_wrapper("Wipe old string time columns", 3) def _wipe_old_string_time_columns( instance: Recorder, engine: Engine, session: Session @@ -2165,50 +2138,6 @@ def post_migrate_entity_ids(instance: Recorder) -> bool: return is_done -@retryable_database_job("cleanup_legacy_event_ids") -def cleanup_legacy_states_event_ids(instance: Recorder) -> bool: - """Remove old event_id index from states. - - We used to link states to events using the event_id column but we no - longer store state changed events in the events table. - - If all old states have been purged and existing states are in the new - format we can drop the index since it can take up ~10MB per 1M rows. - """ - session_maker = instance.get_session - _LOGGER.debug("Cleanup legacy entity_ids") - with session_scope(session=session_maker()) as session: - result = session.execute(has_used_states_event_ids()).scalar() - # In the future we may migrate existing states to the new format - # but in practice very few of these still exist in production and - # removing the index is the likely all that needs to happen. - all_gone = not result - - if all_gone: - # Only drop the index if there are no more event_ids in the states table - # ex all NULL - assert instance.engine is not None, "engine should never be None" - if instance.dialect_name == SupportedDialect.SQLITE: - # SQLite does not support dropping foreign key constraints - # so we have to rebuild the table - fk_remove_ok = rebuild_sqlite_table(session_maker, instance.engine, States) - else: - try: - _drop_foreign_key_constraints( - session_maker, instance.engine, TABLE_STATES, "event_id" - ) - except (InternalError, OperationalError): - fk_remove_ok = False - else: - fk_remove_ok = True - if fk_remove_ok: - _drop_index(session_maker, "states", LEGACY_STATES_EVENT_ID_INDEX) - instance.use_legacy_events_index = False - _mark_migration_done(session, EventIDPostMigration) - - return True - - def _initialize_database(session: Session) -> bool: """Initialize a new database. @@ -2263,8 +2192,6 @@ def run(self, instance: Recorder) -> None: if not self.migrator.migrate_data(instance): # Schedule a new migration task if this one didn't finish instance.queue_task(MigrationTask(self.migrator)) - else: - self.migrator.migration_done(instance, None) @dataclass(slots=True) @@ -2275,8 +2202,8 @@ class CommitBeforeMigrationTask(MigrationTask): @dataclass(frozen=True, kw_only=True) -class NeedsMigrateResult: - """Container for the return value of BaseRunTimeMigration.needs_migrate_impl.""" +class DataMigrationStatus: + """Container for data migrator status.""" needs_migrate: bool migration_done: bool @@ -2285,6 +2212,7 @@ class NeedsMigrateResult: class BaseRunTimeMigration(ABC): """Base class for run time migrations.""" + index_to_drop: tuple[str, str] | None = None required_schema_version = 0 migration_version = 1 migration_id: str @@ -2302,18 +2230,30 @@ def do_migrate(self, instance: Recorder, session: Session) -> None: else: self.migration_done(instance, session) - @staticmethod - @abstractmethod - def migrate_data(instance: Recorder) -> bool: + @retryable_database_job_method("migrate data") + def migrate_data(self, instance: Recorder) -> bool: """Migrate some data, returns True if migration is completed.""" + status = self.migrate_data_impl(instance) + if status.migration_done: + if self.index_to_drop is not None: + table, index = self.index_to_drop + _drop_index(instance.get_session, table, index) + with session_scope(session=instance.get_session()) as session: + self.migration_done(instance, session) + _mark_migration_done(session, self.__class__) + return not status.needs_migrate + + @abstractmethod + def migrate_data_impl(self, instance: Recorder) -> DataMigrationStatus: + """Migrate some data, return if the migration needs to run and if it is done.""" - def migration_done(self, instance: Recorder, session: Session | None) -> None: + def migration_done(self, instance: Recorder, session: Session) -> None: """Will be called after migrate returns True or if migration is not needed.""" @abstractmethod def needs_migrate_impl( self, instance: Recorder, session: Session - ) -> NeedsMigrateResult: + ) -> DataMigrationStatus: """Return if the migration needs to run and if it is done.""" def needs_migrate(self, instance: Recorder, session: Session) -> bool: @@ -2332,8 +2272,14 @@ def needs_migrate(self, instance: Recorder, session: Session) -> bool: # The migration changes table indicates that the migration has been done return False # We do not know if the migration is done from the - # migration changes table so we must check the data + # migration changes table so we must check the index and data # This is the slow path + if ( + self.index_to_drop is not None + and get_index_by_name(session, self.index_to_drop[0], self.index_to_drop[1]) + is not None + ): + return True needs_migrate = self.needs_migrate_impl(instance, session) if needs_migrate.migration_done: _mark_migration_done(session, self.__class__) @@ -2349,10 +2295,10 @@ def needs_migrate_query(self) -> StatementLambdaElement: def needs_migrate_impl( self, instance: Recorder, session: Session - ) -> NeedsMigrateResult: + ) -> DataMigrationStatus: """Return if the migration needs to run.""" needs_migrate = execute_stmt_lambda_element(session, self.needs_migrate_query()) - return NeedsMigrateResult( + return DataMigrationStatus( needs_migrate=bool(needs_migrate), migration_done=not needs_migrate ) @@ -2362,10 +2308,9 @@ class StatesContextIDMigration(BaseRunTimeMigrationWithQuery): required_schema_version = CONTEXT_ID_AS_BINARY_SCHEMA_VERSION migration_id = "state_context_id_as_binary" + index_to_drop = ("states", "ix_states_context_id") - @staticmethod - @retryable_database_job("migrate states context_ids to binary format") - def migrate_data(instance: Recorder) -> bool: + def migrate_data_impl(self, instance: Recorder) -> DataMigrationStatus: """Migrate states context_ids to use binary format, return True if completed.""" _to_bytes = _context_id_to_bytes session_maker = instance.get_session @@ -2390,16 +2335,10 @@ def migrate_data(instance: Recorder) -> bool: for state_id, last_updated_ts, context_id, context_user_id, context_parent_id in states ], ) - # If there is more work to do return False - # so that we can be called again - if is_done := not states: - _mark_migration_done(session, StatesContextIDMigration) - - if is_done: - _drop_index(session_maker, "states", "ix_states_context_id") + is_done = not states _LOGGER.debug("Migrating states context_ids to binary format: done=%s", is_done) - return is_done + return DataMigrationStatus(needs_migrate=not is_done, migration_done=is_done) def needs_migrate_query(self) -> StatementLambdaElement: """Return the query to check if the migration needs to run.""" @@ -2411,10 +2350,9 @@ class EventsContextIDMigration(BaseRunTimeMigrationWithQuery): required_schema_version = CONTEXT_ID_AS_BINARY_SCHEMA_VERSION migration_id = "event_context_id_as_binary" + index_to_drop = ("events", "ix_events_context_id") - @staticmethod - @retryable_database_job("migrate events context_ids to binary format") - def migrate_data(instance: Recorder) -> bool: + def migrate_data_impl(self, instance: Recorder) -> DataMigrationStatus: """Migrate events context_ids to use binary format, return True if completed.""" _to_bytes = _context_id_to_bytes session_maker = instance.get_session @@ -2439,16 +2377,10 @@ def migrate_data(instance: Recorder) -> bool: for event_id, time_fired_ts, context_id, context_user_id, context_parent_id in events ], ) - # If there is more work to do return False - # so that we can be called again - if is_done := not events: - _mark_migration_done(session, EventsContextIDMigration) - - if is_done: - _drop_index(session_maker, "events", "ix_events_context_id") + is_done = not events _LOGGER.debug("Migrating events context_ids to binary format: done=%s", is_done) - return is_done + return DataMigrationStatus(needs_migrate=not is_done, migration_done=is_done) def needs_migrate_query(self) -> StatementLambdaElement: """Return the query to check if the migration needs to run.""" @@ -2465,9 +2397,7 @@ class EventTypeIDMigration(BaseRunTimeMigrationWithQuery): # no new pending event_types about to be added to # the db since this happens live - @staticmethod - @retryable_database_job("migrate events event_types to event_type_ids") - def migrate_data(instance: Recorder) -> bool: + def migrate_data_impl(self, instance: Recorder) -> DataMigrationStatus: """Migrate event_type to event_type_ids, return True if completed.""" session_maker = instance.get_session _LOGGER.debug("Migrating event_types") @@ -2520,15 +2450,12 @@ def migrate_data(instance: Recorder) -> bool: ], ) - # If there is more work to do return False - # so that we can be called again - if is_done := not events: - _mark_migration_done(session, EventTypeIDMigration) + is_done = not events _LOGGER.debug("Migrating event_types done=%s", is_done) - return is_done + return DataMigrationStatus(needs_migrate=not is_done, migration_done=is_done) - def migration_done(self, instance: Recorder, session: Session | None) -> None: + def migration_done(self, instance: Recorder, session: Session) -> None: """Will be called after migrate returns True.""" _LOGGER.debug("Activating event_types manager as all data is migrated") instance.event_type_manager.active = True @@ -2548,9 +2475,7 @@ class EntityIDMigration(BaseRunTimeMigrationWithQuery): # no new pending states_meta about to be added to # the db since this happens live - @staticmethod - @retryable_database_job("migrate states entity_ids to states_meta") - def migrate_data(instance: Recorder) -> bool: + def migrate_data_impl(self, instance: Recorder) -> DataMigrationStatus: """Migrate entity_ids to states_meta, return True if completed. We do this in two steps because we need the history queries to work @@ -2613,15 +2538,12 @@ def migrate_data(instance: Recorder) -> bool: ], ) - # If there is more work to do return False - # so that we can be called again - if is_done := not states: - _mark_migration_done(session, EntityIDMigration) + is_done = not states _LOGGER.debug("Migrating entity_ids done=%s", is_done) - return is_done + return DataMigrationStatus(needs_migrate=not is_done, migration_done=is_done) - def migration_done(self, instance: Recorder, _session: Session | None) -> None: + def migration_done(self, instance: Recorder, session: Session) -> None: """Will be called after migrate returns True.""" # The migration has finished, now we start the post migration # to remove the old entity_id data from the states table @@ -2629,15 +2551,7 @@ def migration_done(self, instance: Recorder, _session: Session | None) -> None: # so we set active to True _LOGGER.debug("Activating states_meta manager as all data is migrated") instance.states_meta_manager.active = True - session_generator = ( - contextlib.nullcontext(_session) - if _session - else session_scope(session=instance.get_session()) - ) - with ( - contextlib.suppress(SQLAlchemyError), - session_generator as session, - ): + with contextlib.suppress(SQLAlchemyError): # If ix_states_entity_id_last_updated_ts still exists # on the states table it means the entity id migration # finished by the EntityIDPostMigrationTask did not @@ -2662,10 +2576,48 @@ class EventIDPostMigration(BaseRunTimeMigration): task = MigrationTask migration_version = 2 - @staticmethod - def migrate_data(instance: Recorder) -> bool: - """Migrate some data, returns True if migration is completed.""" - return cleanup_legacy_states_event_ids(instance) + def migrate_data_impl(self, instance: Recorder) -> DataMigrationStatus: + """Remove old event_id index from states, returns True if completed. + + We used to link states to events using the event_id column but we no + longer store state changed events in the events table. + + If all old states have been purged and existing states are in the new + format we can drop the index since it can take up ~10MB per 1M rows. + """ + session_maker = instance.get_session + _LOGGER.debug("Cleanup legacy entity_ids") + with session_scope(session=session_maker()) as session: + result = session.execute(has_used_states_event_ids()).scalar() + # In the future we may migrate existing states to the new format + # but in practice very few of these still exist in production and + # removing the index is the likely all that needs to happen. + all_gone = not result + + if all_gone: + # Only drop the index if there are no more event_ids in the states table + # ex all NULL + assert instance.engine is not None, "engine should never be None" + if instance.dialect_name == SupportedDialect.SQLITE: + # SQLite does not support dropping foreign key constraints + # so we have to rebuild the table + fk_remove_ok = rebuild_sqlite_table( + session_maker, instance.engine, States + ) + else: + try: + _drop_foreign_key_constraints( + session_maker, instance.engine, TABLE_STATES, "event_id" + ) + except (InternalError, OperationalError): + fk_remove_ok = False + else: + fk_remove_ok = True + if fk_remove_ok: + _drop_index(session_maker, "states", LEGACY_STATES_EVENT_ID_INDEX) + instance.use_legacy_events_index = False + + return DataMigrationStatus(needs_migrate=False, migration_done=fk_remove_ok) @staticmethod def _legacy_event_id_foreign_key_exists(instance: Recorder) -> bool: @@ -2686,16 +2638,27 @@ def _legacy_event_id_foreign_key_exists(instance: Recorder) -> bool: def needs_migrate_impl( self, instance: Recorder, session: Session - ) -> NeedsMigrateResult: + ) -> DataMigrationStatus: """Return if the migration needs to run.""" if self.schema_version <= LEGACY_STATES_EVENT_ID_INDEX_SCHEMA_VERSION: - return NeedsMigrateResult(needs_migrate=False, migration_done=False) + return DataMigrationStatus(needs_migrate=False, migration_done=False) if get_index_by_name( session, TABLE_STATES, LEGACY_STATES_EVENT_ID_INDEX ) is not None or self._legacy_event_id_foreign_key_exists(instance): instance.use_legacy_events_index = True - return NeedsMigrateResult(needs_migrate=True, migration_done=False) - return NeedsMigrateResult(needs_migrate=False, migration_done=True) + return DataMigrationStatus(needs_migrate=True, migration_done=False) + return DataMigrationStatus(needs_migrate=False, migration_done=True) + + +@dataclass(slots=True) +class EntityIDPostMigrationTask(RecorderTask): + """An object to insert into the recorder queue to cleanup after entity_ids migration.""" + + def run(self, instance: Recorder) -> None: + """Run entity_id post migration task.""" + if not post_migrate_entity_ids(instance): + # Schedule a new migration task if this one didn't finish + instance.queue_task(EntityIDPostMigrationTask()) def _mark_migration_done( @@ -2724,10 +2687,7 @@ def rebuild_sqlite_table( orig_name = table_table.name temp_name = f"{table_table.name}_temp_{int(time())}" - _LOGGER.warning( - "Rebuilding SQLite table %s; This will take a while; Please be patient!", - orig_name, - ) + _LOGGER.warning("Rebuilding SQLite table %s; %s", orig_name, MIGRATION_NOTE_WHILE) try: # 12 step SQLite table rebuild diff --git a/homeassistant/components/recorder/models/time.py b/homeassistant/components/recorder/models/time.py index 6295060c8d35dc..8f0f89a9ffad25 100644 --- a/homeassistant/components/recorder/models/time.py +++ b/homeassistant/components/recorder/models/time.py @@ -65,9 +65,7 @@ def process_datetime_to_timestamp(ts: datetime) -> float: def datetime_to_timestamp_or_none(dt: datetime | None) -> float | None: """Convert a datetime to a timestamp.""" - if dt is None: - return None - return dt_util.utc_to_timestamp(dt) + return None if dt is None else dt.timestamp() def timestamp_to_datetime_or_none(ts: float | None) -> datetime | None: diff --git a/homeassistant/components/recorder/statistics.py b/homeassistant/components/recorder/statistics.py index aeeb30816d728d..ba19c016d19d3c 100644 --- a/homeassistant/components/recorder/statistics.py +++ b/homeassistant/components/recorder/statistics.py @@ -148,6 +148,12 @@ **{unit: VolumeFlowRateConverter for unit in VolumeFlowRateConverter.VALID_UNITS}, } + +UNIT_CLASSES = { + unit: converter.UNIT_CLASS + for unit, converter in STATISTIC_UNIT_TO_UNIT_CONVERTER.items() +} + DATA_SHORT_TERM_STATISTICS_RUN_CACHE = "recorder_short_term_statistics_run_cache" @@ -211,13 +217,6 @@ class StatisticsRow(BaseStatisticsRow, total=False): change: float | None -def _get_unit_class(unit: str | None) -> str | None: - """Get corresponding unit class from from the statistics unit.""" - if converter := STATISTIC_UNIT_TO_UNIT_CONVERTER.get(unit): - return converter.UNIT_CLASS - return None - - def get_display_unit( hass: HomeAssistant, statistic_id: str, @@ -807,7 +806,7 @@ def _statistic_by_id_from_metadata( "has_sum": meta["has_sum"], "name": meta["name"], "source": meta["source"], - "unit_class": _get_unit_class(meta["unit_of_measurement"]), + "unit_class": UNIT_CLASSES.get(meta["unit_of_measurement"]), "unit_of_measurement": meta["unit_of_measurement"], } for _, meta in metadata.values() @@ -881,7 +880,7 @@ def list_statistic_ids( "has_sum": meta["has_sum"], "name": meta["name"], "source": meta["source"], - "unit_class": _get_unit_class(meta["unit_of_measurement"]), + "unit_class": UNIT_CLASSES.get(meta["unit_of_measurement"]), "unit_of_measurement": meta["unit_of_measurement"], } @@ -2089,71 +2088,38 @@ def _build_stats( db_rows: list[Row], table_duration_seconds: float, start_ts_idx: int, - mean_idx: int | None, - min_idx: int | None, - max_idx: int | None, - last_reset_ts_idx: int | None, - state_idx: int | None, - sum_idx: int | None, + row_mapping: tuple[tuple[str, int], ...], ) -> list[StatisticsRow]: """Build a list of statistics without unit conversion.""" - result: list[StatisticsRow] = [] - ent_results_append = result.append - for db_row in db_rows: - row: StatisticsRow = { + return [ + { "start": (start_ts := db_row[start_ts_idx]), "end": start_ts + table_duration_seconds, + **{key: db_row[idx] for key, idx in row_mapping}, # type: ignore[typeddict-item] } - if last_reset_ts_idx is not None: - row["last_reset"] = db_row[last_reset_ts_idx] - if mean_idx is not None: - row["mean"] = db_row[mean_idx] - if min_idx is not None: - row["min"] = db_row[min_idx] - if max_idx is not None: - row["max"] = db_row[max_idx] - if state_idx is not None: - row["state"] = db_row[state_idx] - if sum_idx is not None: - row["sum"] = db_row[sum_idx] - ent_results_append(row) - return result + for db_row in db_rows + ] def _build_converted_stats( db_rows: list[Row], table_duration_seconds: float, start_ts_idx: int, - mean_idx: int | None, - min_idx: int | None, - max_idx: int | None, - last_reset_ts_idx: int | None, - state_idx: int | None, - sum_idx: int | None, + row_mapping: tuple[tuple[str, int], ...], convert: Callable[[float | None], float | None] | Callable[[float], float], ) -> list[StatisticsRow]: """Build a list of statistics with unit conversion.""" - result: list[StatisticsRow] = [] - ent_results_append = result.append - for db_row in db_rows: - row: StatisticsRow = { + return [ + { "start": (start_ts := db_row[start_ts_idx]), "end": start_ts + table_duration_seconds, + **{ + key: None if (v := db_row[idx]) is None else convert(v) # type: ignore[typeddict-item] + for key, idx in row_mapping + }, } - if last_reset_ts_idx is not None: - row["last_reset"] = db_row[last_reset_ts_idx] - if mean_idx is not None: - row["mean"] = None if (v := db_row[mean_idx]) is None else convert(v) - if min_idx is not None: - row["min"] = None if (v := db_row[min_idx]) is None else convert(v) - if max_idx is not None: - row["max"] = None if (v := db_row[max_idx]) is None else convert(v) - if state_idx is not None: - row["state"] = None if (v := db_row[state_idx]) is None else convert(v) - if sum_idx is not None: - row["sum"] = None if (v := db_row[sum_idx]) is None else convert(v) - ent_results_append(row) - return result + for db_row in db_rows + ] def _sorted_statistics_to_dict( @@ -2193,14 +2159,11 @@ def _sorted_statistics_to_dict( # Figure out which fields we need to extract from the SQL result # and which indices they have in the result so we can avoid the overhead # of doing a dict lookup for each row - mean_idx = field_map["mean"] if "mean" in types else None - min_idx = field_map["min"] if "min" in types else None - max_idx = field_map["max"] if "max" in types else None - last_reset_ts_idx = field_map["last_reset_ts"] if "last_reset" in types else None - state_idx = field_map["state"] if "state" in types else None + if "last_reset_ts" in field_map: + field_map["last_reset"] = field_map.pop("last_reset_ts") sum_idx = field_map["sum"] if "sum" in types else None sum_only = len(types) == 1 and sum_idx is not None - row_idxes = (mean_idx, min_idx, max_idx, last_reset_ts_idx, state_idx, sum_idx) + row_mapping = tuple((key, field_map[key]) for key in types if key in field_map) # Append all statistic entries, and optionally do unit conversion table_duration_seconds = table.duration.total_seconds() for meta_id, db_rows in stats_by_meta_id.items(): @@ -2229,9 +2192,9 @@ def _sorted_statistics_to_dict( else: _stats = _build_sum_stats(*build_args, sum_idx) elif convert: - _stats = _build_converted_stats(*build_args, *row_idxes, convert) + _stats = _build_converted_stats(*build_args, row_mapping, convert) else: - _stats = _build_stats(*build_args, *row_idxes) + _stats = _build_stats(*build_args, row_mapping) result[statistic_id] = _stats diff --git a/homeassistant/components/recorder/table_managers/statistics_meta.py b/homeassistant/components/recorder/table_managers/statistics_meta.py index 9b33eff0c9bbb2..77fc34518db4dc 100644 --- a/homeassistant/components/recorder/table_managers/statistics_meta.py +++ b/homeassistant/components/recorder/table_managers/statistics_meta.py @@ -4,7 +4,7 @@ import logging import threading -from typing import TYPE_CHECKING, Literal, cast +from typing import TYPE_CHECKING, Final, Literal from lru import LRU from sqlalchemy import lambda_stmt, select @@ -33,6 +33,14 @@ StatisticsMeta.name, ) +INDEX_ID: Final = 0 +INDEX_STATISTIC_ID: Final = 1 +INDEX_SOURCE: Final = 2 +INDEX_UNIT_OF_MEASUREMENT: Final = 3 +INDEX_HAS_MEAN: Final = 4 +INDEX_HAS_SUM: Final = 5 +INDEX_NAME: Final = 6 + def _generate_get_metadata_stmt( statistic_ids: set[str] | None = None, @@ -52,23 +60,6 @@ def _generate_get_metadata_stmt( return stmt -def _statistics_meta_to_id_statistics_metadata( - meta: StatisticsMeta, -) -> tuple[int, StatisticMetaData]: - """Convert StatisticsMeta tuple of metadata_id and StatisticMetaData.""" - return ( - meta.id, - { - "has_mean": meta.has_mean, # type: ignore[typeddict-item] - "has_sum": meta.has_sum, # type: ignore[typeddict-item] - "name": meta.name, - "source": meta.source, # type: ignore[typeddict-item] - "statistic_id": meta.statistic_id, # type: ignore[typeddict-item] - "unit_of_measurement": meta.unit_of_measurement, - }, - ) - - class StatisticsMetaManager: """Manage the StatisticsMeta table.""" @@ -100,6 +91,10 @@ def _get_from_database( and self.recorder.thread_id == threading.get_ident() ) results: dict[str, tuple[int, StatisticMetaData]] = {} + id_meta: tuple[int, StatisticMetaData] + meta: StatisticMetaData + statistic_id: str + row_id: int with session.no_autoflush: stat_id_to_id_meta = self._stat_id_to_id_meta for row in execute_stmt_lambda_element( @@ -109,10 +104,17 @@ def _get_from_database( ), orm_rows=False, ): - statistics_meta = cast(StatisticsMeta, row) - id_meta = _statistics_meta_to_id_statistics_metadata(statistics_meta) - - statistic_id = cast(str, statistics_meta.statistic_id) + statistic_id = row[INDEX_STATISTIC_ID] + row_id = row[INDEX_ID] + meta = { + "has_mean": row[INDEX_HAS_MEAN], + "has_sum": row[INDEX_HAS_SUM], + "name": row[INDEX_NAME], + "source": row[INDEX_SOURCE], + "statistic_id": statistic_id, + "unit_of_measurement": row[INDEX_UNIT_OF_MEASUREMENT], + } + id_meta = (row_id, meta) results[statistic_id] = id_meta if update_cache: stat_id_to_id_meta[statistic_id] = id_meta diff --git a/homeassistant/components/recorder/tasks.py b/homeassistant/components/recorder/tasks.py index 46e529d4909dbb..2529e8012bff55 100644 --- a/homeassistant/components/recorder/tasks.py +++ b/homeassistant/components/recorder/tasks.py @@ -322,31 +322,6 @@ def run(self, instance: Recorder) -> None: instance.hass.loop.call_soon_threadsafe(self.event.set) -@dataclass(slots=True) -class PostSchemaMigrationTask(RecorderTask): - """Post migration task to update schema.""" - - old_version: int - new_version: int - - def run(self, instance: Recorder) -> None: - """Handle the task.""" - instance._post_schema_migration( # noqa: SLF001 - self.old_version, self.new_version - ) - - -@dataclass(slots=True) -class StatisticsTimestampMigrationCleanupTask(RecorderTask): - """An object to insert into the recorder queue to run a statistics migration cleanup task.""" - - def run(self, instance: Recorder) -> None: - """Run statistics timestamp cleanup task.""" - if not statistics.cleanup_statistics_timestamp_migration(instance): - # Schedule a new statistics migration task if this one didn't finish - instance.queue_task(StatisticsTimestampMigrationCleanupTask()) - - @dataclass(slots=True) class AdjustLRUSizeTask(RecorderTask): """An object to insert into the recorder queue to adjust the LRU size.""" @@ -358,19 +333,6 @@ def run(self, instance: Recorder) -> None: instance._adjust_lru_size() # noqa: SLF001 -@dataclass(slots=True) -class EntityIDPostMigrationTask(RecorderTask): - """An object to insert into the recorder queue to cleanup after entity_ids migration.""" - - def run(self, instance: Recorder) -> None: - """Run entity_id post migration task.""" - if ( - not instance._post_migrate_entity_ids() # noqa: SLF001 - ): - # Schedule a new migration task if this one didn't finish - instance.queue_task(EntityIDPostMigrationTask()) - - @dataclass(slots=True) class RefreshEventTypesTask(RecorderTask): """An object to insert into the recorder queue to refresh event types.""" diff --git a/homeassistant/components/recorder/util.py b/homeassistant/components/recorder/util.py index 4d494aed7d541a..d078c32cb88cab 100644 --- a/homeassistant/components/recorder/util.py +++ b/homeassistant/components/recorder/util.py @@ -644,47 +644,70 @@ def _is_retryable_error(instance: Recorder, err: OperationalError) -> bool: ) -type _FuncType[_T, **_P, _R] = Callable[Concatenate[_T, _P], _R] +type _FuncType[**P, R] = Callable[Concatenate[Recorder, P], R] +type _MethType[Self, **P, R] = Callable[Concatenate[Self, Recorder, P], R] +type _FuncOrMethType[**_P, _R] = Callable[_P, _R] -def retryable_database_job[_RecorderT: Recorder, **_P]( +def retryable_database_job[**_P]( description: str, -) -> Callable[[_FuncType[_RecorderT, _P, bool]], _FuncType[_RecorderT, _P, bool]]: +) -> Callable[[_FuncType[_P, bool]], _FuncType[_P, bool]]: """Try to execute a database job. The job should return True if it finished, and False if it needs to be rescheduled. """ - def decorator( - job: _FuncType[_RecorderT, _P, bool], - ) -> _FuncType[_RecorderT, _P, bool]: - @functools.wraps(job) - def wrapper(instance: _RecorderT, *args: _P.args, **kwargs: _P.kwargs) -> bool: - try: - return job(instance, *args, **kwargs) - except OperationalError as err: - if _is_retryable_error(instance, err): - assert isinstance(err.orig, BaseException) # noqa: PT017 - _LOGGER.info( - "%s; %s not completed, retrying", err.orig.args[1], description - ) - time.sleep(instance.db_retry_wait) - # Failed with retryable error - return False + def decorator(job: _FuncType[_P, bool]) -> _FuncType[_P, bool]: + return _wrap_func_or_meth(job, description, False) - _LOGGER.warning("Error executing %s: %s", description, err) + return decorator - # Failed with permanent error - return True - return wrapper +def retryable_database_job_method[_Self, **_P]( + description: str, +) -> Callable[[_MethType[_Self, _P, bool]], _MethType[_Self, _P, bool]]: + """Try to execute a database job. + + The job should return True if it finished, and False if it needs to be rescheduled. + """ + + def decorator(job: _MethType[_Self, _P, bool]) -> _MethType[_Self, _P, bool]: + return _wrap_func_or_meth(job, description, True) return decorator -def database_job_retry_wrapper[_RecorderT: Recorder, **_P]( +def _wrap_func_or_meth[**_P]( + job: _FuncOrMethType[_P, bool], description: str, method: bool +) -> _FuncOrMethType[_P, bool]: + recorder_pos = 1 if method else 0 + + @functools.wraps(job) + def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> bool: + instance: Recorder = args[recorder_pos] # type: ignore[assignment] + try: + return job(*args, **kwargs) + except OperationalError as err: + if _is_retryable_error(instance, err): + assert isinstance(err.orig, BaseException) # noqa: PT017 + _LOGGER.info( + "%s; %s not completed, retrying", err.orig.args[1], description + ) + time.sleep(instance.db_retry_wait) + # Failed with retryable error + return False + + _LOGGER.warning("Error executing %s: %s", description, err) + + # Failed with permanent error + return True + + return wrapper + + +def database_job_retry_wrapper[**_P]( description: str, attempts: int = 5 -) -> Callable[[_FuncType[_RecorderT, _P, None]], _FuncType[_RecorderT, _P, None]]: +) -> Callable[[_FuncType[_P, None]], _FuncType[_P, None]]: """Try to execute a database job multiple times. This wrapper handles InnoDB deadlocks and lock timeouts. @@ -694,10 +717,10 @@ def database_job_retry_wrapper[_RecorderT: Recorder, **_P]( """ def decorator( - job: _FuncType[_RecorderT, _P, None], - ) -> _FuncType[_RecorderT, _P, None]: + job: _FuncType[_P, None], + ) -> _FuncType[_P, None]: @functools.wraps(job) - def wrapper(instance: _RecorderT, *args: _P.args, **kwargs: _P.kwargs) -> None: + def wrapper(instance: Recorder, *args: _P.args, **kwargs: _P.kwargs) -> None: for attempt in range(attempts): try: job(instance, *args, **kwargs) diff --git a/homeassistant/components/recorder/websocket_api.py b/homeassistant/components/recorder/websocket_api.py index 5e0eef37721f73..f08f7bdcb976e6 100644 --- a/homeassistant/components/recorder/websocket_api.py +++ b/homeassistant/components/recorder/websocket_api.py @@ -15,6 +15,7 @@ from homeassistant.helpers.json import json_bytes from homeassistant.util import dt as dt_util from homeassistant.util.unit_conversion import ( + ConductivityConverter, DataRateConverter, DistanceConverter, DurationConverter, @@ -48,7 +49,7 @@ UNIT_SCHEMA = vol.Schema( { - vol.Optional("conductivity"): vol.In(DataRateConverter.VALID_UNITS), + vol.Optional("conductivity"): vol.In(ConductivityConverter.VALID_UNITS), vol.Optional("data_rate"): vol.In(DataRateConverter.VALID_UNITS), vol.Optional("distance"): vol.In(DistanceConverter.VALID_UNITS), vol.Optional("duration"): vol.In(DurationConverter.VALID_UNITS), diff --git a/homeassistant/components/remember_the_milk/icons.json b/homeassistant/components/remember_the_milk/icons.json index 3ca17113fb8b62..04502aea5ef578 100644 --- a/homeassistant/components/remember_the_milk/icons.json +++ b/homeassistant/components/remember_the_milk/icons.json @@ -1,6 +1,10 @@ { "services": { - "create_task": "mdi:check", - "complete_task": "mdi:check-all" + "create_task": { + "service": "mdi:check" + }, + "complete_task": { + "service": "mdi:check-all" + } } } diff --git a/homeassistant/components/remote/icons.json b/homeassistant/components/remote/icons.json index 07526a4bc7942a..43a7f6ee7b659f 100644 --- a/homeassistant/components/remote/icons.json +++ b/homeassistant/components/remote/icons.json @@ -8,11 +8,23 @@ } }, "services": { - "delete_command": "mdi:delete", - "learn_command": "mdi:school", - "send_command": "mdi:remote", - "toggle": "mdi:remote", - "turn_off": "mdi:remote-off", - "turn_on": "mdi:remote" + "delete_command": { + "service": "mdi:delete" + }, + "learn_command": { + "service": "mdi:school" + }, + "send_command": { + "service": "mdi:remote" + }, + "toggle": { + "service": "mdi:remote" + }, + "turn_off": { + "service": "mdi:remote-off" + }, + "turn_on": { + "service": "mdi:remote" + } } } diff --git a/homeassistant/components/renault/icons.json b/homeassistant/components/renault/icons.json index 75356fda411290..883725eb601915 100644 --- a/homeassistant/components/renault/icons.json +++ b/homeassistant/components/renault/icons.json @@ -64,8 +64,14 @@ } }, "services": { - "ac_start": "mdi:hvac", - "ac_cancel": "mdi:hvac-off", - "charge_set_schedules": "mdi:calendar-clock" + "ac_start": { + "service": "mdi:hvac" + }, + "ac_cancel": { + "service": "mdi:hvac-off" + }, + "charge_set_schedules": { + "service": "mdi:calendar-clock" + } } } diff --git a/homeassistant/components/renault/manifest.json b/homeassistant/components/renault/manifest.json index 6691921e85063b..716f2086bf1d99 100644 --- a/homeassistant/components/renault/manifest.json +++ b/homeassistant/components/renault/manifest.json @@ -8,5 +8,5 @@ "iot_class": "cloud_polling", "loggers": ["renault_api"], "quality_scale": "platinum", - "requirements": ["renault-api==0.2.5"] + "requirements": ["renault-api==0.2.7"] } diff --git a/homeassistant/components/renson/icons.json b/homeassistant/components/renson/icons.json index b7b1fdfdd8cbf3..b558759a0dd612 100644 --- a/homeassistant/components/renson/icons.json +++ b/homeassistant/components/renson/icons.json @@ -17,8 +17,14 @@ } }, "services": { - "set_timer_level": "mdi:timer", - "set_breeze": "mdi:weather-windy", - "set_pollution_settings": "mdi:air-filter" + "set_timer_level": { + "service": "mdi:timer" + }, + "set_breeze": { + "service": "mdi:weather-windy" + }, + "set_pollution_settings": { + "service": "mdi:air-filter" + } } } diff --git a/homeassistant/components/reolink/__init__.py b/homeassistant/components/reolink/__init__.py index a319024633cbae..f64c6bd9cf36cf 100644 --- a/homeassistant/components/reolink/__init__.py +++ b/homeassistant/components/reolink/__init__.py @@ -3,7 +3,6 @@ from __future__ import annotations import asyncio -from dataclasses import dataclass from datetime import timedelta import logging @@ -14,13 +13,20 @@ from homeassistant.const import EVENT_HOMEASSISTANT_STOP, Platform from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady -from homeassistant.helpers import device_registry as dr, entity_registry as er +from homeassistant.helpers import ( + config_validation as cv, + device_registry as dr, + entity_registry as er, +) from homeassistant.helpers.device_registry import format_mac +from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from .const import DOMAIN from .exceptions import PasswordIncompatible, ReolinkException, UserNotAdmin from .host import ReolinkHost +from .services import async_setup_services +from .util import ReolinkData, get_device_uid_and_ch _LOGGER = logging.getLogger(__name__) @@ -40,14 +46,14 @@ FIRMWARE_UPDATE_INTERVAL = timedelta(hours=12) NUM_CRED_ERRORS = 3 +CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN) -@dataclass -class ReolinkData: - """Data for the Reolink integration.""" - host: ReolinkHost - device_coordinator: DataUpdateCoordinator[None] - firmware_coordinator: DataUpdateCoordinator[None] +async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: + """Set up Reolink shared code.""" + + async_setup_services(hass) + return True async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: @@ -265,28 +271,6 @@ async def async_remove_config_entry_device( return False -def get_device_uid_and_ch( - device: dr.DeviceEntry, host: ReolinkHost -) -> tuple[list[str], int | None, bool]: - """Get the channel and the split device_uid from a reolink DeviceEntry.""" - device_uid = [ - dev_id[1].split("_") for dev_id in device.identifiers if dev_id[0] == DOMAIN - ][0] - - is_chime = False - if len(device_uid) < 2: - # NVR itself - ch = None - elif device_uid[1].startswith("ch") and len(device_uid[1]) <= 5: - ch = int(device_uid[1][2:]) - elif device_uid[1].startswith("chime"): - ch = int(device_uid[1][5:]) - is_chime = True - else: - ch = host.api.channel_for_uid(device_uid[1]) - return (device_uid, ch, is_chime) - - def migrate_entity_ids( hass: HomeAssistant, config_entry_id: str, host: ReolinkHost ) -> None: diff --git a/homeassistant/components/reolink/button.py b/homeassistant/components/reolink/button.py index eba0570a3fb9e2..3340cbad29ace6 100644 --- a/homeassistant/components/reolink/button.py +++ b/homeassistant/components/reolink/button.py @@ -37,6 +37,7 @@ ATTR_SPEED = "speed" SUPPORT_PTZ_SPEED = CameraEntityFeature.STREAM +SERVICE_PTZ_MOVE = "ptz_move" @dataclass(frozen=True, kw_only=True) @@ -172,7 +173,7 @@ async def async_setup_entry( platform = async_get_current_platform() platform.async_register_entity_service( - "ptz_move", + SERVICE_PTZ_MOVE, {vol.Required(ATTR_SPEED): cv.positive_int}, "async_ptz_move", [SUPPORT_PTZ_SPEED], diff --git a/homeassistant/components/reolink/config_flow.py b/homeassistant/components/reolink/config_flow.py index 6d0381b025fe59..067a7e24b8ee07 100644 --- a/homeassistant/components/reolink/config_flow.py +++ b/homeassistant/components/reolink/config_flow.py @@ -48,7 +48,7 @@ class ReolinkOptionsFlowHandler(OptionsFlow): """Handle Reolink options.""" - def __init__(self, config_entry): + def __init__(self, config_entry: ConfigEntry) -> None: """Initialize ReolinkOptionsFlowHandler.""" self.config_entry = config_entry diff --git a/homeassistant/components/reolink/host.py b/homeassistant/components/reolink/host.py index 310188b720ecce..0df4918be76980 100644 --- a/homeassistant/components/reolink/host.py +++ b/homeassistant/components/reolink/host.py @@ -437,7 +437,15 @@ async def _async_stop_long_polling(self) -> None: self._long_poll_task.cancel() self._long_poll_task = None - await self._api.unsubscribe(sub_type=SubType.long_poll) + try: + await self._api.unsubscribe(sub_type=SubType.long_poll) + except ReolinkError as err: + _LOGGER.error( + "Reolink error while unsubscribing from host %s:%s: %s", + self._api.host, + self._api.port, + err, + ) async def stop(self, event=None) -> None: """Disconnect the API.""" @@ -511,9 +519,7 @@ async def _renew(self, sub_type: Literal[SubType.push, SubType.long_poll]) -> No ) if sub_type == SubType.push: await self.subscribe() - else: - await self._api.subscribe(self._webhook_url, sub_type) - return + return timer = self._api.renewtimer(sub_type) _LOGGER.debug( diff --git a/homeassistant/components/reolink/icons.json b/homeassistant/components/reolink/icons.json index 7ca4c2d7f2bcfa..f1c6f88a0f007e 100644 --- a/homeassistant/components/reolink/icons.json +++ b/homeassistant/components/reolink/icons.json @@ -300,6 +300,11 @@ } }, "services": { - "ptz_move": "mdi:pan" + "ptz_move": { + "service": "mdi:pan" + }, + "play_chime": { + "service": "mdi:music" + } } } diff --git a/homeassistant/components/reolink/light.py b/homeassistant/components/reolink/light.py index 877bf80080bf50..fe34cccc0c4414 100644 --- a/homeassistant/components/reolink/light.py +++ b/homeassistant/components/reolink/light.py @@ -108,8 +108,7 @@ def is_on(self) -> bool: @property def brightness(self) -> int | None: """Return the brightness of this light between 0.255.""" - if self.entity_description.get_brightness_fn is None: - return None + assert self.entity_description.get_brightness_fn is not None bright_pct = self.entity_description.get_brightness_fn( self._host.api, self._channel diff --git a/homeassistant/components/reolink/manifest.json b/homeassistant/components/reolink/manifest.json index 9671a4b4fc14b8..b90f7f4a045f27 100644 --- a/homeassistant/components/reolink/manifest.json +++ b/homeassistant/components/reolink/manifest.json @@ -18,5 +18,5 @@ "documentation": "https://www.home-assistant.io/integrations/reolink", "iot_class": "local_push", "loggers": ["reolink_aio"], - "requirements": ["reolink-aio==0.9.7"] + "requirements": ["reolink-aio==0.9.8"] } diff --git a/homeassistant/components/reolink/media_source.py b/homeassistant/components/reolink/media_source.py index ae865b77913eb4..3c5d60030a3659 100644 --- a/homeassistant/components/reolink/media_source.py +++ b/homeassistant/components/reolink/media_source.py @@ -174,10 +174,7 @@ async def _async_generate_root(self) -> BrowseMediaSource: if len(ch_id) > 3: ch = host.api.channel_for_uid(ch_id) - if ( - host.api.api_version("recReplay", int(ch)) < 1 - or not host.api.hdd_info - ): + if not host.api.supported(int(ch), "replay") or not host.api.hdd_info: # playback stream not supported by this camera or no storage installed continue @@ -281,12 +278,16 @@ async def _async_generate_resolution_select( config_entry_id, channel, "sub" ) + title = host.api.camera_name(channel) + if host.api.model in DUAL_LENS_MODELS: + title = f"{host.api.camera_name(channel)} lens {channel}" + return BrowseMediaSource( domain=DOMAIN, identifier=f"RESs|{config_entry_id}|{channel}", media_class=MediaClass.CHANNEL, media_content_type=MediaType.PLAYLIST, - title=host.api.camera_name(channel), + title=title, can_play=False, can_expand=True, children=children, @@ -328,12 +329,16 @@ async def _async_generate_camera_days( for day in status.days ] + title = f"{host.api.camera_name(channel)} {res_name(stream)}" + if host.api.model in DUAL_LENS_MODELS: + title = f"{host.api.camera_name(channel)} lens {channel} {res_name(stream)}" + return BrowseMediaSource( domain=DOMAIN, identifier=f"DAYS|{config_entry_id}|{channel}|{stream}", media_class=MediaClass.CHANNEL, media_content_type=MediaType.PLAYLIST, - title=f"{host.api.camera_name(channel)} {res_name(stream)}", + title=title, can_play=False, can_expand=True, children=children, @@ -388,12 +393,18 @@ async def _async_generate_camera_files( ) ) + title = ( + f"{host.api.camera_name(channel)} {res_name(stream)} {year}/{month}/{day}" + ) + if host.api.model in DUAL_LENS_MODELS: + title = f"{host.api.camera_name(channel)} lens {channel} {res_name(stream)} {year}/{month}/{day}" + return BrowseMediaSource( domain=DOMAIN, identifier=f"FILES|{config_entry_id}|{channel}|{stream}", media_class=MediaClass.CHANNEL, media_content_type=MediaType.PLAYLIST, - title=f"{host.api.camera_name(channel)} {res_name(stream)} {year}/{month}/{day}", + title=title, can_play=False, can_expand=True, children=children, diff --git a/homeassistant/components/reolink/services.py b/homeassistant/components/reolink/services.py new file mode 100644 index 00000000000000..d5cb402c74bdbc --- /dev/null +++ b/homeassistant/components/reolink/services.py @@ -0,0 +1,80 @@ +"""Reolink additional services.""" + +from __future__ import annotations + +from reolink_aio.api import Chime +from reolink_aio.enums import ChimeToneEnum +from reolink_aio.exceptions import InvalidParameterError, ReolinkError +import voluptuous as vol + +from homeassistant.config_entries import ConfigEntryState +from homeassistant.const import ATTR_DEVICE_ID +from homeassistant.core import HomeAssistant, ServiceCall, callback +from homeassistant.exceptions import HomeAssistantError, ServiceValidationError +from homeassistant.helpers import device_registry as dr + +from .const import DOMAIN +from .host import ReolinkHost +from .util import get_device_uid_and_ch + +ATTR_RINGTONE = "ringtone" + + +@callback +def async_setup_services(hass: HomeAssistant) -> None: + """Set up Reolink services.""" + + async def async_play_chime(service_call: ServiceCall) -> None: + """Play a ringtone.""" + service_data = service_call.data + device_registry = dr.async_get(hass) + + for device_id in service_data[ATTR_DEVICE_ID]: + config_entry = None + device = device_registry.async_get(device_id) + if device is not None: + for entry_id in device.config_entries: + config_entry = hass.config_entries.async_get_entry(entry_id) + if config_entry is not None and config_entry.domain == DOMAIN: + break + if ( + config_entry is None + or device is None + or config_entry.state == ConfigEntryState.NOT_LOADED + ): + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="service_entry_ex", + translation_placeholders={"service_name": "play_chime"}, + ) + host: ReolinkHost = hass.data[DOMAIN][config_entry.entry_id].host + (device_uid, chime_id, is_chime) = get_device_uid_and_ch(device, host) + chime: Chime | None = host.api.chime(chime_id) + if not is_chime or chime is None: + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="service_not_chime", + translation_placeholders={"device_name": str(device.name)}, + ) + + ringtone = service_data[ATTR_RINGTONE] + try: + await chime.play(ChimeToneEnum[ringtone].value) + except InvalidParameterError as err: + raise ServiceValidationError(err) from err + except ReolinkError as err: + raise HomeAssistantError(err) from err + + hass.services.async_register( + DOMAIN, + "play_chime", + async_play_chime, + schema=vol.Schema( + { + vol.Required(ATTR_DEVICE_ID): list[str], + vol.Required(ATTR_RINGTONE): vol.In( + [method.name for method in ChimeToneEnum][1:] + ), + } + ), + ) diff --git a/homeassistant/components/reolink/services.yaml b/homeassistant/components/reolink/services.yaml index 42b9af34eb0e06..fe7fba9cdc7d3e 100644 --- a/homeassistant/components/reolink/services.yaml +++ b/homeassistant/components/reolink/services.yaml @@ -16,3 +16,30 @@ ptz_move: min: 1 max: 64 step: 1 + +play_chime: + fields: + device_id: + required: true + selector: + device: + multiple: true + filter: + integration: reolink + model: "Reolink Chime" + ringtone: + required: true + selector: + select: + translation_key: ringtone + options: + - citybird + - originaltune + - pianokey + - loop + - attraction + - hophop + - goodday + - operetta + - moonlight + - waybackhome diff --git a/homeassistant/components/reolink/strings.json b/homeassistant/components/reolink/strings.json index cad09f71562039..3710c3743fa28b 100644 --- a/homeassistant/components/reolink/strings.json +++ b/homeassistant/components/reolink/strings.json @@ -50,6 +50,14 @@ } } }, + "exceptions": { + "service_entry_ex": { + "message": "Reolink {service_name} error: config entry not found or not loaded" + }, + "service_not_chime": { + "message": "Reolink play_chime error: {device_name} is not a chime" + } + }, "issues": { "https_webhook": { "title": "Reolink webhook URL uses HTTPS (SSL)", @@ -86,6 +94,36 @@ "description": "PTZ move speed." } } + }, + "play_chime": { + "name": "Play chime", + "description": "Play a ringtone on a chime.", + "fields": { + "device_id": { + "name": "Target chime", + "description": "The chime to play the ringtone on." + }, + "ringtone": { + "name": "Ringtone", + "description": "Ringtone to play." + } + } + } + }, + "selector": { + "ringtone": { + "options": { + "citybird": "[%key:component::reolink::entity::select::motion_tone::state::citybird%]", + "originaltune": "[%key:component::reolink::entity::select::motion_tone::state::originaltune%]", + "pianokey": "[%key:component::reolink::entity::select::motion_tone::state::pianokey%]", + "loop": "[%key:component::reolink::entity::select::motion_tone::state::loop%]", + "attraction": "[%key:component::reolink::entity::select::motion_tone::state::attraction%]", + "hophop": "[%key:component::reolink::entity::select::motion_tone::state::hophop%]", + "goodday": "[%key:component::reolink::entity::select::motion_tone::state::goodday%]", + "operetta": "[%key:component::reolink::entity::select::motion_tone::state::operetta%]", + "moonlight": "[%key:component::reolink::entity::select::motion_tone::state::moonlight%]", + "waybackhome": "[%key:component::reolink::entity::select::motion_tone::state::waybackhome%]" + } } }, "entity": { diff --git a/homeassistant/components/reolink/update.py b/homeassistant/components/reolink/update.py index 9b710c6576d965..3c1e70612a7faf 100644 --- a/homeassistant/components/reolink/update.py +++ b/homeassistant/components/reolink/update.py @@ -137,8 +137,7 @@ def supported_features(self) -> UpdateEntityFeature: async def async_release_notes(self) -> str | None: """Return the release notes.""" new_firmware = self._host.api.firmware_update_available(self._channel) - if not isinstance(new_firmware, NewSoftwareVersion): - return None + assert isinstance(new_firmware, NewSoftwareVersion) return ( "If the install button fails, download this" @@ -229,8 +228,7 @@ def supported_features(self) -> UpdateEntityFeature: async def async_release_notes(self) -> str | None: """Return the release notes.""" new_firmware = self._host.api.firmware_update_available() - if not isinstance(new_firmware, NewSoftwareVersion): - return None + assert isinstance(new_firmware, NewSoftwareVersion) return ( "If the install button fails, download this" diff --git a/homeassistant/components/reolink/util.py b/homeassistant/components/reolink/util.py index cf4659224e35ab..305579e35cb941 100644 --- a/homeassistant/components/reolink/util.py +++ b/homeassistant/components/reolink/util.py @@ -2,11 +2,24 @@ from __future__ import annotations +from dataclasses import dataclass + from homeassistant import config_entries from homeassistant.core import HomeAssistant +from homeassistant.helpers import device_registry as dr +from homeassistant.helpers.update_coordinator import DataUpdateCoordinator -from . import ReolinkData from .const import DOMAIN +from .host import ReolinkHost + + +@dataclass +class ReolinkData: + """Data for the Reolink integration.""" + + host: ReolinkHost + device_coordinator: DataUpdateCoordinator[None] + firmware_coordinator: DataUpdateCoordinator[None] def is_connected(hass: HomeAssistant, config_entry: config_entries.ConfigEntry) -> bool: @@ -19,3 +32,25 @@ def is_connected(hass: HomeAssistant, config_entry: config_entries.ConfigEntry) and config_entry.state == config_entries.ConfigEntryState.LOADED and reolink_data.device_coordinator.last_update_success ) + + +def get_device_uid_and_ch( + device: dr.DeviceEntry, host: ReolinkHost +) -> tuple[list[str], int | None, bool]: + """Get the channel and the split device_uid from a reolink DeviceEntry.""" + device_uid = [ + dev_id[1].split("_") for dev_id in device.identifiers if dev_id[0] == DOMAIN + ][0] + + is_chime = False + if len(device_uid) < 2: + # NVR itself + ch = None + elif device_uid[1].startswith("ch") and len(device_uid[1]) <= 5: + ch = int(device_uid[1][2:]) + elif device_uid[1].startswith("chime"): + ch = int(device_uid[1][5:]) + is_chime = True + else: + ch = host.api.channel_for_uid(device_uid[1]) + return (device_uid, ch, is_chime) diff --git a/homeassistant/components/rest/icons.json b/homeassistant/components/rest/icons.json index a03163179cb5bd..a9829425570a0c 100644 --- a/homeassistant/components/rest/icons.json +++ b/homeassistant/components/rest/icons.json @@ -1,5 +1,7 @@ { "services": { - "reload": "mdi:reload" + "reload": { + "service": "mdi:reload" + } } } diff --git a/homeassistant/components/rest_command/icons.json b/homeassistant/components/rest_command/icons.json index a03163179cb5bd..a9829425570a0c 100644 --- a/homeassistant/components/rest_command/icons.json +++ b/homeassistant/components/rest_command/icons.json @@ -1,5 +1,7 @@ { "services": { - "reload": "mdi:reload" + "reload": { + "service": "mdi:reload" + } } } diff --git a/homeassistant/components/rflink/icons.json b/homeassistant/components/rflink/icons.json index 988b048eee7e00..de2942f44ac8b3 100644 --- a/homeassistant/components/rflink/icons.json +++ b/homeassistant/components/rflink/icons.json @@ -1,5 +1,7 @@ { "services": { - "send_command": "mdi:send" + "send_command": { + "service": "mdi:send" + } } } diff --git a/homeassistant/components/rfxtrx/icons.json b/homeassistant/components/rfxtrx/icons.json index c1b8e741e4536c..cbc48cf2105355 100644 --- a/homeassistant/components/rfxtrx/icons.json +++ b/homeassistant/components/rfxtrx/icons.json @@ -1,5 +1,7 @@ { "services": { - "send": "mdi:send" + "send": { + "service": "mdi:send" + } } } diff --git a/homeassistant/components/ring/__init__.py b/homeassistant/components/ring/__init__.py index 36c66550ddcf5e..3714802b63a939 100644 --- a/homeassistant/components/ring/__init__.py +++ b/homeassistant/components/ring/__init__.py @@ -3,7 +3,6 @@ from __future__ import annotations from dataclasses import dataclass -from functools import partial import logging from typing import Any, cast @@ -13,6 +12,7 @@ from homeassistant.const import APPLICATION_NAME, CONF_TOKEN, __version__ from homeassistant.core import HomeAssistant, ServiceCall, callback from homeassistant.helpers import device_registry as dr, entity_registry as er +from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue from .const import DOMAIN, PLATFORMS @@ -31,21 +31,24 @@ class RingData: notifications_coordinator: RingNotificationsCoordinator -async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: +type RingConfigEntry = ConfigEntry[RingData] + + +async def async_setup_entry(hass: HomeAssistant, entry: RingConfigEntry) -> bool: """Set up a config entry.""" def token_updater(token: dict[str, Any]) -> None: - """Handle from sync context when token is updated.""" - hass.loop.call_soon_threadsafe( - partial( - hass.config_entries.async_update_entry, - entry, - data={**entry.data, CONF_TOKEN: token}, - ) + """Handle from async context when token is updated.""" + hass.config_entries.async_update_entry( + entry, + data={**entry.data, CONF_TOKEN: token}, ) auth = Auth( - f"{APPLICATION_NAME}/{__version__}", entry.data[CONF_TOKEN], token_updater + f"{APPLICATION_NAME}/{__version__}", + entry.data[CONF_TOKEN], + token_updater, + http_client_session=async_get_clientsession(hass), ) ring = Ring(auth) @@ -56,7 +59,7 @@ def token_updater(token: dict[str, Any]) -> None: await devices_coordinator.async_config_entry_first_refresh() await notifications_coordinator.async_config_entry_first_refresh() - hass.data.setdefault(DOMAIN, {})[entry.entry_id] = RingData( + entry.runtime_data = RingData( api=ring, devices=ring.devices(), devices_coordinator=devices_coordinator, @@ -86,11 +89,9 @@ async def async_refresh_all(_: ServiceCall) -> None: severity=IssueSeverity.WARNING, translation_key="deprecated_service_ring_update", ) - - for info in hass.data[DOMAIN].values(): - ring_data = cast(RingData, info) - await ring_data.devices_coordinator.async_refresh() - await ring_data.notifications_coordinator.async_refresh() + for loaded_entry in hass.config_entries.async_loaded_entries(DOMAIN): + await loaded_entry.runtime_data.devices_coordinator.async_refresh() + await loaded_entry.runtime_data.notifications_coordinator.async_refresh() # register service hass.services.async_register(DOMAIN, "update", async_refresh_all) @@ -100,18 +101,13 @@ async def async_refresh_all(_: ServiceCall) -> None: async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Unload Ring entry.""" - if not await hass.config_entries.async_unload_platforms(entry, PLATFORMS): - return False + unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) - hass.data[DOMAIN].pop(entry.entry_id) + if len(hass.config_entries.async_loaded_entries(DOMAIN)) == 1: + # This is the last loaded entry, clean up service + hass.services.async_remove(DOMAIN, "update") - if len(hass.data[DOMAIN]) != 0: - return True - - # Last entry unloaded, clean up service - hass.services.async_remove(DOMAIN, "update") - - return True + return unload_ok async def async_remove_config_entry_device( diff --git a/homeassistant/components/ring/binary_sensor.py b/homeassistant/components/ring/binary_sensor.py index 2db04cfd46168f..2fb557ddde0e96 100644 --- a/homeassistant/components/ring/binary_sensor.py +++ b/homeassistant/components/ring/binary_sensor.py @@ -14,12 +14,10 @@ BinarySensorEntity, BinarySensorEntityDescription, ) -from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import RingData -from .const import DOMAIN +from . import RingConfigEntry from .coordinator import RingNotificationsCoordinator from .entity import RingBaseEntity @@ -50,11 +48,11 @@ class RingBinarySensorEntityDescription(BinarySensorEntityDescription): async def async_setup_entry( hass: HomeAssistant, - config_entry: ConfigEntry, + entry: RingConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: """Set up the Ring binary sensors from a config entry.""" - ring_data: RingData = hass.data[DOMAIN][config_entry.entry_id] + ring_data = entry.runtime_data entities = [ RingBinarySensor( diff --git a/homeassistant/components/ring/button.py b/homeassistant/components/ring/button.py index 15d56a8b7cfc97..b9d5cceb373386 100644 --- a/homeassistant/components/ring/button.py +++ b/homeassistant/components/ring/button.py @@ -5,12 +5,10 @@ from ring_doorbell import RingOther from homeassistant.components.button import ButtonEntity, ButtonEntityDescription -from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import RingData -from .const import DOMAIN +from . import RingConfigEntry from .coordinator import RingDataCoordinator from .entity import RingEntity, exception_wrap @@ -21,11 +19,11 @@ async def async_setup_entry( hass: HomeAssistant, - config_entry: ConfigEntry, + entry: RingConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: """Create the buttons for the Ring devices.""" - ring_data: RingData = hass.data[DOMAIN][config_entry.entry_id] + ring_data = entry.runtime_data devices_coordinator = ring_data.devices_coordinator async_add_entities( @@ -53,6 +51,6 @@ def __init__( self._attr_unique_id = f"{device.id}-{description.key}" @exception_wrap - def press(self) -> None: + async def async_press(self) -> None: """Open the door.""" - self._device.open_door() + await self._device.async_open_door() diff --git a/homeassistant/components/ring/camera.py b/homeassistant/components/ring/camera.py index ba75b68434d0be..9c66df9d89e0bd 100644 --- a/homeassistant/components/ring/camera.py +++ b/homeassistant/components/ring/camera.py @@ -12,14 +12,12 @@ from homeassistant.components import ffmpeg from homeassistant.components.camera import Camera -from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.aiohttp_client import async_aiohttp_proxy_stream from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.util import dt as dt_util -from . import RingData -from .const import DOMAIN +from . import RingConfigEntry from .coordinator import RingDataCoordinator from .entity import RingEntity, exception_wrap @@ -31,11 +29,11 @@ async def async_setup_entry( hass: HomeAssistant, - config_entry: ConfigEntry, + entry: RingConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: """Set up a Ring Door Bell and StickUp Camera.""" - ring_data: RingData = hass.data[DOMAIN][config_entry.entry_id] + ring_data = entry.runtime_data devices_coordinator = ring_data.devices_coordinator ffmpeg_manager = ffmpeg.get_ffmpeg_manager(hass) @@ -81,6 +79,8 @@ def _handle_coordinator_update(self) -> None: history_data = self._device.last_history if history_data: self._last_event = history_data[0] + # will call async_update to update the attributes and get the + # video url from the api self.async_schedule_update_ha_state(True) else: self._last_event = None @@ -159,36 +159,36 @@ async def async_update(self) -> None: if self._last_video_id != self._last_event["id"]: self._image = None - self._video_url = await self.hass.async_add_executor_job(self._get_video) + self._video_url = await self._async_get_video() self._last_video_id = self._last_event["id"] self._expires_at = FORCE_REFRESH_INTERVAL + utcnow @exception_wrap - def _get_video(self) -> str | None: + async def _async_get_video(self) -> str | None: if TYPE_CHECKING: # _last_event is set before calling update so will never be None assert self._last_event event_id = self._last_event.get("id") assert event_id and isinstance(event_id, int) - return self._device.recording_url(event_id) + return await self._device.async_recording_url(event_id) @exception_wrap - def _set_motion_detection_enabled(self, new_state: bool) -> None: + async def _async_set_motion_detection_enabled(self, new_state: bool) -> None: if not self._device.has_capability(MOTION_DETECTION_CAPABILITY): _LOGGER.error( "Entity %s does not have motion detection capability", self.entity_id ) return - self._device.motion_detection = new_state + await self._device.async_set_motion_detection(new_state) self._attr_motion_detection_enabled = new_state - self.schedule_update_ha_state(False) + self.async_write_ha_state() - def enable_motion_detection(self) -> None: + async def async_enable_motion_detection(self) -> None: """Enable motion detection in the camera.""" - self._set_motion_detection_enabled(True) + await self._async_set_motion_detection_enabled(True) - def disable_motion_detection(self) -> None: + async def async_disable_motion_detection(self) -> None: """Disable motion detection in camera.""" - self._set_motion_detection_enabled(False) + await self._async_set_motion_detection_enabled(False) diff --git a/homeassistant/components/ring/config_flow.py b/homeassistant/components/ring/config_flow.py index 6239105580db6f..74546567270630 100644 --- a/homeassistant/components/ring/config_flow.py +++ b/homeassistant/components/ring/config_flow.py @@ -17,6 +17,7 @@ ) from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError +from homeassistant.helpers.aiohttp_client import async_get_clientsession from .const import CONF_2FA, DOMAIN @@ -31,11 +32,13 @@ async def validate_input(hass: HomeAssistant, data: dict[str, str]) -> dict[str, Any]: """Validate the user input allows us to connect.""" - auth = Auth(f"{APPLICATION_NAME}/{ha_version}") + auth = Auth( + f"{APPLICATION_NAME}/{ha_version}", + http_client_session=async_get_clientsession(hass), + ) try: - token = await hass.async_add_executor_job( - auth.fetch_token, + token = await auth.async_fetch_token( data[CONF_USERNAME], data[CONF_PASSWORD], data.get(CONF_2FA), @@ -62,6 +65,8 @@ async def async_step_user( """Handle the initial step.""" errors: dict[str, str] = {} if user_input is not None: + await self.async_set_unique_id(user_input[CONF_USERNAME]) + self._abort_if_unique_id_configured() try: token = await validate_input(self.hass, user_input) except Require2FA: @@ -74,7 +79,6 @@ async def async_step_user( _LOGGER.exception("Unexpected exception") errors["base"] = "unknown" else: - await self.async_set_unique_id(user_input[CONF_USERNAME]) return self.async_create_entry( title=user_input[CONF_USERNAME], data={CONF_USERNAME: user_input[CONF_USERNAME], CONF_TOKEN: token}, diff --git a/homeassistant/components/ring/coordinator.py b/homeassistant/components/ring/coordinator.py index 1a52fc78988021..600743005ebcb9 100644 --- a/homeassistant/components/ring/coordinator.py +++ b/homeassistant/components/ring/coordinator.py @@ -1,8 +1,9 @@ """Data coordinators for the ring integration.""" from asyncio import TaskGroup -from collections.abc import Callable +from collections.abc import Callable, Coroutine import logging +from typing import Any from ring_doorbell import AuthenticationError, Ring, RingDevices, RingError, RingTimeout @@ -16,10 +17,13 @@ async def _call_api[*_Ts, _R]( - hass: HomeAssistant, target: Callable[[*_Ts], _R], *args: *_Ts, msg_suffix: str = "" + hass: HomeAssistant, + target: Callable[[*_Ts], Coroutine[Any, Any, _R]], + *args: *_Ts, + msg_suffix: str = "", ) -> _R: try: - return await hass.async_add_executor_job(target, *args) + return await target(*args) except AuthenticationError as err: # Raising ConfigEntryAuthFailed will cancel future updates # and start a config flow with SOURCE_REAUTH (async_step_reauth) @@ -52,7 +56,9 @@ def __init__( async def _async_update_data(self) -> RingDevices: """Fetch data from API endpoint.""" - update_method: str = "update_data" if self.first_call else "update_devices" + update_method: str = ( + "async_update_data" if self.first_call else "async_update_devices" + ) await _call_api(self.hass, getattr(self.ring_api, update_method)) self.first_call = False devices: RingDevices = self.ring_api.devices() @@ -67,7 +73,7 @@ async def _async_update_data(self) -> RingDevices: tg.create_task( _call_api( self.hass, - lambda device: device.history(limit=10), + lambda device: device.async_history(limit=10), device, msg_suffix=f" for device {device.name}", # device_id is the mac ) @@ -75,7 +81,7 @@ async def _async_update_data(self) -> RingDevices: tg.create_task( _call_api( self.hass, - device.update_health_data, + device.async_update_health_data, msg_suffix=f" for device {device.name}", ) ) @@ -100,4 +106,4 @@ def __init__(self, hass: HomeAssistant, ring_api: Ring) -> None: async def _async_update_data(self) -> None: """Fetch data from API endpoint.""" - await _call_api(self.hass, self.ring_api.update_dings) + await _call_api(self.hass, self.ring_api.async_update_dings) diff --git a/homeassistant/components/ring/diagnostics.py b/homeassistant/components/ring/diagnostics.py index 2e7604d9f502e6..cecf26a46a7626 100644 --- a/homeassistant/components/ring/diagnostics.py +++ b/homeassistant/components/ring/diagnostics.py @@ -5,11 +5,9 @@ from typing import Any from homeassistant.components.diagnostics import async_redact_data -from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant -from . import RingData -from .const import DOMAIN +from . import RingConfigEntry TO_REDACT = { "id", @@ -29,10 +27,10 @@ async def async_get_config_entry_diagnostics( - hass: HomeAssistant, entry: ConfigEntry + hass: HomeAssistant, entry: RingConfigEntry ) -> dict[str, Any]: """Return diagnostics for a config entry.""" - ring_data: RingData = hass.data[DOMAIN][entry.entry_id] + ring_data = entry.runtime_data devices_data = ring_data.api.devices_data devices_raw = [ devices_data[device_type][device_id] diff --git a/homeassistant/components/ring/entity.py b/homeassistant/components/ring/entity.py index a4275815450660..72deb09b76fc17 100644 --- a/homeassistant/components/ring/entity.py +++ b/homeassistant/components/ring/entity.py @@ -1,6 +1,6 @@ """Base class for Ring entity.""" -from collections.abc import Callable +from collections.abc import Callable, Coroutine from typing import Any, Concatenate, Generic, cast from ring_doorbell import ( @@ -29,25 +29,23 @@ def exception_wrap[_RingBaseEntityT: RingBaseEntity[Any, Any], **_P, _R]( - func: Callable[Concatenate[_RingBaseEntityT, _P], _R], -) -> Callable[Concatenate[_RingBaseEntityT, _P], _R]: + async_func: Callable[Concatenate[_RingBaseEntityT, _P], Coroutine[Any, Any, _R]], +) -> Callable[Concatenate[_RingBaseEntityT, _P], Coroutine[Any, Any, _R]]: """Define a wrapper to catch exceptions and raise HomeAssistant errors.""" - def _wrap(self: _RingBaseEntityT, *args: _P.args, **kwargs: _P.kwargs) -> _R: + async def _wrap(self: _RingBaseEntityT, *args: _P.args, **kwargs: _P.kwargs) -> _R: try: - return func(self, *args, **kwargs) + return await async_func(self, *args, **kwargs) except AuthenticationError as err: - self.hass.loop.call_soon_threadsafe( - self.coordinator.config_entry.async_start_reauth, self.hass - ) + self.coordinator.config_entry.async_start_reauth(self.hass) raise HomeAssistantError(err) from err except RingTimeout as err: raise HomeAssistantError( - f"Timeout communicating with API {func}: {err}" + f"Timeout communicating with API {async_func}: {err}" ) from err except RingError as err: raise HomeAssistantError( - f"Error communicating with API{func}: {err}" + f"Error communicating with API{async_func}: {err}" ) from err return _wrap diff --git a/homeassistant/components/ring/icons.json b/homeassistant/components/ring/icons.json index 9dd31fd0fd1443..5820fbf77c82d7 100644 --- a/homeassistant/components/ring/icons.json +++ b/homeassistant/components/ring/icons.json @@ -39,6 +39,8 @@ } }, "services": { - "update": "mdi:refresh" + "update": { + "service": "mdi:refresh" + } } } diff --git a/homeassistant/components/ring/light.py b/homeassistant/components/ring/light.py index 5747c9e77f73d8..9e29373a3aa56a 100644 --- a/homeassistant/components/ring/light.py +++ b/homeassistant/components/ring/light.py @@ -8,13 +8,11 @@ from ring_doorbell import RingStickUpCam from homeassistant.components.light import ColorMode, LightEntity -from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback import homeassistant.util.dt as dt_util -from . import RingData -from .const import DOMAIN +from . import RingConfigEntry from .coordinator import RingDataCoordinator from .entity import RingEntity, exception_wrap @@ -38,11 +36,11 @@ class OnOffState(StrEnum): async def async_setup_entry( hass: HomeAssistant, - config_entry: ConfigEntry, + entry: RingConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: """Create the lights for the Ring devices.""" - ring_data: RingData = hass.data[DOMAIN][config_entry.entry_id] + ring_data = entry.runtime_data devices_coordinator = ring_data.devices_coordinator async_add_entities( @@ -80,18 +78,18 @@ def _handle_coordinator_update(self) -> None: super()._handle_coordinator_update() @exception_wrap - def _set_light(self, new_state: OnOffState) -> None: + async def _async_set_light(self, new_state: OnOffState) -> None: """Update light state, and causes Home Assistant to correctly update.""" - self._device.lights = new_state + await self._device.async_set_lights(new_state) self._attr_is_on = new_state == OnOffState.ON self._no_updates_until = dt_util.utcnow() + SKIP_UPDATES_DELAY - self.schedule_update_ha_state() + self.async_write_ha_state() - def turn_on(self, **kwargs: Any) -> None: + async def async_turn_on(self, **kwargs: Any) -> None: """Turn the light on for 30 seconds.""" - self._set_light(OnOffState.ON) + await self._async_set_light(OnOffState.ON) - def turn_off(self, **kwargs: Any) -> None: + async def async_turn_off(self, **kwargs: Any) -> None: """Turn the light off.""" - self._set_light(OnOffState.OFF) + await self._async_set_light(OnOffState.OFF) diff --git a/homeassistant/components/ring/manifest.json b/homeassistant/components/ring/manifest.json index a3d15bd711dc97..3aced8fd1ea4c7 100644 --- a/homeassistant/components/ring/manifest.json +++ b/homeassistant/components/ring/manifest.json @@ -14,5 +14,5 @@ "iot_class": "cloud_polling", "loggers": ["ring_doorbell"], "quality_scale": "silver", - "requirements": ["ring-doorbell[listen]==0.8.12"] + "requirements": ["ring-doorbell[listen]==0.9.3"] } diff --git a/homeassistant/components/ring/sensor.py b/homeassistant/components/ring/sensor.py index b6849e37d966b3..83d07dbd9b4f68 100644 --- a/homeassistant/components/ring/sensor.py +++ b/homeassistant/components/ring/sensor.py @@ -21,7 +21,6 @@ SensorEntityDescription, SensorStateClass, ) -from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( PERCENTAGE, SIGNAL_STRENGTH_DECIBELS_MILLIWATT, @@ -31,19 +30,18 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import StateType -from . import RingData -from .const import DOMAIN +from . import RingConfigEntry from .coordinator import RingDataCoordinator from .entity import RingDeviceT, RingEntity async def async_setup_entry( hass: HomeAssistant, - config_entry: ConfigEntry, + entry: RingConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: """Set up a sensor for a Ring device.""" - ring_data: RingData = hass.data[DOMAIN][config_entry.entry_id] + ring_data = entry.runtime_data devices_coordinator = ring_data.devices_coordinator entities = [ diff --git a/homeassistant/components/ring/siren.py b/homeassistant/components/ring/siren.py index f63f9d33182d04..f5730d942b8dea 100644 --- a/homeassistant/components/ring/siren.py +++ b/homeassistant/components/ring/siren.py @@ -6,12 +6,10 @@ from ring_doorbell import RingChime, RingEventKind from homeassistant.components.siren import ATTR_TONE, SirenEntity, SirenEntityFeature -from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import RingData -from .const import DOMAIN +from . import RingConfigEntry from .coordinator import RingDataCoordinator from .entity import RingEntity, exception_wrap @@ -20,11 +18,11 @@ async def async_setup_entry( hass: HomeAssistant, - config_entry: ConfigEntry, + entry: RingConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: """Create the sirens for the Ring devices.""" - ring_data: RingData = hass.data[DOMAIN][config_entry.entry_id] + ring_data = entry.runtime_data devices_coordinator = ring_data.devices_coordinator async_add_entities( @@ -47,8 +45,8 @@ def __init__(self, device: RingChime, coordinator: RingDataCoordinator) -> None: self._attr_unique_id = f"{self._device.id}-siren" @exception_wrap - def turn_on(self, **kwargs: Any) -> None: + async def async_turn_on(self, **kwargs: Any) -> None: """Play the test sound on a Ring Chime device.""" tone = kwargs.get(ATTR_TONE) or RingEventKind.DING.value - self._device.test_sound(kind=tone) + await self._device.async_test_sound(kind=tone) diff --git a/homeassistant/components/ring/strings.json b/homeassistant/components/ring/strings.json index ed0319b7a4b8b9..6bd7d1941367d9 100644 --- a/homeassistant/components/ring/strings.json +++ b/homeassistant/components/ring/strings.json @@ -27,7 +27,7 @@ "unknown": "[%key:common::config_flow::error::unknown%]" }, "abort": { - "already_configured": "[%key:common::config_flow::abort::already_configured_device%]", + "already_configured": "[%key:common::config_flow::abort::already_configured_account%]", "reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]" } }, diff --git a/homeassistant/components/ring/switch.py b/homeassistant/components/ring/switch.py index 0e032907bae8d9..01d321572acda1 100644 --- a/homeassistant/components/ring/switch.py +++ b/homeassistant/components/ring/switch.py @@ -7,13 +7,11 @@ from ring_doorbell import RingStickUpCam from homeassistant.components.switch import SwitchEntity -from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback import homeassistant.util.dt as dt_util -from . import RingData -from .const import DOMAIN +from . import RingConfigEntry from .coordinator import RingDataCoordinator from .entity import RingEntity, exception_wrap @@ -30,11 +28,11 @@ async def async_setup_entry( hass: HomeAssistant, - config_entry: ConfigEntry, + entry: RingConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: """Create the switches for the Ring devices.""" - ring_data: RingData = hass.data[DOMAIN][config_entry.entry_id] + ring_data = entry.runtime_data devices_coordinator = ring_data.devices_coordinator async_add_entities( @@ -81,18 +79,18 @@ def _handle_coordinator_update(self) -> None: super()._handle_coordinator_update() @exception_wrap - def _set_switch(self, new_state: int) -> None: + async def _async_set_switch(self, new_state: int) -> None: """Update switch state, and causes Home Assistant to correctly update.""" - self._device.siren = new_state + await self._device.async_set_siren(new_state) self._attr_is_on = new_state > 0 self._no_updates_until = dt_util.utcnow() + SKIP_UPDATES_DELAY - self.schedule_update_ha_state() + self.async_write_ha_state() - def turn_on(self, **kwargs: Any) -> None: + async def async_turn_on(self, **kwargs: Any) -> None: """Turn the siren on for 30 seconds.""" - self._set_switch(1) + await self._async_set_switch(1) - def turn_off(self, **kwargs: Any) -> None: + async def async_turn_off(self, **kwargs: Any) -> None: """Turn the siren off.""" - self._set_switch(0) + await self._async_set_switch(0) diff --git a/homeassistant/components/roborock/__init__.py b/homeassistant/components/roborock/__init__.py index d107a0bee8b433..88a603eca2b286 100644 --- a/homeassistant/components/roborock/__init__.py +++ b/homeassistant/components/roborock/__init__.py @@ -168,7 +168,9 @@ async def setup_device_v1( home_data_rooms: list[HomeDataRoom], ) -> RoborockDataUpdateCoordinator | None: """Set up a device Coordinator.""" - mqtt_client = RoborockMqttClientV1(user_data, DeviceData(device, product_info.name)) + mqtt_client = await hass.async_add_executor_job( + RoborockMqttClientV1, user_data, DeviceData(device, product_info.name) + ) try: networking = await mqtt_client.get_networking() if networking is None: diff --git a/homeassistant/components/roborock/icons.json b/homeassistant/components/roborock/icons.json index 6a615ab82a1a47..c7df6d35460b58 100644 --- a/homeassistant/components/roborock/icons.json +++ b/homeassistant/components/roborock/icons.json @@ -119,6 +119,8 @@ } }, "services": { - "get_maps": "mdi:floor-plan" + "get_maps": { + "service": "mdi:floor-plan" + } } } diff --git a/homeassistant/components/roku/icons.json b/homeassistant/components/roku/icons.json index 02e5d1e56989df..355b5a715e5013 100644 --- a/homeassistant/components/roku/icons.json +++ b/homeassistant/components/roku/icons.json @@ -32,6 +32,8 @@ } }, "services": { - "search": "mdi:magnify" + "search": { + "service": "mdi:magnify" + } } } diff --git a/homeassistant/components/roon/config_flow.py b/homeassistant/components/roon/config_flow.py index f555cc52dd198a..b896f6775ae11f 100644 --- a/homeassistant/components/roon/config_flow.py +++ b/homeassistant/components/roon/config_flow.py @@ -2,11 +2,12 @@ import asyncio import logging +from typing import Any from roonapi import RoonApi, RoonDiscovery import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_PORT from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError @@ -36,14 +37,14 @@ class RoonHub: """Interact with roon during config flow.""" - def __init__(self, hass): + def __init__(self, hass: HomeAssistant) -> None: """Initialise the RoonHub.""" self._hass = hass - async def discover(self): + async def discover(self) -> list[tuple[str, int]]: """Try and discover roon servers.""" - def get_discovered_servers(discovery): + def get_discovered_servers(discovery: RoonDiscovery) -> list[tuple[str, int]]: servers = discovery.all() discovery.stop() return servers @@ -93,7 +94,7 @@ def stop_apis(apis): return (token, core_id, core_name) -async def discover(hass): +async def discover(hass: HomeAssistant) -> list[tuple[str, int]]: """Connect and authenticate home assistant.""" hub = RoonHub(hass) @@ -122,13 +123,15 @@ class RoonConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - def __init__(self): + def __init__(self) -> None: """Initialize the Roon flow.""" self._host = None self._port = None - self._servers = [] + self._servers: list[tuple[str, int]] = [] - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Get roon core details via discovery.""" self._servers = await discover(self.hass) @@ -139,9 +142,11 @@ async def async_step_user(self, user_input=None): return await self.async_step_fallback() - async def async_step_fallback(self, user_input=None): + async def async_step_fallback( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Get host and port details from the user.""" - errors = {} + errors: dict[str, str] = {} if user_input is not None: self._host = user_input["host"] @@ -152,7 +157,9 @@ async def async_step_fallback(self, user_input=None): step_id="fallback", data_schema=DATA_SCHEMA, errors=errors ) - async def async_step_link(self, user_input=None): + async def async_step_link( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle linking and authenticating with the roon server.""" errors = {} if user_input is not None: diff --git a/homeassistant/components/roon/icons.json b/homeassistant/components/roon/icons.json index 571ca3f45a21bb..1e1dd42b765d1b 100644 --- a/homeassistant/components/roon/icons.json +++ b/homeassistant/components/roon/icons.json @@ -1,5 +1,7 @@ { "services": { - "transfer": "mdi:monitor-multiple" + "transfer": { + "service": "mdi:monitor-multiple" + } } } diff --git a/homeassistant/components/route53/icons.json b/homeassistant/components/route53/icons.json index 30a854991f0093..5afe13ce9499c1 100644 --- a/homeassistant/components/route53/icons.json +++ b/homeassistant/components/route53/icons.json @@ -1,5 +1,7 @@ { "services": { - "update_records": "mdi:database-refresh" + "update_records": { + "service": "mdi:database-refresh" + } } } diff --git a/homeassistant/components/rova/config_flow.py b/homeassistant/components/rova/config_flow.py index e5e3a31b8af4be..a28e6202466773 100644 --- a/homeassistant/components/rova/config_flow.py +++ b/homeassistant/components/rova/config_flow.py @@ -60,11 +60,11 @@ async def async_step_user( errors=errors, ) - async def async_step_import(self, user_input: dict[str, Any]) -> ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Import the yaml config.""" - zip_code = user_input[CONF_ZIP_CODE] - number = user_input[CONF_HOUSE_NUMBER] - suffix = user_input[CONF_HOUSE_NUMBER_SUFFIX] + zip_code = import_data[CONF_ZIP_CODE] + number = import_data[CONF_HOUSE_NUMBER] + suffix = import_data[CONF_HOUSE_NUMBER_SUFFIX] await self.async_set_unique_id(f"{zip_code}{number}{suffix}".strip()) self._abort_if_unique_id_configured() diff --git a/homeassistant/components/ruckus_unleashed/manifest.json b/homeassistant/components/ruckus_unleashed/manifest.json index edaf0aa95d269b..039840efc14f20 100644 --- a/homeassistant/components/ruckus_unleashed/manifest.json +++ b/homeassistant/components/ruckus_unleashed/manifest.json @@ -6,6 +6,6 @@ "documentation": "https://www.home-assistant.io/integrations/ruckus_unleashed", "integration_type": "hub", "iot_class": "local_polling", - "loggers": ["aioruckus", "xmltodict"], - "requirements": ["aioruckus==0.34"] + "loggers": ["aioruckus"], + "requirements": ["aioruckus==0.41"] } diff --git a/homeassistant/components/russound_rio/config_flow.py b/homeassistant/components/russound_rio/config_flow.py index e25ac7dde2e5db..df173d29f6118d 100644 --- a/homeassistant/components/russound_rio/config_flow.py +++ b/homeassistant/components/russound_rio/config_flow.py @@ -80,13 +80,11 @@ async def async_step_user( step_id="user", data_schema=DATA_SCHEMA, errors=errors ) - async def async_step_import( - self, import_config: dict[str, Any] - ) -> ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Attempt to import the existing configuration.""" - self._async_abort_entries_match({CONF_HOST: import_config[CONF_HOST]}) - host = import_config[CONF_HOST] - port = import_config.get(CONF_PORT, 9621) + self._async_abort_entries_match({CONF_HOST: import_data[CONF_HOST]}) + host = import_data[CONF_HOST] + port = import_data.get(CONF_PORT, 9621) # Connection logic is repeated here since this method will be removed in future releases russ = Russound(self.hass.loop, host, port) diff --git a/homeassistant/components/sabnzbd/config_flow.py b/homeassistant/components/sabnzbd/config_flow.py index 944c3f2936cd88..2637659e91a98a 100644 --- a/homeassistant/components/sabnzbd/config_flow.py +++ b/homeassistant/components/sabnzbd/config_flow.py @@ -65,7 +65,7 @@ async def async_step_user( errors=errors, ) - async def async_step_import(self, import_data): + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Import sabnzbd config from configuration.yaml.""" protocol = "https://" if import_data[CONF_SSL] else "http://" import_data[CONF_URL] = ( diff --git a/homeassistant/components/sabnzbd/icons.json b/homeassistant/components/sabnzbd/icons.json index a693e9fec86d89..ca4f4d584ae1fb 100644 --- a/homeassistant/components/sabnzbd/icons.json +++ b/homeassistant/components/sabnzbd/icons.json @@ -1,7 +1,13 @@ { "services": { - "pause": "mdi:pause", - "resume": "mdi:play", - "set_speed": "mdi:speedometer" + "pause": { + "service": "mdi:pause" + }, + "resume": { + "service": "mdi:play" + }, + "set_speed": { + "service": "mdi:speedometer" + } } } diff --git a/homeassistant/components/scene/icons.json b/homeassistant/components/scene/icons.json index 563c0f31ddcd79..b08d06fb43472a 100644 --- a/homeassistant/components/scene/icons.json +++ b/homeassistant/components/scene/icons.json @@ -5,10 +5,20 @@ } }, "services": { - "turn_on": "mdi:power", - "reload": "mdi:reload", - "apply": "mdi:check", - "create": "mdi:plus", - "delete": "mdi:delete" + "turn_on": { + "service": "mdi:power" + }, + "reload": { + "service": "mdi:reload" + }, + "apply": { + "service": "mdi:check" + }, + "create": { + "service": "mdi:plus" + }, + "delete": { + "service": "mdi:delete" + } } } diff --git a/homeassistant/components/schedule/icons.json b/homeassistant/components/schedule/icons.json index a03163179cb5bd..a9829425570a0c 100644 --- a/homeassistant/components/schedule/icons.json +++ b/homeassistant/components/schedule/icons.json @@ -1,5 +1,7 @@ { "services": { - "reload": "mdi:reload" + "reload": { + "service": "mdi:reload" + } } } diff --git a/homeassistant/components/screenlogic/config_flow.py b/homeassistant/components/screenlogic/config_flow.py index 74a01fdeaa2ae1..4a46756cf2f6a1 100644 --- a/homeassistant/components/screenlogic/config_flow.py +++ b/homeassistant/components/screenlogic/config_flow.py @@ -32,9 +32,9 @@ PENTAIR_OUI = "00-C0-33" -async def async_discover_gateways_by_unique_id(hass): +async def async_discover_gateways_by_unique_id() -> dict[str, dict[str, Any]]: """Discover gateways and return a dict of them by unique id.""" - discovered_gateways = {} + discovered_gateways: dict[str, dict[str, Any]] = {} try: hosts = await discovery.async_discover() _LOGGER.debug("Discovered hosts: %s", hosts) @@ -51,16 +51,16 @@ async def async_discover_gateways_by_unique_id(hass): return discovered_gateways -def _extract_mac_from_name(name): +def _extract_mac_from_name(name: str) -> str: return format_mac(f"{PENTAIR_OUI}-{name.split(':')[1].strip()}") -def short_mac(mac): +def short_mac(mac: str) -> str: """Short version of the mac as seen in the app.""" return "-".join(mac.split(":")[3:]).upper() -def name_for_mac(mac): +def name_for_mac(mac: str) -> str: """Derive the gateway name from the mac.""" return f"Pentair: {short_mac(mac)}" @@ -83,9 +83,11 @@ def async_get_options_flow( """Get the options flow for ScreenLogic.""" return ScreenLogicOptionsFlowHandler(config_entry) - async def async_step_user(self, user_input=None) -> ConfigFlowResult: + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the start of the config flow.""" - self.discovered_gateways = await async_discover_gateways_by_unique_id(self.hass) + self.discovered_gateways = await async_discover_gateways_by_unique_id() return await self.async_step_gateway_select() async def async_step_dhcp( diff --git a/homeassistant/components/screenlogic/coordinator.py b/homeassistant/components/screenlogic/coordinator.py index 281bac86e01025..a90c9cb2cf40d5 100644 --- a/homeassistant/components/screenlogic/coordinator.py +++ b/homeassistant/components/screenlogic/coordinator.py @@ -2,6 +2,7 @@ from datetime import timedelta import logging +from typing import TYPE_CHECKING from screenlogicpy import ScreenLogicGateway from screenlogicpy.const.common import ( @@ -33,11 +34,13 @@ async def async_get_connect_info( """Construct connect_info from configuration entry and returns it to caller.""" mac = entry.unique_id # Attempt to rediscover gateway to follow IP changes - discovered_gateways = await async_discover_gateways_by_unique_id(hass) + discovered_gateways = await async_discover_gateways_by_unique_id() if mac in discovered_gateways: return discovered_gateways[mac] _LOGGER.debug("Gateway rediscovery failed for %s", entry.title) + if TYPE_CHECKING: + assert mac is not None # Static connection defined or fallback from discovery return { SL_GATEWAY_NAME: name_for_mac(mac), diff --git a/homeassistant/components/screenlogic/icons.json b/homeassistant/components/screenlogic/icons.json index d8d021c20e62e1..ef8dc46f61d61a 100644 --- a/homeassistant/components/screenlogic/icons.json +++ b/homeassistant/components/screenlogic/icons.json @@ -1,7 +1,13 @@ { "services": { - "set_color_mode": "mdi:palette", - "start_super_chlorination": "mdi:pool", - "stop_super_chlorination": "mdi:pool" + "set_color_mode": { + "service": "mdi:palette" + }, + "start_super_chlorination": { + "service": "mdi:pool" + }, + "stop_super_chlorination": { + "service": "mdi:pool" + } } } diff --git a/homeassistant/components/script/icons.json b/homeassistant/components/script/icons.json index d253d0fd829902..7e160941c0581d 100644 --- a/homeassistant/components/script/icons.json +++ b/homeassistant/components/script/icons.json @@ -8,9 +8,17 @@ } }, "services": { - "reload": "mdi:reload", - "turn_on": "mdi:script-text-play", - "turn_off": "mdi:script-text", - "toggle": "mdi:script-text" + "reload": { + "service": "mdi:reload" + }, + "turn_on": { + "service": "mdi:script-text-play" + }, + "turn_off": { + "service": "mdi:script-text" + }, + "toggle": { + "service": "mdi:script-text" + } } } diff --git a/homeassistant/components/select/icons.json b/homeassistant/components/select/icons.json index 1b440d2a1defbe..fbd1d4568f1072 100644 --- a/homeassistant/components/select/icons.json +++ b/homeassistant/components/select/icons.json @@ -5,10 +5,20 @@ } }, "services": { - "select_first": "mdi:format-list-bulleted", - "select_last": "mdi:format-list-bulleted", - "select_next": "mdi:format-list-bulleted", - "select_option": "mdi:format-list-bulleted", - "select_previous": "mdi:format-list-bulleted" + "select_first": { + "service": "mdi:format-list-bulleted" + }, + "select_last": { + "service": "mdi:format-list-bulleted" + }, + "select_next": { + "service": "mdi:format-list-bulleted" + }, + "select_option": { + "service": "mdi:format-list-bulleted" + }, + "select_previous": { + "service": "mdi:format-list-bulleted" + } } } diff --git a/homeassistant/components/sense/config_flow.py b/homeassistant/components/sense/config_flow.py index dab80b99e1a5f7..c0df40aec9d2aa 100644 --- a/homeassistant/components/sense/config_flow.py +++ b/homeassistant/components/sense/config_flow.py @@ -34,13 +34,13 @@ class SenseConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - def __init__(self): + _gateway: ASyncSenseable + + def __init__(self) -> None: """Init Config .""" - self._gateway = None - self._auth_data = {} - super().__init__() + self._auth_data: dict[str, Any] = {} - async def validate_input(self, data): + async def validate_input(self, data: Mapping[str, Any]) -> None: """Validate the user input allows us to connect. Data has the keys from DATA_SCHEMA with values provided by the user. @@ -64,7 +64,7 @@ async def validate_input(self, data): self._auth_data[CONF_EMAIL], self._auth_data[CONF_PASSWORD] ) - async def create_entry_from_data(self): + async def create_entry_from_data(self) -> ConfigFlowResult: """Create the entry from the config data.""" self._auth_data["access_token"] = self._gateway.sense_access_token self._auth_data["user_id"] = self._gateway.sense_user_id @@ -79,7 +79,9 @@ async def create_entry_from_data(self): return self.async_update_reload_and_abort(existing_entry, data=self._auth_data) - async def validate_input_and_create_entry(self, user_input, errors): + async def validate_input_and_create_entry( + self, user_input: Mapping[str, Any], errors: dict[str, str] + ) -> ConfigFlowResult | None: """Validate the input and create the entry from the data.""" try: await self.validate_input(user_input) @@ -96,7 +98,9 @@ async def validate_input_and_create_entry(self, user_input, errors): return await self.create_entry_from_data() return None - async def async_step_validation(self, user_input=None): + async def async_step_validation( + self, user_input: dict[str, str] | None = None + ) -> ConfigFlowResult: """Handle validation (2fa) step.""" errors = {} if user_input: @@ -118,9 +122,11 @@ async def async_step_validation(self, user_input=None): errors=errors, ) - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" - errors = {} + errors: dict[str, str] = {} if user_input is not None: if result := await self.validate_input_and_create_entry(user_input, errors): return result @@ -136,9 +142,11 @@ async def async_step_reauth( self._auth_data = dict(entry_data) return await self.async_step_reauth_validate(entry_data) - async def async_step_reauth_validate(self, user_input=None): + async def async_step_reauth_validate( + self, user_input: Mapping[str, Any] + ) -> ConfigFlowResult: """Handle reauth and validation.""" - errors = {} + errors: dict[str, str] = {} if user_input is not None: if result := await self.validate_input_and_create_entry(user_input, errors): return result diff --git a/homeassistant/components/sensibo/icons.json b/homeassistant/components/sensibo/icons.json index e26840e48eb4cb..ccab3c198d2c0c 100644 --- a/homeassistant/components/sensibo/icons.json +++ b/homeassistant/components/sensibo/icons.json @@ -45,10 +45,20 @@ } }, "services": { - "assume_state": "mdi:shape-outline", - "enable_timer": "mdi:timer-play", - "enable_pure_boost": "mdi:air-filter", - "full_state": "mdi:shape", - "enable_climate_react": "mdi:wizard-hat" + "assume_state": { + "service": "mdi:shape-outline" + }, + "enable_timer": { + "service": "mdi:timer-play" + }, + "enable_pure_boost": { + "service": "mdi:air-filter" + }, + "full_state": { + "service": "mdi:shape" + }, + "enable_climate_react": { + "service": "mdi:wizard-hat" + } } } diff --git a/homeassistant/components/sensor/icons.json b/homeassistant/components/sensor/icons.json index f23826cfe95cd1..6132fcbc1e92e4 100644 --- a/homeassistant/components/sensor/icons.json +++ b/homeassistant/components/sensor/icons.json @@ -18,6 +18,9 @@ "carbon_monoxide": { "default": "mdi:molecule-co" }, + "conductivity": { + "default": "mdi:sprout-outline" + }, "current": { "default": "mdi:current-ac" }, diff --git a/homeassistant/components/sensor/recorder.py b/homeassistant/components/sensor/recorder.py index c02c3ce7b7a62a..fce41a13ca61ca 100644 --- a/homeassistant/components/sensor/recorder.py +++ b/homeassistant/components/sensor/recorder.py @@ -33,6 +33,7 @@ from homeassistant.core import HomeAssistant, State, split_entity_id from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.entity import entity_sources +from homeassistant.helpers.typing import UNDEFINED, UndefinedType from homeassistant.loader import async_suggest_report_issue from homeassistant.util import dt as dt_util from homeassistant.util.enum import try_parse_enum @@ -220,13 +221,13 @@ def _normalize_states( LINK_DEV_STATISTICS, ) return None, [] - state_unit = fstates[0][1].attributes.get(ATTR_UNIT_OF_MEASUREMENT) + return state_unit, fstates converter = statistics.STATISTIC_UNIT_TO_UNIT_CONVERTER[statistics_unit] valid_fstates: list[tuple[float, State]] = [] convert: Callable[[float], float] | None = None - last_unit: str | None | object = object() + last_unit: str | None | UndefinedType = UNDEFINED valid_units = converter.VALID_UNITS for fstate, state in fstates: @@ -640,32 +641,31 @@ def list_statistic_ids( result: dict[str, StatisticMetaData] = {} for state in entities: - state_class = state.attributes[ATTR_STATE_CLASS] - state_unit = state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) + entity_id = state.entity_id + if statistic_ids is not None and entity_id not in statistic_ids: + continue + attributes = state.attributes + state_class = attributes[ATTR_STATE_CLASS] provided_statistics = DEFAULT_STATISTICS[state_class] if statistic_type is not None and statistic_type not in provided_statistics: continue - if statistic_ids is not None and state.entity_id not in statistic_ids: - continue - if ( - "sum" in provided_statistics - and ATTR_LAST_RESET not in state.attributes - and state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT + (has_sum := "sum" in provided_statistics) + and ATTR_LAST_RESET not in attributes + and state_class == SensorStateClass.MEASUREMENT ): continue - result[state.entity_id] = { + result[entity_id] = { "has_mean": "mean" in provided_statistics, - "has_sum": "sum" in provided_statistics, + "has_sum": has_sum, "name": None, "source": RECORDER_DOMAIN, - "statistic_id": state.entity_id, - "unit_of_measurement": state_unit, + "statistic_id": entity_id, + "unit_of_measurement": attributes.get(ATTR_UNIT_OF_MEASUREMENT), } - continue return result diff --git a/homeassistant/components/seventeentrack/__init__.py b/homeassistant/components/seventeentrack/__init__.py index 56d87b1935d7ec..695ca1799667dd 100644 --- a/homeassistant/components/seventeentrack/__init__.py +++ b/homeassistant/components/seventeentrack/__init__.py @@ -1,136 +1,30 @@ """The seventeentrack component.""" -from typing import Final - from pyseventeentrack import Client as SeventeenTrackClient from pyseventeentrack.errors import SeventeenTrackError -from pyseventeentrack.package import PACKAGE_STATUS_MAP -import voluptuous as vol - -from homeassistant.config_entries import ConfigEntry, ConfigEntryState -from homeassistant.const import ( - ATTR_FRIENDLY_NAME, - ATTR_LOCATION, - CONF_PASSWORD, - CONF_USERNAME, - Platform, -) -from homeassistant.core import ( - HomeAssistant, - ServiceCall, - ServiceResponse, - SupportsResponse, -) -from homeassistant.exceptions import ConfigEntryNotReady, ServiceValidationError -from homeassistant.helpers import config_validation as cv, selector + +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import ConfigEntryNotReady +from homeassistant.helpers import config_validation as cv from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.typing import ConfigType -from homeassistant.util import slugify - -from .const import ( - ATTR_CONFIG_ENTRY_ID, - ATTR_DESTINATION_COUNTRY, - ATTR_INFO_TEXT, - ATTR_ORIGIN_COUNTRY, - ATTR_PACKAGE_STATE, - ATTR_PACKAGE_TYPE, - ATTR_STATUS, - ATTR_TIMESTAMP, - ATTR_TRACKING_INFO_LANGUAGE, - ATTR_TRACKING_NUMBER, - DOMAIN, - SERVICE_GET_PACKAGES, -) + +from .const import DOMAIN from .coordinator import SeventeenTrackCoordinator +from .services import setup_services PLATFORMS: list[Platform] = [Platform.SENSOR] CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN) -SERVICE_SCHEMA: Final = vol.Schema( - { - vol.Required(ATTR_CONFIG_ENTRY_ID): selector.ConfigEntrySelector( - { - "integration": DOMAIN, - } - ), - vol.Optional(ATTR_PACKAGE_STATE): selector.SelectSelector( - selector.SelectSelectorConfig( - multiple=True, - options=[ - value.lower().replace(" ", "_") - for value in PACKAGE_STATUS_MAP.values() - ], - mode=selector.SelectSelectorMode.DROPDOWN, - translation_key=ATTR_PACKAGE_STATE, - ) - ), - } -) - async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up the 17Track component.""" - async def get_packages(call: ServiceCall) -> ServiceResponse: - """Get packages from 17Track.""" - config_entry_id = call.data[ATTR_CONFIG_ENTRY_ID] - package_states = call.data.get(ATTR_PACKAGE_STATE, []) - - entry: ConfigEntry | None = hass.config_entries.async_get_entry(config_entry_id) - - if not entry: - raise ServiceValidationError( - translation_domain=DOMAIN, - translation_key="invalid_config_entry", - translation_placeholders={ - "config_entry_id": config_entry_id, - }, - ) - if entry.state != ConfigEntryState.LOADED: - raise ServiceValidationError( - translation_domain=DOMAIN, - translation_key="unloaded_config_entry", - translation_placeholders={ - "config_entry_id": entry.title, - }, - ) - - seventeen_coordinator: SeventeenTrackCoordinator = hass.data[DOMAIN][ - config_entry_id - ] - live_packages = sorted( - await seventeen_coordinator.client.profile.packages( - show_archived=seventeen_coordinator.show_archived - ) - ) - - return { - "packages": [ - { - ATTR_DESTINATION_COUNTRY: package.destination_country, - ATTR_ORIGIN_COUNTRY: package.origin_country, - ATTR_PACKAGE_TYPE: package.package_type, - ATTR_TRACKING_INFO_LANGUAGE: package.tracking_info_language, - ATTR_TRACKING_NUMBER: package.tracking_number, - ATTR_LOCATION: package.location, - ATTR_STATUS: package.status, - ATTR_TIMESTAMP: package.timestamp, - ATTR_INFO_TEXT: package.info_text, - ATTR_FRIENDLY_NAME: package.friendly_name, - } - for package in live_packages - if slugify(package.status) in package_states or package_states == [] - ] - } - - hass.services.async_register( - DOMAIN, - SERVICE_GET_PACKAGES, - get_packages, - schema=SERVICE_SCHEMA, - supports_response=SupportsResponse.ONLY, - ) + setup_services(hass) + return True diff --git a/homeassistant/components/seventeentrack/const.py b/homeassistant/components/seventeentrack/const.py index 584eca507e94ba..6b888590600c53 100644 --- a/homeassistant/components/seventeentrack/const.py +++ b/homeassistant/components/seventeentrack/const.py @@ -42,8 +42,11 @@ VALUE_DELIVERED = "Delivered" SERVICE_GET_PACKAGES = "get_packages" +SERVICE_ARCHIVE_PACKAGE = "archive_package" ATTR_PACKAGE_STATE = "package_state" +ATTR_PACKAGE_TRACKING_NUMBER = "package_tracking_number" ATTR_CONFIG_ENTRY_ID = "config_entry_id" + DEPRECATED_KEY = "deprecated" diff --git a/homeassistant/components/seventeentrack/icons.json b/homeassistant/components/seventeentrack/icons.json index 78ca65edc4d7a6..a5cac0a9f84333 100644 --- a/homeassistant/components/seventeentrack/icons.json +++ b/homeassistant/components/seventeentrack/icons.json @@ -28,6 +28,11 @@ } }, "services": { - "get_packages": "mdi:package" + "get_packages": { + "service": "mdi:package" + }, + "archive_package": { + "service": "mdi:archive" + } } } diff --git a/homeassistant/components/seventeentrack/services.py b/homeassistant/components/seventeentrack/services.py new file mode 100644 index 00000000000000..9a7a4d2d4b60d9 --- /dev/null +++ b/homeassistant/components/seventeentrack/services.py @@ -0,0 +1,145 @@ +"""Services for the seventeentrack integration.""" + +from typing import Final + +from pyseventeentrack.package import PACKAGE_STATUS_MAP +import voluptuous as vol + +from homeassistant.config_entries import ConfigEntry, ConfigEntryState +from homeassistant.const import ATTR_FRIENDLY_NAME, ATTR_LOCATION +from homeassistant.core import ( + HomeAssistant, + ServiceCall, + ServiceResponse, + SupportsResponse, +) +from homeassistant.exceptions import ServiceValidationError +from homeassistant.helpers import config_validation as cv, selector +from homeassistant.util import slugify + +from . import SeventeenTrackCoordinator +from .const import ( + ATTR_CONFIG_ENTRY_ID, + ATTR_DESTINATION_COUNTRY, + ATTR_INFO_TEXT, + ATTR_ORIGIN_COUNTRY, + ATTR_PACKAGE_STATE, + ATTR_PACKAGE_TRACKING_NUMBER, + ATTR_PACKAGE_TYPE, + ATTR_STATUS, + ATTR_TIMESTAMP, + ATTR_TRACKING_INFO_LANGUAGE, + ATTR_TRACKING_NUMBER, + DOMAIN, + SERVICE_ARCHIVE_PACKAGE, + SERVICE_GET_PACKAGES, +) + +SERVICE_ADD_PACKAGES_SCHEMA: Final = vol.Schema( + { + vol.Required(ATTR_CONFIG_ENTRY_ID): cv.string, + vol.Optional(ATTR_PACKAGE_STATE): selector.SelectSelector( + selector.SelectSelectorConfig( + multiple=True, + options=[ + value.lower().replace(" ", "_") + for value in PACKAGE_STATUS_MAP.values() + ], + mode=selector.SelectSelectorMode.DROPDOWN, + translation_key=ATTR_PACKAGE_STATE, + ) + ), + } +) + +SERVICE_ARCHIVE_PACKAGE_SCHEMA: Final = vol.Schema( + { + vol.Required(ATTR_CONFIG_ENTRY_ID): cv.string, + vol.Required(ATTR_PACKAGE_TRACKING_NUMBER): cv.string, + } +) + + +def setup_services(hass: HomeAssistant) -> None: + """Set up the services for the seventeentrack integration.""" + + async def get_packages(call: ServiceCall) -> ServiceResponse: + """Get packages from 17Track.""" + config_entry_id = call.data[ATTR_CONFIG_ENTRY_ID] + package_states = call.data.get(ATTR_PACKAGE_STATE, []) + + await _validate_service(config_entry_id) + + seventeen_coordinator: SeventeenTrackCoordinator = hass.data[DOMAIN][ + config_entry_id + ] + live_packages = sorted( + await seventeen_coordinator.client.profile.packages( + show_archived=seventeen_coordinator.show_archived + ) + ) + + return { + "packages": [ + { + ATTR_DESTINATION_COUNTRY: package.destination_country, + ATTR_ORIGIN_COUNTRY: package.origin_country, + ATTR_PACKAGE_TYPE: package.package_type, + ATTR_TRACKING_INFO_LANGUAGE: package.tracking_info_language, + ATTR_TRACKING_NUMBER: package.tracking_number, + ATTR_LOCATION: package.location, + ATTR_STATUS: package.status, + ATTR_TIMESTAMP: package.timestamp, + ATTR_INFO_TEXT: package.info_text, + ATTR_FRIENDLY_NAME: package.friendly_name, + } + for package in live_packages + if slugify(package.status) in package_states or package_states == [] + ] + } + + async def archive_package(call: ServiceCall) -> None: + config_entry_id = call.data[ATTR_CONFIG_ENTRY_ID] + tracking_number = call.data[ATTR_PACKAGE_TRACKING_NUMBER] + + await _validate_service(config_entry_id) + + seventeen_coordinator: SeventeenTrackCoordinator = hass.data[DOMAIN][ + config_entry_id + ] + + await seventeen_coordinator.client.profile.archive_package(tracking_number) + + async def _validate_service(config_entry_id): + entry: ConfigEntry | None = hass.config_entries.async_get_entry(config_entry_id) + if not entry: + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="invalid_config_entry", + translation_placeholders={ + "config_entry_id": config_entry_id, + }, + ) + if entry.state != ConfigEntryState.LOADED: + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="unloaded_config_entry", + translation_placeholders={ + "config_entry_id": entry.title, + }, + ) + + hass.services.async_register( + DOMAIN, + SERVICE_GET_PACKAGES, + get_packages, + schema=SERVICE_ADD_PACKAGES_SCHEMA, + supports_response=SupportsResponse.ONLY, + ) + + hass.services.async_register( + DOMAIN, + SERVICE_ARCHIVE_PACKAGE, + archive_package, + schema=SERVICE_ARCHIVE_PACKAGE_SCHEMA, + ) diff --git a/homeassistant/components/seventeentrack/services.yaml b/homeassistant/components/seventeentrack/services.yaml index 41cb66ada5fe03..d4592dc8aabf84 100644 --- a/homeassistant/components/seventeentrack/services.yaml +++ b/homeassistant/components/seventeentrack/services.yaml @@ -18,3 +18,14 @@ get_packages: selector: config_entry: integration: seventeentrack +archive_package: + fields: + package_tracking_number: + required: true + selector: + text: + config_entry_id: + required: true + selector: + config_entry: + integration: seventeentrack diff --git a/homeassistant/components/seventeentrack/strings.json b/homeassistant/components/seventeentrack/strings.json index 0fbac13736e65a..fda5575ff951dc 100644 --- a/homeassistant/components/seventeentrack/strings.json +++ b/homeassistant/components/seventeentrack/strings.json @@ -100,6 +100,20 @@ "description": "The packages will be retrieved for the selected service." } } + }, + "archive_package": { + "name": "Archive package", + "description": "Archive a package", + "fields": { + "package_tracking_number": { + "name": "Package tracking number", + "description": "The package will be archived for the specified tracking number." + }, + "config_entry_id": { + "name": "[%key:component::seventeentrack::services::get_packages::fields::config_entry_id::name%]", + "description": "The package will be archived for the selected service." + } + } } }, "selector": { diff --git a/homeassistant/components/sharkiq/config_flow.py b/homeassistant/components/sharkiq/config_flow.py index 492b8f2a36527c..87367fcf093ea4 100644 --- a/homeassistant/components/sharkiq/config_flow.py +++ b/homeassistant/components/sharkiq/config_flow.py @@ -116,9 +116,15 @@ async def async_step_user( ) async def async_step_reauth( - self, user_input: Mapping[str, Any] + self, entry_data: Mapping[str, Any] ) -> ConfigFlowResult: """Handle re-auth if login is invalid.""" + return await self.async_step_reauth_confirm() + + async def async_step_reauth_confirm( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle a flow initiated by reauthentication.""" errors: dict[str, str] = {} if user_input is not None: @@ -134,7 +140,7 @@ async def async_step_reauth( return self.async_abort(reason=errors["base"]) return self.async_show_form( - step_id="reauth", + step_id="reauth_confirm", data_schema=SHARKIQ_SCHEMA, errors=errors, ) diff --git a/homeassistant/components/sharkiq/icons.json b/homeassistant/components/sharkiq/icons.json index 13fd58ce66d4d7..e58a317f5034f7 100644 --- a/homeassistant/components/sharkiq/icons.json +++ b/homeassistant/components/sharkiq/icons.json @@ -1,5 +1,7 @@ { "services": { - "clean_room": "mdi:robot-vacuum" + "clean_room": { + "service": "mdi:robot-vacuum" + } } } diff --git a/homeassistant/components/sharkiq/strings.json b/homeassistant/components/sharkiq/strings.json index 63d4f6af48b35c..40b569e13b750e 100644 --- a/homeassistant/components/sharkiq/strings.json +++ b/homeassistant/components/sharkiq/strings.json @@ -13,7 +13,7 @@ "region": "Shark IQ uses different services in the EU. Select your region to connect to the correct service for your account." } }, - "reauth": { + "reauth_confirm": { "data": { "username": "[%key:common::config_flow::data::username%]", "password": "[%key:common::config_flow::data::password%]", diff --git a/homeassistant/components/shelly/__init__.py b/homeassistant/components/shelly/__init__.py index 994b1ed04304c5..e0d9d17d55d452 100644 --- a/homeassistant/components/shelly/__init__.py +++ b/homeassistant/components/shelly/__init__.py @@ -21,6 +21,7 @@ from homeassistant.helpers import ( config_validation as cv, device_registry as dr, + entity_registry as er, issue_registry as ir, ) from homeassistant.helpers.aiohttp_client import async_get_clientsession @@ -192,8 +193,15 @@ async def _async_setup_block_entry( await hass.config_entries.async_forward_entry_setups( entry, runtime_data.platforms ) - elif sleep_period is None or device_entry is None: + elif ( + sleep_period is None + or device_entry is None + or not er.async_entries_for_device(er.async_get(hass), device_entry.id) + ): # Need to get sleep info or first time sleeping device setup, wait for device + # If there are no entities for the device, it means we added the device, but + # Home Assistant was restarted before the device was online. In this case we + # cannot restore the entities, so we need to wait for the device to be online. LOGGER.debug( "Setup for device %s will resume when device is online", entry.title ) @@ -268,13 +276,25 @@ async def _async_setup_rpc_entry(hass: HomeAssistant, entry: ShellyConfigEntry) await hass.config_entries.async_forward_entry_setups( entry, runtime_data.platforms ) - elif sleep_period is None or device_entry is None: + elif ( + sleep_period is None + or device_entry is None + or not er.async_entries_for_device(er.async_get(hass), device_entry.id) + ): # Need to get sleep info or first time sleeping device setup, wait for device + # If there are no entities for the device, it means we added the device, but + # Home Assistant was restarted before the device was online. In this case we + # cannot restore the entities, so we need to wait for the device to be online. LOGGER.debug( "Setup for device %s will resume when device is online", entry.title ) runtime_data.rpc = ShellyRpcCoordinator(hass, entry, device) runtime_data.rpc.async_setup(runtime_data.platforms) + # Try to connect to the device, if we reached here from config flow + # and user woke up the device when adding it, we can continue setup + # otherwise we will wait for the device to wake up + if sleep_period: + await runtime_data.rpc.async_device_online("setup") else: # Restore sensors for sleeping device LOGGER.debug("Setting up offline RPC device %s", entry.title) diff --git a/homeassistant/components/shelly/const.py b/homeassistant/components/shelly/const.py index 1759f4bdd189a1..fe4108a1f52b60 100644 --- a/homeassistant/components/shelly/const.py +++ b/homeassistant/components/shelly/const.py @@ -254,3 +254,6 @@ class BLEScannerMode(StrEnum): "field": NumberMode.BOX, "slider": NumberMode.SLIDER, } + + +API_WS_URL = "/api/shelly/ws" diff --git a/homeassistant/components/shelly/coordinator.py b/homeassistant/components/shelly/coordinator.py index 2710565f9601c2..c8e6cc03a06c58 100644 --- a/homeassistant/components/shelly/coordinator.py +++ b/homeassistant/components/shelly/coordinator.py @@ -6,6 +6,7 @@ from collections.abc import Callable, Coroutine from dataclasses import dataclass from datetime import timedelta +from functools import cached_property from typing import Any, cast from aioshelly.ble import async_ensure_ble_enabled, async_stop_scanner @@ -64,6 +65,7 @@ get_host, get_http_port, get_rpc_device_wakeup_period, + get_rpc_ws_url, update_device_fw_info, ) @@ -101,6 +103,9 @@ def __init__( self._pending_platforms: list[Platform] | None = None device_name = device.name if device.initialized else entry.title interval_td = timedelta(seconds=update_interval) + # The device has come online at least once. In the case of a sleeping RPC + # device, this means that the device has connected to the WS server at least once. + self._came_online_once = False super().__init__(hass, LOGGER, name=device_name, update_interval=interval_td) self._debounced_reload: Debouncer[Coroutine[Any, Any, None]] = Debouncer( @@ -116,12 +121,12 @@ def __init__( hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, self._handle_ha_stop) ) - @property + @cached_property def model(self) -> str: """Model of the device.""" return cast(str, self.entry.data["model"]) - @property + @cached_property def mac(self) -> str: """Mac address of the device.""" return cast(str, self.entry.unique_id) @@ -169,7 +174,7 @@ async def _async_device_connect_task(self) -> bool: await self.device.initialize() update_device_fw_info(self.hass, self.device, self.entry) except DeviceConnectionError as err: - LOGGER.error( + LOGGER.debug( "Error connecting to Shelly device %s, error: %r", self.name, err ) return False @@ -184,7 +189,7 @@ async def _async_device_connect_task(self) -> bool: if not self._pending_platforms: return True - LOGGER.debug("Device %s is online, resuming setup", self.entry.title) + LOGGER.debug("Device %s is online, resuming setup", self.name) platforms = self._pending_platforms self._pending_platforms = None @@ -372,6 +377,7 @@ def _async_handle_update( """Handle device update.""" LOGGER.debug("Shelly %s handle update, type: %s", self.name, update_type) if update_type is BlockUpdateType.ONLINE: + self._came_online_once = True self.entry.async_create_background_task( self.hass, self._async_device_connect_task(), @@ -472,9 +478,26 @@ def __init__( self._event_listeners: list[Callable[[dict[str, Any]], None]] = [] self._ota_event_listeners: list[Callable[[dict[str, Any]], None]] = [] self._input_event_listeners: list[Callable[[dict[str, Any]], None]] = [] - + self._connect_task: asyncio.Task | None = None entry.async_on_unload(entry.add_update_listener(self._async_update_listener)) + async def async_device_online(self, source: str) -> None: + """Handle device going online.""" + if not self.sleep_period: + await self.async_request_refresh() + elif not self._came_online_once or not self.device.initialized: + LOGGER.debug( + "Sleepy device %s is online (source: %s), trying to poll and configure", + self.name, + source, + ) + # Source told us the device is online, try to poll + # the device and if possible, set up the outbound + # websocket so the device will send us updates + # instead of relying on polling it fast enough before + # it goes to sleep again + self._async_handle_rpc_device_online() + def update_sleep_period(self) -> bool: """Check device sleep period & update if changed.""" if ( @@ -598,15 +621,15 @@ async def _async_update_data(self) -> None: async def _async_disconnected(self, reconnect: bool) -> None: """Handle device disconnected.""" - # Sleeping devices send data and disconnect - # There are no disconnect events for sleeping devices - if self.sleep_period: - return - async with self._connection_lock: if not self.connected: # Already disconnected return self.connected = False + # Sleeping devices send data and disconnect + # There are no disconnect events for sleeping devices + # but we do need to make sure self.connected is False + if self.sleep_period: + return self._async_run_disconnected_events() # Try to reconnect right away if triggered by disconnect event if reconnect: @@ -645,6 +668,21 @@ async def _async_run_connected_events(self) -> None: """ if not self.sleep_period: await self._async_connect_ble_scanner() + else: + await self._async_setup_outbound_websocket() + + async def _async_setup_outbound_websocket(self) -> None: + """Set up outbound websocket if it is not enabled.""" + config = self.device.config + if ( + (ws_config := config.get("ws")) + and (not ws_config["server"] or not ws_config["enable"]) + and (ws_url := get_rpc_ws_url(self.hass)) + ): + LOGGER.debug( + "Setting up outbound websocket for device %s - %s", self.name, ws_url + ) + await self.device.update_outbound_websocket(ws_url) async def _async_connect_ble_scanner(self) -> None: """Connect BLE scanner.""" @@ -662,6 +700,21 @@ async def _async_connect_ble_scanner(self) -> None: await async_connect_scanner(self.hass, self, ble_scanner_mode) ) + @callback + def _async_handle_rpc_device_online(self) -> None: + """Handle device going online.""" + if self.device.connected or ( + self._connect_task and not self._connect_task.done() + ): + LOGGER.debug("Device %s already connected/connecting", self.name) + return + self._connect_task = self.entry.async_create_background_task( + self.hass, + self._async_device_connect_task(), + "rpc device online", + eager_start=True, + ) + @callback def _async_handle_update( self, device_: RpcDevice, update_type: RpcUpdateType @@ -669,19 +722,13 @@ def _async_handle_update( """Handle device update.""" LOGGER.debug("Shelly %s handle update, type: %s", self.name, update_type) if update_type is RpcUpdateType.ONLINE: - if self.device.connected: - LOGGER.debug("Device %s already connected", self.name) - return - self.entry.async_create_background_task( - self.hass, - self._async_device_connect_task(), - "rpc device online", - eager_start=True, - ) + self._came_online_once = True + self._async_handle_rpc_device_online() elif update_type is RpcUpdateType.INITIALIZED: self.entry.async_create_background_task( self.hass, self._async_connected(), "rpc device init", eager_start=True ) + # Make sure entities are marked available self.async_set_updated_data(None) elif update_type is RpcUpdateType.DISCONNECTED: self.entry.async_create_background_task( @@ -690,6 +737,8 @@ def _async_handle_update( "rpc device disconnected", eager_start=True, ) + # Make sure entities are marked as unavailable + self.async_set_updated_data(None) elif update_type is RpcUpdateType.STATUS: self.async_set_updated_data(None) if self.sleep_period: @@ -744,8 +793,7 @@ async def _async_update_data(self) -> None: LOGGER.debug("Polling Shelly RPC Device - %s", self.name) try: - await self.device.update_status() - await self.device.get_dynamic_components() + await self.device.poll() except (DeviceConnectionError, RpcCallError) as err: raise UpdateFailed(f"Device disconnected: {err!r}") from err except InvalidAuthError: @@ -795,14 +843,13 @@ def get_rpc_coordinator_by_device_id( async def async_reconnect_soon(hass: HomeAssistant, entry: ShellyConfigEntry) -> None: """Try to reconnect soon.""" if ( - not entry.data.get(CONF_SLEEP_PERIOD) - and not hass.is_stopping - and entry.state == ConfigEntryState.LOADED + not hass.is_stopping + and entry.state is ConfigEntryState.LOADED and (coordinator := entry.runtime_data.rpc) ): entry.async_create_background_task( hass, - coordinator.async_request_refresh(), + coordinator.async_device_online("zeroconf"), "reconnect soon", eager_start=True, ) diff --git a/homeassistant/components/shelly/diagnostics.py b/homeassistant/components/shelly/diagnostics.py index e70b76a7c003ff..a5fe1f5b6c00c8 100644 --- a/homeassistant/components/shelly/diagnostics.py +++ b/homeassistant/components/shelly/diagnostics.py @@ -11,6 +11,7 @@ from homeassistant.helpers.device_registry import format_mac from .coordinator import ShellyConfigEntry +from .utils import get_rpc_ws_url TO_REDACT = {CONF_USERNAME, CONF_PASSWORD} @@ -73,6 +74,12 @@ async def async_get_config_entry_diagnostics( device_settings = { k: v for k, v in rpc_coordinator.device.config.items() if k in ["cloud"] } + ws_config = rpc_coordinator.device.config["ws"] + device_settings["ws_outbound_enabled"] = ws_config["enable"] + if ws_config["enable"]: + device_settings["ws_outbound_server_valid"] = bool( + ws_config["server"] == get_rpc_ws_url(hass) + ) device_status = { k: v for k, v in rpc_coordinator.device.status.items() diff --git a/homeassistant/components/shelly/entity.py b/homeassistant/components/shelly/entity.py index 5bf8a4113778e5..aea060e09e2ed2 100644 --- a/homeassistant/components/shelly/entity.py +++ b/homeassistant/components/shelly/entity.py @@ -358,6 +358,14 @@ def __init__(self, coordinator: ShellyRpcCoordinator, key: str) -> None: self._attr_unique_id = f"{coordinator.mac}-{key}" self._attr_name = get_rpc_entity_name(coordinator.device, key) + @property + def available(self) -> bool: + """Check if device is available and initialized or sleepy.""" + coordinator = self.coordinator + return super().available and ( + coordinator.device.initialized or bool(coordinator.sleep_period) + ) + @property def status(self) -> dict: """Device status by entity key.""" @@ -480,7 +488,7 @@ def available(self) -> bool: @property def attribute_value(self) -> StateType: """Value of sensor.""" - if callable(self.entity_description.value): + if self.entity_description.value is not None: self._last_value = self.entity_description.value( self.block_coordinator.device.status, self._last_value ) @@ -510,7 +518,7 @@ def __init__( id_key = key.split(":")[-1] self._id = int(id_key) if id_key.isnumeric() else None - if callable(description.unit): + if description.unit is not None: self._attr_native_unit_of_measurement = description.unit( coordinator.device.config[key] ) @@ -536,7 +544,7 @@ def sub_status(self) -> Any: @property def attribute_value(self) -> StateType: """Value of sensor.""" - if callable(self.entity_description.value): + if self.entity_description.value is not None: # using "get" here since subkey might not exist (e.g. "errors" sub_key) self._last_value = self.entity_description.value( self.status.get(self.entity_description.sub_key), self._last_value diff --git a/homeassistant/components/shelly/manifest.json b/homeassistant/components/shelly/manifest.json index da3bbc4bb6edef..5e2522ea456302 100644 --- a/homeassistant/components/shelly/manifest.json +++ b/homeassistant/components/shelly/manifest.json @@ -9,7 +9,7 @@ "iot_class": "local_push", "loggers": ["aioshelly"], "quality_scale": "platinum", - "requirements": ["aioshelly==11.2.4"], + "requirements": ["aioshelly==11.4.2"], "zeroconf": [ { "type": "_http._tcp.local.", diff --git a/homeassistant/components/shelly/number.py b/homeassistant/components/shelly/number.py index 67c33faf1506c1..1e0f5b020ac865 100644 --- a/homeassistant/components/shelly/number.py +++ b/homeassistant/components/shelly/number.py @@ -207,17 +207,17 @@ def __init__( """Initialize sensor.""" super().__init__(coordinator, key, attribute, description) - if callable(description.max_fn): + if description.max_fn is not None: self._attr_native_max_value = description.max_fn( coordinator.device.config[key] ) - if callable(description.min_fn): + if description.min_fn is not None: self._attr_native_min_value = description.min_fn( coordinator.device.config[key] ) - if callable(description.step_fn): + if description.step_fn is not None: self._attr_native_step = description.step_fn(coordinator.device.config[key]) - if callable(description.mode_fn): + if description.mode_fn is not None: self._attr_mode = description.mode_fn(coordinator.device.config[key]) @property diff --git a/homeassistant/components/shelly/sensor.py b/homeassistant/components/shelly/sensor.py index 6cabee6ccba0a3..1ef174119e4a2d 100644 --- a/homeassistant/components/shelly/sensor.py +++ b/homeassistant/components/shelly/sensor.py @@ -1082,6 +1082,7 @@ class RestSensorDescription(RestEntityDescription, SensorEntityDescription): or config[key]["enable"] is False or status[key].get("xpercent") is None ), + unit=lambda config: config["xpercent"]["unit"] or None, ), "pulse_counter": RpcSensorDescription( key="input", @@ -1104,6 +1105,7 @@ class RestSensorDescription(RestEntityDescription, SensorEntityDescription): or config[key]["enable"] is False or status[key]["counts"].get("xtotal") is None ), + unit=lambda config: config["xcounts"]["unit"] or None, ), "counter_frequency": RpcSensorDescription( key="input", @@ -1124,6 +1126,7 @@ class RestSensorDescription(RestEntityDescription, SensorEntityDescription): or config[key]["enable"] is False or status[key].get("xfreq") is None ), + unit=lambda config: config["xfreq"]["unit"] or None, ), "text": RpcSensorDescription( key="text", @@ -1263,13 +1266,15 @@ def __init__( @property def native_value(self) -> StateType: """Return value of sensor.""" + attribute_value = self.attribute_value + if not self.option_map: - return self.attribute_value + return attribute_value - if not isinstance(self.attribute_value, str): + if not isinstance(attribute_value, str): return None - return self.option_map[self.attribute_value] + return self.option_map[attribute_value] class BlockSleepingSensor(ShellySleepingBlockAttributeEntity, RestoreSensor): diff --git a/homeassistant/components/shelly/utils.py b/homeassistant/components/shelly/utils.py index 339f678117144f..d0a8a1230c5cb9 100644 --- a/homeassistant/components/shelly/utils.py +++ b/homeassistant/components/shelly/utils.py @@ -23,6 +23,7 @@ RPC_GENERATIONS, ) from aioshelly.rpc_device import RpcDevice, WsServer +from yarl import URL from homeassistant.components import network from homeassistant.components.http import HomeAssistantView @@ -36,9 +37,11 @@ singleton, ) from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC +from homeassistant.helpers.network import NoURLAvailableError, get_url from homeassistant.util.dt import utcnow from .const import ( + API_WS_URL, BASIC_INPUTS_EVENTS_TYPES, CONF_COAP_PORT, CONF_GEN, @@ -254,7 +257,7 @@ class ShellyReceiver(HomeAssistantView): """Handle pushes from Shelly Gen2 devices.""" requires_auth = False - url = "/api/shelly/ws" + url = API_WS_URL name = "api:shelly:ws" def __init__(self, ws_server: WsServer) -> None: @@ -571,3 +574,15 @@ def async_remove_orphaned_virtual_entities( if orphaned_entities: async_remove_shelly_rpc_entities(hass, platform, mac, orphaned_entities) + + +def get_rpc_ws_url(hass: HomeAssistant) -> str | None: + """Return the RPC websocket URL.""" + try: + raw_url = get_url(hass, prefer_external=False, allow_cloud=False) + except NoURLAvailableError: + LOGGER.debug("URL not available, skipping outbound websocket setup") + return None + url = URL(raw_url) + ws_url = url.with_scheme("wss" if url.scheme == "https" else "ws") + return str(ws_url.joinpath(API_WS_URL.removeprefix("/"))) diff --git a/homeassistant/components/shopping_list/icons.json b/homeassistant/components/shopping_list/icons.json index 7de3eb1b948b2a..9b3d8a08a79beb 100644 --- a/homeassistant/components/shopping_list/icons.json +++ b/homeassistant/components/shopping_list/icons.json @@ -7,13 +7,29 @@ } }, "services": { - "add_item": "mdi:cart-plus", - "remove_item": "mdi:cart-remove", - "complete_item": "mdi:cart-check", - "incomplete_item": "mdi:cart-off", - "complete_all": "mdi:cart-check", - "incomplete_all": "mdi:cart-off", - "clear_completed_items": "mdi:cart-remove", - "sort": "mdi:sort" + "add_item": { + "service": "mdi:cart-plus" + }, + "remove_item": { + "service": "mdi:cart-remove" + }, + "complete_item": { + "service": "mdi:cart-check" + }, + "incomplete_item": { + "service": "mdi:cart-off" + }, + "complete_all": { + "service": "mdi:cart-check" + }, + "incomplete_all": { + "service": "mdi:cart-off" + }, + "clear_completed_items": { + "service": "mdi:cart-remove" + }, + "sort": { + "service": "mdi:sort" + } } } diff --git a/homeassistant/components/simplefin/__init__.py b/homeassistant/components/simplefin/__init__.py index 0aa33dec9ac67f..c47b3118415324 100644 --- a/homeassistant/components/simplefin/__init__.py +++ b/homeassistant/components/simplefin/__init__.py @@ -11,7 +11,10 @@ from .const import CONF_ACCESS_URL from .coordinator import SimpleFinDataUpdateCoordinator -PLATFORMS: list[str] = [Platform.SENSOR] +PLATFORMS: list[str] = [ + Platform.BINARY_SENSOR, + Platform.SENSOR, +] type SimpleFinConfigEntry = ConfigEntry[SimpleFinDataUpdateCoordinator] diff --git a/homeassistant/components/simplefin/binary_sensor.py b/homeassistant/components/simplefin/binary_sensor.py new file mode 100644 index 00000000000000..5805fc370b65a1 --- /dev/null +++ b/homeassistant/components/simplefin/binary_sensor.py @@ -0,0 +1,68 @@ +"""Binary Sensor for SimpleFin.""" + +from collections.abc import Callable +from dataclasses import dataclass + +from simplefin4py import Account + +from homeassistant.components.binary_sensor import ( + BinarySensorDeviceClass, + BinarySensorEntity, + BinarySensorEntityDescription, +) +from homeassistant.const import EntityCategory +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from . import SimpleFinConfigEntry +from .entity import SimpleFinEntity + + +@dataclass(frozen=True, kw_only=True) +class SimpleFinBinarySensorEntityDescription(BinarySensorEntityDescription): + """Describes a sensor entity.""" + + value_fn: Callable[[Account], bool] + + +SIMPLEFIN_BINARY_SENSORS: tuple[SimpleFinBinarySensorEntityDescription, ...] = ( + SimpleFinBinarySensorEntityDescription( + key="possible_error", + translation_key="possible_error", + device_class=BinarySensorDeviceClass.PROBLEM, + entity_category=EntityCategory.DIAGNOSTIC, + value_fn=lambda account: account.possible_error, + ), +) + + +async def async_setup_entry( + hass: HomeAssistant, + config_entry: SimpleFinConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up SimpleFIN sensors for config entries.""" + + sf_coordinator = config_entry.runtime_data + accounts = sf_coordinator.data.accounts + + async_add_entities( + SimpleFinBinarySensor( + sf_coordinator, + sensor_description, + account, + ) + for account in accounts + for sensor_description in SIMPLEFIN_BINARY_SENSORS + ) + + +class SimpleFinBinarySensor(SimpleFinEntity, BinarySensorEntity): + """Extends IntellifireEntity with Binary Sensor specific logic.""" + + entity_description: SimpleFinBinarySensorEntityDescription + + @property + def is_on(self) -> bool: + """Use this to get the correct value.""" + return self.entity_description.value_fn(self.account_data) diff --git a/homeassistant/components/simplefin/strings.json b/homeassistant/components/simplefin/strings.json index d6690e604c5602..3ac03fe2cc00c4 100644 --- a/homeassistant/components/simplefin/strings.json +++ b/homeassistant/components/simplefin/strings.json @@ -21,6 +21,9 @@ } }, "entity": { + "binary_sensor": { + "possible_error": { "name": "Possible error" } + }, "sensor": { "balance": { "name": "Balance" diff --git a/homeassistant/components/simplisafe/config_flow.py b/homeassistant/components/simplisafe/config_flow.py index c0d98c5644fd21..6fdbd351a299e3 100644 --- a/homeassistant/components/simplisafe/config_flow.py +++ b/homeassistant/components/simplisafe/config_flow.py @@ -69,7 +69,9 @@ def async_get_options_flow( """Define the config flow to handle options.""" return SimpliSafeOptionsFlowHandler(config_entry) - async def async_step_reauth(self, config: Mapping[str, Any]) -> ConfigFlowResult: + async def async_step_reauth( + self, entry_data: Mapping[str, Any] + ) -> ConfigFlowResult: """Handle configuration by re-auth.""" self._reauth = True return await self.async_step_user() diff --git a/homeassistant/components/simplisafe/icons.json b/homeassistant/components/simplisafe/icons.json index 60ddb7f09824c0..8552993210f9f9 100644 --- a/homeassistant/components/simplisafe/icons.json +++ b/homeassistant/components/simplisafe/icons.json @@ -1,7 +1,13 @@ { "services": { - "remove_pin": "mdi:alarm-panel-outline", - "set_pin": "mdi:alarm-panel", - "set_system_properties": "mdi:cog" + "remove_pin": { + "service": "mdi:alarm-panel-outline" + }, + "set_pin": { + "service": "mdi:alarm-panel" + }, + "set_system_properties": { + "service": "mdi:cog" + } } } diff --git a/homeassistant/components/siren/icons.json b/homeassistant/components/siren/icons.json index 0083a2540c702f..75caf6417da2f0 100644 --- a/homeassistant/components/siren/icons.json +++ b/homeassistant/components/siren/icons.json @@ -5,8 +5,14 @@ } }, "services": { - "toggle": "mdi:bullhorn", - "turn_off": "mdi:bullhorn", - "turn_on": "mdi:bullhorn" + "toggle": { + "service": "mdi:bullhorn" + }, + "turn_off": { + "service": "mdi:bullhorn" + }, + "turn_on": { + "service": "mdi:bullhorn" + } } } diff --git a/homeassistant/components/sisyphus/manifest.json b/homeassistant/components/sisyphus/manifest.json index dbb40344d66f3e..4e344c0b25e83c 100644 --- a/homeassistant/components/sisyphus/manifest.json +++ b/homeassistant/components/sisyphus/manifest.json @@ -5,5 +5,5 @@ "documentation": "https://www.home-assistant.io/integrations/sisyphus", "iot_class": "local_push", "loggers": ["sisyphus_control"], - "requirements": ["sisyphus-control==3.1.3"] + "requirements": ["sisyphus-control==3.1.4"] } diff --git a/homeassistant/components/sleepiq/config_flow.py b/homeassistant/components/sleepiq/config_flow.py index 4a4813192c3726..26f3672d58868b 100644 --- a/homeassistant/components/sleepiq/config_flow.py +++ b/homeassistant/components/sleepiq/config_flow.py @@ -28,22 +28,20 @@ def __init__(self) -> None: """Initialize the config flow.""" self._reauth_entry: ConfigEntry | None = None - async def async_step_import( - self, import_config: dict[str, Any] - ) -> ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Import a SleepIQ account as a config entry. This flow is triggered by 'async_setup' for configured accounts. """ - await self.async_set_unique_id(import_config[CONF_USERNAME].lower()) + await self.async_set_unique_id(import_data[CONF_USERNAME].lower()) self._abort_if_unique_id_configured() - if error := await try_connection(self.hass, import_config): + if error := await try_connection(self.hass, import_data): _LOGGER.error("Could not authenticate with SleepIQ server: %s", error) return self.async_abort(reason=error) return self.async_create_entry( - title=import_config[CONF_USERNAME], data=import_config + title=import_data[CONF_USERNAME], data=import_data ) async def async_step_user( diff --git a/homeassistant/components/smappee/config_flow.py b/homeassistant/components/smappee/config_flow.py index 6ed189052338e6..f92f8b17662e12 100644 --- a/homeassistant/components/smappee/config_flow.py +++ b/homeassistant/components/smappee/config_flow.py @@ -1,6 +1,7 @@ """Config flow for Smappee.""" import logging +from typing import Any from pysmappee import helper, mqtt import voluptuous as vol @@ -68,9 +69,11 @@ async def async_step_zeroconf( return await self.async_step_zeroconf_confirm() - async def async_step_zeroconf_confirm(self, user_input=None): + async def async_step_zeroconf_confirm( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Confirm zeroconf flow.""" - errors = {} + errors: dict[str, str] = {} # Check if already configured (cloud) if self.is_cloud_device_already_added(): @@ -106,7 +109,9 @@ async def async_step_zeroconf_confirm(self, user_input=None): data={CONF_IP_ADDRESS: ip_address, CONF_SERIALNUMBER: serial_number}, ) - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle a flow initiated by the user.""" # If there is a CLOUD entry already, abort a new LOCAL entry @@ -115,7 +120,9 @@ async def async_step_user(self, user_input=None): return await self.async_step_environment() - async def async_step_environment(self, user_input=None): + async def async_step_environment( + self, user_input: dict[str, str] | None = None + ) -> ConfigFlowResult: """Decide environment, cloud or local.""" if user_input is None: return self.async_show_form( @@ -141,7 +148,9 @@ async def async_step_environment(self, user_input=None): return await self.async_step_pick_implementation() - async def async_step_local(self, user_input=None): + async def async_step_local( + self, user_input: dict[str, str] | None = None + ) -> ConfigFlowResult: """Handle local flow.""" if user_input is None: return self.async_show_form( diff --git a/homeassistant/components/smart_meter_texas/config_flow.py b/homeassistant/components/smart_meter_texas/config_flow.py index bbe1361b795bce..b60855b62c8026 100644 --- a/homeassistant/components/smart_meter_texas/config_flow.py +++ b/homeassistant/components/smart_meter_texas/config_flow.py @@ -1,6 +1,7 @@ """Config flow for Smart Meter Texas integration.""" import logging +from typing import Any from aiohttp import ClientError from smart_meter_texas import Account, Client, ClientSSLContext @@ -10,7 +11,7 @@ ) import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError @@ -52,7 +53,9 @@ class SMTConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" errors = {} diff --git a/homeassistant/components/smartthings/climate.py b/homeassistant/components/smartthings/climate.py index c3929ababc1e01..0598e549f24271 100644 --- a/homeassistant/components/smartthings/climate.py +++ b/homeassistant/components/smartthings/climate.py @@ -143,7 +143,6 @@ def get_capabilities(capabilities: Sequence[str]) -> Sequence[str] | None: # Or must have all of these thermostat capabilities thermostat_capabilities = [ Capability.temperature_measurement, - Capability.thermostat_cooling_setpoint, Capability.thermostat_heating_setpoint, Capability.thermostat_mode, ] diff --git a/homeassistant/components/smartthings/config_flow.py b/homeassistant/components/smartthings/config_flow.py index 2ecc337502630a..081f833787e08c 100644 --- a/homeassistant/components/smartthings/config_flow.py +++ b/homeassistant/components/smartthings/config_flow.py @@ -2,13 +2,14 @@ from http import HTTPStatus import logging +from typing import Any from aiohttp import ClientResponseError from pysmartthings import APIResponseError, AppOAuth, SmartThings from pysmartthings.installedapp import format_install_url import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_ACCESS_TOKEN, CONF_CLIENT_ID, CONF_CLIENT_SECRET from homeassistant.helpers.aiohttp_client import async_get_clientsession @@ -41,23 +42,26 @@ class SmartThingsFlowHandler(ConfigFlow, domain=DOMAIN): VERSION = 2 + api: SmartThings + app_id: str + location_id: str + def __init__(self) -> None: """Create a new instance of the flow handler.""" - self.access_token = None - self.app_id = None - self.api = None + self.access_token: str | None = None self.oauth_client_secret = None self.oauth_client_id = None self.installed_app_id = None self.refresh_token = None - self.location_id = None self.endpoints_initialized = False - async def async_step_import(self, user_input=None): + async def async_step_import(self, import_data: None) -> ConfigFlowResult: """Occurs when a previously entry setup fails and is re-initiated.""" - return await self.async_step_user(user_input) + return await self.async_step_user(import_data) - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Validate and confirm webhook setup.""" if not self.endpoints_initialized: self.endpoints_initialized = True @@ -88,9 +92,11 @@ async def async_step_user(self, user_input=None): # Show the next screen return await self.async_step_pat() - async def async_step_pat(self, user_input=None): + async def async_step_pat( + self, user_input: dict[str, str] | None = None + ) -> ConfigFlowResult: """Get the Personal Access Token and validate it.""" - errors = {} + errors: dict[str, str] = {} if user_input is None or CONF_ACCESS_TOKEN not in user_input: return self._show_step_pat(errors) @@ -166,7 +172,9 @@ async def async_step_pat(self, user_input=None): return await self.async_step_select_location() - async def async_step_select_location(self, user_input=None): + async def async_step_select_location( + self, user_input: dict[str, str] | None = None + ) -> ConfigFlowResult: """Ask user to select the location to setup.""" if user_input is None or CONF_LOCATION_ID not in user_input: # Get available locations @@ -193,7 +201,9 @@ async def async_step_select_location(self, user_input=None): await self.async_set_unique_id(format_unique_id(self.app_id, self.location_id)) return await self.async_step_authorize() - async def async_step_authorize(self, user_input=None): + async def async_step_authorize( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Wait for the user to authorize the app installation.""" user_input = {} if user_input is None else user_input self.installed_app_id = user_input.get(CONF_INSTALLED_APP_ID) @@ -230,7 +240,9 @@ def _show_step_pat(self, errors): }, ) - async def async_step_install(self, data=None): + async def async_step_install( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Create a config entry at completion of a flow and authorization of the app.""" data = { CONF_ACCESS_TOKEN: self.access_token, diff --git a/homeassistant/components/smarttub/config_flow.py b/homeassistant/components/smarttub/config_flow.py index 60f14b03e45270..5caff953d6d0b5 100644 --- a/homeassistant/components/smarttub/config_flow.py +++ b/homeassistant/components/smarttub/config_flow.py @@ -3,7 +3,7 @@ from __future__ import annotations from collections.abc import Mapping -from typing import Any +from typing import TYPE_CHECKING, Any from smarttub import LoginFailed import voluptuous as vol @@ -30,7 +30,9 @@ def __init__(self) -> None: self._reauth_input: Mapping[str, Any] | None = None self._reauth_entry: ConfigEntry | None = None - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle a flow initiated by the user.""" errors = {} @@ -53,6 +55,8 @@ async def async_step_user(self, user_input=None): ) # this is a reauth attempt + if TYPE_CHECKING: + assert self._reauth_entry if self._reauth_entry.unique_id != self.unique_id: # there is a config entry matching this account, # but it is not the one we were trying to reauth @@ -77,9 +81,13 @@ async def async_step_reauth( ) return await self.async_step_reauth_confirm() - async def async_step_reauth_confirm(self, user_input=None): + async def async_step_reauth_confirm( + self, user_input: dict[str, str] | None = None + ) -> ConfigFlowResult: """Dialog that informs the user that reauth is required.""" if user_input is None: + if TYPE_CHECKING: + assert self._reauth_input is not None # same as DATA_SCHEMA but with default email data_schema = vol.Schema( { diff --git a/homeassistant/components/smarttub/icons.json b/homeassistant/components/smarttub/icons.json index 7ae96d03383a7a..2b89445754ca63 100644 --- a/homeassistant/components/smarttub/icons.json +++ b/homeassistant/components/smarttub/icons.json @@ -1,8 +1,16 @@ { "services": { - "set_primary_filtration": "mdi:filter", - "set_secondary_filtration": "mdi:filter-multiple", - "snooze_reminder": "mdi:timer-pause", - "reset_reminder": "mdi:timer-sync" + "set_primary_filtration": { + "service": "mdi:filter" + }, + "set_secondary_filtration": { + "service": "mdi:filter-multiple" + }, + "snooze_reminder": { + "service": "mdi:timer-pause" + }, + "reset_reminder": { + "service": "mdi:timer-sync" + } } } diff --git a/homeassistant/components/smarty/__init__.py b/homeassistant/components/smarty/__init__.py index cc2e3850ef9c37..17c4bd0a26a850 100644 --- a/homeassistant/components/smarty/__init__.py +++ b/homeassistant/components/smarty/__init__.py @@ -4,7 +4,7 @@ import ipaddress import logging -from pysmarty import Smarty +from pysmarty2 import Smarty import voluptuous as vol from homeassistant.const import CONF_HOST, CONF_NAME, Platform diff --git a/homeassistant/components/smarty/binary_sensor.py b/homeassistant/components/smarty/binary_sensor.py index cf40dc7b9825ab..b31c51244b8b08 100644 --- a/homeassistant/components/smarty/binary_sensor.py +++ b/homeassistant/components/smarty/binary_sensor.py @@ -4,7 +4,7 @@ import logging -from pysmarty import Smarty +from pysmarty2 import Smarty from homeassistant.components.binary_sensor import ( BinarySensorDeviceClass, diff --git a/homeassistant/components/smarty/fan.py b/homeassistant/components/smarty/fan.py index 37f7c2e493f09e..a2d72250197b1a 100644 --- a/homeassistant/components/smarty/fan.py +++ b/homeassistant/components/smarty/fan.py @@ -6,7 +6,7 @@ import math from typing import Any -from pysmarty import Smarty +from pysmarty2 import Smarty from homeassistant.components.fan import FanEntity, FanEntityFeature from homeassistant.core import HomeAssistant, callback diff --git a/homeassistant/components/smarty/manifest.json b/homeassistant/components/smarty/manifest.json index 8769aa666a785d..b83319b674418f 100644 --- a/homeassistant/components/smarty/manifest.json +++ b/homeassistant/components/smarty/manifest.json @@ -2,9 +2,9 @@ "domain": "smarty", "name": "Salda Smarty", "codeowners": ["@z0mbieprocess"], - "disabled": "Dependencies not compatible with the new pip resolver", "documentation": "https://www.home-assistant.io/integrations/smarty", + "integration_type": "hub", "iot_class": "local_polling", - "loggers": ["pymodbus", "pysmarty"], - "requirements": ["pysmarty==0.8"] + "loggers": ["pymodbus", "pysmarty2"], + "requirements": ["pysmarty2==0.10.1"] } diff --git a/homeassistant/components/smarty/sensor.py b/homeassistant/components/smarty/sensor.py index a0c15b3825f3cd..3c6873611b4add 100644 --- a/homeassistant/components/smarty/sensor.py +++ b/homeassistant/components/smarty/sensor.py @@ -5,7 +5,7 @@ import datetime as dt import logging -from pysmarty import Smarty +from pysmarty2 import Smarty from homeassistant.components.sensor import SensorDeviceClass, SensorEntity from homeassistant.const import UnitOfTemperature diff --git a/homeassistant/components/smlight/__init__.py b/homeassistant/components/smlight/__init__.py index 16eb60b9c87593..47dc943423ea1d 100644 --- a/homeassistant/components/smlight/__init__.py +++ b/homeassistant/components/smlight/__init__.py @@ -9,6 +9,7 @@ from .coordinator import SmDataUpdateCoordinator PLATFORMS: list[Platform] = [ + Platform.BUTTON, Platform.SENSOR, ] type SmConfigEntry = ConfigEntry[SmDataUpdateCoordinator] diff --git a/homeassistant/components/smlight/button.py b/homeassistant/components/smlight/button.py new file mode 100644 index 00000000000000..b6a0c24c2ed7e5 --- /dev/null +++ b/homeassistant/components/smlight/button.py @@ -0,0 +1,87 @@ +"""Support for SLZB-06 buttons.""" + +from __future__ import annotations + +from collections.abc import Awaitable, Callable +from dataclasses import dataclass +import logging +from typing import Final + +from pysmlight.web import CmdWrapper + +from homeassistant.components.button import ( + ButtonDeviceClass, + ButtonEntity, + ButtonEntityDescription, +) +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import EntityCategory +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from .coordinator import SmDataUpdateCoordinator +from .entity import SmEntity + +_LOGGER = logging.getLogger(__name__) + + +@dataclass(frozen=True, kw_only=True) +class SmButtonDescription(ButtonEntityDescription): + """Class to describe a Button entity.""" + + press_fn: Callable[[CmdWrapper], Awaitable[None]] + + +BUTTONS: Final = [ + SmButtonDescription( + key="core_restart", + translation_key="core_restart", + device_class=ButtonDeviceClass.RESTART, + press_fn=lambda cmd: cmd.reboot(), + ), + SmButtonDescription( + key="zigbee_restart", + translation_key="zigbee_restart", + device_class=ButtonDeviceClass.RESTART, + press_fn=lambda cmd: cmd.zb_restart(), + ), + SmButtonDescription( + key="zigbee_flash_mode", + translation_key="zigbee_flash_mode", + entity_registry_enabled_default=False, + press_fn=lambda cmd: cmd.zb_bootloader(), + ), +] + + +async def async_setup_entry( + hass: HomeAssistant, + entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up SMLIGHT buttons based on a config entry.""" + coordinator = entry.runtime_data + + async_add_entities(SmButton(coordinator, button) for button in BUTTONS) + + +class SmButton(SmEntity, ButtonEntity): + """Defines a SLZB-06 button.""" + + entity_description: SmButtonDescription + _attr_entity_category = EntityCategory.CONFIG + + def __init__( + self, + coordinator: SmDataUpdateCoordinator, + description: SmButtonDescription, + ) -> None: + """Initialize SLZB-06 button entity.""" + super().__init__(coordinator) + + self.entity_description = description + self._attr_unique_id = f"{coordinator.unique_id}-{description.key}" + + async def async_press(self) -> None: + """Trigger button press.""" + await self.entity_description.press_fn(self.coordinator.client.cmds) diff --git a/homeassistant/components/smlight/config_flow.py b/homeassistant/components/smlight/config_flow.py index 1b8cc4efeb1fc4..98da153ce751d2 100644 --- a/homeassistant/components/smlight/config_flow.py +++ b/homeassistant/components/smlight/config_flow.py @@ -2,6 +2,7 @@ from __future__ import annotations +from collections.abc import Mapping from typing import Any from pysmlight import Api2 @@ -14,6 +15,7 @@ from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.device_registry import format_mac +from . import SmConfigEntry from .const import DOMAIN STEP_USER_DATA_SCHEMA = vol.Schema( @@ -37,6 +39,7 @@ def __init__(self) -> None: """Initialize the config flow.""" self.client: Api2 self.host: str | None = None + self._reauth_entry: SmConfigEntry | None = None async def async_step_user( self, user_input: dict[str, Any] | None = None @@ -127,6 +130,52 @@ async def async_step_confirm_discovery( errors=errors, ) + async def async_step_reauth( + self, entry_data: Mapping[str, Any] + ) -> ConfigFlowResult: + """Handle reauth when API Authentication failed.""" + + self._reauth_entry = self.hass.config_entries.async_get_entry( + self.context["entry_id"] + ) + host = entry_data[CONF_HOST] + self.context["title_placeholders"] = { + "host": host, + "name": entry_data.get(CONF_USERNAME, "unknown"), + } + self.client = Api2(host, session=async_get_clientsession(self.hass)) + self.host = host + + return await self.async_step_reauth_confirm() + + async def async_step_reauth_confirm( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle re-authentication of an existing config entry.""" + errors = {} + if user_input is not None: + try: + await self.client.authenticate( + user_input[CONF_USERNAME], user_input[CONF_PASSWORD] + ) + except SmlightAuthError: + errors["base"] = "invalid_auth" + except SmlightConnectionError: + return self.async_abort(reason="cannot_connect") + else: + assert self._reauth_entry is not None + + return self.async_update_reload_and_abort( + self._reauth_entry, data={**user_input, CONF_HOST: self.host} + ) + + return self.async_show_form( + step_id="reauth_confirm", + data_schema=STEP_AUTH_DATA_SCHEMA, + description_placeholders=self.context["title_placeholders"], + errors=errors, + ) + async def _async_check_auth_required(self, user_input: dict[str, Any]) -> bool: """Check if auth required and attempt to authenticate.""" if await self.client.check_auth_needed(): diff --git a/homeassistant/components/smlight/const.py b/homeassistant/components/smlight/const.py index de3270fe3be461..791b00c3e93ec9 100644 --- a/homeassistant/components/smlight/const.py +++ b/homeassistant/components/smlight/const.py @@ -9,3 +9,4 @@ LOGGER = logging.getLogger(__package__) SCAN_INTERVAL = timedelta(seconds=300) +UPTIME_DEVIATION = timedelta(seconds=5) diff --git a/homeassistant/components/smlight/coordinator.py b/homeassistant/components/smlight/coordinator.py index 6a29f14fafdfb2..380644c81d1e19 100644 --- a/homeassistant/components/smlight/coordinator.py +++ b/homeassistant/components/smlight/coordinator.py @@ -8,7 +8,7 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.core import HomeAssistant -from homeassistant.exceptions import ConfigEntryError +from homeassistant.exceptions import ConfigEntryAuthFailed from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.device_registry import format_mac from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed @@ -54,8 +54,10 @@ async def _async_setup(self) -> None: self.config_entry.data[CONF_PASSWORD], ) except SmlightAuthError as err: - LOGGER.error("Failed to authenticate: %s", err) - raise ConfigEntryError from err + raise ConfigEntryAuthFailed from err + else: + # Auth required but no credentials available + raise ConfigEntryAuthFailed info = await self.client.get_info() self.unique_id = format_mac(info.MAC) @@ -67,5 +69,8 @@ async def _async_update_data(self) -> SmData: sensors=await self.client.get_sensors(), info=await self.client.get_info(), ) + except SmlightAuthError as err: + raise ConfigEntryAuthFailed from err + except SmlightConnectionError as err: raise UpdateFailed(err) from err diff --git a/homeassistant/components/smlight/manifest.json b/homeassistant/components/smlight/manifest.json index 0dbd25e90bd55e..72d915666e5208 100644 --- a/homeassistant/components/smlight/manifest.json +++ b/homeassistant/components/smlight/manifest.json @@ -6,7 +6,7 @@ "documentation": "https://www.home-assistant.io/integrations/smlight", "integration_type": "device", "iot_class": "local_polling", - "requirements": ["pysmlight==0.0.12"], + "requirements": ["pysmlight==0.0.13"], "zeroconf": [ { "type": "_slzb-06._tcp.local." diff --git a/homeassistant/components/smlight/sensor.py b/homeassistant/components/smlight/sensor.py index d9c03760fb805b..f5193522c4c5e5 100644 --- a/homeassistant/components/smlight/sensor.py +++ b/homeassistant/components/smlight/sensor.py @@ -4,6 +4,8 @@ from collections.abc import Callable from dataclasses import dataclass +from datetime import datetime, timedelta +from itertools import chain from pysmlight import Sensors @@ -16,8 +18,10 @@ from homeassistant.const import EntityCategory, UnitOfInformation, UnitOfTemperature from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.util.dt import utcnow from . import SmConfigEntry +from .const import UPTIME_DEVIATION from .coordinator import SmDataUpdateCoordinator from .entity import SmEntity @@ -67,6 +71,23 @@ class SmSensorEntityDescription(SensorEntityDescription): ), ] +UPTIME = [ + SmSensorEntityDescription( + key="core_uptime", + translation_key="core_uptime", + device_class=SensorDeviceClass.TIMESTAMP, + entity_registry_enabled_default=False, + value_fn=lambda x: x.uptime, + ), + SmSensorEntityDescription( + key="socket_uptime", + translation_key="socket_uptime", + device_class=SensorDeviceClass.TIMESTAMP, + entity_registry_enabled_default=False, + value_fn=lambda x: x.socket_uptime, + ), +] + async def async_setup_entry( hass: HomeAssistant, @@ -77,7 +98,10 @@ async def async_setup_entry( coordinator = entry.runtime_data async_add_entities( - SmSensorEntity(coordinator, description) for description in SENSORS + chain( + (SmSensorEntity(coordinator, description) for description in SENSORS), + (SmUptimeSensorEntity(coordinator, description) for description in UPTIME), + ) ) @@ -98,6 +122,48 @@ def __init__( self._attr_unique_id = f"{coordinator.unique_id}_{description.key}" @property - def native_value(self) -> float | None: + def native_value(self) -> datetime | float | None: """Return the sensor value.""" return self.entity_description.value_fn(self.coordinator.data.sensors) + + +class SmUptimeSensorEntity(SmSensorEntity): + """Representation of a slzb uptime sensor.""" + + def __init__( + self, + coordinator: SmDataUpdateCoordinator, + description: SmSensorEntityDescription, + ) -> None: + "Initialize uptime sensor instance." + super().__init__(coordinator, description) + self._last_uptime: datetime | None = None + + def get_uptime(self, uptime: float | None) -> datetime | None: + """Return device uptime or zigbee socket uptime. + + Converts uptime from seconds to a datetime value, allow up to 5 + seconds deviation. This avoids unnecessary updates to sensor state, + that may be caused by clock jitter. + """ + if uptime is None: + # reset to unknown state + self._last_uptime = None + return None + + new_uptime = utcnow() - timedelta(seconds=uptime) + + if ( + not self._last_uptime + or abs(new_uptime - self._last_uptime) > UPTIME_DEVIATION + ): + self._last_uptime = new_uptime + + return self._last_uptime + + @property + def native_value(self) -> datetime | None: + """Return the sensor value.""" + value = self.entity_description.value_fn(self.coordinator.data.sensors) + + return self.get_uptime(value) diff --git a/homeassistant/components/smlight/strings.json b/homeassistant/components/smlight/strings.json index 02b9ebcc4e81b7..f22966df9048f9 100644 --- a/homeassistant/components/smlight/strings.json +++ b/homeassistant/components/smlight/strings.json @@ -17,6 +17,14 @@ "password": "[%key:common::config_flow::data::password%]" } }, + "reauth_confirm": { + "title": "[%key:common::config_flow::title::reauth%]", + "description": "Please enter the correct username and password for host: {host}", + "data": { + "username": "[%key:common::config_flow::data::username%]", + "password": "[%key:common::config_flow::data::password%]" + } + }, "confirm_discovery": { "description": "Do you want to set up SMLIGHT at {host}?" } @@ -27,7 +35,10 @@ "unknown": "[%key:common::config_flow::error::unknown%]" }, "abort": { - "already_configured": "[%key:common::config_flow::abort::already_configured_device%]" + "already_configured": "[%key:common::config_flow::abort::already_configured_device%]", + "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", + "reauth_failed": "[%key:common::config_flow::error::invalid_auth%]", + "reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]" } }, "entity": { @@ -43,6 +54,23 @@ }, "ram_usage": { "name": "RAM usage" + }, + "core_uptime": { + "name": "Core uptime" + }, + "socket_uptime": { + "name": "Zigbee uptime" + } + }, + "button": { + "core_restart": { + "name": "Core restart" + }, + "zigbee_restart": { + "name": "Zigbee restart" + }, + "zigbee_flash_mode": { + "name": "Zigbee flash mode" } } } diff --git a/homeassistant/components/sms/config_flow.py b/homeassistant/components/sms/config_flow.py index aec9674da9d8fe..d2188a94632a49 100644 --- a/homeassistant/components/sms/config_flow.py +++ b/homeassistant/components/sms/config_flow.py @@ -1,11 +1,12 @@ """Config flow for SMS integration.""" import logging +from typing import Any import gammu import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_DEVICE from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError @@ -26,7 +27,7 @@ ) -async def get_imei_from_config(hass: HomeAssistant, data): +async def get_imei_from_config(hass: HomeAssistant, data: dict[str, Any]) -> str: """Validate the user input allows us to connect. Data has the keys from DATA_SCHEMA with values provided by the user. @@ -56,7 +57,9 @@ class SMSFlowHandler(ConfigFlow, domain=DOMAIN): VERSION = 1 - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" if self._async_current_entries(): return self.async_abort(reason="single_instance_allowed") @@ -79,10 +82,6 @@ async def async_step_user(self, user_input=None): step_id="user", data_schema=DATA_SCHEMA, errors=errors ) - async def async_step_import(self, user_input): - """Handle import.""" - return await self.async_step_user(user_input) - class CannotConnect(HomeAssistantError): """Error to indicate we cannot connect.""" diff --git a/homeassistant/components/smtp/icons.json b/homeassistant/components/smtp/icons.json index a03163179cb5bd..a9829425570a0c 100644 --- a/homeassistant/components/smtp/icons.json +++ b/homeassistant/components/smtp/icons.json @@ -1,5 +1,7 @@ { "services": { - "reload": "mdi:reload" + "reload": { + "service": "mdi:reload" + } } } diff --git a/homeassistant/components/snapcast/icons.json b/homeassistant/components/snapcast/icons.json index bdc20665282404..d6511d768e2ddd 100644 --- a/homeassistant/components/snapcast/icons.json +++ b/homeassistant/components/snapcast/icons.json @@ -1,9 +1,19 @@ { "services": { - "join": "mdi:music-note-plus", - "unjoin": "mdi:music-note-minus", - "snapshot": "mdi:camera", - "restore": "mdi:camera-retake", - "set_latency": "mdi:camera-timer" + "join": { + "service": "mdi:music-note-plus" + }, + "unjoin": { + "service": "mdi:music-note-minus" + }, + "snapshot": { + "service": "mdi:camera" + }, + "restore": { + "service": "mdi:camera-retake" + }, + "set_latency": { + "service": "mdi:camera-timer" + } } } diff --git a/homeassistant/components/snips/icons.json b/homeassistant/components/snips/icons.json index 0d465465fe4d3e..9c86a7ad5b36a0 100644 --- a/homeassistant/components/snips/icons.json +++ b/homeassistant/components/snips/icons.json @@ -1,8 +1,16 @@ { "services": { - "feedback_off": "mdi:message-alert", - "feedback_on": "mdi:message-alert", - "say": "mdi:chat", - "say_action": "mdi:account-voice" + "feedback_off": { + "service": "mdi:message-alert" + }, + "feedback_on": { + "service": "mdi:message-alert" + }, + "say": { + "service": "mdi:chat" + }, + "say_action": { + "service": "mdi:account-voice" + } } } diff --git a/homeassistant/components/snooz/icons.json b/homeassistant/components/snooz/icons.json index d9cccfff4eab90..be7d2714a2093a 100644 --- a/homeassistant/components/snooz/icons.json +++ b/homeassistant/components/snooz/icons.json @@ -1,6 +1,10 @@ { "services": { - "transition_on": "mdi:blur", - "transition_off": "mdi:blur-off" + "transition_on": { + "service": "mdi:blur" + }, + "transition_off": { + "service": "mdi:blur-off" + } } } diff --git a/homeassistant/components/solarlog/__init__.py b/homeassistant/components/solarlog/__init__.py index 962efa4e1903a5..f23305ca8f2c51 100644 --- a/homeassistant/components/solarlog/__init__.py +++ b/homeassistant/components/solarlog/__init__.py @@ -7,17 +7,17 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er -from .coordinator import SolarlogData +from .coordinator import SolarLogCoordinator _LOGGER = logging.getLogger(__name__) PLATFORMS = [Platform.SENSOR] -type SolarlogConfigEntry = ConfigEntry[SolarlogData] +type SolarlogConfigEntry = ConfigEntry[SolarLogCoordinator] async def async_setup_entry(hass: HomeAssistant, entry: SolarlogConfigEntry) -> bool: """Set up a config entry for solarlog.""" - coordinator = SolarlogData(hass, entry) + coordinator = SolarLogCoordinator(hass, entry) await coordinator.async_config_entry_first_refresh() entry.runtime_data = coordinator await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) diff --git a/homeassistant/components/solarlog/config_flow.py b/homeassistant/components/solarlog/config_flow.py index 7c8401be2b830f..5f047a9c84452e 100644 --- a/homeassistant/components/solarlog/config_flow.py +++ b/homeassistant/components/solarlog/config_flow.py @@ -10,7 +10,6 @@ from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_HOST, CONF_NAME -from homeassistant.core import HomeAssistant, callback from homeassistant.util import slugify from .const import DEFAULT_HOST, DEFAULT_NAME, DOMAIN @@ -18,14 +17,6 @@ _LOGGER = logging.getLogger(__name__) -@callback -def solarlog_entries(hass: HomeAssistant): - """Return the hosts already configured.""" - return { - entry.data[CONF_HOST] for entry in hass.config_entries.async_entries(DOMAIN) - } - - class SolarLogConfigFlow(ConfigFlow, domain=DOMAIN): """Handle a config flow for solarlog.""" @@ -36,12 +27,6 @@ def __init__(self) -> None: """Initialize the config flow.""" self._errors: dict = {} - def _host_in_configuration_exists(self, host) -> bool: - """Return True if host exists in configuration.""" - if host in solarlog_entries(self.hass): - return True - return False - def _parse_url(self, host: str) -> str: """Return parsed host url.""" url = urlparse(host, "http") @@ -50,7 +35,7 @@ def _parse_url(self, host: str) -> str: url = ParseResult("http", netloc, path, *url[3:]) return url.geturl() - async def _test_connection(self, host): + async def _test_connection(self, host: str) -> bool: """Check if we can connect to the Solar-Log device.""" solarlog = SolarLogConnector(host) try: @@ -66,58 +51,37 @@ async def _test_connection(self, host): return True - async def async_step_user(self, user_input=None) -> ConfigFlowResult: + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Step when user initializes a integration.""" self._errors = {} if user_input is not None: - # set some defaults in case we need to return to the form - user_input[CONF_NAME] = slugify(user_input[CONF_NAME]) user_input[CONF_HOST] = self._parse_url(user_input[CONF_HOST]) - if self._host_in_configuration_exists(user_input[CONF_HOST]): - self._errors[CONF_HOST] = "already_configured" - elif await self._test_connection(user_input[CONF_HOST]): + self._async_abort_entries_match({CONF_HOST: user_input[CONF_HOST]}) + + user_input[CONF_NAME] = slugify(user_input[CONF_NAME]) + + if await self._test_connection(user_input[CONF_HOST]): return self.async_create_entry( title=user_input[CONF_NAME], data=user_input ) else: - user_input = {} - user_input[CONF_NAME] = DEFAULT_NAME - user_input[CONF_HOST] = DEFAULT_HOST + user_input = {CONF_NAME: DEFAULT_NAME, CONF_HOST: DEFAULT_HOST} return self.async_show_form( step_id="user", data_schema=vol.Schema( { - vol.Required( - CONF_NAME, default=user_input.get(CONF_NAME, DEFAULT_NAME) - ): str, - vol.Required( - CONF_HOST, default=user_input.get(CONF_HOST, DEFAULT_HOST) - ): str, + vol.Required(CONF_NAME, default=user_input[CONF_NAME]): str, + vol.Required(CONF_HOST, default=user_input[CONF_HOST]): str, vol.Required("extended_data", default=False): bool, } ), errors=self._errors, ) - async def async_step_import(self, user_input: dict[str, Any]) -> ConfigFlowResult: - """Import a config entry.""" - - user_input = { - CONF_HOST: DEFAULT_HOST, - CONF_NAME: DEFAULT_NAME, - "extended_data": False, - **user_input, - } - - user_input[CONF_HOST] = self._parse_url(user_input[CONF_HOST]) - - if self._host_in_configuration_exists(user_input[CONF_HOST]): - return self.async_abort(reason="already_configured") - - return await self.async_step_user(user_input) - async def async_step_reconfigure( self, user_input: dict[str, Any] | None = None ) -> ConfigFlowResult: diff --git a/homeassistant/components/solarlog/coordinator.py b/homeassistant/components/solarlog/coordinator.py index d2963e1950ebda..5c9aa540261f82 100644 --- a/homeassistant/components/solarlog/coordinator.py +++ b/homeassistant/components/solarlog/coordinator.py @@ -12,11 +12,12 @@ SolarLogConnectionError, SolarLogUpdateError, ) +from solarlog_cli.solarlog_models import SolarlogData from homeassistant.const import CONF_HOST from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady -from homeassistant.helpers import update_coordinator +from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed _LOGGER = logging.getLogger(__name__) @@ -24,7 +25,7 @@ from . import SolarlogConfigEntry -class SolarlogData(update_coordinator.DataUpdateCoordinator): +class SolarLogCoordinator(DataUpdateCoordinator[SolarlogData]): """Get and update the latest data.""" def __init__(self, hass: HomeAssistant, entry: SolarlogConfigEntry) -> None: @@ -43,22 +44,29 @@ def __init__(self, hass: HomeAssistant, entry: SolarlogConfigEntry) -> None: self.name = entry.title self.host = url.geturl() - extended_data = entry.data["extended_data"] - self.solarlog = SolarLogConnector( - self.host, extended_data, hass.config.time_zone + self.host, entry.data["extended_data"], hass.config.time_zone ) - async def _async_update_data(self): + async def _async_setup(self) -> None: + """Do initialization logic.""" + if self.solarlog.extended_data: + device_list = await self.solarlog.update_device_list() + self.solarlog.set_enabled_devices({key: True for key in device_list}) + + async def _async_update_data(self) -> SolarlogData: """Update the data from the SolarLog device.""" _LOGGER.debug("Start data update") try: data = await self.solarlog.update_data() + if self.solarlog.extended_data: + await self.solarlog.update_device_list() + data.inverter_data = await self.solarlog.update_inverter_data() except SolarLogConnectionError as err: raise ConfigEntryNotReady(err) from err except SolarLogUpdateError as err: - raise update_coordinator.UpdateFailed(err) from err + raise UpdateFailed(err) from err _LOGGER.debug("Data successfully updated") diff --git a/homeassistant/components/solarlog/diagnostics.py b/homeassistant/components/solarlog/diagnostics.py new file mode 100644 index 00000000000000..02f6c96edc2eab --- /dev/null +++ b/homeassistant/components/solarlog/diagnostics.py @@ -0,0 +1,27 @@ +"""Provides diagnostics for Solarlog.""" + +from __future__ import annotations + +from typing import Any + +from homeassistant.components.diagnostics import async_redact_data +from homeassistant.const import CONF_HOST +from homeassistant.core import HomeAssistant + +from . import SolarlogConfigEntry + +TO_REDACT = [ + CONF_HOST, +] + + +async def async_get_config_entry_diagnostics( + hass: HomeAssistant, config_entry: SolarlogConfigEntry +) -> dict[str, Any]: + """Return diagnostics for a config entry.""" + data = config_entry.runtime_data.data + + return { + "config_entry": async_redact_data(config_entry.as_dict(), TO_REDACT), + "solarlog_data": data.to_dict(), + } diff --git a/homeassistant/components/solarlog/entity.py b/homeassistant/components/solarlog/entity.py new file mode 100644 index 00000000000000..1d91fc8726b210 --- /dev/null +++ b/homeassistant/components/solarlog/entity.py @@ -0,0 +1,71 @@ +"""Entities for SolarLog integration.""" + +from __future__ import annotations + +from homeassistant.components.sensor import SensorEntityDescription +from homeassistant.helpers.device_registry import DeviceInfo +from homeassistant.helpers.update_coordinator import CoordinatorEntity +from homeassistant.util import slugify + +from .const import DOMAIN +from .coordinator import SolarLogCoordinator + + +class SolarLogBaseEntity(CoordinatorEntity[SolarLogCoordinator]): + """SolarLog base entity.""" + + _attr_has_entity_name = True + + def __init__( + self, + coordinator: SolarLogCoordinator, + description: SensorEntityDescription, + ) -> None: + """Initialize the SolarLogCoordinator sensor.""" + super().__init__(coordinator) + + self.entity_description = description + + +class SolarLogCoordinatorEntity(SolarLogBaseEntity): + """Base SolarLog Coordinator entity.""" + + def __init__( + self, + coordinator: SolarLogCoordinator, + description: SensorEntityDescription, + ) -> None: + """Initialize the SolarLogCoordinator sensor.""" + super().__init__(coordinator, description) + + self._attr_unique_id = f"{coordinator.unique_id}-{description.key}" + self._attr_device_info = DeviceInfo( + manufacturer="Solar-Log", + model="Controller", + identifiers={(DOMAIN, coordinator.unique_id)}, + name=coordinator.name, + configuration_url=coordinator.host, + ) + + +class SolarLogInverterEntity(SolarLogBaseEntity): + """Base SolarLog inverter entity.""" + + def __init__( + self, + coordinator: SolarLogCoordinator, + description: SensorEntityDescription, + device_id: int, + ) -> None: + """Initialize the SolarLogInverter sensor.""" + super().__init__(coordinator, description) + name = f"{coordinator.unique_id}-{slugify(coordinator.solarlog.device_name(device_id))}" + self._attr_unique_id = f"{name}-{description.key}" + self._attr_device_info = DeviceInfo( + manufacturer="Solar-Log", + model="Inverter", + identifiers={(DOMAIN, name)}, + name=coordinator.solarlog.device_name(device_id), + via_device=(DOMAIN, coordinator.unique_id), + ) + self.device_id = device_id diff --git a/homeassistant/components/solarlog/manifest.json b/homeassistant/components/solarlog/manifest.json index 0c097b7146d4aa..eb2268e08dada9 100644 --- a/homeassistant/components/solarlog/manifest.json +++ b/homeassistant/components/solarlog/manifest.json @@ -6,5 +6,5 @@ "documentation": "https://www.home-assistant.io/integrations/solarlog", "iot_class": "local_polling", "loggers": ["solarlog_cli"], - "requirements": ["solarlog_cli==0.1.6"] + "requirements": ["solarlog_cli==0.2.2"] } diff --git a/homeassistant/components/solarlog/sensor.py b/homeassistant/components/solarlog/sensor.py index 45961133e8ac8a..91e18da1cb2675 100644 --- a/homeassistant/components/solarlog/sensor.py +++ b/homeassistant/components/solarlog/sensor.py @@ -1,9 +1,13 @@ """Platform for solarlog sensors.""" +from __future__ import annotations + from collections.abc import Callable from dataclasses import dataclass from datetime import datetime +from solarlog_cli.solarlog_models import InverterData, SolarlogData + from homeassistant.components.sensor import ( SensorDeviceClass, SensorEntity, @@ -17,184 +21,246 @@ UnitOfPower, ) from homeassistant.core import HomeAssistant -from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback -from homeassistant.helpers.update_coordinator import CoordinatorEntity +from homeassistant.helpers.typing import StateType + +from . import SolarlogConfigEntry +from .entity import SolarLogCoordinatorEntity, SolarLogInverterEntity + + +@dataclass(frozen=True, kw_only=True) +class SolarLogCoordinatorSensorEntityDescription(SensorEntityDescription): + """Describes Solarlog coordinator sensor entity.""" -from . import SolarlogConfigEntry, SolarlogData -from .const import DOMAIN + value_fn: Callable[[SolarlogData], StateType | datetime | None] -@dataclass(frozen=True) -class SolarLogSensorEntityDescription(SensorEntityDescription): - """Describes Solarlog sensor entity.""" +@dataclass(frozen=True, kw_only=True) +class SolarLogInverterSensorEntityDescription(SensorEntityDescription): + """Describes Solarlog inverter sensor entity.""" - value: Callable[[float | int], float] | Callable[[datetime], datetime] | None = None + value_fn: Callable[[InverterData], float | None] -SENSOR_TYPES: tuple[SolarLogSensorEntityDescription, ...] = ( - SolarLogSensorEntityDescription( +SOLARLOG_SENSOR_TYPES: tuple[SolarLogCoordinatorSensorEntityDescription, ...] = ( + SolarLogCoordinatorSensorEntityDescription( key="last_updated", translation_key="last_update", device_class=SensorDeviceClass.TIMESTAMP, + value_fn=lambda data: data.last_updated, ), - SolarLogSensorEntityDescription( + SolarLogCoordinatorSensorEntityDescription( key="power_ac", translation_key="power_ac", native_unit_of_measurement=UnitOfPower.WATT, device_class=SensorDeviceClass.POWER, state_class=SensorStateClass.MEASUREMENT, + value_fn=lambda data: data.power_ac, ), - SolarLogSensorEntityDescription( + SolarLogCoordinatorSensorEntityDescription( key="power_dc", translation_key="power_dc", native_unit_of_measurement=UnitOfPower.WATT, device_class=SensorDeviceClass.POWER, state_class=SensorStateClass.MEASUREMENT, + value_fn=lambda data: data.power_dc, ), - SolarLogSensorEntityDescription( + SolarLogCoordinatorSensorEntityDescription( key="voltage_ac", translation_key="voltage_ac", native_unit_of_measurement=UnitOfElectricPotential.VOLT, device_class=SensorDeviceClass.VOLTAGE, state_class=SensorStateClass.MEASUREMENT, + value_fn=lambda data: data.voltage_ac, ), - SolarLogSensorEntityDescription( + SolarLogCoordinatorSensorEntityDescription( key="voltage_dc", translation_key="voltage_dc", native_unit_of_measurement=UnitOfElectricPotential.VOLT, device_class=SensorDeviceClass.VOLTAGE, state_class=SensorStateClass.MEASUREMENT, + value_fn=lambda data: data.voltage_dc, ), - SolarLogSensorEntityDescription( + SolarLogCoordinatorSensorEntityDescription( key="yield_day", translation_key="yield_day", - native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, - value=lambda value: round(value / 1000, 3), + suggested_display_precision=3, + value_fn=lambda data: data.yield_day, ), - SolarLogSensorEntityDescription( + SolarLogCoordinatorSensorEntityDescription( key="yield_yesterday", translation_key="yield_yesterday", - native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, - value=lambda value: round(value / 1000, 3), + suggested_display_precision=3, + value_fn=lambda data: data.yield_yesterday, ), - SolarLogSensorEntityDescription( + SolarLogCoordinatorSensorEntityDescription( key="yield_month", translation_key="yield_month", - native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, - value=lambda value: round(value / 1000, 3), + suggested_display_precision=3, + value_fn=lambda data: data.yield_month, ), - SolarLogSensorEntityDescription( + SolarLogCoordinatorSensorEntityDescription( key="yield_year", translation_key="yield_year", - native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, - value=lambda value: round(value / 1000, 3), + value_fn=lambda data: data.yield_year, ), - SolarLogSensorEntityDescription( + SolarLogCoordinatorSensorEntityDescription( key="yield_total", translation_key="yield_total", - native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, state_class=SensorStateClass.TOTAL, - value=lambda value: round(value / 1000, 3), + suggested_display_precision=3, + value_fn=lambda data: data.yield_total, ), - SolarLogSensorEntityDescription( + SolarLogCoordinatorSensorEntityDescription( key="consumption_ac", translation_key="consumption_ac", native_unit_of_measurement=UnitOfPower.WATT, device_class=SensorDeviceClass.POWER, state_class=SensorStateClass.MEASUREMENT, + value_fn=lambda data: data.consumption_ac, ), - SolarLogSensorEntityDescription( + SolarLogCoordinatorSensorEntityDescription( key="consumption_day", translation_key="consumption_day", - native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, - value=lambda value: round(value / 1000, 3), + suggested_display_precision=3, + value_fn=lambda data: data.consumption_day, ), - SolarLogSensorEntityDescription( + SolarLogCoordinatorSensorEntityDescription( key="consumption_yesterday", translation_key="consumption_yesterday", - native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, - value=lambda value: round(value / 1000, 3), + suggested_display_precision=3, + value_fn=lambda data: data.consumption_yesterday, ), - SolarLogSensorEntityDescription( + SolarLogCoordinatorSensorEntityDescription( key="consumption_month", translation_key="consumption_month", - native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, - value=lambda value: round(value / 1000, 3), + suggested_display_precision=3, + value_fn=lambda data: data.consumption_month, ), - SolarLogSensorEntityDescription( + SolarLogCoordinatorSensorEntityDescription( key="consumption_year", translation_key="consumption_year", - native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, - value=lambda value: round(value / 1000, 3), + suggested_display_precision=3, + value_fn=lambda data: data.consumption_year, ), - SolarLogSensorEntityDescription( + SolarLogCoordinatorSensorEntityDescription( key="consumption_total", translation_key="consumption_total", - native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, state_class=SensorStateClass.TOTAL, - value=lambda value: round(value / 1000, 3), + suggested_display_precision=3, + value_fn=lambda data: data.consumption_total, ), - SolarLogSensorEntityDescription( + SolarLogCoordinatorSensorEntityDescription( key="self_consumption_year", translation_key="self_consumption_year", native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, state_class=SensorStateClass.TOTAL_INCREASING, + value_fn=lambda data: data.self_consumption_year, ), - SolarLogSensorEntityDescription( + SolarLogCoordinatorSensorEntityDescription( key="total_power", translation_key="total_power", native_unit_of_measurement=UnitOfPower.WATT, device_class=SensorDeviceClass.POWER, + value_fn=lambda data: data.total_power, ), - SolarLogSensorEntityDescription( + SolarLogCoordinatorSensorEntityDescription( key="alternator_loss", translation_key="alternator_loss", native_unit_of_measurement=UnitOfPower.WATT, device_class=SensorDeviceClass.POWER, state_class=SensorStateClass.MEASUREMENT, + value_fn=lambda data: data.alternator_loss, ), - SolarLogSensorEntityDescription( + SolarLogCoordinatorSensorEntityDescription( key="capacity", translation_key="capacity", native_unit_of_measurement=PERCENTAGE, device_class=SensorDeviceClass.POWER_FACTOR, state_class=SensorStateClass.MEASUREMENT, - value=lambda value: round(value * 100, 1), + suggested_display_precision=1, + value_fn=lambda data: data.capacity, ), - SolarLogSensorEntityDescription( + SolarLogCoordinatorSensorEntityDescription( key="efficiency", translation_key="efficiency", native_unit_of_measurement=PERCENTAGE, device_class=SensorDeviceClass.POWER_FACTOR, state_class=SensorStateClass.MEASUREMENT, - value=lambda value: round(value * 100, 1), + suggested_display_precision=1, + value_fn=lambda data: data.efficiency, ), - SolarLogSensorEntityDescription( + SolarLogCoordinatorSensorEntityDescription( key="power_available", translation_key="power_available", native_unit_of_measurement=UnitOfPower.WATT, device_class=SensorDeviceClass.POWER, state_class=SensorStateClass.MEASUREMENT, + value_fn=lambda data: data.power_available, ), - SolarLogSensorEntityDescription( + SolarLogCoordinatorSensorEntityDescription( key="usage", translation_key="usage", native_unit_of_measurement=PERCENTAGE, device_class=SensorDeviceClass.POWER_FACTOR, state_class=SensorStateClass.MEASUREMENT, - value=lambda value: round(value * 100, 1), + suggested_display_precision=1, + value_fn=lambda data: data.usage, + ), +) + +INVERTER_SENSOR_TYPES: tuple[SolarLogInverterSensorEntityDescription, ...] = ( + SolarLogInverterSensorEntityDescription( + key="current_power", + translation_key="current_power", + native_unit_of_measurement=UnitOfPower.WATT, + device_class=SensorDeviceClass.POWER, + state_class=SensorStateClass.MEASUREMENT, + value_fn=lambda inverter: inverter.current_power, + ), + SolarLogInverterSensorEntityDescription( + key="consumption_year", + translation_key="consumption_year", + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + device_class=SensorDeviceClass.ENERGY, + suggested_display_precision=3, + value_fn=( + lambda inverter: None + if inverter.consumption_year is None + else inverter.consumption_year + ), ), ) @@ -206,39 +272,45 @@ async def async_setup_entry( ) -> None: """Add solarlog entry.""" coordinator = entry.runtime_data - async_add_entities( - SolarlogSensor(coordinator, description) for description in SENSOR_TYPES - ) - - -class SolarlogSensor(CoordinatorEntity[SolarlogData], SensorEntity): - """Representation of a Sensor.""" - - _attr_has_entity_name = True - - entity_description: SolarLogSensorEntityDescription - - def __init__( - self, - coordinator: SolarlogData, - description: SolarLogSensorEntityDescription, - ) -> None: - """Initialize the sensor.""" - super().__init__(coordinator) - self.entity_description = description - self._attr_unique_id = f"{coordinator.unique_id}_{description.key}" - self._attr_device_info = DeviceInfo( - identifiers={(DOMAIN, coordinator.unique_id)}, - manufacturer="Solar-Log", - name=coordinator.name, - configuration_url=coordinator.host, + + entities: list[SensorEntity] = [ + SolarLogCoordinatorSensor(coordinator, sensor) + for sensor in SOLARLOG_SENSOR_TYPES + ] + + device_data = coordinator.data.inverter_data + + if device_data: + entities.extend( + SolarLogInverterSensor(coordinator, sensor, device_id) + for device_id in device_data + for sensor in INVERTER_SENSOR_TYPES ) + async_add_entities(entities) + + +class SolarLogCoordinatorSensor(SolarLogCoordinatorEntity, SensorEntity): + """Represents a SolarLog sensor.""" + + entity_description: SolarLogCoordinatorSensorEntityDescription + @property - def native_value(self): - """Return the native sensor value.""" - raw_attr = self.coordinator.data.get(self.entity_description.key) + def native_value(self) -> StateType | datetime: + """Return the state for this sensor.""" + + return self.entity_description.value_fn(self.coordinator.data) + + +class SolarLogInverterSensor(SolarLogInverterEntity, SensorEntity): + """Represents a SolarLog inverter sensor.""" - if self.entity_description.value: - return self.entity_description.value(raw_attr) - return raw_attr + entity_description: SolarLogInverterSensorEntityDescription + + @property + def native_value(self) -> StateType: + """Return the state for this sensor.""" + + return self.entity_description.value_fn( + self.coordinator.data.inverter_data[self.device_id] + ) diff --git a/homeassistant/components/soma/config_flow.py b/homeassistant/components/soma/config_flow.py index 773a24d5b44cd4..caf361d5c3c39b 100644 --- a/homeassistant/components/soma/config_flow.py +++ b/homeassistant/components/soma/config_flow.py @@ -1,12 +1,13 @@ """Config flow for Soma.""" import logging +from typing import Any from api.soma_api import SomaApi from requests import RequestException import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_HOST, CONF_PORT from .const import DOMAIN @@ -24,7 +25,9 @@ class SomaFlowHandler(ConfigFlow, domain=DOMAIN): def __init__(self) -> None: """Instantiate config flow.""" - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle a flow start.""" if user_input is None: data = { @@ -36,7 +39,7 @@ async def async_step_user(self, user_input=None): return await self.async_step_creation(user_input) - async def async_step_creation(self, user_input=None): + async def async_step_creation(self, user_input: dict[str, Any]) -> ConfigFlowResult: """Finish config flow.""" try: api = await self.hass.async_add_executor_job( @@ -64,8 +67,8 @@ async def async_step_creation(self, user_input=None): _LOGGER.error("Connection to SOMA Connect failed with KeyError") return self.async_abort(reason="connection_error") - async def async_step_import(self, user_input=None): + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Handle flow start from existing config section.""" if self._async_current_entries(): return self.async_abort(reason="already_setup") - return await self.async_step_creation(user_input) + return await self.async_step_creation(import_data) diff --git a/homeassistant/components/somfy_mylink/config_flow.py b/homeassistant/components/somfy_mylink/config_flow.py index a13f036210d126..705db43362eefa 100644 --- a/homeassistant/components/somfy_mylink/config_flow.py +++ b/homeassistant/components/somfy_mylink/config_flow.py @@ -4,6 +4,7 @@ from copy import deepcopy import logging +from typing import Any from somfy_mylink_synergy import SomfyMyLinkSynergy import voluptuous as vol @@ -61,11 +62,11 @@ class SomfyConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - def __init__(self): + def __init__(self) -> None: """Initialize the somfy_mylink flow.""" - self.host = None - self.mac = None - self.ip_address = None + self.host: str | None = None + self.mac: str | None = None + self.ip_address: str | None = None async def async_step_dhcp( self, discovery_info: dhcp.DhcpServiceInfo @@ -82,7 +83,9 @@ async def async_step_dhcp( self.context["title_placeholders"] = {"ip": self.ip_address, "mac": self.mac} return await self.async_step_user() - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" errors = {} @@ -113,11 +116,6 @@ async def async_step_user(self, user_input=None): errors=errors, ) - async def async_step_import(self, user_input): - """Handle import.""" - self._async_abort_entries_match({CONF_HOST: user_input[CONF_HOST]}) - return await self.async_step_user(user_input) - @staticmethod @callback def async_get_options_flow( @@ -134,7 +132,7 @@ def __init__(self, config_entry: ConfigEntry) -> None: """Initialize options flow.""" self.config_entry = config_entry self.options = deepcopy(dict(config_entry.options)) - self._target_id = None + self._target_id: str | None = None @callback def _async_callback_targets(self): @@ -152,7 +150,9 @@ def _async_get_target_name(self, target_id) -> str: return cover["name"] raise KeyError - async def async_step_init(self, user_input=None): + async def async_step_init( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle options flow.""" if self.config_entry.state is not ConfigEntryState.LOADED: @@ -175,9 +175,13 @@ async def async_step_init(self, user_input=None): return self.async_show_form(step_id="init", data_schema=data_schema, errors={}) - async def async_step_target_config(self, user_input=None, target_id=None): + async def async_step_target_config( + self, user_input: dict[str, bool] | None = None, target_id: str | None = None + ) -> ConfigFlowResult: """Handle options flow for target.""" - reversed_target_ids = self.options.setdefault(CONF_REVERSED_TARGET_IDS, {}) + reversed_target_ids: dict[str | None, bool] = self.options.setdefault( + CONF_REVERSED_TARGET_IDS, {} + ) if user_input is not None: if user_input[CONF_REVERSE] != reversed_target_ids.get(self._target_id): diff --git a/homeassistant/components/songpal/config_flow.py b/homeassistant/components/songpal/config_flow.py index f8a0db3815de78..7f10d22b8c6f0f 100644 --- a/homeassistant/components/songpal/config_flow.py +++ b/homeassistant/components/songpal/config_flow.py @@ -3,6 +3,7 @@ from __future__ import annotations import logging +from typing import TYPE_CHECKING, Any from urllib.parse import urlparse from songpal import Device, SongpalException @@ -20,7 +21,7 @@ class SongpalConfig: """Device Configuration.""" - def __init__(self, name, host, endpoint): + def __init__(self, name: str, host: str | None, endpoint: str) -> None: """Initialize Configuration.""" self.name = name self.host = host @@ -32,11 +33,11 @@ class SongpalConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - def __init__(self) -> None: - """Initialize the flow.""" - self.conf: SongpalConfig | None = None + conf: SongpalConfig - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, str] | None = None + ) -> ConfigFlowResult: """Handle a flow initiated by the user.""" if user_input is None: return self.async_show_form( @@ -72,7 +73,9 @@ async def async_step_user(self, user_input=None): return await self.async_step_init(user_input) - async def async_step_init(self, user_input=None): + async def async_step_init( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle a flow start.""" # Check if already configured self._async_abort_entries_match({CONF_ENDPOINT: self.conf.endpoint}) @@ -119,14 +122,16 @@ async def async_step_ssdp( CONF_HOST: parsed_url.hostname, } + if TYPE_CHECKING: + assert isinstance(parsed_url.hostname, str) self.conf = SongpalConfig(friendly_name, parsed_url.hostname, endpoint) return await self.async_step_init() - async def async_step_import(self, user_input=None): + async def async_step_import(self, import_data: dict[str, str]) -> ConfigFlowResult: """Import a config entry.""" - name = user_input.get(CONF_NAME) - endpoint = user_input.get(CONF_ENDPOINT) + name = import_data.get(CONF_NAME) + endpoint = import_data[CONF_ENDPOINT] parsed_url = urlparse(endpoint) # Try to connect to test the endpoint @@ -143,4 +148,4 @@ async def async_step_import(self, user_input=None): self.conf = SongpalConfig(name, parsed_url.hostname, endpoint) - return await self.async_step_init(user_input) + return await self.async_step_init(import_data) diff --git a/homeassistant/components/songpal/icons.json b/homeassistant/components/songpal/icons.json index 1c831fbbd008b6..6e7cf359c238b0 100644 --- a/homeassistant/components/songpal/icons.json +++ b/homeassistant/components/songpal/icons.json @@ -1,5 +1,7 @@ { "services": { - "set_sound_setting": "mdi:volume-high" + "set_sound_setting": { + "service": "mdi:volume-high" + } } } diff --git a/homeassistant/components/sonos/icons.json b/homeassistant/components/sonos/icons.json index e2545358ba6e09..45027d8eabddc9 100644 --- a/homeassistant/components/sonos/icons.json +++ b/homeassistant/components/sonos/icons.json @@ -44,12 +44,29 @@ } }, "services": { - "snapshot": "mdi:camera", - "restore": "mdi:camera-retake", - "set_sleep_timer": "mdi:alarm", - "clear_sleep_timer": "mdi:alarm-off", - "play_queue": "mdi:play", - "remove_from_queue": "mdi:playlist-remove", - "update_alarm": "mdi:alarm" + "snapshot": { + "service": "mdi:camera" + }, + "restore": { + "service": "mdi:camera-retake" + }, + "set_sleep_timer": { + "service": "mdi:alarm" + }, + "clear_sleep_timer": { + "service": "mdi:alarm-off" + }, + "play_queue": { + "service": "mdi:play" + }, + "remove_from_queue": { + "service": "mdi:playlist-remove" + }, + "update_alarm": { + "service": "mdi:alarm" + }, + "get_queue": { + "service": "mdi:queue-first-in-last-out" + } } } diff --git a/homeassistant/components/sonos/media_player.py b/homeassistant/components/sonos/media_player.py index ff6981cfc9b5e8..c4d417b0394eb6 100644 --- a/homeassistant/components/sonos/media_player.py +++ b/homeassistant/components/sonos/media_player.py @@ -14,7 +14,7 @@ PLAY_MODE_BY_MEANING, PLAY_MODES, ) -from soco.data_structures import DidlFavorite +from soco.data_structures import DidlFavorite, DidlMusicTrack from soco.ms_data_structures import MusicServiceItem from sonos_websocket.exception import SonosWebsocketError import voluptuous as vol @@ -22,8 +22,12 @@ from homeassistant.components import media_source, spotify from homeassistant.components.media_player import ( ATTR_INPUT_SOURCE, + ATTR_MEDIA_ALBUM_NAME, ATTR_MEDIA_ANNOUNCE, + ATTR_MEDIA_ARTIST, + ATTR_MEDIA_CONTENT_ID, ATTR_MEDIA_ENQUEUE, + ATTR_MEDIA_TITLE, BrowseMedia, MediaPlayerDeviceClass, MediaPlayerEnqueue, @@ -38,7 +42,7 @@ from homeassistant.components.plex.services import process_plex_payload from homeassistant.config_entries import ConfigEntry from homeassistant.const import ATTR_TIME -from homeassistant.core import HomeAssistant, ServiceCall, callback +from homeassistant.core import HomeAssistant, ServiceCall, SupportsResponse, callback from homeassistant.exceptions import HomeAssistantError, ServiceValidationError from homeassistant.helpers import config_validation as cv, entity_platform, service from homeassistant.helpers.dispatcher import async_dispatcher_connect @@ -88,6 +92,7 @@ SERVICE_UPDATE_ALARM = "update_alarm" SERVICE_PLAY_QUEUE = "play_queue" SERVICE_REMOVE_FROM_QUEUE = "remove_from_queue" +SERVICE_GET_QUEUE = "get_queue" ATTR_SLEEP_TIME = "sleep_time" ATTR_ALARM_ID = "alarm_id" @@ -190,6 +195,13 @@ async def async_service_handle(service_call: ServiceCall) -> None: "remove_from_queue", ) + platform.async_register_entity_service( + SERVICE_GET_QUEUE, + None, + "get_queue", + supports_response=SupportsResponse.ONLY, + ) + class SonosMediaPlayerEntity(SonosEntity, MediaPlayerEntity): """Representation of a Sonos entity.""" @@ -542,8 +554,13 @@ async def async_play_media( raise HomeAssistantError( f"Error when calling Sonos websocket: {exc}" ) from exc - if response["success"]: + if response.get("success"): return + raise HomeAssistantError( + translation_domain=SONOS_DOMAIN, + translation_key="announce_media_error", + translation_placeholders={"media_id": media_id, "response": response}, + ) if spotify.is_spotify_media_type(media_type): media_type = spotify.resolve_spotify_media_type(media_type) @@ -655,14 +672,23 @@ def _play_media( soco.play_from_queue(0) elif media_type in PLAYABLE_MEDIA_TYPES: item = media_browser.get_media(self.media.library, media_id, media_type) - if not item: - _LOGGER.error('Could not find "%s" in the library', media_id) - return - + raise ServiceValidationError( + translation_domain=SONOS_DOMAIN, + translation_key="invalid_media", + translation_placeholders={ + "media_id": media_id, + }, + ) self._play_media_queue(soco, item, enqueue) else: - _LOGGER.error('Sonos does not support a media type of "%s"', media_type) + raise ServiceValidationError( + translation_domain=SONOS_DOMAIN, + translation_key="invalid_content_type", + translation_placeholders={ + "media_type": media_type, + }, + ) def _play_media_queue( self, soco: SoCo, item: MusicServiceItem, enqueue: MediaPlayerEnqueue @@ -736,6 +762,20 @@ def remove_from_queue(self, queue_position: int = 0) -> None: """Remove item from the queue.""" self.coordinator.soco.remove_from_queue(queue_position) + @soco_error() + def get_queue(self) -> list[dict]: + """Get the queue.""" + queue: list[DidlMusicTrack] = self.coordinator.soco.get_queue(max_items=0) + return [ + { + ATTR_MEDIA_TITLE: track.title, + ATTR_MEDIA_ALBUM_NAME: track.album, + ATTR_MEDIA_ARTIST: track.creator, + ATTR_MEDIA_CONTENT_ID: track.get_uri(), + } + for track in queue + ] + @property def extra_state_attributes(self) -> dict[str, Any]: """Return entity specific state attributes.""" diff --git a/homeassistant/components/sonos/services.yaml b/homeassistant/components/sonos/services.yaml index f6df83ef6ed287..89706428899725 100644 --- a/homeassistant/components/sonos/services.yaml +++ b/homeassistant/components/sonos/services.yaml @@ -63,6 +63,12 @@ remove_from_queue: max: 10000 mode: box +get_queue: + target: + entity: + integration: sonos + domain: media_player + update_alarm: target: device: diff --git a/homeassistant/components/sonos/strings.json b/homeassistant/components/sonos/strings.json index 5833e5a27f2439..d3774e85213b15 100644 --- a/homeassistant/components/sonos/strings.json +++ b/homeassistant/components/sonos/strings.json @@ -172,6 +172,10 @@ "description": "Enable or disable including grouped rooms." } } + }, + "get_queue": { + "name": "Get queue", + "description": "Returns the contents of the queue." } }, "exceptions": { @@ -180,6 +184,15 @@ }, "invalid_sonos_playlist": { "message": "Could not find Sonos playlist: {name}" + }, + "invalid_media": { + "message": "Could not find media in library: {media_id}" + }, + "invalid_content_type": { + "message": "Sonos does not support media content type: {media_type}" + }, + "announce_media_error": { + "message": "Announcing clip {media_id} failed {response}" } } } diff --git a/homeassistant/components/soundtouch/config_flow.py b/homeassistant/components/soundtouch/config_flow.py index c8e8ce945db48e..7c637d7111132a 100644 --- a/homeassistant/components/soundtouch/config_flow.py +++ b/homeassistant/components/soundtouch/config_flow.py @@ -1,6 +1,7 @@ """Config flow for Bose SoundTouch integration.""" import logging +from typing import Any from libsoundtouch import soundtouch_device from requests import RequestException @@ -21,12 +22,14 @@ class SoundtouchConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - def __init__(self): + def __init__(self) -> None: """Initialize a new SoundTouch config flow.""" - self.host = None + self.host: str | None = None self.name = None - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle a flow initiated by the user.""" errors = {} @@ -65,7 +68,9 @@ async def async_step_zeroconf( self.context["title_placeholders"] = {"name": self.name} return await self.async_step_zeroconf_confirm() - async def async_step_zeroconf_confirm(self, user_input=None): + async def async_step_zeroconf_confirm( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle user-confirmation of discovered node.""" if user_input is not None: return await self._async_create_soundtouch_entry() diff --git a/homeassistant/components/soundtouch/icons.json b/homeassistant/components/soundtouch/icons.json index 0dd41f4f881a9d..721a5c77032d6f 100644 --- a/homeassistant/components/soundtouch/icons.json +++ b/homeassistant/components/soundtouch/icons.json @@ -1,8 +1,16 @@ { "services": { - "play_everywhere": "mdi:play", - "create_zone": "mdi:plus", - "add_zone_slave": "mdi:plus", - "remove_zone_slave": "mdi:minus" + "play_everywhere": { + "service": "mdi:play" + }, + "create_zone": { + "service": "mdi:plus" + }, + "add_zone_slave": { + "service": "mdi:plus" + }, + "remove_zone_slave": { + "service": "mdi:minus" + } } } diff --git a/homeassistant/components/spider/config_flow.py b/homeassistant/components/spider/config_flow.py index a678ea730516be..0c305adbc394b5 100644 --- a/homeassistant/components/spider/config_flow.py +++ b/homeassistant/components/spider/config_flow.py @@ -1,11 +1,12 @@ """Config flow for Spider.""" import logging +from typing import Any from spiderpy.spiderapi import SpiderApi, SpiderApiException, UnauthorizedException import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_PASSWORD, CONF_SCAN_INTERVAL, CONF_USERNAME from .const import DEFAULT_SCAN_INTERVAL, DOMAIN @@ -49,7 +50,9 @@ def _try_connect(self): return RESULT_SUCCESS - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle a flow initiated by the user.""" if self._async_current_entries(): return self.async_abort(reason="single_instance_allowed") @@ -79,6 +82,6 @@ async def async_step_user(self, user_input=None): errors=errors, ) - async def async_step_import(self, import_data): + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Import spider config from configuration.yaml.""" return await self.async_step_user(import_data) diff --git a/homeassistant/components/spotify/browse_media.py b/homeassistant/components/spotify/browse_media.py index cff7cae5ebddf5..abcb6df6205395 100644 --- a/homeassistant/components/spotify/browse_media.py +++ b/homeassistant/components/spotify/browse_media.py @@ -172,10 +172,17 @@ async def async_browse_media( # Check for config entry specifier, and extract Spotify URI parsed_url = yarl.URL(media_content_id) + host = parsed_url.host if ( - parsed_url.host is None - or (entry := hass.config_entries.async_get_entry(parsed_url.host)) is None + host is None + # config entry ids can be upper or lower case. Yarl always returns host + # names in lower case, so we need to look for the config entry in both + or ( + entry := hass.config_entries.async_get_entry(host) + or hass.config_entries.async_get_entry(host.upper()) + ) + is None or not isinstance(entry.runtime_data, HomeAssistantSpotifyData) ): raise BrowseError("Invalid Spotify account specified") diff --git a/homeassistant/components/squeezebox/browse_media.py b/homeassistant/components/squeezebox/browse_media.py index bc63bcb7f2ffc3..61ae7b7a403ffd 100644 --- a/homeassistant/components/squeezebox/browse_media.py +++ b/homeassistant/components/squeezebox/browse_media.py @@ -1,19 +1,27 @@ """Support for media browsing.""" +from __future__ import annotations + import contextlib +from typing import Any + +from pysqueezebox import Player from homeassistant.components import media_source from homeassistant.components.media_player import ( BrowseError, BrowseMedia, MediaClass, + MediaPlayerEntity, MediaType, ) +from homeassistant.core import HomeAssistant from homeassistant.helpers.network import is_internal_request -LIBRARY = ["Artists", "Albums", "Tracks", "Playlists", "Genres"] +LIBRARY = ["Favorites", "Artists", "Albums", "Tracks", "Playlists", "Genres"] MEDIA_TYPE_TO_SQUEEZEBOX = { + "Favorites": "favorites", "Artists": "artists", "Albums": "albums", "Tracks": "titles", @@ -32,9 +40,11 @@ MediaType.TRACK: "track_id", MediaType.PLAYLIST: "playlist_id", MediaType.GENRE: "genre_id", + "Favorites": "item_id", } -CONTENT_TYPE_MEDIA_CLASS = { +CONTENT_TYPE_MEDIA_CLASS: dict[str | MediaType, dict[str, MediaClass | None]] = { + "Favorites": {"item": MediaClass.DIRECTORY, "children": MediaClass.TRACK}, "Artists": {"item": MediaClass.DIRECTORY, "children": MediaClass.ARTIST}, "Albums": {"item": MediaClass.DIRECTORY, "children": MediaClass.ALBUM}, "Tracks": {"item": MediaClass.DIRECTORY, "children": MediaClass.TRACK}, @@ -57,20 +67,28 @@ "Tracks": MediaType.TRACK, "Playlists": MediaType.PLAYLIST, "Genres": MediaType.GENRE, + "Favorites": None, # can only be determined after inspecting the item } BROWSE_LIMIT = 1000 -async def build_item_response(entity, player, payload): +async def build_item_response( + entity: MediaPlayerEntity, player: Player, payload: dict[str, str | None] +) -> BrowseMedia: """Create response payload for search described by payload.""" + internal_request = is_internal_request(entity.hass) search_id = payload["search_id"] search_type = payload["search_type"] - + assert ( + search_type is not None + ) # async_browse_media will not call this function if search_type is None media_class = CONTENT_TYPE_MEDIA_CLASS[search_type] + children = None + if search_id and search_id != search_type: browse_id = (SQUEEZEBOX_ID_BY_TYPE[search_type], search_id) else: @@ -82,18 +100,38 @@ async def build_item_response(entity, player, payload): browse_id=browse_id, ) - children = None - if result is not None and result.get("items"): item_type = CONTENT_TYPE_TO_CHILD_TYPE[search_type] - child_media_class = CONTENT_TYPE_MEDIA_CLASS[item_type] children = [] for item in result["items"]: item_id = str(item["id"]) - item_thumbnail = None + item_thumbnail: str | None = None + if item_type: + child_item_type: MediaType | str = item_type + child_media_class = CONTENT_TYPE_MEDIA_CLASS[item_type] + can_expand = child_media_class["children"] is not None + can_play = True + + if search_type == "Favorites": + if "album_id" in item: + item_id = str(item["album_id"]) + child_item_type = MediaType.ALBUM + child_media_class = CONTENT_TYPE_MEDIA_CLASS[MediaType.ALBUM] + can_expand = True + can_play = True + elif item["hasitems"]: + child_item_type = "Favorites" + child_media_class = CONTENT_TYPE_MEDIA_CLASS["Favorites"] + can_expand = True + can_play = False + else: + child_item_type = "Favorites" + child_media_class = CONTENT_TYPE_MEDIA_CLASS[MediaType.TRACK] + can_expand = False + can_play = True - if artwork_track_id := item.get("artwork_track_id"): + if artwork_track_id := item.get("artwork_track_id") and item_type: if internal_request: item_thumbnail = player.generate_image_url_from_track_id( artwork_track_id @@ -102,15 +140,18 @@ async def build_item_response(entity, player, payload): item_thumbnail = entity.get_browse_image_url( item_type, item_id, artwork_track_id ) + else: + item_thumbnail = item.get("image_url") # will not be proxied by HA + assert child_media_class["item"] is not None children.append( BrowseMedia( title=item["title"], media_class=child_media_class["item"], media_content_id=item_id, - media_content_type=item_type, - can_play=True, - can_expand=child_media_class["children"] is not None, + media_content_type=child_item_type, + can_play=can_play, + can_expand=can_expand, thumbnail=item_thumbnail, ) ) @@ -118,21 +159,24 @@ async def build_item_response(entity, player, payload): if children is None: raise BrowseError(f"Media not found: {search_type} / {search_id}") + assert media_class["item"] is not None + if not search_id: + search_id = search_type return BrowseMedia( title=result.get("title"), media_class=media_class["item"], children_media_class=media_class["children"], media_content_id=search_id, media_content_type=search_type, - can_play=True, + can_play=search_type != "Favorites", children=children, can_expand=True, ) -async def library_payload(hass, player): +async def library_payload(hass: HomeAssistant, player: Player) -> BrowseMedia: """Create response payload to describe contents of library.""" - library_info = { + library_info: dict[str, Any] = { "title": "Music Library", "media_class": MediaClass.DIRECTORY, "media_content_id": "library", @@ -144,31 +188,33 @@ async def library_payload(hass, player): for item in LIBRARY: media_class = CONTENT_TYPE_MEDIA_CLASS[item] + result = await player.async_browse( MEDIA_TYPE_TO_SQUEEZEBOX[item], limit=1, ) if result is not None and result.get("items") is not None: + assert media_class["children"] is not None library_info["children"].append( BrowseMedia( title=item, media_class=media_class["children"], media_content_id=item, media_content_type=item, - can_play=True, + can_play=item != "Favorites", can_expand=True, ) ) with contextlib.suppress(media_source.BrowseError): - item = await media_source.async_browse_media( + browse = await media_source.async_browse_media( hass, None, content_filter=media_source_content_filter ) # If domain is None, it's overview of available sources - if item.domain is None: - library_info["children"].extend(item.children) + if browse.domain is None: + library_info["children"].extend(browse.children) else: - library_info["children"].append(item) + library_info["children"].append(browse) return BrowseMedia(**library_info) @@ -178,16 +224,19 @@ def media_source_content_filter(item: BrowseMedia) -> bool: return item.media_content_type.startswith("audio/") -async def generate_playlist(player, payload): +async def generate_playlist(player: Player, payload: dict[str, str]) -> list | None: """Generate playlist from browsing payload.""" media_type = payload["search_type"] media_id = payload["search_id"] if media_type not in SQUEEZEBOX_ID_BY_TYPE: - return None + raise BrowseError(f"Media type not supported: {media_type}") browse_id = (SQUEEZEBOX_ID_BY_TYPE[media_type], media_id) result = await player.async_browse( "titles", limit=BROWSE_LIMIT, browse_id=browse_id ) - return result.get("items") + if result and "items" in result: + items: list = result["items"] + return items + raise BrowseError(f"Media not found: {media_type} / {media_id}") diff --git a/homeassistant/components/squeezebox/config_flow.py b/homeassistant/components/squeezebox/config_flow.py index 95af3e8032ab96..c372c7262d456c 100644 --- a/homeassistant/components/squeezebox/config_flow.py +++ b/homeassistant/components/squeezebox/config_flow.py @@ -1,5 +1,7 @@ """Config flow for Squeezebox integration.""" +from __future__ import annotations + import asyncio from http import HTTPStatus import logging @@ -24,9 +26,11 @@ TIMEOUT = 5 -def _base_schema(discovery_info=None): +def _base_schema( + discovery_info: dict[str, Any] | None = None, +) -> vol.Schema: """Generate base schema.""" - base_schema = {} + base_schema: dict[Any, Any] = {} if discovery_info and CONF_HOST in discovery_info: base_schema.update( { @@ -71,14 +75,14 @@ class SqueezeboxConfigFlow(ConfigFlow, domain=DOMAIN): def __init__(self) -> None: """Initialize an instance of the squeezebox config flow.""" self.data_schema = _base_schema() - self.discovery_info = None + self.discovery_info: dict[str, Any] | None = None - async def _discover(self, uuid=None): + async def _discover(self, uuid: str | None = None) -> None: """Discover an unconfigured LMS server.""" self.discovery_info = None discovery_event = asyncio.Event() - def _discovery_callback(server): + def _discovery_callback(server: Server) -> None: if server.uuid: # ignore already configured uuids for entry in self._async_current_entries(): @@ -131,7 +135,9 @@ async def _validate_input(self, data: dict[str, Any]) -> str | None: return None - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle a flow initialized by the user.""" errors = {} if user_input and CONF_HOST in user_input: @@ -154,7 +160,9 @@ async def async_step_user(self, user_input=None): errors=errors, ) - async def async_step_edit(self, user_input=None): + async def async_step_edit( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Edit a discovered or manually inputted server.""" errors = {} if user_input: @@ -169,7 +177,9 @@ async def async_step_edit(self, user_input=None): step_id="edit", data_schema=self.data_schema, errors=errors ) - async def async_step_integration_discovery(self, discovery_info): + async def async_step_integration_discovery( + self, discovery_info: dict[str, Any] + ) -> ConfigFlowResult: """Handle discovery of a server.""" _LOGGER.debug("Reached server discovery flow with info: %s", discovery_info) if "uuid" in discovery_info: diff --git a/homeassistant/components/squeezebox/icons.json b/homeassistant/components/squeezebox/icons.json index d58f0d5634d866..b11311e1292cff 100644 --- a/homeassistant/components/squeezebox/icons.json +++ b/homeassistant/components/squeezebox/icons.json @@ -1,8 +1,16 @@ { "services": { - "call_method": "mdi:console", - "call_query": "mdi:database", - "sync": "mdi:sync", - "unsync": "mdi:sync-off" + "call_method": { + "service": "mdi:console" + }, + "call_query": { + "service": "mdi:database" + }, + "sync": { + "service": "mdi:sync" + }, + "unsync": { + "service": "mdi:sync-off" + } } } diff --git a/homeassistant/components/squeezebox/manifest.json b/homeassistant/components/squeezebox/manifest.json index 40bc8f36d2294d..c43225f94cd95c 100644 --- a/homeassistant/components/squeezebox/manifest.json +++ b/homeassistant/components/squeezebox/manifest.json @@ -12,5 +12,5 @@ "documentation": "https://www.home-assistant.io/integrations/squeezebox", "iot_class": "local_polling", "loggers": ["pysqueezebox"], - "requirements": ["pysqueezebox==0.7.1"] + "requirements": ["pysqueezebox==0.8.1"] } diff --git a/homeassistant/components/squeezebox/media_player.py b/homeassistant/components/squeezebox/media_player.py index 552b8ed800c49d..0294c17f50aa0e 100644 --- a/homeassistant/components/squeezebox/media_player.py +++ b/homeassistant/components/squeezebox/media_player.py @@ -8,12 +8,13 @@ import logging from typing import Any -from pysqueezebox import Player, async_discover +from pysqueezebox import Player, Server, async_discover import voluptuous as vol from homeassistant.components import media_source from homeassistant.components.media_player import ( ATTR_MEDIA_ENQUEUE, + BrowseMedia, MediaPlayerEnqueue, MediaPlayerEntity, MediaPlayerEntityFeature, @@ -55,11 +56,8 @@ SERVICE_CALL_METHOD = "call_method" SERVICE_CALL_QUERY = "call_query" -SERVICE_SYNC = "sync" -SERVICE_UNSYNC = "unsync" ATTR_QUERY_RESULT = "query_result" -ATTR_SYNC_GROUP = "sync_group" SIGNAL_PLAYER_REDISCOVERED = "squeezebox_player_rediscovered" @@ -74,7 +72,6 @@ ATTR_TO_PROPERTY = [ ATTR_QUERY_RESULT, - ATTR_SYNC_GROUP, ] SQUEEZEBOX_MODE = { @@ -87,7 +84,7 @@ async def start_server_discovery(hass: HomeAssistant) -> None: """Start a server discovery task.""" - def _discovered_server(server): + def _discovered_server(server: Server) -> None: discovery_flow.async_create_flow( hass, DOMAIN, @@ -118,10 +115,10 @@ async def async_setup_entry( known_players = hass.data[DOMAIN].setdefault(KNOWN_PLAYERS, []) lms = entry.runtime_data - async def _player_discovery(now=None): + async def _player_discovery(now: datetime | None = None) -> None: """Discover squeezebox players by polling server.""" - async def _discovered_player(player): + async def _discovered_player(player: Player) -> None: """Handle a (re)discovered player.""" entity = next( ( @@ -180,12 +177,6 @@ async def _discovered_player(player): }, "async_call_query", ) - platform.async_register_entity_service( - SERVICE_SYNC, - {vol.Required(ATTR_OTHER_PLAYER): cv.string}, - "async_sync", - ) - platform.async_register_entity_service(SERVICE_UNSYNC, None, "async_unsync") # Start server discovery task if not already running entry.async_on_unload(async_at_start(hass, start_server_discovery)) @@ -234,7 +225,7 @@ def __init__(self, player: Player) -> None: ) @property - def extra_state_attributes(self): + def extra_state_attributes(self) -> dict[str, Any]: """Return device-specific attributes.""" return { attr: getattr(self, attr) @@ -243,12 +234,13 @@ def extra_state_attributes(self): } @callback - def rediscovered(self, unique_id, connected): + def rediscovered(self, unique_id: str, connected: bool) -> None: """Make a player available again.""" if unique_id == self.unique_id and connected: self._attr_available = True _LOGGER.debug("Player %s is available again", self.name) - self._remove_dispatcher() + if self._remove_dispatcher: + self._remove_dispatcher() @property def state(self) -> MediaPlayerState | None: @@ -288,22 +280,22 @@ def volume_level(self) -> float | None: return None @property - def is_volume_muted(self): + def is_volume_muted(self) -> bool: """Return true if volume is muted.""" - return self._player.muting + return bool(self._player.muting) @property - def media_content_id(self): + def media_content_id(self) -> str | None: """Content ID of current playing media.""" if not self._player.playlist: return None if len(self._player.playlist) > 1: urls = [{"url": track["url"]} for track in self._player.playlist] return json.dumps({"index": self._player.current_index, "urls": urls}) - return self._player.url + return str(self._player.url) @property - def media_content_type(self): + def media_content_type(self) -> MediaType | None: """Content type of current playing media.""" if not self._player.playlist: return None @@ -312,47 +304,47 @@ def media_content_type(self): return MediaType.MUSIC @property - def media_duration(self): + def media_duration(self) -> int: """Duration of current playing media in seconds.""" - return self._player.duration + return int(self._player.duration) if self._player.duration else 0 @property - def media_position(self): + def media_position(self) -> int: """Position of current playing media in seconds.""" - return self._player.time + return int(self._player.time) if self._player.time else 0 @property - def media_position_updated_at(self): + def media_position_updated_at(self) -> datetime | None: """Last time status was updated.""" return self._last_update @property - def media_image_url(self): + def media_image_url(self) -> str | None: """Image url of current playing media.""" - return self._player.image_url + return str(self._player.image_url) @property - def media_title(self): + def media_title(self) -> str | None: """Title of current playing media.""" - return self._player.title + return str(self._player.title) @property - def media_channel(self): + def media_channel(self) -> str | None: """Channel (e.g. webradio name) of current playing media.""" - return self._player.remote_title + return str(self._player.remote_title) @property - def media_artist(self): + def media_artist(self) -> str | None: """Artist of current playing media.""" - return self._player.artist + return str(self._player.artist) @property - def media_album_name(self): + def media_album_name(self) -> str | None: """Album of current playing media.""" - return self._player.album + return str(self._player.album) @property - def repeat(self): + def repeat(self) -> RepeatMode: """Repeat setting.""" if self._player.repeat == "song": return RepeatMode.ONE @@ -361,13 +353,13 @@ def repeat(self): return RepeatMode.OFF @property - def shuffle(self): + def shuffle(self) -> bool: """Boolean if shuffle is enabled.""" # Squeezebox has a third shuffle mode (album) not recognized by Home Assistant - return self._player.shuffle == "song" + return bool(self._player.shuffle == "song") @property - def group_members(self): + def group_members(self) -> list[str]: """List players we are synced with.""" player_ids = { p.unique_id: p.entity_id for p in self.hass.data[DOMAIN][KNOWN_PLAYERS] @@ -379,12 +371,12 @@ def group_members(self): ] @property - def sync_group(self): + def sync_group(self) -> list[str]: """List players we are synced with. Deprecated.""" return self.group_members @property - def query_result(self): + def query_result(self) -> dict | bool: """Return the result from the call_query service.""" return self._query_result @@ -477,7 +469,7 @@ async def async_play_media( try: # a saved playlist by number payload = { - "search_id": int(media_id), + "search_id": media_id, "search_type": MediaType.PLAYLIST, } playlist = await generate_playlist(self._player, payload) @@ -519,7 +511,9 @@ async def async_clear_playlist(self) -> None: """Send the media player the command for clear playlist.""" await self._player.async_clear_playlist() - async def async_call_method(self, command, parameters=None): + async def async_call_method( + self, command: str, parameters: list[str] | None = None + ) -> None: """Call Squeezebox JSON/RPC method. Additional parameters are added to the command to form the list of @@ -530,7 +524,9 @@ async def async_call_method(self, command, parameters=None): all_params.extend(parameters) await self._player.async_query(*all_params) - async def async_call_query(self, command, parameters=None): + async def async_call_query( + self, command: str, parameters: list[str] | None = None + ) -> None: """Call Squeezebox JSON/RPC method where we care about the result. Additional parameters are added to the command to form the list of @@ -560,27 +556,15 @@ async def async_join_players(self, group_members: list[str]) -> None: "Could not find player_id for %s. Not syncing", other_player ) - async def async_sync(self, other_player): - """Sync this Squeezebox player to another. Deprecated.""" - _LOGGER.warning( - "Service squeezebox.sync is deprecated; use media_player.join_players" - " instead" - ) - await self.async_join_players([other_player]) - async def async_unjoin_player(self) -> None: """Unsync this Squeezebox player.""" await self._player.async_unsync() - async def async_unsync(self): - """Unsync this Squeezebox player. Deprecated.""" - _LOGGER.warning( - "Service squeezebox.unsync is deprecated; use media_player.unjoin_player" - " instead" - ) - await self.async_unjoin_player() - - async def async_browse_media(self, media_content_type=None, media_content_id=None): + async def async_browse_media( + self, + media_content_type: MediaType | str | None = None, + media_content_id: str | None = None, + ) -> BrowseMedia: """Implement the websocket media browsing helper.""" _LOGGER.debug( "Reached async_browse_media with content_type %s and content_id %s", @@ -591,7 +575,7 @@ async def async_browse_media(self, media_content_type=None, media_content_id=Non if media_content_type in [None, "library"]: return await library_payload(self.hass, self._player) - if media_source.is_media_source_id(media_content_id): + if media_content_id and media_source.is_media_source_id(media_content_id): return await media_source.async_browse_media( self.hass, media_content_id, content_filter=media_source_content_filter ) diff --git a/homeassistant/components/ssdp/__init__.py b/homeassistant/components/ssdp/__init__.py index 7ca2f3e9318308..f5e2a012730de3 100644 --- a/homeassistant/components/ssdp/__init__.py +++ b/homeassistant/components/ssdp/__init__.py @@ -284,16 +284,13 @@ def async_setup( def async_matching_domains(self, info_with_desc: CaseInsensitiveDict) -> set[str]: """Find domains matching the passed CaseInsensitiveDict.""" assert self._match_by_key is not None - domains = set() - for key, matchers_by_key in self._match_by_key.items(): - if not (match_value := info_with_desc.get(key)): - continue - for domain, matcher in matchers_by_key.get(match_value, []): - if domain in domains: - continue - if all(info_with_desc.get(k) == v for (k, v) in matcher.items()): - domains.add(domain) - return domains + return { + domain + for key, matchers_by_key in self._match_by_key.items() + if (match_value := info_with_desc.get(key)) + for domain, matcher in matchers_by_key.get(match_value, ()) + if info_with_desc.items() >= matcher.items() + } class Scanner: diff --git a/homeassistant/components/starline/config_flow.py b/homeassistant/components/starline/config_flow.py index fbb7fa9acdcec8..5235bd5230b645 100644 --- a/homeassistant/components/starline/config_flow.py +++ b/homeassistant/components/starline/config_flow.py @@ -5,7 +5,7 @@ from starline import StarlineAuth import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.core import callback @@ -31,6 +31,10 @@ class StarlineFlowHandler(ConfigFlow, domain=DOMAIN): VERSION = 1 + _app_code: str + _app_token: str + _captcha_image: str + def __init__(self) -> None: """Initialize flow.""" self._app_id: str | None = None @@ -39,57 +43,64 @@ def __init__(self) -> None: self._password: str | None = None self._mfa_code: str | None = None - self._app_code = None - self._app_token = None self._user_slid = None self._user_id = None self._slnet_token = None self._slnet_token_expires = None - self._captcha_image = None - self._captcha_sid = None - self._captcha_code = None + self._captcha_sid: str | None = None + self._captcha_code: str | None = None self._phone_number = None self._auth = StarlineAuth() - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, str] | None = None + ) -> ConfigFlowResult: """Handle a flow initialized by the user.""" return await self.async_step_auth_app(user_input) - async def async_step_auth_app(self, user_input=None, error=None): + async def async_step_auth_app( + self, user_input: dict[str, str] | None = None + ) -> ConfigFlowResult: """Authenticate application step.""" if user_input is not None: self._app_id = user_input[CONF_APP_ID] self._app_secret = user_input[CONF_APP_SECRET] - return await self._async_authenticate_app(error) - return self._async_form_auth_app(error) + return await self._async_authenticate_app() + return self._async_form_auth_app() - async def async_step_auth_user(self, user_input=None, error=None): + async def async_step_auth_user( + self, user_input: dict[str, str] | None = None + ) -> ConfigFlowResult: """Authenticate user step.""" if user_input is not None: self._username = user_input[CONF_USERNAME] self._password = user_input[CONF_PASSWORD] - return await self._async_authenticate_user(error) - return self._async_form_auth_user(error) + return await self._async_authenticate_user() + return self._async_form_auth_user() - async def async_step_auth_mfa(self, user_input=None, error=None): + async def async_step_auth_mfa( + self, user_input: dict[str, str] | None = None + ) -> ConfigFlowResult: """Authenticate mfa step.""" if user_input is not None: self._mfa_code = user_input[CONF_MFA_CODE] - return await self._async_authenticate_user(error) - return self._async_form_auth_mfa(error) + return await self._async_authenticate_user() + return self._async_form_auth_mfa() - async def async_step_auth_captcha(self, user_input=None, error=None): + async def async_step_auth_captcha( + self, user_input: dict[str, str] | None = None + ) -> ConfigFlowResult: """Captcha verification step.""" if user_input is not None: self._captcha_code = user_input[CONF_CAPTCHA_CODE] - return await self._async_authenticate_user(error) - return self._async_form_auth_captcha(error) + return await self._async_authenticate_user() + return self._async_form_auth_captcha() @callback - def _async_form_auth_app(self, error=None): + def _async_form_auth_app(self, error: str | None = None) -> ConfigFlowResult: """Authenticate application form.""" - errors = {} + errors: dict[str, str] = {} if error is not None: errors["base"] = error @@ -109,7 +120,7 @@ def _async_form_auth_app(self, error=None): ) @callback - def _async_form_auth_user(self, error=None): + def _async_form_auth_user(self, error: str | None = None) -> ConfigFlowResult: """Authenticate user form.""" errors = {} if error is not None: @@ -131,7 +142,7 @@ def _async_form_auth_user(self, error=None): ) @callback - def _async_form_auth_mfa(self, error=None): + def _async_form_auth_mfa(self, error: str | None = None) -> ConfigFlowResult: """Authenticate mfa form.""" errors = {} if error is not None: @@ -151,7 +162,7 @@ def _async_form_auth_mfa(self, error=None): ) @callback - def _async_form_auth_captcha(self, error=None): + def _async_form_auth_captcha(self, error: str | None = None) -> ConfigFlowResult: """Captcha verification form.""" errors = {} if error is not None: @@ -172,7 +183,9 @@ def _async_form_auth_captcha(self, error=None): }, ) - async def _async_authenticate_app(self, error=None): + async def _async_authenticate_app( + self, error: str | None = None + ) -> ConfigFlowResult: """Authenticate application.""" try: self._app_code = await self.hass.async_add_executor_job( @@ -186,7 +199,9 @@ async def _async_authenticate_app(self, error=None): _LOGGER.error("Error auth StarLine: %s", err) return self._async_form_auth_app(ERROR_AUTH_APP) - async def _async_authenticate_user(self, error=None): + async def _async_authenticate_user( + self, error: str | None = None + ) -> ConfigFlowResult: """Authenticate user.""" try: state, data = await self.hass.async_add_executor_job( @@ -219,7 +234,7 @@ async def _async_authenticate_user(self, error=None): _LOGGER.error("Error auth user: %s", err) return self._async_form_auth_user(ERROR_AUTH_USER) - async def _async_get_entry(self): + async def _async_get_entry(self) -> ConfigFlowResult: """Create entry.""" ( self._slnet_token, diff --git a/homeassistant/components/starline/icons.json b/homeassistant/components/starline/icons.json index b98c4178af10e6..8a4f85a89bf7c7 100644 --- a/homeassistant/components/starline/icons.json +++ b/homeassistant/components/starline/icons.json @@ -72,8 +72,14 @@ } }, "services": { - "update_state": "mdi:reload", - "set_scan_interval": "mdi:timer", - "set_scan_obd_interval": "mdi:timer" + "update_state": { + "service": "mdi:reload" + }, + "set_scan_interval": { + "service": "mdi:timer" + }, + "set_scan_obd_interval": { + "service": "mdi:timer" + } } } diff --git a/homeassistant/components/statistics/icons.json b/homeassistant/components/statistics/icons.json index a03163179cb5bd..a9829425570a0c 100644 --- a/homeassistant/components/statistics/icons.json +++ b/homeassistant/components/statistics/icons.json @@ -1,5 +1,7 @@ { "services": { - "reload": "mdi:reload" + "reload": { + "service": "mdi:reload" + } } } diff --git a/homeassistant/components/stream/manifest.json b/homeassistant/components/stream/manifest.json index 37158aa5fe309d..dffd6d65a6eb15 100644 --- a/homeassistant/components/stream/manifest.json +++ b/homeassistant/components/stream/manifest.json @@ -7,5 +7,5 @@ "integration_type": "system", "iot_class": "local_push", "quality_scale": "internal", - "requirements": ["PyTurboJPEG==1.7.1", "ha-av==10.1.1", "numpy==1.26.0"] + "requirements": ["PyTurboJPEG==1.7.5", "ha-av==10.1.1", "numpy==1.26.0"] } diff --git a/homeassistant/components/streamlabswater/icons.json b/homeassistant/components/streamlabswater/icons.json index aebe224b35ea10..0cc64fd24cb27d 100644 --- a/homeassistant/components/streamlabswater/icons.json +++ b/homeassistant/components/streamlabswater/icons.json @@ -1,5 +1,7 @@ { "services": { - "set_away_mode": "mdi:home" + "set_away_mode": { + "service": "mdi:home" + } } } diff --git a/homeassistant/components/stt/__init__.py b/homeassistant/components/stt/__init__.py index 676d8b8aa76668..f82d6c2ab93b04 100644 --- a/homeassistant/components/stt/__init__.py +++ b/homeassistant/components/stt/__init__.py @@ -72,9 +72,18 @@ @callback def async_default_engine(hass: HomeAssistant) -> str | None: """Return the domain or entity id of the default engine.""" - return async_default_provider(hass) or next( - iter(hass.states.async_entity_ids(DOMAIN)), None - ) + component: EntityComponent[SpeechToTextEntity] = hass.data[DOMAIN] + + default_entity_id: str | None = None + + for entity in component.entities: + if entity.platform and entity.platform.platform_name == "cloud": + return entity.entity_id + + if default_entity_id is None: + default_entity_id = entity.entity_id + + return default_entity_id or async_default_provider(hass) @callback @@ -439,6 +448,7 @@ def websocket_list_engines( for engine_id, provider in legacy_providers.items(): provider_info = { "engine_id": engine_id, + "name": provider.name, "supported_languages": provider.supported_languages, } if language: diff --git a/homeassistant/components/subaru/config_flow.py b/homeassistant/components/subaru/config_flow.py index 5ecaf9670d70c5..3d96a89a14f737 100644 --- a/homeassistant/components/subaru/config_flow.py +++ b/homeassistant/components/subaru/config_flow.py @@ -4,7 +4,7 @@ from datetime import datetime import logging -from typing import Any +from typing import TYPE_CHECKING, Any from subarulink import ( Controller as SubaruAPI, @@ -44,10 +44,10 @@ class SubaruConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - def __init__(self): + def __init__(self) -> None: """Initialize config flow.""" - self.config_data = {CONF_PIN: None} - self.controller = None + self.config_data: dict[str, Any] = {CONF_PIN: None} + self.controller: SubaruAPI | None = None async def async_step_user( self, user_input: dict[str, Any] | None = None @@ -66,6 +66,8 @@ async def async_step_user( _LOGGER.error("Unable to communicate with Subaru API: %s", ex.message) return self.async_abort(reason="cannot_connect") else: + if TYPE_CHECKING: + assert self.controller if not self.controller.device_registered: _LOGGER.debug("2FA validation is required") return await self.async_step_two_factor() @@ -137,6 +139,8 @@ async def async_step_two_factor( ) -> ConfigFlowResult: """Select contact method and request 2FA code from Subaru.""" error = None + if TYPE_CHECKING: + assert self.controller if user_input: # self.controller.contact_methods is a dict: # {"phone":"555-555-5555", "userName":"my@email.com"} @@ -165,6 +169,8 @@ async def async_step_two_factor_validate( ) -> ConfigFlowResult: """Validate received 2FA code with Subaru.""" error = None + if TYPE_CHECKING: + assert self.controller if user_input: try: vol.Match(r"^[0-9]{6}$")(user_input[CONF_VALIDATION_CODE]) @@ -190,6 +196,8 @@ async def async_step_pin( ) -> ConfigFlowResult: """Handle second part of config flow, if required.""" error = None + if TYPE_CHECKING: + assert self.controller if user_input and self.controller.update_saved_pin(user_input[CONF_PIN]): try: vol.Match(r"[0-9]{4}")(user_input[CONF_PIN]) diff --git a/homeassistant/components/subaru/icons.json b/homeassistant/components/subaru/icons.json index f6c3597c3c32d7..ca8648296c7c75 100644 --- a/homeassistant/components/subaru/icons.json +++ b/homeassistant/components/subaru/icons.json @@ -24,6 +24,8 @@ } }, "services": { - "unlock_specific_door": "mdi:lock-open-variant" + "unlock_specific_door": { + "service": "mdi:lock-open-variant" + } } } diff --git a/homeassistant/components/sun/config_flow.py b/homeassistant/components/sun/config_flow.py index 30b64c60b9f49a..16c465be8adb0f 100644 --- a/homeassistant/components/sun/config_flow.py +++ b/homeassistant/components/sun/config_flow.py @@ -23,6 +23,6 @@ async def async_step_user( return self.async_show_form(step_id="user") - async def async_step_import(self, user_input: dict[str, Any]) -> ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Handle import from configuration.yaml.""" - return await self.async_step_user(user_input) + return await self.async_step_user(import_data) diff --git a/homeassistant/components/surepetcare/icons.json b/homeassistant/components/surepetcare/icons.json index 1db15b599dfd5f..0daad594c48b7f 100644 --- a/homeassistant/components/surepetcare/icons.json +++ b/homeassistant/components/surepetcare/icons.json @@ -1,6 +1,10 @@ { "services": { - "set_lock_state": "mdi:lock", - "set_pet_location": "mdi:dog" + "set_lock_state": { + "service": "mdi:lock" + }, + "set_pet_location": { + "service": "mdi:dog" + } } } diff --git a/homeassistant/components/swiss_public_transport/icons.json b/homeassistant/components/swiss_public_transport/icons.json index 7c2e543683476a..0f868c18c1fac4 100644 --- a/homeassistant/components/swiss_public_transport/icons.json +++ b/homeassistant/components/swiss_public_transport/icons.json @@ -25,6 +25,8 @@ } }, "services": { - "fetch_connections": "mdi:bus-clock" + "fetch_connections": { + "service": "mdi:bus-clock" + } } } diff --git a/homeassistant/components/switch/icons.json b/homeassistant/components/switch/icons.json index fbc1af5a126ba4..10299a2ffc8e94 100644 --- a/homeassistant/components/switch/icons.json +++ b/homeassistant/components/switch/icons.json @@ -20,8 +20,14 @@ } }, "services": { - "toggle": "mdi:toggle-switch-variant", - "turn_off": "mdi:toggle-switch-variant-off", - "turn_on": "mdi:toggle-switch-variant" + "toggle": { + "service": "mdi:toggle-switch-variant" + }, + "turn_off": { + "service": "mdi:toggle-switch-variant-off" + }, + "turn_on": { + "service": "mdi:toggle-switch-variant" + } } } diff --git a/homeassistant/components/switchbot/config_flow.py b/homeassistant/components/switchbot/config_flow.py index a1c947fd61186b..0468db5618adf7 100644 --- a/homeassistant/components/switchbot/config_flow.py +++ b/homeassistant/components/switchbot/config_flow.py @@ -38,13 +38,16 @@ from .const import ( CONF_ENCRYPTION_KEY, CONF_KEY_ID, + CONF_LOCK_NIGHTLATCH, CONF_RETRY_COUNT, CONNECTABLE_SUPPORTED_MODEL_TYPES, + DEFAULT_LOCK_NIGHTLATCH, DEFAULT_RETRY_COUNT, DOMAIN, NON_CONNECTABLE_SUPPORTED_MODEL_TYPES, SUPPORTED_LOCK_MODELS, SUPPORTED_MODEL_TYPES, + SupportedModels, ) _LOGGER = logging.getLogger(__name__) @@ -355,7 +358,7 @@ async def async_step_init( # Update common entity options for all other entities. return self.async_create_entry(title="", data=user_input) - options = { + options: dict[vol.Optional, Any] = { vol.Optional( CONF_RETRY_COUNT, default=self.config_entry.options.get( @@ -363,5 +366,16 @@ async def async_step_init( ), ): int } + if self.config_entry.data.get(CONF_SENSOR_TYPE) == SupportedModels.LOCK_PRO: + options.update( + { + vol.Optional( + CONF_LOCK_NIGHTLATCH, + default=self.config_entry.options.get( + CONF_LOCK_NIGHTLATCH, DEFAULT_LOCK_NIGHTLATCH + ), + ): bool + } + ) return self.async_show_form(step_id="init", data_schema=vol.Schema(options)) diff --git a/homeassistant/components/switchbot/const.py b/homeassistant/components/switchbot/const.py index 0a1ac01e5302e8..bd727edfea400c 100644 --- a/homeassistant/components/switchbot/const.py +++ b/homeassistant/components/switchbot/const.py @@ -64,11 +64,13 @@ class SupportedModels(StrEnum): # Config Defaults DEFAULT_RETRY_COUNT = 3 +DEFAULT_LOCK_NIGHTLATCH = False # Config Options CONF_RETRY_COUNT = "retry_count" CONF_KEY_ID = "key_id" CONF_ENCRYPTION_KEY = "encryption_key" +CONF_LOCK_NIGHTLATCH = "lock_force_nightlatch" # Deprecated config Entry Options to be removed in 2023.4 CONF_TIME_BETWEEN_UPDATE_COMMAND = "update_time" diff --git a/homeassistant/components/switchbot/lock.py b/homeassistant/components/switchbot/lock.py index cb41d14cf667f2..a3bee5661b2692 100644 --- a/homeassistant/components/switchbot/lock.py +++ b/homeassistant/components/switchbot/lock.py @@ -9,6 +9,7 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback +from .const import CONF_LOCK_NIGHTLATCH, DEFAULT_LOCK_NIGHTLATCH from .coordinator import SwitchbotConfigEntry, SwitchbotDataUpdateCoordinator from .entity import SwitchbotEntity @@ -19,7 +20,8 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """Set up Switchbot lock based on a config entry.""" - async_add_entities([(SwitchBotLock(entry.runtime_data))]) + force_nightlatch = entry.options.get(CONF_LOCK_NIGHTLATCH, DEFAULT_LOCK_NIGHTLATCH) + async_add_entities([SwitchBotLock(entry.runtime_data, force_nightlatch)]) # noinspection PyAbstractClass @@ -30,11 +32,13 @@ class SwitchBotLock(SwitchbotEntity, LockEntity): _attr_name = None _device: switchbot.SwitchbotLock - def __init__(self, coordinator: SwitchbotDataUpdateCoordinator) -> None: + def __init__( + self, coordinator: SwitchbotDataUpdateCoordinator, force_nightlatch + ) -> None: """Initialize the entity.""" super().__init__(coordinator) self._async_update_attrs() - if self._device.is_night_latch_enabled(): + if self._device.is_night_latch_enabled() or force_nightlatch: self._attr_supported_features = LockEntityFeature.OPEN def _async_update_attrs(self) -> None: @@ -55,7 +59,7 @@ async def async_lock(self, **kwargs: Any) -> None: async def async_unlock(self, **kwargs: Any) -> None: """Unlock the lock.""" - if self._device.is_night_latch_enabled(): + if self._attr_supported_features & (LockEntityFeature.OPEN): self._last_run_success = await self._device.unlock_without_unlatch() else: self._last_run_success = await self._device.unlock() diff --git a/homeassistant/components/switchbot/manifest.json b/homeassistant/components/switchbot/manifest.json index 0cbbd70a805c48..f97162184c68ce 100644 --- a/homeassistant/components/switchbot/manifest.json +++ b/homeassistant/components/switchbot/manifest.json @@ -39,5 +39,5 @@ "documentation": "https://www.home-assistant.io/integrations/switchbot", "iot_class": "local_push", "loggers": ["switchbot"], - "requirements": ["PySwitchbot==0.48.1"] + "requirements": ["PySwitchbot==0.48.2"] } diff --git a/homeassistant/components/switchbot/strings.json b/homeassistant/components/switchbot/strings.json index a20b4939f8f67a..80ca32d48266c5 100644 --- a/homeassistant/components/switchbot/strings.json +++ b/homeassistant/components/switchbot/strings.json @@ -54,7 +54,8 @@ "step": { "init": { "data": { - "retry_count": "Retry count" + "retry_count": "Retry count", + "lock_force_nightlatch": "Force Nightlatch operation mode" } } } diff --git a/homeassistant/components/switcher_kis/icons.json b/homeassistant/components/switcher_kis/icons.json index 4d3576f1a99841..6ca8e0e83516f6 100644 --- a/homeassistant/components/switcher_kis/icons.json +++ b/homeassistant/components/switcher_kis/icons.json @@ -24,7 +24,11 @@ } }, "services": { - "set_auto_off": "mdi:progress-clock", - "turn_on_with_timer": "mdi:timer" + "set_auto_off": { + "service": "mdi:progress-clock" + }, + "turn_on_with_timer": { + "service": "mdi:timer" + } } } diff --git a/homeassistant/components/syncthing/config_flow.py b/homeassistant/components/syncthing/config_flow.py index 2d7d2ddcc92aa8..86ea52c43a3be6 100644 --- a/homeassistant/components/syncthing/config_flow.py +++ b/homeassistant/components/syncthing/config_flow.py @@ -1,9 +1,11 @@ """Config flow for syncthing integration.""" +from typing import Any + import aiosyncthing import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_TOKEN, CONF_URL, CONF_VERIFY_SSL from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError @@ -42,7 +44,9 @@ class SyncThingConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" errors = {} diff --git a/homeassistant/components/syncthru/config_flow.py b/homeassistant/components/syncthru/config_flow.py index 8cd1c2c7b3b149..1fb155a5648537 100644 --- a/homeassistant/components/syncthru/config_flow.py +++ b/homeassistant/components/syncthru/config_flow.py @@ -1,6 +1,7 @@ """Config flow for Samsung SyncThru.""" import re +from typing import Any from urllib.parse import urlparse from pysyncthru import ConnectionMode, SyncThru, SyncThruAPINotSupported @@ -23,7 +24,9 @@ class SyncThruConfigFlow(ConfigFlow, domain=DOMAIN): url: str name: str - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle user initiated flow.""" if user_input is None: return await self._async_show_form(step_id="user") @@ -61,7 +64,9 @@ async def async_step_ssdp( self.context["title_placeholders"] = {CONF_NAME: self.name} return await self.async_step_confirm() - async def async_step_confirm(self, user_input=None): + async def async_step_confirm( + self, user_input: dict[str, str] | None = None + ) -> ConfigFlowResult: """Handle discovery confirmation by user.""" if user_input is not None: return await self._async_check_and_create("confirm", user_input) diff --git a/homeassistant/components/synology_dsm/icons.json b/homeassistant/components/synology_dsm/icons.json index 8e6d2b17f02b14..3c4d028dc7ad36 100644 --- a/homeassistant/components/synology_dsm/icons.json +++ b/homeassistant/components/synology_dsm/icons.json @@ -78,7 +78,11 @@ } }, "services": { - "reboot": "mdi:restart", - "shutdown": "mdi:power" + "reboot": { + "service": "mdi:restart" + }, + "shutdown": { + "service": "mdi:power" + } } } diff --git a/homeassistant/components/system_bridge/icons.json b/homeassistant/components/system_bridge/icons.json index cc648889f0b9cb..a03f77049a315d 100644 --- a/homeassistant/components/system_bridge/icons.json +++ b/homeassistant/components/system_bridge/icons.json @@ -1,11 +1,25 @@ { "services": { - "get_process_by_id": "mdi:console", - "get_processes_by_name": "mdi:console", - "open_path": "mdi:folder-open", - "open_url": "mdi:web", - "send_keypress": "mdi:keyboard", - "send_text": "mdi:keyboard", - "power_command": "mdi:power" + "get_process_by_id": { + "service": "mdi:console" + }, + "get_processes_by_name": { + "service": "mdi:console" + }, + "open_path": { + "service": "mdi:folder-open" + }, + "open_url": { + "service": "mdi:web" + }, + "send_keypress": { + "service": "mdi:keyboard" + }, + "send_text": { + "service": "mdi:keyboard" + }, + "power_command": { + "service": "mdi:power" + } } } diff --git a/homeassistant/components/system_log/icons.json b/homeassistant/components/system_log/icons.json index 436a6c348085a9..fe269c5154dd1e 100644 --- a/homeassistant/components/system_log/icons.json +++ b/homeassistant/components/system_log/icons.json @@ -1,6 +1,10 @@ { "services": { - "clear": "mdi:delete", - "write": "mdi:pencil" + "clear": { + "service": "mdi:delete" + }, + "write": { + "service": "mdi:pencil" + } } } diff --git a/homeassistant/components/tado/climate.py b/homeassistant/components/tado/climate.py index 314a2315d0ae49..60096c253010b0 100644 --- a/homeassistant/components/tado/climate.py +++ b/homeassistant/components/tado/climate.py @@ -16,6 +16,7 @@ SWING_BOTH, SWING_HORIZONTAL, SWING_OFF, + SWING_ON, SWING_VERTICAL, ClimateEntity, ClimateEntityFeature, @@ -47,7 +48,6 @@ HA_TO_TADO_FAN_MODE_MAP, HA_TO_TADO_FAN_MODE_MAP_LEGACY, HA_TO_TADO_HVAC_MODE_MAP, - HA_TO_TADO_SWING_MODE_MAP, ORDERED_KNOWN_TADO_MODES, PRESET_AUTO, SIGNAL_TADO_UPDATE_RECEIVED, @@ -55,17 +55,20 @@ SUPPORT_PRESET_MANUAL, TADO_DEFAULT_MAX_TEMP, TADO_DEFAULT_MIN_TEMP, - TADO_FAN_LEVELS, - TADO_FAN_SPEEDS, + TADO_FANLEVEL_SETTING, + TADO_FANSPEED_SETTING, + TADO_HORIZONTAL_SWING_SETTING, TADO_HVAC_ACTION_TO_HA_HVAC_ACTION, TADO_MODES_WITH_NO_TEMP_SETTING, TADO_SWING_OFF, TADO_SWING_ON, + TADO_SWING_SETTING, TADO_TO_HA_FAN_MODE_MAP, TADO_TO_HA_FAN_MODE_MAP_LEGACY, TADO_TO_HA_HVAC_MODE_MAP, TADO_TO_HA_OFFSET_MAP, TADO_TO_HA_SWING_MODE_MAP, + TADO_VERTICAL_SWING_SETTING, TEMP_OFFSET, TYPE_AIR_CONDITIONING, TYPE_HEATING, @@ -166,29 +169,30 @@ def create_climate_entity( supported_hvac_modes.append(TADO_TO_HA_HVAC_MODE_MAP[mode]) if ( - capabilities[mode].get("swings") - or capabilities[mode].get("verticalSwing") - or capabilities[mode].get("horizontalSwing") + TADO_SWING_SETTING in capabilities[mode] + or TADO_VERTICAL_SWING_SETTING in capabilities[mode] + or TADO_VERTICAL_SWING_SETTING in capabilities[mode] ): support_flags |= ClimateEntityFeature.SWING_MODE supported_swing_modes = [] - if capabilities[mode].get("swings"): + if TADO_SWING_SETTING in capabilities[mode]: supported_swing_modes.append( TADO_TO_HA_SWING_MODE_MAP[TADO_SWING_ON] ) - if capabilities[mode].get("verticalSwing"): + if TADO_VERTICAL_SWING_SETTING in capabilities[mode]: supported_swing_modes.append(SWING_VERTICAL) - if capabilities[mode].get("horizontalSwing"): + if TADO_HORIZONTAL_SWING_SETTING in capabilities[mode]: supported_swing_modes.append(SWING_HORIZONTAL) if ( SWING_HORIZONTAL in supported_swing_modes - and SWING_HORIZONTAL in supported_swing_modes + and SWING_VERTICAL in supported_swing_modes ): supported_swing_modes.append(SWING_BOTH) supported_swing_modes.append(TADO_TO_HA_SWING_MODE_MAP[TADO_SWING_OFF]) - if not capabilities[mode].get("fanSpeeds") and not capabilities[mode].get( - "fanLevel" + if ( + TADO_FANSPEED_SETTING not in capabilities[mode] + and TADO_FANLEVEL_SETTING not in capabilities[mode] ): continue @@ -197,14 +201,15 @@ def create_climate_entity( if supported_fan_modes: continue - if capabilities[mode].get("fanSpeeds"): + if TADO_FANSPEED_SETTING in capabilities[mode]: supported_fan_modes = generate_supported_fanmodes( - TADO_TO_HA_FAN_MODE_MAP_LEGACY, capabilities[mode]["fanSpeeds"] + TADO_TO_HA_FAN_MODE_MAP_LEGACY, + capabilities[mode][TADO_FANSPEED_SETTING], ) else: supported_fan_modes = generate_supported_fanmodes( - TADO_TO_HA_FAN_MODE_MAP, capabilities[mode]["fanLevel"] + TADO_TO_HA_FAN_MODE_MAP, capabilities[mode][TADO_FANLEVEL_SETTING] ) cool_temperatures = capabilities[CONST_MODE_COOL]["temperatures"] @@ -316,12 +321,16 @@ def __init__( self._target_temp: float | None = None self._current_tado_fan_speed = CONST_FAN_OFF + self._current_tado_fan_level = CONST_FAN_OFF self._current_tado_hvac_mode = CONST_MODE_OFF self._current_tado_hvac_action = HVACAction.OFF self._current_tado_swing_mode = TADO_SWING_OFF self._current_tado_vertical_swing = TADO_SWING_OFF self._current_tado_horizontal_swing = TADO_SWING_OFF + capabilities = tado.get_capabilities(zone_id) + self._current_tado_capabilities = capabilities + self._tado_zone_data: PyTado.TadoZone = {} self._tado_geofence_data: dict[str, str] | None = None @@ -382,20 +391,23 @@ def hvac_action(self) -> HVACAction: def fan_mode(self) -> str | None: """Return the fan setting.""" if self._ac_device: - return TADO_TO_HA_FAN_MODE_MAP.get( - self._current_tado_fan_speed, - TADO_TO_HA_FAN_MODE_MAP_LEGACY.get( + if self._is_valid_setting_for_hvac_mode(TADO_FANSPEED_SETTING): + return TADO_TO_HA_FAN_MODE_MAP_LEGACY.get( self._current_tado_fan_speed, FAN_AUTO - ), - ) + ) + if self._is_valid_setting_for_hvac_mode(TADO_FANLEVEL_SETTING): + return TADO_TO_HA_FAN_MODE_MAP.get( + self._current_tado_fan_level, FAN_AUTO + ) + return FAN_AUTO return None def set_fan_mode(self, fan_mode: str) -> None: """Turn fan on/off.""" - if self._current_tado_fan_speed in TADO_FAN_LEVELS: - self._control_hvac(fan_mode=HA_TO_TADO_FAN_MODE_MAP[fan_mode]) - else: + if self._is_valid_setting_for_hvac_mode(TADO_FANSPEED_SETTING): self._control_hvac(fan_mode=HA_TO_TADO_FAN_MODE_MAP_LEGACY[fan_mode]) + elif self._is_valid_setting_for_hvac_mode(TADO_FANLEVEL_SETTING): + self._control_hvac(fan_mode=HA_TO_TADO_FAN_MODE_MAP[fan_mode]) @property def preset_mode(self) -> str: @@ -555,24 +567,30 @@ def set_swing_mode(self, swing_mode: str) -> None: swing = None if self._attr_swing_modes is None: return - if ( - SWING_VERTICAL in self._attr_swing_modes - or SWING_HORIZONTAL in self._attr_swing_modes - ): - if swing_mode == SWING_VERTICAL: + if swing_mode == SWING_OFF: + if self._is_valid_setting_for_hvac_mode(TADO_SWING_SETTING): + swing = TADO_SWING_OFF + if self._is_valid_setting_for_hvac_mode(TADO_HORIZONTAL_SWING_SETTING): + horizontal_swing = TADO_SWING_OFF + if self._is_valid_setting_for_hvac_mode(TADO_VERTICAL_SWING_SETTING): + vertical_swing = TADO_SWING_OFF + if swing_mode == SWING_ON: + swing = TADO_SWING_ON + if swing_mode == SWING_VERTICAL: + if self._is_valid_setting_for_hvac_mode(TADO_VERTICAL_SWING_SETTING): vertical_swing = TADO_SWING_ON - elif swing_mode == SWING_HORIZONTAL: + if self._is_valid_setting_for_hvac_mode(TADO_HORIZONTAL_SWING_SETTING): + horizontal_swing = TADO_SWING_OFF + if swing_mode == SWING_HORIZONTAL: + if self._is_valid_setting_for_hvac_mode(TADO_VERTICAL_SWING_SETTING): + vertical_swing = TADO_SWING_OFF + if self._is_valid_setting_for_hvac_mode(TADO_HORIZONTAL_SWING_SETTING): horizontal_swing = TADO_SWING_ON - elif swing_mode == SWING_BOTH: + if swing_mode == SWING_BOTH: + if self._is_valid_setting_for_hvac_mode(TADO_VERTICAL_SWING_SETTING): vertical_swing = TADO_SWING_ON + if self._is_valid_setting_for_hvac_mode(TADO_HORIZONTAL_SWING_SETTING): horizontal_swing = TADO_SWING_ON - elif swing_mode == SWING_OFF: - if SWING_VERTICAL in self._attr_swing_modes: - vertical_swing = TADO_SWING_OFF - if SWING_HORIZONTAL in self._attr_swing_modes: - horizontal_swing = TADO_SWING_OFF - else: - swing = HA_TO_TADO_SWING_MODE_MAP[swing_mode] self._control_hvac( swing_mode=swing, @@ -596,21 +614,23 @@ def _async_update_zone_data(self) -> None: self._device_id ][TEMP_OFFSET][offset_key] - self._current_tado_fan_speed = ( - self._tado_zone_data.current_fan_level - if self._tado_zone_data.current_fan_level is not None - else self._tado_zone_data.current_fan_speed - ) - self._current_tado_hvac_mode = self._tado_zone_data.current_hvac_mode self._current_tado_hvac_action = self._tado_zone_data.current_hvac_action - self._current_tado_swing_mode = self._tado_zone_data.current_swing_mode - self._current_tado_vertical_swing = ( - self._tado_zone_data.current_vertical_swing_mode - ) - self._current_tado_horizontal_swing = ( - self._tado_zone_data.current_horizontal_swing_mode - ) + + if self._is_valid_setting_for_hvac_mode(TADO_FANLEVEL_SETTING): + self._current_tado_fan_level = self._tado_zone_data.current_fan_level + if self._is_valid_setting_for_hvac_mode(TADO_FANSPEED_SETTING): + self._current_tado_fan_speed = self._tado_zone_data.current_fan_speed + if self._is_valid_setting_for_hvac_mode(TADO_SWING_SETTING): + self._current_tado_swing_mode = self._tado_zone_data.current_swing_mode + if self._is_valid_setting_for_hvac_mode(TADO_VERTICAL_SWING_SETTING): + self._current_tado_vertical_swing = ( + self._tado_zone_data.current_vertical_swing_mode + ) + if self._is_valid_setting_for_hvac_mode(TADO_HORIZONTAL_SWING_SETTING): + self._current_tado_horizontal_swing = ( + self._tado_zone_data.current_horizontal_swing_mode + ) @callback def _async_update_zone_callback(self) -> None: @@ -665,7 +685,10 @@ def _control_hvac( self._target_temp = target_temp if fan_mode: - self._current_tado_fan_speed = fan_mode + if self._is_valid_setting_for_hvac_mode(TADO_FANSPEED_SETTING): + self._current_tado_fan_speed = fan_mode + if self._is_valid_setting_for_hvac_mode(TADO_FANLEVEL_SETTING): + self._current_tado_fan_level = fan_mode if swing_mode: self._current_tado_swing_mode = swing_mode @@ -735,21 +758,32 @@ def _control_hvac( fan_speed = None fan_level = None if self.supported_features & ClimateEntityFeature.FAN_MODE: - if self._current_tado_fan_speed in TADO_FAN_LEVELS: - fan_level = self._current_tado_fan_speed - elif self._current_tado_fan_speed in TADO_FAN_SPEEDS: + if self._is_current_setting_supported_by_current_hvac_mode( + TADO_FANSPEED_SETTING, self._current_tado_fan_speed + ): fan_speed = self._current_tado_fan_speed + if self._is_current_setting_supported_by_current_hvac_mode( + TADO_FANLEVEL_SETTING, self._current_tado_fan_level + ): + fan_level = self._current_tado_fan_level + swing = None vertical_swing = None horizontal_swing = None if ( self.supported_features & ClimateEntityFeature.SWING_MODE ) and self._attr_swing_modes is not None: - if SWING_VERTICAL in self._attr_swing_modes: + if self._is_current_setting_supported_by_current_hvac_mode( + TADO_VERTICAL_SWING_SETTING, self._current_tado_vertical_swing + ): vertical_swing = self._current_tado_vertical_swing - if SWING_HORIZONTAL in self._attr_swing_modes: + if self._is_current_setting_supported_by_current_hvac_mode( + TADO_HORIZONTAL_SWING_SETTING, self._current_tado_horizontal_swing + ): horizontal_swing = self._current_tado_horizontal_swing - if vertical_swing is None and horizontal_swing is None: + if self._is_current_setting_supported_by_current_hvac_mode( + TADO_SWING_SETTING, self._current_tado_swing_mode + ): swing = self._current_tado_swing_mode self._tado.set_zone_overlay( @@ -765,3 +799,20 @@ def _control_hvac( vertical_swing=vertical_swing, # api defaults to not sending verticalSwing if swing not None horizontal_swing=horizontal_swing, # api defaults to not sending horizontalSwing if swing not None ) + + def _is_valid_setting_for_hvac_mode(self, setting: str) -> bool: + return ( + self._current_tado_capabilities.get(self._current_tado_hvac_mode, {}).get( + setting + ) + is not None + ) + + def _is_current_setting_supported_by_current_hvac_mode( + self, setting: str, current_state: str | None + ) -> bool: + if self._is_valid_setting_for_hvac_mode(setting): + return current_state in self._current_tado_capabilities[ + self._current_tado_hvac_mode + ].get(setting, []) + return False diff --git a/homeassistant/components/tado/const.py b/homeassistant/components/tado/const.py index 5c6a80c5bebcea..8033a653325b3c 100644 --- a/homeassistant/components/tado/const.py +++ b/homeassistant/components/tado/const.py @@ -234,3 +234,10 @@ ATTR_MESSAGE = "message" WATER_HEATER_FALLBACK_REPAIR = "water_heater_fallback" + +TADO_SWING_SETTING = "swings" +TADO_FANSPEED_SETTING = "fanSpeeds" + +TADO_FANLEVEL_SETTING = "fanLevel" +TADO_VERTICAL_SWING_SETTING = "verticalSwing" +TADO_HORIZONTAL_SWING_SETTING = "horizontalSwing" diff --git a/homeassistant/components/tado/icons.json b/homeassistant/components/tado/icons.json index 83ef6d4b332266..c799bef0260acb 100644 --- a/homeassistant/components/tado/icons.json +++ b/homeassistant/components/tado/icons.json @@ -1,8 +1,16 @@ { "services": { - "set_climate_timer": "mdi:timer", - "set_water_heater_timer": "mdi:timer", - "set_climate_temperature_offset": "mdi:thermometer", - "add_meter_reading": "mdi:counter" + "set_climate_timer": { + "service": "mdi:timer" + }, + "set_water_heater_timer": { + "service": "mdi:timer" + }, + "set_climate_temperature_offset": { + "service": "mdi:thermometer" + }, + "add_meter_reading": { + "service": "mdi:counter" + } } } diff --git a/homeassistant/components/tailwind/config_flow.py b/homeassistant/components/tailwind/config_flow.py index 1cb946252664b3..13682a3e9c40a9 100644 --- a/homeassistant/components/tailwind/config_flow.py +++ b/homeassistant/components/tailwind/config_flow.py @@ -144,7 +144,9 @@ async def async_step_zeroconf_confirm( errors=errors, ) - async def async_step_reauth(self, _: Mapping[str, Any]) -> ConfigFlowResult: + async def async_step_reauth( + self, entry_data: Mapping[str, Any] + ) -> ConfigFlowResult: """Handle initiation of re-authentication with a Tailwind device.""" self.reauth_entry = self.hass.config_entries.async_get_entry( self.context["entry_id"] diff --git a/homeassistant/components/tami4/button.py b/homeassistant/components/tami4/button.py index 2d8af3fcf89b0f..11377a2dcfbe32 100644 --- a/homeassistant/components/tami4/button.py +++ b/homeassistant/components/tami4/button.py @@ -5,10 +5,12 @@ import logging from Tami4EdgeAPI import Tami4EdgeAPI +from Tami4EdgeAPI.drink import Drink from homeassistant.components.button import ButtonEntity, ButtonEntityDescription from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity import EntityDescription from homeassistant.helpers.entity_platform import AddEntitiesCallback from .const import API, DOMAIN @@ -24,12 +26,17 @@ class Tami4EdgeButtonEntityDescription(ButtonEntityDescription): press_fn: Callable[[Tami4EdgeAPI], None] -BUTTONS: tuple[Tami4EdgeButtonEntityDescription] = ( - Tami4EdgeButtonEntityDescription( - key="boil_water", - translation_key="boil_water", - press_fn=lambda api: api.boil_water(), - ), +@dataclass(frozen=True, kw_only=True) +class Tami4EdgeDrinkButtonEntityDescription(ButtonEntityDescription): + """A class that describes Tami4Edge Drink button entities.""" + + press_fn: Callable[[Tami4EdgeAPI, Drink], None] + + +BOIL_WATER_BUTTON = Tami4EdgeButtonEntityDescription( + key="boil_water", + translation_key="boil_water", + press_fn=lambda api: api.boil_water(), ) @@ -37,12 +44,29 @@ async def async_setup_entry( hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback ) -> None: """Perform the setup for Tami4Edge.""" - api: Tami4EdgeAPI = hass.data[DOMAIN][entry.entry_id][API] - async_add_entities( - Tami4EdgeButton(api, entity_description) for entity_description in BUTTONS + api: Tami4EdgeAPI = hass.data[DOMAIN][entry.entry_id][API] + buttons: list[Tami4EdgeBaseEntity] = [Tami4EdgeButton(api, BOIL_WATER_BUTTON)] + + device = await hass.async_add_executor_job(api.get_device) + drinks = device.drinks + + buttons.extend( + Tami4EdgeDrinkButton( + api=api, + entity_description=Tami4EdgeDrinkButtonEntityDescription( + key=drink.id, + translation_key="prepare_drink", + translation_placeholders={"drink_name": drink.name}, + press_fn=lambda api, drink: api.prepare_drink(drink), + ), + drink=drink, + ) + for drink in drinks ) + async_add_entities(buttons) + class Tami4EdgeButton(Tami4EdgeBaseEntity, ButtonEntity): """Button entity for Tami4Edge.""" @@ -52,3 +76,20 @@ class Tami4EdgeButton(Tami4EdgeBaseEntity, ButtonEntity): def press(self) -> None: """Handle the button press.""" self.entity_description.press_fn(self._api) + + +class Tami4EdgeDrinkButton(Tami4EdgeBaseEntity, ButtonEntity): + """Drink Button entity for Tami4Edge.""" + + entity_description: Tami4EdgeDrinkButtonEntityDescription + + def __init__( + self, api: Tami4EdgeAPI, entity_description: EntityDescription, drink: Drink + ) -> None: + """Initialize the drink button.""" + super().__init__(api=api, entity_description=entity_description) + self.drink = drink + + def press(self) -> None: + """Handle the button press.""" + self.entity_description.press_fn(self._api, self.drink) diff --git a/homeassistant/components/tami4/icons.json b/homeassistant/components/tami4/icons.json index d623bdc60071c1..803ed9a501631d 100644 --- a/homeassistant/components/tami4/icons.json +++ b/homeassistant/components/tami4/icons.json @@ -3,6 +3,9 @@ "button": { "boil_water": { "default": "mdi:kettle-steam" + }, + "prepare_drink": { + "default": "mdi:beer" } }, "sensor": { diff --git a/homeassistant/components/tami4/strings.json b/homeassistant/components/tami4/strings.json index 406964a3bffd95..9c33b6607e47f5 100644 --- a/homeassistant/components/tami4/strings.json +++ b/homeassistant/components/tami4/strings.json @@ -26,6 +26,9 @@ "button": { "boil_water": { "name": "Boil water" + }, + "prepare_drink": { + "name": "Prepare {drink_name}" } } }, diff --git a/homeassistant/components/technove/binary_sensor.py b/homeassistant/components/technove/binary_sensor.py index a1ff9d16baf0b8..1ecefe6f85c6db 100644 --- a/homeassistant/components/technove/binary_sensor.py +++ b/homeassistant/components/technove/binary_sensor.py @@ -4,19 +4,28 @@ from collections.abc import Callable from dataclasses import dataclass +from typing import TYPE_CHECKING from technove import Station as TechnoVEStation from homeassistant.components.binary_sensor import ( + DOMAIN as BINARY_SENSOR_DOMAIN, BinarySensorDeviceClass, BinarySensorEntity, BinarySensorEntityDescription, ) from homeassistant.const import EntityCategory from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.issue_registry import ( + IssueSeverity, + async_create_issue, + async_delete_issue, +) from . import TechnoVEConfigEntry +from .const import DOMAIN from .coordinator import TechnoVEDataUpdateCoordinator from .entity import TechnoVEEntity @@ -25,6 +34,7 @@ class TechnoVEBinarySensorDescription(BinarySensorEntityDescription): """Describes TechnoVE binary sensor entity.""" + deprecated_version: str | None = None value_fn: Callable[[TechnoVEStation], bool | None] @@ -52,6 +62,9 @@ class TechnoVEBinarySensorDescription(BinarySensorEntityDescription): entity_category=EntityCategory.DIAGNOSTIC, device_class=BinarySensorDeviceClass.BATTERY_CHARGING, value_fn=lambda station: station.info.is_session_active, + deprecated_version="2025.2.0", + # Disabled by default, as this entity is deprecated + entity_registry_enabled_default=False, ), TechnoVEBinarySensorDescription( key="is_static_ip", @@ -100,3 +113,34 @@ def is_on(self) -> bool | None: """Return the state of the sensor.""" return self.entity_description.value_fn(self.coordinator.data) + + async def async_added_to_hass(self) -> None: + """Raise issue when entity is registered and was not disabled.""" + if TYPE_CHECKING: + assert self.unique_id + if entity_id := er.async_get(self.hass).async_get_entity_id( + BINARY_SENSOR_DOMAIN, DOMAIN, self.unique_id + ): + if self.enabled and self.entity_description.deprecated_version: + async_create_issue( + self.hass, + DOMAIN, + f"deprecated_entity_{self.entity_description.key}", + breaks_in_ha_version=self.entity_description.deprecated_version, + is_fixable=False, + severity=IssueSeverity.WARNING, + translation_key=f"deprecated_entity_{self.entity_description.key}", + translation_placeholders={ + "sensor_name": self.name + if isinstance(self.name, str) + else entity_id, + "entity": entity_id, + }, + ) + else: + async_delete_issue( + self.hass, + DOMAIN, + f"deprecated_entity_{self.entity_description.key}", + ) + await super().async_added_to_hass() diff --git a/homeassistant/components/technove/icons.json b/homeassistant/components/technove/icons.json index ff47d3c32bcac1..52307405ff7c5b 100644 --- a/homeassistant/components/technove/icons.json +++ b/homeassistant/components/technove/icons.json @@ -4,6 +4,11 @@ "ssid": { "default": "mdi:wifi" } + }, + "switch": { + "session_active": { + "default": "mdi:ev-station" + } } } } diff --git a/homeassistant/components/technove/strings.json b/homeassistant/components/technove/strings.json index 8799909d95c218..06c93939db899c 100644 --- a/homeassistant/components/technove/strings.json +++ b/homeassistant/components/technove/strings.json @@ -77,12 +77,24 @@ "switch": { "auto_charge": { "name": "Auto charge" + }, + "session_active": { + "name": "Charging Enabled" } } }, "exceptions": { "max_current_in_sharing_mode": { "message": "Cannot set the max current when power sharing mode is enabled." + }, + "set_charging_enabled_on_auto_charge": { + "message": "Cannot enable or disable charging when auto-charge is enabled. Try disabling auto-charge first." + } + }, + "issues": { + "deprecated_entity_is_session_active": { + "title": "The TechnoVE `{sensor_name}` binary sensor is deprecated", + "description": "`{entity}` is deprecated.\nPlease update your automations and scripts to replace the binary sensor entity with the newly added switch entity.\nWhen you are done migrating you can disable `{entity}`." } } } diff --git a/homeassistant/components/technove/switch.py b/homeassistant/components/technove/switch.py index bb9250215be646..a8ad7581da51e1 100644 --- a/homeassistant/components/technove/switch.py +++ b/homeassistant/components/technove/switch.py @@ -2,30 +2,59 @@ from __future__ import annotations -from collections.abc import Awaitable, Callable +from collections.abc import Callable, Coroutine from dataclasses import dataclass from typing import Any -from technove import Station as TechnoVEStation, TechnoVE +from technove import Station as TechnoVEStation from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription from homeassistant.const import EntityCategory from homeassistant.core import HomeAssistant +from homeassistant.exceptions import ServiceValidationError from homeassistant.helpers.entity_platform import AddEntitiesCallback from . import TechnoVEConfigEntry +from .const import DOMAIN from .coordinator import TechnoVEDataUpdateCoordinator from .entity import TechnoVEEntity from .helpers import technove_exception_handler +async def _set_charging_enabled( + coordinator: TechnoVEDataUpdateCoordinator, enabled: bool +) -> None: + if coordinator.data.info.auto_charge: + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="set_charging_enabled_on_auto_charge", + ) + await coordinator.technove.set_charging_enabled(enabled=enabled) + coordinator.data.info.is_session_active = enabled + coordinator.async_set_updated_data(coordinator.data) + + +async def _enable_charging(coordinator: TechnoVEDataUpdateCoordinator) -> None: + await _set_charging_enabled(coordinator, True) + + +async def _disable_charging(coordinator: TechnoVEDataUpdateCoordinator) -> None: + await _set_charging_enabled(coordinator, False) + + +async def _set_auto_charge( + coordinator: TechnoVEDataUpdateCoordinator, enabled: bool +) -> None: + await coordinator.technove.set_auto_charge(enabled=enabled) + + @dataclass(frozen=True, kw_only=True) class TechnoVESwitchDescription(SwitchEntityDescription): """Describes TechnoVE binary sensor entity.""" is_on_fn: Callable[[TechnoVEStation], bool] - turn_on_fn: Callable[[TechnoVE], Awaitable[dict[str, Any]]] - turn_off_fn: Callable[[TechnoVE], Awaitable[dict[str, Any]]] + turn_on_fn: Callable[[TechnoVEDataUpdateCoordinator], Coroutine[Any, Any, None]] + turn_off_fn: Callable[[TechnoVEDataUpdateCoordinator], Coroutine[Any, Any, None]] SWITCHES = [ @@ -34,8 +63,16 @@ class TechnoVESwitchDescription(SwitchEntityDescription): translation_key="auto_charge", entity_category=EntityCategory.CONFIG, is_on_fn=lambda station: station.info.auto_charge, - turn_on_fn=lambda technoVE: technoVE.set_auto_charge(enabled=True), - turn_off_fn=lambda technoVE: technoVE.set_auto_charge(enabled=False), + turn_on_fn=lambda coordinator: _set_auto_charge(coordinator, True), + turn_off_fn=lambda coordinator: _set_auto_charge(coordinator, False), + ), + TechnoVESwitchDescription( + key="session_active", + translation_key="session_active", + entity_category=EntityCategory.CONFIG, + is_on_fn=lambda station: station.info.is_session_active, + turn_on_fn=_enable_charging, + turn_off_fn=_disable_charging, ), ] @@ -76,11 +113,9 @@ def is_on(self) -> bool: @technove_exception_handler async def async_turn_on(self, **kwargs: Any) -> None: """Turn on the TechnoVE switch.""" - await self.entity_description.turn_on_fn(self.coordinator.technove) - await self.coordinator.async_request_refresh() + await self.entity_description.turn_on_fn(self.coordinator) @technove_exception_handler async def async_turn_off(self, **kwargs: Any) -> None: """Turn off the TechnoVE switch.""" - await self.entity_description.turn_off_fn(self.coordinator.technove) - await self.coordinator.async_request_refresh() + await self.entity_description.turn_off_fn(self.coordinator) diff --git a/homeassistant/components/telegram/icons.json b/homeassistant/components/telegram/icons.json index a03163179cb5bd..a9829425570a0c 100644 --- a/homeassistant/components/telegram/icons.json +++ b/homeassistant/components/telegram/icons.json @@ -1,5 +1,7 @@ { "services": { - "reload": "mdi:reload" + "reload": { + "service": "mdi:reload" + } } } diff --git a/homeassistant/components/telegram_bot/__init__.py b/homeassistant/components/telegram_bot/__init__.py index 9d1a5398055dab..2d53c744c225f8 100644 --- a/homeassistant/components/telegram_bot/__init__.py +++ b/homeassistant/components/telegram_bot/__init__.py @@ -41,6 +41,7 @@ from homeassistant.helpers import config_validation as cv, issue_registry as ir from homeassistant.helpers.typing import ConfigType from homeassistant.loader import async_get_loaded_integration +from homeassistant.util.ssl import get_default_context, get_default_no_verify_context _LOGGER = logging.getLogger(__name__) @@ -378,7 +379,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: for p_config in domain_config: # Each platform config gets its own bot - bot = initialize_bot(hass, p_config) + bot = await hass.async_add_executor_job(initialize_bot, hass, p_config) p_type: str = p_config[CONF_PLATFORM] platform = platforms[p_type] @@ -486,7 +487,7 @@ def initialize_bot(hass: HomeAssistant, p_config: dict) -> Bot: # Auth can actually be stuffed into the URL, but the docs have previously # indicated to put them here. auth = proxy_params.pop("username"), proxy_params.pop("password") - ir.async_create_issue( + ir.create_issue( hass, DOMAIN, "proxy_params_auth_deprecation", @@ -503,7 +504,7 @@ def initialize_bot(hass: HomeAssistant, p_config: dict) -> Bot: learn_more_url="https://github.com/home-assistant/core/pull/112778", ) else: - ir.async_create_issue( + ir.create_issue( hass, DOMAIN, "proxy_params_deprecation", @@ -852,7 +853,11 @@ async def send_file( username=kwargs.get(ATTR_USERNAME), password=kwargs.get(ATTR_PASSWORD), authentication=kwargs.get(ATTR_AUTHENTICATION), - verify_ssl=kwargs.get(ATTR_VERIFY_SSL), + verify_ssl=( + get_default_context() + if kwargs.get(ATTR_VERIFY_SSL, False) + else get_default_no_verify_context() + ), ) if file_content: diff --git a/homeassistant/components/telegram_bot/icons.json b/homeassistant/components/telegram_bot/icons.json index f410d3874356d9..0acf20d561ae1e 100644 --- a/homeassistant/components/telegram_bot/icons.json +++ b/homeassistant/components/telegram_bot/icons.json @@ -1,18 +1,46 @@ { "services": { - "send_message": "mdi:send", - "send_photo": "mdi:camera", - "send_sticker": "mdi:sticker", - "send_animation": "mdi:animation", - "send_video": "mdi:video", - "send_voice": "mdi:microphone", - "send_document": "mdi:file-document", - "send_location": "mdi:map-marker", - "send_poll": "mdi:poll", - "edit_message": "mdi:pencil", - "edit_caption": "mdi:pencil", - "edit_replymarkup": "mdi:pencil", - "answer_callback_query": "mdi:check", - "delete_message": "mdi:delete" + "send_message": { + "service": "mdi:send" + }, + "send_photo": { + "service": "mdi:camera" + }, + "send_sticker": { + "service": "mdi:sticker" + }, + "send_animation": { + "service": "mdi:animation" + }, + "send_video": { + "service": "mdi:video" + }, + "send_voice": { + "service": "mdi:microphone" + }, + "send_document": { + "service": "mdi:file-document" + }, + "send_location": { + "service": "mdi:map-marker" + }, + "send_poll": { + "service": "mdi:poll" + }, + "edit_message": { + "service": "mdi:pencil" + }, + "edit_caption": { + "service": "mdi:pencil" + }, + "edit_replymarkup": { + "service": "mdi:pencil" + }, + "answer_callback_query": { + "service": "mdi:check" + }, + "delete_message": { + "service": "mdi:delete" + } } } diff --git a/homeassistant/components/telegram_bot/manifest.json b/homeassistant/components/telegram_bot/manifest.json index c176e6c2cdf7c2..b432c88762fb4d 100644 --- a/homeassistant/components/telegram_bot/manifest.json +++ b/homeassistant/components/telegram_bot/manifest.json @@ -6,5 +6,5 @@ "documentation": "https://www.home-assistant.io/integrations/telegram_bot", "iot_class": "cloud_push", "loggers": ["telegram"], - "requirements": ["python-telegram-bot[socks]==21.0.1"] + "requirements": ["python-telegram-bot[socks]==21.5"] } diff --git a/homeassistant/components/telegram_bot/polling.py b/homeassistant/components/telegram_bot/polling.py index 45d2ee65b45a4a..bee7f752f6c987 100644 --- a/homeassistant/components/telegram_bot/polling.py +++ b/homeassistant/components/telegram_bot/polling.py @@ -25,14 +25,22 @@ async def async_setup_platform(hass, bot, config): async def process_error(update: Update, context: CallbackContext) -> None: """Telegram bot error handler.""" + if context.error: + error_callback(context.error, update) + + +def error_callback(error: Exception, update: Update | None = None) -> None: + """Log the error.""" try: - if context.error: - raise context.error + raise error except (TimedOut, NetworkError, RetryAfter): # Long polling timeout or connection problem. Nothing serious. pass except TelegramError: - _LOGGER.error('Update "%s" caused error: "%s"', update, context.error) + if update is not None: + _LOGGER.error('Update "%s" caused error: "%s"', update, error) + else: + _LOGGER.error("%s: %s", error.__class__.__name__, error) class PollBot(BaseTelegramBotEntity): @@ -53,7 +61,7 @@ async def start_polling(self, event=None): """Start the polling task.""" _LOGGER.debug("Starting polling") await self.application.initialize() - await self.application.updater.start_polling() + await self.application.updater.start_polling(error_callback=error_callback) await self.application.start() async def stop_polling(self, event=None): diff --git a/homeassistant/components/tellduslive/config_flow.py b/homeassistant/components/tellduslive/config_flow.py index 6f1318ca61ecde..6b5e7150d67fba 100644 --- a/homeassistant/components/tellduslive/config_flow.py +++ b/homeassistant/components/tellduslive/config_flow.py @@ -3,11 +3,12 @@ import asyncio import logging import os +from typing import Any from tellduslive import Session, supports_local_api import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_HOST from homeassistant.util.json import load_json_object @@ -50,7 +51,9 @@ def _get_auth_url(self): ) return self._session.authorize_url - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Let user select host or cloud.""" if self._async_current_entries(): return self.async_abort(reason="already_setup") @@ -122,14 +125,14 @@ async def async_step_discovery(self, discovery_info): return await self.async_step_user() - async def async_step_import(self, user_input): + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Import a config entry.""" if self._async_current_entries(): return self.async_abort(reason="already_setup") - self._scan_interval = user_input[KEY_SCAN_INTERVAL] - if user_input[CONF_HOST] != DOMAIN: - self._hosts.append(user_input[CONF_HOST]) + self._scan_interval = import_data[KEY_SCAN_INTERVAL] + if import_data[CONF_HOST] != DOMAIN: + self._hosts.append(import_data[CONF_HOST]) if not await self.hass.async_add_executor_job( os.path.isfile, self.hass.config.path(TELLDUS_CONFIG_FILE) @@ -141,7 +144,7 @@ async def async_step_import(self, user_input): ) host = next(iter(conf)) - if user_input[CONF_HOST] != host: + if import_data[CONF_HOST] != host: return await self.async_step_user() host = CLOUD_NAME if host == "tellduslive" else host diff --git a/homeassistant/components/tellduslive/manifest.json b/homeassistant/components/tellduslive/manifest.json index 929d502971f99f..dc1389c15c5738 100644 --- a/homeassistant/components/tellduslive/manifest.json +++ b/homeassistant/components/tellduslive/manifest.json @@ -6,5 +6,5 @@ "documentation": "https://www.home-assistant.io/integrations/tellduslive", "iot_class": "cloud_polling", "quality_scale": "silver", - "requirements": ["tellduslive==0.10.11"] + "requirements": ["tellduslive==0.10.12"] } diff --git a/homeassistant/components/template/icons.json b/homeassistant/components/template/icons.json index a03163179cb5bd..a9829425570a0c 100644 --- a/homeassistant/components/template/icons.json +++ b/homeassistant/components/template/icons.json @@ -1,5 +1,7 @@ { "services": { - "reload": "mdi:reload" + "reload": { + "service": "mdi:reload" + } } } diff --git a/homeassistant/components/template/number.py b/homeassistant/components/template/number.py index 955600a9b9e079..499ddc192ccb7a 100644 --- a/homeassistant/components/template/number.py +++ b/homeassistant/components/template/number.py @@ -70,7 +70,7 @@ vol.Required(CONF_NAME): cv.template, vol.Required(CONF_STATE): cv.template, vol.Required(CONF_STEP): cv.template, - vol.Optional(CONF_SET_VALUE): cv.SCRIPT_SCHEMA, + vol.Required(CONF_SET_VALUE): cv.SCRIPT_SCHEMA, vol.Optional(CONF_MIN): cv.template, vol.Optional(CONF_MAX): cv.template, vol.Optional(CONF_DEVICE_ID): selector.DeviceSelector(), @@ -154,11 +154,10 @@ def __init__( super().__init__(hass, config=config, unique_id=unique_id) assert self._attr_name is not None self._value_template = config[CONF_STATE] - self._command_set_value = ( - Script(hass, config[CONF_SET_VALUE], self._attr_name, DOMAIN) - if config.get(CONF_SET_VALUE, None) is not None - else None + self._command_set_value = Script( + hass, config[CONF_SET_VALUE], self._attr_name, DOMAIN ) + self._step_template = config[CONF_STEP] self._min_value_template = config[CONF_MIN] self._max_value_template = config[CONF_MAX] diff --git a/homeassistant/components/tesla_fleet/__init__.py b/homeassistant/components/tesla_fleet/__init__.py index 47a2a9173a5d09..3bcb0bf7ef9334 100644 --- a/homeassistant/components/tesla_fleet/__init__.py +++ b/homeassistant/components/tesla_fleet/__init__.py @@ -3,6 +3,7 @@ import asyncio from typing import Final +from aiohttp.client_exceptions import ClientResponseError import jwt from tesla_fleet_api import EnergySpecific, TeslaFleetApi, VehicleSpecific from tesla_fleet_api.const import Scope @@ -38,7 +39,12 @@ from .models import TeslaFleetData, TeslaFleetEnergyData, TeslaFleetVehicleData from .oauth import TeslaSystemImplementation -PLATFORMS: Final = [Platform.BINARY_SENSOR, Platform.DEVICE_TRACKER, Platform.SENSOR] +PLATFORMS: Final = [ + Platform.BINARY_SENSOR, + Platform.CLIMATE, + Platform.DEVICE_TRACKER, + Platform.SENSOR, +] type TeslaFleetConfigEntry = ConfigEntry[TeslaFleetData] @@ -52,8 +58,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: TeslaFleetConfigEntry) - session = async_get_clientsession(hass) token = jwt.decode(access_token, options={"verify_signature": False}) - scopes = token["scp"] - region = token["ou_code"].lower() + scopes: list[Scope] = [Scope(s) for s in token["scp"]] + region: str = token["ou_code"].lower() OAuth2FlowHandler.async_register_implementation( hass, @@ -66,7 +72,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: TeslaFleetConfigEntry) - async def _refresh_token() -> str: async with refresh_lock: - await oauth_session.async_ensure_token_valid() + try: + await oauth_session.async_ensure_token_valid() + except ClientResponseError as e: + if e.status == 401: + raise ConfigEntryAuthFailed from e + raise ConfigEntryNotReady from e token: str = oauth_session.token[CONF_ACCESS_TOKEN] return token @@ -127,6 +138,7 @@ async def _refresh_token() -> str: coordinator=coordinator, vin=vin, device=device, + signing=product["command_signing"] == "required", ) ) elif "energy_site_id" in product and hasattr(tesla, "energy"): diff --git a/homeassistant/components/tesla_fleet/climate.py b/homeassistant/components/tesla_fleet/climate.py new file mode 100644 index 00000000000000..6199ee112b5dcd --- /dev/null +++ b/homeassistant/components/tesla_fleet/climate.py @@ -0,0 +1,330 @@ +"""Climate platform for Tesla Fleet integration.""" + +from __future__ import annotations + +from itertools import chain +from typing import Any, cast + +from tesla_fleet_api.const import CabinOverheatProtectionTemp, Scope + +from homeassistant.components.climate import ( + ATTR_HVAC_MODE, + ClimateEntity, + ClimateEntityFeature, + HVACMode, +) +from homeassistant.const import ( + ATTR_TEMPERATURE, + PRECISION_HALVES, + PRECISION_WHOLE, + UnitOfTemperature, +) +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import ServiceValidationError +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from . import TeslaFleetConfigEntry +from .const import DOMAIN, TeslaFleetClimateSide +from .entity import TeslaFleetVehicleEntity +from .helpers import handle_vehicle_command +from .models import TeslaFleetVehicleData + +DEFAULT_MIN_TEMP = 15 +DEFAULT_MAX_TEMP = 28 + +PARALLEL_UPDATES = 0 + + +async def async_setup_entry( + hass: HomeAssistant, + entry: TeslaFleetConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up the Tesla Fleet Climate platform from a config entry.""" + + async_add_entities( + chain( + ( + TeslaFleetClimateEntity( + vehicle, TeslaFleetClimateSide.DRIVER, entry.runtime_data.scopes + ) + for vehicle in entry.runtime_data.vehicles + ), + ( + TeslaFleetCabinOverheatProtectionEntity( + vehicle, entry.runtime_data.scopes + ) + for vehicle in entry.runtime_data.vehicles + ), + ) + ) + + +class TeslaFleetClimateEntity(TeslaFleetVehicleEntity, ClimateEntity): + """Tesla Fleet vehicle climate entity.""" + + _attr_precision = PRECISION_HALVES + + _attr_temperature_unit = UnitOfTemperature.CELSIUS + _attr_hvac_modes = [HVACMode.HEAT_COOL, HVACMode.OFF] + _attr_supported_features = ( + ClimateEntityFeature.TURN_ON + | ClimateEntityFeature.TURN_OFF + | ClimateEntityFeature.TARGET_TEMPERATURE + | ClimateEntityFeature.PRESET_MODE + ) + _attr_preset_modes = ["off", "keep", "dog", "camp"] + _enable_turn_on_off_backwards_compatibility = False + + def __init__( + self, + data: TeslaFleetVehicleData, + side: TeslaFleetClimateSide, + scopes: Scope, + ) -> None: + """Initialize the climate.""" + + self.read_only = Scope.VEHICLE_CMDS not in scopes or data.signing + + if self.read_only: + self._attr_supported_features = ClimateEntityFeature(0) + self._attr_hvac_modes = [] + + super().__init__( + data, + side, + ) + + def _async_update_attrs(self) -> None: + """Update the attributes of the entity.""" + value = self.get("climate_state_is_climate_on") + if value is None: + self._attr_hvac_mode = None + elif value: + self._attr_hvac_mode = HVACMode.HEAT_COOL + else: + self._attr_hvac_mode = HVACMode.OFF + + # If not scoped, prevent the user from changing the HVAC mode by making it the only option + if self._attr_hvac_mode and self.read_only: + self._attr_hvac_modes = [self._attr_hvac_mode] + + self._attr_current_temperature = self.get("climate_state_inside_temp") + self._attr_target_temperature = self.get(f"climate_state_{self.key}_setting") + self._attr_preset_mode = self.get("climate_state_climate_keeper_mode") + self._attr_min_temp = cast( + float, self.get("climate_state_min_avail_temp", DEFAULT_MIN_TEMP) + ) + self._attr_max_temp = cast( + float, self.get("climate_state_max_avail_temp", DEFAULT_MAX_TEMP) + ) + + async def async_turn_on(self) -> None: + """Set the climate state to on.""" + + await self.wake_up_if_asleep() + await handle_vehicle_command(self.api.auto_conditioning_start()) + + self._attr_hvac_mode = HVACMode.HEAT_COOL + self.async_write_ha_state() + + async def async_turn_off(self) -> None: + """Set the climate state to off.""" + + await self.wake_up_if_asleep() + await handle_vehicle_command(self.api.auto_conditioning_stop()) + + self._attr_hvac_mode = HVACMode.OFF + self._attr_preset_mode = self._attr_preset_modes[0] + self.async_write_ha_state() + + async def async_set_temperature(self, **kwargs: Any) -> None: + """Set the climate temperature.""" + + if ATTR_TEMPERATURE not in kwargs: + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="missing_temperature", + ) + + temp = kwargs[ATTR_TEMPERATURE] + await self.wake_up_if_asleep() + await handle_vehicle_command( + self.api.set_temps( + driver_temp=temp, + passenger_temp=temp, + ) + ) + self._attr_target_temperature = temp + + if mode := kwargs.get(ATTR_HVAC_MODE): + # Set HVAC mode will call write_ha_state + await self.async_set_hvac_mode(mode) + else: + self.async_write_ha_state() + + async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None: + """Set the climate mode and state.""" + if hvac_mode not in self.hvac_modes: + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="invalid_hvac_mode", + translation_placeholders={"hvac_mode": hvac_mode}, + ) + if hvac_mode == HVACMode.OFF: + await self.async_turn_off() + else: + await self.async_turn_on() + + async def async_set_preset_mode(self, preset_mode: str) -> None: + """Set the climate preset mode.""" + await self.wake_up_if_asleep() + await handle_vehicle_command( + self.api.set_climate_keeper_mode( + climate_keeper_mode=self._attr_preset_modes.index(preset_mode) + ) + ) + self._attr_preset_mode = preset_mode + if preset_mode != self._attr_preset_modes[0]: + self._attr_hvac_mode = HVACMode.HEAT_COOL + self.async_write_ha_state() + + +COP_MODES = { + "Off": HVACMode.OFF, + "On": HVACMode.COOL, + "FanOnly": HVACMode.FAN_ONLY, +} + +# String to celsius +COP_LEVELS = { + "Low": 30, + "Medium": 35, + "High": 40, +} + +# Celsius to IntEnum +TEMP_LEVELS = { + 30: CabinOverheatProtectionTemp.LOW, + 35: CabinOverheatProtectionTemp.MEDIUM, + 40: CabinOverheatProtectionTemp.HIGH, +} + + +class TeslaFleetCabinOverheatProtectionEntity(TeslaFleetVehicleEntity, ClimateEntity): + """Tesla Fleet vehicle cabin overheat protection entity.""" + + _attr_precision = PRECISION_WHOLE + _attr_target_temperature_step = 5 + _attr_min_temp = COP_LEVELS["Low"] + _attr_max_temp = COP_LEVELS["High"] + _attr_temperature_unit = UnitOfTemperature.CELSIUS + _attr_hvac_modes = list(COP_MODES.values()) + _enable_turn_on_off_backwards_compatibility = False + _attr_entity_registry_enabled_default = False + + def __init__( + self, + data: TeslaFleetVehicleData, + scopes: Scope, + ) -> None: + """Initialize the cabin overheat climate entity.""" + + # Scopes + self.read_only = Scope.VEHICLE_CMDS not in scopes or data.signing + + # Supported Features + if self.read_only: + self._attr_supported_features = ClimateEntityFeature(0) + self._attr_hvac_modes = [] + else: + self._attr_supported_features = ( + ClimateEntityFeature.TURN_ON | ClimateEntityFeature.TURN_OFF + ) + + super().__init__(data, "climate_state_cabin_overheat_protection") + + def _async_update_attrs(self) -> None: + """Update the attributes of the entity.""" + + if (state := self.get("climate_state_cabin_overheat_protection")) is None: + self._attr_hvac_mode = None + else: + self._attr_hvac_mode = COP_MODES.get(state) + + # If not scoped, prevent the user from changing the HVAC mode by making it the only option + if self._attr_hvac_mode and self.read_only: + self._attr_hvac_modes = [self._attr_hvac_mode] + + if (level := self.get("climate_state_cop_activation_temperature")) is None: + self._attr_target_temperature = None + else: + self._attr_target_temperature = COP_LEVELS.get(level) + + self._attr_current_temperature = self.get("climate_state_inside_temp") + + @property + def supported_features(self) -> ClimateEntityFeature: + """Return the list of supported features.""" + if not self.read_only and self.get( + "vehicle_config_cop_user_set_temp_supported" + ): + return ( + self._attr_supported_features | ClimateEntityFeature.TARGET_TEMPERATURE + ) + return self._attr_supported_features + + async def async_turn_on(self) -> None: + """Set the climate state to on.""" + await self.async_set_hvac_mode(HVACMode.COOL) + + async def async_turn_off(self) -> None: + """Set the climate state to off.""" + await self.async_set_hvac_mode(HVACMode.OFF) + + async def async_set_temperature(self, **kwargs: Any) -> None: + """Set the climate temperature.""" + + if ATTR_TEMPERATURE not in kwargs: + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="missing_temperature", + ) + + temp = kwargs[ATTR_TEMPERATURE] + if (cop_mode := TEMP_LEVELS.get(temp)) is None: + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="invalid_cop_temp", + ) + + await self.wake_up_if_asleep() + await handle_vehicle_command(self.api.set_cop_temp(cop_mode)) + self._attr_target_temperature = temp + + if mode := kwargs.get(ATTR_HVAC_MODE): + await self._async_set_cop(mode) + + self.async_write_ha_state() + + async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None: + """Set the climate mode and state.""" + await self.wake_up_if_asleep() + await self._async_set_cop(hvac_mode) + self.async_write_ha_state() + + async def _async_set_cop(self, hvac_mode: HVACMode) -> None: + if hvac_mode == HVACMode.OFF: + await handle_vehicle_command( + self.api.set_cabin_overheat_protection(on=False, fan_only=False) + ) + elif hvac_mode == HVACMode.COOL: + await handle_vehicle_command( + self.api.set_cabin_overheat_protection(on=True, fan_only=False) + ) + elif hvac_mode == HVACMode.FAN_ONLY: + await handle_vehicle_command( + self.api.set_cabin_overheat_protection(on=True, fan_only=True) + ) + + self._attr_hvac_mode = hvac_mode diff --git a/homeassistant/components/tesla_fleet/config_flow.py b/homeassistant/components/tesla_fleet/config_flow.py index 0ffdca1aec601a..64b88792387de2 100644 --- a/homeassistant/components/tesla_fleet/config_flow.py +++ b/homeassistant/components/tesla_fleet/config_flow.py @@ -83,5 +83,8 @@ async def async_step_reauth_confirm( ) -> ConfigFlowResult: """Confirm reauth dialog.""" if user_input is None: - return self.async_show_form(step_id="reauth_confirm") + return self.async_show_form( + step_id="reauth_confirm", + description_placeholders={"name": "Tesla Fleet"}, + ) return await self.async_step_user() diff --git a/homeassistant/components/tesla_fleet/const.py b/homeassistant/components/tesla_fleet/const.py index 081225c296cf9c..53e34092326e96 100644 --- a/homeassistant/components/tesla_fleet/const.py +++ b/homeassistant/components/tesla_fleet/const.py @@ -41,3 +41,10 @@ class TeslaFleetState(StrEnum): ONLINE = "online" ASLEEP = "asleep" OFFLINE = "offline" + + +class TeslaFleetClimateSide(StrEnum): + """Tesla Fleet Climate Keeper Modes.""" + + DRIVER = "driver_temp" + PASSENGER = "passenger_temp" diff --git a/homeassistant/components/tesla_fleet/entity.py b/homeassistant/components/tesla_fleet/entity.py index c853bb798b56f6..103fd216953c2b 100644 --- a/homeassistant/components/tesla_fleet/entity.py +++ b/homeassistant/components/tesla_fleet/entity.py @@ -14,6 +14,7 @@ TeslaFleetEnergySiteLiveCoordinator, TeslaFleetVehicleDataCoordinator, ) +from .helpers import wake_up_vehicle from .models import TeslaFleetEnergyData, TeslaFleetVehicleData @@ -27,6 +28,7 @@ class TeslaFleetEntity( """Parent class for all TeslaFleet entities.""" _attr_has_entity_name = True + read_only: bool def __init__( self, @@ -100,6 +102,10 @@ def _value(self) -> Any | None: """Return a specific value from coordinator data.""" return self.coordinator.data.get(self.key) + async def wake_up_if_asleep(self) -> None: + """Wake up the vehicle if its asleep.""" + await wake_up_vehicle(self.vehicle) + class TeslaFleetEnergyLiveEntity(TeslaFleetEntity): """Parent class for TeslaFleet Energy Site Live entities.""" diff --git a/homeassistant/components/tesla_fleet/helpers.py b/homeassistant/components/tesla_fleet/helpers.py new file mode 100644 index 00000000000000..d554ccce70c666 --- /dev/null +++ b/homeassistant/components/tesla_fleet/helpers.py @@ -0,0 +1,80 @@ +"""Tesla Fleet helper functions.""" + +import asyncio +from collections.abc import Awaitable +from typing import Any + +from tesla_fleet_api.exceptions import TeslaFleetError + +from homeassistant.exceptions import HomeAssistantError + +from .const import DOMAIN, LOGGER, TeslaFleetState +from .models import TeslaFleetVehicleData + + +async def wake_up_vehicle(vehicle: TeslaFleetVehicleData) -> None: + """Wake up a vehicle.""" + async with vehicle.wakelock: + times = 0 + while vehicle.coordinator.data["state"] != TeslaFleetState.ONLINE: + try: + if times == 0: + cmd = await vehicle.api.wake_up() + else: + cmd = await vehicle.api.vehicle() + state = cmd["response"]["state"] + except TeslaFleetError as e: + raise HomeAssistantError(str(e)) from e + vehicle.coordinator.data["state"] = state + if state != TeslaFleetState.ONLINE: + times += 1 + if times >= 4: # Give up after 30 seconds total + raise HomeAssistantError("Could not wake up vehicle") + await asyncio.sleep(times * 5) + + +async def handle_command(command: Awaitable) -> dict[str, Any]: + """Handle a command.""" + try: + result = await command + except TeslaFleetError as e: + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="command_failed", + translation_placeholders={"message": e.message}, + ) from e + LOGGER.debug("Command result: %s", result) + return result + + +async def handle_vehicle_command(command: Awaitable) -> bool: + """Handle a vehicle command.""" + result = await handle_command(command) + if (response := result.get("response")) is None: + if error := result.get("error"): + # No response with error + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="command_error", + translation_placeholders={"error": error}, + ) + # No response without error (unexpected) + raise HomeAssistantError(f"Unknown response: {response}") + if (result := response.get("result")) is not True: + if reason := response.get("reason"): + if reason in ("already_set", "not_charging", "requested"): + # Reason is acceptable + return result + # Result of false with reason + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="command_reason", + translation_placeholders={"reason": reason}, + ) + # Result of false without reason (unexpected) + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="command_no_reason", + ) + # Response with result of true + return result diff --git a/homeassistant/components/tesla_fleet/icons.json b/homeassistant/components/tesla_fleet/icons.json index 2dbde45ee08c01..dc40f2820374ef 100644 --- a/homeassistant/components/tesla_fleet/icons.json +++ b/homeassistant/components/tesla_fleet/icons.json @@ -38,6 +38,20 @@ } } }, + "climate": { + "driver_temp": { + "state_attributes": { + "preset_mode": { + "state": { + "off": "mdi:power", + "keep": "mdi:fan", + "dog": "mdi:dog", + "camp": "mdi:tent" + } + } + } + } + }, "device_tracker": { "location": { "default": "mdi:map-marker" diff --git a/homeassistant/components/tesla_fleet/models.py b/homeassistant/components/tesla_fleet/models.py index 1b1f5f083cdd66..ae945dd96bf1c3 100644 --- a/homeassistant/components/tesla_fleet/models.py +++ b/homeassistant/components/tesla_fleet/models.py @@ -2,6 +2,7 @@ from __future__ import annotations +import asyncio from dataclasses import dataclass from tesla_fleet_api import EnergySpecific, VehicleSpecific @@ -33,6 +34,8 @@ class TeslaFleetVehicleData: coordinator: TeslaFleetVehicleDataCoordinator vin: str device: DeviceInfo + signing: bool + wakelock = asyncio.Lock() @dataclass diff --git a/homeassistant/components/tesla_fleet/strings.json b/homeassistant/components/tesla_fleet/strings.json index 6e74714ddd5dae..5b59d3efc5c3d6 100644 --- a/homeassistant/components/tesla_fleet/strings.json +++ b/homeassistant/components/tesla_fleet/strings.json @@ -19,7 +19,7 @@ }, "reauth_confirm": { "title": "[%key:common::config_flow::title::reauth%]", - "description": "The Withings integration needs to re-authenticate your account" + "description": "The {name} integration needs to re-authenticate your account" } }, "create_entry": { @@ -107,6 +107,24 @@ "name": "Tire pressure warning rear right" } }, + "climate": { + "climate_state_cabin_overheat_protection": { + "name": "Cabin overheat protection" + }, + "driver_temp": { + "name": "[%key:component::climate::title%]", + "state_attributes": { + "preset_mode": { + "state": { + "off": "Normal", + "keep": "Keep mode", + "dog": "Dog mode", + "camp": "Camp mode" + } + } + } + } + }, "device_tracker": { "location": { "name": "Location" @@ -272,7 +290,28 @@ }, "exceptions": { "update_failed": { - "message": "{endpoint} data request failed. {message}" + "message": "{endpoint} data request failed: {message}" + }, + "command_failed": { + "message": "Command failed: {message}" + }, + "command_error": { + "message": "Command returned an error: {error}" + }, + "command_reason": { + "message": "Command was unsuccessful: {reason}" + }, + "command_no_reason": { + "message": "Command was unsuccessful but did not return a reason why." + }, + "invalid_cop_temp": { + "message": "Cabin overheat protection does not support that temperature." + }, + "invalid_hvac_mode": { + "message": "Climate mode {hvac_mode} is not supported." + }, + "missing_temperature": { + "message": "Temperature is required for this action." } } } diff --git a/homeassistant/components/teslemetry/climate.py b/homeassistant/components/teslemetry/climate.py index 5b093b0c6f1268..9218be4dcb1780 100644 --- a/homeassistant/components/teslemetry/climate.py +++ b/homeassistant/components/teslemetry/climate.py @@ -84,8 +84,10 @@ def __init__( ) -> None: """Initialize the climate.""" self.scoped = Scope.VEHICLE_CMDS in scopes + if not self.scoped: self._attr_supported_features = ClimateEntityFeature(0) + self._attr_hvac_modes = [] super().__init__( data, @@ -102,6 +104,10 @@ def _async_update_attrs(self) -> None: else: self._attr_hvac_mode = HVACMode.OFF + # If not scoped, prevent the user from changing the HVAC mode by making it the only option + if self._attr_hvac_mode and not self.scoped: + self._attr_hvac_modes = [self._attr_hvac_mode] + self._attr_current_temperature = self.get("climate_state_inside_temp") self._attr_target_temperature = self.get(f"climate_state_{self.key}_setting") self._attr_preset_mode = self.get("climate_state_climate_keeper_mode") @@ -114,7 +120,6 @@ def _async_update_attrs(self) -> None: async def async_turn_on(self) -> None: """Set the climate state to on.""" - self.raise_for_scope() await self.wake_up_if_asleep() await handle_vehicle_command(self.api.auto_conditioning_start()) @@ -124,7 +129,6 @@ async def async_turn_on(self) -> None: async def async_turn_off(self) -> None: """Set the climate state to off.""" - self.raise_for_scope() await self.wake_up_if_asleep() await handle_vehicle_command(self.api.auto_conditioning_stop()) @@ -135,7 +139,6 @@ async def async_turn_off(self) -> None: async def async_set_temperature(self, **kwargs: Any) -> None: """Set the climate temperature.""" - if temp := kwargs.get(ATTR_TEMPERATURE): await self.wake_up_if_asleep() await handle_vehicle_command( @@ -168,9 +171,8 @@ async def async_set_preset_mode(self, preset_mode: str) -> None: ) ) self._attr_preset_mode = preset_mode - if preset_mode == self._attr_preset_modes[0]: - self._attr_hvac_mode = HVACMode.OFF - else: + if preset_mode != self._attr_preset_modes[0]: + # Changing preset mode will also turn on climate self._attr_hvac_mode = HVACMode.HEAT_COOL self.async_write_ha_state() @@ -181,20 +183,28 @@ async def async_set_preset_mode(self, preset_mode: str) -> None: "FanOnly": HVACMode.FAN_ONLY, } +# String to celsius COP_LEVELS = { "Low": 30, "Medium": 35, "High": 40, } +# Celsius to IntEnum +TEMP_LEVELS = { + 30: CabinOverheatProtectionTemp.LOW, + 35: CabinOverheatProtectionTemp.MEDIUM, + 40: CabinOverheatProtectionTemp.HIGH, +} + class TeslemetryCabinOverheatProtectionEntity(TeslemetryVehicleEntity, ClimateEntity): """Telemetry vehicle cabin overheat protection entity.""" _attr_precision = PRECISION_WHOLE _attr_target_temperature_step = 5 - _attr_min_temp = 30 - _attr_max_temp = 40 + _attr_min_temp = COP_LEVELS["Low"] + _attr_max_temp = COP_LEVELS["High"] _attr_temperature_unit = UnitOfTemperature.CELSIUS _attr_hvac_modes = list(COP_MODES.values()) _enable_turn_on_off_backwards_compatibility = False @@ -207,20 +217,21 @@ def __init__( ) -> None: """Initialize the climate.""" + self.scoped = Scope.VEHICLE_CMDS in scopes + if self.scoped: + self._attr_supported_features = ( + ClimateEntityFeature.TURN_ON | ClimateEntityFeature.TURN_OFF + ) + else: + self._attr_supported_features = ClimateEntityFeature(0) + self._attr_hvac_modes = [] + super().__init__(data, "climate_state_cabin_overheat_protection") - # Supported Features - self._attr_supported_features = ( - ClimateEntityFeature.TURN_ON | ClimateEntityFeature.TURN_OFF - ) - if self.get("vehicle_config_cop_user_set_temp_supported"): + # Supported Features from data + if self.scoped and self.get("vehicle_config_cop_user_set_temp_supported"): self._attr_supported_features |= ClimateEntityFeature.TARGET_TEMPERATURE - # Scopes - self.scoped = Scope.VEHICLE_CMDS in scopes - if not self.scoped: - self._attr_supported_features = ClimateEntityFeature(0) - def _async_update_attrs(self) -> None: """Update the attributes of the entity.""" @@ -229,6 +240,10 @@ def _async_update_attrs(self) -> None: else: self._attr_hvac_mode = COP_MODES.get(state) + # If not scoped, prevent the user from changing the HVAC mode by making it the only option + if self._attr_hvac_mode and not self.scoped: + self._attr_hvac_modes = [self._attr_hvac_mode] + if (level := self.get("climate_state_cop_activation_temperature")) is None: self._attr_target_temperature = None else: @@ -246,18 +261,10 @@ async def async_turn_off(self) -> None: async def async_set_temperature(self, **kwargs: Any) -> None: """Set the climate temperature.""" - self.raise_for_scope() - if not (temp := kwargs.get(ATTR_TEMPERATURE)): return - if temp == 30: - cop_mode = CabinOverheatProtectionTemp.LOW - elif temp == 35: - cop_mode = CabinOverheatProtectionTemp.MEDIUM - elif temp == 40: - cop_mode = CabinOverheatProtectionTemp.HIGH - else: + if (cop_mode := TEMP_LEVELS.get(temp)) is None: raise ServiceValidationError( translation_domain=DOMAIN, translation_key="invalid_cop_temp", diff --git a/homeassistant/components/teslemetry/icons.json b/homeassistant/components/teslemetry/icons.json index aea98e95e0ba9b..1912d2265f64c3 100644 --- a/homeassistant/components/teslemetry/icons.json +++ b/homeassistant/components/teslemetry/icons.json @@ -129,7 +129,6 @@ "off": "mdi:car-seat" } }, - "components_customer_preferred_export_rule": { "default": "mdi:transmission-tower", "state": { @@ -259,11 +258,23 @@ } }, "services": { - "navigation_gps_request": "mdi:crosshairs-gps", - "set_scheduled_charging": "mdi:timeline-clock-outline", - "set_scheduled_departure": "mdi:home-clock", - "speed_limit": "mdi:car-speed-limiter", - "valet_mode": "mdi:speedometer-slow", - "time_of_use": "mdi:clock-time-eight-outline" + "navigation_gps_request": { + "service": "mdi:crosshairs-gps" + }, + "set_scheduled_charging": { + "service": "mdi:timeline-clock-outline" + }, + "set_scheduled_departure": { + "service": "mdi:home-clock" + }, + "speed_limit": { + "service": "mdi:car-speed-limiter" + }, + "valet_mode": { + "service": "mdi:speedometer-slow" + }, + "time_of_use": { + "service": "mdi:clock-time-eight-outline" + } } } diff --git a/homeassistant/components/tessie/config_flow.py b/homeassistant/components/tessie/config_flow.py index 1cbc070e463b22..bee518ce95fda3 100644 --- a/homeassistant/components/tessie/config_flow.py +++ b/homeassistant/components/tessie/config_flow.py @@ -66,7 +66,7 @@ async def async_step_user( ) async def async_step_reauth( - self, user_input: Mapping[str, Any] + self, entry_data: Mapping[str, Any] ) -> ConfigFlowResult: """Handle re-auth.""" self._reauth_entry = self.hass.config_entries.async_get_entry( diff --git a/homeassistant/components/text/icons.json b/homeassistant/components/text/icons.json index 355c439ec3338e..9448c9a73252ab 100644 --- a/homeassistant/components/text/icons.json +++ b/homeassistant/components/text/icons.json @@ -5,6 +5,8 @@ } }, "services": { - "set_value": "mdi:form-textbox" + "set_value": { + "service": "mdi:form-textbox" + } } } diff --git a/homeassistant/components/thethingsnetwork/config_flow.py b/homeassistant/components/thethingsnetwork/config_flow.py index cbb780e70647ff..7480e4cb1d922c 100644 --- a/homeassistant/components/thethingsnetwork/config_flow.py +++ b/homeassistant/components/thethingsnetwork/config_flow.py @@ -89,7 +89,7 @@ async def async_step_user( return self.async_show_form(step_id="user", data_schema=schema, errors=errors) async def async_step_reauth( - self, user_input: Mapping[str, Any] + self, entry_data: Mapping[str, Any] ) -> ConfigFlowResult: """Handle a flow initialized by a reauth event.""" diff --git a/homeassistant/components/thethingsnetwork/manifest.json b/homeassistant/components/thethingsnetwork/manifest.json index c39b2b7c421a9f..8d826750e39e56 100644 --- a/homeassistant/components/thethingsnetwork/manifest.json +++ b/homeassistant/components/thethingsnetwork/manifest.json @@ -6,5 +6,5 @@ "documentation": "https://www.home-assistant.io/integrations/thethingsnetwork", "integration_type": "hub", "iot_class": "cloud_polling", - "requirements": ["ttn_client==1.1.0"] + "requirements": ["ttn_client==1.2.0"] } diff --git a/homeassistant/components/thread/config_flow.py b/homeassistant/components/thread/config_flow.py index b4b6eac0fc88a5..568b76d4999396 100644 --- a/homeassistant/components/thread/config_flow.py +++ b/homeassistant/components/thread/config_flow.py @@ -15,9 +15,7 @@ class ThreadConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - async def async_step_import( - self, import_data: dict[str, str] | None = None - ) -> ConfigFlowResult: + async def async_step_import(self, import_data: None) -> ConfigFlowResult: """Set up by import from async_setup.""" await self._async_handle_discovery_without_unique_id() return self.async_create_entry(title="Thread", data={}) diff --git a/homeassistant/components/tibber/__init__.py b/homeassistant/components/tibber/__init__.py index 7c44e797780cd2..ce05b8070f6c8d 100644 --- a/homeassistant/components/tibber/__init__.py +++ b/homeassistant/components/tibber/__init__.py @@ -18,7 +18,7 @@ from homeassistant.helpers.aiohttp_client import async_get_clientsession import homeassistant.helpers.config_validation as cv from homeassistant.helpers.typing import ConfigType -from homeassistant.util import dt as dt_util +from homeassistant.util import dt as dt_util, ssl as ssl_util from .const import DATA_HASS_CONFIG, DOMAIN from .services import async_setup_services @@ -47,6 +47,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: access_token=entry.data[CONF_ACCESS_TOKEN], websession=async_get_clientsession(hass), time_zone=dt_util.get_default_time_zone(), + ssl=ssl_util.get_default_context(), ) hass.data[DOMAIN] = tibber_connection @@ -61,13 +62,13 @@ async def _close(event: Event) -> None: except ( TimeoutError, aiohttp.ClientError, - tibber.RetryableHttpException, + tibber.RetryableHttpExceptionError, ) as err: raise ConfigEntryNotReady("Unable to connect") from err - except tibber.InvalidLogin as exp: + except tibber.InvalidLoginError as exp: _LOGGER.error("Failed to login. %s", exp) return False - except tibber.FatalHttpException: + except tibber.FatalHttpExceptionError: return False await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) diff --git a/homeassistant/components/tibber/config_flow.py b/homeassistant/components/tibber/config_flow.py index abee3ea50bc82a..2d4df5107a29cc 100644 --- a/homeassistant/components/tibber/config_flow.py +++ b/homeassistant/components/tibber/config_flow.py @@ -47,12 +47,12 @@ async def async_step_user( await tibber_connection.update_info() except TimeoutError: errors[CONF_ACCESS_TOKEN] = ERR_TIMEOUT - except tibber.InvalidLogin: + except tibber.InvalidLoginError: errors[CONF_ACCESS_TOKEN] = ERR_TOKEN except ( aiohttp.ClientError, - tibber.RetryableHttpException, - tibber.FatalHttpException, + tibber.RetryableHttpExceptionError, + tibber.FatalHttpExceptionError, ): errors[CONF_ACCESS_TOKEN] = ERR_CLIENT diff --git a/homeassistant/components/tibber/coordinator.py b/homeassistant/components/tibber/coordinator.py index c3746cb9a582bb..78841f9db91d7f 100644 --- a/homeassistant/components/tibber/coordinator.py +++ b/homeassistant/components/tibber/coordinator.py @@ -49,9 +49,9 @@ async def _async_update_data(self) -> None: await self._tibber_connection.fetch_consumption_data_active_homes() await self._tibber_connection.fetch_production_data_active_homes() await self._insert_statistics() - except tibber.RetryableHttpException as err: + except tibber.RetryableHttpExceptionError as err: raise UpdateFailed(f"Error communicating with API ({err.status})") from err - except tibber.FatalHttpException: + except tibber.FatalHttpExceptionError: # Fatal error. Reload config entry to show correct error. self.hass.async_create_task( self.hass.config_entries.async_reload(self.config_entry.entry_id) diff --git a/homeassistant/components/tibber/icons.json b/homeassistant/components/tibber/icons.json index c6cdd9b0e25475..ddc8c73514564a 100644 --- a/homeassistant/components/tibber/icons.json +++ b/homeassistant/components/tibber/icons.json @@ -1,5 +1,7 @@ { "services": { - "get_prices": "mdi:cash" + "get_prices": { + "service": "mdi:cash" + } } } diff --git a/homeassistant/components/tibber/manifest.json b/homeassistant/components/tibber/manifest.json index 1d8120a4321e38..527364b6866c80 100644 --- a/homeassistant/components/tibber/manifest.json +++ b/homeassistant/components/tibber/manifest.json @@ -8,5 +8,5 @@ "iot_class": "cloud_polling", "loggers": ["tibber"], "quality_scale": "silver", - "requirements": ["pyTibber==0.28.2"] + "requirements": ["pyTibber==0.30.1"] } diff --git a/homeassistant/components/tibber/sensor.py b/homeassistant/components/tibber/sensor.py index a9090add49b375..09b36f4192972e 100644 --- a/homeassistant/components/tibber/sensor.py +++ b/homeassistant/components/tibber/sensor.py @@ -383,6 +383,7 @@ def __init__(self, tibber_home: tibber.TibberHome) -> None: "off_peak_1": None, "peak": None, "off_peak_2": None, + "intraday_price_ranking": None, } self._attr_icon = ICON self._attr_unique_id = self._tibber_home.home_id @@ -411,8 +412,9 @@ async def async_update(self) -> None: return res = self._tibber_home.current_price_data() - self._attr_native_value, price_level, self._last_updated = res + self._attr_native_value, price_level, self._last_updated, price_rank = res self._attr_extra_state_attributes["price_level"] = price_level + self._attr_extra_state_attributes["intraday_price_ranking"] = price_rank attrs = self._tibber_home.current_attributes() self._attr_extra_state_attributes.update(attrs) diff --git a/homeassistant/components/tile/config_flow.py b/homeassistant/components/tile/config_flow.py index 108d9b1b300dba..534259583417f6 100644 --- a/homeassistant/components/tile/config_flow.py +++ b/homeassistant/components/tile/config_flow.py @@ -71,11 +71,9 @@ async def _async_verify(self, step_id: str, schema: vol.Schema) -> ConfigFlowRes return self.async_create_entry(title=self._username, data=data) - async def async_step_import( - self, import_config: dict[str, Any] - ) -> ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Import a config entry from configuration.yaml.""" - return await self.async_step_user(import_config) + return await self.async_step_user(import_data) async def async_step_reauth( self, entry_data: Mapping[str, Any] diff --git a/homeassistant/components/time/icons.json b/homeassistant/components/time/icons.json index c08e457e04dd9e..f172c28ae0dfb6 100644 --- a/homeassistant/components/time/icons.json +++ b/homeassistant/components/time/icons.json @@ -5,6 +5,8 @@ } }, "services": { - "set_value": "mdi:clock-edit" + "set_value": { + "service": "mdi:clock-edit" + } } } diff --git a/homeassistant/components/timer/__init__.py b/homeassistant/components/timer/__init__.py index c2057551239348..19b1de427ef4cf 100644 --- a/homeassistant/components/timer/__init__.py +++ b/homeassistant/components/timer/__init__.py @@ -338,7 +338,9 @@ def async_change(self, duration: timedelta) -> None: raise HomeAssistantError( f"Timer {self.entity_id} is not running, only active timers can be changed" ) - if self._remaining and (self._remaining + duration) > self._running_duration: + # Check against new remaining time before checking boundaries + new_remaining = (self._end + duration) - dt_util.utcnow().replace(microsecond=0) + if self._remaining and new_remaining > self._running_duration: raise HomeAssistantError( f"Not possible to change timer {self.entity_id} beyond duration" ) @@ -349,7 +351,7 @@ def async_change(self, duration: timedelta) -> None: self._listener() self._end += duration - self._remaining = self._end - dt_util.utcnow().replace(microsecond=0) + self._remaining = new_remaining self.async_write_ha_state() self.hass.bus.async_fire(EVENT_TIMER_CHANGED, {ATTR_ENTITY_ID: self.entity_id}) self._listener = async_track_point_in_utc_time( diff --git a/homeassistant/components/timer/icons.json b/homeassistant/components/timer/icons.json index 1e352f7280b2ca..a5319688646b9f 100644 --- a/homeassistant/components/timer/icons.json +++ b/homeassistant/components/timer/icons.json @@ -1,10 +1,22 @@ { "services": { - "start": "mdi:play", - "pause": "mdi:pause", - "cancel": "mdi:cancel", - "finish": "mdi:check", - "change": "mdi:pencil", - "reload": "mdi:reload" + "start": { + "service": "mdi:play" + }, + "pause": { + "service": "mdi:pause" + }, + "cancel": { + "service": "mdi:cancel" + }, + "finish": { + "service": "mdi:check" + }, + "change": { + "service": "mdi:pencil" + }, + "reload": { + "service": "mdi:reload" + } } } diff --git a/homeassistant/components/todo/icons.json b/homeassistant/components/todo/icons.json index 05c9af7463095f..4040a0c6b8f909 100644 --- a/homeassistant/components/todo/icons.json +++ b/homeassistant/components/todo/icons.json @@ -5,10 +5,20 @@ } }, "services": { - "add_item": "mdi:clipboard-plus", - "get_items": "mdi:clipboard-arrow-down", - "remove_completed_items": "mdi:clipboard-remove", - "remove_item": "mdi:clipboard-minus", - "update_item": "mdi:clipboard-edit" + "add_item": { + "service": "mdi:clipboard-plus" + }, + "get_items": { + "service": "mdi:clipboard-arrow-down" + }, + "remove_completed_items": { + "service": "mdi:clipboard-remove" + }, + "remove_item": { + "service": "mdi:clipboard-minus" + }, + "update_item": { + "service": "mdi:clipboard-edit" + } } } diff --git a/homeassistant/components/todoist/icons.json b/homeassistant/components/todoist/icons.json index d3b881d480c565..73778f1ca23e7e 100644 --- a/homeassistant/components/todoist/icons.json +++ b/homeassistant/components/todoist/icons.json @@ -1,5 +1,7 @@ { "services": { - "new_task": "mdi:checkbox-marked-circle-plus-outline" + "new_task": { + "service": "mdi:checkbox-marked-circle-plus-outline" + } } } diff --git a/homeassistant/components/toon/config_flow.py b/homeassistant/components/toon/config_flow.py index 40e83c3c9bea61..af9f7b06850903 100644 --- a/homeassistant/components/toon/config_flow.py +++ b/homeassistant/components/toon/config_flow.py @@ -48,7 +48,7 @@ async def async_oauth_create_entry(self, data: dict[str, Any]) -> ConfigFlowResu return await self.async_step_agreement() async def async_step_import( - self, config: dict[str, Any] | None = None + self, import_data: dict[str, Any] | None ) -> ConfigFlowResult: """Start a configuration flow based on imported data. @@ -57,8 +57,8 @@ async def async_step_import( the version 1 schema. """ - if config is not None and CONF_MIGRATE in config: - self.context.update({CONF_MIGRATE: config[CONF_MIGRATE]}) + if import_data is not None and CONF_MIGRATE in import_data: + self.context.update({CONF_MIGRATE: import_data[CONF_MIGRATE]}) else: await self._async_handle_discovery_without_unique_id() diff --git a/homeassistant/components/toon/icons.json b/homeassistant/components/toon/icons.json index 650bf0b6d19f57..217f1240893477 100644 --- a/homeassistant/components/toon/icons.json +++ b/homeassistant/components/toon/icons.json @@ -1,5 +1,7 @@ { "services": { - "update": "mdi:update" + "update": { + "service": "mdi:update" + } } } diff --git a/homeassistant/components/totalconnect/config_flow.py b/homeassistant/components/totalconnect/config_flow.py index 19d8f09933ef1a..63973fd44e936e 100644 --- a/homeassistant/components/totalconnect/config_flow.py +++ b/homeassistant/components/totalconnect/config_flow.py @@ -28,14 +28,16 @@ class TotalConnectConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - def __init__(self): + def __init__(self) -> None: """Initialize the config flow.""" self.username = None self.password = None - self.usercodes = {} + self.usercodes: dict[str, Any] = {} self.client = None - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle a flow initiated by the user.""" errors = {} diff --git a/homeassistant/components/totalconnect/icons.json b/homeassistant/components/totalconnect/icons.json index cb62a79c7bba8f..a21df03e15d18b 100644 --- a/homeassistant/components/totalconnect/icons.json +++ b/homeassistant/components/totalconnect/icons.json @@ -10,7 +10,11 @@ } }, "services": { - "arm_away_instant": "mdi:shield-lock", - "arm_home_instant": "mdi:shield-home" + "arm_away_instant": { + "service": "mdi:shield-lock" + }, + "arm_home_instant": { + "service": "mdi:shield-home" + } } } diff --git a/homeassistant/components/touchline_sl/__init__.py b/homeassistant/components/touchline_sl/__init__.py new file mode 100644 index 00000000000000..45a851856731e5 --- /dev/null +++ b/homeassistant/components/touchline_sl/__init__.py @@ -0,0 +1,63 @@ +"""The Roth Touchline SL integration.""" + +from __future__ import annotations + +import asyncio + +from pytouchlinesl import TouchlineSL + +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers import device_registry as dr + +from .const import DOMAIN +from .coordinator import TouchlineSLModuleCoordinator + +PLATFORMS: list[Platform] = [Platform.CLIMATE] + +type TouchlineSLConfigEntry = ConfigEntry[list[TouchlineSLModuleCoordinator]] + + +async def async_setup_entry(hass: HomeAssistant, entry: TouchlineSLConfigEntry) -> bool: + """Set up Roth Touchline SL from a config entry.""" + account = TouchlineSL( + username=entry.data[CONF_USERNAME], password=entry.data[CONF_PASSWORD] + ) + + coordinators: list[TouchlineSLModuleCoordinator] = [ + TouchlineSLModuleCoordinator(hass, module) for module in await account.modules() + ] + + await asyncio.gather( + *[ + coordinator.async_config_entry_first_refresh() + for coordinator in coordinators + ] + ) + + device_registry = dr.async_get(hass) + + # Create a new Device for each coorodinator to represent each module + for c in coordinators: + module = c.data.module + device_registry.async_get_or_create( + config_entry_id=entry.entry_id, + identifiers={(DOMAIN, module.id)}, + name=module.name, + manufacturer="Roth", + model=module.type, + sw_version=module.version, + ) + + entry.runtime_data = coordinators + await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) + + return True + + +async def async_unload_entry( + hass: HomeAssistant, entry: TouchlineSLConfigEntry +) -> bool: + """Unload a config entry.""" + return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) diff --git a/homeassistant/components/touchline_sl/climate.py b/homeassistant/components/touchline_sl/climate.py new file mode 100644 index 00000000000000..93328823749b99 --- /dev/null +++ b/homeassistant/components/touchline_sl/climate.py @@ -0,0 +1,126 @@ +"""Roth Touchline SL climate integration implementation for Home Assistant.""" + +from typing import Any + +from pytouchlinesl import Zone + +from homeassistant.components.climate import ( + ClimateEntity, + ClimateEntityFeature, + HVACMode, +) +from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature +from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers.device_registry import DeviceInfo +from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.update_coordinator import CoordinatorEntity + +from . import TouchlineSLConfigEntry +from .const import DOMAIN +from .coordinator import TouchlineSLModuleCoordinator + + +async def async_setup_entry( + hass: HomeAssistant, + entry: TouchlineSLConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up the Touchline devices.""" + coordinators = entry.runtime_data + async_add_entities( + TouchlineSLZone(coordinator=coordinator, zone_id=zone_id) + for coordinator in coordinators + for zone_id in coordinator.data.zones + ) + + +CONSTANT_TEMPERATURE = "constant_temperature" + + +class TouchlineSLZone(CoordinatorEntity[TouchlineSLModuleCoordinator], ClimateEntity): + """Roth Touchline SL Zone.""" + + _attr_has_entity_name = True + _attr_hvac_mode = HVACMode.HEAT + _attr_hvac_modes = [HVACMode.HEAT] + _attr_name = None + _attr_supported_features = ( + ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.PRESET_MODE + ) + _attr_temperature_unit = UnitOfTemperature.CELSIUS + _attr_translation_key = "zone" + + def __init__(self, coordinator: TouchlineSLModuleCoordinator, zone_id: int) -> None: + """Construct a Touchline SL climate zone.""" + super().__init__(coordinator) + self.zone_id: int = zone_id + + self._attr_unique_id = ( + f"module-{self.coordinator.data.module.id}-zone-{self.zone_id}" + ) + + self._attr_device_info = DeviceInfo( + identifiers={(DOMAIN, str(zone_id))}, + name=self.zone.name, + manufacturer="Roth", + via_device=(DOMAIN, coordinator.data.module.id), + model="zone", + suggested_area=self.zone.name, + ) + + # Call this in __init__ so data is populated right away, since it's + # already available in the coordinator data. + self.set_attr() + + @callback + def _handle_coordinator_update(self) -> None: + """Handle updated data from the coordinator.""" + self.set_attr() + super()._handle_coordinator_update() + + @property + def zone(self) -> Zone: + """Return the device object from the coordinator data.""" + return self.coordinator.data.zones[self.zone_id] + + @property + def available(self) -> bool: + """Return if the device is available.""" + return super().available and self.zone_id in self.coordinator.data.zones + + async def async_set_temperature(self, **kwargs: Any) -> None: + """Set new target temperature.""" + if (temperature := kwargs.get(ATTR_TEMPERATURE)) is None: + return + + await self.zone.set_temperature(temperature) + await self.coordinator.async_request_refresh() + + async def async_set_preset_mode(self, preset_mode: str) -> None: + """Assign the zone to a particular global schedule.""" + if not self.zone: + return + + if preset_mode == CONSTANT_TEMPERATURE and self._attr_target_temperature: + await self.zone.set_temperature(temperature=self._attr_target_temperature) + await self.coordinator.async_request_refresh() + return + + if schedule := self.coordinator.data.schedules[preset_mode]: + await self.zone.set_schedule(schedule_id=schedule.id) + await self.coordinator.async_request_refresh() + + def set_attr(self) -> None: + """Populate attributes with data from the coordinator.""" + schedule_names = self.coordinator.data.schedules.keys() + + self._attr_current_temperature = self.zone.temperature + self._attr_target_temperature = self.zone.target_temperature + self._attr_current_humidity = int(self.zone.humidity) + self._attr_preset_modes = [*schedule_names, CONSTANT_TEMPERATURE] + + if self.zone.mode == "constantTemp": + self._attr_preset_mode = CONSTANT_TEMPERATURE + elif self.zone.mode == "globalSchedule": + schedule = self.zone.schedule + self._attr_preset_mode = schedule.name diff --git a/homeassistant/components/touchline_sl/config_flow.py b/homeassistant/components/touchline_sl/config_flow.py new file mode 100644 index 00000000000000..91d959b5a0a9a4 --- /dev/null +++ b/homeassistant/components/touchline_sl/config_flow.py @@ -0,0 +1,62 @@ +"""Config flow for Roth Touchline SL integration.""" + +from __future__ import annotations + +import logging +from typing import Any + +from pytouchlinesl import TouchlineSL +from pytouchlinesl.client import RothAPIError +import voluptuous as vol + +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult +from homeassistant.const import CONF_PASSWORD, CONF_USERNAME + +from .const import DOMAIN + +_LOGGER = logging.getLogger(__name__) + +STEP_USER_DATA_SCHEMA = vol.Schema( + { + vol.Required(CONF_USERNAME): str, + vol.Required(CONF_PASSWORD): str, + } +) + + +class TouchlineSLConfigFlow(ConfigFlow, domain=DOMAIN): + """Handle a config flow for Roth Touchline SL.""" + + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle the initial step that gathers username and password.""" + errors: dict[str, str] = {} + + if user_input is not None: + try: + account = TouchlineSL( + username=user_input[CONF_USERNAME], + password=user_input[CONF_PASSWORD], + ) + await account.user_id() + except RothAPIError as e: + if e.status == 401: + errors["base"] = "invalid_auth" + else: + errors["base"] = "cannot_connect" + except Exception: + _LOGGER.exception("Unexpected exception") + errors["base"] = "unknown" + else: + unique_account_id = await account.user_id() + await self.async_set_unique_id(str(unique_account_id)) + self._abort_if_unique_id_configured() + + return self.async_create_entry( + title=user_input[CONF_USERNAME], data=user_input + ) + + return self.async_show_form( + step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors + ) diff --git a/homeassistant/components/touchline_sl/const.py b/homeassistant/components/touchline_sl/const.py new file mode 100644 index 00000000000000..e441e3721b378f --- /dev/null +++ b/homeassistant/components/touchline_sl/const.py @@ -0,0 +1,3 @@ +"""Constants for the Roth Touchline SL integration.""" + +DOMAIN = "touchline_sl" diff --git a/homeassistant/components/touchline_sl/coordinator.py b/homeassistant/components/touchline_sl/coordinator.py new file mode 100644 index 00000000000000..cd74ba6130f032 --- /dev/null +++ b/homeassistant/components/touchline_sl/coordinator.py @@ -0,0 +1,59 @@ +"""Define an object to manage fetching Touchline SL data.""" + +from __future__ import annotations + +from dataclasses import dataclass +from datetime import timedelta +import logging + +from pytouchlinesl import Module, Zone +from pytouchlinesl.client import RothAPIError +from pytouchlinesl.client.models import GlobalScheduleModel + +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import ConfigEntryAuthFailed +from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed + +_LOGGER = logging.getLogger(__name__) + + +@dataclass +class TouchlineSLModuleData: + """Provide type safe way of accessing module data from the coordinator.""" + + module: Module + zones: dict[int, Zone] + schedules: dict[str, GlobalScheduleModel] + + +class TouchlineSLModuleCoordinator(DataUpdateCoordinator[TouchlineSLModuleData]): + """A coordinator to manage the fetching of Touchline SL data.""" + + def __init__(self, hass: HomeAssistant, module: Module) -> None: + """Initialize coordinator.""" + super().__init__( + hass, + logger=_LOGGER, + name=f"Touchline SL ({module.name})", + update_interval=timedelta(seconds=30), + ) + + self.module = module + + async def _async_update_data(self) -> TouchlineSLModuleData: + """Fetch data from the upstream API and pre-process into the right format.""" + try: + zones = await self.module.zones() + schedules = await self.module.schedules() + except RothAPIError as error: + if error.status == 401: + # Trigger a reauthentication if the data update fails due to + # bad authentication. + raise ConfigEntryAuthFailed from error + raise UpdateFailed(error) from error + + return TouchlineSLModuleData( + module=self.module, + zones={z.id: z for z in zones}, + schedules={s.name: s for s in schedules}, + ) diff --git a/homeassistant/components/touchline_sl/manifest.json b/homeassistant/components/touchline_sl/manifest.json new file mode 100644 index 00000000000000..8a50b06d613e10 --- /dev/null +++ b/homeassistant/components/touchline_sl/manifest.json @@ -0,0 +1,10 @@ +{ + "domain": "touchline_sl", + "name": "Roth Touchline SL", + "codeowners": ["@jnsgruk"], + "config_flow": true, + "documentation": "https://www.home-assistant.io/integrations/touchline_sl", + "integration_type": "hub", + "iot_class": "cloud_polling", + "requirements": ["pytouchlinesl==0.1.5"] +} diff --git a/homeassistant/components/touchline_sl/strings.json b/homeassistant/components/touchline_sl/strings.json new file mode 100644 index 00000000000000..e3a0ef5a741914 --- /dev/null +++ b/homeassistant/components/touchline_sl/strings.json @@ -0,0 +1,36 @@ +{ + "config": { + "flow_title": "Touchline SL Setup Flow", + "error": { + "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", + "invalid_auth": "[%key:common::config_flow::error::invalid_auth%]", + "unknown": "[%key:common::config_flow::error::unknown%]" + }, + "step": { + "user": { + "title": "Login to Touchline SL", + "description": "Your credentials for the Roth Touchline SL mobile app/web service", + "data": { + "username": "[%key:common::config_flow::data::username%]", + "password": "[%key:common::config_flow::data::password%]" + } + } + }, + "abort": { + "already_configured": "[%key:common::config_flow::abort::already_configured_device%]" + } + }, + "entity": { + "climate": { + "zone": { + "state_attributes": { + "preset_mode": { + "state": { + "constant_temperature": "Constant temperature" + } + } + } + } + } + } +} diff --git a/homeassistant/components/tplink/config_flow.py b/homeassistant/components/tplink/config_flow.py index a0f0ca6eb76e75..1c02466aef1c23 100644 --- a/homeassistant/components/tplink/config_flow.py +++ b/homeassistant/components/tplink/config_flow.py @@ -410,9 +410,18 @@ async def _async_try_discover_and_update( self._discovered_device = await Discover.discover_single( host, credentials=credentials ) - except TimeoutError: - # Try connect() to legacy devices if discovery fails - self._discovered_device = await Device.connect(config=DeviceConfig(host)) + except TimeoutError as ex: + # Try connect() to legacy devices if discovery fails. This is a + # fallback mechanism for legacy that can handle connections without + # discovery info but if it fails raise the original error which is + # applicable for newer devices. + try: + self._discovered_device = await Device.connect( + config=DeviceConfig(host) + ) + except Exception: # noqa: BLE001 + # Raise the original error instead of the fallback error + raise ex from ex else: if self._discovered_device.config.uses_http: self._discovered_device.config.http_client = ( diff --git a/homeassistant/components/tplink/entity.py b/homeassistant/components/tplink/entity.py index 4ec0480cf82e66..beb71d4e5cedec 100644 --- a/homeassistant/components/tplink/entity.py +++ b/homeassistant/components/tplink/entity.py @@ -68,6 +68,8 @@ # update "current_firmware_version", "available_firmware_version", + "update_available", + "check_latest_firmware", } diff --git a/homeassistant/components/tplink/icons.json b/homeassistant/components/tplink/icons.json index 3da3b4806d341f..96ea8f41bb7f0d 100644 --- a/homeassistant/components/tplink/icons.json +++ b/homeassistant/components/tplink/icons.json @@ -109,7 +109,11 @@ } }, "services": { - "sequence_effect": "mdi:playlist-play", - "random_effect": "mdi:shuffle-variant" + "sequence_effect": { + "service": "mdi:playlist-play" + }, + "random_effect": { + "service": "mdi:shuffle-variant" + } } } diff --git a/homeassistant/components/tplink/manifest.json b/homeassistant/components/tplink/manifest.json index 10b0ef61153b78..0d9761ec8ce5b4 100644 --- a/homeassistant/components/tplink/manifest.json +++ b/homeassistant/components/tplink/manifest.json @@ -301,5 +301,5 @@ "iot_class": "local_polling", "loggers": ["kasa"], "quality_scale": "platinum", - "requirements": ["python-kasa[speedups]==0.7.1"] + "requirements": ["python-kasa[speedups]==0.7.2"] } diff --git a/homeassistant/components/tplink/sensor.py b/homeassistant/components/tplink/sensor.py index 3da414d74d3f2d..1307079937f0fb 100644 --- a/homeassistant/components/tplink/sensor.py +++ b/homeassistant/components/tplink/sensor.py @@ -154,7 +154,5 @@ def _async_update_attrs(self) -> None: self._attr_native_value = value # Map to homeassistant units and fallback to upstream one if none found - if self._feature.unit is not None: - self._attr_native_unit_of_measurement = UNIT_MAPPING.get( - self._feature.unit, self._feature.unit - ) + if (unit := self._feature.unit) is not None: + self._attr_native_unit_of_measurement = UNIT_MAPPING.get(unit, unit) diff --git a/homeassistant/components/traccar_server/config_flow.py b/homeassistant/components/traccar_server/config_flow.py index 45a43c08685043..a4d109030ae61f 100644 --- a/homeassistant/components/traccar_server/config_flow.py +++ b/homeassistant/components/traccar_server/config_flow.py @@ -2,7 +2,6 @@ from __future__ import annotations -from collections.abc import Mapping from typing import Any from pytraccar import ApiClient, ServerModel, TraccarException @@ -161,36 +160,34 @@ async def async_step_user( errors=errors, ) - async def async_step_import( - self, import_info: Mapping[str, Any] - ) -> ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Import an entry.""" - configured_port = str(import_info[CONF_PORT]) + configured_port = str(import_data[CONF_PORT]) self._async_abort_entries_match( { - CONF_HOST: import_info[CONF_HOST], + CONF_HOST: import_data[CONF_HOST], CONF_PORT: configured_port, } ) - if "all_events" in (imported_events := import_info.get("event", [])): + if "all_events" in (imported_events := import_data.get("event", [])): events = list(EVENTS.values()) else: events = imported_events return self.async_create_entry( - title=f"{import_info[CONF_HOST]}:{configured_port}", + title=f"{import_data[CONF_HOST]}:{configured_port}", data={ - CONF_HOST: import_info[CONF_HOST], + CONF_HOST: import_data[CONF_HOST], CONF_PORT: configured_port, - CONF_SSL: import_info.get(CONF_SSL, False), - CONF_VERIFY_SSL: import_info.get(CONF_VERIFY_SSL, True), - CONF_USERNAME: import_info[CONF_USERNAME], - CONF_PASSWORD: import_info[CONF_PASSWORD], + CONF_SSL: import_data.get(CONF_SSL, False), + CONF_VERIFY_SSL: import_data.get(CONF_VERIFY_SSL, True), + CONF_USERNAME: import_data[CONF_USERNAME], + CONF_PASSWORD: import_data[CONF_PASSWORD], }, options={ - CONF_MAX_ACCURACY: import_info[CONF_MAX_ACCURACY], + CONF_MAX_ACCURACY: import_data[CONF_MAX_ACCURACY], CONF_EVENTS: events, - CONF_CUSTOM_ATTRIBUTES: import_info.get("monitored_conditions", []), - CONF_SKIP_ACCURACY_FILTER_FOR: import_info.get( + CONF_CUSTOM_ATTRIBUTES: import_data.get("monitored_conditions", []), + CONF_SKIP_ACCURACY_FILTER_FOR: import_data.get( "skip_accuracy_filter_on", [] ), }, diff --git a/homeassistant/components/transmission/icons.json b/homeassistant/components/transmission/icons.json index 56ae46f933df71..4458f5109514b6 100644 --- a/homeassistant/components/transmission/icons.json +++ b/homeassistant/components/transmission/icons.json @@ -1,8 +1,16 @@ { "services": { - "add_torrent": "mdi:download", - "remove_torrent": "mdi:download-off", - "start_torrent": "mdi:play", - "stop_torrent": "mdi:stop" + "add_torrent": { + "service": "mdi:download" + }, + "remove_torrent": { + "service": "mdi:download-off" + }, + "start_torrent": { + "service": "mdi:play" + }, + "stop_torrent": { + "service": "mdi:stop" + } } } diff --git a/homeassistant/components/trend/icons.json b/homeassistant/components/trend/icons.json index a03163179cb5bd..a9829425570a0c 100644 --- a/homeassistant/components/trend/icons.json +++ b/homeassistant/components/trend/icons.json @@ -1,5 +1,7 @@ { "services": { - "reload": "mdi:reload" + "reload": { + "service": "mdi:reload" + } } } diff --git a/homeassistant/components/tts/__init__.py b/homeassistant/components/tts/__init__.py index 5286b01f67f6d1..9e3d9f65a768b0 100644 --- a/homeassistant/components/tts/__init__.py +++ b/homeassistant/components/tts/__init__.py @@ -77,6 +77,7 @@ "ATTR_PREFERRED_FORMAT", "ATTR_PREFERRED_SAMPLE_RATE", "ATTR_PREFERRED_SAMPLE_CHANNELS", + "ATTR_PREFERRED_SAMPLE_BYTES", "CONF_LANG", "DEFAULT_CACHE_DIR", "generate_media_source_id", @@ -95,6 +96,7 @@ ATTR_PREFERRED_FORMAT = "preferred_format" ATTR_PREFERRED_SAMPLE_RATE = "preferred_sample_rate" ATTR_PREFERRED_SAMPLE_CHANNELS = "preferred_sample_channels" +ATTR_PREFERRED_SAMPLE_BYTES = "preferred_sample_bytes" ATTR_MEDIA_PLAYER_ENTITY_ID = "media_player_entity_id" ATTR_VOICE = "voice" @@ -103,6 +105,7 @@ ATTR_PREFERRED_FORMAT, ATTR_PREFERRED_SAMPLE_RATE, ATTR_PREFERRED_SAMPLE_CHANNELS, + ATTR_PREFERRED_SAMPLE_BYTES, } CONF_LANG = "language" @@ -137,15 +140,16 @@ def async_default_engine(hass: HomeAssistant) -> str | None: component: EntityComponent[TextToSpeechEntity] = hass.data[DOMAIN] manager: SpeechManager = hass.data[DATA_TTS_MANAGER] - if "cloud" in manager.providers: - return "cloud" + default_entity_id: str | None = None - entity = next(iter(component.entities), None) + for entity in component.entities: + if entity.platform and entity.platform.platform_name == "cloud": + return entity.entity_id - if entity is not None: - return entity.entity_id + if default_entity_id is None: + default_entity_id = entity.entity_id - return next(iter(manager.providers), None) + return default_entity_id or next(iter(manager.providers), None) @callback @@ -222,6 +226,7 @@ async def async_convert_audio( to_extension: str, to_sample_rate: int | None = None, to_sample_channels: int | None = None, + to_sample_bytes: int | None = None, ) -> bytes: """Convert audio to a preferred format using ffmpeg.""" ffmpeg_manager = ffmpeg.get_ffmpeg_manager(hass) @@ -233,6 +238,7 @@ async def async_convert_audio( to_extension, to_sample_rate=to_sample_rate, to_sample_channels=to_sample_channels, + to_sample_bytes=to_sample_bytes, ) ) @@ -244,6 +250,7 @@ def _convert_audio( to_extension: str, to_sample_rate: int | None = None, to_sample_channels: int | None = None, + to_sample_bytes: int | None = None, ) -> bytes: """Convert audio to a preferred format using ffmpeg.""" @@ -276,6 +283,10 @@ def _convert_audio( # Max quality for MP3 command.extend(["-q:a", "0"]) + if to_sample_bytes == 2: + # 16-bit samples + command.extend(["-sample_fmt", "s16"]) + command.append(output_file.name) with subprocess.Popen( @@ -737,11 +748,25 @@ async def _async_get_tts_audio( else: sample_rate = options.pop(ATTR_PREFERRED_SAMPLE_RATE, None) + if sample_rate is not None: + sample_rate = int(sample_rate) + if ATTR_PREFERRED_SAMPLE_CHANNELS in supported_options: sample_channels = options.get(ATTR_PREFERRED_SAMPLE_CHANNELS) else: sample_channels = options.pop(ATTR_PREFERRED_SAMPLE_CHANNELS, None) + if sample_channels is not None: + sample_channels = int(sample_channels) + + if ATTR_PREFERRED_SAMPLE_BYTES in supported_options: + sample_bytes = options.get(ATTR_PREFERRED_SAMPLE_BYTES) + else: + sample_bytes = options.pop(ATTR_PREFERRED_SAMPLE_BYTES, None) + + if sample_bytes is not None: + sample_bytes = int(sample_bytes) + async def get_tts_data() -> str: """Handle data available.""" if engine_instance.name is None or engine_instance.name is UNDEFINED: @@ -768,6 +793,7 @@ async def get_tts_data() -> str: (final_extension != extension) or (sample_rate is not None) or (sample_channels is not None) + or (sample_bytes is not None) ) if needs_conversion: @@ -778,6 +804,7 @@ async def get_tts_data() -> str: to_extension=final_extension, to_sample_rate=sample_rate, to_sample_channels=sample_channels, + to_sample_bytes=sample_bytes, ) # Create file infos @@ -1085,6 +1112,7 @@ def websocket_list_engines( language = msg.get("language") providers = [] provider_info: dict[str, Any] + entity_domains: set[str] = set() for entity in component.entities: provider_info = { @@ -1096,15 +1124,20 @@ def websocket_list_engines( language, entity.supported_languages, country ) providers.append(provider_info) + if entity.platform: + entity_domains.add(entity.platform.platform_name) for engine_id, provider in manager.providers.items(): provider_info = { "engine_id": engine_id, + "name": provider.name, "supported_languages": provider.supported_languages, } if language: provider_info["supported_languages"] = language_util.matches( language, provider.supported_languages, country ) + if engine_id in entity_domains: + provider_info["deprecated"] = True providers.append(provider_info) connection.send_message( @@ -1147,6 +1180,8 @@ def websocket_get_engine( "engine_id": engine_id, "supported_languages": provider.supported_languages, } + if isinstance(provider, Provider): + provider_info["name"] = provider.name connection.send_message( websocket_api.result_message(msg["id"], {"provider": provider_info}) diff --git a/homeassistant/components/tts/icons.json b/homeassistant/components/tts/icons.json index cda5f877b25900..8cfae7cc8e9d06 100644 --- a/homeassistant/components/tts/icons.json +++ b/homeassistant/components/tts/icons.json @@ -5,8 +5,14 @@ } }, "services": { - "clear_cache": "mdi:delete", - "say": "mdi:speaker-message", - "speak": "mdi:speaker-message" + "clear_cache": { + "service": "mdi:delete" + }, + "say": { + "service": "mdi:speaker-message" + }, + "speak": { + "service": "mdi:speaker-message" + } } } diff --git a/homeassistant/components/tuya/config_flow.py b/homeassistant/components/tuya/config_flow.py index bdef321de7ac48..104c3b7c9fa174 100644 --- a/homeassistant/components/tuya/config_flow.py +++ b/homeassistant/components/tuya/config_flow.py @@ -146,7 +146,9 @@ async def async_step_scan( data=entry_data, ) - async def async_step_reauth(self, _: Mapping[str, Any]) -> ConfigFlowResult: + async def async_step_reauth( + self, entry_data: Mapping[str, Any] + ) -> ConfigFlowResult: """Handle initiation of re-authentication with Tuya.""" self.__reauth_entry = self.hass.config_entries.async_get_entry( self.context["entry_id"] diff --git a/homeassistant/components/tuya/const.py b/homeassistant/components/tuya/const.py index 55af95f0d34e6b..eb56761d26a14d 100644 --- a/homeassistant/components/tuya/const.py +++ b/homeassistant/components/tuya/const.py @@ -96,6 +96,7 @@ class DPCode(StrEnum): """ AIR_QUALITY = "air_quality" + AIR_QUALITY_INDEX = "air_quality_index" ALARM_SWITCH = "alarm_switch" # Alarm switch ALARM_TIME = "alarm_time" # Alarm time ALARM_VOLUME = "alarm_volume" # Alarm volume diff --git a/homeassistant/components/tuya/icons.json b/homeassistant/components/tuya/icons.json index 48ae61f36fd405..e28371f2b3dc2f 100644 --- a/homeassistant/components/tuya/icons.json +++ b/homeassistant/components/tuya/icons.json @@ -236,6 +236,9 @@ }, "air_quality": { "default": "mdi:air-filter" + }, + "air_quality_index": { + "default": "mdi:air-filter" } }, "switch": { diff --git a/homeassistant/components/tuya/sensor.py b/homeassistant/components/tuya/sensor.py index 1ab3ea700d7665..4f3c609937726a 100644 --- a/homeassistant/components/tuya/sensor.py +++ b/homeassistant/components/tuya/sensor.py @@ -264,8 +264,12 @@ class TuyaSensorEntityDescription(SensorEntityDescription): ), ), # Air Quality Monitor - # No specification on Tuya portal + # https://developer.tuya.com/en/docs/iot/hjjcy?id=Kbeoad8y1nnlv "hjjcy": ( + TuyaSensorEntityDescription( + key=DPCode.AIR_QUALITY_INDEX, + translation_key="air_quality_index", + ), TuyaSensorEntityDescription( key=DPCode.TEMP_CURRENT, translation_key="temperature", @@ -301,6 +305,13 @@ class TuyaSensorEntityDescription(SensorEntityDescription): device_class=SensorDeviceClass.PM25, state_class=SensorStateClass.MEASUREMENT, ), + TuyaSensorEntityDescription( + key=DPCode.PM10, + translation_key="pm10", + device_class=SensorDeviceClass.PM10, + state_class=SensorStateClass.MEASUREMENT, + ), + *BATTERY_SENSORS, ), # Formaldehyde Detector # Note: Not documented diff --git a/homeassistant/components/tuya/strings.json b/homeassistant/components/tuya/strings.json index 6b699c0ffc0af7..865fbaffbbea15 100644 --- a/homeassistant/components/tuya/strings.json +++ b/homeassistant/components/tuya/strings.json @@ -620,6 +620,17 @@ "good": "Good", "severe": "Severe" } + }, + "air_quality_index": { + "name": "Air quality index", + "state": { + "level_1": "Level 1", + "level_2": "Level 2", + "level_3": "Level 3", + "level_4": "Level 4", + "level_5": "Level 5", + "level_6": "Level 6" + } } }, "switch": { diff --git a/homeassistant/components/twinkly/config_flow.py b/homeassistant/components/twinkly/config_flow.py index 98802c8bd33cca..68c455dc619007 100644 --- a/homeassistant/components/twinkly/config_flow.py +++ b/homeassistant/components/twinkly/config_flow.py @@ -28,7 +28,9 @@ def __init__(self) -> None: """Initialize the config flow.""" self._discovered_device: tuple[dict[str, Any], str] | None = None - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle config steps.""" host = user_input[CONF_HOST] if user_input else None diff --git a/homeassistant/components/ukraine_alarm/config_flow.py b/homeassistant/components/ukraine_alarm/config_flow.py index bafe6d1fe11086..12059124fa2f32 100644 --- a/homeassistant/components/ukraine_alarm/config_flow.py +++ b/homeassistant/components/ukraine_alarm/config_flow.py @@ -3,12 +3,13 @@ from __future__ import annotations import logging +from typing import TYPE_CHECKING, Any import aiohttp from uasiren.client import Client import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_NAME, CONF_REGION from homeassistant.helpers.aiohttp_client import async_get_clientsession @@ -22,12 +23,14 @@ class UkraineAlarmConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - def __init__(self): + def __init__(self) -> None: """Initialize a new UkraineAlarmConfigFlow.""" - self.states = None - self.selected_region = None + self.states: list[dict[str, Any]] | None = None + self.selected_region: dict[str, Any] | None = None - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle a flow initialized by the user.""" if len(self._async_current_entries()) == 5: @@ -66,17 +69,25 @@ async def async_step_user(self, user_input=None): return await self._handle_pick_region("user", "district", user_input) - async def async_step_district(self, user_input=None): + async def async_step_district( + self, user_input: dict[str, str] | None = None + ) -> ConfigFlowResult: """Handle user-chosen district.""" return await self._handle_pick_region("district", "community", user_input) - async def async_step_community(self, user_input=None): + async def async_step_community( + self, user_input: dict[str, str] | None = None + ) -> ConfigFlowResult: """Handle user-chosen community.""" return await self._handle_pick_region("community", None, user_input, True) async def _handle_pick_region( - self, step_id: str, next_step: str | None, user_input, last_step=False - ): + self, + step_id: str, + next_step: str | None, + user_input: dict[str, str] | None, + last_step: bool = False, + ) -> ConfigFlowResult: """Handle picking a (sub)region.""" if self.selected_region: source = self.selected_region["regionChildIds"] @@ -91,7 +102,11 @@ async def _handle_pick_region( ): self.selected_region = _find(source, user_input[CONF_REGION]) - if next_step and self.selected_region["regionChildIds"]: + if ( + next_step + and self.selected_region + and self.selected_region["regionChildIds"] + ): return await getattr(self, f"async_step_{next_step}")() return await self._async_finish_flow() @@ -114,8 +129,10 @@ async def _handle_pick_region( step_id=step_id, data_schema=schema, last_step=last_step ) - async def _async_finish_flow(self): + async def _async_finish_flow(self) -> ConfigFlowResult: """Finish the setup.""" + if TYPE_CHECKING: + assert self.selected_region is not None await self.async_set_unique_id(self.selected_region["regionId"]) self._abort_if_unique_id_configured() @@ -128,10 +145,10 @@ async def _async_finish_flow(self): ) -def _find(regions, region_id): +def _find(regions: list[dict[str, Any]], region_id): return next((region for region in regions if region["regionId"] == region_id), None) -def _make_regions_object(regions): +def _make_regions_object(regions: list[dict[str, Any]]) -> dict[str, str]: regions = sorted(regions, key=lambda region: region["regionName"].lower()) return {region["regionId"]: region["regionName"] for region in regions} diff --git a/homeassistant/components/unifi/icons.json b/homeassistant/components/unifi/icons.json index 2d5017a318707d..b089d8eff9cb02 100644 --- a/homeassistant/components/unifi/icons.json +++ b/homeassistant/components/unifi/icons.json @@ -1,6 +1,10 @@ { "services": { - "reconnect_client": "mdi:sync", - "remove_clients": "mdi:delete" + "reconnect_client": { + "service": "mdi:sync" + }, + "remove_clients": { + "service": "mdi:delete" + } } } diff --git a/homeassistant/components/unifiprotect/icons.json b/homeassistant/components/unifiprotect/icons.json index bb713d4ee7903e..5e80e3095b31f8 100644 --- a/homeassistant/components/unifiprotect/icons.json +++ b/homeassistant/components/unifiprotect/icons.json @@ -1,8 +1,16 @@ { "services": { - "add_doorbell_text": "mdi:message-plus", - "remove_doorbell_text": "mdi:message-minus", - "set_chime_paired_doorbells": "mdi:bell-cog", - "remove_privacy_zone": "mdi:eye-minus" + "add_doorbell_text": { + "service": "mdi:message-plus" + }, + "remove_doorbell_text": { + "service": "mdi:message-minus" + }, + "set_chime_paired_doorbells": { + "service": "mdi:bell-cog" + }, + "remove_privacy_zone": { + "service": "mdi:eye-minus" + } } } diff --git a/homeassistant/components/universal/icons.json b/homeassistant/components/universal/icons.json index a03163179cb5bd..a9829425570a0c 100644 --- a/homeassistant/components/universal/icons.json +++ b/homeassistant/components/universal/icons.json @@ -1,5 +1,7 @@ { "services": { - "reload": "mdi:reload" + "reload": { + "service": "mdi:reload" + } } } diff --git a/homeassistant/components/upb/config_flow.py b/homeassistant/components/upb/config_flow.py index fec93a51202630..d9f111049fd909 100644 --- a/homeassistant/components/upb/config_flow.py +++ b/homeassistant/components/upb/config_flow.py @@ -3,12 +3,13 @@ import asyncio from contextlib import suppress import logging +from typing import Any from urllib.parse import urlparse import upb_lib import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_ADDRESS, CONF_FILE_PATH, CONF_HOST, CONF_PROTOCOL from homeassistant.exceptions import HomeAssistantError @@ -78,11 +79,9 @@ class UPBConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - def __init__(self) -> None: - """Initialize the UPB config flow.""" - self.importing = False - - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" errors = {} if user_input is not None: @@ -102,9 +101,6 @@ async def async_step_user(self, user_input=None): await self.async_set_unique_id(network_id) self._abort_if_unique_id_configured() - if self.importing: - return self.async_create_entry(title=info["title"], data=user_input) - return self.async_create_entry( title=info["title"], data={ @@ -117,11 +113,6 @@ async def async_step_user(self, user_input=None): step_id="user", data_schema=DATA_SCHEMA, errors=errors ) - async def async_step_import(self, user_input): - """Handle import.""" - self.importing = True - return await self.async_step_user(user_input) - def _url_already_configured(self, url): """See if we already have a UPB PIM matching user input configured.""" existing_hosts = { diff --git a/homeassistant/components/upb/icons.json b/homeassistant/components/upb/icons.json index 187f0f60970b11..0274233da520cb 100644 --- a/homeassistant/components/upb/icons.json +++ b/homeassistant/components/upb/icons.json @@ -1,12 +1,28 @@ { "services": { - "light_fade_start": "mdi:transition", - "light_fade_stop": "mdi:transition-masked", - "light_blink": "mdi:eye", - "link_deactivate": "mdi:link-off", - "link_goto": "mdi:link-variant", - "link_fade_start": "mdi:transition", - "link_fade_stop": "mdi:transition-masked", - "link_blink": "mdi:eye" + "light_fade_start": { + "service": "mdi:transition" + }, + "light_fade_stop": { + "service": "mdi:transition-masked" + }, + "light_blink": { + "service": "mdi:eye" + }, + "link_deactivate": { + "service": "mdi:link-off" + }, + "link_goto": { + "service": "mdi:link-variant" + }, + "link_fade_start": { + "service": "mdi:transition" + }, + "link_fade_stop": { + "service": "mdi:transition-masked" + }, + "link_blink": { + "service": "mdi:eye" + } } } diff --git a/homeassistant/components/update/icons.json b/homeassistant/components/update/icons.json index 96920c962537bc..89af07de67f360 100644 --- a/homeassistant/components/update/icons.json +++ b/homeassistant/components/update/icons.json @@ -8,8 +8,14 @@ } }, "services": { - "clear_skipped": "mdi:package", - "install": "mdi:package-down", - "skip": "mdi:package-check" + "clear_skipped": { + "service": "mdi:package" + }, + "install": { + "service": "mdi:package-down" + }, + "skip": { + "service": "mdi:package-check" + } } } diff --git a/homeassistant/components/utility_meter/icons.json b/homeassistant/components/utility_meter/icons.json index 3c447b4a8108c5..2539b73d168ce7 100644 --- a/homeassistant/components/utility_meter/icons.json +++ b/homeassistant/components/utility_meter/icons.json @@ -12,7 +12,11 @@ } }, "services": { - "reset": "mdi:numeric-0-box-outline", - "calibrate": "mdi:auto-fix" + "reset": { + "service": "mdi:numeric-0-box-outline" + }, + "calibrate": { + "service": "mdi:auto-fix" + } } } diff --git a/homeassistant/components/vacuum/icons.json b/homeassistant/components/vacuum/icons.json index 25f0cfd03ef2f7..4169729efec2a1 100644 --- a/homeassistant/components/vacuum/icons.json +++ b/homeassistant/components/vacuum/icons.json @@ -5,17 +5,41 @@ } }, "services": { - "clean_spot": "mdi:target-variant", - "locate": "mdi:map-marker", - "pause": "mdi:pause", - "return_to_base": "mdi:home-import-outline", - "send_command": "mdi:send", - "set_fan_speed": "mdi:fan", - "start": "mdi:play", - "start_pause": "mdi:play-pause", - "stop": "mdi:stop", - "toggle": "mdi:play-pause", - "turn_off": "mdi:stop", - "turn_on": "mdi:play" + "clean_spot": { + "service": "mdi:target-variant" + }, + "locate": { + "service": "mdi:map-marker" + }, + "pause": { + "service": "mdi:pause" + }, + "return_to_base": { + "service": "mdi:home-import-outline" + }, + "send_command": { + "service": "mdi:send" + }, + "set_fan_speed": { + "service": "mdi:fan" + }, + "start": { + "service": "mdi:play" + }, + "start_pause": { + "service": "mdi:play-pause" + }, + "stop": { + "service": "mdi:stop" + }, + "toggle": { + "service": "mdi:play-pause" + }, + "turn_off": { + "service": "mdi:stop" + }, + "turn_on": { + "service": "mdi:play" + } } } diff --git a/homeassistant/components/vallox/icons.json b/homeassistant/components/vallox/icons.json index 67b41d216d2ce6..f6beb55f1daa9a 100644 --- a/homeassistant/components/vallox/icons.json +++ b/homeassistant/components/vallox/icons.json @@ -37,8 +37,14 @@ } }, "services": { - "set_profile_fan_speed_home": "mdi:home", - "set_profile_fan_speed_away": "mdi:walk", - "set_profile_fan_speed_boost": "mdi:speedometer" + "set_profile_fan_speed_home": { + "service": "mdi:home" + }, + "set_profile_fan_speed_away": { + "service": "mdi:walk" + }, + "set_profile_fan_speed_boost": { + "service": "mdi:speedometer" + } } } diff --git a/homeassistant/components/valve/icons.json b/homeassistant/components/valve/icons.json index 2c887ebf273c2b..c9c6b632dcbc6b 100644 --- a/homeassistant/components/valve/icons.json +++ b/homeassistant/components/valve/icons.json @@ -17,10 +17,20 @@ } }, "services": { - "close_valve": "mdi:valve-closed", - "open_valve": "mdi:valve-open", - "set_valve_position": "mdi:valve", - "stop_valve": "mdi:stop", - "toggle": "mdi:valve-open" + "close_valve": { + "service": "mdi:valve-closed" + }, + "open_valve": { + "service": "mdi:valve-open" + }, + "set_valve_position": { + "service": "mdi:valve" + }, + "stop_valve": { + "service": "mdi:stop" + }, + "toggle": { + "service": "mdi:valve-open" + } } } diff --git a/homeassistant/components/velbus/icons.json b/homeassistant/components/velbus/icons.json index a806782d189a1a..a46f5e5fbf113f 100644 --- a/homeassistant/components/velbus/icons.json +++ b/homeassistant/components/velbus/icons.json @@ -1,8 +1,16 @@ { "services": { - "sync_clock": "mdi:clock", - "scan": "mdi:magnify", - "clear_cache": "mdi:delete", - "set_memo_text": "mdi:note-text" + "sync_clock": { + "service": "mdi:clock" + }, + "scan": { + "service": "mdi:magnify" + }, + "clear_cache": { + "service": "mdi:delete" + }, + "set_memo_text": { + "service": "mdi:note-text" + } } } diff --git a/homeassistant/components/velux/icons.json b/homeassistant/components/velux/icons.json index a16e7b5009399b..78cb5b148385d3 100644 --- a/homeassistant/components/velux/icons.json +++ b/homeassistant/components/velux/icons.json @@ -1,5 +1,7 @@ { "services": { - "reboot_gateway": "mdi:restart" + "reboot_gateway": { + "service": "mdi:restart" + } } } diff --git a/homeassistant/components/venstar/config_flow.py b/homeassistant/components/venstar/config_flow.py index 289f7936676e75..929f5718c19ac5 100644 --- a/homeassistant/components/venstar/config_flow.py +++ b/homeassistant/components/venstar/config_flow.py @@ -15,7 +15,6 @@ ) from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError -from homeassistant.helpers.typing import ConfigType from .const import _LOGGER, DOMAIN, VENSTAR_TIMEOUT @@ -85,7 +84,7 @@ async def async_step_user( errors=errors, ) - async def async_step_import(self, import_data: ConfigType) -> ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Import entry from configuration.yaml.""" self._async_abort_entries_match({CONF_HOST: import_data[CONF_HOST]}) return await self.async_step_user( diff --git a/homeassistant/components/vera/config_flow.py b/homeassistant/components/vera/config_flow.py index 181849f46a161b..08e7640773b1db 100644 --- a/homeassistant/components/vera/config_flow.py +++ b/homeassistant/components/vera/config_flow.py @@ -127,7 +127,7 @@ async def async_step_user( ), ) - async def async_step_import(self, config: dict[str, Any]) -> ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Handle a flow initialized by import.""" # If there are entities with the legacy unique_id, then this imported config @@ -146,7 +146,7 @@ async def async_step_import(self, config: dict[str, Any]) -> ConfigFlowResult: return await self.async_step_finish( { - **config, + **import_data, CONF_SOURCE: SOURCE_IMPORT, CONF_LEGACY_UNIQUE_ID: use_legacy_unique_id, } diff --git a/homeassistant/components/verisure/icons.json b/homeassistant/components/verisure/icons.json index 35f6960b1e80d0..809cf004a3fd6f 100644 --- a/homeassistant/components/verisure/icons.json +++ b/homeassistant/components/verisure/icons.json @@ -1,7 +1,13 @@ { "services": { - "capture_smartcam": "mdi:camera", - "enable_autolock": "mdi:lock", - "disable_autolock": "mdi:lock-off" + "capture_smartcam": { + "service": "mdi:camera" + }, + "enable_autolock": { + "service": "mdi:lock" + }, + "disable_autolock": { + "service": "mdi:lock-off" + } } } diff --git a/homeassistant/components/vesync/config_flow.py b/homeassistant/components/vesync/config_flow.py index 15f9f548e35473..6115cb9ee76664 100644 --- a/homeassistant/components/vesync/config_flow.py +++ b/homeassistant/components/vesync/config_flow.py @@ -1,40 +1,42 @@ """Config flow utilities.""" -from collections import OrderedDict +from typing import Any from pyvesync import VeSync import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.core import callback +import homeassistant.helpers.config_validation as cv from .const import DOMAIN +DATA_SCHEMA = vol.Schema( + { + vol.Required(CONF_USERNAME): cv.string, + vol.Required(CONF_PASSWORD): cv.string, + } +) + class VeSyncFlowHandler(ConfigFlow, domain=DOMAIN): """Handle a config flow.""" VERSION = 1 - def __init__(self) -> None: - """Instantiate config flow.""" - self._username = None - self._password = None - self.data_schema = OrderedDict() - self.data_schema[vol.Required(CONF_USERNAME)] = str - self.data_schema[vol.Required(CONF_PASSWORD)] = str - @callback - def _show_form(self, errors=None): + def _show_form(self, errors: dict[str, str] | None = None) -> ConfigFlowResult: """Show form to the user.""" return self.async_show_form( step_id="user", - data_schema=vol.Schema(self.data_schema), + data_schema=DATA_SCHEMA, errors=errors if errors else {}, ) - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle a flow start.""" if self._async_current_entries(): return self.async_abort(reason="single_instance_allowed") @@ -42,15 +44,15 @@ async def async_step_user(self, user_input=None): if not user_input: return self._show_form() - self._username = user_input[CONF_USERNAME] - self._password = user_input[CONF_PASSWORD] + username = user_input[CONF_USERNAME] + password = user_input[CONF_PASSWORD] - manager = VeSync(self._username, self._password) + manager = VeSync(username, password) login = await self.hass.async_add_executor_job(manager.login) if not login: return self._show_form(errors={"base": "invalid_auth"}) return self.async_create_entry( - title=self._username, - data={CONF_USERNAME: self._username, CONF_PASSWORD: self._password}, + title=username, + data={CONF_USERNAME: username, CONF_PASSWORD: password}, ) diff --git a/homeassistant/components/vesync/icons.json b/homeassistant/components/vesync/icons.json index a4bf4afd410bca..cfdefb2ed09d49 100644 --- a/homeassistant/components/vesync/icons.json +++ b/homeassistant/components/vesync/icons.json @@ -1,5 +1,7 @@ { "services": { - "update_devices": "mdi:update" + "update_devices": { + "service": "mdi:update" + } } } diff --git a/homeassistant/components/vicare/__init__.py b/homeassistant/components/vicare/__init__.py index 0c87cd6f4fe480..ead210e281646f 100644 --- a/homeassistant/components/vicare/__init__.py +++ b/homeassistant/components/vicare/__init__.py @@ -15,10 +15,12 @@ PyViCareInvalidCredentialsError, ) +from homeassistant.components.climate import DOMAIN as DOMAIN_CLIMATE from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_CLIENT_ID, CONF_PASSWORD, CONF_USERNAME -from homeassistant.core import HomeAssistant +from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import ConfigEntryAuthFailed +from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.helpers.storage import STORAGE_DIR from .const import ( @@ -47,6 +49,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: except (PyViCareInvalidConfigurationError, PyViCareInvalidCredentialsError) as err: raise ConfigEntryAuthFailed("Authentication failed") from err + for device in hass.data[DOMAIN][entry.entry_id][DEVICE_LIST]: + # Migration can be removed in 2025.4.0 + await async_migrate_devices(hass, entry, device) + # Migration can be removed in 2025.4.0 + await async_migrate_entities(hass, entry, device) + await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) return True @@ -109,6 +117,72 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: return unload_ok +async def async_migrate_devices( + hass: HomeAssistant, entry: ConfigEntry, device: ViCareDevice +) -> None: + """Migrate old entry.""" + registry = dr.async_get(hass) + + gateway_serial: str = device.config.getConfig().serial + device_serial: str = device.api.getSerial() + + old_identifier = gateway_serial + new_identifier = f"{gateway_serial}_{device_serial}" + + # Migrate devices + for device_entry in dr.async_entries_for_config_entry(registry, entry.entry_id): + if device_entry.identifiers == {(DOMAIN, old_identifier)}: + _LOGGER.debug("Migrating device %s", device_entry.name) + registry.async_update_device( + device_entry.id, + serial_number=device_serial, + new_identifiers={(DOMAIN, new_identifier)}, + ) + + +async def async_migrate_entities( + hass: HomeAssistant, entry: ConfigEntry, device: ViCareDevice +) -> None: + """Migrate old entry.""" + gateway_serial: str = device.config.getConfig().serial + device_serial: str = device.api.getSerial() + new_identifier = f"{gateway_serial}_{device_serial}" + + @callback + def _update_unique_id( + entity_entry: er.RegistryEntry, + ) -> dict[str, str] | None: + """Update unique ID of entity entry.""" + if not entity_entry.unique_id.startswith(gateway_serial): + # belongs to other device/gateway + return None + if entity_entry.unique_id.startswith(f"{gateway_serial}_"): + # Already correct, nothing to do + return None + + unique_id_parts = entity_entry.unique_id.split("-") + unique_id_parts[0] = new_identifier + + # convert climate entity unique id from `-` to `-heating-` + if entity_entry.domain == DOMAIN_CLIMATE: + unique_id_parts[len(unique_id_parts) - 1] = ( + f"{entity_entry.translation_key}-{unique_id_parts[len(unique_id_parts)-1]}" + ) + + entity_new_unique_id = "-".join(unique_id_parts) + + _LOGGER.debug( + "Migrating entity %s from %s to new id %s", + entity_entry.entity_id, + entity_entry.unique_id, + entity_new_unique_id, + ) + return {"new_unique_id": entity_new_unique_id} + + # Migrate entities + await er.async_migrate_entries(hass, entry.entry_id, _update_unique_id) + + def get_supported_devices( devices: list[PyViCareDeviceConfig], ) -> list[PyViCareDeviceConfig]: diff --git a/homeassistant/components/vicare/binary_sensor.py b/homeassistant/components/vicare/binary_sensor.py index 2df8a2f06d38bf..7fe248fa266f59 100644 --- a/homeassistant/components/vicare/binary_sensor.py +++ b/homeassistant/components/vicare/binary_sensor.py @@ -10,7 +10,7 @@ from PyViCare.PyViCareDevice import Device as PyViCareDevice from PyViCare.PyViCareDeviceConfig import PyViCareDeviceConfig from PyViCare.PyViCareHeatingDevice import ( - HeatingDeviceWithComponent as PyViCareHeatingDeviceWithComponent, + HeatingDeviceWithComponent as PyViCareHeatingDeviceComponent, ) from PyViCare.PyViCareUtils import ( PyViCareInvalidDataError, @@ -112,61 +112,36 @@ def _build_entities( entities: list[ViCareBinarySensor] = [] for device in device_list: - entities.extend(_build_entities_for_device(device.api, device.config)) + # add device entities entities.extend( - _build_entities_for_component( - get_circuits(device.api), device.config, CIRCUIT_SENSORS + ViCareBinarySensor( + description, + device.config, + device.api, ) + for description in GLOBAL_SENSORS + if is_supported(description.key, description, device.api) ) - entities.extend( - _build_entities_for_component( - get_burners(device.api), device.config, BURNER_SENSORS + # add component entities + for component_list, entity_description_list in ( + (get_circuits(device.api), CIRCUIT_SENSORS), + (get_burners(device.api), BURNER_SENSORS), + (get_compressors(device.api), COMPRESSOR_SENSORS), + ): + entities.extend( + ViCareBinarySensor( + description, + device.config, + device.api, + component, + ) + for component in component_list + for description in entity_description_list + if is_supported(description.key, description, component) ) - ) - entities.extend( - _build_entities_for_component( - get_compressors(device.api), device.config, COMPRESSOR_SENSORS - ) - ) return entities -def _build_entities_for_device( - device: PyViCareDevice, - device_config: PyViCareDeviceConfig, -) -> list[ViCareBinarySensor]: - """Create device specific ViCare binary sensor entities.""" - - return [ - ViCareBinarySensor( - device, - device_config, - description, - ) - for description in GLOBAL_SENSORS - if is_supported(description.key, description, device) - ] - - -def _build_entities_for_component( - components: list[PyViCareHeatingDeviceWithComponent], - device_config: PyViCareDeviceConfig, - entity_descriptions: tuple[ViCareBinarySensorEntityDescription, ...], -) -> list[ViCareBinarySensor]: - """Create component specific ViCare binary sensor entities.""" - - return [ - ViCareBinarySensor( - component, - device_config, - description, - ) - for component in components - for description in entity_descriptions - if is_supported(description.key, description, component) - ] - - async def async_setup_entry( hass: HomeAssistant, config_entry: ConfigEntry, @@ -190,12 +165,13 @@ class ViCareBinarySensor(ViCareEntity, BinarySensorEntity): def __init__( self, - api: PyViCareDevice, - device_config: PyViCareDeviceConfig, description: ViCareBinarySensorEntityDescription, + device_config: PyViCareDeviceConfig, + device: PyViCareDevice, + component: PyViCareHeatingDeviceComponent | None = None, ) -> None: """Initialize the sensor.""" - super().__init__(device_config, api, description.key) + super().__init__(description.key, device_config, device, component) self.entity_description = description @property diff --git a/homeassistant/components/vicare/button.py b/homeassistant/components/vicare/button.py index c927055dadddbc..51a763c1fccd49 100644 --- a/homeassistant/components/vicare/button.py +++ b/homeassistant/components/vicare/button.py @@ -54,9 +54,9 @@ def _build_entities( return [ ViCareButton( - device.api, - device.config, description, + device.config, + device.api, ) for device in device_list for description in BUTTON_DESCRIPTIONS @@ -87,12 +87,12 @@ class ViCareButton(ViCareEntity, ButtonEntity): def __init__( self, - api: PyViCareDevice, - device_config: PyViCareDeviceConfig, description: ViCareButtonEntityDescription, + device_config: PyViCareDeviceConfig, + device: PyViCareDevice, ) -> None: """Initialize the button.""" - super().__init__(device_config, api, description.key) + super().__init__(description.key, device_config, device) self.entity_description = description def press(self) -> None: diff --git a/homeassistant/components/vicare/climate.py b/homeassistant/components/vicare/climate.py index 1333327609db7e..410395760eabd4 100644 --- a/homeassistant/components/vicare/climate.py +++ b/homeassistant/components/vicare/climate.py @@ -87,10 +87,9 @@ def _build_entities( """Create ViCare climate entities for a device.""" return [ ViCareClimate( + device.config, device.api, circuit, - device.config, - "heating", ) for device in device_list for circuit in get_circuits(device.api) @@ -136,25 +135,23 @@ class ViCareClimate(ViCareEntity, ClimateEntity): _attr_min_temp = VICARE_TEMP_HEATING_MIN _attr_max_temp = VICARE_TEMP_HEATING_MAX _attr_target_temperature_step = PRECISION_WHOLE + _attr_translation_key = "heating" _current_action: bool | None = None _current_mode: str | None = None + _current_program: str | None = None _enable_turn_on_off_backwards_compatibility = False def __init__( self, - api: PyViCareDevice, - circuit: PyViCareHeatingCircuit, device_config: PyViCareDeviceConfig, - translation_key: str, + device: PyViCareDevice, + circuit: PyViCareHeatingCircuit, ) -> None: """Initialize the climate device.""" - super().__init__(device_config, api, circuit.id) - self._circuit = circuit + super().__init__(self._attr_translation_key, device_config, device, circuit) + self._device = device self._attributes: dict[str, Any] = {} - self._current_program = None - self._attr_translation_key = translation_key - - self._attributes["vicare_programs"] = self._circuit.getPrograms() + self._attributes["vicare_programs"] = self._api.getPrograms() self._attr_preset_modes = [ preset for heating_program in self._attributes["vicare_programs"] @@ -166,11 +163,11 @@ def update(self) -> None: try: _room_temperature = None with suppress(PyViCareNotSupportedFeatureError): - _room_temperature = self._circuit.getRoomTemperature() + _room_temperature = self._api.getRoomTemperature() _supply_temperature = None with suppress(PyViCareNotSupportedFeatureError): - _supply_temperature = self._circuit.getSupplyTemperature() + _supply_temperature = self._api.getSupplyTemperature() if _room_temperature is not None: self._attr_current_temperature = _room_temperature @@ -180,15 +177,13 @@ def update(self) -> None: self._attr_current_temperature = None with suppress(PyViCareNotSupportedFeatureError): - self._current_program = self._circuit.getActiveProgram() + self._current_program = self._api.getActiveProgram() with suppress(PyViCareNotSupportedFeatureError): - self._attr_target_temperature = ( - self._circuit.getCurrentDesiredTemperature() - ) + self._attr_target_temperature = self._api.getCurrentDesiredTemperature() with suppress(PyViCareNotSupportedFeatureError): - self._current_mode = self._circuit.getActiveMode() + self._current_mode = self._api.getActiveMode() # Update the generic device attributes self._attributes = { @@ -199,25 +194,25 @@ def update(self) -> None: with suppress(PyViCareNotSupportedFeatureError): self._attributes["heating_curve_slope"] = ( - self._circuit.getHeatingCurveSlope() + self._api.getHeatingCurveSlope() ) with suppress(PyViCareNotSupportedFeatureError): self._attributes["heating_curve_shift"] = ( - self._circuit.getHeatingCurveShift() + self._api.getHeatingCurveShift() ) with suppress(PyViCareNotSupportedFeatureError): - self._attributes["vicare_modes"] = self._circuit.getModes() + self._attributes["vicare_modes"] = self._api.getModes() self._current_action = False # Update the specific device attributes with suppress(PyViCareNotSupportedFeatureError): - for burner in get_burners(self._api): + for burner in get_burners(self._device): self._current_action = self._current_action or burner.getActive() with suppress(PyViCareNotSupportedFeatureError): - for compressor in get_compressors(self._api): + for compressor in get_compressors(self._device): self._current_action = ( self._current_action or compressor.getActive() ) @@ -248,9 +243,9 @@ def set_hvac_mode(self, hvac_mode: HVACMode) -> None: raise ValueError(f"Cannot set invalid hvac mode: {hvac_mode}") _LOGGER.debug("Setting hvac mode to %s / %s", hvac_mode, vicare_mode) - self._circuit.setMode(vicare_mode) + self._api.setMode(vicare_mode) - def vicare_mode_from_hvac_mode(self, hvac_mode): + def vicare_mode_from_hvac_mode(self, hvac_mode) -> str | None: """Return the corresponding vicare mode for an hvac_mode.""" if "vicare_modes" not in self._attributes: return None @@ -286,7 +281,7 @@ def hvac_action(self) -> HVACAction: def set_temperature(self, **kwargs: Any) -> None: """Set new target temperatures.""" if (temp := kwargs.get(ATTR_TEMPERATURE)) is not None: - self._circuit.setProgramTemperature(self._current_program, temp) + self._api.setProgramTemperature(self._current_program, temp) self._attr_target_temperature = temp @property @@ -315,7 +310,7 @@ def set_preset_mode(self, preset_mode: str) -> None: ): _LOGGER.debug("deactivating %s", self._current_program) try: - self._circuit.deactivateProgram(self._current_program) + self._api.deactivateProgram(self._current_program) except PyViCareCommandError as err: raise ServiceValidationError( translation_domain=DOMAIN, @@ -329,7 +324,7 @@ def set_preset_mode(self, preset_mode: str) -> None: if target_program in CHANGABLE_HEATING_PROGRAMS: _LOGGER.debug("activating %s", target_program) try: - self._circuit.activateProgram(target_program) + self._api.activateProgram(target_program) except PyViCareCommandError as err: raise ServiceValidationError( translation_domain=DOMAIN, @@ -340,13 +335,13 @@ def set_preset_mode(self, preset_mode: str) -> None: ) from err @property - def extra_state_attributes(self): + def extra_state_attributes(self) -> dict[str, Any]: """Show Device Attributes.""" return self._attributes - def set_vicare_mode(self, vicare_mode): + def set_vicare_mode(self, vicare_mode) -> None: """Service function to set vicare modes directly.""" if vicare_mode not in self._attributes["vicare_modes"]: raise ValueError(f"Cannot set invalid vicare mode: {vicare_mode}.") - self._circuit.setMode(vicare_mode) + self._api.setMode(vicare_mode) diff --git a/homeassistant/components/vicare/entity.py b/homeassistant/components/vicare/entity.py index 1bb2993cd3a438..f48243e83e165c 100644 --- a/homeassistant/components/vicare/entity.py +++ b/homeassistant/components/vicare/entity.py @@ -2,6 +2,9 @@ from PyViCare.PyViCareDevice import Device as PyViCareDevice from PyViCare.PyViCareDeviceConfig import PyViCareDeviceConfig +from PyViCare.PyViCareHeatingDevice import ( + HeatingDeviceWithComponent as PyViCareHeatingDeviceComponent, +) from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.entity import Entity @@ -16,21 +19,26 @@ class ViCareEntity(Entity): def __init__( self, + unique_id_suffix: str, device_config: PyViCareDeviceConfig, device: PyViCareDevice, - unique_id_suffix: str, + component: PyViCareHeatingDeviceComponent | None = None, ) -> None: """Initialize the entity.""" - self._api = device + gateway_serial = device_config.getConfig().serial + device_serial = device.getSerial() + identifier = f"{gateway_serial}_{device_serial}" - self._attr_unique_id = f"{device_config.getConfig().serial}-{unique_id_suffix}" - # valid for compressors, circuits, burners (HeatingDeviceWithComponent) - if hasattr(device, "id"): - self._attr_unique_id += f"-{device.id}" + self._api: PyViCareDevice | PyViCareHeatingDeviceComponent = ( + component if component else device + ) + self._attr_unique_id = f"{identifier}-{unique_id_suffix}" + if component: + self._attr_unique_id += f"-{component.id}" self._attr_device_info = DeviceInfo( - identifiers={(DOMAIN, device_config.getConfig().serial)}, - serial_number=device_config.getConfig().serial, + identifiers={(DOMAIN, identifier)}, + serial_number=device_serial, name=device_config.getModel(), manufacturer="Viessmann", model=device_config.getModel(), diff --git a/homeassistant/components/vicare/fan.py b/homeassistant/components/vicare/fan.py index 088e54c7354789..d7dbd037b569a4 100644 --- a/homeassistant/components/vicare/fan.py +++ b/homeassistant/components/vicare/fan.py @@ -3,6 +3,7 @@ from __future__ import annotations from contextlib import suppress +import enum import logging from PyViCare.PyViCareDevice import Device as PyViCareDevice @@ -28,10 +29,58 @@ from .const import DEVICE_LIST, DOMAIN from .entity import ViCareEntity -from .types import VentilationMode, VentilationProgram _LOGGER = logging.getLogger(__name__) + +class VentilationProgram(enum.StrEnum): + """ViCare preset ventilation programs. + + As listed in https://github.com/somm15/PyViCare/blob/6c5b023ca6c8bb2d38141dd1746dc1705ec84ce8/PyViCare/PyViCareVentilationDevice.py#L37 + """ + + LEVEL_ONE = "levelOne" + LEVEL_TWO = "levelTwo" + LEVEL_THREE = "levelThree" + LEVEL_FOUR = "levelFour" + + +class VentilationMode(enum.StrEnum): + """ViCare ventilation modes.""" + + PERMANENT = "permanent" # on, speed controlled by program (levelOne-levelFour) + VENTILATION = "ventilation" # activated by schedule + SENSOR_DRIVEN = "sensor_driven" # activated by schedule, override by sensor + SENSOR_OVERRIDE = "sensor_override" # activated by sensor + + @staticmethod + def to_vicare_mode(mode: str | None) -> str | None: + """Return the mapped ViCare ventilation mode for the Home Assistant mode.""" + if mode: + try: + ventilation_mode = VentilationMode(mode) + except ValueError: + # ignore unsupported / unmapped modes + return None + return HA_TO_VICARE_MODE_VENTILATION.get(ventilation_mode) if mode else None + return None + + @staticmethod + def from_vicare_mode(vicare_mode: str | None) -> str | None: + """Return the mapped Home Assistant mode for the ViCare ventilation mode.""" + for mode in VentilationMode: + if HA_TO_VICARE_MODE_VENTILATION.get(VentilationMode(mode)) == vicare_mode: + return mode + return None + + +HA_TO_VICARE_MODE_VENTILATION = { + VentilationMode.PERMANENT: "permanent", + VentilationMode.VENTILATION: "ventilation", + VentilationMode.SENSOR_DRIVEN: "sensorDriven", + VentilationMode.SENSOR_OVERRIDE: "sensorOverride", +} + ORDERED_NAMED_FAN_SPEEDS = [ VentilationProgram.LEVEL_ONE, VentilationProgram.LEVEL_TWO, @@ -80,7 +129,7 @@ def __init__( device: PyViCareDevice, ) -> None: """Initialize the fan entity.""" - super().__init__(device_config, device, self._attr_translation_key) + super().__init__(self._attr_translation_key, device_config, device) def update(self) -> None: """Update state of fan.""" diff --git a/homeassistant/components/vicare/icons.json b/homeassistant/components/vicare/icons.json index 2f40d8a882214e..9d0f27a863c5ac 100644 --- a/homeassistant/components/vicare/icons.json +++ b/homeassistant/components/vicare/icons.json @@ -88,6 +88,8 @@ } }, "services": { - "set_vicare_mode": "mdi:cog" + "set_vicare_mode": { + "service": "mdi:cog" + } } } diff --git a/homeassistant/components/vicare/manifest.json b/homeassistant/components/vicare/manifest.json index 186e9ef6289752..7a3089d04c31f8 100644 --- a/homeassistant/components/vicare/manifest.json +++ b/homeassistant/components/vicare/manifest.json @@ -11,5 +11,5 @@ "documentation": "https://www.home-assistant.io/integrations/vicare", "iot_class": "cloud_polling", "loggers": ["PyViCare"], - "requirements": ["PyViCare-neo==0.2.1"] + "requirements": ["PyViCare-neo==0.3.0"] } diff --git a/homeassistant/components/vicare/number.py b/homeassistant/components/vicare/number.py index c0564170274289..a7f679f7224e71 100644 --- a/homeassistant/components/vicare/number.py +++ b/homeassistant/components/vicare/number.py @@ -50,6 +50,18 @@ class ViCareNumberEntityDescription(NumberEntityDescription, ViCareRequiredKeysM DEVICE_ENTITY_DESCRIPTIONS: tuple[ViCareNumberEntityDescription, ...] = ( + ViCareNumberEntityDescription( + key="dhw_temperature", + translation_key="dhw_temperature", + entity_category=EntityCategory.CONFIG, + device_class=NumberDeviceClass.TEMPERATURE, + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + value_getter=lambda api: api.getDomesticHotWaterConfiguredTemperature(), + value_setter=lambda api, value: api.setDomesticHotWaterTemperature(value), + min_value_getter=lambda api: api.getDomesticHotWaterMinTemperature(), + max_value_getter=lambda api: api.getDomesticHotWaterMaxTemperature(), + native_step=1, + ), ViCareNumberEntityDescription( key="dhw_secondary_temperature", translation_key="dhw_secondary_temperature", @@ -63,6 +75,34 @@ class ViCareNumberEntityDescription(NumberEntityDescription, ViCareRequiredKeysM native_max_value=60, native_step=1, ), + ViCareNumberEntityDescription( + key="dhw_hysteresis_switch_on", + translation_key="dhw_hysteresis_switch_on", + entity_category=EntityCategory.CONFIG, + device_class=NumberDeviceClass.TEMPERATURE, + native_unit_of_measurement=UnitOfTemperature.KELVIN, + value_getter=lambda api: api.getDomesticHotWaterHysteresisSwitchOn(), + value_setter=lambda api, value: api.setDomesticHotWaterHysteresisSwitchOn( + value + ), + min_value_getter=lambda api: api.getDomesticHotWaterHysteresisSwitchOnMin(), + max_value_getter=lambda api: api.getDomesticHotWaterHysteresisSwitchOnMax(), + stepping_getter=lambda api: api.getDomesticHotWaterHysteresisSwitchOnStepping(), + ), + ViCareNumberEntityDescription( + key="dhw_hysteresis_switch_off", + translation_key="dhw_hysteresis_switch_off", + entity_category=EntityCategory.CONFIG, + device_class=NumberDeviceClass.TEMPERATURE, + native_unit_of_measurement=UnitOfTemperature.KELVIN, + value_getter=lambda api: api.getDomesticHotWaterHysteresisSwitchOff(), + value_setter=lambda api, value: api.setDomesticHotWaterHysteresisSwitchOff( + value + ), + min_value_getter=lambda api: api.getDomesticHotWaterHysteresisSwitchOffMin(), + max_value_getter=lambda api: api.getDomesticHotWaterHysteresisSwitchOffMax(), + stepping_getter=lambda api: api.getDomesticHotWaterHysteresisSwitchOffStepping(), + ), ) @@ -233,30 +273,30 @@ def _build_entities( ) -> list[ViCareNumber]: """Create ViCare number entities for a device.""" - entities: list[ViCareNumber] = [ - ViCareNumber( - device.api, - device.config, - description, - ) - for device in device_list - for description in DEVICE_ENTITY_DESCRIPTIONS - if is_supported(description.key, description, device.api) - ] - - entities.extend( - [ + entities: list[ViCareNumber] = [] + for device in device_list: + # add device entities + entities.extend( ViCareNumber( - circuit, + description, device.config, + device.api, + ) + for description in DEVICE_ENTITY_DESCRIPTIONS + if is_supported(description.key, description, device.api) + ) + # add component entities + entities.extend( + ViCareNumber( description, + device.config, + device.api, + circuit, ) - for device in device_list for circuit in get_circuits(device.api) for description in CIRCUIT_ENTITY_DESCRIPTIONS if is_supported(description.key, description, circuit) - ] - ) + ) return entities @@ -283,12 +323,13 @@ class ViCareNumber(ViCareEntity, NumberEntity): def __init__( self, - api: PyViCareHeatingDeviceComponent, - device_config: PyViCareDeviceConfig, description: ViCareNumberEntityDescription, + device_config: PyViCareDeviceConfig, + device: PyViCareDevice, + component: PyViCareHeatingDeviceComponent | None = None, ) -> None: """Initialize the number.""" - super().__init__(device_config, api, description.key) + super().__init__(description.key, device_config, device, component) self.entity_description = description @property diff --git a/homeassistant/components/vicare/sensor.py b/homeassistant/components/vicare/sensor.py index 0271ffc97983a9..bdcb6dfa3aa01d 100644 --- a/homeassistant/components/vicare/sensor.py +++ b/homeassistant/components/vicare/sensor.py @@ -10,7 +10,7 @@ from PyViCare.PyViCareDevice import Device as PyViCareDevice from PyViCare.PyViCareDeviceConfig import PyViCareDeviceConfig from PyViCare.PyViCareHeatingDevice import ( - HeatingDeviceWithComponent as PyViCareHeatingDeviceWithComponent, + HeatingDeviceWithComponent as PyViCareHeatingDeviceComponent, ) from PyViCare.PyViCareUtils import ( PyViCareInvalidDataError, @@ -747,7 +747,6 @@ class ViCareSensorEntityDescription(SensorEntityDescription, ViCareRequiredKeysM ), ) - CIRCUIT_SENSORS: tuple[ViCareSensorEntityDescription, ...] = ( ViCareSensorEntityDescription( key="supply_temperature", @@ -865,61 +864,36 @@ def _build_entities( entities: list[ViCareSensor] = [] for device in device_list: - entities.extend(_build_entities_for_device(device.api, device.config)) - entities.extend( - _build_entities_for_component( - get_circuits(device.api), device.config, CIRCUIT_SENSORS - ) - ) + # add device entities entities.extend( - _build_entities_for_component( - get_burners(device.api), device.config, BURNER_SENSORS + ViCareSensor( + description, + device.config, + device.api, ) + for description in GLOBAL_SENSORS + if is_supported(description.key, description, device.api) ) - entities.extend( - _build_entities_for_component( - get_compressors(device.api), device.config, COMPRESSOR_SENSORS + # add component entities + for component_list, entity_description_list in ( + (get_circuits(device.api), CIRCUIT_SENSORS), + (get_burners(device.api), BURNER_SENSORS), + (get_compressors(device.api), COMPRESSOR_SENSORS), + ): + entities.extend( + ViCareSensor( + description, + device.config, + device.api, + component, + ) + for component in component_list + for description in entity_description_list + if is_supported(description.key, description, component) ) - ) return entities -def _build_entities_for_device( - device: PyViCareDevice, - device_config: PyViCareDeviceConfig, -) -> list[ViCareSensor]: - """Create device specific ViCare sensor entities.""" - - return [ - ViCareSensor( - device, - device_config, - description, - ) - for description in GLOBAL_SENSORS - if is_supported(description.key, description, device) - ] - - -def _build_entities_for_component( - components: list[PyViCareHeatingDeviceWithComponent], - device_config: PyViCareDeviceConfig, - entity_descriptions: tuple[ViCareSensorEntityDescription, ...], -) -> list[ViCareSensor]: - """Create component specific ViCare sensor entities.""" - - return [ - ViCareSensor( - component, - device_config, - description, - ) - for component in components - for description in entity_descriptions - if is_supported(description.key, description, component) - ] - - async def async_setup_entry( hass: HomeAssistant, config_entry: ConfigEntry, @@ -932,7 +906,9 @@ async def async_setup_entry( await hass.async_add_executor_job( _build_entities, device_list, - ) + ), + # run update to have device_class set depending on unit_of_measurement + True, ) @@ -943,15 +919,14 @@ class ViCareSensor(ViCareEntity, SensorEntity): def __init__( self, - api, - device_config: PyViCareDeviceConfig, description: ViCareSensorEntityDescription, + device_config: PyViCareDeviceConfig, + device: PyViCareDevice, + component: PyViCareHeatingDeviceComponent | None = None, ) -> None: """Initialize the sensor.""" - super().__init__(device_config, api, description.key) + super().__init__(description.key, device_config, device, component) self.entity_description = description - # run update to have device_class set depending on unit_of_measurement - self.update() @property def available(self) -> bool: diff --git a/homeassistant/components/vicare/strings.json b/homeassistant/components/vicare/strings.json index 0452a560cb879f..752645137df5a4 100644 --- a/homeassistant/components/vicare/strings.json +++ b/homeassistant/components/vicare/strings.json @@ -105,8 +105,17 @@ "comfort_heating_temperature": { "name": "[%key:component::vicare::entity::number::comfort_temperature::name%]" }, + "dhw_temperature": { + "name": "DHW temperature" + }, "dhw_secondary_temperature": { "name": "DHW secondary temperature" + }, + "dhw_hysteresis_switch_on": { + "name": "DHW hysteresis switch on" + }, + "dhw_hysteresis_switch_off": { + "name": "DHW hysteresis switch off" } }, "sensor": { diff --git a/homeassistant/components/vicare/types.py b/homeassistant/components/vicare/types.py index 596605fccdd0f3..7e1ec7f8beee0a 100644 --- a/homeassistant/components/vicare/types.py +++ b/homeassistant/components/vicare/types.py @@ -64,55 +64,6 @@ def from_ha_preset( } -class VentilationMode(enum.StrEnum): - """ViCare ventilation modes.""" - - PERMANENT = "permanent" # on, speed controlled by program (levelOne-levelFour) - VENTILATION = "ventilation" # activated by schedule - SENSOR_DRIVEN = "sensor_driven" # activated by schedule, override by sensor - SENSOR_OVERRIDE = "sensor_override" # activated by sensor - - @staticmethod - def to_vicare_mode(mode: str | None) -> str | None: - """Return the mapped ViCare ventilation mode for the Home Assistant mode.""" - if mode: - try: - ventilation_mode = VentilationMode(mode) - except ValueError: - # ignore unsupported / unmapped modes - return None - return HA_TO_VICARE_MODE_VENTILATION.get(ventilation_mode) if mode else None - return None - - @staticmethod - def from_vicare_mode(vicare_mode: str | None) -> str | None: - """Return the mapped Home Assistant mode for the ViCare ventilation mode.""" - for mode in VentilationMode: - if HA_TO_VICARE_MODE_VENTILATION.get(VentilationMode(mode)) == vicare_mode: - return mode - return None - - -HA_TO_VICARE_MODE_VENTILATION = { - VentilationMode.PERMANENT: "permanent", - VentilationMode.VENTILATION: "ventilation", - VentilationMode.SENSOR_DRIVEN: "sensorDriven", - VentilationMode.SENSOR_OVERRIDE: "sensorOverride", -} - - -class VentilationProgram(enum.StrEnum): - """ViCare preset ventilation programs. - - As listed in https://github.com/somm15/PyViCare/blob/6c5b023ca6c8bb2d38141dd1746dc1705ec84ce8/PyViCare/PyViCareVentilationDevice.py#L37 - """ - - LEVEL_ONE = "levelOne" - LEVEL_TWO = "levelTwo" - LEVEL_THREE = "levelThree" - LEVEL_FOUR = "levelFour" - - @dataclass(frozen=True) class ViCareDevice: """Dataclass holding the device api and config.""" diff --git a/homeassistant/components/vicare/water_heater.py b/homeassistant/components/vicare/water_heater.py index 223217f4e13be9..621d2f2a09ba90 100644 --- a/homeassistant/components/vicare/water_heater.py +++ b/homeassistant/components/vicare/water_heater.py @@ -69,10 +69,9 @@ def _build_entities( return [ ViCareWater( + device.config, device.api, circuit, - device.config, - "domestic_hot_water", ) for device in device_list for circuit in get_circuits(device.api) @@ -104,20 +103,19 @@ class ViCareWater(ViCareEntity, WaterHeaterEntity): _attr_min_temp = VICARE_TEMP_WATER_MIN _attr_max_temp = VICARE_TEMP_WATER_MAX _attr_operation_list = list(HA_TO_VICARE_HVAC_DHW) + _attr_translation_key = "domestic_hot_water" + _current_mode: str | None = None def __init__( self, - api: PyViCareDevice, - circuit: PyViCareHeatingCircuit, device_config: PyViCareDeviceConfig, - translation_key: str, + device: PyViCareDevice, + circuit: PyViCareHeatingCircuit, ) -> None: """Initialize the DHW water_heater device.""" - super().__init__(device_config, api, circuit.id) + super().__init__(circuit.id, device_config, device) self._circuit = circuit self._attributes: dict[str, Any] = {} - self._current_mode = None - self._attr_translation_key = translation_key def update(self) -> None: """Let HA know there has been an update from the ViCare API.""" @@ -151,6 +149,8 @@ def set_temperature(self, **kwargs: Any) -> None: self._attr_target_temperature = temp @property - def current_operation(self): + def current_operation(self) -> str | None: """Return current operation ie. heat, cool, idle.""" - return VICARE_TO_HA_HVAC_DHW.get(self._current_mode) + if self._current_mode is None: + return None + return VICARE_TO_HA_HVAC_DHW.get(self._current_mode, None) diff --git a/homeassistant/components/vilfo/config_flow.py b/homeassistant/components/vilfo/config_flow.py index b21c63bfb97df1..a6cff506f79eae 100644 --- a/homeassistant/components/vilfo/config_flow.py +++ b/homeassistant/components/vilfo/config_flow.py @@ -1,6 +1,7 @@ """Config flow for Vilfo Router integration.""" import logging +from typing import Any from vilfo import Client as VilfoClient from vilfo.exceptions import ( @@ -9,7 +10,7 @@ ) import voluptuous as vol -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_ACCESS_TOKEN, CONF_HOST, CONF_ID, CONF_MAC from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError @@ -99,7 +100,9 @@ class DomainConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" errors = {} if user_input is not None: diff --git a/homeassistant/components/vizio/config_flow.py b/homeassistant/components/vizio/config_flow.py index d8b99595f5403a..c8f1aaa21cb289 100644 --- a/homeassistant/components/vizio/config_flow.py +++ b/homeassistant/components/vizio/config_flow.py @@ -285,9 +285,7 @@ async def async_step_user( return self.async_show_form(step_id="user", data_schema=schema, errors=errors) - async def async_step_import( - self, import_config: dict[str, Any] - ) -> ConfigFlowResult: + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Import a config entry from configuration.yaml.""" # Check if new config entry matches any existing config entries for entry in self._async_current_entries(): @@ -296,28 +294,28 @@ async def async_step_import( continue if await self.hass.async_add_executor_job( - _host_is_same, entry.data[CONF_HOST], import_config[CONF_HOST] + _host_is_same, entry.data[CONF_HOST], import_data[CONF_HOST] ): updated_options: dict[str, Any] = {} updated_data: dict[str, Any] = {} remove_apps = False - if entry.data[CONF_HOST] != import_config[CONF_HOST]: - updated_data[CONF_HOST] = import_config[CONF_HOST] + if entry.data[CONF_HOST] != import_data[CONF_HOST]: + updated_data[CONF_HOST] = import_data[CONF_HOST] - if entry.data[CONF_NAME] != import_config[CONF_NAME]: - updated_data[CONF_NAME] = import_config[CONF_NAME] + if entry.data[CONF_NAME] != import_data[CONF_NAME]: + updated_data[CONF_NAME] = import_data[CONF_NAME] # Update entry.data[CONF_APPS] if import_config[CONF_APPS] differs, and # pop entry.data[CONF_APPS] if import_config[CONF_APPS] is not specified - if entry.data.get(CONF_APPS) != import_config.get(CONF_APPS): - if not import_config.get(CONF_APPS): + if entry.data.get(CONF_APPS) != import_data.get(CONF_APPS): + if not import_data.get(CONF_APPS): remove_apps = True else: - updated_options[CONF_APPS] = import_config[CONF_APPS] + updated_options[CONF_APPS] = import_data[CONF_APPS] - if entry.data.get(CONF_VOLUME_STEP) != import_config[CONF_VOLUME_STEP]: - updated_options[CONF_VOLUME_STEP] = import_config[CONF_VOLUME_STEP] + if entry.data.get(CONF_VOLUME_STEP) != import_data[CONF_VOLUME_STEP]: + updated_options[CONF_VOLUME_STEP] = import_data[CONF_VOLUME_STEP] if updated_options or updated_data or remove_apps: new_data = entry.data.copy() @@ -345,9 +343,9 @@ async def async_step_import( self._must_show_form = True # Store config key/value pairs that are not configurable in user step so they # don't get lost on user step - if import_config.get(CONF_APPS): - self._apps = copy.deepcopy(import_config[CONF_APPS]) - return await self.async_step_user(user_input=import_config) + if import_data.get(CONF_APPS): + self._apps = copy.deepcopy(import_data[CONF_APPS]) + return await self.async_step_user(user_input=import_data) async def async_step_zeroconf( self, discovery_info: zeroconf.ZeroconfServiceInfo diff --git a/homeassistant/components/vizio/icons.json b/homeassistant/components/vizio/icons.json index ccdaf816bb09ee..be6f727de6f91f 100644 --- a/homeassistant/components/vizio/icons.json +++ b/homeassistant/components/vizio/icons.json @@ -1,5 +1,7 @@ { "services": { - "update_setting": "mdi:cog" + "update_setting": { + "service": "mdi:cog" + } } } diff --git a/homeassistant/components/voip/voip.py b/homeassistant/components/voip/voip.py index 161e938a3b674d..be1e58b6eecf4e 100644 --- a/homeassistant/components/voip/voip.py +++ b/homeassistant/components/voip/voip.py @@ -33,7 +33,7 @@ ) from homeassistant.components.assist_pipeline.audio_enhancer import ( AudioEnhancer, - MicroVadEnhancer, + MicroVadSpeexEnhancer, ) from homeassistant.components.assist_pipeline.vad import ( AudioBuffer, @@ -235,7 +235,7 @@ async def _run_pipeline( try: # Wait for speech before starting pipeline segmenter = VoiceCommandSegmenter(silence_seconds=self.silence_seconds) - audio_enhancer = MicroVadEnhancer(0, 0, True) + audio_enhancer = MicroVadSpeexEnhancer(0, 0, True) chunk_buffer: deque[bytes] = deque( maxlen=self.buffered_chunks_before_speech, ) diff --git a/homeassistant/components/volumio/config_flow.py b/homeassistant/components/volumio/config_flow.py index 8edda1d20b09cf..4c7a48f36c7b24 100644 --- a/homeassistant/components/volumio/config_flow.py +++ b/homeassistant/components/volumio/config_flow.py @@ -3,6 +3,7 @@ from __future__ import annotations import logging +from typing import Any from pyvolumio import CannotConnectError, Volumio import voluptuous as vol @@ -68,7 +69,9 @@ async def _set_uid_and_abort(self): } ) - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" errors = {} if user_input is not None: diff --git a/homeassistant/components/volvooncall/config_flow.py b/homeassistant/components/volvooncall/config_flow.py index 80358a28cedc4d..b3a1745351b807 100644 --- a/homeassistant/components/volvooncall/config_flow.py +++ b/homeassistant/components/volvooncall/config_flow.py @@ -107,7 +107,7 @@ async def async_step_user( ) async def async_step_reauth( - self, user_input: Mapping[str, Any] + self, entry_data: Mapping[str, Any] ) -> ConfigFlowResult: """Perform reauth upon an API authentication error.""" self._reauth_entry = self.hass.config_entries.async_get_entry( diff --git a/homeassistant/components/vulcan/config_flow.py b/homeassistant/components/vulcan/config_flow.py index 560d777b51722d..5938e4ce690c8b 100644 --- a/homeassistant/components/vulcan/config_flow.py +++ b/homeassistant/components/vulcan/config_flow.py @@ -44,7 +44,9 @@ def __init__(self) -> None: self.keystore = None self.students = None - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle config flow.""" if self._async_current_entries(): return await self.async_step_add_next_config_entry() diff --git a/homeassistant/components/wake_on_lan/icons.json b/homeassistant/components/wake_on_lan/icons.json index 6426c478157f62..f083b0342f475b 100644 --- a/homeassistant/components/wake_on_lan/icons.json +++ b/homeassistant/components/wake_on_lan/icons.json @@ -1,5 +1,7 @@ { "services": { - "send_magic_packet": "mdi:cube-send" + "send_magic_packet": { + "service": "mdi:cube-send" + } } } diff --git a/homeassistant/components/wallbox/const.py b/homeassistant/components/wallbox/const.py index 69633cbda224cb..c38b896777678e 100644 --- a/homeassistant/components/wallbox/const.py +++ b/homeassistant/components/wallbox/const.py @@ -22,11 +22,15 @@ CHARGER_DATA_KEY = "config_data" CHARGER_DEPOT_PRICE_KEY = "depot_price" CHARGER_ENERGY_PRICE_KEY = "energy_price" +CHARGER_FEATURES_KEY = "features" CHARGER_SERIAL_NUMBER_KEY = "serial_number" CHARGER_PART_NUMBER_KEY = "part_number" +CHARGER_PLAN_KEY = "plan" +CHARGER_POWER_BOOST_KEY = "POWER_BOOST" CHARGER_SOFTWARE_KEY = "software" CHARGER_MAX_AVAILABLE_POWER_KEY = "max_available_power" CHARGER_MAX_CHARGING_CURRENT_KEY = "max_charging_current" +CHARGER_MAX_ICP_CURRENT_KEY = "icp_max_current" CHARGER_PAUSE_RESUME_KEY = "paused" CHARGER_LOCKED_UNLOCKED_KEY = "locked" CHARGER_NAME_KEY = "name" diff --git a/homeassistant/components/wallbox/coordinator.py b/homeassistant/components/wallbox/coordinator.py index e24ccd28440d19..f3679551bc4734 100644 --- a/homeassistant/components/wallbox/coordinator.py +++ b/homeassistant/components/wallbox/coordinator.py @@ -19,8 +19,12 @@ CHARGER_CURRENCY_KEY, CHARGER_DATA_KEY, CHARGER_ENERGY_PRICE_KEY, + CHARGER_FEATURES_KEY, CHARGER_LOCKED_UNLOCKED_KEY, CHARGER_MAX_CHARGING_CURRENT_KEY, + CHARGER_MAX_ICP_CURRENT_KEY, + CHARGER_PLAN_KEY, + CHARGER_POWER_BOOST_KEY, CHARGER_STATUS_DESCRIPTION_KEY, CHARGER_STATUS_ID_KEY, CODE_KEY, @@ -130,6 +134,16 @@ def _get_data(self) -> dict[str, Any]: data[CHARGER_ENERGY_PRICE_KEY] = data[CHARGER_DATA_KEY][ CHARGER_ENERGY_PRICE_KEY ] + # Only show max_icp_current if power_boost is available in the wallbox unit: + if ( + data[CHARGER_DATA_KEY].get(CHARGER_MAX_ICP_CURRENT_KEY, 0) > 0 + and CHARGER_POWER_BOOST_KEY + in data[CHARGER_DATA_KEY][CHARGER_PLAN_KEY][CHARGER_FEATURES_KEY] + ): + data[CHARGER_MAX_ICP_CURRENT_KEY] = data[CHARGER_DATA_KEY][ + CHARGER_MAX_ICP_CURRENT_KEY + ] + data[CHARGER_CURRENCY_KEY] = ( f"{data[CHARGER_DATA_KEY][CHARGER_CURRENCY_KEY][CODE_KEY]}/kWh" ) @@ -160,6 +174,21 @@ async def async_set_charging_current(self, charging_current: float) -> None: ) await self.async_request_refresh() + @_require_authentication + def _set_icp_current(self, icp_current: float) -> None: + """Set maximum icp current for Wallbox.""" + try: + self._wallbox.setIcpMaxCurrent(self._station, icp_current) + except requests.exceptions.HTTPError as wallbox_connection_error: + if wallbox_connection_error.response.status_code == 403: + raise InvalidAuth from wallbox_connection_error + raise + + async def async_set_icp_current(self, icp_current: float) -> None: + """Set maximum icp current for Wallbox.""" + await self.hass.async_add_executor_job(self._set_icp_current, icp_current) + await self.async_request_refresh() + @_require_authentication def _set_energy_cost(self, energy_cost: float) -> None: """Set energy cost for Wallbox.""" diff --git a/homeassistant/components/wallbox/number.py b/homeassistant/components/wallbox/number.py index 8ae4c473299c39..24cdd16f99dfe1 100644 --- a/homeassistant/components/wallbox/number.py +++ b/homeassistant/components/wallbox/number.py @@ -21,6 +21,7 @@ CHARGER_ENERGY_PRICE_KEY, CHARGER_MAX_AVAILABLE_POWER_KEY, CHARGER_MAX_CHARGING_CURRENT_KEY, + CHARGER_MAX_ICP_CURRENT_KEY, CHARGER_PART_NUMBER_KEY, CHARGER_SERIAL_NUMBER_KEY, DOMAIN, @@ -67,6 +68,16 @@ class WallboxNumberEntityDescription(NumberEntityDescription): set_value_fn=lambda coordinator: coordinator.async_set_energy_cost, native_step=0.01, ), + CHARGER_MAX_ICP_CURRENT_KEY: WallboxNumberEntityDescription( + key=CHARGER_MAX_ICP_CURRENT_KEY, + translation_key="maximum_icp_current", + max_value_fn=lambda coordinator: cast( + float, coordinator.data[CHARGER_MAX_AVAILABLE_POWER_KEY] + ), + min_value_fn=lambda _: 6, + set_value_fn=lambda coordinator: coordinator.async_set_icp_current, + native_step=1, + ), } diff --git a/homeassistant/components/wallbox/sensor.py b/homeassistant/components/wallbox/sensor.py index eadbc04dca259c..18d8afb5612537 100644 --- a/homeassistant/components/wallbox/sensor.py +++ b/homeassistant/components/wallbox/sensor.py @@ -38,6 +38,7 @@ CHARGER_ENERGY_PRICE_KEY, CHARGER_MAX_AVAILABLE_POWER_KEY, CHARGER_MAX_CHARGING_CURRENT_KEY, + CHARGER_MAX_ICP_CURRENT_KEY, CHARGER_SERIAL_NUMBER_KEY, CHARGER_STATE_OF_CHARGE_KEY, CHARGER_STATUS_DESCRIPTION_KEY, @@ -145,6 +146,13 @@ class WallboxSensorEntityDescription(SensorEntityDescription): device_class=SensorDeviceClass.CURRENT, state_class=SensorStateClass.MEASUREMENT, ), + CHARGER_MAX_ICP_CURRENT_KEY: WallboxSensorEntityDescription( + key=CHARGER_MAX_ICP_CURRENT_KEY, + translation_key=CHARGER_MAX_ICP_CURRENT_KEY, + native_unit_of_measurement=UnitOfElectricCurrent.AMPERE, + device_class=SensorDeviceClass.CURRENT, + state_class=SensorStateClass.MEASUREMENT, + ), } diff --git a/homeassistant/components/wallbox/strings.json b/homeassistant/components/wallbox/strings.json index dd96cebf605546..f4378b328d8cca 100644 --- a/homeassistant/components/wallbox/strings.json +++ b/homeassistant/components/wallbox/strings.json @@ -38,6 +38,9 @@ }, "energy_price": { "name": "Energy price" + }, + "maximum_icp_current": { + "name": "Maximum ICP current" } }, "sensor": { @@ -79,6 +82,9 @@ }, "max_charging_current": { "name": "Max charging current" + }, + "icp_max_current": { + "name": "Max ICP current" } }, "switch": { diff --git a/homeassistant/components/water_heater/icons.json b/homeassistant/components/water_heater/icons.json index af6996374c52a8..bc80128c6a30e7 100644 --- a/homeassistant/components/water_heater/icons.json +++ b/homeassistant/components/water_heater/icons.json @@ -22,10 +22,20 @@ } }, "services": { - "set_away_mode": "mdi:account-arrow-right", - "set_operation_mode": "mdi:water-boiler", - "set_temperature": "mdi:thermometer", - "turn_off": "mdi:water-boiler-off", - "turn_on": "mdi:water-boiler" + "set_away_mode": { + "service": "mdi:account-arrow-right" + }, + "set_operation_mode": { + "service": "mdi:water-boiler" + }, + "set_temperature": { + "service": "mdi:thermometer" + }, + "turn_off": { + "service": "mdi:water-boiler-off" + }, + "turn_on": { + "service": "mdi:water-boiler" + } } } diff --git a/homeassistant/components/waze_travel_time/icons.json b/homeassistant/components/waze_travel_time/icons.json index fa95e8fdd8af26..98e6f26774c074 100644 --- a/homeassistant/components/waze_travel_time/icons.json +++ b/homeassistant/components/waze_travel_time/icons.json @@ -7,6 +7,8 @@ } }, "services": { - "get_travel_times": "mdi:timelapse" + "get_travel_times": { + "service": "mdi:timelapse" + } } } diff --git a/homeassistant/components/weather/icons.json b/homeassistant/components/weather/icons.json index cc53861e700a10..04b3c1d3df84cb 100644 --- a/homeassistant/components/weather/icons.json +++ b/homeassistant/components/weather/icons.json @@ -21,7 +21,11 @@ } }, "services": { - "get_forecast": "mdi:weather-cloudy-clock", - "get_forecasts": "mdi:weather-cloudy-clock" + "get_forecast": { + "service": "mdi:weather-cloudy-clock" + }, + "get_forecasts": { + "service": "mdi:weather-cloudy-clock" + } } } diff --git a/homeassistant/components/weatherflow_cloud/config_flow.py b/homeassistant/components/weatherflow_cloud/config_flow.py index e8972c320ed147..cbb83b6f25bfdc 100644 --- a/homeassistant/components/weatherflow_cloud/config_flow.py +++ b/homeassistant/components/weatherflow_cloud/config_flow.py @@ -33,9 +33,15 @@ class WeatherFlowCloudConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 async def async_step_reauth( - self, user_input: Mapping[str, Any] + self, entry_data: Mapping[str, Any] ) -> ConfigFlowResult: """Handle a flow for reauth.""" + return await self.async_step_reauth_confirm() + + async def async_step_reauth_confirm( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle a flow initiated by reauthentication.""" errors = {} if user_input is not None: @@ -54,7 +60,7 @@ async def async_step_reauth( ) return self.async_show_form( - step_id="reauth", + step_id="reauth_confirm", data_schema=vol.Schema({vol.Required(CONF_API_TOKEN): str}), errors=errors, ) diff --git a/homeassistant/components/weatherflow_cloud/manifest.json b/homeassistant/components/weatherflow_cloud/manifest.json index 354b9642c06036..aaa5bce2e16061 100644 --- a/homeassistant/components/weatherflow_cloud/manifest.json +++ b/homeassistant/components/weatherflow_cloud/manifest.json @@ -6,5 +6,5 @@ "documentation": "https://www.home-assistant.io/integrations/weatherflow_cloud", "iot_class": "cloud_polling", "loggers": ["weatherflow4py"], - "requirements": ["weatherflow4py==0.2.21"] + "requirements": ["weatherflow4py==0.2.23"] } diff --git a/homeassistant/components/weatherflow_cloud/sensor.py b/homeassistant/components/weatherflow_cloud/sensor.py index 1c7fa5fb377f80..aeab955878fdb6 100644 --- a/homeassistant/components/weatherflow_cloud/sensor.py +++ b/homeassistant/components/weatherflow_cloud/sensor.py @@ -180,11 +180,9 @@ async def async_setup_entry( entry.entry_id ] - stations = coordinator.data.keys() - async_add_entities( WeatherFlowCloudSensor(coordinator, sensor_description, station_id) - for station_id in stations + for station_id in coordinator.data for sensor_description in WF_SENSORS ) diff --git a/homeassistant/components/weatherflow_cloud/strings.json b/homeassistant/components/weatherflow_cloud/strings.json index df561c8b7532d3..f707cbb035383b 100644 --- a/homeassistant/components/weatherflow_cloud/strings.json +++ b/homeassistant/components/weatherflow_cloud/strings.json @@ -7,7 +7,7 @@ "api_token": "Personal api token" } }, - "reauth": { + "reauth_confirm": { "description": "Reauthenticate with WeatherFlow", "data": { "api_token": "[%key:component::weatherflow_cloud::config::step::user::data::api_token%]" diff --git a/homeassistant/components/webostv/icons.json b/homeassistant/components/webostv/icons.json index deb9729a99fff9..edc058d099fd37 100644 --- a/homeassistant/components/webostv/icons.json +++ b/homeassistant/components/webostv/icons.json @@ -1,7 +1,13 @@ { "services": { - "button": "mdi:button-pointer", - "command": "mdi:console", - "select_sound_output": "mdi:volume-source" + "button": { + "service": "mdi:button-pointer" + }, + "command": { + "service": "mdi:console" + }, + "select_sound_output": { + "service": "mdi:volume-source" + } } } diff --git a/homeassistant/components/websocket_api/commands.py b/homeassistant/components/websocket_api/commands.py index f66930c8d0049f..c9347012183938 100644 --- a/homeassistant/components/websocket_api/commands.py +++ b/homeassistant/components/websocket_api/commands.py @@ -36,6 +36,10 @@ ) from homeassistant.helpers import config_validation as cv, entity, template from homeassistant.helpers.dispatcher import async_dispatcher_connect +from homeassistant.helpers.entityfilter import ( + INCLUDE_EXCLUDE_BASE_FILTER_SCHEMA, + convert_include_exclude_filter, +) from homeassistant.helpers.event import ( TrackTemplate, TrackTemplateResult, @@ -366,14 +370,17 @@ def _send_handle_get_states_response( @callback def _forward_entity_changes( send_message: Callable[[str | bytes | dict[str, Any]], None], - entity_ids: set[str], + entity_ids: set[str] | None, + entity_filter: Callable[[str], bool] | None, user: User, message_id_as_bytes: bytes, event: Event[EventStateChangedData], ) -> None: """Forward entity state changed events to websocket.""" entity_id = event.data["entity_id"] - if entity_ids and entity_id not in entity_ids: + if (entity_ids and entity_id not in entity_ids) or ( + entity_filter and not entity_filter(entity_id) + ): return # We have to lookup the permissions again because the user might have # changed since the subscription was created. @@ -381,7 +388,7 @@ def _forward_entity_changes( if ( not user.is_admin and not permissions.access_all_entities(POLICY_READ) - and not permissions.check_entity(event.data["entity_id"], POLICY_READ) + and not permissions.check_entity(entity_id, POLICY_READ) ): return send_message(messages.cached_state_diff_message(message_id_as_bytes, event)) @@ -392,43 +399,55 @@ def _forward_entity_changes( { vol.Required("type"): "subscribe_entities", vol.Optional("entity_ids"): cv.entity_ids, + **INCLUDE_EXCLUDE_BASE_FILTER_SCHEMA.schema, } ) def handle_subscribe_entities( hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any] ) -> None: """Handle subscribe entities command.""" - entity_ids = set(msg.get("entity_ids", [])) + entity_ids = set(msg.get("entity_ids", [])) or None + _filter = convert_include_exclude_filter(msg) + entity_filter = None if _filter.empty_filter else _filter.get_filter() # We must never await between sending the states and listening for # state changed events or we will introduce a race condition # where some states are missed states = _async_get_allowed_states(hass, connection) - message_id_as_bytes = str(msg["id"]).encode() - connection.subscriptions[msg["id"]] = hass.bus.async_listen( + msg_id = msg["id"] + message_id_as_bytes = str(msg_id).encode() + connection.subscriptions[msg_id] = hass.bus.async_listen( EVENT_STATE_CHANGED, partial( _forward_entity_changes, connection.send_message, entity_ids, + entity_filter, connection.user, message_id_as_bytes, ), ) - connection.send_result(msg["id"]) + connection.send_result(msg_id) # JSON serialize here so we can recover if it blows up due to the # state machine containing unserializable data. This command is required # to succeed for the UI to show. try: - serialized_states = [ - state.as_compressed_state_json - for state in states - if not entity_ids or state.entity_id in entity_ids - ] + if entity_ids or entity_filter: + serialized_states = [ + state.as_compressed_state_json + for state in states + if (not entity_ids or state.entity_id in entity_ids) + and (not entity_filter or entity_filter(state.entity_id)) + ] + else: + # Fast path when not filtering + serialized_states = [state.as_compressed_state_json for state in states] except (ValueError, TypeError): pass else: - _send_handle_entities_init_response(connection, msg["id"], serialized_states) + _send_handle_entities_init_response( + connection, message_id_as_bytes, serialized_states + ) return serialized_states = [] @@ -443,18 +462,22 @@ def handle_subscribe_entities( ), ) - _send_handle_entities_init_response(connection, msg["id"], serialized_states) + _send_handle_entities_init_response( + connection, message_id_as_bytes, serialized_states + ) def _send_handle_entities_init_response( - connection: ActiveConnection, msg_id: int, serialized_states: list[bytes] + connection: ActiveConnection, + message_id_as_bytes: bytes, + serialized_states: list[bytes], ) -> None: """Send handle entities init response.""" connection.send_message( b"".join( ( b'{"id":', - str(msg_id).encode(), + message_id_as_bytes, b',"type":"event","event":{"a":{', b",".join(serialized_states), b"}}}", diff --git a/homeassistant/components/websocket_api/messages.py b/homeassistant/components/websocket_api/messages.py index 238f8be0c3b4d2..0a8200c5700649 100644 --- a/homeassistant/components/websocket_api/messages.py +++ b/homeassistant/components/websocket_api/messages.py @@ -224,9 +224,12 @@ def _state_diff_event( if (old_attributes := old_state.attributes) != ( new_attributes := new_state.attributes ): - for key, value in new_attributes.items(): - if old_attributes.get(key) != value: - additions.setdefault(COMPRESSED_STATE_ATTRIBUTES, {})[key] = value + if added := { + key: value + for key, value in new_attributes.items() + if key not in old_attributes or old_attributes[key] != value + }: + additions[COMPRESSED_STATE_ATTRIBUTES] = added if removed := old_attributes.keys() - new_attributes: # sets are not JSON serializable by default so we convert to list # here if there are any values to avoid jumping into the json_encoder_default diff --git a/homeassistant/components/wemo/icons.json b/homeassistant/components/wemo/icons.json index c5ddf5912d6dc3..af5024afcff9ee 100644 --- a/homeassistant/components/wemo/icons.json +++ b/homeassistant/components/wemo/icons.json @@ -1,6 +1,10 @@ { "services": { - "set_humidity": "mdi:water-percent", - "reset_filter_life": "mdi:refresh" + "set_humidity": { + "service": "mdi:water-percent" + }, + "reset_filter_life": { + "service": "mdi:refresh" + } } } diff --git a/homeassistant/components/wiffi/config_flow.py b/homeassistant/components/wiffi/config_flow.py index 17262dd0276ae5..6e4872ea400b0e 100644 --- a/homeassistant/components/wiffi/config_flow.py +++ b/homeassistant/components/wiffi/config_flow.py @@ -6,11 +6,17 @@ from __future__ import annotations import errno +from typing import Any import voluptuous as vol from wiffi import WiffiTcpServer -from homeassistant.config_entries import ConfigEntry, ConfigFlow, OptionsFlow +from homeassistant.config_entries import ( + ConfigEntry, + ConfigFlow, + ConfigFlowResult, + OptionsFlow, +) from homeassistant.const import CONF_PORT, CONF_TIMEOUT from homeassistant.core import callback @@ -30,7 +36,9 @@ def async_get_options_flow( """Create Wiffi server setup option flow.""" return OptionsFlowHandler(config_entry) - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the start of the config flow. Called after wiffi integration has been selected in the 'add integration diff --git a/homeassistant/components/wilight/config_flow.py b/homeassistant/components/wilight/config_flow.py index 52b3b426c201f6..babc011fc35159 100644 --- a/homeassistant/components/wilight/config_flow.py +++ b/homeassistant/components/wilight/config_flow.py @@ -24,13 +24,13 @@ class WiLightFlowHandler(ConfigFlow, domain=DOMAIN): VERSION = 1 - def __init__(self): + def __init__(self) -> None: """Initialize the WiLight flow.""" self._host = None self._serial_number = None self._title = None self._model_name = None - self._wilight_components = [] + self._wilight_components: list[str] = [] self._components_text = "" def _wilight_update(self, host, serial_number, model_name): diff --git a/homeassistant/components/wilight/icons.json b/homeassistant/components/wilight/icons.json index 3c5d0112de1ab3..48bcae2a3016ec 100644 --- a/homeassistant/components/wilight/icons.json +++ b/homeassistant/components/wilight/icons.json @@ -10,8 +10,14 @@ } }, "services": { - "set_watering_time": "mdi:timer", - "set_pause_time": "mdi:timer-pause", - "set_trigger": "mdi:gesture-tap-button" + "set_watering_time": { + "service": "mdi:timer" + }, + "set_pause_time": { + "service": "mdi:timer-pause" + }, + "set_trigger": { + "service": "mdi:gesture-tap-button" + } } } diff --git a/homeassistant/components/wled/config_flow.py b/homeassistant/components/wled/config_flow.py index 7853ad2101e33a..2798e0d46d1ab3 100644 --- a/homeassistant/components/wled/config_flow.py +++ b/homeassistant/components/wled/config_flow.py @@ -46,7 +46,9 @@ async def async_step_user( except WLEDConnectionError: errors["base"] = "cannot_connect" else: - await self.async_set_unique_id(device.info.mac_address) + await self.async_set_unique_id( + device.info.mac_address, raise_on_progress=False + ) self._abort_if_unique_id_configured( updates={CONF_HOST: user_input[CONF_HOST]} ) @@ -56,8 +58,6 @@ async def async_step_user( CONF_HOST: user_input[CONF_HOST], }, ) - else: - user_input = {} return self.async_show_form( step_id="user", diff --git a/homeassistant/components/wolflink/config_flow.py b/homeassistant/components/wolflink/config_flow.py index 6e218bfd1ce0ff..a2678580a231fa 100644 --- a/homeassistant/components/wolflink/config_flow.py +++ b/homeassistant/components/wolflink/config_flow.py @@ -1,13 +1,14 @@ """Config flow for Wolf SmartSet Service integration.""" import logging +from typing import Any from httpcore import ConnectError import voluptuous as vol from wolf_comm.token_auth import InvalidAuth from wolf_comm.wolf_client import WolfClient -from homeassistant.config_entries import ConfigFlow +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from .const import DEVICE_GATEWAY, DEVICE_ID, DEVICE_NAME, DOMAIN @@ -30,7 +31,9 @@ def __init__(self) -> None: self.password = None self.fetched_systems = None - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step to get connection parameters.""" errors = {} if user_input is not None: diff --git a/homeassistant/components/workday/binary_sensor.py b/homeassistant/components/workday/binary_sensor.py index 4635b2209a696d..33c2e249024a62 100644 --- a/homeassistant/components/workday/binary_sensor.py +++ b/homeassistant/components/workday/binary_sensor.py @@ -92,7 +92,7 @@ def _get_obj_holidays( subdiv=province, years=year, language=language, - categories=set_categories, # type: ignore[arg-type] + categories=set_categories, ) if (supported_languages := obj_holidays.supported_languages) and language == "en": for lang in supported_languages: @@ -102,7 +102,7 @@ def _get_obj_holidays( subdiv=province, years=year, language=lang, - categories=set_categories, # type: ignore[arg-type] + categories=set_categories, ) LOGGER.debug("Changing language from %s to %s", language, lang) return obj_holidays diff --git a/homeassistant/components/workday/icons.json b/homeassistant/components/workday/icons.json index 10d3c93a288611..ec5c64dce971bb 100644 --- a/homeassistant/components/workday/icons.json +++ b/homeassistant/components/workday/icons.json @@ -1,5 +1,7 @@ { "services": { - "check_date": "mdi:calendar-check" + "check_date": { + "service": "mdi:calendar-check" + } } } diff --git a/homeassistant/components/workday/manifest.json b/homeassistant/components/workday/manifest.json index 133c82454bc792..297b20b8c0e405 100644 --- a/homeassistant/components/workday/manifest.json +++ b/homeassistant/components/workday/manifest.json @@ -7,5 +7,5 @@ "iot_class": "local_polling", "loggers": ["holidays"], "quality_scale": "internal", - "requirements": ["holidays==0.54"] + "requirements": ["holidays==0.56"] } diff --git a/homeassistant/components/ws66i/config_flow.py b/homeassistant/components/ws66i/config_flow.py index b0cf6717e4dcea..330e9963f952ae 100644 --- a/homeassistant/components/ws66i/config_flow.py +++ b/homeassistant/components/ws66i/config_flow.py @@ -8,7 +8,12 @@ from pyws66i import WS66i, get_ws66i import voluptuous as vol -from homeassistant.config_entries import ConfigEntry, ConfigFlow, OptionsFlow +from homeassistant.config_entries import ( + ConfigEntry, + ConfigFlow, + ConfigFlowResult, + OptionsFlow, +) from homeassistant.const import CONF_IP_ADDRESS from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import HomeAssistantError @@ -94,7 +99,9 @@ class WS66iConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" errors = {} if user_input is not None: diff --git a/homeassistant/components/xbox/config_flow.py b/homeassistant/components/xbox/config_flow.py index e1434aac67cab0..86157be5d7f903 100644 --- a/homeassistant/components/xbox/config_flow.py +++ b/homeassistant/components/xbox/config_flow.py @@ -1,7 +1,9 @@ """Config flow for xbox.""" import logging +from typing import Any +from homeassistant.config_entries import ConfigFlowResult from homeassistant.helpers import config_entry_oauth2_flow from .const import DOMAIN @@ -25,7 +27,9 @@ def extra_authorize_data(self) -> dict: scopes = ["Xboxlive.signin", "Xboxlive.offline_access"] return {"scope": " ".join(scopes)} - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle a flow start.""" await self.async_set_unique_id(DOMAIN) diff --git a/homeassistant/components/xiaomi_aqara/config_flow.py b/homeassistant/components/xiaomi_aqara/config_flow.py index 8f391c8ddf3467..a89bb8447a364f 100644 --- a/homeassistant/components/xiaomi_aqara/config_flow.py +++ b/homeassistant/components/xiaomi_aqara/config_flow.py @@ -2,6 +2,7 @@ import logging from socket import gaierror +from typing import TYPE_CHECKING, Any import voluptuous as vol from xiaomi_gateway import MULTICAST_PORT, XiaomiGateway, XiaomiGatewayDiscovery @@ -49,13 +50,13 @@ class XiaomiAqaraFlowHandler(ConfigFlow, domain=DOMAIN): VERSION = 1 - def __init__(self): + def __init__(self) -> None: """Initialize.""" - self.host = None + self.host: str | None = None self.interface = DEFAULT_INTERFACE - self.sid = None - self.gateways = None - self.selected_gateway = None + self.sid: str | None = None + self.gateways: dict[str, XiaomiGateway] | None = None + self.selected_gateway: XiaomiGateway | None = None @callback def async_show_form_step_user(self, errors): @@ -66,9 +67,11 @@ def async_show_form_step_user(self, errors): return self.async_show_form(step_id="user", data_schema=schema, errors=errors) - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle a flow initialized by the user.""" - errors = {} + errors: dict[str, str] = {} if user_input is None: return self.async_show_form_step_user(errors) @@ -96,6 +99,8 @@ async def async_step_user(self, user_input=None): None, ) + if TYPE_CHECKING: + assert self.selected_gateway if self.selected_gateway.connection_error: errors[CONF_HOST] = "invalid_host" if self.selected_gateway.mac_error: @@ -115,6 +120,8 @@ async def async_step_user(self, user_input=None): self.gateways = xiaomi.gateways + if TYPE_CHECKING: + assert self.gateways is not None if len(self.gateways) == 1: self.selected_gateway = list(self.gateways.values())[0] self.sid = self.selected_gateway.sid diff --git a/homeassistant/components/xiaomi_aqara/icons.json b/homeassistant/components/xiaomi_aqara/icons.json index 4975414833dce6..62149b0dd402af 100644 --- a/homeassistant/components/xiaomi_aqara/icons.json +++ b/homeassistant/components/xiaomi_aqara/icons.json @@ -1,8 +1,16 @@ { "services": { - "add_device": "mdi:cellphone-link", - "play_ringtone": "mdi:music", - "remove_device": "mdi:cellphone-link", - "stop_ringtone": "mdi:music-off" + "add_device": { + "service": "mdi:cellphone-link" + }, + "play_ringtone": { + "service": "mdi:music" + }, + "remove_device": { + "service": "mdi:cellphone-link" + }, + "stop_ringtone": { + "service": "mdi:music-off" + } } } diff --git a/homeassistant/components/xiaomi_ble/binary_sensor.py b/homeassistant/components/xiaomi_ble/binary_sensor.py index 5336c4d8f7fcf4..b853f83b9676da 100644 --- a/homeassistant/components/xiaomi_ble/binary_sensor.py +++ b/homeassistant/components/xiaomi_ble/binary_sensor.py @@ -50,6 +50,10 @@ key=XiaomiBinarySensorDeviceClass.MOTION, device_class=BinarySensorDeviceClass.MOTION, ), + XiaomiBinarySensorDeviceClass.OCCUPANCY: BinarySensorEntityDescription( + key=XiaomiBinarySensorDeviceClass.OCCUPANCY, + device_class=BinarySensorDeviceClass.OCCUPANCY, + ), XiaomiBinarySensorDeviceClass.OPENING: BinarySensorEntityDescription( key=XiaomiBinarySensorDeviceClass.OPENING, device_class=BinarySensorDeviceClass.OPENING, diff --git a/homeassistant/components/xiaomi_ble/manifest.json b/homeassistant/components/xiaomi_ble/manifest.json index 21e9bc45bb8183..da7169635e94af 100644 --- a/homeassistant/components/xiaomi_ble/manifest.json +++ b/homeassistant/components/xiaomi_ble/manifest.json @@ -24,5 +24,5 @@ "dependencies": ["bluetooth_adapters"], "documentation": "https://www.home-assistant.io/integrations/xiaomi_ble", "iot_class": "local_push", - "requirements": ["xiaomi-ble==0.30.2"] + "requirements": ["xiaomi-ble==0.31.1"] } diff --git a/homeassistant/components/xiaomi_ble/sensor.py b/homeassistant/components/xiaomi_ble/sensor.py index 3108c285dbe99a..891caaf3e68a0e 100644 --- a/homeassistant/components/xiaomi_ble/sensor.py +++ b/homeassistant/components/xiaomi_ble/sensor.py @@ -155,6 +155,24 @@ (ExtendedSensorDeviceClass.LOCK_METHOD, None): SensorEntityDescription( key=str(ExtendedSensorDeviceClass.LOCK_METHOD), icon="mdi:key-variant" ), + # Duration of detected status (in minutes) for Occpancy Sensor + ( + ExtendedSensorDeviceClass.DURATION_DETECTED, + Units.TIME_MINUTES, + ): SensorEntityDescription( + key=str(ExtendedSensorDeviceClass.DURATION_DETECTED), + native_unit_of_measurement=UnitOfTime.MINUTES, + state_class=SensorStateClass.MEASUREMENT, + ), + # Duration of cleared status (in minutes) for Occpancy Sensor + ( + ExtendedSensorDeviceClass.DURATION_CLEARED, + Units.TIME_MINUTES, + ): SensorEntityDescription( + key=str(ExtendedSensorDeviceClass.DURATION_CLEARED), + native_unit_of_measurement=UnitOfTime.MINUTES, + state_class=SensorStateClass.MEASUREMENT, + ), } diff --git a/homeassistant/components/xiaomi_miio/icons.json b/homeassistant/components/xiaomi_miio/icons.json index 2e5084a1f6c8bd..cc0800f1d9d30d 100644 --- a/homeassistant/components/xiaomi_miio/icons.json +++ b/homeassistant/components/xiaomi_miio/icons.json @@ -14,29 +14,77 @@ } }, "services": { - "fan_reset_filter": "mdi:refresh", - "fan_set_extra_features": "mdi:cog", - "light_set_scene": "mdi:palette", - "light_set_delayed_turn_off": "mdi:timer", - "light_reminder_on": "mdi:alarm", - "light_reminder_off": "mdi:alarm-off", - "light_night_light_mode_on": "mdi:weather-night", - "light_night_light_mode_off": "mdi:weather-sunny", - "light_eyecare_mode_on": "mdi:eye", - "light_eyecare_mode_off": "mdi:eye-off", - "remote_learn_command": "mdi:remote", - "remote_set_led_on": "mdi:led-on", - "remote_set_led_off": "mdi:led-off", - "switch_set_wifi_led_on": "mdi:wifi", - "switch_set_wifi_led_off": "mdi:wifi-off", - "switch_set_power_price": "mdi:currency-usd", - "switch_set_power_mode": "mdi:power", - "vacuum_remote_control_start": "mdi:play", - "vacuum_remote_control_stop": "mdi:stop", - "vacuum_remote_control_move": "mdi:remote", - "vacuum_remote_control_move_step": "mdi:remote", - "vacuum_clean_zone": "mdi:map-marker", - "vacuum_goto": "mdi:map-marker", - "vacuum_clean_segment": "mdi:map-marker" + "fan_reset_filter": { + "service": "mdi:refresh" + }, + "fan_set_extra_features": { + "service": "mdi:cog" + }, + "light_set_scene": { + "service": "mdi:palette" + }, + "light_set_delayed_turn_off": { + "service": "mdi:timer" + }, + "light_reminder_on": { + "service": "mdi:alarm" + }, + "light_reminder_off": { + "service": "mdi:alarm-off" + }, + "light_night_light_mode_on": { + "service": "mdi:weather-night" + }, + "light_night_light_mode_off": { + "service": "mdi:weather-sunny" + }, + "light_eyecare_mode_on": { + "service": "mdi:eye" + }, + "light_eyecare_mode_off": { + "service": "mdi:eye-off" + }, + "remote_learn_command": { + "service": "mdi:remote" + }, + "remote_set_led_on": { + "service": "mdi:led-on" + }, + "remote_set_led_off": { + "service": "mdi:led-off" + }, + "switch_set_wifi_led_on": { + "service": "mdi:wifi" + }, + "switch_set_wifi_led_off": { + "service": "mdi:wifi-off" + }, + "switch_set_power_price": { + "service": "mdi:currency-usd" + }, + "switch_set_power_mode": { + "service": "mdi:power" + }, + "vacuum_remote_control_start": { + "service": "mdi:play" + }, + "vacuum_remote_control_stop": { + "service": "mdi:stop" + }, + "vacuum_remote_control_move": { + "service": "mdi:remote" + }, + "vacuum_remote_control_move_step": { + "service": "mdi:remote" + }, + "vacuum_clean_zone": { + "service": "mdi:map-marker" + }, + "vacuum_goto": { + "service": "mdi:map-marker" + }, + "vacuum_clean_segment": { + "service": "mdi:map-marker" + } } } diff --git a/homeassistant/components/yale/__init__.py b/homeassistant/components/yale/__init__.py new file mode 100644 index 00000000000000..1cbd9c87b572e7 --- /dev/null +++ b/homeassistant/components/yale/__init__.py @@ -0,0 +1,81 @@ +"""Support for Yale devices.""" + +from __future__ import annotations + +from pathlib import Path +from typing import cast + +from aiohttp import ClientResponseError +from yalexs.const import Brand +from yalexs.exceptions import YaleApiError +from yalexs.manager.const import CONF_BRAND +from yalexs.manager.exceptions import CannotConnect, InvalidAuth, RequireValidation +from yalexs.manager.gateway import Config as YaleXSConfig + +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import EVENT_HOMEASSISTANT_STOP +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady +from homeassistant.helpers import config_entry_oauth2_flow, device_registry as dr + +from .const import DOMAIN, PLATFORMS +from .data import YaleData +from .gateway import YaleGateway +from .util import async_create_yale_clientsession + +type YaleConfigEntry = ConfigEntry[YaleData] + + +async def async_setup_entry(hass: HomeAssistant, entry: YaleConfigEntry) -> bool: + """Set up yale from a config entry.""" + session = async_create_yale_clientsession(hass) + implementation = ( + await config_entry_oauth2_flow.async_get_config_entry_implementation( + hass, entry + ) + ) + oauth_session = config_entry_oauth2_flow.OAuth2Session(hass, entry, implementation) + yale_gateway = YaleGateway(Path(hass.config.config_dir), session, oauth_session) + try: + await async_setup_yale(hass, entry, yale_gateway) + except (RequireValidation, InvalidAuth) as err: + raise ConfigEntryAuthFailed from err + except TimeoutError as err: + raise ConfigEntryNotReady("Timed out connecting to yale api") from err + except (YaleApiError, ClientResponseError, CannotConnect) as err: + raise ConfigEntryNotReady from err + await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) + return True + + +async def async_unload_entry(hass: HomeAssistant, entry: YaleConfigEntry) -> bool: + """Unload a config entry.""" + return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) + + +async def async_setup_yale( + hass: HomeAssistant, entry: YaleConfigEntry, yale_gateway: YaleGateway +) -> None: + """Set up the yale component.""" + config = cast(YaleXSConfig, entry.data) + await yale_gateway.async_setup({**config, CONF_BRAND: Brand.YALE_GLOBAL}) + await yale_gateway.async_authenticate() + await yale_gateway.async_refresh_access_token_if_needed() + data = entry.runtime_data = YaleData(hass, yale_gateway) + entry.async_on_unload( + hass.bus.async_listen(EVENT_HOMEASSISTANT_STOP, data.async_stop) + ) + entry.async_on_unload(data.async_stop) + await data.async_setup() + + +async def async_remove_config_entry_device( + hass: HomeAssistant, config_entry: YaleConfigEntry, device_entry: dr.DeviceEntry +) -> bool: + """Remove yale config entry from a device if its no longer present.""" + return not any( + identifier + for identifier in device_entry.identifiers + if identifier[0] == DOMAIN + and config_entry.runtime_data.get_device(identifier[1]) + ) diff --git a/homeassistant/components/yale/application_credentials.py b/homeassistant/components/yale/application_credentials.py new file mode 100644 index 00000000000000..31b5b7a92c787b --- /dev/null +++ b/homeassistant/components/yale/application_credentials.py @@ -0,0 +1,15 @@ +"""application_credentials platform the yale integration.""" + +from homeassistant.components.application_credentials import AuthorizationServer +from homeassistant.core import HomeAssistant + +OAUTH2_AUTHORIZE = "https://oauth.aaecosystem.com/authorize" +OAUTH2_TOKEN = "https://oauth.aaecosystem.com/access_token" + + +async def async_get_authorization_server(hass: HomeAssistant) -> AuthorizationServer: + """Return authorization server.""" + return AuthorizationServer( + authorize_url=OAUTH2_AUTHORIZE, + token_url=OAUTH2_TOKEN, + ) diff --git a/homeassistant/components/yale/binary_sensor.py b/homeassistant/components/yale/binary_sensor.py new file mode 100644 index 00000000000000..dbb00ad7d42dbf --- /dev/null +++ b/homeassistant/components/yale/binary_sensor.py @@ -0,0 +1,188 @@ +"""Support for Yale binary sensors.""" + +from __future__ import annotations + +from collections.abc import Callable +from dataclasses import dataclass +from datetime import datetime, timedelta +from functools import partial +import logging + +from yalexs.activity import Activity, ActivityType +from yalexs.doorbell import DoorbellDetail +from yalexs.lock import LockDetail, LockDoorStatus +from yalexs.manager.const import ACTIVITY_UPDATE_INTERVAL +from yalexs.util import update_lock_detail_from_activity + +from homeassistant.components.binary_sensor import ( + BinarySensorDeviceClass, + BinarySensorEntity, + BinarySensorEntityDescription, +) +from homeassistant.const import EntityCategory +from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.event import async_call_later + +from . import YaleConfigEntry, YaleData +from .entity import YaleDescriptionEntity +from .util import ( + retrieve_ding_activity, + retrieve_doorbell_motion_activity, + retrieve_online_state, + retrieve_time_based_activity, +) + +_LOGGER = logging.getLogger(__name__) + +TIME_TO_RECHECK_DETECTION = timedelta( + seconds=ACTIVITY_UPDATE_INTERVAL.total_seconds() * 3 +) + + +@dataclass(frozen=True, kw_only=True) +class YaleDoorbellBinarySensorEntityDescription(BinarySensorEntityDescription): + """Describes Yale binary_sensor entity.""" + + value_fn: Callable[[YaleData, DoorbellDetail | LockDetail], Activity | None] + is_time_based: bool + + +SENSOR_TYPE_DOOR = BinarySensorEntityDescription( + key="open", + device_class=BinarySensorDeviceClass.DOOR, +) + +SENSOR_TYPES_VIDEO_DOORBELL = ( + YaleDoorbellBinarySensorEntityDescription( + key="motion", + device_class=BinarySensorDeviceClass.MOTION, + value_fn=retrieve_doorbell_motion_activity, + is_time_based=True, + ), + YaleDoorbellBinarySensorEntityDescription( + key="image capture", + translation_key="image_capture", + value_fn=partial( + retrieve_time_based_activity, {ActivityType.DOORBELL_IMAGE_CAPTURE} + ), + is_time_based=True, + ), + YaleDoorbellBinarySensorEntityDescription( + key="online", + device_class=BinarySensorDeviceClass.CONNECTIVITY, + entity_category=EntityCategory.DIAGNOSTIC, + value_fn=retrieve_online_state, + is_time_based=False, + ), +) + + +SENSOR_TYPES_DOORBELL: tuple[YaleDoorbellBinarySensorEntityDescription, ...] = ( + YaleDoorbellBinarySensorEntityDescription( + key="ding", + translation_key="ding", + device_class=BinarySensorDeviceClass.OCCUPANCY, + value_fn=retrieve_ding_activity, + is_time_based=True, + ), +) + + +async def async_setup_entry( + hass: HomeAssistant, + config_entry: YaleConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up the Yale binary sensors.""" + data = config_entry.runtime_data + entities: list[BinarySensorEntity] = [] + + for lock in data.locks: + detail = data.get_device_detail(lock.device_id) + if detail.doorsense: + entities.append(YaleDoorBinarySensor(data, lock, SENSOR_TYPE_DOOR)) + + if detail.doorbell: + entities.extend( + YaleDoorbellBinarySensor(data, lock, description) + for description in SENSOR_TYPES_DOORBELL + ) + + entities.extend( + YaleDoorbellBinarySensor(data, doorbell, description) + for description in SENSOR_TYPES_DOORBELL + SENSOR_TYPES_VIDEO_DOORBELL + for doorbell in data.doorbells + ) + async_add_entities(entities) + + +class YaleDoorBinarySensor(YaleDescriptionEntity, BinarySensorEntity): + """Representation of an Yale Door binary sensor.""" + + _attr_device_class = BinarySensorDeviceClass.DOOR + description: BinarySensorEntityDescription + + @callback + def _update_from_data(self) -> None: + """Get the latest state of the sensor and update activity.""" + if door_activity := self._get_latest({ActivityType.DOOR_OPERATION}): + update_lock_detail_from_activity(self._detail, door_activity) + if door_activity.was_pushed: + self._detail.set_online(True) + + if bridge_activity := self._get_latest({ActivityType.BRIDGE_OPERATION}): + update_lock_detail_from_activity(self._detail, bridge_activity) + self._attr_available = self._detail.bridge_is_online + self._attr_is_on = self._detail.door_state == LockDoorStatus.OPEN + + +class YaleDoorbellBinarySensor(YaleDescriptionEntity, BinarySensorEntity): + """Representation of an Yale binary sensor.""" + + entity_description: YaleDoorbellBinarySensorEntityDescription + _check_for_off_update_listener: Callable[[], None] | None = None + + @callback + def _update_from_data(self) -> None: + """Get the latest state of the sensor.""" + self._cancel_any_pending_updates() + self._attr_is_on = bool( + self.entity_description.value_fn(self._data, self._detail) + ) + + if self.entity_description.is_time_based: + self._attr_available = retrieve_online_state(self._data, self._detail) + self._schedule_update_to_recheck_turn_off_sensor() + else: + self._attr_available = True + + @callback + def _async_scheduled_update(self, now: datetime) -> None: + """Timer callback for sensor update.""" + self._check_for_off_update_listener = None + self._update_from_data() + if not self.is_on: + self.async_write_ha_state() + + def _schedule_update_to_recheck_turn_off_sensor(self) -> None: + """Schedule an update to recheck the sensor to see if it is ready to turn off.""" + # If the sensor is already off there is nothing to do + if not self.is_on: + return + self._check_for_off_update_listener = async_call_later( + self.hass, TIME_TO_RECHECK_DETECTION, self._async_scheduled_update + ) + + def _cancel_any_pending_updates(self) -> None: + """Cancel any updates to recheck a sensor to see if it is ready to turn off.""" + if not self._check_for_off_update_listener: + return + _LOGGER.debug("%s: canceled pending update", self.entity_id) + self._check_for_off_update_listener() + self._check_for_off_update_listener = None + + async def async_will_remove_from_hass(self) -> None: + """When removing cancel any scheduled updates.""" + self._cancel_any_pending_updates() + await super().async_will_remove_from_hass() diff --git a/homeassistant/components/yale/button.py b/homeassistant/components/yale/button.py new file mode 100644 index 00000000000000..b04ad638f0cf27 --- /dev/null +++ b/homeassistant/components/yale/button.py @@ -0,0 +1,32 @@ +"""Support for Yale buttons.""" + +from homeassistant.components.button import ButtonEntity +from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from . import YaleConfigEntry +from .entity import YaleEntity + + +async def async_setup_entry( + hass: HomeAssistant, + config_entry: YaleConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up Yale lock wake buttons.""" + data = config_entry.runtime_data + async_add_entities(YaleWakeLockButton(data, lock, "wake") for lock in data.locks) + + +class YaleWakeLockButton(YaleEntity, ButtonEntity): + """Representation of an Yale lock wake button.""" + + _attr_translation_key = "wake" + + async def async_press(self) -> None: + """Wake the device.""" + await self._data.async_status_async(self._device_id, self._hyper_bridge) + + @callback + def _update_from_data(self) -> None: + """Nothing to update as buttons are stateless.""" diff --git a/homeassistant/components/yale/camera.py b/homeassistant/components/yale/camera.py new file mode 100644 index 00000000000000..217e8f5f6fd494 --- /dev/null +++ b/homeassistant/components/yale/camera.py @@ -0,0 +1,90 @@ +"""Support for Yale doorbell camera.""" + +from __future__ import annotations + +import logging + +from aiohttp import ClientSession +from yalexs.activity import ActivityType +from yalexs.doorbell import Doorbell +from yalexs.util import update_doorbell_image_from_activity + +from homeassistant.components.camera import Camera +from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers import aiohttp_client +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from . import YaleConfigEntry, YaleData +from .const import DEFAULT_NAME, DEFAULT_TIMEOUT +from .entity import YaleEntity + +_LOGGER = logging.getLogger(__name__) + + +async def async_setup_entry( + hass: HomeAssistant, + config_entry: YaleConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up Yale cameras.""" + data = config_entry.runtime_data + # Create an aiohttp session instead of using the default one since the + # default one is likely to trigger yale's WAF if another integration + # is also using Cloudflare + session = aiohttp_client.async_create_clientsession(hass) + async_add_entities( + YaleCamera(data, doorbell, session, DEFAULT_TIMEOUT) + for doorbell in data.doorbells + ) + + +class YaleCamera(YaleEntity, Camera): + """An implementation of an Yale security camera.""" + + _attr_translation_key = "camera" + _attr_motion_detection_enabled = True + _attr_brand = DEFAULT_NAME + _image_url: str | None = None + _image_content: bytes | None = None + + def __init__( + self, data: YaleData, device: Doorbell, session: ClientSession, timeout: int + ) -> None: + """Initialize an Yale security camera.""" + super().__init__(data, device, "camera") + self._timeout = timeout + self._session = session + self._attr_model = self._detail.model + + @property + def is_recording(self) -> bool: + """Return true if the device is recording.""" + return self._device.has_subscription + + async def _async_update(self): + """Update device.""" + _LOGGER.debug("async_update called %s", self._detail.device_name) + await self._data.refresh_camera_by_id(self._device_id) + self._update_from_data() + + @callback + def _update_from_data(self) -> None: + """Get the latest state of the sensor.""" + if doorbell_activity := self._get_latest( + {ActivityType.DOORBELL_MOTION, ActivityType.DOORBELL_IMAGE_CAPTURE} + ): + update_doorbell_image_from_activity(self._detail, doorbell_activity) + + async def async_camera_image( + self, width: int | None = None, height: int | None = None + ) -> bytes | None: + """Return bytes of camera image.""" + self._update_from_data() + + if self._image_url is not self._detail.image_url: + self._image_content = await self._data.async_get_doorbell_image( + self._device_id, self._session, timeout=self._timeout + ) + self._image_url = self._detail.image_url + + return self._image_content diff --git a/homeassistant/components/yale/config_flow.py b/homeassistant/components/yale/config_flow.py new file mode 100644 index 00000000000000..6cbc9543ea4170 --- /dev/null +++ b/homeassistant/components/yale/config_flow.py @@ -0,0 +1,60 @@ +"""Config flow for Yale integration.""" + +from collections.abc import Mapping +import logging +from typing import Any + +import jwt + +from homeassistant.config_entries import ConfigEntry, ConfigFlowResult +from homeassistant.helpers import config_entry_oauth2_flow + +from .const import DOMAIN + +_LOGGER = logging.getLogger(__name__) + + +class YaleConfigFlow(config_entry_oauth2_flow.AbstractOAuth2FlowHandler, domain=DOMAIN): + """Handle a config flow for Yale.""" + + VERSION = 1 + DOMAIN = DOMAIN + reauth_entry: ConfigEntry | None = None + + @property + def logger(self) -> logging.Logger: + """Return logger.""" + return _LOGGER + + async def async_step_reauth( + self, entry_data: Mapping[str, Any] + ) -> ConfigFlowResult: + """Handle configuration by re-auth.""" + self.reauth_entry = self.hass.config_entries.async_get_entry( + self.context["entry_id"] + ) + return await self.async_step_user() + + def _async_get_user_id_from_access_token(self, encoded: str) -> str: + """Get user ID from access token.""" + decoded = jwt.decode( + encoded, + "", + verify=False, + options={"verify_signature": False}, + algorithms=["HS256"], + ) + return decoded["userId"] + + async def async_oauth_create_entry(self, data: dict) -> ConfigFlowResult: + """Create an entry for the flow.""" + user_id = self._async_get_user_id_from_access_token( + data["token"]["access_token"] + ) + if entry := self.reauth_entry: + if entry.unique_id != user_id: + return self.async_abort(reason="reauth_invalid_user") + return self.async_update_reload_and_abort(entry, data=data) + await self.async_set_unique_id(user_id) + self._abort_if_unique_id_configured() + return await super().async_oauth_create_entry(data) diff --git a/homeassistant/components/yale/const.py b/homeassistant/components/yale/const.py new file mode 100644 index 00000000000000..3da4fb1dfb41f4 --- /dev/null +++ b/homeassistant/components/yale/const.py @@ -0,0 +1,43 @@ +"""Constants for Yale devices.""" + +from homeassistant.const import Platform + +DEFAULT_TIMEOUT = 25 + +CONF_ACCESS_TOKEN_CACHE_FILE = "access_token_cache_file" +CONF_BRAND = "brand" +CONF_LOGIN_METHOD = "login_method" +CONF_INSTALL_ID = "install_id" + +VERIFICATION_CODE_KEY = "verification_code" + +MANUFACTURER = "Yale Home Inc." + +DEFAULT_NAME = "Yale" +DOMAIN = "yale" + +OPERATION_METHOD_AUTORELOCK = "autorelock" +OPERATION_METHOD_REMOTE = "remote" +OPERATION_METHOD_KEYPAD = "keypad" +OPERATION_METHOD_MANUAL = "manual" +OPERATION_METHOD_TAG = "tag" +OPERATION_METHOD_MOBILE_DEVICE = "mobile" + +ATTR_OPERATION_AUTORELOCK = "autorelock" +ATTR_OPERATION_METHOD = "method" +ATTR_OPERATION_REMOTE = "remote" +ATTR_OPERATION_KEYPAD = "keypad" +ATTR_OPERATION_MANUAL = "manual" +ATTR_OPERATION_TAG = "tag" + +LOGIN_METHODS = ["phone", "email"] +DEFAULT_LOGIN_METHOD = "email" + +PLATFORMS = [ + Platform.BINARY_SENSOR, + Platform.BUTTON, + Platform.CAMERA, + Platform.EVENT, + Platform.LOCK, + Platform.SENSOR, +] diff --git a/homeassistant/components/yale/data.py b/homeassistant/components/yale/data.py new file mode 100644 index 00000000000000..12736f7733d0ff --- /dev/null +++ b/homeassistant/components/yale/data.py @@ -0,0 +1,52 @@ +"""Support for Yale devices.""" + +from __future__ import annotations + +from yalexs.lock import LockDetail +from yalexs.manager.data import YaleXSData +from yalexs_ble import YaleXSBLEDiscovery + +from homeassistant.config_entries import SOURCE_INTEGRATION_DISCOVERY +from homeassistant.core import HomeAssistant, callback +from homeassistant.exceptions import HomeAssistantError +from homeassistant.helpers import discovery_flow + +from .gateway import YaleGateway + +YALEXS_BLE_DOMAIN = "yalexs_ble" + + +@callback +def _async_trigger_ble_lock_discovery( + hass: HomeAssistant, locks_with_offline_keys: list[LockDetail] +) -> None: + """Update keys for the yalexs-ble integration if available.""" + for lock_detail in locks_with_offline_keys: + discovery_flow.async_create_flow( + hass, + YALEXS_BLE_DOMAIN, + context={"source": SOURCE_INTEGRATION_DISCOVERY}, + data=YaleXSBLEDiscovery( + { + "name": lock_detail.device_name, + "address": lock_detail.mac_address, + "serial": lock_detail.serial_number, + "key": lock_detail.offline_key, + "slot": lock_detail.offline_slot, + } + ), + ) + + +class YaleData(YaleXSData): + """yale data object.""" + + def __init__(self, hass: HomeAssistant, yale_gateway: YaleGateway) -> None: + """Init yale data object.""" + self._hass = hass + super().__init__(yale_gateway, HomeAssistantError) + + @callback + def async_offline_key_discovered(self, detail: LockDetail) -> None: + """Handle offline key discovery.""" + _async_trigger_ble_lock_discovery(self._hass, [detail]) diff --git a/homeassistant/components/yale/diagnostics.py b/homeassistant/components/yale/diagnostics.py new file mode 100644 index 00000000000000..7e7f6179e7af78 --- /dev/null +++ b/homeassistant/components/yale/diagnostics.py @@ -0,0 +1,50 @@ +"""Diagnostics support for yale.""" + +from __future__ import annotations + +from typing import Any + +from yalexs.const import Brand + +from homeassistant.components.diagnostics import async_redact_data +from homeassistant.core import HomeAssistant + +from . import YaleConfigEntry + +TO_REDACT = { + "HouseID", + "OfflineKeys", + "installUserID", + "invitations", + "key", + "pins", + "pubsubChannel", + "recentImage", + "remoteOperateSecret", + "users", + "zWaveDSK", + "contentToken", +} + + +async def async_get_config_entry_diagnostics( + hass: HomeAssistant, entry: YaleConfigEntry +) -> dict[str, Any]: + """Return diagnostics for a config entry.""" + data = entry.runtime_data + + return { + "locks": { + lock.device_id: async_redact_data( + data.get_device_detail(lock.device_id).raw, TO_REDACT + ) + for lock in data.locks + }, + "doorbells": { + doorbell.device_id: async_redact_data( + data.get_device_detail(doorbell.device_id).raw, TO_REDACT + ) + for doorbell in data.doorbells + }, + "brand": Brand.YALE_GLOBAL.value, + } diff --git a/homeassistant/components/yale/entity.py b/homeassistant/components/yale/entity.py new file mode 100644 index 00000000000000..152070c0be33ed --- /dev/null +++ b/homeassistant/components/yale/entity.py @@ -0,0 +1,115 @@ +"""Base class for Yale entity.""" + +from abc import abstractmethod + +from yalexs.activity import Activity, ActivityType +from yalexs.doorbell import Doorbell, DoorbellDetail +from yalexs.keypad import KeypadDetail +from yalexs.lock import Lock, LockDetail +from yalexs.util import get_configuration_url + +from homeassistant.const import ATTR_CONNECTIONS +from homeassistant.core import callback +from homeassistant.helpers import device_registry as dr +from homeassistant.helpers.device_registry import DeviceInfo +from homeassistant.helpers.entity import Entity, EntityDescription + +from . import DOMAIN, YaleData +from .const import MANUFACTURER + +DEVICE_TYPES = ["keypad", "lock", "camera", "doorbell", "door", "bell"] + + +class YaleEntity(Entity): + """Base implementation for Yale device.""" + + _attr_should_poll = False + _attr_has_entity_name = True + + def __init__( + self, data: YaleData, device: Doorbell | Lock | KeypadDetail, unique_id: str + ) -> None: + """Initialize an Yale device.""" + super().__init__() + self._data = data + self._stream = data.activity_stream + self._device = device + detail = self._detail + self._device_id = device.device_id + self._attr_unique_id = f"{device.device_id}_{unique_id}" + self._attr_device_info = DeviceInfo( + identifiers={(DOMAIN, self._device_id)}, + manufacturer=MANUFACTURER, + model=detail.model, + name=device.device_name, + sw_version=detail.firmware_version, + suggested_area=_remove_device_types(device.device_name, DEVICE_TYPES), + configuration_url=get_configuration_url(data.brand), + ) + if isinstance(detail, LockDetail) and (mac := detail.mac_address): + self._attr_device_info[ATTR_CONNECTIONS] = {(dr.CONNECTION_BLUETOOTH, mac)} + + @property + def _detail(self) -> DoorbellDetail | LockDetail: + return self._data.get_device_detail(self._device.device_id) + + @property + def _hyper_bridge(self) -> bool: + """Check if the lock has a paired hyper bridge.""" + return bool(self._detail.bridge and self._detail.bridge.hyper_bridge) + + @callback + def _get_latest(self, activity_types: set[ActivityType]) -> Activity | None: + """Get the latest activity for the device.""" + return self._stream.get_latest_device_activity(self._device_id, activity_types) + + @callback + def _update_from_data_and_write_state(self) -> None: + self._update_from_data() + self.async_write_ha_state() + + @abstractmethod + def _update_from_data(self) -> None: + """Update the entity state from the data object.""" + + async def async_added_to_hass(self) -> None: + """Subscribe to updates.""" + self.async_on_remove( + self._data.async_subscribe_device_id( + self._device_id, self._update_from_data_and_write_state + ) + ) + self.async_on_remove( + self._stream.async_subscribe_device_id( + self._device_id, self._update_from_data_and_write_state + ) + ) + self._update_from_data() + + +class YaleDescriptionEntity(YaleEntity): + """An Yale entity with a description.""" + + def __init__( + self, + data: YaleData, + device: Doorbell | Lock | KeypadDetail, + description: EntityDescription, + ) -> None: + """Initialize an Yale entity with a description.""" + super().__init__(data, device, description.key) + self.entity_description = description + + +def _remove_device_types(name: str, device_types: list[str]) -> str: + """Strip device types from a string. + + Yale stores the name as Master Bed Lock + or Master Bed Door. We can come up with a + reasonable suggestion by removing the supported + device types from the string. + """ + lower_name = name.lower() + for device_type in device_types: + lower_name = lower_name.removesuffix(f" {device_type}") + return name[: len(lower_name)] diff --git a/homeassistant/components/yale/event.py b/homeassistant/components/yale/event.py new file mode 100644 index 00000000000000..935ba7376f8208 --- /dev/null +++ b/homeassistant/components/yale/event.py @@ -0,0 +1,98 @@ +"""Support for yale events.""" + +from __future__ import annotations + +from collections.abc import Callable +from dataclasses import dataclass +from typing import TYPE_CHECKING + +from yalexs.activity import Activity +from yalexs.doorbell import DoorbellDetail +from yalexs.lock import LockDetail + +from homeassistant.components.event import ( + EventDeviceClass, + EventEntity, + EventEntityDescription, +) +from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from . import YaleConfigEntry, YaleData +from .entity import YaleDescriptionEntity +from .util import ( + retrieve_ding_activity, + retrieve_doorbell_motion_activity, + retrieve_online_state, +) + + +@dataclass(kw_only=True, frozen=True) +class YaleEventEntityDescription(EventEntityDescription): + """Describe yale event entities.""" + + value_fn: Callable[[YaleData, DoorbellDetail | LockDetail], Activity | None] + + +TYPES_VIDEO_DOORBELL: tuple[YaleEventEntityDescription, ...] = ( + YaleEventEntityDescription( + key="motion", + translation_key="motion", + device_class=EventDeviceClass.MOTION, + event_types=["motion"], + value_fn=retrieve_doorbell_motion_activity, + ), +) + + +TYPES_DOORBELL: tuple[YaleEventEntityDescription, ...] = ( + YaleEventEntityDescription( + key="doorbell", + translation_key="doorbell", + device_class=EventDeviceClass.DOORBELL, + event_types=["ring"], + value_fn=retrieve_ding_activity, + ), +) + + +async def async_setup_entry( + hass: HomeAssistant, + config_entry: YaleConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up the yale event platform.""" + data = config_entry.runtime_data + entities: list[YaleEventEntity] = [ + YaleEventEntity(data, lock, description) + for description in TYPES_DOORBELL + for lock in data.locks + if (detail := data.get_device_detail(lock.device_id)) and detail.doorbell + ] + entities.extend( + YaleEventEntity(data, doorbell, description) + for description in TYPES_DOORBELL + TYPES_VIDEO_DOORBELL + for doorbell in data.doorbells + ) + async_add_entities(entities) + + +class YaleEventEntity(YaleDescriptionEntity, EventEntity): + """An yale event entity.""" + + entity_description: YaleEventEntityDescription + _last_activity: Activity | None = None + + @callback + def _update_from_data(self) -> None: + """Update from data.""" + self._attr_available = retrieve_online_state(self._data, self._detail) + current_activity = self.entity_description.value_fn(self._data, self._detail) + if not current_activity or current_activity == self._last_activity: + return + self._last_activity = current_activity + event_types = self.entity_description.event_types + if TYPE_CHECKING: + assert event_types is not None + self._trigger_event(event_type=event_types[0]) + self.async_write_ha_state() diff --git a/homeassistant/components/yale/gateway.py b/homeassistant/components/yale/gateway.py new file mode 100644 index 00000000000000..cd7796182d27af --- /dev/null +++ b/homeassistant/components/yale/gateway.py @@ -0,0 +1,43 @@ +"""Handle Yale connection setup and authentication.""" + +import logging +from pathlib import Path + +from aiohttp import ClientSession +from yalexs.authenticator_common import Authentication, AuthenticationState +from yalexs.manager.gateway import Gateway + +from homeassistant.helpers import config_entry_oauth2_flow + +_LOGGER = logging.getLogger(__name__) + + +class YaleGateway(Gateway): + """Handle the connection to Yale.""" + + def __init__( + self, + config_path: Path, + aiohttp_session: ClientSession, + oauth_session: config_entry_oauth2_flow.OAuth2Session, + ) -> None: + """Init the connection.""" + super().__init__(config_path, aiohttp_session) + self._oauth_session = oauth_session + + async def async_get_access_token(self) -> str: + """Get access token.""" + await self._oauth_session.async_ensure_token_valid() + return self._oauth_session.token["access_token"] + + async def async_refresh_access_token_if_needed(self) -> None: + """Refresh the access token if needed.""" + await self._oauth_session.async_ensure_token_valid() + + async def async_authenticate(self) -> Authentication: + """Authenticate with the details provided to setup.""" + await self._oauth_session.async_ensure_token_valid() + self.authentication = Authentication( + AuthenticationState.AUTHENTICATED, None, None, None + ) + return self.authentication diff --git a/homeassistant/components/yale/icons.json b/homeassistant/components/yale/icons.json new file mode 100644 index 00000000000000..b654b6d912a6ec --- /dev/null +++ b/homeassistant/components/yale/icons.json @@ -0,0 +1,9 @@ +{ + "entity": { + "binary_sensor": { + "image_capture": { + "default": "mdi:file-image" + } + } + } +} diff --git a/homeassistant/components/yale/lock.py b/homeassistant/components/yale/lock.py new file mode 100644 index 00000000000000..b911c92ba0f876 --- /dev/null +++ b/homeassistant/components/yale/lock.py @@ -0,0 +1,147 @@ +"""Support for Yale lock.""" + +from __future__ import annotations + +from collections.abc import Callable, Coroutine +import logging +from typing import Any + +from aiohttp import ClientResponseError +from yalexs.activity import ActivityType, ActivityTypes +from yalexs.lock import Lock, LockStatus +from yalexs.util import get_latest_activity, update_lock_detail_from_activity + +from homeassistant.components.lock import ATTR_CHANGED_BY, LockEntity, LockEntityFeature +from homeassistant.const import ATTR_BATTERY_LEVEL +from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.restore_state import RestoreEntity +import homeassistant.util.dt as dt_util + +from . import YaleConfigEntry, YaleData +from .entity import YaleEntity + +_LOGGER = logging.getLogger(__name__) + +LOCK_JAMMED_ERR = 531 + + +async def async_setup_entry( + hass: HomeAssistant, + config_entry: YaleConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up Yale locks.""" + data = config_entry.runtime_data + async_add_entities(YaleLock(data, lock) for lock in data.locks) + + +class YaleLock(YaleEntity, RestoreEntity, LockEntity): + """Representation of an Yale lock.""" + + _attr_name = None + _lock_status: LockStatus | None = None + + def __init__(self, data: YaleData, device: Lock) -> None: + """Initialize the lock.""" + super().__init__(data, device, "lock") + if self._detail.unlatch_supported: + self._attr_supported_features = LockEntityFeature.OPEN + + async def async_lock(self, **kwargs: Any) -> None: + """Lock the device.""" + if self._data.push_updates_connected: + await self._data.async_lock_async(self._device_id, self._hyper_bridge) + return + await self._call_lock_operation(self._data.async_lock) + + async def async_open(self, **kwargs: Any) -> None: + """Open/unlatch the device.""" + if self._data.push_updates_connected: + await self._data.async_unlatch_async(self._device_id, self._hyper_bridge) + return + await self._call_lock_operation(self._data.async_unlatch) + + async def async_unlock(self, **kwargs: Any) -> None: + """Unlock the device.""" + if self._data.push_updates_connected: + await self._data.async_unlock_async(self._device_id, self._hyper_bridge) + return + await self._call_lock_operation(self._data.async_unlock) + + async def _call_lock_operation( + self, lock_operation: Callable[[str], Coroutine[Any, Any, list[ActivityTypes]]] + ) -> None: + try: + activities = await lock_operation(self._device_id) + except ClientResponseError as err: + if err.status == LOCK_JAMMED_ERR: + self._detail.lock_status = LockStatus.JAMMED + self._detail.lock_status_datetime = dt_util.utcnow() + else: + raise + else: + for lock_activity in activities: + update_lock_detail_from_activity(self._detail, lock_activity) + + if self._update_lock_status_from_detail(): + _LOGGER.debug( + "async_signal_device_id_update (from lock operation): %s", + self._device_id, + ) + self._data.async_signal_device_id_update(self._device_id) + + def _update_lock_status_from_detail(self) -> bool: + self._attr_available = self._detail.bridge_is_online + + if self._lock_status != self._detail.lock_status: + self._lock_status = self._detail.lock_status + return True + return False + + @callback + def _update_from_data(self) -> None: + """Get the latest state of the sensor and update activity.""" + detail = self._detail + if lock_activity := self._get_latest({ActivityType.LOCK_OPERATION}): + self._attr_changed_by = lock_activity.operated_by + lock_activity_without_operator = self._get_latest( + {ActivityType.LOCK_OPERATION_WITHOUT_OPERATOR} + ) + if latest_activity := get_latest_activity( + lock_activity_without_operator, lock_activity + ): + if latest_activity.was_pushed: + self._detail.set_online(True) + update_lock_detail_from_activity(detail, latest_activity) + + if bridge_activity := self._get_latest({ActivityType.BRIDGE_OPERATION}): + update_lock_detail_from_activity(detail, bridge_activity) + + self._update_lock_status_from_detail() + lock_status = self._lock_status + if lock_status is None or lock_status is LockStatus.UNKNOWN: + self._attr_is_locked = None + else: + self._attr_is_locked = lock_status is LockStatus.LOCKED + self._attr_is_jammed = lock_status is LockStatus.JAMMED + self._attr_is_locking = lock_status is LockStatus.LOCKING + self._attr_is_unlocking = lock_status in ( + LockStatus.UNLOCKING, + LockStatus.UNLATCHING, + ) + self._attr_extra_state_attributes = {ATTR_BATTERY_LEVEL: detail.battery_level} + if keypad := detail.keypad: + self._attr_extra_state_attributes["keypad_battery_level"] = ( + keypad.battery_level + ) + + async def async_added_to_hass(self) -> None: + """Restore ATTR_CHANGED_BY on startup since it is likely no longer in the activity log.""" + await super().async_added_to_hass() + + if not (last_state := await self.async_get_last_state()): + return + + if ATTR_CHANGED_BY in last_state.attributes: + self._attr_changed_by = last_state.attributes[ATTR_CHANGED_BY] diff --git a/homeassistant/components/yale/manifest.json b/homeassistant/components/yale/manifest.json new file mode 100644 index 00000000000000..fc93d2598919b8 --- /dev/null +++ b/homeassistant/components/yale/manifest.json @@ -0,0 +1,17 @@ +{ + "domain": "yale", + "name": "Yale", + "codeowners": ["@bdraco"], + "config_flow": true, + "dependencies": ["application_credentials", "cloud"], + "dhcp": [ + { + "hostname": "yale-connect-plus", + "macaddress": "00177A*" + } + ], + "documentation": "https://www.home-assistant.io/integrations/yale", + "iot_class": "cloud_push", + "loggers": ["socketio", "engineio", "yalexs"], + "requirements": ["yalexs==8.6.3", "yalexs-ble==2.4.3"] +} diff --git a/homeassistant/components/yale/sensor.py b/homeassistant/components/yale/sensor.py new file mode 100644 index 00000000000000..bb3d4317277dfc --- /dev/null +++ b/homeassistant/components/yale/sensor.py @@ -0,0 +1,211 @@ +"""Support for Yale sensors.""" + +from __future__ import annotations + +from collections.abc import Callable +from dataclasses import dataclass +from typing import Any, cast + +from yalexs.activity import ActivityType, LockOperationActivity +from yalexs.doorbell import Doorbell +from yalexs.keypad import KeypadDetail +from yalexs.lock import LockDetail + +from homeassistant.components.sensor import ( + RestoreSensor, + SensorDeviceClass, + SensorEntity, + SensorEntityDescription, + SensorStateClass, +) +from homeassistant.const import ( + ATTR_ENTITY_PICTURE, + PERCENTAGE, + STATE_UNAVAILABLE, + EntityCategory, +) +from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from . import YaleConfigEntry +from .const import ( + ATTR_OPERATION_AUTORELOCK, + ATTR_OPERATION_KEYPAD, + ATTR_OPERATION_MANUAL, + ATTR_OPERATION_METHOD, + ATTR_OPERATION_REMOTE, + ATTR_OPERATION_TAG, + OPERATION_METHOD_AUTORELOCK, + OPERATION_METHOD_KEYPAD, + OPERATION_METHOD_MANUAL, + OPERATION_METHOD_MOBILE_DEVICE, + OPERATION_METHOD_REMOTE, + OPERATION_METHOD_TAG, +) +from .entity import YaleDescriptionEntity, YaleEntity + + +def _retrieve_device_battery_state(detail: LockDetail) -> int: + """Get the latest state of the sensor.""" + return detail.battery_level + + +def _retrieve_linked_keypad_battery_state(detail: KeypadDetail) -> int | None: + """Get the latest state of the sensor.""" + return detail.battery_percentage + + +@dataclass(frozen=True, kw_only=True) +class YaleSensorEntityDescription[T: LockDetail | KeypadDetail]( + SensorEntityDescription +): + """Mixin for required keys.""" + + value_fn: Callable[[T], int | None] + + +SENSOR_TYPE_DEVICE_BATTERY = YaleSensorEntityDescription[LockDetail]( + key="device_battery", + entity_category=EntityCategory.DIAGNOSTIC, + state_class=SensorStateClass.MEASUREMENT, + value_fn=_retrieve_device_battery_state, +) + +SENSOR_TYPE_KEYPAD_BATTERY = YaleSensorEntityDescription[KeypadDetail]( + key="linked_keypad_battery", + entity_category=EntityCategory.DIAGNOSTIC, + state_class=SensorStateClass.MEASUREMENT, + value_fn=_retrieve_linked_keypad_battery_state, +) + + +async def async_setup_entry( + hass: HomeAssistant, + config_entry: YaleConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up the Yale sensors.""" + data = config_entry.runtime_data + entities: list[SensorEntity] = [] + + for device in data.locks: + detail = data.get_device_detail(device.device_id) + entities.append(YaleOperatorSensor(data, device, "lock_operator")) + if SENSOR_TYPE_DEVICE_BATTERY.value_fn(detail): + entities.append( + YaleBatterySensor[LockDetail](data, device, SENSOR_TYPE_DEVICE_BATTERY) + ) + if keypad := detail.keypad: + entities.append( + YaleBatterySensor[KeypadDetail]( + data, keypad, SENSOR_TYPE_KEYPAD_BATTERY + ) + ) + + entities.extend( + YaleBatterySensor[Doorbell](data, device, SENSOR_TYPE_DEVICE_BATTERY) + for device in data.doorbells + if SENSOR_TYPE_DEVICE_BATTERY.value_fn(data.get_device_detail(device.device_id)) + ) + + async_add_entities(entities) + + +class YaleOperatorSensor(YaleEntity, RestoreSensor): + """Representation of an Yale lock operation sensor.""" + + _attr_translation_key = "operator" + _operated_remote: bool | None = None + _operated_keypad: bool | None = None + _operated_manual: bool | None = None + _operated_tag: bool | None = None + _operated_autorelock: bool | None = None + + @callback + def _update_from_data(self) -> None: + """Get the latest state of the sensor and update activity.""" + self._attr_available = True + if lock_activity := self._get_latest({ActivityType.LOCK_OPERATION}): + lock_activity = cast(LockOperationActivity, lock_activity) + self._attr_native_value = lock_activity.operated_by + self._operated_remote = lock_activity.operated_remote + self._operated_keypad = lock_activity.operated_keypad + self._operated_manual = lock_activity.operated_manual + self._operated_tag = lock_activity.operated_tag + self._operated_autorelock = lock_activity.operated_autorelock + self._attr_entity_picture = lock_activity.operator_thumbnail_url + + @property + def extra_state_attributes(self) -> dict[str, Any]: + """Return the device specific state attributes.""" + attributes: dict[str, Any] = {} + + if self._operated_remote is not None: + attributes[ATTR_OPERATION_REMOTE] = self._operated_remote + if self._operated_keypad is not None: + attributes[ATTR_OPERATION_KEYPAD] = self._operated_keypad + if self._operated_manual is not None: + attributes[ATTR_OPERATION_MANUAL] = self._operated_manual + if self._operated_tag is not None: + attributes[ATTR_OPERATION_TAG] = self._operated_tag + if self._operated_autorelock is not None: + attributes[ATTR_OPERATION_AUTORELOCK] = self._operated_autorelock + + if self._operated_remote: + attributes[ATTR_OPERATION_METHOD] = OPERATION_METHOD_REMOTE + elif self._operated_keypad: + attributes[ATTR_OPERATION_METHOD] = OPERATION_METHOD_KEYPAD + elif self._operated_manual: + attributes[ATTR_OPERATION_METHOD] = OPERATION_METHOD_MANUAL + elif self._operated_tag: + attributes[ATTR_OPERATION_METHOD] = OPERATION_METHOD_TAG + elif self._operated_autorelock: + attributes[ATTR_OPERATION_METHOD] = OPERATION_METHOD_AUTORELOCK + else: + attributes[ATTR_OPERATION_METHOD] = OPERATION_METHOD_MOBILE_DEVICE + + return attributes + + async def async_added_to_hass(self) -> None: + """Restore ATTR_CHANGED_BY on startup since it is likely no longer in the activity log.""" + await super().async_added_to_hass() + + last_state = await self.async_get_last_state() + last_sensor_state = await self.async_get_last_sensor_data() + if ( + not last_state + or not last_sensor_state + or last_state.state == STATE_UNAVAILABLE + ): + return + + self._attr_native_value = last_sensor_state.native_value + last_attrs = last_state.attributes + if ATTR_ENTITY_PICTURE in last_attrs: + self._attr_entity_picture = last_attrs[ATTR_ENTITY_PICTURE] + if ATTR_OPERATION_REMOTE in last_attrs: + self._operated_remote = last_attrs[ATTR_OPERATION_REMOTE] + if ATTR_OPERATION_KEYPAD in last_attrs: + self._operated_keypad = last_attrs[ATTR_OPERATION_KEYPAD] + if ATTR_OPERATION_MANUAL in last_attrs: + self._operated_manual = last_attrs[ATTR_OPERATION_MANUAL] + if ATTR_OPERATION_TAG in last_attrs: + self._operated_tag = last_attrs[ATTR_OPERATION_TAG] + if ATTR_OPERATION_AUTORELOCK in last_attrs: + self._operated_autorelock = last_attrs[ATTR_OPERATION_AUTORELOCK] + + +class YaleBatterySensor[T: LockDetail | KeypadDetail]( + YaleDescriptionEntity, SensorEntity +): + """Representation of an Yale sensor.""" + + entity_description: YaleSensorEntityDescription[T] + _attr_device_class = SensorDeviceClass.BATTERY + _attr_native_unit_of_measurement = PERCENTAGE + + @callback + def _update_from_data(self) -> None: + """Get the latest state of the sensor.""" + self._attr_native_value = self.entity_description.value_fn(self._detail) + self._attr_available = self._attr_native_value is not None diff --git a/homeassistant/components/yale/strings.json b/homeassistant/components/yale/strings.json new file mode 100644 index 00000000000000..3fb1345a3b036c --- /dev/null +++ b/homeassistant/components/yale/strings.json @@ -0,0 +1,71 @@ +{ + "config": { + "step": { + "pick_implementation": { + "title": "[%key:common::config_flow::title::oauth2_pick_implementation%]" + } + }, + "abort": { + "already_configured": "[%key:common::config_flow::abort::already_configured_account%]", + "already_in_progress": "[%key:common::config_flow::abort::already_in_progress%]", + "oauth_error": "[%key:common::config_flow::abort::oauth2_error%]", + "missing_configuration": "[%key:common::config_flow::abort::oauth2_missing_configuration%]", + "authorize_url_timeout": "[%key:common::config_flow::abort::oauth2_authorize_url_timeout%]", + "no_url_available": "[%key:common::config_flow::abort::oauth2_no_url_available%]", + "user_rejected_authorize": "[%key:common::config_flow::abort::oauth2_user_rejected_authorize%]", + "reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]", + "oauth_timeout": "[%key:common::config_flow::abort::oauth2_timeout%]", + "oauth_unauthorized": "[%key:common::config_flow::abort::oauth2_unauthorized%]", + "oauth_failed": "[%key:common::config_flow::abort::oauth2_failed%]", + "reauth_invalid_user": "Reauthenticate must use the same account." + }, + "create_entry": { + "default": "[%key:common::config_flow::create_entry::authenticated%]" + } + }, + "entity": { + "binary_sensor": { + "ding": { + "name": "Doorbell ding" + }, + "image_capture": { + "name": "Image capture" + } + }, + "button": { + "wake": { + "name": "Wake" + } + }, + "camera": { + "camera": { + "name": "[%key:component::camera::title%]" + } + }, + "sensor": { + "operator": { + "name": "Operator" + } + }, + "event": { + "doorbell": { + "state_attributes": { + "event_type": { + "state": { + "ring": "Ring" + } + } + } + }, + "motion": { + "state_attributes": { + "event_type": { + "state": { + "motion": "Motion" + } + } + } + } + } + } +} diff --git a/homeassistant/components/yale/util.py b/homeassistant/components/yale/util.py new file mode 100644 index 00000000000000..3462c576fd914c --- /dev/null +++ b/homeassistant/components/yale/util.py @@ -0,0 +1,78 @@ +"""Yale util functions.""" + +from __future__ import annotations + +from datetime import datetime, timedelta +from functools import partial + +import aiohttp +from yalexs.activity import ACTION_DOORBELL_CALL_MISSED, Activity, ActivityType +from yalexs.doorbell import DoorbellDetail +from yalexs.lock import LockDetail +from yalexs.manager.const import ACTIVITY_UPDATE_INTERVAL + +from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers import aiohttp_client + +from . import YaleData + +TIME_TO_DECLARE_DETECTION = timedelta(seconds=ACTIVITY_UPDATE_INTERVAL.total_seconds()) + + +@callback +def async_create_yale_clientsession(hass: HomeAssistant) -> aiohttp.ClientSession: + """Create an aiohttp session for the yale integration.""" + # Create an aiohttp session instead of using the default one since the + # default one is likely to trigger yale's WAF if another integration + # is also using Cloudflare + return aiohttp_client.async_create_clientsession(hass) + + +def retrieve_time_based_activity( + activities: set[ActivityType], data: YaleData, detail: DoorbellDetail | LockDetail +) -> Activity | None: + """Get the latest state of the sensor.""" + stream = data.activity_stream + if latest := stream.get_latest_device_activity(detail.device_id, activities): + return _activity_time_based(latest) + return False + + +_RING_ACTIVITIES = {ActivityType.DOORBELL_DING} + + +def retrieve_ding_activity( + data: YaleData, detail: DoorbellDetail | LockDetail +) -> Activity | None: + """Get the ring/ding state.""" + stream = data.activity_stream + latest = stream.get_latest_device_activity(detail.device_id, _RING_ACTIVITIES) + if latest is None or ( + data.push_updates_connected and latest.action == ACTION_DOORBELL_CALL_MISSED + ): + return None + return _activity_time_based(latest) + + +retrieve_doorbell_motion_activity = partial( + retrieve_time_based_activity, {ActivityType.DOORBELL_MOTION} +) + + +def _activity_time_based(latest: Activity) -> Activity | None: + """Get the latest state of the sensor.""" + start = latest.activity_start_time + end = latest.activity_end_time + TIME_TO_DECLARE_DETECTION + if start <= datetime.now() <= end: + return latest + return None + + +def retrieve_online_state(data: YaleData, detail: DoorbellDetail | LockDetail) -> bool: + """Get the latest state of the sensor.""" + # The doorbell will go into standby mode when there is no motion + # for a short while. It will wake by itself when needed so we need + # to consider is available or we will not report motion or dings + if isinstance(detail, DoorbellDetail): + return detail.is_online or detail.is_standby + return detail.bridge_is_online diff --git a/homeassistant/components/yale_home/manifest.json b/homeassistant/components/yale_home/manifest.json index 0e45b0da7d0336..c497fa3fe34f97 100644 --- a/homeassistant/components/yale_home/manifest.json +++ b/homeassistant/components/yale_home/manifest.json @@ -2,5 +2,5 @@ "domain": "yale_home", "name": "Yale Home", "integration_type": "virtual", - "supported_by": "august" + "supported_by": "yale" } diff --git a/homeassistant/components/yale_smart_alarm/coordinator.py b/homeassistant/components/yale_smart_alarm/coordinator.py index 1067b9279a4abf..b47545ea88b789 100644 --- a/homeassistant/components/yale_smart_alarm/coordinator.py +++ b/homeassistant/components/yale_smart_alarm/coordinator.py @@ -36,8 +36,10 @@ def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None: async def _async_setup(self) -> None: """Set up connection to Yale.""" try: - self.yale = YaleSmartAlarmClient( - self.entry.data[CONF_USERNAME], self.entry.data[CONF_PASSWORD] + self.yale = await self.hass.async_add_executor_job( + YaleSmartAlarmClient, + self.entry.data[CONF_USERNAME], + self.entry.data[CONF_PASSWORD], ) except AuthenticationError as error: raise ConfigEntryAuthFailed from error diff --git a/homeassistant/components/yamaha/icons.json b/homeassistant/components/yamaha/icons.json index f7075508b0dd93..40eceda3b3e00e 100644 --- a/homeassistant/components/yamaha/icons.json +++ b/homeassistant/components/yamaha/icons.json @@ -1,7 +1,13 @@ { "services": { - "enable_output": "mdi:audio-input-stereo-minijack", - "menu_cursor": "mdi:cursor-default", - "select_scene": "mdi:palette" + "enable_output": { + "service": "mdi:audio-input-stereo-minijack" + }, + "menu_cursor": { + "service": "mdi:cursor-default" + }, + "select_scene": { + "service": "mdi:palette" + } } } diff --git a/homeassistant/components/yardian/icons.json b/homeassistant/components/yardian/icons.json index 79bcc32adf2f06..4ca3d83bd158c6 100644 --- a/homeassistant/components/yardian/icons.json +++ b/homeassistant/components/yardian/icons.json @@ -7,6 +7,8 @@ } }, "services": { - "start_irrigation": "mdi:water" + "start_irrigation": { + "service": "mdi:water" + } } } diff --git a/homeassistant/components/yeelight/config_flow.py b/homeassistant/components/yeelight/config_flow.py index d7bf4e25996b89..1b36fba59df208 100644 --- a/homeassistant/components/yeelight/config_flow.py +++ b/homeassistant/components/yeelight/config_flow.py @@ -3,6 +3,7 @@ from __future__ import annotations import logging +from typing import TYPE_CHECKING, Any from urllib.parse import urlparse import voluptuous as vol @@ -57,11 +58,11 @@ def async_get_options_flow(config_entry: ConfigEntry) -> OptionsFlowHandler: """Return the options flow.""" return OptionsFlowHandler(config_entry) - def __init__(self): + def __init__(self) -> None: """Initialize the config flow.""" - self._discovered_devices = {} + self._discovered_devices: dict[str, Any] = {} self._discovered_model = None - self._discovered_ip = None + self._discovered_ip: str | None = None async def async_step_homekit( self, discovery_info: zeroconf.ZeroconfServiceInfo @@ -162,7 +163,9 @@ async def async_step_discovery_confirm(self, user_input=None): step_id="discovery_confirm", description_placeholders=placeholders ) - async def async_step_user(self, user_input=None): + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: """Handle the initial step.""" errors = {} if user_input is not None: @@ -176,6 +179,8 @@ async def async_step_user(self, user_input=None): errors["base"] = "cannot_connect" else: self._abort_if_unique_id_configured() + if TYPE_CHECKING: + assert self.unique_id return self.async_create_entry( title=async_format_model_id(model, self.unique_id), data={ @@ -239,21 +244,21 @@ async def async_step_pick_device(self, user_input=None): data_schema=vol.Schema({vol.Required(CONF_DEVICE): vol.In(devices_name)}), ) - async def async_step_import(self, user_input=None): + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: """Handle import step.""" - host = user_input[CONF_HOST] + host = import_data[CONF_HOST] try: await self._async_try_connect(host, raise_on_progress=False) except CannotConnect: _LOGGER.error("Failed to import %s: cannot connect", host) return self.async_abort(reason="cannot_connect") - if CONF_NIGHTLIGHT_SWITCH_TYPE in user_input: - user_input[CONF_NIGHTLIGHT_SWITCH] = ( - user_input.pop(CONF_NIGHTLIGHT_SWITCH_TYPE) + if CONF_NIGHTLIGHT_SWITCH_TYPE in import_data: + import_data[CONF_NIGHTLIGHT_SWITCH] = ( + import_data.pop(CONF_NIGHTLIGHT_SWITCH_TYPE) == NIGHTLIGHT_SWITCH_TYPE_LIGHT ) self._abort_if_unique_id_configured() - return self.async_create_entry(title=user_input[CONF_NAME], data=user_input) + return self.async_create_entry(title=import_data[CONF_NAME], data=import_data) async def _async_try_connect(self, host, raise_on_progress=True): """Set up with options.""" diff --git a/homeassistant/components/yeelight/icons.json b/homeassistant/components/yeelight/icons.json index bf0d0c497f0ee8..898637e752c3ea 100644 --- a/homeassistant/components/yeelight/icons.json +++ b/homeassistant/components/yeelight/icons.json @@ -7,13 +7,29 @@ } }, "services": { - "set_mode": "mdi:cog", - "set_color_scene": "mdi:palette", - "set_hsv_scene": "mdi:palette", - "set_color_temp_scene": "mdi:palette", - "set_color_flow_scene": "mdi:palette", - "set_auto_delay_off_scene": "mdi:timer", - "start_flow": "mdi:play", - "set_music_mode": "mdi:music" + "set_mode": { + "service": "mdi:cog" + }, + "set_color_scene": { + "service": "mdi:palette" + }, + "set_hsv_scene": { + "service": "mdi:palette" + }, + "set_color_temp_scene": { + "service": "mdi:palette" + }, + "set_color_flow_scene": { + "service": "mdi:palette" + }, + "set_auto_delay_off_scene": { + "service": "mdi:timer" + }, + "start_flow": { + "service": "mdi:play" + }, + "set_music_mode": { + "service": "mdi:music" + } } } diff --git a/homeassistant/components/yolink/const.py b/homeassistant/components/yolink/const.py index 686160d92489e5..217dd66d063a42 100644 --- a/homeassistant/components/yolink/const.py +++ b/homeassistant/components/yolink/const.py @@ -23,3 +23,11 @@ DEV_MODEL_TH_SENSOR_YS8014_EC = "YS8014-EC" DEV_MODEL_TH_SENSOR_YS8017_UC = "YS8017-UC" DEV_MODEL_TH_SENSOR_YS8017_EC = "YS8017-EC" +DEV_MODEL_FLEX_FOB_YS3604_UC = "YS3604-UC" +DEV_MODEL_FLEX_FOB_YS3604_EC = "YS3604-EC" +DEV_MODEL_FLEX_FOB_YS3614_UC = "YS3614-UC" +DEV_MODEL_FLEX_FOB_YS3614_EC = "YS3614-EC" +DEV_MODEL_PLUG_YS6602_UC = "YS6602-UC" +DEV_MODEL_PLUG_YS6602_EC = "YS6602-EC" +DEV_MODEL_PLUG_YS6803_UC = "YS6803-UC" +DEV_MODEL_PLUG_YS6803_EC = "YS6803-EC" diff --git a/homeassistant/components/yolink/device_trigger.py b/homeassistant/components/yolink/device_trigger.py index b7f83623be594c..6e247bf858e71d 100644 --- a/homeassistant/components/yolink/device_trigger.py +++ b/homeassistant/components/yolink/device_trigger.py @@ -16,6 +16,12 @@ from homeassistant.helpers.typing import ConfigType from . import DOMAIN, YOLINK_EVENT +from .const import ( + DEV_MODEL_FLEX_FOB_YS3604_EC, + DEV_MODEL_FLEX_FOB_YS3604_UC, + DEV_MODEL_FLEX_FOB_YS3614_EC, + DEV_MODEL_FLEX_FOB_YS3614_UC, +) CONF_BUTTON_1 = "button_1" CONF_BUTTON_2 = "button_2" @@ -24,7 +30,7 @@ CONF_SHORT_PRESS = "short_press" CONF_LONG_PRESS = "long_press" -REMOTE_TRIGGER_TYPES = { +FLEX_FOB_4_BUTTONS = { f"{CONF_BUTTON_1}_{CONF_SHORT_PRESS}", f"{CONF_BUTTON_1}_{CONF_LONG_PRESS}", f"{CONF_BUTTON_2}_{CONF_SHORT_PRESS}", @@ -35,14 +41,24 @@ f"{CONF_BUTTON_4}_{CONF_LONG_PRESS}", } +FLEX_FOB_2_BUTTONS = { + f"{CONF_BUTTON_1}_{CONF_SHORT_PRESS}", + f"{CONF_BUTTON_1}_{CONF_LONG_PRESS}", + f"{CONF_BUTTON_2}_{CONF_SHORT_PRESS}", + f"{CONF_BUTTON_2}_{CONF_LONG_PRESS}", +} + TRIGGER_SCHEMA = DEVICE_TRIGGER_BASE_SCHEMA.extend( - {vol.Required(CONF_TYPE): vol.In(REMOTE_TRIGGER_TYPES)} + {vol.Required(CONF_TYPE): vol.In(FLEX_FOB_4_BUTTONS)} ) -# YoLink Remotes YS3604/YS3605/YS3606/YS3607 -DEVICE_TRIGGER_TYPES: dict[str, set[str]] = { - ATTR_DEVICE_SMART_REMOTER: REMOTE_TRIGGER_TYPES, +# YoLink Remotes YS3604/YS3614 +FLEX_FOB_TRIGGER_TYPES: dict[str, set[str]] = { + DEV_MODEL_FLEX_FOB_YS3604_EC: FLEX_FOB_4_BUTTONS, + DEV_MODEL_FLEX_FOB_YS3604_UC: FLEX_FOB_4_BUTTONS, + DEV_MODEL_FLEX_FOB_YS3614_UC: FLEX_FOB_2_BUTTONS, + DEV_MODEL_FLEX_FOB_YS3614_EC: FLEX_FOB_2_BUTTONS, } @@ -54,7 +70,8 @@ async def async_get_triggers( registry_device = device_registry.async_get(device_id) if not registry_device or registry_device.model != ATTR_DEVICE_SMART_REMOTER: return [] - + if registry_device.model_id not in list(FLEX_FOB_TRIGGER_TYPES.keys()): + return [] return [ { CONF_DEVICE_ID: device_id, @@ -62,7 +79,7 @@ async def async_get_triggers( CONF_PLATFORM: "device", CONF_TYPE: trigger, } - for trigger in DEVICE_TRIGGER_TYPES[ATTR_DEVICE_SMART_REMOTER] + for trigger in FLEX_FOB_TRIGGER_TYPES[registry_device.model_id] ] diff --git a/homeassistant/components/yolink/entity.py b/homeassistant/components/yolink/entity.py index d9ca2968493d70..0f500b72404274 100644 --- a/homeassistant/components/yolink/entity.py +++ b/homeassistant/components/yolink/entity.py @@ -55,6 +55,7 @@ def device_info(self) -> DeviceInfo: identifiers={(DOMAIN, self.coordinator.device.device_id)}, manufacturer=MANUFACTURER, model=self.coordinator.device.device_type, + model_id=self.coordinator.device.device_model_name, name=self.coordinator.device.device_name, ) diff --git a/homeassistant/components/yolink/icons.json b/homeassistant/components/yolink/icons.json index ee9037c864a76e..c58d219a2e0e58 100644 --- a/homeassistant/components/yolink/icons.json +++ b/homeassistant/components/yolink/icons.json @@ -17,6 +17,9 @@ }, "power_failure_alarm_beep": { "default": "mdi:bullhorn" + }, + "water_meter_reading": { + "default": "mdi:gauge" } }, "switch": { @@ -26,6 +29,8 @@ } }, "services": { - "play_on_speaker_hub": "mdi:speaker" + "play_on_speaker_hub": { + "service": "mdi:speaker" + } } } diff --git a/homeassistant/components/yolink/lock.py b/homeassistant/components/yolink/lock.py index 177a8808de143e..d675fd8cf06a1f 100644 --- a/homeassistant/components/yolink/lock.py +++ b/homeassistant/components/yolink/lock.py @@ -1,11 +1,11 @@ -"""YoLink Lock.""" +"""YoLink Lock V1/V2.""" from __future__ import annotations from typing import Any from yolink.client_request import ClientRequest -from yolink.const import ATTR_DEVICE_LOCK +from yolink.const import ATTR_DEVICE_LOCK, ATTR_DEVICE_LOCK_V2 from homeassistant.components.lock import LockEntity from homeassistant.config_entries import ConfigEntry @@ -27,7 +27,8 @@ async def async_setup_entry( entities = [ YoLinkLockEntity(config_entry, device_coordinator) for device_coordinator in device_coordinators.values() - if device_coordinator.device.device_type == ATTR_DEVICE_LOCK + if device_coordinator.device.device_type + in [ATTR_DEVICE_LOCK, ATTR_DEVICE_LOCK_V2] ] async_add_entities(entities) @@ -50,21 +51,41 @@ def __init__( def update_entity_state(self, state: dict[str, Any]) -> None: """Update HA Entity State.""" state_value = state.get("state") - self._attr_is_locked = ( - state_value == "locked" if state_value is not None else None - ) + if self.coordinator.device.device_type == ATTR_DEVICE_LOCK_V2: + self._attr_is_locked = ( + state_value["lock"] == "locked" if state_value is not None else None + ) + else: + self._attr_is_locked = ( + state_value == "locked" if state_value is not None else None + ) self.async_write_ha_state() async def call_lock_state_change(self, state: str) -> None: """Call setState api to change lock state.""" - await self.call_device(ClientRequest("setState", {"state": state})) + if self.coordinator.device.device_type == ATTR_DEVICE_LOCK_V2: + await self.call_device( + ClientRequest("setState", {"state": {"lock": state}}) + ) + else: + await self.call_device(ClientRequest("setState", {"state": state})) self._attr_is_locked = state == "lock" self.async_write_ha_state() async def async_lock(self, **kwargs: Any) -> None: """Lock device.""" - await self.call_lock_state_change("lock") + state_param = ( + "locked" + if self.coordinator.device.device_type == ATTR_DEVICE_LOCK_V2 + else "lock" + ) + await self.call_lock_state_change(state_param) async def async_unlock(self, **kwargs: Any) -> None: """Unlock device.""" - await self.call_lock_state_change("unlock") + state_param = ( + "unlocked" + if self.coordinator.device.device_type == ATTR_DEVICE_LOCK_V2 + else "unlock" + ) + await self.call_lock_state_change(state_param) diff --git a/homeassistant/components/yolink/sensor.py b/homeassistant/components/yolink/sensor.py index 77bbccb2f6a736..b8f2a77516c7fb 100644 --- a/homeassistant/components/yolink/sensor.py +++ b/homeassistant/components/yolink/sensor.py @@ -40,7 +40,9 @@ PERCENTAGE, SIGNAL_STRENGTH_DECIBELS_MILLIWATT, EntityCategory, + UnitOfEnergy, UnitOfLength, + UnitOfPower, UnitOfTemperature, UnitOfVolume, ) @@ -49,6 +51,10 @@ from homeassistant.util import percentage from .const import ( + DEV_MODEL_PLUG_YS6602_EC, + DEV_MODEL_PLUG_YS6602_UC, + DEV_MODEL_PLUG_YS6803_EC, + DEV_MODEL_PLUG_YS6803_UC, DEV_MODEL_TH_SENSOR_YS8004_EC, DEV_MODEL_TH_SENSOR_YS8004_UC, DEV_MODEL_TH_SENSOR_YS8014_EC, @@ -125,6 +131,13 @@ class YoLinkSensorEntityDescription(SensorEntityDescription): DEV_MODEL_TH_SENSOR_YS8017_EC, ] +POWER_SUPPORT_MODELS = [ + DEV_MODEL_PLUG_YS6602_UC, + DEV_MODEL_PLUG_YS6602_EC, + DEV_MODEL_PLUG_YS6803_UC, + DEV_MODEL_PLUG_YS6803_EC, +] + def cvt_battery(val: int | None) -> int | None: """Convert battery to percentage.""" @@ -228,13 +241,32 @@ def cvt_volume(val: int | None) -> str | None: key="meter_reading", translation_key="water_meter_reading", device_class=SensorDeviceClass.WATER, - icon="mdi:gauge", native_unit_of_measurement=UnitOfVolume.CUBIC_METERS, state_class=SensorStateClass.TOTAL_INCREASING, should_update_entity=lambda value: value is not None, exists_fn=lambda device: device.device_type in ATTR_DEVICE_WATER_METER_CONTROLLER, ), + YoLinkSensorEntityDescription( + key="power", + translation_key="current_power", + device_class=SensorDeviceClass.POWER, + native_unit_of_measurement=UnitOfPower.WATT, + state_class=SensorStateClass.MEASUREMENT, + should_update_entity=lambda value: value is not None, + exists_fn=lambda device: device.device_model_name in POWER_SUPPORT_MODELS, + value=lambda value: value / 10 if value is not None else None, + ), + YoLinkSensorEntityDescription( + key="watt", + translation_key="power_consumption", + device_class=SensorDeviceClass.ENERGY, + native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + state_class=SensorStateClass.TOTAL, + should_update_entity=lambda value: value is not None, + exists_fn=lambda device: device.device_model_name in POWER_SUPPORT_MODELS, + value=lambda value: value / 100 if value is not None else None, + ), ) diff --git a/homeassistant/components/yolink/strings.json b/homeassistant/components/yolink/strings.json index bc8fb435e76b37..cefc7737a7908c 100644 --- a/homeassistant/components/yolink/strings.json +++ b/homeassistant/components/yolink/strings.json @@ -51,6 +51,12 @@ "plug_4": { "name": "Plug 4" } }, "sensor": { + "current_power": { + "name": "Current power" + }, + "power_consumption": { + "name": "Power consumption" + }, "power_failure_alarm": { "name": "Power failure alarm", "state": { diff --git a/homeassistant/components/youtube/const.py b/homeassistant/components/youtube/const.py index a663c487d0abc5..da5a554f364594 100644 --- a/homeassistant/components/youtube/const.py +++ b/homeassistant/components/youtube/const.py @@ -15,6 +15,7 @@ LOGGER = logging.getLogger(__package__) ATTR_TITLE = "title" +ATTR_TOTAL_VIEWS = "total_views" ATTR_LATEST_VIDEO = "latest_video" ATTR_SUBSCRIBER_COUNT = "subscriber_count" ATTR_DESCRIPTION = "description" diff --git a/homeassistant/components/youtube/coordinator.py b/homeassistant/components/youtube/coordinator.py index 4599342c84db38..0da480f1169135 100644 --- a/homeassistant/components/youtube/coordinator.py +++ b/homeassistant/components/youtube/coordinator.py @@ -22,6 +22,7 @@ ATTR_SUBSCRIBER_COUNT, ATTR_THUMBNAIL, ATTR_TITLE, + ATTR_TOTAL_VIEWS, ATTR_VIDEO_ID, CONF_CHANNELS, DOMAIN, @@ -68,6 +69,7 @@ async def _async_update_data(self) -> dict[str, Any]: ATTR_ICON: channel.snippet.thumbnails.get_highest_quality().url, ATTR_LATEST_VIDEO: latest_video, ATTR_SUBSCRIBER_COUNT: channel.statistics.subscriber_count, + ATTR_TOTAL_VIEWS: channel.statistics.view_count, } except UnauthorizedError as err: raise ConfigEntryAuthFailed from err diff --git a/homeassistant/components/youtube/sensor.py b/homeassistant/components/youtube/sensor.py index bc69f92e8fd9c7..8832382508c077 100644 --- a/homeassistant/components/youtube/sensor.py +++ b/homeassistant/components/youtube/sensor.py @@ -20,6 +20,7 @@ ATTR_SUBSCRIBER_COUNT, ATTR_THUMBNAIL, ATTR_TITLE, + ATTR_TOTAL_VIEWS, ATTR_VIDEO_ID, COORDINATOR, DOMAIN, @@ -58,6 +59,15 @@ class YouTubeSensorEntityDescription(SensorEntityDescription): entity_picture_fn=lambda channel: channel[ATTR_ICON], attributes_fn=None, ), + YouTubeSensorEntityDescription( + key="views", + translation_key="views", + native_unit_of_measurement="views", + available_fn=lambda _: True, + value_fn=lambda channel: channel[ATTR_TOTAL_VIEWS], + entity_picture_fn=lambda channel: channel[ATTR_ICON], + attributes_fn=None, + ), ] diff --git a/homeassistant/components/youtube/strings.json b/homeassistant/components/youtube/strings.json index d664e2f15e7842..5902d3a4482d17 100644 --- a/homeassistant/components/youtube/strings.json +++ b/homeassistant/components/youtube/strings.json @@ -46,7 +46,8 @@ "published_at": { "name": "Published at" } } }, - "subscribers": { "name": "Subscribers" } + "subscribers": { "name": "Subscribers" }, + "views": { "name": "Views" } } } } diff --git a/homeassistant/components/zeroconf/manifest.json b/homeassistant/components/zeroconf/manifest.json index 0a76af3b9c2a9d..8b332400805d12 100644 --- a/homeassistant/components/zeroconf/manifest.json +++ b/homeassistant/components/zeroconf/manifest.json @@ -8,5 +8,5 @@ "iot_class": "local_push", "loggers": ["zeroconf"], "quality_scale": "internal", - "requirements": ["zeroconf==0.132.2"] + "requirements": ["zeroconf==0.133.0"] } diff --git a/homeassistant/components/zha/diagnostics.py b/homeassistant/components/zha/diagnostics.py index f276630dfeeadc..ad73978d24d6c1 100644 --- a/homeassistant/components/zha/diagnostics.py +++ b/homeassistant/components/zha/diagnostics.py @@ -50,6 +50,15 @@ CLUSTER_DETAILS = "cluster_details" UNSUPPORTED_ATTRIBUTES = "unsupported_attributes" +BELLOWS_VERSION = version("bellows") +ZIGPY_VERSION = version("zigpy") +ZIGPY_DECONZ_VERSION = version("zigpy-deconz") +ZIGPY_XBEE_VERSION = version("zigpy-xbee") +ZIGPY_ZNP_VERSION = version("zigpy-znp") +ZIGPY_ZIGATE_VERSION = version("zigpy-zigate") +ZHA_QUIRKS_VERSION = version("zha-quirks") +ZHA_VERSION = version("zha") + def shallow_asdict(obj: Any) -> dict: """Return a shallow copy of a dataclass as a dict.""" @@ -86,14 +95,14 @@ async def async_get_config_entry_diagnostics( channel: 100 * energy / 255 for channel, energy in energy_scan.items() }, "versions": { - "bellows": version("bellows"), - "zigpy": version("zigpy"), - "zigpy_deconz": version("zigpy-deconz"), - "zigpy_xbee": version("zigpy-xbee"), - "zigpy_znp": version("zigpy_znp"), - "zigpy_zigate": version("zigpy-zigate"), - "zhaquirks": version("zha-quirks"), - "zha": version("zha"), + "bellows": BELLOWS_VERSION, + "zigpy": ZIGPY_VERSION, + "zigpy_deconz": ZIGPY_DECONZ_VERSION, + "zigpy_xbee": ZIGPY_XBEE_VERSION, + "zigpy_znp": ZIGPY_ZNP_VERSION, + "zigpy_zigate": ZIGPY_ZIGATE_VERSION, + "zhaquirks": ZHA_QUIRKS_VERSION, + "zha": ZHA_VERSION, }, "devices": [ { diff --git a/homeassistant/components/zha/helpers.py b/homeassistant/components/zha/helpers.py index 35a794e863188d..f70c8a9cb3ee16 100644 --- a/homeassistant/components/zha/helpers.py +++ b/homeassistant/components/zha/helpers.py @@ -802,21 +802,24 @@ def _create_entity_metadata( ) def _cleanup_group_entity_registry_entries( - self, zigpy_group: zigpy.group.Group + self, zha_group_proxy: ZHAGroupProxy ) -> None: """Remove entity registry entries for group entities when the groups are removed from HA.""" # first we collect the potential unique ids for entities that could be created from this group possible_entity_unique_ids = [ - f"{domain}_zha_group_0x{zigpy_group.group_id:04x}" + f"{domain}_zha_group_0x{zha_group_proxy.group.group_id:04x}" for domain in GROUP_ENTITY_DOMAINS ] # then we get all group entity entries tied to the coordinator entity_registry = er.async_get(self.hass) - assert self.coordinator_zha_device + assert self.gateway.coordinator_zha_device + coordinator_proxy = self.device_proxies[ + self.gateway.coordinator_zha_device.ieee + ] all_group_entity_entries = er.async_entries_for_device( entity_registry, - self.coordinator_zha_device.device_id, + coordinator_proxy.device_id, include_disabled_entities=True, ) @@ -1012,16 +1015,12 @@ def async_get_zha_device_proxy(hass: HomeAssistant, device_id: str) -> ZHADevice _LOGGER.error("Device id `%s` not found in registry", device_id) raise KeyError(f"Device id `{device_id}` not found in registry.") zha_gateway_proxy = get_zha_gateway_proxy(hass) - try: - ieee_address = list(registry_device.identifiers)[0][1] - ieee = EUI64.convert(ieee_address) - except (IndexError, ValueError) as ex: - _LOGGER.error( - "Unable to determine device IEEE for device with device id `%s`", device_id - ) - raise KeyError( - f"Unable to determine device IEEE for device with device id `{device_id}`." - ) from ex + ieee_address = next( + identifier + for domain, identifier in registry_device.identifiers + if domain == DOMAIN + ) + ieee = EUI64.convert(ieee_address) return zha_gateway_proxy.device_proxies[ieee] diff --git a/homeassistant/components/zha/icons.json b/homeassistant/components/zha/icons.json index 9b060e8105a295..9d5254fe237469 100644 --- a/homeassistant/components/zha/icons.json +++ b/homeassistant/components/zha/icons.json @@ -86,6 +86,18 @@ }, "presence_detection_timeout": { "default": "mdi:timer-edit" + }, + "exercise_trigger_time": { + "default": "mdi:clock" + }, + "external_temperature_sensor": { + "default": "mdi:thermometer" + }, + "load_room_mean": { + "default": "mdi:scale-balance" + }, + "regulation_setpoint_offset": { + "default": "mdi:thermostat" } }, "select": { @@ -94,6 +106,9 @@ }, "keypad_lockout": { "default": "mdi:lock" + }, + "exercise_day_of_week": { + "default": "mdi:wrench-clock" } }, "sensor": { @@ -132,6 +147,15 @@ }, "hooks_state": { "default": "mdi:hook" + }, + "open_window_detected": { + "default": "mdi:window-open" + }, + "load_estimate": { + "default": "mdi:scale-balance" + }, + "preheat_time": { + "default": "mdi:radiator" } }, "switch": { @@ -158,21 +182,60 @@ }, "hooks_locked": { "default": "mdi:lock" + }, + "external_window_sensor": { + "default": "mdi:window-open" + }, + "use_internal_window_detection": { + "default": "mdi:window-open" + }, + "prioritize_external_temperature_sensor": { + "default": "mdi:thermometer" + }, + "heat_available": { + "default": "mdi:water-boiler" + }, + "use_load_balancing": { + "default": "mdi:scale-balance" } } }, "services": { - "permit": "mdi:cellphone-link", - "remove": "mdi:cellphone-remove", - "reconfigure_device": "mdi:cellphone-cog", - "set_zigbee_cluster_attribute": "mdi:cog", - "issue_zigbee_cluster_command": "mdi:console", - "issue_zigbee_group_command": "mdi:console", - "warning_device_squawk": "mdi:alert", - "warning_device_warn": "mdi:alert", - "clear_lock_user_code": "mdi:lock-remove", - "enable_lock_user_code": "mdi:lock", - "disable_lock_user_code": "mdi:lock-off", - "set_lock_user_code": "mdi:lock" + "permit": { + "service": "mdi:cellphone-link" + }, + "remove": { + "service": "mdi:cellphone-remove" + }, + "reconfigure_device": { + "service": "mdi:cellphone-cog" + }, + "set_zigbee_cluster_attribute": { + "service": "mdi:cog" + }, + "issue_zigbee_cluster_command": { + "service": "mdi:console" + }, + "issue_zigbee_group_command": { + "service": "mdi:console" + }, + "warning_device_squawk": { + "service": "mdi:alert" + }, + "warning_device_warn": { + "service": "mdi:alert" + }, + "clear_lock_user_code": { + "service": "mdi:lock-remove" + }, + "enable_lock_user_code": { + "service": "mdi:lock" + }, + "disable_lock_user_code": { + "service": "mdi:lock-off" + }, + "set_lock_user_code": { + "service": "mdi:lock" + } } } diff --git a/homeassistant/components/zha/manifest.json b/homeassistant/components/zha/manifest.json index a5e57fcb1ec020..df60829a1e2e99 100644 --- a/homeassistant/components/zha/manifest.json +++ b/homeassistant/components/zha/manifest.json @@ -21,7 +21,7 @@ "zha", "universal_silabs_flasher" ], - "requirements": ["universal-silabs-flasher==0.0.22", "zha==0.0.31"], + "requirements": ["universal-silabs-flasher==0.0.22", "zha==0.0.32"], "usb": [ { "vid": "10C4", diff --git a/homeassistant/components/zha/update.py b/homeassistant/components/zha/update.py index e12d048b1908fe..3a857f9d89b901 100644 --- a/homeassistant/components/zha/update.py +++ b/homeassistant/components/zha/update.py @@ -95,6 +95,7 @@ class ZHAFirmwareUpdateEntity( UpdateEntityFeature.INSTALL | UpdateEntityFeature.PROGRESS | UpdateEntityFeature.SPECIFIC_VERSION + | UpdateEntityFeature.RELEASE_NOTES ) def __init__(self, entity_data: EntityData, **kwargs: Any) -> None: @@ -143,6 +144,14 @@ def release_summary(self) -> str | None: """ return self.entity_data.entity.release_summary + async def async_release_notes(self) -> str | None: + """Return full release notes. + + This is suitable for a long changelog that does not fit in the release_summary + property. The returned string can contain markdown. + """ + return self.entity_data.entity.release_notes + @property def release_url(self) -> str | None: """URL to the full release notes of the latest version available.""" @@ -155,7 +164,7 @@ async def async_install( ) -> None: """Install an update.""" try: - await self.entity_data.entity.async_install(version=version, backup=backup) + await self.entity_data.entity.async_install(version=version) except ZHAException as exc: raise HomeAssistantError(exc) from exc finally: diff --git a/homeassistant/components/zone/icons.json b/homeassistant/components/zone/icons.json index a03163179cb5bd..a9829425570a0c 100644 --- a/homeassistant/components/zone/icons.json +++ b/homeassistant/components/zone/icons.json @@ -1,5 +1,7 @@ { "services": { - "reload": "mdi:reload" + "reload": { + "service": "mdi:reload" + } } } diff --git a/homeassistant/components/zoneminder/icons.json b/homeassistant/components/zoneminder/icons.json index 8ca180d7399586..3f9f6410a22ad4 100644 --- a/homeassistant/components/zoneminder/icons.json +++ b/homeassistant/components/zoneminder/icons.json @@ -1,5 +1,7 @@ { "services": { - "set_run_state": "mdi:cog" + "set_run_state": { + "service": "mdi:cog" + } } } diff --git a/homeassistant/components/zwave_js/__init__.py b/homeassistant/components/zwave_js/__init__.py index dedae10400f4ea..4844f70720135d 100644 --- a/homeassistant/components/zwave_js/__init__.py +++ b/homeassistant/components/zwave_js/__init__.py @@ -353,7 +353,7 @@ def __init__(self, hass: HomeAssistant, driver_events: DriverEvents) -> None: self.discovered_value_ids: dict[str, set[str]] = defaultdict(set) self.driver_events = driver_events self.dev_reg = driver_events.dev_reg - self.registered_unique_ids: dict[str, dict[str, set[str]]] = defaultdict( + self.registered_unique_ids: dict[str, dict[Platform, set[str]]] = defaultdict( lambda: defaultdict(set) ) self.node_events = NodeEvents(hass, self) diff --git a/homeassistant/components/zwave_js/config_flow.py b/homeassistant/components/zwave_js/config_flow.py index e73fa9fc3a799c..3e979b224ae776 100644 --- a/homeassistant/components/zwave_js/config_flow.py +++ b/homeassistant/components/zwave_js/config_flow.py @@ -366,18 +366,6 @@ def async_get_options_flow( """Return the options flow.""" return OptionsFlowHandler(config_entry) - async def async_step_import(self, data: dict[str, Any]) -> ConfigFlowResult: - """Handle imported data. - - This step will be used when importing data - during Z-Wave to Z-Wave JS migration. - """ - # Note that the data comes from the zwave integration. - # So we don't use our constants here. - self.s0_legacy_key = data.get("network_key") - self.usb_path = data.get("usb_path") - return await self.async_step_user() - async def async_step_user( self, user_input: dict[str, Any] | None = None ) -> ConfigFlowResult: diff --git a/homeassistant/components/zwave_js/discovery.py b/homeassistant/components/zwave_js/discovery.py index 6e750ee8b2d325..6de5a56dc330a2 100644 --- a/homeassistant/components/zwave_js/discovery.py +++ b/homeassistant/components/zwave_js/discovery.py @@ -238,6 +238,12 @@ class ZWaveDiscoverySchema: command_class={CommandClass.SWITCH_BINARY}, property={CURRENT_VALUE_PROPERTY} ) +COLOR_SWITCH_CURRENT_VALUE_SCHEMA = ZWaveValueDiscoverySchema( + command_class={CommandClass.SWITCH_COLOR}, + property={CURRENT_COLOR_PROPERTY}, + property_key={None}, +) + SIREN_TONE_SCHEMA = ZWaveValueDiscoverySchema( command_class={CommandClass.SOUND_SWITCH}, property={TONE_ID_PROPERTY}, @@ -762,33 +768,6 @@ class ZWaveDiscoverySchema: }, ), ), - # HomeSeer HSM-200 v1 - ZWaveDiscoverySchema( - platform=Platform.LIGHT, - hint="black_is_off", - manufacturer_id={0x001E}, - product_id={0x0001}, - product_type={0x0004}, - primary_value=ZWaveValueDiscoverySchema( - command_class={CommandClass.SWITCH_COLOR}, - property={CURRENT_COLOR_PROPERTY}, - property_key={None}, - ), - absent_values=[SWITCH_MULTILEVEL_CURRENT_VALUE_SCHEMA], - ), - # Logic Group ZDB5100 - ZWaveDiscoverySchema( - platform=Platform.LIGHT, - hint="black_is_off", - manufacturer_id={0x0234}, - product_id={0x0121}, - product_type={0x0003}, - primary_value=ZWaveValueDiscoverySchema( - command_class={CommandClass.SWITCH_COLOR}, - property={CURRENT_COLOR_PROPERTY}, - property_key={None}, - ), - ), # ====== START OF GENERIC MAPPING SCHEMAS ======= # locks # Door Lock CC @@ -1014,10 +993,11 @@ class ZWaveDiscoverySchema: ), entity_category=EntityCategory.CONFIG, ), - # binary switches + # binary switches without color support ZWaveDiscoverySchema( platform=Platform.SWITCH, primary_value=SWITCH_BINARY_CURRENT_VALUE_SCHEMA, + absent_values=[COLOR_SWITCH_CURRENT_VALUE_SCHEMA], ), # switch for Indicator CC ZWaveDiscoverySchema( @@ -1111,6 +1091,25 @@ class ZWaveDiscoverySchema: # catch any device with multilevel CC as light # NOTE: keep this at the bottom of the discovery scheme, # to handle all others that need the multilevel CC first + # + # Colored light (legacy device) that can only be controlled through Color Switch CC. + ZWaveDiscoverySchema( + platform=Platform.LIGHT, + hint="color_onoff", + primary_value=COLOR_SWITCH_CURRENT_VALUE_SCHEMA, + absent_values=[ + SWITCH_BINARY_CURRENT_VALUE_SCHEMA, + SWITCH_MULTILEVEL_CURRENT_VALUE_SCHEMA, + ], + ), + # Colored light that can be turned on or off with the Binary Switch CC. + ZWaveDiscoverySchema( + platform=Platform.LIGHT, + hint="color_onoff", + primary_value=SWITCH_BINARY_CURRENT_VALUE_SCHEMA, + required_values=[COLOR_SWITCH_CURRENT_VALUE_SCHEMA], + ), + # Dimmable light with or without color support. ZWaveDiscoverySchema( platform=Platform.LIGHT, primary_value=SWITCH_MULTILEVEL_CURRENT_VALUE_SCHEMA, diff --git a/homeassistant/components/zwave_js/entity.py b/homeassistant/components/zwave_js/entity.py index 4a6f87cc032976..d41c8bb01d0294 100644 --- a/homeassistant/components/zwave_js/entity.py +++ b/homeassistant/components/zwave_js/entity.py @@ -335,5 +335,6 @@ async def _async_set_value( value, new_value, options=options, wait_for_result=wait_for_result ) except BaseZwaveJSServerError as err: - LOGGER.error("Unable to set value %s: %s", value.value_id, err) - raise HomeAssistantError from err + raise HomeAssistantError( + f"Unable to set value {value.value_id}: {err}" + ) from err diff --git a/homeassistant/components/zwave_js/helpers.py b/homeassistant/components/zwave_js/helpers.py index 737b8deff34bea..5885527e01c33a 100644 --- a/homeassistant/components/zwave_js/helpers.py +++ b/homeassistant/components/zwave_js/helpers.py @@ -343,20 +343,18 @@ def async_get_nodes_from_area_id( } ) # Add devices in an area that are Z-Wave JS devices - for device in dr.async_entries_for_area(dev_reg, area_id): - if next( - ( - config_entry_id - for config_entry_id in device.config_entries - if cast( - ConfigEntry, - hass.config_entries.async_get_entry(config_entry_id), - ).domain - == DOMAIN - ), - None, - ): - nodes.add(async_get_node_from_device_id(hass, device.id, dev_reg)) + nodes.update( + async_get_node_from_device_id(hass, device.id, dev_reg) + for device in dr.async_entries_for_area(dev_reg, area_id) + if any( + cast( + ConfigEntry, + hass.config_entries.async_get_entry(config_entry_id), + ).domain + == DOMAIN + for config_entry_id in device.config_entries + ) + ) return nodes diff --git a/homeassistant/components/zwave_js/icons.json b/homeassistant/components/zwave_js/icons.json index 2956cf2c6e002a..b52255e09d15aa 100644 --- a/homeassistant/components/zwave_js/icons.json +++ b/homeassistant/components/zwave_js/icons.json @@ -57,17 +57,41 @@ } }, "services": { - "bulk_set_partial_config_parameters": "mdi:cogs", - "clear_lock_usercode": "mdi:eraser", - "invoke_cc_api": "mdi:api", - "multicast_set_value": "mdi:list-box", - "ping": "mdi:crosshairs-gps", - "refresh_notifications": "mdi:bell", - "refresh_value": "mdi:refresh", - "reset_meter": "mdi:meter-electric", - "set_config_parameter": "mdi:cog", - "set_lock_configuration": "mdi:shield-lock", - "set_lock_usercode": "mdi:lock-smart", - "set_value": "mdi:form-textbox" + "bulk_set_partial_config_parameters": { + "service": "mdi:cogs" + }, + "clear_lock_usercode": { + "service": "mdi:eraser" + }, + "invoke_cc_api": { + "service": "mdi:api" + }, + "multicast_set_value": { + "service": "mdi:list-box" + }, + "ping": { + "service": "mdi:crosshairs-gps" + }, + "refresh_notifications": { + "service": "mdi:bell" + }, + "refresh_value": { + "service": "mdi:refresh" + }, + "reset_meter": { + "service": "mdi:meter-electric" + }, + "set_config_parameter": { + "service": "mdi:cog" + }, + "set_lock_configuration": { + "service": "mdi:shield-lock" + }, + "set_lock_usercode": { + "service": "mdi:lock-smart" + }, + "set_value": { + "service": "mdi:form-textbox" + } } } diff --git a/homeassistant/components/zwave_js/light.py b/homeassistant/components/zwave_js/light.py index 020f1b66b3d108..4a044ca3f52aa9 100644 --- a/homeassistant/components/zwave_js/light.py +++ b/homeassistant/components/zwave_js/light.py @@ -76,8 +76,8 @@ def async_add_light(info: ZwaveDiscoveryInfo) -> None: driver = client.driver assert driver is not None # Driver is ready before platforms are loaded. - if info.platform_hint == "black_is_off": - async_add_entities([ZwaveBlackIsOffLight(config_entry, driver, info)]) + if info.platform_hint == "color_onoff": + async_add_entities([ZwaveColorOnOffLight(config_entry, driver, info)]) else: async_add_entities([ZwaveLight(config_entry, driver, info)]) @@ -111,9 +111,10 @@ def __init__( self._supports_color = False self._supports_rgbw = False self._supports_color_temp = False + self._supports_dimming = False + self._color_mode: str | None = None self._hs_color: tuple[float, float] | None = None self._rgbw_color: tuple[int, int, int, int] | None = None - self._color_mode: str | None = None self._color_temp: int | None = None self._min_mireds = 153 # 6500K as a safe default self._max_mireds = 370 # 2700K as a safe default @@ -129,15 +130,28 @@ def __init__( ) self._supported_color_modes: set[ColorMode] = set() + self._target_brightness: Value | None = None + # get additional (optional) values and set features - # If the command class is Basic, we must geenerate a name that includes - # the command class name to avoid ambiguity - self._target_brightness = self.get_zwave_value( - TARGET_VALUE_PROPERTY, - CommandClass.SWITCH_MULTILEVEL, - add_to_watched_value_ids=False, - ) - if self.info.primary_value.command_class == CommandClass.BASIC: + if self.info.primary_value.command_class == CommandClass.SWITCH_BINARY: + # This light can not be dimmed separately from the color channels + self._target_brightness = self.get_zwave_value( + TARGET_VALUE_PROPERTY, + CommandClass.SWITCH_BINARY, + add_to_watched_value_ids=False, + ) + self._supports_dimming = False + elif self.info.primary_value.command_class == CommandClass.SWITCH_MULTILEVEL: + # This light can be dimmed separately from the color channels + self._target_brightness = self.get_zwave_value( + TARGET_VALUE_PROPERTY, + CommandClass.SWITCH_MULTILEVEL, + add_to_watched_value_ids=False, + ) + self._supports_dimming = True + elif self.info.primary_value.command_class == CommandClass.BASIC: + # If the command class is Basic, we must generate a name that includes + # the command class name to avoid ambiguity self._attr_name = self.generate_name( include_value_name=True, alternate_value_name="Basic" ) @@ -146,6 +160,13 @@ def __init__( CommandClass.BASIC, add_to_watched_value_ids=False, ) + self._supports_dimming = True + + self._current_color = self.get_zwave_value( + CURRENT_COLOR_PROPERTY, + CommandClass.SWITCH_COLOR, + value_property_key=None, + ) self._target_color = self.get_zwave_value( TARGET_COLOR_PROPERTY, CommandClass.SWITCH_COLOR, @@ -216,7 +237,7 @@ def hs_color(self) -> tuple[float, float] | None: @property def rgbw_color(self) -> tuple[int, int, int, int] | None: - """Return the hs color.""" + """Return the RGBW color.""" return self._rgbw_color @property @@ -243,11 +264,39 @@ async def async_turn_on(self, **kwargs: Any) -> None: """Turn the device on.""" transition = kwargs.get(ATTR_TRANSITION) + brightness = kwargs.get(ATTR_BRIGHTNESS) - # RGB/HS color hs_color = kwargs.get(ATTR_HS_COLOR) + color_temp = kwargs.get(ATTR_COLOR_TEMP) + rgbw = kwargs.get(ATTR_RGBW_COLOR) + + new_colors = self._get_new_colors(hs_color, color_temp, rgbw) + if new_colors is not None: + await self._async_set_colors(new_colors, transition) + + # set brightness (or turn on if dimming is not supported) + await self._async_set_brightness(brightness, transition) + + async def async_turn_off(self, **kwargs: Any) -> None: + """Turn the light off.""" + await self._async_set_brightness(0, kwargs.get(ATTR_TRANSITION)) + + def _get_new_colors( + self, + hs_color: tuple[float, float] | None, + color_temp: int | None, + rgbw: tuple[int, int, int, int] | None, + brightness_scale: float | None = None, + ) -> dict[ColorComponent, int] | None: + """Determine the new color dict to set.""" + + # RGB/HS color if hs_color is not None and self._supports_color: red, green, blue = color_util.color_hs_to_RGB(*hs_color) + if brightness_scale is not None: + red = round(red * brightness_scale) + green = round(green * brightness_scale) + blue = round(blue * brightness_scale) colors = { ColorComponent.RED: red, ColorComponent.GREEN: green, @@ -257,10 +306,9 @@ async def async_turn_on(self, **kwargs: Any) -> None: # turn of white leds when setting rgb colors[ColorComponent.WARM_WHITE] = 0 colors[ColorComponent.COLD_WHITE] = 0 - await self._async_set_colors(colors, transition) + return colors # Color temperature - color_temp = kwargs.get(ATTR_COLOR_TEMP) if color_temp is not None and self._supports_color_temp: # Limit color temp to min/max values cold = max( @@ -275,20 +323,18 @@ async def async_turn_on(self, **kwargs: Any) -> None: ), ) warm = 255 - cold - await self._async_set_colors( - { - # turn off color leds when setting color temperature - ColorComponent.RED: 0, - ColorComponent.GREEN: 0, - ColorComponent.BLUE: 0, - ColorComponent.WARM_WHITE: warm, - ColorComponent.COLD_WHITE: cold, - }, - transition, - ) + colors = { + ColorComponent.WARM_WHITE: warm, + ColorComponent.COLD_WHITE: cold, + } + if self._supports_color: + # turn off color leds when setting color temperature + colors[ColorComponent.RED] = 0 + colors[ColorComponent.GREEN] = 0 + colors[ColorComponent.BLUE] = 0 + return colors # RGBW - rgbw = kwargs.get(ATTR_RGBW_COLOR) if rgbw is not None and self._supports_rgbw: rgbw_channels = { ColorComponent.RED: rgbw[0], @@ -300,17 +346,15 @@ async def async_turn_on(self, **kwargs: Any) -> None: if self._cold_white: rgbw_channels[ColorComponent.COLD_WHITE] = rgbw[3] - await self._async_set_colors(rgbw_channels, transition) - # set brightness - await self._async_set_brightness(kwargs.get(ATTR_BRIGHTNESS), transition) + return rgbw_channels - async def async_turn_off(self, **kwargs: Any) -> None: - """Turn the light off.""" - await self._async_set_brightness(0, kwargs.get(ATTR_TRANSITION)) + return None async def _async_set_colors( - self, colors: dict[ColorComponent, int], transition: float | None = None + self, + colors: dict[ColorComponent, int], + transition: float | None = None, ) -> None: """Set (multiple) defined colors to given value(s).""" # prefer the (new) combined color property @@ -361,9 +405,14 @@ async def _async_set_brightness( zwave_transition = {TRANSITION_DURATION_OPTION: "default"} # setting a value requires setting targetValue - await self._async_set_value( - self._target_brightness, zwave_brightness, zwave_transition - ) + if self._supports_dimming: + await self._async_set_value( + self._target_brightness, zwave_brightness, zwave_transition + ) + else: + await self._async_set_value( + self._target_brightness, zwave_brightness > 0, zwave_transition + ) # We do an optimistic state update when setting to a previous value # to avoid waiting for the value to be updated from the device which is # typically delayed and causes a confusing UX. @@ -427,15 +476,8 @@ def _calculate_color_values(self) -> None: """Calculate light colors.""" (red_val, green_val, blue_val, ww_val, cw_val) = self._get_color_values() - # prefer the (new) combined color property - # https://github.com/zwave-js/node-zwave-js/pull/1782 - combined_color_val = self.get_zwave_value( - CURRENT_COLOR_PROPERTY, - CommandClass.SWITCH_COLOR, - value_property_key=None, - ) - if combined_color_val and isinstance(combined_color_val.value, dict): - multi_color = combined_color_val.value + if self._current_color and isinstance(self._current_color.value, dict): + multi_color = self._current_color.value else: multi_color = {} @@ -486,11 +528,10 @@ def _calculate_color_values(self) -> None: self._color_mode = ColorMode.RGBW -class ZwaveBlackIsOffLight(ZwaveLight): - """Representation of a Z-Wave light where setting the color to black turns it off. +class ZwaveColorOnOffLight(ZwaveLight): + """Representation of a colored Z-Wave light with an optional binary switch to turn on/off. - Currently only supports lights with RGB, no color temperature, and no white - channels. + Dimming for RGB lights is realized by scaling the color channels. """ def __init__( @@ -499,61 +540,137 @@ def __init__( """Initialize the light.""" super().__init__(config_entry, driver, info) - self._last_color: dict[str, int] | None = None - self._supported_color_modes.discard(ColorMode.BRIGHTNESS) + self._last_on_color: dict[ColorComponent, int] | None = None + self._last_brightness: int | None = None @property - def brightness(self) -> int: - """Return the brightness of this light between 0..255.""" - return 255 + def brightness(self) -> int | None: + """Return the brightness of this light between 0..255. - @property - def is_on(self) -> bool | None: - """Return true if device is on (brightness above 0).""" + Z-Wave multilevel switches use a range of [0, 99] to control brightness. + """ if self.info.primary_value.value is None: return None - return any(value != 0 for value in self.info.primary_value.value.values()) + if self._target_brightness and self.info.primary_value.value is False: + # Binary switch exists and is turned off + return 0 + + # Brightness is encoded in the color channels by scaling them lower than 255 + color_values = [ + v.value + for v in self._get_color_values() + if v is not None and v.value is not None + ] + return max(color_values) if color_values else 0 async def async_turn_on(self, **kwargs: Any) -> None: """Turn the device on.""" + if ( kwargs.get(ATTR_RGBW_COLOR) is not None or kwargs.get(ATTR_COLOR_TEMP) is not None - or kwargs.get(ATTR_HS_COLOR) is not None ): + # RGBW and color temp are not supported in this mode, + # delegate to the parent class await super().async_turn_on(**kwargs) return transition = kwargs.get(ATTR_TRANSITION) - # turn on light to last color if known, otherwise set to white - if self._last_color is not None: - await self._async_set_colors( - { - ColorComponent.RED: self._last_color["red"], - ColorComponent.GREEN: self._last_color["green"], - ColorComponent.BLUE: self._last_color["blue"], - }, - transition, - ) - else: - await self._async_set_colors( - { + brightness = kwargs.get(ATTR_BRIGHTNESS) + hs_color = kwargs.get(ATTR_HS_COLOR) + new_colors: dict[ColorComponent, int] | None = None + scale: float | None = None + + if brightness is None and hs_color is None: + # Turned on without specifying brightness or color + if self._last_on_color is not None: + if self._target_brightness: + # Color is already set, use the binary switch to turn on + await self._async_set_brightness(None, transition) + return + + # Preserve the previous color + new_colors = self._last_on_color + elif self._supports_color: + # Turned on for the first time. Make it white + new_colors = { ColorComponent.RED: 255, ColorComponent.GREEN: 255, ColorComponent.BLUE: 255, - }, - transition, + } + elif brightness is not None: + # If brightness gets set, preserve the color and mix it with the new brightness + if self.color_mode == ColorMode.HS: + scale = brightness / 255 + if ( + self._last_on_color is not None + and None not in self._last_on_color.values() + ): + # Changed brightness from 0 to >0 + old_brightness = max(self._last_on_color.values()) + new_scale = brightness / old_brightness + scale = new_scale + new_colors = {} + for color, value in self._last_on_color.items(): + new_colors[color] = round(value * new_scale) + elif hs_color is None and self._color_mode == ColorMode.HS: + hs_color = self._hs_color + elif hs_color is not None and brightness is None: + # Turned on by using the color controls + current_brightness = self.brightness + if current_brightness == 0 and self._last_brightness is not None: + # Use the last brightness value if the light is currently off + scale = self._last_brightness / 255 + elif current_brightness is not None: + scale = current_brightness / 255 + + # Reset last color until turning off again + self._last_on_color = None + + if new_colors is None: + new_colors = self._get_new_colors( + hs_color=hs_color, color_temp=None, rgbw=None, brightness_scale=scale ) + if new_colors is not None: + await self._async_set_colors(new_colors, transition) + + # Turn the binary switch on if there is one + await self._async_set_brightness(brightness, transition) + async def async_turn_off(self, **kwargs: Any) -> None: """Turn the light off.""" - self._last_color = self.info.primary_value.value - await self._async_set_colors( - { + + # Remember last color and brightness to restore it when turning on + self._last_brightness = self.brightness + if self._current_color and isinstance(self._current_color.value, dict): + red = self._current_color.value.get(COLOR_SWITCH_COMBINED_RED) + green = self._current_color.value.get(COLOR_SWITCH_COMBINED_GREEN) + blue = self._current_color.value.get(COLOR_SWITCH_COMBINED_BLUE) + + last_color: dict[ColorComponent, int] = {} + if red is not None: + last_color[ColorComponent.RED] = red + if green is not None: + last_color[ColorComponent.GREEN] = green + if blue is not None: + last_color[ColorComponent.BLUE] = blue + + if last_color: + self._last_on_color = last_color + + if self._target_brightness: + # Turn off the binary switch only + await self._async_set_brightness(0, kwargs.get(ATTR_TRANSITION)) + else: + # turn off all color channels + colors = { ColorComponent.RED: 0, ColorComponent.GREEN: 0, ColorComponent.BLUE: 0, - }, - kwargs.get(ATTR_TRANSITION), - ) - await self._async_set_brightness(0, kwargs.get(ATTR_TRANSITION)) + } + + await self._async_set_colors( + colors, + kwargs.get(ATTR_TRANSITION), + ) diff --git a/homeassistant/components/zwave_js/migrate.py b/homeassistant/components/zwave_js/migrate.py index bde53137dc1ec6..ac749cb516b5f9 100644 --- a/homeassistant/components/zwave_js/migrate.py +++ b/homeassistant/components/zwave_js/migrate.py @@ -6,20 +6,16 @@ import logging from zwave_js_server.model.driver import Driver +from zwave_js_server.model.node import Node from zwave_js_server.model.value import Value as ZwaveValue -from homeassistant.const import STATE_UNAVAILABLE +from homeassistant.const import STATE_UNAVAILABLE, Platform from homeassistant.core import HomeAssistant, callback -from homeassistant.helpers.device_registry import DeviceEntry -from homeassistant.helpers.entity_registry import ( - EntityRegistry, - RegistryEntry, - async_entries_for_device, -) +from homeassistant.helpers import device_registry as dr, entity_registry as er from .const import DOMAIN from .discovery import ZwaveDiscoveryInfo -from .helpers import get_unique_id +from .helpers import get_unique_id, get_valueless_base_unique_id _LOGGER = logging.getLogger(__name__) @@ -62,10 +58,10 @@ def is_same_value_different_endpoints(self, other: ValueID) -> bool: @callback def async_migrate_old_entity( hass: HomeAssistant, - ent_reg: EntityRegistry, + ent_reg: er.EntityRegistry, registered_unique_ids: set[str], - platform: str, - device: DeviceEntry, + platform: Platform, + device: dr.DeviceEntry, unique_id: str, ) -> None: """Migrate existing entity if current one can't be found and an old one exists.""" @@ -77,8 +73,8 @@ def async_migrate_old_entity( # Look for existing entities in the registry that could be the same value but on # a different endpoint - existing_entity_entries: list[RegistryEntry] = [] - for entry in async_entries_for_device(ent_reg, device.id): + existing_entity_entries: list[er.RegistryEntry] = [] + for entry in er.async_entries_for_device(ent_reg, device.id): # If entity is not in the domain for this discovery info or entity has already # been processed, skip it if entry.domain != platform or entry.unique_id in registered_unique_ids: @@ -109,35 +105,40 @@ def async_migrate_old_entity( @callback def async_migrate_unique_id( - ent_reg: EntityRegistry, platform: str, old_unique_id: str, new_unique_id: str + ent_reg: er.EntityRegistry, + platform: Platform, + old_unique_id: str, + new_unique_id: str, ) -> None: """Check if entity with old unique ID exists, and if so migrate it to new ID.""" - if entity_id := ent_reg.async_get_entity_id(platform, DOMAIN, old_unique_id): + if not (entity_id := ent_reg.async_get_entity_id(platform, DOMAIN, old_unique_id)): + return + + _LOGGER.debug( + "Migrating entity %s from old unique ID '%s' to new unique ID '%s'", + entity_id, + old_unique_id, + new_unique_id, + ) + try: + ent_reg.async_update_entity(entity_id, new_unique_id=new_unique_id) + except ValueError: _LOGGER.debug( - "Migrating entity %s from old unique ID '%s' to new unique ID '%s'", + ( + "Entity %s can't be migrated because the unique ID is taken; " + "Cleaning it up since it is likely no longer valid" + ), entity_id, - old_unique_id, - new_unique_id, ) - try: - ent_reg.async_update_entity(entity_id, new_unique_id=new_unique_id) - except ValueError: - _LOGGER.debug( - ( - "Entity %s can't be migrated because the unique ID is taken; " - "Cleaning it up since it is likely no longer valid" - ), - entity_id, - ) - ent_reg.async_remove(entity_id) + ent_reg.async_remove(entity_id) @callback def async_migrate_discovered_value( hass: HomeAssistant, - ent_reg: EntityRegistry, + ent_reg: er.EntityRegistry, registered_unique_ids: set[str], - device: DeviceEntry, + device: dr.DeviceEntry, driver: Driver, disc_info: ZwaveDiscoveryInfo, ) -> None: @@ -160,7 +161,7 @@ def async_migrate_discovered_value( ] if ( - disc_info.platform == "binary_sensor" + disc_info.platform == Platform.BINARY_SENSOR and disc_info.platform_hint == "notification" ): for state_key in disc_info.primary_value.metadata.states: @@ -211,6 +212,24 @@ def async_migrate_discovered_value( registered_unique_ids.add(new_unique_id) +@callback +def async_migrate_statistics_sensors( + hass: HomeAssistant, driver: Driver, node: Node, key_map: dict[str, str] +) -> None: + """Migrate statistics sensors to new unique IDs. + + - Migrate camel case keys in unique IDs to snake keys. + """ + ent_reg = er.async_get(hass) + base_unique_id = f"{get_valueless_base_unique_id(driver, node)}.statistics" + for new_key, old_key in key_map.items(): + if new_key == old_key: + continue + old_unique_id = f"{base_unique_id}_{old_key}" + new_unique_id = f"{base_unique_id}_{new_key}" + async_migrate_unique_id(ent_reg, Platform.SENSOR, old_unique_id, new_unique_id) + + @callback def get_old_value_ids(value: ZwaveValue) -> list[str]: """Get old value IDs so we can migrate entity unique ID.""" diff --git a/homeassistant/components/zwave_js/sensor.py b/homeassistant/components/zwave_js/sensor.py index e43c620ff545c8..f52801109a1d5a 100644 --- a/homeassistant/components/zwave_js/sensor.py +++ b/homeassistant/components/zwave_js/sensor.py @@ -4,7 +4,6 @@ from collections.abc import Callable, Mapping from dataclasses import dataclass -from datetime import datetime from typing import Any import voluptuous as vol @@ -16,10 +15,10 @@ ) from zwave_js_server.exceptions import BaseZwaveJSServerError from zwave_js_server.model.controller import Controller -from zwave_js_server.model.controller.statistics import ControllerStatisticsDataType +from zwave_js_server.model.controller.statistics import ControllerStatistics from zwave_js_server.model.driver import Driver from zwave_js_server.model.node import Node as ZwaveNode -from zwave_js_server.model.node.statistics import NodeStatisticsDataType +from zwave_js_server.model.node.statistics import NodeStatistics from zwave_js_server.util.command_class.meter import get_meter_type from homeassistant.components.sensor import ( @@ -90,6 +89,7 @@ ) from .entity import ZWaveBaseEntity from .helpers import get_device_info, get_valueless_base_unique_id +from .migrate import async_migrate_statistics_sensors PARALLEL_UPDATES = 0 @@ -328,152 +328,172 @@ } -def convert_dict_of_dicts( - statistics: ControllerStatisticsDataType | NodeStatisticsDataType, key: str +def convert_nested_attr( + statistics: ControllerStatistics | NodeStatistics, key: str ) -> Any: - """Convert a dictionary of dictionaries to a value.""" - keys = key.split(".") - return statistics.get(keys[0], {}).get(keys[1], {}).get(keys[2]) # type: ignore[attr-defined] + """Convert a string that represents a nested attr to a value.""" + data = statistics + for _key in key.split("."): + if data is None: + return None # type: ignore[unreachable] + data = getattr(data, _key) + return data @dataclass(frozen=True, kw_only=True) class ZWaveJSStatisticsSensorEntityDescription(SensorEntityDescription): """Class to represent a Z-Wave JS statistics sensor entity description.""" - convert: Callable[ - [ControllerStatisticsDataType | NodeStatisticsDataType, str], Any - ] = lambda statistics, key: statistics.get(key) + convert: Callable[[ControllerStatistics | NodeStatistics, str], Any] = getattr entity_registry_enabled_default: bool = False # Controller statistics descriptions ENTITY_DESCRIPTION_CONTROLLER_STATISTICS_LIST = [ ZWaveJSStatisticsSensorEntityDescription( - key="messagesTX", + key="messages_tx", translation_key="successful_messages", translation_placeholders={"direction": "TX"}, state_class=SensorStateClass.TOTAL, ), ZWaveJSStatisticsSensorEntityDescription( - key="messagesRX", + key="messages_rx", translation_key="successful_messages", translation_placeholders={"direction": "RX"}, state_class=SensorStateClass.TOTAL, ), ZWaveJSStatisticsSensorEntityDescription( - key="messagesDroppedTX", + key="messages_dropped_tx", translation_key="messages_dropped", translation_placeholders={"direction": "TX"}, state_class=SensorStateClass.TOTAL, ), ZWaveJSStatisticsSensorEntityDescription( - key="messagesDroppedRX", + key="messages_dropped_rx", translation_key="messages_dropped", translation_placeholders={"direction": "RX"}, state_class=SensorStateClass.TOTAL, ), ZWaveJSStatisticsSensorEntityDescription( - key="NAK", translation_key="nak", state_class=SensorStateClass.TOTAL + key="nak", translation_key="nak", state_class=SensorStateClass.TOTAL ), ZWaveJSStatisticsSensorEntityDescription( - key="CAN", translation_key="can", state_class=SensorStateClass.TOTAL + key="can", translation_key="can", state_class=SensorStateClass.TOTAL ), ZWaveJSStatisticsSensorEntityDescription( - key="timeoutACK", + key="timeout_ack", translation_key="timeout_ack", state_class=SensorStateClass.TOTAL, ), ZWaveJSStatisticsSensorEntityDescription( - key="timeoutResponse", + key="timeout_response", translation_key="timeout_response", state_class=SensorStateClass.TOTAL, ), ZWaveJSStatisticsSensorEntityDescription( - key="timeoutCallback", + key="timeout_callback", translation_key="timeout_callback", state_class=SensorStateClass.TOTAL, ), ZWaveJSStatisticsSensorEntityDescription( - key="backgroundRSSI.channel0.average", + key="background_rssi.channel_0.average", translation_key="average_background_rssi", translation_placeholders={"channel": "0"}, native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT, device_class=SensorDeviceClass.SIGNAL_STRENGTH, - convert=convert_dict_of_dicts, + convert=convert_nested_attr, ), ZWaveJSStatisticsSensorEntityDescription( - key="backgroundRSSI.channel0.current", + key="background_rssi.channel_0.current", translation_key="current_background_rssi", translation_placeholders={"channel": "0"}, native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT, device_class=SensorDeviceClass.SIGNAL_STRENGTH, state_class=SensorStateClass.MEASUREMENT, - convert=convert_dict_of_dicts, + convert=convert_nested_attr, ), ZWaveJSStatisticsSensorEntityDescription( - key="backgroundRSSI.channel1.average", + key="background_rssi.channel_1.average", translation_key="average_background_rssi", translation_placeholders={"channel": "1"}, native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT, device_class=SensorDeviceClass.SIGNAL_STRENGTH, - convert=convert_dict_of_dicts, + convert=convert_nested_attr, ), ZWaveJSStatisticsSensorEntityDescription( - key="backgroundRSSI.channel1.current", + key="background_rssi.channel_1.current", translation_key="current_background_rssi", translation_placeholders={"channel": "1"}, native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT, device_class=SensorDeviceClass.SIGNAL_STRENGTH, state_class=SensorStateClass.MEASUREMENT, - convert=convert_dict_of_dicts, + convert=convert_nested_attr, ), ZWaveJSStatisticsSensorEntityDescription( - key="backgroundRSSI.channel2.average", + key="background_rssi.channel_2.average", translation_key="average_background_rssi", translation_placeholders={"channel": "2"}, native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT, device_class=SensorDeviceClass.SIGNAL_STRENGTH, - convert=convert_dict_of_dicts, + convert=convert_nested_attr, ), ZWaveJSStatisticsSensorEntityDescription( - key="backgroundRSSI.channel2.current", + key="background_rssi.channel_2.current", translation_key="current_background_rssi", translation_placeholders={"channel": "2"}, native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT, device_class=SensorDeviceClass.SIGNAL_STRENGTH, state_class=SensorStateClass.MEASUREMENT, - convert=convert_dict_of_dicts, + convert=convert_nested_attr, ), ] +CONTROLLER_STATISTICS_KEY_MAP: dict[str, str] = { + "messages_tx": "messagesTX", + "messages_rx": "messagesRX", + "messages_dropped_tx": "messagesDroppedTX", + "messages_dropped_rx": "messagesDroppedRX", + "nak": "NAK", + "can": "CAN", + "timeout_ack": "timeoutAck", + "timeout_response": "timeoutResponse", + "timeout_callback": "timeoutCallback", + "background_rssi.channel_0.average": "backgroundRSSI.channel0.average", + "background_rssi.channel_0.current": "backgroundRSSI.channel0.current", + "background_rssi.channel_1.average": "backgroundRSSI.channel1.average", + "background_rssi.channel_1.current": "backgroundRSSI.channel1.current", + "background_rssi.channel_2.average": "backgroundRSSI.channel2.average", + "background_rssi.channel_2.current": "backgroundRSSI.channel2.current", +} + # Node statistics descriptions ENTITY_DESCRIPTION_NODE_STATISTICS_LIST = [ ZWaveJSStatisticsSensorEntityDescription( - key="commandsRX", + key="commands_rx", translation_key="successful_commands", translation_placeholders={"direction": "RX"}, state_class=SensorStateClass.TOTAL, ), ZWaveJSStatisticsSensorEntityDescription( - key="commandsTX", + key="commands_tx", translation_key="successful_commands", translation_placeholders={"direction": "TX"}, state_class=SensorStateClass.TOTAL, ), ZWaveJSStatisticsSensorEntityDescription( - key="commandsDroppedRX", + key="commands_dropped_rx", translation_key="commands_dropped", translation_placeholders={"direction": "RX"}, state_class=SensorStateClass.TOTAL, ), ZWaveJSStatisticsSensorEntityDescription( - key="commandsDroppedTX", + key="commands_dropped_tx", translation_key="commands_dropped", translation_placeholders={"direction": "TX"}, state_class=SensorStateClass.TOTAL, ), ZWaveJSStatisticsSensorEntityDescription( - key="timeoutResponse", + key="timeout_response", translation_key="timeout_response", state_class=SensorStateClass.TOTAL, ), @@ -492,20 +512,24 @@ class ZWaveJSStatisticsSensorEntityDescription(SensorEntityDescription): state_class=SensorStateClass.MEASUREMENT, ), ZWaveJSStatisticsSensorEntityDescription( - key="lastSeen", + key="last_seen", translation_key="last_seen", device_class=SensorDeviceClass.TIMESTAMP, - convert=( - lambda statistics, key: ( - datetime.fromisoformat(dt) # type: ignore[arg-type] - if (dt := statistics.get(key)) - else None - ) - ), entity_registry_enabled_default=True, ), ] +NODE_STATISTICS_KEY_MAP: dict[str, str] = { + "commands_rx": "commandsRX", + "commands_tx": "commandsTX", + "commands_dropped_rx": "commandsDroppedRX", + "commands_dropped_tx": "commandsDroppedTX", + "timeout_response": "timeoutResponse", + "rtt": "rtt", + "rssi": "rssi", + "last_seen": "lastSeen", +} + def get_entity_description( data: NumericSensorDataTemplateData, @@ -588,6 +612,14 @@ def async_add_node_status_sensor(node: ZwaveNode) -> None: @callback def async_add_statistics_sensors(node: ZwaveNode) -> None: """Add statistics sensors.""" + async_migrate_statistics_sensors( + hass, + driver, + node, + CONTROLLER_STATISTICS_KEY_MAP + if driver.controller.own_node == node + else NODE_STATISTICS_KEY_MAP, + ) async_add_entities( [ ZWaveStatisticsSensor( @@ -750,10 +782,9 @@ async def async_reset_meter( CommandClass.METER, "reset", *args, wait_for_result=False ) except BaseZwaveJSServerError as err: - LOGGER.error( - "Failed to reset meters on node %s endpoint %s: %s", node, endpoint, err - ) - raise HomeAssistantError from err + raise HomeAssistantError( + f"Failed to reset meters on node {node} endpoint {endpoint}: {err}" + ) from err LOGGER.debug( "Meters on node %s endpoint %s reset with the following options: %s", node, @@ -1002,7 +1033,7 @@ async def async_poll_value(self, _: bool) -> None: def statistics_updated(self, event_data: dict) -> None: """Call when statistics updated event is received.""" self._attr_native_value = self.entity_description.convert( - event_data["statistics"], self.entity_description.key + event_data["statistics_updated"], self.entity_description.key ) self.async_write_ha_state() @@ -1028,5 +1059,5 @@ async def async_added_to_hass(self) -> None: # Set initial state self._attr_native_value = self.entity_description.convert( - self.statistics_src.statistics.data, self.entity_description.key + self.statistics_src.statistics, self.entity_description.key ) diff --git a/homeassistant/components/zwave_js/services.py b/homeassistant/components/zwave_js/services.py index e5c0bd64781aa2..969a235bb414ea 100644 --- a/homeassistant/components/zwave_js/services.py +++ b/homeassistant/components/zwave_js/services.py @@ -48,6 +48,12 @@ type _NodeOrEndpointType = ZwaveNode | Endpoint +TARGET_VALIDATORS = { + vol.Optional(ATTR_AREA_ID): vol.All(cv.ensure_list, [cv.string]), + vol.Optional(ATTR_DEVICE_ID): vol.All(cv.ensure_list, [cv.string]), + vol.Optional(ATTR_ENTITY_ID): cv.entity_ids, +} + def parameter_name_does_not_need_bitmask( val: dict[str, int | str | list[str]], @@ -261,13 +267,7 @@ def validate_entities(val: dict[str, Any]) -> dict[str, Any]: schema=vol.Schema( vol.All( { - vol.Optional(ATTR_AREA_ID): vol.All( - cv.ensure_list, [cv.string] - ), - vol.Optional(ATTR_DEVICE_ID): vol.All( - cv.ensure_list, [cv.string] - ), - vol.Optional(ATTR_ENTITY_ID): cv.entity_ids, + **TARGET_VALIDATORS, vol.Optional(const.ATTR_ENDPOINT, default=0): vol.Coerce(int), vol.Required(const.ATTR_CONFIG_PARAMETER): vol.Any( vol.Coerce(int), cv.string @@ -305,13 +305,7 @@ def validate_entities(val: dict[str, Any]) -> dict[str, Any]: schema=vol.Schema( vol.All( { - vol.Optional(ATTR_AREA_ID): vol.All( - cv.ensure_list, [cv.string] - ), - vol.Optional(ATTR_DEVICE_ID): vol.All( - cv.ensure_list, [cv.string] - ), - vol.Optional(ATTR_ENTITY_ID): cv.entity_ids, + **TARGET_VALIDATORS, vol.Optional(const.ATTR_ENDPOINT, default=0): vol.Coerce(int), vol.Required(const.ATTR_CONFIG_PARAMETER): vol.Coerce(int), vol.Required(const.ATTR_CONFIG_VALUE): vol.Any( @@ -356,13 +350,7 @@ def validate_entities(val: dict[str, Any]) -> dict[str, Any]: schema=vol.Schema( vol.All( { - vol.Optional(ATTR_AREA_ID): vol.All( - cv.ensure_list, [cv.string] - ), - vol.Optional(ATTR_DEVICE_ID): vol.All( - cv.ensure_list, [cv.string] - ), - vol.Optional(ATTR_ENTITY_ID): cv.entity_ids, + **TARGET_VALIDATORS, vol.Required(const.ATTR_COMMAND_CLASS): vol.Coerce(int), vol.Required(const.ATTR_PROPERTY): vol.Any( vol.Coerce(int), str @@ -391,13 +379,7 @@ def validate_entities(val: dict[str, Any]) -> dict[str, Any]: schema=vol.Schema( vol.All( { - vol.Optional(ATTR_AREA_ID): vol.All( - cv.ensure_list, [cv.string] - ), - vol.Optional(ATTR_DEVICE_ID): vol.All( - cv.ensure_list, [cv.string] - ), - vol.Optional(ATTR_ENTITY_ID): cv.entity_ids, + **TARGET_VALIDATORS, vol.Optional(const.ATTR_BROADCAST, default=False): cv.boolean, vol.Required(const.ATTR_COMMAND_CLASS): vol.Coerce(int), vol.Required(const.ATTR_PROPERTY): vol.Any( @@ -428,15 +410,7 @@ def validate_entities(val: dict[str, Any]) -> dict[str, Any]: self.async_ping, schema=vol.Schema( vol.All( - { - vol.Optional(ATTR_AREA_ID): vol.All( - cv.ensure_list, [cv.string] - ), - vol.Optional(ATTR_DEVICE_ID): vol.All( - cv.ensure_list, [cv.string] - ), - vol.Optional(ATTR_ENTITY_ID): cv.entity_ids, - }, + TARGET_VALIDATORS, cv.has_at_least_one_key( ATTR_DEVICE_ID, ATTR_ENTITY_ID, ATTR_AREA_ID ), @@ -453,13 +427,7 @@ def validate_entities(val: dict[str, Any]) -> dict[str, Any]: schema=vol.Schema( vol.All( { - vol.Optional(ATTR_AREA_ID): vol.All( - cv.ensure_list, [cv.string] - ), - vol.Optional(ATTR_DEVICE_ID): vol.All( - cv.ensure_list, [cv.string] - ), - vol.Optional(ATTR_ENTITY_ID): cv.entity_ids, + **TARGET_VALIDATORS, vol.Required(const.ATTR_COMMAND_CLASS): vol.All( vol.Coerce(int), vol.Coerce(CommandClass) ), @@ -483,13 +451,7 @@ def validate_entities(val: dict[str, Any]) -> dict[str, Any]: schema=vol.Schema( vol.All( { - vol.Optional(ATTR_AREA_ID): vol.All( - cv.ensure_list, [cv.string] - ), - vol.Optional(ATTR_DEVICE_ID): vol.All( - cv.ensure_list, [cv.string] - ), - vol.Optional(ATTR_ENTITY_ID): cv.entity_ids, + **TARGET_VALIDATORS, vol.Required(const.ATTR_NOTIFICATION_TYPE): vol.All( vol.Coerce(int), vol.Coerce(NotificationType) ), diff --git a/homeassistant/components/zwave_js/services.yaml b/homeassistant/components/zwave_js/services.yaml index 81809e3fbeb745..f5063fdfd93834 100644 --- a/homeassistant/components/zwave_js/services.yaml +++ b/homeassistant/components/zwave_js/services.yaml @@ -89,10 +89,28 @@ set_lock_configuration: boolean: set_config_parameter: - target: - entity: - integration: zwave_js fields: + area_id: + example: living_room + selector: + area: + device: + - integration: zwave_js + multiple: true + device_id: + example: "8f4219cfa57e23f6f669c4616c2205e2" + selector: + device: + filter: + - integration: zwave_js + multiple: true + entity_id: + example: sensor.living_room_temperature + selector: + entity: + filter: + - integration: zwave_js + multiple: true endpoint: example: 1 default: 0 @@ -127,10 +145,28 @@ set_config_parameter: max: 3 bulk_set_partial_config_parameters: - target: - entity: - integration: zwave_js fields: + area_id: + example: living_room + selector: + area: + device: + - integration: zwave_js + multiple: true + device_id: + example: "8f4219cfa57e23f6f669c4616c2205e2" + selector: + device: + filter: + - integration: zwave_js + multiple: true + entity_id: + example: sensor.living_room_temperature + selector: + entity: + filter: + - integration: zwave_js + multiple: true endpoint: example: 1 default: 0 @@ -169,10 +205,28 @@ refresh_value: boolean: set_value: - target: - entity: - integration: zwave_js fields: + area_id: + example: living_room + selector: + area: + device: + - integration: zwave_js + multiple: true + device_id: + example: "8f4219cfa57e23f6f669c4616c2205e2" + selector: + device: + filter: + - integration: zwave_js + multiple: true + entity_id: + example: sensor.living_room_temperature + selector: + entity: + filter: + - integration: zwave_js + multiple: true command_class: example: 117 required: true @@ -208,10 +262,28 @@ set_value: boolean: multicast_set_value: - target: - entity: - integration: zwave_js fields: + area_id: + example: living_room + selector: + area: + device: + - integration: zwave_js + multiple: true + device_id: + example: "8f4219cfa57e23f6f669c4616c2205e2" + selector: + device: + filter: + - integration: zwave_js + multiple: true + entity_id: + example: sensor.living_room_temperature + selector: + entity: + filter: + - integration: zwave_js + multiple: true broadcast: example: true required: false @@ -248,16 +320,55 @@ multicast_set_value: object: ping: - target: - entity: - integration: zwave_js + fields: + area_id: + example: living_room + selector: + area: + device: + - integration: zwave_js + multiple: true + device_id: + example: "8f4219cfa57e23f6f669c4616c2205e2" + selector: + device: + filter: + - integration: zwave_js + multiple: true + entity_id: + example: sensor.living_room_temperature + selector: + entity: + filter: + - integration: zwave_js + multiple: true reset_meter: - target: - entity: - domain: sensor - integration: zwave_js fields: + area_id: + example: living_room + selector: + area: + entity: + - integration: zwave_js + domain: sensor + multiple: true + device_id: + example: "8f4219cfa57e23f6f669c4616c2205e2" + selector: + device: + entity: + - integration: zwave_js + domain: sensor + multiple: true + entity_id: + example: sensor.living_room_temperature + selector: + entity: + filter: + - integration: zwave_js + domain: sensor + multiple: true meter_type: example: 1 required: false @@ -270,10 +381,28 @@ reset_meter: text: invoke_cc_api: - target: - entity: - integration: zwave_js fields: + area_id: + example: living_room + selector: + area: + device: + - integration: zwave_js + multiple: true + device_id: + example: "8f4219cfa57e23f6f669c4616c2205e2" + selector: + device: + filter: + - integration: zwave_js + multiple: true + entity_id: + example: sensor.living_room_temperature + selector: + entity: + filter: + - integration: zwave_js + multiple: true command_class: example: 132 required: true @@ -296,10 +425,28 @@ invoke_cc_api: object: refresh_notifications: - target: - entity: - integration: zwave_js fields: + area_id: + example: living_room + selector: + area: + device: + - integration: zwave_js + multiple: true + device_id: + example: "8f4219cfa57e23f6f669c4616c2205e2" + selector: + device: + filter: + - integration: zwave_js + multiple: true + entity_id: + example: sensor.living_room_temperature + selector: + entity: + filter: + - integration: zwave_js + multiple: true notification_type: example: 1 required: true diff --git a/homeassistant/components/zwave_js/strings.json b/homeassistant/components/zwave_js/strings.json index 4bba3e0538cf8b..ca7d5153e6e0ba 100644 --- a/homeassistant/components/zwave_js/strings.json +++ b/homeassistant/components/zwave_js/strings.json @@ -265,10 +265,22 @@ "bulk_set_partial_config_parameters": { "description": "Allows for bulk setting partial parameters. Useful when multiple partial parameters have to be set at the same time.", "fields": { + "area_id": { + "description": "[%key:component::zwave_js::services::set_value::fields::area_id::description%]", + "name": "[%key:component::zwave_js::services::set_value::fields::area_id::name%]" + }, + "device_id": { + "description": "[%key:component::zwave_js::services::set_value::fields::device_id::description%]", + "name": "[%key:component::zwave_js::services::set_value::fields::device_id::name%]" + }, "endpoint": { "description": "[%key:component::zwave_js::services::set_config_parameter::fields::endpoint::description%]", "name": "[%key:component::zwave_js::services::set_config_parameter::fields::endpoint::name%]" }, + "entity_id": { + "description": "[%key:component::zwave_js::services::set_value::fields::entity_id::description%]", + "name": "[%key:component::zwave_js::services::set_value::fields::entity_id::name%]" + }, "parameter": { "description": "[%key:component::zwave_js::services::set_config_parameter::fields::parameter::description%]", "name": "[%key:component::zwave_js::services::set_config_parameter::fields::parameter::name%]" @@ -293,14 +305,26 @@ "invoke_cc_api": { "description": "Calls a Command Class API on a node. Some Command Classes can't be fully controlled via the `set_value` action and require direct calls to the Command Class API.", "fields": { + "area_id": { + "description": "The area(s) to target for this service. If an area is specified, all zwave_js devices and entities in that area will be targeted for this service.", + "name": "Area ID(s)" + }, "command_class": { "description": "The ID of the command class that you want to issue a command to.", "name": "[%key:component::zwave_js::services::set_value::fields::command_class::name%]" }, + "device_id": { + "description": "The device(s) to target for this service.", + "name": "Device ID(s)" + }, "endpoint": { "description": "The endpoint to call the API on. If an endpoint is specified, that endpoint will be targeted for all nodes associated with the target areas, devices, and/or entities. If an endpoint is not specified, the root endpoint (0) will be targeted for nodes associated with target areas and devices, and the endpoint for the primary value of each entity will be targeted.", "name": "[%key:component::zwave_js::services::set_config_parameter::fields::endpoint::name%]" }, + "entity_id": { + "description": "The entity ID(s) to target for this service.", + "name": "Entity ID(s)" + }, "method_name": { "description": "The name of the API method to call. Refer to the Z-Wave JS Command Class API documentation (https://zwave-js.github.io/node-zwave-js/#/api/CCs/index) for available methods.", "name": "Method name" @@ -315,6 +339,10 @@ "multicast_set_value": { "description": "Changes any value that Z-Wave JS recognizes on multiple Z-Wave devices using multicast, so all devices receive the message simultaneously. This action has minimal validation so only use this action if you know what you are doing.", "fields": { + "area_id": { + "description": "[%key:component::zwave_js::services::set_value::fields::area_id::description%]", + "name": "[%key:component::zwave_js::services::set_value::fields::area_id::name%]" + }, "broadcast": { "description": "Whether command should be broadcast to all devices on the network.", "name": "Broadcast?" @@ -323,10 +351,18 @@ "description": "[%key:component::zwave_js::services::set_value::fields::command_class::description%]", "name": "[%key:component::zwave_js::services::set_value::fields::command_class::name%]" }, + "device_id": { + "description": "[%key:component::zwave_js::services::set_value::fields::device_id::description%]", + "name": "[%key:component::zwave_js::services::set_value::fields::device_id::name%]" + }, "endpoint": { "description": "[%key:component::zwave_js::services::set_value::fields::endpoint::description%]", "name": "[%key:component::zwave_js::services::set_config_parameter::fields::endpoint::name%]" }, + "entity_id": { + "description": "[%key:component::zwave_js::services::set_value::fields::entity_id::description%]", + "name": "[%key:component::zwave_js::services::set_value::fields::entity_id::name%]" + }, "options": { "description": "[%key:component::zwave_js::services::set_value::fields::options::description%]", "name": "[%key:component::zwave_js::services::set_value::fields::options::name%]" @@ -348,11 +384,37 @@ }, "ping": { "description": "Forces Z-Wave JS to try to reach a node. This can be used to update the status of the node in Z-Wave JS when you think it doesn't accurately reflect reality, e.g. reviving a failed/dead node or marking the node as asleep.", + "fields": { + "area_id": { + "description": "[%key:component::zwave_js::services::set_value::fields::area_id::description%]", + "name": "[%key:component::zwave_js::services::set_value::fields::area_id::name%]" + }, + "device_id": { + "description": "[%key:component::zwave_js::services::set_value::fields::device_id::description%]", + "name": "[%key:component::zwave_js::services::set_value::fields::device_id::name%]" + }, + "entity_id": { + "description": "[%key:component::zwave_js::services::set_value::fields::entity_id::description%]", + "name": "[%key:component::zwave_js::services::set_value::fields::entity_id::name%]" + } + }, "name": "Ping a node" }, "refresh_notifications": { "description": "Refreshes notifications on a node based on notification type and optionally notification event.", "fields": { + "area_id": { + "description": "[%key:component::zwave_js::services::set_value::fields::area_id::description%]", + "name": "[%key:component::zwave_js::services::set_value::fields::area_id::name%]" + }, + "device_id": { + "description": "[%key:component::zwave_js::services::set_value::fields::device_id::description%]", + "name": "[%key:component::zwave_js::services::set_value::fields::device_id::name%]" + }, + "entity_id": { + "description": "[%key:component::zwave_js::services::set_value::fields::entity_id::description%]", + "name": "[%key:component::zwave_js::services::set_value::fields::entity_id::name%]" + }, "notification_event": { "description": "The Notification Event number as defined in the Z-Wave specs.", "name": "Notification Event" @@ -381,6 +443,18 @@ "reset_meter": { "description": "Resets the meters on a node.", "fields": { + "area_id": { + "description": "[%key:component::zwave_js::services::set_value::fields::area_id::description%]", + "name": "[%key:component::zwave_js::services::set_value::fields::area_id::name%]" + }, + "device_id": { + "description": "[%key:component::zwave_js::services::set_value::fields::device_id::description%]", + "name": "[%key:component::zwave_js::services::set_value::fields::device_id::name%]" + }, + "entity_id": { + "description": "[%key:component::zwave_js::services::set_value::fields::entity_id::description%]", + "name": "[%key:component::zwave_js::services::set_value::fields::entity_id::name%]" + }, "meter_type": { "description": "The type of meter to reset. Not all meters support the ability to pick a meter type to reset.", "name": "Meter type" @@ -395,14 +469,26 @@ "set_config_parameter": { "description": "Changes the configuration parameters of your Z-Wave devices.", "fields": { + "area_id": { + "description": "[%key:component::zwave_js::services::set_value::fields::area_id::description%]", + "name": "[%key:component::zwave_js::services::set_value::fields::area_id::name%]" + }, "bitmask": { "description": "Target a specific bitmask (see the documentation for more information). Cannot be combined with value_size or value_format.", "name": "Bitmask" }, + "device_id": { + "description": "[%key:component::zwave_js::services::set_value::fields::device_id::description%]", + "name": "[%key:component::zwave_js::services::set_value::fields::device_id::name%]" + }, "endpoint": { "description": "The configuration parameter's endpoint.", "name": "Endpoint" }, + "entity_id": { + "description": "[%key:component::zwave_js::services::set_value::fields::entity_id::description%]", + "name": "[%key:component::zwave_js::services::set_value::fields::entity_id::name%]" + }, "parameter": { "description": "The name (or ID) of the configuration parameter you want to configure.", "name": "Parameter" @@ -477,14 +563,26 @@ "set_value": { "description": "Changes any value that Z-Wave JS recognizes on a Z-Wave device. This action has minimal validation so only use this action if you know what you are doing.", "fields": { + "area_id": { + "description": "The area(s) to target for this service. If an area is specified, all zwave_js devices and entities in that area will be targeted for this service.", + "name": "Area ID(s)" + }, "command_class": { "description": "The ID of the command class for the value.", "name": "Command class" }, + "device_id": { + "description": "The device(s) to target for this service.", + "name": "Device ID(s)" + }, "endpoint": { "description": "The endpoint for the value.", "name": "[%key:component::zwave_js::services::set_config_parameter::fields::endpoint::name%]" }, + "entity_id": { + "description": "The entity ID(s) to target for this service.", + "name": "Entity ID(s)" + }, "options": { "description": "Set value options map. Refer to the Z-Wave JS documentation for more information on what options can be set.", "name": "Options" diff --git a/homeassistant/config_entries.py b/homeassistant/config_entries.py index e48313cab334fb..e64d2001efa1ae 100644 --- a/homeassistant/config_entries.py +++ b/homeassistant/config_entries.py @@ -434,26 +434,10 @@ def __repr__(self) -> str: def __setattr__(self, key: str, value: Any) -> None: """Set an attribute.""" if key in UPDATE_ENTRY_CONFIG_ENTRY_ATTRS: - if key == "unique_id": - # Setting unique_id directly will corrupt internal state - # There is no deprecation period for this key - # as changing them will corrupt internal state - # so we raise an error here - raise AttributeError( - "unique_id cannot be changed directly, use async_update_entry instead" - ) - report( - f'sets "{key}" directly to update a config entry. This is deprecated and will' - " stop working in Home Assistant 2024.9, it should be updated to use" - " async_update_entry instead", - error_if_core=False, + raise AttributeError( + f"{key} cannot be changed directly, use async_update_entry instead" ) - - elif key in FROZEN_CONFIG_ENTRY_ATTRS: - # These attributes are frozen and cannot be changed - # There is no deprecation period for these - # as changing them will corrupt internal state - # so we raise an error here + if key in FROZEN_CONFIG_ENTRY_ATTRS: raise AttributeError(f"{key} cannot be changed") super().__setattr__(key, value) @@ -1742,6 +1726,16 @@ def async_entries( and (include_disabled or not entry.disabled_by) ] + @callback + def async_loaded_entries(self, domain: str) -> list[ConfigEntry]: + """Return loaded entries for a specific domain. + + This will exclude ignored or disabled config entruis. + """ + entries = self._entries.get_entries_for_domain(domain) + + return [entry for entry in entries if entry.state == ConfigEntryState.LOADED] + @callback def async_entry_for_domain_unique_id( self, domain: str, unique_id: str diff --git a/homeassistant/const.py b/homeassistant/const.py index e29f22bdf0573f..1525ddab719753 100644 --- a/homeassistant/const.py +++ b/homeassistant/const.py @@ -23,7 +23,7 @@ APPLICATION_NAME: Final = "HomeAssistant" MAJOR_VERSION: Final = 2024 -MINOR_VERSION: Final = 9 +MINOR_VERSION: Final = 10 PATCH_VERSION: Final = "0.dev0" __short_version__: Final = f"{MAJOR_VERSION}.{MINOR_VERSION}" __version__: Final = f"{__short_version__}.{PATCH_VERSION}" @@ -60,7 +60,6 @@ class Platform(StrEnum): LAWN_MOWER = "lawn_mower" LIGHT = "light" LOCK = "lock" - MAILBOX = "mailbox" MEDIA_PLAYER = "media_player" NOTIFY = "notify" NUMBER = "number" diff --git a/homeassistant/generated/amazon_polly.py b/homeassistant/generated/amazon_polly.py new file mode 100644 index 00000000000000..1d870bf6c92f7e --- /dev/null +++ b/homeassistant/generated/amazon_polly.py @@ -0,0 +1,137 @@ +"""Automatically generated file. + +To update, run python3 -m script.amazon_polly +""" + +from __future__ import annotations + +from typing import Final + +SUPPORTED_ENGINES: Final[set[str]] = { + "generative", + "long-form", + "neural", + "standard", +} + +SUPPORTED_REGIONS: Final[set[str]] = { + "af-south-1", + "ap-east-1", + "ap-northeast-1", + "ap-northeast-2", + "ap-northeast-3", + "ap-south-1", + "ap-southeast-1", + "ap-southeast-2", + "ca-central-1", + "eu-central-1", + "eu-north-1", + "eu-west-1", + "eu-west-2", + "eu-west-3", + "me-south-1", + "sa-east-1", + "us-east-1", + "us-east-2", + "us-west-1", + "us-west-2", +} + +SUPPORTED_VOICES: Final[set[str]] = { + "Aditi", + "Adriano", + "Amy", + "Andres", + "Aria", + "Arlet", + "Arthur", + "Astrid", + "Ayanda", + "Bianca", + "Brian", + "Burcu", + "Camila", + "Carla", + "Carmen", + "Celine", + "Chantal", + "Conchita", + "Cristiano", + "Daniel", + "Danielle", + "Dora", + "Elin", + "Emma", + "Enrique", + "Ewa", + "Filiz", + "Gabrielle", + "Geraint", + "Giorgio", + "Gregory", + "Gwyneth", + "Hala", + "Hannah", + "Hans", + "Hiujin", + "Ida", + "Ines", + "Isabelle", + "Ivy", + "Jacek", + "Jan", + "Joanna", + "Joey", + "Justin", + "Kajal", + "Karl", + "Kazuha", + "Kendra", + "Kevin", + "Kimberly", + "Laura", + "Lea", + "Liam", + "Lisa", + "Liv", + "Lotte", + "Lucia", + "Lupe", + "Mads", + "Maja", + "Marlene", + "Mathieu", + "Matthew", + "Maxim", + "Mia", + "Miguel", + "Mizuki", + "Naja", + "Niamh", + "Nicole", + "Ola", + "Olivia", + "Pedro", + "Penelope", + "Raveena", + "Remi", + "Ricardo", + "Ruben", + "Russell", + "Ruth", + "Salli", + "Seoyeon", + "Sergio", + "Sofie", + "Stephen", + "Suvi", + "Takumi", + "Tatyana", + "Thiago", + "Tomoko", + "Vicki", + "Vitoria", + "Zayd", + "Zeina", + "Zhiyu", +} diff --git a/homeassistant/generated/application_credentials.py b/homeassistant/generated/application_credentials.py index dc30f9d76f004b..efb6f426d3643d 100644 --- a/homeassistant/generated/application_credentials.py +++ b/homeassistant/generated/application_credentials.py @@ -10,6 +10,7 @@ "google", "google_assistant_sdk", "google_mail", + "google_photos", "google_sheets", "google_tasks", "home_connect", @@ -29,6 +30,7 @@ "twitch", "withings", "xbox", + "yale", "yolink", "youtube", ] diff --git a/homeassistant/generated/config_flows.py b/homeassistant/generated/config_flows.py index 5e6d29f29f9701..c7c8cd0f9f1cab 100644 --- a/homeassistant/generated/config_flows.py +++ b/homeassistant/generated/config_flows.py @@ -114,6 +114,7 @@ "cpuspeed", "crownstone", "daikin", + "deako", "deconz", "deluge", "denonavr", @@ -156,6 +157,7 @@ "elkm1", "elmax", "elvia", + "emoncms", "emonitor", "emulated_roku", "energenie_power_sockets", @@ -221,8 +223,10 @@ "goodwe", "google", "google_assistant_sdk", + "google_cloud", "google_generative_ai_conversation", "google_mail", + "google_photos", "google_sheets", "google_tasks", "google_translate", @@ -251,6 +255,7 @@ "homewizard", "homeworks", "honeywell", + "html5", "huawei_lte", "hue", "huisbaasje", @@ -280,6 +285,7 @@ "ipp", "iqvia", "iron_os", + "iskra", "islamic_prayer_times", "israel_rail", "iss", @@ -314,8 +320,10 @@ "ld2410_ble", "leaone", "led_ble", + "lektrico", "lg_netcast", "lg_soundbar", + "lg_thinq", "lidarr", "lifx", "linear_garage_door", @@ -594,6 +602,7 @@ "tomorrowio", "toon", "totalconnect", + "touchline_sl", "tplink", "tplink_omada", "traccar", @@ -663,6 +672,7 @@ "xiaomi_aqara", "xiaomi_ble", "xiaomi_miio", + "yale", "yale_smart_alarm", "yalexs_ble", "yamaha_musiccast", diff --git a/homeassistant/generated/dhcp.py b/homeassistant/generated/dhcp.py index f6df799d01ec96..8f5964f1618100 100644 --- a/homeassistant/generated/dhcp.py +++ b/homeassistant/generated/dhcp.py @@ -12,11 +12,6 @@ "domain": "airzone", "macaddress": "E84F25*", }, - { - "domain": "august", - "hostname": "yale-connect-plus", - "macaddress": "00177A*", - }, { "domain": "august", "hostname": "connect", @@ -1094,6 +1089,11 @@ "domain": "wiz", "hostname": "wiz_*", }, + { + "domain": "yale", + "hostname": "yale-connect-plus", + "macaddress": "00177A*", + }, { "domain": "yeelight", "hostname": "yeelink-*", diff --git a/homeassistant/generated/integrations.json b/homeassistant/generated/integrations.json index 52215d232ade23..f6854aeb58dccc 100644 --- a/homeassistant/generated/integrations.json +++ b/homeassistant/generated/integrations.json @@ -1091,6 +1091,13 @@ "config_flow": false, "iot_class": "local_polling" }, + "deako": { + "name": "Deako", + "integration_type": "hub", + "config_flow": true, + "iot_class": "local_polling", + "single_config_entry": true + }, "debugpy": { "name": "Remote Python Debugger", "integration_type": "service", @@ -1562,7 +1569,7 @@ "integrations": { "emoncms": { "integration_type": "hub", - "config_flow": false, + "config_flow": true, "iot_class": "local_polling", "name": "Emoncms" }, @@ -2244,10 +2251,10 @@ "name": "Google Assistant SDK" }, "google_cloud": { - "integration_type": "hub", - "config_flow": false, + "integration_type": "service", + "config_flow": true, "iot_class": "cloud_push", - "name": "Google Cloud Platform" + "name": "Google Cloud" }, "google_domains": { "integration_type": "hub", @@ -2273,6 +2280,12 @@ "iot_class": "cloud_polling", "name": "Google Maps" }, + "google_photos": { + "integration_type": "hub", + "config_flow": true, + "iot_class": "cloud_polling", + "name": "Google Photos" + }, "google_pubsub": { "integration_type": "hub", "config_flow": false, @@ -2620,8 +2633,9 @@ "html5": { "name": "HTML5 Push Notifications", "integration_type": "hub", - "config_flow": false, - "iot_class": "cloud_push" + "config_flow": true, + "iot_class": "cloud_push", + "single_config_entry": true }, "huawei_lte": { "name": "Huawei LTE", @@ -2894,6 +2908,12 @@ "config_flow": true, "iot_class": "local_polling" }, + "iskra": { + "name": "iskra", + "integration_type": "hub", + "config_flow": true, + "iot_class": "local_polling" + }, "islamic_prayer_times": { "integration_type": "hub", "config_flow": true, @@ -3204,6 +3224,12 @@ "integration_type": "virtual", "supported_by": "netatmo" }, + "lektrico": { + "name": "Lektrico Charging Station", + "integration_type": "device", + "config_flow": true, + "iot_class": "local_polling" + }, "leviton": { "name": "Leviton", "iot_standards": [ @@ -3219,6 +3245,12 @@ "iot_class": "local_polling", "name": "LG Netcast" }, + "lg_thinq": { + "integration_type": "hub", + "config_flow": true, + "iot_class": "cloud_push", + "name": "LG ThinQ" + }, "lg_soundbar": { "integration_type": "hub", "config_flow": true, @@ -5134,6 +5166,23 @@ "config_flow": true, "iot_class": "local_push" }, + "roth": { + "name": "Roth", + "integrations": { + "touchline": { + "integration_type": "hub", + "config_flow": false, + "iot_class": "local_polling", + "name": "Roth Touchline" + }, + "touchline_sl": { + "integration_type": "hub", + "config_flow": true, + "iot_class": "cloud_polling", + "name": "Roth Touchline SL" + } + } + }, "rova": { "name": "ROVA", "integration_type": "hub", @@ -6297,12 +6346,6 @@ "config_flow": true, "iot_class": "cloud_polling" }, - "touchline": { - "name": "Roth Touchline", - "integration_type": "hub", - "config_flow": false, - "iot_class": "local_polling" - }, "tplink": { "name": "TP-Link", "integrations": { @@ -6994,8 +7037,14 @@ "yale_home": { "integration_type": "virtual", "config_flow": false, - "supported_by": "august", + "supported_by": "yale", "name": "Yale Home" + }, + "yale": { + "integration_type": "hub", + "config_flow": true, + "iot_class": "cloud_push", + "name": "Yale" } } }, diff --git a/homeassistant/generated/zeroconf.py b/homeassistant/generated/zeroconf.py index 3e5e34090d1272..2e3ffa23ff5e9e 100644 --- a/homeassistant/generated/zeroconf.py +++ b/homeassistant/generated/zeroconf.py @@ -415,6 +415,11 @@ "domain": "forked_daapd", }, ], + "_deako._tcp.local.": [ + { + "domain": "deako", + }, + ], "_devialet-http._tcp.local.": [ { "domain": "devialet", @@ -514,6 +519,10 @@ "domain": "bosch_shc", "name": "bosch shc*", }, + { + "domain": "lektrico", + "name": "lektrico*", + }, { "domain": "loqed", "name": "loqed*", diff --git a/homeassistant/helpers/area_registry.py b/homeassistant/helpers/area_registry.py index 3e101f185edbe0..5009ec654cfcde 100644 --- a/homeassistant/helpers/area_registry.py +++ b/homeassistant/helpers/area_registry.py @@ -153,22 +153,23 @@ def __init__(self) -> None: def _index_entry(self, key: str, entry: AreaEntry) -> None: """Index an entry.""" + super()._index_entry(key, entry) if entry.floor_id is not None: self._floors_index[entry.floor_id][key] = True for label in entry.labels: self._labels_index[label][key] = True - super()._index_entry(key, entry) def _unindex_entry( self, key: str, replacement_entry: AreaEntry | None = None ) -> None: + # always call base class before other indices + super()._unindex_entry(key, replacement_entry) entry = self.data[key] if labels := entry.labels: for label in labels: self._unindex_entry_value(key, label, self._labels_index) if floor_id := entry.floor_id: self._unindex_entry_value(key, floor_id, self._floors_index) - return super()._unindex_entry(key, replacement_entry) def get_areas_for_label(self, label: str) -> list[AreaEntry]: """Get areas for label.""" diff --git a/homeassistant/helpers/collection.py b/homeassistant/helpers/collection.py index 9151a9dfc6b5e5..86d3450c3a012f 100644 --- a/homeassistant/helpers/collection.py +++ b/homeassistant/helpers/collection.py @@ -7,6 +7,7 @@ from collections.abc import Awaitable, Callable, Coroutine, Iterable from dataclasses import dataclass from functools import partial +from hashlib import md5 from itertools import groupby import logging from operator import attrgetter @@ -25,6 +26,7 @@ from . import entity_registry from .entity import Entity from .entity_component import EntityComponent +from .json import json_bytes from .storage import Store from .typing import ConfigType, VolDictType @@ -50,6 +52,7 @@ class CollectionChange: change_type: str item_id: str item: Any + item_hash: str | None = None type ChangeListener = Callable[ @@ -273,7 +276,9 @@ async def async_load(self) -> None: await self.notify_changes( [ - CollectionChange(CHANGE_ADDED, item[CONF_ID], item) + CollectionChange( + CHANGE_ADDED, item[CONF_ID], item, self._hash_item(item) + ) for item in raw_storage["items"] ] ) @@ -313,7 +318,16 @@ async def async_create_item(self, data: dict) -> _ItemT: item = self._create_item(item_id, validated_data) self.data[item_id] = item self._async_schedule_save() - await self.notify_changes([CollectionChange(CHANGE_ADDED, item_id, item)]) + await self.notify_changes( + [ + CollectionChange( + CHANGE_ADDED, + item_id, + item, + self._hash_item(self._serialize_item(item_id, item)), + ) + ] + ) return item async def async_update_item(self, item_id: str, updates: dict) -> _ItemT: @@ -331,7 +345,16 @@ async def async_update_item(self, item_id: str, updates: dict) -> _ItemT: self.data[item_id] = updated self._async_schedule_save() - await self.notify_changes([CollectionChange(CHANGE_UPDATED, item_id, updated)]) + await self.notify_changes( + [ + CollectionChange( + CHANGE_UPDATED, + item_id, + updated, + self._hash_item(self._serialize_item(item_id, updated)), + ) + ] + ) return self.data[item_id] @@ -365,6 +388,10 @@ def _base_data_to_save(self) -> SerializedStorageCollection: def _data_to_save(self) -> _StoreT: """Return JSON-compatible date for storing to file.""" + def _hash_item(self, item: dict) -> str: + """Return a hash of the item.""" + return md5(json_bytes(item)).hexdigest() + class DictStorageCollection(StorageCollection[dict, SerializedStorageCollection]): """A specialized StorageCollection where the items are untyped dicts.""" @@ -464,6 +491,10 @@ async def _remove_entity(self, change_set: CollectionChange) -> None: async def _update_entity(self, change_set: CollectionChange) -> None: if entity := self.entities.get(change_set.item_id): + if change_set.item_hash: + self.ent_reg.async_update_entity_options( + entity.entity_id, "collection", {"hash": change_set.item_hash} + ) await entity.async_update_config(change_set.item) async def _collection_changed(self, change_set: Iterable[CollectionChange]) -> None: diff --git a/homeassistant/helpers/config_validation.py b/homeassistant/helpers/config_validation.py index 823aefd11e2152..059be3026e578f 100644 --- a/homeassistant/helpers/config_validation.py +++ b/homeassistant/helpers/config_validation.py @@ -716,8 +716,19 @@ def template(value: Any | None) -> template_helper.Template: raise vol.Invalid("template value is None") if isinstance(value, (list, dict, template_helper.Template)): raise vol.Invalid("template value should be a string") + if not (hass := _async_get_hass_or_none()): + # pylint: disable-next=import-outside-toplevel + from .frame import report - template_value = template_helper.Template(str(value), _async_get_hass_or_none()) + report( + ( + "validates schema outside the event loop, " + "which will stop working in HA Core 2025.10" + ), + error_if_core=False, + ) + + template_value = template_helper.Template(str(value), hass) try: template_value.ensure_valid() @@ -734,8 +745,19 @@ def dynamic_template(value: Any | None) -> template_helper.Template: raise vol.Invalid("template value should be a string") if not template_helper.is_template_string(str(value)): raise vol.Invalid("template value does not contain a dynamic template") + if not (hass := _async_get_hass_or_none()): + # pylint: disable-next=import-outside-toplevel + from .frame import report - template_value = template_helper.Template(str(value), _async_get_hass_or_none()) + report( + ( + "validates schema outside the event loop, " + "which will stop working in HA Core 2025.10" + ), + error_if_core=False, + ) + + template_value = template_helper.Template(str(value), hass) try: template_value.ensure_valid() @@ -1091,6 +1113,11 @@ def validator(value: dict[_KT, _VT]) -> dict[_KT, _VT]: def custom_serializer(schema: Any) -> Any: + """Serialize additional types for voluptuous_serialize.""" + return _custom_serializer(schema, allow_section=True) + + +def _custom_serializer(schema: Any, *, allow_section: bool) -> Any: """Serialize additional types for voluptuous_serialize.""" from .. import data_entry_flow # pylint: disable=import-outside-toplevel from . import selector # pylint: disable=import-outside-toplevel @@ -1105,10 +1132,15 @@ def custom_serializer(schema: Any) -> Any: return {"type": "boolean"} if isinstance(schema, data_entry_flow.section): + if not allow_section: + raise ValueError("Nesting expandable sections is not supported") return { "type": "expandable", "schema": voluptuous_serialize.convert( - schema.schema, custom_serializer=custom_serializer + schema.schema, + custom_serializer=functools.partial( + _custom_serializer, allow_section=False + ), ), "expanded": not schema.options["collapsed"], } @@ -1306,9 +1338,28 @@ def platform_only_config_schema(domain: str) -> Callable[[dict], dict]: _HAS_ENTITY_SERVICE_FIELD = has_at_least_one_key(*ENTITY_SERVICE_FIELDS) +def is_entity_service_schema(validator: VolSchemaType) -> bool: + """Check if the passed validator is an entity schema validator. + + The validator must be either of: + - A validator returned by cv._make_entity_service_schema + - A validator returned by cv._make_entity_service_schema, wrapped in a vol.Schema + - A validator returned by cv._make_entity_service_schema, wrapped in a vol.All + Nesting is allowed. + """ + if hasattr(validator, "_entity_service_schema"): + return True + if isinstance(validator, (vol.All)): + return any(is_entity_service_schema(val) for val in validator.validators) + if isinstance(validator, (vol.Schema)): + return is_entity_service_schema(validator.schema) + + return False + + def _make_entity_service_schema(schema: dict, extra: int) -> VolSchemaType: """Create an entity service schema.""" - return vol.All( + validator = vol.All( vol.Schema( { # The frontend stores data here. Don't use in core. @@ -1320,6 +1371,8 @@ def _make_entity_service_schema(schema: dict, extra: int) -> VolSchemaType: ), _HAS_ENTITY_SERVICE_FIELD, ) + setattr(validator, "_entity_service_schema", True) + return validator BASE_ENTITY_SCHEMA = _make_entity_service_schema({}, vol.PREVENT_EXTRA) diff --git a/homeassistant/helpers/event.py b/homeassistant/helpers/event.py index 38f461d8d7a9c4..97a85fdde89251 100644 --- a/homeassistant/helpers/event.py +++ b/homeassistant/helpers/event.py @@ -981,6 +981,22 @@ def __init__( self._last_result: dict[Template, bool | str | TemplateError] = {} + for track_template_ in track_templates: + if track_template_.template.hass: + continue + + # pylint: disable-next=import-outside-toplevel + from .frame import report + + report( + ( + "calls async_track_template_result with template without hass, " + "which will stop working in HA Core 2025.10" + ), + error_if_core=False, + ) + track_template_.template.hass = hass + self._rate_limit = KeyedRateLimit(hass) self._info: dict[Template, RenderInfo] = {} self._track_state_changes: _TrackStateChangeFiltered | None = None diff --git a/homeassistant/helpers/frame.py b/homeassistant/helpers/frame.py index 8a30c26886ef36..e8df1cea21b6eb 100644 --- a/homeassistant/helpers/frame.py +++ b/homeassistant/helpers/frame.py @@ -129,15 +129,19 @@ class MissingIntegrationFrame(HomeAssistantError): def report( what: str, - exclude_integrations: set | None = None, + *, + exclude_integrations: set[str] | None = None, error_if_core: bool = True, + error_if_integration: bool = False, level: int = logging.WARNING, log_custom_component_only: bool = False, - error_if_integration: bool = False, ) -> None: """Report incorrect usage. - Async friendly. + If error_if_core is True, raise instead of log if an integration is not found + when unwinding the stack frame. + If error_if_integration is True, raise instead of log if an integration is found + when unwinding the stack frame. """ try: integration_frame = get_integration_frame( diff --git a/homeassistant/helpers/icon.py b/homeassistant/helpers/icon.py index e759719f6673a3..ce8205eb915218 100644 --- a/homeassistant/helpers/icon.py +++ b/homeassistant/helpers/icon.py @@ -7,7 +7,7 @@ from functools import lru_cache import logging import pathlib -from typing import Any +from typing import Any, cast from homeassistant.core import HomeAssistant, callback from homeassistant.loader import Integration, async_get_integrations @@ -21,12 +21,34 @@ _LOGGER = logging.getLogger(__name__) +def convert_shorthand_service_icon( + value: str | dict[str, str | dict[str, str]], +) -> dict[str, str | dict[str, str]]: + """Convert shorthand service icon to dict.""" + if isinstance(value, str): + return {"service": value} + return value + + +def _load_icons_file( + icons_file: pathlib.Path, +) -> dict[str, Any]: + """Load and parse an icons.json file.""" + icons = load_json_object(icons_file) + if "services" not in icons: + return icons + services = cast(dict[str, str | dict[str, str | dict[str, str]]], icons["services"]) + for service, service_icons in services.items(): + services[service] = convert_shorthand_service_icon(service_icons) + return icons + + def _load_icons_files( icons_files: dict[str, pathlib.Path], ) -> dict[str, dict[str, Any]]: """Load and parse icons.json files.""" return { - component: load_json_object(icons_file) + component: _load_icons_file(icons_file) for component, icons_file in icons_files.items() } diff --git a/homeassistant/helpers/service.py b/homeassistant/helpers/service.py index 0551b5289c548d..ac21f1da3fcda8 100644 --- a/homeassistant/helpers/service.py +++ b/homeassistant/helpers/service.py @@ -1264,19 +1264,16 @@ def async_register_entity_service( """ if schema is None or isinstance(schema, dict): schema = cv.make_entity_service_schema(schema) - # Do a sanity check to check this is a valid entity service schema, - # the check could be extended to require All/Any to have sub schema(s) - # with all entity service fields - elif ( - # Don't check All/Any - not isinstance(schema, (vol.All, vol.Any)) - # Don't check All/Any wrapped in schema - and not isinstance(schema.schema, (vol.All, vol.Any)) - and any(key not in schema.schema for key in cv.ENTITY_SERVICE_FIELDS) - ): - raise HomeAssistantError( - "The schema does not include all required keys: " - f"{", ".join(str(key) for key in cv.ENTITY_SERVICE_FIELDS)}" + elif not cv.is_entity_service_schema(schema): + # pylint: disable-next=import-outside-toplevel + from .frame import report + + report( + ( + "registers an entity service with a non entity service schema " + "which will stop working in HA Core 2025.9" + ), + error_if_core=False, ) service_func: str | HassJob[..., Any] diff --git a/homeassistant/helpers/template.py b/homeassistant/helpers/template.py index 7742418c5a7ddc..9f8eb628e63bdf 100644 --- a/homeassistant/helpers/template.py +++ b/homeassistant/helpers/template.py @@ -51,6 +51,7 @@ from homeassistant.core import ( Context, HomeAssistant, + ServiceResponse, State, callback, split_entity_id, @@ -80,6 +81,7 @@ label_registry, location as loc_helper, ) +from .deprecation import deprecated_function from .singleton import singleton from .translation import async_translate_state from .typing import TemplateVarsType @@ -149,6 +151,7 @@ EVAL_CACHE_SIZE = 512 MAX_CUSTOM_TEMPLATE_SIZE = 5 * 1024 * 1024 +MAX_TEMPLATE_OUTPUT = 256 * 1024 # 256KiB CACHED_TEMPLATE_LRU: LRU[State, TemplateState] = LRU(CACHED_TEMPLATE_STATES) CACHED_TEMPLATE_NO_COLLECT_LRU: LRU[State, TemplateState] = LRU(CACHED_TEMPLATE_STATES) @@ -205,15 +208,24 @@ def _async_adjust_lru_sizes(_: Any) -> None: @bind_hass +@deprecated_function( + "automatic setting of Template.hass introduced by HA Core PR #89242", + breaks_in_ha_version="2025.10", +) def attach(hass: HomeAssistant, obj: Any) -> None: + """Recursively attach hass to all template instances in list and dict.""" + return _attach(hass, obj) + + +def _attach(hass: HomeAssistant, obj: Any) -> None: """Recursively attach hass to all template instances in list and dict.""" if isinstance(obj, list): for child in obj: - attach(hass, child) + _attach(hass, child) elif isinstance(obj, collections.abc.Mapping): for child_key, child_value in obj.items(): - attach(hass, child_key) - attach(hass, child_value) + _attach(hass, child_key) + _attach(hass, child_value) elif isinstance(obj, Template): obj.hass = hass @@ -495,10 +507,26 @@ class Template: ) def __init__(self, template: str, hass: HomeAssistant | None = None) -> None: - """Instantiate a template.""" + """Instantiate a template. + + Note: A valid hass instance should always be passed in. The hass parameter + will be non optional in Home Assistant Core 2025.10. + """ + # pylint: disable-next=import-outside-toplevel + from .frame import report + if not isinstance(template, str): raise TypeError("Expected template to be a string") + if not hass: + report( + ( + "creates a template object without passing hass, " + "which will stop working in HA Core 2025.10" + ), + error_if_core=False, + ) + self.template: str = template.strip() self._compiled_code: CodeType | None = None self._compiled: jinja2.Template | None = None @@ -604,6 +632,11 @@ def async_render( except Exception as err: raise TemplateError(err) from err + if len(render_result) > MAX_TEMPLATE_OUTPUT: + raise TemplateError( + f"Template output exceeded maximum size of {MAX_TEMPLATE_OUTPUT} characters" + ) + render_result = render_result.strip() if not parse_result or self.hass and self.hass.config.legacy_templates: @@ -2112,6 +2145,62 @@ def as_timedelta(value: str) -> timedelta | None: return dt_util.parse_duration(value) +def merge_response(value: ServiceResponse) -> list[Any]: + """Merge action responses into single list. + + Checks that the input is a correct service response: + { + "entity_id": {str: dict[str, Any]}, + } + If response is a single list, it will extend the list with the items + and add the entity_id and value_key to each dictionary for reference. + If response is a dictionary or multiple lists, + it will append the dictionary/lists to the list + and add the entity_id to each dictionary for reference. + """ + if not isinstance(value, dict): + raise TypeError("Response is not a dictionary") + if not value: + # Bail out early if response is an empty dictionary + return [] + + is_single_list = False + response_items: list = [] + for entity_id, entity_response in value.items(): # pylint: disable=too-many-nested-blocks + if not isinstance(entity_response, dict): + raise TypeError("Response is not a dictionary") + for value_key, type_response in entity_response.items(): + if len(entity_response) == 1 and isinstance(type_response, list): + # Provides special handling for responses such as calendar events + # and weather forecasts where the response contains a single list with multiple + # dictionaries inside. + is_single_list = True + for dict_in_list in type_response: + if isinstance(dict_in_list, dict): + if ATTR_ENTITY_ID in dict_in_list: + raise ValueError( + f"Response dictionary already contains key '{ATTR_ENTITY_ID}'" + ) + dict_in_list[ATTR_ENTITY_ID] = entity_id + dict_in_list["value_key"] = value_key + response_items.extend(type_response) + else: + # Break the loop if not a single list as the logic is then managed in the outer loop + # which handles both dictionaries and in the case of multiple lists. + break + + if not is_single_list: + _response = entity_response.copy() + if ATTR_ENTITY_ID in _response: + raise ValueError( + f"Response dictionary already contains key '{ATTR_ENTITY_ID}'" + ) + _response[ATTR_ENTITY_ID] = entity_id + response_items.append(_response) + + return response_items + + def strptime(string, fmt, default=_SENTINEL): """Parse a time string to datetime.""" try: @@ -2827,6 +2916,7 @@ def __init__( self.globals["as_timedelta"] = as_timedelta self.globals["as_timestamp"] = forgiving_as_timestamp self.globals["timedelta"] = timedelta + self.globals["merge_response"] = merge_response self.globals["strptime"] = strptime self.globals["urlencode"] = urlencode self.globals["average"] = average @@ -2844,6 +2934,7 @@ def __init__( self.globals["iif"] = iif self.globals["bool"] = forgiving_boolean self.globals["version"] = version + self.globals["zip"] = zip self.tests["is_number"] = is_number self.tests["list"] = _is_list self.tests["set"] = _is_set diff --git a/homeassistant/package_constraints.txt b/homeassistant/package_constraints.txt index 31100828978c61..e489006867f41d 100644 --- a/homeassistant/package_constraints.txt +++ b/homeassistant/package_constraints.txt @@ -18,21 +18,21 @@ bleak-retry-connector==3.5.0 bleak==0.22.2 bluetooth-adapters==0.19.4 bluetooth-auto-recovery==1.4.2 -bluetooth-data-tools==1.19.4 -cached_ipaddress==0.3.0 +bluetooth-data-tools==1.20.0 +cached-ipaddress==0.5.0 certifi>=2021.5.30 ciso8601==2.3.1 cryptography==43.0.0 -dbus-fast==2.22.1 -fnv-hash-fast==0.5.0 +dbus-fast==2.24.0 +fnv-hash-fast==1.0.2 ha-av==10.1.1 ha-ffmpeg==3.2.0 -habluetooth==3.3.2 +habluetooth==3.4.0 hass-nabucasa==0.81.1 hassil==1.7.4 home-assistant-bluetooth==1.12.2 -home-assistant-frontend==20240809.0 -home-assistant-intents==2024.8.7 +home-assistant-frontend==20240904.0 +home-assistant-intents==2024.9.4 httpx==0.27.0 ifaddr==0.2.0 Jinja2==3.1.4 @@ -49,20 +49,21 @@ pymicro-vad==1.0.1 PyNaCl==1.5.0 pyOpenSSL==24.2.1 pyserial==3.5 +pyspeex-noise==1.0.2 python-slugify==8.0.4 -PyTurboJPEG==1.7.1 +PyTurboJPEG==1.7.5 pyudev==0.24.1 PyYAML==6.0.2 requests==2.32.3 SQLAlchemy==2.0.31 typing-extensions>=4.12.2,<5.0 -ulid-transform==0.13.1 +ulid-transform==1.0.2 urllib3>=1.26.5,<2 voluptuous-openapi==0.0.5 voluptuous-serialize==2.6.0 voluptuous==0.15.2 -yarl==1.9.4 -zeroconf==0.132.2 +yarl==1.9.9 +zeroconf==0.133.0 # Constrain pycryptodome to avoid vulnerability # see https://github.com/home-assistant/core/pull/16238 @@ -108,12 +109,6 @@ hyperframe>=5.2.0 # Ensure we run compatible with musllinux build env numpy==1.26.0 -# Prevent dependency conflicts between sisyphus-control and aioambient -# until upper bounds for sisyphus-control have been updated -# https://github.com/jkeljo/sisyphus-control/issues/6 -python-engineio>=3.13.1,<4.0 -python-socketio>=4.6.0,<5.0 - # Constrain multidict to avoid typing issues # https://github.com/home-assistant/core/pull/67046 multidict>=6.0.2 diff --git a/homeassistant/runner.py b/homeassistant/runner.py index 4bac12ec399a50..102dbafe147f9b 100644 --- a/homeassistant/runner.py +++ b/homeassistant/runner.py @@ -175,7 +175,7 @@ def _enable_posix_spawn() -> None: # less efficient. This is a workaround to force posix_spawn() # when using musl since cpython is not aware its supported. tag = next(packaging.tags.sys_tags()) - subprocess._USE_POSIX_SPAWN = "musllinux" in tag.platform # noqa: SLF001 + subprocess._USE_POSIX_SPAWN = "musllinux" in tag.platform # type: ignore[misc] # noqa: SLF001 def run(runtime_config: RuntimeConfig) -> int: diff --git a/homeassistant/util/executor.py b/homeassistant/util/executor.py index 47b6d08a197cfa..5f0fdd5c273fe0 100644 --- a/homeassistant/util/executor.py +++ b/homeassistant/util/executor.py @@ -63,10 +63,18 @@ def join_or_interrupt_threads( class InterruptibleThreadPoolExecutor(ThreadPoolExecutor): """A ThreadPoolExecutor instance that will not deadlock on shutdown.""" - def shutdown(self, *args: Any, **kwargs: Any) -> None: - """Shutdown with interrupt support added.""" + def shutdown( + self, *args: Any, join_threads_or_timeout: bool = True, **kwargs: Any + ) -> None: + """Shutdown with interrupt support added. + + By default shutdown will wait for threads to finish up + to the timeout before forcefully stopping them. This can + be disabled by setting `join_threads_or_timeout` to False. + """ super().shutdown(wait=False, cancel_futures=True) - self.join_threads_or_timeout() + if join_threads_or_timeout: + self.join_threads_or_timeout() def join_threads_or_timeout(self) -> None: """Join threads or timeout.""" diff --git a/homeassistant/util/frozen_dataclass_compat.py b/homeassistant/util/frozen_dataclass_compat.py index 6184e4564ebcd8..81ce9961a0baba 100644 --- a/homeassistant/util/frozen_dataclass_compat.py +++ b/homeassistant/util/frozen_dataclass_compat.py @@ -8,7 +8,10 @@ import dataclasses import sys -from typing import Any, dataclass_transform +from typing import TYPE_CHECKING, Any, cast, dataclass_transform + +if TYPE_CHECKING: + from _typeshed import DataclassInstance def _class_fields(cls: type, kw_only: bool) -> list[tuple[str, Any, Any]]: @@ -111,6 +114,8 @@ def __new__(*args: Any, **kwargs: Any) -> object: """ cls, *_args = args if dataclasses.is_dataclass(cls): + if TYPE_CHECKING: + cls = cast(type[DataclassInstance], cls) return object.__new__(cls) return cls._dataclass(*_args, **kwargs) diff --git a/homeassistant/util/unit_conversion.py b/homeassistant/util/unit_conversion.py index 2b9f73afab7e1f..d5586704fc5388 100644 --- a/homeassistant/util/unit_conversion.py +++ b/homeassistant/util/unit_conversion.py @@ -68,7 +68,6 @@ class BaseUnitConverter: """Define the format of a conversion utility.""" UNIT_CLASS: str - NORMALIZED_UNIT: str | None VALID_UNITS: set[str | None] _UNIT_CONVERSION: dict[str | None, float] @@ -125,7 +124,6 @@ class DataRateConverter(BaseUnitConverter): """Utility to convert data rate values.""" UNIT_CLASS = "data_rate" - NORMALIZED_UNIT = UnitOfDataRate.BITS_PER_SECOND # Units in terms of bits _UNIT_CONVERSION: dict[str | None, float] = { UnitOfDataRate.BITS_PER_SECOND: 1, @@ -147,7 +145,6 @@ class DistanceConverter(BaseUnitConverter): """Utility to convert distance values.""" UNIT_CLASS = "distance" - NORMALIZED_UNIT = UnitOfLength.METERS _UNIT_CONVERSION: dict[str | None, float] = { UnitOfLength.METERS: 1, UnitOfLength.MILLIMETERS: 1 / _MM_TO_M, @@ -174,7 +171,6 @@ class ConductivityConverter(BaseUnitConverter): """Utility to convert electric current values.""" UNIT_CLASS = "conductivity" - NORMALIZED_UNIT = UnitOfConductivity.MICROSIEMENS _UNIT_CONVERSION: dict[str | None, float] = { UnitOfConductivity.MICROSIEMENS: 1, UnitOfConductivity.MILLISIEMENS: 1e-3, @@ -187,7 +183,6 @@ class ElectricCurrentConverter(BaseUnitConverter): """Utility to convert electric current values.""" UNIT_CLASS = "electric_current" - NORMALIZED_UNIT = UnitOfElectricCurrent.AMPERE _UNIT_CONVERSION: dict[str | None, float] = { UnitOfElectricCurrent.AMPERE: 1, UnitOfElectricCurrent.MILLIAMPERE: 1e3, @@ -199,7 +194,6 @@ class ElectricPotentialConverter(BaseUnitConverter): """Utility to convert electric potential values.""" UNIT_CLASS = "voltage" - NORMALIZED_UNIT = UnitOfElectricPotential.VOLT _UNIT_CONVERSION: dict[str | None, float] = { UnitOfElectricPotential.VOLT: 1, UnitOfElectricPotential.MILLIVOLT: 1e3, @@ -214,7 +208,6 @@ class EnergyConverter(BaseUnitConverter): """Utility to convert energy values.""" UNIT_CLASS = "energy" - NORMALIZED_UNIT = UnitOfEnergy.KILO_WATT_HOUR _UNIT_CONVERSION: dict[str | None, float] = { UnitOfEnergy.WATT_HOUR: 1 * 1000, UnitOfEnergy.KILO_WATT_HOUR: 1, @@ -235,7 +228,6 @@ class InformationConverter(BaseUnitConverter): """Utility to convert information values.""" UNIT_CLASS = "information" - NORMALIZED_UNIT = UnitOfInformation.BITS # Units in terms of bits _UNIT_CONVERSION: dict[str | None, float] = { UnitOfInformation.BITS: 1, @@ -267,7 +259,6 @@ class MassConverter(BaseUnitConverter): """Utility to convert mass values.""" UNIT_CLASS = "mass" - NORMALIZED_UNIT = UnitOfMass.GRAMS _UNIT_CONVERSION: dict[str | None, float] = { UnitOfMass.MICROGRAMS: 1 * 1000 * 1000, UnitOfMass.MILLIGRAMS: 1 * 1000, @@ -292,7 +283,6 @@ class PowerConverter(BaseUnitConverter): """Utility to convert power values.""" UNIT_CLASS = "power" - NORMALIZED_UNIT = UnitOfPower.WATT _UNIT_CONVERSION: dict[str | None, float] = { UnitOfPower.WATT: 1, UnitOfPower.KILO_WATT: 1 / 1000, @@ -307,7 +297,6 @@ class PressureConverter(BaseUnitConverter): """Utility to convert pressure values.""" UNIT_CLASS = "pressure" - NORMALIZED_UNIT = UnitOfPressure.PA _UNIT_CONVERSION: dict[str | None, float] = { UnitOfPressure.PA: 1, UnitOfPressure.HPA: 1 / 100, @@ -338,7 +327,6 @@ class SpeedConverter(BaseUnitConverter): """Utility to convert speed values.""" UNIT_CLASS = "speed" - NORMALIZED_UNIT = UnitOfSpeed.METERS_PER_SECOND _UNIT_CONVERSION: dict[str | None, float] = { UnitOfVolumetricFlux.INCHES_PER_DAY: _DAYS_TO_SECS / _IN_TO_M, UnitOfVolumetricFlux.INCHES_PER_HOUR: _HRS_TO_SECS / _IN_TO_M, @@ -433,7 +421,6 @@ class TemperatureConverter(BaseUnitConverter): """Utility to convert temperature values.""" UNIT_CLASS = "temperature" - NORMALIZED_UNIT = UnitOfTemperature.CELSIUS VALID_UNITS = { UnitOfTemperature.CELSIUS, UnitOfTemperature.FAHRENHEIT, @@ -564,7 +551,6 @@ class UnitlessRatioConverter(BaseUnitConverter): """Utility to convert unitless ratios.""" UNIT_CLASS = "unitless" - NORMALIZED_UNIT = None _UNIT_CONVERSION: dict[str | None, float] = { None: 1, CONCENTRATION_PARTS_PER_BILLION: 1000000000, @@ -581,7 +567,6 @@ class VolumeConverter(BaseUnitConverter): """Utility to convert volume values.""" UNIT_CLASS = "volume" - NORMALIZED_UNIT = UnitOfVolume.CUBIC_METERS # Units in terms of m³ _UNIT_CONVERSION: dict[str | None, float] = { UnitOfVolume.LITERS: 1 / _L_TO_CUBIC_METER, @@ -607,7 +592,6 @@ class VolumeFlowRateConverter(BaseUnitConverter): """Utility to convert volume values.""" UNIT_CLASS = "volume_flow_rate" - NORMALIZED_UNIT = UnitOfVolumeFlowRate.CUBIC_METERS_PER_HOUR # Units in terms of m³/h _UNIT_CONVERSION: dict[str | None, float] = { UnitOfVolumeFlowRate.CUBIC_METERS_PER_HOUR: 1, @@ -630,7 +614,6 @@ class DurationConverter(BaseUnitConverter): """Utility to convert duration values.""" UNIT_CLASS = "duration" - NORMALIZED_UNIT = UnitOfTime.SECONDS _UNIT_CONVERSION: dict[str | None, float] = { UnitOfTime.MICROSECONDS: 1000000, UnitOfTime.MILLISECONDS: 1000, diff --git a/homeassistant/util/yaml/loader.py b/homeassistant/util/yaml/loader.py index ff9b7cb3601932..31efced60f6419 100644 --- a/homeassistant/util/yaml/loader.py +++ b/homeassistant/util/yaml/loader.py @@ -221,13 +221,21 @@ def __report_deprecated() -> None: def load_yaml( fname: str | os.PathLike[str], secrets: Secrets | None = None ) -> JSON_TYPE | None: - """Load a YAML file.""" + """Load a YAML file. + + If opening the file raises an OSError it will be wrapped in a HomeAssistantError, + except for FileNotFoundError which will be re-raised. + """ try: with open(fname, encoding="utf-8") as conf_file: return parse_yaml(conf_file, secrets) except UnicodeDecodeError as exc: _LOGGER.error("Unable to read file %s: %s", fname, exc) raise HomeAssistantError(exc) from exc + except FileNotFoundError: + raise + except OSError as exc: + raise HomeAssistantError(exc) from exc def load_yaml_dict( @@ -348,6 +356,20 @@ def _add_reference_to_node_class( return obj +def _raise_if_no_value[NodeT: yaml.nodes.Node, _R]( + func: Callable[[LoaderType, NodeT], _R], +) -> Callable[[LoaderType, NodeT], _R]: + def wrapper(loader: LoaderType, node: NodeT) -> _R: + if not node.value: + raise HomeAssistantError( + f"{node.start_mark}: {node.tag} needs an argument." + ) + return func(loader, node) + + return wrapper + + +@_raise_if_no_value def _include_yaml(loader: LoaderType, node: yaml.nodes.Node) -> JSON_TYPE: """Load another YAML file and embed it using the !include tag. @@ -363,7 +385,7 @@ def _include_yaml(loader: LoaderType, node: yaml.nodes.Node) -> JSON_TYPE: return _add_reference(loaded_yaml, loader, node) except FileNotFoundError as exc: raise HomeAssistantError( - f"{node.start_mark}: Unable to read file {fname}." + f"{node.start_mark}: Unable to read file {fname}" ) from exc @@ -382,6 +404,7 @@ def _find_files(directory: str, pattern: str) -> Iterator[str]: yield filename +@_raise_if_no_value def _include_dir_named_yaml(loader: LoaderType, node: yaml.nodes.Node) -> NodeDictClass: """Load multiple files from directory as a dictionary.""" mapping = NodeDictClass() @@ -399,6 +422,7 @@ def _include_dir_named_yaml(loader: LoaderType, node: yaml.nodes.Node) -> NodeDi return _add_reference_to_node_class(mapping, loader, node) +@_raise_if_no_value def _include_dir_merge_named_yaml( loader: LoaderType, node: yaml.nodes.Node ) -> NodeDictClass: @@ -414,6 +438,7 @@ def _include_dir_merge_named_yaml( return _add_reference_to_node_class(mapping, loader, node) +@_raise_if_no_value def _include_dir_list_yaml( loader: LoaderType, node: yaml.nodes.Node ) -> list[JSON_TYPE]: @@ -427,6 +452,7 @@ def _include_dir_list_yaml( ] +@_raise_if_no_value def _include_dir_merge_list_yaml( loader: LoaderType, node: yaml.nodes.Node ) -> JSON_TYPE: diff --git a/mypy.ini b/mypy.ini index 2a361f56397502..b352d2747be362 100644 --- a/mypy.ini +++ b/mypy.ini @@ -855,6 +855,16 @@ disallow_untyped_defs = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.bluesound.*] +check_untyped_defs = true +disallow_incomplete_defs = true +disallow_subclassing_any = true +disallow_untyped_calls = true +disallow_untyped_decorators = true +disallow_untyped_defs = true +warn_return_any = true +warn_unreachable = true + [mypy-homeassistant.components.bluetooth.*] check_untyped_defs = true disallow_incomplete_defs = true @@ -1145,6 +1155,16 @@ disallow_untyped_defs = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.deako.*] +check_untyped_defs = true +disallow_incomplete_defs = true +disallow_subclassing_any = true +disallow_untyped_calls = true +disallow_untyped_decorators = true +disallow_untyped_defs = true +warn_return_any = true +warn_unreachable = true + [mypy-homeassistant.components.deconz.*] check_untyped_defs = true disallow_incomplete_defs = true @@ -1836,6 +1856,26 @@ disallow_untyped_defs = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.google_cloud.*] +check_untyped_defs = true +disallow_incomplete_defs = true +disallow_subclassing_any = true +disallow_untyped_calls = true +disallow_untyped_decorators = true +disallow_untyped_defs = true +warn_return_any = true +warn_unreachable = true + +[mypy-homeassistant.components.google_photos.*] +check_untyped_defs = true +disallow_incomplete_defs = true +disallow_subclassing_any = true +disallow_untyped_calls = true +disallow_untyped_decorators = true +disallow_untyped_defs = true +warn_return_any = true +warn_unreachable = true + [mypy-homeassistant.components.google_sheets.*] check_untyped_defs = true disallow_incomplete_defs = true @@ -2536,6 +2576,16 @@ disallow_untyped_defs = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.lektrico.*] +check_untyped_defs = true +disallow_incomplete_defs = true +disallow_subclassing_any = true +disallow_untyped_calls = true +disallow_untyped_decorators = true +disallow_untyped_defs = true +warn_return_any = true +warn_unreachable = true + [mypy-homeassistant.components.lidarr.*] check_untyped_defs = true disallow_incomplete_defs = true @@ -2696,16 +2746,6 @@ disallow_untyped_defs = true warn_return_any = true warn_unreachable = true -[mypy-homeassistant.components.mailbox.*] -check_untyped_defs = true -disallow_incomplete_defs = true -disallow_subclassing_any = true -disallow_untyped_calls = true -disallow_untyped_decorators = true -disallow_untyped_defs = true -warn_return_any = true -warn_unreachable = true - [mypy-homeassistant.components.manual.*] check_untyped_defs = true disallow_incomplete_defs = true @@ -3126,6 +3166,16 @@ disallow_untyped_defs = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.onkyo.*] +check_untyped_defs = true +disallow_incomplete_defs = true +disallow_subclassing_any = true +disallow_untyped_calls = true +disallow_untyped_decorators = true +disallow_untyped_defs = true +warn_return_any = true +warn_unreachable = true + [mypy-homeassistant.components.open_meteo.*] check_untyped_defs = true disallow_incomplete_defs = true @@ -3836,6 +3886,16 @@ disallow_untyped_defs = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.solarlog.*] +check_untyped_defs = true +disallow_incomplete_defs = true +disallow_subclassing_any = true +disallow_untyped_calls = true +disallow_untyped_decorators = true +disallow_untyped_defs = true +warn_return_any = true +warn_unreachable = true + [mypy-homeassistant.components.sonarr.*] check_untyped_defs = true disallow_incomplete_defs = true @@ -3866,6 +3926,16 @@ disallow_untyped_defs = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.squeezebox.*] +check_untyped_defs = true +disallow_incomplete_defs = true +disallow_subclassing_any = true +disallow_untyped_calls = true +disallow_untyped_decorators = true +disallow_untyped_defs = true +warn_return_any = true +warn_unreachable = true + [mypy-homeassistant.components.ssdp.*] check_untyped_defs = true disallow_incomplete_defs = true diff --git a/pylint/plugins/hass_enforce_type_hints.py b/pylint/plugins/hass_enforce_type_hints.py index e1812de44d38a8..13499134668940 100644 --- a/pylint/plugins/hass_enforce_type_hints.py +++ b/pylint/plugins/hass_enforce_type_hints.py @@ -1761,39 +1761,6 @@ class ClassTypeHintMatch: ], ), ], - "mailbox": [ - ClassTypeHintMatch( - base_class="Mailbox", - matches=[ - TypeHintMatch( - function_name="media_type", - return_type="str", - ), - TypeHintMatch( - function_name="can_delete", - return_type="bool", - ), - TypeHintMatch( - function_name="has_media", - return_type="bool", - ), - TypeHintMatch( - function_name="async_get_media", - arg_types={1: "str"}, - return_type="bytes", - ), - TypeHintMatch( - function_name="async_get_messages", - return_type="list[dict[str, Any]]", - ), - TypeHintMatch( - function_name="async_delete", - arg_types={1: "str"}, - return_type="bool", - ), - ], - ), - ], "media_player": [ ClassTypeHintMatch( base_class="Entity", diff --git a/pyproject.toml b/pyproject.toml index c9b0ed43e5104c..e2d5e213811d11 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "homeassistant" -version = "2024.9.0.dev0" +version = "2024.10.0.dev0" license = {text = "Apache-2.0"} description = "Open-source home automation platform running on Python 3." readme = "README.rst" @@ -36,7 +36,7 @@ dependencies = [ "bcrypt==4.1.3", "certifi>=2021.5.30", "ciso8601==2.3.1", - "fnv-hash-fast==0.5.0", + "fnv-hash-fast==1.0.2", # hass-nabucasa is imported by helpers which don't depend on the cloud # integration "hass-nabucasa==0.81.1", @@ -61,7 +61,7 @@ dependencies = [ "requests==2.32.3", "SQLAlchemy==2.0.31", "typing-extensions>=4.12.2,<5.0", - "ulid-transform==0.13.1", + "ulid-transform==1.0.2", # Constrain urllib3 to ensure we deal with CVE-2020-26137 and CVE-2021-33503 # Temporary setting an upper bound, to prevent compat issues with urllib3>=2 # https://github.com/home-assistant/core/issues/97248 @@ -69,7 +69,7 @@ dependencies = [ "voluptuous==0.15.2", "voluptuous-serialize==2.6.0", "voluptuous-openapi==0.0.5", - "yarl==1.9.4", + "yarl==1.9.9", ] [project.urls] @@ -768,6 +768,7 @@ select = [ "SLOT", # flake8-slots "T100", # Trace found: {name} used "T20", # flake8-print + "TCH", # flake8-type-checking "TID251", # Banned imports "TRY", # tryceratops "UP", # pyupgrade @@ -800,6 +801,12 @@ ignore = [ "SIM103", # Return the condition {condition} directly "SIM108", # Use ternary operator {contents} instead of if-else-block "SIM115", # Use context handler for opening files + + # Moving imports into type-checking blocks can mess with pytest.patch() + "TCH001", # Move application import {} into a type-checking block + "TCH002", # Move third-party import {} into a type-checking block + "TCH003", # Move standard library import {} into a type-checking block + "TRY003", # Avoid specifying long messages outside the exception class "TRY400", # Use `logging.exception` instead of `logging.error` # Ignored due to performance: https://github.com/charliermarsh/ruff/issues/2923 @@ -844,7 +851,6 @@ voluptuous = "vol" "homeassistant.components.lawn_mower.PLATFORM_SCHEMA" = "LAWN_MOWER_PLATFORM_SCHEMA" "homeassistant.components.light.PLATFORM_SCHEMA" = "LIGHT_PLATFORM_SCHEMA" "homeassistant.components.lock.PLATFORM_SCHEMA" = "LOCK_PLATFORM_SCHEMA" -"homeassistant.components.mailbox.PLATFORM_SCHEMA" = "MAILBOX_PLATFORM_SCHEMA" "homeassistant.components.media_player.PLATFORM_SCHEMA" = "MEDIA_PLAYER_PLATFORM_SCHEMA" "homeassistant.components.notify.PLATFORM_SCHEMA" = "NOTIFY_PLATFORM_SCHEMA" "homeassistant.components.number.PLATFORM_SCHEMA" = "NUMBER_PLATFORM_SCHEMA" diff --git a/requirements.txt b/requirements.txt index 35290a5ff17458..1d6b4e74d2270c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,7 +16,7 @@ awesomeversion==24.6.0 bcrypt==4.1.3 certifi>=2021.5.30 ciso8601==2.3.1 -fnv-hash-fast==0.5.0 +fnv-hash-fast==1.0.2 hass-nabucasa==0.81.1 httpx==0.27.0 home-assistant-bluetooth==1.12.2 @@ -36,9 +36,9 @@ PyYAML==6.0.2 requests==2.32.3 SQLAlchemy==2.0.31 typing-extensions>=4.12.2,<5.0 -ulid-transform==0.13.1 +ulid-transform==1.0.2 urllib3>=1.26.5,<2 voluptuous==0.15.2 voluptuous-serialize==2.6.0 voluptuous-openapi==0.0.5 -yarl==1.9.4 +yarl==1.9.9 diff --git a/requirements_all.txt b/requirements_all.txt index a7bee65a8b1bcd..e95011f247bab5 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -36,7 +36,7 @@ Mastodon.py==1.8.1 Pillow==10.4.0 # homeassistant.components.plex -PlexAPI==4.15.14 +PlexAPI==4.15.16 # homeassistant.components.progettihwsw ProgettiHWSW==0.1.3 @@ -64,7 +64,7 @@ PyMetEireann==2021.8.0 # homeassistant.components.met # homeassistant.components.norway_air -PyMetno==0.12.0 +PyMetno==0.13.0 # homeassistant.components.keymitt_ble PyMicroBot==0.0.17 @@ -84,7 +84,7 @@ PyQRCode==1.2.1 PyRMVtransport==0.3.3 # homeassistant.components.switchbot -PySwitchbot==0.48.1 +PySwitchbot==0.48.2 # homeassistant.components.switchmate PySwitchmate==0.5.1 @@ -97,10 +97,10 @@ PyTransportNSW==0.1.1 # homeassistant.components.camera # homeassistant.components.stream -PyTurboJPEG==1.7.1 +PyTurboJPEG==1.7.5 # homeassistant.components.vicare -PyViCare-neo==0.2.1 +PyViCare-neo==0.3.0 # homeassistant.components.xiaomi_aqara PyXiaomiGateway==0.14.3 @@ -170,20 +170,20 @@ aio-geojson-nsw-rfs-incidents==0.7 aio-geojson-usgs-earthquakes==0.3 # homeassistant.components.gdacs -aio-georss-gdacs==0.9 +aio-georss-gdacs==0.10 # homeassistant.components.airq aioairq==0.3.2 # homeassistant.components.airzone_cloud -aioairzone-cloud==0.6.2 +aioairzone-cloud==0.6.5 # homeassistant.components.airzone aioairzone==0.8.2 # homeassistant.components.ambient_network # homeassistant.components.ambient_station -aioambient==2024.01.0 +aioambient==2024.08.0 # homeassistant.components.apcupsd aioapcaccess==0.4.2 @@ -201,7 +201,7 @@ aioasuswrt==1.4.0 aioautomower==2024.8.0 # homeassistant.components.azure_devops -aioazuredevops==2.1.1 +aioazuredevops==2.2.1 # homeassistant.components.baf aiobafi6==0.9.0 @@ -237,7 +237,7 @@ aioelectricitymaps==0.4.0 aioemonitor==1.0.5 # homeassistant.components.esphome -aioesphomeapi==25.1.0 +aioesphomeapi==25.3.2 # homeassistant.components.flo aioflo==2021.11.0 @@ -258,7 +258,7 @@ aioharmony==0.2.10 aiohomekit==3.2.3 # homeassistant.components.hue -aiohue==4.7.2 +aiohue==4.7.3 # homeassistant.components.imap aioimaplib==1.1.0 @@ -276,7 +276,7 @@ aiolifx-effects==0.3.2 aiolifx-themes==0.5.0 # homeassistant.components.lifx -aiolifx==1.0.6 +aiolifx==1.0.9 # homeassistant.components.livisi aiolivisi==0.0.19 @@ -288,7 +288,7 @@ aiolookin==1.0.0 aiolyric==2.0.1 # homeassistant.components.mealie -aiomealie==0.8.1 +aiomealie==0.9.2 # homeassistant.components.modern_forms aiomodernforms==0.1.8 @@ -318,7 +318,7 @@ aiooui==0.1.6 aiopegelonline==0.0.10 # homeassistant.components.acmeda -aiopulse==0.4.4 +aiopulse==0.4.6 # homeassistant.components.purpleair aiopurpleair==2022.12.1 @@ -347,7 +347,7 @@ aiorecollect==2023.09.0 aioridwell==2024.01.0 # homeassistant.components.ruckus_unleashed -aioruckus==0.34 +aioruckus==0.41 # homeassistant.components.russound_rio aiorussound==2.3.2 @@ -359,7 +359,7 @@ aioruuvigateway==0.1.0 aiosenz==1.0.0 # homeassistant.components.shelly -aioshelly==11.2.4 +aioshelly==11.4.2 # homeassistant.components.skybell aioskybell==22.7.0 @@ -416,7 +416,7 @@ airgradient==0.8.0 airly==1.1.0 # homeassistant.components.airthings_ble -airthings-ble==0.9.0 +airthings-ble==0.9.1 # homeassistant.components.airthings airthings-cloud==0.2.0 @@ -440,7 +440,7 @@ amcrest==1.9.8 androidtv[async]==0.0.73 # homeassistant.components.androidtv_remote -androidtvremote2==0.1.1 +androidtvremote2==0.1.2 # homeassistant.components.anel_pwrctrl anel-pwrctrl-homeassistant==0.0.1.dev2 @@ -470,7 +470,7 @@ apsystems-ez1==2.2.1 aqualogic==2.6 # homeassistant.components.aranet -aranet4==2.3.4 +aranet4==2.4.0 # homeassistant.components.arcam_fmj arcam-fmj==1.5.2 @@ -559,7 +559,7 @@ beautifulsoup4==4.12.3 # beewi-smartclim==0.0.10 # homeassistant.components.bmw_connected_drive -bimmer-connected[china]==0.16.1 +bimmer-connected[china]==0.16.3 # homeassistant.components.bizkaibus bizkaibus==0.1.1 @@ -603,7 +603,7 @@ bluetooth-auto-recovery==1.4.2 # homeassistant.components.ld2410_ble # homeassistant.components.led_ble # homeassistant.components.private_ble_device -bluetooth-data-tools==1.19.4 +bluetooth-data-tools==1.20.0 # homeassistant.components.bond bond-async==0.2.1 @@ -649,7 +649,7 @@ btsmarthub-devicelist==0.2.3 buienradar==1.0.6 # homeassistant.components.dhcp -cached_ipaddress==0.3.0 +cached-ipaddress==0.5.0 # homeassistant.components.caldav caldav==1.3.9 @@ -703,7 +703,7 @@ datadog==0.15.0 datapoint==0.9.9 # homeassistant.components.bluetooth -dbus-fast==2.22.1 +dbus-fast==2.24.0 # homeassistant.components.debugpy debugpy==1.8.1 @@ -715,7 +715,7 @@ debugpy==1.8.1 # decora==0.6 # homeassistant.components.ecovacs -deebot-client==8.3.0 +deebot-client==8.4.0 # homeassistant.components.ihc # homeassistant.components.namecheapdns @@ -895,14 +895,14 @@ fjaraskupan==2.3.0 flexit_bacnet==2.2.1 # homeassistant.components.flipr -flipr-api==1.5.1 +flipr-api==1.6.1 # homeassistant.components.flux_led flux-led==1.0.4 # homeassistant.components.homekit # homeassistant.components.recorder -fnv-hash-fast==0.5.0 +fnv-hash-fast==1.0.2 # homeassistant.components.foobot foobot_async==1.0.0 @@ -924,13 +924,13 @@ freesms==0.2.0 fritzconnection[qr]==1.13.2 # homeassistant.components.fyta -fyta_cli==0.6.0 +fyta_cli==0.6.6 # homeassistant.components.google_translate gTTS==2.2.4 # homeassistant.components.gardena_bluetooth -gardena-bluetooth==1.4.2 +gardena-bluetooth==1.4.3 # homeassistant.components.google_assistant_sdk gassist-text==0.0.11 @@ -983,16 +983,22 @@ goodwe==0.3.6 google-api-python-client==2.71.0 # homeassistant.components.google_pubsub -google-cloud-pubsub==2.13.11 +google-cloud-pubsub==2.23.0 # homeassistant.components.google_cloud -google-cloud-texttospeech==2.16.3 +google-cloud-speech==2.27.0 + +# homeassistant.components.google_cloud +google-cloud-texttospeech==2.17.2 # homeassistant.components.google_generative_ai_conversation -google-generativeai==0.6.0 +google-generativeai==0.7.2 # homeassistant.components.nest -google-nest-sdm==4.0.7 +google-nest-sdm==5.0.0 + +# homeassistant.components.google_photos +google-photos-library-api==0.8.0 # homeassistant.components.google_travel_time googlemaps==2.5.1 @@ -1059,7 +1065,7 @@ ha-philipsjs==3.2.2 habitipy==0.3.1 # homeassistant.components.bluetooth -habluetooth==3.3.2 +habluetooth==3.4.0 # homeassistant.components.cloud hass-nabucasa==0.81.1 @@ -1099,13 +1105,13 @@ hole==0.8.0 # homeassistant.components.holiday # homeassistant.components.workday -holidays==0.54 +holidays==0.56 # homeassistant.components.frontend -home-assistant-frontend==20240809.0 +home-assistant-frontend==20240904.0 # homeassistant.components.conversation -home-assistant-intents==2024.8.7 +home-assistant-intents==2024.9.4 # homeassistant.components.home_connect homeconnect==0.8.0 @@ -1179,7 +1185,7 @@ inkbird-ble==0.5.8 insteon-frontend-home-assistant==0.5.0 # homeassistant.components.intellifire -intellifire4py==2.2.2 +intellifire4py==4.1.9 # homeassistant.components.iotty iottycloud==0.1.3 @@ -1225,7 +1231,7 @@ kiwiki-client==0.1.1 knocki==0.3.1 # homeassistant.components.knx -knx-frontend==2024.8.9.225351 +knx-frontend==2024.9.4.64538 # homeassistant.components.konnected konnected==1.2.0 @@ -1243,7 +1249,7 @@ lakeside==0.13 laundrify-aio==1.2.2 # homeassistant.components.lcn -lcn-frontend==0.1.5 +lcn-frontend==0.1.6 # homeassistant.components.ld2410_ble ld2410-ble==0.1.1 @@ -1254,6 +1260,9 @@ leaone-ble==0.1.0 # homeassistant.components.led_ble led-ble==1.0.2 +# homeassistant.components.lektrico +lektricowifi==0.0.41 + # homeassistant.components.foscam libpyfoscam==1.2.2 @@ -1282,7 +1291,7 @@ linear-garage-door==0.2.9 linode-api==4.1.9b1 # homeassistant.components.lamarzocco -lmcloud==1.1.13 +lmcloud==1.2.2 # homeassistant.components.google_maps locationsharinglib==5.0.1 @@ -1369,7 +1378,7 @@ mopeka-iot-ble==0.8.0 motionblinds==0.6.24 # homeassistant.components.motionblinds_ble -motionblindsble==0.1.0 +motionblindsble==0.1.1 # homeassistant.components.motioneye motioneye-client==0.3.14 @@ -1423,13 +1432,13 @@ nextcloudmonitor==1.5.1 nextcord==2.6.0 # homeassistant.components.nextdns -nextdns==3.1.0 +nextdns==3.2.0 # homeassistant.components.nibe_heatpump nibe==2.11.0 # homeassistant.components.nice_go -nice-go==0.3.0 +nice-go==0.3.8 # homeassistant.components.niko_home_control niko-home-control==0.2.1 @@ -1523,7 +1532,7 @@ openwrt-luci-rpc==1.1.17 openwrt-ubus-rpc==0.0.2 # homeassistant.components.opower -opower==0.6.0 +opower==0.7.0 # homeassistant.components.oralb oralb-ble==0.17.6 @@ -1656,7 +1665,7 @@ py-dormakaba-dkey==1.0.5 py-improv-ble-client==1.0.3 # homeassistant.components.madvr -py-madvr2==1.6.29 +py-madvr2==1.6.32 # homeassistant.components.melissa py-melissa-climate==2.1.4 @@ -1686,7 +1695,7 @@ pyAtome==0.1.1 pyCEC==0.5.2 # homeassistant.components.control4 -pyControl4==1.1.0 +pyControl4==1.2.0 # homeassistant.components.duotecno pyDuotecno==2024.5.1 @@ -1695,7 +1704,7 @@ pyDuotecno==2024.5.1 pyElectra==1.2.4 # homeassistant.components.emby -pyEmby==1.9 +pyEmby==1.10 # homeassistant.components.hikvision pyHik==0.3.2 @@ -1707,7 +1716,7 @@ pyRFXtrx==0.31.1 pySDCP==1 # homeassistant.components.tibber -pyTibber==0.28.2 +pyTibber==0.30.1 # homeassistant.components.dlink pyW215==0.7.0 @@ -1741,7 +1750,7 @@ pyasuswrt==0.1.21 pyatag==0.3.5.3 # homeassistant.components.netatmo -pyatmo==8.0.3 +pyatmo==8.1.0 # homeassistant.components.apple_tv pyatv==0.15.0 @@ -1759,7 +1768,7 @@ pybbox==0.0.5-alpha pyblackbird==0.6 # homeassistant.components.bluesound -pyblu==0.4.0 +pyblu==1.0.1 # homeassistant.components.neato pybotvac==0.0.25 @@ -1798,11 +1807,14 @@ pycsspeechtts==1.0.8 # pycups==1.9.73 # homeassistant.components.daikin -pydaikin==2.13.4 +pydaikin==2.13.6 # homeassistant.components.danfoss_air pydanfossair==0.1.0 +# homeassistant.components.deako +pydeako==0.4.0 + # homeassistant.components.deconz pydeconz==116 @@ -1944,6 +1956,9 @@ pyiqvia==2022.04.0 # homeassistant.components.irish_rail_transport pyirishrail==0.0.2 +# homeassistant.components.iskra +pyiskra==0.1.8 + # homeassistant.components.iss pyiss==1.0.1 @@ -2210,11 +2225,14 @@ pysmartapp==0.3.5 # homeassistant.components.smartthings pysmartthings==0.7.8 +# homeassistant.components.smarty +pysmarty2==0.10.1 + # homeassistant.components.edl21 pysml==0.0.12 # homeassistant.components.smlight -pysmlight==0.0.12 +pysmlight==0.0.13 # homeassistant.components.snmp pysnmp==6.2.5 @@ -2228,8 +2246,11 @@ pysoma==0.0.12 # homeassistant.components.spc pyspcwebgw==0.7.0 +# homeassistant.components.assist_pipeline +pyspeex-noise==1.0.2 + # homeassistant.components.squeezebox -pysqueezebox==0.7.1 +pysqueezebox==0.8.1 # homeassistant.components.stiebel_eltron pystiebeleltron==0.0.1.dev2 @@ -2310,10 +2331,10 @@ python-join-api==0.0.9 python-juicenet==1.1.0 # homeassistant.components.tplink -python-kasa[speedups]==0.7.1 +python-kasa[speedups]==0.7.2 # homeassistant.components.linkplay -python-linkplay==0.0.8 +python-linkplay==0.0.9 # homeassistant.components.lirc # python-lirc==1.2.3 @@ -2365,7 +2386,7 @@ python-tado==0.17.6 python-technove==1.3.1 # homeassistant.components.telegram_bot -python-telegram-bot[socks]==21.0.1 +python-telegram-bot[socks]==21.5 # homeassistant.components.vlc python-vlc==3.0.18122 @@ -2382,6 +2403,9 @@ pytomorrowio==0.3.6 # homeassistant.components.touchline pytouchline==0.7 +# homeassistant.components.touchline_sl +pytouchlinesl==0.1.5 + # homeassistant.components.traccar # homeassistant.components.traccar_server pytraccar==2.1.1 @@ -2492,13 +2516,13 @@ refoss-ha==1.2.4 regenmaschine==2024.03.0 # homeassistant.components.renault -renault-api==0.2.5 +renault-api==0.2.7 # homeassistant.components.renson renson-endura-delta==1.7.1 # homeassistant.components.reolink -reolink-aio==0.9.7 +reolink-aio==0.9.8 # homeassistant.components.idteck_prox rfk101py==0.0.1 @@ -2507,7 +2531,7 @@ rfk101py==0.0.1 rflink==0.0.66 # homeassistant.components.ring -ring-doorbell[listen]==0.8.12 +ring-doorbell[listen]==0.9.3 # homeassistant.components.fleetgo ritassist==0.9.2 @@ -2613,7 +2637,7 @@ simplepush==2.2.3 simplisafe-python==2024.01.0 # homeassistant.components.sisyphus -sisyphus-control==3.1.3 +sisyphus-control==3.1.4 # homeassistant.components.slack slackclient==2.5.0 @@ -2637,7 +2661,7 @@ soco==0.30.4 solaredge-local==0.2.3 # homeassistant.components.solarlog -solarlog_cli==0.1.6 +solarlog_cli==0.2.2 # homeassistant.components.solax solax==3.1.1 @@ -2736,7 +2760,7 @@ tellcore-net==0.4 tellcore-py==1.1.2 # homeassistant.components.tellduslive -tellduslive==0.10.11 +tellduslive==0.10.12 # homeassistant.components.lg_soundbar temescal==0.5 @@ -2776,6 +2800,9 @@ thermoworks-smoke==0.1.8 # homeassistant.components.thingspeak thingspeak==1.0.0 +# homeassistant.components.lg_thinq +thinqconnect==0.9.6 + # homeassistant.components.tikteck tikteck==0.4 @@ -2810,7 +2837,7 @@ transmission-rpc==7.0.3 ttls==1.8.3 # homeassistant.components.thethingsnetwork -ttn_client==1.1.0 +ttn_client==1.2.0 # homeassistant.components.tuya tuya-device-sharing-sdk==0.1.9 @@ -2912,7 +2939,7 @@ watchdog==2.3.1 waterfurnace==1.1.0 # homeassistant.components.weatherflow_cloud -weatherflow4py==0.2.21 +weatherflow4py==0.2.23 # homeassistant.components.webmin webmin-xmlrpc==0.0.2 @@ -2942,10 +2969,10 @@ wyoming==1.5.4 xbox-webapi==2.0.11 # homeassistant.components.xiaomi_ble -xiaomi-ble==0.30.2 +xiaomi-ble==0.31.1 # homeassistant.components.knx -xknx==3.1.0 +xknx==3.1.1 # homeassistant.components.knx xknxproject==3.7.1 @@ -2964,11 +2991,13 @@ xs1-api-client==3.0.0 yalesmartalarmclient==0.4.0 # homeassistant.components.august +# homeassistant.components.yale # homeassistant.components.yalexs_ble yalexs-ble==2.4.3 # homeassistant.components.august -yalexs==8.1.2 +# homeassistant.components.yale +yalexs==8.6.3 # homeassistant.components.yeelight yeelight==0.7.14 @@ -2995,13 +3024,13 @@ zamg==0.3.6 zengge==0.2 # homeassistant.components.zeroconf -zeroconf==0.132.2 +zeroconf==0.133.0 # homeassistant.components.zeversolar zeversolar==0.3.1 # homeassistant.components.zha -zha==0.0.31 +zha==0.0.32 # homeassistant.components.zhong_hong zhong-hong-hvac==1.0.12 diff --git a/requirements_test.txt b/requirements_test.txt index 19a60b6aa28942..87203daae96fc8 100644 --- a/requirements_test.txt +++ b/requirements_test.txt @@ -11,7 +11,7 @@ astroid==3.2.4 coverage==7.6.0 freezegun==1.5.1 mock-open==1.4.0 -mypy-dev==1.12.0a2 +mypy-dev==1.12.0a3 pre-commit==3.7.1 pydantic==1.10.17 pylint==3.2.6 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 7585c0cfa4ea84..1657969b7e5a2e 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -36,7 +36,7 @@ Mastodon.py==1.8.1 Pillow==10.4.0 # homeassistant.components.plex -PlexAPI==4.15.14 +PlexAPI==4.15.16 # homeassistant.components.progettihwsw ProgettiHWSW==0.1.3 @@ -61,7 +61,7 @@ PyMetEireann==2021.8.0 # homeassistant.components.met # homeassistant.components.norway_air -PyMetno==0.12.0 +PyMetno==0.13.0 # homeassistant.components.keymitt_ble PyMicroBot==0.0.17 @@ -81,7 +81,7 @@ PyQRCode==1.2.1 PyRMVtransport==0.3.3 # homeassistant.components.switchbot -PySwitchbot==0.48.1 +PySwitchbot==0.48.2 # homeassistant.components.syncthru PySyncThru==0.7.10 @@ -91,10 +91,10 @@ PyTransportNSW==0.1.1 # homeassistant.components.camera # homeassistant.components.stream -PyTurboJPEG==1.7.1 +PyTurboJPEG==1.7.5 # homeassistant.components.vicare -PyViCare-neo==0.2.1 +PyViCare-neo==0.3.0 # homeassistant.components.xiaomi_aqara PyXiaomiGateway==0.14.3 @@ -158,20 +158,20 @@ aio-geojson-nsw-rfs-incidents==0.7 aio-geojson-usgs-earthquakes==0.3 # homeassistant.components.gdacs -aio-georss-gdacs==0.9 +aio-georss-gdacs==0.10 # homeassistant.components.airq aioairq==0.3.2 # homeassistant.components.airzone_cloud -aioairzone-cloud==0.6.2 +aioairzone-cloud==0.6.5 # homeassistant.components.airzone aioairzone==0.8.2 # homeassistant.components.ambient_network # homeassistant.components.ambient_station -aioambient==2024.01.0 +aioambient==2024.08.0 # homeassistant.components.apcupsd aioapcaccess==0.4.2 @@ -189,7 +189,7 @@ aioasuswrt==1.4.0 aioautomower==2024.8.0 # homeassistant.components.azure_devops -aioazuredevops==2.1.1 +aioazuredevops==2.2.1 # homeassistant.components.baf aiobafi6==0.9.0 @@ -225,7 +225,7 @@ aioelectricitymaps==0.4.0 aioemonitor==1.0.5 # homeassistant.components.esphome -aioesphomeapi==25.1.0 +aioesphomeapi==25.3.2 # homeassistant.components.flo aioflo==2021.11.0 @@ -243,7 +243,7 @@ aioharmony==0.2.10 aiohomekit==3.2.3 # homeassistant.components.hue -aiohue==4.7.2 +aiohue==4.7.3 # homeassistant.components.imap aioimaplib==1.1.0 @@ -258,7 +258,7 @@ aiolifx-effects==0.3.2 aiolifx-themes==0.5.0 # homeassistant.components.lifx -aiolifx==1.0.6 +aiolifx==1.0.9 # homeassistant.components.livisi aiolivisi==0.0.19 @@ -270,7 +270,7 @@ aiolookin==1.0.0 aiolyric==2.0.1 # homeassistant.components.mealie -aiomealie==0.8.1 +aiomealie==0.9.2 # homeassistant.components.modern_forms aiomodernforms==0.1.8 @@ -300,7 +300,7 @@ aiooui==0.1.6 aiopegelonline==0.0.10 # homeassistant.components.acmeda -aiopulse==0.4.4 +aiopulse==0.4.6 # homeassistant.components.purpleair aiopurpleair==2022.12.1 @@ -329,7 +329,7 @@ aiorecollect==2023.09.0 aioridwell==2024.01.0 # homeassistant.components.ruckus_unleashed -aioruckus==0.34 +aioruckus==0.41 # homeassistant.components.russound_rio aiorussound==2.3.2 @@ -341,7 +341,7 @@ aioruuvigateway==0.1.0 aiosenz==1.0.0 # homeassistant.components.shelly -aioshelly==11.2.4 +aioshelly==11.4.2 # homeassistant.components.skybell aioskybell==22.7.0 @@ -398,7 +398,7 @@ airgradient==0.8.0 airly==1.1.0 # homeassistant.components.airthings_ble -airthings-ble==0.9.0 +airthings-ble==0.9.1 # homeassistant.components.airthings airthings-cloud==0.2.0 @@ -416,7 +416,7 @@ amberelectric==1.1.1 androidtv[async]==0.0.73 # homeassistant.components.androidtv_remote -androidtvremote2==0.1.1 +androidtvremote2==0.1.2 # homeassistant.components.anova anova-wifi==0.17.0 @@ -440,7 +440,7 @@ aprslib==0.7.2 apsystems-ez1==2.2.1 # homeassistant.components.aranet -aranet4==2.3.4 +aranet4==2.4.0 # homeassistant.components.arcam_fmj arcam-fmj==1.5.2 @@ -493,7 +493,7 @@ base36==0.1.1 beautifulsoup4==4.12.3 # homeassistant.components.bmw_connected_drive -bimmer-connected[china]==0.16.1 +bimmer-connected[china]==0.16.3 # homeassistant.components.eq3btsmart # homeassistant.components.esphome @@ -527,7 +527,7 @@ bluetooth-auto-recovery==1.4.2 # homeassistant.components.ld2410_ble # homeassistant.components.led_ble # homeassistant.components.private_ble_device -bluetooth-data-tools==1.19.4 +bluetooth-data-tools==1.20.0 # homeassistant.components.bond bond-async==0.2.1 @@ -560,7 +560,7 @@ bthome-ble==3.9.1 buienradar==1.0.6 # homeassistant.components.dhcp -cached_ipaddress==0.3.0 +cached-ipaddress==0.5.0 # homeassistant.components.caldav caldav==1.3.9 @@ -599,13 +599,13 @@ datadog==0.15.0 datapoint==0.9.9 # homeassistant.components.bluetooth -dbus-fast==2.22.1 +dbus-fast==2.24.0 # homeassistant.components.debugpy debugpy==1.8.1 # homeassistant.components.ecovacs -deebot-client==8.3.0 +deebot-client==8.4.0 # homeassistant.components.ihc # homeassistant.components.namecheapdns @@ -754,14 +754,14 @@ fjaraskupan==2.3.0 flexit_bacnet==2.2.1 # homeassistant.components.flipr -flipr-api==1.5.1 +flipr-api==1.6.1 # homeassistant.components.flux_led flux-led==1.0.4 # homeassistant.components.homekit # homeassistant.components.recorder -fnv-hash-fast==0.5.0 +fnv-hash-fast==1.0.2 # homeassistant.components.foobot foobot_async==1.0.0 @@ -777,13 +777,13 @@ freebox-api==1.1.0 fritzconnection[qr]==1.13.2 # homeassistant.components.fyta -fyta_cli==0.6.0 +fyta_cli==0.6.6 # homeassistant.components.google_translate gTTS==2.2.4 # homeassistant.components.gardena_bluetooth -gardena-bluetooth==1.4.2 +gardena-bluetooth==1.4.3 # homeassistant.components.google_assistant_sdk gassist-text==0.0.11 @@ -833,13 +833,22 @@ goodwe==0.3.6 google-api-python-client==2.71.0 # homeassistant.components.google_pubsub -google-cloud-pubsub==2.13.11 +google-cloud-pubsub==2.23.0 + +# homeassistant.components.google_cloud +google-cloud-speech==2.27.0 + +# homeassistant.components.google_cloud +google-cloud-texttospeech==2.17.2 # homeassistant.components.google_generative_ai_conversation -google-generativeai==0.6.0 +google-generativeai==0.7.2 # homeassistant.components.nest -google-nest-sdm==4.0.7 +google-nest-sdm==5.0.0 + +# homeassistant.components.google_photos +google-photos-library-api==0.8.0 # homeassistant.components.google_travel_time googlemaps==2.5.1 @@ -894,7 +903,7 @@ ha-philipsjs==3.2.2 habitipy==0.3.1 # homeassistant.components.bluetooth -habluetooth==3.3.2 +habluetooth==3.4.0 # homeassistant.components.cloud hass-nabucasa==0.81.1 @@ -922,13 +931,13 @@ hole==0.8.0 # homeassistant.components.holiday # homeassistant.components.workday -holidays==0.54 +holidays==0.56 # homeassistant.components.frontend -home-assistant-frontend==20240809.0 +home-assistant-frontend==20240904.0 # homeassistant.components.conversation -home-assistant-intents==2024.8.7 +home-assistant-intents==2024.9.4 # homeassistant.components.home_connect homeconnect==0.8.0 @@ -987,7 +996,7 @@ inkbird-ble==0.5.8 insteon-frontend-home-assistant==0.5.0 # homeassistant.components.intellifire -intellifire4py==2.2.2 +intellifire4py==4.1.9 # homeassistant.components.iotty iottycloud==0.1.3 @@ -1021,7 +1030,7 @@ kegtron-ble==0.4.0 knocki==0.3.1 # homeassistant.components.knx -knx-frontend==2024.8.9.225351 +knx-frontend==2024.9.4.64538 # homeassistant.components.konnected konnected==1.2.0 @@ -1036,7 +1045,7 @@ lacrosse-view==1.0.2 laundrify-aio==1.2.2 # homeassistant.components.lcn -lcn-frontend==0.1.5 +lcn-frontend==0.1.6 # homeassistant.components.ld2410_ble ld2410-ble==0.1.1 @@ -1047,6 +1056,9 @@ leaone-ble==0.1.0 # homeassistant.components.led_ble led-ble==1.0.2 +# homeassistant.components.lektrico +lektricowifi==0.0.41 + # homeassistant.components.foscam libpyfoscam==1.2.2 @@ -1060,7 +1072,7 @@ libsoundtouch==0.8 linear-garage-door==0.2.9 # homeassistant.components.lamarzocco -lmcloud==1.1.13 +lmcloud==1.2.2 # homeassistant.components.london_underground london-tube-status==0.5 @@ -1135,7 +1147,7 @@ mopeka-iot-ble==0.8.0 motionblinds==0.6.24 # homeassistant.components.motionblinds_ble -motionblindsble==0.1.0 +motionblindsble==0.1.1 # homeassistant.components.motioneye motioneye-client==0.3.14 @@ -1180,13 +1192,13 @@ nextcloudmonitor==1.5.1 nextcord==2.6.0 # homeassistant.components.nextdns -nextdns==3.1.0 +nextdns==3.2.0 # homeassistant.components.nibe_heatpump nibe==2.11.0 # homeassistant.components.nice_go -nice-go==0.3.0 +nice-go==0.3.8 # homeassistant.components.nfandroidtv notifications-android-tv==0.1.5 @@ -1250,7 +1262,7 @@ openhomedevice==2.2.0 openwebifpy==4.2.7 # homeassistant.components.opower -opower==0.6.0 +opower==0.7.0 # homeassistant.components.oralb oralb-ble==0.17.6 @@ -1348,7 +1360,7 @@ py-dormakaba-dkey==1.0.5 py-improv-ble-client==1.0.3 # homeassistant.components.madvr -py-madvr2==1.6.29 +py-madvr2==1.6.32 # homeassistant.components.melissa py-melissa-climate==2.1.4 @@ -1369,7 +1381,7 @@ py-synologydsm-api==2.5.2 pyCEC==0.5.2 # homeassistant.components.control4 -pyControl4==1.1.0 +pyControl4==1.2.0 # homeassistant.components.duotecno pyDuotecno==2024.5.1 @@ -1381,7 +1393,7 @@ pyElectra==1.2.4 pyRFXtrx==0.31.1 # homeassistant.components.tibber -pyTibber==0.28.2 +pyTibber==0.30.1 # homeassistant.components.dlink pyW215==0.7.0 @@ -1409,7 +1421,7 @@ pyasuswrt==0.1.21 pyatag==0.3.5.3 # homeassistant.components.netatmo -pyatmo==8.0.3 +pyatmo==8.1.0 # homeassistant.components.apple_tv pyatv==0.15.0 @@ -1424,7 +1436,7 @@ pybalboa==1.0.2 pyblackbird==0.6 # homeassistant.components.bluesound -pyblu==0.4.0 +pyblu==1.0.1 # homeassistant.components.neato pybotvac==0.0.25 @@ -1445,7 +1457,10 @@ pycoolmasternet-async==0.2.2 pycsspeechtts==1.0.8 # homeassistant.components.daikin -pydaikin==2.13.4 +pydaikin==2.13.6 + +# homeassistant.components.deako +pydeako==0.4.0 # homeassistant.components.deconz pydeconz==116 @@ -1552,6 +1567,9 @@ pyipp==0.16.0 # homeassistant.components.iqvia pyiqvia==2022.04.0 +# homeassistant.components.iskra +pyiskra==0.1.8 + # homeassistant.components.iss pyiss==1.0.1 @@ -1768,7 +1786,7 @@ pysmartthings==0.7.8 pysml==0.0.12 # homeassistant.components.smlight -pysmlight==0.0.12 +pysmlight==0.0.13 # homeassistant.components.snmp pysnmp==6.2.5 @@ -1782,8 +1800,11 @@ pysoma==0.0.12 # homeassistant.components.spc pyspcwebgw==0.7.0 +# homeassistant.components.assist_pipeline +pyspeex-noise==1.0.2 + # homeassistant.components.squeezebox -pysqueezebox==0.7.1 +pysqueezebox==0.8.1 # homeassistant.components.suez_water pysuez==0.2.0 @@ -1828,10 +1849,10 @@ python-izone==1.2.9 python-juicenet==1.1.0 # homeassistant.components.tplink -python-kasa[speedups]==0.7.1 +python-kasa[speedups]==0.7.2 # homeassistant.components.linkplay -python-linkplay==0.0.8 +python-linkplay==0.0.9 # homeassistant.components.matter python-matter-server==6.3.0 @@ -1877,7 +1898,7 @@ python-tado==0.17.6 python-technove==1.3.1 # homeassistant.components.telegram_bot -python-telegram-bot[socks]==21.0.1 +python-telegram-bot[socks]==21.5 # homeassistant.components.tile pytile==2023.12.0 @@ -1885,6 +1906,9 @@ pytile==2023.12.0 # homeassistant.components.tomorrowio pytomorrowio==0.3.6 +# homeassistant.components.touchline_sl +pytouchlinesl==0.1.5 + # homeassistant.components.traccar # homeassistant.components.traccar_server pytraccar==2.1.1 @@ -1974,19 +1998,19 @@ refoss-ha==1.2.4 regenmaschine==2024.03.0 # homeassistant.components.renault -renault-api==0.2.5 +renault-api==0.2.7 # homeassistant.components.renson renson-endura-delta==1.7.1 # homeassistant.components.reolink -reolink-aio==0.9.7 +reolink-aio==0.9.8 # homeassistant.components.rflink rflink==0.0.66 # homeassistant.components.ring -ring-doorbell[listen]==0.8.12 +ring-doorbell[listen]==0.9.3 # homeassistant.components.roku rokuecp==0.19.3 @@ -2080,7 +2104,7 @@ snapcast==2.3.6 soco==0.30.4 # homeassistant.components.solarlog -solarlog_cli==0.1.6 +solarlog_cli==0.2.2 # homeassistant.components.solax solax==3.1.1 @@ -2158,7 +2182,7 @@ systembridgemodels==4.2.4 tailscale==0.6.1 # homeassistant.components.tellduslive -tellduslive==0.10.11 +tellduslive==0.10.12 # homeassistant.components.lg_soundbar temescal==0.5 @@ -2186,6 +2210,9 @@ thermobeacon-ble==0.7.0 # homeassistant.components.thermopro thermopro-ble==0.10.0 +# homeassistant.components.lg_thinq +thinqconnect==0.9.6 + # homeassistant.components.tilt_ble tilt-ble==0.2.3 @@ -2211,7 +2238,7 @@ transmission-rpc==7.0.3 ttls==1.8.3 # homeassistant.components.thethingsnetwork -ttn_client==1.1.0 +ttn_client==1.2.0 # homeassistant.components.tuya tuya-device-sharing-sdk==0.1.9 @@ -2298,7 +2325,7 @@ wallbox==0.7.0 watchdog==2.3.1 # homeassistant.components.weatherflow_cloud -weatherflow4py==0.2.21 +weatherflow4py==0.2.23 # homeassistant.components.webmin webmin-xmlrpc==0.0.2 @@ -2325,10 +2352,10 @@ wyoming==1.5.4 xbox-webapi==2.0.11 # homeassistant.components.xiaomi_ble -xiaomi-ble==0.30.2 +xiaomi-ble==0.31.1 # homeassistant.components.knx -xknx==3.1.0 +xknx==3.1.1 # homeassistant.components.knx xknxproject==3.7.1 @@ -2344,11 +2371,13 @@ xmltodict==0.13.0 yalesmartalarmclient==0.4.0 # homeassistant.components.august +# homeassistant.components.yale # homeassistant.components.yalexs_ble yalexs-ble==2.4.3 # homeassistant.components.august -yalexs==8.1.2 +# homeassistant.components.yale +yalexs==8.6.3 # homeassistant.components.yeelight yeelight==0.7.14 @@ -2369,13 +2398,13 @@ yt-dlp==2024.08.06 zamg==0.3.6 # homeassistant.components.zeroconf -zeroconf==0.132.2 +zeroconf==0.133.0 # homeassistant.components.zeversolar zeversolar==0.3.1 # homeassistant.components.zha -zha==0.0.31 +zha==0.0.32 # homeassistant.components.zwave_js zwave-js-server-python==0.57.0 diff --git a/requirements_test_pre_commit.txt b/requirements_test_pre_commit.txt index 6bd2fbbc145bba..0c8d2b3796ba14 100644 --- a/requirements_test_pre_commit.txt +++ b/requirements_test_pre_commit.txt @@ -1,5 +1,5 @@ # Automatically generated from .pre-commit-config.yaml by gen_requirements_all.py, do not edit codespell==2.3.0 -ruff==0.6.1 +ruff==0.6.2 yamllint==1.35.1 diff --git a/script/amazon_polly.py b/script/amazon_polly.py new file mode 100644 index 00000000000000..fcb0a4b7987e25 --- /dev/null +++ b/script/amazon_polly.py @@ -0,0 +1,70 @@ +"""Helper script to update supported languages for Amazone Polly text-to-speech (TTS). + +N.B. This script requires AWS credentials. +""" + +from dataclasses import dataclass +from pathlib import Path +from typing import Self + +import boto3 + +from .hassfest.serializer import format_python_namespace + + +@dataclass(frozen=True) +class AmazonPollyVoice: + """Amazon Polly Voice.""" + + id: str + name: str + gender: str + language_name: str + language_code: str + supported_engines: set[str] + additional_language_codes: set[str] + + @classmethod + def validate(cls, model: dict[str, str | list[str]]) -> Self: + """Validate data model.""" + return cls( + id=model["Id"], + name=model["Name"], + gender=model["Gender"], + language_name=model["LanguageName"], + language_code=model["LanguageCode"], + supported_engines=set(model["SupportedEngines"]), + additional_language_codes=set(model.get("AdditionalLanguageCodes", [])), + ) + + +def get_all_voices(client: boto3.client) -> list[AmazonPollyVoice]: + """Get list of all supported voices from Amazon Polly.""" + response = client.describe_voices() + return [AmazonPollyVoice.validate(voice) for voice in response["Voices"]] + + +supported_regions = set( + boto3.session.Session().get_available_regions(service_name="polly") +) + +polly_client = boto3.client(service_name="polly", region_name="us-east-1") +voices = get_all_voices(polly_client) +supported_voices = set({v.id for v in voices}) +supported_engines = set().union(*[v.supported_engines for v in voices]) + +Path("homeassistant/generated/amazon_polly.py").write_text( + format_python_namespace( + { + "SUPPORTED_VOICES": supported_voices, + "SUPPORTED_REGIONS": supported_regions, + "SUPPORTED_ENGINES": supported_engines, + }, + annotations={ + "SUPPORTED_VOICES": "Final[set[str]]", + "SUPPORTED_REGIONS": "Final[set[str]]", + "SUPPORTED_ENGINES": "Final[set[str]]", + }, + generator="script.amazon_polly", + ) +) diff --git a/script/gen_requirements_all.py b/script/gen_requirements_all.py index 6ce97468699f0c..b2165289ad8525 100755 --- a/script/gen_requirements_all.py +++ b/script/gen_requirements_all.py @@ -15,7 +15,7 @@ from typing import Any from homeassistant.util.yaml.loader import load_yaml -from script.hassfest.model import Integration +from script.hassfest.model import Config, Integration # Requirements which can't be installed on all systems because they rely on additional # system packages. Requirements listed in EXCLUDED_REQUIREMENTS_ALL will be commented-out @@ -130,12 +130,6 @@ # Ensure we run compatible with musllinux build env numpy==1.26.0 -# Prevent dependency conflicts between sisyphus-control and aioambient -# until upper bounds for sisyphus-control have been updated -# https://github.com/jkeljo/sisyphus-control/issues/6 -python-engineio>=3.13.1,<4.0 -python-socketio>=4.6.0,<5.0 - # Constrain multidict to avoid typing issues # https://github.com/home-assistant/core/pull/67046 multidict>=6.0.2 @@ -276,7 +270,9 @@ def gather_recursive_requirements( seen = set() seen.add(domain) - integration = Integration(Path(f"homeassistant/components/{domain}")) + integration = Integration( + Path(f"homeassistant/components/{domain}"), _get_hassfest_config() + ) integration.load_manifest() reqs = {x for x in integration.requirements if x not in CONSTRAINT_BASE} for dep_domain in integration.dependencies: @@ -342,7 +338,8 @@ def gather_requirements_from_manifests( errors: list[str], reqs: dict[str, list[str]] ) -> None: """Gather all of the requirements from manifests.""" - integrations = Integration.load_dir(Path("homeassistant/components")) + config = _get_hassfest_config() + integrations = Integration.load_dir(config.core_integrations_path, config) for domain in sorted(integrations): integration = integrations[domain] @@ -590,6 +587,17 @@ def main(validate: bool, ci: bool) -> int: return 0 +def _get_hassfest_config() -> Config: + """Get hassfest config.""" + return Config( + root=Path(".").absolute(), + specific_integrations=None, + action="validate", + requirements=True, + core_integrations_path=Path("homeassistant/components"), + ) + + if __name__ == "__main__": _VAL = sys.argv[-1] == "validate" _CI = sys.argv[-1] == "ci" diff --git a/script/hassfest/__main__.py b/script/hassfest/__main__.py index ea3c56200a25f6..b48871b465163d 100644 --- a/script/hassfest/__main__.py +++ b/script/hassfest/__main__.py @@ -107,6 +107,12 @@ def get_config() -> Config: default=ALL_PLUGIN_NAMES, help="Comma-separate list of plugins to run. Valid plugin names: %(default)s", ) + parser.add_argument( + "--core-integrations-path", + type=pathlib.Path, + default=pathlib.Path("homeassistant/components"), + help="Path to core integrations", + ) parsed = parser.parse_args() if parsed.action is None: @@ -129,6 +135,7 @@ def get_config() -> Config: action=parsed.action, requirements=parsed.requirements, plugins=set(parsed.plugins), + core_integrations_path=parsed.core_integrations_path, ) @@ -146,12 +153,12 @@ def main() -> int: integrations = {} for int_path in config.specific_integrations: - integration = Integration(int_path) + integration = Integration(int_path, config) integration.load_manifest() integrations[integration.domain] = integration else: - integrations = Integration.load_dir(pathlib.Path("homeassistant/components")) + integrations = Integration.load_dir(config.core_integrations_path, config) plugins += HASS_PLUGINS for plugin in plugins: diff --git a/script/hassfest/brand.py b/script/hassfest/brand.py index fe47d31067a02c..6139e12393e87f 100644 --- a/script/hassfest/brand.py +++ b/script/hassfest/brand.py @@ -18,6 +18,8 @@ } ) +BRAND_EXCEPTIONS = ["u_tec"] + def _validate_brand( brand: Brand, integrations: dict[str, Integration], config: Config @@ -38,10 +40,14 @@ def _validate_brand( f"Domain '{brand.domain}' does not match file name {brand.path.name}", ) - if not brand.integrations and not brand.iot_standards: + if ( + len(brand.integrations) < 2 + and not brand.iot_standards + and brand.domain not in BRAND_EXCEPTIONS + ): config.add_error( "brand", - f"{brand.path.name}: At least one of integrations or " + f"{brand.path.name}: At least two integrations or " "iot_standards must be non-empty", ) diff --git a/script/hassfest/docker.py b/script/hassfest/docker.py index e38a238be7d7ac..bce77e1ece0cd1 100644 --- a/script/hassfest/docker.py +++ b/script/hassfest/docker.py @@ -1,7 +1,12 @@ """Generate and validate the dockerfile.""" +from dataclasses import dataclass +from pathlib import Path + from homeassistant import core +from homeassistant.const import Platform from homeassistant.util import executor, thread +from script.gen_requirements_all import gather_recursive_requirements from .model import Config, Integration from .requirements import PACKAGE_REGEX, PIP_VERSION_RANGE_SEPARATOR @@ -20,7 +25,7 @@ ARG QEMU_CPU # Install uv -RUN pip3 install uv=={uv_version} +RUN pip3 install uv=={uv} WORKDIR /usr/src @@ -61,30 +66,102 @@ WORKDIR /config """ +_HASSFEST_TEMPLATE = r"""# Automatically generated by hassfest. +# +# To update, run python3 -m script.hassfest -p docker +FROM python:alpine + +ENV \ + UV_SYSTEM_PYTHON=true \ + UV_EXTRA_INDEX_URL="https://wheels.home-assistant.io/musllinux-index/" + +SHELL ["/bin/sh", "-o", "pipefail", "-c"] +ENTRYPOINT ["/usr/src/homeassistant/script/hassfest/docker/entrypoint.sh"] +WORKDIR "/github/workspace" + +COPY . /usr/src/homeassistant -def _get_uv_version() -> str: - with open("requirements_test.txt") as fp: +# Uv is only needed during build +RUN --mount=from=ghcr.io/astral-sh/uv:{uv},source=/uv,target=/bin/uv \ + # Required for PyTurboJPEG + apk add --no-cache libturbojpeg \ + && uv pip install \ + --no-build \ + --no-cache \ + -c /usr/src/homeassistant/homeassistant/package_constraints.txt \ + -r /usr/src/homeassistant/requirements.txt \ + stdlib-list==0.10.0 pipdeptree=={pipdeptree} tqdm=={tqdm} ruff=={ruff} \ + {required_components_packages} + +LABEL "name"="hassfest" +LABEL "maintainer"="Home Assistant " + +LABEL "com.github.actions.name"="hassfest" +LABEL "com.github.actions.description"="Run hassfest to validate standalone integration repositories" +LABEL "com.github.actions.icon"="terminal" +LABEL "com.github.actions.color"="gray-dark" +""" + + +def _get_package_versions(file: str, packages: set[str]) -> dict[str, str]: + package_versions: dict[str, str] = {} + with open(file, encoding="UTF-8") as fp: for _, line in enumerate(fp): + if package_versions.keys() == packages: + return package_versions + if match := PACKAGE_REGEX.match(line): pkg, sep, version = match.groups() - if pkg != "uv": + if pkg not in packages: continue if sep != "==" or not version: raise RuntimeError( - 'Requirement uv need to be pinned "uv==".' + f'Requirement {pkg} need to be pinned "{pkg}==".' ) for part in version.split(";", 1)[0].split(","): version_part = PIP_VERSION_RANGE_SEPARATOR.match(part) if version_part: - return version_part.group(2) + package_versions[pkg] = version_part.group(2) + break + + if package_versions.keys() == packages: + return package_versions - raise RuntimeError("Invalid uv requirement in requirements_test.txt") + raise RuntimeError("At least one package was not found in the requirements file.") -def _generate_dockerfile() -> str: +@dataclass +class File: + """File.""" + + content: str + path: Path + + +def _generate_hassfest_dockerimage( + config: Config, timeout: int, package_versions: dict[str, str] +) -> File: + packages = set() + already_checked_domains = set() + for platform in Platform: + packages.update( + gather_recursive_requirements(platform.value, already_checked_domains) + ) + + return File( + _HASSFEST_TEMPLATE.format( + timeout=timeout, + required_components_packages=" ".join(sorted(packages)), + **package_versions, + ), + config.root / "script/hassfest/docker/Dockerfile", + ) + + +def _generate_files(config: Config) -> list[File]: timeout = ( core.STOPPING_STAGE_SHUTDOWN_TIMEOUT + core.STOP_STAGE_SHUTDOWN_TIMEOUT @@ -93,27 +170,39 @@ def _generate_dockerfile() -> str: + executor.EXECUTOR_SHUTDOWN_TIMEOUT + thread.THREADING_SHUTDOWN_TIMEOUT + 10 + ) * 1000 + + package_versions = _get_package_versions( + "requirements_test.txt", {"pipdeptree", "tqdm", "uv"} ) - return DOCKERFILE_TEMPLATE.format( - timeout=timeout * 1000, uv_version=_get_uv_version() + package_versions |= _get_package_versions( + "requirements_test_pre_commit.txt", {"ruff"} ) + return [ + File( + DOCKERFILE_TEMPLATE.format(timeout=timeout, **package_versions), + config.root / "Dockerfile", + ), + _generate_hassfest_dockerimage(config, timeout, package_versions), + ] + def validate(integrations: dict[str, Integration], config: Config) -> None: """Validate dockerfile.""" - dockerfile_content = _generate_dockerfile() - config.cache["dockerfile"] = dockerfile_content - - dockerfile_path = config.root / "Dockerfile" - if dockerfile_path.read_text() != dockerfile_content: - config.add_error( - "docker", - "File Dockerfile is not up to date. Run python3 -m script.hassfest", - fixable=True, - ) + docker_files = _generate_files(config) + config.cache["docker"] = docker_files + + for file in docker_files: + if file.content != file.path.read_text(): + config.add_error( + "docker", + f"File {file.path} is not up to date. Run python3 -m script.hassfest", + fixable=True, + ) def generate(integrations: dict[str, Integration], config: Config) -> None: """Generate dockerfile.""" - dockerfile_path = config.root / "Dockerfile" - dockerfile_path.write_text(config.cache["dockerfile"]) + for file in _generate_files(config): + file.path.write_text(file.content) diff --git a/script/hassfest/docker/Dockerfile b/script/hassfest/docker/Dockerfile new file mode 100644 index 00000000000000..4dbea0e4c959b5 --- /dev/null +++ b/script/hassfest/docker/Dockerfile @@ -0,0 +1,34 @@ +# Automatically generated by hassfest. +# +# To update, run python3 -m script.hassfest -p docker +FROM python:alpine + +ENV \ + UV_SYSTEM_PYTHON=true \ + UV_EXTRA_INDEX_URL="https://wheels.home-assistant.io/musllinux-index/" + +SHELL ["/bin/sh", "-o", "pipefail", "-c"] +ENTRYPOINT ["/usr/src/homeassistant/script/hassfest/docker/entrypoint.sh"] +WORKDIR "/github/workspace" + +COPY . /usr/src/homeassistant + +# Uv is only needed during build +RUN --mount=from=ghcr.io/astral-sh/uv:0.2.27,source=/uv,target=/bin/uv \ + # Required for PyTurboJPEG + apk add --no-cache libturbojpeg \ + && uv pip install \ + --no-build \ + --no-cache \ + -c /usr/src/homeassistant/homeassistant/package_constraints.txt \ + -r /usr/src/homeassistant/requirements.txt \ + stdlib-list==0.10.0 pipdeptree==2.23.1 tqdm==4.66.4 ruff==0.6.2 \ + PyTurboJPEG==1.7.5 ha-ffmpeg==3.2.0 hassil==1.7.4 home-assistant-intents==2024.9.4 mutagen==1.47.0 + +LABEL "name"="hassfest" +LABEL "maintainer"="Home Assistant " + +LABEL "com.github.actions.name"="hassfest" +LABEL "com.github.actions.description"="Run hassfest to validate standalone integration repositories" +LABEL "com.github.actions.icon"="terminal" +LABEL "com.github.actions.color"="gray-dark" diff --git a/script/hassfest/docker/Dockerfile.dockerignore b/script/hassfest/docker/Dockerfile.dockerignore new file mode 100644 index 00000000000000..c109421fce17b8 --- /dev/null +++ b/script/hassfest/docker/Dockerfile.dockerignore @@ -0,0 +1,11 @@ +# Ignore everything except the specified files +* + +!homeassistant/ +!requirements.txt +!script/ +script/hassfest/docker/ +!script/hassfest/docker/entrypoint.sh + +# Temporary files +**/__pycache__ \ No newline at end of file diff --git a/script/hassfest/docker/entrypoint.sh b/script/hassfest/docker/entrypoint.sh new file mode 100755 index 00000000000000..7b75eb186d2eff --- /dev/null +++ b/script/hassfest/docker/entrypoint.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +integrations="" +integration_path="" + +# Enable recursive globbing using find +for manifest in $(find . -name "manifest.json"); do + manifest_path=$(realpath "${manifest}") + integrations="$integrations --integration-path ${manifest_path%/*}" +done + +if [ -z "$integrations" ]; then + echo "Error: No integrations found!" + exit 1 +fi + +cd /usr/src/homeassistant || exit 1 +exec python3 -m script.hassfest --action validate $integrations "$@" diff --git a/script/hassfest/icons.py b/script/hassfest/icons.py index 087d395afebe0d..f6bcd865c23cd7 100644 --- a/script/hassfest/icons.py +++ b/script/hassfest/icons.py @@ -9,6 +9,7 @@ from voluptuous.humanize import humanize_error import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.icon import convert_shorthand_service_icon from .model import Config, Integration from .translations import translation_key_validator @@ -51,7 +52,7 @@ def ensure_not_same_as_default(value: dict) -> dict: { "step": { str: { - "section": { + "sections": { str: icon_value_validator, } } @@ -60,7 +61,38 @@ def ensure_not_same_as_default(value: dict) -> dict: ) -def icon_schema(integration_type: str, no_entity_platform: bool) -> vol.Schema: +CORE_SERVICE_ICONS_SCHEMA = cv.schema_with_slug_keys( + vol.Schema( + { + vol.Optional("service"): icon_value_validator, + vol.Optional("sections"): cv.schema_with_slug_keys( + icon_value_validator, slug_validator=translation_key_validator + ), + } + ), + slug_validator=translation_key_validator, +) + + +CUSTOM_INTEGRATION_SERVICE_ICONS_SCHEMA = cv.schema_with_slug_keys( + vol.All( + convert_shorthand_service_icon, + vol.Schema( + { + vol.Optional("service"): icon_value_validator, + vol.Optional("sections"): cv.schema_with_slug_keys( + icon_value_validator, slug_validator=translation_key_validator + ), + } + ), + ), + slug_validator=translation_key_validator, +) + + +def icon_schema( + core_integration: bool, integration_type: str, no_entity_platform: bool +) -> vol.Schema: """Create an icon schema.""" state_validator = cv.schema_with_slug_keys( @@ -91,7 +123,9 @@ def icon_schema_slug(marker: type[vol.Marker]) -> dict[vol.Marker, Any]: {str: {"fix_flow": DATA_ENTRY_ICONS_SCHEMA}} ), vol.Optional("options"): DATA_ENTRY_ICONS_SCHEMA, - vol.Optional("services"): state_validator, + vol.Optional("services"): CORE_SERVICE_ICONS_SCHEMA + if core_integration + else CUSTOM_INTEGRATION_SERVICE_ICONS_SCHEMA, } ) @@ -146,7 +180,9 @@ def validate_icon_file(config: Config, integration: Integration) -> None: no_entity_platform = integration.domain in ("notify", "image_processing") - schema = icon_schema(integration.integration_type, no_entity_platform) + schema = icon_schema( + integration.core, integration.integration_type, no_entity_platform + ) try: schema(icons) diff --git a/script/hassfest/manifest.py b/script/hassfest/manifest.py index 1c01ee7cf583bd..8643e34725f4a8 100644 --- a/script/hassfest/manifest.py +++ b/script/hassfest/manifest.py @@ -117,8 +117,6 @@ class QualityScale(IntEnum): # https://github.com/home-assistant/developers.home-assistant/pull/1512 NO_DIAGNOSTICS = [ "dlna_dms", - "gdacs", - "geonetnz_quakes", "hyperion", "nightscout", "pvpc_hourly_pricing", diff --git a/script/hassfest/model.py b/script/hassfest/model.py index 736fb6874be8c5..63e9b025ed459e 100644 --- a/script/hassfest/model.py +++ b/script/hassfest/model.py @@ -29,6 +29,7 @@ class Config: root: pathlib.Path action: Literal["validate", "generate"] requirements: bool + core_integrations_path: pathlib.Path errors: list[Error] = field(default_factory=list) cache: dict[str, Any] = field(default_factory=dict) plugins: set[str] = field(default_factory=set) @@ -105,7 +106,7 @@ class Integration: """Represent an integration in our validator.""" @classmethod - def load_dir(cls, path: pathlib.Path) -> dict[str, Integration]: + def load_dir(cls, path: pathlib.Path, config: Config) -> dict[str, Integration]: """Load all integrations in a directory.""" assert path.is_dir() integrations: dict[str, Integration] = {} @@ -123,13 +124,14 @@ def load_dir(cls, path: pathlib.Path) -> dict[str, Integration]: ) continue - integration = cls(fil) + integration = cls(fil, config) integration.load_manifest() integrations[integration.domain] = integration return integrations path: pathlib.Path + _config: Config _manifest: dict[str, Any] | None = None manifest_path: pathlib.Path | None = None errors: list[Error] = field(default_factory=list) @@ -150,7 +152,9 @@ def domain(self) -> str: @property def core(self) -> bool: """Core integration.""" - return self.path.as_posix().startswith("homeassistant/components") + return self.path.as_posix().startswith( + self._config.core_integrations_path.as_posix() + ) @property def disabled(self) -> str | None: diff --git a/script/hassfest/translations.py b/script/hassfest/translations.py index c5efd05948fc44..fa12ce626ad2f5 100644 --- a/script/hassfest/translations.py +++ b/script/hassfest/translations.py @@ -167,7 +167,7 @@ def gen_data_entry_schema( vol.Optional("data_description"): {str: translation_value_validator}, vol.Optional("menu_options"): {str: translation_value_validator}, vol.Optional("submit"): translation_value_validator, - vol.Optional("section"): { + vol.Optional("sections"): { str: { vol.Optional("data"): {str: translation_value_validator}, vol.Optional("description"): translation_value_validator, diff --git a/script/licenses.py b/script/licenses.py index bf14adca474eaa..84797372309faa 100644 --- a/script/licenses.py +++ b/script/licenses.py @@ -129,7 +129,6 @@ def from_dict(cls, data: dict[str, str]) -> PackageDefinition: "aiooui", # https://github.com/Bluetooth-Devices/aiooui/pull/8 "aioruuvigateway", # https://github.com/akx/aioruuvigateway/pull/6 "aiovodafone", # https://github.com/chemelli74/aiovodafone/pull/131 - "airthings-ble", # https://github.com/Airthings/airthings-ble/pull/42 "apple_weatherkit", # https://github.com/tjhorner/python-weatherkit/pull/3 "asyncio", # PSF License "chacha20poly1305", # LGPL @@ -154,9 +153,7 @@ def from_dict(cls, data: dict[str, str]) -> PackageDefinition: "neurio", # https://github.com/jordanh/neurio-python/pull/13 "nsw-fuel-api-client", # https://github.com/nickw444/nsw-fuel-api-client/pull/14 "pigpio", # https://github.com/joan2937/pigpio/pull/608 - "pyEmby", # https://github.com/mezz64/pyEmby/pull/12 "pymitv", # MIT - "pyTibber", # https://github.com/Danielhiversen/pyTibber/pull/294 "pybbox", # https://github.com/HydrelioxGitHub/pybbox/pull/5 "pyeconet", # https://github.com/w1ll1am23/pyeconet/pull/41 "pysabnzbd", # https://github.com/jeradM/pysabnzbd/pull/6 @@ -168,7 +165,6 @@ def from_dict(cls, data: dict[str, str]) -> PackageDefinition: "sensirion-ble", # https://github.com/akx/sensirion-ble/pull/9 "sharp_aquos_rc", # https://github.com/jmoore987/sharp_aquos_rc/pull/14 "tapsaff", # https://github.com/bazwilliams/python-taps-aff/pull/5 - "tellduslive", # https://github.com/molobrakos/tellduslive/pull/24 "tellsticknet", # https://github.com/molobrakos/tellsticknet/pull/33 "vincenty", # Public domain "zeversolar", # https://github.com/kvanzuijlen/zeversolar/pull/46 diff --git a/tests/common.py b/tests/common.py index 893c9ffcd67b76..c2d561551ca274 100644 --- a/tests/common.py +++ b/tests/common.py @@ -47,7 +47,7 @@ _async_get_device_automation_capabilities as async_get_device_automation_capabilities, ) from homeassistant.config import IntegrationConfigInfo, async_process_component_config -from homeassistant.config_entries import ConfigEntry, ConfigFlow +from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResult from homeassistant.const import ( DEVICE_DEFAULT_NAME, EVENT_HOMEASSISTANT_CLOSE, @@ -1054,6 +1054,25 @@ def mock_state( """ self._async_set_state(hass, state, reason) + async def start_reauth_flow( + self, + hass: HomeAssistant, + context: dict[str, Any] | None = None, + data: dict[str, Any] | None = None, + ) -> ConfigFlowResult: + """Start a reauthentication flow.""" + return await hass.config_entries.flow.async_init( + self.domain, + context={ + "source": config_entries.SOURCE_REAUTH, + "entry_id": self.entry_id, + "title_placeholders": {"name": self.title}, + "unique_id": self.unique_id, + } + | (context or {}), + data=self.data | (data or {}), + ) + def patch_yaml_files(files_dict, endswith=True): """Patch load_yaml with a dictionary of yaml files.""" diff --git a/tests/components/abode/test_config_flow.py b/tests/components/abode/test_config_flow.py index 265a77560f729d..a37fb8cbe33eab 100644 --- a/tests/components/abode/test_config_flow.py +++ b/tests/components/abode/test_config_flow.py @@ -12,7 +12,7 @@ from homeassistant.components.abode import config_flow from homeassistant.components.abode.const import CONF_POLLING, DOMAIN -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_USER from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -161,18 +161,15 @@ async def test_step_reauth(hass: HomeAssistant) -> None: """Test the reauth flow.""" conf = {CONF_USERNAME: "user@email.com", CONF_PASSWORD: "password"} - MockConfigEntry( + entry = MockConfigEntry( domain=DOMAIN, unique_id="user@email.com", data=conf, - ).add_to_hass(hass) + ) + entry.add_to_hass(hass) with patch("homeassistant.components.abode.config_flow.Abode"): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH}, - data=conf, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/accuweather/snapshots/test_sensor.ambr b/tests/components/accuweather/snapshots/test_sensor.ambr index 5e28be5a72b632..3468d638bc0c94 100644 --- a/tests/components/accuweather/snapshots/test_sensor.ambr +++ b/tests/components/accuweather/snapshots/test_sensor.ambr @@ -1969,6 +1969,58 @@ 'state': '9.2', }) # --- +# name: test_sensor[sensor.home_humidity-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.home_humidity', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Humidity', + 'platform': 'accuweather', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'humidity', + 'unique_id': '0123456-relativehumidity', + 'unit_of_measurement': '%', + }) +# --- +# name: test_sensor[sensor.home_humidity-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by AccuWeather', + 'device_class': 'humidity', + 'friendly_name': 'Home Humidity', + 'state_class': , + 'unit_of_measurement': '%', + }), + 'context': , + 'entity_id': 'sensor.home_humidity', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '67', + }) +# --- # name: test_sensor[sensor.home_mold_pollen_day_0-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ @@ -2267,6 +2319,61 @@ 'state': '0.0', }) # --- +# name: test_sensor[sensor.home_pressure-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.home_pressure', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 0, + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Pressure', + 'platform': 'accuweather', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'pressure', + 'unique_id': '0123456-pressure', + 'unit_of_measurement': , + }) +# --- +# name: test_sensor[sensor.home_pressure-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by AccuWeather', + 'device_class': 'pressure', + 'friendly_name': 'Home Pressure', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.home_pressure', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '1012.0', + }) +# --- # name: test_sensor[sensor.home_pressure_tendency-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ @@ -4145,6 +4252,58 @@ 'state': '276.1', }) # --- +# name: test_sensor[sensor.home_temperature-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.home_temperature', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Temperature', + 'platform': 'accuweather', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'temperature', + 'unique_id': '0123456-temperature', + 'unit_of_measurement': , + }) +# --- +# name: test_sensor[sensor.home_temperature-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by AccuWeather', + 'device_class': 'temperature', + 'friendly_name': 'Home Temperature', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.home_temperature', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '22.6', + }) +# --- # name: test_sensor[sensor.home_thunderstorm_probability_day_0-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ diff --git a/tests/components/accuweather/test_sensor.py b/tests/components/accuweather/test_sensor.py index 41c1c0d930a24b..37ebe260f394c4 100644 --- a/tests/components/accuweather/test_sensor.py +++ b/tests/components/accuweather/test_sensor.py @@ -148,6 +148,7 @@ async def test_manual_update_entity( assert mock_accuweather_client.async_get_current_conditions.call_count == 2 +@pytest.mark.usefixtures("entity_registry_enabled_by_default") async def test_sensor_imperial_units( hass: HomeAssistant, mock_accuweather_client: AsyncMock ) -> None: diff --git a/tests/components/airgradient/snapshots/test_init.ambr b/tests/components/airgradient/snapshots/test_init.ambr index e47c5b38bbc302..72cb12535f130d 100644 --- a/tests/components/airgradient/snapshots/test_init.ambr +++ b/tests/components/airgradient/snapshots/test_init.ambr @@ -57,7 +57,7 @@ 'name': 'Airgradient', 'name_by_user': None, 'primary_config_entry': , - 'serial_number': '84fce60bec38', + 'serial_number': '84fce612f5b8', 'suggested_area': None, 'sw_version': '3.1.1', 'via_device_id': None, diff --git a/tests/components/airgradient/test_button.py b/tests/components/airgradient/test_button.py index 7901c3a067b395..83de2c2f048398 100644 --- a/tests/components/airgradient/test_button.py +++ b/tests/components/airgradient/test_button.py @@ -7,7 +7,7 @@ from freezegun.api import FrozenDateTimeFactory from syrupy import SnapshotAssertion -from homeassistant.components.airgradient import DOMAIN +from homeassistant.components.airgradient.const import DOMAIN from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS from homeassistant.const import ATTR_ENTITY_ID, Platform from homeassistant.core import HomeAssistant diff --git a/tests/components/airgradient/test_config_flow.py b/tests/components/airgradient/test_config_flow.py index 222ac5d04af107..73dbd17a2133e0 100644 --- a/tests/components/airgradient/test_config_flow.py +++ b/tests/components/airgradient/test_config_flow.py @@ -9,7 +9,7 @@ ConfigurationControl, ) -from homeassistant.components.airgradient import DOMAIN +from homeassistant.components.airgradient.const import DOMAIN from homeassistant.components.zeroconf import ZeroconfServiceInfo from homeassistant.config_entries import SOURCE_USER, SOURCE_ZEROCONF from homeassistant.const import CONF_HOST @@ -253,3 +253,32 @@ async def test_zeroconf_flow_abort_old_firmware(hass: HomeAssistant) -> None: ) assert result["type"] is FlowResultType.ABORT assert result["reason"] == "invalid_version" + + +async def test_user_flow_works_discovery( + hass: HomeAssistant, + mock_new_airgradient_client: AsyncMock, + mock_setup_entry: AsyncMock, +) -> None: + """Test user flow can continue after discovery happened.""" + await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_ZEROCONF}, + data=ZEROCONF_DISCOVERY, + ) + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_USER}, + ) + assert len(hass.config_entries.flow.async_progress(DOMAIN)) == 2 + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {CONF_HOST: "10.0.0.131"}, + ) + assert result["type"] is FlowResultType.CREATE_ENTRY + + # Verify the discovery flow was aborted + assert not hass.config_entries.flow.async_progress(DOMAIN) diff --git a/tests/components/airgradient/test_init.py b/tests/components/airgradient/test_init.py index 408e6f5f3ba94f..a566254d1067ca 100644 --- a/tests/components/airgradient/test_init.py +++ b/tests/components/airgradient/test_init.py @@ -4,7 +4,7 @@ from syrupy import SnapshotAssertion -from homeassistant.components.airgradient import DOMAIN +from homeassistant.components.airgradient.const import DOMAIN from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr diff --git a/tests/components/airgradient/test_number.py b/tests/components/airgradient/test_number.py index 0803c0d437f141..7aabda8f81c730 100644 --- a/tests/components/airgradient/test_number.py +++ b/tests/components/airgradient/test_number.py @@ -7,7 +7,7 @@ from freezegun.api import FrozenDateTimeFactory from syrupy import SnapshotAssertion -from homeassistant.components.airgradient import DOMAIN +from homeassistant.components.airgradient.const import DOMAIN from homeassistant.components.number import ( ATTR_VALUE, DOMAIN as NUMBER_DOMAIN, diff --git a/tests/components/airgradient/test_select.py b/tests/components/airgradient/test_select.py index 61679a15c0721e..de4a7beaaa7f3d 100644 --- a/tests/components/airgradient/test_select.py +++ b/tests/components/airgradient/test_select.py @@ -8,7 +8,7 @@ import pytest from syrupy import SnapshotAssertion -from homeassistant.components.airgradient import DOMAIN +from homeassistant.components.airgradient.const import DOMAIN from homeassistant.components.select import ( DOMAIN as SELECT_DOMAIN, SERVICE_SELECT_OPTION, diff --git a/tests/components/airgradient/test_sensor.py b/tests/components/airgradient/test_sensor.py index c2e53ef4de2b1e..e3fed70839a992 100644 --- a/tests/components/airgradient/test_sensor.py +++ b/tests/components/airgradient/test_sensor.py @@ -8,7 +8,7 @@ import pytest from syrupy import SnapshotAssertion -from homeassistant.components.airgradient import DOMAIN +from homeassistant.components.airgradient.const import DOMAIN from homeassistant.const import STATE_UNAVAILABLE, Platform from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er diff --git a/tests/components/airgradient/test_switch.py b/tests/components/airgradient/test_switch.py index 20a1cb7470bc8c..a0cbdd17d75686 100644 --- a/tests/components/airgradient/test_switch.py +++ b/tests/components/airgradient/test_switch.py @@ -7,7 +7,7 @@ from freezegun.api import FrozenDateTimeFactory from syrupy import SnapshotAssertion -from homeassistant.components.airgradient import DOMAIN +from homeassistant.components.airgradient.const import DOMAIN from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN from homeassistant.const import ( ATTR_ENTITY_ID, diff --git a/tests/components/airvisual/test_config_flow.py b/tests/components/airvisual/test_config_flow.py index b9643b17c079fd..e38fc64587e526 100644 --- a/tests/components/airvisual/test_config_flow.py +++ b/tests/components/airvisual/test_config_flow.py @@ -18,7 +18,7 @@ INTEGRATION_TYPE_GEOGRAPHY_COORDS, INTEGRATION_TYPE_GEOGRAPHY_NAME, ) -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_USER from homeassistant.const import CONF_API_KEY, CONF_SHOW_ON_MAP from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -33,6 +33,8 @@ TEST_STATE, ) +from tests.common import MockConfigEntry + pytestmark = pytest.mark.usefixtures("mock_setup_entry") @@ -146,12 +148,10 @@ async def test_options_flow( async def test_step_reauth( - hass: HomeAssistant, config_entry, setup_config_entry + hass: HomeAssistant, config_entry: MockConfigEntry, setup_config_entry ) -> None: """Test that the reauth step works.""" - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_REAUTH}, data=config_entry.data - ) + result = await config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/airvisual_pro/test_config_flow.py b/tests/components/airvisual_pro/test_config_flow.py index 803a335f52c17d..9298b8cf528115 100644 --- a/tests/components/airvisual_pro/test_config_flow.py +++ b/tests/components/airvisual_pro/test_config_flow.py @@ -10,11 +10,13 @@ import pytest from homeassistant.components.airvisual_pro.const import DOMAIN -from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER from homeassistant.const import CONF_IP_ADDRESS, CONF_PASSWORD from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType +from tests.common import MockConfigEntry + pytestmark = pytest.mark.usefixtures("mock_setup_entry") @@ -98,22 +100,14 @@ async def test_step_import(hass: HomeAssistant, config, setup_airvisual_pro) -> async def test_reauth( hass: HomeAssistant, config, - config_entry, + config_entry: MockConfigEntry, connect_errors, connect_mock, pro, setup_airvisual_pro, ) -> None: """Test re-auth (including errors).""" - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "entry_id": config_entry.entry_id, - "unique_id": config_entry.unique_id, - }, - data=config, - ) + result = await config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/airzone/test_select.py b/tests/components/airzone/test_select.py index 01617eab17546b..343c033728a064 100644 --- a/tests/components/airzone/test_select.py +++ b/tests/components/airzone/test_select.py @@ -2,17 +2,19 @@ from unittest.mock import patch +from aioairzone.common import OperationMode from aioairzone.const import ( API_COLD_ANGLE, API_DATA, API_HEAT_ANGLE, + API_MODE, API_SLEEP, API_SYSTEM_ID, API_ZONE_ID, ) import pytest -from homeassistant.components.select import DOMAIN as SELECT_DOMAIN +from homeassistant.components.select import ATTR_OPTIONS, DOMAIN as SELECT_DOMAIN from homeassistant.const import ATTR_ENTITY_ID, ATTR_OPTION, SERVICE_SELECT_OPTION from homeassistant.core import HomeAssistant from homeassistant.exceptions import ServiceValidationError @@ -31,6 +33,9 @@ async def test_airzone_create_selects(hass: HomeAssistant) -> None: state = hass.states.get("select.despacho_heat_angle") assert state.state == "90deg" + state = hass.states.get("select.despacho_mode") + assert state is None + state = hass.states.get("select.despacho_sleep") assert state.state == "off" @@ -40,6 +45,9 @@ async def test_airzone_create_selects(hass: HomeAssistant) -> None: state = hass.states.get("select.dorm_1_heat_angle") assert state.state == "90deg" + state = hass.states.get("select.dorm_1_mode") + assert state is None + state = hass.states.get("select.dorm_1_sleep") assert state.state == "off" @@ -49,6 +57,9 @@ async def test_airzone_create_selects(hass: HomeAssistant) -> None: state = hass.states.get("select.dorm_2_heat_angle") assert state.state == "90deg" + state = hass.states.get("select.dorm_2_mode") + assert state is None + state = hass.states.get("select.dorm_2_sleep") assert state.state == "off" @@ -58,6 +69,9 @@ async def test_airzone_create_selects(hass: HomeAssistant) -> None: state = hass.states.get("select.dorm_ppal_heat_angle") assert state.state == "50deg" + state = hass.states.get("select.dorm_ppal_mode") + assert state is None + state = hass.states.get("select.dorm_ppal_sleep") assert state.state == "30m" @@ -67,6 +81,16 @@ async def test_airzone_create_selects(hass: HomeAssistant) -> None: state = hass.states.get("select.salon_heat_angle") assert state.state == "90deg" + state = hass.states.get("select.salon_mode") + assert state.state == "heat" + assert state.attributes.get(ATTR_OPTIONS) == [ + "cool", + "dry", + "fan", + "heat", + "stop", + ] + state = hass.states.get("select.salon_sleep") assert state.state == "off" @@ -115,6 +139,50 @@ async def test_airzone_select_sleep(hass: HomeAssistant) -> None: assert state.state == "30m" +async def test_airzone_select_mode(hass: HomeAssistant) -> None: + """Test select HVAC mode.""" + + await async_init_integration(hass) + + put_hvac_mode = { + API_DATA: [ + { + API_SYSTEM_ID: 1, + API_ZONE_ID: 1, + API_MODE: OperationMode.COOLING, + } + ] + } + + with pytest.raises(ServiceValidationError): + await hass.services.async_call( + SELECT_DOMAIN, + SERVICE_SELECT_OPTION, + { + ATTR_ENTITY_ID: "select.salon_mode", + ATTR_OPTION: "Invalid", + }, + blocking=True, + ) + + with patch( + "homeassistant.components.airzone.AirzoneLocalApi.put_hvac", + return_value=put_hvac_mode, + ): + await hass.services.async_call( + SELECT_DOMAIN, + SERVICE_SELECT_OPTION, + { + ATTR_ENTITY_ID: "select.salon_mode", + ATTR_OPTION: "cool", + }, + blocking=True, + ) + + state = hass.states.get("select.salon_mode") + assert state.state == "cool" + + async def test_airzone_select_grille_angle(hass: HomeAssistant) -> None: """Test select sleep.""" diff --git a/tests/components/airzone_cloud/snapshots/test_diagnostics.ambr b/tests/components/airzone_cloud/snapshots/test_diagnostics.ambr index 26a606bde4235b..2e6463d35a154e 100644 --- a/tests/components/airzone_cloud/snapshots/test_diagnostics.ambr +++ b/tests/components/airzone_cloud/snapshots/test_diagnostics.ambr @@ -154,6 +154,9 @@ 'available': True, 'double-set-point': True, 'id': 'aidoo_pro', + 'indoor-exchanger-temperature': 26.0, + 'indoor-return-temperature': 26.0, + 'indoor-work-temperature': 25.0, 'installation': 'installation1', 'is-connected': True, 'mode': 2, @@ -166,6 +169,12 @@ 5, ]), 'name': 'Bron Pro', + 'outdoor-condenser-pressure': 150.0, + 'outdoor-discharge-temperature': 121.0, + 'outdoor-electric-current': 3.0, + 'outdoor-evaporator-pressure': 20.0, + 'outdoor-exchanger-temperature': -25.0, + 'outdoor-temperature': 29.0, 'power': True, 'problems': False, 'speed': 3, diff --git a/tests/components/airzone_cloud/test_sensor.py b/tests/components/airzone_cloud/test_sensor.py index cf291ec23a6bd9..672e10adedbf9c 100644 --- a/tests/components/airzone_cloud/test_sensor.py +++ b/tests/components/airzone_cloud/test_sensor.py @@ -20,6 +20,33 @@ async def test_airzone_create_sensors(hass: HomeAssistant) -> None: state = hass.states.get("sensor.bron_pro_temperature") assert state.state == "20.0" + state = hass.states.get("sensor.bron_pro_indoor_exchanger_temperature") + assert state.state == "26.0" + + state = hass.states.get("sensor.bron_pro_indoor_return_temperature") + assert state.state == "26.0" + + state = hass.states.get("sensor.bron_pro_indoor_working_temperature") + assert state.state == "25.0" + + state = hass.states.get("sensor.bron_pro_outdoor_condenser_pressure") + assert state.state == "150.0" + + state = hass.states.get("sensor.bron_pro_outdoor_discharge_temperature") + assert state.state == "121.0" + + state = hass.states.get("sensor.bron_pro_outdoor_electric_current") + assert state.state == "3.0" + + state = hass.states.get("sensor.bron_pro_outdoor_evaporator_pressure") + assert state.state == "20.0" + + state = hass.states.get("sensor.bron_pro_outdoor_exchanger_temperature") + assert state.state == "-25.0" + + state = hass.states.get("sensor.bron_pro_outdoor_temperature") + assert state.state == "29.0" + # WebServers state = hass.states.get("sensor.webserver_11_22_33_44_55_66_cpu_usage") assert state.state == "32" diff --git a/tests/components/airzone_cloud/util.py b/tests/components/airzone_cloud/util.py index fb538ea7c8e897..52b0ae0bec3d2c 100644 --- a/tests/components/airzone_cloud/util.py +++ b/tests/components/airzone_cloud/util.py @@ -24,12 +24,17 @@ API_CELSIUS, API_CONFIG, API_CONNECTION_DATE, + API_CONSUMPTION_UE, API_CPU_WS, API_DEVICE_ID, API_DEVICES, + API_DISCH_COMP_TEMP_UE, API_DISCONNECTION_DATE, API_DOUBLE_SET_POINT, API_ERRORS, + API_EXCH_HEAT_TEMP_IU, + API_EXCH_HEAT_TEMP_UE, + API_EXT_TEMP, API_FAH, API_FREE, API_FREE_MEM, @@ -46,6 +51,8 @@ API_MODE_AVAIL, API_NAME, API_OLD_ID, + API_PC_UE, + API_PE_UE, API_POWER, API_POWERFUL_MODE, API_RAD_ACTIVE, @@ -69,6 +76,7 @@ API_RANGE_SP_MIN_HOT_AIR, API_RANGE_SP_MIN_STOP_AIR, API_RANGE_SP_MIN_VENT_AIR, + API_RETURN_TEMP, API_SETPOINT, API_SP_AIR_AUTO, API_SP_AIR_COOL, @@ -94,6 +102,7 @@ API_THERMOSTAT_TYPE, API_TYPE, API_WARNINGS, + API_WORK_TEMP, API_WS_CONNECTED, API_WS_FW, API_WS_ID, @@ -266,6 +275,18 @@ def mock_get_device_config(device: Device) -> dict[str, Any]: """Mock API device config.""" + if device.get_id() == "aidoo_pro": + return { + API_CONSUMPTION_UE: 3, + API_DISCH_COMP_TEMP_UE: {API_CELSIUS: 121, API_FAH: -250}, + API_EXCH_HEAT_TEMP_IU: {API_CELSIUS: 26, API_FAH: 79}, + API_EXCH_HEAT_TEMP_UE: {API_CELSIUS: -25, API_FAH: -13}, + API_EXT_TEMP: {API_CELSIUS: 29, API_FAH: 84}, + API_PC_UE: 0.15, + API_PE_UE: 0.02, + API_RETURN_TEMP: {API_CELSIUS: 26, API_FAH: 79}, + API_WORK_TEMP: {API_CELSIUS: 25, API_FAH: 77}, + } if device.get_id() == "system1": return { API_SYSTEM_FW: "3.35", diff --git a/tests/components/alexa/test_capabilities.py b/tests/components/alexa/test_capabilities.py index 162149f095b6a3..b56d8054d7b972 100644 --- a/tests/components/alexa/test_capabilities.py +++ b/tests/components/alexa/test_capabilities.py @@ -70,6 +70,7 @@ async def test_discovery_remote( { "current_activity": current_activity, "activity_list": activity_list, + "supported_features": 4, }, ) msg = await smart_home.async_handle_message(hass, get_default_config(hass), request) @@ -790,22 +791,37 @@ async def test_report_remote_activity(hass: HomeAssistant) -> None: hass.states.async_set( "remote.unknown", "on", - {"current_activity": "UNKNOWN"}, + { + "current_activity": "UNKNOWN", + "supported_features": 4, + }, ) hass.states.async_set( "remote.tv", "on", - {"current_activity": "TV", "activity_list": ["TV", "MUSIC", "DVD"]}, + { + "current_activity": "TV", + "activity_list": ["TV", "MUSIC", "DVD"], + "supported_features": 4, + }, ) hass.states.async_set( "remote.music", "on", - {"current_activity": "MUSIC", "activity_list": ["TV", "MUSIC", "DVD"]}, + { + "current_activity": "MUSIC", + "activity_list": ["TV", "MUSIC", "DVD"], + "supported_features": 4, + }, ) hass.states.async_set( "remote.dvd", "on", - {"current_activity": "DVD", "activity_list": ["TV", "MUSIC", "DVD"]}, + { + "current_activity": "DVD", + "activity_list": ["TV", "MUSIC", "DVD"], + "supported_features": 4, + }, ) properties = await reported_properties(hass, "remote#unknown") diff --git a/tests/components/androidtv_remote/test_media_player.py b/tests/components/androidtv_remote/test_media_player.py index 46678f18fd325f..e292a5b273f411 100644 --- a/tests/components/androidtv_remote/test_media_player.py +++ b/tests/components/androidtv_remote/test_media_player.py @@ -20,10 +20,11 @@ async def test_media_player_receives_push_updates( hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_api: MagicMock ) -> None: """Test the Android TV Remote media player receives push updates and state is updated.""" - mock_config_entry.options = { - "apps": {"com.google.android.youtube.tv": {"app_name": "YouTube"}} - } mock_config_entry.add_to_hass(hass) + hass.config_entries.async_update_entry( + mock_config_entry, + options={"apps": {"com.google.android.youtube.tv": {"app_name": "YouTube"}}}, + ) await hass.config_entries.async_setup(mock_config_entry.entry_id) assert mock_config_entry.state is ConfigEntryState.LOADED @@ -322,7 +323,7 @@ async def test_browse_media( mock_api: MagicMock, ) -> None: """Test the Android TV Remote media player browse media.""" - mock_config_entry.options = { + new_options = { "apps": { "com.google.android.youtube.tv": { "app_name": "YouTube", @@ -332,6 +333,7 @@ async def test_browse_media( } } mock_config_entry.add_to_hass(hass) + hass.config_entries.async_update_entry(mock_config_entry, options=new_options) await hass.config_entries.async_setup(mock_config_entry.entry_id) assert mock_config_entry.state is ConfigEntryState.LOADED diff --git a/tests/components/androidtv_remote/test_remote.py b/tests/components/androidtv_remote/test_remote.py index 7ca63685747c44..b3c3ce1c2834d7 100644 --- a/tests/components/androidtv_remote/test_remote.py +++ b/tests/components/androidtv_remote/test_remote.py @@ -19,10 +19,9 @@ async def test_remote_receives_push_updates( hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_api: MagicMock ) -> None: """Test the Android TV Remote receives push updates and state is updated.""" - mock_config_entry.options = { - "apps": {"com.google.android.youtube.tv": {"app_name": "YouTube"}} - } + new_options = {"apps": {"com.google.android.youtube.tv": {"app_name": "YouTube"}}} mock_config_entry.add_to_hass(hass) + hass.config_entries.async_update_entry(mock_config_entry, options=new_options) await hass.config_entries.async_setup(mock_config_entry.entry_id) assert mock_config_entry.state is ConfigEntryState.LOADED @@ -53,10 +52,9 @@ async def test_remote_toggles( hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_api: MagicMock ) -> None: """Test the Android TV Remote toggles.""" - mock_config_entry.options = { - "apps": {"com.google.android.youtube.tv": {"app_name": "YouTube"}} - } + new_options = {"apps": {"com.google.android.youtube.tv": {"app_name": "YouTube"}}} mock_config_entry.add_to_hass(hass) + hass.config_entries.async_update_entry(mock_config_entry, options=new_options) await hass.config_entries.async_setup(mock_config_entry.entry_id) assert mock_config_entry.state is ConfigEntryState.LOADED diff --git a/tests/components/aquacell/test_config_flow.py b/tests/components/aquacell/test_config_flow.py index b73852d513f1b5..f677b3f8348fe8 100644 --- a/tests/components/aquacell/test_config_flow.py +++ b/tests/components/aquacell/test_config_flow.py @@ -79,6 +79,7 @@ async def test_full_flow( ("exception", "error"), [ (ApiException, "cannot_connect"), + (TimeoutError, "cannot_connect"), (AuthenticationFailed, "invalid_auth"), (Exception, "unknown"), ], diff --git a/tests/components/aranet/__init__.py b/tests/components/aranet/__init__.py index 18bebfb44a47c7..711c605fd28a3d 100644 --- a/tests/components/aranet/__init__.py +++ b/tests/components/aranet/__init__.py @@ -82,3 +82,11 @@ def fake_service_info(name, service_uuid, manufacturer_data): 1794: b"\x02!&\x04\x01\x00`-\x00\x00\x08\x98\x05\x00n\x00\x00d\x00,\x01\xfd\x00\xc7" }, ) + +VALID_ARANET_RADON_DATA_SERVICE_INFO = fake_service_info( + "AranetRn+ 12345", + "0000fce0-0000-1000-8000-00805f9b34fb", + { + 1794: b"\x03!\x04\x06\x01\x00\x00\x00\x07\x00\xfe\x01\xc9'\xce\x01\x00d\x01X\x02\xf6\x01\x08" + }, +) diff --git a/tests/components/aranet/test_sensor.py b/tests/components/aranet/test_sensor.py index c932a92c1e8bd5..7bd00af4837aee 100644 --- a/tests/components/aranet/test_sensor.py +++ b/tests/components/aranet/test_sensor.py @@ -11,6 +11,7 @@ DISABLED_INTEGRATIONS_SERVICE_INFO, VALID_ARANET2_DATA_SERVICE_INFO, VALID_ARANET_RADIATION_DATA_SERVICE_INFO, + VALID_ARANET_RADON_DATA_SERVICE_INFO, VALID_DATA_SERVICE_INFO, ) @@ -188,6 +189,71 @@ async def test_sensors_aranet4(hass: HomeAssistant) -> None: await hass.async_block_till_done() +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_sensors_aranetrn(hass: HomeAssistant) -> None: + """Test setting up creates the sensors for Aranet Radon device.""" + entry = MockConfigEntry( + domain=DOMAIN, + unique_id="aa:bb:cc:dd:ee:ff", + ) + entry.add_to_hass(hass) + + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + assert len(hass.states.async_all("sensor")) == 0 + inject_bluetooth_service_info(hass, VALID_ARANET_RADON_DATA_SERVICE_INFO) + await hass.async_block_till_done() + assert len(hass.states.async_all("sensor")) == 6 + + batt_sensor = hass.states.get("sensor.aranetrn_12345_battery") + batt_sensor_attrs = batt_sensor.attributes + assert batt_sensor.state == "100" + assert batt_sensor_attrs[ATTR_FRIENDLY_NAME] == "AranetRn+ 12345 Battery" + assert batt_sensor_attrs[ATTR_UNIT_OF_MEASUREMENT] == "%" + assert batt_sensor_attrs[ATTR_STATE_CLASS] == "measurement" + + co2_sensor = hass.states.get("sensor.aranetrn_12345_radon_concentration") + co2_sensor_attrs = co2_sensor.attributes + assert co2_sensor.state == "7" + assert co2_sensor_attrs[ATTR_FRIENDLY_NAME] == "AranetRn+ 12345 Radon Concentration" + assert co2_sensor_attrs[ATTR_UNIT_OF_MEASUREMENT] == "Bq/m³" + assert co2_sensor_attrs[ATTR_STATE_CLASS] == "measurement" + + humid_sensor = hass.states.get("sensor.aranetrn_12345_humidity") + humid_sensor_attrs = humid_sensor.attributes + assert humid_sensor.state == "46.2" + assert humid_sensor_attrs[ATTR_FRIENDLY_NAME] == "AranetRn+ 12345 Humidity" + assert humid_sensor_attrs[ATTR_UNIT_OF_MEASUREMENT] == "%" + assert humid_sensor_attrs[ATTR_STATE_CLASS] == "measurement" + + temp_sensor = hass.states.get("sensor.aranetrn_12345_temperature") + temp_sensor_attrs = temp_sensor.attributes + assert temp_sensor.state == "25.5" + assert temp_sensor_attrs[ATTR_FRIENDLY_NAME] == "AranetRn+ 12345 Temperature" + assert temp_sensor_attrs[ATTR_UNIT_OF_MEASUREMENT] == "°C" + assert temp_sensor_attrs[ATTR_STATE_CLASS] == "measurement" + + press_sensor = hass.states.get("sensor.aranetrn_12345_pressure") + press_sensor_attrs = press_sensor.attributes + assert press_sensor.state == "1018.5" + assert press_sensor_attrs[ATTR_FRIENDLY_NAME] == "AranetRn+ 12345 Pressure" + assert press_sensor_attrs[ATTR_UNIT_OF_MEASUREMENT] == "hPa" + assert press_sensor_attrs[ATTR_STATE_CLASS] == "measurement" + + interval_sensor = hass.states.get("sensor.aranetrn_12345_update_interval") + interval_sensor_attrs = interval_sensor.attributes + assert interval_sensor.state == "600" + assert ( + interval_sensor_attrs[ATTR_FRIENDLY_NAME] == "AranetRn+ 12345 Update Interval" + ) + assert interval_sensor_attrs[ATTR_UNIT_OF_MEASUREMENT] == "s" + assert interval_sensor_attrs[ATTR_STATE_CLASS] == "measurement" + + assert await hass.config_entries.async_unload(entry.entry_id) + await hass.async_block_till_done() + + @pytest.mark.usefixtures("entity_registry_enabled_by_default") async def test_smart_home_integration_disabled(hass: HomeAssistant) -> None: """Test disabling smart home integration marks entities as unavailable.""" diff --git a/tests/components/aseko_pool_live/test_config_flow.py b/tests/components/aseko_pool_live/test_config_flow.py index 4307e527cee987..e4dedf36da471b 100644 --- a/tests/components/aseko_pool_live/test_config_flow.py +++ b/tests/components/aseko_pool_live/test_config_flow.py @@ -133,13 +133,7 @@ async def test_async_step_reauth_success(hass: HomeAssistant) -> None: ) mock_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": mock_entry.entry_id, - }, - ) + result = await mock_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -179,13 +173,7 @@ async def test_async_step_reauth_exception( ) mock_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": mock_entry.entry_id, - }, - ) + result = await mock_entry.start_reauth_flow(hass) with patch( "homeassistant.components.aseko_pool_live.config_flow.WebAccount.login", diff --git a/tests/components/assist_pipeline/conftest.py b/tests/components/assist_pipeline/conftest.py index b7bf83a7ed0a0c..0f6872edbfedf3 100644 --- a/tests/components/assist_pipeline/conftest.py +++ b/tests/components/assist_pipeline/conftest.py @@ -23,7 +23,7 @@ ) from homeassistant.config_entries import ConfigEntry, ConfigFlow from homeassistant.const import Platform -from homeassistant.core import HomeAssistant, callback +from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.setup import async_setup_component @@ -36,6 +36,8 @@ mock_integration, mock_platform, ) +from tests.components.stt.common import MockSTTProvider, MockSTTProviderEntity +from tests.components.tts.common import MockTTSProvider _TRANSCRIPT = "test transcript" @@ -47,107 +49,6 @@ def mock_tts_cache_dir_autouse(mock_tts_cache_dir: Path) -> None: """Mock the TTS cache dir with empty dir.""" -class BaseProvider: - """Mock STT provider.""" - - _supported_languages = ["en-US"] - - def __init__(self, text: str) -> None: - """Init test provider.""" - self.text = text - self.received: list[bytes] = [] - - @property - def supported_languages(self) -> list[str]: - """Return a list of supported languages.""" - return self._supported_languages - - @property - def supported_formats(self) -> list[stt.AudioFormats]: - """Return a list of supported formats.""" - return [stt.AudioFormats.WAV] - - @property - def supported_codecs(self) -> list[stt.AudioCodecs]: - """Return a list of supported codecs.""" - return [stt.AudioCodecs.PCM] - - @property - def supported_bit_rates(self) -> list[stt.AudioBitRates]: - """Return a list of supported bitrates.""" - return [stt.AudioBitRates.BITRATE_16] - - @property - def supported_sample_rates(self) -> list[stt.AudioSampleRates]: - """Return a list of supported samplerates.""" - return [stt.AudioSampleRates.SAMPLERATE_16000] - - @property - def supported_channels(self) -> list[stt.AudioChannels]: - """Return a list of supported channels.""" - return [stt.AudioChannels.CHANNEL_MONO] - - async def async_process_audio_stream( - self, metadata: stt.SpeechMetadata, stream: AsyncIterable[bytes] - ) -> stt.SpeechResult: - """Process an audio stream.""" - async for data in stream: - if not data: - break - self.received.append(data) - return stt.SpeechResult(self.text, stt.SpeechResultState.SUCCESS) - - -class MockSttProvider(BaseProvider, stt.Provider): - """Mock provider.""" - - -class MockSttProviderEntity(BaseProvider, stt.SpeechToTextEntity): - """Mock provider entity.""" - - _attr_name = "Mock STT" - - -class MockTTSProvider(tts.Provider): - """Mock TTS provider.""" - - name = "Test" - _supported_languages = ["en-US"] - _supported_voices = { - "en-US": [ - tts.Voice("james_earl_jones", "James Earl Jones"), - tts.Voice("fran_drescher", "Fran Drescher"), - ] - } - _supported_options = ["voice", "age", tts.ATTR_AUDIO_OUTPUT] - - @property - def default_language(self) -> str: - """Return the default language.""" - return "en" - - @property - def supported_languages(self) -> list[str]: - """Return list of supported languages.""" - return self._supported_languages - - @callback - def async_get_supported_voices(self, language: str) -> list[tts.Voice] | None: - """Return a list of supported voices for a language.""" - return self._supported_voices.get(language) - - @property - def supported_options(self) -> list[str]: - """Return list of supported options like voice, emotions.""" - return self._supported_options - - def get_tts_audio( - self, message: str, language: str, options: dict[str, Any] - ) -> tts.TtsAudioType: - """Load TTS data.""" - return ("mp3", b"") - - class MockTTSPlatform(MockPlatform): """A mock TTS platform.""" @@ -162,19 +63,23 @@ def __init__(self, *, async_get_engine, **kwargs: Any) -> None: @pytest.fixture async def mock_tts_provider() -> MockTTSProvider: """Mock TTS provider.""" - return MockTTSProvider() + provider = MockTTSProvider("en") + provider._supported_languages = ["en-US"] + return provider @pytest.fixture -async def mock_stt_provider() -> MockSttProvider: +async def mock_stt_provider() -> MockSTTProvider: """Mock STT provider.""" - return MockSttProvider(_TRANSCRIPT) + return MockSTTProvider(supported_languages=["en-US"], text=_TRANSCRIPT) @pytest.fixture -def mock_stt_provider_entity() -> MockSttProviderEntity: +def mock_stt_provider_entity() -> MockSTTProviderEntity: """Test provider entity fixture.""" - return MockSttProviderEntity(_TRANSCRIPT) + entity = MockSTTProviderEntity(supported_languages=["en-US"], text=_TRANSCRIPT) + entity._attr_name = "Mock STT" + return entity class MockSttPlatform(MockPlatform): @@ -290,8 +195,8 @@ def config_flow_fixture(hass: HomeAssistant) -> Generator[None]: @pytest.fixture async def init_supporting_components( hass: HomeAssistant, - mock_stt_provider: MockSttProvider, - mock_stt_provider_entity: MockSttProviderEntity, + mock_stt_provider: MockSTTProvider, + mock_stt_provider_entity: MockSTTProviderEntity, mock_tts_provider: MockTTSProvider, mock_wake_word_provider_entity: MockWakeWordEntity, mock_wake_word_provider_entity2: MockWakeWordEntity2, diff --git a/tests/components/assist_pipeline/snapshots/test_init.ambr b/tests/components/assist_pipeline/snapshots/test_init.ambr index 8124ed4ab85813..7f29534e473428 100644 --- a/tests/components/assist_pipeline/snapshots/test_init.ambr +++ b/tests/components/assist_pipeline/snapshots/test_init.ambr @@ -10,7 +10,7 @@ }), dict({ 'data': dict({ - 'engine': 'test', + 'engine': 'stt.mock_stt', 'metadata': dict({ 'bit_rate': , 'channel': , @@ -301,7 +301,7 @@ }), dict({ 'data': dict({ - 'engine': 'test', + 'engine': 'stt.mock_stt', 'metadata': dict({ 'bit_rate': , 'channel': , diff --git a/tests/components/assist_pipeline/snapshots/test_websocket.ambr b/tests/components/assist_pipeline/snapshots/test_websocket.ambr index fb1ca6db121fa5..7ea6af7e0bdf50 100644 --- a/tests/components/assist_pipeline/snapshots/test_websocket.ambr +++ b/tests/components/assist_pipeline/snapshots/test_websocket.ambr @@ -11,7 +11,7 @@ # --- # name: test_audio_pipeline.1 dict({ - 'engine': 'test', + 'engine': 'stt.mock_stt', 'metadata': dict({ 'bit_rate': 16, 'channel': 1, @@ -92,7 +92,7 @@ # --- # name: test_audio_pipeline_debug.1 dict({ - 'engine': 'test', + 'engine': 'stt.mock_stt', 'metadata': dict({ 'bit_rate': 16, 'channel': 1, @@ -185,7 +185,7 @@ # --- # name: test_audio_pipeline_with_enhancements.1 dict({ - 'engine': 'test', + 'engine': 'stt.mock_stt', 'metadata': dict({ 'bit_rate': 16, 'channel': 1, @@ -288,7 +288,7 @@ # --- # name: test_audio_pipeline_with_wake_word_no_timeout.3 dict({ - 'engine': 'test', + 'engine': 'stt.mock_stt', 'metadata': dict({ 'bit_rate': 16, 'channel': 1, @@ -401,7 +401,7 @@ # --- # name: test_device_capture.1 dict({ - 'engine': 'test', + 'engine': 'stt.mock_stt', 'metadata': dict({ 'bit_rate': 16, 'channel': 1, @@ -427,7 +427,7 @@ # --- # name: test_device_capture_override.1 dict({ - 'engine': 'test', + 'engine': 'stt.mock_stt', 'metadata': dict({ 'bit_rate': 16, 'channel': 1, @@ -475,7 +475,7 @@ # --- # name: test_device_capture_queue_full.1 dict({ - 'engine': 'test', + 'engine': 'stt.mock_stt', 'metadata': dict({ 'bit_rate': 16, 'channel': 1, @@ -649,7 +649,7 @@ # --- # name: test_stt_stream_failed.1 dict({ - 'engine': 'test', + 'engine': 'stt.mock_stt', 'metadata': dict({ 'bit_rate': 16, 'channel': 1, diff --git a/tests/components/assist_pipeline/test_init.py b/tests/components/assist_pipeline/test_init.py index 4206a288331f57..c4696573bade3a 100644 --- a/tests/components/assist_pipeline/test_init.py +++ b/tests/components/assist_pipeline/test_init.py @@ -22,8 +22,8 @@ from .conftest import ( BYTES_ONE_SECOND, - MockSttProvider, - MockSttProviderEntity, + MockSTTProvider, + MockSTTProviderEntity, MockTTSProvider, MockWakeWordEntity, make_10ms_chunk, @@ -47,7 +47,7 @@ def process_events(events: list[assist_pipeline.PipelineEvent]) -> list[dict]: async def test_pipeline_from_audio_stream_auto( hass: HomeAssistant, - mock_stt_provider: MockSttProvider, + mock_stt_provider_entity: MockSTTProviderEntity, init_components, snapshot: SnapshotAssertion, ) -> None: @@ -80,15 +80,15 @@ async def audio_data(): ) assert process_events(events) == snapshot - assert len(mock_stt_provider.received) == 2 - assert mock_stt_provider.received[0].startswith(b"part1") - assert mock_stt_provider.received[1].startswith(b"part2") + assert len(mock_stt_provider_entity.received) == 2 + assert mock_stt_provider_entity.received[0].startswith(b"part1") + assert mock_stt_provider_entity.received[1].startswith(b"part2") async def test_pipeline_from_audio_stream_legacy( hass: HomeAssistant, hass_ws_client: WebSocketGenerator, - mock_stt_provider: MockSttProvider, + mock_stt_provider: MockSTTProvider, init_components, snapshot: SnapshotAssertion, ) -> None: @@ -153,7 +153,7 @@ async def audio_data(): async def test_pipeline_from_audio_stream_entity( hass: HomeAssistant, hass_ws_client: WebSocketGenerator, - mock_stt_provider_entity: MockSttProviderEntity, + mock_stt_provider_entity: MockSTTProviderEntity, init_components, snapshot: SnapshotAssertion, ) -> None: @@ -218,7 +218,7 @@ async def audio_data(): async def test_pipeline_from_audio_stream_no_stt( hass: HomeAssistant, hass_ws_client: WebSocketGenerator, - mock_stt_provider: MockSttProvider, + mock_stt_provider: MockSTTProvider, init_components, snapshot: SnapshotAssertion, ) -> None: @@ -281,7 +281,7 @@ async def audio_data(): async def test_pipeline_from_audio_stream_unknown_pipeline( hass: HomeAssistant, hass_ws_client: WebSocketGenerator, - mock_stt_provider: MockSttProvider, + mock_stt_provider: MockSTTProvider, init_components, snapshot: SnapshotAssertion, ) -> None: @@ -319,7 +319,7 @@ async def audio_data(): async def test_pipeline_from_audio_stream_wake_word( hass: HomeAssistant, - mock_stt_provider: MockSttProvider, + mock_stt_provider_entity: MockSTTProviderEntity, mock_wake_word_provider_entity: MockWakeWordEntity, init_components, snapshot: SnapshotAssertion, @@ -381,21 +381,21 @@ async def audio_data(): # 2. queued audio (from mock wake word entity) # 3. part1 # 4. part2 - assert len(mock_stt_provider.received) > 3 + assert len(mock_stt_provider_entity.received) > 3 first_chunk = bytes( - [c_byte for c in mock_stt_provider.received[:-3] for c_byte in c] + [c_byte for c in mock_stt_provider_entity.received[:-3] for c_byte in c] ) assert first_chunk == wake_chunk_1[len(wake_chunk_1) // 2 :] + wake_chunk_2 - assert mock_stt_provider.received[-3] == b"queued audio" - assert mock_stt_provider.received[-2].startswith(b"part1") - assert mock_stt_provider.received[-1].startswith(b"part2") + assert mock_stt_provider_entity.received[-3] == b"queued audio" + assert mock_stt_provider_entity.received[-2].startswith(b"part1") + assert mock_stt_provider_entity.received[-1].startswith(b"part2") async def test_pipeline_save_audio( hass: HomeAssistant, - mock_stt_provider: MockSttProvider, + mock_stt_provider: MockSTTProvider, mock_wake_word_provider_entity: MockWakeWordEntity, init_supporting_components, snapshot: SnapshotAssertion, @@ -474,7 +474,7 @@ async def audio_data(): async def test_pipeline_saved_audio_with_device_id( hass: HomeAssistant, - mock_stt_provider: MockSttProvider, + mock_stt_provider: MockSTTProvider, mock_wake_word_provider_entity: MockWakeWordEntity, init_supporting_components, snapshot: SnapshotAssertion, @@ -529,7 +529,7 @@ async def audio_data(): async def test_pipeline_saved_audio_write_error( hass: HomeAssistant, - mock_stt_provider: MockSttProvider, + mock_stt_provider: MockSTTProvider, mock_wake_word_provider_entity: MockWakeWordEntity, init_supporting_components, snapshot: SnapshotAssertion, @@ -578,7 +578,7 @@ async def audio_data(): async def test_pipeline_saved_audio_empty_queue( hass: HomeAssistant, - mock_stt_provider: MockSttProvider, + mock_stt_provider: MockSTTProvider, mock_wake_word_provider_entity: MockWakeWordEntity, init_supporting_components, snapshot: SnapshotAssertion, @@ -641,7 +641,7 @@ def proc_wrapper(run_recording_dir, queue): async def test_wake_word_detection_aborted( hass: HomeAssistant, - mock_stt_provider: MockSttProvider, + mock_stt_provider: MockSTTProvider, mock_wake_word_provider_entity: MockWakeWordEntity, init_components, pipeline_data: assist_pipeline.pipeline.PipelineData, @@ -788,13 +788,12 @@ async def test_tts_audio_output( assert len(extra_options) == 0, extra_options -async def test_tts_supports_preferred_format( +async def test_tts_wav_preferred_format( hass: HomeAssistant, hass_client: ClientSessionGenerator, mock_tts_provider: MockTTSProvider, init_components, pipeline_data: assist_pipeline.pipeline.PipelineData, - snapshot: SnapshotAssertion, ) -> None: """Test that preferred format options are given to the TTS system if supported.""" client = await hass_client() @@ -829,6 +828,80 @@ async def test_tts_supports_preferred_format( tts.ATTR_PREFERRED_FORMAT, tts.ATTR_PREFERRED_SAMPLE_RATE, tts.ATTR_PREFERRED_SAMPLE_CHANNELS, + tts.ATTR_PREFERRED_SAMPLE_BYTES, + ] + ) + + with ( + patch.object(mock_tts_provider, "_supported_options", supported_options), + patch.object(mock_tts_provider, "get_tts_audio") as mock_get_tts_audio, + ): + await pipeline_input.execute() + + for event in events: + if event.type == assist_pipeline.PipelineEventType.TTS_END: + # We must fetch the media URL to trigger the TTS + assert event.data + media_id = event.data["tts_output"]["media_id"] + resolved = await media_source.async_resolve_media(hass, media_id, None) + await client.get(resolved.url) + + assert mock_get_tts_audio.called + options = mock_get_tts_audio.call_args_list[0].kwargs["options"] + + # We should have received preferred format options in get_tts_audio + assert options.get(tts.ATTR_PREFERRED_FORMAT) == "wav" + assert int(options.get(tts.ATTR_PREFERRED_SAMPLE_RATE)) == 16000 + assert int(options.get(tts.ATTR_PREFERRED_SAMPLE_CHANNELS)) == 1 + assert int(options.get(tts.ATTR_PREFERRED_SAMPLE_BYTES)) == 2 + + +async def test_tts_dict_preferred_format( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + mock_tts_provider: MockTTSProvider, + init_components, + pipeline_data: assist_pipeline.pipeline.PipelineData, +) -> None: + """Test that preferred format options are given to the TTS system if supported.""" + client = await hass_client() + assert await async_setup_component(hass, media_source.DOMAIN, {}) + + events: list[assist_pipeline.PipelineEvent] = [] + + pipeline_store = pipeline_data.pipeline_store + pipeline_id = pipeline_store.async_get_preferred_item() + pipeline = assist_pipeline.pipeline.async_get_pipeline(hass, pipeline_id) + + pipeline_input = assist_pipeline.pipeline.PipelineInput( + tts_input="This is a test.", + conversation_id=None, + device_id=None, + run=assist_pipeline.pipeline.PipelineRun( + hass, + context=Context(), + pipeline=pipeline, + start_stage=assist_pipeline.PipelineStage.TTS, + end_stage=assist_pipeline.PipelineStage.TTS, + event_callback=events.append, + tts_audio_output={ + tts.ATTR_PREFERRED_FORMAT: "flac", + tts.ATTR_PREFERRED_SAMPLE_RATE: 48000, + tts.ATTR_PREFERRED_SAMPLE_CHANNELS: 2, + tts.ATTR_PREFERRED_SAMPLE_BYTES: 2, + }, + ), + ) + await pipeline_input.validate() + + # Make the TTS provider support preferred format options + supported_options = list(mock_tts_provider.supported_options or []) + supported_options.extend( + [ + tts.ATTR_PREFERRED_FORMAT, + tts.ATTR_PREFERRED_SAMPLE_RATE, + tts.ATTR_PREFERRED_SAMPLE_CHANNELS, + tts.ATTR_PREFERRED_SAMPLE_BYTES, ] ) @@ -850,6 +923,7 @@ async def test_tts_supports_preferred_format( options = mock_get_tts_audio.call_args_list[0].kwargs["options"] # We should have received preferred format options in get_tts_audio - assert tts.ATTR_PREFERRED_FORMAT in options - assert tts.ATTR_PREFERRED_SAMPLE_RATE in options - assert tts.ATTR_PREFERRED_SAMPLE_CHANNELS in options + assert options.get(tts.ATTR_PREFERRED_FORMAT) == "flac" + assert int(options.get(tts.ATTR_PREFERRED_SAMPLE_RATE)) == 48000 + assert int(options.get(tts.ATTR_PREFERRED_SAMPLE_CHANNELS)) == 2 + assert int(options.get(tts.ATTR_PREFERRED_SAMPLE_BYTES)) == 2 diff --git a/tests/components/assist_pipeline/test_pipeline.py b/tests/components/assist_pipeline/test_pipeline.py index 45a661c0f075dd..50d0fc9bed8244 100644 --- a/tests/components/assist_pipeline/test_pipeline.py +++ b/tests/components/assist_pipeline/test_pipeline.py @@ -26,7 +26,7 @@ from homeassistant.setup import async_setup_component from . import MANY_LANGUAGES -from .conftest import MockSttProvider, MockTTSProvider +from .conftest import MockSTTProviderEntity, MockTTSProvider from tests.common import flush_store @@ -398,7 +398,7 @@ async def test_default_pipeline_no_stt_tts( @pytest.mark.usefixtures("init_supporting_components") async def test_default_pipeline( hass: HomeAssistant, - mock_stt_provider: MockSttProvider, + mock_stt_provider_entity: MockSTTProviderEntity, mock_tts_provider: MockTTSProvider, ha_language: str, ha_country: str | None, @@ -412,7 +412,7 @@ async def test_default_pipeline( hass.config.language = ha_language with ( - patch.object(mock_stt_provider, "_supported_languages", MANY_LANGUAGES), + patch.object(mock_stt_provider_entity, "_supported_languages", MANY_LANGUAGES), patch.object(mock_tts_provider, "_supported_languages", MANY_LANGUAGES), ): assert await async_setup_component(hass, "assist_pipeline", {}) @@ -429,7 +429,7 @@ async def test_default_pipeline( id=pipeline.id, language=pipeline_language, name="Home Assistant", - stt_engine="test", + stt_engine="stt.mock_stt", stt_language=stt_language, tts_engine="test", tts_language=tts_language, @@ -441,10 +441,10 @@ async def test_default_pipeline( @pytest.mark.usefixtures("init_supporting_components") async def test_default_pipeline_unsupported_stt_language( - hass: HomeAssistant, mock_stt_provider: MockSttProvider + hass: HomeAssistant, mock_stt_provider_entity: MockSTTProviderEntity ) -> None: """Test async_get_pipeline.""" - with patch.object(mock_stt_provider, "_supported_languages", ["smurfish"]): + with patch.object(mock_stt_provider_entity, "_supported_languages", ["smurfish"]): assert await async_setup_component(hass, "assist_pipeline", {}) pipeline_data: PipelineData = hass.data[DOMAIN] @@ -489,7 +489,7 @@ async def test_default_pipeline_unsupported_tts_language( id=pipeline.id, language="en", name="Home Assistant", - stt_engine="test", + stt_engine="stt.mock_stt", stt_language="en-US", tts_engine=None, tts_language=None, diff --git a/tests/components/assist_pipeline/test_vad.py b/tests/components/assist_pipeline/test_vad.py index db039ab3140fb4..fda26d2fb94e66 100644 --- a/tests/components/assist_pipeline/test_vad.py +++ b/tests/components/assist_pipeline/test_vad.py @@ -206,3 +206,23 @@ def test_timeout() -> None: assert not segmenter.process(_ONE_SECOND * 0.5, False) assert segmenter.timed_out + + +def test_command_seconds() -> None: + """Test minimum number of seconds for voice command.""" + + segmenter = VoiceCommandSegmenter( + command_seconds=3, speech_seconds=1, silence_seconds=1, reset_seconds=1 + ) + + assert segmenter.process(_ONE_SECOND, True) + + # Silence counts towards total command length + assert segmenter.process(_ONE_SECOND * 0.5, False) + + # Enough to finish command now + assert segmenter.process(_ONE_SECOND, True) + assert segmenter.process(_ONE_SECOND * 0.5, False) + + # Silence to finish + assert not segmenter.process(_ONE_SECOND * 0.5, False) diff --git a/tests/components/assist_pipeline/test_websocket.py b/tests/components/assist_pipeline/test_websocket.py index 2da914f4252b0b..e339ee74fbb8d6 100644 --- a/tests/components/assist_pipeline/test_websocket.py +++ b/tests/components/assist_pipeline/test_websocket.py @@ -682,7 +682,7 @@ async def test_stt_provider_missing( ) -> None: """Test events from a pipeline run with a non-existent STT provider.""" with patch( - "homeassistant.components.stt.async_get_provider", + "homeassistant.components.stt.async_get_speech_to_text_entity", return_value=None, ): client = await hass_ws_client(hass) @@ -708,11 +708,11 @@ async def test_stt_provider_bad_metadata( hass: HomeAssistant, hass_ws_client: WebSocketGenerator, init_components, - mock_stt_provider, + mock_stt_provider_entity, snapshot: SnapshotAssertion, ) -> None: """Test events from a pipeline run with wrong metadata.""" - with patch.object(mock_stt_provider, "check_metadata", return_value=False): + with patch.object(mock_stt_provider_entity, "check_metadata", return_value=False): client = await hass_ws_client(hass) await client.send_json_auto_id( @@ -743,7 +743,7 @@ async def test_stt_stream_failed( client = await hass_ws_client(hass) with patch( - "tests.components.assist_pipeline.conftest.MockSttProvider.async_process_audio_stream", + "tests.components.assist_pipeline.conftest.MockSTTProviderEntity.async_process_audio_stream", side_effect=RuntimeError, ): await client.send_json_auto_id( @@ -1188,7 +1188,7 @@ async def test_get_pipeline( "id": ANY, "language": "en", "name": "Home Assistant", - "stt_engine": "test", + "stt_engine": "stt.mock_stt", "stt_language": "en-US", "tts_engine": "test", "tts_language": "en-US", @@ -1213,7 +1213,7 @@ async def test_get_pipeline( "language": "en", "name": "Home Assistant", # It found these defaults - "stt_engine": "test", + "stt_engine": "stt.mock_stt", "stt_language": "en-US", "tts_engine": "test", "tts_language": "en-US", @@ -1297,7 +1297,7 @@ async def test_list_pipelines( "id": ANY, "language": "en", "name": "Home Assistant", - "stt_engine": "test", + "stt_engine": "stt.mock_stt", "stt_language": "en-US", "tts_engine": "test", "tts_language": "en-US", diff --git a/tests/components/asuswrt/conftest.py b/tests/components/asuswrt/conftest.py index 7710e26707cea5..f850a26b997ee4 100644 --- a/tests/components/asuswrt/conftest.py +++ b/tests/components/asuswrt/conftest.py @@ -16,12 +16,30 @@ MOCK_BYTES_TOTAL = 60000000000, 50000000000 MOCK_BYTES_TOTAL_HTTP = dict(enumerate(MOCK_BYTES_TOTAL)) +MOCK_CPU_USAGE = { + "cpu1_usage": 0.1, + "cpu2_usage": 0.2, + "cpu3_usage": 0.3, + "cpu4_usage": 0.4, + "cpu5_usage": 0.5, + "cpu6_usage": 0.6, + "cpu7_usage": 0.7, + "cpu8_usage": 0.8, + "cpu_total_usage": 0.9, +} MOCK_CURRENT_TRANSFER_RATES = 20000000, 10000000 MOCK_CURRENT_TRANSFER_RATES_HTTP = dict(enumerate(MOCK_CURRENT_TRANSFER_RATES)) MOCK_LOAD_AVG_HTTP = {"load_avg_1": 1.1, "load_avg_5": 1.2, "load_avg_15": 1.3} MOCK_LOAD_AVG = list(MOCK_LOAD_AVG_HTTP.values()) +MOCK_MEMORY_USAGE = { + "mem_usage_perc": 52.4, + "mem_total": 1048576, + "mem_free": 393216, + "mem_used": 655360, +} MOCK_TEMPERATURES = {"2.4GHz": 40.2, "5.0GHz": 0, "CPU": 71.2} MOCK_TEMPERATURES_HTTP = {**MOCK_TEMPERATURES, "5.0GHz_2": 40.3, "6.0GHz": 40.4} +MOCK_UPTIME = {"last_boot": "2024-08-02T00:47:00+00:00", "uptime": 1625927} @pytest.fixture(name="patch_setup_entry") @@ -121,6 +139,11 @@ def mock_controller_connect_http(mock_devices_http): service_mock.return_value.async_get_temperatures.return_value = { k: v for k, v in MOCK_TEMPERATURES_HTTP.items() if k != "5.0GHz" } + service_mock.return_value.async_get_cpu_usage.return_value = MOCK_CPU_USAGE + service_mock.return_value.async_get_memory_usage.return_value = ( + MOCK_MEMORY_USAGE + ) + service_mock.return_value.async_get_uptime.return_value = MOCK_UPTIME yield service_mock @@ -133,13 +156,22 @@ def mock_controller_connect_http_sens_fail(connect_http): connect_http.return_value.async_get_traffic_rates.side_effect = AsusWrtError connect_http.return_value.async_get_loadavg.side_effect = AsusWrtError connect_http.return_value.async_get_temperatures.side_effect = AsusWrtError + connect_http.return_value.async_get_cpu_usage.side_effect = AsusWrtError + connect_http.return_value.async_get_memory_usage.side_effect = AsusWrtError + connect_http.return_value.async_get_uptime.side_effect = AsusWrtError @pytest.fixture(name="connect_http_sens_detect") def mock_controller_connect_http_sens_detect(): """Mock a successful sensor detection using http library.""" - with patch( - f"{ASUSWRT_BASE}.bridge.AsusWrtHttpBridge._get_available_temperature_sensors", - return_value=[*MOCK_TEMPERATURES_HTTP], - ) as mock_sens_detect: - yield mock_sens_detect + with ( + patch( + f"{ASUSWRT_BASE}.bridge.AsusWrtHttpBridge._get_available_temperature_sensors", + return_value=[*MOCK_TEMPERATURES_HTTP], + ) as mock_sens_temp_detect, + patch( + f"{ASUSWRT_BASE}.bridge.AsusWrtHttpBridge._get_available_cpu_sensors", + return_value=[*MOCK_CPU_USAGE], + ) as mock_sens_cpu_detect, + ): + yield mock_sens_temp_detect, mock_sens_cpu_detect diff --git a/tests/components/asuswrt/test_sensor.py b/tests/components/asuswrt/test_sensor.py index 3de830f3f346b2..0036c40a6f25f1 100644 --- a/tests/components/asuswrt/test_sensor.py +++ b/tests/components/asuswrt/test_sensor.py @@ -2,6 +2,7 @@ from datetime import timedelta +from freezegun.api import FrozenDateTimeFactory from pyasuswrt.exceptions import AsusWrtError, AsusWrtNotAvailableInfoError import pytest @@ -10,10 +11,13 @@ CONF_INTERFACE, DOMAIN, SENSORS_BYTES, + SENSORS_CPU, SENSORS_LOAD_AVG, + SENSORS_MEMORY, SENSORS_RATES, SENSORS_TEMPERATURES, SENSORS_TEMPERATURES_LEGACY, + SENSORS_UPTIME, ) from homeassistant.components.device_tracker import CONF_CONSIDER_HOME from homeassistant.config_entries import ConfigEntryState @@ -26,7 +30,6 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.util import slugify -from homeassistant.util.dt import utcnow from .common import ( CONFIG_DATA_HTTP, @@ -42,7 +45,14 @@ SENSORS_DEFAULT = [*SENSORS_BYTES, *SENSORS_RATES] SENSORS_ALL_LEGACY = [*SENSORS_DEFAULT, *SENSORS_LOAD_AVG, *SENSORS_TEMPERATURES_LEGACY] -SENSORS_ALL_HTTP = [*SENSORS_DEFAULT, *SENSORS_LOAD_AVG, *SENSORS_TEMPERATURES] +SENSORS_ALL_HTTP = [ + *SENSORS_DEFAULT, + *SENSORS_CPU, + *SENSORS_LOAD_AVG, + *SENSORS_MEMORY, + *SENSORS_TEMPERATURES, + *SENSORS_UPTIME, +] @pytest.fixture(name="create_device_registry_devices") @@ -95,6 +105,7 @@ def _setup_entry(hass: HomeAssistant, config, sensors, unique_id=None): async def _test_sensors( hass: HomeAssistant, + freezer: FrozenDateTimeFactory, mock_devices, config, entry_unique_id, @@ -125,7 +136,8 @@ async def _test_sensors( # initial devices setup assert await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() - async_fire_time_changed(hass, utcnow() + timedelta(seconds=30)) + freezer.tick(timedelta(seconds=30)) + async_fire_time_changed(hass) await hass.async_block_till_done() assert hass.states.get(f"{device_tracker.DOMAIN}.test").state == STATE_HOME @@ -139,7 +151,8 @@ async def _test_sensors( # remove first tracked device mock_devices.pop(MOCK_MACS[0]) - async_fire_time_changed(hass, utcnow() + timedelta(seconds=30)) + freezer.tick(timedelta(seconds=30)) + async_fire_time_changed(hass) await hass.async_block_till_done() # consider home option set, all devices still home but only 1 device connected @@ -160,7 +173,8 @@ async def _test_sensors( config_entry, options={CONF_CONSIDER_HOME: 0} ) await hass.async_block_till_done() - async_fire_time_changed(hass, utcnow() + timedelta(seconds=30)) + freezer.tick(timedelta(seconds=30)) + async_fire_time_changed(hass) await hass.async_block_till_done() # consider home option set to 0, device "test" not home @@ -176,13 +190,16 @@ async def _test_sensors( ) async def test_sensors_legacy( hass: HomeAssistant, - connect_legacy, + freezer: FrozenDateTimeFactory, mock_devices_legacy, - create_device_registry_devices, entry_unique_id, + connect_legacy, + create_device_registry_devices, ) -> None: """Test creating AsusWRT default sensors and tracker with legacy protocol.""" - await _test_sensors(hass, mock_devices_legacy, CONFIG_DATA_TELNET, entry_unique_id) + await _test_sensors( + hass, freezer, mock_devices_legacy, CONFIG_DATA_TELNET, entry_unique_id + ) @pytest.mark.parametrize( @@ -191,16 +208,21 @@ async def test_sensors_legacy( ) async def test_sensors_http( hass: HomeAssistant, - connect_http, + freezer: FrozenDateTimeFactory, mock_devices_http, - create_device_registry_devices, entry_unique_id, + connect_http, + create_device_registry_devices, ) -> None: """Test creating AsusWRT default sensors and tracker with http protocol.""" - await _test_sensors(hass, mock_devices_http, CONFIG_DATA_HTTP, entry_unique_id) + await _test_sensors( + hass, freezer, mock_devices_http, CONFIG_DATA_HTTP, entry_unique_id + ) -async def _test_loadavg_sensors(hass: HomeAssistant, config) -> None: +async def _test_loadavg_sensors( + hass: HomeAssistant, freezer: FrozenDateTimeFactory, config +) -> None: """Test creating an AsusWRT load average sensors.""" config_entry, sensor_prefix = _setup_entry(hass, config, SENSORS_LOAD_AVG) config_entry.add_to_hass(hass) @@ -208,7 +230,8 @@ async def _test_loadavg_sensors(hass: HomeAssistant, config) -> None: # initial devices setup assert await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() - async_fire_time_changed(hass, utcnow() + timedelta(seconds=30)) + freezer.tick(timedelta(seconds=30)) + async_fire_time_changed(hass) await hass.async_block_till_done() # assert temperature sensor available @@ -217,18 +240,22 @@ async def _test_loadavg_sensors(hass: HomeAssistant, config) -> None: assert hass.states.get(f"{sensor_prefix}_sensor_load_avg15").state == "1.3" -async def test_loadavg_sensors_legacy(hass: HomeAssistant, connect_legacy) -> None: +async def test_loadavg_sensors_legacy( + hass: HomeAssistant, freezer: FrozenDateTimeFactory, connect_legacy +) -> None: """Test creating an AsusWRT load average sensors.""" - await _test_loadavg_sensors(hass, CONFIG_DATA_TELNET) + await _test_loadavg_sensors(hass, freezer, CONFIG_DATA_TELNET) -async def test_loadavg_sensors_http(hass: HomeAssistant, connect_http) -> None: +async def test_loadavg_sensors_http( + hass: HomeAssistant, freezer: FrozenDateTimeFactory, connect_http +) -> None: """Test creating an AsusWRT load average sensors.""" - await _test_loadavg_sensors(hass, CONFIG_DATA_HTTP) + await _test_loadavg_sensors(hass, freezer, CONFIG_DATA_HTTP) async def test_loadavg_sensors_unaivalable_http( - hass: HomeAssistant, connect_http + hass: HomeAssistant, freezer: FrozenDateTimeFactory, connect_http ) -> None: """Test load average sensors no available using http.""" config_entry, sensor_prefix = _setup_entry(hass, CONFIG_DATA_HTTP, SENSORS_LOAD_AVG) @@ -241,7 +268,8 @@ async def test_loadavg_sensors_unaivalable_http( # initial devices setup assert await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() - async_fire_time_changed(hass, utcnow() + timedelta(seconds=30)) + freezer.tick(timedelta(seconds=30)) + async_fire_time_changed(hass) await hass.async_block_till_done() # assert load average sensors not available @@ -271,7 +299,9 @@ async def test_temperature_sensors_http_fail( assert not hass.states.get(f"{sensor_prefix}_6_0ghz") -async def _test_temperature_sensors(hass: HomeAssistant, config, sensors) -> str: +async def _test_temperature_sensors( + hass: HomeAssistant, freezer: FrozenDateTimeFactory, config, sensors +) -> str: """Test creating a AsusWRT temperature sensors.""" config_entry, sensor_prefix = _setup_entry(hass, config, sensors) config_entry.add_to_hass(hass) @@ -279,16 +309,19 @@ async def _test_temperature_sensors(hass: HomeAssistant, config, sensors) -> str # initial devices setup assert await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() - async_fire_time_changed(hass, utcnow() + timedelta(seconds=30)) + freezer.tick(timedelta(seconds=30)) + async_fire_time_changed(hass) await hass.async_block_till_done() return sensor_prefix -async def test_temperature_sensors_legacy(hass: HomeAssistant, connect_legacy) -> None: +async def test_temperature_sensors_legacy( + hass: HomeAssistant, freezer: FrozenDateTimeFactory, connect_legacy +) -> None: """Test creating a AsusWRT temperature sensors.""" sensor_prefix = await _test_temperature_sensors( - hass, CONFIG_DATA_TELNET, SENSORS_TEMPERATURES_LEGACY + hass, freezer, CONFIG_DATA_TELNET, SENSORS_TEMPERATURES_LEGACY ) # assert temperature sensor available assert hass.states.get(f"{sensor_prefix}_2_4ghz").state == "40.2" @@ -296,10 +329,12 @@ async def test_temperature_sensors_legacy(hass: HomeAssistant, connect_legacy) - assert not hass.states.get(f"{sensor_prefix}_5_0ghz") -async def test_temperature_sensors_http(hass: HomeAssistant, connect_http) -> None: +async def test_temperature_sensors_http( + hass: HomeAssistant, freezer: FrozenDateTimeFactory, connect_http +) -> None: """Test creating a AsusWRT temperature sensors.""" sensor_prefix = await _test_temperature_sensors( - hass, CONFIG_DATA_HTTP, SENSORS_TEMPERATURES + hass, freezer, CONFIG_DATA_HTTP, SENSORS_TEMPERATURES ) # assert temperature sensor available assert hass.states.get(f"{sensor_prefix}_2_4ghz").state == "40.2" @@ -309,6 +344,97 @@ async def test_temperature_sensors_http(hass: HomeAssistant, connect_http) -> No assert not hass.states.get(f"{sensor_prefix}_5_0ghz") +async def test_cpu_sensors_http_fail( + hass: HomeAssistant, connect_http_sens_fail +) -> None: + """Test fail creating AsusWRT cpu sensors.""" + config_entry, sensor_prefix = _setup_entry(hass, CONFIG_DATA_HTTP, SENSORS_CPU) + config_entry.add_to_hass(hass) + + # initial devices setup + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + # assert cpu availability exception is handled correctly + assert not hass.states.get(f"{sensor_prefix}_cpu1_usage") + assert not hass.states.get(f"{sensor_prefix}_cpu2_usage") + assert not hass.states.get(f"{sensor_prefix}_cpu3_usage") + assert not hass.states.get(f"{sensor_prefix}_cpu4_usage") + assert not hass.states.get(f"{sensor_prefix}_cpu5_usage") + assert not hass.states.get(f"{sensor_prefix}_cpu6_usage") + assert not hass.states.get(f"{sensor_prefix}_cpu7_usage") + assert not hass.states.get(f"{sensor_prefix}_cpu8_usage") + assert not hass.states.get(f"{sensor_prefix}_cpu_total_usage") + + +async def test_cpu_sensors_http( + hass: HomeAssistant, freezer: FrozenDateTimeFactory, connect_http +) -> None: + """Test creating AsusWRT cpu sensors.""" + config_entry, sensor_prefix = _setup_entry(hass, CONFIG_DATA_HTTP, SENSORS_CPU) + config_entry.add_to_hass(hass) + + # initial devices setup + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + freezer.tick(timedelta(seconds=30)) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + # assert cpu sensors available + assert hass.states.get(f"{sensor_prefix}_cpu1_usage").state == "0.1" + assert hass.states.get(f"{sensor_prefix}_cpu2_usage").state == "0.2" + assert hass.states.get(f"{sensor_prefix}_cpu3_usage").state == "0.3" + assert hass.states.get(f"{sensor_prefix}_cpu4_usage").state == "0.4" + assert hass.states.get(f"{sensor_prefix}_cpu5_usage").state == "0.5" + assert hass.states.get(f"{sensor_prefix}_cpu6_usage").state == "0.6" + assert hass.states.get(f"{sensor_prefix}_cpu7_usage").state == "0.7" + assert hass.states.get(f"{sensor_prefix}_cpu8_usage").state == "0.8" + assert hass.states.get(f"{sensor_prefix}_cpu_total_usage").state == "0.9" + + +async def test_memory_sensors_http( + hass: HomeAssistant, freezer: FrozenDateTimeFactory, connect_http +) -> None: + """Test creating AsusWRT memory sensors.""" + config_entry, sensor_prefix = _setup_entry(hass, CONFIG_DATA_HTTP, SENSORS_MEMORY) + config_entry.add_to_hass(hass) + + # initial devices setup + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + freezer.tick(timedelta(seconds=30)) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + # assert memory sensors available + assert hass.states.get(f"{sensor_prefix}_mem_usage_perc").state == "52.4" + assert hass.states.get(f"{sensor_prefix}_mem_free").state == "384.0" + assert hass.states.get(f"{sensor_prefix}_mem_used").state == "640.0" + + +async def test_uptime_sensors_http( + hass: HomeAssistant, freezer: FrozenDateTimeFactory, connect_http +) -> None: + """Test creating AsusWRT uptime sensors.""" + config_entry, sensor_prefix = _setup_entry(hass, CONFIG_DATA_HTTP, SENSORS_UPTIME) + config_entry.add_to_hass(hass) + + # initial devices setup + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + freezer.tick(timedelta(seconds=30)) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + # assert uptime sensors available + assert ( + hass.states.get(f"{sensor_prefix}_sensor_last_boot").state + == "2024-08-02T00:47:00+00:00" + ) + assert hass.states.get(f"{sensor_prefix}_sensor_uptime").state == "1625927" + + @pytest.mark.parametrize( "side_effect", [OSError, None], @@ -359,7 +485,9 @@ async def test_connect_fail_http( assert config_entry.state is ConfigEntryState.SETUP_RETRY -async def _test_sensors_polling_fails(hass: HomeAssistant, config, sensors) -> None: +async def _test_sensors_polling_fails( + hass: HomeAssistant, freezer: FrozenDateTimeFactory, config, sensors +) -> None: """Test AsusWRT sensors are unavailable when polling fails.""" config_entry, sensor_prefix = _setup_entry(hass, config, sensors) config_entry.add_to_hass(hass) @@ -367,7 +495,8 @@ async def _test_sensors_polling_fails(hass: HomeAssistant, config, sensors) -> N # initial devices setup assert await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() - async_fire_time_changed(hass, utcnow() + timedelta(seconds=30)) + freezer.tick(timedelta(seconds=30)) + async_fire_time_changed(hass) await hass.async_block_till_done() for sensor_name in sensors: @@ -380,22 +509,28 @@ async def _test_sensors_polling_fails(hass: HomeAssistant, config, sensors) -> N async def test_sensors_polling_fails_legacy( hass: HomeAssistant, + freezer: FrozenDateTimeFactory, connect_legacy_sens_fail, ) -> None: """Test AsusWRT sensors are unavailable when polling fails.""" - await _test_sensors_polling_fails(hass, CONFIG_DATA_TELNET, SENSORS_ALL_LEGACY) + await _test_sensors_polling_fails( + hass, freezer, CONFIG_DATA_TELNET, SENSORS_ALL_LEGACY + ) async def test_sensors_polling_fails_http( hass: HomeAssistant, + freezer: FrozenDateTimeFactory, connect_http_sens_fail, connect_http_sens_detect, ) -> None: """Test AsusWRT sensors are unavailable when polling fails.""" - await _test_sensors_polling_fails(hass, CONFIG_DATA_HTTP, SENSORS_ALL_HTTP) + await _test_sensors_polling_fails(hass, freezer, CONFIG_DATA_HTTP, SENSORS_ALL_HTTP) -async def test_options_reload(hass: HomeAssistant, connect_legacy) -> None: +async def test_options_reload( + hass: HomeAssistant, freezer: FrozenDateTimeFactory, connect_legacy +) -> None: """Test AsusWRT integration is reload changing an options that require this.""" config_entry = MockConfigEntry( domain=DOMAIN, @@ -408,7 +543,8 @@ async def test_options_reload(hass: HomeAssistant, connect_legacy) -> None: await hass.async_block_till_done() assert connect_legacy.return_value.connection.async_connect.call_count == 1 - async_fire_time_changed(hass, utcnow() + timedelta(seconds=30)) + freezer.tick(timedelta(seconds=30)) + async_fire_time_changed(hass) await hass.async_block_till_done() # change an option that requires integration reload @@ -451,7 +587,10 @@ async def test_unique_id_migration( async def test_decorator_errors( - hass: HomeAssistant, connect_legacy, mock_available_temps + hass: HomeAssistant, + freezer: FrozenDateTimeFactory, + connect_legacy, + mock_available_temps, ) -> None: """Test AsusWRT sensors are unavailable on decorator type check error.""" sensors = [*SENSORS_BYTES, *SENSORS_TEMPERATURES_LEGACY] @@ -465,7 +604,8 @@ async def test_decorator_errors( # initial devices setup assert await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() - async_fire_time_changed(hass, utcnow() + timedelta(seconds=30)) + freezer.tick(timedelta(seconds=30)) + async_fire_time_changed(hass) await hass.async_block_till_done() for sensor_name in sensors: diff --git a/tests/components/august/conftest.py b/tests/components/august/conftest.py index 052cde7d2a2711..78cb2cdad890c5 100644 --- a/tests/components/august/conftest.py +++ b/tests/components/august/conftest.py @@ -3,6 +3,7 @@ from unittest.mock import patch import pytest +from yalexs.manager.ratelimit import _RateLimitChecker @pytest.fixture(name="mock_discovery", autouse=True) @@ -12,3 +13,10 @@ def mock_discovery_fixture(): "homeassistant.components.august.data.discovery_flow.async_create_flow" ) as mock_discovery: yield mock_discovery + + +@pytest.fixture(name="disable_ratelimit_checks", autouse=True) +def disable_ratelimit_checks_fixture(): + """Disable rate limit checks.""" + with patch.object(_RateLimitChecker, "register_wakeup"): + yield diff --git a/tests/components/august/mocks.py b/tests/components/august/mocks.py index c2ab8ce743c15b..43cc4957445be9 100644 --- a/tests/components/august/mocks.py +++ b/tests/components/august/mocks.py @@ -82,10 +82,7 @@ async def _mock_setup_august( ) entry.add_to_hass(hass) with ( - patch( - "yalexs.manager.data.async_create_pubnub", - return_value=AsyncMock(), - ), + patch.object(pubnub_mock, "run"), patch("yalexs.manager.data.AugustPubNub", return_value=pubnub_mock), ): assert await hass.config_entries.async_setup(entry.entry_id) diff --git a/tests/components/august/snapshots/test_binary_sensor.ambr b/tests/components/august/snapshots/test_binary_sensor.ambr new file mode 100644 index 00000000000000..6e95b0ce5521da --- /dev/null +++ b/tests/components/august/snapshots/test_binary_sensor.ambr @@ -0,0 +1,33 @@ +# serializer version: 1 +# name: test_doorbell_device_registry + DeviceRegistryEntrySnapshot({ + 'area_id': 'tmt100_name', + 'config_entries': , + 'configuration_url': 'https://account.august.com', + 'connections': set({ + }), + 'disabled_by': None, + 'entry_type': None, + 'hw_version': None, + 'id': , + 'identifiers': set({ + tuple( + 'august', + 'tmt100', + ), + }), + 'is_new': False, + 'labels': set({ + }), + 'manufacturer': 'August Home Inc.', + 'model': 'hydra1', + 'model_id': None, + 'name': 'tmt100 Name', + 'name_by_user': None, + 'primary_config_entry': , + 'serial_number': None, + 'suggested_area': 'tmt100 Name', + 'sw_version': '3.1.0-HYDRC75+201909251139', + 'via_device_id': None, + }) +# --- diff --git a/tests/components/august/snapshots/test_lock.ambr b/tests/components/august/snapshots/test_lock.ambr new file mode 100644 index 00000000000000..6aad3a140ca55f --- /dev/null +++ b/tests/components/august/snapshots/test_lock.ambr @@ -0,0 +1,37 @@ +# serializer version: 1 +# name: test_lock_device_registry + DeviceRegistryEntrySnapshot({ + 'area_id': 'online_with_doorsense_name', + 'config_entries': , + 'configuration_url': 'https://account.august.com', + 'connections': set({ + tuple( + 'bluetooth', + '12:22', + ), + }), + 'disabled_by': None, + 'entry_type': None, + 'hw_version': None, + 'id': , + 'identifiers': set({ + tuple( + 'august', + 'online_with_doorsense', + ), + }), + 'is_new': False, + 'labels': set({ + }), + 'manufacturer': 'August Home Inc.', + 'model': 'AUG-MD01', + 'model_id': None, + 'name': 'online_with_doorsense Name', + 'name_by_user': None, + 'primary_config_entry': , + 'serial_number': None, + 'suggested_area': 'online_with_doorsense Name', + 'sw_version': 'undefined-4.3.0-1.8.14', + 'via_device_id': None, + }) +# --- diff --git a/tests/components/august/test_binary_sensor.py b/tests/components/august/test_binary_sensor.py index 33d582de8d8b78..4ae300ae56b610 100644 --- a/tests/components/august/test_binary_sensor.py +++ b/tests/components/august/test_binary_sensor.py @@ -1,8 +1,10 @@ """The binary_sensor tests for the august platform.""" import datetime -from unittest.mock import Mock, patch +from unittest.mock import Mock +from freezegun.api import FrozenDateTimeFactory +from syrupy import SnapshotAssertion from yalexs.pubnub_async import AugustPubNub from homeassistant.components.lock import DOMAIN as LOCK_DOMAIN @@ -36,28 +38,20 @@ async def test_doorsense(hass: HomeAssistant) -> None: hass, "get_lock.online_with_doorsense.json" ) await _create_august_with_devices(hass, [lock_one]) + states = hass.states - binary_sensor_online_with_doorsense_name = hass.states.get( - "binary_sensor.online_with_doorsense_name_door" - ) - assert binary_sensor_online_with_doorsense_name.state == STATE_ON + assert states.get("binary_sensor.online_with_doorsense_name_door").state == STATE_ON data = {ATTR_ENTITY_ID: "lock.online_with_doorsense_name"} await hass.services.async_call(LOCK_DOMAIN, SERVICE_UNLOCK, data, blocking=True) - await hass.async_block_till_done() - binary_sensor_online_with_doorsense_name = hass.states.get( - "binary_sensor.online_with_doorsense_name_door" - ) - assert binary_sensor_online_with_doorsense_name.state == STATE_ON + assert states.get("binary_sensor.online_with_doorsense_name_door").state == STATE_ON await hass.services.async_call(LOCK_DOMAIN, SERVICE_LOCK, data, blocking=True) - await hass.async_block_till_done() - binary_sensor_online_with_doorsense_name = hass.states.get( - "binary_sensor.online_with_doorsense_name_door" + assert ( + states.get("binary_sensor.online_with_doorsense_name_door").state == STATE_OFF ) - assert binary_sensor_online_with_doorsense_name.state == STATE_OFF async def test_lock_bridge_offline(hass: HomeAssistant) -> None: @@ -69,113 +63,82 @@ async def test_lock_bridge_offline(hass: HomeAssistant) -> None: hass, "get_activity.bridge_offline.json" ) await _create_august_with_devices(hass, [lock_one], activities=activities) - - binary_sensor_online_with_doorsense_name = hass.states.get( - "binary_sensor.online_with_doorsense_name_door" + states = hass.states + assert ( + states.get("binary_sensor.online_with_doorsense_name_door").state + == STATE_UNAVAILABLE ) - assert binary_sensor_online_with_doorsense_name.state == STATE_UNAVAILABLE async def test_create_doorbell(hass: HomeAssistant) -> None: """Test creation of a doorbell.""" doorbell_one = await _mock_doorbell_from_fixture(hass, "get_doorbell.json") await _create_august_with_devices(hass, [doorbell_one]) + states = hass.states - binary_sensor_k98gidt45gul_name_motion = hass.states.get( - "binary_sensor.k98gidt45gul_name_motion" + assert states.get("binary_sensor.k98gidt45gul_name_motion").state == STATE_OFF + assert ( + states.get("binary_sensor.k98gidt45gul_name_image_capture").state == STATE_OFF ) - assert binary_sensor_k98gidt45gul_name_motion.state == STATE_OFF - binary_sensor_k98gidt45gul_name_image_capture = hass.states.get( - "binary_sensor.k98gidt45gul_name_image_capture" + assert states.get("binary_sensor.k98gidt45gul_name_connectivity").state == STATE_ON + assert ( + states.get("binary_sensor.k98gidt45gul_name_doorbell_ding").state == STATE_OFF ) - assert binary_sensor_k98gidt45gul_name_image_capture.state == STATE_OFF - binary_sensor_k98gidt45gul_name_online = hass.states.get( - "binary_sensor.k98gidt45gul_name_connectivity" + assert states.get("binary_sensor.k98gidt45gul_name_motion").state == STATE_OFF + assert ( + states.get("binary_sensor.k98gidt45gul_name_image_capture").state == STATE_OFF ) - assert binary_sensor_k98gidt45gul_name_online.state == STATE_ON - binary_sensor_k98gidt45gul_name_ding = hass.states.get( - "binary_sensor.k98gidt45gul_name_doorbell_ding" - ) - assert binary_sensor_k98gidt45gul_name_ding.state == STATE_OFF - binary_sensor_k98gidt45gul_name_motion = hass.states.get( - "binary_sensor.k98gidt45gul_name_motion" - ) - assert binary_sensor_k98gidt45gul_name_motion.state == STATE_OFF - binary_sensor_k98gidt45gul_name_image_capture = hass.states.get( - "binary_sensor.k98gidt45gul_name_image_capture" - ) - assert binary_sensor_k98gidt45gul_name_image_capture.state == STATE_OFF async def test_create_doorbell_offline(hass: HomeAssistant) -> None: """Test creation of a doorbell that is offline.""" doorbell_one = await _mock_doorbell_from_fixture(hass, "get_doorbell.offline.json") await _create_august_with_devices(hass, [doorbell_one]) + states = hass.states - binary_sensor_tmt100_name_motion = hass.states.get( - "binary_sensor.tmt100_name_motion" - ) - assert binary_sensor_tmt100_name_motion.state == STATE_UNAVAILABLE - binary_sensor_tmt100_name_online = hass.states.get( - "binary_sensor.tmt100_name_connectivity" + assert states.get("binary_sensor.tmt100_name_motion").state == STATE_UNAVAILABLE + assert states.get("binary_sensor.tmt100_name_connectivity").state == STATE_OFF + assert ( + states.get("binary_sensor.tmt100_name_doorbell_ding").state == STATE_UNAVAILABLE ) - assert binary_sensor_tmt100_name_online.state == STATE_OFF - binary_sensor_tmt100_name_ding = hass.states.get( - "binary_sensor.tmt100_name_doorbell_ding" - ) - assert binary_sensor_tmt100_name_ding.state == STATE_UNAVAILABLE -async def test_create_doorbell_with_motion(hass: HomeAssistant) -> None: +async def test_create_doorbell_with_motion( + hass: HomeAssistant, freezer: FrozenDateTimeFactory +) -> None: """Test creation of a doorbell.""" doorbell_one = await _mock_doorbell_from_fixture(hass, "get_doorbell.json") activities = await _mock_activities_from_fixture( hass, "get_activity.doorbell_motion.json" ) await _create_august_with_devices(hass, [doorbell_one], activities=activities) + states = hass.states - binary_sensor_k98gidt45gul_name_motion = hass.states.get( - "binary_sensor.k98gidt45gul_name_motion" - ) - assert binary_sensor_k98gidt45gul_name_motion.state == STATE_ON - binary_sensor_k98gidt45gul_name_online = hass.states.get( - "binary_sensor.k98gidt45gul_name_connectivity" - ) - assert binary_sensor_k98gidt45gul_name_online.state == STATE_ON - binary_sensor_k98gidt45gul_name_ding = hass.states.get( - "binary_sensor.k98gidt45gul_name_doorbell_ding" + assert states.get("binary_sensor.k98gidt45gul_name_motion").state == STATE_ON + assert states.get("binary_sensor.k98gidt45gul_name_connectivity").state == STATE_ON + assert ( + states.get("binary_sensor.k98gidt45gul_name_doorbell_ding").state == STATE_OFF ) - assert binary_sensor_k98gidt45gul_name_ding.state == STATE_OFF - new_time = dt_util.utcnow() + datetime.timedelta(seconds=40) - native_time = datetime.datetime.now() + datetime.timedelta(seconds=40) - with patch( - "homeassistant.components.august.util._native_datetime", - return_value=native_time, - ): - async_fire_time_changed(hass, new_time) - await hass.async_block_till_done() - binary_sensor_k98gidt45gul_name_motion = hass.states.get( - "binary_sensor.k98gidt45gul_name_motion" - ) - assert binary_sensor_k98gidt45gul_name_motion.state == STATE_OFF + freezer.tick(40) + async_fire_time_changed(hass) + await hass.async_block_till_done() + assert states.get("binary_sensor.k98gidt45gul_name_motion").state == STATE_OFF -async def test_doorbell_update_via_pubnub(hass: HomeAssistant) -> None: +async def test_doorbell_update_via_pubnub( + hass: HomeAssistant, freezer: FrozenDateTimeFactory +) -> None: """Test creation of a doorbell that can be updated via pubnub.""" doorbell_one = await _mock_doorbell_from_fixture(hass, "get_doorbell.json") pubnub = AugustPubNub() await _create_august_with_devices(hass, [doorbell_one], pubnub=pubnub) assert doorbell_one.pubsub_channel == "7c7a6672-59c8-3333-ffff-dcd98705cccc" - - binary_sensor_k98gidt45gul_name_motion = hass.states.get( - "binary_sensor.k98gidt45gul_name_motion" + states = hass.states + assert states.get("binary_sensor.k98gidt45gul_name_motion").state == STATE_OFF + assert ( + states.get("binary_sensor.k98gidt45gul_name_doorbell_ding").state == STATE_OFF ) - assert binary_sensor_k98gidt45gul_name_motion.state == STATE_OFF - binary_sensor_k98gidt45gul_name_ding = hass.states.get( - "binary_sensor.k98gidt45gul_name_doorbell_ding" - ) - assert binary_sensor_k98gidt45gul_name_ding.state == STATE_OFF pubnub.message( pubnub, @@ -198,10 +161,7 @@ async def test_doorbell_update_via_pubnub(hass: HomeAssistant) -> None: await hass.async_block_till_done() - binary_sensor_k98gidt45gul_name_image_capture = hass.states.get( - "binary_sensor.k98gidt45gul_name_image_capture" - ) - assert binary_sensor_k98gidt45gul_name_image_capture.state == STATE_ON + assert states.get("binary_sensor.k98gidt45gul_name_image_capture").state == STATE_ON pubnub.message( pubnub, @@ -235,29 +195,19 @@ async def test_doorbell_update_via_pubnub(hass: HomeAssistant) -> None: await hass.async_block_till_done() - binary_sensor_k98gidt45gul_name_motion = hass.states.get( - "binary_sensor.k98gidt45gul_name_motion" - ) - assert binary_sensor_k98gidt45gul_name_motion.state == STATE_ON + assert states.get("binary_sensor.k98gidt45gul_name_motion").state == STATE_ON - binary_sensor_k98gidt45gul_name_ding = hass.states.get( - "binary_sensor.k98gidt45gul_name_doorbell_ding" + assert ( + states.get("binary_sensor.k98gidt45gul_name_doorbell_ding").state == STATE_OFF ) - assert binary_sensor_k98gidt45gul_name_ding.state == STATE_OFF - - new_time = dt_util.utcnow() + datetime.timedelta(seconds=40) - native_time = datetime.datetime.now() + datetime.timedelta(seconds=40) - with patch( - "homeassistant.components.august.util._native_datetime", - return_value=native_time, - ): - async_fire_time_changed(hass, new_time) - await hass.async_block_till_done() - - binary_sensor_k98gidt45gul_name_image_capture = hass.states.get( - "binary_sensor.k98gidt45gul_name_image_capture" + + freezer.tick(40) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + assert ( + states.get("binary_sensor.k98gidt45gul_name_image_capture").state == STATE_OFF ) - assert binary_sensor_k98gidt45gul_name_image_capture.state == STATE_OFF pubnub.message( pubnub, @@ -271,37 +221,25 @@ async def test_doorbell_update_via_pubnub(hass: HomeAssistant) -> None: ) await hass.async_block_till_done() - binary_sensor_k98gidt45gul_name_ding = hass.states.get( - "binary_sensor.k98gidt45gul_name_doorbell_ding" - ) - assert binary_sensor_k98gidt45gul_name_ding.state == STATE_ON - new_time = dt_util.utcnow() + datetime.timedelta(seconds=40) - native_time = datetime.datetime.now() + datetime.timedelta(seconds=40) - with patch( - "homeassistant.components.august.util._native_datetime", - return_value=native_time, - ): - async_fire_time_changed(hass, new_time) - await hass.async_block_till_done() - - binary_sensor_k98gidt45gul_name_ding = hass.states.get( - "binary_sensor.k98gidt45gul_name_doorbell_ding" + assert states.get("binary_sensor.k98gidt45gul_name_doorbell_ding").state == STATE_ON + freezer.tick(40) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + assert ( + states.get("binary_sensor.k98gidt45gul_name_doorbell_ding").state == STATE_OFF ) - assert binary_sensor_k98gidt45gul_name_ding.state == STATE_OFF async def test_doorbell_device_registry( - hass: HomeAssistant, device_registry: dr.DeviceRegistry + hass: HomeAssistant, device_registry: dr.DeviceRegistry, snapshot: SnapshotAssertion ) -> None: """Test creation of a lock with doorsense and bridge ands up in the registry.""" doorbell_one = await _mock_doorbell_from_fixture(hass, "get_doorbell.offline.json") await _create_august_with_devices(hass, [doorbell_one]) reg_device = device_registry.async_get_device(identifiers={("august", "tmt100")}) - assert reg_device.model == "hydra1" - assert reg_device.name == "tmt100 Name" - assert reg_device.manufacturer == "August Home Inc." - assert reg_device.sw_version == "3.1.0-HYDRC75+201909251139" + assert reg_device == snapshot async def test_door_sense_update_via_pubnub(hass: HomeAssistant) -> None: @@ -314,11 +252,9 @@ async def test_door_sense_update_via_pubnub(hass: HomeAssistant) -> None: config_entry = await _create_august_with_devices( hass, [lock_one], activities=activities, pubnub=pubnub ) + states = hass.states - binary_sensor_online_with_doorsense_name = hass.states.get( - "binary_sensor.online_with_doorsense_name_door" - ) - assert binary_sensor_online_with_doorsense_name.state == STATE_ON + assert states.get("binary_sensor.online_with_doorsense_name_door").state == STATE_ON pubnub.message( pubnub, @@ -330,10 +266,9 @@ async def test_door_sense_update_via_pubnub(hass: HomeAssistant) -> None: ) await hass.async_block_till_done() - binary_sensor_online_with_doorsense_name = hass.states.get( - "binary_sensor.online_with_doorsense_name_door" + assert ( + states.get("binary_sensor.online_with_doorsense_name_door").state == STATE_OFF ) - assert binary_sensor_online_with_doorsense_name.state == STATE_OFF pubnub.message( pubnub, @@ -344,33 +279,22 @@ async def test_door_sense_update_via_pubnub(hass: HomeAssistant) -> None: ), ) await hass.async_block_till_done() - binary_sensor_online_with_doorsense_name = hass.states.get( - "binary_sensor.online_with_doorsense_name_door" - ) - assert binary_sensor_online_with_doorsense_name.state == STATE_ON + assert states.get("binary_sensor.online_with_doorsense_name_door").state == STATE_ON async_fire_time_changed(hass, dt_util.utcnow() + datetime.timedelta(seconds=30)) await hass.async_block_till_done() - binary_sensor_online_with_doorsense_name = hass.states.get( - "binary_sensor.online_with_doorsense_name_door" - ) - assert binary_sensor_online_with_doorsense_name.state == STATE_ON + assert states.get("binary_sensor.online_with_doorsense_name_door").state == STATE_ON pubnub.connected = True async_fire_time_changed(hass, dt_util.utcnow() + datetime.timedelta(seconds=30)) await hass.async_block_till_done() - binary_sensor_online_with_doorsense_name = hass.states.get( - "binary_sensor.online_with_doorsense_name_door" - ) - assert binary_sensor_online_with_doorsense_name.state == STATE_ON + assert states.get("binary_sensor.online_with_doorsense_name_door").state == STATE_ON # Ensure pubnub status is always preserved async_fire_time_changed(hass, dt_util.utcnow() + datetime.timedelta(hours=2)) await hass.async_block_till_done() - binary_sensor_online_with_doorsense_name = hass.states.get( - "binary_sensor.online_with_doorsense_name_door" - ) - assert binary_sensor_online_with_doorsense_name.state == STATE_ON + + assert states.get("binary_sensor.online_with_doorsense_name_door").state == STATE_ON pubnub.message( pubnub, @@ -381,17 +305,11 @@ async def test_door_sense_update_via_pubnub(hass: HomeAssistant) -> None: ), ) await hass.async_block_till_done() - binary_sensor_online_with_doorsense_name = hass.states.get( - "binary_sensor.online_with_doorsense_name_door" - ) - assert binary_sensor_online_with_doorsense_name.state == STATE_ON + assert states.get("binary_sensor.online_with_doorsense_name_door").state == STATE_ON async_fire_time_changed(hass, dt_util.utcnow() + datetime.timedelta(hours=4)) await hass.async_block_till_done() - binary_sensor_online_with_doorsense_name = hass.states.get( - "binary_sensor.online_with_doorsense_name_door" - ) - assert binary_sensor_online_with_doorsense_name.state == STATE_ON + assert states.get("binary_sensor.online_with_doorsense_name_door").state == STATE_ON await hass.config_entries.async_unload(config_entry.entry_id) await hass.async_block_till_done() @@ -402,7 +320,10 @@ async def test_create_lock_with_doorbell(hass: HomeAssistant) -> None: lock_one = await _mock_lock_from_fixture(hass, "lock_with_doorbell.online.json") await _create_august_with_devices(hass, [lock_one]) - ding_sensor = hass.states.get( - "binary_sensor.a6697750d607098bae8d6baa11ef8063_name_doorbell_ding" + states = hass.states + assert ( + states.get( + "binary_sensor.a6697750d607098bae8d6baa11ef8063_name_doorbell_ding" + ).state + == STATE_OFF ) - assert ding_sensor.state == STATE_OFF diff --git a/tests/components/august/test_button.py b/tests/components/august/test_button.py index 8ae2bc8a70ddb9..948b59b2286a16 100644 --- a/tests/components/august/test_button.py +++ b/tests/components/august/test_button.py @@ -20,5 +20,4 @@ async def test_wake_lock(hass: HomeAssistant) -> None: await hass.services.async_call( BUTTON_DOMAIN, SERVICE_PRESS, {ATTR_ENTITY_ID: entity_id}, blocking=True ) - await hass.async_block_till_done() api_instance.async_status_async.assert_called_once() diff --git a/tests/components/august/test_camera.py b/tests/components/august/test_camera.py index 539a26cc30fd71..5ab7d49c3b8224 100644 --- a/tests/components/august/test_camera.py +++ b/tests/components/august/test_camera.py @@ -25,14 +25,10 @@ async def test_create_doorbell( ): await _create_august_with_devices(hass, [doorbell_one], brand=Brand.AUGUST) - camera_k98gidt45gul_name_camera = hass.states.get( - "camera.k98gidt45gul_name_camera" - ) - assert camera_k98gidt45gul_name_camera.state == STATE_IDLE + camera_state = hass.states.get("camera.k98gidt45gul_name_camera") + assert camera_state.state == STATE_IDLE - url = hass.states.get("camera.k98gidt45gul_name_camera").attributes[ - "entity_picture" - ] + url = camera_state.attributes["entity_picture"] client = await hass_client_no_auth() resp = await client.get(url) diff --git a/tests/components/august/test_config_flow.py b/tests/components/august/test_config_flow.py index fdebb8d5c4636d..b3138342b8c67d 100644 --- a/tests/components/august/test_config_flow.py +++ b/tests/components/august/test_config_flow.py @@ -5,7 +5,6 @@ from yalexs.authenticator_common import ValidationResult from yalexs.manager.exceptions import CannotConnect, InvalidAuth, RequireValidation -from homeassistant import config_entries from homeassistant.components.august.const import ( CONF_ACCESS_TOKEN_CACHE_FILE, CONF_BRAND, @@ -14,6 +13,7 @@ DOMAIN, VERIFICATION_CODE_KEY, ) +from homeassistant.config_entries import SOURCE_USER from homeassistant.const import CONF_PASSWORD, CONF_TIMEOUT, CONF_USERNAME from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -25,7 +25,7 @@ async def test_form(hass: HomeAssistant) -> None: """Test we get the form.""" result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_USER} + DOMAIN, context={"source": SOURCE_USER} ) assert result["type"] is FlowResultType.FORM assert result["errors"] == {} @@ -66,7 +66,7 @@ async def test_form(hass: HomeAssistant) -> None: async def test_form_invalid_auth(hass: HomeAssistant) -> None: """Test we handle invalid auth.""" result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_USER} + DOMAIN, context={"source": SOURCE_USER} ) with patch( @@ -90,7 +90,7 @@ async def test_form_invalid_auth(hass: HomeAssistant) -> None: async def test_user_unexpected_exception(hass: HomeAssistant) -> None: """Test we handle an unexpected exception.""" result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_USER} + DOMAIN, context={"source": SOURCE_USER} ) with patch( @@ -115,7 +115,7 @@ async def test_user_unexpected_exception(hass: HomeAssistant) -> None: async def test_form_cannot_connect(hass: HomeAssistant) -> None: """Test we handle cannot connect error.""" result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_USER} + DOMAIN, context={"source": SOURCE_USER} ) with patch( @@ -138,7 +138,7 @@ async def test_form_cannot_connect(hass: HomeAssistant) -> None: async def test_form_needs_validate(hass: HomeAssistant) -> None: """Test we present validation when we need to validate.""" result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_USER} + DOMAIN, context={"source": SOURCE_USER} ) with ( @@ -248,9 +248,7 @@ async def test_form_reauth(hass: HomeAssistant) -> None: ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_REAUTH}, data=entry.data - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["errors"] == {} @@ -294,9 +292,7 @@ async def test_form_reauth_with_2fa(hass: HomeAssistant) -> None: ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_REAUTH}, data=entry.data - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["errors"] == {} @@ -371,7 +367,7 @@ async def test_switching_brands(hass: HomeAssistant) -> None: ) entry.add_to_hass(hass) result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_USER} + DOMAIN, context={"source": SOURCE_USER} ) assert result["type"] is FlowResultType.FORM assert result["errors"] == {} @@ -389,7 +385,7 @@ async def test_switching_brands(hass: HomeAssistant) -> None: result2 = await hass.config_entries.flow.async_configure( result["flow_id"], { - CONF_BRAND: "yale_home", + CONF_BRAND: "yale_access", CONF_LOGIN_METHOD: "email", CONF_USERNAME: "my@email.tld", CONF_PASSWORD: "test-password", @@ -400,4 +396,4 @@ async def test_switching_brands(hass: HomeAssistant) -> None: assert result2["type"] is FlowResultType.ABORT assert result2["reason"] == "reauth_successful" assert len(mock_setup_entry.mock_calls) == 1 - assert entry.data[CONF_BRAND] == "yale_home" + assert entry.data[CONF_BRAND] == "yale_access" diff --git a/tests/components/august/test_event.py b/tests/components/august/test_event.py index 61b7560f46207a..0bb482c5b89bcc 100644 --- a/tests/components/august/test_event.py +++ b/tests/components/august/test_event.py @@ -1,13 +1,12 @@ """The event tests for the august.""" -import datetime -from unittest.mock import Mock, patch +from unittest.mock import Mock +from freezegun.api import FrozenDateTimeFactory from yalexs.pubnub_async import AugustPubNub from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN from homeassistant.core import HomeAssistant -import homeassistant.util.dt as dt_util from .mocks import ( _create_august_with_devices, @@ -45,7 +44,9 @@ async def test_create_doorbell_offline(hass: HomeAssistant) -> None: assert doorbell_state.state == STATE_UNAVAILABLE -async def test_create_doorbell_with_motion(hass: HomeAssistant) -> None: +async def test_create_doorbell_with_motion( + hass: HomeAssistant, freezer: FrozenDateTimeFactory +) -> None: """Test creation of a doorbell.""" doorbell_one = await _mock_doorbell_from_fixture(hass, "get_doorbell.json") activities = await _mock_activities_from_fixture( @@ -61,19 +62,16 @@ async def test_create_doorbell_with_motion(hass: HomeAssistant) -> None: assert doorbell_state is not None assert doorbell_state.state == STATE_UNKNOWN - new_time = dt_util.utcnow() + datetime.timedelta(seconds=40) - native_time = datetime.datetime.now() + datetime.timedelta(seconds=40) - with patch( - "homeassistant.components.august.util._native_datetime", - return_value=native_time, - ): - async_fire_time_changed(hass, new_time) - await hass.async_block_till_done() + freezer.tick(40) + async_fire_time_changed(hass) + await hass.async_block_till_done() motion_state = hass.states.get("event.k98gidt45gul_name_motion") assert motion_state.state == isotime -async def test_doorbell_update_via_pubnub(hass: HomeAssistant) -> None: +async def test_doorbell_update_via_pubnub( + hass: HomeAssistant, freezer: FrozenDateTimeFactory +) -> None: """Test creation of a doorbell that can be updated via pubnub.""" doorbell_one = await _mock_doorbell_from_fixture(hass, "get_doorbell.json") pubnub = AugustPubNub() @@ -125,14 +123,9 @@ async def test_doorbell_update_via_pubnub(hass: HomeAssistant) -> None: assert motion_state.state != STATE_UNKNOWN isotime = motion_state.state - new_time = dt_util.utcnow() + datetime.timedelta(seconds=40) - native_time = datetime.datetime.now() + datetime.timedelta(seconds=40) - with patch( - "homeassistant.components.august.util._native_datetime", - return_value=native_time, - ): - async_fire_time_changed(hass, new_time) - await hass.async_block_till_done() + freezer.tick(40) + async_fire_time_changed(hass) + await hass.async_block_till_done() motion_state = hass.states.get("event.k98gidt45gul_name_motion") assert motion_state is not None @@ -155,14 +148,9 @@ async def test_doorbell_update_via_pubnub(hass: HomeAssistant) -> None: assert doorbell_state.state != STATE_UNKNOWN isotime = motion_state.state - new_time = dt_util.utcnow() + datetime.timedelta(seconds=40) - native_time = datetime.datetime.now() + datetime.timedelta(seconds=40) - with patch( - "homeassistant.components.august.util._native_datetime", - return_value=native_time, - ): - async_fire_time_changed(hass, new_time) - await hass.async_block_till_done() + freezer.tick(40) + async_fire_time_changed(hass) + await hass.async_block_till_done() doorbell_state = hass.states.get("event.k98gidt45gul_name_doorbell") assert doorbell_state is not None diff --git a/tests/components/august/test_init.py b/tests/components/august/test_init.py index 8261e32d6685fd..1bbe8033ec83fe 100644 --- a/tests/components/august/test_init.py +++ b/tests/components/august/test_init.py @@ -5,6 +5,7 @@ from aiohttp import ClientResponseError import pytest from yalexs.authenticator_common import AuthenticationState +from yalexs.const import Brand from yalexs.exceptions import AugustApiAIOHTTPError from homeassistant.components.august.const import DOMAIN @@ -20,7 +21,11 @@ ) from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError -from homeassistant.helpers import device_registry as dr, entity_registry as er +from homeassistant.helpers import ( + device_registry as dr, + entity_registry as er, + issue_registry as ir, +) from homeassistant.setup import async_setup_component from .mocks import ( @@ -122,16 +127,16 @@ def _unlock_return_activities_side_effect(access_token, device_id): "unlock_return_activities": _unlock_return_activities_side_effect }, ) - last_err = None data = {ATTR_ENTITY_ID: "lock.a6697750d607098bae8d6baa11ef8063_name"} - try: + + with pytest.raises( + HomeAssistantError, + match=( + "A6697750D607098BAE8D6BAA11EF8063 Name: This should bubble up as its user" + " consumable" + ), + ): await hass.services.async_call(LOCK_DOMAIN, SERVICE_UNLOCK, data, blocking=True) - except HomeAssistantError as err: - last_err = err - assert str(last_err) == ( - "A6697750D607098BAE8D6BAA11EF8063 Name: This should bubble up as its user" - " consumable" - ) async def test_lock_throws_august_api_http_error(hass: HomeAssistant) -> None: @@ -152,16 +157,15 @@ def _lock_return_activities_side_effect(access_token, device_id): "lock_return_activities": _lock_return_activities_side_effect }, ) - last_err = None data = {ATTR_ENTITY_ID: "lock.a6697750d607098bae8d6baa11ef8063_name"} - try: + with pytest.raises( + HomeAssistantError, + match=( + "A6697750D607098BAE8D6BAA11EF8063 Name: This should bubble up as its user" + " consumable" + ), + ): await hass.services.async_call(LOCK_DOMAIN, SERVICE_LOCK, data, blocking=True) - except HomeAssistantError as err: - last_err = err - assert str(last_err) == ( - "A6697750D607098BAE8D6BAA11EF8063 Name: This should bubble up as its user" - " consumable" - ) async def test_open_throws_hass_service_not_supported_error( @@ -371,6 +375,7 @@ async def test_load_unload(hass: HomeAssistant) -> None: await hass.config_entries.async_unload(config_entry.entry_id) await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.NOT_LOADED async def test_load_triggers_ble_discovery( @@ -420,3 +425,24 @@ async def test_device_remove_devices( ) response = await client.remove_device(dead_device_entry.id, config_entry.entry_id) assert response["success"] + + +async def test_brand_migration_issue(hass: HomeAssistant) -> None: + """Test creating and removing the brand migration issue.""" + august_operative_lock = await _mock_operative_august_lock_detail(hass) + config_entry = await _create_august_with_devices( + hass, [august_operative_lock], brand=Brand.YALE_HOME + ) + + assert config_entry.state is ConfigEntryState.LOADED + + issue_reg = ir.async_get(hass) + issue_entry = issue_reg.async_get_issue(DOMAIN, "yale_brand_migration") + assert issue_entry + assert issue_entry.severity == ir.IssueSeverity.CRITICAL + assert issue_entry.translation_placeholders == { + "migrate_url": "https://my.home-assistant.io/redirect/config_flow_start?domain=yale" + } + + await hass.config_entries.async_remove(config_entry.entry_id) + assert not issue_reg.async_get_issue(DOMAIN, "yale_brand_migration") diff --git a/tests/components/august/test_lock.py b/tests/components/august/test_lock.py index 8bb71826d24089..e786cebf3e1b26 100644 --- a/tests/components/august/test_lock.py +++ b/tests/components/august/test_lock.py @@ -6,6 +6,7 @@ from aiohttp import ClientResponseError from freezegun.api import FrozenDateTimeFactory import pytest +from syrupy import SnapshotAssertion from yalexs.manager.activity import INITIAL_LOCK_RESYNC_TIME from yalexs.pubnub_async import AugustPubNub @@ -43,7 +44,7 @@ async def test_lock_device_registry( - hass: HomeAssistant, device_registry: dr.DeviceRegistry + hass: HomeAssistant, device_registry: dr.DeviceRegistry, snapshot: SnapshotAssertion ) -> None: """Test creation of a lock with doorsense and bridge ands up in the registry.""" lock_one = await _mock_doorsense_enabled_august_lock_detail(hass) @@ -52,10 +53,7 @@ async def test_lock_device_registry( reg_device = device_registry.async_get_device( identifiers={("august", "online_with_doorsense")} ) - assert reg_device.model == "AUG-MD01" - assert reg_device.sw_version == "undefined-4.3.0-1.8.14" - assert reg_device.name == "online_with_doorsense Name" - assert reg_device.manufacturer == "August Home Inc." + assert reg_device == snapshot async def test_lock_changed_by(hass: HomeAssistant) -> None: @@ -65,14 +63,10 @@ async def test_lock_changed_by(hass: HomeAssistant) -> None: activities = await _mock_activities_from_fixture(hass, "get_activity.lock.json") await _create_august_with_devices(hass, [lock_one], activities=activities) - lock_online_with_doorsense_name = hass.states.get("lock.online_with_doorsense_name") + lock_state = hass.states.get("lock.online_with_doorsense_name") - assert lock_online_with_doorsense_name.state == STATE_LOCKED - - assert ( - lock_online_with_doorsense_name.attributes.get("changed_by") - == "Your favorite elven princess" - ) + assert lock_state.state == STATE_LOCKED + assert lock_state.attributes["changed_by"] == "Your favorite elven princess" async def test_state_locking(hass: HomeAssistant) -> None: @@ -82,9 +76,7 @@ async def test_state_locking(hass: HomeAssistant) -> None: activities = await _mock_activities_from_fixture(hass, "get_activity.locking.json") await _create_august_with_devices(hass, [lock_one], activities=activities) - lock_online_with_doorsense_name = hass.states.get("lock.online_with_doorsense_name") - - assert lock_online_with_doorsense_name.state == STATE_LOCKING + assert hass.states.get("lock.online_with_doorsense_name").state == STATE_LOCKING async def test_state_unlocking(hass: HomeAssistant) -> None: @@ -96,9 +88,7 @@ async def test_state_unlocking(hass: HomeAssistant) -> None: ) await _create_august_with_devices(hass, [lock_one], activities=activities) - lock_online_with_doorsense_name = hass.states.get("lock.online_with_doorsense_name") - - assert lock_online_with_doorsense_name.state == STATE_UNLOCKING + assert hass.states.get("lock.online_with_doorsense_name").state == STATE_UNLOCKING async def test_state_jammed(hass: HomeAssistant) -> None: @@ -108,9 +98,7 @@ async def test_state_jammed(hass: HomeAssistant) -> None: activities = await _mock_activities_from_fixture(hass, "get_activity.jammed.json") await _create_august_with_devices(hass, [lock_one], activities=activities) - lock_online_with_doorsense_name = hass.states.get("lock.online_with_doorsense_name") - - assert lock_online_with_doorsense_name.state == STATE_JAMMED + assert hass.states.get("lock.online_with_doorsense_name").state == STATE_JAMMED async def test_one_lock_operation( @@ -119,35 +107,27 @@ async def test_one_lock_operation( """Test creation of a lock with doorsense and bridge.""" lock_one = await _mock_doorsense_enabled_august_lock_detail(hass) await _create_august_with_devices(hass, [lock_one]) + states = hass.states - lock_online_with_doorsense_name = hass.states.get("lock.online_with_doorsense_name") + lock_state = states.get("lock.online_with_doorsense_name") - assert lock_online_with_doorsense_name.state == STATE_LOCKED + assert lock_state.state == STATE_LOCKED - assert lock_online_with_doorsense_name.attributes.get("battery_level") == 92 - assert ( - lock_online_with_doorsense_name.attributes.get("friendly_name") - == "online_with_doorsense Name" - ) + assert lock_state.attributes["battery_level"] == 92 + assert lock_state.attributes["friendly_name"] == "online_with_doorsense Name" data = {ATTR_ENTITY_ID: "lock.online_with_doorsense_name"} await hass.services.async_call(LOCK_DOMAIN, SERVICE_UNLOCK, data, blocking=True) - await hass.async_block_till_done() - lock_online_with_doorsense_name = hass.states.get("lock.online_with_doorsense_name") - assert lock_online_with_doorsense_name.state == STATE_UNLOCKED + lock_state = states.get("lock.online_with_doorsense_name") + assert lock_state.state == STATE_UNLOCKED - assert lock_online_with_doorsense_name.attributes.get("battery_level") == 92 - assert ( - lock_online_with_doorsense_name.attributes.get("friendly_name") - == "online_with_doorsense Name" - ) + assert lock_state.attributes["battery_level"] == 92 + assert lock_state.attributes["friendly_name"] == "online_with_doorsense Name" await hass.services.async_call(LOCK_DOMAIN, SERVICE_LOCK, data, blocking=True) - await hass.async_block_till_done() - lock_online_with_doorsense_name = hass.states.get("lock.online_with_doorsense_name") - assert lock_online_with_doorsense_name.state == STATE_LOCKED + assert states.get("lock.online_with_doorsense_name").state == STATE_LOCKED # No activity means it will be unavailable until the activity feed has data lock_operator_sensor = entity_registry.async_get( @@ -155,8 +135,7 @@ async def test_one_lock_operation( ) assert lock_operator_sensor assert ( - hass.states.get("sensor.online_with_doorsense_name_operator").state - == STATE_UNKNOWN + states.get("sensor.online_with_doorsense_name_operator").state == STATE_UNKNOWN ) @@ -170,7 +149,6 @@ async def test_open_lock_operation(hass: HomeAssistant) -> None: data = {ATTR_ENTITY_ID: "lock.online_with_unlatch_name"} await hass.services.async_call(LOCK_DOMAIN, SERVICE_OPEN, data, blocking=True) - await hass.async_block_till_done() lock_online_with_unlatch_name = hass.states.get("lock.online_with_unlatch_name") assert lock_online_with_unlatch_name.state == STATE_UNLOCKED @@ -189,12 +167,10 @@ async def test_open_lock_operation_pubnub_connected( await _create_august_with_devices(hass, [lock_with_unlatch], pubnub=pubnub) pubnub.connected = True - lock_online_with_unlatch_name = hass.states.get("lock.online_with_unlatch_name") - assert lock_online_with_unlatch_name.state == STATE_LOCKED + assert hass.states.get("lock.online_with_unlatch_name").state == STATE_LOCKED data = {ATTR_ENTITY_ID: "lock.online_with_unlatch_name"} await hass.services.async_call(LOCK_DOMAIN, SERVICE_OPEN, data, blocking=True) - await hass.async_block_till_done() pubnub.message( pubnub, @@ -209,8 +185,7 @@ async def test_open_lock_operation_pubnub_connected( await hass.async_block_till_done() await hass.async_block_till_done() - lock_online_with_unlatch_name = hass.states.get("lock.online_with_unlatch_name") - assert lock_online_with_unlatch_name.state == STATE_UNLOCKED + assert hass.states.get("lock.online_with_unlatch_name").state == STATE_UNLOCKED await hass.async_block_till_done() @@ -227,19 +202,15 @@ async def test_one_lock_operation_pubnub_connected( await _create_august_with_devices(hass, [lock_one], pubnub=pubnub) pubnub.connected = True - lock_online_with_doorsense_name = hass.states.get("lock.online_with_doorsense_name") + lock_state = hass.states.get("lock.online_with_doorsense_name") - assert lock_online_with_doorsense_name.state == STATE_LOCKED + assert lock_state.state == STATE_LOCKED - assert lock_online_with_doorsense_name.attributes.get("battery_level") == 92 - assert ( - lock_online_with_doorsense_name.attributes.get("friendly_name") - == "online_with_doorsense Name" - ) + assert lock_state.attributes["battery_level"] == 92 + assert lock_state.attributes["friendly_name"] == "online_with_doorsense Name" data = {ATTR_ENTITY_ID: "lock.online_with_doorsense_name"} await hass.services.async_call(LOCK_DOMAIN, SERVICE_UNLOCK, data, blocking=True) - await hass.async_block_till_done() pubnub.message( pubnub, @@ -254,17 +225,13 @@ async def test_one_lock_operation_pubnub_connected( await hass.async_block_till_done() await hass.async_block_till_done() - lock_online_with_doorsense_name = hass.states.get("lock.online_with_doorsense_name") - assert lock_online_with_doorsense_name.state == STATE_UNLOCKED + lock_state = hass.states.get("lock.online_with_doorsense_name") + assert lock_state.state == STATE_UNLOCKED - assert lock_online_with_doorsense_name.attributes.get("battery_level") == 92 - assert ( - lock_online_with_doorsense_name.attributes.get("friendly_name") - == "online_with_doorsense Name" - ) + assert lock_state.attributes["battery_level"] == 92 + assert lock_state.attributes["friendly_name"] == "online_with_doorsense Name" await hass.services.async_call(LOCK_DOMAIN, SERVICE_LOCK, data, blocking=True) - await hass.async_block_till_done() pubnub.message( pubnub, @@ -279,8 +246,8 @@ async def test_one_lock_operation_pubnub_connected( await hass.async_block_till_done() await hass.async_block_till_done() - lock_online_with_doorsense_name = hass.states.get("lock.online_with_doorsense_name") - assert lock_online_with_doorsense_name.state == STATE_LOCKED + lock_state = hass.states.get("lock.online_with_doorsense_name") + assert lock_state.state == STATE_LOCKED # No activity means it will be unavailable until the activity feed has data lock_operator_sensor = entity_registry.async_get( @@ -306,8 +273,8 @@ async def test_one_lock_operation_pubnub_connected( ) await hass.async_block_till_done() - lock_online_with_doorsense_name = hass.states.get("lock.online_with_doorsense_name") - assert lock_online_with_doorsense_name.state == STATE_UNLOCKED + lock_state = hass.states.get("lock.online_with_doorsense_name") + assert lock_state.state == STATE_UNLOCKED async def test_lock_jammed(hass: HomeAssistant) -> None: @@ -325,22 +292,18 @@ def _unlock_return_activities_side_effect(access_token, device_id): }, ) - lock_online_with_doorsense_name = hass.states.get("lock.online_with_doorsense_name") + lock_state = hass.states.get("lock.online_with_doorsense_name") - assert lock_online_with_doorsense_name.state == STATE_LOCKED + assert lock_state.state == STATE_LOCKED - assert lock_online_with_doorsense_name.attributes.get("battery_level") == 92 - assert ( - lock_online_with_doorsense_name.attributes.get("friendly_name") - == "online_with_doorsense Name" - ) + assert lock_state.attributes["battery_level"] == 92 + assert lock_state.attributes["friendly_name"] == "online_with_doorsense Name" data = {ATTR_ENTITY_ID: "lock.online_with_doorsense_name"} await hass.services.async_call(LOCK_DOMAIN, SERVICE_UNLOCK, data, blocking=True) - await hass.async_block_till_done() - lock_online_with_doorsense_name = hass.states.get("lock.online_with_doorsense_name") - assert lock_online_with_doorsense_name.state == STATE_JAMMED + lock_state = hass.states.get("lock.online_with_doorsense_name") + assert lock_state.state == STATE_JAMMED async def test_lock_throws_exception_on_unknown_status_code( @@ -360,15 +323,12 @@ def _unlock_return_activities_side_effect(access_token, device_id): }, ) - lock_online_with_doorsense_name = hass.states.get("lock.online_with_doorsense_name") + lock_state = hass.states.get("lock.online_with_doorsense_name") - assert lock_online_with_doorsense_name.state == STATE_LOCKED + assert lock_state.state == STATE_LOCKED - assert lock_online_with_doorsense_name.attributes.get("battery_level") == 92 - assert ( - lock_online_with_doorsense_name.attributes.get("friendly_name") - == "online_with_doorsense Name" - ) + assert lock_state.attributes["battery_level"] == 92 + assert lock_state.attributes["friendly_name"] == "online_with_doorsense Name" data = {ATTR_ENTITY_ID: "lock.online_with_doorsense_name"} with pytest.raises(ClientResponseError): @@ -383,9 +343,7 @@ async def test_one_lock_unknown_state(hass: HomeAssistant) -> None: ) await _create_august_with_devices(hass, [lock_one]) - lock_brokenid_name = hass.states.get("lock.brokenid_name") - - assert lock_brokenid_name.state == STATE_UNKNOWN + assert hass.states.get("lock.brokenid_name").state == STATE_UNKNOWN async def test_lock_bridge_offline(hass: HomeAssistant) -> None: @@ -397,9 +355,7 @@ async def test_lock_bridge_offline(hass: HomeAssistant) -> None: ) await _create_august_with_devices(hass, [lock_one], activities=activities) - lock_online_with_doorsense_name = hass.states.get("lock.online_with_doorsense_name") - - assert lock_online_with_doorsense_name.state == STATE_UNAVAILABLE + assert hass.states.get("lock.online_with_doorsense_name").state == STATE_UNAVAILABLE async def test_lock_bridge_online(hass: HomeAssistant) -> None: @@ -411,14 +367,13 @@ async def test_lock_bridge_online(hass: HomeAssistant) -> None: ) await _create_august_with_devices(hass, [lock_one], activities=activities) - lock_online_with_doorsense_name = hass.states.get("lock.online_with_doorsense_name") - - assert lock_online_with_doorsense_name.state == STATE_LOCKED + assert hass.states.get("lock.online_with_doorsense_name").state == STATE_LOCKED async def test_lock_update_via_pubnub(hass: HomeAssistant) -> None: """Test creation of a lock with doorsense and bridge.""" lock_one = await _mock_doorsense_enabled_august_lock_detail(hass) + states = hass.states assert lock_one.pubsub_channel == "pubsub" pubnub = AugustPubNub() @@ -428,9 +383,7 @@ async def test_lock_update_via_pubnub(hass: HomeAssistant) -> None: ) pubnub.connected = True - lock_online_with_doorsense_name = hass.states.get("lock.online_with_doorsense_name") - - assert lock_online_with_doorsense_name.state == STATE_LOCKED + assert states.get("lock.online_with_doorsense_name").state == STATE_LOCKED pubnub.message( pubnub, @@ -446,8 +399,7 @@ async def test_lock_update_via_pubnub(hass: HomeAssistant) -> None: await hass.async_block_till_done() await hass.async_block_till_done() - lock_online_with_doorsense_name = hass.states.get("lock.online_with_doorsense_name") - assert lock_online_with_doorsense_name.state == STATE_UNLOCKING + assert states.get("lock.online_with_doorsense_name").state == STATE_UNLOCKING pubnub.message( pubnub, @@ -463,25 +415,21 @@ async def test_lock_update_via_pubnub(hass: HomeAssistant) -> None: await hass.async_block_till_done() await hass.async_block_till_done() - lock_online_with_doorsense_name = hass.states.get("lock.online_with_doorsense_name") - assert lock_online_with_doorsense_name.state == STATE_LOCKING + assert states.get("lock.online_with_doorsense_name").state == STATE_LOCKING async_fire_time_changed(hass, dt_util.utcnow() + datetime.timedelta(seconds=30)) await hass.async_block_till_done() - lock_online_with_doorsense_name = hass.states.get("lock.online_with_doorsense_name") - assert lock_online_with_doorsense_name.state == STATE_LOCKING + assert hass.states.get("lock.online_with_doorsense_name").state == STATE_LOCKING pubnub.connected = True async_fire_time_changed(hass, dt_util.utcnow() + datetime.timedelta(seconds=30)) await hass.async_block_till_done() - lock_online_with_doorsense_name = hass.states.get("lock.online_with_doorsense_name") - assert lock_online_with_doorsense_name.state == STATE_LOCKING + assert states.get("lock.online_with_doorsense_name").state == STATE_LOCKING # Ensure pubnub status is always preserved async_fire_time_changed(hass, dt_util.utcnow() + datetime.timedelta(hours=2)) await hass.async_block_till_done() - lock_online_with_doorsense_name = hass.states.get("lock.online_with_doorsense_name") - assert lock_online_with_doorsense_name.state == STATE_LOCKING + assert states.get("lock.online_with_doorsense_name").state == STATE_LOCKING pubnub.message( pubnub, @@ -496,13 +444,11 @@ async def test_lock_update_via_pubnub(hass: HomeAssistant) -> None: await hass.async_block_till_done() await hass.async_block_till_done() - lock_online_with_doorsense_name = hass.states.get("lock.online_with_doorsense_name") - assert lock_online_with_doorsense_name.state == STATE_UNLOCKING + assert states.get("lock.online_with_doorsense_name").state == STATE_UNLOCKING async_fire_time_changed(hass, dt_util.utcnow() + datetime.timedelta(hours=4)) await hass.async_block_till_done() - lock_online_with_doorsense_name = hass.states.get("lock.online_with_doorsense_name") - assert lock_online_with_doorsense_name.state == STATE_UNLOCKING + assert states.get("lock.online_with_doorsense_name").state == STATE_UNLOCKING await hass.config_entries.async_unload(config_entry.entry_id) await hass.async_block_till_done() diff --git a/tests/components/august/test_sensor.py b/tests/components/august/test_sensor.py index 67223e9dff0105..2d72d287ce37ae 100644 --- a/tests/components/august/test_sensor.py +++ b/tests/components/august/test_sensor.py @@ -28,13 +28,9 @@ async def test_create_doorbell(hass: HomeAssistant) -> None: doorbell_one = await _mock_doorbell_from_fixture(hass, "get_doorbell.json") await _create_august_with_devices(hass, [doorbell_one]) - sensor_k98gidt45gul_name_battery = hass.states.get( - "sensor.k98gidt45gul_name_battery" - ) - assert sensor_k98gidt45gul_name_battery.state == "96" - assert ( - sensor_k98gidt45gul_name_battery.attributes["unit_of_measurement"] == PERCENTAGE - ) + battery_state = hass.states.get("sensor.k98gidt45gul_name_battery") + assert battery_state.state == "96" + assert battery_state.attributes["unit_of_measurement"] == PERCENTAGE async def test_create_doorbell_offline( @@ -44,9 +40,9 @@ async def test_create_doorbell_offline( doorbell_one = await _mock_doorbell_from_fixture(hass, "get_doorbell.offline.json") await _create_august_with_devices(hass, [doorbell_one]) - sensor_tmt100_name_battery = hass.states.get("sensor.tmt100_name_battery") - assert sensor_tmt100_name_battery.state == "81" - assert sensor_tmt100_name_battery.attributes["unit_of_measurement"] == PERCENTAGE + battery_state = hass.states.get("sensor.tmt100_name_battery") + assert battery_state.state == "81" + assert battery_state.attributes["unit_of_measurement"] == PERCENTAGE entry = entity_registry.async_get("sensor.tmt100_name_battery") assert entry @@ -60,8 +56,7 @@ async def test_create_doorbell_hardwired(hass: HomeAssistant) -> None: ) await _create_august_with_devices(hass, [doorbell_one]) - sensor_tmt100_name_battery = hass.states.get("sensor.tmt100_name_battery") - assert sensor_tmt100_name_battery is None + assert hass.states.get("sensor.tmt100_name_battery") is None async def test_create_lock_with_linked_keypad( @@ -71,25 +66,21 @@ async def test_create_lock_with_linked_keypad( lock_one = await _mock_lock_from_fixture(hass, "get_lock.doorsense_init.json") await _create_august_with_devices(hass, [lock_one]) - sensor_a6697750d607098bae8d6baa11ef8063_name_battery = hass.states.get( + battery_state = hass.states.get( "sensor.a6697750d607098bae8d6baa11ef8063_name_battery" ) - assert sensor_a6697750d607098bae8d6baa11ef8063_name_battery.state == "88" - assert ( - sensor_a6697750d607098bae8d6baa11ef8063_name_battery.attributes[ - "unit_of_measurement" - ] - == PERCENTAGE - ) + assert battery_state.state == "88" + assert battery_state.attributes["unit_of_measurement"] == PERCENTAGE + entry = entity_registry.async_get( "sensor.a6697750d607098bae8d6baa11ef8063_name_battery" ) assert entry assert entry.unique_id == "A6697750D607098BAE8D6BAA11EF8063_device_battery" - state = hass.states.get("sensor.front_door_lock_keypad_battery") - assert state.state == "62" - assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == PERCENTAGE + keypad_battery_state = hass.states.get("sensor.front_door_lock_keypad_battery") + assert keypad_battery_state.state == "62" + assert keypad_battery_state.attributes[ATTR_UNIT_OF_MEASUREMENT] == PERCENTAGE entry = entity_registry.async_get("sensor.front_door_lock_keypad_battery") assert entry assert entry.unique_id == "5bc65c24e6ef2a263e1450a8_linked_keypad_battery" @@ -101,42 +92,32 @@ async def test_create_lock_with_low_battery_linked_keypad( """Test creation of a lock with a linked keypad that both have a battery.""" lock_one = await _mock_lock_from_fixture(hass, "get_lock.low_keypad_battery.json") await _create_august_with_devices(hass, [lock_one]) + states = hass.states - sensor_a6697750d607098bae8d6baa11ef8063_name_battery = hass.states.get( - "sensor.a6697750d607098bae8d6baa11ef8063_name_battery" - ) - assert sensor_a6697750d607098bae8d6baa11ef8063_name_battery.state == "88" - assert ( - sensor_a6697750d607098bae8d6baa11ef8063_name_battery.attributes[ - "unit_of_measurement" - ] - == PERCENTAGE - ) + battery_state = states.get("sensor.a6697750d607098bae8d6baa11ef8063_name_battery") + assert battery_state.state == "88" + assert battery_state.attributes["unit_of_measurement"] == PERCENTAGE entry = entity_registry.async_get( "sensor.a6697750d607098bae8d6baa11ef8063_name_battery" ) assert entry assert entry.unique_id == "A6697750D607098BAE8D6BAA11EF8063_device_battery" - state = hass.states.get("sensor.front_door_lock_keypad_battery") - assert state.state == "10" - assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == PERCENTAGE + keypad_battery_state = states.get("sensor.front_door_lock_keypad_battery") + assert keypad_battery_state.state == "10" + assert keypad_battery_state.attributes[ATTR_UNIT_OF_MEASUREMENT] == PERCENTAGE entry = entity_registry.async_get("sensor.front_door_lock_keypad_battery") assert entry assert entry.unique_id == "5bc65c24e6ef2a263e1450a8_linked_keypad_battery" # No activity means it will be unavailable until someone unlocks/locks it - lock_operator_sensor = entity_registry.async_get( + operator_entry = entity_registry.async_get( "sensor.a6697750d607098bae8d6baa11ef8063_name_operator" ) - assert ( - lock_operator_sensor.unique_id - == "A6697750D607098BAE8D6BAA11EF8063_lock_operator" - ) - assert ( - hass.states.get("sensor.a6697750d607098bae8d6baa11ef8063_name_operator").state - == STATE_UNKNOWN - ) + assert operator_entry.unique_id == "A6697750D607098BAE8D6BAA11EF8063_lock_operator" + + operator_state = states.get("sensor.a6697750d607098bae8d6baa11ef8063_name_operator") + assert operator_state.state == STATE_UNKNOWN async def test_lock_operator_bluetooth( diff --git a/tests/components/aussie_broadband/test_config_flow.py b/tests/components/aussie_broadband/test_config_flow.py index 6ee674ab0f4a46..76e96c5cc02cce 100644 --- a/tests/components/aussie_broadband/test_config_flow.py +++ b/tests/components/aussie_broadband/test_config_flow.py @@ -13,6 +13,8 @@ from .common import FAKE_DATA, FAKE_SERVICES +from tests.common import MockConfigEntry + TEST_USERNAME = FAKE_DATA[CONF_USERNAME] TEST_PASSWORD = FAKE_DATA[CONF_PASSWORD] @@ -163,41 +165,15 @@ async def test_form_network_issue(hass: HomeAssistant) -> None: async def test_reauth(hass: HomeAssistant) -> None: """Test reauth flow.""" - - # Test reauth but the entry doesn't exist - result1 = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_REAUTH}, data=FAKE_DATA + mock_entry = MockConfigEntry( + domain=DOMAIN, + data=FAKE_DATA, + unique_id=FAKE_DATA[CONF_USERNAME], ) - - with ( - patch("aussiebb.asyncio.AussieBB.__init__", return_value=None), - patch("aussiebb.asyncio.AussieBB.login", return_value=True), - patch( - "aussiebb.asyncio.AussieBB.get_services", return_value=[FAKE_SERVICES[0]] - ), - patch( - "homeassistant.components.aussie_broadband.async_setup_entry", - return_value=True, - ), - ): - result2 = await hass.config_entries.flow.async_configure( - result1["flow_id"], - { - CONF_PASSWORD: TEST_PASSWORD, - }, - ) - await hass.async_block_till_done() - - assert result2["type"] is FlowResultType.CREATE_ENTRY - assert result2["title"] == TEST_USERNAME - assert result2["data"] == FAKE_DATA + mock_entry.add_to_hass(hass) # Test failed reauth - result5 = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_REAUTH}, - data=FAKE_DATA, - ) + result5 = await mock_entry.start_reauth_flow(hass) assert result5["step_id"] == "reauth_confirm" with ( diff --git a/tests/components/awair/test_config_flow.py b/tests/components/awair/test_config_flow.py index ab9f5faa425f72..ac17cf414489fd 100644 --- a/tests/components/awair/test_config_flow.py +++ b/tests/components/awair/test_config_flow.py @@ -7,7 +7,7 @@ from python_awair.exceptions import AuthError, AwairError from homeassistant.components.awair.const import DOMAIN -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER, SOURCE_ZEROCONF +from homeassistant.config_entries import SOURCE_USER, SOURCE_ZEROCONF from homeassistant.const import CONF_ACCESS_TOKEN, CONF_HOST from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -136,11 +136,7 @@ async def test_reauth(hass: HomeAssistant, user, cloud_devices) -> None: ) mock_config.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH, "unique_id": CLOUD_UNIQUE_ID}, - data={**CLOUD_CONFIG, CONF_ACCESS_TOKEN: "blah"}, - ) + result = await mock_config.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" assert result["errors"] == {} @@ -180,11 +176,7 @@ async def test_reauth_error(hass: HomeAssistant) -> None: ) mock_config.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH, "unique_id": CLOUD_UNIQUE_ID}, - data={**CLOUD_CONFIG, CONF_ACCESS_TOKEN: "blah"}, - ) + result = await mock_config.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" assert result["errors"] == {} diff --git a/tests/components/axis/snapshots/test_camera.ambr b/tests/components/axis/snapshots/test_camera.ambr new file mode 100644 index 00000000000000..564ff96b3d8068 --- /dev/null +++ b/tests/components/axis/snapshots/test_camera.ambr @@ -0,0 +1,101 @@ +# serializer version: 1 +# name: test_camera[config_entry_options0-][camera.home-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'camera', + 'entity_category': None, + 'entity_id': 'camera.home', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': None, + 'platform': 'axis', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': None, + 'unique_id': '00:40:8c:12:34:56-camera', + 'unit_of_measurement': None, + }) +# --- +# name: test_camera[config_entry_options0-][camera.home-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'access_token': '1', + 'entity_picture': '/api/camera_proxy/camera.home?token=1', + 'friendly_name': 'home', + 'frontend_stream_type': , + 'supported_features': , + }), + 'context': , + 'entity_id': 'camera.home', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'idle', + }) +# --- +# name: test_camera[config_entry_options1-streamprofile=profile_1][camera.home-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'camera', + 'entity_category': None, + 'entity_id': 'camera.home', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': None, + 'platform': 'axis', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': None, + 'unique_id': '00:40:8c:12:34:56-camera', + 'unit_of_measurement': None, + }) +# --- +# name: test_camera[config_entry_options1-streamprofile=profile_1][camera.home-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'access_token': '1', + 'entity_picture': '/api/camera_proxy/camera.home?token=1', + 'friendly_name': 'home', + 'frontend_stream_type': , + 'supported_features': , + }), + 'context': , + 'entity_id': 'camera.home', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'idle', + }) +# --- diff --git a/tests/components/axis/test_camera.py b/tests/components/axis/test_camera.py index 00fe4391b0ca05..91e24a8c0c0f23 100644 --- a/tests/components/axis/test_camera.py +++ b/tests/components/axis/test_camera.py @@ -1,71 +1,77 @@ """Axis camera platform tests.""" +from unittest.mock import patch + import pytest +from syrupy import SnapshotAssertion from homeassistant.components import camera from homeassistant.components.axis.const import CONF_STREAM_PROFILE from homeassistant.components.camera import DOMAIN as CAMERA_DOMAIN -from homeassistant.const import STATE_IDLE +from homeassistant.const import Platform from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er +from .conftest import ConfigEntryFactoryType from .const import MAC, NAME +from tests.common import snapshot_platform -@pytest.mark.usefixtures("config_entry_setup") -async def test_camera(hass: HomeAssistant) -> None: - """Test that Axis camera platform is loaded properly.""" - assert len(hass.states.async_entity_ids(CAMERA_DOMAIN)) == 1 - entity_id = f"{CAMERA_DOMAIN}.{NAME}" +@pytest.fixture(autouse=True) +def mock_getrandbits(): + """Mock camera access token which normally is randomized.""" + with patch( + "homeassistant.components.camera.SystemRandom.getrandbits", + return_value=1, + ): + yield - cam = hass.states.get(entity_id) - assert cam.state == STATE_IDLE - assert cam.name == NAME - camera_entity = camera._get_camera_from_entity_id(hass, entity_id) - assert camera_entity.image_source == "http://1.2.3.4:80/axis-cgi/jpg/image.cgi" - assert camera_entity.mjpeg_source == "http://1.2.3.4:80/axis-cgi/mjpg/video.cgi" - assert ( - await camera_entity.stream_source() - == "rtsp://root:pass@1.2.3.4/axis-media/media.amp?videocodec=h264" - ) +PROPERTY_DATA = f"""root.Properties.API.HTTP.Version=3 +root.Properties.API.Metadata.Metadata=yes +root.Properties.API.Metadata.Version=1.0 +root.Properties.EmbeddedDevelopment.Version=2.16 +root.Properties.Firmware.BuildDate=Feb 15 2019 09:42 +root.Properties.Firmware.BuildNumber=26 +root.Properties.Firmware.Version=9.10.1 +root.Properties.System.SerialNumber={MAC} +""" # No image format data to signal camera support -@pytest.mark.parametrize("config_entry_options", [{CONF_STREAM_PROFILE: "profile_1"}]) -@pytest.mark.usefixtures("config_entry_setup") -async def test_camera_with_stream_profile(hass: HomeAssistant) -> None: - """Test that Axis camera entity is using the correct path with stream profike.""" - assert len(hass.states.async_entity_ids(CAMERA_DOMAIN)) == 1 +@pytest.mark.parametrize( + ("config_entry_options", "stream_profile"), + [ + ({}, ""), + ({CONF_STREAM_PROFILE: "profile_1"}, "streamprofile=profile_1"), + ], +) +async def test_camera( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + config_entry_factory: ConfigEntryFactoryType, + snapshot: SnapshotAssertion, + stream_profile: str, +) -> None: + """Test that Axis camera platform is loaded properly.""" + with patch("homeassistant.components.deconz.PLATFORMS", [Platform.CAMERA]): + config_entry = await config_entry_factory() + await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id) entity_id = f"{CAMERA_DOMAIN}.{NAME}" - - cam = hass.states.get(entity_id) - assert cam.state == STATE_IDLE - assert cam.name == NAME - camera_entity = camera._get_camera_from_entity_id(hass, entity_id) assert camera_entity.image_source == "http://1.2.3.4:80/axis-cgi/jpg/image.cgi" assert ( - camera_entity.mjpeg_source - == "http://1.2.3.4:80/axis-cgi/mjpg/video.cgi?streamprofile=profile_1" + camera_entity.mjpeg_source == "http://1.2.3.4:80/axis-cgi/mjpg/video.cgi" + f"{"" if not stream_profile else f"?{stream_profile}"}" ) assert ( await camera_entity.stream_source() - == "rtsp://root:pass@1.2.3.4/axis-media/media.amp?videocodec=h264&streamprofile=profile_1" + == "rtsp://root:pass@1.2.3.4/axis-media/media.amp?videocodec=h264" + f"{"" if not stream_profile else f"&{stream_profile}"}" ) -PROPERTY_DATA = f"""root.Properties.API.HTTP.Version=3 -root.Properties.API.Metadata.Metadata=yes -root.Properties.API.Metadata.Version=1.0 -root.Properties.EmbeddedDevelopment.Version=2.16 -root.Properties.Firmware.BuildDate=Feb 15 2019 09:42 -root.Properties.Firmware.BuildNumber=26 -root.Properties.Firmware.Version=9.10.1 -root.Properties.System.SerialNumber={MAC} -""" # No image format data to signal camera support - - @pytest.mark.parametrize("param_properties_payload", [PROPERTY_DATA]) @pytest.mark.usefixtures("config_entry_setup") async def test_camera_disabled(hass: HomeAssistant) -> None: diff --git a/tests/components/axis/test_config_flow.py b/tests/components/axis/test_config_flow.py index 5ceb6588fbde31..8591b4583c133d 100644 --- a/tests/components/axis/test_config_flow.py +++ b/tests/components/axis/test_config_flow.py @@ -17,7 +17,6 @@ ) from homeassistant.config_entries import ( SOURCE_DHCP, - SOURCE_REAUTH, SOURCE_RECONFIGURE, SOURCE_SSDP, SOURCE_USER, @@ -205,12 +204,7 @@ async def test_reauth_flow_update_configuration( assert config_entry_setup.data[CONF_USERNAME] == "root" assert config_entry_setup.data[CONF_PASSWORD] == "pass" - result = await hass.config_entries.flow.async_init( - AXIS_DOMAIN, - context={"source": SOURCE_REAUTH}, - data=config_entry_setup.data, - ) - + result = await config_entry_setup.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "user" diff --git a/tests/components/azure_devops/__init__.py b/tests/components/azure_devops/__init__.py index d636a6fda6d686..6414fe0257c915 100644 --- a/tests/components/azure_devops/__init__.py +++ b/tests/components/azure_devops/__init__.py @@ -1,9 +1,12 @@ """Tests for the Azure DevOps integration.""" +from datetime import datetime from typing import Final -from aioazuredevops.models.builds import Build, BuildDefinition +from aioazuredevops.models.build import Build, BuildDefinition from aioazuredevops.models.core import Project +from aioazuredevops.models.work_item import WorkItem, WorkItemFields +from aioazuredevops.models.work_item_type import Category, Icon, State, WorkItemType from homeassistant.components.azure_devops.const import CONF_ORG, CONF_PAT, CONF_PROJECT from homeassistant.core import HomeAssistant @@ -77,6 +80,55 @@ build_id=9876, ) +DEVOPS_WORK_ITEM_TYPES = [ + WorkItemType( + name="Bug", + reference_name="System.Bug", + description="Bug", + color="ff0000", + icon=Icon(id="1234", url="https://example.com/icon.png"), + is_disabled=False, + xml_form="", + fields=[], + field_instances=[], + transitions={}, + states=[ + State(name="New", color="ff0000", category=Category.PROPOSED), + State(name="Active", color="ff0000", category=Category.IN_PROGRESS), + State(name="Resolved", color="ff0000", category=Category.RESOLVED), + State(name="Closed", color="ff0000", category=Category.COMPLETED), + ], + url="", + ) +] + +DEVOPS_WORK_ITEM_IDS = [1] + +DEVOPS_WORK_ITEMS = [ + WorkItem( + id=1, + rev=1, + fields=WorkItemFields( + area_path="", + team_project="", + iteration_path="", + work_item_type="Bug", + state="New", + reason="New", + assigned_to=None, + created_date=datetime(2021, 1, 1), + created_by=None, + changed_date=datetime(2021, 1, 1), + changed_by=None, + comment_count=0, + title="Test", + microsoft_vsts_common_state_change_date=datetime(2021, 1, 1), + microsoft_vsts_common_priority=1, + ), + url="https://example.com", + ) +] + async def setup_integration( hass: HomeAssistant, diff --git a/tests/components/azure_devops/conftest.py b/tests/components/azure_devops/conftest.py index c65adaa4da51cb..54c730f9523b19 100644 --- a/tests/components/azure_devops/conftest.py +++ b/tests/components/azure_devops/conftest.py @@ -7,7 +7,16 @@ from homeassistant.components.azure_devops.const import DOMAIN -from . import DEVOPS_BUILD, DEVOPS_PROJECT, FIXTURE_USER_INPUT, PAT, UNIQUE_ID +from . import ( + DEVOPS_BUILD, + DEVOPS_PROJECT, + DEVOPS_WORK_ITEM_IDS, + DEVOPS_WORK_ITEM_TYPES, + DEVOPS_WORK_ITEMS, + FIXTURE_USER_INPUT, + PAT, + UNIQUE_ID, +) from tests.common import MockConfigEntry @@ -33,8 +42,9 @@ async def mock_devops_client() -> AsyncGenerator[MagicMock]: devops_client.get_project.return_value = DEVOPS_PROJECT devops_client.get_builds.return_value = [DEVOPS_BUILD] devops_client.get_build.return_value = DEVOPS_BUILD - devops_client.get_work_item_ids.return_value = None - devops_client.get_work_items.return_value = None + devops_client.get_work_item_types.return_value = DEVOPS_WORK_ITEM_TYPES + devops_client.get_work_item_ids.return_value = DEVOPS_WORK_ITEM_IDS + devops_client.get_work_items.return_value = DEVOPS_WORK_ITEMS yield devops_client diff --git a/tests/components/azure_devops/test_config_flow.py b/tests/components/azure_devops/test_config_flow.py index 45dc10802b9c64..9ebc9991939f42 100644 --- a/tests/components/azure_devops/test_config_flow.py +++ b/tests/components/azure_devops/test_config_flow.py @@ -53,18 +53,14 @@ async def test_authorization_error( async def test_reauth_authorization_error( hass: HomeAssistant, + mock_config_entry: MockConfigEntry, mock_devops_client: AsyncMock, ) -> None: """Test we show user form on Azure DevOps authorization error.""" mock_devops_client.authorize.return_value = False mock_devops_client.authorized = False - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_REAUTH}, - data=FIXTURE_USER_INPUT, - ) - + result = await mock_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth" @@ -108,17 +104,14 @@ async def test_connection_error( async def test_reauth_connection_error( hass: HomeAssistant, + mock_config_entry: MockConfigEntry, mock_devops_client: AsyncMock, ) -> None: """Test we show user form on Azure DevOps connection error.""" mock_devops_client.authorize.side_effect = aiohttp.ClientError mock_devops_client.authorized = False - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_REAUTH}, - data=FIXTURE_USER_INPUT, - ) + result = await mock_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth" @@ -174,11 +167,7 @@ async def test_reauth_project_error( mock_config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_REAUTH}, - data=FIXTURE_USER_INPUT, - ) + result = await mock_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth" @@ -205,11 +194,7 @@ async def test_reauth_flow( mock_config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_REAUTH}, - data=FIXTURE_USER_INPUT, - ) + result = await mock_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth" diff --git a/tests/components/azure_devops/test_init.py b/tests/components/azure_devops/test_init.py index a7655042f255e9..dd512cb12e0304 100644 --- a/tests/components/azure_devops/test_init.py +++ b/tests/components/azure_devops/test_init.py @@ -91,3 +91,48 @@ async def test_no_builds( assert mock_devops_client.get_builds.call_count == 1 assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY + + +async def test_no_work_item_types( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_devops_client: MagicMock, +) -> None: + """Test a failed update entry.""" + mock_devops_client.get_work_item_types.return_value = None + + await setup_integration(hass, mock_config_entry) + + assert mock_devops_client.get_work_item_types.call_count == 1 + + assert mock_config_entry.state is ConfigEntryState.LOADED + + +async def test_no_work_item_ids( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_devops_client: MagicMock, +) -> None: + """Test a failed update entry.""" + mock_devops_client.get_work_item_ids.return_value = None + + await setup_integration(hass, mock_config_entry) + + assert mock_devops_client.get_work_item_ids.call_count == 1 + + assert mock_config_entry.state is ConfigEntryState.LOADED + + +async def test_no_work_items( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_devops_client: MagicMock, +) -> None: + """Test a failed update entry.""" + mock_devops_client.get_work_items.return_value = None + + await setup_integration(hass, mock_config_entry) + + assert mock_devops_client.get_work_items.call_count == 1 + + assert mock_config_entry.state is ConfigEntryState.LOADED diff --git a/tests/components/bang_olufsen/conftest.py b/tests/components/bang_olufsen/conftest.py index 4764798f34d4a3..dd6c4a73469c0e 100644 --- a/tests/components/bang_olufsen/conftest.py +++ b/tests/components/bang_olufsen/conftest.py @@ -36,7 +36,7 @@ @pytest.fixture -def mock_config_entry(): +def mock_config_entry() -> MockConfigEntry: """Mock config entry.""" return MockConfigEntry( domain=DOMAIN, @@ -47,7 +47,11 @@ def mock_config_entry(): @pytest.fixture -async def mock_media_player(hass: HomeAssistant, mock_config_entry, mock_mozart_client): +async def mock_media_player( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_mozart_client: AsyncMock, +) -> None: """Mock media_player entity.""" mock_config_entry.add_to_hass(hass) @@ -241,14 +245,17 @@ def mock_mozart_client() -> Generator[AsyncMock]: # Non-REST API client methods client.check_device_connection = AsyncMock() client.close_api_client = AsyncMock() + + # WebSocket listener client.connect_notifications = AsyncMock() client.disconnect_notifications = Mock() + client.websocket_connected = False yield client @pytest.fixture -def mock_setup_entry(): +def mock_setup_entry() -> Generator[AsyncMock]: """Mock successful setup entry.""" with patch( "homeassistant.components.bang_olufsen.async_setup_entry", return_value=True diff --git a/tests/components/bang_olufsen/test_config_flow.py b/tests/components/bang_olufsen/test_config_flow.py index e637120a6ae598..5d5f34a79e6cc3 100644 --- a/tests/components/bang_olufsen/test_config_flow.py +++ b/tests/components/bang_olufsen/test_config_flow.py @@ -1,6 +1,6 @@ """Test the bang_olufsen config_flow.""" -from unittest.mock import Mock +from unittest.mock import AsyncMock, Mock from aiohttp.client_exceptions import ClientConnectorError from mozart_api.exceptions import ApiException @@ -25,7 +25,7 @@ async def test_config_flow_timeout_error( - hass: HomeAssistant, mock_mozart_client + hass: HomeAssistant, mock_mozart_client: AsyncMock ) -> None: """Test we handle timeout_error.""" mock_mozart_client.get_beolink_self.side_effect = TimeoutError() @@ -42,7 +42,7 @@ async def test_config_flow_timeout_error( async def test_config_flow_client_connector_error( - hass: HomeAssistant, mock_mozart_client + hass: HomeAssistant, mock_mozart_client: AsyncMock ) -> None: """Test we handle client_connector_error.""" mock_mozart_client.get_beolink_self.side_effect = ClientConnectorError( @@ -73,7 +73,7 @@ async def test_config_flow_invalid_ip(hass: HomeAssistant) -> None: async def test_config_flow_api_exception( - hass: HomeAssistant, mock_mozart_client + hass: HomeAssistant, mock_mozart_client: AsyncMock ) -> None: """Test we handle api_exception.""" mock_mozart_client.get_beolink_self.side_effect = ApiException() @@ -89,7 +89,7 @@ async def test_config_flow_api_exception( assert mock_mozart_client.get_beolink_self.call_count == 1 -async def test_config_flow(hass: HomeAssistant, mock_mozart_client) -> None: +async def test_config_flow(hass: HomeAssistant, mock_mozart_client: AsyncMock) -> None: """Test config flow.""" result_init = await hass.config_entries.flow.async_init( @@ -112,7 +112,9 @@ async def test_config_flow(hass: HomeAssistant, mock_mozart_client) -> None: assert mock_mozart_client.get_beolink_self.call_count == 1 -async def test_config_flow_zeroconf(hass: HomeAssistant, mock_mozart_client) -> None: +async def test_config_flow_zeroconf( + hass: HomeAssistant, mock_mozart_client: AsyncMock +) -> None: """Test zeroconf discovery.""" result_zeroconf = await hass.config_entries.flow.async_init( @@ -162,7 +164,7 @@ async def test_config_flow_zeroconf_ipv6(hass: HomeAssistant) -> None: async def test_config_flow_zeroconf_invalid_ip( - hass: HomeAssistant, mock_mozart_client + hass: HomeAssistant, mock_mozart_client: AsyncMock ) -> None: """Test zeroconf discovery with invalid IP address.""" mock_mozart_client.get_beolink_self.side_effect = ClientConnectorError( diff --git a/tests/components/bang_olufsen/test_init.py b/tests/components/bang_olufsen/test_init.py index 11742b846ae8f1..3eb98e956beb14 100644 --- a/tests/components/bang_olufsen/test_init.py +++ b/tests/components/bang_olufsen/test_init.py @@ -1,5 +1,7 @@ """Test the bang_olufsen __init__.""" +from unittest.mock import AsyncMock + from aiohttp.client_exceptions import ServerTimeoutError from homeassistant.components.bang_olufsen import DOMAIN @@ -9,12 +11,14 @@ from .const import TEST_MODEL_BALANCE, TEST_NAME, TEST_SERIAL_NUMBER +from tests.common import MockConfigEntry + async def test_setup_entry( hass: HomeAssistant, - mock_config_entry, - mock_mozart_client, device_registry: DeviceRegistry, + mock_config_entry: MockConfigEntry, + mock_mozart_client: AsyncMock, ) -> None: """Test async_setup_entry.""" @@ -41,7 +45,9 @@ async def test_setup_entry( async def test_setup_entry_failed( - hass: HomeAssistant, mock_config_entry, mock_mozart_client + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_mozart_client: AsyncMock, ) -> None: """Test failed async_setup_entry.""" @@ -66,7 +72,9 @@ async def test_setup_entry_failed( async def test_unload_entry( - hass: HomeAssistant, mock_config_entry, mock_mozart_client + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_mozart_client: AsyncMock, ) -> None: """Test unload_entry.""" diff --git a/tests/components/bang_olufsen/test_media_player.py b/tests/components/bang_olufsen/test_media_player.py index 37f375bdb397bb..9928a626a4f2be 100644 --- a/tests/components/bang_olufsen/test_media_player.py +++ b/tests/components/bang_olufsen/test_media_player.py @@ -1,17 +1,22 @@ """Test the Bang & Olufsen media_player entity.""" +from collections.abc import Callable from contextlib import nullcontext as does_not_raise import logging -from unittest.mock import patch +from unittest.mock import AsyncMock, patch -from mozart_api.models import PlaybackContentMetadata +from mozart_api.models import ( + PlaybackContentMetadata, + RenderingState, + Source, + WebsocketNotificationTag, +) import pytest from homeassistant.components.bang_olufsen.const import ( BANG_OLUFSEN_STATES, DOMAIN, BangOlufsenSource, - WebsocketNotification, ) from homeassistant.components.media_player import ( ATTR_INPUT_SOURCE, @@ -37,7 +42,6 @@ from homeassistant.const import ATTR_ENTITY_ID from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError, ServiceValidationError -from homeassistant.helpers.dispatcher import async_dispatcher_send from homeassistant.setup import async_setup_component from .const import ( @@ -58,7 +62,6 @@ TEST_PLAYBACK_STATE_TURN_OFF, TEST_RADIO_STATION, TEST_SEEK_POSITION_HOME_ASSISTANT_FORMAT, - TEST_SERIAL_NUMBER, TEST_SOURCES, TEST_VIDEO_SOURCES, TEST_VOLUME, @@ -75,7 +78,7 @@ async def test_initialization( hass: HomeAssistant, caplog: pytest.LogCaptureFixture, mock_config_entry: MockConfigEntry, - mock_mozart_client, + mock_mozart_client: AsyncMock, ) -> None: """Test the integration is initialized properly in _initialize, async_added_to_hass and __init__.""" @@ -102,7 +105,9 @@ async def test_initialization( async def test_async_update_sources_audio_only( - hass: HomeAssistant, mock_config_entry, mock_mozart_client + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_mozart_client: AsyncMock, ) -> None: """Test sources are correctly handled in _async_update_sources.""" mock_mozart_client.get_remote_menu.return_value = {} @@ -115,7 +120,9 @@ async def test_async_update_sources_audio_only( async def test_async_update_sources_outdated_api( - hass: HomeAssistant, mock_mozart_client, mock_config_entry + hass: HomeAssistant, + mock_mozart_client: AsyncMock, + mock_config_entry: MockConfigEntry, ) -> None: """Test fallback sources are correctly handled in _async_update_sources.""" mock_mozart_client.get_available_sources.side_effect = ValueError() @@ -130,14 +137,43 @@ async def test_async_update_sources_outdated_api( ) +async def test_async_update_sources_remote( + hass: HomeAssistant, mock_mozart_client, mock_config_entry: MockConfigEntry +) -> None: + """Test _async_update_sources is called when there are new video sources.""" + + mock_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(mock_config_entry.entry_id) + + notification_callback = mock_mozart_client.get_notification_notifications.call_args[ + 0 + ][0] + + # This is not an ideal check, but I couldn't get anything else to work + assert mock_mozart_client.get_available_sources.call_count == 1 + assert mock_mozart_client.get_remote_menu.call_count == 1 + + # Send the remote menu Websocket event + notification_callback(WebsocketNotificationTag(value="remoteMenuChanged")) + + assert mock_mozart_client.get_available_sources.call_count == 2 + assert mock_mozart_client.get_remote_menu.call_count == 2 + + async def test_async_update_playback_metadata( - hass: HomeAssistant, mock_mozart_client, mock_config_entry + hass: HomeAssistant, + mock_mozart_client: AsyncMock, + mock_config_entry: MockConfigEntry, ) -> None: """Test _async_update_playback_metadata.""" mock_config_entry.add_to_hass(hass) await hass.config_entries.async_setup(mock_config_entry.entry_id) + playback_metadata_callback = ( + mock_mozart_client.get_playback_metadata_notifications.call_args[0][0] + ) + states = hass.states.get(TEST_MEDIA_PLAYER_ENTITY_ID) assert ATTR_MEDIA_DURATION not in states.attributes assert ATTR_MEDIA_TITLE not in states.attributes @@ -147,11 +183,7 @@ async def test_async_update_playback_metadata( assert ATTR_MEDIA_CHANNEL not in states.attributes # Send the WebSocket event dispatch - async_dispatcher_send( - hass, - f"{TEST_SERIAL_NUMBER}_{WebsocketNotification.PLAYBACK_METADATA}", - TEST_PLAYBACK_METADATA, - ) + playback_metadata_callback(TEST_PLAYBACK_METADATA) states = hass.states.get(TEST_MEDIA_PLAYER_ENTITY_ID) assert ( @@ -170,21 +202,21 @@ async def test_async_update_playback_metadata( async def test_async_update_playback_error( hass: HomeAssistant, caplog: pytest.LogCaptureFixture, - mock_mozart_client, - mock_config_entry, + mock_mozart_client: AsyncMock, + mock_config_entry: MockConfigEntry, ) -> None: """Test _async_update_playback_error.""" mock_config_entry.add_to_hass(hass) await hass.config_entries.async_setup(mock_config_entry.entry_id) - # The async_dispatcher_send function seems to swallow exceptions, making pytest.raises unusable - async_dispatcher_send( - hass, - f"{TEST_SERIAL_NUMBER}_{WebsocketNotification.PLAYBACK_ERROR}", - TEST_PLAYBACK_ERROR, + playback_error_callback = ( + mock_mozart_client.get_playback_error_notifications.call_args[0][0] ) + # The async_dispatcher_send function seems to swallow exceptions, making pytest.raises unusable + playback_error_callback(TEST_PLAYBACK_ERROR) + assert ( "Exception in _async_update_playback_error when dispatching '11111111_playback_error': (PlaybackError(error='Test error', item=None),)" in caplog.text @@ -192,23 +224,25 @@ async def test_async_update_playback_error( async def test_async_update_playback_progress( - hass: HomeAssistant, mock_mozart_client, mock_config_entry + hass: HomeAssistant, + mock_mozart_client: AsyncMock, + mock_config_entry: MockConfigEntry, ) -> None: """Test _async_update_playback_progress.""" mock_config_entry.add_to_hass(hass) await hass.config_entries.async_setup(mock_config_entry.entry_id) + playback_progress_callback = ( + mock_mozart_client.get_playback_progress_notifications.call_args[0][0] + ) + states = hass.states.get(TEST_MEDIA_PLAYER_ENTITY_ID) assert ATTR_MEDIA_POSITION not in states.attributes old_updated_at = states.attributes[ATTR_MEDIA_POSITION_UPDATED_AT] assert old_updated_at - async_dispatcher_send( - hass, - f"{TEST_SERIAL_NUMBER}_{WebsocketNotification.PLAYBACK_PROGRESS}", - TEST_PLAYBACK_PROGRESS, - ) + playback_progress_callback(TEST_PLAYBACK_PROGRESS) states = hass.states.get(TEST_MEDIA_PLAYER_ENTITY_ID) assert states.attributes[ATTR_MEDIA_POSITION] == TEST_PLAYBACK_PROGRESS.progress @@ -218,21 +252,23 @@ async def test_async_update_playback_progress( async def test_async_update_playback_state( - hass: HomeAssistant, mock_mozart_client, mock_config_entry + hass: HomeAssistant, + mock_mozart_client: AsyncMock, + mock_config_entry: MockConfigEntry, ) -> None: """Test _async_update_playback_state.""" mock_config_entry.add_to_hass(hass) await hass.config_entries.async_setup(mock_config_entry.entry_id) + playback_state_callback = ( + mock_mozart_client.get_playback_state_notifications.call_args[0][0] + ) + states = hass.states.get(TEST_MEDIA_PLAYER_ENTITY_ID) assert states.state == MediaPlayerState.PLAYING - async_dispatcher_send( - hass, - f"{TEST_SERIAL_NUMBER}_{WebsocketNotification.PLAYBACK_STATE}", - TEST_PLAYBACK_STATE_PAUSED, - ) + playback_state_callback(TEST_PLAYBACK_STATE_PAUSED) states = hass.states.get(TEST_MEDIA_PLAYER_ENTITY_ID) assert states.state == TEST_PLAYBACK_STATE_PAUSED.value @@ -293,43 +329,40 @@ async def test_async_update_playback_state( ], ) async def test_async_update_source_change( - reported_source, - real_source, - content_type, - progress, - metadata, hass: HomeAssistant, - mock_mozart_client, - mock_config_entry, + mock_mozart_client: AsyncMock, + mock_config_entry: MockConfigEntry, + reported_source: Source, + real_source: Source, + content_type: MediaType, + progress: int, + metadata: PlaybackContentMetadata, ) -> None: """Test _async_update_source_change.""" mock_config_entry.add_to_hass(hass) await hass.config_entries.async_setup(mock_config_entry.entry_id) + playback_progress_callback = ( + mock_mozart_client.get_playback_progress_notifications.call_args[0][0] + ) + playback_metadata_callback = ( + mock_mozart_client.get_playback_metadata_notifications.call_args[0][0] + ) + source_change_callback = ( + mock_mozart_client.get_source_change_notifications.call_args[0][0] + ) + states = hass.states.get(TEST_MEDIA_PLAYER_ENTITY_ID) assert ATTR_INPUT_SOURCE not in states.attributes assert states.attributes[ATTR_MEDIA_CONTENT_TYPE] == MediaType.MUSIC # Simulate progress attribute being available - async_dispatcher_send( - hass, - f"{TEST_SERIAL_NUMBER}_{WebsocketNotification.PLAYBACK_PROGRESS}", - TEST_PLAYBACK_PROGRESS, - ) + playback_progress_callback(TEST_PLAYBACK_PROGRESS) # Simulate metadata - async_dispatcher_send( - hass, - f"{TEST_SERIAL_NUMBER}_{WebsocketNotification.PLAYBACK_METADATA}", - metadata, - ) - - async_dispatcher_send( - hass, - f"{TEST_SERIAL_NUMBER}_{WebsocketNotification.SOURCE_CHANGE}", - reported_source, - ) + playback_metadata_callback(metadata) + source_change_callback(reported_source) states = hass.states.get(TEST_MEDIA_PLAYER_ENTITY_ID) assert states.attributes[ATTR_INPUT_SOURCE] == real_source.name @@ -338,13 +371,19 @@ async def test_async_update_source_change( async def test_async_turn_off( - hass: HomeAssistant, mock_mozart_client, mock_config_entry + hass: HomeAssistant, + mock_mozart_client: AsyncMock, + mock_config_entry: MockConfigEntry, ) -> None: """Test async_turn_off.""" mock_config_entry.add_to_hass(hass) await hass.config_entries.async_setup(mock_config_entry.entry_id) + playback_state_callback = ( + mock_mozart_client.get_playback_state_notifications.call_args[0][0] + ) + await hass.services.async_call( "media_player", "turn_off", @@ -352,11 +391,7 @@ async def test_async_turn_off( blocking=True, ) - async_dispatcher_send( - hass, - f"{TEST_SERIAL_NUMBER}_{WebsocketNotification.PLAYBACK_STATE}", - TEST_PLAYBACK_STATE_TURN_OFF, - ) + playback_state_callback(TEST_PLAYBACK_STATE_TURN_OFF) states = hass.states.get(TEST_MEDIA_PLAYER_ENTITY_ID) assert states.state == BANG_OLUFSEN_STATES[TEST_PLAYBACK_STATE_TURN_OFF.value] @@ -366,13 +401,17 @@ async def test_async_turn_off( async def test_async_set_volume_level( - hass: HomeAssistant, mock_mozart_client, mock_config_entry + hass: HomeAssistant, + mock_mozart_client: AsyncMock, + mock_config_entry: MockConfigEntry, ) -> None: """Test async_set_volume_level and _async_update_volume by proxy.""" mock_config_entry.add_to_hass(hass) await hass.config_entries.async_setup(mock_config_entry.entry_id) + volume_callback = mock_mozart_client.get_volume_notifications.call_args[0][0] + states = hass.states.get(TEST_MEDIA_PLAYER_ENTITY_ID) assert ATTR_MEDIA_VOLUME_LEVEL not in states.attributes @@ -387,11 +426,7 @@ async def test_async_set_volume_level( ) # The service call will trigger a WebSocket notification - async_dispatcher_send( - hass, - f"{TEST_SERIAL_NUMBER}_{WebsocketNotification.VOLUME}", - TEST_VOLUME, - ) + volume_callback(TEST_VOLUME) states = hass.states.get(TEST_MEDIA_PLAYER_ENTITY_ID) assert ( @@ -404,13 +439,17 @@ async def test_async_set_volume_level( async def test_async_mute_volume( - hass: HomeAssistant, mock_mozart_client, mock_config_entry + hass: HomeAssistant, + mock_mozart_client: AsyncMock, + mock_config_entry: MockConfigEntry, ) -> None: """Test async_mute_volume.""" mock_config_entry.add_to_hass(hass) await hass.config_entries.async_setup(mock_config_entry.entry_id) + volume_callback = mock_mozart_client.get_volume_notifications.call_args[0][0] + states = hass.states.get(TEST_MEDIA_PLAYER_ENTITY_ID) assert ATTR_MEDIA_VOLUME_MUTED not in states.attributes @@ -425,11 +464,7 @@ async def test_async_mute_volume( ) # The service call will trigger a WebSocket notification - async_dispatcher_send( - hass, - f"{TEST_SERIAL_NUMBER}_{WebsocketNotification.VOLUME}", - TEST_VOLUME_MUTED, - ) + volume_callback(TEST_VOLUME_MUTED) states = hass.states.get(TEST_MEDIA_PLAYER_ENTITY_ID) assert ( @@ -452,24 +487,24 @@ async def test_async_mute_volume( ], ) async def test_async_media_play_pause( - initial_state, - command, hass: HomeAssistant, - mock_mozart_client, - mock_config_entry, + mock_mozart_client: AsyncMock, + mock_config_entry: MockConfigEntry, + initial_state: RenderingState, + command: str, ) -> None: """Test async_media_play_pause.""" mock_config_entry.add_to_hass(hass) await hass.config_entries.async_setup(mock_config_entry.entry_id) - # Set the initial state - async_dispatcher_send( - hass, - f"{TEST_SERIAL_NUMBER}_{WebsocketNotification.PLAYBACK_STATE}", - initial_state, + playback_state_callback = ( + mock_mozart_client.get_playback_state_notifications.call_args[0][0] ) + # Set the initial state + playback_state_callback(initial_state) + states = hass.states.get(TEST_MEDIA_PLAYER_ENTITY_ID) assert states.state == BANG_OLUFSEN_STATES[initial_state.value] @@ -484,20 +519,22 @@ async def test_async_media_play_pause( async def test_async_media_stop( - hass: HomeAssistant, mock_mozart_client, mock_config_entry + hass: HomeAssistant, + mock_mozart_client: AsyncMock, + mock_config_entry: MockConfigEntry, ) -> None: """Test async_media_stop.""" mock_config_entry.add_to_hass(hass) await hass.config_entries.async_setup(mock_config_entry.entry_id) - # Set the state to playing - async_dispatcher_send( - hass, - f"{TEST_SERIAL_NUMBER}_{WebsocketNotification.PLAYBACK_STATE}", - TEST_PLAYBACK_STATE_PLAYING, + playback_state_callback = ( + mock_mozart_client.get_playback_state_notifications.call_args[0][0] ) + # Set the state to playing + playback_state_callback(TEST_PLAYBACK_STATE_PLAYING) + states = hass.states.get(TEST_MEDIA_PLAYER_ENTITY_ID) assert states.state == BANG_OLUFSEN_STATES[TEST_PLAYBACK_STATE_PLAYING.value] @@ -513,7 +550,9 @@ async def test_async_media_stop( async def test_async_media_next_track( - hass: HomeAssistant, mock_mozart_client, mock_config_entry + hass: HomeAssistant, + mock_mozart_client: AsyncMock, + mock_config_entry: MockConfigEntry, ) -> None: """Test async_media_next_track.""" @@ -540,25 +579,25 @@ async def test_async_media_next_track( ], ) async def test_async_media_seek( - source, - expected_result, - seek_called_times, hass: HomeAssistant, - mock_mozart_client, - mock_config_entry, + mock_mozart_client: AsyncMock, + mock_config_entry: MockConfigEntry, + source: Source, + expected_result: Callable, + seek_called_times: int, ) -> None: """Test async_media_seek.""" mock_config_entry.add_to_hass(hass) await hass.config_entries.async_setup(mock_config_entry.entry_id) - # Set the source - async_dispatcher_send( - hass, - f"{TEST_SERIAL_NUMBER}_{WebsocketNotification.SOURCE_CHANGE}", - source, + source_change_callback = ( + mock_mozart_client.get_source_change_notifications.call_args[0][0] ) + # Set the source + source_change_callback(source) + # Check results with expected_result: await hass.services.async_call( @@ -575,7 +614,9 @@ async def test_async_media_seek( async def test_async_media_previous_track( - hass: HomeAssistant, mock_mozart_client, mock_config_entry + hass: HomeAssistant, + mock_mozart_client: AsyncMock, + mock_config_entry: MockConfigEntry, ) -> None: """Test async_media_previous_track.""" @@ -593,7 +634,9 @@ async def test_async_media_previous_track( async def test_async_clear_playlist( - hass: HomeAssistant, mock_mozart_client, mock_config_entry + hass: HomeAssistant, + mock_mozart_client: AsyncMock, + mock_config_entry: MockConfigEntry, ) -> None: """Test async_clear_playlist.""" @@ -622,13 +665,13 @@ async def test_async_clear_playlist( ], ) async def test_async_select_source( - source, - expected_result, - audio_source_call, - video_source_call, hass: HomeAssistant, - mock_mozart_client, - mock_config_entry, + mock_mozart_client: AsyncMock, + mock_config_entry: MockConfigEntry, + source: str, + expected_result: Callable, + audio_source_call: int, + video_source_call: int, ) -> None: """Test async_select_source with an invalid source.""" @@ -651,7 +694,9 @@ async def test_async_select_source( async def test_async_play_media_invalid_type( - hass: HomeAssistant, mock_mozart_client, mock_config_entry + hass: HomeAssistant, + mock_mozart_client: AsyncMock, + mock_config_entry: MockConfigEntry, ) -> None: """Test async_play_media only accepts valid media types.""" @@ -676,7 +721,9 @@ async def test_async_play_media_invalid_type( async def test_async_play_media_url( - hass: HomeAssistant, mock_mozart_client, mock_config_entry + hass: HomeAssistant, + mock_mozart_client: AsyncMock, + mock_config_entry: MockConfigEntry, ) -> None: """Test async_play_media URL.""" @@ -701,7 +748,9 @@ async def test_async_play_media_url( async def test_async_play_media_overlay_absolute_volume_uri( - hass: HomeAssistant, mock_mozart_client, mock_config_entry + hass: HomeAssistant, + mock_mozart_client: AsyncMock, + mock_config_entry: MockConfigEntry, ) -> None: """Test async_play_media overlay with Home Assistant local URI and absolute volume.""" @@ -734,8 +783,8 @@ async def test_async_play_media_overlay_absolute_volume_uri( async def test_async_play_media_overlay_invalid_offset_volume_tts( hass: HomeAssistant, caplog: pytest.LogCaptureFixture, - mock_mozart_client, - mock_config_entry, + mock_mozart_client: AsyncMock, + mock_config_entry: MockConfigEntry, ) -> None: """Test async_play_media with Home Assistant invalid offset volume and B&O tts.""" @@ -765,19 +814,19 @@ async def test_async_play_media_overlay_invalid_offset_volume_tts( async def test_async_play_media_overlay_offset_volume_tts( - hass: HomeAssistant, mock_mozart_client, mock_config_entry + hass: HomeAssistant, + mock_mozart_client: AsyncMock, + mock_config_entry: MockConfigEntry, ) -> None: """Test async_play_media with Home Assistant invalid offset volume and B&O tts.""" mock_config_entry.add_to_hass(hass) await hass.config_entries.async_setup(mock_config_entry.entry_id) + volume_callback = mock_mozart_client.get_volume_notifications.call_args[0][0] + # Set the volume to enable offset - async_dispatcher_send( - hass, - f"{TEST_SERIAL_NUMBER}_{WebsocketNotification.VOLUME}", - TEST_VOLUME, - ) + volume_callback(TEST_VOLUME) await hass.services.async_call( "media_player", @@ -798,7 +847,9 @@ async def test_async_play_media_overlay_offset_volume_tts( async def test_async_play_media_tts( - hass: HomeAssistant, mock_mozart_client, mock_config_entry + hass: HomeAssistant, + mock_mozart_client: AsyncMock, + mock_config_entry: MockConfigEntry, ) -> None: """Test async_play_media with Home Assistant tts.""" @@ -822,7 +873,9 @@ async def test_async_play_media_tts( async def test_async_play_media_radio( - hass: HomeAssistant, mock_mozart_client, mock_config_entry + hass: HomeAssistant, + mock_mozart_client: AsyncMock, + mock_config_entry: MockConfigEntry, ) -> None: """Test async_play_media with B&O radio.""" @@ -846,7 +899,9 @@ async def test_async_play_media_radio( async def test_async_play_media_favourite( - hass: HomeAssistant, mock_mozart_client, mock_config_entry + hass: HomeAssistant, + mock_mozart_client: AsyncMock, + mock_config_entry: MockConfigEntry, ) -> None: """Test async_play_media with B&O favourite.""" @@ -868,7 +923,9 @@ async def test_async_play_media_favourite( async def test_async_play_media_deezer_flow( - hass: HomeAssistant, mock_mozart_client, mock_config_entry + hass: HomeAssistant, + mock_mozart_client: AsyncMock, + mock_config_entry: MockConfigEntry, ) -> None: """Test async_play_media with Deezer flow.""" @@ -894,7 +951,9 @@ async def test_async_play_media_deezer_flow( async def test_async_play_media_deezer_playlist( - hass: HomeAssistant, mock_mozart_client, mock_config_entry + hass: HomeAssistant, + mock_mozart_client: AsyncMock, + mock_config_entry: MockConfigEntry, ) -> None: """Test async_play_media with Deezer playlist.""" @@ -919,7 +978,9 @@ async def test_async_play_media_deezer_playlist( async def test_async_play_media_deezer_track( - hass: HomeAssistant, mock_mozart_client, mock_config_entry + hass: HomeAssistant, + mock_mozart_client: AsyncMock, + mock_config_entry: MockConfigEntry, ) -> None: """Test async_play_media with Deezer track.""" @@ -943,7 +1004,9 @@ async def test_async_play_media_deezer_track( async def test_async_play_media_invalid_deezer( - hass: HomeAssistant, mock_mozart_client, mock_config_entry + hass: HomeAssistant, + mock_mozart_client: AsyncMock, + mock_config_entry: MockConfigEntry, ) -> None: """Test async_play_media with an invalid/no Deezer login.""" @@ -972,7 +1035,9 @@ async def test_async_play_media_invalid_deezer( async def test_async_play_media_url_m3u( - hass: HomeAssistant, mock_mozart_client, mock_config_entry + hass: HomeAssistant, + mock_mozart_client: AsyncMock, + mock_config_entry: MockConfigEntry, ) -> None: """Test async_play_media URL with the m3u extension.""" @@ -1041,12 +1106,12 @@ async def test_async_play_media_url_m3u( ], ) async def test_async_browse_media( - child, - present, hass: HomeAssistant, - mock_mozart_client, - mock_config_entry, hass_ws_client: WebSocketGenerator, + mock_mozart_client: AsyncMock, + mock_config_entry: MockConfigEntry, + child: dict[str, str | bool | None], + present: bool, ) -> None: """Test async_browse_media with audio and video source.""" diff --git a/tests/components/bang_olufsen/test_websocket.py b/tests/components/bang_olufsen/test_websocket.py new file mode 100644 index 00000000000000..209550faee5a91 --- /dev/null +++ b/tests/components/bang_olufsen/test_websocket.py @@ -0,0 +1,163 @@ +"""Test the Bang & Olufsen WebSocket listener.""" + +import logging +from unittest.mock import AsyncMock, Mock + +from mozart_api.models import SoftwareUpdateState +import pytest + +from homeassistant.components.bang_olufsen.const import ( + BANG_OLUFSEN_WEBSOCKET_EVENT, + CONNECTION_STATUS, + DOMAIN, +) +from homeassistant.core import HomeAssistant +from homeassistant.helpers.device_registry import DeviceRegistry +from homeassistant.helpers.dispatcher import async_dispatcher_connect + +from .const import TEST_NAME + +from tests.common import MockConfigEntry + + +async def test_connection( + hass: HomeAssistant, + caplog: pytest.LogCaptureFixture, + mock_config_entry: MockConfigEntry, + mock_mozart_client: AsyncMock, +) -> None: + """Test on_connection and on_connection_lost logs and calls correctly.""" + + mock_mozart_client.websocket_connected = True + + mock_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(mock_config_entry.entry_id) + + connection_callback = mock_mozart_client.get_on_connection.call_args[0][0] + + caplog.set_level(logging.DEBUG) + + mock_connection_callback = Mock() + + async_dispatcher_connect( + hass, + f"{mock_config_entry.unique_id}_{CONNECTION_STATUS}", + mock_connection_callback, + ) + + # Call the WebSocket connection status method + connection_callback() + await hass.async_block_till_done() + + mock_connection_callback.assert_called_once_with(True) + assert f"Connected to the {TEST_NAME} notification channel" in caplog.text + + +async def test_connection_lost( + hass: HomeAssistant, + caplog: pytest.LogCaptureFixture, + mock_config_entry: MockConfigEntry, + mock_mozart_client: AsyncMock, +) -> None: + """Test on_connection_lost logs and calls correctly.""" + + mock_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(mock_config_entry.entry_id) + + connection_lost_callback = mock_mozart_client.get_on_connection_lost.call_args[0][0] + + mock_connection_lost_callback = Mock() + + async_dispatcher_connect( + hass, + f"{mock_config_entry.unique_id}_{CONNECTION_STATUS}", + mock_connection_lost_callback, + ) + + connection_lost_callback() + await hass.async_block_till_done() + + mock_connection_lost_callback.assert_called_once_with(False) + assert f"Lost connection to the {TEST_NAME}" in caplog.text + + +async def test_on_software_update_state( + hass: HomeAssistant, + device_registry: DeviceRegistry, + mock_config_entry: MockConfigEntry, + mock_mozart_client: AsyncMock, +) -> None: + """Test software version is updated through on_software_update_state.""" + + mock_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(mock_config_entry.entry_id) + + software_update_state_callback = ( + mock_mozart_client.get_software_update_state_notifications.call_args[0][0] + ) + + # Trigger the notification + await software_update_state_callback(SoftwareUpdateState()) + + await hass.async_block_till_done() + + device = device_registry.async_get_device( + identifiers={(DOMAIN, mock_config_entry.unique_id)} + ) + assert device.sw_version == "1.0.0" + + +async def test_on_all_notifications_raw( + hass: HomeAssistant, + caplog: pytest.LogCaptureFixture, + device_registry: DeviceRegistry, + mock_config_entry: MockConfigEntry, + mock_mozart_client: AsyncMock, +) -> None: + """Test on_all_notifications_raw logs and fires as expected.""" + + mock_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(mock_config_entry.entry_id) + + all_notifications_raw_callback = ( + mock_mozart_client.get_all_notifications_raw.call_args[0][0] + ) + + raw_notification = { + "eventData": { + "default": {"level": 40}, + "level": {"level": 40}, + "maximum": {"level": 100}, + "muted": {"muted": False}, + }, + "eventType": "WebSocketEventVolume", + } + raw_notification_full = raw_notification + + # Get device ID for the modified notification that is sent as an event and in the log + device = device_registry.async_get_device( + identifiers={(DOMAIN, mock_config_entry.unique_id)} + ) + raw_notification_full.update( + { + "device_id": device.id, + "serial_number": mock_config_entry.unique_id, + } + ) + + caplog.set_level(logging.DEBUG) + + mock_event_callback = Mock() + + # Listen to BANG_OLUFSEN_WEBSOCKET_EVENT events + hass.bus.async_listen(BANG_OLUFSEN_WEBSOCKET_EVENT, mock_event_callback) + + # Trigger the notification + all_notifications_raw_callback(raw_notification) + await hass.async_block_till_done() + + assert str(raw_notification_full) in caplog.text + + mocked_call = mock_event_callback.call_args[0][0].as_dict() + assert mocked_call["event_type"] == BANG_OLUFSEN_WEBSOCKET_EVENT + assert mocked_call["data"] == raw_notification_full diff --git a/tests/components/blink/test_config_flow.py b/tests/components/blink/test_config_flow.py index 9c3193ec7d6e90..c89ab65ea1d5c6 100644 --- a/tests/components/blink/test_config_flow.py +++ b/tests/components/blink/test_config_flow.py @@ -10,6 +10,8 @@ from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType +from tests.common import MockConfigEntry + async def test_form(hass: HomeAssistant) -> None: """Test we get the form.""" @@ -292,10 +294,11 @@ async def test_form_unknown_error(hass: HomeAssistant) -> None: async def test_reauth_shows_user_step(hass: HomeAssistant) -> None: """Test reauth shows the user form.""" - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_REAUTH}, + mock_entry = MockConfigEntry( + domain=DOMAIN, data={"username": "blink@example.com", "password": "invalid_password"}, ) + mock_entry.add_to_hass(hass) + result = await mock_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "user" diff --git a/tests/components/blue_current/test_config_flow.py b/tests/components/blue_current/test_config_flow.py index 3334699042566e..a9dea70431ff70 100644 --- a/tests/components/blue_current/test_config_flow.py +++ b/tests/components/blue_current/test_config_flow.py @@ -129,6 +129,11 @@ async def test_reauth( expected_api_token: str, ) -> None: """Test reauth flow.""" + config_entry.add_to_hass(hass) + result = await config_entry.start_reauth_flow(hass) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + with ( patch( "homeassistant.components.blue_current.config_flow.Client.validate_api_token", @@ -146,20 +151,6 @@ async def test_reauth( lambda self, on_data, on_open: hass.loop.create_future(), ), ): - config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": config_entry.entry_id, - "unique_id": config_entry.unique_id, - }, - data={"api_token": "123"}, - ) - - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "user" - result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={"api_token": "1234567890"}, diff --git a/tests/components/bluesound/test_config_flow.py b/tests/components/bluesound/test_config_flow.py index 8fecba7017d324..53cf40a8d465d5 100644 --- a/tests/components/bluesound/test_config_flow.py +++ b/tests/components/bluesound/test_config_flow.py @@ -2,7 +2,7 @@ from unittest.mock import AsyncMock -from aiohttp import ClientConnectionError +from pyblu.errors import PlayerUnreachableError from homeassistant.components.bluesound.const import DOMAIN from homeassistant.components.zeroconf import ZeroconfServiceInfo @@ -49,7 +49,7 @@ async def test_user_flow_cannot_connect( context={"source": SOURCE_USER}, ) - mock_player.sync_status.side_effect = ClientConnectionError + mock_player.sync_status.side_effect = PlayerUnreachableError("Player not reachable") result = await hass.config_entries.flow.async_configure( result["flow_id"], { @@ -129,7 +129,7 @@ async def test_import_flow_cannot_connect( hass: HomeAssistant, mock_player: AsyncMock ) -> None: """Test we handle cannot connect error.""" - mock_player.sync_status.side_effect = ClientConnectionError + mock_player.sync_status.side_effect = PlayerUnreachableError("Player not reachable") result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_IMPORT}, @@ -200,7 +200,7 @@ async def test_zeroconf_flow_cannot_connect( hass: HomeAssistant, mock_player: AsyncMock ) -> None: """Test we handle cannot connect error.""" - mock_player.sync_status.side_effect = ClientConnectionError + mock_player.sync_status.side_effect = PlayerUnreachableError("Player not reachable") result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_ZEROCONF}, diff --git a/tests/components/bmw_connected_drive/snapshots/test_sensor.ambr b/tests/components/bmw_connected_drive/snapshots/test_sensor.ambr index 8a26acd1040390..2182ff2bb4894a 100644 --- a/tests/components/bmw_connected_drive/snapshots/test_sensor.ambr +++ b/tests/components/bmw_connected_drive/snapshots/test_sensor.ambr @@ -929,6 +929,7 @@ 'options': list([ 'cooling', 'heating', + 'ventilation', 'inactive', 'standby', ]), @@ -968,6 +969,7 @@ 'options': list([ 'cooling', 'heating', + 'ventilation', 'inactive', 'standby', ]), @@ -1933,6 +1935,7 @@ 'options': list([ 'cooling', 'heating', + 'ventilation', 'inactive', 'standby', ]), @@ -1972,6 +1975,7 @@ 'options': list([ 'cooling', 'heating', + 'ventilation', 'inactive', 'standby', ]), @@ -2665,6 +2669,7 @@ 'options': list([ 'cooling', 'heating', + 'ventilation', 'inactive', 'standby', ]), @@ -2704,6 +2709,7 @@ 'options': list([ 'cooling', 'heating', + 'ventilation', 'inactive', 'standby', ]), diff --git a/tests/components/bmw_connected_drive/test_button.py b/tests/components/bmw_connected_drive/test_button.py index 99cabc900fa31b..88c7990cde92e8 100644 --- a/tests/components/bmw_connected_drive/test_button.py +++ b/tests/components/bmw_connected_drive/test_button.py @@ -165,7 +165,7 @@ async def test_service_call_success_state_change( ( "button.i4_edrive40_find_vehicle", "device_tracker.i4_edrive40", - {"latitude": 123.456, "longitude": 34.5678, "direction": 121}, + {"latitude": 12.345, "longitude": 34.5678, "direction": 121}, {"latitude": 48.177334, "longitude": 11.556274, "direction": 180}, ), ], diff --git a/tests/components/bmw_connected_drive/test_config_flow.py b/tests/components/bmw_connected_drive/test_config_flow.py index f346cd70b266ad..f71730fcc17b65 100644 --- a/tests/components/bmw_connected_drive/test_config_flow.py +++ b/tests/components/bmw_connected_drive/test_config_flow.py @@ -188,15 +188,7 @@ async def test_reauth(hass: HomeAssistant) -> None: assert config_entry.data == config_entry_with_wrong_password["data"] - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": config_entry.unique_id, - "entry_id": config_entry.entry_id, - }, - ) - + result = await config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "user" assert result["errors"] == {} diff --git a/tests/components/bosch_shc/test_config_flow.py b/tests/components/bosch_shc/test_config_flow.py index 2c43ec0a370ab1..eaabe1128075ac 100644 --- a/tests/components/bosch_shc/test_config_flow.py +++ b/tests/components/bosch_shc/test_config_flow.py @@ -646,11 +646,7 @@ async def test_reauth(hass: HomeAssistant) -> None: title="shc012345", ) mock_config.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_REAUTH}, - data=mock_config.data, - ) + result = await mock_config.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/braviatv/test_config_flow.py b/tests/components/braviatv/test_config_flow.py index 6fc02dbd36f9af..7a4f93f7f1696a 100644 --- a/tests/components/braviatv/test_config_flow.py +++ b/tests/components/braviatv/test_config_flow.py @@ -17,7 +17,7 @@ DOMAIN, NICKNAME_PREFIX, ) -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_SSDP, SOURCE_USER +from homeassistant.config_entries import SOURCE_SSDP, SOURCE_USER from homeassistant.const import CONF_CLIENT_ID, CONF_HOST, CONF_MAC, CONF_PIN from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -405,6 +405,9 @@ async def test_reauth_successful(hass: HomeAssistant, use_psk, new_pin) -> None: title="TV-Model", ) config_entry.add_to_hass(hass) + result = await config_entry.start_reauth_flow(hass) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "authorize" with ( patch("pybravia.BraviaClient.connect"), @@ -421,15 +424,6 @@ async def test_reauth_successful(hass: HomeAssistant, use_psk, new_pin) -> None: return_value={}, ), ): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH, "entry_id": config_entry.entry_id}, - data=config_entry.data, - ) - - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "authorize" - result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={CONF_USE_PSK: use_psk} ) diff --git a/tests/components/bring/conftest.py b/tests/components/bring/conftest.py index 6c39c5020f9199..60c13a1c208144 100644 --- a/tests/components/bring/conftest.py +++ b/tests/components/bring/conftest.py @@ -3,6 +3,7 @@ from collections.abc import Generator from typing import cast from unittest.mock import AsyncMock, patch +import uuid from bring_api.types import BringAuthResponse import pytest @@ -10,7 +11,7 @@ from homeassistant.components.bring import DOMAIN from homeassistant.const import CONF_EMAIL, CONF_PASSWORD -from tests.common import MockConfigEntry +from tests.common import MockConfigEntry, load_json_object_fixture EMAIL = "test-email" PASSWORD = "test-password" @@ -43,10 +44,23 @@ def mock_bring_client() -> Generator[AsyncMock]: client = mock_client.return_value client.uuid = UUID client.login.return_value = cast(BringAuthResponse, {"name": "Bring"}) - client.load_lists.return_value = {"lists": []} + client.load_lists.return_value = load_json_object_fixture("lists.json", DOMAIN) + client.get_list.return_value = load_json_object_fixture("items.json", DOMAIN) yield client +@pytest.fixture +def mock_uuid() -> Generator[AsyncMock]: + """Mock uuid.""" + + with patch( + "homeassistant.components.bring.todo.uuid.uuid4", + autospec=True, + ) as mock_client: + mock_client.return_value = uuid.UUID("b669ad23-606a-4652-b302-995d34b1cb1c") + yield mock_client + + @pytest.fixture(name="bring_config_entry") def mock_bring_config_entry() -> MockConfigEntry: """Mock bring configuration entry.""" diff --git a/tests/components/bring/fixtures/items.json b/tests/components/bring/fixtures/items.json new file mode 100644 index 00000000000000..43e05a39fbb01d --- /dev/null +++ b/tests/components/bring/fixtures/items.json @@ -0,0 +1,26 @@ +{ + "uuid": "77a151f8-77c4-47a3-8295-c750a0e69d4f", + "status": "REGISTERED", + "purchase": [ + { + "uuid": "b5d0790b-5f32-4d5c-91da-e29066f167de", + "itemId": "Paprika", + "specification": "Rot", + "attributes": [] + }, + { + "uuid": "72d370ab-d8ca-4e41-b956-91df94795b4e", + "itemId": "Pouletbrüstli", + "specification": "Bio", + "attributes": [] + } + ], + "recently": [ + { + "uuid": "fc8db30a-647e-4e6c-9d71-3b85d6a2d954", + "itemId": "Ananas", + "specification": "", + "attributes": [] + } + ] +} diff --git a/tests/components/bring/fixtures/lists.json b/tests/components/bring/fixtures/lists.json new file mode 100644 index 00000000000000..5891d94f7de6e3 --- /dev/null +++ b/tests/components/bring/fixtures/lists.json @@ -0,0 +1,14 @@ +{ + "lists": [ + { + "listUuid": "e542eef6-dba7-4c31-a52c-29e6ab9d83a5", + "name": "Einkauf", + "theme": "ch.publisheria.bring.theme.home" + }, + { + "listUuid": "b4776778-7f6c-496e-951b-92a35d3db0dd", + "name": "Baumarkt", + "theme": "ch.publisheria.bring.theme.home" + } + ] +} diff --git a/tests/components/bring/snapshots/test_todo.ambr b/tests/components/bring/snapshots/test_todo.ambr new file mode 100644 index 00000000000000..6a24b4148b7af0 --- /dev/null +++ b/tests/components/bring/snapshots/test_todo.ambr @@ -0,0 +1,95 @@ +# serializer version: 1 +# name: test_todo[todo.baumarkt-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'todo', + 'entity_category': None, + 'entity_id': 'todo.baumarkt', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Baumarkt', + 'platform': 'bring', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': 'shopping_list', + 'unique_id': '00000000-00000000-00000000-00000000_b4776778-7f6c-496e-951b-92a35d3db0dd', + 'unit_of_measurement': None, + }) +# --- +# name: test_todo[todo.baumarkt-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Baumarkt', + 'supported_features': , + }), + 'context': , + 'entity_id': 'todo.baumarkt', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '2', + }) +# --- +# name: test_todo[todo.einkauf-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'todo', + 'entity_category': None, + 'entity_id': 'todo.einkauf', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Einkauf', + 'platform': 'bring', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': 'shopping_list', + 'unique_id': '00000000-00000000-00000000-00000000_e542eef6-dba7-4c31-a52c-29e6ab9d83a5', + 'unit_of_measurement': None, + }) +# --- +# name: test_todo[todo.einkauf-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Einkauf', + 'supported_features': , + }), + 'context': , + 'entity_id': 'todo.einkauf', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '2', + }) +# --- diff --git a/tests/components/bring/test_config_flow.py b/tests/components/bring/test_config_flow.py index d307e0ccbbe94b..8d215a5d3ee8bb 100644 --- a/tests/components/bring/test_config_flow.py +++ b/tests/components/bring/test_config_flow.py @@ -10,7 +10,7 @@ import pytest from homeassistant.components.bring.const import DOMAIN -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_USER from homeassistant.const import CONF_EMAIL, CONF_PASSWORD from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -123,15 +123,7 @@ async def test_flow_reauth( bring_config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "entry_id": bring_config_entry.entry_id, - "unique_id": bring_config_entry.unique_id, - }, - ) - + result = await bring_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -171,15 +163,7 @@ async def test_flow_reauth_error_and_recover( bring_config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "entry_id": bring_config_entry.entry_id, - "unique_id": bring_config_entry.unique_id, - }, - ) - + result = await bring_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/bring/test_init.py b/tests/components/bring/test_init.py index f1b1f78e775929..613b65e38b68d0 100644 --- a/tests/components/bring/test_init.py +++ b/tests/components/bring/test_init.py @@ -28,9 +28,9 @@ async def setup_integration( await hass.async_block_till_done() +@pytest.mark.usefixtures("mock_bring_client") async def test_load_unload( hass: HomeAssistant, - mock_bring_client: AsyncMock, bring_config_entry: MockConfigEntry, ) -> None: """Test loading and unloading of the config entry.""" @@ -58,7 +58,7 @@ async def test_init_failure( mock_bring_client: AsyncMock, status: ConfigEntryState, exception: Exception, - bring_config_entry: MockConfigEntry | None, + bring_config_entry: MockConfigEntry, ) -> None: """Test an initialization error on integration load.""" mock_bring_client.login.side_effect = exception @@ -79,7 +79,7 @@ async def test_init_exceptions( mock_bring_client: AsyncMock, exception: Exception, expected: Exception, - bring_config_entry: MockConfigEntry | None, + bring_config_entry: MockConfigEntry, ) -> None: """Test an initialization error on integration load.""" bring_config_entry.add_to_hass(hass) @@ -87,3 +87,42 @@ async def test_init_exceptions( with pytest.raises(expected): await async_setup_entry(hass, bring_config_entry) + + +@pytest.mark.parametrize("exception", [BringRequestException, BringParseException]) +@pytest.mark.parametrize("bring_method", ["load_lists", "get_list"]) +async def test_config_entry_not_ready( + hass: HomeAssistant, + bring_config_entry: MockConfigEntry, + mock_bring_client: AsyncMock, + exception: Exception, + bring_method: str, +) -> None: + """Test config entry not ready.""" + getattr(mock_bring_client, bring_method).side_effect = exception + bring_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(bring_config_entry.entry_id) + await hass.async_block_till_done() + + assert bring_config_entry.state is ConfigEntryState.SETUP_RETRY + + +@pytest.mark.parametrize( + "exception", [None, BringAuthException, BringRequestException, BringParseException] +) +async def test_config_entry_not_ready_auth_error( + hass: HomeAssistant, + bring_config_entry: MockConfigEntry, + mock_bring_client: AsyncMock, + exception: Exception | None, +) -> None: + """Test config entry not ready from authentication error.""" + + mock_bring_client.load_lists.side_effect = BringAuthException + mock_bring_client.retrieve_new_access_token.side_effect = exception + + bring_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(bring_config_entry.entry_id) + await hass.async_block_till_done() + + assert bring_config_entry.state is ConfigEntryState.SETUP_RETRY diff --git a/tests/components/bring/test_notification.py b/tests/components/bring/test_notification.py new file mode 100644 index 00000000000000..b1fa28335ad17d --- /dev/null +++ b/tests/components/bring/test_notification.py @@ -0,0 +1,106 @@ +"""Test todo entity notification action of the Bring! integration.""" + +import re +from unittest.mock import AsyncMock + +from bring_api import BringNotificationType, BringRequestException +import pytest + +from homeassistant.components.bring.const import ( + ATTR_ITEM_NAME, + ATTR_NOTIFICATION_TYPE, + DOMAIN, + SERVICE_PUSH_NOTIFICATION, +) +from homeassistant.config_entries import ConfigEntryState +from homeassistant.const import ATTR_ENTITY_ID +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError + +from tests.common import MockConfigEntry + + +async def test_send_notification( + hass: HomeAssistant, + bring_config_entry: MockConfigEntry, + mock_bring_client: AsyncMock, +) -> None: + """Test send bring push notification.""" + + bring_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(bring_config_entry.entry_id) + await hass.async_block_till_done() + + assert bring_config_entry.state is ConfigEntryState.LOADED + + await hass.services.async_call( + DOMAIN, + SERVICE_PUSH_NOTIFICATION, + service_data={ + ATTR_NOTIFICATION_TYPE: "GOING_SHOPPING", + }, + target={ATTR_ENTITY_ID: "todo.einkauf"}, + blocking=True, + ) + + mock_bring_client.notify.assert_called_once_with( + "e542eef6-dba7-4c31-a52c-29e6ab9d83a5", + BringNotificationType.GOING_SHOPPING, + None, + ) + + +async def test_send_notification_exception( + hass: HomeAssistant, + bring_config_entry: MockConfigEntry, + mock_bring_client: AsyncMock, +) -> None: + """Test send bring push notification with exception.""" + + bring_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(bring_config_entry.entry_id) + await hass.async_block_till_done() + + assert bring_config_entry.state is ConfigEntryState.LOADED + mock_bring_client.notify.side_effect = BringRequestException + with pytest.raises( + HomeAssistantError, + match="Failed to send push notification for bring due to a connection error, try again later", + ): + await hass.services.async_call( + DOMAIN, + SERVICE_PUSH_NOTIFICATION, + service_data={ + ATTR_NOTIFICATION_TYPE: "GOING_SHOPPING", + }, + target={ATTR_ENTITY_ID: "todo.einkauf"}, + blocking=True, + ) + + +async def test_send_notification_service_validation_error( + hass: HomeAssistant, + bring_config_entry: MockConfigEntry, + mock_bring_client: AsyncMock, +) -> None: + """Test send bring push notification.""" + + bring_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(bring_config_entry.entry_id) + await hass.async_block_till_done() + + assert bring_config_entry.state is ConfigEntryState.LOADED + mock_bring_client.notify.side_effect = ValueError + with pytest.raises( + HomeAssistantError, + match=re.escape( + "Failed to perform action bring.send_message. 'URGENT_MESSAGE' requires a value @ data['item']. Got None" + ), + ): + await hass.services.async_call( + DOMAIN, + SERVICE_PUSH_NOTIFICATION, + service_data={ATTR_NOTIFICATION_TYPE: "URGENT_MESSAGE", ATTR_ITEM_NAME: ""}, + target={ATTR_ENTITY_ID: "todo.einkauf"}, + blocking=True, + ) diff --git a/tests/components/bring/test_todo.py b/tests/components/bring/test_todo.py new file mode 100644 index 00000000000000..d67429e8f49080 --- /dev/null +++ b/tests/components/bring/test_todo.py @@ -0,0 +1,302 @@ +"""Test for todo platform of the Bring! integration.""" + +import re +from unittest.mock import AsyncMock + +from bring_api import BringItemOperation, BringRequestException +import pytest +from syrupy.assertion import SnapshotAssertion + +from homeassistant.components.todo import ( + ATTR_DESCRIPTION, + ATTR_ITEM, + ATTR_RENAME, + DOMAIN as TODO_DOMAIN, + TodoServices, +) +from homeassistant.config_entries import ConfigEntryState +from homeassistant.const import ATTR_ENTITY_ID +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError +from homeassistant.helpers import entity_registry as er + +from tests.common import MockConfigEntry, snapshot_platform + + +@pytest.mark.usefixtures("mock_bring_client") +async def test_todo( + hass: HomeAssistant, + bring_config_entry: MockConfigEntry, + snapshot: SnapshotAssertion, + entity_registry: er.EntityRegistry, +) -> None: + """Snapshot test states of todo platform.""" + + bring_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(bring_config_entry.entry_id) + await hass.async_block_till_done() + + assert bring_config_entry.state is ConfigEntryState.LOADED + + await snapshot_platform( + hass, entity_registry, snapshot, bring_config_entry.entry_id + ) + + +@pytest.mark.usefixtures("mock_uuid") +async def test_add_item( + hass: HomeAssistant, + bring_config_entry: MockConfigEntry, + mock_bring_client: AsyncMock, +) -> None: + """Test add item to list.""" + + bring_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(bring_config_entry.entry_id) + await hass.async_block_till_done() + + assert bring_config_entry.state is ConfigEntryState.LOADED + + await hass.services.async_call( + TODO_DOMAIN, + TodoServices.ADD_ITEM, + service_data={ATTR_ITEM: "Äpfel", ATTR_DESCRIPTION: "rot"}, + target={ATTR_ENTITY_ID: "todo.einkauf"}, + blocking=True, + ) + + mock_bring_client.save_item.assert_called_once_with( + "e542eef6-dba7-4c31-a52c-29e6ab9d83a5", + "Äpfel", + "rot", + "b669ad23-606a-4652-b302-995d34b1cb1c", + ) + + +async def test_add_item_exception( + hass: HomeAssistant, + bring_config_entry: MockConfigEntry, + mock_bring_client: AsyncMock, +) -> None: + """Test add item to list with exception.""" + + bring_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(bring_config_entry.entry_id) + await hass.async_block_till_done() + + assert bring_config_entry.state is ConfigEntryState.LOADED + + mock_bring_client.save_item.side_effect = BringRequestException + with pytest.raises( + HomeAssistantError, match="Failed to save item Äpfel to Bring! list" + ): + await hass.services.async_call( + TODO_DOMAIN, + TodoServices.ADD_ITEM, + service_data={ATTR_ITEM: "Äpfel", ATTR_DESCRIPTION: "rot"}, + target={ATTR_ENTITY_ID: "todo.einkauf"}, + blocking=True, + ) + + +@pytest.mark.usefixtures("mock_uuid") +async def test_update_item( + hass: HomeAssistant, + bring_config_entry: MockConfigEntry, + mock_bring_client: AsyncMock, +) -> None: + """Test update item.""" + + bring_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(bring_config_entry.entry_id) + await hass.async_block_till_done() + + assert bring_config_entry.state is ConfigEntryState.LOADED + + await hass.services.async_call( + TODO_DOMAIN, + TodoServices.UPDATE_ITEM, + service_data={ + ATTR_ITEM: "b5d0790b-5f32-4d5c-91da-e29066f167de", + ATTR_RENAME: "Paprika", + ATTR_DESCRIPTION: "Rot", + }, + target={ATTR_ENTITY_ID: "todo.einkauf"}, + blocking=True, + ) + + mock_bring_client.batch_update_list.assert_called_once_with( + "e542eef6-dba7-4c31-a52c-29e6ab9d83a5", + { + "itemId": "Paprika", + "spec": "Rot", + "uuid": "b5d0790b-5f32-4d5c-91da-e29066f167de", + }, + BringItemOperation.ADD, + ) + + +async def test_update_item_exception( + hass: HomeAssistant, + bring_config_entry: MockConfigEntry, + mock_bring_client: AsyncMock, +) -> None: + """Test update item with exception.""" + + bring_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(bring_config_entry.entry_id) + await hass.async_block_till_done() + + assert bring_config_entry.state is ConfigEntryState.LOADED + + mock_bring_client.batch_update_list.side_effect = BringRequestException + with pytest.raises( + HomeAssistantError, match="Failed to update item Paprika to Bring! list" + ): + await hass.services.async_call( + TODO_DOMAIN, + TodoServices.UPDATE_ITEM, + service_data={ + ATTR_ITEM: "b5d0790b-5f32-4d5c-91da-e29066f167de", + ATTR_RENAME: "Paprika", + ATTR_DESCRIPTION: "Rot", + }, + target={ATTR_ENTITY_ID: "todo.einkauf"}, + blocking=True, + ) + + +@pytest.mark.usefixtures("mock_uuid") +async def test_rename_item( + hass: HomeAssistant, + bring_config_entry: MockConfigEntry, + mock_bring_client: AsyncMock, +) -> None: + """Test rename item.""" + + bring_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(bring_config_entry.entry_id) + await hass.async_block_till_done() + + assert bring_config_entry.state is ConfigEntryState.LOADED + + await hass.services.async_call( + TODO_DOMAIN, + TodoServices.UPDATE_ITEM, + service_data={ + ATTR_ITEM: "b5d0790b-5f32-4d5c-91da-e29066f167de", + ATTR_RENAME: "Gurke", + ATTR_DESCRIPTION: "", + }, + target={ATTR_ENTITY_ID: "todo.einkauf"}, + blocking=True, + ) + + mock_bring_client.batch_update_list.assert_called_once_with( + "e542eef6-dba7-4c31-a52c-29e6ab9d83a5", + [ + { + "itemId": "Paprika", + "spec": "", + "uuid": "b5d0790b-5f32-4d5c-91da-e29066f167de", + "operation": BringItemOperation.REMOVE, + }, + { + "itemId": "Gurke", + "spec": "", + "uuid": "b669ad23-606a-4652-b302-995d34b1cb1c", + "operation": BringItemOperation.ADD, + }, + ], + ) + + +async def test_rename_item_exception( + hass: HomeAssistant, + bring_config_entry: MockConfigEntry, + mock_bring_client: AsyncMock, +) -> None: + """Test rename item with exception.""" + + bring_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(bring_config_entry.entry_id) + await hass.async_block_till_done() + + assert bring_config_entry.state is ConfigEntryState.LOADED + + mock_bring_client.batch_update_list.side_effect = BringRequestException + with pytest.raises( + HomeAssistantError, match="Failed to rename item Gurke to Bring! list" + ): + await hass.services.async_call( + TODO_DOMAIN, + TodoServices.UPDATE_ITEM, + service_data={ + ATTR_ITEM: "b5d0790b-5f32-4d5c-91da-e29066f167de", + ATTR_RENAME: "Gurke", + ATTR_DESCRIPTION: "", + }, + target={ATTR_ENTITY_ID: "todo.einkauf"}, + blocking=True, + ) + + +@pytest.mark.usefixtures("mock_uuid") +async def test_delete_items( + hass: HomeAssistant, + bring_config_entry: MockConfigEntry, + mock_bring_client: AsyncMock, +) -> None: + """Test delete item.""" + + bring_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(bring_config_entry.entry_id) + await hass.async_block_till_done() + + assert bring_config_entry.state is ConfigEntryState.LOADED + + await hass.services.async_call( + TODO_DOMAIN, + TodoServices.REMOVE_ITEM, + service_data={ATTR_ITEM: "b5d0790b-5f32-4d5c-91da-e29066f167de"}, + target={ATTR_ENTITY_ID: "todo.einkauf"}, + blocking=True, + ) + + mock_bring_client.batch_update_list.assert_called_once_with( + "e542eef6-dba7-4c31-a52c-29e6ab9d83a5", + [ + { + "itemId": "b5d0790b-5f32-4d5c-91da-e29066f167de", + "spec": "", + "uuid": "b5d0790b-5f32-4d5c-91da-e29066f167de", + }, + ], + BringItemOperation.REMOVE, + ) + + +async def test_delete_items_exception( + hass: HomeAssistant, + bring_config_entry: MockConfigEntry, + mock_bring_client: AsyncMock, +) -> None: + """Test delete item.""" + + bring_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(bring_config_entry.entry_id) + await hass.async_block_till_done() + + assert bring_config_entry.state is ConfigEntryState.LOADED + mock_bring_client.batch_update_list.side_effect = BringRequestException + with pytest.raises( + HomeAssistantError, + match=re.escape("Failed to delete 1 item(s) from Bring! list"), + ): + await hass.services.async_call( + TODO_DOMAIN, + TodoServices.REMOVE_ITEM, + service_data={ATTR_ITEM: "b5d0790b-5f32-4d5c-91da-e29066f167de"}, + target={ATTR_ENTITY_ID: "todo.einkauf"}, + blocking=True, + ) diff --git a/tests/components/broadlink/test_config_flow.py b/tests/components/broadlink/test_config_flow.py index 2def8c0b3b9791..f31cb3806316f6 100644 --- a/tests/components/broadlink/test_config_flow.py +++ b/tests/components/broadlink/test_config_flow.py @@ -734,13 +734,9 @@ async def test_flow_reauth_works(hass: HomeAssistant) -> None: mock_entry.add_to_hass(hass) mock_api = device.get_mock_api() mock_api.auth.side_effect = blke.AuthenticationError() - data = {"name": device.name, **device.get_entry_data()} with patch(DEVICE_FACTORY, return_value=mock_api): - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_REAUTH}, data=data - ) - + result = await mock_entry.start_reauth_flow(hass, data={"name": device.name}) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reset" @@ -770,12 +766,8 @@ async def test_flow_reauth_invalid_host(hass: HomeAssistant) -> None: mock_entry.add_to_hass(hass) mock_api = device.get_mock_api() mock_api.auth.side_effect = blke.AuthenticationError() - data = {"name": device.name, **device.get_entry_data()} - with patch(DEVICE_FACTORY, return_value=mock_api): - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_REAUTH}, data=data - ) + result = await mock_entry.start_reauth_flow(hass, data={"name": device.name}) device.mac = get_device("Office").mac mock_api = device.get_mock_api() @@ -804,12 +796,9 @@ async def test_flow_reauth_valid_host(hass: HomeAssistant) -> None: mock_entry.add_to_hass(hass) mock_api = device.get_mock_api() mock_api.auth.side_effect = blke.AuthenticationError() - data = {"name": device.name, **device.get_entry_data()} with patch(DEVICE_FACTORY, return_value=mock_api): - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_REAUTH}, data=data - ) + result = await mock_entry.start_reauth_flow(hass, data={"name": device.name}) device.host = "192.168.1.128" mock_api = device.get_mock_api() diff --git a/tests/components/brunt/test_config_flow.py b/tests/components/brunt/test_config_flow.py index 2796882a3c167f..7a805a9ee523c9 100644 --- a/tests/components/brunt/test_config_flow.py +++ b/tests/components/brunt/test_config_flow.py @@ -110,15 +110,7 @@ async def test_reauth( unique_id="test-username", ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": entry.unique_id, - "entry_id": entry.entry_id, - }, - data=None, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" with patch( diff --git a/tests/components/bsblan/conftest.py b/tests/components/bsblan/conftest.py index 07ca8b648f321a..13d4017d7c8bbf 100644 --- a/tests/components/bsblan/conftest.py +++ b/tests/components/bsblan/conftest.py @@ -3,7 +3,7 @@ from collections.abc import Generator from unittest.mock import AsyncMock, MagicMock, patch -from bsblan import Device, Info, State +from bsblan import Device, Info, State, StaticState import pytest from homeassistant.components.bsblan.const import CONF_PASSKEY, DOMAIN @@ -42,7 +42,6 @@ def mock_setup_entry() -> Generator[AsyncMock]: @pytest.fixture def mock_bsblan() -> Generator[MagicMock]: """Return a mocked BSBLAN client.""" - with ( patch("homeassistant.components.bsblan.BSBLAN", autospec=True) as bsblan_mock, patch("homeassistant.components.bsblan.config_flow.BSBLAN", new=bsblan_mock), @@ -53,6 +52,11 @@ def mock_bsblan() -> Generator[MagicMock]: load_fixture("device.json", DOMAIN) ) bsblan.state.return_value = State.from_json(load_fixture("state.json", DOMAIN)) + + bsblan.static_values.return_value = StaticState.from_json( + load_fixture("static.json", DOMAIN) + ) + yield bsblan diff --git a/tests/components/bsblan/fixtures/static.json b/tests/components/bsblan/fixtures/static.json new file mode 100644 index 00000000000000..8c7abc3397bea7 --- /dev/null +++ b/tests/components/bsblan/fixtures/static.json @@ -0,0 +1,20 @@ +{ + "min_temp": { + "name": "Room temp frost protection setpoint", + "error": 0, + "value": "8.0", + "desc": "", + "dataType": 0, + "readonly": 0, + "unit": "°C" + }, + "max_temp": { + "name": "Summer/winter changeover temp heat circuit 1", + "error": 0, + "value": "20.0", + "desc": "", + "dataType": 0, + "readonly": 0, + "unit": "°C" + } +} diff --git a/tests/components/bsblan/snapshots/test_diagnostics.ambr b/tests/components/bsblan/snapshots/test_diagnostics.ambr index b172d26c249c9c..c9a82edf4e2f86 100644 --- a/tests/components/bsblan/snapshots/test_diagnostics.ambr +++ b/tests/components/bsblan/snapshots/test_diagnostics.ambr @@ -1,6 +1,52 @@ # serializer version: 1 # name: test_diagnostics dict({ + 'coordinator_data': dict({ + 'state': dict({ + 'current_temperature': dict({ + 'data_type': 0, + 'desc': '', + 'name': 'Room temp 1 actual value', + 'unit': '°C', + 'value': '18.6', + }), + 'hvac_action': dict({ + 'data_type': 1, + 'desc': 'Raumtemp’begrenzung', + 'name': 'Status heating circuit 1', + 'unit': '', + 'value': '122', + }), + 'hvac_mode': dict({ + 'data_type': 1, + 'desc': 'Komfort', + 'name': 'Operating mode', + 'unit': '', + 'value': 'heat', + }), + 'hvac_mode2': dict({ + 'data_type': 1, + 'desc': 'Reduziert', + 'name': 'Operating mode', + 'unit': '', + 'value': '2', + }), + 'room1_thermostat_mode': dict({ + 'data_type': 1, + 'desc': 'Kein Bedarf', + 'name': 'Raumthermostat 1', + 'unit': '', + 'value': '0', + }), + 'target_temperature': dict({ + 'data_type': 0, + 'desc': '', + 'name': 'Room temperature Comfort setpoint', + 'unit': '°C', + 'value': '18.5', + }), + }), + }), 'device': dict({ 'MAC': '00:80:41:19:69:90', 'name': 'BSB-LAN', @@ -30,48 +76,20 @@ 'value': 'RVS21.831F/127', }), }), - 'state': dict({ - 'current_temperature': dict({ + 'static': dict({ + 'max_temp': dict({ 'data_type': 0, 'desc': '', - 'name': 'Room temp 1 actual value', + 'name': 'Summer/winter changeover temp heat circuit 1', 'unit': '°C', - 'value': '18.6', - }), - 'hvac_action': dict({ - 'data_type': 1, - 'desc': 'Raumtemp’begrenzung', - 'name': 'Status heating circuit 1', - 'unit': '', - 'value': '122', - }), - 'hvac_mode': dict({ - 'data_type': 1, - 'desc': 'Komfort', - 'name': 'Operating mode', - 'unit': '', - 'value': 'heat', - }), - 'hvac_mode2': dict({ - 'data_type': 1, - 'desc': 'Reduziert', - 'name': 'Operating mode', - 'unit': '', - 'value': '2', - }), - 'room1_thermostat_mode': dict({ - 'data_type': 1, - 'desc': 'Kein Bedarf', - 'name': 'Raumthermostat 1', - 'unit': '', - 'value': '0', + 'value': '20.0', }), - 'target_temperature': dict({ + 'min_temp': dict({ 'data_type': 0, 'desc': '', - 'name': 'Room temperature Comfort setpoint', + 'name': 'Room temp frost protection setpoint', 'unit': '°C', - 'value': '18.5', + 'value': '8.0', }), }), }) diff --git a/tests/components/bsblan/test_diagnostics.py b/tests/components/bsblan/test_diagnostics.py index 316296df78a49f..8939456c2ac012 100644 --- a/tests/components/bsblan/test_diagnostics.py +++ b/tests/components/bsblan/test_diagnostics.py @@ -16,8 +16,7 @@ async def test_diagnostics( snapshot: SnapshotAssertion, ) -> None: """Test diagnostics.""" - - assert ( - await get_diagnostics_for_config_entry(hass, hass_client, init_integration) - == snapshot + diagnostics_data = await get_diagnostics_for_config_entry( + hass, hass_client, init_integration ) + assert diagnostics_data == snapshot diff --git a/tests/components/bthome/test_config_flow.py b/tests/components/bthome/test_config_flow.py index acf490d341e5e2..faf2f1c9ef520e 100644 --- a/tests/components/bthome/test_config_flow.py +++ b/tests/components/bthome/test_config_flow.py @@ -563,16 +563,7 @@ async def test_async_step_reauth_abort_early(hass: HomeAssistant) -> None: device = DeviceData() - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - "title_placeholders": {"name": entry.title}, - "unique_id": entry.unique_id, - }, - data=entry.data | {"device": device}, - ) + result = await entry.start_reauth_flow(hass, data={"device": device}) assert result["type"] is FlowResultType.ABORT assert result["reason"] == "reauth_successful" diff --git a/tests/components/bthome/test_device_trigger.py b/tests/components/bthome/test_device_trigger.py index 459654826f94f4..c4c900ef6e1031 100644 --- a/tests/components/bthome/test_device_trigger.py +++ b/tests/components/bthome/test_device_trigger.py @@ -1,10 +1,19 @@ """Test BTHome BLE events.""" +import pytest + from homeassistant.components import automation from homeassistant.components.bluetooth import DOMAIN as BLUETOOTH_DOMAIN from homeassistant.components.bthome.const import CONF_SUBTYPE, DOMAIN from homeassistant.components.device_automation import DeviceAutomationType -from homeassistant.const import CONF_DEVICE_ID, CONF_DOMAIN, CONF_PLATFORM, CONF_TYPE +from homeassistant.const import ( + CONF_DEVICE_ID, + CONF_DOMAIN, + CONF_PLATFORM, + CONF_TYPE, + STATE_ON, + STATE_UNAVAILABLE, +) from homeassistant.core import HomeAssistant, ServiceCall, callback from homeassistant.helpers import device_registry as dr from homeassistant.setup import async_setup_component @@ -121,6 +130,117 @@ async def test_get_triggers_button( await hass.async_block_till_done() +async def test_get_triggers_multiple_buttons( + hass: HomeAssistant, device_registry: dr.DeviceRegistry +) -> None: + """Test that we get the expected triggers for multiple buttons device.""" + mac = "A4:C1:38:8D:18:B2" + entry = await _async_setup_bthome_device(hass, mac) + events = async_capture_events(hass, "bthome_ble_event") + + # Emit button_1 long press and button_2 press events + # so it creates the device in the registry + inject_bluetooth_service_info_bleak( + hass, + make_bthome_v2_adv(mac, b"\x40\x3a\x04\x3a\x01"), + ) + + # wait for the event + await hass.async_block_till_done() + assert len(events) == 2 + + device = device_registry.async_get_device(identifiers={get_device_id(mac)}) + assert device + expected_trigger1 = { + CONF_PLATFORM: "device", + CONF_DOMAIN: DOMAIN, + CONF_DEVICE_ID: device.id, + CONF_TYPE: "button_1", + CONF_SUBTYPE: "long_press", + "metadata": {}, + } + expected_trigger2 = { + CONF_PLATFORM: "device", + CONF_DOMAIN: DOMAIN, + CONF_DEVICE_ID: device.id, + CONF_TYPE: "button_2", + CONF_SUBTYPE: "press", + "metadata": {}, + } + triggers = await async_get_device_automations( + hass, DeviceAutomationType.TRIGGER, device.id + ) + assert expected_trigger1 in triggers + assert expected_trigger2 in triggers + + assert await hass.config_entries.async_unload(entry.entry_id) + await hass.async_block_till_done() + + +@pytest.mark.parametrize( + ("event_class", "event_type", "expected"), + [ + ("button_1", "long_press", STATE_ON), + ("button_2", "press", STATE_ON), + ("button_3", "long_press", STATE_UNAVAILABLE), + ("button", "long_press", STATE_UNAVAILABLE), + ("button_1", "invalid_press", STATE_UNAVAILABLE), + ], +) +async def test_validate_trigger_config( + hass: HomeAssistant, + device_registry: dr.DeviceRegistry, + event_class: str, + event_type: str, + expected: str, +) -> None: + """Test unsupported trigger does not return a trigger config.""" + mac = "A4:C1:38:8D:18:B2" + entry = await _async_setup_bthome_device(hass, mac) + + # Emit button_1 long press and button_2 press events + # so it creates the device in the registry + inject_bluetooth_service_info_bleak( + hass, + make_bthome_v2_adv(mac, b"\x40\x3a\x04\x3a\x01"), + ) + + # wait for the event + await hass.async_block_till_done() + + device = device_registry.async_get_device(identifiers={get_device_id(mac)}) + + assert await async_setup_component( + hass, + automation.DOMAIN, + { + automation.DOMAIN: [ + { + "trigger": { + CONF_PLATFORM: "device", + CONF_DOMAIN: DOMAIN, + CONF_DEVICE_ID: device.id, + CONF_TYPE: event_class, + CONF_SUBTYPE: event_type, + }, + "action": { + "service": "test.automation", + "data_template": {"some": "test_trigger_button_long_press"}, + }, + }, + ] + }, + ) + await hass.async_block_till_done() + + automations = hass.states.async_entity_ids(automation.DOMAIN) + assert len(automations) == 1 + assert hass.states.get(automations[0]).state == expected + + assert await hass.config_entries.async_unload(entry.entry_id) + await hass.async_block_till_done() + + async def test_get_triggers_dimmer( hass: HomeAssistant, device_registry: dr.DeviceRegistry ) -> None: @@ -235,7 +355,7 @@ async def test_if_fires_on_motion_detected( make_bthome_v2_adv(mac, b"\x40\x3a\x03"), ) - # # wait for the event + # wait for the event await hass.async_block_till_done() device = device_registry.async_get_device(identifiers={get_device_id(mac)}) diff --git a/tests/components/caldav/test_config_flow.py b/tests/components/caldav/test_config_flow.py index 0079e59a931424..bf22fb0bd9c9e8 100644 --- a/tests/components/caldav/test_config_flow.py +++ b/tests/components/caldav/test_config_flow.py @@ -106,13 +106,7 @@ async def test_reauth_success( config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": config_entry.entry_id, - }, - ) + result = await config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -147,13 +141,7 @@ async def test_reauth_failure( config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": config_entry.entry_id, - }, - ) + result = await config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/cast/test_config_flow.py b/tests/components/cast/test_config_flow.py index 7dce3f768e2ed8..2dcf007c6d49dd 100644 --- a/tests/components/cast/test_config_flow.py +++ b/tests/components/cast/test_config_flow.py @@ -250,7 +250,7 @@ async def test_option_flow(hass: HomeAssistant, parameter_data) -> None: user_input=user_input_dict, ) assert result["type"] is FlowResultType.CREATE_ENTRY - assert result["data"] is None + assert result["data"] == {} for other_param in advanced_parameters: if other_param == parameter: continue @@ -264,7 +264,7 @@ async def test_option_flow(hass: HomeAssistant, parameter_data) -> None: user_input={"known_hosts": ""}, ) assert result["type"] is FlowResultType.CREATE_ENTRY - assert result["data"] is None + assert result["data"] == {} expected_data = {**orig_data, "known_hosts": []} if parameter in advanced_parameters: expected_data[parameter] = updated diff --git a/tests/components/climate/conftest.py b/tests/components/climate/conftest.py index fd4368c42191f0..4ade8606e77ceb 100644 --- a/tests/components/climate/conftest.py +++ b/tests/components/climate/conftest.py @@ -4,10 +4,18 @@ import pytest -from homeassistant.config_entries import ConfigFlow +from homeassistant.components.climate import DOMAIN as CLIMATE_DOMAIN +from homeassistant.config_entries import ConfigEntry, ConfigFlow +from homeassistant.const import Platform from homeassistant.core import HomeAssistant -from tests.common import mock_config_flow, mock_platform +from tests.common import ( + MockConfigEntry, + MockModule, + mock_config_flow, + mock_integration, + mock_platform, +) class MockFlow(ConfigFlow): @@ -21,3 +29,41 @@ def config_flow_fixture(hass: HomeAssistant) -> Generator[None]: with mock_config_flow("test", MockFlow): yield + + +@pytest.fixture +def register_test_integration( + hass: HomeAssistant, config_flow_fixture: None +) -> Generator: + """Provide a mocked integration for tests.""" + + config_entry = MockConfigEntry(domain="test") + config_entry.add_to_hass(hass) + + async def help_async_setup_entry_init( + hass: HomeAssistant, config_entry: ConfigEntry + ) -> bool: + """Set up test config entry.""" + await hass.config_entries.async_forward_entry_setups( + config_entry, [CLIMATE_DOMAIN] + ) + return True + + async def help_async_unload_entry( + hass: HomeAssistant, config_entry: ConfigEntry + ) -> bool: + """Unload test config emntry.""" + return await hass.config_entries.async_unload_platforms( + config_entry, [Platform.CLIMATE] + ) + + mock_integration( + hass, + MockModule( + "test", + async_setup_entry=help_async_setup_entry_init, + async_unload_entry=help_async_unload_entry, + ), + ) + + return config_entry diff --git a/tests/components/climate/test_init.py b/tests/components/climate/test_init.py index f306551e5400e0..256ecf92b1de4d 100644 --- a/tests/components/climate/test_init.py +++ b/tests/components/climate/test_init.py @@ -56,6 +56,7 @@ import_and_test_deprecated_constant_enum, mock_integration, mock_platform, + setup_test_component_platform, ) @@ -237,42 +238,15 @@ def test_deprecated_current_constants( async def test_preset_mode_validation( - hass: HomeAssistant, config_flow_fixture: None + hass: HomeAssistant, register_test_integration: MockConfigEntry ) -> None: """Test mode validation for fan, swing and preset.""" + climate_entity = MockClimateEntity(name="test", entity_id="climate.test") - async def async_setup_entry_init( - hass: HomeAssistant, config_entry: ConfigEntry - ) -> bool: - """Set up test config entry.""" - await hass.config_entries.async_forward_entry_setups(config_entry, [DOMAIN]) - return True - - async def async_setup_entry_climate_platform( - hass: HomeAssistant, - config_entry: ConfigEntry, - async_add_entities: AddEntitiesCallback, - ) -> None: - """Set up test climate platform via config entry.""" - async_add_entities([MockClimateEntity(name="test", entity_id="climate.test")]) - - mock_integration( - hass, - MockModule( - "test", - async_setup_entry=async_setup_entry_init, - ), - built_in=False, + setup_test_component_platform( + hass, DOMAIN, entities=[climate_entity], from_config_entry=True ) - mock_platform( - hass, - "test.climate", - MockPlatform(async_setup_entry=async_setup_entry_climate_platform), - ) - - config_entry = MockConfigEntry(domain="test") - config_entry.add_to_hass(hass) - assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.config_entries.async_setup(register_test_integration.entry_id) await hass.async_block_till_done() state = hass.states.get("climate.test") @@ -402,7 +376,9 @@ def supported_features(self) -> int: async def test_warning_not_implemented_turn_on_off_feature( - hass: HomeAssistant, caplog: pytest.LogCaptureFixture, config_flow_fixture: None + hass: HomeAssistant, + caplog: pytest.LogCaptureFixture, + register_test_integration: MockConfigEntry, ) -> None: """Test adding feature flag and warn if missing when methods are set.""" @@ -419,43 +395,15 @@ def turn_off(self) -> None: """Turn off.""" called.append("turn_off") - async def async_setup_entry_init( - hass: HomeAssistant, config_entry: ConfigEntry - ) -> bool: - """Set up test config entry.""" - await hass.config_entries.async_forward_entry_setups(config_entry, [DOMAIN]) - return True - - async def async_setup_entry_climate_platform( - hass: HomeAssistant, - config_entry: ConfigEntry, - async_add_entities: AddEntitiesCallback, - ) -> None: - """Set up test climate platform via config entry.""" - async_add_entities( - [MockClimateEntityTest(name="test", entity_id="climate.test")] - ) - - mock_integration( - hass, - MockModule( - "test", - async_setup_entry=async_setup_entry_init, - ), - built_in=False, - ) - mock_platform( - hass, - "test.climate", - MockPlatform(async_setup_entry=async_setup_entry_climate_platform), - ) + climate_entity = MockClimateEntityTest(name="test", entity_id="climate.test") with patch.object( MockClimateEntityTest, "__module__", "tests.custom_components.climate.test_init" ): - config_entry = MockConfigEntry(domain="test") - config_entry.add_to_hass(hass) - assert await hass.config_entries.async_setup(config_entry.entry_id) + setup_test_component_platform( + hass, DOMAIN, entities=[climate_entity], from_config_entry=True + ) + await hass.config_entries.async_setup(register_test_integration.entry_id) await hass.async_block_till_done() state = hass.states.get("climate.test") @@ -499,7 +447,9 @@ async def async_setup_entry_climate_platform( async def test_implicit_warning_not_implemented_turn_on_off_feature( - hass: HomeAssistant, caplog: pytest.LogCaptureFixture, config_flow_fixture: None + hass: HomeAssistant, + caplog: pytest.LogCaptureFixture, + register_test_integration: MockConfigEntry, ) -> None: """Test adding feature flag and warn if missing when methods are not set. @@ -527,43 +477,15 @@ def hvac_modes(self) -> list[HVACMode]: """ return [HVACMode.OFF, HVACMode.HEAT] - async def async_setup_entry_init( - hass: HomeAssistant, config_entry: ConfigEntry - ) -> bool: - """Set up test config entry.""" - await hass.config_entries.async_forward_entry_setups(config_entry, [DOMAIN]) - return True - - async def async_setup_entry_climate_platform( - hass: HomeAssistant, - config_entry: ConfigEntry, - async_add_entities: AddEntitiesCallback, - ) -> None: - """Set up test climate platform via config entry.""" - async_add_entities( - [MockClimateEntityTest(name="test", entity_id="climate.test")] - ) - - mock_integration( - hass, - MockModule( - "test", - async_setup_entry=async_setup_entry_init, - ), - built_in=False, - ) - mock_platform( - hass, - "test.climate", - MockPlatform(async_setup_entry=async_setup_entry_climate_platform), - ) + climate_entity = MockClimateEntityTest(name="test", entity_id="climate.test") with patch.object( MockClimateEntityTest, "__module__", "tests.custom_components.climate.test_init" ): - config_entry = MockConfigEntry(domain="test") - config_entry.add_to_hass(hass) - assert await hass.config_entries.async_setup(config_entry.entry_id) + setup_test_component_platform( + hass, DOMAIN, entities=[climate_entity], from_config_entry=True + ) + await hass.config_entries.async_setup(register_test_integration.entry_id) await hass.async_block_till_done() state = hass.states.get("climate.test") @@ -579,7 +501,9 @@ async def async_setup_entry_climate_platform( async def test_no_warning_implemented_turn_on_off_feature( - hass: HomeAssistant, caplog: pytest.LogCaptureFixture, config_flow_fixture: None + hass: HomeAssistant, + caplog: pytest.LogCaptureFixture, + register_test_integration: MockConfigEntry, ) -> None: """Test no warning when feature flags are set.""" @@ -594,43 +518,15 @@ class MockClimateEntityTest(MockClimateEntity): | ClimateEntityFeature.TURN_ON ) - async def async_setup_entry_init( - hass: HomeAssistant, config_entry: ConfigEntry - ) -> bool: - """Set up test config entry.""" - await hass.config_entries.async_forward_entry_setups(config_entry, [DOMAIN]) - return True - - async def async_setup_entry_climate_platform( - hass: HomeAssistant, - config_entry: ConfigEntry, - async_add_entities: AddEntitiesCallback, - ) -> None: - """Set up test climate platform via config entry.""" - async_add_entities( - [MockClimateEntityTest(name="test", entity_id="climate.test")] - ) - - mock_integration( - hass, - MockModule( - "test", - async_setup_entry=async_setup_entry_init, - ), - built_in=False, - ) - mock_platform( - hass, - "test.climate", - MockPlatform(async_setup_entry=async_setup_entry_climate_platform), - ) + climate_entity = MockClimateEntityTest(name="test", entity_id="climate.test") with patch.object( MockClimateEntityTest, "__module__", "tests.custom_components.climate.test_init" ): - config_entry = MockConfigEntry(domain="test") - config_entry.add_to_hass(hass) - assert await hass.config_entries.async_setup(config_entry.entry_id) + setup_test_component_platform( + hass, DOMAIN, entities=[climate_entity], from_config_entry=True + ) + await hass.config_entries.async_setup(register_test_integration.entry_id) await hass.async_block_till_done() state = hass.states.get("climate.test") @@ -651,7 +547,9 @@ async def async_setup_entry_climate_platform( async def test_no_warning_integration_has_migrated( - hass: HomeAssistant, caplog: pytest.LogCaptureFixture, config_flow_fixture: None + hass: HomeAssistant, + caplog: pytest.LogCaptureFixture, + register_test_integration: MockConfigEntry, ) -> None: """Test no warning when integration migrated using `_enable_turn_on_off_backwards_compatibility`.""" @@ -665,43 +563,15 @@ class MockClimateEntityTest(MockClimateEntity): | ClimateEntityFeature.SWING_MODE ) - async def async_setup_entry_init( - hass: HomeAssistant, config_entry: ConfigEntry - ) -> bool: - """Set up test config entry.""" - await hass.config_entries.async_forward_entry_setups(config_entry, [DOMAIN]) - return True - - async def async_setup_entry_climate_platform( - hass: HomeAssistant, - config_entry: ConfigEntry, - async_add_entities: AddEntitiesCallback, - ) -> None: - """Set up test climate platform via config entry.""" - async_add_entities( - [MockClimateEntityTest(name="test", entity_id="climate.test")] - ) - - mock_integration( - hass, - MockModule( - "test", - async_setup_entry=async_setup_entry_init, - ), - built_in=False, - ) - mock_platform( - hass, - "test.climate", - MockPlatform(async_setup_entry=async_setup_entry_climate_platform), - ) + climate_entity = MockClimateEntityTest(name="test", entity_id="climate.test") with patch.object( MockClimateEntityTest, "__module__", "tests.custom_components.climate.test_init" ): - config_entry = MockConfigEntry(domain="test") - config_entry.add_to_hass(hass) - assert await hass.config_entries.async_setup(config_entry.entry_id) + setup_test_component_platform( + hass, DOMAIN, entities=[climate_entity], from_config_entry=True + ) + await hass.config_entries.async_setup(register_test_integration.entry_id) await hass.async_block_till_done() state = hass.states.get("climate.test") @@ -722,7 +592,9 @@ async def async_setup_entry_climate_platform( async def test_no_warning_integration_implement_feature_flags( - hass: HomeAssistant, caplog: pytest.LogCaptureFixture, config_flow_fixture: None + hass: HomeAssistant, + caplog: pytest.LogCaptureFixture, + register_test_integration: MockConfigEntry, ) -> None: """Test no warning when integration uses the correct feature flags.""" @@ -737,43 +609,15 @@ class MockClimateEntityTest(MockClimateEntity): | ClimateEntityFeature.TURN_ON ) - async def async_setup_entry_init( - hass: HomeAssistant, config_entry: ConfigEntry - ) -> bool: - """Set up test config entry.""" - await hass.config_entries.async_forward_entry_setups(config_entry, [DOMAIN]) - return True - - async def async_setup_entry_climate_platform( - hass: HomeAssistant, - config_entry: ConfigEntry, - async_add_entities: AddEntitiesCallback, - ) -> None: - """Set up test climate platform via config entry.""" - async_add_entities( - [MockClimateEntityTest(name="test", entity_id="climate.test")] - ) - - mock_integration( - hass, - MockModule( - "test", - async_setup_entry=async_setup_entry_init, - ), - built_in=False, - ) - mock_platform( - hass, - "test.climate", - MockPlatform(async_setup_entry=async_setup_entry_climate_platform), - ) + climate_entity = MockClimateEntityTest(name="test", entity_id="climate.test") with patch.object( MockClimateEntityTest, "__module__", "tests.custom_components.climate.test_init" ): - config_entry = MockConfigEntry(domain="test") - config_entry.add_to_hass(hass) - assert await hass.config_entries.async_setup(config_entry.entry_id) + setup_test_component_platform( + hass, DOMAIN, entities=[climate_entity], from_config_entry=True + ) + await hass.config_entries.async_setup(register_test_integration.entry_id) await hass.async_block_till_done() state = hass.states.get("climate.test") @@ -1022,7 +866,7 @@ async def async_setup_entry_climate_platform( async def test_no_issue_aux_property_deprecated_for_core( hass: HomeAssistant, caplog: pytest.LogCaptureFixture, - config_flow_fixture: None, + register_test_integration: MockConfigEntry, manifest_extra: dict[str, str], translation_key: str, translation_placeholders_extra: dict[str, str], @@ -1061,39 +905,10 @@ async def async_turn_aux_heat_off(self) -> None: entity_id="climate.testing", ) - async def async_setup_entry_init( - hass: HomeAssistant, config_entry: ConfigEntry - ) -> bool: - """Set up test config entry.""" - await hass.config_entries.async_forward_entry_setups(config_entry, [DOMAIN]) - return True - - async def async_setup_entry_climate_platform( - hass: HomeAssistant, - config_entry: ConfigEntry, - async_add_entities: AddEntitiesCallback, - ) -> None: - """Set up test weather platform via config entry.""" - async_add_entities([climate_entity]) - - mock_integration( - hass, - MockModule( - "test", - async_setup_entry=async_setup_entry_init, - partial_manifest=manifest_extra, - ), - built_in=False, + setup_test_component_platform( + hass, DOMAIN, entities=[climate_entity], from_config_entry=True ) - mock_platform( - hass, - "test.climate", - MockPlatform(async_setup_entry=async_setup_entry_climate_platform), - ) - - config_entry = MockConfigEntry(domain="test") - config_entry.add_to_hass(hass) - assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.config_entries.async_setup(register_test_integration.entry_id) await hass.async_block_till_done() assert climate_entity.state == HVACMode.HEAT @@ -1111,7 +926,7 @@ async def async_setup_entry_climate_platform( async def test_no_issue_no_aux_property( hass: HomeAssistant, caplog: pytest.LogCaptureFixture, - config_flow_fixture: None, + register_test_integration: MockConfigEntry, issue_registry: ir.IssueRegistry, ) -> None: """Test the issue is raised on deprecated auxiliary heater attributes.""" @@ -1121,38 +936,10 @@ async def test_no_issue_no_aux_property( entity_id="climate.testing", ) - async def async_setup_entry_init( - hass: HomeAssistant, config_entry: ConfigEntry - ) -> bool: - """Set up test config entry.""" - await hass.config_entries.async_forward_entry_setups(config_entry, [DOMAIN]) - return True - - async def async_setup_entry_climate_platform( - hass: HomeAssistant, - config_entry: ConfigEntry, - async_add_entities: AddEntitiesCallback, - ) -> None: - """Set up test weather platform via config entry.""" - async_add_entities([climate_entity]) - - mock_integration( - hass, - MockModule( - "test", - async_setup_entry=async_setup_entry_init, - ), - built_in=False, - ) - mock_platform( - hass, - "test.climate", - MockPlatform(async_setup_entry=async_setup_entry_climate_platform), + setup_test_component_platform( + hass, DOMAIN, entities=[climate_entity], from_config_entry=True ) - - config_entry = MockConfigEntry(domain="test") - config_entry.add_to_hass(hass) - assert await hass.config_entries.async_setup(config_entry.entry_id) + assert await hass.config_entries.async_setup(register_test_integration.entry_id) await hass.async_block_till_done() assert climate_entity.state == HVACMode.HEAT @@ -1167,7 +954,7 @@ async def async_setup_entry_climate_platform( async def test_temperature_validation( - hass: HomeAssistant, config_flow_fixture: None + hass: HomeAssistant, register_test_integration: MockConfigEntry ) -> None: """Test validation for temperatures.""" @@ -1194,40 +981,15 @@ def set_temperature(self, **kwargs: Any) -> None: self._attr_target_temperature_high = kwargs[ATTR_TARGET_TEMP_HIGH] self._attr_target_temperature_low = kwargs[ATTR_TARGET_TEMP_LOW] - async def async_setup_entry_init( - hass: HomeAssistant, config_entry: ConfigEntry - ) -> bool: - """Set up test config entry.""" - await hass.config_entries.async_forward_entry_setups(config_entry, [DOMAIN]) - return True - - async def async_setup_entry_climate_platform( - hass: HomeAssistant, - config_entry: ConfigEntry, - async_add_entities: AddEntitiesCallback, - ) -> None: - """Set up test climate platform via config entry.""" - async_add_entities( - [MockClimateEntityTemp(name="test", entity_id="climate.test")] - ) - - mock_integration( - hass, - MockModule( - "test", - async_setup_entry=async_setup_entry_init, - ), - built_in=False, - ) - mock_platform( - hass, - "test.climate", - MockPlatform(async_setup_entry=async_setup_entry_climate_platform), + test_climate = MockClimateEntityTemp( + name="Test", + unique_id="unique_climate_test", ) - config_entry = MockConfigEntry(domain="test") - config_entry.add_to_hass(hass) - assert await hass.config_entries.async_setup(config_entry.entry_id) + setup_test_component_platform( + hass, DOMAIN, entities=[test_climate], from_config_entry=True + ) + await hass.config_entries.async_setup(register_test_integration.entry_id) await hass.async_block_till_done() state = hass.states.get("climate.test") diff --git a/tests/components/cloudflare/test_config_flow.py b/tests/components/cloudflare/test_config_flow.py index 1278113c0c7469..f34a423833ce83 100644 --- a/tests/components/cloudflare/test_config_flow.py +++ b/tests/components/cloudflare/test_config_flow.py @@ -5,7 +5,7 @@ import pycfdns from homeassistant.components.cloudflare.const import CONF_RECORDS, DOMAIN -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_USER from homeassistant.const import CONF_API_TOKEN, CONF_SOURCE, CONF_ZONE from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -151,15 +151,7 @@ async def test_reauth_flow(hass: HomeAssistant, cfupdate_flow: MagicMock) -> Non entry = MockConfigEntry(domain=DOMAIN, data=ENTRY_CONFIG) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": entry.unique_id, - "entry_id": entry.entry_id, - }, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/co2signal/test_config_flow.py b/tests/components/co2signal/test_config_flow.py index 7397b6e235583e..ad61ae4f897040 100644 --- a/tests/components/co2signal/test_config_flow.py +++ b/tests/components/co2signal/test_config_flow.py @@ -198,17 +198,10 @@ async def test_reauth( """Test reauth flow.""" config_entry.add_to_hass(hass) - init_result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": config_entry.entry_id, - }, - data=None, - ) + init_result = await config_entry.start_reauth_flow(hass) assert init_result["type"] is FlowResultType.FORM - assert init_result["step_id"] == "reauth" + assert init_result["step_id"] == "reauth_confirm" with patch( "homeassistant.components.co2signal.async_setup_entry", diff --git a/tests/components/co2signal/test_sensor.py b/tests/components/co2signal/test_sensor.py index e9f46e483d1714..fddda17f3ed17a 100644 --- a/tests/components/co2signal/test_sensor.py +++ b/tests/components/co2signal/test_sensor.py @@ -109,4 +109,4 @@ async def test_sensor_reauth_triggered( assert (flows := hass.config_entries.flow.async_progress()) assert len(flows) == 1 - assert flows[0]["step_id"] == "reauth" + assert flows[0]["step_id"] == "reauth_confirm" diff --git a/tests/components/comelit/test_config_flow.py b/tests/components/comelit/test_config_flow.py index 333bf09bd202bb..eeaea0e41e91ec 100644 --- a/tests/components/comelit/test_config_flow.py +++ b/tests/components/comelit/test_config_flow.py @@ -7,7 +7,7 @@ import pytest from homeassistant.components.comelit.const import DOMAIN -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_USER from homeassistant.const import CONF_HOST, CONF_PIN, CONF_PORT from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -100,6 +100,9 @@ async def test_reauth_successful(hass: HomeAssistant) -> None: mock_config = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_BRIDGE_DATA) mock_config.add_to_hass(hass) + result = await mock_config.start_reauth_flow(hass) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reauth_confirm" with ( patch( @@ -113,15 +116,6 @@ async def test_reauth_successful(hass: HomeAssistant) -> None: ): mock_request_get.return_value.status_code = 200 - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH, "entry_id": mock_config.entry_id}, - data=mock_config.data, - ) - - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "reauth_confirm" - result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={ @@ -147,6 +141,9 @@ async def test_reauth_not_successful(hass: HomeAssistant, side_effect, error) -> mock_config = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_BRIDGE_DATA) mock_config.add_to_hass(hass) + result = await mock_config.start_reauth_flow(hass) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reauth_confirm" with ( patch("aiocomelit.api.ComeliteSerialBridgeApi.login", side_effect=side_effect), @@ -155,15 +152,6 @@ async def test_reauth_not_successful(hass: HomeAssistant, side_effect, error) -> ), patch("homeassistant.components.comelit.async_setup_entry"), ): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH, "entry_id": mock_config.entry_id}, - data=mock_config.data, - ) - - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "reauth_confirm" - result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={ diff --git a/tests/components/conftest.py b/tests/components/conftest.py index 7d15bde88c0079..39ff7071dc4c08 100644 --- a/tests/components/conftest.py +++ b/tests/components/conftest.py @@ -6,7 +6,7 @@ from importlib.util import find_spec from pathlib import Path from typing import TYPE_CHECKING, Any -from unittest.mock import MagicMock, patch +from unittest.mock import AsyncMock, MagicMock, patch import pytest @@ -14,6 +14,8 @@ from homeassistant.core import HomeAssistant if TYPE_CHECKING: + from homeassistant.components.hassio.addon_manager import AddonManager + from .conversation import MockAgent from .device_tracker.common import MockScanner from .light.common import MockLight @@ -180,3 +182,230 @@ def mock_legacy_device_tracker_setup() -> Callable[[HomeAssistant, MockScanner], from .device_tracker.common import mock_legacy_device_tracker_setup return mock_legacy_device_tracker_setup + + +@pytest.fixture(name="addon_manager") +def addon_manager_fixture(hass: HomeAssistant) -> AddonManager: + """Return an AddonManager instance.""" + # pylint: disable-next=import-outside-toplevel + from .hassio.common import mock_addon_manager + + return mock_addon_manager(hass) + + +@pytest.fixture(name="discovery_info") +def discovery_info_fixture() -> Any: + """Return the discovery info from the supervisor.""" + # pylint: disable-next=import-outside-toplevel + from .hassio.common import mock_discovery_info + + return mock_discovery_info() + + +@pytest.fixture(name="discovery_info_side_effect") +def discovery_info_side_effect_fixture() -> Any | None: + """Return the discovery info from the supervisor.""" + return None + + +@pytest.fixture(name="get_addon_discovery_info") +def get_addon_discovery_info_fixture( + discovery_info: dict[str, Any], discovery_info_side_effect: Any | None +) -> Generator[AsyncMock]: + """Mock get add-on discovery info.""" + # pylint: disable-next=import-outside-toplevel + from .hassio.common import mock_get_addon_discovery_info + + yield from mock_get_addon_discovery_info(discovery_info, discovery_info_side_effect) + + +@pytest.fixture(name="addon_store_info_side_effect") +def addon_store_info_side_effect_fixture() -> Any | None: + """Return the add-on store info side effect.""" + return None + + +@pytest.fixture(name="addon_store_info") +def addon_store_info_fixture( + addon_store_info_side_effect: Any | None, +) -> Generator[AsyncMock]: + """Mock Supervisor add-on store info.""" + # pylint: disable-next=import-outside-toplevel + from .hassio.common import mock_addon_store_info + + yield from mock_addon_store_info(addon_store_info_side_effect) + + +@pytest.fixture(name="addon_info_side_effect") +def addon_info_side_effect_fixture() -> Any | None: + """Return the add-on info side effect.""" + return None + + +@pytest.fixture(name="addon_info") +def addon_info_fixture(addon_info_side_effect: Any | None) -> Generator[AsyncMock]: + """Mock Supervisor add-on info.""" + # pylint: disable-next=import-outside-toplevel + from .hassio.common import mock_addon_info + + yield from mock_addon_info(addon_info_side_effect) + + +@pytest.fixture(name="addon_not_installed") +def addon_not_installed_fixture( + addon_store_info: AsyncMock, addon_info: AsyncMock +) -> AsyncMock: + """Mock add-on not installed.""" + # pylint: disable-next=import-outside-toplevel + from .hassio.common import mock_addon_not_installed + + return mock_addon_not_installed(addon_store_info, addon_info) + + +@pytest.fixture(name="addon_installed") +def addon_installed_fixture( + addon_store_info: AsyncMock, addon_info: AsyncMock +) -> AsyncMock: + """Mock add-on already installed but not running.""" + # pylint: disable-next=import-outside-toplevel + from .hassio.common import mock_addon_installed + + return mock_addon_installed(addon_store_info, addon_info) + + +@pytest.fixture(name="addon_running") +def addon_running_fixture( + addon_store_info: AsyncMock, addon_info: AsyncMock +) -> AsyncMock: + """Mock add-on already running.""" + # pylint: disable-next=import-outside-toplevel + from .hassio.common import mock_addon_running + + return mock_addon_running(addon_store_info, addon_info) + + +@pytest.fixture(name="install_addon_side_effect") +def install_addon_side_effect_fixture( + addon_store_info: AsyncMock, addon_info: AsyncMock +) -> Any | None: + """Return the install add-on side effect.""" + + # pylint: disable-next=import-outside-toplevel + from .hassio.common import mock_install_addon_side_effect + + return mock_install_addon_side_effect(addon_store_info, addon_info) + + +@pytest.fixture(name="install_addon") +def install_addon_fixture( + install_addon_side_effect: Any | None, +) -> Generator[AsyncMock]: + """Mock install add-on.""" + # pylint: disable-next=import-outside-toplevel + from .hassio.common import mock_install_addon + + yield from mock_install_addon(install_addon_side_effect) + + +@pytest.fixture(name="start_addon_side_effect") +def start_addon_side_effect_fixture( + addon_store_info: AsyncMock, addon_info: AsyncMock +) -> Any | None: + """Return the start add-on options side effect.""" + # pylint: disable-next=import-outside-toplevel + from .hassio.common import mock_start_addon_side_effect + + return mock_start_addon_side_effect(addon_store_info, addon_info) + + +@pytest.fixture(name="start_addon") +def start_addon_fixture(start_addon_side_effect: Any | None) -> Generator[AsyncMock]: + """Mock start add-on.""" + # pylint: disable-next=import-outside-toplevel + from .hassio.common import mock_start_addon + + yield from mock_start_addon(start_addon_side_effect) + + +@pytest.fixture(name="restart_addon_side_effect") +def restart_addon_side_effect_fixture() -> Any | None: + """Return the restart add-on options side effect.""" + return None + + +@pytest.fixture(name="restart_addon") +def restart_addon_fixture( + restart_addon_side_effect: Any | None, +) -> Generator[AsyncMock]: + """Mock restart add-on.""" + # pylint: disable-next=import-outside-toplevel + from .hassio.common import mock_restart_addon + + yield from mock_restart_addon(restart_addon_side_effect) + + +@pytest.fixture(name="stop_addon") +def stop_addon_fixture() -> Generator[AsyncMock]: + """Mock stop add-on.""" + # pylint: disable-next=import-outside-toplevel + from .hassio.common import mock_stop_addon + + yield from mock_stop_addon() + + +@pytest.fixture(name="addon_options") +def addon_options_fixture(addon_info: AsyncMock) -> dict[str, Any]: + """Mock add-on options.""" + # pylint: disable-next=import-outside-toplevel + from .hassio.common import mock_addon_options + + return mock_addon_options(addon_info) + + +@pytest.fixture(name="set_addon_options_side_effect") +def set_addon_options_side_effect_fixture( + addon_options: dict[str, Any], +) -> Any | None: + """Return the set add-on options side effect.""" + # pylint: disable-next=import-outside-toplevel + from .hassio.common import mock_set_addon_options_side_effect + + return mock_set_addon_options_side_effect(addon_options) + + +@pytest.fixture(name="set_addon_options") +def set_addon_options_fixture( + set_addon_options_side_effect: Any | None, +) -> Generator[AsyncMock]: + """Mock set add-on options.""" + # pylint: disable-next=import-outside-toplevel + from .hassio.common import mock_set_addon_options + + yield from mock_set_addon_options(set_addon_options_side_effect) + + +@pytest.fixture(name="uninstall_addon") +def uninstall_addon_fixture() -> Generator[AsyncMock]: + """Mock uninstall add-on.""" + # pylint: disable-next=import-outside-toplevel + from .hassio.common import mock_uninstall_addon + + yield from mock_uninstall_addon() + + +@pytest.fixture(name="create_backup") +def create_backup_fixture() -> Generator[AsyncMock]: + """Mock create backup.""" + # pylint: disable-next=import-outside-toplevel + from .hassio.common import mock_create_backup + + yield from mock_create_backup() + + +@pytest.fixture(name="update_addon") +def update_addon_fixture() -> Generator[AsyncMock]: + """Mock update add-on.""" + # pylint: disable-next=import-outside-toplevel + from .hassio.common import mock_update_addon + + yield from mock_update_addon() diff --git a/tests/components/conversation/test_agent_manager.py b/tests/components/conversation/test_agent_manager.py new file mode 100644 index 00000000000000..47b58a522a8fbd --- /dev/null +++ b/tests/components/conversation/test_agent_manager.py @@ -0,0 +1,34 @@ +"""Test agent manager.""" + +from unittest.mock import patch + +from homeassistant.components.conversation import ConversationResult, async_converse +from homeassistant.core import Context, HomeAssistant +from homeassistant.helpers.intent import IntentResponse + + +async def test_async_converse(hass: HomeAssistant, init_components) -> None: + """Test the async_converse method.""" + context = Context() + with patch( + "homeassistant.components.conversation.default_agent.DefaultAgent.async_process", + return_value=ConversationResult(response=IntentResponse(language="test lang")), + ) as mock_process: + await async_converse( + hass, + text="test command", + conversation_id="test id", + context=context, + language="test lang", + agent_id="conversation.home_assistant", + device_id="test device id", + ) + + assert mock_process.called + conversation_input = mock_process.call_args[0][0] + assert conversation_input.text == "test command" + assert conversation_input.conversation_id == "test id" + assert conversation_input.context is context + assert conversation_input.language == "test lang" + assert conversation_input.agent_id == "conversation.home_assistant" + assert conversation_input.device_id == "test device id" diff --git a/tests/components/deako/__init__.py b/tests/components/deako/__init__.py new file mode 100644 index 00000000000000..248a389f2e6898 --- /dev/null +++ b/tests/components/deako/__init__.py @@ -0,0 +1 @@ +"""Tests for the Deako integration.""" diff --git a/tests/components/deako/conftest.py b/tests/components/deako/conftest.py new file mode 100644 index 00000000000000..659634b87845e2 --- /dev/null +++ b/tests/components/deako/conftest.py @@ -0,0 +1,45 @@ +"""deako session fixtures.""" + +from collections.abc import Generator +from unittest.mock import MagicMock, patch + +import pytest + +from homeassistant.components.deako.const import DOMAIN + +from tests.common import MockConfigEntry + + +@pytest.fixture +def mock_config_entry() -> MockConfigEntry: + """Return the default mocked config entry.""" + return MockConfigEntry( + domain=DOMAIN, + ) + + +@pytest.fixture(autouse=True) +def pydeako_deako_mock() -> Generator[MagicMock]: + """Mock pydeako deako client.""" + with patch("homeassistant.components.deako.Deako", autospec=True) as mock: + yield mock + + +@pytest.fixture(autouse=True) +def pydeako_discoverer_mock(mock_async_zeroconf: MagicMock) -> Generator[MagicMock]: + """Mock pydeako discovery client.""" + with ( + patch("homeassistant.components.deako.DeakoDiscoverer", autospec=True) as mock, + patch("homeassistant.components.deako.config_flow.DeakoDiscoverer", new=mock), + ): + yield mock + + +@pytest.fixture +def mock_deako_setup() -> Generator[MagicMock]: + """Mock async_setup_entry for config flow tests.""" + with patch( + "homeassistant.components.deako.async_setup_entry", + return_value=True, + ) as mock_setup: + yield mock_setup diff --git a/tests/components/deako/snapshots/test_light.ambr b/tests/components/deako/snapshots/test_light.ambr new file mode 100644 index 00000000000000..7bc170654e1faf --- /dev/null +++ b/tests/components/deako/snapshots/test_light.ambr @@ -0,0 +1,168 @@ +# serializer version: 1 +# name: test_dimmable_light_props[light.kitchen-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'supported_color_modes': list([ + , + ]), + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'light', + 'entity_category': None, + 'entity_id': 'light.kitchen', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': None, + 'platform': 'deako', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': 'uuid', + 'unit_of_measurement': None, + }) +# --- +# name: test_dimmable_light_props[light.kitchen-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'brightness': 127, + 'color_mode': , + 'friendly_name': 'kitchen', + 'supported_color_modes': list([ + , + ]), + 'supported_features': , + }), + 'context': , + 'entity_id': 'light.kitchen', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_light_initial_props[light.kitchen-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'supported_color_modes': list([ + , + ]), + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'light', + 'entity_category': None, + 'entity_id': 'light.kitchen', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': None, + 'platform': 'deako', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': 'uuid', + 'unit_of_measurement': None, + }) +# --- +# name: test_light_initial_props[light.kitchen-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'color_mode': None, + 'friendly_name': 'kitchen', + 'supported_color_modes': list([ + , + ]), + 'supported_features': , + }), + 'context': , + 'entity_id': 'light.kitchen', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_light_setup_with_device[light.some_device-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'supported_color_modes': list([ + , + ]), + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'light', + 'entity_category': None, + 'entity_id': 'light.some_device', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': None, + 'platform': 'deako', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': 'some_device', + 'unit_of_measurement': None, + }) +# --- +# name: test_light_setup_with_device[light.some_device-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'brightness': 1, + 'color_mode': , + 'friendly_name': 'some device', + 'supported_color_modes': list([ + , + ]), + 'supported_features': , + }), + 'context': , + 'entity_id': 'light.some_device', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- diff --git a/tests/components/deako/test_config_flow.py b/tests/components/deako/test_config_flow.py new file mode 100644 index 00000000000000..21b10eaaa3616a --- /dev/null +++ b/tests/components/deako/test_config_flow.py @@ -0,0 +1,80 @@ +"""Tests for the deako component config flow.""" + +from unittest.mock import MagicMock + +from pydeako.discover import DevicesNotFoundException + +from homeassistant.components.deako.const import DOMAIN +from homeassistant.config_entries import SOURCE_USER +from homeassistant.core import HomeAssistant +from homeassistant.data_entry_flow import FlowResultType + +from tests.common import MockConfigEntry + + +async def test_found( + hass: HomeAssistant, + pydeako_discoverer_mock: MagicMock, + mock_deako_setup: MagicMock, +) -> None: + """Test finding a Deako device.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + + # Confirmation form + assert result["type"] is FlowResultType.FORM + + result = await hass.config_entries.flow.async_configure(result["flow_id"], {}) + await hass.async_block_till_done() + + assert result["type"] is FlowResultType.CREATE_ENTRY + pydeako_discoverer_mock.return_value.get_address.assert_called_once() + + mock_deako_setup.assert_called_once() + + +async def test_not_found( + hass: HomeAssistant, + pydeako_discoverer_mock: MagicMock, + mock_deako_setup: MagicMock, +) -> None: + """Test not finding any Deako devices.""" + pydeako_discoverer_mock.return_value.get_address.side_effect = ( + DevicesNotFoundException() + ) + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + + # Confirmation form + assert result["type"] is FlowResultType.FORM + + result = await hass.config_entries.flow.async_configure(result["flow_id"], {}) + await hass.async_block_till_done() + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "no_devices_found" + pydeako_discoverer_mock.return_value.get_address.assert_called_once() + + mock_deako_setup.assert_not_called() + + +async def test_already_configured( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_deako_setup: MagicMock, +) -> None: + """Test flow aborts when already configured.""" + + mock_config_entry.add_to_hass(hass) + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "single_instance_allowed" + + mock_deako_setup.assert_not_called() diff --git a/tests/components/deako/test_init.py b/tests/components/deako/test_init.py new file mode 100644 index 00000000000000..b4c0e8bb1f7079 --- /dev/null +++ b/tests/components/deako/test_init.py @@ -0,0 +1,87 @@ +"""Tests for the deako component init.""" + +from unittest.mock import MagicMock + +from pydeako.deako import DeviceListTimeout, FindDevicesTimeout + +from homeassistant.config_entries import ConfigEntryState +from homeassistant.core import HomeAssistant + +from tests.common import MockConfigEntry + + +async def test_deako_async_setup_entry( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + pydeako_deako_mock: MagicMock, + pydeako_discoverer_mock: MagicMock, +) -> None: + """Test successful setup entry.""" + pydeako_deako_mock.return_value.get_devices.return_value = { + "id1": {}, + "id2": {}, + } + + mock_config_entry.add_to_hass(hass) + + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + pydeako_deako_mock.assert_called_once_with( + pydeako_discoverer_mock.return_value.get_address + ) + pydeako_deako_mock.return_value.connect.assert_called_once() + pydeako_deako_mock.return_value.find_devices.assert_called_once() + pydeako_deako_mock.return_value.get_devices.assert_called() + + assert mock_config_entry.runtime_data == pydeako_deako_mock.return_value + + +async def test_deako_async_setup_entry_device_list_timeout( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + pydeako_deako_mock: MagicMock, + pydeako_discoverer_mock: MagicMock, +) -> None: + """Test async_setup_entry raises ConfigEntryNotReady when pydeako raises DeviceListTimeout.""" + + mock_config_entry.add_to_hass(hass) + + pydeako_deako_mock.return_value.find_devices.side_effect = DeviceListTimeout() + + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + pydeako_deako_mock.assert_called_once_with( + pydeako_discoverer_mock.return_value.get_address + ) + pydeako_deako_mock.return_value.connect.assert_called_once() + pydeako_deako_mock.return_value.find_devices.assert_called_once() + pydeako_deako_mock.return_value.disconnect.assert_called_once() + + assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY + + +async def test_deako_async_setup_entry_find_devices_timeout( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + pydeako_deako_mock: MagicMock, + pydeako_discoverer_mock: MagicMock, +) -> None: + """Test async_setup_entry raises ConfigEntryNotReady when pydeako raises FindDevicesTimeout.""" + + mock_config_entry.add_to_hass(hass) + + pydeako_deako_mock.return_value.find_devices.side_effect = FindDevicesTimeout() + + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + pydeako_deako_mock.assert_called_once_with( + pydeako_discoverer_mock.return_value.get_address + ) + pydeako_deako_mock.return_value.connect.assert_called_once() + pydeako_deako_mock.return_value.find_devices.assert_called_once() + pydeako_deako_mock.return_value.disconnect.assert_called_once() + + assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY diff --git a/tests/components/deako/test_light.py b/tests/components/deako/test_light.py new file mode 100644 index 00000000000000..b969c7f71cbec7 --- /dev/null +++ b/tests/components/deako/test_light.py @@ -0,0 +1,192 @@ +"""Tests for the light module.""" + +from unittest.mock import MagicMock + +import pytest +from syrupy.assertion import SnapshotAssertion + +from homeassistant.components.light import ATTR_BRIGHTNESS, DOMAIN as LIGHT_DOMAIN +from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_ON +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from tests.common import MockConfigEntry, snapshot_platform + + +async def test_light_setup_with_device( + hass: HomeAssistant, + pydeako_deako_mock: MagicMock, + mock_config_entry: MockConfigEntry, + entity_registry: er.EntityRegistry, + snapshot: SnapshotAssertion, +) -> None: + """Test light platform setup with device returned.""" + mock_config_entry.add_to_hass(hass) + + pydeako_deako_mock.return_value.get_devices.return_value = { + "some_device": {}, + } + pydeako_deako_mock.return_value.get_name.return_value = "some device" + + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) + + +async def test_light_initial_props( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + pydeako_deako_mock: MagicMock, + entity_registry: er.EntityRegistry, + snapshot: SnapshotAssertion, +) -> None: + """Test on/off light is setup with accurate initial properties.""" + mock_config_entry.add_to_hass(hass) + + pydeako_deako_mock.return_value.get_devices.return_value = { + "uuid": { + "name": "kitchen", + } + } + pydeako_deako_mock.return_value.get_name.return_value = "kitchen" + pydeako_deako_mock.return_value.get_state.return_value = { + "power": False, + } + pydeako_deako_mock.return_value.is_dimmable.return_value = False + + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) + + +async def test_dimmable_light_props( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + pydeako_deako_mock: MagicMock, + entity_registry: er.EntityRegistry, + snapshot: SnapshotAssertion, +) -> None: + """Test dimmable on/off light is setup with accurate initial properties.""" + mock_config_entry.add_to_hass(hass) + + pydeako_deako_mock.return_value.get_devices.return_value = { + "uuid": { + "name": "kitchen", + } + } + pydeako_deako_mock.return_value.get_name.return_value = "kitchen" + pydeako_deako_mock.return_value.get_state.return_value = { + "power": True, + "dim": 50, + } + pydeako_deako_mock.return_value.is_dimmable.return_value = True + + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) + + +async def test_light_power_change_on( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + pydeako_deako_mock: MagicMock, +) -> None: + """Test turing on a deako device.""" + mock_config_entry.add_to_hass(hass) + + pydeako_deako_mock.return_value.get_devices.return_value = { + "uuid": { + "name": "kitchen", + } + } + pydeako_deako_mock.return_value.get_name.return_value = "kitchen" + + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: "light.kitchen"}, + blocking=True, + ) + + pydeako_deako_mock.return_value.control_device.assert_called_once_with( + "uuid", True, None + ) + + +async def test_light_power_change_off( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + pydeako_deako_mock: MagicMock, +) -> None: + """Test turing off a deako device.""" + mock_config_entry.add_to_hass(hass) + + pydeako_deako_mock.return_value.get_devices.return_value = { + "uuid": { + "name": "kitchen", + } + } + pydeako_deako_mock.return_value.get_name.return_value = "kitchen" + + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: "light.kitchen"}, + blocking=True, + ) + + pydeako_deako_mock.return_value.control_device.assert_called_once_with( + "uuid", False, None + ) + + +@pytest.mark.parametrize( + ("dim_input", "expected_dim_value"), + [ + (3, 1), + (255, 100), + (127, 50), + ], +) +async def test_light_brightness_change( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + pydeako_deako_mock: MagicMock, + dim_input: int, + expected_dim_value: int, +) -> None: + """Test turing on a deako device.""" + mock_config_entry.add_to_hass(hass) + + pydeako_deako_mock.return_value.get_devices.return_value = { + "uuid": { + "name": "kitchen", + } + } + pydeako_deako_mock.return_value.get_name.return_value = "kitchen" + + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_ON, + { + ATTR_ENTITY_ID: "light.kitchen", + ATTR_BRIGHTNESS: dim_input, + }, + blocking=True, + ) + + pydeako_deako_mock.return_value.control_device.assert_called_once_with( + "uuid", True, expected_dim_value + ) diff --git a/tests/components/deconz/snapshots/test_select.ambr b/tests/components/deconz/snapshots/test_select.ambr index 12966709947f47..997eab0901f104 100644 --- a/tests/components/deconz/snapshots/test_select.ambr +++ b/tests/components/deconz/snapshots/test_select.ambr @@ -506,3 +506,68 @@ 'state': 'medium', }) # --- +# name: test_select[sensor_payload3-expected3][select.ikea_starkvind_fan_mode-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'options': list([ + 'off', + 'auto', + 'speed_1', + 'speed_2', + 'speed_3', + 'speed_4', + 'speed_5', + ]), + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'select', + 'entity_category': , + 'entity_id': 'select.ikea_starkvind_fan_mode', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'IKEA Starkvind Fan Mode', + 'platform': 'deconz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '0c:43:14:ff:fe:6c:20:12-01-fc7d-fan_mode', + 'unit_of_measurement': None, + }) +# --- +# name: test_select[sensor_payload3-expected3][select.ikea_starkvind_fan_mode-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'IKEA Starkvind Fan Mode', + 'options': list([ + 'off', + 'auto', + 'speed_1', + 'speed_2', + 'speed_3', + 'speed_4', + 'speed_5', + ]), + }), + 'context': , + 'entity_id': 'select.ikea_starkvind_fan_mode', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'speed_1', + }) +# --- diff --git a/tests/components/deconz/test_config_flow.py b/tests/components/deconz/test_config_flow.py index 4971196240762e..8555a6e333beb1 100644 --- a/tests/components/deconz/test_config_flow.py +++ b/tests/components/deconz/test_config_flow.py @@ -22,12 +22,7 @@ ) from homeassistant.components.hassio import HassioServiceInfo from homeassistant.components.ssdp import ATTR_UPNP_MANUFACTURER_URL, ATTR_UPNP_SERIAL -from homeassistant.config_entries import ( - SOURCE_HASSIO, - SOURCE_REAUTH, - SOURCE_SSDP, - SOURCE_USER, -) +from homeassistant.config_entries import SOURCE_HASSIO, SOURCE_SSDP, SOURCE_USER from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_PORT, CONTENT_TYPE_JSON from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -407,12 +402,7 @@ async def test_reauth_flow_update_configuration( config_entry_setup: MockConfigEntry, ) -> None: """Verify reauth flow can update gateway API key.""" - result = await hass.config_entries.flow.async_init( - DECONZ_DOMAIN, - data=config_entry_setup.data, - context={"source": SOURCE_REAUTH}, - ) - + result = await config_entry_setup.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "link" diff --git a/tests/components/deconz/test_select.py b/tests/components/deconz/test_select.py index 900283d88bbe64..c677853841c4c0 100644 --- a/tests/components/deconz/test_select.py +++ b/tests/components/deconz/test_select.py @@ -4,6 +4,7 @@ from typing import Any from unittest.mock import patch +from pydeconz.models.sensor.air_purifier import AirPurifierFanMode from pydeconz.models.sensor.presence import ( PresenceConfigDeviceMode, PresenceConfigTriggerDistance, @@ -119,6 +120,42 @@ "request_data": {"triggerdistance": "far"}, }, ), + ( # Air Purifier Fan Mode + { + "config": { + "filterlifetime": 259200, + "ledindication": True, + "locked": False, + "mode": "speed_1", + "on": True, + "reachable": True, + }, + "ep": 1, + "etag": "de26d19d9e91b2db3ded6ee7ab6b6a4b", + "lastannounced": None, + "lastseen": "2024-08-07T18:27Z", + "manufacturername": "IKEA of Sweden", + "modelid": "STARKVIND Air purifier", + "name": "IKEA Starkvind", + "productid": "E2007", + "state": { + "deviceruntime": 73405, + "filterruntime": 73405, + "lastupdated": "2024-08-07T18:27:52.543", + "replacefilter": False, + "speed": 20, + }, + "swversion": "1.1.001", + "type": "ZHAAirPurifier", + "uniqueid": "0c:43:14:ff:fe:6c:20:12-01-fc7d", + }, + { + "entity_id": "select.ikea_starkvind_fan_mode", + "option": AirPurifierFanMode.AUTO.value, + "request": "/sensors/0/config", + "request_data": {"mode": "auto"}, + }, + ), ] diff --git a/tests/components/deluge/test_config_flow.py b/tests/components/deluge/test_config_flow.py index 37229d4a72e4ad..c336fc81cc638f 100644 --- a/tests/components/deluge/test_config_flow.py +++ b/tests/components/deluge/test_config_flow.py @@ -5,7 +5,7 @@ import pytest from homeassistant.components.deluge.const import DEFAULT_NAME, DOMAIN -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_USER from homeassistant.const import CONF_SOURCE from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -113,16 +113,7 @@ async def test_flow_reauth(hass: HomeAssistant, api) -> None: entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - CONF_SOURCE: SOURCE_REAUTH, - "entry_id": entry.entry_id, - "unique_id": entry.unique_id, - }, - data=CONF_DATA, - ) - + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "user" diff --git a/tests/components/devolo_home_control/test_config_flow.py b/tests/components/devolo_home_control/test_config_flow.py index 48f9bf31f4f961..7c9bfdeff63b7d 100644 --- a/tests/components/devolo_home_control/test_config_flow.py +++ b/tests/components/devolo_home_control/test_config_flow.py @@ -164,21 +164,17 @@ async def test_zeroconf_wrong_device(hass: HomeAssistant) -> None: async def test_form_reauth(hass: HomeAssistant) -> None: """Test that the reauth confirmation form is served.""" - mock_config = MockConfigEntry(domain=DOMAIN, unique_id="123456", data={}) - mock_config.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": mock_config.entry_id, - }, + mock_config = MockConfigEntry( + domain=DOMAIN, + unique_id="123456", data={ "username": "test-username", "password": "test-password", "mydevolo_url": "https://test_mydevolo_url.test", }, ) - + mock_config.add_to_hass(hass) + result = await mock_config.start_reauth_flow(hass) assert result["step_id"] == "reauth_confirm" assert result["type"] is FlowResultType.FORM @@ -205,20 +201,17 @@ async def test_form_reauth(hass: HomeAssistant) -> None: @pytest.mark.parametrize("credentials_valid", [False]) async def test_form_invalid_credentials_reauth(hass: HomeAssistant) -> None: """Test if we get the error message on invalid credentials.""" - mock_config = MockConfigEntry(domain=DOMAIN, unique_id="123456", data={}) - mock_config.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": mock_config.entry_id, - }, + mock_config = MockConfigEntry( + domain=DOMAIN, + unique_id="123456", data={ "username": "test-username", "password": "test-password", "mydevolo_url": "https://test_mydevolo_url.test", }, ) + mock_config.add_to_hass(hass) + result = await mock_config.start_reauth_flow(hass) result = await hass.config_entries.flow.async_configure( result["flow_id"], @@ -230,20 +223,17 @@ async def test_form_invalid_credentials_reauth(hass: HomeAssistant) -> None: async def test_form_uuid_change_reauth(hass: HomeAssistant) -> None: """Test that the reauth confirmation form is served.""" - mock_config = MockConfigEntry(domain=DOMAIN, unique_id="123456", data={}) - mock_config.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": mock_config.entry_id, - }, + mock_config = MockConfigEntry( + domain=DOMAIN, + unique_id="123456", data={ "username": "test-username", "password": "test-password", "mydevolo_url": "https://test_mydevolo_url.test", }, ) + mock_config.add_to_hass(hass) + result = await mock_config.start_reauth_flow(hass) assert result["step_id"] == "reauth_confirm" assert result["type"] is FlowResultType.FORM diff --git a/tests/components/devolo_home_network/test_config_flow.py b/tests/components/devolo_home_network/test_config_flow.py index 5aa2bfa274e570..5234d0f073e997 100644 --- a/tests/components/devolo_home_network/test_config_flow.py +++ b/tests/components/devolo_home_network/test_config_flow.py @@ -179,18 +179,7 @@ async def test_form_reauth(hass: HomeAssistant) -> None: entry = configure_integration(hass) await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - "title_placeholders": { - CONF_NAME: DISCOVERY_INFO.hostname.split(".")[0], - }, - }, - data=entry.data, - ) - + result = await entry.start_reauth_flow(hass) assert result["step_id"] == "reauth_confirm" assert result["type"] is FlowResultType.FORM diff --git a/tests/components/discord/__init__.py b/tests/components/discord/__init__.py index bf7c188b7b5996..1d81388d1e389d 100644 --- a/tests/components/discord/__init__.py +++ b/tests/components/discord/__init__.py @@ -5,7 +5,6 @@ import nextcord from homeassistant.components.discord.const import DOMAIN -from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_API_TOKEN, CONF_NAME from homeassistant.core import HomeAssistant @@ -22,7 +21,7 @@ } -def create_entry(hass: HomeAssistant) -> ConfigEntry: +def create_entry(hass: HomeAssistant) -> MockConfigEntry: """Add config entry in Home Assistant.""" entry = MockConfigEntry( domain=DOMAIN, diff --git a/tests/components/discord/test_config_flow.py b/tests/components/discord/test_config_flow.py index 9b37179e86db37..e9a1344c5553d1 100644 --- a/tests/components/discord/test_config_flow.py +++ b/tests/components/discord/test_config_flow.py @@ -4,7 +4,7 @@ from homeassistant import config_entries from homeassistant.components.discord.const import DOMAIN -from homeassistant.const import CONF_API_TOKEN, CONF_SOURCE +from homeassistant.const import CONF_API_TOKEN from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -123,16 +123,7 @@ async def test_flow_user_unknown_error(hass: HomeAssistant) -> None: async def test_flow_reauth(hass: HomeAssistant) -> None: """Test a reauth flow.""" entry = create_entry(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - CONF_SOURCE: config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - "unique_id": entry.unique_id, - }, - data=entry.data, - ) - + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/discovergy/test_config_flow.py b/tests/components/discovergy/test_config_flow.py index 2464ba3846f8ba..470ef65fccdd77 100644 --- a/tests/components/discovergy/test_config_flow.py +++ b/tests/components/discovergy/test_config_flow.py @@ -6,7 +6,7 @@ import pytest from homeassistant.components.discovergy.const import DOMAIN -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_USER from homeassistant.const import CONF_EMAIL, CONF_PASSWORD from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -49,15 +49,9 @@ async def test_reauth( ) -> None: """Test reauth flow.""" config_entry.add_to_hass(hass) - - init_result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH, "unique_id": config_entry.unique_id}, - data=None, - ) - + init_result = await config_entry.start_reauth_flow(hass) assert init_result["type"] is FlowResultType.FORM - assert init_result["step_id"] == "reauth" + assert init_result["step_id"] == "reauth_confirm" with patch( "homeassistant.components.discovergy.async_setup_entry", diff --git a/tests/components/dormakaba_dkey/test_config_flow.py b/tests/components/dormakaba_dkey/test_config_flow.py index 499e5844949705..8d8140d609a018 100644 --- a/tests/components/dormakaba_dkey/test_config_flow.py +++ b/tests/components/dormakaba_dkey/test_config_flow.py @@ -310,11 +310,7 @@ async def test_reauth(hass: HomeAssistant) -> None: data={"address": DKEY_DISCOVERY_INFO.address}, ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_REAUTH, "entry_id": entry.entry_id}, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/dremel_3d_printer/conftest.py b/tests/components/dremel_3d_printer/conftest.py index 6490b844dc08e2..cc70537db3d7d0 100644 --- a/tests/components/dremel_3d_printer/conftest.py +++ b/tests/components/dremel_3d_printer/conftest.py @@ -34,7 +34,7 @@ def connection() -> None: """Mock Dremel 3D Printer connection.""" with requests_mock.Mocker() as mock: mock.post( - f"http://{HOST}:80/command", + f"http://{HOST}/command", response_list=[ {"text": load_fixture("dremel_3d_printer/command_1.json")}, {"text": load_fixture("dremel_3d_printer/command_2.json")}, diff --git a/tests/components/drop_connect/common.py b/tests/components/drop_connect/common.py index bdba79bbd95d07..9eb76f57dad8c6 100644 --- a/tests/components/drop_connect/common.py +++ b/tests/components/drop_connect/common.py @@ -34,6 +34,10 @@ TEST_DATA_SALT = '{"salt":1}' TEST_DATA_SALT_RESET = '{"salt":0}' +TEST_DATA_ALERT_TOPIC = "drop_connect/DROP-1_C0FFEE/81" +TEST_DATA_ALERT = '{"battery":100,"sens":1,"pwrOff":0,"temp":68.2}' +TEST_DATA_ALERT_RESET = '{"battery":0,"sens":0,"pwrOff":1,"temp":0}' + TEST_DATA_LEAK_TOPIC = "drop_connect/DROP-1_C0FFEE/20" TEST_DATA_LEAK = '{"battery":100,"leak":1,"temp":68.2}' TEST_DATA_LEAK_RESET = '{"battery":0,"leak":0,"temp":0}' @@ -109,6 +113,25 @@ def config_entry_salt() -> ConfigEntry: ) +def config_entry_alert() -> ConfigEntry: + """Config entry version 1 fixture.""" + return MockConfigEntry( + domain=DOMAIN, + unique_id="DROP-1_C0FFEE_81", + data={ + CONF_COMMAND_TOPIC: "drop_connect/DROP-1_C0FFEE/81/cmd", + CONF_DATA_TOPIC: "drop_connect/DROP-1_C0FFEE/81/#", + CONF_DEVICE_DESC: "Alert", + CONF_DEVICE_ID: 81, + CONF_DEVICE_NAME: "Alert", + CONF_DEVICE_TYPE: "alrt", + CONF_HUB_ID: "DROP-1_C0FFEE", + CONF_DEVICE_OWNER_ID: "DROP-1_C0FFEE_255", + }, + version=1, + ) + + def config_entry_leak() -> ConfigEntry: """Config entry version 1 fixture.""" return MockConfigEntry( diff --git a/tests/components/drop_connect/snapshots/test_binary_sensor.ambr b/tests/components/drop_connect/snapshots/test_binary_sensor.ambr index c42cdb8cde1b07..9b0cc201573591 100644 --- a/tests/components/drop_connect/snapshots/test_binary_sensor.ambr +++ b/tests/components/drop_connect/snapshots/test_binary_sensor.ambr @@ -1,4 +1,98 @@ # serializer version: 1 +# name: test_sensors[alert][binary_sensor.alert_power-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': None, + 'entity_id': 'binary_sensor.alert_power', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Power', + 'platform': 'drop_connect', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': 'DROP-1_C0FFEE_81_power', + 'unit_of_measurement': None, + }) +# --- +# name: test_sensors[alert][binary_sensor.alert_power-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'power', + 'friendly_name': 'Alert Power', + }), + 'context': , + 'entity_id': 'binary_sensor.alert_power', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_sensors[alert][binary_sensor.alert_sensor-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': None, + 'entity_id': 'binary_sensor.alert_sensor', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Sensor', + 'platform': 'drop_connect', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'alert_sensor', + 'unique_id': 'DROP-1_C0FFEE_81_alert_sensor', + 'unit_of_measurement': None, + }) +# --- +# name: test_sensors[alert][binary_sensor.alert_sensor-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'problem', + 'friendly_name': 'Alert Sensor', + }), + 'context': , + 'entity_id': 'binary_sensor.alert_sensor', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- # name: test_sensors[hub][binary_sensor.hub_drop_1_c0ffee_leak_detected-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ diff --git a/tests/components/drop_connect/snapshots/test_sensor.ambr b/tests/components/drop_connect/snapshots/test_sensor.ambr index 54e3259e455d4c..a5c91dbe3e42c2 100644 --- a/tests/components/drop_connect/snapshots/test_sensor.ambr +++ b/tests/components/drop_connect/snapshots/test_sensor.ambr @@ -1,4 +1,68 @@ # serializer version: 1 +# name: test_sensors[alert][sensor.alert_battery-data] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'battery', + 'friendly_name': 'Alert Battery', + 'state_class': , + 'unit_of_measurement': '%', + }), + 'context': , + 'entity_id': 'sensor.alert_battery', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '100', + }) +# --- +# name: test_sensors[alert][sensor.alert_battery-reset] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'battery', + 'friendly_name': 'Alert Battery', + 'state_class': , + 'unit_of_measurement': '%', + }), + 'context': , + 'entity_id': 'sensor.alert_battery', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0', + }) +# --- +# name: test_sensors[alert][sensor.alert_temperature-data] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'temperature', + 'friendly_name': 'Alert Temperature', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.alert_temperature', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '20.1111111111111', + }) +# --- +# name: test_sensors[alert][sensor.alert_temperature-reset] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'temperature', + 'friendly_name': 'Alert Temperature', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.alert_temperature', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '-17.7777777777778', + }) +# --- # name: test_sensors[filter][sensor.filter_battery-data] StateSnapshot({ 'attributes': ReadOnlyDict({ diff --git a/tests/components/drop_connect/test_binary_sensor.py b/tests/components/drop_connect/test_binary_sensor.py index 895921291effd3..ab89e05d809032 100644 --- a/tests/components/drop_connect/test_binary_sensor.py +++ b/tests/components/drop_connect/test_binary_sensor.py @@ -10,6 +10,9 @@ from homeassistant.helpers import entity_registry as er from .common import ( + TEST_DATA_ALERT, + TEST_DATA_ALERT_RESET, + TEST_DATA_ALERT_TOPIC, TEST_DATA_HUB, TEST_DATA_HUB_RESET, TEST_DATA_HUB_TOPIC, @@ -28,6 +31,7 @@ TEST_DATA_SOFTENER, TEST_DATA_SOFTENER_RESET, TEST_DATA_SOFTENER_TOPIC, + config_entry_alert, config_entry_hub, config_entry_leak, config_entry_protection_valve, @@ -44,6 +48,12 @@ ("config_entry", "topic", "reset", "data"), [ (config_entry_hub(), TEST_DATA_HUB_TOPIC, TEST_DATA_HUB_RESET, TEST_DATA_HUB), + ( + config_entry_alert(), + TEST_DATA_ALERT_TOPIC, + TEST_DATA_ALERT_RESET, + TEST_DATA_ALERT, + ), ( config_entry_leak(), TEST_DATA_LEAK_TOPIC, @@ -77,6 +87,7 @@ ], ids=[ "hub", + "alert", "leak", "softener", "protection_valve", diff --git a/tests/components/drop_connect/test_sensor.py b/tests/components/drop_connect/test_sensor.py index cb56522a09d92e..c33f0aefe37b2c 100644 --- a/tests/components/drop_connect/test_sensor.py +++ b/tests/components/drop_connect/test_sensor.py @@ -11,6 +11,9 @@ from homeassistant.helpers import entity_registry as er from .common import ( + TEST_DATA_ALERT, + TEST_DATA_ALERT_RESET, + TEST_DATA_ALERT_TOPIC, TEST_DATA_FILTER, TEST_DATA_FILTER_RESET, TEST_DATA_FILTER_TOPIC, @@ -32,6 +35,7 @@ TEST_DATA_SOFTENER, TEST_DATA_SOFTENER_RESET, TEST_DATA_SOFTENER_TOPIC, + config_entry_alert, config_entry_filter, config_entry_hub, config_entry_leak, @@ -57,6 +61,12 @@ def only_sensor_platform() -> Generator[None]: ("config_entry", "topic", "reset", "data"), [ (config_entry_hub(), TEST_DATA_HUB_TOPIC, TEST_DATA_HUB_RESET, TEST_DATA_HUB), + ( + config_entry_alert(), + TEST_DATA_ALERT_TOPIC, + TEST_DATA_ALERT_RESET, + TEST_DATA_ALERT, + ), ( config_entry_leak(), TEST_DATA_LEAK_TOPIC, @@ -96,6 +106,7 @@ def only_sensor_platform() -> Generator[None]: ], ids=[ "hub", + "alert", "leak", "softener", "filter", diff --git a/tests/components/efergy/test_config_flow.py b/tests/components/efergy/test_config_flow.py index 9a66c42bc9a1fe..8b77bbdc7ab31a 100644 --- a/tests/components/efergy/test_config_flow.py +++ b/tests/components/efergy/test_config_flow.py @@ -5,7 +5,7 @@ from pyefergy import exceptions from homeassistant.components.efergy.const import DEFAULT_NAME, DOMAIN -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_USER from homeassistant.const import CONF_API_KEY, CONF_SOURCE from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -76,20 +76,11 @@ async def test_flow_user_unknown(hass: HomeAssistant) -> None: async def test_flow_reauth(hass: HomeAssistant) -> None: """Test reauth step.""" entry = create_entry(hass) - with _patch_efergy(), _patch_setup(): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - CONF_SOURCE: SOURCE_REAUTH, - "entry_id": entry.entry_id, - "unique_id": entry.unique_id, - }, - data=CONF_DATA, - ) - - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "user" + result = await entry.start_reauth_flow(hass) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + with _patch_efergy(), _patch_setup(): new_conf = {CONF_API_KEY: "1234567890"} result = await hass.config_entries.flow.async_configure( result["flow_id"], diff --git a/tests/components/electric_kiwi/test_config_flow.py b/tests/components/electric_kiwi/test_config_flow.py index bf248aafb13267..d23e70422dd4d6 100644 --- a/tests/components/electric_kiwi/test_config_flow.py +++ b/tests/components/electric_kiwi/test_config_flow.py @@ -18,7 +18,6 @@ OAUTH2_TOKEN, SCOPE_VALUES, ) -from homeassistant.config_entries import SOURCE_REAUTH from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType from homeassistant.helpers import config_entry_oauth2_flow @@ -160,16 +159,11 @@ async def test_reauthentication( setup_credentials: None, ) -> None: """Test Electric Kiwi reauthentication.""" + result = await config_entry.start_reauth_flow(hass) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reauth_confirm" - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_REAUTH, "entry_id": DOMAIN} - ) - - flows = hass.config_entries.flow.async_progress() - assert len(flows) == 1 - assert "flow_id" in flows[0] - - result = await hass.config_entries.flow.async_configure(flows[0]["flow_id"], {}) + result = await hass.config_entries.flow.async_configure(result["flow_id"], {}) state = config_entry_oauth2_flow._encode_jwt( hass, diff --git a/tests/components/elmax/test_config_flow.py b/tests/components/elmax/test_config_flow.py index 85e14dd0a3ffb8..7a4d9755fa5409 100644 --- a/tests/components/elmax/test_config_flow.py +++ b/tests/components/elmax/test_config_flow.py @@ -21,7 +21,6 @@ CONF_ELMAX_USERNAME, DOMAIN, ) -from homeassistant.config_entries import SOURCE_REAUTH from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -544,20 +543,7 @@ async def test_show_reauth(hass: HomeAssistant) -> None: ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": entry.unique_id, - "entry_id": entry.entry_id, - }, - data={ - CONF_ELMAX_PANEL_ID: MOCK_PANEL_ID, - CONF_ELMAX_PANEL_PIN: MOCK_PANEL_PIN, - CONF_ELMAX_USERNAME: MOCK_USERNAME, - CONF_ELMAX_PASSWORD: MOCK_PASSWORD, - }, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -577,24 +563,11 @@ async def test_reauth_flow(hass: HomeAssistant) -> None: entry.add_to_hass(hass) # Trigger reauth + reauth_result = await entry.start_reauth_flow(hass) with patch( "homeassistant.components.elmax.async_setup_entry", return_value=True, ): - reauth_result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": entry.unique_id, - "entry_id": entry.entry_id, - }, - data={ - CONF_ELMAX_PANEL_ID: MOCK_PANEL_ID, - CONF_ELMAX_PANEL_PIN: MOCK_PANEL_PIN, - CONF_ELMAX_USERNAME: MOCK_USERNAME, - CONF_ELMAX_PASSWORD: MOCK_PASSWORD, - }, - ) result = await hass.config_entries.flow.async_configure( reauth_result["flow_id"], { @@ -624,24 +597,11 @@ async def test_reauth_panel_disappeared(hass: HomeAssistant) -> None: entry.add_to_hass(hass) # Trigger reauth + reauth_result = await entry.start_reauth_flow(hass) with patch( "elmax_api.http.Elmax.list_control_panels", return_value=[], ): - reauth_result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": entry.unique_id, - "entry_id": entry.entry_id, - }, - data={ - CONF_ELMAX_PANEL_ID: MOCK_PANEL_ID, - CONF_ELMAX_PANEL_PIN: MOCK_PANEL_PIN, - CONF_ELMAX_USERNAME: MOCK_USERNAME, - CONF_ELMAX_PASSWORD: MOCK_PASSWORD, - }, - ) result = await hass.config_entries.flow.async_configure( reauth_result["flow_id"], { @@ -670,24 +630,11 @@ async def test_reauth_invalid_pin(hass: HomeAssistant) -> None: entry.add_to_hass(hass) # Trigger reauth + reauth_result = await entry.start_reauth_flow(hass) with patch( "elmax_api.http.Elmax.get_panel_status", side_effect=ElmaxBadPinError(), ): - reauth_result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": entry.unique_id, - "entry_id": entry.entry_id, - }, - data={ - CONF_ELMAX_PANEL_ID: MOCK_PANEL_ID, - CONF_ELMAX_PANEL_PIN: MOCK_PANEL_PIN, - CONF_ELMAX_USERNAME: MOCK_USERNAME, - CONF_ELMAX_PASSWORD: MOCK_PASSWORD, - }, - ) result = await hass.config_entries.flow.async_configure( reauth_result["flow_id"], { @@ -716,24 +663,11 @@ async def test_reauth_bad_login(hass: HomeAssistant) -> None: entry.add_to_hass(hass) # Trigger reauth + reauth_result = await entry.start_reauth_flow(hass) with patch( "elmax_api.http.Elmax.login", side_effect=ElmaxBadLoginError(), ): - reauth_result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": entry.unique_id, - "entry_id": entry.entry_id, - }, - data={ - CONF_ELMAX_PANEL_ID: MOCK_PANEL_ID, - CONF_ELMAX_PANEL_PIN: MOCK_PANEL_PIN, - CONF_ELMAX_USERNAME: MOCK_USERNAME, - CONF_ELMAX_PASSWORD: MOCK_PASSWORD, - }, - ) result = await hass.config_entries.flow.async_configure( reauth_result["flow_id"], { diff --git a/tests/components/emoncms/__init__.py b/tests/components/emoncms/__init__.py index ecf3c54e9ed47d..59dc4fa08e1be7 100644 --- a/tests/components/emoncms/__init__.py +++ b/tests/components/emoncms/__init__.py @@ -1 +1,12 @@ """Tests for the emoncms component.""" + +from homeassistant.core import HomeAssistant + +from tests.common import MockConfigEntry + + +async def setup_integration(hass: HomeAssistant, entry: MockConfigEntry) -> None: + """Set up the integration.""" + entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() diff --git a/tests/components/emoncms/conftest.py b/tests/components/emoncms/conftest.py index 500fff228e91aa..29e86f3c59d643 100644 --- a/tests/components/emoncms/conftest.py +++ b/tests/components/emoncms/conftest.py @@ -1,10 +1,23 @@ """Fixtures for emoncms integration tests.""" -from collections.abc import AsyncGenerator +from collections.abc import AsyncGenerator, Generator +import copy from unittest.mock import AsyncMock, patch import pytest +from homeassistant.components.emoncms.const import CONF_ONLY_INCLUDE_FEEDID, DOMAIN +from homeassistant.const import ( + CONF_API_KEY, + CONF_ID, + CONF_PLATFORM, + CONF_URL, + CONF_VALUE_TEMPLATE, +) +from homeassistant.helpers.typing import ConfigType + +from tests.common import MockConfigEntry + UNITS = ["kWh", "Wh", "W", "V", "A", "VA", "°C", "°F", "K", "Hz", "hPa", ""] @@ -29,16 +42,102 @@ def get_feed( EMONCMS_FAILURE = {"success": False, "message": "failure"} +FLOW_RESULT = { + CONF_API_KEY: "my_api_key", + CONF_ONLY_INCLUDE_FEEDID: [str(i + 1) for i in range(len(UNITS))], + CONF_URL: "http://1.1.1.1", +} + +SENSOR_NAME = "emoncms@1.1.1.1" + +YAML_BASE = { + CONF_PLATFORM: "emoncms", + CONF_API_KEY: "my_api_key", + CONF_ID: 1, + CONF_URL: "http://1.1.1.1", +} + +YAML = { + **YAML_BASE, + CONF_ONLY_INCLUDE_FEEDID: [1], +} + + +@pytest.fixture +def emoncms_yaml_config() -> ConfigType: + """Mock emoncms yaml configuration.""" + return {"sensor": YAML} + + +@pytest.fixture +def emoncms_yaml_config_with_template() -> ConfigType: + """Mock emoncms yaml conf with template parameter.""" + return {"sensor": {**YAML, CONF_VALUE_TEMPLATE: "{{ value | float + 1500 }}"}} + + +@pytest.fixture +def emoncms_yaml_config_no_include_only_feed_id() -> ConfigType: + """Mock emoncms yaml configuration without include_only_feed_id parameter.""" + return {"sensor": YAML_BASE} + + +@pytest.fixture +def config_entry() -> MockConfigEntry: + """Mock emoncms config entry.""" + return MockConfigEntry( + domain=DOMAIN, + title=SENSOR_NAME, + data=FLOW_RESULT, + ) + + +FLOW_RESULT_NO_FEED = copy.deepcopy(FLOW_RESULT) +FLOW_RESULT_NO_FEED[CONF_ONLY_INCLUDE_FEEDID] = None + + +@pytest.fixture +def config_no_feed() -> MockConfigEntry: + """Mock emoncms config entry with no feed selected.""" + return MockConfigEntry( + domain=DOMAIN, + title=SENSOR_NAME, + data=FLOW_RESULT_NO_FEED, + ) + + +FLOW_RESULT_SINGLE_FEED = copy.deepcopy(FLOW_RESULT) +FLOW_RESULT_SINGLE_FEED[CONF_ONLY_INCLUDE_FEEDID] = ["1"] + + +@pytest.fixture +def config_single_feed() -> MockConfigEntry: + """Mock emoncms config entry with a single feed exposed.""" + return MockConfigEntry( + domain=DOMAIN, + title=SENSOR_NAME, + data=FLOW_RESULT_SINGLE_FEED, + entry_id="XXXXXXXX", + ) + + +@pytest.fixture +def mock_setup_entry() -> Generator[AsyncMock]: + """Override async_setup_entry.""" + with patch( + "homeassistant.components.emoncms.async_setup_entry", return_value=True + ) as mock_setup_entry: + yield mock_setup_entry + @pytest.fixture async def emoncms_client() -> AsyncGenerator[AsyncMock]: """Mock pyemoncms success response.""" with ( patch( - "homeassistant.components.emoncms.sensor.EmoncmsClient", autospec=True + "homeassistant.components.emoncms.EmoncmsClient", autospec=True ) as mock_client, patch( - "homeassistant.components.emoncms.coordinator.EmoncmsClient", + "homeassistant.components.emoncms.config_flow.EmoncmsClient", new=mock_client, ), ): diff --git a/tests/components/emoncms/snapshots/test_sensor.ambr b/tests/components/emoncms/snapshots/test_sensor.ambr index 62c85aaba0142c..5e718c1d8e8407 100644 --- a/tests/components/emoncms/snapshots/test_sensor.ambr +++ b/tests/components/emoncms/snapshots/test_sensor.ambr @@ -1,5 +1,40 @@ # serializer version: 1 -# name: test_coordinator_update[sensor.emoncms_parameter_1] +# name: test_coordinator_update[sensor.emoncms_1_1_1_1_parameter_1-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.emoncms_1_1_1_1_parameter_1', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'emoncms@1.1.1.1 parameter 1', + 'platform': 'emoncms', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': 'XXXXXXXX-1', + 'unit_of_measurement': , + }) +# --- +# name: test_coordinator_update[sensor.emoncms_1_1_1_1_parameter_1-state] StateSnapshot({ 'attributes': ReadOnlyDict({ 'FeedId': '1', @@ -10,12 +45,12 @@ 'Tag': 'tag', 'UserId': '1', 'device_class': 'temperature', - 'friendly_name': 'EmonCMS parameter 1', + 'friendly_name': 'emoncms@1.1.1.1 parameter 1', 'state_class': , 'unit_of_measurement': , }), 'context': , - 'entity_id': 'sensor.emoncms_parameter_1', + 'entity_id': 'sensor.emoncms_1_1_1_1_parameter_1', 'last_changed': , 'last_reported': , 'last_updated': , diff --git a/tests/components/emoncms/test_config_flow.py b/tests/components/emoncms/test_config_flow.py new file mode 100644 index 00000000000000..17ec32a9008d87 --- /dev/null +++ b/tests/components/emoncms/test_config_flow.py @@ -0,0 +1,143 @@ +"""Test emoncms config flow.""" + +from unittest.mock import AsyncMock + +from homeassistant.components.emoncms.const import CONF_ONLY_INCLUDE_FEEDID, DOMAIN +from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER +from homeassistant.const import CONF_API_KEY, CONF_URL +from homeassistant.core import HomeAssistant +from homeassistant.data_entry_flow import FlowResultType + +from . import setup_integration +from .conftest import EMONCMS_FAILURE, FLOW_RESULT_SINGLE_FEED, SENSOR_NAME, YAML + +from tests.common import MockConfigEntry + + +async def test_flow_import_include_feeds( + hass: HomeAssistant, + mock_setup_entry: AsyncMock, + emoncms_client: AsyncMock, +) -> None: + """YAML import with included feed - success test.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_IMPORT}, + data=YAML, + ) + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["title"] == SENSOR_NAME + assert result["data"] == FLOW_RESULT_SINGLE_FEED + + +async def test_flow_import_failure( + hass: HomeAssistant, + emoncms_client: AsyncMock, +) -> None: + """YAML import - failure test.""" + emoncms_client.async_request.return_value = EMONCMS_FAILURE + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_IMPORT}, + data=YAML, + ) + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == EMONCMS_FAILURE["message"] + + +async def test_flow_import_already_configured( + hass: HomeAssistant, + config_entry: MockConfigEntry, + emoncms_client: AsyncMock, +) -> None: + """Test we abort import data set when entry is already configured.""" + config_entry.add_to_hass(hass) + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_IMPORT}, + data=YAML, + ) + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "already_configured" + + +USER_INPUT = { + CONF_URL: "http://1.1.1.1", + CONF_API_KEY: "my_api_key", +} + + +async def test_user_flow( + hass: HomeAssistant, + mock_setup_entry: AsyncMock, + emoncms_client: AsyncMock, +) -> None: + """Test we get the user form.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {} + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + USER_INPUT, + ) + + assert result["type"] is FlowResultType.FORM + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {CONF_ONLY_INCLUDE_FEEDID: ["1"]}, + ) + + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["title"] == SENSOR_NAME + assert result["data"] == {**USER_INPUT, CONF_ONLY_INCLUDE_FEEDID: ["1"]} + assert len(mock_setup_entry.mock_calls) == 1 + + +USER_OPTIONS = { + CONF_ONLY_INCLUDE_FEEDID: ["1"], +} + +CONFIG_ENTRY = { + CONF_API_KEY: "my_api_key", + CONF_ONLY_INCLUDE_FEEDID: ["1"], + CONF_URL: "http://1.1.1.1", +} + + +async def test_options_flow( + hass: HomeAssistant, + mock_setup_entry: AsyncMock, + emoncms_client: AsyncMock, + config_entry: MockConfigEntry, +) -> None: + """Options flow - success test.""" + await setup_integration(hass, config_entry) + result = await hass.config_entries.options.async_init(config_entry.entry_id) + await hass.async_block_till_done() + result = await hass.config_entries.options.async_configure( + result["flow_id"], + user_input=USER_OPTIONS, + ) + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["data"] == CONFIG_ENTRY + assert config_entry.options == CONFIG_ENTRY + + +async def test_options_flow_failure( + hass: HomeAssistant, + mock_setup_entry: AsyncMock, + emoncms_client: AsyncMock, + config_entry: MockConfigEntry, +) -> None: + """Options flow - test failure.""" + emoncms_client.async_request.return_value = EMONCMS_FAILURE + await setup_integration(hass, config_entry) + result = await hass.config_entries.options.async_init(config_entry.entry_id) + await hass.async_block_till_done() + assert result["errors"]["base"] == "failure" + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "init" diff --git a/tests/components/emoncms/test_init.py b/tests/components/emoncms/test_init.py new file mode 100644 index 00000000000000..b89b6e65a66d89 --- /dev/null +++ b/tests/components/emoncms/test_init.py @@ -0,0 +1,40 @@ +"""Test Emoncms component setup process.""" + +from __future__ import annotations + +from unittest.mock import AsyncMock + +from homeassistant.config_entries import ConfigEntryState +from homeassistant.core import HomeAssistant + +from . import setup_integration +from .conftest import EMONCMS_FAILURE + +from tests.common import MockConfigEntry + + +async def test_load_unload_entry( + hass: HomeAssistant, + config_entry: MockConfigEntry, + emoncms_client: AsyncMock, +) -> None: + """Test load and unload entry.""" + await setup_integration(hass, config_entry) + + assert config_entry.state is ConfigEntryState.LOADED + + await hass.config_entries.async_unload(config_entry.entry_id) + await hass.async_block_till_done() + + assert config_entry.state is ConfigEntryState.NOT_LOADED + + +async def test_failure( + hass: HomeAssistant, + config_entry: MockConfigEntry, + emoncms_client: AsyncMock, +) -> None: + """Test load failure.""" + emoncms_client.async_request.return_value = EMONCMS_FAILURE + config_entry.add_to_hass(hass) + assert not await hass.config_entries.async_setup(config_entry.entry_id) diff --git a/tests/components/emoncms/test_sensor.py b/tests/components/emoncms/test_sensor.py index a039239077e250..a7bc8059287e86 100644 --- a/tests/components/emoncms/test_sensor.py +++ b/tests/components/emoncms/test_sensor.py @@ -1,54 +1,112 @@ """Test emoncms sensor.""" -from typing import Any from unittest.mock import AsyncMock from freezegun.api import FrozenDateTimeFactory import pytest from syrupy.assertion import SnapshotAssertion -from homeassistant.components.emoncms.const import CONF_ONLY_INCLUDE_FEEDID, DOMAIN +from homeassistant.components.emoncms.const import DOMAIN from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN -from homeassistant.const import CONF_API_KEY, CONF_ID, CONF_PLATFORM, CONF_URL -from homeassistant.core import HomeAssistant +from homeassistant.config_entries import ConfigEntryState +from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant +from homeassistant.helpers import entity_registry as er, issue_registry as ir from homeassistant.helpers.typing import ConfigType from homeassistant.setup import async_setup_component -from .conftest import EMONCMS_FAILURE, FEEDS, get_feed +from . import setup_integration +from .conftest import EMONCMS_FAILURE, get_feed -from tests.common import async_fire_time_changed +from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform -YAML = { - CONF_PLATFORM: "emoncms", - CONF_API_KEY: "my_api_key", - CONF_ID: 1, - CONF_URL: "http://1.1.1.1", - CONF_ONLY_INCLUDE_FEEDID: [1, 2], - "scan_interval": 30, -} +async def test_deprecated_yaml( + hass: HomeAssistant, + issue_registry: ir.IssueRegistry, + emoncms_yaml_config: ConfigType, + emoncms_client: AsyncMock, +) -> None: + """Test an issue is created when we import from yaml config.""" + + await async_setup_component(hass, SENSOR_DOMAIN, emoncms_yaml_config) + await hass.async_block_till_done() + + assert issue_registry.async_get_issue( + domain=HOMEASSISTANT_DOMAIN, issue_id=f"deprecated_yaml_{DOMAIN}" + ) + + +async def test_yaml_with_template( + hass: HomeAssistant, + issue_registry: ir.IssueRegistry, + emoncms_yaml_config_with_template: ConfigType, + emoncms_client: AsyncMock, +) -> None: + """Test an issue is created when we import a yaml config with a value_template parameter.""" + + await async_setup_component(hass, SENSOR_DOMAIN, emoncms_yaml_config_with_template) + await hass.async_block_till_done() + + assert issue_registry.async_get_issue( + domain=DOMAIN, issue_id=f"remove_value_template_{DOMAIN}" + ) + + +async def test_yaml_no_include_only_feed_id( + hass: HomeAssistant, + issue_registry: ir.IssueRegistry, + emoncms_yaml_config_no_include_only_feed_id: ConfigType, + emoncms_client: AsyncMock, +) -> None: + """Test an issue is created when we import a yaml config without a include_only_feed_id parameter.""" + + await async_setup_component( + hass, SENSOR_DOMAIN, emoncms_yaml_config_no_include_only_feed_id + ) + await hass.async_block_till_done() -@pytest.fixture -def emoncms_yaml_config() -> ConfigType: - """Mock emoncms configuration from yaml.""" - return {"sensor": YAML} + assert issue_registry.async_get_issue( + domain=DOMAIN, issue_id=f"missing_include_only_feed_id_{DOMAIN}" + ) + + +async def test_no_feed_selected( + hass: HomeAssistant, + config_no_feed: MockConfigEntry, + entity_registry: er.EntityRegistry, + emoncms_client: AsyncMock, +) -> None: + """Test with no feed selected.""" + await setup_integration(hass, config_no_feed) + assert config_no_feed.state is ConfigEntryState.LOADED + entity_entries = er.async_entries_for_config_entry( + entity_registry, config_no_feed.entry_id + ) + assert entity_entries == [] -def get_entity_ids(feeds: list[dict[str, Any]]) -> list[str]: - """Get emoncms entity ids.""" - return [ - f"{SENSOR_DOMAIN}.{DOMAIN}_{feed["name"].replace(' ', '_')}" for feed in feeds - ] +async def test_no_feed_broadcast( + hass: HomeAssistant, + config_entry: MockConfigEntry, + entity_registry: er.EntityRegistry, + emoncms_client: AsyncMock, +) -> None: + """Test with no feed broadcasted.""" + emoncms_client.async_request.return_value = {"success": True, "message": []} + await setup_integration(hass, config_entry) -def get_feeds(nbs: list[int]) -> list[dict[str, Any]]: - """Get feeds.""" - return [feed for feed in FEEDS if feed["id"] in str(nbs)] + assert config_entry.state is ConfigEntryState.LOADED + entity_entries = er.async_entries_for_config_entry( + entity_registry, config_entry.entry_id + ) + assert entity_entries == [] async def test_coordinator_update( hass: HomeAssistant, - emoncms_yaml_config: ConfigType, + config_single_feed: MockConfigEntry, + entity_registry: er.EntityRegistry, snapshot: SnapshotAssertion, emoncms_client: AsyncMock, caplog: pytest.LogCaptureFixture, @@ -59,12 +117,11 @@ async def test_coordinator_update( "success": True, "message": [get_feed(1, unit="°C")], } - await async_setup_component(hass, SENSOR_DOMAIN, emoncms_yaml_config) - await hass.async_block_till_done() - feeds = get_feeds([1]) - for entity_id in get_entity_ids(feeds): - state = hass.states.get(entity_id) - assert state == snapshot(name=entity_id) + await setup_integration(hass, config_single_feed) + + await snapshot_platform( + hass, entity_registry, snapshot, config_single_feed.entry_id + ) async def skip_time() -> None: freezer.tick(60) @@ -78,8 +135,12 @@ async def skip_time() -> None: await skip_time() - for entity_id in get_entity_ids(feeds): - state = hass.states.get(entity_id) + entity_entries = er.async_entries_for_config_entry( + entity_registry, config_single_feed.entry_id + ) + + for entity_entry in entity_entries: + state = hass.states.get(entity_entry.entity_id) assert state.attributes["LastUpdated"] == 1665509670 assert state.state == "24.04" diff --git a/tests/components/enphase_envoy/test_config_flow.py b/tests/components/enphase_envoy/test_config_flow.py index c2cc02fcc7cac5..f61a0054ed9761 100644 --- a/tests/components/enphase_envoy/test_config_flow.py +++ b/tests/components/enphase_envoy/test_config_flow.py @@ -14,7 +14,6 @@ OPTION_DIAGNOSTICS_INCLUDE_FIXTURES_DEFAULT_VALUE, ) from homeassistant.config_entries import ( - SOURCE_REAUTH, SOURCE_RECONFIGURE, SOURCE_USER, SOURCE_ZEROCONF, @@ -636,14 +635,7 @@ async def test_reauth( ) -> None: """Test we reauth auth.""" await setup_integration(hass, config_entry) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": config_entry.unique_id, - "entry_id": config_entry.entry_id, - }, - ) + result = await config_entry.start_reauth_flow(hass) result2 = await hass.config_entries.flow.async_configure( result["flow_id"], { diff --git a/tests/components/epson/test_config_flow.py b/tests/components/epson/test_config_flow.py index d485a4bfdef147..f727185362c5e2 100644 --- a/tests/components/epson/test_config_flow.py +++ b/tests/components/epson/test_config_flow.py @@ -5,7 +5,7 @@ from epson_projector.const import PWR_OFF_STATE from homeassistant import config_entries -from homeassistant.components.epson.const import DOMAIN +from homeassistant.components.epson.const import CONF_CONNECTION_TYPE, DOMAIN, HTTP from homeassistant.const import CONF_HOST, CONF_NAME, STATE_UNAVAILABLE from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -33,6 +33,10 @@ async def test_form(hass: HomeAssistant) -> None: patch( "homeassistant.components.epson.async_setup_entry", return_value=True, + ), + patch( + "homeassistant.components.epson.Projector.close", + return_value=True, ) as mock_setup_entry, ): result2 = await hass.config_entries.flow.async_configure( @@ -43,7 +47,7 @@ async def test_form(hass: HomeAssistant) -> None: assert result2["type"] is FlowResultType.CREATE_ENTRY assert result2["title"] == "test-epson" - assert result2["data"] == {CONF_HOST: "1.1.1.1"} + assert result2["data"] == {CONF_CONNECTION_TYPE: HTTP, CONF_HOST: "1.1.1.1"} assert len(mock_setup_entry.mock_calls) == 1 diff --git a/tests/components/epson/test_init.py b/tests/components/epson/test_init.py new file mode 100644 index 00000000000000..964f9e915ab04d --- /dev/null +++ b/tests/components/epson/test_init.py @@ -0,0 +1,37 @@ +"""Test the epson init.""" + +from unittest.mock import patch + +from homeassistant.components.epson.const import CONF_CONNECTION_TYPE, DOMAIN +from homeassistant.const import CONF_HOST +from homeassistant.core import HomeAssistant + +from tests.common import MockConfigEntry + + +async def test_migrate_entry(hass: HomeAssistant) -> None: + """Test successful migration of entry data from version 1 to 1.2.""" + + mock_entry = MockConfigEntry( + domain=DOMAIN, + title="Epson", + version=1, + minor_version=1, + data={CONF_HOST: "1.1.1.1"}, + entry_id="1cb78c095906279574a0442a1f0003ef", + ) + assert mock_entry.version == 1 + + mock_entry.add_to_hass(hass) + + # Create entity entry to migrate to new unique ID + with patch("homeassistant.components.epson.Projector.get_power"): + await hass.config_entries.async_setup(mock_entry.entry_id) + await hass.async_block_till_done() + + # Check that is now has connection_type + assert mock_entry + assert mock_entry.version == 1 + assert mock_entry.minor_version == 2 + assert mock_entry.data.get(CONF_CONNECTION_TYPE) == "http" + assert mock_entry.data.get(CONF_HOST) == "1.1.1.1" diff --git a/tests/components/epson/test_media_player.py b/tests/components/epson/test_media_player.py index e529746dcd0516..188fdd5b700e67 100644 --- a/tests/components/epson/test_media_player.py +++ b/tests/components/epson/test_media_player.py @@ -5,7 +5,7 @@ from freezegun.api import FrozenDateTimeFactory -from homeassistant.components.epson.const import DOMAIN +from homeassistant.components.epson.const import CONF_CONNECTION_TYPE, DOMAIN, HTTP from homeassistant.const import CONF_HOST from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er @@ -22,7 +22,7 @@ async def test_set_unique_id( entry = MockConfigEntry( domain=DOMAIN, title="Epson", - data={CONF_HOST: "1.1.1.1"}, + data={CONF_CONNECTION_TYPE: HTTP, CONF_HOST: "1.1.1.1"}, entry_id="1cb78c095906279574a0442a1f0003ef", ) entry.add_to_hass(hass) diff --git a/tests/components/esphome/conftest.py b/tests/components/esphome/conftest.py index ea4099560cd9e2..b3966875a318fb 100644 --- a/tests/components/esphome/conftest.py +++ b/tests/components/esphome/conftest.py @@ -205,6 +205,7 @@ def __init__( self.home_assistant_state_subscription_callback: Callable[ [str, str | None], None ] + self.home_assistant_state_request_callback: Callable[[str, str | None], None] self.voice_assistant_handle_start_callback: Callable[ [str, int, VoiceAssistantAudioSettings, str | None], Coroutine[Any, Any, int | None], @@ -268,9 +269,11 @@ async def mock_connect_error(self, exc: Exception) -> None: def set_home_assistant_state_subscription_callback( self, on_state_sub: Callable[[str, str | None], None], + on_state_request: Callable[[str, str | None], None], ) -> None: """Set the state call callback.""" self.home_assistant_state_subscription_callback = on_state_sub + self.home_assistant_state_request_callback = on_state_request def mock_home_assistant_state_subscription( self, entity_id: str, attribute: str | None @@ -278,6 +281,12 @@ def mock_home_assistant_state_subscription( """Mock a state subscription.""" self.home_assistant_state_subscription_callback(entity_id, attribute) + def mock_home_assistant_state_request( + self, entity_id: str, attribute: str | None + ) -> None: + """Mock a state request.""" + self.home_assistant_state_request_callback(entity_id, attribute) + def set_subscribe_voice_assistant_callbacks( self, handle_start: Callable[ @@ -378,9 +387,12 @@ def _subscribe_service_calls( def _subscribe_home_assistant_states( on_state_sub: Callable[[str, str | None], None], + on_state_request: Callable[[str, str | None], None], ) -> None: """Subscribe to home assistant states.""" - mock_device.set_home_assistant_state_subscription_callback(on_state_sub) + mock_device.set_home_assistant_state_subscription_callback( + on_state_sub, on_state_request + ) def _subscribe_voice_assistant( *, diff --git a/tests/components/esphome/test_config_flow.py b/tests/components/esphome/test_config_flow.py index 68af666538071d..2f91921e7f22ac 100644 --- a/tests/components/esphome/test_config_flow.py +++ b/tests/components/esphome/test_config_flow.py @@ -798,14 +798,7 @@ async def test_reauth_initiation(hass: HomeAssistant, mock_client) -> None: ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - "esphome", - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - "unique_id": entry.unique_id, - }, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -821,14 +814,7 @@ async def test_reauth_confirm_valid( ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - "esphome", - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - "unique_id": entry.unique_id, - }, - ) + result = await entry.start_reauth_flow(hass) mock_client.device_info.return_value = DeviceInfo(uses_password=False, name="test") result = await hass.config_entries.flow.async_configure( @@ -875,14 +861,7 @@ async def test_reauth_fixed_via_dashboard( "homeassistant.components.esphome.coordinator.ESPHomeDashboardAPI.get_encryption_key", return_value=VALID_NOISE_PSK, ) as mock_get_encryption_key: - result = await hass.config_entries.flow.async_init( - "esphome", - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - "unique_id": entry.unique_id, - }, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.ABORT, result assert result["reason"] == "reauth_successful" @@ -896,7 +875,7 @@ async def test_reauth_fixed_via_dashboard_add_encryption_remove_password( hass: HomeAssistant, mock_client, mock_dashboard: dict[str, Any], - mock_config_entry, + mock_config_entry: MockConfigEntry, mock_setup_entry: None, ) -> None: """Test reauth fixed automatically via dashboard with password removed.""" @@ -918,14 +897,7 @@ async def test_reauth_fixed_via_dashboard_add_encryption_remove_password( "homeassistant.components.esphome.coordinator.ESPHomeDashboardAPI.get_encryption_key", return_value=VALID_NOISE_PSK, ) as mock_get_encryption_key: - result = await hass.config_entries.flow.async_init( - "esphome", - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": mock_config_entry.entry_id, - "unique_id": mock_config_entry.unique_id, - }, - ) + result = await mock_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.ABORT, result assert result["reason"] == "reauth_successful" @@ -938,21 +910,14 @@ async def test_reauth_fixed_via_dashboard_add_encryption_remove_password( async def test_reauth_fixed_via_remove_password( hass: HomeAssistant, mock_client, - mock_config_entry, + mock_config_entry: MockConfigEntry, mock_dashboard: dict[str, Any], mock_setup_entry: None, ) -> None: """Test reauth fixed automatically by seeing password removed.""" mock_client.device_info.return_value = DeviceInfo(uses_password=False, name="test") - result = await hass.config_entries.flow.async_init( - "esphome", - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": mock_config_entry.entry_id, - "unique_id": mock_config_entry.unique_id, - }, - ) + result = await mock_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.ABORT, result assert result["reason"] == "reauth_successful" @@ -981,14 +946,7 @@ async def test_reauth_fixed_via_dashboard_at_confirm( mock_client.device_info.return_value = DeviceInfo(uses_password=False, name="test") - result = await hass.config_entries.flow.async_init( - "esphome", - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - "unique_id": entry.unique_id, - }, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM, result assert result["step_id"] == "reauth_confirm" @@ -1027,14 +985,7 @@ async def test_reauth_confirm_invalid( ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - "esphome", - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - "unique_id": entry.unique_id, - }, - ) + result = await entry.start_reauth_flow(hass) mock_client.device_info.side_effect = InvalidEncryptionKeyAPIError result = await hass.config_entries.flow.async_configure( @@ -1070,14 +1021,7 @@ async def test_reauth_confirm_invalid_with_unique_id( ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - "esphome", - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - "unique_id": entry.unique_id, - }, - ) + result = await entry.start_reauth_flow(hass) mock_client.device_info.side_effect = InvalidEncryptionKeyAPIError result = await hass.config_entries.flow.async_configure( diff --git a/tests/components/esphome/test_dashboard.py b/tests/components/esphome/test_dashboard.py index da805eb2eee334..1641804e458980 100644 --- a/tests/components/esphome/test_dashboard.py +++ b/tests/components/esphome/test_dashboard.py @@ -6,7 +6,7 @@ from aioesphomeapi import DeviceInfo, InvalidAuthAPIError from homeassistant.components.esphome import CONF_NOISE_PSK, coordinator, dashboard -from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState +from homeassistant.config_entries import ConfigEntryState from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -150,7 +150,7 @@ async def test_new_info_reload_config_entries( async def test_new_dashboard_fix_reauth( - hass: HomeAssistant, mock_client, mock_config_entry, mock_dashboard + hass: HomeAssistant, mock_client, mock_config_entry: MockConfigEntry, mock_dashboard ) -> None: """Test config entries waiting for reauth are triggered.""" mock_client.device_info.side_effect = ( @@ -162,14 +162,7 @@ async def test_new_dashboard_fix_reauth( "homeassistant.components.esphome.coordinator.ESPHomeDashboardAPI.get_encryption_key", return_value=VALID_NOISE_PSK, ) as mock_get_encryption_key: - result = await hass.config_entries.flow.async_init( - "esphome", - context={ - "source": SOURCE_REAUTH, - "entry_id": mock_config_entry.entry_id, - "unique_id": mock_config_entry.unique_id, - }, - ) + result = await mock_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" assert len(mock_get_encryption_key.mock_calls) == 0 diff --git a/tests/components/esphome/test_manager.py b/tests/components/esphome/test_manager.py index 9d2a906466e9bf..a14c83bf265653 100644 --- a/tests/components/esphome/test_manager.py +++ b/tests/components/esphome/test_manager.py @@ -721,6 +721,34 @@ async def test_state_subscription( assert mock_client.send_home_assistant_state.mock_calls == [] +async def test_state_request( + mock_client: APIClient, + hass: HomeAssistant, + mock_esphome_device: Callable[ + [APIClient, list[EntityInfo], list[UserService], list[EntityState]], + Awaitable[MockESPHomeDevice], + ], +) -> None: + """Test ESPHome requests state change.""" + device: MockESPHomeDevice = await mock_esphome_device( + mock_client=mock_client, + entity_info=[], + user_service=[], + states=[], + ) + await hass.async_block_till_done() + hass.states.async_set("binary_sensor.test", "on", {"bool": True, "float": 3.0}) + device.mock_home_assistant_state_request("binary_sensor.test", None) + await hass.async_block_till_done() + assert mock_client.send_home_assistant_state.mock_calls == [ + call("binary_sensor.test", None, "on") + ] + mock_client.send_home_assistant_state.reset_mock() + hass.states.async_set("binary_sensor.test", "off", {"bool": False, "float": 5.0}) + await hass.async_block_till_done() + assert mock_client.send_home_assistant_state.mock_calls == [] + + async def test_debug_logging( mock_client: APIClient, hass: HomeAssistant, diff --git a/tests/components/evohome/conftest.py b/tests/components/evohome/conftest.py index 260330896b79db..82c5cd760249de 100644 --- a/tests/components/evohome/conftest.py +++ b/tests/components/evohome/conftest.py @@ -2,8 +2,10 @@ from __future__ import annotations +from collections.abc import Callable from datetime import datetime, timedelta -from typing import Any, Final +from http import HTTPMethod +from typing import Any from unittest.mock import MagicMock, patch from aiohttp import ClientSession @@ -16,75 +18,112 @@ from homeassistant.setup import async_setup_component from homeassistant.util.json import JsonArrayType, JsonObjectType -from .const import ACCESS_TOKEN, REFRESH_TOKEN +from .const import ACCESS_TOKEN, REFRESH_TOKEN, USERNAME from tests.common import load_json_array_fixture, load_json_object_fixture -TEST_CONFIG: Final = { - CONF_USERNAME: "username", - CONF_PASSWORD: "password", -} - -def user_account_config_fixture() -> JsonObjectType: +def user_account_config_fixture(install: str) -> JsonObjectType: """Load JSON for the config of a user's account.""" - return load_json_object_fixture("user_account.json", DOMAIN) + try: + return load_json_object_fixture(f"{install}/user_account.json", DOMAIN) + except FileNotFoundError: + return load_json_object_fixture("default/user_account.json", DOMAIN) -def user_locations_config_fixture() -> JsonArrayType: +def user_locations_config_fixture(install: str) -> JsonArrayType: """Load JSON for the config of a user's installation (a list of locations).""" - return load_json_array_fixture("user_locations.json", DOMAIN) + return load_json_array_fixture(f"{install}/user_locations.json", DOMAIN) -def location_status_fixture(loc_id: str) -> JsonObjectType: +def location_status_fixture(install: str, loc_id: str | None = None) -> JsonObjectType: """Load JSON for the status of a specific location.""" - return load_json_object_fixture(f"status_{loc_id}.json", DOMAIN) + if loc_id is None: + _install = load_json_array_fixture(f"{install}/user_locations.json", DOMAIN) + loc_id = _install[0]["locationInfo"]["locationId"] # type: ignore[assignment, call-overload, index] + return load_json_object_fixture(f"{install}/status_{loc_id}.json", DOMAIN) -def dhw_schedule_fixture() -> JsonObjectType: +def dhw_schedule_fixture(install: str) -> JsonObjectType: """Load JSON for the schedule of a domesticHotWater zone.""" - return load_json_object_fixture("schedule_dhw.json", DOMAIN) + try: + return load_json_object_fixture(f"{install}/schedule_dhw.json", DOMAIN) + except FileNotFoundError: + return load_json_object_fixture("default/schedule_dhw.json", DOMAIN) -def zone_schedule_fixture() -> JsonObjectType: +def zone_schedule_fixture(install: str) -> JsonObjectType: """Load JSON for the schedule of a temperatureZone zone.""" - return load_json_object_fixture("schedule_zone.json", DOMAIN) + try: + return load_json_object_fixture(f"{install}/schedule_zone.json", DOMAIN) + except FileNotFoundError: + return load_json_object_fixture("default/schedule_zone.json", DOMAIN) + + +def mock_get_factory(install: str) -> Callable: + """Return a get method for a specified installation.""" + + async def mock_get( + self: Broker, url: str, **kwargs: Any + ) -> JsonArrayType | JsonObjectType: + """Return the JSON for a HTTP get of a given URL.""" + + # a proxy for the behaviour of the real web API + if self.refresh_token is None: + self.refresh_token = f"new_{REFRESH_TOKEN}" + + if ( + self.access_token_expires is None + or self.access_token_expires < datetime.now() + ): + self.access_token = f"new_{ACCESS_TOKEN}" + self.access_token_expires = datetime.now() + timedelta(minutes=30) + + # assume a valid GET, and return the JSON for that web API + if url == "userAccount": # userAccount + return user_account_config_fixture(install) + + if url.startswith("location"): + if "installationInfo" in url: # location/installationInfo?userId={id} + return user_locations_config_fixture(install) + if "location" in url: # location/{id}/status + return location_status_fixture(install) + elif "schedule" in url: + if url.startswith("domesticHotWater"): # domesticHotWater/{id}/schedule + return dhw_schedule_fixture(install) + if url.startswith("temperatureZone"): # temperatureZone/{id}/schedule + return zone_schedule_fixture(install) -async def mock_get( - self: Broker, url: str, **kwargs: Any -) -> JsonArrayType | JsonObjectType: - """Return the JSON for a HTTP get of a given URL.""" + pytest.fail(f"Unexpected request: {HTTPMethod.GET} {url}") - # a proxy for the behaviour of the real web API - if self.refresh_token is None: - self.refresh_token = f"new_{REFRESH_TOKEN}" + return mock_get - if self.access_token_expires is None or self.access_token_expires < datetime.now(): - self.access_token = f"new_{ACCESS_TOKEN}" - self.access_token_expires = datetime.now() + timedelta(minutes=30) - # assume a valid GET, and return the JSON for that web API - if url == "userAccount": # userAccount - return user_account_config_fixture() +async def block_request( + self: Broker, method: HTTPMethod, url: str, **kwargs: Any +) -> None: + """Fail if the code attempts any actual I/O via aiohttp.""" - if url.startswith("location"): - if "installationInfo" in url: # location/installationInfo?userId={id} - return user_locations_config_fixture() - if "location" in url: # location/{id}/status - return location_status_fixture("2738909") + pytest.fail(f"Unexpected request: {method} {url}") - elif "schedule" in url: - if url.startswith("domesticHotWater"): # domesticHotWater/{id}/schedule - return dhw_schedule_fixture() - if url.startswith("temperatureZone"): # temperatureZone/{id}/schedule - return zone_schedule_fixture() - pytest.xfail(f"Unexpected URL: {url}") +@pytest.fixture +def evo_config() -> dict[str, str]: + "Return a default/minimal configuration." + return { + CONF_USERNAME: USERNAME, + CONF_PASSWORD: "password", + } -@patch("evohomeasync2.broker.Broker.get", mock_get) -async def setup_evohome(hass: HomeAssistant, test_config: dict[str, str]) -> MagicMock: +@patch("evohomeasync.broker.Broker._make_request", block_request) +@patch("evohomeasync2.broker.Broker._client", block_request) +async def setup_evohome( + hass: HomeAssistant, + test_config: dict[str, str], + install: str = "default", +) -> MagicMock: """Set up the evohome integration and return its client. The class is mocked here to check the client was instantiated with the correct args. @@ -93,6 +132,7 @@ async def setup_evohome(hass: HomeAssistant, test_config: dict[str, str]) -> Mag with ( patch("homeassistant.components.evohome.evo.EvohomeClient") as mock_client, patch("homeassistant.components.evohome.ev1.EvohomeClient", return_value=None), + patch("evohomeasync2.broker.Broker.get", mock_get_factory(install)), ): mock_client.side_effect = EvohomeClient diff --git a/tests/components/evohome/const.py b/tests/components/evohome/const.py index 0b298db533ac69..c25a259e6024ca 100644 --- a/tests/components/evohome/const.py +++ b/tests/components/evohome/const.py @@ -8,3 +8,12 @@ REFRESH_TOKEN: Final = "rf_jg68ZCKYdxEI3fF..." SESSION_ID: Final = "F7181186..." USERNAME: Final = "test_user@gmail.com" + +# The h-numbers refer to issues in HA's core repo +TEST_INSTALLS: Final = ( + "minimal", # evohome (single zone, no DHW) + "default", # evohome (multi-zone, with DHW & ghost zones) + "h032585", # VisionProWifi (no preset_mode for TCS) + "h099625", # RoundThermostat + "system_004", # RoundModulation +) diff --git a/tests/components/evohome/fixtures/schedule_dhw.json b/tests/components/evohome/fixtures/default/schedule_dhw.json similarity index 100% rename from tests/components/evohome/fixtures/schedule_dhw.json rename to tests/components/evohome/fixtures/default/schedule_dhw.json diff --git a/tests/components/evohome/fixtures/schedule_zone.json b/tests/components/evohome/fixtures/default/schedule_zone.json similarity index 100% rename from tests/components/evohome/fixtures/schedule_zone.json rename to tests/components/evohome/fixtures/default/schedule_zone.json diff --git a/tests/components/evohome/fixtures/status_2738909.json b/tests/components/evohome/fixtures/default/status_2738909.json similarity index 100% rename from tests/components/evohome/fixtures/status_2738909.json rename to tests/components/evohome/fixtures/default/status_2738909.json diff --git a/tests/components/evohome/fixtures/user_account.json b/tests/components/evohome/fixtures/default/user_account.json similarity index 100% rename from tests/components/evohome/fixtures/user_account.json rename to tests/components/evohome/fixtures/default/user_account.json diff --git a/tests/components/evohome/fixtures/user_locations.json b/tests/components/evohome/fixtures/default/user_locations.json similarity index 99% rename from tests/components/evohome/fixtures/user_locations.json rename to tests/components/evohome/fixtures/default/user_locations.json index cf59aa9ae8a827..f2f4091a2dccb3 100644 --- a/tests/components/evohome/fixtures/user_locations.json +++ b/tests/components/evohome/fixtures/default/user_locations.json @@ -246,7 +246,7 @@ }, { "zoneId": "3450733", - "modelType": "xx", + "modelType": "xxx", "setpointCapabilities": { "maxHeatSetpoint": 35.0, "minHeatSetpoint": 5.0, @@ -268,7 +268,7 @@ "setpointValueResolution": 0.5 }, "name": "Spare Room", - "zoneType": "xx" + "zoneType": "xxx" } ], "dhw": { diff --git a/tests/components/evohome/fixtures/h032585/status_111111.json b/tests/components/evohome/fixtures/h032585/status_111111.json new file mode 100644 index 00000000000000..0ea535c2461e54 --- /dev/null +++ b/tests/components/evohome/fixtures/h032585/status_111111.json @@ -0,0 +1,31 @@ +{ + "locationId": "111111", + "gateways": [ + { + "gatewayId": "222222", + "temperatureControlSystems": [ + { + "systemId": "416856", + "zones": [ + { + "zoneId": "416856", + "temperatureStatus": { + "temperature": 21.5, + "isAvailable": true + }, + "activeFaults": [], + "setpointStatus": { + "targetHeatTemperature": 21.5, + "setpointMode": "FollowSchedule" + }, + "name": "THERMOSTAT" + } + ], + "activeFaults": [], + "systemModeStatus": { "mode": "Heat", "isPermanent": true } + } + ], + "activeFaults": [] + } + ] +} diff --git a/tests/components/evohome/fixtures/h032585/temperatures.json b/tests/components/evohome/fixtures/h032585/temperatures.json new file mode 100644 index 00000000000000..a2015c94f468ff --- /dev/null +++ b/tests/components/evohome/fixtures/h032585/temperatures.json @@ -0,0 +1,3 @@ +{ + "416856": 21.5 +} diff --git a/tests/components/evohome/fixtures/h032585/user_locations.json b/tests/components/evohome/fixtures/h032585/user_locations.json new file mode 100644 index 00000000000000..b4ea2e5c4203ae --- /dev/null +++ b/tests/components/evohome/fixtures/h032585/user_locations.json @@ -0,0 +1,79 @@ +[ + { + "locationInfo": { + "locationId": "111111", + "name": "My Home", + "timeZone": { + "timeZoneId": "GMTStandardTime", + "displayName": "(UTC+00:00) Dublin, Edinburgh, Lisbon, London", + "offsetMinutes": 0, + "currentOffsetMinutes": 60, + "supportsDaylightSaving": true + } + }, + "gateways": [ + { + "gatewayInfo": { + "gatewayId": "222222", + "mac": "00D02DEE0000", + "crc": "1234", + "isWiFi": false + }, + "temperatureControlSystems": [ + { + "systemId": "416856", + "modelType": "VisionProWifiRetail", + "zones": [ + { + "zoneId": "416856", + "modelType": "VisionProWifiRetail", + "setpointCapabilities": { + "vacationHoldCapabilities": { + "isChangeable": true, + "isCancelable": true, + "minDuration": "1.00:00:00", + "maxDuration": "365.23:45:00", + "timingResolution": "00:15:00" + }, + "maxHeatSetpoint": 32.0, + "minHeatSetpoint": 4.5, + "valueResolution": 0.5, + "canControlHeat": true, + "canControlCool": false, + "allowedSetpointModes": [ + "PermanentOverride", + "FollowSchedule", + "TemporaryOverride", + "VacationHold" + ], + "maxDuration": "1.00:00:00", + "timingResolution": "00:15:00" + }, + "scheduleCapabilities": { + "maxSwitchpointsPerDay": 4, + "minSwitchpointsPerDay": 0, + "timingResolution": "00:15:00", + "setpointValueResolution": 0.5 + }, + "name": "THERMOSTAT", + "zoneType": "Thermostat" + } + ], + "allowedSystemModes": [ + { + "systemMode": "Off", + "canBePermanent": true, + "canBeTemporary": false + }, + { + "systemMode": "Heat", + "canBePermanent": true, + "canBeTemporary": false + } + ] + } + ] + } + ] + } +] diff --git a/tests/components/evohome/fixtures/h099625/status_111111.json b/tests/components/evohome/fixtures/h099625/status_111111.json new file mode 100644 index 00000000000000..149d8aba7839d9 --- /dev/null +++ b/tests/components/evohome/fixtures/h099625/status_111111.json @@ -0,0 +1,44 @@ +{ + "locationId": "111111", + "gateways": [ + { + "gatewayId": "222222", + "temperatureControlSystems": [ + { + "systemId": "8557535", + "zones": [ + { + "zoneId": "8557539", + "temperatureStatus": { + "temperature": 21.5, + "isAvailable": true + }, + "activeFaults": [], + "setpointStatus": { + "targetHeatTemperature": 21.5, + "setpointMode": "FollowSchedule" + }, + "name": "THERMOSTAT" + }, + { + "zoneId": "8557541", + "temperatureStatus": { + "temperature": 21.5, + "isAvailable": true + }, + "activeFaults": [], + "setpointStatus": { + "targetHeatTemperature": 21.5, + "setpointMode": "FollowSchedule" + }, + "name": "THERMOSTAT" + } + ], + "activeFaults": [], + "systemModeStatus": { "mode": "Auto", "isPermanent": true } + } + ], + "activeFaults": [] + } + ] +} diff --git a/tests/components/evohome/fixtures/h099625/user_locations.json b/tests/components/evohome/fixtures/h099625/user_locations.json new file mode 100644 index 00000000000000..cc32caccc73376 --- /dev/null +++ b/tests/components/evohome/fixtures/h099625/user_locations.json @@ -0,0 +1,113 @@ +[ + { + "locationInfo": { + "locationId": "111111", + "name": "My Home", + "timeZone": { + "timeZoneId": "FLEStandardTime", + "displayName": "(UTC+02:00) Helsinki, Kyiv, Riga, Sofia, Tallinn, Vilnius", + "offsetMinutes": 120, + "currentOffsetMinutes": 180, + "supportsDaylightSaving": true + } + }, + "gateways": [ + { + "gatewayInfo": { + "gatewayId": "222222", + "mac": "00D02DEE0000", + "crc": "1234", + "isWiFi": false + }, + "temperatureControlSystems": [ + { + "systemId": "8557535", + "modelType": "EvoTouch", + "zones": [ + { + "zoneId": "8557539", + "modelType": "RoundWireless", + "setpointCapabilities": { + "maxHeatSetpoint": 35.0, + "minHeatSetpoint": 5.0, + "valueResolution": 0.5, + "canControlHeat": true, + "canControlCool": false, + "allowedSetpointModes": [ + "PermanentOverride", + "FollowSchedule", + "TemporaryOverride" + ], + "maxDuration": "1.00:00:00", + "timingResolution": "00:10:00" + }, + "scheduleCapabilities": { + "maxSwitchpointsPerDay": 6, + "minSwitchpointsPerDay": 0, + "timingResolution": "00:10:00", + "setpointValueResolution": 0.5 + }, + "name": "Thermostat", + "zoneType": "Thermostat" + }, + { + "zoneId": "8557541", + "modelType": "RoundWireless", + "setpointCapabilities": { + "maxHeatSetpoint": 35.0, + "minHeatSetpoint": 5.0, + "valueResolution": 0.5, + "canControlHeat": true, + "canControlCool": false, + "allowedSetpointModes": [ + "PermanentOverride", + "FollowSchedule", + "TemporaryOverride" + ], + "maxDuration": "1.00:00:00", + "timingResolution": "00:10:00" + }, + "scheduleCapabilities": { + "maxSwitchpointsPerDay": 6, + "minSwitchpointsPerDay": 0, + "timingResolution": "00:10:00", + "setpointValueResolution": 0.5 + }, + "name": "Thermostat 2", + "zoneType": "Thermostat" + } + ], + "allowedSystemModes": [ + { + "systemMode": "Auto", + "canBePermanent": true, + "canBeTemporary": false + }, + { + "systemMode": "AutoWithEco", + "canBePermanent": true, + "canBeTemporary": true, + "maxDuration": "1.00:00:00", + "timingResolution": "01:00:00", + "timingMode": "Duration" + }, + { + "systemMode": "Away", + "canBePermanent": true, + "canBeTemporary": true, + "maxDuration": "99.00:00:00", + "timingResolution": "1.00:00:00", + "timingMode": "Period" + }, + { + "systemMode": "HeatingOff", + "canBePermanent": true, + "canBeTemporary": false + } + ] + } + ] + } + ] + } +] diff --git a/tests/components/evohome/fixtures/minimal/status_2738909.json b/tests/components/evohome/fixtures/minimal/status_2738909.json new file mode 100644 index 00000000000000..4b344314a6711f --- /dev/null +++ b/tests/components/evohome/fixtures/minimal/status_2738909.json @@ -0,0 +1,28 @@ +{ + "locationId": "2738909", + "gateways": [ + { + "gatewayId": "2499896", + "temperatureControlSystems": [ + { + "systemId": "3432522", + "zones": [ + { + "zoneId": "3432576", + "name": "Main Room", + "temperatureStatus": { "temperature": 19.0, "isAvailable": true }, + "setpointStatus": { + "targetHeatTemperature": 17.0, + "setpointMode": "FollowSchedule" + }, + "activeFaults": [] + } + ], + "activeFaults": [], + "systemModeStatus": { "mode": "AutoWithEco", "isPermanent": true } + } + ], + "activeFaults": [] + } + ] +} diff --git a/tests/components/evohome/fixtures/minimal/user_locations.json b/tests/components/evohome/fixtures/minimal/user_locations.json new file mode 100644 index 00000000000000..932686d8728457 --- /dev/null +++ b/tests/components/evohome/fixtures/minimal/user_locations.json @@ -0,0 +1,120 @@ +[ + { + "locationInfo": { + "locationId": "2738909", + "name": "My Home", + "streetAddress": "1 Main Street", + "city": "London", + "country": "UnitedKingdom", + "postcode": "E1 1AA", + "locationType": "Residential", + "useDaylightSaveSwitching": true, + "timeZone": { + "timeZoneId": "GMTStandardTime", + "displayName": "(UTC+00:00) Dublin, Edinburgh, Lisbon, London", + "offsetMinutes": 0, + "currentOffsetMinutes": 60, + "supportsDaylightSaving": true + }, + "locationOwner": { + "userId": "2263181", + "username": "user_2263181@gmail.com", + "firstname": "John", + "lastname": "Smith" + } + }, + "gateways": [ + { + "gatewayInfo": { + "gatewayId": "2499896", + "mac": "00D02DEE0000", + "crc": "1234", + "isWiFi": false + }, + "temperatureControlSystems": [ + { + "systemId": "3432522", + "modelType": "EvoTouch", + "zones": [ + { + "zoneId": "3432576", + "modelType": "HeatingZone", + "setpointCapabilities": { + "maxHeatSetpoint": 35.0, + "minHeatSetpoint": 5.0, + "valueResolution": 0.5, + "canControlHeat": true, + "canControlCool": false, + "allowedSetpointModes": [ + "PermanentOverride", + "FollowSchedule", + "TemporaryOverride" + ], + "maxDuration": "1.00:00:00", + "timingResolution": "00:10:00" + }, + "scheduleCapabilities": { + "maxSwitchpointsPerDay": 6, + "minSwitchpointsPerDay": 1, + "timingResolution": "00:10:00", + "setpointValueResolution": 0.5 + }, + "name": "Main Room", + "zoneType": "RadiatorZone" + } + ], + "allowedSystemModes": [ + { + "systemMode": "HeatingOff", + "canBePermanent": true, + "canBeTemporary": false + }, + { + "systemMode": "Auto", + "canBePermanent": true, + "canBeTemporary": false + }, + { + "systemMode": "AutoWithReset", + "canBePermanent": true, + "canBeTemporary": false + }, + { + "systemMode": "AutoWithEco", + "canBePermanent": true, + "canBeTemporary": true, + "maxDuration": "1.00:00:00", + "timingResolution": "01:00:00", + "timingMode": "Duration" + }, + { + "systemMode": "Away", + "canBePermanent": true, + "canBeTemporary": true, + "maxDuration": "99.00:00:00", + "timingResolution": "1.00:00:00", + "timingMode": "Period" + }, + { + "systemMode": "DayOff", + "canBePermanent": true, + "canBeTemporary": true, + "maxDuration": "99.00:00:00", + "timingResolution": "1.00:00:00", + "timingMode": "Period" + }, + { + "systemMode": "Custom", + "canBePermanent": true, + "canBeTemporary": true, + "maxDuration": "99.00:00:00", + "timingResolution": "1.00:00:00", + "timingMode": "Period" + } + ] + } + ] + } + ] + } +] diff --git a/tests/components/evohome/fixtures/system_004/status_3164610.json b/tests/components/evohome/fixtures/system_004/status_3164610.json new file mode 100644 index 00000000000000..a9ef3f6ee28ab4 --- /dev/null +++ b/tests/components/evohome/fixtures/system_004/status_3164610.json @@ -0,0 +1,33 @@ +{ + "locationId": "3164610", + "gateways": [ + { + "gatewayId": "2938388", + "temperatureControlSystems": [ + { + "systemId": "4187769", + "zones": [ + { + "zoneId": "4187768", + "temperatureStatus": { "temperature": 19.5, "isAvailable": true }, + "activeFaults": [], + "setpointStatus": { + "targetHeatTemperature": 15.0, + "setpointMode": "PermanentOverride" + }, + "name": "Thermostat" + } + ], + "activeFaults": [], + "systemModeStatus": { "mode": "Auto", "isPermanent": true } + } + ], + "activeFaults": [ + { + "faultType": "GatewayCommunicationLost", + "since": "2023-05-04T18:47:36.7727046" + } + ] + } + ] +} diff --git a/tests/components/evohome/fixtures/system_004/user_locations.json b/tests/components/evohome/fixtures/system_004/user_locations.json new file mode 100644 index 00000000000000..9defab8b6ee1e4 --- /dev/null +++ b/tests/components/evohome/fixtures/system_004/user_locations.json @@ -0,0 +1,99 @@ +[ + { + "locationInfo": { + "locationId": "3164610", + "name": "Living room", + "streetAddress": "1 Main Road", + "city": "Boomtown", + "country": "Netherlands", + "postcode": "1234XX", + "locationType": "Residential", + "useDaylightSaveSwitching": true, + "timeZone": { + "timeZoneId": "WEuropeStandardTime", + "displayName": "(UTC+01:00) Amsterdam, Berlijn, Bern, Rome, Stockholm, Wenen", + "offsetMinutes": 60, + "currentOffsetMinutes": 120, + "supportsDaylightSaving": true + }, + "locationOwner": { + "userId": "2624305", + "username": "user_2624305@gmail.com", + "firstname": "Chris", + "lastname": "Jones" + } + }, + "gateways": [ + { + "gatewayInfo": { + "gatewayId": "2938388", + "mac": "00D02D5A7000", + "crc": "1234", + "isWiFi": false + }, + "temperatureControlSystems": [ + { + "systemId": "4187769", + "modelType": "EvoTouch", + "zones": [ + { + "zoneId": "4187768", + "modelType": "RoundModulation", + "setpointCapabilities": { + "maxHeatSetpoint": 35.0, + "minHeatSetpoint": 5.0, + "valueResolution": 0.5, + "canControlHeat": true, + "canControlCool": false, + "allowedSetpointModes": [ + "PermanentOverride", + "FollowSchedule", + "TemporaryOverride" + ], + "maxDuration": "1.00:00:00", + "timingResolution": "00:10:00" + }, + "scheduleCapabilities": { + "maxSwitchpointsPerDay": 6, + "minSwitchpointsPerDay": 0, + "timingResolution": "00:10:00", + "setpointValueResolution": 0.5 + }, + "name": "Thermostat", + "zoneType": "Thermostat" + } + ], + "allowedSystemModes": [ + { + "systemMode": "Auto", + "canBePermanent": true, + "canBeTemporary": false + }, + { + "systemMode": "AutoWithEco", + "canBePermanent": true, + "canBeTemporary": true, + "maxDuration": "1.00:00:00", + "timingResolution": "01:00:00", + "timingMode": "Duration" + }, + { + "systemMode": "Away", + "canBePermanent": true, + "canBeTemporary": true, + "maxDuration": "99.00:00:00", + "timingResolution": "1.00:00:00", + "timingMode": "Period" + }, + { + "systemMode": "HeatingOff", + "canBePermanent": true, + "canBeTemporary": false + } + ] + } + ] + } + ] + } +] diff --git a/tests/components/evohome/snapshots/test_init.ambr b/tests/components/evohome/snapshots/test_init.ambr new file mode 100644 index 00000000000000..8e5338ecb9b728 --- /dev/null +++ b/tests/components/evohome/snapshots/test_init.ambr @@ -0,0 +1,863 @@ +# serializer version: 1 +# name: test_entities[default] + list([ + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'current_temperature': 19.7, + 'friendly_name': 'My Home', + 'hvac_modes': list([ + , + , + ]), + 'icon': 'mdi:thermostat', + 'max_temp': 35, + 'min_temp': 7, + 'preset_mode': 'eco', + 'preset_modes': list([ + 'Reset', + 'eco', + 'away', + 'home', + 'Custom', + ]), + 'status': dict({ + 'active_system_faults': list([ + ]), + 'system_id': '3432522', + 'system_mode_status': dict({ + 'is_permanent': True, + 'mode': 'AutoWithEco', + }), + }), + 'supported_features': , + }), + 'context': , + 'entity_id': 'climate.my_home', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'heat', + }), + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'current_temperature': None, + 'friendly_name': 'Dead Zone', + 'hvac_modes': list([ + , + , + ]), + 'max_temp': 35.0, + 'min_temp': 5.0, + 'preset_mode': 'none', + 'preset_modes': list([ + 'none', + 'temporary', + 'permanent', + ]), + 'status': dict({ + 'active_faults': list([ + ]), + 'setpoint_status': dict({ + 'setpoint_mode': 'FollowSchedule', + 'target_heat_temperature': 17.0, + }), + 'setpoints': dict({ + 'next_sp_from': '2024-07-10T14:10:00-07:00', + 'next_sp_temp': 18.6, + 'this_sp_from': '2024-07-10T00:00:00-07:00', + 'this_sp_temp': 16.0, + }), + 'temperature_status': dict({ + 'is_available': False, + }), + 'zone_id': '3432521', + }), + 'supported_features': , + 'temperature': 17.0, + }), + 'context': , + 'entity_id': 'climate.dead_zone', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'heat', + }), + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'current_temperature': 19.0, + 'friendly_name': 'Main Room', + 'hvac_modes': list([ + , + , + ]), + 'max_temp': 35.0, + 'min_temp': 5.0, + 'preset_mode': 'permanent', + 'preset_modes': list([ + 'none', + 'temporary', + 'permanent', + ]), + 'status': dict({ + 'active_faults': list([ + dict({ + 'faultType': 'TempZoneActuatorCommunicationLost', + 'since': '2022-03-02T15:56:01', + }), + ]), + 'setpoint_status': dict({ + 'setpoint_mode': 'PermanentOverride', + 'target_heat_temperature': 17.0, + }), + 'setpoints': dict({ + 'next_sp_from': '2024-07-10T14:10:00-07:00', + 'next_sp_temp': 18.6, + 'this_sp_from': '2024-07-10T00:00:00-07:00', + 'this_sp_temp': 16.0, + }), + 'temperature_status': dict({ + 'is_available': True, + 'temperature': 19.0, + }), + 'zone_id': '3432576', + }), + 'supported_features': , + 'temperature': 17.0, + }), + 'context': , + 'entity_id': 'climate.main_room', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'heat', + }), + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'current_temperature': 19.0, + 'friendly_name': 'Front Room', + 'hvac_modes': list([ + , + , + ]), + 'max_temp': 35.0, + 'min_temp': 5.0, + 'preset_mode': 'temporary', + 'preset_modes': list([ + 'none', + 'temporary', + 'permanent', + ]), + 'status': dict({ + 'active_faults': list([ + dict({ + 'faultType': 'TempZoneActuatorLowBattery', + 'since': '2022-03-02T04:50:20', + }), + ]), + 'setpoint_status': dict({ + 'setpoint_mode': 'TemporaryOverride', + 'target_heat_temperature': 21.0, + 'until': '2022-03-07T11:00:00-08:00', + }), + 'setpoints': dict({ + 'next_sp_from': '2024-07-10T14:10:00-07:00', + 'next_sp_temp': 18.6, + 'this_sp_from': '2024-07-10T00:00:00-07:00', + 'this_sp_temp': 16.0, + }), + 'temperature_status': dict({ + 'is_available': True, + 'temperature': 19.0, + }), + 'zone_id': '3432577', + }), + 'supported_features': , + 'temperature': 21.0, + }), + 'context': , + 'entity_id': 'climate.front_room', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'heat', + }), + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'current_temperature': 20.0, + 'friendly_name': 'Kitchen', + 'hvac_modes': list([ + , + , + ]), + 'max_temp': 35.0, + 'min_temp': 5.0, + 'preset_mode': 'none', + 'preset_modes': list([ + 'none', + 'temporary', + 'permanent', + ]), + 'status': dict({ + 'active_faults': list([ + ]), + 'setpoint_status': dict({ + 'setpoint_mode': 'FollowSchedule', + 'target_heat_temperature': 17.0, + }), + 'setpoints': dict({ + 'next_sp_from': '2024-07-10T14:10:00-07:00', + 'next_sp_temp': 18.6, + 'this_sp_from': '2024-07-10T00:00:00-07:00', + 'this_sp_temp': 16.0, + }), + 'temperature_status': dict({ + 'is_available': True, + 'temperature': 20.0, + }), + 'zone_id': '3432578', + }), + 'supported_features': , + 'temperature': 17.0, + }), + 'context': , + 'entity_id': 'climate.kitchen', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'heat', + }), + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'current_temperature': 20.0, + 'friendly_name': 'Bathroom Dn', + 'hvac_modes': list([ + , + , + ]), + 'max_temp': 35.0, + 'min_temp': 5.0, + 'preset_mode': 'none', + 'preset_modes': list([ + 'none', + 'temporary', + 'permanent', + ]), + 'status': dict({ + 'active_faults': list([ + ]), + 'setpoint_status': dict({ + 'setpoint_mode': 'FollowSchedule', + 'target_heat_temperature': 16.0, + }), + 'setpoints': dict({ + 'next_sp_from': '2024-07-10T14:10:00-07:00', + 'next_sp_temp': 18.6, + 'this_sp_from': '2024-07-10T00:00:00-07:00', + 'this_sp_temp': 16.0, + }), + 'temperature_status': dict({ + 'is_available': True, + 'temperature': 20.0, + }), + 'zone_id': '3432579', + }), + 'supported_features': , + 'temperature': 16.0, + }), + 'context': , + 'entity_id': 'climate.bathroom_dn', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'heat', + }), + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'current_temperature': 21.0, + 'friendly_name': 'Main Bedroom', + 'hvac_modes': list([ + , + , + ]), + 'max_temp': 35.0, + 'min_temp': 5.0, + 'preset_mode': 'none', + 'preset_modes': list([ + 'none', + 'temporary', + 'permanent', + ]), + 'status': dict({ + 'active_faults': list([ + ]), + 'setpoint_status': dict({ + 'setpoint_mode': 'FollowSchedule', + 'target_heat_temperature': 16.0, + }), + 'setpoints': dict({ + 'next_sp_from': '2024-07-10T14:10:00-07:00', + 'next_sp_temp': 18.6, + 'this_sp_from': '2024-07-10T00:00:00-07:00', + 'this_sp_temp': 16.0, + }), + 'temperature_status': dict({ + 'is_available': True, + 'temperature': 21.0, + }), + 'zone_id': '3432580', + }), + 'supported_features': , + 'temperature': 16.0, + }), + 'context': , + 'entity_id': 'climate.main_bedroom', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'heat', + }), + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'current_temperature': 19.5, + 'friendly_name': 'Kids Room', + 'hvac_modes': list([ + , + , + ]), + 'max_temp': 35.0, + 'min_temp': 5.0, + 'preset_mode': 'none', + 'preset_modes': list([ + 'none', + 'temporary', + 'permanent', + ]), + 'status': dict({ + 'active_faults': list([ + ]), + 'setpoint_status': dict({ + 'setpoint_mode': 'FollowSchedule', + 'target_heat_temperature': 17.0, + }), + 'setpoints': dict({ + 'next_sp_from': '2024-07-10T14:10:00-07:00', + 'next_sp_temp': 18.6, + 'this_sp_from': '2024-07-10T00:00:00-07:00', + 'this_sp_temp': 16.0, + }), + 'temperature_status': dict({ + 'is_available': True, + 'temperature': 19.5, + }), + 'zone_id': '3449703', + }), + 'supported_features': , + 'temperature': 17.0, + }), + 'context': , + 'entity_id': 'climate.kids_room', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'heat', + }), + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'away_mode': 'on', + 'current_temperature': 23, + 'friendly_name': 'Domestic Hot Water', + 'icon': 'mdi:thermometer-lines', + 'max_temp': 60, + 'min_temp': 43, + 'operation_list': list([ + 'auto', + 'on', + 'off', + ]), + 'operation_mode': 'off', + 'status': dict({ + 'active_faults': list([ + ]), + 'dhw_id': '3933910', + 'setpoints': dict({ + 'next_sp_from': '2024-07-10T05:00:00-07:00', + 'next_sp_state': 'Off', + 'this_sp_from': '2024-07-10T04:00:00-07:00', + 'this_sp_state': 'On', + }), + 'state_status': dict({ + 'mode': 'PermanentOverride', + 'state': 'Off', + }), + 'temperature_status': dict({ + 'is_available': True, + 'temperature': 23.0, + }), + }), + 'supported_features': , + 'target_temp_high': None, + 'target_temp_low': None, + 'temperature': None, + }), + 'context': , + 'entity_id': 'water_heater.domestic_hot_water', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }), + ]) +# --- +# name: test_entities[h032585] + list([ + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'current_temperature': 21.5, + 'friendly_name': 'My Home', + 'hvac_modes': list([ + , + , + ]), + 'icon': 'mdi:thermostat', + 'max_temp': 35, + 'min_temp': 7, + 'status': dict({ + 'active_system_faults': list([ + ]), + 'system_id': '416856', + 'system_mode_status': dict({ + 'is_permanent': True, + 'mode': 'Heat', + }), + }), + 'supported_features': , + }), + 'context': , + 'entity_id': 'climate.my_home', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'heat', + }), + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'current_temperature': 21.5, + 'friendly_name': 'THERMOSTAT', + 'hvac_modes': list([ + , + , + ]), + 'max_temp': 32.0, + 'min_temp': 4.5, + 'preset_mode': 'none', + 'preset_modes': list([ + 'none', + 'temporary', + 'permanent', + ]), + 'status': dict({ + 'active_faults': list([ + ]), + 'setpoint_status': dict({ + 'setpoint_mode': 'FollowSchedule', + 'target_heat_temperature': 21.5, + }), + 'setpoints': dict({ + 'next_sp_from': '2024-07-10T14:10:00-07:00', + 'next_sp_temp': 18.6, + 'this_sp_from': '2024-07-10T00:00:00-07:00', + 'this_sp_temp': 16.0, + }), + 'temperature_status': dict({ + 'is_available': True, + 'temperature': 21.5, + }), + 'zone_id': '416856', + }), + 'supported_features': , + 'temperature': 21.5, + }), + 'context': , + 'entity_id': 'climate.thermostat', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'heat', + }), + ]) +# --- +# name: test_entities[h099625] + list([ + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'current_temperature': 21.5, + 'friendly_name': 'My Home', + 'hvac_modes': list([ + , + , + ]), + 'icon': 'mdi:thermostat', + 'max_temp': 35, + 'min_temp': 7, + 'preset_mode': None, + 'preset_modes': list([ + 'eco', + 'away', + ]), + 'status': dict({ + 'active_system_faults': list([ + ]), + 'system_id': '8557535', + 'system_mode_status': dict({ + 'is_permanent': True, + 'mode': 'Auto', + }), + }), + 'supported_features': , + }), + 'context': , + 'entity_id': 'climate.my_home', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'heat', + }), + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'current_temperature': 21.5, + 'friendly_name': 'THERMOSTAT', + 'hvac_modes': list([ + , + , + ]), + 'max_temp': 35.0, + 'min_temp': 5.0, + 'preset_mode': 'none', + 'preset_modes': list([ + 'none', + 'temporary', + 'permanent', + ]), + 'status': dict({ + 'active_faults': list([ + ]), + 'setpoint_status': dict({ + 'setpoint_mode': 'FollowSchedule', + 'target_heat_temperature': 21.5, + }), + 'setpoints': dict({ + 'next_sp_from': '2024-07-10T12:10:00-07:00', + 'next_sp_temp': 18.6, + 'this_sp_from': '2024-07-09T22:00:00-07:00', + 'this_sp_temp': 16.0, + }), + 'temperature_status': dict({ + 'is_available': True, + 'temperature': 21.5, + }), + 'zone_id': '8557539', + }), + 'supported_features': , + 'temperature': 21.5, + }), + 'context': , + 'entity_id': 'climate.thermostat', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'heat', + }), + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'current_temperature': 21.5, + 'friendly_name': 'THERMOSTAT', + 'hvac_modes': list([ + , + , + ]), + 'max_temp': 35.0, + 'min_temp': 5.0, + 'preset_mode': 'none', + 'preset_modes': list([ + 'none', + 'temporary', + 'permanent', + ]), + 'status': dict({ + 'active_faults': list([ + ]), + 'setpoint_status': dict({ + 'setpoint_mode': 'FollowSchedule', + 'target_heat_temperature': 21.5, + }), + 'setpoints': dict({ + 'next_sp_from': '2024-07-10T12:10:00-07:00', + 'next_sp_temp': 18.6, + 'this_sp_from': '2024-07-09T22:00:00-07:00', + 'this_sp_temp': 16.0, + }), + 'temperature_status': dict({ + 'is_available': True, + 'temperature': 21.5, + }), + 'zone_id': '8557541', + }), + 'supported_features': , + 'temperature': 21.5, + }), + 'context': , + 'entity_id': 'climate.thermostat_2', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'heat', + }), + ]) +# --- +# name: test_entities[h118169] + list([ + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'current_temperature': 21.5, + 'friendly_name': 'My Home', + 'hvac_modes': list([ + , + , + ]), + 'icon': 'mdi:thermostat', + 'max_temp': 35, + 'min_temp': 7, + 'status': dict({ + 'active_system_faults': list([ + ]), + 'system_id': '333333', + 'system_mode_status': dict({ + 'is_permanent': True, + 'mode': 'Heat', + }), + }), + 'supported_features': , + }), + 'context': , + 'entity_id': 'climate.my_home', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'heat', + }), + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'current_temperature': 21.5, + 'friendly_name': 'THERMOSTAT', + 'hvac_modes': list([ + , + , + ]), + 'max_temp': 32.0, + 'min_temp': 4.5, + 'preset_mode': 'none', + 'preset_modes': list([ + 'none', + 'temporary', + 'permanent', + ]), + 'status': dict({ + 'active_faults': list([ + ]), + 'setpoint_status': dict({ + 'setpoint_mode': 'FollowSchedule', + 'target_heat_temperature': 21.5, + }), + 'setpoints': dict({ + 'next_sp_from': '2024-08-14T23:00:00-07:00', + 'next_sp_temp': 18.1, + 'this_sp_from': '2024-08-14T15:00:00-07:00', + 'this_sp_temp': 15.9, + }), + 'temperature_status': dict({ + 'is_available': True, + 'temperature': 21.5, + }), + 'zone_id': '444444', + }), + 'supported_features': , + 'temperature': 21.5, + }), + 'context': , + 'entity_id': 'climate.thermostat', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'heat', + }), + ]) +# --- +# name: test_entities[minimal] + list([ + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'current_temperature': 19.0, + 'friendly_name': 'My Home', + 'hvac_modes': list([ + , + , + ]), + 'icon': 'mdi:thermostat', + 'max_temp': 35, + 'min_temp': 7, + 'preset_mode': 'eco', + 'preset_modes': list([ + 'Reset', + 'eco', + 'away', + 'home', + 'Custom', + ]), + 'status': dict({ + 'active_system_faults': list([ + ]), + 'system_id': '3432522', + 'system_mode_status': dict({ + 'is_permanent': True, + 'mode': 'AutoWithEco', + }), + }), + 'supported_features': , + }), + 'context': , + 'entity_id': 'climate.my_home', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'heat', + }), + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'current_temperature': 19.0, + 'friendly_name': 'Main Room', + 'hvac_modes': list([ + , + , + ]), + 'max_temp': 35.0, + 'min_temp': 5.0, + 'preset_mode': 'none', + 'preset_modes': list([ + 'none', + 'temporary', + 'permanent', + ]), + 'status': dict({ + 'active_faults': list([ + ]), + 'setpoint_status': dict({ + 'setpoint_mode': 'FollowSchedule', + 'target_heat_temperature': 17.0, + }), + 'setpoints': dict({ + 'next_sp_from': '2024-07-10T14:10:00-07:00', + 'next_sp_temp': 18.6, + 'this_sp_from': '2024-07-10T00:00:00-07:00', + 'this_sp_temp': 16.0, + }), + 'temperature_status': dict({ + 'is_available': True, + 'temperature': 19.0, + }), + 'zone_id': '3432576', + }), + 'supported_features': , + 'temperature': 17.0, + }), + 'context': , + 'entity_id': 'climate.main_room', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'heat', + }), + ]) +# --- +# name: test_entities[system_004] + list([ + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'current_temperature': 19.5, + 'friendly_name': 'Living room', + 'hvac_modes': list([ + , + , + ]), + 'icon': 'mdi:thermostat', + 'max_temp': 35, + 'min_temp': 7, + 'preset_mode': None, + 'preset_modes': list([ + 'eco', + 'away', + ]), + 'status': dict({ + 'active_system_faults': list([ + ]), + 'system_id': '4187769', + 'system_mode_status': dict({ + 'is_permanent': True, + 'mode': 'Auto', + }), + }), + 'supported_features': , + }), + 'context': , + 'entity_id': 'climate.living_room', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'heat', + }), + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'current_temperature': 19.5, + 'friendly_name': 'Thermostat', + 'hvac_modes': list([ + , + , + ]), + 'max_temp': 35.0, + 'min_temp': 5.0, + 'preset_mode': 'permanent', + 'preset_modes': list([ + 'none', + 'temporary', + 'permanent', + ]), + 'status': dict({ + 'active_faults': list([ + ]), + 'setpoint_status': dict({ + 'setpoint_mode': 'PermanentOverride', + 'target_heat_temperature': 15.0, + }), + 'setpoints': dict({ + 'next_sp_from': '2024-07-10T13:10:00-07:00', + 'next_sp_temp': 18.6, + 'this_sp_from': '2024-07-09T23:00:00-07:00', + 'this_sp_temp': 16.0, + }), + 'temperature_status': dict({ + 'is_available': True, + 'temperature': 19.5, + }), + 'zone_id': '4187768', + }), + 'supported_features': , + 'temperature': 15.0, + }), + 'context': , + 'entity_id': 'climate.thermostat', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'heat', + }), + ]) +# --- diff --git a/tests/components/evohome/test_init.py b/tests/components/evohome/test_init.py new file mode 100644 index 00000000000000..ad688d04882a98 --- /dev/null +++ b/tests/components/evohome/test_init.py @@ -0,0 +1,30 @@ +"""The tests for evohome.""" + +from __future__ import annotations + +from freezegun.api import FrozenDateTimeFactory +import pytest +from syrupy import SnapshotAssertion + +from homeassistant.core import HomeAssistant + +from .conftest import setup_evohome +from .const import TEST_INSTALLS + + +@pytest.mark.parametrize("install", TEST_INSTALLS) +async def test_entities( + hass: HomeAssistant, + evo_config: dict[str, str], + install: str, + snapshot: SnapshotAssertion, + freezer: FrozenDateTimeFactory, +) -> None: + """Test entities and state after setup of a Honeywell TCC-compatible system.""" + + # some extended state attrs are relative the current time + freezer.move_to("2024-07-10 12:00:00+00:00") + + await setup_evohome(hass, evo_config, install=install) + + assert hass.states.async_all() == snapshot diff --git a/tests/components/evohome/test_storage.py b/tests/components/evohome/test_storage.py index e87b847a9ff501..32cd49a1539f3a 100644 --- a/tests/components/evohome/test_storage.py +++ b/tests/components/evohome/test_storage.py @@ -8,7 +8,6 @@ import pytest from homeassistant.components.evohome import ( - CONF_PASSWORD, CONF_USERNAME, DOMAIN, STORAGE_KEY, @@ -56,11 +55,6 @@ def dt_pair(dt_dtm: datetime) -> tuple[datetime, str]: USERNAME_DIFF: Final = f"not_{USERNAME}" USERNAME_SAME: Final = USERNAME -TEST_CONFIG: Final = { - CONF_USERNAME: USERNAME_SAME, - CONF_PASSWORD: "password", -} - TEST_DATA: Final[dict[str, _TokenStoreT]] = { "sans_session_id": { SZ_USERNAME: USERNAME_SAME, @@ -93,13 +87,14 @@ def dt_pair(dt_dtm: datetime) -> tuple[datetime, str]: async def test_auth_tokens_null( hass: HomeAssistant, hass_storage: dict[str, Any], + evo_config: dict[str, str], idx: str, ) -> None: """Test loading/saving authentication tokens when no cached tokens in the store.""" hass_storage[DOMAIN] = DOMAIN_STORAGE_BASE | {"data": TEST_DATA_NULL[idx]} - mock_client = await setup_evohome(hass, TEST_CONFIG) + mock_client = await setup_evohome(hass, evo_config, install="minimal") # Confirm client was instantiated without tokens, as cache was empty... assert SZ_REFRESH_TOKEN not in mock_client.call_args.kwargs @@ -120,13 +115,16 @@ async def test_auth_tokens_null( @pytest.mark.parametrize("idx", TEST_DATA) async def test_auth_tokens_same( - hass: HomeAssistant, hass_storage: dict[str, Any], idx: str + hass: HomeAssistant, + hass_storage: dict[str, Any], + evo_config: dict[str, str], + idx: str, ) -> None: """Test loading/saving authentication tokens when matching username.""" hass_storage[DOMAIN] = DOMAIN_STORAGE_BASE | {"data": TEST_DATA[idx]} - mock_client = await setup_evohome(hass, TEST_CONFIG) + mock_client = await setup_evohome(hass, evo_config, install="minimal") # Confirm client was instantiated with the cached tokens... assert mock_client.call_args.kwargs[SZ_REFRESH_TOKEN] == REFRESH_TOKEN @@ -146,7 +144,10 @@ async def test_auth_tokens_same( @pytest.mark.parametrize("idx", TEST_DATA) async def test_auth_tokens_past( - hass: HomeAssistant, hass_storage: dict[str, Any], idx: str + hass: HomeAssistant, + hass_storage: dict[str, Any], + evo_config: dict[str, str], + idx: str, ) -> None: """Test loading/saving authentication tokens with matching username, but expired.""" @@ -158,7 +159,7 @@ async def test_auth_tokens_past( hass_storage[DOMAIN] = DOMAIN_STORAGE_BASE | {"data": test_data} - mock_client = await setup_evohome(hass, TEST_CONFIG) + mock_client = await setup_evohome(hass, evo_config, install="minimal") # Confirm client was instantiated with the cached tokens... assert mock_client.call_args.kwargs[SZ_REFRESH_TOKEN] == REFRESH_TOKEN @@ -181,14 +182,17 @@ async def test_auth_tokens_past( @pytest.mark.parametrize("idx", TEST_DATA) async def test_auth_tokens_diff( - hass: HomeAssistant, hass_storage: dict[str, Any], idx: str + hass: HomeAssistant, + hass_storage: dict[str, Any], + evo_config: dict[str, str], + idx: str, ) -> None: """Test loading/saving authentication tokens when unmatched username.""" hass_storage[DOMAIN] = DOMAIN_STORAGE_BASE | {"data": TEST_DATA[idx]} mock_client = await setup_evohome( - hass, TEST_CONFIG | {CONF_USERNAME: USERNAME_DIFF} + hass, evo_config | {CONF_USERNAME: USERNAME_DIFF}, install="minimal" ) # Confirm client was instantiated without tokens, as username was different... diff --git a/tests/components/fibaro/test_config_flow.py b/tests/components/fibaro/test_config_flow.py index b6b4e3992cd70b..508bb81973d8a0 100644 --- a/tests/components/fibaro/test_config_flow.py +++ b/tests/components/fibaro/test_config_flow.py @@ -183,15 +183,7 @@ async def test_reauth_success( hass: HomeAssistant, mock_config_entry: MockConfigEntry ) -> None: """Successful reauth flow initialized by the user.""" - - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": mock_config_entry.entry_id, - }, - ) - + result = await mock_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" assert result["errors"] == {} @@ -211,15 +203,7 @@ async def test_reauth_connect_failure( mock_fibaro_client: Mock, ) -> None: """Successful reauth flow initialized by the user.""" - - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": mock_config_entry.entry_id, - }, - ) - + result = await mock_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" assert result["errors"] == {} @@ -244,15 +228,7 @@ async def test_reauth_auth_failure( mock_fibaro_client: Mock, ) -> None: """Successful reauth flow initialized by the user.""" - - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": mock_config_entry.entry_id, - }, - ) - + result = await mock_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" assert result["errors"] == {} diff --git a/tests/components/fireservicerota/test_config_flow.py b/tests/components/fireservicerota/test_config_flow.py index 539906d800bdc2..5555a8d649cf39 100644 --- a/tests/components/fireservicerota/test_config_flow.py +++ b/tests/components/fireservicerota/test_config_flow.py @@ -120,23 +120,8 @@ async def test_reauth(hass: HomeAssistant) -> None: domain=DOMAIN, data=MOCK_CONF, unique_id=MOCK_CONF[CONF_USERNAME] ) entry.add_to_hass(hass) - with patch( - "homeassistant.components.fireservicerota.config_flow.FireServiceRota" - ) as mock_fsr: - mock_fireservicerota = mock_fsr.return_value - mock_fireservicerota.request_tokens.return_value = MOCK_TOKEN_INFO - - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": entry.unique_id, - }, - data=MOCK_CONF, - ) - - await hass.async_block_till_done() - assert result["type"] is FlowResultType.FORM + result = await entry.start_reauth_flow(hass) + assert result["type"] is FlowResultType.FORM with ( patch( diff --git a/tests/components/fitbit/test_config_flow.py b/tests/components/fitbit/test_config_flow.py index d5f3d09abdd630..6f7174594865b8 100644 --- a/tests/components/fitbit/test_config_flow.py +++ b/tests/components/fitbit/test_config_flow.py @@ -472,13 +472,7 @@ async def test_reauth_flow( assert len(entries) == 1 # config_entry.req initiates reauth - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": config_entry.entry_id, - }, - ) + result = await config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -546,13 +540,7 @@ async def test_reauth_wrong_user_id( entries = hass.config_entries.async_entries(DOMAIN) assert len(entries) == 1 - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": config_entry.entry_id, - }, - ) + result = await config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/flume/test_config_flow.py b/tests/components/flume/test_config_flow.py index 915299223e9e69..87fe3a2bbf0659 100644 --- a/tests/components/flume/test_config_flow.py +++ b/tests/components/flume/test_config_flow.py @@ -124,11 +124,7 @@ async def test_reauth(hass: HomeAssistant, requests_mock: Mocker) -> None: ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_REAUTH, "unique_id": "test@test.org"}, - ) - + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/fritz/snapshots/test_button.ambr b/tests/components/fritz/snapshots/test_button.ambr new file mode 100644 index 00000000000000..ed0b0e72160a68 --- /dev/null +++ b/tests/components/fritz/snapshots/test_button.ambr @@ -0,0 +1,235 @@ +# serializer version: 1 +# name: test_button_setup[button.mock_title_cleanup-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'button', + 'entity_category': , + 'entity_id': 'button.mock_title_cleanup', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Cleanup', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'cleanup', + 'unique_id': '1C:ED:6F:12:34:11-cleanup', + 'unit_of_measurement': None, + }) +# --- +# name: test_button_setup[button.mock_title_cleanup-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Mock Title Cleanup', + }), + 'context': , + 'entity_id': 'button.mock_title_cleanup', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'unknown', + }) +# --- +# name: test_button_setup[button.mock_title_firmware_update-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'button', + 'entity_category': , + 'entity_id': 'button.mock_title_firmware_update', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Firmware update', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'firmware_update', + 'unique_id': '1C:ED:6F:12:34:11-firmware_update', + 'unit_of_measurement': None, + }) +# --- +# name: test_button_setup[button.mock_title_firmware_update-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'update', + 'friendly_name': 'Mock Title Firmware update', + }), + 'context': , + 'entity_id': 'button.mock_title_firmware_update', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'unknown', + }) +# --- +# name: test_button_setup[button.mock_title_reconnect-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'button', + 'entity_category': , + 'entity_id': 'button.mock_title_reconnect', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Reconnect', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'reconnect', + 'unique_id': '1C:ED:6F:12:34:11-reconnect', + 'unit_of_measurement': None, + }) +# --- +# name: test_button_setup[button.mock_title_reconnect-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'restart', + 'friendly_name': 'Mock Title Reconnect', + }), + 'context': , + 'entity_id': 'button.mock_title_reconnect', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'unknown', + }) +# --- +# name: test_button_setup[button.mock_title_restart-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'button', + 'entity_category': , + 'entity_id': 'button.mock_title_restart', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Restart', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '1C:ED:6F:12:34:11-reboot', + 'unit_of_measurement': None, + }) +# --- +# name: test_button_setup[button.mock_title_restart-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'restart', + 'friendly_name': 'Mock Title Restart', + }), + 'context': , + 'entity_id': 'button.mock_title_restart', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'unknown', + }) +# --- +# name: test_button_setup[button.printer_wake_on_lan-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'button', + 'entity_category': None, + 'entity_id': 'button.printer_wake_on_lan', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': 'mdi:lan-pending', + 'original_name': 'printer Wake on LAN', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': 'AA:BB:CC:00:11:22_wake_on_lan', + 'unit_of_measurement': None, + }) +# --- +# name: test_button_setup[button.printer_wake_on_lan-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'printer Wake on LAN', + 'icon': 'mdi:lan-pending', + }), + 'context': , + 'entity_id': 'button.printer_wake_on_lan', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'unknown', + }) +# --- diff --git a/tests/components/fritz/snapshots/test_diagnostics.ambr b/tests/components/fritz/snapshots/test_diagnostics.ambr new file mode 100644 index 00000000000000..4b5b8bdea3b4de --- /dev/null +++ b/tests/components/fritz/snapshots/test_diagnostics.ambr @@ -0,0 +1,67 @@ +# serializer version: 1 +# name: test_entry_diagnostics + dict({ + 'device_info': dict({ + 'client_devices': list([ + dict({ + 'connected_to': 'fritz.box', + 'connection_type': 'LAN', + 'hostname': 'printer', + 'is_connected': True, + 'wan_access': True, + }), + ]), + 'connection_type': 'WANPPPConnection', + 'current_firmware': '7.29', + 'discovered_services': list([ + 'DeviceInfo1', + 'Hosts1', + 'LANEthernetInterfaceConfig1', + 'Layer3Forwarding1', + 'UserInterface1', + 'WANCommonIFC1', + 'WANCommonInterfaceConfig1', + 'WANDSLInterfaceConfig1', + 'WANIPConn1', + 'WANPPPConnection1', + 'WLANConfiguration1', + 'X_AVM-DE_Homeauto1', + 'X_AVM-DE_HostFilter1', + ]), + 'is_router': True, + 'last_exception': None, + 'last_update success': True, + 'latest_firmware': None, + 'mesh_role': 'master', + 'model': 'FRITZ!Box 7530 AX', + 'unique_id': '1C:ED:XX:XX:34:11', + 'update_available': False, + 'wan_link_properties': dict({ + 'NewLayer1DownstreamMaxBitRate': 318557000, + 'NewLayer1UpstreamMaxBitRate': 51805000, + 'NewPhysicalLinkStatus': 'Up', + 'NewWANAccessType': 'DSL', + }), + }), + 'entry': dict({ + 'data': dict({ + 'host': 'fake_host', + 'password': '**REDACTED**', + 'port': '1234', + 'ssl': False, + 'username': '**REDACTED**', + }), + 'disabled_by': None, + 'domain': 'fritz', + 'minor_version': 1, + 'options': dict({ + }), + 'pref_disable_new_entities': False, + 'pref_disable_polling': False, + 'source': 'user', + 'title': 'Mock Title', + 'unique_id': None, + 'version': 1, + }), + }) +# --- diff --git a/tests/components/fritz/snapshots/test_sensor.ambr b/tests/components/fritz/snapshots/test_sensor.ambr new file mode 100644 index 00000000000000..50744815aa506f --- /dev/null +++ b/tests/components/fritz/snapshots/test_sensor.ambr @@ -0,0 +1,771 @@ +# serializer version: 1 +# name: test_sensor_setup[sensor.mock_title_connection_uptime-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.mock_title_connection_uptime', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Connection uptime', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'connection_uptime', + 'unique_id': '1C:ED:6F:12:34:11-connection_uptime', + 'unit_of_measurement': None, + }) +# --- +# name: test_sensor_setup[sensor.mock_title_connection_uptime-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'timestamp', + 'friendly_name': 'Mock Title Connection uptime', + }), + 'context': , + 'entity_id': 'sensor.mock_title_connection_uptime', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '2024-09-01T10:11:33+00:00', + }) +# --- +# name: test_sensor_setup[sensor.mock_title_download_throughput-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.mock_title_download_throughput', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Download throughput', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'kb_s_received', + 'unique_id': '1C:ED:6F:12:34:11-kb_s_received', + 'unit_of_measurement': , + }) +# --- +# name: test_sensor_setup[sensor.mock_title_download_throughput-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'data_rate', + 'friendly_name': 'Mock Title Download throughput', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.mock_title_download_throughput', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '67.6', + }) +# --- +# name: test_sensor_setup[sensor.mock_title_external_ip-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.mock_title_external_ip', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'External IP', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'external_ip', + 'unique_id': '1C:ED:6F:12:34:11-external_ip', + 'unit_of_measurement': None, + }) +# --- +# name: test_sensor_setup[sensor.mock_title_external_ip-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Mock Title External IP', + }), + 'context': , + 'entity_id': 'sensor.mock_title_external_ip', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '1.2.3.4', + }) +# --- +# name: test_sensor_setup[sensor.mock_title_external_ipv6-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.mock_title_external_ipv6', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'External IPv6', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'external_ipv6', + 'unique_id': '1C:ED:6F:12:34:11-external_ipv6', + 'unit_of_measurement': None, + }) +# --- +# name: test_sensor_setup[sensor.mock_title_external_ipv6-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Mock Title External IPv6', + }), + 'context': , + 'entity_id': 'sensor.mock_title_external_ipv6', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'fec0::1', + }) +# --- +# name: test_sensor_setup[sensor.mock_title_gb_received-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.mock_title_gb_received', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'GB received', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'gb_received', + 'unique_id': '1C:ED:6F:12:34:11-gb_received', + 'unit_of_measurement': , + }) +# --- +# name: test_sensor_setup[sensor.mock_title_gb_received-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'data_size', + 'friendly_name': 'Mock Title GB received', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.mock_title_gb_received', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '5.2', + }) +# --- +# name: test_sensor_setup[sensor.mock_title_gb_sent-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.mock_title_gb_sent', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'GB sent', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'gb_sent', + 'unique_id': '1C:ED:6F:12:34:11-gb_sent', + 'unit_of_measurement': , + }) +# --- +# name: test_sensor_setup[sensor.mock_title_gb_sent-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'data_size', + 'friendly_name': 'Mock Title GB sent', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.mock_title_gb_sent', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '1.7', + }) +# --- +# name: test_sensor_setup[sensor.mock_title_last_restart-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.mock_title_last_restart', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Last restart', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'device_uptime', + 'unique_id': '1C:ED:6F:12:34:11-device_uptime', + 'unit_of_measurement': None, + }) +# --- +# name: test_sensor_setup[sensor.mock_title_last_restart-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'timestamp', + 'friendly_name': 'Mock Title Last restart', + }), + 'context': , + 'entity_id': 'sensor.mock_title_last_restart', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '2024-08-03T16:30:21+00:00', + }) +# --- +# name: test_sensor_setup[sensor.mock_title_link_download_noise_margin-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.mock_title_link_download_noise_margin', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Link download noise margin', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'link_noise_margin_received', + 'unique_id': '1C:ED:6F:12:34:11-link_noise_margin_received', + 'unit_of_measurement': 'dB', + }) +# --- +# name: test_sensor_setup[sensor.mock_title_link_download_noise_margin-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Mock Title Link download noise margin', + 'unit_of_measurement': 'dB', + }), + 'context': , + 'entity_id': 'sensor.mock_title_link_download_noise_margin', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '8.0', + }) +# --- +# name: test_sensor_setup[sensor.mock_title_link_download_power_attenuation-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.mock_title_link_download_power_attenuation', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Link download power attenuation', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'link_attenuation_received', + 'unique_id': '1C:ED:6F:12:34:11-link_attenuation_received', + 'unit_of_measurement': 'dB', + }) +# --- +# name: test_sensor_setup[sensor.mock_title_link_download_power_attenuation-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Mock Title Link download power attenuation', + 'unit_of_measurement': 'dB', + }), + 'context': , + 'entity_id': 'sensor.mock_title_link_download_power_attenuation', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '12.0', + }) +# --- +# name: test_sensor_setup[sensor.mock_title_link_download_throughput-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.mock_title_link_download_throughput', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Link download throughput', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'link_kb_s_received', + 'unique_id': '1C:ED:6F:12:34:11-link_kb_s_received', + 'unit_of_measurement': , + }) +# --- +# name: test_sensor_setup[sensor.mock_title_link_download_throughput-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'data_rate', + 'friendly_name': 'Mock Title Link download throughput', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.mock_title_link_download_throughput', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '318557.0', + }) +# --- +# name: test_sensor_setup[sensor.mock_title_link_upload_noise_margin-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.mock_title_link_upload_noise_margin', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Link upload noise margin', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'link_noise_margin_sent', + 'unique_id': '1C:ED:6F:12:34:11-link_noise_margin_sent', + 'unit_of_measurement': 'dB', + }) +# --- +# name: test_sensor_setup[sensor.mock_title_link_upload_noise_margin-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Mock Title Link upload noise margin', + 'unit_of_measurement': 'dB', + }), + 'context': , + 'entity_id': 'sensor.mock_title_link_upload_noise_margin', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '9.0', + }) +# --- +# name: test_sensor_setup[sensor.mock_title_link_upload_power_attenuation-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.mock_title_link_upload_power_attenuation', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Link upload power attenuation', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'link_attenuation_sent', + 'unique_id': '1C:ED:6F:12:34:11-link_attenuation_sent', + 'unit_of_measurement': 'dB', + }) +# --- +# name: test_sensor_setup[sensor.mock_title_link_upload_power_attenuation-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Mock Title Link upload power attenuation', + 'unit_of_measurement': 'dB', + }), + 'context': , + 'entity_id': 'sensor.mock_title_link_upload_power_attenuation', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '7.0', + }) +# --- +# name: test_sensor_setup[sensor.mock_title_link_upload_throughput-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.mock_title_link_upload_throughput', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Link upload throughput', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'link_kb_s_sent', + 'unique_id': '1C:ED:6F:12:34:11-link_kb_s_sent', + 'unit_of_measurement': , + }) +# --- +# name: test_sensor_setup[sensor.mock_title_link_upload_throughput-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'data_rate', + 'friendly_name': 'Mock Title Link upload throughput', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.mock_title_link_upload_throughput', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '51805.0', + }) +# --- +# name: test_sensor_setup[sensor.mock_title_max_connection_download_throughput-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.mock_title_max_connection_download_throughput', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Max connection download throughput', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'max_kb_s_received', + 'unique_id': '1C:ED:6F:12:34:11-max_kb_s_received', + 'unit_of_measurement': , + }) +# --- +# name: test_sensor_setup[sensor.mock_title_max_connection_download_throughput-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'data_rate', + 'friendly_name': 'Mock Title Max connection download throughput', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.mock_title_max_connection_download_throughput', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '10087.0', + }) +# --- +# name: test_sensor_setup[sensor.mock_title_max_connection_upload_throughput-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.mock_title_max_connection_upload_throughput', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Max connection upload throughput', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'max_kb_s_sent', + 'unique_id': '1C:ED:6F:12:34:11-max_kb_s_sent', + 'unit_of_measurement': , + }) +# --- +# name: test_sensor_setup[sensor.mock_title_max_connection_upload_throughput-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'data_rate', + 'friendly_name': 'Mock Title Max connection upload throughput', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.mock_title_max_connection_upload_throughput', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '2105.0', + }) +# --- +# name: test_sensor_setup[sensor.mock_title_upload_throughput-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.mock_title_upload_throughput', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Upload throughput', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'kb_s_sent', + 'unique_id': '1C:ED:6F:12:34:11-kb_s_sent', + 'unit_of_measurement': , + }) +# --- +# name: test_sensor_setup[sensor.mock_title_upload_throughput-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'data_rate', + 'friendly_name': 'Mock Title Upload throughput', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.mock_title_upload_throughput', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '3.4', + }) +# --- diff --git a/tests/components/fritz/snapshots/test_switch.ambr b/tests/components/fritz/snapshots/test_switch.ambr new file mode 100644 index 00000000000000..048f6e005ecb39 --- /dev/null +++ b/tests/components/fritz/snapshots/test_switch.ambr @@ -0,0 +1,424 @@ +# serializer version: 1 +# name: test_switch_setup[fc_data0-expected_wifi_names0][switch.mock_title_wi_fi_wifi_2_4ghz-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': , + 'entity_id': 'switch.mock_title_wi_fi_wifi_2_4ghz', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': 'mdi:wifi', + 'original_name': 'Mock Title Wi-Fi WiFi (2.4Ghz)', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '1C:ED:6F:12:34:11-wi_fi_wifi_2_4ghz', + 'unit_of_measurement': None, + }) +# --- +# name: test_switch_setup[fc_data0-expected_wifi_names0][switch.mock_title_wi_fi_wifi_2_4ghz-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Mock Title Wi-Fi WiFi (2.4Ghz)', + 'icon': 'mdi:wifi', + }), + 'context': , + 'entity_id': 'switch.mock_title_wi_fi_wifi_2_4ghz', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_switch_setup[fc_data0-expected_wifi_names0][switch.mock_title_wi_fi_wifi_5ghz-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': , + 'entity_id': 'switch.mock_title_wi_fi_wifi_5ghz', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': 'mdi:wifi', + 'original_name': 'Mock Title Wi-Fi WiFi (5Ghz)', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '1C:ED:6F:12:34:11-wi_fi_wifi_5ghz', + 'unit_of_measurement': None, + }) +# --- +# name: test_switch_setup[fc_data0-expected_wifi_names0][switch.mock_title_wi_fi_wifi_5ghz-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Mock Title Wi-Fi WiFi (5Ghz)', + 'icon': 'mdi:wifi', + }), + 'context': , + 'entity_id': 'switch.mock_title_wi_fi_wifi_5ghz', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_switch_setup[fc_data0-expected_wifi_names0][switch.printer_internet_access-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': , + 'entity_id': 'switch.printer_internet_access', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': 'mdi:router-wireless-settings', + 'original_name': 'printer Internet Access', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': 'AA:BB:CC:00:11:22_internet_access', + 'unit_of_measurement': None, + }) +# --- +# name: test_switch_setup[fc_data0-expected_wifi_names0][switch.printer_internet_access-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'printer Internet Access', + 'icon': 'mdi:router-wireless-settings', + }), + 'context': , + 'entity_id': 'switch.printer_internet_access', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_switch_setup[fc_data1-expected_wifi_names1][switch.mock_title_wi_fi_wifi-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': , + 'entity_id': 'switch.mock_title_wi_fi_wifi', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': 'mdi:wifi', + 'original_name': 'Mock Title Wi-Fi WiFi', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '1C:ED:6F:12:34:11-wi_fi_wifi', + 'unit_of_measurement': None, + }) +# --- +# name: test_switch_setup[fc_data1-expected_wifi_names1][switch.mock_title_wi_fi_wifi-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Mock Title Wi-Fi WiFi', + 'icon': 'mdi:wifi', + }), + 'context': , + 'entity_id': 'switch.mock_title_wi_fi_wifi', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_switch_setup[fc_data1-expected_wifi_names1][switch.mock_title_wi_fi_wifi2-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': , + 'entity_id': 'switch.mock_title_wi_fi_wifi2', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': 'mdi:wifi', + 'original_name': 'Mock Title Wi-Fi WiFi2', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '1C:ED:6F:12:34:11-wi_fi_wifi2', + 'unit_of_measurement': None, + }) +# --- +# name: test_switch_setup[fc_data1-expected_wifi_names1][switch.mock_title_wi_fi_wifi2-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Mock Title Wi-Fi WiFi2', + 'icon': 'mdi:wifi', + }), + 'context': , + 'entity_id': 'switch.mock_title_wi_fi_wifi2', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_switch_setup[fc_data1-expected_wifi_names1][switch.printer_internet_access-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': , + 'entity_id': 'switch.printer_internet_access', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': 'mdi:router-wireless-settings', + 'original_name': 'printer Internet Access', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': 'AA:BB:CC:00:11:22_internet_access', + 'unit_of_measurement': None, + }) +# --- +# name: test_switch_setup[fc_data1-expected_wifi_names1][switch.printer_internet_access-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'printer Internet Access', + 'icon': 'mdi:router-wireless-settings', + }), + 'context': , + 'entity_id': 'switch.printer_internet_access', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_switch_setup[fc_data2-expected_wifi_names2][switch.mock_title_wi_fi_wifi_2_4ghz-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': , + 'entity_id': 'switch.mock_title_wi_fi_wifi_2_4ghz', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': 'mdi:wifi', + 'original_name': 'Mock Title Wi-Fi WiFi (2.4Ghz)', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '1C:ED:6F:12:34:11-wi_fi_wifi_2_4ghz', + 'unit_of_measurement': None, + }) +# --- +# name: test_switch_setup[fc_data2-expected_wifi_names2][switch.mock_title_wi_fi_wifi_2_4ghz-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Mock Title Wi-Fi WiFi (2.4Ghz)', + 'icon': 'mdi:wifi', + }), + 'context': , + 'entity_id': 'switch.mock_title_wi_fi_wifi_2_4ghz', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_switch_setup[fc_data2-expected_wifi_names2][switch.mock_title_wi_fi_wifi_5ghz-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': , + 'entity_id': 'switch.mock_title_wi_fi_wifi_5ghz', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': 'mdi:wifi', + 'original_name': 'Mock Title Wi-Fi WiFi+ (5Ghz)', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '1C:ED:6F:12:34:11-wi_fi_wifi_5ghz', + 'unit_of_measurement': None, + }) +# --- +# name: test_switch_setup[fc_data2-expected_wifi_names2][switch.mock_title_wi_fi_wifi_5ghz-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Mock Title Wi-Fi WiFi+ (5Ghz)', + 'icon': 'mdi:wifi', + }), + 'context': , + 'entity_id': 'switch.mock_title_wi_fi_wifi_5ghz', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_switch_setup[fc_data2-expected_wifi_names2][switch.printer_internet_access-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': , + 'entity_id': 'switch.printer_internet_access', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': 'mdi:router-wireless-settings', + 'original_name': 'printer Internet Access', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': 'AA:BB:CC:00:11:22_internet_access', + 'unit_of_measurement': None, + }) +# --- +# name: test_switch_setup[fc_data2-expected_wifi_names2][switch.printer_internet_access-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'printer Internet Access', + 'icon': 'mdi:router-wireless-settings', + }), + 'context': , + 'entity_id': 'switch.printer_internet_access', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- diff --git a/tests/components/fritz/snapshots/test_update.ambr b/tests/components/fritz/snapshots/test_update.ambr new file mode 100644 index 00000000000000..5544c97249988d --- /dev/null +++ b/tests/components/fritz/snapshots/test_update.ambr @@ -0,0 +1,169 @@ +# serializer version: 1 +# name: test_available_update_can_be_installed[update.mock_title_fritz_os-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'update', + 'entity_category': , + 'entity_id': 'update.mock_title_fritz_os', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'FRITZ!OS', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': None, + 'unique_id': '1C:ED:6F:12:34:11-update', + 'unit_of_measurement': None, + }) +# --- +# name: test_available_update_can_be_installed[update.mock_title_fritz_os-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'auto_update': False, + 'entity_picture': 'https://brands.home-assistant.io/_/fritz/icon.png', + 'friendly_name': 'Mock Title FRITZ!OS', + 'in_progress': False, + 'installed_version': '7.29', + 'latest_version': '7.50', + 'release_summary': None, + 'release_url': 'http://download.avm.de/fritzbox/fritzbox-7530-ax/deutschland/fritz.os/info_de.txt', + 'skipped_version': None, + 'supported_features': , + 'title': 'FRITZ!OS', + }), + 'context': , + 'entity_id': 'update.mock_title_fritz_os', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_update_available[update.mock_title_fritz_os-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'update', + 'entity_category': , + 'entity_id': 'update.mock_title_fritz_os', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'FRITZ!OS', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': None, + 'unique_id': '1C:ED:6F:12:34:11-update', + 'unit_of_measurement': None, + }) +# --- +# name: test_update_available[update.mock_title_fritz_os-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'auto_update': False, + 'entity_picture': 'https://brands.home-assistant.io/_/fritz/icon.png', + 'friendly_name': 'Mock Title FRITZ!OS', + 'in_progress': False, + 'installed_version': '7.29', + 'latest_version': '7.50', + 'release_summary': None, + 'release_url': 'http://download.avm.de/fritzbox/fritzbox-7530-ax/deutschland/fritz.os/info_de.txt', + 'skipped_version': None, + 'supported_features': , + 'title': 'FRITZ!OS', + }), + 'context': , + 'entity_id': 'update.mock_title_fritz_os', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_update_entities_initialized[update.mock_title_fritz_os-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'update', + 'entity_category': , + 'entity_id': 'update.mock_title_fritz_os', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'FRITZ!OS', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': None, + 'unique_id': '1C:ED:6F:12:34:11-update', + 'unit_of_measurement': None, + }) +# --- +# name: test_update_entities_initialized[update.mock_title_fritz_os-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'auto_update': False, + 'entity_picture': 'https://brands.home-assistant.io/_/fritz/icon.png', + 'friendly_name': 'Mock Title FRITZ!OS', + 'in_progress': False, + 'installed_version': '7.29', + 'latest_version': '7.29', + 'release_summary': None, + 'release_url': None, + 'skipped_version': None, + 'supported_features': , + 'title': 'FRITZ!OS', + }), + 'context': , + 'entity_id': 'update.mock_title_fritz_os', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- diff --git a/tests/components/fritz/test_button.py b/tests/components/fritz/test_button.py index 79639835003e2e..507331cde0b917 100644 --- a/tests/components/fritz/test_button.py +++ b/tests/components/fritz/test_button.py @@ -5,11 +5,12 @@ from unittest.mock import patch import pytest +from syrupy.assertion import SnapshotAssertion from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS from homeassistant.components.fritz.const import DOMAIN, MeshRoles from homeassistant.config_entries import ConfigEntryState -from homeassistant.const import ATTR_ENTITY_ID, STATE_UNKNOWN +from homeassistant.const import ATTR_ENTITY_ID, STATE_UNKNOWN, Platform from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.util.dt import utcnow @@ -21,24 +22,30 @@ MOCK_USER_DATA, ) -from tests.common import MockConfigEntry, async_fire_time_changed +from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform -async def test_button_setup(hass: HomeAssistant, fc_class_mock, fh_class_mock) -> None: +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_button_setup( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + fc_class_mock, + fh_class_mock, + snapshot: SnapshotAssertion, +) -> None: """Test setup of Fritz!Tools buttons.""" entry = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_DATA) entry.add_to_hass(hass) - await hass.config_entries.async_setup(entry.entry_id) - await hass.async_block_till_done() - assert entry.state is ConfigEntryState.LOADED + with patch("homeassistant.components.fritz.PLATFORMS", [Platform.BUTTON]): + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() - buttons = hass.states.async_all(BUTTON_DOMAIN) - assert len(buttons) == 4 + states = hass.states.async_all() + assert len(states) == 5 - for button in buttons: - assert button.state == STATE_UNKNOWN + await snapshot_platform(hass, entity_registry, snapshot, entry.entry_id) @pytest.mark.parametrize( diff --git a/tests/components/fritz/test_config_flow.py b/tests/components/fritz/test_config_flow.py index a54acbb0ac0130..deefe7e4e778be 100644 --- a/tests/components/fritz/test_config_flow.py +++ b/tests/components/fritz/test_config_flow.py @@ -23,12 +23,7 @@ FRITZ_AUTH_EXCEPTIONS, ) from homeassistant.components.ssdp import ATTR_UPNP_UDN -from homeassistant.config_entries import ( - SOURCE_REAUTH, - SOURCE_RECONFIGURE, - SOURCE_SSDP, - SOURCE_USER, -) +from homeassistant.config_entries import SOURCE_RECONFIGURE, SOURCE_SSDP, SOURCE_USER from homeassistant.const import ( CONF_HOST, CONF_PASSWORD, @@ -310,6 +305,9 @@ async def test_reauth_successful( mock_config = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_DATA) mock_config.add_to_hass(hass) + result = await mock_config.start_reauth_flow(hass) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reauth_confirm" with ( patch( @@ -335,15 +333,6 @@ async def test_reauth_successful( mock_request_post.return_value.status_code = 200 mock_request_post.return_value.text = MOCK_REQUEST - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH, "entry_id": mock_config.entry_id}, - data=mock_config.data, - ) - - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "reauth_confirm" - result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={ @@ -376,20 +365,14 @@ async def test_reauth_not_successful( mock_config = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_DATA) mock_config.add_to_hass(hass) + result = await mock_config.start_reauth_flow(hass) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reauth_confirm" with patch( "homeassistant.components.fritz.config_flow.FritzConnection", side_effect=side_effect, ): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH, "entry_id": mock_config.entry_id}, - data=mock_config.data, - ) - - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "reauth_confirm" - result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={ diff --git a/tests/components/fritz/test_diagnostics.py b/tests/components/fritz/test_diagnostics.py index 55196eb6988561..cbcaa57dab4c24 100644 --- a/tests/components/fritz/test_diagnostics.py +++ b/tests/components/fritz/test_diagnostics.py @@ -2,14 +2,13 @@ from __future__ import annotations -from homeassistant.components.diagnostics import REDACTED +from syrupy import SnapshotAssertion +from syrupy.filters import props + from homeassistant.components.fritz.const import DOMAIN -from homeassistant.components.fritz.coordinator import AvmWrapper -from homeassistant.components.fritz.diagnostics import TO_REDACT -from homeassistant.config_entries import ConfigEntryState from homeassistant.core import HomeAssistant -from .const import MOCK_MESH_MASTER_MAC, MOCK_USER_DATA +from .const import MOCK_USER_DATA from tests.common import MockConfigEntry from tests.components.diagnostics import get_diagnostics_for_config_entry @@ -21,64 +20,16 @@ async def test_entry_diagnostics( hass_client: ClientSessionGenerator, fc_class_mock, fh_class_mock, + snapshot: SnapshotAssertion, ) -> None: """Test config entry diagnostics.""" entry = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_DATA) entry.add_to_hass(hass) - await hass.config_entries.async_setup(entry.entry_id) + assert await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() - assert entry.state is ConfigEntryState.LOADED - entry_dict = entry.as_dict() - for key in TO_REDACT: - entry_dict["data"][key] = REDACTED result = await get_diagnostics_for_config_entry(hass, hass_client, entry) - avm_wrapper: AvmWrapper = hass.data[DOMAIN][entry.entry_id] - assert result == { - "entry": entry_dict, - "device_info": { - "client_devices": [ - { - "connected_to": device.connected_to, - "connection_type": device.connection_type, - "hostname": device.hostname, - "is_connected": device.is_connected, - "last_activity": device.last_activity.isoformat(), - "wan_access": device.wan_access, - } - for _, device in avm_wrapper.devices.items() - ], - "connection_type": "WANPPPConnection", - "current_firmware": "7.29", - "discovered_services": [ - "DeviceInfo1", - "Hosts1", - "LANEthernetInterfaceConfig1", - "Layer3Forwarding1", - "UserInterface1", - "WANCommonIFC1", - "WANCommonInterfaceConfig1", - "WANDSLInterfaceConfig1", - "WANIPConn1", - "WANPPPConnection1", - "WLANConfiguration1", - "X_AVM-DE_Homeauto1", - "X_AVM-DE_HostFilter1", - ], - "is_router": True, - "last_exception": None, - "last_update success": True, - "latest_firmware": None, - "mesh_role": "master", - "model": "FRITZ!Box 7530 AX", - "unique_id": MOCK_MESH_MASTER_MAC.replace("6F:12", "XX:XX"), - "update_available": False, - "wan_link_properties": { - "NewLayer1DownstreamMaxBitRate": 318557000, - "NewLayer1UpstreamMaxBitRate": 51805000, - "NewPhysicalLinkStatus": "Up", - "NewWANAccessType": "DSL", - }, - }, - } + assert result == snapshot( + exclude=props("created_at", "modified_at", "entry_id", "last_activity") + ) diff --git a/tests/components/fritz/test_sensor.py b/tests/components/fritz/test_sensor.py index f8114238376cca..fcdb4b63450e97 100644 --- a/tests/components/fritz/test_sensor.py +++ b/tests/components/fritz/test_sensor.py @@ -2,123 +2,47 @@ from __future__ import annotations -from datetime import timedelta -from typing import Any +from datetime import UTC, datetime, timedelta +from unittest.mock import patch from fritzconnection.core.exceptions import FritzConnectionException +import pytest +from syrupy.assertion import SnapshotAssertion from homeassistant.components.fritz.const import DOMAIN -from homeassistant.components.fritz.sensor import SENSOR_TYPES -from homeassistant.components.sensor import ( - ATTR_STATE_CLASS, - DOMAIN as SENSOR_DOMAIN, - SensorDeviceClass, - SensorStateClass, -) -from homeassistant.config_entries import ConfigEntryState -from homeassistant.const import ( - ATTR_DEVICE_CLASS, - ATTR_STATE, - ATTR_UNIT_OF_MEASUREMENT, - STATE_UNAVAILABLE, -) +from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN +from homeassistant.const import STATE_UNAVAILABLE, Platform from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er import homeassistant.util.dt as dt_util from .const import MOCK_USER_DATA -from tests.common import MockConfigEntry, async_fire_time_changed - -SENSOR_STATES: dict[str, dict[str, Any]] = { - "sensor.mock_title_external_ip": { - ATTR_STATE: "1.2.3.4", - }, - "sensor.mock_title_external_ipv6": { - ATTR_STATE: "fec0::1", - }, - "sensor.mock_title_last_restart": { - # ATTR_STATE: "2022-02-05T17:46:04+00:00", - ATTR_DEVICE_CLASS: SensorDeviceClass.TIMESTAMP, - }, - "sensor.mock_title_connection_uptime": { - # ATTR_STATE: "2022-03-06T11:27:16+00:00", - ATTR_DEVICE_CLASS: SensorDeviceClass.TIMESTAMP, - }, - "sensor.mock_title_upload_throughput": { - ATTR_STATE: "3.4", - ATTR_STATE_CLASS: SensorStateClass.MEASUREMENT, - ATTR_UNIT_OF_MEASUREMENT: "kB/s", - }, - "sensor.mock_title_download_throughput": { - ATTR_STATE: "67.6", - ATTR_STATE_CLASS: SensorStateClass.MEASUREMENT, - ATTR_UNIT_OF_MEASUREMENT: "kB/s", - }, - "sensor.mock_title_max_connection_upload_throughput": { - ATTR_STATE: "2105.0", - ATTR_UNIT_OF_MEASUREMENT: "kbit/s", - }, - "sensor.mock_title_max_connection_download_throughput": { - ATTR_STATE: "10087.0", - ATTR_UNIT_OF_MEASUREMENT: "kbit/s", - }, - "sensor.mock_title_gb_sent": { - ATTR_STATE: "1.7", - ATTR_STATE_CLASS: SensorStateClass.TOTAL_INCREASING, - ATTR_UNIT_OF_MEASUREMENT: "GB", - }, - "sensor.mock_title_gb_received": { - ATTR_STATE: "5.2", - ATTR_STATE_CLASS: SensorStateClass.TOTAL_INCREASING, - ATTR_UNIT_OF_MEASUREMENT: "GB", - }, - "sensor.mock_title_link_upload_throughput": { - ATTR_STATE: "51805.0", - ATTR_UNIT_OF_MEASUREMENT: "kbit/s", - }, - "sensor.mock_title_link_download_throughput": { - ATTR_STATE: "318557.0", - ATTR_UNIT_OF_MEASUREMENT: "kbit/s", - }, - "sensor.mock_title_link_upload_noise_margin": { - ATTR_STATE: "9.0", - ATTR_UNIT_OF_MEASUREMENT: "dB", - }, - "sensor.mock_title_link_download_noise_margin": { - ATTR_STATE: "8.0", - ATTR_UNIT_OF_MEASUREMENT: "dB", - }, - "sensor.mock_title_link_upload_power_attenuation": { - ATTR_STATE: "7.0", - ATTR_UNIT_OF_MEASUREMENT: "dB", - }, - "sensor.mock_title_link_download_power_attenuation": { - ATTR_STATE: "12.0", - ATTR_UNIT_OF_MEASUREMENT: "dB", - }, -} - - -async def test_sensor_setup(hass: HomeAssistant, fc_class_mock, fh_class_mock) -> None: +from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform + + +@pytest.mark.freeze_time(datetime(2024, 9, 1, 20, tzinfo=UTC)) +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_sensor_setup( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + fc_class_mock, + fh_class_mock, + snapshot: SnapshotAssertion, +) -> None: """Test setup of Fritz!Tools sensors.""" entry = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_DATA) entry.add_to_hass(hass) - await hass.config_entries.async_setup(entry.entry_id) - await hass.async_block_till_done() - assert entry.state is ConfigEntryState.LOADED + with patch("homeassistant.components.fritz.PLATFORMS", [Platform.SENSOR]): + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() - sensors = hass.states.async_all(SENSOR_DOMAIN) - assert len(sensors) == len(SENSOR_TYPES) + states = hass.states.async_all() + assert len(states) == 16 - for sensor in sensors: - assert SENSOR_STATES.get(sensor.entity_id) is not None - for key, val in SENSOR_STATES[sensor.entity_id].items(): - if key == ATTR_STATE: - assert sensor.state == val - else: - assert sensor.attributes.get(key) == val + await snapshot_platform(hass, entity_registry, snapshot, entry.entry_id) async def test_sensor_update_fail( diff --git a/tests/components/fritz/test_switch.py b/tests/components/fritz/test_switch.py index b82587d42bd09c..1542645758eb52 100644 --- a/tests/components/fritz/test_switch.py +++ b/tests/components/fritz/test_switch.py @@ -2,16 +2,19 @@ from __future__ import annotations +from unittest.mock import patch + import pytest +from syrupy.assertion import SnapshotAssertion from homeassistant.components.fritz.const import DOMAIN -from homeassistant.config_entries import ConfigEntryState from homeassistant.const import Platform from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er from .const import MOCK_FB_SERVICES, MOCK_USER_DATA -from tests.common import MockConfigEntry +from tests.common import MockConfigEntry, snapshot_platform MOCK_WLANCONFIGS_SAME_SSID: dict[str, dict] = { "WLANConfiguration1": { @@ -179,23 +182,24 @@ ), ], ) +@pytest.mark.usefixtures("entity_registry_enabled_by_default") async def test_switch_setup( hass: HomeAssistant, + entity_registry: er.EntityRegistry, expected_wifi_names: list[str], fc_class_mock, fh_class_mock, + snapshot: SnapshotAssertion, ) -> None: """Test setup of Fritz!Tools switches.""" - entry = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_DATA) entry.add_to_hass(hass) - await hass.config_entries.async_setup(entry.entry_id) - await hass.async_block_till_done(wait_background_tasks=True) - assert entry.state is ConfigEntryState.LOADED + with patch("homeassistant.components.fritz.PLATFORMS", [Platform.SWITCH]): + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done(wait_background_tasks=True) + + states = hass.states.async_all() + assert len(states) == 3 - switches = hass.states.async_all(Platform.SWITCH) - assert len(switches) == 3 - assert switches[0].name == f"Mock Title Wi-Fi {expected_wifi_names[0]}" - assert switches[1].name == f"Mock Title Wi-Fi {expected_wifi_names[1]}" - assert switches[2].name == "printer Internet Access" + await snapshot_platform(hass, entity_registry, snapshot, entry.entry_id) diff --git a/tests/components/fritz/test_update.py b/tests/components/fritz/test_update.py index 5d7ef852d4c508..cca5decbcc49ad 100644 --- a/tests/components/fritz/test_update.py +++ b/tests/components/fritz/test_update.py @@ -2,10 +2,13 @@ from unittest.mock import patch +import pytest +from syrupy.assertion import SnapshotAssertion + from homeassistant.components.fritz.const import DOMAIN -from homeassistant.components.update import DOMAIN as UPDATE_DOMAIN -from homeassistant.config_entries import ConfigEntryState +from homeassistant.const import Platform from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er from .const import ( MOCK_FB_SERVICES, @@ -14,8 +17,7 @@ MOCK_USER_DATA, ) -from tests.common import MockConfigEntry -from tests.typing import ClientSessionGenerator +from tests.common import MockConfigEntry, snapshot_platform AVAILABLE_UPDATE = { "UserInterface1": { @@ -27,30 +29,36 @@ } +@pytest.mark.usefixtures("entity_registry_enabled_by_default") async def test_update_entities_initialized( hass: HomeAssistant, - hass_client: ClientSessionGenerator, + entity_registry: er.EntityRegistry, fc_class_mock, fh_class_mock, + snapshot: SnapshotAssertion, ) -> None: """Test update entities.""" entry = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_DATA) entry.add_to_hass(hass) - await hass.config_entries.async_setup(entry.entry_id) - await hass.async_block_till_done() - assert entry.state is ConfigEntryState.LOADED + with patch("homeassistant.components.fritz.PLATFORMS", [Platform.UPDATE]): + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + states = hass.states.async_all() + assert len(states) == 1 - updates = hass.states.async_all(UPDATE_DOMAIN) - assert len(updates) == 1 + await snapshot_platform(hass, entity_registry, snapshot, entry.entry_id) +@pytest.mark.usefixtures("entity_registry_enabled_by_default") async def test_update_available( hass: HomeAssistant, - hass_client: ClientSessionGenerator, + entity_registry: er.EntityRegistry, fc_class_mock, fh_class_mock, + snapshot: SnapshotAssertion, ) -> None: """Test update entities.""" @@ -59,64 +67,45 @@ async def test_update_available( entry = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_DATA) entry.add_to_hass(hass) - await hass.config_entries.async_setup(entry.entry_id) - await hass.async_block_till_done() - assert entry.state is ConfigEntryState.LOADED - - update = hass.states.get("update.mock_title_fritz_os") - assert update is not None - assert update.state == "on" - assert update.attributes.get("installed_version") == "7.29" - assert update.attributes.get("latest_version") == MOCK_FIRMWARE_AVAILABLE - assert update.attributes.get("release_url") == MOCK_FIRMWARE_RELEASE_URL - - -async def test_no_update_available( - hass: HomeAssistant, - hass_client: ClientSessionGenerator, - fc_class_mock, - fh_class_mock, -) -> None: - """Test update entities.""" - - entry = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_DATA) - entry.add_to_hass(hass) + with patch("homeassistant.components.fritz.PLATFORMS", [Platform.UPDATE]): + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() - await hass.config_entries.async_setup(entry.entry_id) - await hass.async_block_till_done() - assert entry.state is ConfigEntryState.LOADED + states = hass.states.async_all() + assert len(states) == 1 - update = hass.states.get("update.mock_title_fritz_os") - assert update is not None - assert update.state == "off" - assert update.attributes.get("installed_version") == "7.29" - assert update.attributes.get("latest_version") == "7.29" + await snapshot_platform(hass, entity_registry, snapshot, entry.entry_id) +@pytest.mark.usefixtures("entity_registry_enabled_by_default") async def test_available_update_can_be_installed( hass: HomeAssistant, - hass_client: ClientSessionGenerator, + entity_registry: er.EntityRegistry, fc_class_mock, fh_class_mock, + snapshot: SnapshotAssertion, ) -> None: """Test update entities.""" fc_class_mock().override_services({**MOCK_FB_SERVICES, **AVAILABLE_UPDATE}) - with patch( - "homeassistant.components.fritz.coordinator.FritzBoxTools.async_trigger_firmware_update", - return_value=True, - ) as mocked_update_call: + with ( + patch( + "homeassistant.components.fritz.coordinator.FritzBoxTools.async_trigger_firmware_update", + return_value=True, + ) as mocked_update_call, + patch("homeassistant.components.fritz.PLATFORMS", [Platform.UPDATE]), + ): entry = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_DATA) entry.add_to_hass(hass) - await hass.config_entries.async_setup(entry.entry_id) + assert await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() - assert entry.state is ConfigEntryState.LOADED - update = hass.states.get("update.mock_title_fritz_os") - assert update is not None - assert update.state == "on" + states = hass.states.async_all() + assert len(states) == 1 + + await snapshot_platform(hass, entity_registry, snapshot, entry.entry_id) await hass.services.async_call( "update", diff --git a/tests/components/fritzbox/test_config_flow.py b/tests/components/fritzbox/test_config_flow.py index 72d36a8ab63a16..fd53bd2e63778b 100644 --- a/tests/components/fritzbox/test_config_flow.py +++ b/tests/components/fritzbox/test_config_flow.py @@ -12,12 +12,7 @@ from homeassistant.components import ssdp from homeassistant.components.fritzbox.const import DOMAIN from homeassistant.components.ssdp import ATTR_UPNP_FRIENDLY_NAME, ATTR_UPNP_UDN -from homeassistant.config_entries import ( - SOURCE_REAUTH, - SOURCE_RECONFIGURE, - SOURCE_SSDP, - SOURCE_USER, -) +from homeassistant.config_entries import SOURCE_RECONFIGURE, SOURCE_SSDP, SOURCE_USER from homeassistant.const import CONF_DEVICES, CONF_HOST, CONF_PASSWORD, CONF_USERNAME from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -129,12 +124,7 @@ async def test_reauth_success(hass: HomeAssistant, fritz: Mock) -> None: """Test starting a reauthentication flow.""" mock_config = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_DATA) mock_config.add_to_hass(hass) - - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH, "entry_id": mock_config.entry_id}, - data=mock_config.data, - ) + result = await mock_config.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -158,12 +148,7 @@ async def test_reauth_auth_failed(hass: HomeAssistant, fritz: Mock) -> None: mock_config = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_DATA) mock_config.add_to_hass(hass) - - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH, "entry_id": mock_config.entry_id}, - data=mock_config.data, - ) + result = await mock_config.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -186,12 +171,7 @@ async def test_reauth_not_successful(hass: HomeAssistant, fritz: Mock) -> None: mock_config = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_DATA) mock_config.add_to_hass(hass) - - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH, "entry_id": mock_config.entry_id}, - data=mock_config.data, - ) + result = await mock_config.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/frontier_silicon/test_config_flow.py b/tests/components/frontier_silicon/test_config_flow.py index 04bd1febdf8f71..a6c1ba1e74ff8f 100644 --- a/tests/components/frontier_silicon/test_config_flow.py +++ b/tests/components/frontier_silicon/test_config_flow.py @@ -356,15 +356,7 @@ async def test_reauth_flow(hass: HomeAssistant, config_entry: MockConfigEntry) - config_entry.add_to_hass(hass) assert config_entry.data[CONF_PIN] == "1234" - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": config_entry.unique_id, - "entry_id": config_entry.entry_id, - }, - data=config_entry.data, - ) + result = await config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "device_config" @@ -395,15 +387,7 @@ async def test_reauth_flow_friendly_name_error( config_entry.add_to_hass(hass) assert config_entry.data[CONF_PIN] == "1234" - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": config_entry.unique_id, - "entry_id": config_entry.entry_id, - }, - data=config_entry.data, - ) + result = await config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "device_config" diff --git a/tests/components/fujitsu_fglair/conftest.py b/tests/components/fujitsu_fglair/conftest.py index b73007a566ba1a..04042fb0b09c0f 100644 --- a/tests/components/fujitsu_fglair/conftest.py +++ b/tests/components/fujitsu_fglair/conftest.py @@ -30,7 +30,7 @@ @pytest.fixture -def mock_setup_entry() -> Generator[AsyncMock, None, None]: +def mock_setup_entry() -> Generator[AsyncMock]: """Override async_setup_entry.""" with patch( "homeassistant.components.fujitsu_fglair.async_setup_entry", return_value=True diff --git a/tests/components/fujitsu_fglair/test_climate.py b/tests/components/fujitsu_fglair/test_climate.py index fd016e4e226b45..daddc83a871963 100644 --- a/tests/components/fujitsu_fglair/test_climate.py +++ b/tests/components/fujitsu_fglair/test_climate.py @@ -18,7 +18,7 @@ SWING_BOTH, HVACMode, ) -from homeassistant.components.fujitsu_fglair.const import ( +from homeassistant.components.fujitsu_fglair.climate import ( HA_TO_FUJI_FAN, HA_TO_FUJI_HVAC, HA_TO_FUJI_SWING, diff --git a/tests/components/fujitsu_fglair/test_config_flow.py b/tests/components/fujitsu_fglair/test_config_flow.py index fc6afd9b8f071a..2828cf953397c8 100644 --- a/tests/components/fujitsu_fglair/test_config_flow.py +++ b/tests/components/fujitsu_fglair/test_config_flow.py @@ -6,7 +6,7 @@ import pytest from homeassistant.components.fujitsu_fglair.const import CONF_EUROPE, DOMAIN -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_USER from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResult, FlowResultType @@ -116,20 +116,7 @@ async def test_reauth_success( """Test reauth flow.""" mock_config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "entry_id": mock_config_entry.entry_id, - "title_placeholders": {"name": "test"}, - }, - data={ - CONF_USERNAME: TEST_USERNAME, - CONF_PASSWORD: TEST_PASSWORD, - CONF_EUROPE: False, - }, - ) - + result = await mock_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -164,20 +151,7 @@ async def test_reauth_exceptions( """Test reauth flow when an exception occurs.""" mock_config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "entry_id": mock_config_entry.entry_id, - "title_placeholders": {"name": "test"}, - }, - data={ - CONF_USERNAME: TEST_USERNAME, - CONF_PASSWORD: TEST_PASSWORD, - CONF_EUROPE: False, - }, - ) - + result = await mock_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/fyta/test_config_flow.py b/tests/components/fyta/test_config_flow.py index df0626d0af05d0..e47b78aa893729 100644 --- a/tests/components/fyta/test_config_flow.py +++ b/tests/components/fyta/test_config_flow.py @@ -158,11 +158,7 @@ async def test_reauth( ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_REAUTH, "entry_id": entry.entry_id}, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/gdacs/snapshots/test_diagnostics.ambr b/tests/components/gdacs/snapshots/test_diagnostics.ambr new file mode 100644 index 00000000000000..5b6154307f7893 --- /dev/null +++ b/tests/components/gdacs/snapshots/test_diagnostics.ambr @@ -0,0 +1,21 @@ +# serializer version: 1 +# name: test_entry_diagnostics + dict({ + 'info': dict({ + 'categories': list([ + ]), + 'latitude': '**REDACTED**', + 'longitude': '**REDACTED**', + 'radius': 25, + 'scan_interval': 300.0, + 'unit_system': 'metric', + }), + 'service': dict({ + 'last_timestamp': None, + 'last_update': '2024-09-05T15:00:00', + 'last_update_successful': '2024-09-05T15:00:00', + 'status': 'OK', + 'total': 0, + }), + }) +# --- diff --git a/tests/components/gdacs/test_diagnostics.py b/tests/components/gdacs/test_diagnostics.py new file mode 100644 index 00000000000000..3c6cf4080a604e --- /dev/null +++ b/tests/components/gdacs/test_diagnostics.py @@ -0,0 +1,33 @@ +"""Test GDACS diagnostics.""" + +from __future__ import annotations + +from unittest.mock import patch + +import pytest +from syrupy import SnapshotAssertion + +from homeassistant.core import HomeAssistant + +from tests.common import MockConfigEntry +from tests.components.diagnostics import get_diagnostics_for_config_entry +from tests.typing import ClientSessionGenerator + + +@pytest.mark.freeze_time("2024-09-05 15:00:00") +async def test_entry_diagnostics( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + snapshot: SnapshotAssertion, + config_entry: MockConfigEntry, +) -> None: + """Test config entry diagnostics.""" + with patch("aio_georss_client.feed.GeoRssFeed.update") as mock_feed_update: + mock_feed_update.return_value = "OK", [] + + config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + result = await get_diagnostics_for_config_entry(hass, hass_client, config_entry) + assert result == snapshot diff --git a/tests/components/geocaching/test_config_flow.py b/tests/components/geocaching/test_config_flow.py index 0c2ce66b51323b..5db89de08680d1 100644 --- a/tests/components/geocaching/test_config_flow.py +++ b/tests/components/geocaching/test_config_flow.py @@ -14,7 +14,7 @@ ENVIRONMENT, ENVIRONMENT_URLS, ) -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_USER from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType from homeassistant.helpers import config_entry_oauth2_flow @@ -195,9 +195,7 @@ async def test_reauthentication( """Test Geocaching reauthentication.""" mock_config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_REAUTH} - ) + result = await mock_config_entry.start_reauth_flow(hass) flows = hass.config_entries.flow.async_progress() assert len(flows) == 1 diff --git a/tests/components/geonetnz_quakes/snapshots/test_diagnostics.ambr b/tests/components/geonetnz_quakes/snapshots/test_diagnostics.ambr new file mode 100644 index 00000000000000..481a662ccf9f2a --- /dev/null +++ b/tests/components/geonetnz_quakes/snapshots/test_diagnostics.ambr @@ -0,0 +1,21 @@ +# serializer version: 1 +# name: test_entry_diagnostics + dict({ + 'info': dict({ + 'latitude': '**REDACTED**', + 'longitude': '**REDACTED**', + 'minimum_magnitude': 0.0, + 'mmi': 4, + 'radius': 25, + 'scan_interval': 300.0, + 'unit_system': 'metric', + }), + 'service': dict({ + 'last_timestamp': None, + 'last_update': '2024-09-05T15:00:00', + 'last_update_successful': '2024-09-05T15:00:00', + 'status': 'OK', + 'total': 0, + }), + }) +# --- diff --git a/tests/components/geonetnz_quakes/test_diagnostics.py b/tests/components/geonetnz_quakes/test_diagnostics.py new file mode 100644 index 00000000000000..db5e1300768f55 --- /dev/null +++ b/tests/components/geonetnz_quakes/test_diagnostics.py @@ -0,0 +1,33 @@ +"""Test GeoNet NZ Quakes diagnostics.""" + +from __future__ import annotations + +from unittest.mock import patch + +import pytest +from syrupy import SnapshotAssertion + +from homeassistant.core import HomeAssistant + +from tests.common import MockConfigEntry +from tests.components.diagnostics import get_diagnostics_for_config_entry +from tests.typing import ClientSessionGenerator + + +@pytest.mark.freeze_time("2024-09-05 15:00:00") +async def test_entry_diagnostics( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + snapshot: SnapshotAssertion, + config_entry: MockConfigEntry, +) -> None: + """Test config entry diagnostics.""" + with patch("aio_geojson_client.feed.GeoJsonFeed.update") as mock_feed_update: + mock_feed_update.return_value = "OK", [] + + config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + result = await get_diagnostics_for_config_entry(hass, hass_client, config_entry) + assert result == snapshot diff --git a/tests/components/geonetnz_volcano/test_config_flow.py b/tests/components/geonetnz_volcano/test_config_flow.py index b074bdffa20a11..110fb3b0a9ea3e 100644 --- a/tests/components/geonetnz_volcano/test_config_flow.py +++ b/tests/components/geonetnz_volcano/test_config_flow.py @@ -3,7 +3,8 @@ from datetime import timedelta from unittest.mock import patch -from homeassistant.components.geonetnz_volcano import config_flow +from homeassistant.components.geonetnz_volcano import DOMAIN +from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER from homeassistant.const import ( CONF_LATITUDE, CONF_LONGITUDE, @@ -20,19 +21,18 @@ async def test_duplicate_error(hass: HomeAssistant, config_entry) -> None: conf = {CONF_LATITUDE: -41.2, CONF_LONGITUDE: 174.7, CONF_RADIUS: 25} config_entry.add_to_hass(hass) - flow = config_flow.GeonetnzVolcanoFlowHandler() - flow.hass = hass - result = await flow.async_step_user(user_input=conf) + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER}, data=conf + ) assert result["errors"] == {"base": "already_configured"} async def test_show_form(hass: HomeAssistant) -> None: """Test that the form is served with no input.""" - flow = config_flow.GeonetnzVolcanoFlowHandler() - flow.hass = hass - - result = await flow.async_step_user(user_input=None) + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER}, data=None + ) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "user" @@ -48,9 +48,6 @@ async def test_step_import(hass: HomeAssistant) -> None: CONF_SCAN_INTERVAL: timedelta(minutes=4), } - flow = config_flow.GeonetnzVolcanoFlowHandler() - flow.hass = hass - with ( patch( "homeassistant.components.geonetnz_volcano.async_setup_entry", @@ -60,7 +57,9 @@ async def test_step_import(hass: HomeAssistant) -> None: "homeassistant.components.geonetnz_volcano.async_setup", return_value=True ), ): - result = await flow.async_step_import(import_config=conf) + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_IMPORT}, data=conf + ) assert result["type"] is FlowResultType.CREATE_ENTRY assert result["title"] == "-41.2, 174.7" assert result["data"] == { @@ -78,9 +77,6 @@ async def test_step_user(hass: HomeAssistant) -> None: hass.config.longitude = 174.7 conf = {CONF_RADIUS: 25} - flow = config_flow.GeonetnzVolcanoFlowHandler() - flow.hass = hass - with ( patch( "homeassistant.components.geonetnz_volcano.async_setup_entry", @@ -90,7 +86,9 @@ async def test_step_user(hass: HomeAssistant) -> None: "homeassistant.components.geonetnz_volcano.async_setup", return_value=True ), ): - result = await flow.async_step_user(user_input=conf) + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER}, data=conf + ) assert result["type"] is FlowResultType.CREATE_ENTRY assert result["title"] == "-41.2, 174.7" assert result["data"] == { diff --git a/tests/components/glances/test_config_flow.py b/tests/components/glances/test_config_flow.py index a7d6934e32daea..0fabc387a4f633 100644 --- a/tests/components/glances/test_config_flow.py +++ b/tests/components/glances/test_config_flow.py @@ -89,15 +89,7 @@ async def test_reauth_success(hass: HomeAssistant) -> None: entry = MockConfigEntry(domain=glances.DOMAIN, data=MOCK_USER_INPUT) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - glances.DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - }, - data=MOCK_USER_INPUT, - ) - + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" assert result["description_placeholders"] == {"username": "username"} @@ -128,15 +120,7 @@ async def test_reauth_fails( entry.add_to_hass(hass) mock_api.return_value.get_ha_sensor_data.side_effect = [error, HA_SENSOR_DATA] - result = await hass.config_entries.flow.async_init( - glances.DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - }, - data=MOCK_USER_INPUT, - ) - + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" assert result["description_placeholders"] == {"username": "username"} diff --git a/tests/components/google/test_config_flow.py b/tests/components/google/test_config_flow.py index f4a6c97f50df8d..b7962921ffd5e0 100644 --- a/tests/components/google/test_config_flow.py +++ b/tests/components/google/test_config_flow.py @@ -497,14 +497,7 @@ async def test_reauth_flow( entries = hass.config_entries.async_entries(DOMAIN) assert len(entries) == 1 - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": config_entry.entry_id, - }, - data=config_entry.data, - ) + result = await config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -761,14 +754,7 @@ async def test_web_reauth_flow( entries = hass.config_entries.async_entries(DOMAIN) assert len(entries) == 1 - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": config_entry.entry_id, - }, - data=config_entry.data, - ) + result = await config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/google_cloud/__init__.py b/tests/components/google_cloud/__init__.py new file mode 100644 index 00000000000000..67e83b58c71352 --- /dev/null +++ b/tests/components/google_cloud/__init__.py @@ -0,0 +1 @@ +"""Tests for the Google Cloud integration.""" diff --git a/tests/components/google_cloud/conftest.py b/tests/components/google_cloud/conftest.py new file mode 100644 index 00000000000000..897c352b402d83 --- /dev/null +++ b/tests/components/google_cloud/conftest.py @@ -0,0 +1,124 @@ +"""Tests helpers.""" + +from collections.abc import Generator +import json +from pathlib import Path +from unittest.mock import AsyncMock, MagicMock, patch + +from google.cloud.texttospeech_v1.types import cloud_tts +import pytest + +from homeassistant.components.google_cloud.const import ( + CONF_SERVICE_ACCOUNT_INFO, + DOMAIN, +) + +from tests.common import MockConfigEntry + +VALID_SERVICE_ACCOUNT_INFO = { + "type": "service_account", + "project_id": "my project id", + "private_key_id": "my private key if", + "private_key": "-----BEGIN PRIVATE KEY-----\nMIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKYscIlwm7soDsHAz6L6YvUkCvkrX19rS6yeYOmovvhoK5WeYGWUsd8V72zmsyHB7XO94YgJVjvxfzn5K8bLePjFzwoSJjZvhBJ/ZQ05d8VmbvgyWUoPdG9oEa4fZ/lCYrXoaFdTot2xcJvrb/ZuiRl4s4eZpNeFYvVK/Am7UeFPAgMBAAECgYAUetOfzLYUudofvPCaKHu7tKZ5kQPfEa0w6BAPnBF1Mfl1JiDBRDMryFtKs6AOIAVwx00dY/Ex0BCbB3+Cr58H7t4NaPTJxCpmR09pK7o17B7xAdQv8+SynFNud9/5vQ5AEXMOLNwKiU7wpXT6Z7ZIibUBOR7ewsWgsHCDpN1iqQJBAOMODPTPSiQMwRAUHIc6GPleFSJnIz2PAoG3JOG9KFAL6RtIc19lob2ZXdbQdzKtjSkWo+O5W20WDNAl1k32h6MCQQC7W4ZCIY67mPbL6CxXfHjpSGF4Dr9VWJ7ZrKHr6XUoOIcEvsn/pHvWonjMdy93rQMSfOE8BKd/I1+GHRmNVgplAkAnSo4paxmsZVyfeKt7Jy2dMY+8tVZe17maUuQaAE7Sk00SgJYegwrbMYgQnWCTL39HBfj0dmYA2Zj8CCAuu6O7AkEAryFiYjaUAO9+4iNoL27+ZrFtypeeadyov7gKs0ZKaQpNyzW8A+Zwi7TbTeSqzic/E+z/bOa82q7p/6b7141xsQJBANCAcIwMcVb6KVCHlQbOtKspo5Eh4ZQi8bGl+IcwbQ6JSxeTx915IfAldgbuU047wOB04dYCFB2yLDiUGVXTifU=\n-----END PRIVATE KEY-----\n", + "client_email": "my client email", + "client_id": "my client id", + "auth_uri": "https://accounts.google.com/o/oauth2/auth", + "token_uri": "https://oauth2.googleapis.com/token", + "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", + "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/service-account", + "universe_domain": "googleapis.com", +} + + +@pytest.fixture +def create_google_credentials_json(tmp_path: Path) -> str: + """Create googlecredentials.json.""" + file_path = tmp_path / "googlecredentials.json" + with open(file_path, "w", encoding="utf8") as f: + json.dump(VALID_SERVICE_ACCOUNT_INFO, f) + return str(file_path) + + +@pytest.fixture +def create_invalid_google_credentials_json(create_google_credentials_json: str) -> str: + """Create invalid googlecredentials.json.""" + invalid_service_account_info = VALID_SERVICE_ACCOUNT_INFO.copy() + invalid_service_account_info.pop("client_email") + with open(create_google_credentials_json, "w", encoding="utf8") as f: + json.dump(invalid_service_account_info, f) + return create_google_credentials_json + + +@pytest.fixture +def mock_process_uploaded_file( + create_google_credentials_json: str, +) -> Generator[MagicMock]: + """Mock upload certificate files.""" + ctx_mock = MagicMock() + ctx_mock.__enter__.return_value = Path(create_google_credentials_json) + with patch( + "homeassistant.components.google_cloud.config_flow.process_uploaded_file", + return_value=ctx_mock, + ) as mock_upload: + yield mock_upload + + +@pytest.fixture +def mock_config_entry() -> MockConfigEntry: + """Return the default mocked config entry.""" + return MockConfigEntry( + title="my Google Cloud title", + domain=DOMAIN, + data={CONF_SERVICE_ACCOUNT_INFO: VALID_SERVICE_ACCOUNT_INFO}, + ) + + +@pytest.fixture +def mock_api_tts() -> AsyncMock: + """Return a mocked TTS client.""" + mock_client = AsyncMock() + mock_client.list_voices.return_value = cloud_tts.ListVoicesResponse( + voices=[ + cloud_tts.Voice(language_codes=["en-US"], name="en-US-Standard-A"), + cloud_tts.Voice(language_codes=["en-US"], name="en-US-Standard-B"), + cloud_tts.Voice(language_codes=["el-GR"], name="el-GR-Standard-A"), + ] + ) + return mock_client + + +@pytest.fixture +def mock_api_tts_from_service_account_info( + mock_api_tts: AsyncMock, +) -> Generator[AsyncMock]: + """Return a mocked TTS client created with from_service_account_info.""" + with ( + patch( + "google.cloud.texttospeech.TextToSpeechAsyncClient.from_service_account_info", + return_value=mock_api_tts, + ), + ): + yield mock_api_tts + + +@pytest.fixture +def mock_api_tts_from_service_account_file( + mock_api_tts: AsyncMock, +) -> Generator[AsyncMock]: + """Return a mocked TTS client created with from_service_account_file.""" + with ( + patch( + "google.cloud.texttospeech.TextToSpeechAsyncClient.from_service_account_file", + return_value=mock_api_tts, + ), + ): + yield mock_api_tts + + +@pytest.fixture +def mock_setup_entry() -> Generator[AsyncMock]: + """Override async_setup_entry.""" + with patch( + "homeassistant.components.google_cloud.async_setup_entry", return_value=True + ) as mock_setup_entry: + yield mock_setup_entry diff --git a/tests/components/google_cloud/test_config_flow.py b/tests/components/google_cloud/test_config_flow.py new file mode 100644 index 00000000000000..e4b4631f223dfd --- /dev/null +++ b/tests/components/google_cloud/test_config_flow.py @@ -0,0 +1,185 @@ +"""Test the Google Cloud config flow.""" + +from unittest.mock import AsyncMock, MagicMock +from uuid import uuid4 + +from homeassistant import config_entries +from homeassistant.components import tts +from homeassistant.components.google_cloud.config_flow import UPLOADED_KEY_FILE +from homeassistant.components.google_cloud.const import ( + CONF_KEY_FILE, + CONF_SERVICE_ACCOUNT_INFO, + DOMAIN, +) +from homeassistant.config_entries import SOURCE_USER +from homeassistant.const import CONF_PLATFORM +from homeassistant.core import HomeAssistant +from homeassistant.data_entry_flow import FlowResultType +from homeassistant.setup import async_setup_component + +from .conftest import VALID_SERVICE_ACCOUNT_INFO + +from tests.common import MockConfigEntry + + +async def test_user_flow_success( + hass: HomeAssistant, + mock_process_uploaded_file: MagicMock, + mock_setup_entry: AsyncMock, +) -> None: + """Test user flow creates entry.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + assert result["type"] is FlowResultType.FORM + assert not result["errors"] + + uploaded_file = str(uuid4()) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {UPLOADED_KEY_FILE: uploaded_file}, + ) + + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["title"] == "Google Cloud" + assert result["data"] == {CONF_SERVICE_ACCOUNT_INFO: VALID_SERVICE_ACCOUNT_INFO} + mock_process_uploaded_file.assert_called_with(hass, uploaded_file) + assert len(mock_setup_entry.mock_calls) == 1 + + +async def test_user_flow_missing_file( + hass: HomeAssistant, + mock_setup_entry: AsyncMock, +) -> None: + """Test user flow when uploaded file is missing.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {} + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {UPLOADED_KEY_FILE: str(uuid4())}, + ) + + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {"base": "invalid_file"} + assert len(mock_setup_entry.mock_calls) == 0 + + +async def test_user_flow_invalid_file( + hass: HomeAssistant, + create_invalid_google_credentials_json: str, + mock_process_uploaded_file: MagicMock, + mock_setup_entry: AsyncMock, +) -> None: + """Test user flow when uploaded file is invalid.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {} + + uploaded_file = str(uuid4()) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {UPLOADED_KEY_FILE: uploaded_file}, + ) + + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {"base": "invalid_file"} + mock_process_uploaded_file.assert_called_with(hass, uploaded_file) + assert len(mock_setup_entry.mock_calls) == 0 + + +async def test_import_flow( + hass: HomeAssistant, + create_google_credentials_json: str, + mock_api_tts_from_service_account_file: AsyncMock, + mock_api_tts_from_service_account_info: AsyncMock, +) -> None: + """Test the import flow.""" + assert not hass.config_entries.async_entries(DOMAIN) + assert await async_setup_component( + hass, + tts.DOMAIN, + { + tts.DOMAIN: {CONF_PLATFORM: DOMAIN} + | {CONF_KEY_FILE: create_google_credentials_json} + }, + ) + await hass.async_block_till_done() + assert len(hass.config_entries.async_entries(DOMAIN)) == 1 + config_entry = hass.config_entries.async_entries(DOMAIN)[0] + assert config_entry.state is config_entries.ConfigEntryState.LOADED + + +async def test_import_flow_invalid_file( + hass: HomeAssistant, + create_invalid_google_credentials_json: str, + mock_api_tts_from_service_account_file: AsyncMock, +) -> None: + """Test the import flow when the key file is invalid.""" + assert not hass.config_entries.async_entries(DOMAIN) + assert await async_setup_component( + hass, + tts.DOMAIN, + { + tts.DOMAIN: {CONF_PLATFORM: DOMAIN} + | {CONF_KEY_FILE: create_invalid_google_credentials_json} + }, + ) + await hass.async_block_till_done() + assert not hass.config_entries.async_entries(DOMAIN) + assert mock_api_tts_from_service_account_file.list_voices.call_count == 1 + + +async def test_options_flow( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_api_tts_from_service_account_info: AsyncMock, +) -> None: + """Test options flow.""" + mock_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(mock_config_entry.entry_id) + assert mock_api_tts_from_service_account_info.list_voices.call_count == 1 + + assert mock_config_entry.options == {} + + result = await hass.config_entries.options.async_init(mock_config_entry.entry_id) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "init" + data_schema = result["data_schema"].schema + assert set(data_schema) == { + "language", + "gender", + "voice", + "encoding", + "speed", + "pitch", + "gain", + "profiles", + "text_type", + "stt_model", + } + assert mock_api_tts_from_service_account_info.list_voices.call_count == 2 + + result = await hass.config_entries.options.async_configure( + result["flow_id"], + user_input={"language": "el-GR"}, + ) + assert result["type"] is FlowResultType.CREATE_ENTRY + assert mock_config_entry.options == { + "language": "el-GR", + "gender": "NEUTRAL", + "voice": "", + "encoding": "MP3", + "speed": 1.0, + "pitch": 0.0, + "gain": 0.0, + "profiles": [], + "text_type": "text", + "stt_model": "latest_short", + } + assert mock_api_tts_from_service_account_info.list_voices.call_count == 3 diff --git a/tests/components/google_photos/__init__.py b/tests/components/google_photos/__init__.py new file mode 100644 index 00000000000000..fa3458112166f6 --- /dev/null +++ b/tests/components/google_photos/__init__.py @@ -0,0 +1 @@ +"""Tests for the Google Photos integration.""" diff --git a/tests/components/google_photos/conftest.py b/tests/components/google_photos/conftest.py new file mode 100644 index 00000000000000..3ca64471fa1ff7 --- /dev/null +++ b/tests/components/google_photos/conftest.py @@ -0,0 +1,192 @@ +"""Test fixtures for Google Photos.""" + +from collections.abc import AsyncGenerator, Awaitable, Callable, Generator +import time +from typing import Any +from unittest.mock import AsyncMock, Mock, patch + +from google_photos_library_api.api import GooglePhotosLibraryApi +from google_photos_library_api.model import ( + Album, + ListAlbumResult, + ListMediaItemResult, + MediaItem, + UserInfoResult, +) +import pytest + +from homeassistant.components.application_credentials import ( + ClientCredential, + async_import_client_credential, +) +from homeassistant.components.google_photos.const import DOMAIN, OAUTH2_SCOPES +from homeassistant.core import HomeAssistant +from homeassistant.setup import async_setup_component + +from tests.common import ( + MockConfigEntry, + load_json_array_fixture, + load_json_object_fixture, +) + +USER_IDENTIFIER = "user-identifier-1" +CONFIG_ENTRY_ID = "user-identifier-1" +CLIENT_ID = "1234" +CLIENT_SECRET = "5678" +FAKE_ACCESS_TOKEN = "some-access-token" +FAKE_REFRESH_TOKEN = "some-refresh-token" +EXPIRES_IN = 3600 +USERINFO_URL = "https://www.googleapis.com/oauth2/v1/userinfo" +PHOTOS_BASE_URL = "https://photoslibrary.googleapis.com" +MEDIA_ITEMS_URL = f"{PHOTOS_BASE_URL}/v1/mediaItems" +ALBUMS_URL = f"{PHOTOS_BASE_URL}/v1/albums" +UPLOADS_URL = f"{PHOTOS_BASE_URL}/v1/uploads" +CREATE_MEDIA_ITEMS_URL = f"{PHOTOS_BASE_URL}/v1/mediaItems:batchCreate" + + +@pytest.fixture(name="expires_at") +def mock_expires_at() -> int: + """Fixture to set the oauth token expiration time.""" + return time.time() + EXPIRES_IN + + +@pytest.fixture(name="scopes") +def mock_scopes() -> list[str]: + """Fixture to set scopes used during the config entry.""" + return OAUTH2_SCOPES + + +@pytest.fixture(name="token_entry") +def mock_token_entry(expires_at: int, scopes: list[str]) -> dict[str, Any]: + """Fixture for OAuth 'token' data for a ConfigEntry.""" + return { + "access_token": FAKE_ACCESS_TOKEN, + "refresh_token": FAKE_REFRESH_TOKEN, + "scope": " ".join(scopes), + "type": "Bearer", + "expires_at": expires_at, + "expires_in": EXPIRES_IN, + } + + +@pytest.fixture(name="config_entry_id") +def mock_config_entry_id() -> str | None: + """Provide a json fixture file to load for list media item api responses.""" + return CONFIG_ENTRY_ID + + +@pytest.fixture(name="config_entry") +def mock_config_entry( + config_entry_id: str, token_entry: dict[str, Any] +) -> MockConfigEntry: + """Fixture for a config entry.""" + return MockConfigEntry( + domain=DOMAIN, + unique_id=config_entry_id, + data={ + "auth_implementation": DOMAIN, + "token": token_entry, + }, + title="Account Name", + ) + + +@pytest.fixture(autouse=True) +async def setup_credentials(hass: HomeAssistant) -> None: + """Fixture to setup credentials.""" + assert await async_setup_component(hass, "application_credentials", {}) + await async_import_client_credential( + hass, + DOMAIN, + ClientCredential(CLIENT_ID, CLIENT_SECRET), + ) + + +@pytest.fixture(name="fixture_name") +def mock_fixture_name() -> str | None: + """Provide a json fixture file to load for list media item api responses.""" + return None + + +@pytest.fixture(name="user_identifier") +def mock_user_identifier() -> str | None: + """Provide a json fixture file to load for list media item api responses.""" + return USER_IDENTIFIER + + +@pytest.fixture(name="api_error") +def mock_api_error() -> Exception | None: + """Provide a json fixture file to load for list media item api responses.""" + return None + + +@pytest.fixture(name="mock_api") +def mock_client_api( + fixture_name: str, + user_identifier: str, + api_error: Exception, +) -> Generator[Mock]: + """Set up fake Google Photos API responses from fixtures.""" + mock_api = AsyncMock(GooglePhotosLibraryApi, autospec=True) + mock_api.get_user_info.return_value = UserInfoResult( + id=user_identifier, + name="Test Name", + email="test.name@gmail.com", + ) + + responses = load_json_array_fixture(fixture_name, DOMAIN) if fixture_name else [] + + async def list_media_items(*args: Any) -> AsyncGenerator[ListMediaItemResult]: + for response in responses: + mock_list_media_items = Mock(ListMediaItemResult) + mock_list_media_items.media_items = [ + MediaItem.from_dict(media_item) for media_item in response["mediaItems"] + ] + yield mock_list_media_items + + mock_api.list_media_items.return_value.__aiter__ = list_media_items + mock_api.list_media_items.return_value.__anext__ = list_media_items + mock_api.list_media_items.side_effect = api_error + + # Mock a point lookup by reading contents of the fixture above + async def get_media_item(media_item_id: str, **kwargs: Any) -> Mock: + for response in responses: + for media_item in response["mediaItems"]: + if media_item["id"] == media_item_id: + return MediaItem.from_dict(media_item) + return None + + mock_api.get_media_item = get_media_item + + # Emulate an async iterator for returning pages of response objects. We just + # return a single page. + + async def list_albums(*args: Any, **kwargs: Any) -> AsyncGenerator[ListAlbumResult]: + mock_list_album_result = Mock(ListAlbumResult) + mock_list_album_result.albums = [ + Album.from_dict(album) + for album in load_json_object_fixture("list_albums.json", DOMAIN)["albums"] + ] + yield mock_list_album_result + + mock_api.list_albums.return_value.__aiter__ = list_albums + mock_api.list_albums.return_value.__anext__ = list_albums + mock_api.list_albums.side_effect = api_error + return mock_api + + +@pytest.fixture(name="setup_integration") +async def mock_setup_integration( + hass: HomeAssistant, + config_entry: MockConfigEntry, + mock_api: Mock, +) -> Callable[[], Awaitable[bool]]: + """Fixture to set up the integration.""" + config_entry.add_to_hass(hass) + + with patch( + "homeassistant.components.google_photos.GooglePhotosLibraryApi", + return_value=mock_api, + ): + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() diff --git a/tests/components/google_photos/fixtures/list_albums.json b/tests/components/google_photos/fixtures/list_albums.json new file mode 100644 index 00000000000000..7460e1d36f30c9 --- /dev/null +++ b/tests/components/google_photos/fixtures/list_albums.json @@ -0,0 +1,13 @@ +{ + "albums": [ + { + "id": "album-media-id-1", + "title": "Album title", + "productUrl": "http://photos.google.com/album-media-id-1", + "isWriteable": true, + "mediaItemsCount": 7, + "coverPhotoBaseUrl": "http://img.example.com/id3", + "coverPhotoMediaItemId": "cover-photo-media-id-3" + } + ] +} diff --git a/tests/components/google_photos/fixtures/list_mediaitems.json b/tests/components/google_photos/fixtures/list_mediaitems.json new file mode 100644 index 00000000000000..8e470a2fc04fd8 --- /dev/null +++ b/tests/components/google_photos/fixtures/list_mediaitems.json @@ -0,0 +1,35 @@ +[ + { + "mediaItems": [ + { + "id": "id1", + "description": "some-descripton", + "productUrl": "http://example.com/id1", + "baseUrl": "http://img.example.com/id1", + "mimeType": "image/jpeg", + "mediaMetadata": { + "creationTime": "2014-10-02T15:01:23Z", + "width": 1600, + "height": 768 + }, + "filename": "example1.jpg" + }, + { + "id": "id2", + "description": "some-descripton", + "productUrl": "http://example.com/id2", + "baseUrl": "http://img.example.com/id2", + "mimeType": "video/mp4", + "mediaMetadata": { + "creationTime": "2014-10-02T16:01:23Z", + "width": 1600, + "height": 768, + "video": { + "cameraMake": "Pixel" + } + }, + "filename": "example2.mp4" + } + ] + } +] diff --git a/tests/components/google_photos/fixtures/list_mediaitems_empty.json b/tests/components/google_photos/fixtures/list_mediaitems_empty.json new file mode 100644 index 00000000000000..bf6a4da855f109 --- /dev/null +++ b/tests/components/google_photos/fixtures/list_mediaitems_empty.json @@ -0,0 +1,5 @@ +[ + { + "mediaItems": [] + } +] diff --git a/tests/components/google_photos/test_config_flow.py b/tests/components/google_photos/test_config_flow.py new file mode 100644 index 00000000000000..48c8723df3c106 --- /dev/null +++ b/tests/components/google_photos/test_config_flow.py @@ -0,0 +1,332 @@ +"""Test the Google Photos config flow.""" + +from collections.abc import Generator +from typing import Any +from unittest.mock import Mock, patch + +from google_photos_library_api.exceptions import GooglePhotosApiError +import pytest + +from homeassistant import config_entries +from homeassistant.components.google_photos.const import ( + DOMAIN, + OAUTH2_AUTHORIZE, + OAUTH2_TOKEN, +) +from homeassistant.core import HomeAssistant +from homeassistant.data_entry_flow import FlowResultType +from homeassistant.helpers import config_entry_oauth2_flow + +from .conftest import EXPIRES_IN, FAKE_ACCESS_TOKEN, FAKE_REFRESH_TOKEN, USER_IDENTIFIER + +from tests.common import MockConfigEntry +from tests.test_util.aiohttp import AiohttpClientMocker +from tests.typing import ClientSessionGenerator + +CLIENT_ID = "1234" +CLIENT_SECRET = "5678" + + +@pytest.fixture(name="mock_setup") +def mock_setup_entry() -> Generator[Mock]: + """Fixture to mock out integration setup.""" + with patch( + "homeassistant.components.google_photos.async_setup_entry", return_value=True + ) as mock_setup: + yield mock_setup + + +@pytest.fixture(autouse=True) +def mock_patch_api(mock_api: Mock) -> Generator[None]: + """Fixture to patch the config flow api.""" + with patch( + "homeassistant.components.google_photos.config_flow.GooglePhotosLibraryApi", + return_value=mock_api, + ): + yield + + +@pytest.fixture(name="updated_token_entry", autouse=True) +def mock_updated_token_entry() -> dict[str, Any]: + """Fixture to provide any test specific overrides to token data from the oauth token endpoint.""" + return {} + + +@pytest.fixture(name="mock_oauth_token_request", autouse=True) +def mock_token_request( + aioclient_mock: AiohttpClientMocker, + token_entry: dict[str, any], + updated_token_entry: dict[str, Any], +) -> None: + """Fixture to provide a fake response from the oauth token endpoint.""" + aioclient_mock.clear_requests() + aioclient_mock.post( + OAUTH2_TOKEN, + json={ + **token_entry, + **updated_token_entry, + }, + ) + + +@pytest.mark.usefixtures("current_request_with_host", "mock_api") +@pytest.mark.parametrize("fixture_name", ["list_mediaitems.json"]) +async def test_full_flow( + hass: HomeAssistant, + hass_client_no_auth: ClientSessionGenerator, + mock_setup: Mock, +) -> None: + """Check full flow.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} + ) + state = config_entry_oauth2_flow._encode_jwt( + hass, + { + "flow_id": result["flow_id"], + "redirect_uri": "https://example.com/auth/external/callback", + }, + ) + + assert result["url"] == ( + f"{OAUTH2_AUTHORIZE}?response_type=code&client_id={CLIENT_ID}" + "&redirect_uri=https://example.com/auth/external/callback" + f"&state={state}" + "&scope=https://www.googleapis.com/auth/photoslibrary.readonly" + "+https://www.googleapis.com/auth/photoslibrary.readonly.appcreateddata" + "+https://www.googleapis.com/auth/photoslibrary.appendonly" + "+https://www.googleapis.com/auth/userinfo.profile" + "&access_type=offline&prompt=consent" + ) + + client = await hass_client_no_auth() + resp = await client.get(f"/auth/external/callback?code=abcd&state={state}") + assert resp.status == 200 + assert resp.headers["content-type"] == "text/html; charset=utf-8" + + result = await hass.config_entries.flow.async_configure(result["flow_id"]) + assert result["type"] is FlowResultType.CREATE_ENTRY + config_entry = result["result"] + assert config_entry.unique_id == USER_IDENTIFIER + assert config_entry.title == "Test Name" + config_entry_data = dict(config_entry.data) + assert "token" in config_entry_data + assert "expires_at" in config_entry_data["token"] + del config_entry_data["token"]["expires_at"] + assert config_entry_data == { + "auth_implementation": DOMAIN, + "token": { + "access_token": FAKE_ACCESS_TOKEN, + "expires_in": EXPIRES_IN, + "refresh_token": FAKE_REFRESH_TOKEN, + "type": "Bearer", + "scope": ( + "https://www.googleapis.com/auth/photoslibrary.readonly" + " https://www.googleapis.com/auth/photoslibrary.readonly.appcreateddata" + " https://www.googleapis.com/auth/photoslibrary.appendonly" + " https://www.googleapis.com/auth/userinfo.profile" + ), + }, + } + assert len(hass.config_entries.async_entries(DOMAIN)) == 1 + assert len(mock_setup.mock_calls) == 1 + + +@pytest.mark.usefixtures( + "current_request_with_host", + "setup_credentials", + "mock_api", +) +@pytest.mark.parametrize( + "api_error", + [ + GooglePhotosApiError("some error"), + ], +) +async def test_api_not_enabled( + hass: HomeAssistant, + hass_client_no_auth: ClientSessionGenerator, +) -> None: + """Check flow aborts if api is not enabled.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} + ) + state = config_entry_oauth2_flow._encode_jwt( + hass, + { + "flow_id": result["flow_id"], + "redirect_uri": "https://example.com/auth/external/callback", + }, + ) + + assert result["url"] == ( + f"{OAUTH2_AUTHORIZE}?response_type=code&client_id={CLIENT_ID}" + "&redirect_uri=https://example.com/auth/external/callback" + f"&state={state}" + "&scope=https://www.googleapis.com/auth/photoslibrary.readonly" + "+https://www.googleapis.com/auth/photoslibrary.readonly.appcreateddata" + "+https://www.googleapis.com/auth/photoslibrary.appendonly" + "+https://www.googleapis.com/auth/userinfo.profile" + "&access_type=offline&prompt=consent" + ) + + client = await hass_client_no_auth() + resp = await client.get(f"/auth/external/callback?code=abcd&state={state}") + assert resp.status == 200 + assert resp.headers["content-type"] == "text/html; charset=utf-8" + + result = await hass.config_entries.flow.async_configure(result["flow_id"]) + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "access_not_configured" + assert result["description_placeholders"]["message"].endswith("some error") + + +@pytest.mark.usefixtures("current_request_with_host", "setup_credentials") +async def test_general_exception( + hass: HomeAssistant, + hass_client_no_auth: ClientSessionGenerator, + mock_api: Mock, +) -> None: + """Check flow aborts if exception happens.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} + ) + state = config_entry_oauth2_flow._encode_jwt( + hass, + { + "flow_id": result["flow_id"], + "redirect_uri": "https://example.com/auth/external/callback", + }, + ) + assert result["url"] == ( + f"{OAUTH2_AUTHORIZE}?response_type=code&client_id={CLIENT_ID}" + "&redirect_uri=https://example.com/auth/external/callback" + f"&state={state}" + "&scope=https://www.googleapis.com/auth/photoslibrary.readonly" + "+https://www.googleapis.com/auth/photoslibrary.readonly.appcreateddata" + "+https://www.googleapis.com/auth/photoslibrary.appendonly" + "+https://www.googleapis.com/auth/userinfo.profile" + "&access_type=offline&prompt=consent" + ) + + client = await hass_client_no_auth() + resp = await client.get(f"/auth/external/callback?code=abcd&state={state}") + assert resp.status == 200 + assert resp.headers["content-type"] == "text/html; charset=utf-8" + + mock_api.list_media_items.side_effect = Exception + + result = await hass.config_entries.flow.async_configure(result["flow_id"]) + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "unknown" + + +@pytest.mark.usefixtures("current_request_with_host", "mock_api", "setup_integration") +@pytest.mark.parametrize("fixture_name", ["list_mediaitems.json"]) +@pytest.mark.parametrize( + "updated_token_entry", + [ + { + "access_token": "updated-access-token", + } + ], +) +@pytest.mark.parametrize( + ( + "user_identifier", + "abort_reason", + "resulting_access_token", + "expected_setup_calls", + ), + [ + ( + USER_IDENTIFIER, + "reauth_successful", + "updated-access-token", + 1, + ), + ( + "345", + "wrong_account", + FAKE_ACCESS_TOKEN, + 0, + ), + ], +) +@pytest.mark.usefixtures("current_request_with_host") +async def test_reauth( + hass: HomeAssistant, + hass_client_no_auth: ClientSessionGenerator, + config_entry: MockConfigEntry, + user_identifier: str, + abort_reason: str, + resulting_access_token: str, + mock_setup: Mock, + expected_setup_calls: int, +) -> None: + """Test the re-authentication case updates the correct config entry.""" + + config_entry.async_start_reauth(hass) + await hass.async_block_till_done() + + flows = hass.config_entries.flow.async_progress() + assert len(flows) == 1 + result = flows[0] + assert result["step_id"] == "reauth_confirm" + + result = await hass.config_entries.flow.async_configure(result["flow_id"], {}) + state = config_entry_oauth2_flow._encode_jwt( + hass, + { + "flow_id": result["flow_id"], + "redirect_uri": "https://example.com/auth/external/callback", + }, + ) + assert result["url"] == ( + f"{OAUTH2_AUTHORIZE}?response_type=code&client_id={CLIENT_ID}" + "&redirect_uri=https://example.com/auth/external/callback" + f"&state={state}" + "&scope=https://www.googleapis.com/auth/photoslibrary.readonly" + "+https://www.googleapis.com/auth/photoslibrary.readonly.appcreateddata" + "+https://www.googleapis.com/auth/photoslibrary.appendonly" + "+https://www.googleapis.com/auth/userinfo.profile" + "&access_type=offline&prompt=consent" + ) + client = await hass_client_no_auth() + resp = await client.get(f"/auth/external/callback?code=abcd&state={state}") + assert resp.status == 200 + assert resp.headers["content-type"] == "text/html; charset=utf-8" + + result = await hass.config_entries.flow.async_configure(result["flow_id"]) + + assert len(hass.config_entries.async_entries(DOMAIN)) == 1 + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == abort_reason + + assert config_entry.unique_id == USER_IDENTIFIER + assert config_entry.title == "Account Name" + config_entry_data = dict(config_entry.data) + assert "token" in config_entry_data + assert "expires_at" in config_entry_data["token"] + del config_entry_data["token"]["expires_at"] + assert config_entry_data == { + "auth_implementation": DOMAIN, + "token": { + # Verify token is refreshed or not + "access_token": resulting_access_token, + "expires_in": EXPIRES_IN, + "refresh_token": FAKE_REFRESH_TOKEN, + "type": "Bearer", + "scope": ( + "https://www.googleapis.com/auth/photoslibrary.readonly" + " https://www.googleapis.com/auth/photoslibrary.readonly.appcreateddata" + " https://www.googleapis.com/auth/photoslibrary.appendonly" + " https://www.googleapis.com/auth/userinfo.profile" + ), + }, + } + assert len(hass.config_entries.async_entries(DOMAIN)) == 1 + assert len(mock_setup.mock_calls) == expected_setup_calls diff --git a/tests/components/google_photos/test_init.py b/tests/components/google_photos/test_init.py new file mode 100644 index 00000000000000..ea236cfc712102 --- /dev/null +++ b/tests/components/google_photos/test_init.py @@ -0,0 +1,109 @@ +"""Tests for Google Photos.""" + +import http +import time + +from aiohttp import ClientError +import pytest + +from homeassistant.components.google_photos.const import OAUTH2_TOKEN +from homeassistant.config_entries import ConfigEntryState +from homeassistant.core import HomeAssistant + +from tests.common import MockConfigEntry +from tests.test_util.aiohttp import AiohttpClientMocker + + +@pytest.mark.usefixtures("setup_integration") +async def test_setup( + hass: HomeAssistant, + config_entry: MockConfigEntry, +) -> None: + """Test successful setup and unload.""" + assert config_entry.state is ConfigEntryState.LOADED + + await hass.config_entries.async_unload(config_entry.entry_id) + await hass.async_block_till_done() + + assert config_entry.state is ConfigEntryState.NOT_LOADED + + +@pytest.fixture(name="refresh_token_status") +def mock_refresh_token_status() -> http.HTTPStatus: + """Fixture to set a token refresh status.""" + return http.HTTPStatus.OK + + +@pytest.fixture(name="refresh_token_exception") +def mock_refresh_token_exception() -> Exception | None: + """Fixture to set a token refresh status.""" + return None + + +@pytest.fixture(name="refresh_token") +def mock_refresh_token( + aioclient_mock: AiohttpClientMocker, + refresh_token_status: http.HTTPStatus, + refresh_token_exception: Exception | None, +) -> MockConfigEntry: + """Fixture to simulate a token refresh response.""" + aioclient_mock.clear_requests() + aioclient_mock.post( + OAUTH2_TOKEN, + exc=refresh_token_exception, + status=refresh_token_status, + json={ + "access_token": "updated-access-token", + "refresh_token": "updated-refresh-token", + "expires_at": time.time() + 3600, + "expires_in": 3600, + }, + ) + + +@pytest.mark.usefixtures("refresh_token", "setup_integration") +@pytest.mark.parametrize("expires_at", [time.time() - 3600], ids=["expired"]) +async def test_expired_token_refresh_success( + hass: HomeAssistant, + config_entry: MockConfigEntry, +) -> None: + """Test expired token is refreshed.""" + + assert config_entry.state is ConfigEntryState.LOADED + assert config_entry.data["token"]["access_token"] == "updated-access-token" + assert config_entry.data["token"]["expires_in"] == 3600 + + +@pytest.mark.usefixtures("refresh_token", "setup_integration") +@pytest.mark.parametrize( + ("expires_at", "refresh_token_status", "refresh_token_exception", "expected_state"), + [ + ( + time.time() - 3600, + http.HTTPStatus.UNAUTHORIZED, + None, + ConfigEntryState.SETUP_ERROR, # Reauth + ), + ( + time.time() - 3600, + http.HTTPStatus.INTERNAL_SERVER_ERROR, + None, + ConfigEntryState.SETUP_RETRY, + ), + ( + time.time() - 3600, + None, + ClientError("Client exception raised"), + ConfigEntryState.SETUP_RETRY, + ), + ], + ids=["unauthorized", "internal_server_error", "client_error"], +) +async def test_expired_token_refresh_failure( + hass: HomeAssistant, + config_entry: MockConfigEntry, + expected_state: ConfigEntryState, +) -> None: + """Test failure while refreshing token with a transient error.""" + + assert config_entry.state is expected_state diff --git a/tests/components/google_photos/test_media_source.py b/tests/components/google_photos/test_media_source.py new file mode 100644 index 00000000000000..762a4d5ebd1973 --- /dev/null +++ b/tests/components/google_photos/test_media_source.py @@ -0,0 +1,228 @@ +"""Test the Google Photos media source.""" + +from unittest.mock import Mock + +from google_photos_library_api.exceptions import GooglePhotosApiError +import pytest + +from homeassistant.components.google_photos.const import DOMAIN, UPLOAD_SCOPE +from homeassistant.components.media_source import ( + URI_SCHEME, + BrowseError, + async_browse_media, + async_resolve_media, +) +from homeassistant.core import HomeAssistant +from homeassistant.setup import async_setup_component + +from .conftest import CONFIG_ENTRY_ID + +from tests.common import MockConfigEntry + + +@pytest.fixture(autouse=True) +async def setup_components(hass: HomeAssistant) -> None: + """Fixture to initialize the integration.""" + await async_setup_component(hass, "media_source", {}) + + +@pytest.mark.usefixtures("setup_integration") +async def test_no_config_entries( + hass: HomeAssistant, config_entry: MockConfigEntry +) -> None: + """Test a media source with no active config entry.""" + + await hass.config_entries.async_unload(config_entry.entry_id) + await hass.async_block_till_done() + + browse = await async_browse_media(hass, f"{URI_SCHEME}{DOMAIN}") + + assert browse.domain == DOMAIN + assert browse.identifier is None + assert browse.title == "Google Photos" + assert browse.can_expand + assert not browse.children + + +@pytest.mark.usefixtures("setup_integration", "mock_api") +@pytest.mark.parametrize( + ("scopes"), + [ + [UPLOAD_SCOPE], + ], +) +async def test_no_read_scopes( + hass: HomeAssistant, +) -> None: + """Test a media source with only write scopes configured so no media source exists.""" + browse = await async_browse_media(hass, f"{URI_SCHEME}{DOMAIN}") + assert browse.domain == DOMAIN + assert browse.identifier is None + assert browse.title == "Google Photos" + assert not browse.children + + +@pytest.mark.usefixtures("setup_integration", "mock_api") +@pytest.mark.parametrize( + ("album_path", "expected_album_title"), + [ + (f"{CONFIG_ENTRY_ID}/a/recent", "Recent Photos"), + (f"{CONFIG_ENTRY_ID}/a/favorites", "Favorite Photos"), + (f"{CONFIG_ENTRY_ID}/a/album-media-id-1", "Album title"), + ], +) +@pytest.mark.parametrize( + ("fixture_name", "expected_results", "expected_medias"), + [ + ("list_mediaitems_empty.json", [], []), + ( + "list_mediaitems.json", + [ + (f"{CONFIG_ENTRY_ID}/p/id1", "example1.jpg"), + (f"{CONFIG_ENTRY_ID}/p/id2", "example2.mp4"), + ], + [ + ("http://img.example.com/id1=h2160", "image/jpeg"), + ("http://img.example.com/id2=dv", "video/mp4"), + ], + ), + ], +) +async def test_browse_albums( + hass: HomeAssistant, + album_path: str, + expected_album_title: str, + expected_results: list[tuple[str, str]], + expected_medias: list[tuple[str, str]], +) -> None: + """Test a media source with no eligible camera devices.""" + browse = await async_browse_media(hass, f"{URI_SCHEME}{DOMAIN}") + assert browse.domain == DOMAIN + assert browse.identifier is None + assert browse.title == "Google Photos" + assert [(child.identifier, child.title) for child in browse.children] == [ + (CONFIG_ENTRY_ID, "Account Name") + ] + + browse = await async_browse_media(hass, f"{URI_SCHEME}{DOMAIN}/{CONFIG_ENTRY_ID}") + assert browse.domain == DOMAIN + assert browse.identifier == CONFIG_ENTRY_ID + assert browse.title == "Account Name" + assert [(child.identifier, child.title) for child in browse.children] == [ + (f"{CONFIG_ENTRY_ID}/a/recent", "Recent Photos"), + (f"{CONFIG_ENTRY_ID}/a/favorites", "Favorite Photos"), + (f"{CONFIG_ENTRY_ID}/a/album-media-id-1", "Album title"), + ] + + browse = await async_browse_media(hass, f"{URI_SCHEME}{DOMAIN}/{album_path}") + assert browse.domain == DOMAIN + assert browse.identifier == album_path + assert browse.title == "Account Name" + assert [ + (child.identifier, child.title) for child in browse.children + ] == expected_results + + media = [ + await async_resolve_media( + hass, f"{URI_SCHEME}{DOMAIN}/{child.identifier}", None + ) + for child in browse.children + ] + assert [ + (play_media.url, play_media.mime_type) for play_media in media + ] == expected_medias + + +@pytest.mark.usefixtures("setup_integration", "mock_api") +async def test_invalid_config_entry(hass: HomeAssistant) -> None: + """Test browsing to a config entry that does not exist.""" + with pytest.raises(BrowseError, match="Could not find config entry"): + await async_browse_media(hass, f"{URI_SCHEME}{DOMAIN}/invalid-config-entry") + + +@pytest.mark.usefixtures("setup_integration", "mock_api") +@pytest.mark.parametrize("fixture_name", ["list_mediaitems.json"]) +async def test_browse_invalid_path(hass: HomeAssistant) -> None: + """Test browsing to a photo is not possible.""" + browse = await async_browse_media(hass, f"{URI_SCHEME}{DOMAIN}") + assert browse.domain == DOMAIN + assert browse.identifier is None + assert browse.title == "Google Photos" + assert [(child.identifier, child.title) for child in browse.children] == [ + (CONFIG_ENTRY_ID, "Account Name") + ] + + with pytest.raises(BrowseError, match="Unsupported identifier"): + await async_browse_media( + hass, f"{URI_SCHEME}{DOMAIN}/{CONFIG_ENTRY_ID}/p/some-photo-id" + ) + + +@pytest.mark.usefixtures("setup_integration") +@pytest.mark.parametrize("api_error", [GooglePhotosApiError("some error")]) +async def test_invalid_album_id(hass: HomeAssistant, mock_api: Mock) -> None: + """Test browsing to an album id that does not exist.""" + browse = await async_browse_media(hass, f"{URI_SCHEME}{DOMAIN}") + assert browse.domain == DOMAIN + assert browse.identifier is None + assert browse.title == "Google Photos" + assert [(child.identifier, child.title) for child in browse.children] == [ + (CONFIG_ENTRY_ID, "Account Name") + ] + + with pytest.raises(BrowseError, match="Error listing media items"): + await async_browse_media( + hass, f"{URI_SCHEME}{DOMAIN}/{CONFIG_ENTRY_ID}/a/invalid-album-id" + ) + + +@pytest.mark.usefixtures("setup_integration") +@pytest.mark.parametrize( + ("identifier", "expected_error"), + [ + (CONFIG_ENTRY_ID, "not a Photo"), + ("invalid-config-entry/a/example", "not a Photo"), + ("invalid-config-entry/q/example", "Could not parse"), + ("too/many/slashes/in/path", "Invalid identifier"), + ], +) +async def test_missing_photo_id( + hass: HomeAssistant, identifier: str, expected_error: str +) -> None: + """Test parsing an invalid media identifier.""" + with pytest.raises(BrowseError, match=expected_error): + await async_resolve_media(hass, f"{URI_SCHEME}{DOMAIN}/{identifier}", None) + + +@pytest.mark.usefixtures("setup_integration", "mock_api") +@pytest.mark.parametrize("api_error", [GooglePhotosApiError("some error")]) +async def test_list_albums_failure(hass: HomeAssistant) -> None: + """Test browsing to an album id that does not exist.""" + browse = await async_browse_media(hass, f"{URI_SCHEME}{DOMAIN}") + assert browse.domain == DOMAIN + assert browse.identifier is None + assert browse.title == "Google Photos" + assert [(child.identifier, child.title) for child in browse.children] == [ + (CONFIG_ENTRY_ID, "Account Name") + ] + + with pytest.raises(BrowseError, match="Error listing albums"): + await async_browse_media(hass, f"{URI_SCHEME}{DOMAIN}/{CONFIG_ENTRY_ID}") + + +@pytest.mark.usefixtures("setup_integration", "mock_api") +@pytest.mark.parametrize("api_error", [GooglePhotosApiError("some error")]) +async def test_list_media_items_failure(hass: HomeAssistant) -> None: + """Test browsing to an album id that does not exist.""" + browse = await async_browse_media(hass, f"{URI_SCHEME}{DOMAIN}") + assert browse.domain == DOMAIN + assert browse.identifier is None + assert browse.title == "Google Photos" + assert [(child.identifier, child.title) for child in browse.children] == [ + (CONFIG_ENTRY_ID, "Account Name") + ] + + with pytest.raises(BrowseError, match="Error listing media items"): + await async_browse_media( + hass, f"{URI_SCHEME}{DOMAIN}/{CONFIG_ENTRY_ID}/a/recent" + ) diff --git a/tests/components/google_photos/test_services.py b/tests/components/google_photos/test_services.py new file mode 100644 index 00000000000000..10d57e1d178b54 --- /dev/null +++ b/tests/components/google_photos/test_services.py @@ -0,0 +1,247 @@ +"""Tests for Google Photos.""" + +from unittest.mock import Mock, patch + +from google_photos_library_api.exceptions import GooglePhotosApiError +from google_photos_library_api.model import ( + CreateMediaItemsResult, + MediaItem, + NewMediaItemResult, + Status, +) +import pytest + +from homeassistant.components.google_photos.const import DOMAIN, READ_SCOPES +from homeassistant.config_entries import ConfigEntryState +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError + +from tests.common import MockConfigEntry + + +@pytest.mark.usefixtures("setup_integration") +async def test_upload_service( + hass: HomeAssistant, + config_entry: MockConfigEntry, + mock_api: Mock, +) -> None: + """Test service call to upload content.""" + assert hass.services.has_service(DOMAIN, "upload") + + mock_api.create_media_items.return_value = CreateMediaItemsResult( + new_media_item_results=[ + NewMediaItemResult( + upload_token="some-upload-token", + status=Status(code=200), + media_item=MediaItem(id="new-media-item-id-1"), + ) + ] + ) + + with ( + patch( + "homeassistant.components.google_photos.services.Path.read_bytes", + return_value=b"image bytes", + ), + patch( + "homeassistant.components.google_photos.services.Path.exists", + return_value=True, + ), + patch.object(hass.config, "is_allowed_path", return_value=True), + ): + response = await hass.services.async_call( + DOMAIN, + "upload", + { + "config_entry_id": config_entry.entry_id, + "filename": "doorbell_snapshot.jpg", + }, + blocking=True, + return_response=True, + ) + + assert response == {"media_items": [{"media_item_id": "new-media-item-id-1"}]} + + +@pytest.mark.usefixtures("setup_integration") +async def test_upload_service_config_entry_not_found( + hass: HomeAssistant, + config_entry: MockConfigEntry, +) -> None: + """Test upload service call with a config entry that does not exist.""" + with pytest.raises(HomeAssistantError, match="not found in registry"): + await hass.services.async_call( + DOMAIN, + "upload", + { + "config_entry_id": "invalid-config-entry-id", + "filename": "doorbell_snapshot.jpg", + }, + blocking=True, + return_response=True, + ) + + +@pytest.mark.usefixtures("setup_integration") +async def test_config_entry_not_loaded( + hass: HomeAssistant, + config_entry: MockConfigEntry, +) -> None: + """Test upload service call with a config entry that is not loaded.""" + await hass.config_entries.async_unload(config_entry.entry_id) + await hass.async_block_till_done() + + assert config_entry.state is ConfigEntryState.NOT_LOADED + + with pytest.raises(HomeAssistantError, match="not found in registry"): + await hass.services.async_call( + DOMAIN, + "upload", + { + "config_entry_id": config_entry.unique_id, + "filename": "doorbell_snapshot.jpg", + }, + blocking=True, + return_response=True, + ) + + +@pytest.mark.usefixtures("setup_integration") +async def test_path_is_not_allowed( + hass: HomeAssistant, + config_entry: MockConfigEntry, +) -> None: + """Test upload service call with a filename path that is not allowed.""" + with ( + patch.object(hass.config, "is_allowed_path", return_value=False), + pytest.raises(HomeAssistantError, match="no access to path"), + ): + await hass.services.async_call( + DOMAIN, + "upload", + { + "config_entry_id": config_entry.entry_id, + "filename": "doorbell_snapshot.jpg", + }, + blocking=True, + return_response=True, + ) + + +@pytest.mark.usefixtures("setup_integration") +async def test_filename_does_not_exist( + hass: HomeAssistant, + config_entry: MockConfigEntry, +) -> None: + """Test upload service call with a filename path that does not exist.""" + with ( + patch.object(hass.config, "is_allowed_path", return_value=True), + patch("pathlib.Path.exists", return_value=False), + pytest.raises(HomeAssistantError, match="does not exist"), + ): + await hass.services.async_call( + DOMAIN, + "upload", + { + "config_entry_id": config_entry.entry_id, + "filename": "doorbell_snapshot.jpg", + }, + blocking=True, + return_response=True, + ) + + +@pytest.mark.usefixtures("setup_integration") +async def test_upload_service_upload_content_failure( + hass: HomeAssistant, + config_entry: MockConfigEntry, + mock_api: Mock, +) -> None: + """Test service call to upload content.""" + + mock_api.upload_content.side_effect = GooglePhotosApiError() + + with ( + patch( + "homeassistant.components.google_photos.services.Path.read_bytes", + return_value=b"image bytes", + ), + patch( + "homeassistant.components.google_photos.services.Path.exists", + return_value=True, + ), + patch.object(hass.config, "is_allowed_path", return_value=True), + pytest.raises(HomeAssistantError, match="Failed to upload content"), + ): + await hass.services.async_call( + DOMAIN, + "upload", + { + "config_entry_id": config_entry.entry_id, + "filename": "doorbell_snapshot.jpg", + }, + blocking=True, + return_response=True, + ) + + +@pytest.mark.usefixtures("setup_integration") +async def test_upload_service_fails_create( + hass: HomeAssistant, + config_entry: MockConfigEntry, + mock_api: Mock, +) -> None: + """Test service call to upload content.""" + + mock_api.create_media_items.side_effect = GooglePhotosApiError() + + with ( + patch( + "homeassistant.components.google_photos.services.Path.read_bytes", + return_value=b"image bytes", + ), + patch( + "homeassistant.components.google_photos.services.Path.exists", + return_value=True, + ), + patch.object(hass.config, "is_allowed_path", return_value=True), + pytest.raises( + HomeAssistantError, match="Google Photos API responded with error" + ), + ): + await hass.services.async_call( + DOMAIN, + "upload", + { + "config_entry_id": config_entry.entry_id, + "filename": "doorbell_snapshot.jpg", + }, + blocking=True, + return_response=True, + ) + + +@pytest.mark.usefixtures("setup_integration") +@pytest.mark.parametrize( + ("scopes"), + [ + READ_SCOPES, + ], +) +async def test_upload_service_no_scope( + hass: HomeAssistant, + config_entry: MockConfigEntry, +) -> None: + """Test service call to upload content but the config entry is read-only.""" + + with pytest.raises(HomeAssistantError, match="not granted permission"): + await hass.services.async_call( + DOMAIN, + "upload", + { + "config_entry_id": config_entry.entry_id, + "filename": "doorbell_snapshot.jpg", + }, + blocking=True, + return_response=True, + ) diff --git a/tests/components/google_translate/test_tts.py b/tests/components/google_translate/test_tts.py index 95313df6140eaa..1f199a5db97807 100644 --- a/tests/components/google_translate/test_tts.py +++ b/tests/components/google_translate/test_tts.py @@ -103,7 +103,7 @@ async def mock_config_entry_setup(hass: HomeAssistant, config: dict[str, Any]) - "mock_config_entry_setup", "speak", { - ATTR_ENTITY_ID: "tts.google_en_com", + ATTR_ENTITY_ID: "tts.google_translate_en_com", tts.ATTR_MEDIA_PLAYER_ENTITY_ID: "media_player.something", tts.ATTR_MESSAGE: "There is a person at the front door.", }, @@ -160,7 +160,7 @@ async def test_tts_service( "mock_config_entry_setup", "speak", { - ATTR_ENTITY_ID: "tts.google_de_com", + ATTR_ENTITY_ID: "tts.google_translate_de_com", tts.ATTR_MEDIA_PLAYER_ENTITY_ID: "media_player.something", tts.ATTR_MESSAGE: "There is a person at the front door.", }, @@ -216,7 +216,7 @@ async def test_service_say_german_config( "mock_config_entry_setup", "speak", { - ATTR_ENTITY_ID: "tts.google_en_com", + ATTR_ENTITY_ID: "tts.google_translate_en_com", tts.ATTR_MEDIA_PLAYER_ENTITY_ID: "media_player.something", tts.ATTR_MESSAGE: "There is a person at the front door.", tts.ATTR_LANGUAGE: "de", @@ -273,7 +273,7 @@ async def test_service_say_german_service( "mock_config_entry_setup", "speak", { - ATTR_ENTITY_ID: "tts.google_en_co_uk", + ATTR_ENTITY_ID: "tts.google_translate_en_co_uk", tts.ATTR_MEDIA_PLAYER_ENTITY_ID: "media_player.something", tts.ATTR_MESSAGE: "There is a person at the front door.", }, @@ -329,7 +329,7 @@ async def test_service_say_en_uk_config( "mock_config_entry_setup", "speak", { - ATTR_ENTITY_ID: "tts.google_en_com", + ATTR_ENTITY_ID: "tts.google_translate_en_com", tts.ATTR_MEDIA_PLAYER_ENTITY_ID: "media_player.something", tts.ATTR_MESSAGE: "There is a person at the front door.", tts.ATTR_LANGUAGE: "en-uk", @@ -386,7 +386,7 @@ async def test_service_say_en_uk_service( "mock_config_entry_setup", "speak", { - ATTR_ENTITY_ID: "tts.google_en_com", + ATTR_ENTITY_ID: "tts.google_translate_en_com", tts.ATTR_MEDIA_PLAYER_ENTITY_ID: "media_player.something", tts.ATTR_MESSAGE: "There is a person at the front door.", tts.ATTR_OPTIONS: {"tld": "co.uk"}, @@ -443,7 +443,7 @@ async def test_service_say_en_couk( "mock_config_entry_setup", "speak", { - ATTR_ENTITY_ID: "tts.google_en_com", + ATTR_ENTITY_ID: "tts.google_translate_en_com", tts.ATTR_MEDIA_PLAYER_ENTITY_ID: "media_player.something", tts.ATTR_MESSAGE: "There is a person at the front door.", }, diff --git a/tests/components/habitica/test_config_flow.py b/tests/components/habitica/test_config_flow.py index 4dfc696daf2998..09cda3fbb0a005 100644 --- a/tests/components/habitica/test_config_flow.py +++ b/tests/components/habitica/test_config_flow.py @@ -3,27 +3,59 @@ from unittest.mock import AsyncMock, MagicMock, patch from aiohttp import ClientResponseError +import pytest from homeassistant import config_entries -from homeassistant.components.habitica.const import DEFAULT_URL, DOMAIN +from homeassistant.components.habitica.const import CONF_API_USER, DEFAULT_URL, DOMAIN +from homeassistant.const import ( + CONF_API_KEY, + CONF_PASSWORD, + CONF_URL, + CONF_USERNAME, + CONF_VERIFY_SSL, +) from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType from tests.common import MockConfigEntry +MOCK_DATA_LOGIN_STEP = { + CONF_USERNAME: "test-email@example.com", + CONF_PASSWORD: "test-password", +} +MOCK_DATA_ADVANCED_STEP = { + CONF_API_USER: "test-api-user", + CONF_API_KEY: "test-api-key", + CONF_URL: DEFAULT_URL, + CONF_VERIFY_SSL: True, +} -async def test_form(hass: HomeAssistant) -> None: - """Test we get the form.""" + +async def test_form_login(hass: HomeAssistant) -> None: + """Test we get the login form.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} ) + + assert result["type"] is FlowResultType.MENU + assert "login" in result["menu_options"] + assert result["step_id"] == "user" + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": "login"} + ) assert result["type"] is FlowResultType.FORM assert result["errors"] == {} + assert result["step_id"] == "login" mock_obj = MagicMock() - mock_obj.user.get = AsyncMock() - + mock_obj.user.auth.local.login.post = AsyncMock() + mock_obj.user.auth.local.login.post.return_value = { + "id": "test-api-user", + "apiToken": "test-api-key", + "username": "test-username", + } with ( patch( "homeassistant.components.habitica.config_flow.HabitipyAsync", @@ -37,57 +69,142 @@ async def test_form(hass: HomeAssistant) -> None: return_value=True, ) as mock_setup_entry, ): - result2 = await hass.config_entries.flow.async_configure( + result = await hass.config_entries.flow.async_configure( result["flow_id"], - {"api_user": "test-api-user", "api_key": "test-api-key"}, + user_input=MOCK_DATA_LOGIN_STEP, ) await hass.async_block_till_done() - assert result2["type"] is FlowResultType.CREATE_ENTRY - assert result2["title"] == "Default username" - assert result2["data"] == { - "url": DEFAULT_URL, - "api_user": "test-api-user", - "api_key": "test-api-key", + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["title"] == "test-username" + assert result["data"] == { + **MOCK_DATA_ADVANCED_STEP, + CONF_USERNAME: "test-username", } assert len(mock_setup.mock_calls) == 1 assert len(mock_setup_entry.mock_calls) == 1 -async def test_form_invalid_credentials(hass: HomeAssistant) -> None: +@pytest.mark.parametrize( + ("raise_error", "text_error"), + [ + (ClientResponseError(MagicMock(), (), status=400), "cannot_connect"), + (ClientResponseError(MagicMock(), (), status=401), "invalid_auth"), + (IndexError(), "unknown"), + ], +) +async def test_form_login_errors(hass: HomeAssistant, raise_error, text_error) -> None: """Test we handle invalid credentials error.""" + result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} ) - mock_obj = MagicMock() - mock_obj.user.get = AsyncMock(side_effect=ClientResponseError(MagicMock(), ())) + assert result["type"] is FlowResultType.MENU + assert result["step_id"] == "user" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": "login"} + ) + + mock_obj = MagicMock() + mock_obj.user.auth.local.login.post = AsyncMock(side_effect=raise_error) with patch( "homeassistant.components.habitica.config_flow.HabitipyAsync", return_value=mock_obj, ): result2 = await hass.config_entries.flow.async_configure( result["flow_id"], - { - "url": DEFAULT_URL, - "api_user": "test-api-user", - "api_key": "test-api-key", - }, + user_input=MOCK_DATA_LOGIN_STEP, ) assert result2["type"] is FlowResultType.FORM - assert result2["errors"] == {"base": "invalid_credentials"} + assert result2["errors"] == {"base": text_error} + +async def test_form_advanced(hass: HomeAssistant) -> None: + """Test we get the form.""" -async def test_form_unexpected_exception(hass: HomeAssistant) -> None: - """Test we handle unexpected exception error.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} ) + assert result["type"] is FlowResultType.MENU + assert "advanced" in result["menu_options"] + assert result["step_id"] == "user" + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": "advanced"} + ) + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {} + assert result["step_id"] == "advanced" + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": "advanced"} + ) + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {} + mock_obj = MagicMock() - mock_obj.user.get = AsyncMock(side_effect=Exception) + mock_obj.user.get = AsyncMock() + mock_obj.user.get.return_value = {"auth": {"local": {"username": "test-username"}}} + + with ( + patch( + "homeassistant.components.habitica.config_flow.HabitipyAsync", + return_value=mock_obj, + ), + patch( + "homeassistant.components.habitica.async_setup", return_value=True + ) as mock_setup, + patch( + "homeassistant.components.habitica.async_setup_entry", + return_value=True, + ) as mock_setup_entry, + ): + result2 = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input=MOCK_DATA_ADVANCED_STEP, + ) + await hass.async_block_till_done() + + assert result2["type"] is FlowResultType.CREATE_ENTRY + assert result2["title"] == "test-username" + assert result2["data"] == { + **MOCK_DATA_ADVANCED_STEP, + CONF_USERNAME: "test-username", + } + assert len(mock_setup.mock_calls) == 1 + assert len(mock_setup_entry.mock_calls) == 1 + + +@pytest.mark.parametrize( + ("raise_error", "text_error"), + [ + (ClientResponseError(MagicMock(), (), status=400), "cannot_connect"), + (ClientResponseError(MagicMock(), (), status=401), "invalid_auth"), + (IndexError(), "unknown"), + ], +) +async def test_form_advanced_errors( + hass: HomeAssistant, raise_error, text_error +) -> None: + """Test we handle invalid credentials error.""" + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} + ) + + assert result["type"] is FlowResultType.MENU + assert result["step_id"] == "user" + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": "advanced"} + ) + + mock_obj = MagicMock() + mock_obj.user.get = AsyncMock(side_effect=raise_error) with patch( "homeassistant.components.habitica.config_flow.HabitipyAsync", @@ -95,15 +212,11 @@ async def test_form_unexpected_exception(hass: HomeAssistant) -> None: ): result2 = await hass.config_entries.flow.async_configure( result["flow_id"], - { - "url": DEFAULT_URL, - "api_user": "test-api-user", - "api_key": "test-api-key", - }, + user_input=MOCK_DATA_ADVANCED_STEP, ) assert result2["type"] is FlowResultType.FORM - assert result2["errors"] == {"base": "unknown"} + assert result2["errors"] == {"base": text_error} async def test_manual_flow_config_exist(hass: HomeAssistant) -> None: @@ -119,7 +232,7 @@ async def test_manual_flow_config_exist(hass: HomeAssistant) -> None: ) assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "user" + assert result["step_id"] == "advanced" mock_obj = MagicMock() mock_obj.user.get = AsyncMock(return_value={"api_user": "test-api-user"}) diff --git a/tests/components/habitica/test_init.py b/tests/components/habitica/test_init.py index 31c3a1fae39fa1..56f17bc98893ce 100644 --- a/tests/components/habitica/test_init.py +++ b/tests/components/habitica/test_init.py @@ -52,6 +52,7 @@ def common_requests(aioclient_mock: AiohttpClientMocker) -> AiohttpClientMocker: "https://habitica.com/api/v3/user", json={ "data": { + "auth": {"local": {"username": TEST_USER_NAME}}, "api_user": "test-api-user", "profile": {"name": TEST_USER_NAME}, "stats": { @@ -73,31 +74,31 @@ def common_requests(aioclient_mock: AiohttpClientMocker) -> AiohttpClientMocker: } }, ) - aioclient_mock.get( - "https://habitica.com/api/v3/tasks/user", + "https://habitica.com/api/v3/tasks/user?type=completedTodos", json={ "data": [ { - "text": f"this is a mock {task} #{i}", - "id": f"{i}", - "type": task, - "completed": False, + "text": "this is a mock todo #5", + "id": 5, + "_id": 5, + "type": "todo", + "completed": True, } - for i, task in enumerate(("habit", "daily", "todo", "reward"), start=1) ] }, ) aioclient_mock.get( - "https://habitica.com/api/v3/tasks/user?type=completedTodos", + "https://habitica.com/api/v3/tasks/user", json={ "data": [ { - "text": "this is a mock todo #5", - "id": 5, - "type": "todo", - "completed": True, + "text": f"this is a mock {task} #{i}", + "id": f"{i}", + "type": task, + "completed": False, } + for i, task in enumerate(("habit", "daily", "todo", "reward"), start=1) ] }, ) diff --git a/tests/components/hassio/common.py b/tests/components/hassio/common.py new file mode 100644 index 00000000000000..630368a0a7a423 --- /dev/null +++ b/tests/components/hassio/common.py @@ -0,0 +1,234 @@ +"""Provide common test tools for hassio.""" + +from __future__ import annotations + +from collections.abc import Generator +import logging +from typing import Any +from unittest.mock import DEFAULT, AsyncMock, patch + +from homeassistant.components.hassio.addon_manager import AddonManager +from homeassistant.core import HomeAssistant + +LOGGER = logging.getLogger(__name__) + + +def mock_addon_manager(hass: HomeAssistant) -> AddonManager: + """Return an AddonManager instance.""" + return AddonManager(hass, LOGGER, "Test", "test_addon") + + +def mock_discovery_info() -> Any: + """Return the discovery info from the supervisor.""" + return DEFAULT + + +def mock_get_addon_discovery_info( + discovery_info: dict[str, Any], discovery_info_side_effect: Any | None +) -> Generator[AsyncMock]: + """Mock get add-on discovery info.""" + with patch( + "homeassistant.components.hassio.addon_manager.async_get_addon_discovery_info", + side_effect=discovery_info_side_effect, + return_value=discovery_info, + ) as get_addon_discovery_info: + yield get_addon_discovery_info + + +def mock_addon_store_info( + addon_store_info_side_effect: Any | None, +) -> Generator[AsyncMock]: + """Mock Supervisor add-on store info.""" + with patch( + "homeassistant.components.hassio.addon_manager.async_get_addon_store_info", + side_effect=addon_store_info_side_effect, + ) as addon_store_info: + addon_store_info.return_value = { + "available": True, + "installed": None, + "state": None, + "version": "1.0.0", + } + yield addon_store_info + + +def mock_addon_info(addon_info_side_effect: Any | None) -> Generator[AsyncMock]: + """Mock Supervisor add-on info.""" + with patch( + "homeassistant.components.hassio.addon_manager.async_get_addon_info", + side_effect=addon_info_side_effect, + ) as addon_info: + addon_info.return_value = { + "available": False, + "hostname": None, + "options": {}, + "state": None, + "update_available": False, + "version": None, + } + yield addon_info + + +def mock_addon_not_installed( + addon_store_info: AsyncMock, addon_info: AsyncMock +) -> AsyncMock: + """Mock add-on not installed.""" + addon_store_info.return_value["available"] = True + return addon_info + + +def mock_addon_installed( + addon_store_info: AsyncMock, addon_info: AsyncMock +) -> AsyncMock: + """Mock add-on already installed but not running.""" + addon_store_info.return_value = { + "available": True, + "installed": "1.0.0", + "state": "stopped", + "version": "1.0.0", + } + addon_info.return_value["available"] = True + addon_info.return_value["hostname"] = "core-test-addon" + addon_info.return_value["state"] = "stopped" + addon_info.return_value["version"] = "1.0.0" + return addon_info + + +def mock_addon_running(addon_store_info: AsyncMock, addon_info: AsyncMock) -> AsyncMock: + """Mock add-on already running.""" + addon_store_info.return_value = { + "available": True, + "installed": "1.0.0", + "state": "started", + "version": "1.0.0", + } + addon_info.return_value["available"] = True + addon_info.return_value["hostname"] = "core-test-addon" + addon_info.return_value["state"] = "started" + addon_info.return_value["version"] = "1.0.0" + return addon_info + + +def mock_install_addon_side_effect( + addon_store_info: AsyncMock, addon_info: AsyncMock +) -> Any | None: + """Return the install add-on side effect.""" + + async def install_addon(hass: HomeAssistant, slug): + """Mock install add-on.""" + addon_store_info.return_value = { + "available": True, + "installed": "1.0.0", + "state": "stopped", + "version": "1.0.0", + } + addon_info.return_value["available"] = True + addon_info.return_value["state"] = "stopped" + addon_info.return_value["version"] = "1.0.0" + + return install_addon + + +def mock_install_addon(install_addon_side_effect: Any | None) -> Generator[AsyncMock]: + """Mock install add-on.""" + + with patch( + "homeassistant.components.hassio.addon_manager.async_install_addon", + side_effect=install_addon_side_effect, + ) as install_addon: + yield install_addon + + +def mock_start_addon_side_effect( + addon_store_info: AsyncMock, addon_info: AsyncMock +) -> Any | None: + """Return the start add-on options side effect.""" + + async def start_addon(hass: HomeAssistant, slug): + """Mock start add-on.""" + addon_store_info.return_value = { + "available": True, + "installed": "1.0.0", + "state": "started", + "version": "1.0.0", + } + addon_info.return_value["available"] = True + addon_info.return_value["state"] = "started" + + return start_addon + + +def mock_start_addon(start_addon_side_effect: Any | None) -> Generator[AsyncMock]: + """Mock start add-on.""" + with patch( + "homeassistant.components.hassio.addon_manager.async_start_addon", + side_effect=start_addon_side_effect, + ) as start_addon: + yield start_addon + + +def mock_stop_addon() -> Generator[AsyncMock]: + """Mock stop add-on.""" + with patch( + "homeassistant.components.hassio.addon_manager.async_stop_addon" + ) as stop_addon: + yield stop_addon + + +def mock_restart_addon(restart_addon_side_effect: Any | None) -> Generator[AsyncMock]: + """Mock restart add-on.""" + with patch( + "homeassistant.components.hassio.addon_manager.async_restart_addon", + side_effect=restart_addon_side_effect, + ) as restart_addon: + yield restart_addon + + +def mock_uninstall_addon() -> Generator[AsyncMock]: + """Mock uninstall add-on.""" + with patch( + "homeassistant.components.hassio.addon_manager.async_uninstall_addon" + ) as uninstall_addon: + yield uninstall_addon + + +def mock_addon_options(addon_info: AsyncMock) -> dict[str, Any]: + """Mock add-on options.""" + return addon_info.return_value["options"] + + +def mock_set_addon_options_side_effect(addon_options: dict[str, Any]) -> Any | None: + """Return the set add-on options side effect.""" + + async def set_addon_options(hass: HomeAssistant, slug: str, options: dict) -> None: + """Mock set add-on options.""" + addon_options.update(options["options"]) + + return set_addon_options + + +def mock_set_addon_options( + set_addon_options_side_effect: Any | None, +) -> Generator[AsyncMock]: + """Mock set add-on options.""" + with patch( + "homeassistant.components.hassio.addon_manager.async_set_addon_options", + side_effect=set_addon_options_side_effect, + ) as set_options: + yield set_options + + +def mock_create_backup() -> Generator[AsyncMock]: + """Mock create backup.""" + with patch( + "homeassistant.components.hassio.addon_manager.async_create_backup" + ) as create_backup: + yield create_backup + + +def mock_update_addon() -> Generator[AsyncMock]: + """Mock update add-on.""" + with patch( + "homeassistant.components.hassio.addon_manager.async_update_addon" + ) as update_addon: + yield update_addon diff --git a/tests/components/hassio/test_addon_manager.py b/tests/components/hassio/test_addon_manager.py index 6a20c6eec885e5..4cb57e5b8d8925 100644 --- a/tests/components/hassio/test_addon_manager.py +++ b/tests/components/hassio/test_addon_manager.py @@ -3,10 +3,8 @@ from __future__ import annotations import asyncio -from collections.abc import Generator -import logging from typing import Any -from unittest.mock import AsyncMock, call, patch +from unittest.mock import AsyncMock, call import pytest @@ -19,154 +17,6 @@ from homeassistant.components.hassio.handler import HassioAPIError from homeassistant.core import HomeAssistant -LOGGER = logging.getLogger(__name__) - - -@pytest.fixture(name="addon_manager") -def addon_manager_fixture(hass: HomeAssistant) -> AddonManager: - """Return an AddonManager instance.""" - return AddonManager(hass, LOGGER, "Test", "test_addon") - - -@pytest.fixture(name="addon_not_installed") -def addon_not_installed_fixture( - addon_store_info: AsyncMock, addon_info: AsyncMock -) -> AsyncMock: - """Mock add-on not installed.""" - addon_store_info.return_value["available"] = True - return addon_info - - -@pytest.fixture(name="addon_installed") -def mock_addon_installed( - addon_store_info: AsyncMock, addon_info: AsyncMock -) -> AsyncMock: - """Mock add-on already installed but not running.""" - addon_store_info.return_value = { - "available": True, - "installed": "1.0.0", - "state": "stopped", - "version": "1.0.0", - } - addon_info.return_value["available"] = True - addon_info.return_value["hostname"] = "core-test-addon" - addon_info.return_value["state"] = "stopped" - addon_info.return_value["version"] = "1.0.0" - return addon_info - - -@pytest.fixture(name="get_addon_discovery_info") -def get_addon_discovery_info_fixture() -> Generator[AsyncMock]: - """Mock get add-on discovery info.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_get_addon_discovery_info" - ) as get_addon_discovery_info: - yield get_addon_discovery_info - - -@pytest.fixture(name="addon_store_info") -def addon_store_info_fixture() -> Generator[AsyncMock]: - """Mock Supervisor add-on store info.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_get_addon_store_info" - ) as addon_store_info: - addon_store_info.return_value = { - "available": False, - "installed": None, - "state": None, - "version": "1.0.0", - } - yield addon_store_info - - -@pytest.fixture(name="addon_info") -def addon_info_fixture() -> Generator[AsyncMock]: - """Mock Supervisor add-on info.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_get_addon_info", - ) as addon_info: - addon_info.return_value = { - "available": False, - "hostname": None, - "options": {}, - "state": None, - "update_available": False, - "version": None, - } - yield addon_info - - -@pytest.fixture(name="set_addon_options") -def set_addon_options_fixture() -> Generator[AsyncMock]: - """Mock set add-on options.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_set_addon_options" - ) as set_options: - yield set_options - - -@pytest.fixture(name="install_addon") -def install_addon_fixture() -> Generator[AsyncMock]: - """Mock install add-on.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_install_addon" - ) as install_addon: - yield install_addon - - -@pytest.fixture(name="uninstall_addon") -def uninstall_addon_fixture() -> Generator[AsyncMock]: - """Mock uninstall add-on.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_uninstall_addon" - ) as uninstall_addon: - yield uninstall_addon - - -@pytest.fixture(name="start_addon") -def start_addon_fixture() -> Generator[AsyncMock]: - """Mock start add-on.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_start_addon" - ) as start_addon: - yield start_addon - - -@pytest.fixture(name="restart_addon") -def restart_addon_fixture() -> Generator[AsyncMock]: - """Mock restart add-on.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_restart_addon" - ) as restart_addon: - yield restart_addon - - -@pytest.fixture(name="stop_addon") -def stop_addon_fixture() -> Generator[AsyncMock]: - """Mock stop add-on.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_stop_addon" - ) as stop_addon: - yield stop_addon - - -@pytest.fixture(name="create_backup") -def create_backup_fixture() -> Generator[AsyncMock]: - """Mock create backup.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_create_backup" - ) as create_backup: - yield create_backup - - -@pytest.fixture(name="update_addon") -def mock_update_addon() -> Generator[AsyncMock]: - """Mock update add-on.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_update_addon" - ) as update_addon: - yield update_addon - async def test_not_installed_raises_exception( addon_manager: AddonManager, @@ -888,9 +738,10 @@ async def test_create_backup_error( ) +@pytest.mark.usefixtures("addon_installed") +@pytest.mark.parametrize("set_addon_options_side_effect", [None]) async def test_schedule_install_setup_addon( addon_manager: AddonManager, - addon_installed: AsyncMock, install_addon: AsyncMock, set_addon_options: AsyncMock, start_addon: AsyncMock, @@ -1065,11 +916,10 @@ async def test_schedule_install_setup_addon_logs_error( assert start_addon.call_count == start_addon_calls +@pytest.mark.usefixtures("addon_installed") +@pytest.mark.parametrize("set_addon_options_side_effect", [None]) async def test_schedule_setup_addon( - addon_manager: AddonManager, - addon_installed: AsyncMock, - set_addon_options: AsyncMock, - start_addon: AsyncMock, + addon_manager: AddonManager, set_addon_options: AsyncMock, start_addon: AsyncMock ) -> None: """Test schedule setup addon.""" start_task = addon_manager.async_schedule_setup_addon({"test_key": "test"}) diff --git a/tests/components/hassio/test_handler.py b/tests/components/hassio/test_handler.py index c5fa6ff8254fa0..949f96ece382ca 100644 --- a/tests/components/hassio/test_handler.py +++ b/tests/components/hassio/test_handler.py @@ -468,4 +468,11 @@ async def test_send_command_invalid_command(hass: HomeAssistant) -> None: """Test send command fails when command is invalid.""" hassio: HassIO = hass.data["hassio"] with pytest.raises(HassioAPIError): + # absolute path await hassio.send_command("/test/../bad") + with pytest.raises(HassioAPIError): + # relative path + await hassio.send_command("test/../bad") + with pytest.raises(HassioAPIError): + # relative path with percent encoding + await hassio.send_command("test/%2E%2E/bad") diff --git a/tests/components/hive/test_config_flow.py b/tests/components/hive/test_config_flow.py index fd6eb564a396c3..e5dba49dcc1404 100644 --- a/tests/components/hive/test_config_flow.py +++ b/tests/components/hive/test_config_flow.py @@ -246,14 +246,7 @@ async def test_reauth_flow(hass: HomeAssistant) -> None: "homeassistant.components.hive.config_flow.Auth.login", side_effect=hive_exceptions.HiveInvalidPassword(), ): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": mock_config.unique_id, - }, - data=mock_config.data, - ) + result = await mock_config.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["errors"] == {"base": "invalid_password"} @@ -305,14 +298,7 @@ async def test_reauth_2fa_flow(hass: HomeAssistant) -> None: "homeassistant.components.hive.config_flow.Auth.login", side_effect=hive_exceptions.HiveInvalidPassword(), ): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": mock_config.unique_id, - }, - data=mock_config.data, - ) + result = await mock_config.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["errors"] == {"base": "invalid_password"} diff --git a/tests/components/homeassistant/test_init.py b/tests/components/homeassistant/test_init.py index a0902fe62df2bf..a66d13e5ffec1b 100644 --- a/tests/components/homeassistant/test_init.py +++ b/tests/components/homeassistant/test_init.py @@ -7,7 +7,6 @@ import yaml from homeassistant import config -import homeassistant.components as comps from homeassistant.components.homeassistant import ( ATTR_ENTRY_ID, ATTR_SAFE_MODE, @@ -46,15 +45,6 @@ ) -async def test_is_on(hass: HomeAssistant) -> None: - """Test is_on method.""" - with pytest.raises( - RuntimeError, - match="Detected code that uses homeassistant.components.is_on. This is deprecated and will stop working", - ): - assert comps.is_on(hass, "light.Bowl") - - async def test_turn_on_without_entities(hass: HomeAssistant) -> None: """Test turn_on method without entities.""" await async_setup_component(hass, ha.DOMAIN, {}) diff --git a/tests/components/homeassistant_hardware/conftest.py b/tests/components/homeassistant_hardware/conftest.py index c495b5132e03e2..c63dca74391486 100644 --- a/tests/components/homeassistant_hardware/conftest.py +++ b/tests/components/homeassistant_hardware/conftest.py @@ -6,8 +6,6 @@ import pytest -from homeassistant.core import HomeAssistant - @pytest.fixture(autouse=True) def mock_zha_config_flow_setup() -> Generator[None]: @@ -51,112 +49,6 @@ def mock_zha_get_last_network_settings() -> Generator[None]: yield -@pytest.fixture(name="addon_running") -def mock_addon_running(addon_store_info, addon_info): - """Mock add-on already running.""" - addon_store_info.return_value = { - "installed": "1.0.0", - "state": "started", - "version": "1.0.0", - } - addon_info.return_value["hostname"] = "core-silabs-multiprotocol" - addon_info.return_value["state"] = "started" - addon_info.return_value["version"] = "1.0.0" - return addon_info - - -@pytest.fixture(name="addon_installed") -def mock_addon_installed(addon_store_info, addon_info): - """Mock add-on already installed but not running.""" - addon_store_info.return_value = { - "installed": "1.0.0", - "state": "stopped", - "version": "1.0.0", - } - addon_info.return_value["hostname"] = "core-silabs-multiprotocol" - addon_info.return_value["state"] = "stopped" - addon_info.return_value["version"] = "1.0.0" - return addon_info - - -@pytest.fixture(name="addon_store_info") -def addon_store_info_fixture(): - """Mock Supervisor add-on store info.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_get_addon_store_info" - ) as addon_store_info: - addon_store_info.return_value = { - "available": True, - "installed": None, - "state": None, - "version": "1.0.0", - } - yield addon_store_info - - -@pytest.fixture(name="addon_info") -def addon_info_fixture(): - """Mock Supervisor add-on info.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_get_addon_info", - ) as addon_info: - addon_info.return_value = { - "available": True, - "hostname": None, - "options": {}, - "state": None, - "update_available": False, - "version": None, - } - yield addon_info - - -@pytest.fixture(name="set_addon_options") -def set_addon_options_fixture(): - """Mock set add-on options.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_set_addon_options" - ) as set_options: - yield set_options - - -@pytest.fixture(name="install_addon_side_effect") -def install_addon_side_effect_fixture(addon_store_info, addon_info): - """Return the install add-on side effect.""" - - async def install_addon(hass: HomeAssistant, slug: str) -> None: - """Mock install add-on.""" - addon_store_info.return_value = { - "installed": "1.0.0", - "state": "stopped", - "version": "1.0.0", - } - addon_info.return_value["hostname"] = "core-silabs-multiprotocol" - addon_info.return_value["state"] = "stopped" - addon_info.return_value["version"] = "1.0.0" - - return install_addon - - -@pytest.fixture(name="install_addon") -def mock_install_addon(install_addon_side_effect): - """Mock install add-on.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_install_addon", - side_effect=install_addon_side_effect, - ) as install_addon: - yield install_addon - - -@pytest.fixture(name="start_addon") -def start_addon_fixture(): - """Mock start add-on.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_start_addon" - ) as start_addon: - yield start_addon - - @pytest.fixture(name="stop_addon") def stop_addon_fixture(): """Mock stop add-on.""" @@ -164,12 +56,3 @@ def stop_addon_fixture(): "homeassistant.components.hassio.addon_manager.async_stop_addon" ) as stop_addon: yield stop_addon - - -@pytest.fixture(name="uninstall_addon") -def uninstall_addon_fixture(): - """Mock uninstall add-on.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_uninstall_addon" - ) as uninstall_addon: - yield uninstall_addon diff --git a/tests/components/homeassistant_sky_connect/conftest.py b/tests/components/homeassistant_sky_connect/conftest.py index e93b90c4c7c7b1..d71bf4305b3896 100644 --- a/tests/components/homeassistant_sky_connect/conftest.py +++ b/tests/components/homeassistant_sky_connect/conftest.py @@ -5,8 +5,6 @@ import pytest -from homeassistant.core import HomeAssistant - @pytest.fixture(name="mock_usb_serial_by_id", autouse=True) def mock_usb_serial_by_id_fixture() -> Generator[MagicMock]: @@ -51,112 +49,6 @@ def mock_zha_get_last_network_settings() -> Generator[None]: yield -@pytest.fixture(name="addon_running") -def mock_addon_running(addon_store_info, addon_info): - """Mock add-on already running.""" - addon_store_info.return_value = { - "installed": "1.0.0", - "state": "started", - "version": "1.0.0", - } - addon_info.return_value["hostname"] = "core-silabs-multiprotocol" - addon_info.return_value["state"] = "started" - addon_info.return_value["version"] = "1.0.0" - return addon_info - - -@pytest.fixture(name="addon_installed") -def mock_addon_installed(addon_store_info, addon_info): - """Mock add-on already installed but not running.""" - addon_store_info.return_value = { - "installed": "1.0.0", - "state": "stopped", - "version": "1.0.0", - } - addon_info.return_value["hostname"] = "core-silabs-multiprotocol" - addon_info.return_value["state"] = "stopped" - addon_info.return_value["version"] = "1.0.0" - return addon_info - - -@pytest.fixture(name="addon_store_info") -def addon_store_info_fixture(): - """Mock Supervisor add-on store info.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_get_addon_store_info" - ) as addon_store_info: - addon_store_info.return_value = { - "available": True, - "installed": None, - "state": None, - "version": "1.0.0", - } - yield addon_store_info - - -@pytest.fixture(name="addon_info") -def addon_info_fixture(): - """Mock Supervisor add-on info.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_get_addon_info", - ) as addon_info: - addon_info.return_value = { - "available": True, - "hostname": None, - "options": {}, - "state": None, - "update_available": False, - "version": None, - } - yield addon_info - - -@pytest.fixture(name="set_addon_options") -def set_addon_options_fixture(): - """Mock set add-on options.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_set_addon_options" - ) as set_options: - yield set_options - - -@pytest.fixture(name="install_addon_side_effect") -def install_addon_side_effect_fixture(addon_store_info, addon_info): - """Return the install add-on side effect.""" - - async def install_addon(hass: HomeAssistant, slug: str) -> None: - """Mock install add-on.""" - addon_store_info.return_value = { - "installed": "1.0.0", - "state": "stopped", - "version": "1.0.0", - } - addon_info.return_value["hostname"] = "core-silabs-multiprotocol" - addon_info.return_value["state"] = "stopped" - addon_info.return_value["version"] = "1.0.0" - - return install_addon - - -@pytest.fixture(name="install_addon") -def mock_install_addon(install_addon_side_effect): - """Mock install add-on.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_install_addon", - side_effect=install_addon_side_effect, - ) as install_addon: - yield install_addon - - -@pytest.fixture(name="start_addon") -def start_addon_fixture(): - """Mock start add-on.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_start_addon" - ) as start_addon: - yield start_addon - - @pytest.fixture(name="stop_addon") def stop_addon_fixture(): """Mock stop add-on.""" @@ -164,12 +56,3 @@ def stop_addon_fixture(): "homeassistant.components.hassio.addon_manager.async_stop_addon" ) as stop_addon: yield stop_addon - - -@pytest.fixture(name="uninstall_addon") -def uninstall_addon_fixture(): - """Mock uninstall add-on.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_uninstall_addon" - ) as uninstall_addon: - yield uninstall_addon diff --git a/tests/components/homeassistant_yellow/conftest.py b/tests/components/homeassistant_yellow/conftest.py index 9882d7613d5c0b..7247c7da4e2a07 100644 --- a/tests/components/homeassistant_yellow/conftest.py +++ b/tests/components/homeassistant_yellow/conftest.py @@ -6,8 +6,6 @@ import pytest -from homeassistant.core import HomeAssistant - @pytest.fixture(autouse=True) def mock_zha_config_flow_setup() -> Generator[None]: @@ -49,109 +47,3 @@ def mock_zha_get_last_network_settings() -> Generator[None]: AsyncMock(return_value=None), ): yield - - -@pytest.fixture(name="addon_running") -def mock_addon_running(addon_store_info, addon_info): - """Mock add-on already running.""" - addon_store_info.return_value = { - "installed": "1.0.0", - "state": "started", - "version": "1.0.0", - } - addon_info.return_value["hostname"] = "core-silabs-multiprotocol" - addon_info.return_value["state"] = "started" - addon_info.return_value["version"] = "1.0.0" - return addon_info - - -@pytest.fixture(name="addon_installed") -def mock_addon_installed(addon_store_info, addon_info): - """Mock add-on already installed but not running.""" - addon_store_info.return_value = { - "installed": "1.0.0", - "state": "stopped", - "version": "1.0.0", - } - addon_info.return_value["hostname"] = "core-silabs-multiprotocol" - addon_info.return_value["state"] = "stopped" - addon_info.return_value["version"] = "1.0.0" - return addon_info - - -@pytest.fixture(name="addon_store_info") -def addon_store_info_fixture(): - """Mock Supervisor add-on store info.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_get_addon_store_info" - ) as addon_store_info: - addon_store_info.return_value = { - "available": True, - "installed": None, - "state": None, - "version": "1.0.0", - } - yield addon_store_info - - -@pytest.fixture(name="addon_info") -def addon_info_fixture(): - """Mock Supervisor add-on info.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_get_addon_info", - ) as addon_info: - addon_info.return_value = { - "available": True, - "hostname": None, - "options": {}, - "state": None, - "update_available": False, - "version": None, - } - yield addon_info - - -@pytest.fixture(name="set_addon_options") -def set_addon_options_fixture(): - """Mock set add-on options.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_set_addon_options" - ) as set_options: - yield set_options - - -@pytest.fixture(name="install_addon_side_effect") -def install_addon_side_effect_fixture(addon_store_info, addon_info): - """Return the install add-on side effect.""" - - async def install_addon(hass: HomeAssistant, slug: str) -> None: - """Mock install add-on.""" - addon_store_info.return_value = { - "installed": "1.0.0", - "state": "stopped", - "version": "1.0.0", - } - addon_info.return_value["hostname"] = "core-silabs-multiprotocol" - addon_info.return_value["state"] = "stopped" - addon_info.return_value["version"] = "1.0.0" - - return install_addon - - -@pytest.fixture(name="install_addon") -def mock_install_addon(install_addon_side_effect): - """Mock install add-on.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_install_addon", - side_effect=install_addon_side_effect, - ) as install_addon: - yield install_addon - - -@pytest.fixture(name="start_addon") -def start_addon_fixture(): - """Mock start add-on.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_start_addon" - ) as start_addon: - yield start_addon diff --git a/tests/components/homeassistant_yellow/test_config_flow.py b/tests/components/homeassistant_yellow/test_config_flow.py index 95d7df89c9d535..949e58e61b6601 100644 --- a/tests/components/homeassistant_yellow/test_config_flow.py +++ b/tests/components/homeassistant_yellow/test_config_flow.py @@ -6,8 +6,17 @@ import pytest from homeassistant.components.hassio import DOMAIN as HASSIO_DOMAIN -from homeassistant.components.homeassistant_yellow.const import DOMAIN -from homeassistant.components.zha import DOMAIN as ZHA_DOMAIN +from homeassistant.components.hassio.addon_manager import AddonInfo, AddonState +from homeassistant.components.homeassistant_hardware.firmware_config_flow import ( + STEP_PICK_FIRMWARE_ZIGBEE, +) +from homeassistant.components.homeassistant_hardware.silabs_multiprotocol_addon import ( + CONF_DISABLE_MULTI_PAN, + get_flasher_addon_manager, + get_multiprotocol_addon_manager, +) +from homeassistant.components.homeassistant_hardware.util import ApplicationType +from homeassistant.components.homeassistant_yellow.const import DOMAIN, RADIO_DEVICE from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType from homeassistant.setup import async_setup_component @@ -57,22 +66,28 @@ async def test_config_flow(hass: HomeAssistant) -> None: mock_integration(hass, MockModule("hassio")) await async_setup_component(hass, HASSIO_DOMAIN, {}) - with patch( - "homeassistant.components.homeassistant_yellow.async_setup_entry", - return_value=True, - ) as mock_setup_entry: + with ( + patch( + "homeassistant.components.homeassistant_yellow.async_setup_entry", + return_value=True, + ) as mock_setup_entry, + patch( + "homeassistant.components.homeassistant_hardware.firmware_config_flow.probe_silabs_firmware_type", + return_value=ApplicationType.EZSP, + ), + ): result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": "system"} ) assert result["type"] is FlowResultType.CREATE_ENTRY assert result["title"] == "Home Assistant Yellow" - assert result["data"] == {} + assert result["data"] == {"firmware": "ezsp"} assert result["options"] == {} assert len(mock_setup_entry.mock_calls) == 1 config_entry = hass.config_entries.async_entries(DOMAIN)[0] - assert config_entry.data == {} + assert config_entry.data == {"firmware": "ezsp"} assert config_entry.options == {} assert config_entry.title == "Home Assistant Yellow" @@ -84,10 +99,12 @@ async def test_config_flow_single_entry(hass: HomeAssistant) -> None: # Setup the config entry config_entry = MockConfigEntry( - data={}, + data={"firmware": ApplicationType.EZSP}, domain=DOMAIN, options={}, title="Home Assistant Yellow", + version=1, + minor_version=2, ) config_entry.add_to_hass(hass) @@ -104,165 +121,6 @@ async def test_config_flow_single_entry(hass: HomeAssistant) -> None: mock_setup_entry.assert_not_called() -async def test_option_flow_install_multi_pan_addon( - hass: HomeAssistant, - addon_store_info, - addon_info, - install_addon, - set_addon_options, - start_addon, -) -> None: - """Test installing the multi pan addon.""" - mock_integration(hass, MockModule("hassio")) - await async_setup_component(hass, HASSIO_DOMAIN, {}) - - # Setup the config entry - config_entry = MockConfigEntry( - data={}, - domain=DOMAIN, - options={}, - title="Home Assistant Yellow", - ) - config_entry.add_to_hass(hass) - - result = await hass.config_entries.options.async_init(config_entry.entry_id) - assert result["type"] is FlowResultType.MENU - - with patch( - "homeassistant.components.homeassistant_hardware.silabs_multiprotocol_addon.is_hassio", - side_effect=Mock(return_value=True), - ): - result = await hass.config_entries.options.async_configure( - result["flow_id"], - {"next_step_id": "multipan_settings"}, - ) - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "addon_not_installed" - - result = await hass.config_entries.options.async_configure( - result["flow_id"], - user_input={ - "enable_multi_pan": True, - }, - ) - assert result["type"] is FlowResultType.SHOW_PROGRESS - assert result["step_id"] == "install_addon" - assert result["progress_action"] == "install_addon" - - await hass.async_block_till_done() - install_addon.assert_called_once_with(hass, "core_silabs_multiprotocol") - - result = await hass.config_entries.options.async_configure(result["flow_id"]) - assert result["type"] is FlowResultType.SHOW_PROGRESS - assert result["step_id"] == "start_addon" - set_addon_options.assert_called_once_with( - hass, - "core_silabs_multiprotocol", - { - "options": { - "autoflash_firmware": True, - "device": "/dev/ttyAMA1", - "baudrate": "115200", - "flow_control": True, - } - }, - ) - - await hass.async_block_till_done() - start_addon.assert_called_once_with(hass, "core_silabs_multiprotocol") - - result = await hass.config_entries.options.async_configure(result["flow_id"]) - assert result["type"] is FlowResultType.CREATE_ENTRY - - -async def test_option_flow_install_multi_pan_addon_zha( - hass: HomeAssistant, - addon_store_info, - addon_info, - install_addon, - set_addon_options, - start_addon, -) -> None: - """Test installing the multi pan addon when a zha config entry exists.""" - mock_integration(hass, MockModule("hassio")) - await async_setup_component(hass, HASSIO_DOMAIN, {}) - - # Setup the config entry - config_entry = MockConfigEntry( - data={}, - domain=DOMAIN, - options={}, - title="Home Assistant Yellow", - ) - config_entry.add_to_hass(hass) - - zha_config_entry = MockConfigEntry( - data={"device": {"path": "/dev/ttyAMA1"}, "radio_type": "ezsp"}, - domain=ZHA_DOMAIN, - options={}, - title="Yellow", - ) - zha_config_entry.add_to_hass(hass) - - result = await hass.config_entries.options.async_init(config_entry.entry_id) - assert result["type"] is FlowResultType.MENU - - with patch( - "homeassistant.components.homeassistant_hardware.silabs_multiprotocol_addon.is_hassio", - side_effect=Mock(return_value=True), - ): - result = await hass.config_entries.options.async_configure( - result["flow_id"], - {"next_step_id": "multipan_settings"}, - ) - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "addon_not_installed" - - result = await hass.config_entries.options.async_configure( - result["flow_id"], - user_input={ - "enable_multi_pan": True, - }, - ) - assert result["type"] is FlowResultType.SHOW_PROGRESS - assert result["step_id"] == "install_addon" - assert result["progress_action"] == "install_addon" - - await hass.async_block_till_done() - install_addon.assert_called_once_with(hass, "core_silabs_multiprotocol") - - result = await hass.config_entries.options.async_configure(result["flow_id"]) - assert result["type"] is FlowResultType.SHOW_PROGRESS - assert result["step_id"] == "start_addon" - set_addon_options.assert_called_once_with( - hass, - "core_silabs_multiprotocol", - { - "options": { - "autoflash_firmware": True, - "device": "/dev/ttyAMA1", - "baudrate": "115200", - "flow_control": True, - } - }, - ) - # Check the ZHA config entry data is updated - assert zha_config_entry.data == { - "device": { - "path": "socket://core-silabs-multiprotocol:9999", - "baudrate": 115200, - "flow_control": None, - }, - "radio_type": "ezsp", - } - - await hass.async_block_till_done() - start_addon.assert_called_once_with(hass, "core_silabs_multiprotocol") - - result = await hass.config_entries.options.async_configure(result["flow_id"]) - assert result["type"] is FlowResultType.CREATE_ENTRY - - @pytest.mark.parametrize( ("reboot_menu_choice", "reboot_calls"), [("reboot_now", 1), ("reboot_later", 0)], @@ -281,10 +139,12 @@ async def test_option_flow_led_settings( # Setup the config entry config_entry = MockConfigEntry( - data={}, + data={"firmware": ApplicationType.EZSP}, domain=DOMAIN, options={}, title="Home Assistant Yellow", + version=1, + minor_version=2, ) config_entry.add_to_hass(hass) @@ -327,10 +187,12 @@ async def test_option_flow_led_settings_unchanged( # Setup the config entry config_entry = MockConfigEntry( - data={}, + data={"firmware": ApplicationType.EZSP}, domain=DOMAIN, options={}, title="Home Assistant Yellow", + version=1, + minor_version=2, ) config_entry.add_to_hass(hass) @@ -359,10 +221,12 @@ async def test_option_flow_led_settings_fail_1(hass: HomeAssistant) -> None: # Setup the config entry config_entry = MockConfigEntry( - data={}, + data={"firmware": ApplicationType.EZSP}, domain=DOMAIN, options={}, title="Home Assistant Yellow", + version=1, + minor_version=2, ) config_entry.add_to_hass(hass) @@ -391,10 +255,12 @@ async def test_option_flow_led_settings_fail_2( # Setup the config entry config_entry = MockConfigEntry( - data={}, + data={"firmware": ApplicationType.EZSP}, domain=DOMAIN, options={}, title="Home Assistant Yellow", + version=1, + minor_version=2, ) config_entry.add_to_hass(hass) @@ -418,3 +284,139 @@ async def test_option_flow_led_settings_fail_2( ) assert result["type"] is FlowResultType.ABORT assert result["reason"] == "write_hw_settings_error" + + +async def test_firmware_options_flow(hass: HomeAssistant) -> None: + """Test the firmware options flow for Yellow.""" + mock_integration(hass, MockModule("hassio")) + await async_setup_component(hass, HASSIO_DOMAIN, {}) + + config_entry = MockConfigEntry( + data={"firmware": ApplicationType.SPINEL}, + domain=DOMAIN, + options={}, + title="Home Assistant Yellow", + version=1, + minor_version=2, + ) + config_entry.add_to_hass(hass) + + # First step is confirmation + result = await hass.config_entries.options.async_init(config_entry.entry_id) + assert result["type"] is FlowResultType.MENU + assert result["step_id"] == "main_menu" + assert "firmware_settings" in result["menu_options"] + + # Pick firmware settings + result = await hass.config_entries.options.async_configure( + result["flow_id"], + user_input={"next_step_id": "firmware_settings"}, + ) + + assert result["step_id"] == "pick_firmware" + assert result["description_placeholders"]["firmware_type"] == "spinel" + assert result["description_placeholders"]["model"] == "Home Assistant Yellow" + + async def mock_async_step_pick_firmware_zigbee(self, data): + return await self.async_step_confirm_zigbee(user_input={}) + + with patch( + "homeassistant.components.homeassistant_hardware.firmware_config_flow.BaseFirmwareOptionsFlow.async_step_pick_firmware_zigbee", + autospec=True, + side_effect=mock_async_step_pick_firmware_zigbee, + ): + result = await hass.config_entries.options.async_configure( + result["flow_id"], + user_input={"next_step_id": STEP_PICK_FIRMWARE_ZIGBEE}, + ) + + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["result"] is True + + assert config_entry.data == { + "firmware": "ezsp", + } + + +async def test_options_flow_multipan_uninstall(hass: HomeAssistant) -> None: + """Test options flow for when multi-PAN firmware is installed.""" + mock_integration(hass, MockModule("hassio")) + await async_setup_component(hass, HASSIO_DOMAIN, {}) + + config_entry = MockConfigEntry( + data={"firmware": ApplicationType.CPC}, + domain=DOMAIN, + options={}, + title="Home Assistant Yellow", + version=1, + minor_version=2, + ) + config_entry.add_to_hass(hass) + + # Multi-PAN addon is running + mock_multipan_manager = Mock(spec_set=await get_multiprotocol_addon_manager(hass)) + mock_multipan_manager.async_get_addon_info.return_value = AddonInfo( + available=True, + hostname=None, + options={"device": RADIO_DEVICE}, + state=AddonState.RUNNING, + update_available=False, + version="1.0.0", + ) + + mock_flasher_manager = Mock(spec_set=get_flasher_addon_manager(hass)) + mock_flasher_manager.async_get_addon_info.return_value = AddonInfo( + available=True, + hostname=None, + options={}, + state=AddonState.NOT_RUNNING, + update_available=False, + version="1.0.0", + ) + + with ( + patch( + "homeassistant.components.homeassistant_hardware.silabs_multiprotocol_addon.get_multiprotocol_addon_manager", + return_value=mock_multipan_manager, + ), + patch( + "homeassistant.components.homeassistant_hardware.silabs_multiprotocol_addon.get_flasher_addon_manager", + return_value=mock_flasher_manager, + ), + patch( + "homeassistant.components.homeassistant_hardware.silabs_multiprotocol_addon.is_hassio", + return_value=True, + ), + ): + result = await hass.config_entries.options.async_init(config_entry.entry_id) + assert result["type"] is FlowResultType.MENU + assert result["step_id"] == "main_menu" + assert "multipan_settings" in result["menu_options"] + + # Pick multi-PAN settings + result = await hass.config_entries.options.async_configure( + result["flow_id"], + user_input={"next_step_id": "multipan_settings"}, + ) + + # Pick the uninstall option + result = await hass.config_entries.options.async_configure( + result["flow_id"], + user_input={"next_step_id": "uninstall_addon"}, + ) + + # Check the box + result = await hass.config_entries.options.async_configure( + result["flow_id"], user_input={CONF_DISABLE_MULTI_PAN: True} + ) + + # Finish the flow + result = await hass.config_entries.options.async_configure(result["flow_id"]) + await hass.async_block_till_done(wait_background_tasks=True) + result = await hass.config_entries.options.async_configure(result["flow_id"]) + await hass.async_block_till_done(wait_background_tasks=True) + result = await hass.config_entries.options.async_configure(result["flow_id"]) + assert result["type"] is FlowResultType.CREATE_ENTRY + + # We've reverted the firmware back to Zigbee + assert config_entry.data["firmware"] == "ezsp" diff --git a/tests/components/homeassistant_yellow/test_init.py b/tests/components/homeassistant_yellow/test_init.py index ec3ba4e700569e..5d534dad1e7876 100644 --- a/tests/components/homeassistant_yellow/test_init.py +++ b/tests/components/homeassistant_yellow/test_init.py @@ -6,10 +6,14 @@ from homeassistant.components import zha from homeassistant.components.hassio import DOMAIN as HASSIO_DOMAIN -from homeassistant.components.hassio.handler import HassioAPIError +from homeassistant.components.homeassistant_hardware.util import ( + ApplicationType, + FirmwareGuess, +) from homeassistant.components.homeassistant_yellow.const import DOMAIN from homeassistant.config_entries import ConfigEntryState from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError from homeassistant.setup import async_setup_component from tests.common import MockConfigEntry, MockModule, mock_integration @@ -27,10 +31,12 @@ async def test_setup_entry( # Setup the config entry config_entry = MockConfigEntry( - data={}, + data={"firmware": ApplicationType.EZSP}, domain=DOMAIN, options={}, title="Home Assistant Yellow", + version=1, + minor_version=2, ) config_entry.add_to_hass(hass) with ( @@ -42,6 +48,14 @@ async def test_setup_entry( "homeassistant.components.onboarding.async_is_onboarded", return_value=onboarded, ), + patch( + "homeassistant.components.homeassistant_yellow.guess_firmware_type", + return_value=FirmwareGuess( # Nothing is setup + is_running=False, + firmware_type=ApplicationType.EZSP, + source="unknown", + ), + ), ): assert await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done(wait_background_tasks=True) @@ -74,118 +88,12 @@ async def test_setup_zha(hass: HomeAssistant, addon_store_info) -> None: # Setup the config entry config_entry = MockConfigEntry( - data={}, - domain=DOMAIN, - options={}, - title="Home Assistant Yellow", - ) - config_entry.add_to_hass(hass) - with ( - patch( - "homeassistant.components.homeassistant_yellow.get_os_info", - return_value={"board": "yellow"}, - ) as mock_get_os_info, - patch( - "homeassistant.components.onboarding.async_is_onboarded", return_value=False - ), - ): - assert await hass.config_entries.async_setup(config_entry.entry_id) - await hass.async_block_till_done(wait_background_tasks=True) - assert len(mock_get_os_info.mock_calls) == 1 - - # Finish setting up ZHA - zha_flows = hass.config_entries.flow.async_progress_by_handler("zha") - assert len(zha_flows) == 1 - assert zha_flows[0]["step_id"] == "choose_formation_strategy" - - await hass.config_entries.flow.async_configure( - zha_flows[0]["flow_id"], - user_input={"next_step_id": zha.config_flow.FORMATION_REUSE_SETTINGS}, - ) - await hass.async_block_till_done() - - config_entry = hass.config_entries.async_entries("zha")[0] - assert config_entry.data == { - "device": { - "baudrate": 115200, - "flow_control": "hardware", - "path": "/dev/ttyAMA1", - }, - "radio_type": "ezsp", - } - assert config_entry.options == {} - assert config_entry.title == "Yellow" - - -async def test_setup_zha_multipan( - hass: HomeAssistant, addon_info, addon_running -) -> None: - """Test zha gets the right config.""" - mock_integration(hass, MockModule("hassio")) - await async_setup_component(hass, HASSIO_DOMAIN, {}) - - addon_info.return_value["options"]["device"] = "/dev/ttyAMA1" - - # Setup the config entry - config_entry = MockConfigEntry( - data={}, - domain=DOMAIN, - options={}, - title="Home Assistant Yellow", - ) - config_entry.add_to_hass(hass) - with ( - patch( - "homeassistant.components.homeassistant_yellow.get_os_info", - return_value={"board": "yellow"}, - ) as mock_get_os_info, - patch( - "homeassistant.components.onboarding.async_is_onboarded", return_value=False - ), - ): - assert await hass.config_entries.async_setup(config_entry.entry_id) - await hass.async_block_till_done(wait_background_tasks=True) - assert len(mock_get_os_info.mock_calls) == 1 - - # Finish setting up ZHA - zha_flows = hass.config_entries.flow.async_progress_by_handler("zha") - assert len(zha_flows) == 1 - assert zha_flows[0]["step_id"] == "choose_formation_strategy" - - await hass.config_entries.flow.async_configure( - zha_flows[0]["flow_id"], - user_input={"next_step_id": zha.config_flow.FORMATION_REUSE_SETTINGS}, - ) - await hass.async_block_till_done() - - config_entry = hass.config_entries.async_entries("zha")[0] - assert config_entry.data == { - "device": { - "baudrate": 115200, - "flow_control": None, - "path": "socket://core-silabs-multiprotocol:9999", - }, - "radio_type": "ezsp", - } - assert config_entry.options == {} - assert config_entry.title == "Yellow Multiprotocol" - - -async def test_setup_zha_multipan_other_device( - hass: HomeAssistant, addon_info, addon_running -) -> None: - """Test zha gets the right config.""" - mock_integration(hass, MockModule("hassio")) - await async_setup_component(hass, HASSIO_DOMAIN, {}) - - addon_info.return_value["options"]["device"] = "/dev/not_yellow_radio" - - # Setup the config entry - config_entry = MockConfigEntry( - data={}, + data={"firmware": ApplicationType.EZSP}, domain=DOMAIN, options={}, title="Home Assistant Yellow", + version=1, + minor_version=2, ) config_entry.add_to_hass(hass) with ( @@ -229,10 +137,12 @@ async def test_setup_entry_no_hassio(hass: HomeAssistant) -> None: """Test setup of a config entry without hassio.""" # Setup the config entry config_entry = MockConfigEntry( - data={}, + data={"firmware": ApplicationType.EZSP}, domain=DOMAIN, options={}, title="Home Assistant Yellow", + version=1, + minor_version=2, ) config_entry.add_to_hass(hass) assert len(hass.config_entries.async_entries()) == 1 @@ -254,10 +164,12 @@ async def test_setup_entry_wrong_board(hass: HomeAssistant) -> None: # Setup the config entry config_entry = MockConfigEntry( - data={}, + data={"firmware": ApplicationType.EZSP}, domain=DOMAIN, options={}, title="Home Assistant Yellow", + version=1, + minor_version=2, ) config_entry.add_to_hass(hass) assert len(hass.config_entries.async_entries()) == 1 @@ -280,10 +192,12 @@ async def test_setup_entry_wait_hassio(hass: HomeAssistant) -> None: # Setup the config entry config_entry = MockConfigEntry( - data={}, + data={"firmware": ApplicationType.EZSP}, domain=DOMAIN, options={}, title="Home Assistant Yellow", + version=1, + minor_version=2, ) config_entry.add_to_hass(hass) with patch( @@ -303,14 +217,15 @@ async def test_setup_entry_addon_info_fails( """Test setup of a config entry when fetching addon info fails.""" mock_integration(hass, MockModule("hassio")) await async_setup_component(hass, HASSIO_DOMAIN, {}) - addon_store_info.side_effect = HassioAPIError("Boom") # Setup the config entry config_entry = MockConfigEntry( - data={}, + data={"firmware": ApplicationType.CPC}, domain=DOMAIN, options={}, title="Home Assistant Yellow", + version=1, + minor_version=2, ) config_entry.add_to_hass(hass) with ( @@ -319,41 +234,15 @@ async def test_setup_entry_addon_info_fails( return_value={"board": "yellow"}, ), patch( - "homeassistant.components.onboarding.async_is_onboarded", return_value=False - ), - ): - assert not await hass.config_entries.async_setup(config_entry.entry_id) - - await hass.async_block_till_done() - assert config_entry.state is ConfigEntryState.SETUP_RETRY - - -async def test_setup_entry_addon_not_running( - hass: HomeAssistant, addon_installed, start_addon -) -> None: - """Test the addon is started if it is not running.""" - mock_integration(hass, MockModule("hassio")) - await async_setup_component(hass, HASSIO_DOMAIN, {}) - - # Setup the config entry - config_entry = MockConfigEntry( - data={}, - domain=DOMAIN, - options={}, - title="Home Assistant Yellow", - ) - config_entry.add_to_hass(hass) - with ( - patch( - "homeassistant.components.homeassistant_yellow.get_os_info", - return_value={"board": "yellow"}, + "homeassistant.components.onboarding.async_is_onboarded", + return_value=False, ), patch( - "homeassistant.components.onboarding.async_is_onboarded", return_value=False + "homeassistant.components.homeassistant_yellow.check_multi_pan_addon", + side_effect=HomeAssistantError("Boom"), ), ): assert not await hass.config_entries.async_setup(config_entry.entry_id) - await hass.async_block_till_done() + await hass.async_block_till_done() assert config_entry.state is ConfigEntryState.SETUP_RETRY - start_addon.assert_called_once() diff --git a/tests/components/homekit_controller/fixtures/somfy_venetian_blinds.json b/tests/components/homekit_controller/fixtures/somfy_venetian_blinds.json new file mode 100644 index 00000000000000..65d3126cc4b612 --- /dev/null +++ b/tests/components/homekit_controller/fixtures/somfy_venetian_blinds.json @@ -0,0 +1,146 @@ +[ + { + "aid": 1, + "services": [ + { + "iid": 1, + "type": "0000003E-0000-1000-8000-0026BB765291", + "characteristics": [ + { + "type": "00000023-0000-1000-8000-0026BB765291", + "iid": 2, + "perms": ["pr"], + "format": "string", + "value": "VELUX Internal Cover", + "description": "Name", + "maxLen": 64 + }, + { + "type": "00000020-0000-1000-8000-0026BB765291", + "iid": 3, + "perms": ["pr"], + "format": "string", + "value": "Netatmo", + "description": "Manufacturer", + "maxLen": 64 + }, + { + "type": "00000021-0000-1000-8000-0026BB765291", + "iid": 4, + "perms": ["pr"], + "format": "string", + "value": "VELUX Internal Cover", + "description": "Model", + "maxLen": 64 + }, + { + "type": "00000030-0000-1000-8000-0026BB765291", + "iid": 5, + "perms": ["pr"], + "format": "string", + "value": "**REDACTED**", + "description": "Serial Number", + "maxLen": 64 + }, + { + "type": "00000014-0000-1000-8000-0026BB765291", + "iid": 7, + "perms": ["pw"], + "format": "bool", + "description": "Identify" + }, + { + "type": "00000052-0000-1000-8000-0026BB765291", + "iid": 6, + "perms": ["pr"], + "format": "string", + "value": "0.0.0", + "description": "Firmware Revision", + "maxLen": 64 + }, + { + "type": "00000220-0000-1000-8000-0026BB765291", + "iid": 15, + "perms": ["pr"], + "format": "data", + "value": "+nvrOv1cCQU=" + } + ] + }, + { + "iid": 8, + "type": "0000008C-0000-1000-8000-0026BB765291", + "characteristics": [ + { + "type": "00000023-0000-1000-8000-0026BB765291", + "iid": 9, + "perms": ["pr"], + "format": "string", + "value": "Venetian Blinds", + "description": "Name", + "maxLen": 64 + }, + { + "type": "0000007C-0000-1000-8000-0026BB765291", + "iid": 11, + "perms": ["pr", "pw", "ev"], + "format": "uint8", + "value": 0, + "description": "Target Position", + "unit": "percentage", + "minValue": 0, + "maxValue": 100, + "minStep": 1 + }, + { + "type": "0000006D-0000-1000-8000-0026BB765291", + "iid": 10, + "perms": ["pr", "ev"], + "format": "uint8", + "value": 0, + "description": "Current Position", + "unit": "percentage", + "minValue": 0, + "maxValue": 100, + "minStep": 1 + }, + { + "type": "00000072-0000-1000-8000-0026BB765291", + "iid": 12, + "perms": ["pr", "ev"], + "format": "uint8", + "value": 2, + "description": "Position State", + "minValue": 0, + "maxValue": 2, + "minStep": 1 + }, + { + "type": "0000006C-0000-1000-8000-0026BB765291", + "iid": 13, + "perms": ["pr", "ev"], + "format": "int", + "value": 90, + "description": "Current Horizontal Tilt Angle", + "unit": "arcdegrees", + "minValue": -90, + "maxValue": 90, + "minStep": 1 + }, + { + "type": "0000007B-0000-1000-8000-0026BB765291", + "iid": 14, + "perms": ["pr", "pw", "ev"], + "format": "int", + "value": 90, + "description": "Target Horizontal Tilt Angle", + "unit": "arcdegrees", + "minValue": -90, + "maxValue": 90, + "minStep": 1 + } + ] + } + ] + } +] diff --git a/tests/components/homekit_controller/fixtures/velux_active_netatmo_co2.json b/tests/components/homekit_controller/fixtures/velux_active_netatmo_co2.json new file mode 100644 index 00000000000000..80b2b34648eb09 --- /dev/null +++ b/tests/components/homekit_controller/fixtures/velux_active_netatmo_co2.json @@ -0,0 +1,162 @@ +[ + { + "aid": 1, + "services": [ + { + "iid": 1, + "type": "0000003E-0000-1000-8000-0026BB765291", + "characteristics": [ + { + "type": "00000023-0000-1000-8000-0026BB765291", + "iid": 2, + "perms": ["pr"], + "format": "string", + "value": "VELUX Sensor", + "description": "Name", + "maxLen": 64 + }, + { + "type": "00000020-0000-1000-8000-0026BB765291", + "iid": 3, + "perms": ["pr"], + "format": "string", + "value": "Netatmo", + "description": "Manufacturer", + "maxLen": 64 + }, + { + "type": "00000021-0000-1000-8000-0026BB765291", + "iid": 4, + "perms": ["pr"], + "format": "string", + "value": "VELUX Sensor", + "description": "Model", + "maxLen": 64 + }, + { + "type": "00000030-0000-1000-8000-0026BB765291", + "iid": 5, + "perms": ["pr"], + "format": "string", + "value": "**REDACTED**", + "description": "Serial Number", + "maxLen": 64 + }, + { + "type": "00000014-0000-1000-8000-0026BB765291", + "iid": 7, + "perms": ["pw"], + "format": "bool", + "description": "Identify" + }, + { + "type": "00000052-0000-1000-8000-0026BB765291", + "iid": 6, + "perms": ["pr"], + "format": "string", + "value": "16.0.0", + "description": "Firmware Revision", + "maxLen": 64 + }, + { + "type": "00000220-0000-1000-8000-0026BB765291", + "iid": 18, + "perms": ["pr"], + "format": "data", + "value": "+nvrOv1cCQU=" + } + ] + }, + { + "iid": 8, + "type": "0000008A-0000-1000-8000-0026BB765291", + "characteristics": [ + { + "type": "00000023-0000-1000-8000-0026BB765291", + "iid": 9, + "perms": ["pr"], + "format": "string", + "value": "Temperature sensor", + "description": "Name", + "maxLen": 64 + }, + { + "type": "00000011-0000-1000-8000-0026BB765291", + "iid": 10, + "perms": ["pr", "ev"], + "format": "float", + "value": 23.9, + "description": "Current Temperature", + "unit": "celsius", + "minValue": 0.0, + "maxValue": 50.0, + "minStep": 0.1 + } + ] + }, + { + "iid": 11, + "type": "00000082-0000-1000-8000-0026BB765291", + "characteristics": [ + { + "type": "00000023-0000-1000-8000-0026BB765291", + "iid": 12, + "perms": ["pr"], + "format": "string", + "value": "Humidity sensor", + "description": "Name", + "maxLen": 64 + }, + { + "type": "00000010-0000-1000-8000-0026BB765291", + "iid": 13, + "perms": ["pr", "ev"], + "format": "float", + "value": 69.0, + "description": "Current Relative Humidity", + "unit": "percentage", + "minValue": 0.0, + "maxValue": 100.0, + "minStep": 1.0 + } + ] + }, + { + "iid": 14, + "type": "00000097-0000-1000-8000-0026BB765291", + "characteristics": [ + { + "type": "00000023-0000-1000-8000-0026BB765291", + "iid": 15, + "perms": ["pr"], + "format": "string", + "value": "Carbon Dioxide sensor", + "description": "Name", + "maxLen": 64 + }, + { + "type": "00000092-0000-1000-8000-0026BB765291", + "iid": 16, + "perms": ["pr", "ev"], + "format": "uint8", + "value": 0, + "description": "Carbon Dioxide Detected", + "minValue": 0, + "maxValue": 1, + "minStep": 1 + }, + { + "type": "00000093-0000-1000-8000-0026BB765291", + "iid": 17, + "perms": ["pr", "ev"], + "format": "float", + "value": 1124.0, + "description": "Carbon Dioxide Level", + "minValue": 0.0, + "maxValue": 5000.0 + } + ] + } + ] + } +] diff --git a/tests/components/homekit_controller/fixtures/velux_window.json b/tests/components/homekit_controller/fixtures/velux_window.json new file mode 100644 index 00000000000000..4d9a09344bbc5d --- /dev/null +++ b/tests/components/homekit_controller/fixtures/velux_window.json @@ -0,0 +1,122 @@ +[ + { + "aid": 1, + "services": [ + { + "iid": 1, + "type": "0000003E-0000-1000-8000-0026BB765291", + "characteristics": [ + { + "type": "00000023-0000-1000-8000-0026BB765291", + "iid": 2, + "perms": ["pr"], + "format": "string", + "value": "VELUX Window", + "description": "Name", + "maxLen": 64 + }, + { + "type": "00000020-0000-1000-8000-0026BB765291", + "iid": 3, + "perms": ["pr"], + "format": "string", + "value": "Netatmo", + "description": "Manufacturer", + "maxLen": 64 + }, + { + "type": "00000021-0000-1000-8000-0026BB765291", + "iid": 4, + "perms": ["pr"], + "format": "string", + "value": "VELUX Window", + "description": "Model", + "maxLen": 64 + }, + { + "type": "00000030-0000-1000-8000-0026BB765291", + "iid": 5, + "perms": ["pr"], + "format": "string", + "value": "**REDACTED**", + "description": "Serial Number", + "maxLen": 64 + }, + { + "type": "00000014-0000-1000-8000-0026BB765291", + "iid": 7, + "perms": ["pw"], + "format": "bool", + "description": "Identify" + }, + { + "type": "00000052-0000-1000-8000-0026BB765291", + "iid": 6, + "perms": ["pr"], + "format": "string", + "value": "0.0.0", + "description": "Firmware Revision", + "maxLen": 64 + }, + { + "type": "00000220-0000-1000-8000-0026BB765291", + "iid": 13, + "perms": ["pr"], + "format": "data", + "value": "+nvrOv1cCQU=" + } + ] + }, + { + "iid": 8, + "type": "0000008B-0000-1000-8000-0026BB765291", + "characteristics": [ + { + "type": "00000023-0000-1000-8000-0026BB765291", + "iid": 9, + "perms": ["pr"], + "format": "string", + "value": "Roof Window", + "description": "Name", + "maxLen": 64 + }, + { + "type": "0000007C-0000-1000-8000-0026BB765291", + "iid": 11, + "perms": ["pr", "pw", "ev"], + "format": "uint8", + "value": 0, + "description": "Target Position", + "unit": "percentage", + "minValue": 0, + "maxValue": 100, + "minStep": 1 + }, + { + "type": "0000006D-0000-1000-8000-0026BB765291", + "iid": 10, + "perms": ["pr", "ev"], + "format": "uint8", + "value": 0, + "description": "Current Position", + "unit": "percentage", + "minValue": 0, + "maxValue": 100, + "minStep": 1 + }, + { + "type": "00000072-0000-1000-8000-0026BB765291", + "iid": 12, + "perms": ["pr", "ev"], + "format": "uint8", + "value": 2, + "description": "Position State", + "minValue": 0, + "maxValue": 2, + "minStep": 1 + } + ] + } + ] + } +] diff --git a/tests/components/homekit_controller/fixtures/velux_window_cover.json b/tests/components/homekit_controller/fixtures/velux_window_cover.json new file mode 100644 index 00000000000000..d95fbbd42bf21f --- /dev/null +++ b/tests/components/homekit_controller/fixtures/velux_window_cover.json @@ -0,0 +1,122 @@ +[ + { + "aid": 1, + "services": [ + { + "iid": 1, + "type": "0000003E-0000-1000-8000-0026BB765291", + "characteristics": [ + { + "type": "00000023-0000-1000-8000-0026BB765291", + "iid": 2, + "perms": ["pr"], + "format": "string", + "value": "VELUX External Cover", + "description": "Name", + "maxLen": 64 + }, + { + "type": "00000020-0000-1000-8000-0026BB765291", + "iid": 3, + "perms": ["pr"], + "format": "string", + "value": "Netatmo", + "description": "Manufacturer", + "maxLen": 64 + }, + { + "type": "00000021-0000-1000-8000-0026BB765291", + "iid": 4, + "perms": ["pr"], + "format": "string", + "value": "VELUX External Cover", + "description": "Model", + "maxLen": 64 + }, + { + "type": "00000030-0000-1000-8000-0026BB765291", + "iid": 5, + "perms": ["pr"], + "format": "string", + "value": "**REDACTED**", + "description": "Serial Number", + "maxLen": 64 + }, + { + "type": "00000014-0000-1000-8000-0026BB765291", + "iid": 7, + "perms": ["pw"], + "format": "bool", + "description": "Identify" + }, + { + "type": "00000052-0000-1000-8000-0026BB765291", + "iid": 6, + "perms": ["pr"], + "format": "string", + "value": "15.0.0", + "description": "Firmware Revision", + "maxLen": 64 + }, + { + "type": "00000220-0000-1000-8000-0026BB765291", + "iid": 13, + "perms": ["pr"], + "format": "data", + "value": "+nvrOv1cCQU=" + } + ] + }, + { + "iid": 8, + "type": "0000008C-0000-1000-8000-0026BB765291", + "characteristics": [ + { + "type": "00000023-0000-1000-8000-0026BB765291", + "iid": 9, + "perms": ["pr"], + "format": "string", + "value": "Awning Blinds", + "description": "Name", + "maxLen": 64 + }, + { + "type": "0000007C-0000-1000-8000-0026BB765291", + "iid": 11, + "perms": ["pr", "pw", "ev"], + "format": "uint8", + "value": 0, + "description": "Target Position", + "unit": "percentage", + "minValue": 0, + "maxValue": 100, + "minStep": 1 + }, + { + "type": "0000006D-0000-1000-8000-0026BB765291", + "iid": 10, + "perms": ["pr", "ev"], + "format": "uint8", + "value": 0, + "description": "Current Position", + "unit": "percentage", + "minValue": 0, + "maxValue": 100, + "minStep": 1 + }, + { + "type": "00000072-0000-1000-8000-0026BB765291", + "iid": 12, + "perms": ["pr", "ev"], + "format": "uint8", + "value": 2, + "description": "Position State", + "minValue": 0, + "maxValue": 2, + "minStep": 1 + } + ] + } + ] + } +] diff --git a/tests/components/homekit_controller/snapshots/test_init.ambr b/tests/components/homekit_controller/snapshots/test_init.ambr index 078ef792a55459..6a0fead65d3e25 100644 --- a/tests/components/homekit_controller/snapshots/test_init.ambr +++ b/tests/components/homekit_controller/snapshots/test_init.ambr @@ -17636,7 +17636,7 @@ }), ]) # --- -# name: test_snapshots[velux_gateway] +# name: test_snapshots[somfy_venetian_blinds] list([ dict({ 'device': dict({ @@ -17659,15 +17659,15 @@ 'is_new': False, 'labels': list([ ]), - 'manufacturer': 'VELUX', - 'model': 'VELUX Gateway', + 'manufacturer': 'Netatmo', + 'model': 'VELUX Internal Cover', 'model_id': None, - 'name': 'VELUX Gateway', + 'name': 'VELUX Internal Cover', 'name_by_user': None, 'primary_config_entry': 'TestData', - 'serial_number': 'a1a11a1', + 'serial_number': '**REDACTED**', 'suggested_area': None, - 'sw_version': '70', + 'sw_version': '0.0.0', }), 'entities': list([ dict({ @@ -17683,7 +17683,7 @@ 'disabled_by': None, 'domain': 'button', 'entity_category': , - 'entity_id': 'button.velux_gateway_identify', + 'entity_id': 'button.velux_internal_cover_identify', 'has_entity_name': False, 'hidden_by': None, 'icon': None, @@ -17694,25 +17694,72 @@ }), 'original_device_class': , 'original_icon': None, - 'original_name': 'VELUX Gateway Identify', + 'original_name': 'VELUX Internal Cover Identify', 'platform': 'homekit_controller', 'previous_unique_id': None, 'supported_features': 0, 'translation_key': None, - 'unique_id': '00:00:00:00:00:00_1_1_6', + 'unique_id': '00:00:00:00:00:00_1_1_7', 'unit_of_measurement': None, }), 'state': dict({ 'attributes': dict({ 'device_class': 'identify', - 'friendly_name': 'VELUX Gateway Identify', + 'friendly_name': 'VELUX Internal Cover Identify', }), - 'entity_id': 'button.velux_gateway_identify', + 'entity_id': 'button.velux_internal_cover_identify', 'state': 'unknown', }), }), + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': None, + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'cover', + 'entity_category': None, + 'entity_id': 'cover.velux_internal_cover_venetian_blinds', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'VELUX Internal Cover Venetian Blinds', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_1_8', + 'unit_of_measurement': None, + }), + 'state': dict({ + 'attributes': dict({ + 'current_position': 0, + 'current_tilt_position': 100, + 'friendly_name': 'VELUX Internal Cover Venetian Blinds', + 'supported_features': , + }), + 'entity_id': 'cover.velux_internal_cover_venetian_blinds', + 'state': 'closed', + }), + }), ]), }), + ]) +# --- +# name: test_snapshots[velux_active_netatmo_co2] + list([ dict({ 'device': dict({ 'area_id': None, @@ -17728,21 +17775,21 @@ 'identifiers': list([ list([ 'homekit_controller:accessory-id', - '00:00:00:00:00:00:aid:2', + '00:00:00:00:00:00:aid:1', ]), ]), 'is_new': False, 'labels': list([ ]), - 'manufacturer': 'VELUX', + 'manufacturer': 'Netatmo', 'model': 'VELUX Sensor', 'model_id': None, 'name': 'VELUX Sensor', 'name_by_user': None, 'primary_config_entry': 'TestData', - 'serial_number': 'a11b111', + 'serial_number': '**REDACTED**', 'suggested_area': None, - 'sw_version': '16', + 'sw_version': '16.0.0', }), 'entities': list([ dict({ @@ -17774,7 +17821,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': None, - 'unique_id': '00:00:00:00:00:00_2_1_7', + 'unique_id': '00:00:00:00:00:00_1_1_7', 'unit_of_measurement': None, }), 'state': dict({ @@ -17817,7 +17864,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': None, - 'unique_id': '00:00:00:00:00:00_2_14', + 'unique_id': '00:00:00:00:00:00_1_14', 'unit_of_measurement': 'ppm', }), 'state': dict({ @@ -17828,7 +17875,7 @@ 'unit_of_measurement': 'ppm', }), 'entity_id': 'sensor.velux_sensor_carbon_dioxide_sensor', - 'state': '400', + 'state': '1124.0', }), }), dict({ @@ -17862,7 +17909,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': None, - 'unique_id': '00:00:00:00:00:00_2_11', + 'unique_id': '00:00:00:00:00:00_1_11', 'unit_of_measurement': '%', }), 'state': dict({ @@ -17873,7 +17920,7 @@ 'unit_of_measurement': '%', }), 'entity_id': 'sensor.velux_sensor_humidity_sensor', - 'state': '58', + 'state': '69.0', }), }), dict({ @@ -17907,7 +17954,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': None, - 'unique_id': '00:00:00:00:00:00_2_8', + 'unique_id': '00:00:00:00:00:00_1_8', 'unit_of_measurement': , }), 'state': dict({ @@ -17918,11 +17965,15 @@ 'unit_of_measurement': , }), 'entity_id': 'sensor.velux_sensor_temperature_sensor', - 'state': '18.9', + 'state': '23.9', }), }), ]), }), + ]) +# --- +# name: test_snapshots[velux_gateway] + list([ dict({ 'device': dict({ 'area_id': None, @@ -17938,21 +17989,21 @@ 'identifiers': list([ list([ 'homekit_controller:accessory-id', - '00:00:00:00:00:00:aid:3', + '00:00:00:00:00:00:aid:1', ]), ]), 'is_new': False, 'labels': list([ ]), 'manufacturer': 'VELUX', - 'model': 'VELUX Window', + 'model': 'VELUX Gateway', 'model_id': None, - 'name': 'VELUX Window', + 'name': 'VELUX Gateway', 'name_by_user': None, 'primary_config_entry': 'TestData', - 'serial_number': '1111111a114a111a', + 'serial_number': 'a1a11a1', 'suggested_area': None, - 'sw_version': '48', + 'sw_version': '70', }), 'entities': list([ dict({ @@ -17968,7 +18019,7 @@ 'disabled_by': None, 'domain': 'button', 'entity_category': , - 'entity_id': 'button.velux_window_identify', + 'entity_id': 'button.velux_gateway_identify', 'has_entity_name': False, 'hidden_by': None, 'icon': None, @@ -17979,23 +18030,57 @@ }), 'original_device_class': , 'original_icon': None, - 'original_name': 'VELUX Window Identify', + 'original_name': 'VELUX Gateway Identify', 'platform': 'homekit_controller', 'previous_unique_id': None, 'supported_features': 0, 'translation_key': None, - 'unique_id': '00:00:00:00:00:00_3_1_7', + 'unique_id': '00:00:00:00:00:00_1_1_6', 'unit_of_measurement': None, }), 'state': dict({ 'attributes': dict({ 'device_class': 'identify', - 'friendly_name': 'VELUX Window Identify', + 'friendly_name': 'VELUX Gateway Identify', }), - 'entity_id': 'button.velux_window_identify', + 'entity_id': 'button.velux_gateway_identify', 'state': 'unknown', }), }), + ]), + }), + dict({ + 'device': dict({ + 'area_id': None, + 'config_entries': list([ + 'TestData', + ]), + 'configuration_url': None, + 'connections': list([ + ]), + 'disabled_by': None, + 'entry_type': None, + 'hw_version': '', + 'identifiers': list([ + list([ + 'homekit_controller:accessory-id', + '00:00:00:00:00:00:aid:2', + ]), + ]), + 'is_new': False, + 'labels': list([ + ]), + 'manufacturer': 'VELUX', + 'model': 'VELUX Sensor', + 'model_id': None, + 'name': 'VELUX Sensor', + 'name_by_user': None, + 'primary_config_entry': 'TestData', + 'serial_number': 'a11b111', + 'suggested_area': None, + 'sw_version': '16', + }), + 'entities': list([ dict({ 'entry': dict({ 'aliases': list([ @@ -18007,9 +18092,9 @@ 'config_entry_id': 'TestData', 'device_class': None, 'disabled_by': None, - 'domain': 'cover', - 'entity_category': None, - 'entity_id': 'cover.velux_window_roof_window', + 'domain': 'button', + 'entity_category': , + 'entity_id': 'button.velux_sensor_identify', 'has_entity_name': False, 'hidden_by': None, 'icon': None, @@ -18018,24 +18103,2150 @@ 'name': None, 'options': dict({ }), - 'original_device_class': , + 'original_device_class': , 'original_icon': None, - 'original_name': 'VELUX Window Roof Window', + 'original_name': 'VELUX Sensor Identify', 'platform': 'homekit_controller', 'previous_unique_id': None, - 'supported_features': , + 'supported_features': 0, 'translation_key': None, - 'unique_id': '00:00:00:00:00:00_3_8', + 'unique_id': '00:00:00:00:00:00_2_1_7', 'unit_of_measurement': None, }), 'state': dict({ 'attributes': dict({ - 'current_position': 0, - 'device_class': 'window', - 'friendly_name': 'VELUX Window Roof Window', - 'supported_features': , + 'device_class': 'identify', + 'friendly_name': 'VELUX Sensor Identify', }), - 'entity_id': 'cover.velux_window_roof_window', + 'entity_id': 'button.velux_sensor_identify', + 'state': 'unknown', + }), + }), + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.velux_sensor_carbon_dioxide_sensor', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'VELUX Sensor Carbon Dioxide sensor', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_2_14', + 'unit_of_measurement': 'ppm', + }), + 'state': dict({ + 'attributes': dict({ + 'device_class': 'carbon_dioxide', + 'friendly_name': 'VELUX Sensor Carbon Dioxide sensor', + 'state_class': , + 'unit_of_measurement': 'ppm', + }), + 'entity_id': 'sensor.velux_sensor_carbon_dioxide_sensor', + 'state': '400', + }), + }), + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.velux_sensor_humidity_sensor', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'VELUX Sensor Humidity sensor', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_2_11', + 'unit_of_measurement': '%', + }), + 'state': dict({ + 'attributes': dict({ + 'device_class': 'humidity', + 'friendly_name': 'VELUX Sensor Humidity sensor', + 'state_class': , + 'unit_of_measurement': '%', + }), + 'entity_id': 'sensor.velux_sensor_humidity_sensor', + 'state': '58', + }), + }), + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.velux_sensor_temperature_sensor', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'VELUX Sensor Temperature sensor', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_2_8', + 'unit_of_measurement': , + }), + 'state': dict({ + 'attributes': dict({ + 'device_class': 'temperature', + 'friendly_name': 'VELUX Sensor Temperature sensor', + 'state_class': , + 'unit_of_measurement': , + }), + 'entity_id': 'sensor.velux_sensor_temperature_sensor', + 'state': '18.9', + }), + }), + ]), + }), + dict({ + 'device': dict({ + 'area_id': None, + 'config_entries': list([ + 'TestData', + ]), + 'configuration_url': None, + 'connections': list([ + ]), + 'disabled_by': None, + 'entry_type': None, + 'hw_version': '', + 'identifiers': list([ + list([ + 'homekit_controller:accessory-id', + '00:00:00:00:00:00:aid:3', + ]), + ]), + 'is_new': False, + 'labels': list([ + ]), + 'manufacturer': 'VELUX', + 'model': 'VELUX Window', + 'model_id': None, + 'name': 'VELUX Window', + 'name_by_user': None, + 'primary_config_entry': 'TestData', + 'serial_number': '1111111a114a111a', + 'suggested_area': None, + 'sw_version': '48', + }), + 'entities': list([ + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': None, + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'button', + 'entity_category': , + 'entity_id': 'button.velux_window_identify', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'VELUX Window Identify', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_3_1_7', + 'unit_of_measurement': None, + }), + 'state': dict({ + 'attributes': dict({ + 'device_class': 'identify', + 'friendly_name': 'VELUX Window Identify', + }), + 'entity_id': 'button.velux_window_identify', + 'state': 'unknown', + }), + }), + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': None, + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'cover', + 'entity_category': None, + 'entity_id': 'cover.velux_window_roof_window', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'VELUX Window Roof Window', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_3_8', + 'unit_of_measurement': None, + }), + 'state': dict({ + 'attributes': dict({ + 'current_position': 0, + 'device_class': 'window', + 'friendly_name': 'VELUX Window Roof Window', + 'supported_features': , + }), + 'entity_id': 'cover.velux_window_roof_window', + 'state': 'closed', + }), + }), + ]), + }), + ]) +# --- +# name: test_snapshots[velux_somfy_venetian_blinds] + list([ + dict({ + 'device': dict({ + 'area_id': None, + 'config_entries': list([ + 'TestData', + ]), + 'configuration_url': None, + 'connections': list([ + ]), + 'disabled_by': None, + 'entry_type': None, + 'hw_version': '', + 'identifiers': list([ + list([ + 'homekit_controller:accessory-id', + '00:00:00:00:00:00:aid:5', + ]), + ]), + 'is_new': False, + 'labels': list([ + ]), + 'manufacturer': 'Netatmo', + 'model': 'VELUX External Cover', + 'model_id': None, + 'name': 'VELUX External Cover', + 'name_by_user': None, + 'primary_config_entry': 'TestData', + 'serial_number': '**REDACTED**', + 'suggested_area': None, + 'sw_version': '15.0.0', + }), + 'entities': list([ + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': None, + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'button', + 'entity_category': , + 'entity_id': 'button.velux_external_cover_identify', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'VELUX External Cover Identify', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_5_1_7', + 'unit_of_measurement': None, + }), + 'state': dict({ + 'attributes': dict({ + 'device_class': 'identify', + 'friendly_name': 'VELUX External Cover Identify', + }), + 'entity_id': 'button.velux_external_cover_identify', + 'state': 'unknown', + }), + }), + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': None, + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'cover', + 'entity_category': None, + 'entity_id': 'cover.velux_external_cover_awning_blinds', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'VELUX External Cover Awning Blinds', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_5_8', + 'unit_of_measurement': None, + }), + 'state': dict({ + 'attributes': dict({ + 'current_position': 0, + 'friendly_name': 'VELUX External Cover Awning Blinds', + 'supported_features': , + }), + 'entity_id': 'cover.velux_external_cover_awning_blinds', + 'state': 'closed', + }), + }), + ]), + }), + dict({ + 'device': dict({ + 'area_id': None, + 'config_entries': list([ + 'TestData', + ]), + 'configuration_url': None, + 'connections': list([ + ]), + 'disabled_by': None, + 'entry_type': None, + 'hw_version': '', + 'identifiers': list([ + list([ + 'homekit_controller:accessory-id', + '00:00:00:00:00:00:aid:8', + ]), + ]), + 'is_new': False, + 'labels': list([ + ]), + 'manufacturer': 'Netatmo', + 'model': 'VELUX External Cover', + 'model_id': None, + 'name': 'VELUX External Cover', + 'name_by_user': None, + 'primary_config_entry': 'TestData', + 'serial_number': '**REDACTED**', + 'suggested_area': None, + 'sw_version': '0.0.0', + }), + 'entities': list([ + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': None, + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'button', + 'entity_category': , + 'entity_id': 'button.velux_external_cover_identify_2', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'VELUX External Cover Identify', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_8_1_7', + 'unit_of_measurement': None, + }), + 'state': dict({ + 'attributes': dict({ + 'device_class': 'identify', + 'friendly_name': 'VELUX External Cover Identify', + }), + 'entity_id': 'button.velux_external_cover_identify_2', + 'state': 'unknown', + }), + }), + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': None, + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'cover', + 'entity_category': None, + 'entity_id': 'cover.velux_external_cover_awning_blinds_2', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'VELUX External Cover Awning Blinds', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_8_8', + 'unit_of_measurement': None, + }), + 'state': dict({ + 'attributes': dict({ + 'current_position': 45, + 'friendly_name': 'VELUX External Cover Awning Blinds', + 'supported_features': , + }), + 'entity_id': 'cover.velux_external_cover_awning_blinds_2', + 'state': 'open', + }), + }), + ]), + }), + dict({ + 'device': dict({ + 'area_id': None, + 'config_entries': list([ + 'TestData', + ]), + 'configuration_url': None, + 'connections': list([ + ]), + 'disabled_by': None, + 'entry_type': None, + 'hw_version': '', + 'identifiers': list([ + list([ + 'homekit_controller:accessory-id', + '00:00:00:00:00:00:aid:11', + ]), + ]), + 'is_new': False, + 'labels': list([ + ]), + 'manufacturer': 'Netatmo', + 'model': 'VELUX External Cover', + 'model_id': None, + 'name': 'VELUX External Cover', + 'name_by_user': None, + 'primary_config_entry': 'TestData', + 'serial_number': '**REDACTED**', + 'suggested_area': None, + 'sw_version': '15.0.0', + }), + 'entities': list([ + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': None, + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'button', + 'entity_category': , + 'entity_id': 'button.velux_external_cover_identify_3', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'VELUX External Cover Identify', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_11_1_7', + 'unit_of_measurement': None, + }), + 'state': dict({ + 'attributes': dict({ + 'device_class': 'identify', + 'friendly_name': 'VELUX External Cover Identify', + }), + 'entity_id': 'button.velux_external_cover_identify_3', + 'state': 'unknown', + }), + }), + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': None, + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'cover', + 'entity_category': None, + 'entity_id': 'cover.velux_external_cover_awning_blinds_3', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'VELUX External Cover Awning Blinds', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_11_8', + 'unit_of_measurement': None, + }), + 'state': dict({ + 'attributes': dict({ + 'current_position': 0, + 'friendly_name': 'VELUX External Cover Awning Blinds', + 'supported_features': , + }), + 'entity_id': 'cover.velux_external_cover_awning_blinds_3', + 'state': 'closed', + }), + }), + ]), + }), + dict({ + 'device': dict({ + 'area_id': None, + 'config_entries': list([ + 'TestData', + ]), + 'configuration_url': None, + 'connections': list([ + ]), + 'disabled_by': None, + 'entry_type': None, + 'hw_version': '', + 'identifiers': list([ + list([ + 'homekit_controller:accessory-id', + '00:00:00:00:00:00:aid:12', + ]), + ]), + 'is_new': False, + 'labels': list([ + ]), + 'manufacturer': 'Netatmo', + 'model': 'VELUX External Cover', + 'model_id': None, + 'name': 'VELUX External Cover', + 'name_by_user': None, + 'primary_config_entry': 'TestData', + 'serial_number': '**REDACTED**', + 'suggested_area': None, + 'sw_version': '15.0.0', + }), + 'entities': list([ + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': None, + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'button', + 'entity_category': , + 'entity_id': 'button.velux_external_cover_identify_4', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'VELUX External Cover Identify', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_12_1_7', + 'unit_of_measurement': None, + }), + 'state': dict({ + 'attributes': dict({ + 'device_class': 'identify', + 'friendly_name': 'VELUX External Cover Identify', + }), + 'entity_id': 'button.velux_external_cover_identify_4', + 'state': 'unknown', + }), + }), + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': None, + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'cover', + 'entity_category': None, + 'entity_id': 'cover.velux_external_cover_awning_blinds_4', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'VELUX External Cover Awning Blinds', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_12_8', + 'unit_of_measurement': None, + }), + 'state': dict({ + 'attributes': dict({ + 'current_position': 0, + 'friendly_name': 'VELUX External Cover Awning Blinds', + 'supported_features': , + }), + 'entity_id': 'cover.velux_external_cover_awning_blinds_4', + 'state': 'closed', + }), + }), + ]), + }), + dict({ + 'device': dict({ + 'area_id': None, + 'config_entries': list([ + 'TestData', + ]), + 'configuration_url': None, + 'connections': list([ + ]), + 'disabled_by': None, + 'entry_type': None, + 'hw_version': '', + 'identifiers': list([ + list([ + 'homekit_controller:accessory-id', + '00:00:00:00:00:00:aid:1', + ]), + ]), + 'is_new': False, + 'labels': list([ + ]), + 'manufacturer': 'Netatmo', + 'model': 'VELUX Gateway', + 'model_id': None, + 'name': 'VELUX Gateway', + 'name_by_user': None, + 'primary_config_entry': 'TestData', + 'serial_number': '**REDACTED**', + 'suggested_area': None, + 'sw_version': '132.0.0', + }), + 'entities': list([ + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': None, + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'button', + 'entity_category': , + 'entity_id': 'button.velux_gateway_identify', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'VELUX Gateway Identify', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_1_1_6', + 'unit_of_measurement': None, + }), + 'state': dict({ + 'attributes': dict({ + 'device_class': 'identify', + 'friendly_name': 'VELUX Gateway Identify', + }), + 'entity_id': 'button.velux_gateway_identify', + 'state': 'unknown', + }), + }), + ]), + }), + dict({ + 'device': dict({ + 'area_id': None, + 'config_entries': list([ + 'TestData', + ]), + 'configuration_url': None, + 'connections': list([ + ]), + 'disabled_by': None, + 'entry_type': None, + 'hw_version': '', + 'identifiers': list([ + list([ + 'homekit_controller:accessory-id', + '00:00:00:00:00:00:aid:9', + ]), + ]), + 'is_new': False, + 'labels': list([ + ]), + 'manufacturer': 'Netatmo', + 'model': 'VELUX Internal Cover', + 'model_id': None, + 'name': 'VELUX Internal Cover', + 'name_by_user': None, + 'primary_config_entry': 'TestData', + 'serial_number': '**REDACTED**', + 'suggested_area': None, + 'sw_version': '0.0.0', + }), + 'entities': list([ + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': None, + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'button', + 'entity_category': , + 'entity_id': 'button.velux_internal_cover_identify', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'VELUX Internal Cover Identify', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_9_1_7', + 'unit_of_measurement': None, + }), + 'state': dict({ + 'attributes': dict({ + 'device_class': 'identify', + 'friendly_name': 'VELUX Internal Cover Identify', + }), + 'entity_id': 'button.velux_internal_cover_identify', + 'state': 'unknown', + }), + }), + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': None, + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'cover', + 'entity_category': None, + 'entity_id': 'cover.velux_internal_cover_venetian_blinds', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'VELUX Internal Cover Venetian Blinds', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_9_8', + 'unit_of_measurement': None, + }), + 'state': dict({ + 'attributes': dict({ + 'current_position': 0, + 'current_tilt_position': 100, + 'friendly_name': 'VELUX Internal Cover Venetian Blinds', + 'supported_features': , + }), + 'entity_id': 'cover.velux_internal_cover_venetian_blinds', + 'state': 'closed', + }), + }), + ]), + }), + dict({ + 'device': dict({ + 'area_id': None, + 'config_entries': list([ + 'TestData', + ]), + 'configuration_url': None, + 'connections': list([ + ]), + 'disabled_by': None, + 'entry_type': None, + 'hw_version': '', + 'identifiers': list([ + list([ + 'homekit_controller:accessory-id', + '00:00:00:00:00:00:aid:13', + ]), + ]), + 'is_new': False, + 'labels': list([ + ]), + 'manufacturer': 'Netatmo', + 'model': 'VELUX Internal Cover', + 'model_id': None, + 'name': 'VELUX Internal Cover', + 'name_by_user': None, + 'primary_config_entry': 'TestData', + 'serial_number': '**REDACTED**', + 'suggested_area': None, + 'sw_version': '0.0.0', + }), + 'entities': list([ + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': None, + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'button', + 'entity_category': , + 'entity_id': 'button.velux_internal_cover_identify_2', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'VELUX Internal Cover Identify', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_13_1_7', + 'unit_of_measurement': None, + }), + 'state': dict({ + 'attributes': dict({ + 'device_class': 'identify', + 'friendly_name': 'VELUX Internal Cover Identify', + }), + 'entity_id': 'button.velux_internal_cover_identify_2', + 'state': 'unknown', + }), + }), + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': None, + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'cover', + 'entity_category': None, + 'entity_id': 'cover.velux_internal_cover_venetian_blinds_2', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'VELUX Internal Cover Venetian Blinds', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_13_8', + 'unit_of_measurement': None, + }), + 'state': dict({ + 'attributes': dict({ + 'current_position': 100, + 'current_tilt_position': 0, + 'friendly_name': 'VELUX Internal Cover Venetian Blinds', + 'supported_features': , + }), + 'entity_id': 'cover.velux_internal_cover_venetian_blinds_2', + 'state': 'open', + }), + }), + ]), + }), + dict({ + 'device': dict({ + 'area_id': None, + 'config_entries': list([ + 'TestData', + ]), + 'configuration_url': None, + 'connections': list([ + ]), + 'disabled_by': None, + 'entry_type': None, + 'hw_version': '', + 'identifiers': list([ + list([ + 'homekit_controller:accessory-id', + '00:00:00:00:00:00:aid:14', + ]), + ]), + 'is_new': False, + 'labels': list([ + ]), + 'manufacturer': 'Netatmo', + 'model': 'VELUX Internal Cover', + 'model_id': None, + 'name': 'VELUX Internal Cover', + 'name_by_user': None, + 'primary_config_entry': 'TestData', + 'serial_number': '**REDACTED**', + 'suggested_area': None, + 'sw_version': '0.0.0', + }), + 'entities': list([ + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': None, + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'button', + 'entity_category': , + 'entity_id': 'button.velux_internal_cover_identify_3', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'VELUX Internal Cover Identify', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_14_1_7', + 'unit_of_measurement': None, + }), + 'state': dict({ + 'attributes': dict({ + 'device_class': 'identify', + 'friendly_name': 'VELUX Internal Cover Identify', + }), + 'entity_id': 'button.velux_internal_cover_identify_3', + 'state': 'unknown', + }), + }), + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': None, + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'cover', + 'entity_category': None, + 'entity_id': 'cover.velux_internal_cover_venetian_blinds_3', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'VELUX Internal Cover Venetian Blinds', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_14_8', + 'unit_of_measurement': None, + }), + 'state': dict({ + 'attributes': dict({ + 'current_position': 0, + 'current_tilt_position': 100, + 'friendly_name': 'VELUX Internal Cover Venetian Blinds', + 'supported_features': , + }), + 'entity_id': 'cover.velux_internal_cover_venetian_blinds_3', + 'state': 'closed', + }), + }), + ]), + }), + dict({ + 'device': dict({ + 'area_id': None, + 'config_entries': list([ + 'TestData', + ]), + 'configuration_url': None, + 'connections': list([ + ]), + 'disabled_by': None, + 'entry_type': None, + 'hw_version': '', + 'identifiers': list([ + list([ + 'homekit_controller:accessory-id', + '00:00:00:00:00:00:aid:15', + ]), + ]), + 'is_new': False, + 'labels': list([ + ]), + 'manufacturer': 'Netatmo', + 'model': 'VELUX Internal Cover', + 'model_id': None, + 'name': 'VELUX Internal Cover', + 'name_by_user': None, + 'primary_config_entry': 'TestData', + 'serial_number': '**REDACTED**', + 'suggested_area': None, + 'sw_version': '0.0.0', + }), + 'entities': list([ + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': None, + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'button', + 'entity_category': , + 'entity_id': 'button.velux_internal_cover_identify_4', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'VELUX Internal Cover Identify', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_15_1_7', + 'unit_of_measurement': None, + }), + 'state': dict({ + 'attributes': dict({ + 'device_class': 'identify', + 'friendly_name': 'VELUX Internal Cover Identify', + }), + 'entity_id': 'button.velux_internal_cover_identify_4', + 'state': 'unknown', + }), + }), + ]), + }), + dict({ + 'device': dict({ + 'area_id': None, + 'config_entries': list([ + 'TestData', + ]), + 'configuration_url': None, + 'connections': list([ + ]), + 'disabled_by': None, + 'entry_type': None, + 'hw_version': '', + 'identifiers': list([ + list([ + 'homekit_controller:accessory-id', + '00:00:00:00:00:00:aid:2', + ]), + ]), + 'is_new': False, + 'labels': list([ + ]), + 'manufacturer': 'Netatmo', + 'model': 'VELUX Sensor', + 'model_id': None, + 'name': 'VELUX Sensor', + 'name_by_user': None, + 'primary_config_entry': 'TestData', + 'serial_number': '**REDACTED**', + 'suggested_area': None, + 'sw_version': '16.0.0', + }), + 'entities': list([ + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': None, + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'button', + 'entity_category': , + 'entity_id': 'button.velux_sensor_identify', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'VELUX Sensor Identify', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_2_1_7', + 'unit_of_measurement': None, + }), + 'state': dict({ + 'attributes': dict({ + 'device_class': 'identify', + 'friendly_name': 'VELUX Sensor Identify', + }), + 'entity_id': 'button.velux_sensor_identify', + 'state': 'unknown', + }), + }), + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.velux_sensor_carbon_dioxide_sensor', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'VELUX Sensor Carbon Dioxide sensor', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_2_14', + 'unit_of_measurement': 'ppm', + }), + 'state': dict({ + 'attributes': dict({ + 'device_class': 'carbon_dioxide', + 'friendly_name': 'VELUX Sensor Carbon Dioxide sensor', + 'state_class': , + 'unit_of_measurement': 'ppm', + }), + 'entity_id': 'sensor.velux_sensor_carbon_dioxide_sensor', + 'state': '1124.0', + }), + }), + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.velux_sensor_humidity_sensor', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'VELUX Sensor Humidity sensor', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_2_11', + 'unit_of_measurement': '%', + }), + 'state': dict({ + 'attributes': dict({ + 'device_class': 'humidity', + 'friendly_name': 'VELUX Sensor Humidity sensor', + 'state_class': , + 'unit_of_measurement': '%', + }), + 'entity_id': 'sensor.velux_sensor_humidity_sensor', + 'state': '69.0', + }), + }), + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.velux_sensor_temperature_sensor', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'VELUX Sensor Temperature sensor', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_2_8', + 'unit_of_measurement': , + }), + 'state': dict({ + 'attributes': dict({ + 'device_class': 'temperature', + 'friendly_name': 'VELUX Sensor Temperature sensor', + 'state_class': , + 'unit_of_measurement': , + }), + 'entity_id': 'sensor.velux_sensor_temperature_sensor', + 'state': '23.9', + }), + }), + ]), + }), + dict({ + 'device': dict({ + 'area_id': None, + 'config_entries': list([ + 'TestData', + ]), + 'configuration_url': None, + 'connections': list([ + ]), + 'disabled_by': None, + 'entry_type': None, + 'hw_version': '', + 'identifiers': list([ + list([ + 'homekit_controller:accessory-id', + '00:00:00:00:00:00:aid:3', + ]), + ]), + 'is_new': False, + 'labels': list([ + ]), + 'manufacturer': 'Netatmo', + 'model': 'VELUX Sensor', + 'model_id': None, + 'name': 'VELUX Sensor', + 'name_by_user': None, + 'primary_config_entry': 'TestData', + 'serial_number': '**REDACTED**', + 'suggested_area': None, + 'sw_version': '16.0.0', + }), + 'entities': list([ + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': None, + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'button', + 'entity_category': , + 'entity_id': 'button.velux_sensor_identify_2', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'VELUX Sensor Identify', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_3_1_7', + 'unit_of_measurement': None, + }), + 'state': dict({ + 'attributes': dict({ + 'device_class': 'identify', + 'friendly_name': 'VELUX Sensor Identify', + }), + 'entity_id': 'button.velux_sensor_identify_2', + 'state': 'unknown', + }), + }), + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.velux_sensor_carbon_dioxide_sensor_2', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'VELUX Sensor Carbon Dioxide sensor', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_3_14', + 'unit_of_measurement': 'ppm', + }), + 'state': dict({ + 'attributes': dict({ + 'device_class': 'carbon_dioxide', + 'friendly_name': 'VELUX Sensor Carbon Dioxide sensor', + 'state_class': , + 'unit_of_measurement': 'ppm', + }), + 'entity_id': 'sensor.velux_sensor_carbon_dioxide_sensor_2', + 'state': '1074.0', + }), + }), + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.velux_sensor_humidity_sensor_2', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'VELUX Sensor Humidity sensor', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_3_11', + 'unit_of_measurement': '%', + }), + 'state': dict({ + 'attributes': dict({ + 'device_class': 'humidity', + 'friendly_name': 'VELUX Sensor Humidity sensor', + 'state_class': , + 'unit_of_measurement': '%', + }), + 'entity_id': 'sensor.velux_sensor_humidity_sensor_2', + 'state': '64.0', + }), + }), + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.velux_sensor_temperature_sensor_2', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'VELUX Sensor Temperature sensor', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_3_8', + 'unit_of_measurement': , + }), + 'state': dict({ + 'attributes': dict({ + 'device_class': 'temperature', + 'friendly_name': 'VELUX Sensor Temperature sensor', + 'state_class': , + 'unit_of_measurement': , + }), + 'entity_id': 'sensor.velux_sensor_temperature_sensor_2', + 'state': '24.5', + }), + }), + ]), + }), + dict({ + 'device': dict({ + 'area_id': None, + 'config_entries': list([ + 'TestData', + ]), + 'configuration_url': None, + 'connections': list([ + ]), + 'disabled_by': None, + 'entry_type': None, + 'hw_version': '', + 'identifiers': list([ + list([ + 'homekit_controller:accessory-id', + '00:00:00:00:00:00:aid:4', + ]), + ]), + 'is_new': False, + 'labels': list([ + ]), + 'manufacturer': 'Netatmo', + 'model': 'VELUX Window', + 'model_id': None, + 'name': 'VELUX Window', + 'name_by_user': None, + 'primary_config_entry': 'TestData', + 'serial_number': '**REDACTED**', + 'suggested_area': None, + 'sw_version': '0.0.0', + }), + 'entities': list([ + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': None, + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'button', + 'entity_category': , + 'entity_id': 'button.velux_window_identify', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'VELUX Window Identify', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_4_1_7', + 'unit_of_measurement': None, + }), + 'state': dict({ + 'attributes': dict({ + 'device_class': 'identify', + 'friendly_name': 'VELUX Window Identify', + }), + 'entity_id': 'button.velux_window_identify', + 'state': 'unknown', + }), + }), + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': None, + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'cover', + 'entity_category': None, + 'entity_id': 'cover.velux_window_roof_window', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'VELUX Window Roof Window', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_4_8', + 'unit_of_measurement': None, + }), + 'state': dict({ + 'attributes': dict({ + 'current_position': 0, + 'device_class': 'window', + 'friendly_name': 'VELUX Window Roof Window', + 'supported_features': , + }), + 'entity_id': 'cover.velux_window_roof_window', + 'state': 'closed', + }), + }), + ]), + }), + dict({ + 'device': dict({ + 'area_id': None, + 'config_entries': list([ + 'TestData', + ]), + 'configuration_url': None, + 'connections': list([ + ]), + 'disabled_by': None, + 'entry_type': None, + 'hw_version': '', + 'identifiers': list([ + list([ + 'homekit_controller:accessory-id', + '00:00:00:00:00:00:aid:7', + ]), + ]), + 'is_new': False, + 'labels': list([ + ]), + 'manufacturer': 'Netatmo', + 'model': 'VELUX Window', + 'model_id': None, + 'name': 'VELUX Window', + 'name_by_user': None, + 'primary_config_entry': 'TestData', + 'serial_number': '**REDACTED**', + 'suggested_area': None, + 'sw_version': '0.0.0', + }), + 'entities': list([ + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': None, + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'button', + 'entity_category': , + 'entity_id': 'button.velux_window_identify_2', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'VELUX Window Identify', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_7_1_7', + 'unit_of_measurement': None, + }), + 'state': dict({ + 'attributes': dict({ + 'device_class': 'identify', + 'friendly_name': 'VELUX Window Identify', + }), + 'entity_id': 'button.velux_window_identify_2', + 'state': 'unknown', + }), + }), + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': None, + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'cover', + 'entity_category': None, + 'entity_id': 'cover.velux_window_roof_window_2', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'VELUX Window Roof Window', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_7_8', + 'unit_of_measurement': None, + }), + 'state': dict({ + 'attributes': dict({ + 'current_position': 0, + 'device_class': 'window', + 'friendly_name': 'VELUX Window Roof Window', + 'supported_features': , + }), + 'entity_id': 'cover.velux_window_roof_window_2', + 'state': 'closed', + }), + }), + ]), + }), + ]) +# --- +# name: test_snapshots[velux_window] + list([ + dict({ + 'device': dict({ + 'area_id': None, + 'config_entries': list([ + 'TestData', + ]), + 'configuration_url': None, + 'connections': list([ + ]), + 'disabled_by': None, + 'entry_type': None, + 'hw_version': '', + 'identifiers': list([ + list([ + 'homekit_controller:accessory-id', + '00:00:00:00:00:00:aid:1', + ]), + ]), + 'is_new': False, + 'labels': list([ + ]), + 'manufacturer': 'Netatmo', + 'model': 'VELUX Window', + 'model_id': None, + 'name': 'VELUX Window', + 'name_by_user': None, + 'primary_config_entry': 'TestData', + 'serial_number': '**REDACTED**', + 'suggested_area': None, + 'sw_version': '0.0.0', + }), + 'entities': list([ + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': None, + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'button', + 'entity_category': , + 'entity_id': 'button.velux_window_identify', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'VELUX Window Identify', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_1_1_7', + 'unit_of_measurement': None, + }), + 'state': dict({ + 'attributes': dict({ + 'device_class': 'identify', + 'friendly_name': 'VELUX Window Identify', + }), + 'entity_id': 'button.velux_window_identify', + 'state': 'unknown', + }), + }), + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': None, + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'cover', + 'entity_category': None, + 'entity_id': 'cover.velux_window_roof_window', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'VELUX Window Roof Window', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_1_8', + 'unit_of_measurement': None, + }), + 'state': dict({ + 'attributes': dict({ + 'current_position': 0, + 'device_class': 'window', + 'friendly_name': 'VELUX Window Roof Window', + 'supported_features': , + }), + 'entity_id': 'cover.velux_window_roof_window', + 'state': 'closed', + }), + }), + ]), + }), + ]) +# --- +# name: test_snapshots[velux_window_cover] + list([ + dict({ + 'device': dict({ + 'area_id': None, + 'config_entries': list([ + 'TestData', + ]), + 'configuration_url': None, + 'connections': list([ + ]), + 'disabled_by': None, + 'entry_type': None, + 'hw_version': '', + 'identifiers': list([ + list([ + 'homekit_controller:accessory-id', + '00:00:00:00:00:00:aid:1', + ]), + ]), + 'is_new': False, + 'labels': list([ + ]), + 'manufacturer': 'Netatmo', + 'model': 'VELUX External Cover', + 'model_id': None, + 'name': 'VELUX External Cover', + 'name_by_user': None, + 'primary_config_entry': 'TestData', + 'serial_number': '**REDACTED**', + 'suggested_area': None, + 'sw_version': '15.0.0', + }), + 'entities': list([ + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': None, + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'button', + 'entity_category': , + 'entity_id': 'button.velux_external_cover_identify', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'VELUX External Cover Identify', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_1_1_7', + 'unit_of_measurement': None, + }), + 'state': dict({ + 'attributes': dict({ + 'device_class': 'identify', + 'friendly_name': 'VELUX External Cover Identify', + }), + 'entity_id': 'button.velux_external_cover_identify', + 'state': 'unknown', + }), + }), + dict({ + 'entry': dict({ + 'aliases': list([ + ]), + 'area_id': None, + 'capabilities': None, + 'categories': dict({ + }), + 'config_entry_id': 'TestData', + 'device_class': None, + 'disabled_by': None, + 'domain': 'cover', + 'entity_category': None, + 'entity_id': 'cover.velux_external_cover_awning_blinds', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'labels': list([ + ]), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'VELUX External Cover Awning Blinds', + 'platform': 'homekit_controller', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': None, + 'unique_id': '00:00:00:00:00:00_1_8', + 'unit_of_measurement': None, + }), + 'state': dict({ + 'attributes': dict({ + 'current_position': 0, + 'friendly_name': 'VELUX External Cover Awning Blinds', + 'supported_features': , + }), + 'entity_id': 'cover.velux_external_cover_awning_blinds', 'state': 'closed', }), }), diff --git a/tests/components/homekit_controller/test_connection.py b/tests/components/homekit_controller/test_connection.py index 503ff1715336fa..7ea791f9a1e0f9 100644 --- a/tests/components/homekit_controller/test_connection.py +++ b/tests/components/homekit_controller/test_connection.py @@ -12,6 +12,7 @@ import pytest from homeassistant.components.homekit_controller.const import ( + DEBOUNCE_COOLDOWN, DOMAIN, IDENTIFIER_ACCESSORY_ID, IDENTIFIER_LEGACY_ACCESSORY_ID, @@ -22,12 +23,14 @@ from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import device_registry as dr, entity_registry as er +from homeassistant.helpers.entity_component import async_update_entity from .common import ( setup_accessories_from_file, setup_platform, setup_test_accessories, setup_test_component, + time_changed, ) from tests.common import MockConfigEntry @@ -399,3 +402,40 @@ def _create_accessory(accessory: Accessory) -> Service: state = await helper.poll_and_get_state() assert state.state == STATE_OFF assert mock_get_characteristics.call_count == 8 + + +async def test_manual_poll_all_chars( + hass: HomeAssistant, get_next_aid: Callable[[], int] +) -> None: + """Test that a manual poll will check all chars.""" + + def _create_accessory(accessory: Accessory) -> Service: + service = accessory.add_service(ServicesTypes.LIGHTBULB, name="TestDevice") + + on_char = service.add_char(CharacteristicsTypes.ON) + on_char.value = 0 + + brightness = service.add_char(CharacteristicsTypes.BRIGHTNESS) + brightness.value = 0 + + return service + + helper = await setup_test_component(hass, get_next_aid(), _create_accessory) + + with mock.patch.object( + helper.pairing, + "get_characteristics", + wraps=helper.pairing.get_characteristics, + ) as mock_get_characteristics: + # Initial state is that the light is off + await helper.poll_and_get_state() + # Verify only firmware version is polled + assert mock_get_characteristics.call_args_list[0][0][0] == {(1, 7)} + + # Now do a manual poll to ensure all chars are polled + mock_get_characteristics.reset_mock() + await async_update_entity(hass, helper.entity_id) + await time_changed(hass, 60) + await time_changed(hass, DEBOUNCE_COOLDOWN) + await hass.async_block_till_done() + assert len(mock_get_characteristics.call_args_list[0][0][0]) > 1 diff --git a/tests/components/homekit_controller/test_cover.py b/tests/components/homekit_controller/test_cover.py index 7415e97a4b1c0d..11870c801e187d 100644 --- a/tests/components/homekit_controller/test_cover.py +++ b/tests/components/homekit_controller/test_cover.py @@ -116,6 +116,32 @@ def create_window_covering_service_with_none_tilt(accessory: Accessory) -> None: tilt_target.maxValue = 0 +def create_window_covering_service_with_no_minmax_tilt(accessory): + """Apply use values (-90 to 90) if min/max not provided.""" + service = create_window_covering_service(accessory) + + tilt_current = service.add_char(CharacteristicsTypes.HORIZONTAL_TILT_CURRENT) + tilt_current.value = 0 + + tilt_target = service.add_char(CharacteristicsTypes.HORIZONTAL_TILT_TARGET) + tilt_target.value = 0 + + +def create_window_covering_service_with_full_range_tilt(accessory): + """Somfi Velux Integration.""" + service = create_window_covering_service(accessory) + + tilt_current = service.add_char(CharacteristicsTypes.HORIZONTAL_TILT_CURRENT) + tilt_current.value = 0 + tilt_current.minValue = -90 + tilt_current.maxValue = 90 + + tilt_target = service.add_char(CharacteristicsTypes.HORIZONTAL_TILT_TARGET) + tilt_target.value = 0 + tilt_target.minValue = -90 + tilt_target.maxValue = 90 + + async def test_change_window_cover_state( hass: HomeAssistant, get_next_aid: Callable[[], int] ) -> None: @@ -267,6 +293,40 @@ async def test_read_window_cover_tilt_missing_tilt( assert state.state != STATE_UNAVAILABLE +async def test_read_window_cover_tilt_full_range( + hass: HomeAssistant, get_next_aid: Callable[[], int] +) -> None: + """Test that horizontal tilt is handled correctly.""" + helper = await setup_test_component( + hass, get_next_aid(), create_window_covering_service_with_full_range_tilt + ) + + await helper.async_update( + ServicesTypes.WINDOW_COVERING, + {CharacteristicsTypes.HORIZONTAL_TILT_CURRENT: 0}, + ) + state = await helper.poll_and_get_state() + # Expect converted value from arcdegree scale to percentage scale. + assert state.attributes["current_tilt_position"] == 50 + + +async def test_read_window_cover_tilt_no_minmax( + hass: HomeAssistant, get_next_aid: Callable[[], int] +) -> None: + """Test that horizontal tilt is handled correctly.""" + helper = await setup_test_component( + hass, get_next_aid(), create_window_covering_service_with_no_minmax_tilt + ) + + await helper.async_update( + ServicesTypes.WINDOW_COVERING, + {CharacteristicsTypes.HORIZONTAL_TILT_CURRENT: 90}, + ) + state = await helper.poll_and_get_state() + # Expect converted value from arcdegree scale to percentage scale. + assert state.attributes["current_tilt_position"] == 100 + + async def test_write_window_cover_tilt_horizontal( hass: HomeAssistant, get_next_aid: Callable[[], int] ) -> None: @@ -359,6 +419,29 @@ async def test_write_window_cover_tilt_vertical_2( ) +async def test_write_window_cover_tilt_no_minmax( + hass: HomeAssistant, get_next_aid: Callable[[], int] +) -> None: + """Test that horizontal tilt is written correctly.""" + helper = await setup_test_component( + hass, get_next_aid(), create_window_covering_service_with_no_minmax_tilt + ) + + await hass.services.async_call( + "cover", + "set_cover_tilt_position", + {"entity_id": helper.entity_id, "tilt_position": 90}, + blocking=True, + ) + # Expect converted value from percentage scale to arcdegree scale. + helper.async_assert_service_values( + ServicesTypes.WINDOW_COVERING, + { + CharacteristicsTypes.HORIZONTAL_TILT_TARGET: 72, + }, + ) + + async def test_window_cover_stop( hass: HomeAssistant, get_next_aid: Callable[[], int] ) -> None: @@ -378,6 +461,57 @@ async def test_window_cover_stop( ) +async def test_write_window_cover_tilt_full_range( + hass: HomeAssistant, get_next_aid: Callable[[], int] +) -> None: + """Test that full-range tilt is working correctly.""" + helper = await setup_test_component( + hass, get_next_aid(), create_window_covering_service_with_full_range_tilt + ) + + await hass.services.async_call( + "cover", + "set_cover_tilt_position", + {"entity_id": helper.entity_id, "tilt_position": 10}, + blocking=True, + ) + # Expect converted value from percentage scale to arc on -90 to +90 scale. + helper.async_assert_service_values( + ServicesTypes.WINDOW_COVERING, + { + CharacteristicsTypes.HORIZONTAL_TILT_TARGET: -72, + }, + ) + + await hass.services.async_call( + "cover", + "set_cover_tilt_position", + {"entity_id": helper.entity_id, "tilt_position": 50}, + blocking=True, + ) + # Expect converted value from percentage scale to arc on -90 to +90 scale. + helper.async_assert_service_values( + ServicesTypes.WINDOW_COVERING, + { + CharacteristicsTypes.HORIZONTAL_TILT_TARGET: 0, + }, + ) + + await hass.services.async_call( + "cover", + "set_cover_tilt_position", + {"entity_id": helper.entity_id, "tilt_position": 90}, + blocking=True, + ) + # Expect converted value from percentage scale to arc on -90 to +90 scale. + helper.async_assert_service_values( + ServicesTypes.WINDOW_COVERING, + { + CharacteristicsTypes.HORIZONTAL_TILT_TARGET: 72, + }, + ) + + def create_garage_door_opener_service(accessory: Accessory) -> None: """Define a garage-door-opener chars as per page 217 of HAP spec.""" service = accessory.add_service(ServicesTypes.GARAGE_DOOR_OPENER) diff --git a/tests/components/homewizard/snapshots/test_sensor.ambr b/tests/components/homewizard/snapshots/test_sensor.ambr index dd50b098d407a1..5d5b458dccc4c3 100644 --- a/tests/components/homewizard/snapshots/test_sensor.ambr +++ b/tests/components/homewizard/snapshots/test_sensor.ambr @@ -1,37 +1,4 @@ # serializer version: 1 -# name: test_gas_meter_migrated[sensor.homewizard_aabbccddeeff_total_gas_m3:entity-registry] - EntityRegistryEntrySnapshot({ - 'aliases': set({ - }), - 'area_id': None, - 'capabilities': None, - 'config_entry_id': , - 'device_class': None, - 'device_id': , - 'disabled_by': None, - 'domain': 'sensor', - 'entity_category': None, - 'entity_id': 'sensor.homewizard_aabbccddeeff_total_gas_m3', - 'has_entity_name': False, - 'hidden_by': None, - 'icon': None, - 'id': , - 'labels': set({ - }), - 'name': None, - 'options': dict({ - }), - 'original_device_class': None, - 'original_icon': None, - 'original_name': None, - 'platform': 'homewizard', - 'previous_unique_id': 'aabbccddeeff_total_gas_m3', - 'supported_features': 0, - 'translation_key': None, - 'unique_id': 'homewizard_gas_meter_01FFEEDDCCBBAA99887766554433221100', - 'unit_of_measurement': None, - }) -# --- # name: test_sensors[HWE-KWH1-entity_ids7][sensor.device_apparent_power:device-registry] DeviceRegistryEntrySnapshot({ 'area_id': None, diff --git a/tests/components/homewizard/test_config_flow.py b/tests/components/homewizard/test_config_flow.py index 8d12a8a1787de8..442659f2aad2de 100644 --- a/tests/components/homewizard/test_config_flow.py +++ b/tests/components/homewizard/test_config_flow.py @@ -341,13 +341,7 @@ async def test_reauth_flow( """Test reauth flow while API is enabled.""" mock_config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": mock_config_entry.entry_id, - }, - ) + result = await mock_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -367,13 +361,7 @@ async def test_reauth_error( mock_homewizardenergy.device.side_effect = DisabledError mock_config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": mock_config_entry.entry_id, - }, - ) + result = await mock_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/homewizard/test_sensor.py b/tests/components/homewizard/test_sensor.py index abcd6a879c5945..c180c2a4defbc8 100644 --- a/tests/components/homewizard/test_sensor.py +++ b/tests/components/homewizard/test_sensor.py @@ -7,14 +7,13 @@ import pytest from syrupy.assertion import SnapshotAssertion -from homeassistant.components.homewizard import DOMAIN from homeassistant.components.homewizard.const import UPDATE_INTERVAL -from homeassistant.const import STATE_UNAVAILABLE, Platform +from homeassistant.const import STATE_UNAVAILABLE from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr, entity_registry as er import homeassistant.util.dt as dt_util -from tests.common import MockConfigEntry, async_fire_time_changed +from tests.common import async_fire_time_changed pytestmark = [ pytest.mark.usefixtures("init_integration"), @@ -815,49 +814,3 @@ async def test_entities_not_created_for_device( """Ensures entities for a specific device are not created.""" for entity_id in entity_ids: assert not hass.states.get(entity_id) - - -async def test_gas_meter_migrated( - hass: HomeAssistant, - entity_registry: er.EntityRegistry, - init_integration: MockConfigEntry, - snapshot: SnapshotAssertion, -) -> None: - """Test old gas meter sensor is migrated.""" - entity_registry.async_get_or_create( - Platform.SENSOR, - DOMAIN, - "aabbccddeeff_total_gas_m3", - ) - - await hass.config_entries.async_reload(init_integration.entry_id) - await hass.async_block_till_done() - - entity_id = "sensor.homewizard_aabbccddeeff_total_gas_m3" - - assert (entity_entry := entity_registry.async_get(entity_id)) - assert snapshot(name=f"{entity_id}:entity-registry") == entity_entry - - # Make really sure this happens - assert entity_entry.previous_unique_id == "aabbccddeeff_total_gas_m3" - - -async def test_gas_unique_id_removed( - hass: HomeAssistant, - entity_registry: er.EntityRegistry, - init_integration: MockConfigEntry, - snapshot: SnapshotAssertion, -) -> None: - """Test old gas meter id sensor is removed.""" - entity_registry.async_get_or_create( - Platform.SENSOR, - DOMAIN, - "aabbccddeeff_gas_unique_id", - ) - - await hass.config_entries.async_reload(init_integration.entry_id) - await hass.async_block_till_done() - - entity_id = "sensor.homewizard_aabbccddeeff_gas_unique_id" - - assert not entity_registry.async_get(entity_id) diff --git a/tests/components/honeywell/snapshots/test_climate.ambr b/tests/components/honeywell/snapshots/test_climate.ambr index 25bb73851c6857..f26064b335a20e 100644 --- a/tests/components/honeywell/snapshots/test_climate.ambr +++ b/tests/components/honeywell/snapshots/test_climate.ambr @@ -1,7 +1,6 @@ # serializer version: 1 # name: test_static_attributes ReadOnlyDict({ - 'aux_heat': 'off', 'current_humidity': 50, 'current_temperature': 20, 'fan_action': 'idle', @@ -30,7 +29,7 @@ 'away', 'hold', ]), - 'supported_features': , + 'supported_features': , 'target_temp_high': None, 'target_temp_low': None, 'temperature': None, diff --git a/tests/components/honeywell/test_climate.py b/tests/components/honeywell/test_climate.py index 55a55f7d7e7967..9485f2f4302da9 100644 --- a/tests/components/honeywell/test_climate.py +++ b/tests/components/honeywell/test_climate.py @@ -10,7 +10,6 @@ from syrupy.filters import props from homeassistant.components.climate import ( - ATTR_AUX_HEAT, ATTR_FAN_MODE, ATTR_HVAC_MODE, ATTR_PRESET_MODE, @@ -22,7 +21,6 @@ FAN_ON, PRESET_AWAY, PRESET_NONE, - SERVICE_SET_AUX_HEAT, SERVICE_SET_FAN_MODE, SERVICE_SET_HVAC_MODE, SERVICE_SET_PRESET_MODE, @@ -40,7 +38,6 @@ ATTR_TEMPERATURE, SERVICE_TURN_OFF, SERVICE_TURN_ON, - STATE_UNAVAILABLE, Platform, ) from homeassistant.core import HomeAssistant @@ -221,53 +218,6 @@ async def test_mode_service_calls( ) -async def test_auxheat_service_calls( - hass: HomeAssistant, device: MagicMock, config_entry: MagicMock -) -> None: - """Test controlling the auxheat through service calls.""" - await init_integration(hass, config_entry) - entity_id = f"climate.{device.name}" - - await hass.services.async_call( - CLIMATE_DOMAIN, - SERVICE_SET_AUX_HEAT, - {ATTR_ENTITY_ID: entity_id, ATTR_AUX_HEAT: True}, - blocking=True, - ) - device.set_system_mode.assert_called_once_with("emheat") - - device.set_system_mode.reset_mock() - - await hass.services.async_call( - CLIMATE_DOMAIN, - SERVICE_SET_AUX_HEAT, - {ATTR_ENTITY_ID: entity_id, ATTR_AUX_HEAT: False}, - blocking=True, - ) - device.set_system_mode.assert_called_once_with("heat") - - device.set_system_mode.reset_mock() - device.set_system_mode.side_effect = aiosomecomfort.SomeComfortError - with pytest.raises(HomeAssistantError): - await hass.services.async_call( - CLIMATE_DOMAIN, - SERVICE_SET_AUX_HEAT, - {ATTR_ENTITY_ID: entity_id, ATTR_AUX_HEAT: True}, - blocking=True, - ) - device.set_system_mode.assert_called_once_with("emheat") - - device.set_system_mode.reset_mock() - device.set_system_mode.side_effect = aiosomecomfort.SomeComfortError - with pytest.raises(HomeAssistantError): - await hass.services.async_call( - CLIMATE_DOMAIN, - SERVICE_SET_AUX_HEAT, - {ATTR_ENTITY_ID: entity_id, ATTR_AUX_HEAT: False}, - blocking=True, - ) - - async def test_fan_modes_service_calls( hass: HomeAssistant, device: MagicMock, config_entry: MagicMock ) -> None: @@ -1240,37 +1190,6 @@ async def test_async_update_errors( assert state.state == "unavailable" -async def test_aux_heat_off_service_call( - hass: HomeAssistant, - entity_registry: er.EntityRegistry, - device: MagicMock, - config_entry: MagicMock, -) -> None: - """Test aux heat off turns of system when no heat configured.""" - device.raw_ui_data["SwitchHeatAllowed"] = False - device.raw_ui_data["SwitchAutoAllowed"] = False - device.raw_ui_data["SwitchEmergencyHeatAllowed"] = True - - await init_integration(hass, config_entry) - - entity_id = f"climate.{device.name}" - entry = entity_registry.async_get(entity_id) - assert entry - - state = hass.states.get(entity_id) - assert state is not None - assert state.state != STATE_UNAVAILABLE - assert state.state == HVACMode.OFF - - await hass.services.async_call( - CLIMATE_DOMAIN, - SERVICE_SET_AUX_HEAT, - {ATTR_ENTITY_ID: entity_id, ATTR_AUX_HEAT: False}, - blocking=True, - ) - device.set_system_mode.assert_called_once_with("off") - - async def test_unique_id( hass: HomeAssistant, device: MagicMock, diff --git a/tests/components/honeywell/test_config_flow.py b/tests/components/honeywell/test_config_flow.py index 7cd987f0d8325c..ed9c86f5e10381 100644 --- a/tests/components/honeywell/test_config_flow.py +++ b/tests/components/honeywell/test_config_flow.py @@ -10,7 +10,7 @@ CONF_HEAT_AWAY_TEMPERATURE, DOMAIN, ) -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER, ConfigEntryState +from homeassistant.config_entries import SOURCE_USER, ConfigEntryState from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -129,21 +129,7 @@ async def test_reauth_flow(hass: HomeAssistant) -> None: unique_id="test-username", ) mock_entry.add_to_hass(hass) - with patch( - "homeassistant.components.honeywell.async_setup_entry", - return_value=True, - ): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": mock_entry.unique_id, - "entry_id": mock_entry.entry_id, - }, - data={CONF_USERNAME: "test-username", CONF_PASSWORD: "new-password"}, - ) - - await hass.async_block_till_done() + result = await mock_entry.start_reauth_flow(hass) assert result["step_id"] == "reauth_confirm" assert result["type"] is FlowResultType.FORM @@ -177,16 +163,7 @@ async def test_reauth_flow_auth_error(hass: HomeAssistant, client: MagicMock) -> ) mock_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": mock_entry.unique_id, - "entry_id": mock_entry.entry_id, - }, - data={CONF_USERNAME: "test-username", CONF_PASSWORD: "new-password"}, - ) - await hass.async_block_till_done() + result = await mock_entry.start_reauth_flow(hass) assert result["step_id"] == "reauth_confirm" assert result["type"] is FlowResultType.FORM @@ -226,17 +203,7 @@ async def test_reauth_flow_connnection_error( unique_id="test-username", ) mock_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": mock_entry.unique_id, - "entry_id": mock_entry.entry_id, - }, - data={CONF_USERNAME: "test-username", CONF_PASSWORD: "new-password"}, - ) - await hass.async_block_till_done() - + result = await mock_entry.start_reauth_flow(hass) assert result["step_id"] == "reauth_confirm" assert result["type"] is FlowResultType.FORM assert result["errors"] == {} diff --git a/tests/components/html5/test_config_flow.py b/tests/components/html5/test_config_flow.py new file mode 100644 index 00000000000000..ca0b3da03893e0 --- /dev/null +++ b/tests/components/html5/test_config_flow.py @@ -0,0 +1,203 @@ +"""Test the HTML5 config flow.""" + +from unittest.mock import patch + +import pytest + +from homeassistant import config_entries, data_entry_flow +from homeassistant.components.html5.const import ( + ATTR_VAPID_EMAIL, + ATTR_VAPID_PRV_KEY, + ATTR_VAPID_PUB_KEY, + DOMAIN, +) +from homeassistant.components.html5.issues import ( + FAILED_IMPORT_TRANSLATION_KEY, + SUCCESSFUL_IMPORT_TRANSLATION_KEY, +) +from homeassistant.const import CONF_NAME +from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant +import homeassistant.helpers.issue_registry as ir + +MOCK_CONF = { + ATTR_VAPID_EMAIL: "test@example.com", + ATTR_VAPID_PRV_KEY: "h6acSRds8_KR8hT9djD8WucTL06Gfe29XXyZ1KcUjN8", +} +MOCK_CONF_PUB_KEY = "BIUtPN7Rq_8U7RBEqClZrfZ5dR9zPCfvxYPtLpWtRVZTJEc7lzv2dhzDU6Aw1m29Ao0-UA1Uq6XO9Df8KALBKqA" + + +async def test_step_user_success(hass: HomeAssistant) -> None: + """Test a successful user config flow.""" + + with patch( + "homeassistant.components.html5.async_setup_entry", + return_value=True, + ) as mock_setup_entry: + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": config_entries.SOURCE_USER}, + data=MOCK_CONF.copy(), + ) + + await hass.async_block_till_done() + + assert result["type"] is data_entry_flow.FlowResultType.CREATE_ENTRY + assert result["data"] == { + ATTR_VAPID_PRV_KEY: MOCK_CONF[ATTR_VAPID_PRV_KEY], + ATTR_VAPID_PUB_KEY: MOCK_CONF_PUB_KEY, + ATTR_VAPID_EMAIL: MOCK_CONF[ATTR_VAPID_EMAIL], + CONF_NAME: DOMAIN, + } + + assert mock_setup_entry.call_count == 1 + + +async def test_step_user_success_generate(hass: HomeAssistant) -> None: + """Test a successful user config flow, generating a key pair.""" + + with patch( + "homeassistant.components.html5.async_setup_entry", + return_value=True, + ) as mock_setup_entry: + conf = {ATTR_VAPID_EMAIL: MOCK_CONF[ATTR_VAPID_EMAIL]} + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER}, data=conf + ) + + await hass.async_block_till_done() + + assert result["type"] is data_entry_flow.FlowResultType.CREATE_ENTRY + assert result["data"][ATTR_VAPID_EMAIL] == MOCK_CONF[ATTR_VAPID_EMAIL] + + assert mock_setup_entry.call_count == 1 + + +async def test_step_user_new_form(hass: HomeAssistant) -> None: + """Test new user input.""" + + with patch( + "homeassistant.components.html5.async_setup_entry", + return_value=True, + ) as mock_setup_entry: + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER}, data=None + ) + + await hass.async_block_till_done() + + assert result["type"] is data_entry_flow.FlowResultType.FORM + assert mock_setup_entry.call_count == 0 + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], MOCK_CONF + ) + assert result["type"] is data_entry_flow.FlowResultType.CREATE_ENTRY + assert mock_setup_entry.call_count == 1 + + +@pytest.mark.parametrize( + ("key", "value"), + [ + (ATTR_VAPID_PRV_KEY, "invalid"), + ], +) +async def test_step_user_form_invalid_key( + hass: HomeAssistant, key: str, value: str +) -> None: + """Test invalid user input.""" + + with patch( + "homeassistant.components.html5.async_setup_entry", + return_value=True, + ) as mock_setup_entry: + bad_conf = MOCK_CONF.copy() + bad_conf[key] = value + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER}, data=bad_conf + ) + + await hass.async_block_till_done() + + assert result["type"] == data_entry_flow.FlowResultType.FORM + assert mock_setup_entry.call_count == 0 + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], MOCK_CONF + ) + assert result["type"] is data_entry_flow.FlowResultType.CREATE_ENTRY + assert mock_setup_entry.call_count == 1 + + +async def test_step_import_good( + hass: HomeAssistant, + issue_registry: ir.IssueRegistry, +) -> None: + """Test valid import input.""" + + with ( + patch( + "homeassistant.components.html5.async_setup_entry", + return_value=True, + ) as mock_setup_entry, + ): + conf = MOCK_CONF.copy() + conf[ATTR_VAPID_PUB_KEY] = MOCK_CONF_PUB_KEY + conf["random_key"] = "random_value" + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data=conf + ) + + await hass.async_block_till_done() + + assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY + assert result["data"] == { + ATTR_VAPID_PRV_KEY: conf[ATTR_VAPID_PRV_KEY], + ATTR_VAPID_PUB_KEY: MOCK_CONF_PUB_KEY, + ATTR_VAPID_EMAIL: conf[ATTR_VAPID_EMAIL], + CONF_NAME: DOMAIN, + } + + assert mock_setup_entry.call_count == 1 + assert len(issue_registry.issues) == 1 + issue = issue_registry.async_get_issue( + HOMEASSISTANT_DOMAIN, f"deprecated_yaml_{DOMAIN}" + ) + assert issue + assert issue.translation_key == SUCCESSFUL_IMPORT_TRANSLATION_KEY + + +@pytest.mark.parametrize( + ("key", "value"), + [ + (ATTR_VAPID_PRV_KEY, "invalid"), + ], +) +async def test_step_import_bad( + hass: HomeAssistant, issue_registry: ir.IssueRegistry, key: str, value: str +) -> None: + """Test invalid import input.""" + + with ( + patch( + "homeassistant.components.html5.async_setup_entry", + return_value=True, + ) as mock_setup_entry, + ): + bad_conf = MOCK_CONF.copy() + bad_conf[key] = value + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data=bad_conf + ) + + await hass.async_block_till_done() + + assert result["type"] == data_entry_flow.FlowResultType.ABORT + assert mock_setup_entry.call_count == 0 + + assert len(issue_registry.issues) == 1 + issue = issue_registry.async_get_issue(DOMAIN, f"deprecated_yaml_{DOMAIN}") + assert issue + assert issue.translation_key == FAILED_IMPORT_TRANSLATION_KEY diff --git a/tests/components/html5/test_init.py b/tests/components/html5/test_init.py new file mode 100644 index 00000000000000..290cb381296f37 --- /dev/null +++ b/tests/components/html5/test_init.py @@ -0,0 +1,44 @@ +"""Test the HTML5 setup.""" + +from homeassistant.core import HomeAssistant +import homeassistant.helpers.issue_registry as ir +from homeassistant.setup import async_setup_component + +from tests.common import MockConfigEntry + +NOTIFY_CONF = { + "notify": [ + { + "platform": "html5", + "name": "html5", + "vapid_pub_key": "BIUtPN7Rq_8U7RBEqClZrfZ5dR9zPCfvxYPtLpWtRVZTJEc7lzv2dhzDU6Aw1m29Ao0-UA1Uq6XO9Df8KALBKqA", + "vapid_prv_key": "h6acSRds8_KR8hT9djD8WucTL06Gfe29XXyZ1KcUjN8", + "vapid_email": "test@example.com", + } + ] +} + + +async def test_setup_entry( + hass: HomeAssistant, + issue_registry: ir.IssueRegistry, +) -> None: + """Test setup of a good config entry.""" + config_entry = MockConfigEntry(domain="html5", data={}) + config_entry.add_to_hass(hass) + assert await async_setup_component(hass, "html5", {}) + + assert len(issue_registry.issues) == 0 + + +async def test_setup_entry_issue( + hass: HomeAssistant, + issue_registry: ir.IssueRegistry, +) -> None: + """Test setup of an imported config entry with deprecated YAML.""" + config_entry = MockConfigEntry(domain="html5", data={}) + config_entry.add_to_hass(hass) + assert await async_setup_component(hass, "notify", NOTIFY_CONF) + assert await async_setup_component(hass, "html5", NOTIFY_CONF) + + assert len(issue_registry.issues) == 1 diff --git a/tests/components/html5/test_notify.py b/tests/components/html5/test_notify.py index 42ca6067418dd8..85a790c06104f6 100644 --- a/tests/components/html5/test_notify.py +++ b/tests/components/html5/test_notify.py @@ -94,7 +94,7 @@ async def test_get_service_with_no_json(hass: HomeAssistant) -> None: await async_setup_component(hass, "http", {}) m = mock_open() with patch("homeassistant.util.json.open", m, create=True): - service = await html5.async_get_service(hass, VAPID_CONF) + service = await html5.async_get_service(hass, {}, VAPID_CONF) assert service is not None @@ -109,7 +109,7 @@ async def test_dismissing_message(mock_wp, hass: HomeAssistant) -> None: m = mock_open(read_data=json.dumps(data)) with patch("homeassistant.util.json.open", m, create=True): - service = await html5.async_get_service(hass, VAPID_CONF) + service = await html5.async_get_service(hass, {}, VAPID_CONF) service.hass = hass assert service is not None @@ -138,7 +138,7 @@ async def test_sending_message(mock_wp, hass: HomeAssistant) -> None: m = mock_open(read_data=json.dumps(data)) with patch("homeassistant.util.json.open", m, create=True): - service = await html5.async_get_service(hass, VAPID_CONF) + service = await html5.async_get_service(hass, {}, VAPID_CONF) service.hass = hass assert service is not None @@ -169,7 +169,7 @@ async def test_fcm_key_include(mock_wp, hass: HomeAssistant) -> None: m = mock_open(read_data=json.dumps(data)) with patch("homeassistant.util.json.open", m, create=True): - service = await html5.async_get_service(hass, VAPID_CONF) + service = await html5.async_get_service(hass, {}, VAPID_CONF) service.hass = hass assert service is not None @@ -194,7 +194,7 @@ async def test_fcm_send_with_unknown_priority(mock_wp, hass: HomeAssistant) -> N m = mock_open(read_data=json.dumps(data)) with patch("homeassistant.util.json.open", m, create=True): - service = await html5.async_get_service(hass, VAPID_CONF) + service = await html5.async_get_service(hass, {}, VAPID_CONF) service.hass = hass assert service is not None @@ -219,7 +219,7 @@ async def test_fcm_no_targets(mock_wp, hass: HomeAssistant) -> None: m = mock_open(read_data=json.dumps(data)) with patch("homeassistant.util.json.open", m, create=True): - service = await html5.async_get_service(hass, VAPID_CONF) + service = await html5.async_get_service(hass, {}, VAPID_CONF) service.hass = hass assert service is not None @@ -244,7 +244,7 @@ async def test_fcm_additional_data(mock_wp, hass: HomeAssistant) -> None: m = mock_open(read_data=json.dumps(data)) with patch("homeassistant.util.json.open", m, create=True): - service = await html5.async_get_service(hass, VAPID_CONF) + service = await html5.async_get_service(hass, {}, VAPID_CONF) service.hass = hass assert service is not None @@ -479,7 +479,7 @@ async def test_callback_view_with_jwt( mock_wp().send().status_code = 201 await hass.services.async_call( "notify", - "notify", + "html5", {"message": "Hello", "target": ["device"], "data": {"icon": "beer.png"}}, blocking=True, ) @@ -516,7 +516,7 @@ async def test_send_fcm_without_targets( mock_wp().send().status_code = 201 await hass.services.async_call( "notify", - "notify", + "html5", {"message": "Hello", "target": ["device"], "data": {"icon": "beer.png"}}, blocking=True, ) @@ -541,7 +541,7 @@ async def test_send_fcm_expired( mock_wp().send().status_code = 410 await hass.services.async_call( "notify", - "notify", + "html5", {"message": "Hello", "target": ["device"], "data": {"icon": "beer.png"}}, blocking=True, ) @@ -566,7 +566,7 @@ async def test_send_fcm_expired_save_fails( mock_wp().send().status_code = 410 await hass.services.async_call( "notify", - "notify", + "html5", {"message": "Hello", "target": ["device"], "data": {"icon": "beer.png"}}, blocking=True, ) diff --git a/tests/components/huawei_lte/test_config_flow.py b/tests/components/huawei_lte/test_config_flow.py index 862af02963c6da..a9a147eb17e7b5 100644 --- a/tests/components/huawei_lte/test_config_flow.py +++ b/tests/components/huawei_lte/test_config_flow.py @@ -385,15 +385,7 @@ async def test_reauth( ) entry.add_to_hass(hass) - context = { - "source": config_entries.SOURCE_REAUTH, - "unique_id": entry.unique_id, - "entry_id": entry.entry_id, - } - result = await hass.config_entries.flow.async_init( - DOMAIN, context=context, data=entry.data - ) - + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" assert result["data_schema"] is not None diff --git a/tests/components/hue/fixtures/v2_resources.json b/tests/components/hue/fixtures/v2_resources.json index 980086d098885c..3d718f24c5052e 100644 --- a/tests/components/hue/fixtures/v2_resources.json +++ b/tests/components/hue/fixtures/v2_resources.json @@ -1288,7 +1288,9 @@ }, { "button": { - "last_event": "short_release" + "button_report": { + "event": "short_release" + } }, "id": "c658d3d8-a013-4b81-8ac6-78b248537e70", "id_v1": "/sensors/50", @@ -1327,7 +1329,9 @@ }, { "button": { - "last_event": "short_release" + "button_report": { + "event": "short_release" + } }, "id": "7f1ab9f6-cc2b-4b40-9011-65e2af153f75", "id_v1": "/sensors/10", @@ -1366,7 +1370,9 @@ }, { "button": { - "last_event": "short_release" + "button_report": { + "event": "short_release" + } }, "id": "31cffcda-efc2-401f-a152-e10db3eed232", "id_v1": "/sensors/5", diff --git a/tests/components/husqvarna_automower/test_lawn_mower.py b/tests/components/husqvarna_automower/test_lawn_mower.py index 2ae427e0e1e7e4..552a3a6a9cf176 100644 --- a/tests/components/husqvarna_automower/test_lawn_mower.py +++ b/tests/components/husqvarna_automower/test_lawn_mower.py @@ -44,6 +44,7 @@ async def test_lawn_mower_states( ("UNKNOWN", "PAUSED", LawnMowerActivity.PAUSED), ("MOWING", "NOT_APPLICABLE", LawnMowerActivity.MOWING), ("NOT_APPLICABLE", "ERROR", LawnMowerActivity.ERROR), + ("GOING_HOME", "IN_OPERATION", LawnMowerActivity.RETURNING), ): values[TEST_MOWER_ID].mower.activity = activity values[TEST_MOWER_ID].mower.state = state diff --git a/tests/components/hyperion/__init__.py b/tests/components/hyperion/__init__.py index 72aba96e81f3a2..36137ce0ddde59 100644 --- a/tests/components/hyperion/__init__.py +++ b/tests/components/hyperion/__init__.py @@ -124,9 +124,9 @@ def add_test_config_entry( hass: HomeAssistant, data: dict[str, Any] | None = None, options: dict[str, Any] | None = None, -) -> ConfigEntry: +) -> MockConfigEntry: """Add a test config entry.""" - config_entry: MockConfigEntry = MockConfigEntry( + config_entry = MockConfigEntry( entry_id=TEST_CONFIG_ENTRY_ID, domain=DOMAIN, data=data diff --git a/tests/components/hyperion/test_config_flow.py b/tests/components/hyperion/test_config_flow.py index fb4fa1fe67112d..4109fe0f653147 100644 --- a/tests/components/hyperion/test_config_flow.py +++ b/tests/components/hyperion/test_config_flow.py @@ -20,7 +20,7 @@ DOMAIN, ) from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_SSDP, SOURCE_USER +from homeassistant.config_entries import SOURCE_SSDP, SOURCE_USER from homeassistant.const import ( ATTR_ENTITY_ID, CONF_HOST, @@ -861,12 +861,7 @@ async def test_reauth_success(hass: HomeAssistant) -> None: ), patch("homeassistant.components.hyperion.async_setup_entry", return_value=True), ): - result = await _init_flow( - hass, - source=SOURCE_REAUTH, - data=config_data, - ) - await hass.async_block_till_done() + result = await config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM result = await _configure_flow( @@ -886,18 +881,13 @@ async def test_reauth_cannot_connect(hass: HomeAssistant) -> None: CONF_PORT: TEST_PORT, } - add_test_config_entry(hass, data=config_data) + config_entry = add_test_config_entry(hass, data=config_data) client = create_mock_client() client.async_client_connect = AsyncMock(return_value=False) with patch( "homeassistant.components.hyperion.client.HyperionClient", return_value=client ): - result = await _init_flow( - hass, - source=SOURCE_REAUTH, - data=config_data, - ) - await hass.async_block_till_done() + result = await config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.ABORT assert result["reason"] == "cannot_connect" diff --git a/tests/components/icloud/test_config_flow.py b/tests/components/icloud/test_config_flow.py index ec8d11f1135d02..c0bc5d7ed2e3f9 100644 --- a/tests/components/icloud/test_config_flow.py +++ b/tests/components/icloud/test_config_flow.py @@ -18,7 +18,7 @@ DEFAULT_WITH_FAMILY, DOMAIN, ) -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_USER from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -386,12 +386,7 @@ async def test_password_update( ) config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH, "unique_id": config_entry.unique_id}, - data={**MOCK_CONFIG}, - ) - + result = await config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM result = await hass.config_entries.flow.async_configure( @@ -410,12 +405,7 @@ async def test_password_update_wrong_password(hass: HomeAssistant) -> None: ) config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH, "unique_id": config_entry.unique_id}, - data={**MOCK_CONFIG}, - ) - + result = await config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM with patch( diff --git a/tests/components/imap/test_config_flow.py b/tests/components/imap/test_config_flow.py index 459cecec4a6bf0..fb97bf0505d5d3 100644 --- a/tests/components/imap/test_config_flow.py +++ b/tests/components/imap/test_config_flow.py @@ -215,15 +215,7 @@ async def test_reauth_success(hass: HomeAssistant, mock_setup_entry: AsyncMock) ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - }, - data=MOCK_CONFIG, - ) - + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" assert result["description_placeholders"] == {CONF_USERNAME: "email@email.com"} @@ -256,15 +248,7 @@ async def test_reauth_failed(hass: HomeAssistant) -> None: ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - }, - data=MOCK_CONFIG, - ) - + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -294,15 +278,7 @@ async def test_reauth_failed_conn_error(hass: HomeAssistant) -> None: ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - }, - data=MOCK_CONFIG, - ) - + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/intellifire/__init__.py b/tests/components/intellifire/__init__.py index f655ccc2fa451e..50497939f7f367 100644 --- a/tests/components/intellifire/__init__.py +++ b/tests/components/intellifire/__init__.py @@ -1 +1,13 @@ """Tests for the IntelliFire integration.""" + +from homeassistant.core import HomeAssistant + +from tests.common import MockConfigEntry + + +async def setup_integration(hass: HomeAssistant, config_entry: MockConfigEntry) -> None: + """Fixture for setting up the component.""" + config_entry.add_to_hass(hass) + + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() diff --git a/tests/components/intellifire/conftest.py b/tests/components/intellifire/conftest.py index cf1e085c10fa5f..0bd7073ee47f3b 100644 --- a/tests/components/intellifire/conftest.py +++ b/tests/components/intellifire/conftest.py @@ -1,11 +1,37 @@ """Fixtures for IntelliFire integration tests.""" from collections.abc import Generator -from unittest.mock import AsyncMock, MagicMock, Mock, patch +from unittest.mock import AsyncMock, MagicMock, Mock, PropertyMock, patch -from aiohttp.client_reqrep import ConnectionKey +from intellifire4py.const import IntelliFireApiMode +from intellifire4py.model import ( + IntelliFireCommonFireplaceData, + IntelliFirePollData, + IntelliFireUserData, +) import pytest +from homeassistant.components.intellifire.const import ( + API_MODE_CLOUD, + API_MODE_LOCAL, + CONF_AUTH_COOKIE, + CONF_CONTROL_MODE, + CONF_READ_MODE, + CONF_SERIAL, + CONF_USER_ID, + CONF_WEB_CLIENT_ID, + DOMAIN, +) +from homeassistant.const import ( + CONF_API_KEY, + CONF_HOST, + CONF_IP_ADDRESS, + CONF_PASSWORD, + CONF_USERNAME, +) + +from tests.common import MockConfigEntry, load_json_object_fixture + @pytest.fixture def mock_setup_entry() -> Generator[AsyncMock]: @@ -22,39 +48,201 @@ def mock_fireplace_finder_none() -> Generator[MagicMock]: mock_found_fireplaces = Mock() mock_found_fireplaces.ips = [] with patch( - "homeassistant.components.intellifire.config_flow.AsyncUDPFireplaceFinder.search_fireplace" + "homeassistant.components.intellifire.config_flow.UDPFireplaceFinder.search_fireplace" ): yield mock_found_fireplaces @pytest.fixture -def mock_fireplace_finder_single() -> Generator[MagicMock]: - """Mock fireplace finder.""" - mock_found_fireplaces = Mock() - mock_found_fireplaces.ips = ["192.168.1.69"] - with patch( - "homeassistant.components.intellifire.config_flow.AsyncUDPFireplaceFinder.search_fireplace" - ): - yield mock_found_fireplaces +def mock_config_entry_current() -> MockConfigEntry: + """Return a mock config entry.""" + return MockConfigEntry( + domain=DOMAIN, + version=1, + minor_version=2, + data={ + CONF_IP_ADDRESS: "192.168.2.108", + CONF_USERNAME: "grumpypanda@china.cn", + CONF_PASSWORD: "you-stole-my-pandas", + CONF_SERIAL: "3FB284769E4736F30C8973A7ED358123", + CONF_WEB_CLIENT_ID: "FA2B1C3045601234D0AE17D72F8E975", + CONF_API_KEY: "B5C4DA27AAEF31D1FB21AFF9BFA6BCD2", + CONF_AUTH_COOKIE: "B984F21A6378560019F8A1CDE41B6782", + CONF_USER_ID: "52C3F9E8B9D3AC99F8E4D12345678901FE9A2BC7D85F7654E28BF98BCD123456", + }, + options={CONF_READ_MODE: API_MODE_LOCAL, CONF_CONTROL_MODE: API_MODE_CLOUD}, + unique_id="3FB284769E4736F30C8973A7ED358123", + ) + + +@pytest.fixture +def mock_config_entry_old() -> MockConfigEntry: + """For migration testing.""" + return MockConfigEntry( + domain=DOMAIN, + version=1, + minor_version=1, + title="Fireplace 3FB284769E4736F30C8973A7ED358123", + data={ + CONF_HOST: "192.168.2.108", + CONF_USERNAME: "grumpypanda@china.cn", + CONF_PASSWORD: "you-stole-my-pandas", + CONF_USER_ID: "52C3F9E8B9D3AC99F8E4D12345678901FE9A2BC7D85F7654E28BF98BCD123456", + }, + ) + + +@pytest.fixture +def mock_common_data_local() -> IntelliFireCommonFireplaceData: + """Fixture for mock common data.""" + return IntelliFireCommonFireplaceData( + auth_cookie="B984F21A6378560019F8A1CDE41B6782", + user_id="52C3F9E8B9D3AC99F8E4D12345678901FE9A2BC7D85F7654E28BF98BCD123456", + web_client_id="FA2B1C3045601234D0AE17D72F8E975", + serial="3FB284769E4736F30C8973A7ED358123", + api_key="B5C4DA27AAEF31D1FB21AFF9BFA6BCD2", + ip_address="192.168.2.108", + read_mode=IntelliFireApiMode.LOCAL, + control_mode=IntelliFireApiMode.LOCAL, + ) + + +@pytest.fixture +def mock_apis_multifp( + mock_cloud_interface, mock_local_interface, mock_fp +) -> Generator[tuple[AsyncMock, AsyncMock, MagicMock]]: + """Multi fireplace version of mocks.""" + return mock_local_interface, mock_cloud_interface, mock_fp @pytest.fixture -def mock_intellifire_config_flow() -> Generator[MagicMock]: - """Return a mocked IntelliFire client.""" - data_mock = Mock() - data_mock.serial = "12345" +def mock_apis_single_fp( + mock_cloud_interface, mock_local_interface, mock_fp +) -> Generator[tuple[AsyncMock, AsyncMock, MagicMock]]: + """Single fire place version of the mocks.""" + data_v1 = IntelliFireUserData( + **load_json_object_fixture("user_data_1.json", DOMAIN) + ) + with patch.object( + type(mock_cloud_interface), "user_data", new_callable=PropertyMock + ) as mock_user_data: + mock_user_data.return_value = data_v1 + yield mock_local_interface, mock_cloud_interface, mock_fp + + +@pytest.fixture +def mock_cloud_interface() -> Generator[AsyncMock]: + """Mock cloud interface to use for testing.""" + user_data = IntelliFireUserData( + **load_json_object_fixture("user_data_3.json", DOMAIN) + ) + + with ( + patch( + "homeassistant.components.intellifire.IntelliFireCloudInterface", + autospec=True, + ) as mock_client, + patch( + "homeassistant.components.intellifire.config_flow.IntelliFireCloudInterface", + new=mock_client, + ), + patch( + "intellifire4py.cloud_interface.IntelliFireCloudInterface", + new=mock_client, + ), + ): + # Mock async context manager + mock_client = mock_client.return_value + mock_client.__aenter__ = AsyncMock(return_value=mock_client) + mock_client.__aexit__ = AsyncMock(return_value=None) + + # Mock other async methods if needed + mock_client.login_with_credentials = AsyncMock() + mock_client.poll = AsyncMock() + type(mock_client).user_data = PropertyMock(return_value=user_data) + yield mock_client # Yielding to the test + + +@pytest.fixture +def mock_local_interface() -> Generator[AsyncMock]: + """Mock version of IntelliFireAPILocal.""" + poll_data = IntelliFirePollData( + **load_json_object_fixture("intellifire/local_poll.json") + ) with patch( - "homeassistant.components.intellifire.config_flow.IntellifireAPILocal", + "homeassistant.components.intellifire.config_flow.IntelliFireAPILocal", autospec=True, - ) as intellifire_mock: - intellifire = intellifire_mock.return_value - intellifire.data = data_mock - yield intellifire + ) as mock_client: + mock_client = mock_client.return_value + # Mock all instances of the class + type(mock_client).data = PropertyMock(return_value=poll_data) + yield mock_client + + +@pytest.fixture +def mock_fp(mock_common_data_local) -> Generator[AsyncMock]: + """Mock fireplace.""" + + local_poll_data = IntelliFirePollData( + **load_json_object_fixture("local_poll.json", DOMAIN) + ) + + assert local_poll_data.connection_quality == 988451 + + with patch( + "homeassistant.components.intellifire.UnifiedFireplace" + ) as mock_unified_fireplace: + # Create an instance of the mock + mock_instance = mock_unified_fireplace.return_value + + # Mock methods and properties of the instance + mock_instance.perform_cloud_poll = AsyncMock() + mock_instance.perform_local_poll = AsyncMock() + + mock_instance.async_validate_connectivity = AsyncMock(return_value=(True, True)) + + type(mock_instance).is_cloud_polling = PropertyMock(return_value=False) + type(mock_instance).is_local_polling = PropertyMock(return_value=True) + + mock_instance.get_user_data_as_json.return_value = '{"mock": "data"}' + + mock_instance.ip_address = "192.168.1.100" + mock_instance.api_key = "mock_api_key" + mock_instance.serial = "mock_serial" + mock_instance.user_id = "mock_user_id" + mock_instance.auth_cookie = "mock_auth_cookie" + mock_instance.web_client_id = "mock_web_client_id" + + # Configure the READ Api + mock_instance.read_api = MagicMock() + mock_instance.read_api.poll = MagicMock(return_value=local_poll_data) + mock_instance.read_api.data = local_poll_data + + mock_instance.control_api = MagicMock() + + mock_instance.local_connectivity = True + mock_instance.cloud_connectivity = False + + mock_instance._read_mode = IntelliFireApiMode.LOCAL + mock_instance.read_mode = IntelliFireApiMode.LOCAL + + mock_instance.control_mode = IntelliFireApiMode.LOCAL + mock_instance._control_mode = IntelliFireApiMode.LOCAL + + mock_instance.data = local_poll_data + + mock_instance.set_read_mode = AsyncMock() + mock_instance.set_control_mode = AsyncMock() + mock_instance.async_validate_connectivity = AsyncMock( + return_value=(True, False) + ) -def mock_api_connection_error() -> ConnectionError: - """Return a fake a ConnectionError for iftapi.net.""" - ret = ConnectionError() - ret.args = [ConnectionKey("iftapi.net", 443, False, None, None, None, None)] - return ret + # Patch class methods + with patch( + "homeassistant.components.intellifire.UnifiedFireplace.build_fireplace_from_common", + new_callable=AsyncMock, + return_value=mock_instance, + ): + yield mock_instance diff --git a/tests/components/intellifire/fixtures/local_poll.json b/tests/components/intellifire/fixtures/local_poll.json new file mode 100644 index 00000000000000..9dac47c698d893 --- /dev/null +++ b/tests/components/intellifire/fixtures/local_poll.json @@ -0,0 +1,29 @@ +{ + "name": "", + "serial": "4GC295860E5837G40D9974B7FD459234", + "temperature": 17, + "battery": 0, + "pilot": 1, + "light": 0, + "height": 1, + "fanspeed": 1, + "hot": 0, + "power": 1, + "thermostat": 0, + "setpoint": 0, + "timer": 0, + "timeremaining": 0, + "prepurge": 0, + "feature_light": 0, + "feature_thermostat": 1, + "power_vent": 0, + "feature_fan": 1, + "errors": [], + "fw_version": "0x00030200", + "fw_ver_str": "0.3.2+hw2", + "downtime": 0, + "uptime": 117, + "connection_quality": 988451, + "ecm_latency": 0, + "ipv4_address": "192.168.2.108" +} diff --git a/tests/components/intellifire/fixtures/user_data_1.json b/tests/components/intellifire/fixtures/user_data_1.json new file mode 100644 index 00000000000000..501d240662b3c6 --- /dev/null +++ b/tests/components/intellifire/fixtures/user_data_1.json @@ -0,0 +1,17 @@ +{ + "auth_cookie": "B984F21A6378560019F8A1CDE41B6782", + "user_id": "52C3F9E8B9D3AC99F8E4D12345678901FE9A2BC7D85F7654E28BF98BCD123456", + "web_client_id": "FA2B1C3045601234D0AE17D72F8E975", + "fireplaces": [ + { + "auth_cookie": "B984F21A6378560019F8A1CDE41B6782", + "user_id": "52C3F9E8B9D3AC99F8E4D12345678901FE9A2BC7D85F7654E28BF98BCD123456", + "web_client_id": "FA2B1C3045601234D0AE17D72F8E975", + "ip_address": "192.168.2.108", + "api_key": "B5C4DA27AAEF31D1FB21AFF9BFA6BCD2", + "serial": "3FB284769E4736F30C8973A7ED358123" + } + ], + "username": "grumpypanda@china.cn", + "password": "you-stole-my-pandas" +} diff --git a/tests/components/intellifire/fixtures/user_data_3.json b/tests/components/intellifire/fixtures/user_data_3.json new file mode 100644 index 00000000000000..39e9c95abbdbf0 --- /dev/null +++ b/tests/components/intellifire/fixtures/user_data_3.json @@ -0,0 +1,33 @@ +{ + "auth_cookie": "B984F21A6378560019F8A1CDE41B6782", + "user_id": "52C3F9E8B9D3AC99F8E4D12345678901FE9A2BC7D85F7654E28BF98BCD123456", + "web_client_id": "FA2B1C3045601234D0AE17D72F8E975", + "fireplaces": [ + { + "auth_cookie": "B984F21A6378560019F8A1CDE41B6782", + "user_id": "52C3F9E8B9D3AC99F8E4D12345678901FE9A2BC7D85F7654E28BF98BCD123456", + "web_client_id": "FA2B1C3045601234D0AE17D72F8E975", + "ip_address": "192.168.2.108", + "api_key": "B5C4DA27AAEF31D1FB21AFF9BFA6BCD2", + "serial": "3FB284769E4736F30C8973A7ED358123" + }, + { + "auth_cookie": "B984F21A6378560019F8A1CDE41B6782", + "user_id": "52C3F9E8B9D3AC99F8E4D12345678901FE9A2BC7D85F7654E28BF98BCD123456", + "web_client_id": "FA2B1C3045601234D0AE17D72F8E975", + "ip_address": "192.168.2.109", + "api_key": "D4C5EB28BBFF41E1FB21AFF9BFA6CD34", + "serial": "4GC295860E5837G40D9974B7FD459234" + }, + { + "auth_cookie": "B984F21A6378560019F8A1CDE41B6782", + "user_id": "52C3F9E8B9D3AC99F8E4D12345678901FE9A2BC7D85F7654E28BF98BCD123456", + "web_client_id": "FA2B1C3045601234D0AE17D72F8E975", + "ip_address": "192.168.2.110", + "api_key": "E5D6FC39CCED52F1FB21AFF9BFA6DE56", + "serial": "5HD306971F5938H51EAA85C8GE561345" + } + ], + "username": "grumpypanda@china.cn", + "password": "you-stole-my-pandas" +} diff --git a/tests/components/intellifire/snapshots/test_binary_sensor.ambr b/tests/components/intellifire/snapshots/test_binary_sensor.ambr new file mode 100644 index 00000000000000..34d5836a02523b --- /dev/null +++ b/tests/components/intellifire/snapshots/test_binary_sensor.ambr @@ -0,0 +1,717 @@ +# serializer version: 1 +# name: test_all_binary_sensor_entities[binary_sensor.intellifire_accessory_error-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.intellifire_accessory_error', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Accessory error', + 'platform': 'intellifire', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'accessory_error', + 'unique_id': 'error_accessory_mock_serial', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_binary_sensor_entities[binary_sensor.intellifire_accessory_error-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by unpublished Intellifire API', + 'device_class': 'problem', + 'friendly_name': 'IntelliFire Accessory error', + }), + 'context': , + 'entity_id': 'binary_sensor.intellifire_accessory_error', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_all_binary_sensor_entities[binary_sensor.intellifire_disabled_error-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.intellifire_disabled_error', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Disabled error', + 'platform': 'intellifire', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'disabled_error', + 'unique_id': 'error_disabled_mock_serial', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_binary_sensor_entities[binary_sensor.intellifire_disabled_error-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by unpublished Intellifire API', + 'device_class': 'problem', + 'friendly_name': 'IntelliFire Disabled error', + }), + 'context': , + 'entity_id': 'binary_sensor.intellifire_disabled_error', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_all_binary_sensor_entities[binary_sensor.intellifire_ecm_offline_error-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.intellifire_ecm_offline_error', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'ECM offline error', + 'platform': 'intellifire', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'ecm_offline_error', + 'unique_id': 'error_ecm_offline_mock_serial', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_binary_sensor_entities[binary_sensor.intellifire_ecm_offline_error-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by unpublished Intellifire API', + 'device_class': 'problem', + 'friendly_name': 'IntelliFire ECM offline error', + }), + 'context': , + 'entity_id': 'binary_sensor.intellifire_ecm_offline_error', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_all_binary_sensor_entities[binary_sensor.intellifire_fan_delay_error-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.intellifire_fan_delay_error', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Fan delay error', + 'platform': 'intellifire', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'fan_delay_error', + 'unique_id': 'error_fan_delay_mock_serial', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_binary_sensor_entities[binary_sensor.intellifire_fan_delay_error-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by unpublished Intellifire API', + 'device_class': 'problem', + 'friendly_name': 'IntelliFire Fan delay error', + }), + 'context': , + 'entity_id': 'binary_sensor.intellifire_fan_delay_error', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_all_binary_sensor_entities[binary_sensor.intellifire_fan_error-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.intellifire_fan_error', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Fan error', + 'platform': 'intellifire', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'fan_error', + 'unique_id': 'error_fan_mock_serial', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_binary_sensor_entities[binary_sensor.intellifire_fan_error-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by unpublished Intellifire API', + 'device_class': 'problem', + 'friendly_name': 'IntelliFire Fan error', + }), + 'context': , + 'entity_id': 'binary_sensor.intellifire_fan_error', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_all_binary_sensor_entities[binary_sensor.intellifire_flame-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': None, + 'entity_id': 'binary_sensor.intellifire_flame', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Flame', + 'platform': 'intellifire', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'flame', + 'unique_id': 'on_off_mock_serial', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_binary_sensor_entities[binary_sensor.intellifire_flame-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by unpublished Intellifire API', + 'friendly_name': 'IntelliFire Flame', + }), + 'context': , + 'entity_id': 'binary_sensor.intellifire_flame', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_all_binary_sensor_entities[binary_sensor.intellifire_flame_error-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.intellifire_flame_error', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Flame Error', + 'platform': 'intellifire', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'flame_error', + 'unique_id': 'error_flame_mock_serial', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_binary_sensor_entities[binary_sensor.intellifire_flame_error-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by unpublished Intellifire API', + 'device_class': 'problem', + 'friendly_name': 'IntelliFire Flame Error', + }), + 'context': , + 'entity_id': 'binary_sensor.intellifire_flame_error', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_all_binary_sensor_entities[binary_sensor.intellifire_lights_error-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.intellifire_lights_error', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Lights error', + 'platform': 'intellifire', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'lights_error', + 'unique_id': 'error_lights_mock_serial', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_binary_sensor_entities[binary_sensor.intellifire_lights_error-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by unpublished Intellifire API', + 'device_class': 'problem', + 'friendly_name': 'IntelliFire Lights error', + }), + 'context': , + 'entity_id': 'binary_sensor.intellifire_lights_error', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_all_binary_sensor_entities[binary_sensor.intellifire_maintenance_error-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.intellifire_maintenance_error', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Maintenance error', + 'platform': 'intellifire', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'maintenance_error', + 'unique_id': 'error_maintenance_mock_serial', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_binary_sensor_entities[binary_sensor.intellifire_maintenance_error-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by unpublished Intellifire API', + 'device_class': 'problem', + 'friendly_name': 'IntelliFire Maintenance error', + }), + 'context': , + 'entity_id': 'binary_sensor.intellifire_maintenance_error', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_all_binary_sensor_entities[binary_sensor.intellifire_offline_error-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.intellifire_offline_error', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Offline error', + 'platform': 'intellifire', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'offline_error', + 'unique_id': 'error_offline_mock_serial', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_binary_sensor_entities[binary_sensor.intellifire_offline_error-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by unpublished Intellifire API', + 'device_class': 'problem', + 'friendly_name': 'IntelliFire Offline error', + }), + 'context': , + 'entity_id': 'binary_sensor.intellifire_offline_error', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_all_binary_sensor_entities[binary_sensor.intellifire_pilot_flame_error-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.intellifire_pilot_flame_error', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Pilot flame error', + 'platform': 'intellifire', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'pilot_flame_error', + 'unique_id': 'error_pilot_flame_mock_serial', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_binary_sensor_entities[binary_sensor.intellifire_pilot_flame_error-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by unpublished Intellifire API', + 'device_class': 'problem', + 'friendly_name': 'IntelliFire Pilot flame error', + }), + 'context': , + 'entity_id': 'binary_sensor.intellifire_pilot_flame_error', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_all_binary_sensor_entities[binary_sensor.intellifire_pilot_light_on-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': None, + 'entity_id': 'binary_sensor.intellifire_pilot_light_on', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Pilot light on', + 'platform': 'intellifire', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'pilot_light_on', + 'unique_id': 'pilot_light_on_mock_serial', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_binary_sensor_entities[binary_sensor.intellifire_pilot_light_on-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by unpublished Intellifire API', + 'friendly_name': 'IntelliFire Pilot light on', + }), + 'context': , + 'entity_id': 'binary_sensor.intellifire_pilot_light_on', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_all_binary_sensor_entities[binary_sensor.intellifire_soft_lock_out_error-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.intellifire_soft_lock_out_error', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Soft lock out error', + 'platform': 'intellifire', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'soft_lock_out_error', + 'unique_id': 'error_soft_lock_out_mock_serial', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_binary_sensor_entities[binary_sensor.intellifire_soft_lock_out_error-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by unpublished Intellifire API', + 'device_class': 'problem', + 'friendly_name': 'IntelliFire Soft lock out error', + }), + 'context': , + 'entity_id': 'binary_sensor.intellifire_soft_lock_out_error', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_all_binary_sensor_entities[binary_sensor.intellifire_thermostat_on-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': None, + 'entity_id': 'binary_sensor.intellifire_thermostat_on', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Thermostat on', + 'platform': 'intellifire', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'thermostat_on', + 'unique_id': 'thermostat_on_mock_serial', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_binary_sensor_entities[binary_sensor.intellifire_thermostat_on-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by unpublished Intellifire API', + 'friendly_name': 'IntelliFire Thermostat on', + }), + 'context': , + 'entity_id': 'binary_sensor.intellifire_thermostat_on', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_all_binary_sensor_entities[binary_sensor.intellifire_timer_on-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': None, + 'entity_id': 'binary_sensor.intellifire_timer_on', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Timer on', + 'platform': 'intellifire', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'timer_on', + 'unique_id': 'timer_on_mock_serial', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_binary_sensor_entities[binary_sensor.intellifire_timer_on-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by unpublished Intellifire API', + 'friendly_name': 'IntelliFire Timer on', + }), + 'context': , + 'entity_id': 'binary_sensor.intellifire_timer_on', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- diff --git a/tests/components/intellifire/snapshots/test_climate.ambr b/tests/components/intellifire/snapshots/test_climate.ambr new file mode 100644 index 00000000000000..36f719d2264b73 --- /dev/null +++ b/tests/components/intellifire/snapshots/test_climate.ambr @@ -0,0 +1,66 @@ +# serializer version: 1 +# name: test_all_sensor_entities[climate.intellifire_thermostat-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'hvac_modes': list([ + , + , + ]), + 'max_temp': 37, + 'min_temp': 0, + 'target_temp_step': 1.0, + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'climate', + 'entity_category': None, + 'entity_id': 'climate.intellifire_thermostat', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Thermostat', + 'platform': 'intellifire', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': None, + 'unique_id': 'climate_mock_serial', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_sensor_entities[climate.intellifire_thermostat-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by unpublished Intellifire API', + 'current_temperature': 17.0, + 'friendly_name': 'IntelliFire Thermostat', + 'hvac_modes': list([ + , + , + ]), + 'max_temp': 37, + 'min_temp': 0, + 'supported_features': , + 'target_temp_step': 1.0, + 'temperature': 0.0, + }), + 'context': , + 'entity_id': 'climate.intellifire_thermostat', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- diff --git a/tests/components/intellifire/snapshots/test_sensor.ambr b/tests/components/intellifire/snapshots/test_sensor.ambr new file mode 100644 index 00000000000000..d5e59e3f00fa22 --- /dev/null +++ b/tests/components/intellifire/snapshots/test_sensor.ambr @@ -0,0 +1,587 @@ +# serializer version: 1 +# name: test_all_sensor_entities[sensor.intellifire_connection_quality-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.intellifire_connection_quality', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Connection quality', + 'platform': 'intellifire', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'connection_quality', + 'unique_id': 'connection_quality_mock_serial', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_sensor_entities[sensor.intellifire_connection_quality-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by unpublished Intellifire API', + 'friendly_name': 'IntelliFire Connection quality', + }), + 'context': , + 'entity_id': 'sensor.intellifire_connection_quality', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '988451', + }) +# --- +# name: test_all_sensor_entities[sensor.intellifire_downtime-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.intellifire_downtime', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Downtime', + 'platform': 'intellifire', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'downtime', + 'unique_id': 'downtime_mock_serial', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_sensor_entities[sensor.intellifire_downtime-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by unpublished Intellifire API', + 'device_class': 'timestamp', + 'friendly_name': 'IntelliFire Downtime', + }), + 'context': , + 'entity_id': 'sensor.intellifire_downtime', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'unknown', + }) +# --- +# name: test_all_sensor_entities[sensor.intellifire_ecm_latency-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.intellifire_ecm_latency', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'ECM latency', + 'platform': 'intellifire', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'ecm_latency', + 'unique_id': 'ecm_latency_mock_serial', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_sensor_entities[sensor.intellifire_ecm_latency-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by unpublished Intellifire API', + 'friendly_name': 'IntelliFire ECM latency', + }), + 'context': , + 'entity_id': 'sensor.intellifire_ecm_latency', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0', + }) +# --- +# name: test_all_sensor_entities[sensor.intellifire_fan_speed-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.intellifire_fan_speed', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Fan Speed', + 'platform': 'intellifire', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'fan_speed', + 'unique_id': 'fan_speed_mock_serial', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_sensor_entities[sensor.intellifire_fan_speed-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by unpublished Intellifire API', + 'friendly_name': 'IntelliFire Fan Speed', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.intellifire_fan_speed', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '1', + }) +# --- +# name: test_all_sensor_entities[sensor.intellifire_flame_height-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.intellifire_flame_height', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Flame height', + 'platform': 'intellifire', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'flame_height', + 'unique_id': 'flame_height_mock_serial', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_sensor_entities[sensor.intellifire_flame_height-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by unpublished Intellifire API', + 'friendly_name': 'IntelliFire Flame height', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.intellifire_flame_height', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '2', + }) +# --- +# name: test_all_sensor_entities[sensor.intellifire_ip_address-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.intellifire_ip_address', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'IP address', + 'platform': 'intellifire', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'ipv4_address', + 'unique_id': 'ipv4_address_mock_serial', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_sensor_entities[sensor.intellifire_ip_address-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by unpublished Intellifire API', + 'friendly_name': 'IntelliFire IP address', + }), + 'context': , + 'entity_id': 'sensor.intellifire_ip_address', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '192.168.2.108', + }) +# --- +# name: test_all_sensor_entities[sensor.intellifire_local_connectivity-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.intellifire_local_connectivity', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Local connectivity', + 'platform': 'intellifire', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'local_connectivity', + 'unique_id': 'local_connectivity_mock_serial', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_sensor_entities[sensor.intellifire_local_connectivity-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by unpublished Intellifire API', + 'friendly_name': 'IntelliFire Local connectivity', + }), + 'context': , + 'entity_id': 'sensor.intellifire_local_connectivity', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'True', + }) +# --- +# name: test_all_sensor_entities[sensor.intellifire_none-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.intellifire_none', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': None, + 'platform': 'intellifire', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'local_connectivity', + 'unique_id': 'local_connectivity_mock_serial', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_sensor_entities[sensor.intellifire_none-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by unpublished Intellifire API', + 'friendly_name': 'IntelliFire None', + }), + 'context': , + 'entity_id': 'sensor.intellifire_none', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'True', + }) +# --- +# name: test_all_sensor_entities[sensor.intellifire_target_temperature-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.intellifire_target_temperature', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Target temperature', + 'platform': 'intellifire', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'target_temp', + 'unique_id': 'target_temp_mock_serial', + 'unit_of_measurement': , + }) +# --- +# name: test_all_sensor_entities[sensor.intellifire_target_temperature-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by unpublished Intellifire API', + 'device_class': 'temperature', + 'friendly_name': 'IntelliFire Target temperature', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.intellifire_target_temperature', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0.0', + }) +# --- +# name: test_all_sensor_entities[sensor.intellifire_temperature-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.intellifire_temperature', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Temperature', + 'platform': 'intellifire', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': 'temperature_mock_serial', + 'unit_of_measurement': , + }) +# --- +# name: test_all_sensor_entities[sensor.intellifire_temperature-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by unpublished Intellifire API', + 'device_class': 'temperature', + 'friendly_name': 'IntelliFire Temperature', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.intellifire_temperature', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '17', + }) +# --- +# name: test_all_sensor_entities[sensor.intellifire_timer_end-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.intellifire_timer_end', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Timer end', + 'platform': 'intellifire', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'timer_end_timestamp', + 'unique_id': 'timer_end_timestamp_mock_serial', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_sensor_entities[sensor.intellifire_timer_end-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by unpublished Intellifire API', + 'device_class': 'timestamp', + 'friendly_name': 'IntelliFire Timer end', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.intellifire_timer_end', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'unknown', + }) +# --- +# name: test_all_sensor_entities[sensor.intellifire_uptime-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.intellifire_uptime', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Uptime', + 'platform': 'intellifire', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'uptime', + 'unique_id': 'uptime_mock_serial', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_sensor_entities[sensor.intellifire_uptime-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by unpublished Intellifire API', + 'device_class': 'timestamp', + 'friendly_name': 'IntelliFire Uptime', + }), + 'context': , + 'entity_id': 'sensor.intellifire_uptime', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '2021-01-01T11:58:03+00:00', + }) +# --- diff --git a/tests/components/intellifire/test_binary_sensor.py b/tests/components/intellifire/test_binary_sensor.py new file mode 100644 index 00000000000000..a40f92b84d5dc3 --- /dev/null +++ b/tests/components/intellifire/test_binary_sensor.py @@ -0,0 +1,35 @@ +"""Test IntelliFire Binary Sensors.""" + +from unittest.mock import AsyncMock, patch + +import pytest +from syrupy import SnapshotAssertion + +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from . import setup_integration + +from tests.common import MockConfigEntry, snapshot_platform + + +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_all_binary_sensor_entities( + hass: HomeAssistant, + snapshot: SnapshotAssertion, + mock_config_entry_current: MockConfigEntry, + entity_registry: er.EntityRegistry, + mock_apis_single_fp: tuple[AsyncMock, AsyncMock, AsyncMock], +) -> None: + """Test all entities.""" + + with ( + patch( + "homeassistant.components.intellifire.PLATFORMS", [Platform.BINARY_SENSOR] + ), + ): + await setup_integration(hass, mock_config_entry_current) + await snapshot_platform( + hass, entity_registry, snapshot, mock_config_entry_current.entry_id + ) diff --git a/tests/components/intellifire/test_climate.py b/tests/components/intellifire/test_climate.py new file mode 100644 index 00000000000000..da1b2864791b99 --- /dev/null +++ b/tests/components/intellifire/test_climate.py @@ -0,0 +1,34 @@ +"""Test climate.""" + +from unittest.mock import patch + +from freezegun import freeze_time +import pytest +from syrupy import SnapshotAssertion + +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from . import setup_integration + +from tests.common import MockConfigEntry, snapshot_platform + + +@freeze_time("2021-01-01T12:00:00Z") +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_all_sensor_entities( + hass: HomeAssistant, + snapshot: SnapshotAssertion, + mock_config_entry_current: MockConfigEntry, + entity_registry: er.EntityRegistry, + mock_fp, +) -> None: + """Test all entities.""" + with ( + patch("homeassistant.components.intellifire.PLATFORMS", [Platform.CLIMATE]), + ): + await setup_integration(hass, mock_config_entry_current) + await snapshot_platform( + hass, entity_registry, snapshot, mock_config_entry_current.entry_id + ) diff --git a/tests/components/intellifire/test_config_flow.py b/tests/components/intellifire/test_config_flow.py index ba4e2f039a3ddc..f1465c4dcd4bfc 100644 --- a/tests/components/intellifire/test_config_flow.py +++ b/tests/components/intellifire/test_config_flow.py @@ -1,323 +1,168 @@ """Test the IntelliFire config flow.""" -from unittest.mock import AsyncMock, MagicMock, patch +from unittest.mock import AsyncMock -from intellifire4py.exceptions import LoginException +from intellifire4py.exceptions import LoginError from homeassistant import config_entries from homeassistant.components import dhcp -from homeassistant.components.intellifire.config_flow import MANUAL_ENTRY_STRING -from homeassistant.components.intellifire.const import CONF_USER_ID, DOMAIN -from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_PASSWORD, CONF_USERNAME +from homeassistant.components.intellifire.const import CONF_SERIAL, DOMAIN +from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType -from .conftest import mock_api_connection_error - from tests.common import MockConfigEntry -@patch.multiple( - "homeassistant.components.intellifire.config_flow.IntellifireAPICloud", - login=AsyncMock(), - get_user_id=MagicMock(return_value="intellifire"), - get_fireplace_api_key=MagicMock(return_value="key"), -) -async def test_no_discovery( +async def test_standard_config_with_single_fireplace( hass: HomeAssistant, mock_setup_entry: AsyncMock, - mock_intellifire_config_flow: MagicMock, + mock_apis_single_fp, ) -> None: - """Test we should get the manual discovery form - because no discovered fireplaces.""" - with patch( - "homeassistant.components.intellifire.config_flow.AsyncUDPFireplaceFinder.search_fireplace", - return_value=[], - ): - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_USER} - ) - assert result["type"] is FlowResultType.FORM - assert result["errors"] == {} - assert result["step_id"] == "manual_device_entry" + """Test standard flow with a user who has only a single fireplace.""" - result2 = await hass.config_entries.flow.async_configure( - result["flow_id"], - { - CONF_HOST: "1.1.1.1", - }, + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} ) - await hass.async_block_till_done() - - assert result2["type"] is FlowResultType.FORM - assert result2["step_id"] == "api_config" + assert result["type"] == FlowResultType.FORM + assert result["errors"] == {} + assert result["step_id"] == "cloud_api" - result3 = await hass.config_entries.flow.async_configure( + result = await hass.config_entries.flow.async_configure( result["flow_id"], - {CONF_USERNAME: "test", CONF_PASSWORD: "AROONIE"}, - ) - await hass.async_block_till_done() - - assert result3["type"] is FlowResultType.CREATE_ENTRY - assert result3["title"] == "Fireplace 12345" - assert result3["data"] == { - CONF_HOST: "1.1.1.1", - CONF_USERNAME: "test", - CONF_PASSWORD: "AROONIE", - CONF_API_KEY: "key", - CONF_USER_ID: "intellifire", + {CONF_USERNAME: "donJulio", CONF_PASSWORD: "Tequila0FD00m"}, + ) + # For a single fireplace we just create it + assert result["type"] == FlowResultType.CREATE_ENTRY + assert result["data"] == { + "ip_address": "192.168.2.108", + "api_key": "B5C4DA27AAEF31D1FB21AFF9BFA6BCD2", + "serial": "3FB284769E4736F30C8973A7ED358123", + "auth_cookie": "B984F21A6378560019F8A1CDE41B6782", + "web_client_id": "FA2B1C3045601234D0AE17D72F8E975", + "user_id": "52C3F9E8B9D3AC99F8E4D12345678901FE9A2BC7D85F7654E28BF98BCD123456", + "username": "grumpypanda@china.cn", + "password": "you-stole-my-pandas", } - assert len(mock_setup_entry.mock_calls) == 1 -@patch.multiple( - "homeassistant.components.intellifire.config_flow.IntellifireAPICloud", - login=AsyncMock(side_effect=mock_api_connection_error()), - get_user_id=MagicMock(return_value="intellifire"), - get_fireplace_api_key=MagicMock(return_value="key"), -) -async def test_single_discovery( +async def test_standard_config_with_pre_configured_fireplace( hass: HomeAssistant, mock_setup_entry: AsyncMock, - mock_intellifire_config_flow: MagicMock, + mock_config_entry_current, + mock_apis_single_fp, ) -> None: - """Test single fireplace UDP discovery.""" - with patch( - "homeassistant.components.intellifire.config_flow.AsyncUDPFireplaceFinder.search_fireplace", - return_value=["192.168.1.69"], - ): - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_USER} - ) - - await hass.config_entries.flow.async_configure( - result["flow_id"], {CONF_HOST: "192.168.1.69"} - ) - await hass.async_block_till_done() - result3 = await hass.config_entries.flow.async_configure( - result["flow_id"], - {CONF_USERNAME: "test", CONF_PASSWORD: "AROONIE"}, - ) - await hass.async_block_till_done() - assert result3["type"] is FlowResultType.FORM - assert result3["errors"] == {"base": "iftapi_connect"} - - -@patch.multiple( - "homeassistant.components.intellifire.config_flow.IntellifireAPICloud", - login=AsyncMock(side_effect=LoginException), - get_user_id=MagicMock(return_value="intellifire"), - get_fireplace_api_key=MagicMock(return_value="key"), -) -async def test_single_discovery_loign_error( - hass: HomeAssistant, - mock_setup_entry: AsyncMock, - mock_intellifire_config_flow: MagicMock, -) -> None: - """Test single fireplace UDP discovery.""" - with patch( - "homeassistant.components.intellifire.config_flow.AsyncUDPFireplaceFinder.search_fireplace", - return_value=["192.168.1.69"], - ): - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_USER} - ) - - await hass.config_entries.flow.async_configure( - result["flow_id"], {CONF_HOST: "192.168.1.69"} - ) - await hass.async_block_till_done() - result3 = await hass.config_entries.flow.async_configure( - result["flow_id"], - {CONF_USERNAME: "test", CONF_PASSWORD: "AROONIE"}, - ) - await hass.async_block_till_done() - assert result3["type"] is FlowResultType.FORM - assert result3["errors"] == {"base": "api_error"} - + """What if we try to configure an already configured fireplace.""" + # Configure an existing entry + mock_config_entry_current.add_to_hass(hass) -async def test_manual_entry( - hass: HomeAssistant, - mock_setup_entry: AsyncMock, - mock_intellifire_config_flow: MagicMock, -) -> None: - """Test for multiple Fireplace discovery - involving a pick_device step.""" - with patch( - "homeassistant.components.intellifire.config_flow.AsyncUDPFireplaceFinder.search_fireplace", - return_value=["192.168.1.69", "192.168.1.33", "192.168.169"], - ): - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_USER} - ) - - assert result["step_id"] == "pick_device" - result2 = await hass.config_entries.flow.async_configure( - result["flow_id"], user_input={CONF_HOST: MANUAL_ENTRY_STRING} + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} ) + assert result["type"] == FlowResultType.FORM + assert result["errors"] == {} + assert result["step_id"] == "cloud_api" - await hass.async_block_till_done() - assert result2["step_id"] == "manual_device_entry" - - -async def test_multi_discovery( - hass: HomeAssistant, - mock_setup_entry: AsyncMock, - mock_intellifire_config_flow: MagicMock, -) -> None: - """Test for multiple fireplace discovery - involving a pick_device step.""" - with patch( - "homeassistant.components.intellifire.config_flow.AsyncUDPFireplaceFinder.search_fireplace", - return_value=["192.168.1.69", "192.168.1.33", "192.168.169"], - ): - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_USER} - ) - - assert result["step_id"] == "pick_device" - await hass.config_entries.flow.async_configure( - result["flow_id"], user_input={CONF_HOST: "192.168.1.33"} + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {CONF_USERNAME: "donJulio", CONF_PASSWORD: "Tequila0FD00m"}, ) - await hass.async_block_till_done() - assert result["step_id"] == "pick_device" + + # For a single fireplace we just create it + assert result["type"] == FlowResultType.ABORT + assert result["reason"] == "no_available_devices" -async def test_multi_discovery_cannot_connect( +async def test_standard_config_with_single_fireplace_and_bad_credentials( hass: HomeAssistant, mock_setup_entry: AsyncMock, - mock_intellifire_config_flow: MagicMock, -) -> None: - """Test for multiple fireplace discovery - involving a pick_device step.""" - with patch( - "homeassistant.components.intellifire.config_flow.AsyncUDPFireplaceFinder.search_fireplace", - return_value=["192.168.1.69", "192.168.1.33", "192.168.169"], - ): - mock_intellifire_config_flow.poll.side_effect = ConnectionError - - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_USER} - ) - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "pick_device" - - result2 = await hass.config_entries.flow.async_configure( - result["flow_id"], user_input={CONF_HOST: "192.168.1.33"} - ) - await hass.async_block_till_done() - assert result2["type"] is FlowResultType.FORM - assert result2["errors"] == {"base": "cannot_connect"} - - -async def test_form_cannot_connect_manual_entry( - hass: HomeAssistant, - mock_intellifire_config_flow: MagicMock, - mock_fireplace_finder_single: AsyncMock, + mock_apis_single_fp, ) -> None: - """Test we handle cannot connect error.""" - mock_intellifire_config_flow.poll.side_effect = ConnectionError + """Test bad credentials on a login.""" + mock_local_interface, mock_cloud_interface, mock_fp = mock_apis_single_fp + # Set login error + mock_cloud_interface.login_with_credentials.side_effect = LoginError result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} ) assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "manual_device_entry" + assert result["errors"] == {} + assert result["step_id"] == "cloud_api" - result2 = await hass.config_entries.flow.async_configure( + result = await hass.config_entries.flow.async_configure( result["flow_id"], - { - CONF_HOST: "1.1.1.1", - }, + {CONF_USERNAME: "donJulio", CONF_PASSWORD: "Tequila0FD00m"}, ) - assert result2["type"] is FlowResultType.FORM - assert result2["errors"] == {"base": "cannot_connect"} - + # Erase the error + mock_cloud_interface.login_with_credentials.side_effect = None -async def test_picker_already_discovered( - hass: HomeAssistant, - mock_setup_entry: AsyncMock, - mock_intellifire_config_flow: MagicMock, -) -> None: - """Test single fireplace UDP discovery.""" - - entry = MockConfigEntry( - domain=DOMAIN, - data={ - "host": "192.168.1.3", - }, - title="Fireplace", - unique_id=44444, - ) - entry.add_to_hass(hass) - with patch( - "homeassistant.components.intellifire.config_flow.AsyncUDPFireplaceFinder.search_fireplace", - return_value=["192.168.1.3"], - ): - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_USER} - ) - await hass.async_block_till_done() - - result2 = await hass.config_entries.flow.async_configure( + assert result["type"] == FlowResultType.FORM + assert result["errors"] == {"base": "api_error"} + assert result["step_id"] == "cloud_api" + result = await hass.config_entries.flow.async_configure( result["flow_id"], - { - CONF_HOST: "192.168.1.4", - }, - ) - assert result2["type"] is FlowResultType.FORM - assert len(mock_setup_entry.mock_calls) == 0 + {CONF_USERNAME: "donJulio", CONF_PASSWORD: "Tequila0FD00m"}, + ) + # For a single fireplace we just create it + assert result["type"] == FlowResultType.CREATE_ENTRY + assert result["data"] == { + "ip_address": "192.168.2.108", + "api_key": "B5C4DA27AAEF31D1FB21AFF9BFA6BCD2", + "serial": "3FB284769E4736F30C8973A7ED358123", + "auth_cookie": "B984F21A6378560019F8A1CDE41B6782", + "web_client_id": "FA2B1C3045601234D0AE17D72F8E975", + "user_id": "52C3F9E8B9D3AC99F8E4D12345678901FE9A2BC7D85F7654E28BF98BCD123456", + "username": "grumpypanda@china.cn", + "password": "you-stole-my-pandas", + } -@patch.multiple( - "homeassistant.components.intellifire.config_flow.IntellifireAPICloud", - login=AsyncMock(), - get_user_id=MagicMock(return_value="intellifire"), - get_fireplace_api_key=MagicMock(return_value="key"), -) -async def test_reauth_flow( +async def test_standard_config_with_multiple_fireplace( hass: HomeAssistant, mock_setup_entry: AsyncMock, - mock_intellifire_config_flow: MagicMock, + mock_apis_multifp, ) -> None: - """Test the reauth flow.""" - - entry = MockConfigEntry( - domain=DOMAIN, - data={ - "host": "192.168.1.3", - }, - title="Fireplace 1234", - version=1, - unique_id="4444", - ) - entry.add_to_hass(hass) - + """Test multi-fireplace user who must be very rich.""" result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": "reauth", - "unique_id": entry.unique_id, - "entry_id": entry.entry_id, - }, + DOMAIN, context={"source": config_entries.SOURCE_USER} ) + assert result["type"] == FlowResultType.FORM + assert result["errors"] == {} + assert result["step_id"] == "cloud_api" - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "api_config" - - result3 = await hass.config_entries.flow.async_configure( + result = await hass.config_entries.flow.async_configure( result["flow_id"], - {CONF_USERNAME: "test", CONF_PASSWORD: "AROONIE"}, + {CONF_USERNAME: "donJulio", CONF_PASSWORD: "Tequila0FD00m"}, ) - await hass.async_block_till_done() - assert result3["type"] is FlowResultType.ABORT - assert entry.data[CONF_PASSWORD] == "AROONIE" - assert entry.data[CONF_USERNAME] == "test" + # When we have multiple fireplaces we get to pick a serial + assert result["type"] == FlowResultType.FORM + assert result["step_id"] == "pick_cloud_device" + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {CONF_SERIAL: "4GC295860E5837G40D9974B7FD459234"}, + ) + assert result["type"] == FlowResultType.CREATE_ENTRY + assert result["data"] == { + "ip_address": "192.168.2.109", + "api_key": "D4C5EB28BBFF41E1FB21AFF9BFA6CD34", + "serial": "4GC295860E5837G40D9974B7FD459234", + "auth_cookie": "B984F21A6378560019F8A1CDE41B6782", + "web_client_id": "FA2B1C3045601234D0AE17D72F8E975", + "user_id": "52C3F9E8B9D3AC99F8E4D12345678901FE9A2BC7D85F7654E28BF98BCD123456", + "username": "grumpypanda@china.cn", + "password": "you-stole-my-pandas", + } async def test_dhcp_discovery_intellifire_device( hass: HomeAssistant, mock_setup_entry: AsyncMock, - mock_intellifire_config_flow: MagicMock, + mock_apis_multifp, ) -> None: """Test successful DHCP Discovery.""" + result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_DHCP}, @@ -327,26 +172,26 @@ async def test_dhcp_discovery_intellifire_device( hostname="zentrios-Test", ), ) - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "dhcp_confirm" - result2 = await hass.config_entries.flow.async_configure(result["flow_id"]) - assert result2["type"] is FlowResultType.FORM - assert result2["step_id"] == "dhcp_confirm" - result3 = await hass.config_entries.flow.async_configure( - result2["flow_id"], user_input={} + assert result["type"] == FlowResultType.FORM + assert result["step_id"] == "cloud_api" + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {CONF_USERNAME: "donJulio", CONF_PASSWORD: "Tequila0FD00m"}, ) - assert result3["title"] == "Fireplace 12345" - assert result3["data"] == {"host": "1.1.1.1"} + assert result["type"] == FlowResultType.CREATE_ENTRY async def test_dhcp_discovery_non_intellifire_device( hass: HomeAssistant, - mock_intellifire_config_flow: MagicMock, mock_setup_entry: AsyncMock, + mock_apis_multifp, ) -> None: - """Test failed DHCP Discovery.""" + """Test successful DHCP Discovery of a non intellifire device..""" - mock_intellifire_config_flow.poll.side_effect = ConnectionError + # Patch poll with an exception + mock_local_interface, mock_cloud_interface, mock_fp = mock_apis_multifp + mock_local_interface.poll.side_effect = ConnectionError result = await hass.config_entries.flow.async_init( DOMAIN, @@ -357,6 +202,28 @@ async def test_dhcp_discovery_non_intellifire_device( hostname="zentrios-Evil", ), ) - - assert result["type"] is FlowResultType.ABORT + assert result["type"] == FlowResultType.ABORT assert result["reason"] == "not_intellifire_device" + # Test is finished - the DHCP scanner detected a hostname that "might" be an IntelliFire device, but it was not. + + +async def test_reauth_flow( + hass: HomeAssistant, + mock_config_entry_current: MockConfigEntry, + mock_apis_single_fp, + mock_setup_entry: AsyncMock, +) -> None: + """Test reauth.""" + + mock_config_entry_current.add_to_hass(hass) + result = await mock_config_entry_current.start_reauth_flow(hass) + assert result["type"] == FlowResultType.FORM + result["step_id"] = "cloud_api" + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {CONF_USERNAME: "donJulio", CONF_PASSWORD: "Tequila0FD00m"}, + ) + + assert result["type"] == FlowResultType.ABORT + assert result["reason"] == "reauth_successful" diff --git a/tests/components/intellifire/test_init.py b/tests/components/intellifire/test_init.py new file mode 100644 index 00000000000000..6d08fda26c3a56 --- /dev/null +++ b/tests/components/intellifire/test_init.py @@ -0,0 +1,111 @@ +"""Test the IntelliFire config flow.""" + +from unittest.mock import AsyncMock, patch + +from homeassistant.components.intellifire import CONF_USER_ID +from homeassistant.components.intellifire.const import ( + API_MODE_CLOUD, + API_MODE_LOCAL, + CONF_AUTH_COOKIE, + CONF_CONTROL_MODE, + CONF_READ_MODE, + CONF_SERIAL, + CONF_WEB_CLIENT_ID, + DOMAIN, +) +from homeassistant.config_entries import ConfigEntryState +from homeassistant.const import ( + CONF_API_KEY, + CONF_HOST, + CONF_IP_ADDRESS, + CONF_PASSWORD, + CONF_USERNAME, +) +from homeassistant.core import HomeAssistant + +from tests.common import MockConfigEntry + + +async def test_minor_migration( + hass: HomeAssistant, mock_config_entry_old, mock_apis_single_fp +) -> None: + """With the new library we are going to end up rewriting the config entries.""" + mock_config_entry_old.add_to_hass(hass) + await hass.config_entries.async_setup(mock_config_entry_old.entry_id) + + assert mock_config_entry_old.data == { + "ip_address": "192.168.2.108", + "host": "192.168.2.108", + "api_key": "B5C4DA27AAEF31D1FB21AFF9BFA6BCD2", + "serial": "3FB284769E4736F30C8973A7ED358123", + "auth_cookie": "B984F21A6378560019F8A1CDE41B6782", + "web_client_id": "FA2B1C3045601234D0AE17D72F8E975", + "user_id": "52C3F9E8B9D3AC99F8E4D12345678901FE9A2BC7D85F7654E28BF98BCD123456", + "username": "grumpypanda@china.cn", + "password": "you-stole-my-pandas", + } + + +async def test_minor_migration_error(hass: HomeAssistant, mock_apis_single_fp) -> None: + """Test the case where we completely fail to initialize.""" + mock_config_entry = MockConfigEntry( + domain=DOMAIN, + version=1, + minor_version=1, + title="Fireplace of testing", + data={ + CONF_HOST: "11.168.2.218", + CONF_USERNAME: "grumpypanda@china.cn", + CONF_PASSWORD: "you-stole-my-pandas", + CONF_USER_ID: "52C3F9E8B9D3AC99F8E4D12345678901FE9A2BC7D85F7654E28BF98BCD123456", + }, + ) + + mock_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + assert mock_config_entry.state is ConfigEntryState.MIGRATION_ERROR + + +async def test_init_with_no_username(hass: HomeAssistant, mock_apis_single_fp) -> None: + """Test the case where we completely fail to initialize.""" + mock_config_entry = MockConfigEntry( + domain=DOMAIN, + version=1, + minor_version=2, + data={ + CONF_IP_ADDRESS: "192.168.2.108", + CONF_PASSWORD: "you-stole-my-pandas", + CONF_SERIAL: "3FB284769E4736F30C8973A7ED358123", + CONF_WEB_CLIENT_ID: "FA2B1C3045601234D0AE17D72F8E975", + CONF_API_KEY: "B5C4DA27AAEF31D1FB21AFF9BFA6BCD2", + CONF_AUTH_COOKIE: "B984F21A6378560019F8A1CDE41B6782", + CONF_USER_ID: "52C3F9E8B9D3AC99F8E4D12345678901FE9A2BC7D85F7654E28BF98BCD123456", + }, + options={CONF_READ_MODE: API_MODE_LOCAL, CONF_CONTROL_MODE: API_MODE_CLOUD}, + unique_id="3FB284769E4736F30C8973A7ED358123", + ) + + mock_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + assert mock_config_entry.state is ConfigEntryState.SETUP_ERROR + + +async def test_connectivity_bad( + hass: HomeAssistant, + mock_config_entry_current, + mock_apis_single_fp, +) -> None: + """Test a timeout error on the setup flow.""" + + with patch( + "homeassistant.components.intellifire.UnifiedFireplace.build_fireplace_from_common", + new_callable=AsyncMock, + side_effect=TimeoutError, + ): + mock_config_entry_current.add_to_hass(hass) + await hass.config_entries.async_setup(mock_config_entry_current.entry_id) + + await hass.async_block_till_done() + assert len(hass.states.async_all()) == 0 diff --git a/tests/components/intellifire/test_sensor.py b/tests/components/intellifire/test_sensor.py new file mode 100644 index 00000000000000..96e344d77fc163 --- /dev/null +++ b/tests/components/intellifire/test_sensor.py @@ -0,0 +1,35 @@ +"""Test IntelliFire Binary Sensors.""" + +from unittest.mock import AsyncMock, patch + +from freezegun import freeze_time +import pytest +from syrupy import SnapshotAssertion + +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from . import setup_integration + +from tests.common import MockConfigEntry, snapshot_platform + + +@freeze_time("2021-01-01T12:00:00Z") +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_all_sensor_entities( + hass: HomeAssistant, + snapshot: SnapshotAssertion, + mock_config_entry_current: MockConfigEntry, + entity_registry: er.EntityRegistry, + mock_apis_single_fp: tuple[AsyncMock, AsyncMock, AsyncMock], +) -> None: + """Test all entities.""" + + with ( + patch("homeassistant.components.intellifire.PLATFORMS", [Platform.SENSOR]), + ): + await setup_integration(hass, mock_config_entry_current) + await snapshot_platform( + hass, entity_registry, snapshot, mock_config_entry_current.entry_id + ) diff --git a/tests/components/iskra/__init__.py b/tests/components/iskra/__init__.py new file mode 100644 index 00000000000000..ca93572a9e4d57 --- /dev/null +++ b/tests/components/iskra/__init__.py @@ -0,0 +1 @@ +"""Tests for the Iskra component.""" diff --git a/tests/components/iskra/conftest.py b/tests/components/iskra/conftest.py new file mode 100644 index 00000000000000..d9cc6808aaa26d --- /dev/null +++ b/tests/components/iskra/conftest.py @@ -0,0 +1,46 @@ +"""Fixtures for mocking pyiskra's different protocols. + +Fixtures: +- `mock_pyiskra_rest`: Mock pyiskra Rest API protocol. +- `mock_pyiskra_modbus`: Mock pyiskra Modbus protocol. +""" + +from unittest.mock import patch + +import pytest + +from .const import PQ_MODEL, SERIAL, SG_MODEL + + +class MockBasicInfo: + """Mock BasicInfo class.""" + + def __init__(self, model) -> None: + """Initialize the mock class.""" + self.serial = SERIAL + self.model = model + self.description = "Iskra mock device" + self.location = "imagination" + self.sw_ver = "1.0.0" + + +@pytest.fixture +def mock_pyiskra_rest(): + """Mock Iskra API authenticate with Rest API protocol.""" + + with patch( + "pyiskra.adapters.RestAPI.RestAPI.get_basic_info", + return_value=MockBasicInfo(model=SG_MODEL), + ) as basic_info_mock: + yield basic_info_mock + + +@pytest.fixture +def mock_pyiskra_modbus(): + """Mock Iskra API authenticate with Rest API protocol.""" + + with patch( + "pyiskra.adapters.Modbus.Modbus.get_basic_info", + return_value=MockBasicInfo(model=PQ_MODEL), + ) as basic_info_mock: + yield basic_info_mock diff --git a/tests/components/iskra/const.py b/tests/components/iskra/const.py new file mode 100644 index 00000000000000..bf38c9a4a79307 --- /dev/null +++ b/tests/components/iskra/const.py @@ -0,0 +1,10 @@ +"""Constants used in the Iskra component tests.""" + +SG_MODEL = "SG-W1" +PQ_MODEL = "MC784" +SERIAL = "XXXXXXX" +HOST = "192.1.0.1" +MODBUS_PORT = 10001 +MODBUS_ADDRESS = 33 +USERNAME = "test_username" +PASSWORD = "test_password" diff --git a/tests/components/iskra/test_config_flow.py b/tests/components/iskra/test_config_flow.py new file mode 100644 index 00000000000000..0c128be98505e5 --- /dev/null +++ b/tests/components/iskra/test_config_flow.py @@ -0,0 +1,300 @@ +"""Tests for the Iskra config flow.""" + +from pyiskra.exceptions import ( + DeviceConnectionError, + DeviceTimeoutError, + InvalidResponseCode, + NotAuthorised, +) +import pytest + +from homeassistant.components.iskra import DOMAIN +from homeassistant.config_entries import SOURCE_USER +from homeassistant.const import ( + CONF_ADDRESS, + CONF_HOST, + CONF_PASSWORD, + CONF_PORT, + CONF_PROTOCOL, + CONF_USERNAME, +) +from homeassistant.core import HomeAssistant +from homeassistant.data_entry_flow import FlowResultType + +from .const import ( + HOST, + MODBUS_ADDRESS, + MODBUS_PORT, + PASSWORD, + PQ_MODEL, + SERIAL, + SG_MODEL, + USERNAME, +) + +from tests.common import MockConfigEntry + + +# Test step_user with Rest API protocol +async def test_user_rest_no_auth(hass: HomeAssistant, mock_pyiskra_rest) -> None: + """Test the user flow with Rest API protocol.""" + + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_USER}, + ) + + # Test if user form is provided + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + + # Test no authentication required + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={CONF_HOST: HOST, CONF_PROTOCOL: "rest_api"}, + ) + + # Test successful Rest API configuration + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["result"].unique_id == SERIAL + assert result["title"] == SG_MODEL + assert result["data"] == {CONF_HOST: HOST, CONF_PROTOCOL: "rest_api"} + + +async def test_user_rest_auth(hass: HomeAssistant, mock_pyiskra_rest) -> None: + """Test the user flow with Rest API protocol and authentication required.""" + mock_pyiskra_rest.side_effect = NotAuthorised + + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_USER}, + ) + + # Test if user form is provided + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + + # Test if prompted to enter username and password if not authorised + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={CONF_HOST: HOST, CONF_PROTOCOL: "rest_api"}, + ) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "authentication" + + # Test failed authentication + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={CONF_USERNAME: USERNAME, CONF_PASSWORD: PASSWORD}, + ) + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {"base": "invalid_auth"} + assert result["step_id"] == "authentication" + + # Test successful authentication + mock_pyiskra_rest.side_effect = None + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={CONF_USERNAME: USERNAME, CONF_PASSWORD: PASSWORD}, + ) + + # Test successful Rest API configuration + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["result"].unique_id == SERIAL + assert result["title"] == SG_MODEL + assert result["data"] == { + CONF_HOST: HOST, + CONF_PROTOCOL: "rest_api", + CONF_USERNAME: USERNAME, + CONF_PASSWORD: PASSWORD, + } + + +async def test_user_modbus(hass: HomeAssistant, mock_pyiskra_modbus) -> None: + """Test the user flow with Modbus TCP protocol.""" + + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_USER}, + ) + + # Test if user form is provided + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={CONF_HOST: HOST, CONF_PROTOCOL: "modbus_tcp"}, + ) + + # Test if propmpted to enter port and address + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "modbus_tcp" + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={ + CONF_PORT: MODBUS_PORT, + CONF_ADDRESS: MODBUS_ADDRESS, + }, + ) + + # Test successful Modbus TCP configuration + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["result"].unique_id == SERIAL + assert result["title"] == PQ_MODEL + assert result["data"] == { + CONF_HOST: HOST, + CONF_PROTOCOL: "modbus_tcp", + CONF_PORT: MODBUS_PORT, + CONF_ADDRESS: MODBUS_ADDRESS, + } + + +async def test_modbus_abort_if_already_setup( + hass: HomeAssistant, mock_pyiskra_modbus +) -> None: + """Test we abort if Iskra is already setup.""" + + MockConfigEntry(domain=DOMAIN, unique_id=SERIAL).add_to_hass(hass) + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_USER}, + data={CONF_HOST: HOST, CONF_PROTOCOL: "modbus_tcp"}, + ) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "modbus_tcp" + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={ + CONF_PORT: MODBUS_PORT, + CONF_ADDRESS: MODBUS_ADDRESS, + }, + ) + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "already_configured" + + +async def test_rest_api_abort_if_already_setup( + hass: HomeAssistant, mock_pyiskra_rest +) -> None: + """Test we abort if Iskra is already setup.""" + + MockConfigEntry(domain=DOMAIN, unique_id=SERIAL).add_to_hass(hass) + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_USER}, + data={CONF_HOST: HOST, CONF_PROTOCOL: "rest_api"}, + ) + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "already_configured" + + +@pytest.mark.parametrize( + ("s_effect", "reason"), + [ + (DeviceConnectionError, "cannot_connect"), + (DeviceTimeoutError, "cannot_connect"), + (InvalidResponseCode, "cannot_connect"), + (Exception, "unknown"), + ], +) +async def test_modbus_device_error( + hass: HomeAssistant, + mock_pyiskra_modbus, + s_effect, + reason, +) -> None: + """Test device error with Modbus TCP protocol.""" + mock_pyiskra_modbus.side_effect = s_effect + + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_USER}, + data={CONF_HOST: HOST, CONF_PROTOCOL: "modbus_tcp"}, + ) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "modbus_tcp" + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={ + CONF_PORT: MODBUS_PORT, + CONF_ADDRESS: MODBUS_ADDRESS, + }, + ) + + # Test if error returned + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "modbus_tcp" + assert result["errors"] == {"base": reason} + + # Remove side effect + mock_pyiskra_modbus.side_effect = None + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={ + CONF_PORT: MODBUS_PORT, + CONF_ADDRESS: MODBUS_ADDRESS, + }, + ) + + # Test successful Modbus TCP configuration + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["result"].unique_id == SERIAL + assert result["title"] == PQ_MODEL + assert result["data"] == { + CONF_HOST: HOST, + CONF_PROTOCOL: "modbus_tcp", + CONF_PORT: MODBUS_PORT, + CONF_ADDRESS: MODBUS_ADDRESS, + } + + +@pytest.mark.parametrize( + ("s_effect", "reason"), + [ + (DeviceConnectionError, "cannot_connect"), + (DeviceTimeoutError, "cannot_connect"), + (InvalidResponseCode, "cannot_connect"), + (Exception, "unknown"), + ], +) +async def test_rest_device_error( + hass: HomeAssistant, + mock_pyiskra_rest, + s_effect, + reason, +) -> None: + """Test device error with Modbus TCP protocol.""" + mock_pyiskra_rest.side_effect = s_effect + + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_USER}, + data={CONF_HOST: HOST, CONF_PROTOCOL: "rest_api"}, + ) + + # Test if error returned + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + assert result["errors"] == {"base": reason} + + # Remove side effect + mock_pyiskra_rest.side_effect = None + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={CONF_HOST: HOST, CONF_PROTOCOL: "rest_api"}, + ) + + # Test successful Rest API configuration + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["result"].unique_id == SERIAL + assert result["title"] == SG_MODEL + assert result["data"] == {CONF_HOST: HOST, CONF_PROTOCOL: "rest_api"} diff --git a/tests/components/ista_ecotrend/test_config_flow.py b/tests/components/ista_ecotrend/test_config_flow.py index b702b0331e8a78..d6c88c51c99455 100644 --- a/tests/components/ista_ecotrend/test_config_flow.py +++ b/tests/components/ista_ecotrend/test_config_flow.py @@ -6,7 +6,7 @@ import pytest from homeassistant.components.ista_ecotrend.const import DOMAIN -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_USER from homeassistant.const import CONF_EMAIL, CONF_PASSWORD from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -98,15 +98,7 @@ async def test_reauth( ista_config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "entry_id": ista_config_entry.entry_id, - "unique_id": ista_config_entry.unique_id, - }, - ) - + result = await ista_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -148,15 +140,7 @@ async def test_reauth_error_and_recover( ista_config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "entry_id": ista_config_entry.entry_id, - "unique_id": ista_config_entry.unique_id, - }, - ) - + result = await ista_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/isy994/test_config_flow.py b/tests/components/isy994/test_config_flow.py index 411439e2e70622..34e267fe904ce5 100644 --- a/tests/components/isy994/test_config_flow.py +++ b/tests/components/isy994/test_config_flow.py @@ -644,10 +644,7 @@ async def test_reauth(hass: HomeAssistant) -> None: ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_REAUTH, "unique_id": MOCK_UUID}, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/jellyfin/test_config_flow.py b/tests/components/jellyfin/test_config_flow.py index c84a12d26a586b..a8ffbcbf46cd90 100644 --- a/tests/components/jellyfin/test_config_flow.py +++ b/tests/components/jellyfin/test_config_flow.py @@ -222,14 +222,7 @@ async def test_reauth( assert await hass.config_entries.async_setup(mock_config_entry.entry_id) await hass.async_block_till_done() - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": mock_config_entry.entry_id, - }, - data=USER_INPUT, - ) + result = await mock_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -272,14 +265,7 @@ async def test_reauth_cannot_connect( assert await hass.config_entries.async_setup(mock_config_entry.entry_id) await hass.async_block_till_done() - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": mock_config_entry.entry_id, - }, - data=USER_INPUT, - ) + result = await mock_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -339,14 +325,7 @@ async def test_reauth_invalid( assert await hass.config_entries.async_setup(mock_config_entry.entry_id) await hass.async_block_till_done() - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": mock_config_entry.entry_id, - }, - data=USER_INPUT, - ) + result = await mock_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -400,14 +379,7 @@ async def test_reauth_exception( assert await hass.config_entries.async_setup(mock_config_entry.entry_id) await hass.async_block_till_done() - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": mock_config_entry.entry_id, - }, - data=USER_INPUT, - ) + result = await mock_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/justnimbus/test_config_flow.py b/tests/components/justnimbus/test_config_flow.py index f66693a752ccc4..330b05bf48c65e 100644 --- a/tests/components/justnimbus/test_config_flow.py +++ b/tests/components/justnimbus/test_config_flow.py @@ -125,14 +125,7 @@ async def test_reauth_flow(hass: HomeAssistant) -> None: ) mock_config.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": mock_config.entry_id, - }, - data=FIXTURE_OLD_USER_INPUT, - ) + result = await mock_config.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "user" diff --git a/tests/components/jvc_projector/test_config_flow.py b/tests/components/jvc_projector/test_config_flow.py index 282411540a4c29..d7eb0995bbd54a 100644 --- a/tests/components/jvc_projector/test_config_flow.py +++ b/tests/components/jvc_projector/test_config_flow.py @@ -6,7 +6,7 @@ import pytest from homeassistant.components.jvc_projector.const import DOMAIN -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_USER from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -163,14 +163,7 @@ async def test_reauth_config_flow_success( hass: HomeAssistant, mock_device: AsyncMock, mock_integration: MockConfigEntry ) -> None: """Test reauth config flow success.""" - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "entry_id": mock_integration.entry_id, - }, - data={CONF_HOST: MOCK_HOST, CONF_PORT: MOCK_PORT}, - ) + result = await mock_integration.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -194,14 +187,7 @@ async def test_reauth_config_flow_auth_error( """Test reauth config flow when connect fails.""" mock_device.connect.side_effect = JvcProjectorAuthError - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "entry_id": mock_integration.entry_id, - }, - data={CONF_HOST: MOCK_HOST, CONF_PORT: MOCK_PORT}, - ) + result = await mock_integration.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -218,14 +204,7 @@ async def test_reauth_config_flow_auth_error( mock_device.connect.side_effect = None - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "entry_id": mock_integration.entry_id, - }, - data={CONF_HOST: MOCK_HOST, CONF_PORT: MOCK_PORT}, - ) + result = await mock_integration.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -249,14 +228,7 @@ async def test_reauth_config_flow_connect_error( """Test reauth config flow when connect fails.""" mock_device.connect.side_effect = JvcProjectorConnectError - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "entry_id": mock_integration.entry_id, - }, - data={CONF_HOST: MOCK_HOST, CONF_PORT: MOCK_PORT}, - ) + result = await mock_integration.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -273,14 +245,7 @@ async def test_reauth_config_flow_connect_error( mock_device.connect.side_effect = None - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "entry_id": mock_integration.entry_id, - }, - data={CONF_HOST: MOCK_HOST, CONF_PORT: MOCK_PORT}, - ) + result = await mock_integration.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/jvc_projector/test_coordinator.py b/tests/components/jvc_projector/test_coordinator.py index 24297348653026..b9211250aff7f8 100644 --- a/tests/components/jvc_projector/test_coordinator.py +++ b/tests/components/jvc_projector/test_coordinator.py @@ -5,7 +5,6 @@ from jvcprojector import JvcProjectorAuthError, JvcProjectorConnectError -from homeassistant.components.jvc_projector import DOMAIN from homeassistant.components.jvc_projector.coordinator import ( INTERVAL_FAST, INTERVAL_SLOW, @@ -29,7 +28,7 @@ async def test_coordinator_update( ) await hass.async_block_till_done() assert mock_device.get_state.call_count == 3 - coordinator = hass.data[DOMAIN][mock_integration.entry_id] + coordinator = mock_integration.runtime_data assert coordinator.update_interval == INTERVAL_SLOW @@ -69,5 +68,5 @@ async def test_coordinator_device_on( mock_config_entry.add_to_hass(hass) await hass.config_entries.async_setup(mock_config_entry.entry_id) await hass.async_block_till_done() - coordinator = hass.data[DOMAIN][mock_config_entry.entry_id] + coordinator = mock_config_entry.runtime_data assert coordinator.update_interval == INTERVAL_FAST diff --git a/tests/components/jvc_projector/test_init.py b/tests/components/jvc_projector/test_init.py index ef9de41ca3233a..baf088a5dba620 100644 --- a/tests/components/jvc_projector/test_init.py +++ b/tests/components/jvc_projector/test_init.py @@ -38,8 +38,6 @@ async def test_unload_config_entry( await hass.config_entries.async_unload(mock_config_entry.entry_id) await hass.async_block_till_done() - assert mock_config_entry.entry_id not in hass.data[DOMAIN] - async def test_config_entry_connect_error( hass: HomeAssistant, diff --git a/tests/components/kitchen_sink/snapshots/test_lawn_mower.ambr b/tests/components/kitchen_sink/snapshots/test_lawn_mower.ambr index 4189de18ce4e85..e3e413c5a44c78 100644 --- a/tests/components/kitchen_sink/snapshots/test_lawn_mower.ambr +++ b/tests/components/kitchen_sink/snapshots/test_lawn_mower.ambr @@ -49,6 +49,18 @@ 'last_updated': , 'state': 'docked', }), + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Mower can return', + 'supported_features': , + }), + 'context': , + 'entity_id': 'lawn_mower.mower_can_return', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'returning', + }), StateSnapshot({ 'attributes': ReadOnlyDict({ 'friendly_name': 'Mower is paused', diff --git a/tests/components/kitchen_sink/test_init.py b/tests/components/kitchen_sink/test_init.py index 0575141bb3bd69..b832577a48a502 100644 --- a/tests/components/kitchen_sink/test_init.py +++ b/tests/components/kitchen_sink/test_init.py @@ -5,6 +5,7 @@ from unittest.mock import ANY import pytest +import voluptuous as vol from homeassistant.components.kitchen_sink import DOMAIN from homeassistant.components.recorder import get_instance @@ -324,3 +325,24 @@ async def test_issues_created( }, ] } + + +async def test_service( + hass: HomeAssistant, +) -> None: + """Test we can call the service.""" + assert await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) + + with pytest.raises(vol.error.MultipleInvalid): + await hass.services.async_call(DOMAIN, "test_service_1", blocking=True) + + await hass.services.async_call( + DOMAIN, "test_service_1", {"field_1": 1, "field_2": "auto"}, blocking=True + ) + + await hass.services.async_call( + DOMAIN, + "test_service_1", + {"field_1": 1, "field_2": "auto", "field_3": 1, "field_4": "forwards"}, + blocking=True, + ) diff --git a/tests/components/kitchen_sink/test_lawn_mower.py b/tests/components/kitchen_sink/test_lawn_mower.py index 48914ab5a46323..e1ba201a7226a4 100644 --- a/tests/components/kitchen_sink/test_lawn_mower.py +++ b/tests/components/kitchen_sink/test_lawn_mower.py @@ -72,6 +72,12 @@ async def test_states(hass: HomeAssistant, snapshot: SnapshotAssertion) -> None: LawnMowerActivity.MOWING, LawnMowerActivity.DOCKED, ), + ( + "lawn_mower.mower_can_return", + SERVICE_DOCK, + LawnMowerActivity.RETURNING, + LawnMowerActivity.DOCKED, + ), ], ) async def test_mower( diff --git a/tests/components/knx/test_expose.py b/tests/components/knx/test_expose.py index c4d0acf0ce2e64..0fd790a3e336b2 100644 --- a/tests/components/knx/test_expose.py +++ b/tests/components/knx/test_expose.py @@ -108,6 +108,11 @@ async def test_expose_attribute(hass: HomeAssistant, knx: KNXTestKit) -> None: await hass.async_block_till_done() await knx.assert_telegram_count(0) + # Ignore "unavailable" state + hass.states.async_set(entity_id, "unavailable", {attribute: None}) + await hass.async_block_till_done() + await knx.assert_telegram_count(0) + async def test_expose_attribute_with_default( hass: HomeAssistant, knx: KNXTestKit @@ -131,7 +136,7 @@ async def test_expose_attribute_with_default( await knx.receive_read("1/1/8") await knx.assert_response("1/1/8", (0,)) - # Change state to "on"; no attribute + # Change state to "on"; no attribute -> default hass.states.async_set(entity_id, "on", {}) await hass.async_block_till_done() await knx.assert_write("1/1/8", (0,)) @@ -146,6 +151,11 @@ async def test_expose_attribute_with_default( await hass.async_block_till_done() await knx.assert_no_telegram() + # Use default for "unavailable" state + hass.states.async_set(entity_id, "unavailable") + await hass.async_block_till_done() + await knx.assert_write("1/1/8", (0,)) + # Change state and attribute hass.states.async_set(entity_id, "on", {attribute: 3}) await hass.async_block_till_done() @@ -290,8 +300,18 @@ async def test_expose_value_template( assert "Error rendering value template for KNX expose" in caplog.text +@pytest.mark.parametrize( + "invalid_attribute", + [ + 101.0, + "invalid", # can't cast to float + ], +) async def test_expose_conversion_exception( - hass: HomeAssistant, caplog: pytest.LogCaptureFixture, knx: KNXTestKit + hass: HomeAssistant, + caplog: pytest.LogCaptureFixture, + knx: KNXTestKit, + invalid_attribute: str, ) -> None: """Test expose throws exception.""" @@ -313,16 +333,17 @@ async def test_expose_conversion_exception( await knx.receive_read("1/1/8") await knx.assert_response("1/1/8", (3,)) + caplog.clear() # Change attribute: Expect no exception hass.states.async_set( entity_id, "on", - {attribute: 101}, + {attribute: invalid_attribute}, ) await hass.async_block_till_done() await knx.assert_no_telegram() assert ( - 'Could not expose fake.entity fake_attribute value "101.0" to KNX:' + f'Could not expose fake.entity fake_attribute value "{invalid_attribute}" to KNX:' in caplog.text ) diff --git a/tests/components/knx/test_telegrams.py b/tests/components/knx/test_telegrams.py index 2eda718f5aca49..69e3208879c275 100644 --- a/tests/components/knx/test_telegrams.py +++ b/tests/components/knx/test_telegrams.py @@ -39,7 +39,7 @@ "dpt_name": None, "payload": [1, 2, 3, 4], "source": "0.0.0", - "source_name": "", + "source_name": "Home Assistant", "telegramtype": "GroupValueWrite", "timestamp": MOCK_TIMESTAMP, "unit": None, diff --git a/tests/components/knx/test_websocket.py b/tests/components/knx/test_websocket.py index 309ea11170993f..e747b0daade6af 100644 --- a/tests/components/knx/test_websocket.py +++ b/tests/components/knx/test_websocket.py @@ -3,6 +3,8 @@ from typing import Any from unittest.mock import patch +import pytest + from homeassistant.components.knx import DOMAIN, KNX_ADDRESS, SwitchSchema from homeassistant.components.knx.project import STORAGE_KEY as KNX_PROJECT_STORAGE_KEY from homeassistant.const import CONF_NAME @@ -355,3 +357,28 @@ async def test_knx_subscribe_telegrams_command_project( ) assert res["event"]["direction"] == "Incoming" assert res["event"]["timestamp"] is not None + + +@pytest.mark.parametrize( + "endpoint", + [ + "knx/info", # sync ws-command + "knx/get_knx_project", # async ws-command + ], +) +async def test_websocket_when_config_entry_unloaded( + hass: HomeAssistant, + knx: KNXTestKit, + hass_ws_client: WebSocketGenerator, + endpoint: str, +) -> None: + """Test websocket connection when config entry is unloaded.""" + await knx.setup_integration({}) + await hass.config_entries.async_unload(knx.mock_config_entry.entry_id) + client = await hass_ws_client(hass) + + await client.send_json_auto_id({"type": endpoint}) + res = await client.receive_json() + assert not res["success"] + assert res["error"]["code"] == "home_assistant_error" + assert res["error"]["message"] == "KNX integration not loaded." diff --git a/tests/components/lacrosse_view/test_config_flow.py b/tests/components/lacrosse_view/test_config_flow.py index 5a48b3d15fe811..9ca7fb78bdd6cf 100644 --- a/tests/components/lacrosse_view/test_config_flow.py +++ b/tests/components/lacrosse_view/test_config_flow.py @@ -251,16 +251,7 @@ async def test_reauth(hass: HomeAssistant) -> None: ) mock_config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": mock_config_entry.entry_id, - "title_placeholders": {"name": mock_config_entry.title}, - "unique_id": mock_config_entry.unique_id, - }, - data=data, - ) + result = await mock_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "user" diff --git a/tests/components/lamarzocco/test_config_flow.py b/tests/components/lamarzocco/test_config_flow.py index 92ecd0a13f4c49..39896926c6170b 100644 --- a/tests/components/lamarzocco/test_config_flow.py +++ b/tests/components/lamarzocco/test_config_flow.py @@ -7,12 +7,7 @@ from homeassistant.components.lamarzocco.config_flow import CONF_MACHINE from homeassistant.components.lamarzocco.const import CONF_USE_BLUETOOTH, DOMAIN -from homeassistant.config_entries import ( - SOURCE_BLUETOOTH, - SOURCE_REAUTH, - SOURCE_USER, - ConfigEntryState, -) +from homeassistant.config_entries import SOURCE_BLUETOOTH, SOURCE_USER, ConfigEntryState from homeassistant.const import ( CONF_HOST, CONF_MAC, @@ -247,15 +242,7 @@ async def test_reauth_flow( mock_config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": mock_config_entry.unique_id, - "entry_id": mock_config_entry.entry_id, - }, - data=mock_config_entry.data, - ) + result = await mock_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/lametric/test_config_flow.py b/tests/components/lametric/test_config_flow.py index 2a21423ad03c45..3fbe606c7f1cbf 100644 --- a/tests/components/lametric/test_config_flow.py +++ b/tests/components/lametric/test_config_flow.py @@ -20,12 +20,7 @@ ATTR_UPNP_SERIAL, SsdpServiceInfo, ) -from homeassistant.config_entries import ( - SOURCE_DHCP, - SOURCE_REAUTH, - SOURCE_SSDP, - SOURCE_USER, -) +from homeassistant.config_entries import SOURCE_DHCP, SOURCE_SSDP, SOURCE_USER from homeassistant.const import CONF_API_KEY, CONF_DEVICE, CONF_HOST, CONF_MAC from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -753,15 +748,7 @@ async def test_reauth_cloud_import( """Test reauth flow importing api keys from the cloud.""" mock_config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": mock_config_entry.unique_id, - "entry_id": mock_config_entry.entry_id, - }, - data=mock_config_entry.data, - ) + result = await mock_config_entry.start_reauth_flow(hass) flow_id = result["flow_id"] @@ -817,15 +804,7 @@ async def test_reauth_cloud_abort_device_not_found( mock_config_entry.add_to_hass(hass) hass.config_entries.async_update_entry(mock_config_entry, unique_id="UKNOWN_DEVICE") - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": mock_config_entry.unique_id, - "entry_id": mock_config_entry.entry_id, - }, - data=mock_config_entry.data, - ) + result = await mock_config_entry.start_reauth_flow(hass) flow_id = result["flow_id"] @@ -872,15 +851,7 @@ async def test_reauth_manual( """Test reauth flow with manual entry.""" mock_config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": mock_config_entry.unique_id, - "entry_id": mock_config_entry.entry_id, - }, - data=mock_config_entry.data, - ) + result = await mock_config_entry.start_reauth_flow(hass) flow_id = result["flow_id"] @@ -914,15 +885,7 @@ async def test_reauth_manual_sky( """Test reauth flow with manual entry for LaMetric Sky.""" mock_config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": mock_config_entry.unique_id, - "entry_id": mock_config_entry.entry_id, - }, - data=mock_config_entry.data, - ) + result = await mock_config_entry.start_reauth_flow(hass) flow_id = result["flow_id"] diff --git a/tests/components/laundrify/test_config_flow.py b/tests/components/laundrify/test_config_flow.py index 69a4b957cf5e03..8bb8211195c2d4 100644 --- a/tests/components/laundrify/test_config_flow.py +++ b/tests/components/laundrify/test_config_flow.py @@ -3,7 +3,7 @@ from laundrify_aio import exceptions from homeassistant.components.laundrify.const import DOMAIN -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_USER from homeassistant.const import CONF_ACCESS_TOKEN, CONF_CODE, CONF_SOURCE from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -95,9 +95,8 @@ async def test_form_unkown_exception( async def test_step_reauth(hass: HomeAssistant) -> None: """Test the reauth form is shown.""" - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_REAUTH} - ) + config_entry = create_entry(hass) + result = await config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["errors"] is None diff --git a/tests/components/lcn/test_config_flow.py b/tests/components/lcn/test_config_flow.py index e4cb4c588e999e..d002c5fe625288 100644 --- a/tests/components/lcn/test_config_flow.py +++ b/tests/components/lcn/test_config_flow.py @@ -129,7 +129,7 @@ async def test_show_form(hass: HomeAssistant) -> None: assert result["step_id"] == "user" -async def test_step_user(hass): +async def test_step_user(hass: HomeAssistant) -> None: """Test for user step.""" with ( patch("pypck.connection.PchkConnectionManager.async_connect"), @@ -150,7 +150,9 @@ async def test_step_user(hass): } -async def test_step_user_existing_host(hass, entry): +async def test_step_user_existing_host( + hass: HomeAssistant, entry: MockConfigEntry +) -> None: """Test for user defined host already exists.""" entry.add_to_hass(hass) @@ -172,7 +174,9 @@ async def test_step_user_existing_host(hass, entry): (TimeoutError, {CONF_BASE: "connection_refused"}), ], ) -async def test_step_user_error(hass, error, errors): +async def test_step_user_error( + hass: HomeAssistant, error: type[Exception], errors: dict[str, str] +) -> None: """Test for error in user step is handled correctly.""" with patch( "pypck.connection.PchkConnectionManager.async_connect", side_effect=error @@ -187,7 +191,7 @@ async def test_step_user_error(hass, error, errors): assert result["errors"] == errors -async def test_step_reconfigure(hass, entry): +async def test_step_reconfigure(hass: HomeAssistant, entry: MockConfigEntry) -> None: """Test for reconfigure step.""" entry.add_to_hass(hass) old_entry_data = entry.data.copy() @@ -222,7 +226,12 @@ async def test_step_reconfigure(hass, entry): (TimeoutError, {CONF_BASE: "connection_refused"}), ], ) -async def test_step_reconfigure_error(hass, entry, error, errors): +async def test_step_reconfigure_error( + hass: HomeAssistant, + entry: MockConfigEntry, + error: type[Exception], + errors: dict[str, str], +) -> None: """Test for error in reconfigure step is handled correctly.""" entry.add_to_hass(hass) with patch( @@ -242,7 +251,7 @@ async def test_step_reconfigure_error(hass, entry, error, errors): assert result["errors"] == errors -async def test_validate_connection(): +async def test_validate_connection() -> None: """Test the connection validation.""" data = CONNECTION_DATA.copy() diff --git a/tests/components/lektrico/__init__.py b/tests/components/lektrico/__init__.py new file mode 100644 index 00000000000000..449da2b35c4971 --- /dev/null +++ b/tests/components/lektrico/__init__.py @@ -0,0 +1,13 @@ +"""Tests for Lektrico integration.""" + +from homeassistant.core import HomeAssistant + +from tests.common import MockConfigEntry + + +async def setup_integration(hass: HomeAssistant, config_entry: MockConfigEntry) -> None: + """Fixture for setting up the component.""" + config_entry.add_to_hass(hass) + + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() diff --git a/tests/components/lektrico/conftest.py b/tests/components/lektrico/conftest.py new file mode 100644 index 00000000000000..fd840b0c290e36 --- /dev/null +++ b/tests/components/lektrico/conftest.py @@ -0,0 +1,92 @@ +"""Fixtures for Lektrico Charging Station integration tests.""" + +from collections.abc import Generator +from ipaddress import ip_address +import json +from unittest.mock import AsyncMock, patch + +import pytest + +from homeassistant.components.lektrico.const import DOMAIN +from homeassistant.components.zeroconf import ZeroconfServiceInfo +from homeassistant.const import ( + ATTR_HW_VERSION, + ATTR_SERIAL_NUMBER, + CONF_HOST, + CONF_TYPE, +) + +from tests.common import MockConfigEntry, load_fixture + +MOCKED_DEVICE_IP_ADDRESS = "192.168.100.10" +MOCKED_DEVICE_SERIAL_NUMBER = "500006" +MOCKED_DEVICE_TYPE = "1p7k" +MOCKED_DEVICE_BOARD_REV = "B" + +MOCKED_DEVICE_ZC_NAME = "Lektrico-1p7k-500006._http._tcp" +MOCKED_DEVICE_ZC_TYPE = "_http._tcp.local." +MOCKED_DEVICE_ZEROCONF_DATA = ZeroconfServiceInfo( + ip_address=ip_address(MOCKED_DEVICE_IP_ADDRESS), + ip_addresses=[ip_address(MOCKED_DEVICE_IP_ADDRESS)], + hostname=f"{MOCKED_DEVICE_ZC_NAME.lower()}.local.", + port=80, + type=MOCKED_DEVICE_ZC_TYPE, + name=MOCKED_DEVICE_ZC_NAME, + properties={ + "id": "1p7k_500006", + "fw_id": "20230109-124642/v1.22-36-g56a3edd-develop-dirty", + }, +) + + +@pytest.fixture +def mock_device() -> Generator[AsyncMock]: + """Mock a Lektrico device.""" + with ( + patch( + "homeassistant.components.lektrico.Device", + autospec=True, + ) as mock_device, + patch( + "homeassistant.components.lektrico.config_flow.Device", + new=mock_device, + ), + patch( + "homeassistant.components.lektrico.coordinator.Device", + new=mock_device, + ), + ): + device = mock_device.return_value + + device.device_config.return_value = json.loads( + load_fixture("get_config.json", DOMAIN) + ) + device.device_info.return_value = json.loads( + load_fixture("get_info.json", DOMAIN) + ) + + yield device + + +@pytest.fixture +def mock_setup_entry() -> Generator[AsyncMock]: + """Mock setup entry.""" + with patch( + "homeassistant.components.lektrico.async_setup_entry", return_value=True + ) as mock_setup_entry: + yield mock_setup_entry + + +@pytest.fixture +def mock_config_entry() -> MockConfigEntry: + """Mock a config entry.""" + return MockConfigEntry( + domain=DOMAIN, + data={ + CONF_HOST: MOCKED_DEVICE_IP_ADDRESS, + CONF_TYPE: MOCKED_DEVICE_TYPE, + ATTR_SERIAL_NUMBER: MOCKED_DEVICE_SERIAL_NUMBER, + ATTR_HW_VERSION: "B", + }, + unique_id=MOCKED_DEVICE_SERIAL_NUMBER, + ) diff --git a/tests/components/lektrico/fixtures/current_measures.json b/tests/components/lektrico/fixtures/current_measures.json new file mode 100644 index 00000000000000..1175b49f63c5fc --- /dev/null +++ b/tests/components/lektrico/fixtures/current_measures.json @@ -0,0 +1,16 @@ +{ + "charger_state": "Available", + "charging_time": 0, + "instant_power": 0, + "session_energy": 0.0, + "temperature": 34.5, + "total_charged_energy": 0, + "install_current": 6, + "current_limit_reason": "Installation current", + "voltage_l1": 220.0, + "current_l1": 0.0, + "type": "1p7k", + "serial_number": "500006", + "board_revision": "B", + "fw_version": "1.44" +} diff --git a/tests/components/lektrico/fixtures/get_config.json b/tests/components/lektrico/fixtures/get_config.json new file mode 100644 index 00000000000000..175475004ecde5 --- /dev/null +++ b/tests/components/lektrico/fixtures/get_config.json @@ -0,0 +1,5 @@ +{ + "type": "1p7k", + "serial_number": "500006", + "board_revision": "B" +} diff --git a/tests/components/lektrico/fixtures/get_info.json b/tests/components/lektrico/fixtures/get_info.json new file mode 100644 index 00000000000000..a8f2a56b8d8b8d --- /dev/null +++ b/tests/components/lektrico/fixtures/get_info.json @@ -0,0 +1,13 @@ +{ + "charger_state": "available", + "charging_time": 0, + "instant_power": 0, + "session_energy": 0.0, + "temperature": 34.5, + "total_charged_energy": 0, + "install_current": 6, + "current_limit_reason": "installation_current", + "voltage_l1": 220.0, + "current_l1": 0.0, + "fw_version": "1.44" +} diff --git a/tests/components/lektrico/snapshots/test_init.ambr b/tests/components/lektrico/snapshots/test_init.ambr new file mode 100644 index 00000000000000..63739e1c9d87b2 --- /dev/null +++ b/tests/components/lektrico/snapshots/test_init.ambr @@ -0,0 +1,33 @@ +# serializer version: 1 +# name: test_device_info + DeviceRegistryEntrySnapshot({ + 'area_id': None, + 'config_entries': , + 'configuration_url': None, + 'connections': set({ + }), + 'disabled_by': None, + 'entry_type': None, + 'hw_version': 'B', + 'id': , + 'identifiers': set({ + tuple( + 'lektrico', + '500006', + ), + }), + 'is_new': False, + 'labels': set({ + }), + 'manufacturer': 'Lektrico', + 'model': '1P7K', + 'model_id': None, + 'name': '1p7k_500006', + 'name_by_user': None, + 'primary_config_entry': , + 'serial_number': '500006', + 'suggested_area': None, + 'sw_version': '1.44', + 'via_device_id': None, + }) +# --- diff --git a/tests/components/lektrico/snapshots/test_sensor.ambr b/tests/components/lektrico/snapshots/test_sensor.ambr new file mode 100644 index 00000000000000..7df5df70218d3a --- /dev/null +++ b/tests/components/lektrico/snapshots/test_sensor.ambr @@ -0,0 +1,534 @@ +# serializer version: 1 +# name: test_all_entities[sensor.1p7k_500006_charging_time-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.1p7k_500006_charging_time', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Charging time', + 'platform': 'lektrico', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'charging_time', + 'unique_id': '500006_charging_time', + 'unit_of_measurement': , + }) +# --- +# name: test_all_entities[sensor.1p7k_500006_charging_time-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'duration', + 'friendly_name': '1p7k_500006 Charging time', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.1p7k_500006_charging_time', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0', + }) +# --- +# name: test_all_entities[sensor.1p7k_500006_current-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.1p7k_500006_current', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Current', + 'platform': 'lektrico', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '500006_current', + 'unit_of_measurement': , + }) +# --- +# name: test_all_entities[sensor.1p7k_500006_current-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'current', + 'friendly_name': '1p7k_500006 Current', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.1p7k_500006_current', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0.0', + }) +# --- +# name: test_all_entities[sensor.1p7k_500006_energy-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.1p7k_500006_energy', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Energy', + 'platform': 'lektrico', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '500006_energy', + 'unit_of_measurement': , + }) +# --- +# name: test_all_entities[sensor.1p7k_500006_energy-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'energy', + 'friendly_name': '1p7k_500006 Energy', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.1p7k_500006_energy', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0.0', + }) +# --- +# name: test_all_entities[sensor.1p7k_500006_installation_current-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.1p7k_500006_installation_current', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Installation current', + 'platform': 'lektrico', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'installation_current', + 'unique_id': '500006_installation_current', + 'unit_of_measurement': , + }) +# --- +# name: test_all_entities[sensor.1p7k_500006_installation_current-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'current', + 'friendly_name': '1p7k_500006 Installation current', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.1p7k_500006_installation_current', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '6', + }) +# --- +# name: test_all_entities[sensor.1p7k_500006_lifetime_energy-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.1p7k_500006_lifetime_energy', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Lifetime energy', + 'platform': 'lektrico', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'lifetime_energy', + 'unique_id': '500006_lifetime_energy', + 'unit_of_measurement': , + }) +# --- +# name: test_all_entities[sensor.1p7k_500006_lifetime_energy-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'energy', + 'friendly_name': '1p7k_500006 Lifetime energy', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.1p7k_500006_lifetime_energy', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0', + }) +# --- +# name: test_all_entities[sensor.1p7k_500006_limit_reason-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'options': list([ + 'no_limit', + 'installation_current', + 'user_limit', + 'dynamic_limit', + 'schedule', + 'em_offline', + 'em', + 'ocpp', + ]), + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.1p7k_500006_limit_reason', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Limit reason', + 'platform': 'lektrico', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'limit_reason', + 'unique_id': '500006_limit_reason', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_entities[sensor.1p7k_500006_limit_reason-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'enum', + 'friendly_name': '1p7k_500006 Limit reason', + 'options': list([ + 'no_limit', + 'installation_current', + 'user_limit', + 'dynamic_limit', + 'schedule', + 'em_offline', + 'em', + 'ocpp', + ]), + }), + 'context': , + 'entity_id': 'sensor.1p7k_500006_limit_reason', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'installation_current', + }) +# --- +# name: test_all_entities[sensor.1p7k_500006_power-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.1p7k_500006_power', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Power', + 'platform': 'lektrico', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '500006_power', + 'unit_of_measurement': , + }) +# --- +# name: test_all_entities[sensor.1p7k_500006_power-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'power', + 'friendly_name': '1p7k_500006 Power', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.1p7k_500006_power', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0.0000', + }) +# --- +# name: test_all_entities[sensor.1p7k_500006_state-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'options': list([ + 'available', + 'connected', + 'need_auth', + 'paused', + 'charging', + 'error', + 'updating_firmware', + ]), + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.1p7k_500006_state', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'State', + 'platform': 'lektrico', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'state', + 'unique_id': '500006_state', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_entities[sensor.1p7k_500006_state-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'enum', + 'friendly_name': '1p7k_500006 State', + 'options': list([ + 'available', + 'connected', + 'need_auth', + 'paused', + 'charging', + 'error', + 'updating_firmware', + ]), + }), + 'context': , + 'entity_id': 'sensor.1p7k_500006_state', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'available', + }) +# --- +# name: test_all_entities[sensor.1p7k_500006_temperature-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.1p7k_500006_temperature', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Temperature', + 'platform': 'lektrico', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '500006_temperature', + 'unit_of_measurement': , + }) +# --- +# name: test_all_entities[sensor.1p7k_500006_temperature-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'temperature', + 'friendly_name': '1p7k_500006 Temperature', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.1p7k_500006_temperature', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '34.5', + }) +# --- +# name: test_all_entities[sensor.1p7k_500006_voltage-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.1p7k_500006_voltage', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Voltage', + 'platform': 'lektrico', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '500006_voltage', + 'unit_of_measurement': , + }) +# --- +# name: test_all_entities[sensor.1p7k_500006_voltage-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'voltage', + 'friendly_name': '1p7k_500006 Voltage', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.1p7k_500006_voltage', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '220.0', + }) +# --- diff --git a/tests/components/lektrico/test_config_flow.py b/tests/components/lektrico/test_config_flow.py new file mode 100644 index 00000000000000..15ab5f7cdda1da --- /dev/null +++ b/tests/components/lektrico/test_config_flow.py @@ -0,0 +1,173 @@ +"""Tests for the Lektrico Charging Station config flow.""" + +import dataclasses +from ipaddress import ip_address + +from lektricowifi import DeviceConnectionError + +from homeassistant.components.lektrico.const import DOMAIN +from homeassistant.config_entries import SOURCE_USER, SOURCE_ZEROCONF +from homeassistant.const import ( + ATTR_HW_VERSION, + ATTR_SERIAL_NUMBER, + CONF_HOST, + CONF_TYPE, +) +from homeassistant.core import HomeAssistant +from homeassistant.data_entry_flow import FlowResultType + +from .conftest import ( + MOCKED_DEVICE_BOARD_REV, + MOCKED_DEVICE_IP_ADDRESS, + MOCKED_DEVICE_SERIAL_NUMBER, + MOCKED_DEVICE_TYPE, + MOCKED_DEVICE_ZEROCONF_DATA, +) + +from tests.common import MockConfigEntry + + +async def test_user_setup(hass: HomeAssistant, mock_device, mock_setup_entry) -> None: + """Test manually setting up.""" + + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_USER}, + ) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == SOURCE_USER + assert "flow_id" in result + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + { + CONF_HOST: MOCKED_DEVICE_IP_ADDRESS, + }, + ) + + assert result.get("type") is FlowResultType.CREATE_ENTRY + assert result.get("title") == f"{MOCKED_DEVICE_TYPE}_{MOCKED_DEVICE_SERIAL_NUMBER}" + assert result.get("data") == { + CONF_HOST: MOCKED_DEVICE_IP_ADDRESS, + ATTR_SERIAL_NUMBER: MOCKED_DEVICE_SERIAL_NUMBER, + CONF_TYPE: MOCKED_DEVICE_TYPE, + ATTR_HW_VERSION: MOCKED_DEVICE_BOARD_REV, + } + assert "result" in result + assert len(mock_setup_entry.mock_calls) == 1 + assert result.get("result").unique_id == MOCKED_DEVICE_SERIAL_NUMBER + + +async def test_user_setup_already_exists( + hass: HomeAssistant, mock_device, mock_config_entry: MockConfigEntry +) -> None: + """Test manually setting up when the device already exists.""" + mock_config_entry.add_to_hass(hass) + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + assert not result["errors"] + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + { + CONF_HOST: MOCKED_DEVICE_IP_ADDRESS, + }, + ) + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "already_configured" + + +async def test_user_setup_device_offline(hass: HomeAssistant, mock_device) -> None: + """Test manually setting up when device is offline.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + assert not result["errors"] + + mock_device.device_config.side_effect = DeviceConnectionError + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + { + CONF_HOST: MOCKED_DEVICE_IP_ADDRESS, + }, + ) + + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {CONF_HOST: "cannot_connect"} + assert result["step_id"] == "user" + + mock_device.device_config.side_effect = None + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + { + CONF_HOST: MOCKED_DEVICE_IP_ADDRESS, + }, + ) + + assert result["type"] is FlowResultType.CREATE_ENTRY + + +async def test_discovered_zeroconf( + hass: HomeAssistant, mock_device, mock_setup_entry +) -> None: + """Test we can setup when discovered from zeroconf.""" + + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_ZEROCONF}, + data=MOCKED_DEVICE_ZEROCONF_DATA, + ) + assert result["type"] is FlowResultType.FORM + assert result["errors"] is None + assert result.get("step_id") == "confirm" + + result2 = await hass.config_entries.flow.async_configure(result["flow_id"], {}) + assert result2["type"] is FlowResultType.CREATE_ENTRY + assert result2["data"] == { + CONF_HOST: MOCKED_DEVICE_IP_ADDRESS, + ATTR_SERIAL_NUMBER: MOCKED_DEVICE_SERIAL_NUMBER, + CONF_TYPE: MOCKED_DEVICE_TYPE, + ATTR_HW_VERSION: MOCKED_DEVICE_BOARD_REV, + } + assert result2["title"] == f"{MOCKED_DEVICE_TYPE}_{MOCKED_DEVICE_SERIAL_NUMBER}" + + +async def test_zeroconf_setup_already_exists( + hass: HomeAssistant, mock_device, mock_config_entry: MockConfigEntry +) -> None: + """Test we abort zeroconf flow if device already configured.""" + mock_config_entry.add_to_hass(hass) + zc_data_new_ip = dataclasses.replace(MOCKED_DEVICE_ZEROCONF_DATA) + zc_data_new_ip.ip_address = ip_address(MOCKED_DEVICE_IP_ADDRESS) + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_ZEROCONF}, + data=zc_data_new_ip, + ) + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "already_configured" + + +async def test_discovered_zeroconf_device_connection_error( + hass: HomeAssistant, mock_device +) -> None: + """Test we can setup when discovered from zeroconf but device went offline.""" + + mock_device.device_config.side_effect = DeviceConnectionError + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_ZEROCONF}, + data=MOCKED_DEVICE_ZEROCONF_DATA, + ) + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "cannot_connect" diff --git a/tests/components/lektrico/test_init.py b/tests/components/lektrico/test_init.py new file mode 100644 index 00000000000000..93068ffe5317ad --- /dev/null +++ b/tests/components/lektrico/test_init.py @@ -0,0 +1,29 @@ +"""Tests for the Lektrico integration.""" + +from unittest.mock import AsyncMock + +from syrupy import SnapshotAssertion + +from homeassistant.components.lektrico.const import DOMAIN +from homeassistant.core import HomeAssistant +from homeassistant.helpers import device_registry as dr + +from . import setup_integration + +from tests.common import MockConfigEntry + + +async def test_device_info( + hass: HomeAssistant, + snapshot: SnapshotAssertion, + mock_device: AsyncMock, + mock_config_entry: MockConfigEntry, + device_registry: dr.DeviceRegistry, +) -> None: + """Test device registry integration.""" + await setup_integration(hass, mock_config_entry) + device_entry = device_registry.async_get_device( + identifiers={(DOMAIN, mock_config_entry.unique_id)} + ) + assert device_entry is not None + assert device_entry == snapshot diff --git a/tests/components/lektrico/test_sensor.py b/tests/components/lektrico/test_sensor.py new file mode 100644 index 00000000000000..756f149d3ad9b9 --- /dev/null +++ b/tests/components/lektrico/test_sensor.py @@ -0,0 +1,31 @@ +"""Tests for the Lektrico sensor platform.""" + +from unittest.mock import AsyncMock, patch + +import pytest +from syrupy import SnapshotAssertion + +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from . import setup_integration + +from tests.common import MockConfigEntry, snapshot_platform + + +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_all_entities( + hass: HomeAssistant, + snapshot: SnapshotAssertion, + mock_device: AsyncMock, + mock_config_entry: MockConfigEntry, + entity_registry: er.EntityRegistry, +) -> None: + """Test all entities.""" + with patch( + "homeassistant.components.lektrico.CHARGERS_PLATFORMS", [Platform.SENSOR] + ): + await setup_integration(hass, mock_config_entry) + + await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) diff --git a/tests/components/lg_thinq/__init__.py b/tests/components/lg_thinq/__init__.py new file mode 100644 index 00000000000000..68ffb960f71e1b --- /dev/null +++ b/tests/components/lg_thinq/__init__.py @@ -0,0 +1 @@ +"""Tests for the lgthinq integration.""" diff --git a/tests/components/lg_thinq/conftest.py b/tests/components/lg_thinq/conftest.py new file mode 100644 index 00000000000000..cae2de61fa4f89 --- /dev/null +++ b/tests/components/lg_thinq/conftest.py @@ -0,0 +1,86 @@ +"""Configure tests for the LGThinQ integration.""" + +from collections.abc import Generator +from unittest.mock import AsyncMock, MagicMock, patch + +import pytest +from thinqconnect import ThinQAPIException + +from homeassistant.components.lg_thinq.const import CONF_CONNECT_CLIENT_ID, DOMAIN +from homeassistant.const import CONF_ACCESS_TOKEN, CONF_COUNTRY + +from .const import MOCK_CONNECT_CLIENT_ID, MOCK_COUNTRY, MOCK_PAT, MOCK_UUID + +from tests.common import MockConfigEntry + + +def mock_thinq_api_response( + *, + status: int = 200, + body: dict | None = None, + error_code: str | None = None, + error_message: str | None = None, +) -> MagicMock: + """Create a mock thinq api response.""" + response = MagicMock() + response.status = status + response.body = body + response.error_code = error_code + response.error_message = error_message + return response + + +@pytest.fixture +def mock_config_entry() -> MockConfigEntry: + """Create a mock config entry.""" + return MockConfigEntry( + domain=DOMAIN, + title=f"Test {DOMAIN}", + unique_id=MOCK_PAT, + data={ + CONF_ACCESS_TOKEN: MOCK_PAT, + CONF_CONNECT_CLIENT_ID: MOCK_CONNECT_CLIENT_ID, + CONF_COUNTRY: MOCK_COUNTRY, + }, + ) + + +@pytest.fixture +def mock_uuid() -> Generator[AsyncMock]: + """Mock a uuid.""" + with ( + patch("uuid.uuid4", autospec=True, return_value=MOCK_UUID) as mock_uuid, + patch( + "homeassistant.components.lg_thinq.config_flow.uuid.uuid4", + new=mock_uuid, + ), + ): + yield mock_uuid.return_value + + +@pytest.fixture +def mock_thinq_api() -> Generator[AsyncMock]: + """Mock a thinq api.""" + with ( + patch("thinqconnect.ThinQApi", autospec=True) as mock_api, + patch( + "homeassistant.components.lg_thinq.config_flow.ThinQApi", + new=mock_api, + ), + ): + thinq_api = mock_api.return_value + thinq_api.async_get_device_list = AsyncMock( + return_value=mock_thinq_api_response(status=200, body={}) + ) + yield thinq_api + + +@pytest.fixture +def mock_invalid_thinq_api(mock_thinq_api: AsyncMock) -> AsyncMock: + """Mock an invalid thinq api.""" + mock_thinq_api.async_get_device_list = AsyncMock( + side_effect=ThinQAPIException( + code="1309", message="Not allowed api call", headers=None + ) + ) + return mock_thinq_api diff --git a/tests/components/lg_thinq/const.py b/tests/components/lg_thinq/const.py new file mode 100644 index 00000000000000..f46baa61c38aba --- /dev/null +++ b/tests/components/lg_thinq/const.py @@ -0,0 +1,8 @@ +"""Constants for lgthinq test.""" + +from typing import Final + +MOCK_PAT: Final[str] = "123abc4567de8f90g123h4ij56klmn789012p345rst6uvw789xy" +MOCK_UUID: Final[str] = "1b3deabc-123d-456d-987d-2a1c7b3bdb67" +MOCK_CONNECT_CLIENT_ID: Final[str] = f"home-assistant-{MOCK_UUID}" +MOCK_COUNTRY: Final[str] = "KR" diff --git a/tests/components/lg_thinq/test_config_flow.py b/tests/components/lg_thinq/test_config_flow.py new file mode 100644 index 00000000000000..db0e2d29450838 --- /dev/null +++ b/tests/components/lg_thinq/test_config_flow.py @@ -0,0 +1,66 @@ +"""Test the lgthinq config flow.""" + +from unittest.mock import AsyncMock + +from homeassistant.components.lg_thinq.const import CONF_CONNECT_CLIENT_ID, DOMAIN +from homeassistant.config_entries import SOURCE_USER +from homeassistant.const import CONF_ACCESS_TOKEN, CONF_COUNTRY +from homeassistant.core import HomeAssistant +from homeassistant.data_entry_flow import FlowResultType + +from .const import MOCK_CONNECT_CLIENT_ID, MOCK_COUNTRY, MOCK_PAT + +from tests.common import MockConfigEntry + + +async def test_config_flow( + hass: HomeAssistant, mock_thinq_api: AsyncMock, mock_uuid: AsyncMock +) -> None: + """Test that an thinq entry is normally created.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={CONF_ACCESS_TOKEN: MOCK_PAT, CONF_COUNTRY: MOCK_COUNTRY}, + ) + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["data"] == { + CONF_ACCESS_TOKEN: MOCK_PAT, + CONF_COUNTRY: MOCK_COUNTRY, + CONF_CONNECT_CLIENT_ID: MOCK_CONNECT_CLIENT_ID, + } + + mock_thinq_api.async_get_device_list.assert_called_once() + + +async def test_config_flow_invalid_pat( + hass: HomeAssistant, mock_invalid_thinq_api: AsyncMock +) -> None: + """Test that an thinq flow should be aborted with an invalid PAT.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_USER}, + data={CONF_ACCESS_TOKEN: MOCK_PAT, CONF_COUNTRY: MOCK_COUNTRY}, + ) + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {"base": "token_unauthorized"} + mock_invalid_thinq_api.async_get_device_list.assert_called_once() + + +async def test_config_flow_already_configured( + hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_thinq_api: AsyncMock +) -> None: + """Test that thinq flow should be aborted when already configured.""" + mock_config_entry.add_to_hass(hass) + + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_USER}, + data={CONF_ACCESS_TOKEN: MOCK_PAT, CONF_COUNTRY: MOCK_COUNTRY}, + ) + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "already_configured" diff --git a/tests/components/lidarr/test_config_flow.py b/tests/components/lidarr/test_config_flow.py index e44b03cd2a26dd..0097e66fe2425a 100644 --- a/tests/components/lidarr/test_config_flow.py +++ b/tests/components/lidarr/test_config_flow.py @@ -1,13 +1,15 @@ """Test Lidarr config flow.""" from homeassistant.components.lidarr.const import DEFAULT_NAME, DOMAIN -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_USER from homeassistant.const import CONF_API_KEY, CONF_SOURCE from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType from .conftest import CONF_DATA, MOCK_INPUT, ComponentSetup +from tests.common import MockConfigEntry + async def test_flow_user_form(hass: HomeAssistant, connection) -> None: """Test that the user set up form is served.""" @@ -95,20 +97,14 @@ async def test_flow_user_unknown_error(hass: HomeAssistant, unknown) -> None: async def test_flow_reauth( - hass: HomeAssistant, setup_integration: ComponentSetup, connection + hass: HomeAssistant, + setup_integration: ComponentSetup, + connection, + config_entry: MockConfigEntry, ) -> None: """Test reauth.""" await setup_integration() - entry = hass.config_entries.async_entries(DOMAIN)[0] - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - CONF_SOURCE: SOURCE_REAUTH, - "entry_id": entry.entry_id, - "unique_id": entry.unique_id, - }, - data=CONF_DATA, - ) + result = await config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" result = await hass.config_entries.flow.async_configure( @@ -123,4 +119,4 @@ async def test_flow_reauth( ) assert result["type"] is FlowResultType.ABORT assert result["reason"] == "reauth_successful" - assert entry.data[CONF_API_KEY] == "abc123" + assert config_entry.data[CONF_API_KEY] == "abc123" diff --git a/tests/components/linear_garage_door/test_config_flow.py b/tests/components/linear_garage_door/test_config_flow.py index 4599bd24aefe27..64bdc589194494 100644 --- a/tests/components/linear_garage_door/test_config_flow.py +++ b/tests/components/linear_garage_door/test_config_flow.py @@ -6,7 +6,7 @@ import pytest from homeassistant.components.linear_garage_door.const import DOMAIN -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_USER from homeassistant.const import CONF_EMAIL, CONF_PASSWORD from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -61,16 +61,7 @@ async def test_reauth( ) -> None: """Test reauthentication.""" mock_config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "entry_id": mock_config_entry.entry_id, - "title_placeholders": {"name": mock_config_entry.title}, - "unique_id": mock_config_entry.unique_id, - }, - data=mock_config_entry.data, - ) + result = await mock_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "user" diff --git a/tests/components/linkplay/conftest.py b/tests/components/linkplay/conftest.py index b3d65422e08d31..be83dd2412d325 100644 --- a/tests/components/linkplay/conftest.py +++ b/tests/components/linkplay/conftest.py @@ -3,6 +3,7 @@ from collections.abc import Generator from unittest.mock import AsyncMock, patch +from aiohttp import ClientSession from linkplay.bridge import LinkPlayBridge, LinkPlayDevice import pytest @@ -14,11 +15,15 @@ @pytest.fixture def mock_linkplay_factory_bridge() -> Generator[AsyncMock]: - """Mock for linkplay_factory_bridge.""" + """Mock for linkplay_factory_httpapi_bridge.""" with ( patch( - "homeassistant.components.linkplay.config_flow.linkplay_factory_bridge" + "homeassistant.components.linkplay.config_flow.async_get_client_session", + return_value=AsyncMock(spec=ClientSession), + ), + patch( + "homeassistant.components.linkplay.config_flow.linkplay_factory_httpapi_bridge", ) as factory, ): bridge = AsyncMock(spec=LinkPlayBridge) diff --git a/tests/components/linkplay/test_config_flow.py b/tests/components/linkplay/test_config_flow.py index 641f09893c2061..3fd1fbea95ebb6 100644 --- a/tests/components/linkplay/test_config_flow.py +++ b/tests/components/linkplay/test_config_flow.py @@ -3,6 +3,9 @@ from ipaddress import ip_address from unittest.mock import AsyncMock +from linkplay.exceptions import LinkPlayRequestException +import pytest + from homeassistant.components.linkplay.const import DOMAIN from homeassistant.components.zeroconf import ZeroconfServiceInfo from homeassistant.config_entries import SOURCE_USER, SOURCE_ZEROCONF @@ -47,10 +50,9 @@ ) +@pytest.mark.usefixtures("mock_linkplay_factory_bridge", "mock_setup_entry") async def test_user_flow( hass: HomeAssistant, - mock_linkplay_factory_bridge: AsyncMock, - mock_setup_entry: AsyncMock, ) -> None: """Test user setup config flow.""" result = await hass.config_entries.flow.async_init( @@ -74,10 +76,9 @@ async def test_user_flow( assert result["result"].unique_id == UUID +@pytest.mark.usefixtures("mock_linkplay_factory_bridge") async def test_user_flow_re_entry( hass: HomeAssistant, - mock_linkplay_factory_bridge: AsyncMock, - mock_setup_entry: AsyncMock, ) -> None: """Test user setup config flow when an entry with the same unique id already exists.""" @@ -105,10 +106,9 @@ async def test_user_flow_re_entry( assert result["reason"] == "already_configured" +@pytest.mark.usefixtures("mock_linkplay_factory_bridge", "mock_setup_entry") async def test_zeroconf_flow( hass: HomeAssistant, - mock_linkplay_factory_bridge: AsyncMock, - mock_setup_entry: AsyncMock, ) -> None: """Test Zeroconf flow.""" result = await hass.config_entries.flow.async_init( @@ -133,10 +133,9 @@ async def test_zeroconf_flow( assert result["result"].unique_id == UUID +@pytest.mark.usefixtures("mock_linkplay_factory_bridge") async def test_zeroconf_flow_re_entry( hass: HomeAssistant, - mock_linkplay_factory_bridge: AsyncMock, - mock_setup_entry: AsyncMock, ) -> None: """Test Zeroconf flow when an entry with the same unique id already exists.""" @@ -160,16 +159,35 @@ async def test_zeroconf_flow_re_entry( assert result["reason"] == "already_configured" -async def test_flow_errors( +@pytest.mark.usefixtures("mock_setup_entry") +async def test_zeroconf_flow_errors( + hass: HomeAssistant, + mock_linkplay_factory_bridge: AsyncMock, +) -> None: + """Test flow when the device discovered through Zeroconf cannot be reached.""" + + # Temporarily make the mock_linkplay_factory_bridge throw an exception + mock_linkplay_factory_bridge.side_effect = (LinkPlayRequestException("Error"),) + + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_ZEROCONF}, + data=ZEROCONF_DISCOVERY, + ) + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "cannot_connect" + + +@pytest.mark.usefixtures("mock_setup_entry") +async def test_user_flow_errors( hass: HomeAssistant, mock_linkplay_factory_bridge: AsyncMock, - mock_setup_entry: AsyncMock, ) -> None: """Test flow when the device cannot be reached.""" - # Temporarily store bridge in a separate variable and set factory to return None - bridge = mock_linkplay_factory_bridge.return_value - mock_linkplay_factory_bridge.return_value = None + # Temporarily make the mock_linkplay_factory_bridge throw an exception + mock_linkplay_factory_bridge.side_effect = (LinkPlayRequestException("Error"),) result = await hass.config_entries.flow.async_init( DOMAIN, @@ -188,8 +206,8 @@ async def test_flow_errors( assert result["step_id"] == "user" assert result["errors"] == {"base": "cannot_connect"} - # Make linkplay_factory_bridge return a mock bridge again - mock_linkplay_factory_bridge.return_value = bridge + # Make mock_linkplay_factory_bridge_exception no longer throw an exception + mock_linkplay_factory_bridge.side_effect = None result = await hass.config_entries.flow.async_configure( result["flow_id"], diff --git a/tests/components/litterrobot/test_config_flow.py b/tests/components/litterrobot/test_config_flow.py index 5ffb78c77826f4..9420d3cb8a8dd4 100644 --- a/tests/components/litterrobot/test_config_flow.py +++ b/tests/components/litterrobot/test_config_flow.py @@ -7,7 +7,7 @@ from homeassistant import config_entries from homeassistant.components import litterrobot -from homeassistant.const import CONF_PASSWORD, CONF_SOURCE +from homeassistant.const import CONF_PASSWORD from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -124,15 +124,7 @@ async def test_step_reauth(hass: HomeAssistant, mock_account: Account) -> None: ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - CONF_SOURCE: config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - "unique_id": entry.unique_id, - }, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -164,15 +156,7 @@ async def test_step_reauth_failed(hass: HomeAssistant, mock_account: Account) -> ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - CONF_SOURCE: config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - "unique_id": entry.unique_id, - }, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/lyric/test_config_flow.py b/tests/components/lyric/test_config_flow.py index 1e0ae04f741694..e1916924e9f7de 100644 --- a/tests/components/lyric/test_config_flow.py +++ b/tests/components/lyric/test_config_flow.py @@ -126,9 +126,7 @@ async def test_reauthentication_flow( ) old_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_REAUTH}, data=old_entry.data - ) + result = await old_entry.start_reauth_flow(hass) flows = hass.config_entries.flow.async_progress() assert len(flows) == 1 diff --git a/tests/components/madvr/conftest.py b/tests/components/madvr/conftest.py index 187786c6964841..3136e04b06bc44 100644 --- a/tests/components/madvr/conftest.py +++ b/tests/components/madvr/conftest.py @@ -57,6 +57,7 @@ def mock_config_entry() -> MockConfigEntry: data=MOCK_CONFIG, unique_id=MOCK_MAC, title=DEFAULT_NAME, + entry_id="3bd2acb0e4f0476d40865546d0d91132", ) diff --git a/tests/components/madvr/snapshots/test_diagnostics.ambr b/tests/components/madvr/snapshots/test_diagnostics.ambr new file mode 100644 index 00000000000000..f8008a651f22dc --- /dev/null +++ b/tests/components/madvr/snapshots/test_diagnostics.ambr @@ -0,0 +1,26 @@ +# serializer version: 1 +# name: test_entry_diagnostics[positive_payload0] + dict({ + 'config_entry': dict({ + 'data': dict({ + 'host': '**REDACTED**', + 'port': 44077, + }), + 'disabled_by': None, + 'domain': 'madvr', + 'entry_id': '3bd2acb0e4f0476d40865546d0d91132', + 'minor_version': 1, + 'options': dict({ + }), + 'pref_disable_new_entities': False, + 'pref_disable_polling': False, + 'source': 'user', + 'title': 'envy', + 'unique_id': '00:11:22:33:44:55', + 'version': 1, + }), + 'madvr_data': dict({ + 'is_on': True, + }), + }) +# --- diff --git a/tests/components/madvr/test_diagnostics.py b/tests/components/madvr/test_diagnostics.py new file mode 100644 index 00000000000000..453eaba8d94b26 --- /dev/null +++ b/tests/components/madvr/test_diagnostics.py @@ -0,0 +1,48 @@ +"""Test madVR diagnostics.""" + +from unittest.mock import AsyncMock, patch + +import pytest +from syrupy import SnapshotAssertion +from syrupy.filters import props + +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant + +from . import setup_integration +from .conftest import get_update_callback + +from tests.common import MockConfigEntry +from tests.components.diagnostics import get_diagnostics_for_config_entry +from tests.typing import ClientSessionGenerator + + +@pytest.mark.parametrize( + ("positive_payload"), + [ + {"is_on": True}, + ], +) +async def test_entry_diagnostics( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + mock_config_entry: MockConfigEntry, + mock_madvr_client: AsyncMock, + snapshot: SnapshotAssertion, + positive_payload: dict, +) -> None: + """Test config entry diagnostics.""" + with patch("homeassistant.components.madvr.PLATFORMS", [Platform.SENSOR]): + await setup_integration(hass, mock_config_entry) + + update_callback = get_update_callback(mock_madvr_client) + + # Add data to test storing diagnostic data + update_callback(positive_payload) + await hass.async_block_till_done() + + result = await get_diagnostics_for_config_entry( + hass, hass_client, mock_config_entry + ) + + assert result == snapshot(exclude=props("created_at", "modified_at")) diff --git a/tests/components/mailbox/__init__.py b/tests/components/mailbox/__init__.py deleted file mode 100644 index 5e212354579481..00000000000000 --- a/tests/components/mailbox/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""The tests for mailbox platforms.""" diff --git a/tests/components/mailbox/test_init.py b/tests/components/mailbox/test_init.py deleted file mode 100644 index 6fcf9176aae19c..00000000000000 --- a/tests/components/mailbox/test_init.py +++ /dev/null @@ -1,225 +0,0 @@ -"""The tests for the mailbox component.""" - -from datetime import datetime -from hashlib import sha1 -from http import HTTPStatus -from typing import Any - -from aiohttp.test_utils import TestClient -import pytest - -from homeassistant.components import mailbox -from homeassistant.core import HomeAssistant -from homeassistant.helpers import issue_registry as ir -from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType -from homeassistant.setup import async_setup_component -from homeassistant.util import dt as dt_util - -from tests.common import MockModule, mock_integration, mock_platform -from tests.typing import ClientSessionGenerator - -MAILBOX_NAME = "TestMailbox" -MEDIA_DATA = b"3f67c4ea33b37d1710f" -MESSAGE_TEXT = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " - - -def _create_message(idx: int) -> dict[str, Any]: - """Create a sample message.""" - msgtime = dt_util.as_timestamp(datetime(2010, 12, idx + 1, 13, 17, 00)) - msgtxt = f"Message {idx + 1}. {MESSAGE_TEXT}" - msgsha = sha1(msgtxt.encode("utf-8")).hexdigest() - return { - "info": { - "origtime": int(msgtime), - "callerid": "John Doe <212-555-1212>", - "duration": "10", - }, - "text": msgtxt, - "sha": msgsha, - } - - -class TestMailbox(mailbox.Mailbox): - """Test Mailbox, with 10 sample messages.""" - - # This class doesn't contain any tests! Skip pytest test collection. - __test__ = False - - def __init__(self, hass: HomeAssistant, name: str) -> None: - """Initialize Test mailbox.""" - super().__init__(hass, name) - self._messages: dict[str, dict[str, Any]] = {} - for idx in range(10): - msg = _create_message(idx) - msgsha = msg["sha"] - self._messages[msgsha] = msg - - @property - def media_type(self) -> str: - """Return the supported media type.""" - return mailbox.CONTENT_TYPE_MPEG - - @property - def can_delete(self) -> bool: - """Return if messages can be deleted.""" - return True - - @property - def has_media(self) -> bool: - """Return if messages have attached media files.""" - return True - - async def async_get_media(self, msgid: str) -> bytes: - """Return the media blob for the msgid.""" - if msgid not in self._messages: - raise mailbox.StreamError("Message not found") - - return MEDIA_DATA - - async def async_get_messages(self) -> list[dict[str, Any]]: - """Return a list of the current messages.""" - return sorted( - self._messages.values(), - key=lambda item: item["info"]["origtime"], # type: ignore[no-any-return] - reverse=True, - ) - - async def async_delete(self, msgid: str) -> bool: - """Delete the specified messages.""" - if msgid in self._messages: - del self._messages[msgid] - self.async_update() - return True - - -class MockMailbox: - """A mock mailbox platform.""" - - async def async_get_handler( - self, - hass: HomeAssistant, - config: ConfigType, - discovery_info: DiscoveryInfoType | None = None, - ) -> mailbox.Mailbox: - """Set up the Test mailbox.""" - return TestMailbox(hass, MAILBOX_NAME) - - -@pytest.fixture -def mock_mailbox(hass: HomeAssistant) -> None: - """Mock mailbox.""" - mock_integration(hass, MockModule(domain="test")) - mock_platform(hass, "test.mailbox", MockMailbox()) - - -@pytest.fixture -async def mock_http_client( - hass: HomeAssistant, hass_client: ClientSessionGenerator, mock_mailbox: None -) -> TestClient: - """Start the Home Assistant HTTP component.""" - assert await async_setup_component( - hass, mailbox.DOMAIN, {mailbox.DOMAIN: {"platform": "test"}} - ) - return await hass_client() - - -async def test_get_platforms_from_mailbox(mock_http_client: TestClient) -> None: - """Get platforms from mailbox.""" - url = "/api/mailbox/platforms" - - req = await mock_http_client.get(url) - assert req.status == HTTPStatus.OK - result = await req.json() - assert len(result) == 1 - assert result[0].get("name") == "TestMailbox" - - -async def test_get_messages_from_mailbox(mock_http_client: TestClient) -> None: - """Get messages from mailbox.""" - url = "/api/mailbox/messages/TestMailbox" - - req = await mock_http_client.get(url) - assert req.status == HTTPStatus.OK - result = await req.json() - assert len(result) == 10 - - -async def test_get_media_from_mailbox(mock_http_client: TestClient) -> None: - """Get audio from mailbox.""" - mp3sha = "7cad61312c7b66f619295be2da8c7ac73b4968f1" - msgtxt = "Message 1. Lorem ipsum dolor sit amet, consectetur adipiscing elit. " - msgsha = sha1(msgtxt.encode("utf-8")).hexdigest() - - url = f"/api/mailbox/media/TestMailbox/{msgsha}" - req = await mock_http_client.get(url) - assert req.status == HTTPStatus.OK - data = await req.read() - assert sha1(data).hexdigest() == mp3sha - - -async def test_delete_from_mailbox(mock_http_client: TestClient) -> None: - """Get audio from mailbox.""" - msgtxt1 = "Message 1. Lorem ipsum dolor sit amet, consectetur adipiscing elit. " - msgtxt2 = "Message 3. Lorem ipsum dolor sit amet, consectetur adipiscing elit. " - msgsha1 = sha1(msgtxt1.encode("utf-8")).hexdigest() - msgsha2 = sha1(msgtxt2.encode("utf-8")).hexdigest() - - for msg in (msgsha1, msgsha2): - url = f"/api/mailbox/delete/TestMailbox/{msg}" - req = await mock_http_client.delete(url) - assert req.status == HTTPStatus.OK - - url = "/api/mailbox/messages/TestMailbox" - req = await mock_http_client.get(url) - assert req.status == HTTPStatus.OK - result = await req.json() - assert len(result) == 8 - - -async def test_get_messages_from_invalid_mailbox(mock_http_client: TestClient) -> None: - """Get messages from mailbox.""" - url = "/api/mailbox/messages/mailbox.invalid_mailbox" - - req = await mock_http_client.get(url) - assert req.status == HTTPStatus.NOT_FOUND - - -async def test_get_media_from_invalid_mailbox(mock_http_client: TestClient) -> None: - """Get messages from mailbox.""" - msgsha = "0000000000000000000000000000000000000000" - url = f"/api/mailbox/media/mailbox.invalid_mailbox/{msgsha}" - - req = await mock_http_client.get(url) - assert req.status == HTTPStatus.NOT_FOUND - - -async def test_get_media_from_invalid_msgid(mock_http_client: TestClient) -> None: - """Get messages from mailbox.""" - msgsha = "0000000000000000000000000000000000000000" - url = f"/api/mailbox/media/TestMailbox/{msgsha}" - - req = await mock_http_client.get(url) - assert req.status == HTTPStatus.INTERNAL_SERVER_ERROR - - -async def test_delete_from_invalid_mailbox(mock_http_client: TestClient) -> None: - """Get audio from mailbox.""" - msgsha = "0000000000000000000000000000000000000000" - url = f"/api/mailbox/delete/mailbox.invalid_mailbox/{msgsha}" - - req = await mock_http_client.delete(url) - assert req.status == HTTPStatus.NOT_FOUND - - -async def test_repair_issue_is_created( - hass: HomeAssistant, issue_registry: ir.IssueRegistry, mock_mailbox: None -) -> None: - """Test repair issue is created.""" - assert await async_setup_component( - hass, mailbox.DOMAIN, {mailbox.DOMAIN: {"platform": "test"}} - ) - await hass.async_block_till_done() - assert ( - mailbox.DOMAIN, - "deprecated_mailbox_test", - ) in issue_registry.issues diff --git a/tests/components/matter/conftest.py b/tests/components/matter/conftest.py index f3d8740a73bc21..b4af00a0b478ff 100644 --- a/tests/components/matter/conftest.py +++ b/tests/components/matter/conftest.py @@ -3,7 +3,7 @@ from __future__ import annotations import asyncio -from collections.abc import AsyncGenerator, Generator +from collections.abc import AsyncGenerator from unittest.mock import AsyncMock, MagicMock, patch from matter_server.client.models.node import MatterNode @@ -70,153 +70,6 @@ async def integration_fixture( return entry -@pytest.fixture(name="create_backup") -def create_backup_fixture() -> Generator[AsyncMock]: - """Mock Supervisor create backup of add-on.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_create_backup" - ) as create_backup: - yield create_backup - - -@pytest.fixture(name="addon_store_info") -def addon_store_info_fixture() -> Generator[AsyncMock]: - """Mock Supervisor add-on store info.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_get_addon_store_info" - ) as addon_store_info: - addon_store_info.return_value = { - "available": False, - "installed": None, - "state": None, - "version": "1.0.0", - } - yield addon_store_info - - -@pytest.fixture(name="addon_info") -def addon_info_fixture() -> Generator[AsyncMock]: - """Mock Supervisor add-on info.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_get_addon_info", - ) as addon_info: - addon_info.return_value = { - "available": False, - "hostname": None, - "options": {}, - "state": None, - "update_available": False, - "version": None, - } - yield addon_info - - -@pytest.fixture(name="addon_not_installed") -def addon_not_installed_fixture( - addon_store_info: AsyncMock, addon_info: AsyncMock -) -> AsyncMock: - """Mock add-on not installed.""" - addon_store_info.return_value["available"] = True - return addon_info - - -@pytest.fixture(name="addon_installed") -def addon_installed_fixture( - addon_store_info: AsyncMock, addon_info: AsyncMock -) -> AsyncMock: - """Mock add-on already installed but not running.""" - addon_store_info.return_value = { - "available": True, - "installed": "1.0.0", - "state": "stopped", - "version": "1.0.0", - } - addon_info.return_value["available"] = True - addon_info.return_value["hostname"] = "core-matter-server" - addon_info.return_value["state"] = "stopped" - addon_info.return_value["version"] = "1.0.0" - return addon_info - - -@pytest.fixture(name="addon_running") -def addon_running_fixture( - addon_store_info: AsyncMock, addon_info: AsyncMock -) -> AsyncMock: - """Mock add-on already running.""" - addon_store_info.return_value = { - "available": True, - "installed": "1.0.0", - "state": "started", - "version": "1.0.0", - } - addon_info.return_value["available"] = True - addon_info.return_value["hostname"] = "core-matter-server" - addon_info.return_value["state"] = "started" - addon_info.return_value["version"] = "1.0.0" - return addon_info - - -@pytest.fixture(name="install_addon") -def install_addon_fixture( - addon_store_info: AsyncMock, addon_info: AsyncMock -) -> Generator[AsyncMock]: - """Mock install add-on.""" - - async def install_addon_side_effect(hass: HomeAssistant, slug: str) -> None: - """Mock install add-on.""" - addon_store_info.return_value = { - "available": True, - "installed": "1.0.0", - "state": "stopped", - "version": "1.0.0", - } - addon_info.return_value["available"] = True - addon_info.return_value["state"] = "stopped" - addon_info.return_value["version"] = "1.0.0" - - with patch( - "homeassistant.components.hassio.addon_manager.async_install_addon" - ) as install_addon: - install_addon.side_effect = install_addon_side_effect - yield install_addon - - -@pytest.fixture(name="start_addon") -def start_addon_fixture() -> Generator[AsyncMock]: - """Mock start add-on.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_start_addon" - ) as start_addon: - yield start_addon - - -@pytest.fixture(name="stop_addon") -def stop_addon_fixture() -> Generator[AsyncMock]: - """Mock stop add-on.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_stop_addon" - ) as stop_addon: - yield stop_addon - - -@pytest.fixture(name="uninstall_addon") -def uninstall_addon_fixture() -> Generator[AsyncMock]: - """Mock uninstall add-on.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_uninstall_addon" - ) as uninstall_addon: - yield uninstall_addon - - -@pytest.fixture(name="update_addon") -def update_addon_fixture() -> Generator[AsyncMock]: - """Mock update add-on.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_update_addon" - ) as update_addon: - yield update_addon - - @pytest.fixture(name="door_lock") async def door_lock_fixture( hass: HomeAssistant, matter_client: MagicMock diff --git a/tests/components/matter/test_config_flow.py b/tests/components/matter/test_config_flow.py index 642bfe0f804288..a4ddc18802f2d2 100644 --- a/tests/components/matter/test_config_flow.py +++ b/tests/components/matter/test_config_flow.py @@ -4,8 +4,7 @@ from collections.abc import Generator from ipaddress import ip_address -from typing import Any -from unittest.mock import DEFAULT, AsyncMock, MagicMock, call, patch +from unittest.mock import AsyncMock, MagicMock, call, patch from matter_server.client.exceptions import CannotConnect, InvalidServerVersion import pytest @@ -93,20 +92,9 @@ def supervisor_fixture() -> Generator[MagicMock]: yield is_hassio -@pytest.fixture(name="discovery_info") -def discovery_info_fixture() -> Any: - """Return the discovery info from the supervisor.""" - return DEFAULT - - -@pytest.fixture(name="get_addon_discovery_info", autouse=True) -def get_addon_discovery_info_fixture(discovery_info: Any) -> Generator[AsyncMock]: +@pytest.fixture(autouse=True) +def mock_get_addon_discovery_info(get_addon_discovery_info: AsyncMock) -> None: """Mock get add-on discovery info.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_get_addon_discovery_info", - return_value=discovery_info, - ) as get_addon_discovery_info: - yield get_addon_discovery_info @pytest.fixture(name="addon_setup_time", autouse=True) diff --git a/tests/components/mealie/snapshots/test_diagnostics.ambr b/tests/components/mealie/snapshots/test_diagnostics.ambr index e6c72c950cc4f3..a694c72fcf696e 100644 --- a/tests/components/mealie/snapshots/test_diagnostics.ambr +++ b/tests/components/mealie/snapshots/test_diagnostics.ambr @@ -10,6 +10,7 @@ 'description': None, 'entry_type': 'breakfast', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'mealplan_date': dict({ '__type': "", 'isoformat': '2024-01-23', @@ -18,6 +19,7 @@ 'recipe': dict({ 'description': 'The BEST Roast Chicken recipe is simple, budget friendly, and gives you a tender, mouth-watering chicken full of flavor! Served with roasted vegetables, this recipe is simple enough for any cook!', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'image': 'JeQ2', 'name': 'Roast Chicken', 'original_url': 'https://tastesbetterfromscratch.com/roast-chicken/', @@ -35,6 +37,7 @@ 'description': None, 'entry_type': 'dinner', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'mealplan_date': dict({ '__type': "", 'isoformat': '2024-01-22', @@ -43,6 +46,7 @@ 'recipe': dict({ 'description': "Een traybake is eigenlijk altijd een goed idee. Deze zoete aardappel curry traybake dus ook. Waarom? Omdat je alleen maar wat groenten - en in dit geval kip - op een bakplaat (traybake dus) legt, hier wat kruiden aan toevoegt en deze in de oven schuift. Ideaal dus als je geen zin hebt om lang in de keuken te staan. Maar gewoon lekker op de bank wil ploffen om te wachten tot de oven klaar is. Joe! That\\'s what we like. Deze zoete aardappel curry traybake bevat behalve zoete aardappel en curry ook kikkererwten, kippendijfilet en bloemkoolroosjes. Je gebruikt yoghurt en limoen als een soort dressing. En je serveert deze heerlijke traybake met naanbrood. Je kunt natuurljk ook voor deze traybake met chipolataworstjes gaan. Wil je graag meer ovengerechten? Dan moet je eigenlijk even kijken naar onze Ovenbijbel. Onmisbaar in je keuken! We willen je deze zoete aardappelstamppot met prei ook niet onthouden. Megalekker bordje comfortfood als je \\'t ons vraagt.", 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'image': 'AiIo', 'name': 'Zoete aardappel curry traybake', 'original_url': 'https://chickslovefood.com/recept/zoete-aardappel-curry-traybake/', @@ -58,6 +62,7 @@ 'description': None, 'entry_type': 'dinner', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'mealplan_date': dict({ '__type': "", 'isoformat': '2024-01-23', @@ -66,6 +71,7 @@ 'recipe': dict({ 'description': 'Εύκολη μακαρονάδα με κεφτεδάκια στον φούρνο από τον Άκη Πετρετζίκη. Φτιάξτε την πιο εύκολη μακαρονάδα με κεφτεδάκια σε μόνο ένα σκεύος.', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'image': 'En9o', 'name': 'Εύκολη μακαρονάδα με κεφτεδάκια στον φούρνο (1)', 'original_url': 'https://akispetretzikis.com/recipe/7959/efkolh-makaronada-me-keftedakia-ston-fourno', @@ -81,6 +87,7 @@ 'description': None, 'entry_type': 'dinner', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'mealplan_date': dict({ '__type': "", 'isoformat': '2024-01-23', @@ -89,6 +96,7 @@ 'recipe': dict({ 'description': 'Delicious Greek turkey meatballs with lemon orzo, tender veggies, and a creamy feta yogurt sauce. These healthy baked Greek turkey meatballs are filled with tons of wonderful herbs and make the perfect protein-packed weeknight meal!', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'image': 'Kn62', 'name': 'Greek Turkey Meatballs with Lemon Orzo & Creamy Feta Yogurt Sauce', 'original_url': 'https://www.ambitiouskitchen.com/greek-turkey-meatballs/', @@ -104,6 +112,7 @@ 'description': None, 'entry_type': 'dinner', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'mealplan_date': dict({ '__type': "", 'isoformat': '2024-01-23', @@ -112,6 +121,7 @@ 'recipe': dict({ 'description': 'This is a modified Pampered Chef recipe. You can use a trifle bowl or large glass punch/salad bowl to show it off. It is really easy to make and I never have any leftovers. Cook time includes chill time.', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'image': 'ibL6', 'name': 'Pampered Chef Double Chocolate Mocha Trifle', 'original_url': 'https://www.food.com/recipe/pampered-chef-double-chocolate-mocha-trifle-74963', @@ -127,6 +137,7 @@ 'description': None, 'entry_type': 'dinner', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'mealplan_date': dict({ '__type': "", 'isoformat': '2024-01-22', @@ -135,6 +146,7 @@ 'recipe': dict({ 'description': 'Cheeseburger Sliders are juicy, cheesy and beefy - everything we love about classic burgers! These sliders are quick and easy plus they are make-ahead and reheat really well.', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'image': 'beGq', 'name': 'Cheeseburger Sliders (Easy, 30-min Recipe)', 'original_url': 'https://natashaskitchen.com/cheeseburger-sliders/', @@ -150,6 +162,7 @@ 'description': None, 'entry_type': 'dinner', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'mealplan_date': dict({ '__type': "", 'isoformat': '2024-01-23', @@ -158,6 +171,7 @@ 'recipe': dict({ 'description': 'This All-American beef stew recipe includes tender beef coated in a rich, intense sauce and vegetables that bring complementary texture and flavor.', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'image': '356X', 'name': 'All-American Beef Stew Recipe', 'original_url': 'https://www.seriouseats.com/all-american-beef-stew-recipe', @@ -173,6 +187,7 @@ 'description': None, 'entry_type': 'dinner', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'mealplan_date': dict({ '__type': "", 'isoformat': '2024-01-22', @@ -181,6 +196,7 @@ 'recipe': dict({ 'description': 'Einfacher Nudelauflauf mit Brokkoli, Sahnesauce und extra Käse. Dieses vegetarische 5 Zutaten Rezept ist super schnell gemacht und SO gut!', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'image': 'nOPT', 'name': 'Einfacher Nudelauflauf mit Brokkoli', 'original_url': 'https://kochkarussell.com/einfacher-nudelauflauf-brokkoli/', @@ -196,6 +212,7 @@ 'description': None, 'entry_type': 'dinner', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'mealplan_date': dict({ '__type': "", 'isoformat': '2024-01-23', @@ -204,6 +221,7 @@ 'recipe': dict({ 'description': 'Simple to prepare and ready in 25 minutes, this vegetarian miso noodle recipe can be eaten on its own or served as a side.', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'image': '5G1v', 'name': 'Miso Udon Noodles with Spinach and Tofu', 'original_url': 'https://www.allrecipes.com/recipe/284039/miso-udon-noodles-with-spinach-and-tofu/', @@ -219,6 +237,7 @@ 'description': None, 'entry_type': 'dinner', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'mealplan_date': dict({ '__type': "", 'isoformat': '2024-01-22', @@ -227,6 +246,7 @@ 'recipe': dict({ 'description': 'Avis aux nostalgiques des années 1980, la mousse de saumon est de retour dans une présentation adaptée au goût du jour. On utilise une technique sans faille : un saumon frais cuit au micro-ondes et mélangé au robot avec du fromage à la crème et de la crème sure. On obtient ainsi une texture onctueuse à tartiner, qui n’a rien à envier aux préparations gélatineuses d’antan !', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'image': 'rrNL', 'name': 'Mousse de saumon', 'original_url': 'https://www.ricardocuisine.com/recettes/8919-mousse-de-saumon', @@ -242,6 +262,7 @@ 'description': 'Dineren met de boys', 'entry_type': 'dinner', 'group_id': '3931df86-0679-4579-8c63-4bedc9ca9a85', + 'household_id': None, 'mealplan_date': dict({ '__type': "", 'isoformat': '2024-01-21', @@ -257,6 +278,7 @@ 'description': None, 'entry_type': 'lunch', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'mealplan_date': dict({ '__type': "", 'isoformat': '2024-01-23', @@ -265,6 +287,7 @@ 'recipe': dict({ 'description': 'Te explicamos paso a paso, de manera sencilla, la elaboración de la receta de pollo al curry con leche de coco en 10 minutos. Ingredientes, tiempo de...', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'image': 'INQz', 'name': 'Receta de pollo al curry en 10 minutos (con vídeo incluido)', 'original_url': 'https://www.directoalpaladar.com/recetas-de-carnes-y-aves/receta-de-pollo-al-curry-en-10-minutos', @@ -280,6 +303,7 @@ 'description': None, 'entry_type': 'lunch', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'mealplan_date': dict({ '__type': "", 'isoformat': '2024-01-23', @@ -288,6 +312,7 @@ 'recipe': dict({ 'description': 'bourguignon, oignon, carotte, bouquet garni, vin rouge, beurre, sel, poivre', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'image': 'nj5M', 'name': 'Boeuf bourguignon : la vraie recette (2)', 'original_url': 'https://www.marmiton.org/recettes/recette_boeuf-bourguignon_18889.aspx', @@ -303,6 +328,7 @@ 'description': None, 'entry_type': 'lunch', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'mealplan_date': dict({ '__type': "", 'isoformat': '2024-01-22', @@ -311,6 +337,7 @@ 'recipe': dict({ 'description': 'This All-American beef stew recipe includes tender beef coated in a rich, intense sauce and vegetables that bring complementary texture and flavor.', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'image': '356X', 'name': 'All-American Beef Stew Recipe', 'original_url': 'https://www.seriouseats.com/all-american-beef-stew-recipe', @@ -328,6 +355,7 @@ 'description': None, 'entry_type': 'side', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'mealplan_date': dict({ '__type': "", 'isoformat': '2024-01-23', @@ -336,6 +364,7 @@ 'recipe': dict({ 'description': 'Einfacher Nudelauflauf mit Brokkoli, Sahnesauce und extra Käse. Dieses vegetarische 5 Zutaten Rezept ist super schnell gemacht und SO gut!', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'image': 'nOPT', 'name': 'Einfacher Nudelauflauf mit Brokkoli', 'original_url': 'https://kochkarussell.com/einfacher-nudelauflauf-brokkoli/', diff --git a/tests/components/mealie/snapshots/test_services.ambr b/tests/components/mealie/snapshots/test_services.ambr index 3ae158f1d2d244..4f9ee6a5c09138 100644 --- a/tests/components/mealie/snapshots/test_services.ambr +++ b/tests/components/mealie/snapshots/test_services.ambr @@ -5,6 +5,7 @@ 'date_added': datetime.date(2024, 6, 29), 'description': 'The world’s most famous cake, the Original Sacher-Torte, is the consequence of several lucky twists of fate. The first was in 1832, when the Austrian State Chancellor, Prince Klemens Wenzel von Metternich, tasked his kitchen staff with concocting an extraordinary dessert to impress his special guests. As fortune had it, the chef had fallen ill that evening, leaving the apprentice chef, the then-16-year-old Franz Sacher, to perform this culinary magic trick. Metternich’s parting words to the talented teenager: “I hope you won’t disgrace me tonight.”', 'group_id': '24477569-f6af-4b53-9e3f-6d04b0ca6916', + 'household_id': None, 'image': 'SuPW', 'ingredients': list([ dict({ @@ -196,11 +197,13 @@ 'description': None, 'entry_type': , 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'mealplan_date': FakeDate(2024, 1, 22), 'mealplan_id': 230, 'recipe': dict({ 'description': "Een traybake is eigenlijk altijd een goed idee. Deze zoete aardappel curry traybake dus ook. Waarom? Omdat je alleen maar wat groenten - en in dit geval kip - op een bakplaat (traybake dus) legt, hier wat kruiden aan toevoegt en deze in de oven schuift. Ideaal dus als je geen zin hebt om lang in de keuken te staan. Maar gewoon lekker op de bank wil ploffen om te wachten tot de oven klaar is. Joe! That\\'s what we like. Deze zoete aardappel curry traybake bevat behalve zoete aardappel en curry ook kikkererwten, kippendijfilet en bloemkoolroosjes. Je gebruikt yoghurt en limoen als een soort dressing. En je serveert deze heerlijke traybake met naanbrood. Je kunt natuurljk ook voor deze traybake met chipolataworstjes gaan. Wil je graag meer ovengerechten? Dan moet je eigenlijk even kijken naar onze Ovenbijbel. Onmisbaar in je keuken! We willen je deze zoete aardappelstamppot met prei ook niet onthouden. Megalekker bordje comfortfood als je \\'t ons vraagt.", 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'image': 'AiIo', 'name': 'Zoete aardappel curry traybake', 'original_url': 'https://chickslovefood.com/recept/zoete-aardappel-curry-traybake/', @@ -216,11 +219,13 @@ 'description': None, 'entry_type': , 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'mealplan_date': FakeDate(2024, 1, 23), 'mealplan_id': 229, 'recipe': dict({ 'description': 'The BEST Roast Chicken recipe is simple, budget friendly, and gives you a tender, mouth-watering chicken full of flavor! Served with roasted vegetables, this recipe is simple enough for any cook!', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'image': 'JeQ2', 'name': 'Roast Chicken', 'original_url': 'https://tastesbetterfromscratch.com/roast-chicken/', @@ -236,11 +241,13 @@ 'description': None, 'entry_type': , 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'mealplan_date': FakeDate(2024, 1, 23), 'mealplan_id': 226, 'recipe': dict({ 'description': 'Te explicamos paso a paso, de manera sencilla, la elaboración de la receta de pollo al curry con leche de coco en 10 minutos. Ingredientes, tiempo de...', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'image': 'INQz', 'name': 'Receta de pollo al curry en 10 minutos (con vídeo incluido)', 'original_url': 'https://www.directoalpaladar.com/recetas-de-carnes-y-aves/receta-de-pollo-al-curry-en-10-minutos', @@ -256,11 +263,13 @@ 'description': None, 'entry_type': , 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'mealplan_date': FakeDate(2024, 1, 23), 'mealplan_id': 224, 'recipe': dict({ 'description': 'bourguignon, oignon, carotte, bouquet garni, vin rouge, beurre, sel, poivre', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'image': 'nj5M', 'name': 'Boeuf bourguignon : la vraie recette (2)', 'original_url': 'https://www.marmiton.org/recettes/recette_boeuf-bourguignon_18889.aspx', @@ -276,11 +285,13 @@ 'description': None, 'entry_type': , 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'mealplan_date': FakeDate(2024, 1, 23), 'mealplan_id': 222, 'recipe': dict({ 'description': 'Εύκολη μακαρονάδα με κεφτεδάκια στον φούρνο από τον Άκη Πετρετζίκη. Φτιάξτε την πιο εύκολη μακαρονάδα με κεφτεδάκια σε μόνο ένα σκεύος.', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'image': 'En9o', 'name': 'Εύκολη μακαρονάδα με κεφτεδάκια στον φούρνο (1)', 'original_url': 'https://akispetretzikis.com/recipe/7959/efkolh-makaronada-me-keftedakia-ston-fourno', @@ -296,11 +307,13 @@ 'description': None, 'entry_type': , 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'mealplan_date': FakeDate(2024, 1, 23), 'mealplan_id': 221, 'recipe': dict({ 'description': 'Delicious Greek turkey meatballs with lemon orzo, tender veggies, and a creamy feta yogurt sauce. These healthy baked Greek turkey meatballs are filled with tons of wonderful herbs and make the perfect protein-packed weeknight meal!', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'image': 'Kn62', 'name': 'Greek Turkey Meatballs with Lemon Orzo & Creamy Feta Yogurt Sauce', 'original_url': 'https://www.ambitiouskitchen.com/greek-turkey-meatballs/', @@ -316,11 +329,13 @@ 'description': None, 'entry_type': , 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'mealplan_date': FakeDate(2024, 1, 23), 'mealplan_id': 220, 'recipe': dict({ 'description': 'Einfacher Nudelauflauf mit Brokkoli, Sahnesauce und extra Käse. Dieses vegetarische 5 Zutaten Rezept ist super schnell gemacht und SO gut!', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'image': 'nOPT', 'name': 'Einfacher Nudelauflauf mit Brokkoli', 'original_url': 'https://kochkarussell.com/einfacher-nudelauflauf-brokkoli/', @@ -336,11 +351,13 @@ 'description': None, 'entry_type': , 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'mealplan_date': FakeDate(2024, 1, 23), 'mealplan_id': 219, 'recipe': dict({ 'description': 'This is a modified Pampered Chef recipe. You can use a trifle bowl or large glass punch/salad bowl to show it off. It is really easy to make and I never have any leftovers. Cook time includes chill time.', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'image': 'ibL6', 'name': 'Pampered Chef Double Chocolate Mocha Trifle', 'original_url': 'https://www.food.com/recipe/pampered-chef-double-chocolate-mocha-trifle-74963', @@ -356,11 +373,13 @@ 'description': None, 'entry_type': , 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'mealplan_date': FakeDate(2024, 1, 22), 'mealplan_id': 217, 'recipe': dict({ 'description': 'Cheeseburger Sliders are juicy, cheesy and beefy - everything we love about classic burgers! These sliders are quick and easy plus they are make-ahead and reheat really well.', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'image': 'beGq', 'name': 'Cheeseburger Sliders (Easy, 30-min Recipe)', 'original_url': 'https://natashaskitchen.com/cheeseburger-sliders/', @@ -376,11 +395,13 @@ 'description': None, 'entry_type': , 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'mealplan_date': FakeDate(2024, 1, 22), 'mealplan_id': 216, 'recipe': dict({ 'description': 'This All-American beef stew recipe includes tender beef coated in a rich, intense sauce and vegetables that bring complementary texture and flavor.', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'image': '356X', 'name': 'All-American Beef Stew Recipe', 'original_url': 'https://www.seriouseats.com/all-american-beef-stew-recipe', @@ -396,11 +417,13 @@ 'description': None, 'entry_type': , 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'mealplan_date': FakeDate(2024, 1, 23), 'mealplan_id': 212, 'recipe': dict({ 'description': 'This All-American beef stew recipe includes tender beef coated in a rich, intense sauce and vegetables that bring complementary texture and flavor.', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'image': '356X', 'name': 'All-American Beef Stew Recipe', 'original_url': 'https://www.seriouseats.com/all-american-beef-stew-recipe', @@ -416,11 +439,13 @@ 'description': None, 'entry_type': , 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'mealplan_date': FakeDate(2024, 1, 22), 'mealplan_id': 211, 'recipe': dict({ 'description': 'Einfacher Nudelauflauf mit Brokkoli, Sahnesauce und extra Käse. Dieses vegetarische 5 Zutaten Rezept ist super schnell gemacht und SO gut!', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'image': 'nOPT', 'name': 'Einfacher Nudelauflauf mit Brokkoli', 'original_url': 'https://kochkarussell.com/einfacher-nudelauflauf-brokkoli/', @@ -436,11 +461,13 @@ 'description': None, 'entry_type': , 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'mealplan_date': FakeDate(2024, 1, 23), 'mealplan_id': 196, 'recipe': dict({ 'description': 'Simple to prepare and ready in 25 minutes, this vegetarian miso noodle recipe can be eaten on its own or served as a side.', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'image': '5G1v', 'name': 'Miso Udon Noodles with Spinach and Tofu', 'original_url': 'https://www.allrecipes.com/recipe/284039/miso-udon-noodles-with-spinach-and-tofu/', @@ -456,11 +483,13 @@ 'description': None, 'entry_type': , 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'mealplan_date': FakeDate(2024, 1, 22), 'mealplan_id': 195, 'recipe': dict({ 'description': 'Avis aux nostalgiques des années 1980, la mousse de saumon est de retour dans une présentation adaptée au goût du jour. On utilise une technique sans faille : un saumon frais cuit au micro-ondes et mélangé au robot avec du fromage à la crème et de la crème sure. On obtient ainsi une texture onctueuse à tartiner, qui n’a rien à envier aux préparations gélatineuses d’antan !', 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'image': 'rrNL', 'name': 'Mousse de saumon', 'original_url': 'https://www.ricardocuisine.com/recettes/8919-mousse-de-saumon', @@ -476,6 +505,7 @@ 'description': 'Dineren met de boys', 'entry_type': , 'group_id': '3931df86-0679-4579-8c63-4bedc9ca9a85', + 'household_id': None, 'mealplan_date': FakeDate(2024, 1, 21), 'mealplan_id': 1, 'recipe': None, @@ -491,6 +521,7 @@ 'date_added': datetime.date(2024, 6, 29), 'description': 'The world’s most famous cake, the Original Sacher-Torte, is the consequence of several lucky twists of fate. The first was in 1832, when the Austrian State Chancellor, Prince Klemens Wenzel von Metternich, tasked his kitchen staff with concocting an extraordinary dessert to impress his special guests. As fortune had it, the chef had fallen ill that evening, leaving the apprentice chef, the then-16-year-old Franz Sacher, to perform this culinary magic trick. Metternich’s parting words to the talented teenager: “I hope you won’t disgrace me tonight.”', 'group_id': '24477569-f6af-4b53-9e3f-6d04b0ca6916', + 'household_id': None, 'image': 'SuPW', 'ingredients': list([ dict({ @@ -681,11 +712,13 @@ 'description': None, 'entry_type': , 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'mealplan_date': datetime.date(2024, 1, 22), 'mealplan_id': 230, 'recipe': dict({ 'description': "Een traybake is eigenlijk altijd een goed idee. Deze zoete aardappel curry traybake dus ook. Waarom? Omdat je alleen maar wat groenten - en in dit geval kip - op een bakplaat (traybake dus) legt, hier wat kruiden aan toevoegt en deze in de oven schuift. Ideaal dus als je geen zin hebt om lang in de keuken te staan. Maar gewoon lekker op de bank wil ploffen om te wachten tot de oven klaar is. Joe! That\\'s what we like. Deze zoete aardappel curry traybake bevat behalve zoete aardappel en curry ook kikkererwten, kippendijfilet en bloemkoolroosjes. Je gebruikt yoghurt en limoen als een soort dressing. En je serveert deze heerlijke traybake met naanbrood. Je kunt natuurljk ook voor deze traybake met chipolataworstjes gaan. Wil je graag meer ovengerechten? Dan moet je eigenlijk even kijken naar onze Ovenbijbel. Onmisbaar in je keuken! We willen je deze zoete aardappelstamppot met prei ook niet onthouden. Megalekker bordje comfortfood als je \\'t ons vraagt.", 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'image': 'AiIo', 'name': 'Zoete aardappel curry traybake', 'original_url': 'https://chickslovefood.com/recept/zoete-aardappel-curry-traybake/', @@ -705,11 +738,13 @@ 'description': None, 'entry_type': , 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'mealplan_date': datetime.date(2024, 1, 22), 'mealplan_id': 230, 'recipe': dict({ 'description': "Een traybake is eigenlijk altijd een goed idee. Deze zoete aardappel curry traybake dus ook. Waarom? Omdat je alleen maar wat groenten - en in dit geval kip - op een bakplaat (traybake dus) legt, hier wat kruiden aan toevoegt en deze in de oven schuift. Ideaal dus als je geen zin hebt om lang in de keuken te staan. Maar gewoon lekker op de bank wil ploffen om te wachten tot de oven klaar is. Joe! That\\'s what we like. Deze zoete aardappel curry traybake bevat behalve zoete aardappel en curry ook kikkererwten, kippendijfilet en bloemkoolroosjes. Je gebruikt yoghurt en limoen als een soort dressing. En je serveert deze heerlijke traybake met naanbrood. Je kunt natuurljk ook voor deze traybake met chipolataworstjes gaan. Wil je graag meer ovengerechten? Dan moet je eigenlijk even kijken naar onze Ovenbijbel. Onmisbaar in je keuken! We willen je deze zoete aardappelstamppot met prei ook niet onthouden. Megalekker bordje comfortfood als je \\'t ons vraagt.", 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'image': 'AiIo', 'name': 'Zoete aardappel curry traybake', 'original_url': 'https://chickslovefood.com/recept/zoete-aardappel-curry-traybake/', @@ -729,11 +764,13 @@ 'description': None, 'entry_type': , 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'mealplan_date': datetime.date(2024, 1, 22), 'mealplan_id': 230, 'recipe': dict({ 'description': "Een traybake is eigenlijk altijd een goed idee. Deze zoete aardappel curry traybake dus ook. Waarom? Omdat je alleen maar wat groenten - en in dit geval kip - op een bakplaat (traybake dus) legt, hier wat kruiden aan toevoegt en deze in de oven schuift. Ideaal dus als je geen zin hebt om lang in de keuken te staan. Maar gewoon lekker op de bank wil ploffen om te wachten tot de oven klaar is. Joe! That\\'s what we like. Deze zoete aardappel curry traybake bevat behalve zoete aardappel en curry ook kikkererwten, kippendijfilet en bloemkoolroosjes. Je gebruikt yoghurt en limoen als een soort dressing. En je serveert deze heerlijke traybake met naanbrood. Je kunt natuurljk ook voor deze traybake met chipolataworstjes gaan. Wil je graag meer ovengerechten? Dan moet je eigenlijk even kijken naar onze Ovenbijbel. Onmisbaar in je keuken! We willen je deze zoete aardappelstamppot met prei ook niet onthouden. Megalekker bordje comfortfood als je \\'t ons vraagt.", 'group_id': '0bf60b2e-ca89-42a9-94d4-8f67ca72b157', + 'household_id': None, 'image': 'AiIo', 'name': 'Zoete aardappel curry traybake', 'original_url': 'https://chickslovefood.com/recept/zoete-aardappel-curry-traybake/', diff --git a/tests/components/mealie/test_config_flow.py b/tests/components/mealie/test_config_flow.py index f28865787440f6..777d25fdef528c 100644 --- a/tests/components/mealie/test_config_flow.py +++ b/tests/components/mealie/test_config_flow.py @@ -6,7 +6,7 @@ import pytest from homeassistant.components.mealie.const import DOMAIN -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_RECONFIGURE, SOURCE_USER +from homeassistant.config_entries import SOURCE_RECONFIGURE, SOURCE_USER from homeassistant.const import CONF_API_TOKEN, CONF_HOST, CONF_VERIFY_SSL from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -152,11 +152,7 @@ async def test_reauth_flow( """Test reauth flow.""" await setup_integration(hass, mock_config_entry) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH, "entry_id": mock_config_entry.entry_id}, - data=mock_config_entry.data, - ) + result = await mock_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -179,11 +175,7 @@ async def test_reauth_flow_wrong_account( """Test reauth flow with wrong account.""" await setup_integration(hass, mock_config_entry) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH, "entry_id": mock_config_entry.entry_id}, - data=mock_config_entry.data, - ) + result = await mock_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -218,11 +210,7 @@ async def test_reauth_flow_exceptions( await setup_integration(hass, mock_config_entry) mock_mealie_client.get_user_info.side_effect = exception - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH, "entry_id": mock_config_entry.entry_id}, - data=mock_config_entry.data, - ) + result = await mock_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/meater/test_config_flow.py b/tests/components/meater/test_config_flow.py index b8c1be1526830e..9049cf4ac9ada6 100644 --- a/tests/components/meater/test_config_flow.py +++ b/tests/components/meater/test_config_flow.py @@ -123,11 +123,7 @@ async def test_reauth_flow(hass: HomeAssistant, mock_meater) -> None: ) mock_config.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_REAUTH}, - data=data, - ) + result = await mock_config.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/melcloud/test_config_flow.py b/tests/components/melcloud/test_config_flow.py index c1c6c10ac4c6d4..74b16aab6ed95f 100644 --- a/tests/components/melcloud/test_config_flow.py +++ b/tests/components/melcloud/test_config_flow.py @@ -9,7 +9,7 @@ from homeassistant import config_entries from homeassistant.components.melcloud.const import DOMAIN -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_RECONFIGURE +from homeassistant.config_entries import SOURCE_RECONFIGURE from homeassistant.const import CONF_PASSWORD from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -166,15 +166,7 @@ async def test_token_reauthentication( ) mock_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": mock_entry.unique_id, - "entry_id": mock_entry.entry_id, - }, - data=mock_entry.data, - ) + result = await mock_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -212,15 +204,7 @@ async def test_form_errors_reauthentication( ) mock_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": mock_entry.unique_id, - "entry_id": mock_entry.entry_id, - }, - data=mock_entry.data, - ) + result = await mock_entry.start_reauth_flow(hass) with patch( "homeassistant.components.melcloud.async_setup_entry", @@ -270,15 +254,7 @@ async def test_client_errors_reauthentication( ) mock_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": mock_entry.unique_id, - "entry_id": mock_entry.entry_id, - }, - data=mock_entry.data, - ) + result = await mock_entry.start_reauth_flow(hass) with patch( "homeassistant.components.melcloud.async_setup_entry", diff --git a/tests/components/met/conftest.py b/tests/components/met/conftest.py index 699c1c81795ae4..92b81d3d320435 100644 --- a/tests/components/met/conftest.py +++ b/tests/components/met/conftest.py @@ -17,8 +17,9 @@ def mock_weather(): "pressure": 100, "humidity": 50, "wind_speed": 10, - "wind_bearing": "NE", + "wind_bearing": 90, "dew_point": 12.1, + "uv_index": 1.1, } mock_data.get_forecast.return_value = {} yield mock_data diff --git a/tests/components/met/test_weather.py b/tests/components/met/test_weather.py index 80820ef0186b2a..ac3904684e319b 100644 --- a/tests/components/met/test_weather.py +++ b/tests/components/met/test_weather.py @@ -2,10 +2,22 @@ from homeassistant import config_entries from homeassistant.components.met import DOMAIN -from homeassistant.components.weather import DOMAIN as WEATHER_DOMAIN +from homeassistant.components.weather import ( + ATTR_CONDITION_CLOUDY, + ATTR_WEATHER_DEW_POINT, + ATTR_WEATHER_HUMIDITY, + ATTR_WEATHER_PRESSURE, + ATTR_WEATHER_TEMPERATURE, + ATTR_WEATHER_UV_INDEX, + ATTR_WEATHER_WIND_BEARING, + ATTR_WEATHER_WIND_SPEED, + DOMAIN as WEATHER_DOMAIN, +) from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er +from . import init_integration + async def test_new_config_entry( hass: HomeAssistant, entity_registry: er.EntityRegistry, mock_weather @@ -36,6 +48,25 @@ async def test_legacy_config_entry( assert len(er.async_entries_for_config_entry(entity_registry, entry.entry_id)) == 1 +async def test_weather(hass: HomeAssistant, mock_weather) -> None: + """Test states of the weather.""" + + await init_integration(hass) + assert len(hass.states.async_entity_ids("weather")) == 1 + entity_id = hass.states.async_entity_ids("weather")[0] + + state = hass.states.get(entity_id) + assert state + assert state.state == ATTR_CONDITION_CLOUDY + assert state.attributes[ATTR_WEATHER_TEMPERATURE] == 15 + assert state.attributes[ATTR_WEATHER_PRESSURE] == 100 + assert state.attributes[ATTR_WEATHER_HUMIDITY] == 50 + assert state.attributes[ATTR_WEATHER_WIND_SPEED] == 10 + assert state.attributes[ATTR_WEATHER_WIND_BEARING] == 90 + assert state.attributes[ATTR_WEATHER_DEW_POINT] == 12.1 + assert state.attributes[ATTR_WEATHER_UV_INDEX] == 1.1 + + async def test_tracking_home(hass: HomeAssistant, mock_weather) -> None: """Test we track home.""" await hass.config_entries.flow.async_init("met", context={"source": "onboarding"}) diff --git a/tests/components/microbees/test_config_flow.py b/tests/components/microbees/test_config_flow.py index d168dcd5017a42..f4e074d000defd 100644 --- a/tests/components/microbees/test_config_flow.py +++ b/tests/components/microbees/test_config_flow.py @@ -6,7 +6,7 @@ import pytest from homeassistant.components.microbees.const import DOMAIN -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_USER from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType from homeassistant.helpers import config_entry_oauth2_flow @@ -144,14 +144,7 @@ async def test_config_reauth_profile( """Test reauth an existing profile reauthenticates the config entry.""" await setup_integration(hass, config_entry) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "entry_id": config_entry.entry_id, - }, - data=config_entry.data, - ) + result = await config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -205,14 +198,7 @@ async def test_config_reauth_wrong_account( """Test reauth with wrong account.""" await setup_integration(hass, config_entry) microbees.return_value.getMyProfile.return_value.id = 12345 - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "entry_id": config_entry.entry_id, - }, - data=config_entry.data, - ) + result = await config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/mikrotik/test_config_flow.py b/tests/components/mikrotik/test_config_flow.py index f34fde0c9a5b74..d95a6488fc7b00 100644 --- a/tests/components/mikrotik/test_config_flow.py +++ b/tests/components/mikrotik/test_config_flow.py @@ -175,14 +175,7 @@ async def test_reauth_success(hass: HomeAssistant, api) -> None: ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - }, - data=DEMO_USER_INPUT, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -207,14 +200,7 @@ async def test_reauth_failed(hass: HomeAssistant, auth_error) -> None: ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - }, - data=DEMO_USER_INPUT, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -240,14 +226,7 @@ async def test_reauth_failed_conn_error(hass: HomeAssistant, conn_error) -> None ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - }, - data=DEMO_USER_INPUT, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/modern_forms/snapshots/test_diagnostics.ambr b/tests/components/modern_forms/snapshots/test_diagnostics.ambr new file mode 100644 index 00000000000000..56e299aa12a35d --- /dev/null +++ b/tests/components/modern_forms/snapshots/test_diagnostics.ambr @@ -0,0 +1,50 @@ +# serializer version: 1 +# name: test_entry_diagnostics + dict({ + 'config_entry': dict({ + 'data': dict({ + 'host': '192.168.1.123', + 'mac': '**REDACTED**', + }), + 'disabled_by': None, + 'domain': 'modern_forms', + 'minor_version': 1, + 'options': dict({ + }), + 'pref_disable_new_entities': False, + 'pref_disable_polling': False, + 'source': 'user', + 'title': 'Mock Title', + 'unique_id': None, + 'version': 1, + }), + 'device': dict({ + 'info': dict({ + 'client_id': 'MF_000000000000', + 'device_name': 'ModernFormsFan', + 'fan_motor_type': 'DC125X25', + 'fan_type': '1818-56', + 'federated_identity': 'us-east-1:f3da237b-c19c-4f61-b387-0e6dde2e470b', + 'firmware_url': '', + 'firmware_version': '01.03.0025', + 'light_type': 'F6IN-120V-R1-30', + 'mac_address': '**REDACTED**', + 'main_mcu_firmware_version': '01.03.3008', + 'owner': '**REDACTED**', + 'product_sku': '', + 'production_lot_number': '', + }), + 'status': dict({ + 'adaptive_learning_enabled': False, + 'away_mode_enabled': False, + 'fan_direction': 'forward', + 'fan_on': True, + 'fan_sleep_timer': 0, + 'fan_speed': 3, + 'light_brightness': 50, + 'light_on': True, + 'light_sleep_timer': 0, + }), + }), + }) +# --- diff --git a/tests/components/modern_forms/test_diagnostics.py b/tests/components/modern_forms/test_diagnostics.py new file mode 100644 index 00000000000000..9eb2e4efa9428a --- /dev/null +++ b/tests/components/modern_forms/test_diagnostics.py @@ -0,0 +1,26 @@ +"""Tests for the Modern Forms diagnostics platform.""" + +from syrupy import SnapshotAssertion +from syrupy.filters import props + +from homeassistant.core import HomeAssistant + +from . import init_integration + +from tests.components.diagnostics import get_diagnostics_for_config_entry +from tests.test_util.aiohttp import AiohttpClientMocker +from tests.typing import ClientSessionGenerator + + +async def test_entry_diagnostics( + hass: HomeAssistant, + aioclient_mock: AiohttpClientMocker, + hass_client: ClientSessionGenerator, + snapshot: SnapshotAssertion, +) -> None: + """Test the creation and values of the Modern Forms fans.""" + entry = await init_integration(hass, aioclient_mock) + + result = await get_diagnostics_for_config_entry(hass, hass_client, entry) + + assert result == snapshot(exclude=props("created_at", "modified_at", "entry_id")) diff --git a/tests/components/monzo/test_config_flow.py b/tests/components/monzo/test_config_flow.py index b7d0de9cdc395b..63daa2bfb434d3 100644 --- a/tests/components/monzo/test_config_flow.py +++ b/tests/components/monzo/test_config_flow.py @@ -154,14 +154,7 @@ async def test_config_reauth_profile( """Test reauth an existing profile reauthenticates the config entry.""" await setup_integration(hass, polling_config_entry) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "entry_id": polling_config_entry.entry_id, - }, - data=polling_config_entry.data, - ) + result = await polling_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -223,14 +216,7 @@ async def test_config_reauth_wrong_account( """Test reauth with wrong account.""" await setup_integration(hass, polling_config_entry) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "entry_id": polling_config_entry.entry_id, - }, - data=polling_config_entry.data, - ) + result = await polling_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/motionblinds_ble/conftest.py b/tests/components/motionblinds_ble/conftest.py index f89cf4f305dde9..ffd3bc5a2abe83 100644 --- a/tests/components/motionblinds_ble/conftest.py +++ b/tests/components/motionblinds_ble/conftest.py @@ -109,6 +109,7 @@ def mock_config_entry( return MockConfigEntry( title="mock_title", domain=DOMAIN, + entry_id="mock_entry_id", unique_id=address, data={ CONF_ADDRESS: address, diff --git a/tests/components/motionblinds_ble/snapshots/test_diagnostics.ambr b/tests/components/motionblinds_ble/snapshots/test_diagnostics.ambr new file mode 100644 index 00000000000000..ccb5b1ed87b3c9 --- /dev/null +++ b/tests/components/motionblinds_ble/snapshots/test_diagnostics.ambr @@ -0,0 +1,34 @@ +# serializer version: 1 +# name: test_diagnostics + dict({ + 'device': dict({ + 'blind_type': 'Roller blind', + 'calibration_type': None, + 'connection_type': 'disconnected', + 'end_position_info': None, + 'position': None, + 'tilt': None, + 'timezone': None, + }), + 'entry': dict({ + 'data': dict({ + 'address': 'cc:cc:cc:cc:cc:cc', + 'blind_type': 'roller', + 'local_name': 'Motionblind CCCC', + 'mac_code': 'CCCC', + }), + 'disabled_by': None, + 'domain': 'motionblinds_ble', + 'entry_id': 'mock_entry_id', + 'minor_version': 1, + 'options': dict({ + }), + 'pref_disable_new_entities': False, + 'pref_disable_polling': False, + 'source': 'user', + 'title': '**REDACTED**', + 'unique_id': '**REDACTED**', + 'version': 1, + }), + }) +# --- diff --git a/tests/components/motionblinds_ble/test_diagnostics.py b/tests/components/motionblinds_ble/test_diagnostics.py new file mode 100644 index 00000000000000..878d2caa326193 --- /dev/null +++ b/tests/components/motionblinds_ble/test_diagnostics.py @@ -0,0 +1,27 @@ +"""Test Motionblinds Bluetooth diagnostics.""" + +from syrupy import SnapshotAssertion +from syrupy.filters import props + +from homeassistant.core import HomeAssistant + +from . import setup_integration + +from tests.common import MockConfigEntry +from tests.components.diagnostics import get_diagnostics_for_config_entry +from tests.typing import ClientSessionGenerator + + +async def test_diagnostics( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + mock_config_entry: MockConfigEntry, + snapshot: SnapshotAssertion, +) -> None: + """Test diagnostics.""" + + await setup_integration(hass, mock_config_entry) + + assert await get_diagnostics_for_config_entry( + hass, hass_client, mock_config_entry + ) == snapshot(exclude=props("created_at", "modified_at", "repr")) diff --git a/tests/components/motionblinds_ble/test_entity.py b/tests/components/motionblinds_ble/test_entity.py index 1bfd3b185e560b..00369ba1e222cf 100644 --- a/tests/components/motionblinds_ble/test_entity.py +++ b/tests/components/motionblinds_ble/test_entity.py @@ -23,6 +23,7 @@ from tests.common import MockConfigEntry +@pytest.mark.usefixtures("motionblinds_ble_connect") @pytest.mark.parametrize( ("platform", "entity"), [ diff --git a/tests/components/motioneye/test_config_flow.py b/tests/components/motioneye/test_config_flow.py index 816fb31933a818..d2ec91b08e38f4 100644 --- a/tests/components/motioneye/test_config_flow.py +++ b/tests/components/motioneye/test_config_flow.py @@ -264,14 +264,7 @@ async def test_reauth(hass: HomeAssistant) -> None: config_entry = create_mock_motioneye_config_entry(hass, data=config_data) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": config_entry.entry_id, - }, - data=config_entry.data, - ) + result = await config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert not result["errors"] diff --git a/tests/components/mqtt/test_client.py b/tests/components/mqtt/test_client.py index dcded7d187a9aa..31c062b1abda06 100644 --- a/tests/components/mqtt/test_client.py +++ b/tests/components/mqtt/test_client.py @@ -37,11 +37,6 @@ from tests.typing import MqttMockHAClient, MqttMockHAClientGenerator, MqttMockPahoClient -@pytest.fixture(autouse=True) -def mock_storage(hass_storage: dict[str, Any]) -> None: - """Autouse hass_storage for the TestCase tests.""" - - def help_assert_message( msg: ReceiveMessage, topic: str | None = None, diff --git a/tests/components/mqtt/test_config_flow.py b/tests/components/mqtt/test_config_flow.py index 2b4cb20ccf94f2..d2f399899b17ca 100644 --- a/tests/components/mqtt/test_config_flow.py +++ b/tests/components/mqtt/test_config_flow.py @@ -14,6 +14,8 @@ from homeassistant import config_entries from homeassistant.components import mqtt from homeassistant.components.hassio import HassioServiceInfo +from homeassistant.components.hassio.addon_manager import AddonError +from homeassistant.components.hassio.handler import HassioAPIError from homeassistant.components.mqtt.config_flow import PWD_NOT_CHANGED from homeassistant.const import ( CONF_CLIENT_ID, @@ -28,6 +30,15 @@ from tests.common import MockConfigEntry from tests.typing import MqttMockHAClientGenerator, MqttMockPahoClient +ADD_ON_DISCOVERY_INFO = { + "addon": "Mosquitto Mqtt Broker", + "host": "core-mosquitto", + "port": 1883, + "username": "mock-user", + "password": "mock-pass", + "protocol": "3.1.1", + "ssl": False, +} MOCK_CLIENT_CERT = b"## mock client certificate file ##" MOCK_CLIENT_KEY = b"## mock key file ##" @@ -186,6 +197,29 @@ def _mock_process_uploaded_file( yield mock_upload +@pytest.fixture(name="supervisor") +def supervisor_fixture() -> Generator[MagicMock]: + """Mock Supervisor.""" + with patch( + "homeassistant.components.mqtt.config_flow.is_hassio", return_value=True + ) as is_hassio: + yield is_hassio + + +@pytest.fixture(name="addon_setup_time", autouse=True) +def addon_setup_time_fixture() -> Generator[int]: + """Mock add-on setup sleep time.""" + with patch( + "homeassistant.components.mqtt.config_flow.ADDON_SETUP_TIMEOUT", new=0 + ) as addon_setup_time: + yield addon_setup_time + + +@pytest.fixture(autouse=True) +def mock_get_addon_discovery_info(get_addon_discovery_info: AsyncMock) -> None: + """Mock get add-on discovery info.""" + + @pytest.mark.usefixtures("mqtt_client_mock") async def test_user_connection_works( hass: HomeAssistant, @@ -216,6 +250,47 @@ async def test_user_connection_works( assert len(mock_finish_setup.mock_calls) == 1 +@pytest.mark.usefixtures("mqtt_client_mock", "supervisor") +async def test_user_connection_works_with_supervisor( + hass: HomeAssistant, + mock_try_connection: MagicMock, + mock_finish_setup: MagicMock, +) -> None: + """Test we can finish a config flow with a supervised install.""" + mock_try_connection.return_value = True + + result = await hass.config_entries.flow.async_init( + "mqtt", context={"source": config_entries.SOURCE_USER} + ) + assert result["type"] is FlowResultType.MENU + assert result["menu_options"] == ["addon", "broker"] + assert result["step_id"] == "user" + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {"next_step_id": "broker"}, + ) + + # Assert a manual setup flow + assert result["type"] is FlowResultType.FORM + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], {"broker": "127.0.0.1"} + ) + + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["result"].data == { + "broker": "127.0.0.1", + "port": 1883, + "discovery": True, + } + # Check we tried the connection + assert len(mock_try_connection.mock_calls) == 1 + # Check config entry got setup + assert len(mock_finish_setup.mock_calls) == 1 + await hass.async_block_till_done(wait_background_tasks=True) + + @pytest.mark.usefixtures("mqtt_client_mock") async def test_user_v5_connection_works( hass: HomeAssistant, @@ -382,16 +457,8 @@ async def test_hassio_confirm( result = await hass.config_entries.flow.async_init( "mqtt", data=HassioServiceInfo( - config={ - "addon": "Mock Addon", - "host": "mock-broker", - "port": 1883, - "username": "mock-user", - "password": "mock-pass", - "protocol": "3.1.1", # Set by the addon's discovery, ignored by HA - "ssl": False, # Set by the addon's discovery, ignored by HA - }, - name="Mock Addon", + config=ADD_ON_DISCOVERY_INFO.copy(), + name="Mosquitto Mqtt Broker", slug="mosquitto", uuid="1234", ), @@ -399,7 +466,7 @@ async def test_hassio_confirm( ) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "hassio_confirm" - assert result["description_placeholders"] == {"addon": "Mock Addon"} + assert result["description_placeholders"] == {"addon": "Mosquitto Mqtt Broker"} mock_try_connection_success.reset_mock() result = await hass.config_entries.flow.async_configure( @@ -408,7 +475,7 @@ async def test_hassio_confirm( assert result["type"] is FlowResultType.CREATE_ENTRY assert result["result"].data == { - "broker": "mock-broker", + "broker": "core-mosquitto", "port": 1883, "username": "mock-user", "password": "mock-pass", @@ -426,14 +493,12 @@ async def test_hassio_cannot_connect( mock_finish_setup: MagicMock, ) -> None: """Test a config flow is aborted when a connection was not successful.""" - mock_try_connection.return_value = True - result = await hass.config_entries.flow.async_init( "mqtt", data=HassioServiceInfo( config={ "addon": "Mock Addon", - "host": "mock-broker", + "host": "core-mosquitto", "port": 1883, "username": "mock-user", "password": "mock-pass", @@ -463,6 +528,362 @@ async def test_hassio_cannot_connect( assert len(mock_finish_setup.mock_calls) == 0 +@pytest.mark.usefixtures( + "mqtt_client_mock", "supervisor", "addon_info", "addon_running" +) +@pytest.mark.parametrize("discovery_info", [{"config": ADD_ON_DISCOVERY_INFO.copy()}]) +async def test_addon_flow_with_supervisor_addon_running( + hass: HomeAssistant, + mock_try_connection_success: MagicMock, + mock_finish_setup: MagicMock, +) -> None: + """Test we perform an auto config flow with a supervised install. + + Case: The Mosquitto add-on is already installed, and running. + """ + # show menu + result = await hass.config_entries.flow.async_init( + "mqtt", context={"source": config_entries.SOURCE_USER} + ) + assert result["type"] is FlowResultType.MENU + assert result["menu_options"] == ["addon", "broker"] + assert result["step_id"] == "user" + + # select install via add-on + mock_try_connection_success.reset_mock() + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {"next_step_id": "addon"}, + ) + await hass.async_block_till_done(wait_background_tasks=True) + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["result"].data == { + "broker": "core-mosquitto", + "port": 1883, + "username": "mock-user", + "password": "mock-pass", + "discovery": True, + } + # Check we tried the connection + assert len(mock_try_connection_success.mock_calls) + # Check config entry got setup + assert len(mock_finish_setup.mock_calls) == 1 + + +@pytest.mark.usefixtures( + "mqtt_client_mock", "supervisor", "addon_info", "addon_installed", "start_addon" +) +@pytest.mark.parametrize("discovery_info", [{"config": ADD_ON_DISCOVERY_INFO.copy()}]) +async def test_addon_flow_with_supervisor_addon_installed( + hass: HomeAssistant, + mock_try_connection_success: MagicMock, + mock_finish_setup: MagicMock, +) -> None: + """Test we perform an auto config flow with a supervised install. + + Case: The Mosquitto add-on is installed, but not running. + """ + # show menu + result = await hass.config_entries.flow.async_init( + "mqtt", context={"source": config_entries.SOURCE_USER} + ) + assert result["type"] is FlowResultType.MENU + assert result["menu_options"] == ["addon", "broker"] + assert result["step_id"] == "user" + + # select install via add-on + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {"next_step_id": "addon"}, + ) + + # add-on installed but not started, so we wait for start-up + assert result["type"] is FlowResultType.SHOW_PROGRESS + assert result["progress_action"] == "start_addon" + assert result["step_id"] == "start_addon" + await hass.async_block_till_done() + await hass.async_block_till_done(wait_background_tasks=True) + mock_try_connection_success.reset_mock() + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {"next_step_id": "start_addon"}, + ) + + # add-on is running, so entry can be installed + await hass.async_block_till_done(wait_background_tasks=True) + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["result"].data == { + "broker": "core-mosquitto", + "port": 1883, + "username": "mock-user", + "password": "mock-pass", + "discovery": True, + } + # Check we tried the connection + assert len(mock_try_connection_success.mock_calls) + # Check config entry got setup + assert len(mock_finish_setup.mock_calls) == 1 + + +@pytest.mark.usefixtures( + "mqtt_client_mock", "supervisor", "addon_info", "addon_running" +) +@pytest.mark.parametrize("discovery_info", [{"config": ADD_ON_DISCOVERY_INFO.copy()}]) +async def test_addon_flow_with_supervisor_addon_running_connection_fails( + hass: HomeAssistant, + mock_try_connection: MagicMock, +) -> None: + """Test we perform an auto config flow with a supervised install. + + Case: The Mosquitto add-on is already installed, and running. + """ + # show menu + result = await hass.config_entries.flow.async_init( + "mqtt", context={"source": config_entries.SOURCE_USER} + ) + assert result["type"] is FlowResultType.MENU + assert result["menu_options"] == ["addon", "broker"] + assert result["step_id"] == "user" + + # select install via add-on but the connection fails and the flow will be aborted. + mock_try_connection.return_value = False + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {"next_step_id": "addon"}, + ) + await hass.async_block_till_done(wait_background_tasks=True) + assert result["type"] is FlowResultType.ABORT + + +@pytest.mark.usefixtures( + "mqtt_client_mock", + "supervisor", + "addon_info", + "addon_installed", +) +async def test_addon_not_running_api_error( + hass: HomeAssistant, + start_addon: AsyncMock, +) -> None: + """Test we perform an auto config flow with a supervised install. + + Case: The Mosquitto add-on start fails on a API error. + """ + start_addon.side_effect = HassioAPIError() + + result = await hass.config_entries.flow.async_init( + "mqtt", context={"source": config_entries.SOURCE_USER} + ) + assert result["type"] is FlowResultType.MENU + assert result["menu_options"] == ["addon", "broker"] + assert result["step_id"] == "user" + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {"next_step_id": "addon"}, + ) + # add-on not installed, so we wait for install + assert result["type"] is FlowResultType.SHOW_PROGRESS + assert result["progress_action"] == "start_addon" + assert result["step_id"] == "start_addon" + await hass.async_block_till_done() + await hass.async_block_till_done(wait_background_tasks=True) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {"next_step_id": "install_addon"}, + ) + + # add-on start-up failed + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "addon_start_failed" + + +@pytest.mark.usefixtures( + "mqtt_client_mock", + "supervisor", + "start_addon", + "addon_installed", +) +async def test_addon_discovery_info_error( + hass: HomeAssistant, + addon_info: AsyncMock, + get_addon_discovery_info: AsyncMock, +) -> None: + """Test we perform an auto config flow with a supervised install. + + Case: The Mosquitto add-on start on a discovery error. + """ + get_addon_discovery_info.side_effect = AddonError + + result = await hass.config_entries.flow.async_init( + "mqtt", context={"source": config_entries.SOURCE_USER} + ) + assert result["type"] is FlowResultType.MENU + assert result["menu_options"] == ["addon", "broker"] + assert result["step_id"] == "user" + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {"next_step_id": "addon"}, + ) + # Addon will retry + assert result["type"] is FlowResultType.SHOW_PROGRESS + assert result["progress_action"] == "start_addon" + assert result["step_id"] == "start_addon" + await hass.async_block_till_done() + await hass.async_block_till_done(wait_background_tasks=True) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {"next_step_id": "start_addon"}, + ) + + # add-on start-up failed + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "addon_start_failed" + + +@pytest.mark.usefixtures( + "mqtt_client_mock", + "supervisor", + "start_addon", + "addon_installed", +) +async def test_addon_info_error( + hass: HomeAssistant, + addon_info: AsyncMock, +) -> None: + """Test we perform an auto config flow with a supervised install. + + Case: The Mosquitto add-on info could not be retrieved. + """ + addon_info.side_effect = AddonError() + + result = await hass.config_entries.flow.async_init( + "mqtt", context={"source": config_entries.SOURCE_USER} + ) + assert result["type"] is FlowResultType.MENU + assert result["menu_options"] == ["addon", "broker"] + assert result["step_id"] == "user" + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {"next_step_id": "addon"}, + ) + + # add-on info failed + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "addon_info_failed" + + +@pytest.mark.usefixtures( + "mqtt_client_mock", + "supervisor", + "addon_info", + "addon_not_installed", + "install_addon", + "start_addon", +) +@pytest.mark.parametrize("discovery_info", [{"config": ADD_ON_DISCOVERY_INFO.copy()}]) +async def test_addon_flow_with_supervisor_addon_not_installed( + hass: HomeAssistant, + mock_try_connection_success: MagicMock, + mock_finish_setup: MagicMock, +) -> None: + """Test we perform an auto config flow with a supervised install. + + Case: The Mosquitto add-on is not yet installed nor running. + """ + result = await hass.config_entries.flow.async_init( + "mqtt", context={"source": config_entries.SOURCE_USER} + ) + assert result["type"] is FlowResultType.MENU + assert result["menu_options"] == ["addon", "broker"] + assert result["step_id"] == "user" + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {"next_step_id": "addon"}, + ) + # add-on not installed, so we wait for install + assert result["type"] is FlowResultType.SHOW_PROGRESS + assert result["progress_action"] == "install_addon" + assert result["step_id"] == "install_addon" + await hass.async_block_till_done() + await hass.async_block_till_done(wait_background_tasks=True) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {"next_step_id": "install_addon"}, + ) + + # add-on installed but not started, so we wait for start-up + assert result["type"] is FlowResultType.SHOW_PROGRESS + assert result["progress_action"] == "start_addon" + assert result["step_id"] == "start_addon" + await hass.async_block_till_done() + await hass.async_block_till_done(wait_background_tasks=True) + mock_try_connection_success.reset_mock() + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {"next_step_id": "start_addon"}, + ) + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["result"].data == { + "broker": "core-mosquitto", + "port": 1883, + "username": "mock-user", + "password": "mock-pass", + "discovery": True, + } + # Check we tried the connection + assert len(mock_try_connection_success.mock_calls) + # Check config entry got setup + assert len(mock_finish_setup.mock_calls) == 1 + + +@pytest.mark.usefixtures( + "mqtt_client_mock", + "supervisor", + "addon_info", + "addon_not_installed", + "start_addon", +) +async def test_addon_not_installed_failures( + hass: HomeAssistant, + install_addon: AsyncMock, +) -> None: + """Test we perform an auto config flow with a supervised install. + + Case: The Mosquitto add-on install fails. + """ + install_addon.side_effect = HassioAPIError() + + result = await hass.config_entries.flow.async_init( + "mqtt", context={"source": config_entries.SOURCE_USER} + ) + assert result["type"] is FlowResultType.MENU + assert result["menu_options"] == ["addon", "broker"] + assert result["step_id"] == "user" + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {"next_step_id": "addon"}, + ) + # add-on not installed, so we wait for install + assert result["type"] is FlowResultType.SHOW_PROGRESS + assert result["progress_action"] == "install_addon" + assert result["step_id"] == "install_addon" + await hass.async_block_till_done() + await hass.async_block_till_done(wait_background_tasks=True) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {"next_step_id": "install_addon"}, + ) + + # add-on install failed + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "addon_install_failed" + + async def test_option_flow( hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator, @@ -1130,14 +1551,7 @@ async def test_step_reauth( assert result["context"]["source"] == "reauth" # Show the form - result = await hass.config_entries.flow.async_init( - mqtt.DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": config_entry.entry_id, - }, - data=config_entry.data, - ) + result = await config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -1166,6 +1580,108 @@ async def test_step_reauth( await hass.async_block_till_done() +@pytest.mark.parametrize("discovery_info", [{"config": ADD_ON_DISCOVERY_INFO.copy()}]) +@pytest.mark.usefixtures( + "mqtt_client_mock", "mock_reload_after_entry_update", "supervisor", "addon_running" +) +async def test_step_hassio_reauth( + hass: HomeAssistant, mock_try_connection: MagicMock, addon_info: AsyncMock +) -> None: + """Test that the reauth step works in case the Mosquitto broker add-on was re-installed.""" + + # Set up entry data based on the discovery data, but with a stale password + entry_data = { + mqtt.CONF_BROKER: "core-mosquitto", + CONF_PORT: 1883, + CONF_USERNAME: "mock-user", + CONF_PASSWORD: "stale-secret", + } + + addon_info["hostname"] = "core-mosquitto" + + # Prepare the config entry + config_entry = MockConfigEntry(domain=mqtt.DOMAIN, data=entry_data) + config_entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(config_entry.entry_id) + + assert config_entry.data.get(CONF_PASSWORD) == "stale-secret" + + # Start reauth flow + mock_try_connection.reset_mock() + mock_try_connection.return_value = True + config_entry.async_start_reauth(hass) + await hass.async_block_till_done() + flows = hass.config_entries.flow.async_progress() + assert len(flows) == 0 + + # Assert the entry is updated automatically + assert config_entry.data.get(CONF_PASSWORD) == "mock-pass" + mock_try_connection.assert_called_once_with( + { + "broker": "core-mosquitto", + "port": 1883, + "username": "mock-user", + "password": "mock-pass", + } + ) + + +@pytest.mark.parametrize( + ("discovery_info", "discovery_info_side_effect", "broker"), + [ + ({"config": ADD_ON_DISCOVERY_INFO.copy()}, AddonError, "core-mosquitto"), + ({"config": ADD_ON_DISCOVERY_INFO.copy()}, None, "broker-not-addon"), + ], +) +@pytest.mark.usefixtures( + "mqtt_client_mock", "mock_reload_after_entry_update", "supervisor", "addon_running" +) +async def test_step_hassio_reauth_no_discovery_info( + hass: HomeAssistant, + mock_try_connection: MagicMock, + addon_info: AsyncMock, + broker: str, +) -> None: + """Test hassio reauth flow defaults to manual flow. + + Test that the reauth step defaults to + normal reauth flow if fetching add-on discovery info failed, + or the broker is not the add-on. + """ + + # Set up entry data based on the discovery data, but with a stale password + entry_data = { + mqtt.CONF_BROKER: broker, + CONF_PORT: 1883, + CONF_USERNAME: "mock-user", + CONF_PASSWORD: "wrong-pass", + } + + addon_info["hostname"] = "core-mosquitto" + + # Prepare the config entry + config_entry = MockConfigEntry(domain=mqtt.DOMAIN, data=entry_data) + config_entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(config_entry.entry_id) + + assert config_entry.data.get(CONF_PASSWORD) == "wrong-pass" + + # Start reauth flow + mock_try_connection.reset_mock() + mock_try_connection.return_value = True + config_entry.async_start_reauth(hass) + await hass.async_block_till_done() + flows = hass.config_entries.flow.async_progress() + assert len(flows) == 1 + result = flows[0] + assert result["step_id"] == "reauth_confirm" + assert result["context"]["source"] == "reauth" + + # Assert the entry is not updated + assert config_entry.data.get(CONF_PASSWORD) == "wrong-pass" + mock_try_connection.assert_not_called() + + async def test_options_user_connection_fails( hass: HomeAssistant, mock_try_connection_time_out: MagicMock ) -> None: diff --git a/tests/components/mqtt/test_device_trigger.py b/tests/components/mqtt/test_device_trigger.py index 10322dd90468a8..1acfe8dd9f5a96 100644 --- a/tests/components/mqtt/test_device_trigger.py +++ b/tests/components/mqtt/test_device_trigger.py @@ -18,7 +18,7 @@ from .test_common import help_test_unload_config_entry from tests.common import async_fire_mqtt_message, async_get_device_automations -from tests.typing import MqttMockHAClient, MqttMockHAClientGenerator, WebSocketGenerator +from tests.typing import MqttMockHAClientGenerator, WebSocketGenerator @pytest.fixture(autouse=True, name="stub_blueprint_populate") @@ -1672,11 +1672,11 @@ async def test_trigger_debug_info( assert debug_info_data["triggers"][0]["discovery_data"]["payload"] == config2 +@pytest.mark.usefixtures("mqtt_mock") async def test_unload_entry( hass: HomeAssistant, service_calls: list[ServiceCall], device_registry: dr.DeviceRegistry, - mqtt_mock: MqttMockHAClient, ) -> None: """Test unloading the MQTT entry.""" @@ -1738,3 +1738,4 @@ async def test_unload_entry( async_fire_mqtt_message(hass, "foobar/triggers/button1", "short_press") await hass.async_block_till_done() assert len(service_calls) == 2 + await hass.async_block_till_done(wait_background_tasks=True) diff --git a/tests/components/mqtt/test_init.py b/tests/components/mqtt/test_init.py index 333960d8ad4f49..8f7f7ed6289eaf 100644 --- a/tests/components/mqtt/test_init.py +++ b/tests/components/mqtt/test_init.py @@ -77,11 +77,6 @@ class _DebugInfo(TypedDict): config: _DebugDeviceInfo -@pytest.fixture(autouse=True) -def mock_storage(hass_storage: dict[str, Any]) -> None: - """Autouse hass_storage for the TestCase tests.""" - - async def test_command_template_value(hass: HomeAssistant) -> None: """Test the rendering of MQTT command template.""" @@ -2458,7 +2453,6 @@ async def test_multi_platform_discovery( "PayloadSentinel", "PublishPayloadType", "ReceiveMessage", - "ReceivePayloadType", "async_prepare_subscribe_topics", "async_publish", "async_subscribe", diff --git a/tests/components/mqtt/test_lawn_mower.py b/tests/components/mqtt/test_lawn_mower.py index 4906f6cfda329d..101a45787ef4ec 100644 --- a/tests/components/mqtt/test_lawn_mower.py +++ b/tests/components/mqtt/test_lawn_mower.py @@ -103,6 +103,13 @@ async def test_run_lawn_mower_setup_and_state_updates( state = hass.states.get("lawn_mower.test_lawn_mower") assert state.state == "mowing" + async_fire_mqtt_message(hass, "test/lawn_mower_stat", "returning") + + await hass.async_block_till_done() + + state = hass.states.get("lawn_mower.test_lawn_mower") + assert state.state == "returning" + async_fire_mqtt_message(hass, "test/lawn_mower_stat", "docked") await hass.async_block_till_done() @@ -198,6 +205,13 @@ async def test_value_template( state = hass.states.get("lawn_mower.test_lawn_mower") assert state.state == "paused" + async_fire_mqtt_message(hass, "test/lawn_mower_stat", '{"val":"returning"}') + + await hass.async_block_till_done() + + state = hass.states.get("lawn_mower.test_lawn_mower") + assert state.state == "returning" + async_fire_mqtt_message(hass, "test/lawn_mower_stat", '{"val": null}') await hass.async_block_till_done() @@ -702,7 +716,8 @@ async def test_mqtt_payload_not_a_valid_activity_warning( assert ( "Invalid activity for lawn_mower.test_lawn_mower: 'painting' " - "(valid activities: ['error', 'paused', 'mowing', 'docked'])" in caplog.text + "(valid activities: ['error', 'paused', 'mowing', 'docked', 'returning'])" + in caplog.text ) @@ -774,6 +789,7 @@ async def test_reloadable( [ ("activity_state_topic", "paused", None, "paused"), ("activity_state_topic", "docked", None, "docked"), + ("activity_state_topic", "returning", None, "returning"), ("activity_state_topic", "mowing", None, "mowing"), ], ) diff --git a/tests/components/mqtt/test_legacy_vacuum.py b/tests/components/mqtt/test_legacy_vacuum.py deleted file mode 100644 index 9b45b65d2cc4cc..00000000000000 --- a/tests/components/mqtt/test_legacy_vacuum.py +++ /dev/null @@ -1,83 +0,0 @@ -"""The tests for the Legacy Mqtt vacuum platform.""" - -# The legacy schema for MQTT vacuum was deprecated with HA Core 2023.8.0 -# and was removed with HA Core 2024.2.0 -# cleanup is planned with HA Core 2025.2 - -import json - -import pytest - -from homeassistant.components import mqtt, vacuum -from homeassistant.core import HomeAssistant -from homeassistant.helpers.typing import DiscoveryInfoType - -from tests.common import async_fire_mqtt_message -from tests.typing import MqttMockHAClientGenerator - -DEFAULT_CONFIG = {mqtt.DOMAIN: {vacuum.DOMAIN: {"name": "test"}}} - - -@pytest.mark.parametrize( - ("hass_config", "removed"), - [ - ({mqtt.DOMAIN: {vacuum.DOMAIN: {"name": "test", "schema": "legacy"}}}, True), - ({mqtt.DOMAIN: {vacuum.DOMAIN: {"name": "test"}}}, False), - ({mqtt.DOMAIN: {vacuum.DOMAIN: {"name": "test", "schema": "state"}}}, True), - ], -) -async def test_removed_support_yaml( - hass: HomeAssistant, - mqtt_mock_entry: MqttMockHAClientGenerator, - caplog: pytest.LogCaptureFixture, - removed: bool, -) -> None: - """Test that the removed support validation for the legacy schema works.""" - assert await mqtt_mock_entry() - entity = hass.states.get("vacuum.test") - - if removed: - assert entity is None - assert ( - "The 'schema' option has been removed, " - "please remove it from your configuration" in caplog.text - ) - else: - assert entity is not None - - -@pytest.mark.parametrize( - ("config", "removed"), - [ - ({"name": "test", "schema": "legacy"}, True), - ({"name": "test"}, False), - ({"name": "test", "schema": "state"}, True), - ], -) -async def test_removed_support_discovery( - hass: HomeAssistant, - mqtt_mock_entry: MqttMockHAClientGenerator, - caplog: pytest.LogCaptureFixture, - config: DiscoveryInfoType, - removed: bool, -) -> None: - """Test that the removed support validation for the legacy schema works.""" - assert await mqtt_mock_entry() - - config_payload = json.dumps(config) - async_fire_mqtt_message(hass, "homeassistant/vacuum/test/config", config_payload) - await hass.async_block_till_done() - - entity = hass.states.get("vacuum.test") - assert entity is not None - - if removed: - assert ( - "The 'schema' option has been removed, " - "please remove it from your configuration" in caplog.text - ) - else: - assert ( - "The 'schema' option has been removed, " - "please remove it from your configuration" not in caplog.text - ) diff --git a/tests/components/mqtt/test_sensor.py b/tests/components/mqtt/test_sensor.py index 4b117aaa4d5eb7..a62c36404ca520 100644 --- a/tests/components/mqtt/test_sensor.py +++ b/tests/components/mqtt/test_sensor.py @@ -3,6 +3,7 @@ import copy from datetime import datetime, timedelta import json +import logging from pathlib import Path from typing import Any from unittest.mock import MagicMock, patch @@ -110,6 +111,48 @@ async def test_setting_sensor_value_via_mqtt_message( assert state.attributes.get("unit_of_measurement") == "fav unit" +@pytest.mark.parametrize( + "hass_config", + [ + { + mqtt.DOMAIN: { + sensor.DOMAIN: { + "name": "test", + "state_topic": "test-topic", + "device_class": "enum", + "options": ["red", "green", "blue"], + } + } + }, + ], +) +async def test_setting_enum_sensor_value_via_mqtt_message( + hass: HomeAssistant, + mqtt_mock_entry: MqttMockHAClientGenerator, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test the setting of the value via MQTT of an enum type sensor.""" + await mqtt_mock_entry() + + async_fire_mqtt_message(hass, "test-topic", "red") + state = hass.states.get("sensor.test") + assert state.state == "red" + + async_fire_mqtt_message(hass, "test-topic", "green") + state = hass.states.get("sensor.test") + assert state.state == "green" + + with caplog.at_level(logging.WARNING): + async_fire_mqtt_message(hass, "test-topic", "yellow") + assert ( + "Ignoring invalid option received on topic 'test-topic', " + "got 'yellow', allowed: red, green, blue" in caplog.text + ) + # Assert the state update was filtered out and ignored + state = hass.states.get("sensor.test") + assert state.state == "green" + + @pytest.mark.parametrize( "hass_config", [ @@ -874,6 +917,61 @@ async def test_invalid_state_class( assert "expected SensorStateClass or one of" in caplog.text +@pytest.mark.parametrize( + ("hass_config", "error_logged"), + [ + ( + { + mqtt.DOMAIN: { + sensor.DOMAIN: { + "name": "test", + "state_topic": "test-topic", + "state_class": "measurement", + "options": ["red", "green", "blue"], + } + } + }, + "Specifying `options` is not allowed together with the `state_class` " + "or `unit_of_measurement` option", + ), + ( + { + mqtt.DOMAIN: { + sensor.DOMAIN: { + "name": "test", + "state_topic": "test-topic", + "device_class": "gas", + "options": ["red", "green", "blue"], + } + } + }, + "The option `options` can only be used together with " + "device class `enum`, got `device_class` 'gas'", + ), + ( + { + mqtt.DOMAIN: { + sensor.DOMAIN: { + "name": "test", + "state_topic": "test-topic", + "options": [], + } + } + }, + "An empty options list is not allowed", + ), + ], +) +async def test_invalid_options_config( + mqtt_mock_entry: MqttMockHAClientGenerator, + caplog: pytest.LogCaptureFixture, + error_logged: str, +) -> None: + """Test state_class, deviceclass with sensor options.""" + assert await mqtt_mock_entry() + assert error_logged in caplog.text + + @pytest.mark.parametrize( "hass_config", [ @@ -891,6 +989,13 @@ async def test_invalid_state_class( "state_topic": "test-topic", "state_class": None, }, + { + "name": "Test 4", + "state_topic": "test-topic", + "state_class": None, + "device_class": "enum", + "options": ["red", "green", "blue"], + }, ] } } diff --git a/tests/components/mqtt/test_vacuum.py b/tests/components/mqtt/test_vacuum.py index 7fc4ff981fd88d..fbffe062261049 100644 --- a/tests/components/mqtt/test_vacuum.py +++ b/tests/components/mqtt/test_vacuum.py @@ -2,7 +2,6 @@ from copy import deepcopy import json -import logging from typing import Any from unittest.mock import patch @@ -102,32 +101,6 @@ ) -async def test_warning_schema_option( - hass: HomeAssistant, - mqtt_mock_entry: MqttMockHAClientGenerator, - caplog: pytest.LogCaptureFixture, -) -> None: - """Test the warning on use of deprecated schema option.""" - await mqtt_mock_entry() - # Send discovery message with deprecated schema option - async_fire_mqtt_message( - hass, - f"homeassistant/{vacuum.DOMAIN}/bla/config", - '{"name": "test", "schema": "state", "o": {"name": "Bla2MQTT", "sw": "0.99", "url":"https://example.com/support"}}', - ) - await hass.async_block_till_done() - await hass.async_block_till_done(wait_background_tasks=True) - - state = hass.states.get("vacuum.test") - # We do not fail if the schema option is still in the payload, but we log an error - assert state is not None - with caplog.at_level(logging.WARNING): - assert ( - "The 'schema' option has been removed, " - "please remove it from your configuration" in caplog.text - ) - - @pytest.mark.parametrize("hass_config", [DEFAULT_CONFIG]) async def test_default_supported_features( hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator diff --git a/tests/components/myuplink/test_config_flow.py b/tests/components/myuplink/test_config_flow.py index 3ae32575257b11..c24d26057debb3 100644 --- a/tests/components/myuplink/test_config_flow.py +++ b/tests/components/myuplink/test_config_flow.py @@ -105,14 +105,7 @@ async def test_flow_reauth( assert len(hass.config_entries.async_entries(DOMAIN)) == 1 - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": mock_config_entry.entry_id, - }, - data=mock_config_entry.data, - ) + result = await mock_config_entry.start_reauth_flow(hass) assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/nam/test_config_flow.py b/tests/components/nam/test_config_flow.py index b96eddfd18bd62..f3465e59fb6801 100644 --- a/tests/components/nam/test_config_flow.py +++ b/tests/components/nam/test_config_flow.py @@ -9,7 +9,6 @@ from homeassistant.components import zeroconf from homeassistant.components.nam.const import DOMAIN from homeassistant.config_entries import ( - SOURCE_REAUTH, SOURCE_RECONFIGURE, SOURCE_USER, SOURCE_ZEROCONF, @@ -122,6 +121,9 @@ async def test_reauth_successful(hass: HomeAssistant) -> None: data={"host": "10.10.2.3"}, ) entry.add_to_hass(hass) + result = await entry.start_reauth_flow(hass) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reauth_confirm" with ( patch( @@ -133,15 +135,6 @@ async def test_reauth_successful(hass: HomeAssistant) -> None: return_value="aa:bb:cc:dd:ee:ff", ), ): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH, "entry_id": entry.entry_id}, - data=entry.data, - ) - - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "reauth_confirm" - result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input=VALID_AUTH, @@ -160,20 +153,14 @@ async def test_reauth_unsuccessful(hass: HomeAssistant) -> None: data={"host": "10.10.2.3"}, ) entry.add_to_hass(hass) + result = await entry.start_reauth_flow(hass) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reauth_confirm" with patch( "homeassistant.components.nam.NettigoAirMonitor.async_check_credentials", side_effect=ApiError("API Error"), ): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH, "entry_id": entry.entry_id}, - data=entry.data, - ) - - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "reauth_confirm" - result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input=VALID_AUTH, diff --git a/tests/components/nanoleaf/test_config_flow.py b/tests/components/nanoleaf/test_config_flow.py index eaa1c60dcd4800..97a314b0bf4d90 100644 --- a/tests/components/nanoleaf/test_config_flow.py +++ b/tests/components/nanoleaf/test_config_flow.py @@ -297,15 +297,7 @@ async def test_reauth(hass: HomeAssistant) -> None: return_value=True, ), ): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - "unique_id": entry.unique_id, - }, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "link" diff --git a/tests/components/neato/test_config_flow.py b/tests/components/neato/test_config_flow.py index 1b86c4e9980792..c5289927d91c2a 100644 --- a/tests/components/neato/test_config_flow.py +++ b/tests/components/neato/test_config_flow.py @@ -111,16 +111,15 @@ async def test_reauth( hass, NEATO_DOMAIN, ClientCredential(CLIENT_ID, CLIENT_SECRET) ) - MockConfigEntry( + entry = MockConfigEntry( entry_id="my_entry", domain=NEATO_DOMAIN, data={"username": "abcdef", "password": "123456", "vendor": "neato"}, - ).add_to_hass(hass) + ) + entry.add_to_hass(hass) # Should show form - result = await hass.config_entries.flow.async_init( - "neato", context={"source": config_entries.SOURCE_REAUTH} - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/nest/test_event.py b/tests/components/nest/test_event.py new file mode 100644 index 00000000000000..f45e6c1c6e6a71 --- /dev/null +++ b/tests/components/nest/test_event.py @@ -0,0 +1,325 @@ +"""Test for Nest event platform.""" + +import datetime +from typing import Any +from unittest.mock import patch + +from freezegun.api import FrozenDateTimeFactory +from google_nest_sdm.event import EventMessage, EventType +from google_nest_sdm.traits import TraitType +import pytest + +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant +from homeassistant.util.dt import utcnow + +from .common import DEVICE_ID, CreateDevice, FakeSubscriber +from .conftest import PlatformSetup + +EVENT_SESSION_ID = "CjY5Y3VKaTZwR3o4Y19YbTVfMF..." +EVENT_ID = "FWWVQVUdGNUlTU2V4MGV2aTNXV..." +ENCODED_EVENT_ID = "WyJDalk1WTNWS2FUWndSM280WTE5WWJUVmZNRi4uLiIsICJGV1dWUVZVZEdOVWxUVTJWNE1HVjJhVE5YVi4uLiJd" + +EVENT_SESSION_ID2 = "DjY5Y3VKaTZwR3o4Y19YbTVfMF..." +EVENT_ID2 = "GWWVQVUdGNUlTU2V4MGV2aTNXV..." +ENCODED_EVENT_ID2 = "WyJEalk1WTNWS2FUWndSM280WTE5WWJUVmZNRi4uLiIsICJHV1dWUVZVZEdOVWxUVTJWNE1HVjJhVE5YVi4uLiJd" + + +@pytest.fixture +def platforms() -> list[Platform]: + """Fixture for platforms to setup.""" + return [Platform.EVENT] + + +@pytest.fixture(autouse=True) +def enable_prefetch(subscriber: FakeSubscriber) -> None: + """Fixture to enable media fetching for tests to exercise.""" + subscriber.cache_policy.fetch = True + with patch("homeassistant.components.nest.EVENT_MEDIA_CACHE_SIZE", new=5): + yield + + +@pytest.fixture +def device_type() -> str: + """Fixture for the type of device under test.""" + return "sdm.devices.types.DOORBELL" + + +@pytest.fixture +async def device_traits() -> dict[str, Any]: + """Fixture to set default device traits used when creating devices.""" + return { + "sdm.devices.traits.Info": { + "customName": "Front", + }, + "sdm.devices.traits.CameraLiveStream": { + "maxVideoResolution": { + "width": 640, + "height": 480, + }, + "videoCodecs": ["H264"], + "audioCodecs": ["AAC"], + }, + } + + +def create_events(events: str) -> EventMessage: + """Create an EventMessage for events.""" + return create_event_messages( + { + event: { + "eventSessionId": EVENT_SESSION_ID, + "eventId": EVENT_ID, + } + for event in events + } + ) + + +def create_event_messages( + events: dict[str, Any], parameters: dict[str, Any] | None = None +) -> EventMessage: + """Create an EventMessage for events.""" + return EventMessage.create_event( + { + "eventId": "some-event-id", + "timestamp": utcnow().isoformat(timespec="seconds"), + "resourceUpdate": { + "name": DEVICE_ID, + "events": events, + }, + **(parameters if parameters else {}), + }, + auth=None, + ) + + +@pytest.mark.freeze_time("2024-08-24T12:00:00Z") +@pytest.mark.parametrize( + ( + "trait_types", + "entity_id", + "expected_attributes", + "api_event_type", + "expected_event_type", + ), + [ + ( + [TraitType.DOORBELL_CHIME, TraitType.CAMERA_MOTION], + "event.front_chime", + { + "device_class": "doorbell", + "event_types": ["doorbell_chime"], + "friendly_name": "Front Chime", + }, + EventType.DOORBELL_CHIME, + "doorbell_chime", + ), + ( + [TraitType.CAMERA_MOTION, TraitType.CAMERA_PERSON, TraitType.CAMERA_SOUND], + "event.front_motion", + { + "device_class": "motion", + "event_types": ["camera_motion", "camera_person", "camera_sound"], + "friendly_name": "Front Motion", + }, + EventType.CAMERA_MOTION, + "camera_motion", + ), + ( + [TraitType.CAMERA_MOTION, TraitType.CAMERA_PERSON, TraitType.CAMERA_SOUND], + "event.front_motion", + { + "device_class": "motion", + "event_types": ["camera_motion", "camera_person", "camera_sound"], + "friendly_name": "Front Motion", + }, + EventType.CAMERA_PERSON, + "camera_person", + ), + ( + [TraitType.CAMERA_MOTION, TraitType.CAMERA_PERSON, TraitType.CAMERA_SOUND], + "event.front_motion", + { + "device_class": "motion", + "event_types": ["camera_motion", "camera_person", "camera_sound"], + "friendly_name": "Front Motion", + }, + EventType.CAMERA_SOUND, + "camera_sound", + ), + ], +) +async def test_receive_events( + hass: HomeAssistant, + subscriber: FakeSubscriber, + setup_platform: PlatformSetup, + create_device: CreateDevice, + trait_types: list[TraitType], + entity_id: str, + expected_attributes: dict[str, str], + api_event_type: EventType, + expected_event_type: str, +) -> None: + """Test a pubsub message for a camera person event.""" + create_device.create( + raw_traits={ + **{trait_type: {} for trait_type in trait_types}, + api_event_type: {}, + } + ) + await setup_platform() + + state = hass.states.get(entity_id) + assert state.state == "unknown" + assert state.attributes == { + **expected_attributes, + "event_type": None, + } + + await subscriber.async_receive_event(create_events([api_event_type])) + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert state.state == "2024-08-24T12:00:00.000+00:00" + assert state.attributes == { + **expected_attributes, + "event_type": expected_event_type, + "nest_event_id": ENCODED_EVENT_ID, + } + + +@pytest.mark.parametrize(("trait_type"), [(TraitType.DOORBELL_CHIME)]) +async def test_ignore_unrelated_event( + hass: HomeAssistant, + subscriber: FakeSubscriber, + setup_platform: PlatformSetup, + create_device: CreateDevice, + trait_type: TraitType, +) -> None: + """Test a pubsub message for a camera person event.""" + create_device.create( + raw_traits={ + trait_type: {}, + } + ) + await setup_platform() + + # Device does not have traits matching this event type + await subscriber.async_receive_event(create_events([EventType.CAMERA_MOTION])) + await hass.async_block_till_done() + + state = hass.states.get("event.front_chime") + assert state.state == "unknown" + assert state.attributes == { + "device_class": "doorbell", + "event_type": None, + "event_types": ["doorbell_chime"], + "friendly_name": "Front Chime", + } + + +@pytest.mark.freeze_time("2024-08-24T12:00:00Z") +async def test_event_threads( + hass: HomeAssistant, + subscriber: FakeSubscriber, + setup_platform: PlatformSetup, + create_device: CreateDevice, + freezer: FrozenDateTimeFactory, +) -> None: + """Test multiple events delivered as part of a thread are a single home assistant event.""" + create_device.create( + raw_traits={ + TraitType.DOORBELL_CHIME: {}, + TraitType.CAMERA_CLIP_PREVIEW: {}, + } + ) + await setup_platform() + + state = hass.states.get("event.front_chime") + assert state.state == "unknown" + + # Doorbell event is received + freezer.tick(datetime.timedelta(seconds=2)) + await subscriber.async_receive_event( + create_event_messages( + { + EventType.DOORBELL_CHIME: { + "eventSessionId": EVENT_SESSION_ID, + "eventId": EVENT_ID, + } + }, + parameters={"eventThreadState": "STARTED"}, + ) + ) + await hass.async_block_till_done() + + state = hass.states.get("event.front_chime") + assert state.state == "2024-08-24T12:00:02.000+00:00" + assert state.attributes == { + "device_class": "doorbell", + "event_types": ["doorbell_chime"], + "friendly_name": "Front Chime", + "event_type": "doorbell_chime", + "nest_event_id": ENCODED_EVENT_ID, + } + + # Media arrives in a second message that ends the thread + freezer.tick(datetime.timedelta(seconds=2)) + await subscriber.async_receive_event( + create_event_messages( + { + EventType.DOORBELL_CHIME: { + "eventSessionId": EVENT_SESSION_ID, + "eventId": EVENT_ID, + }, + EventType.CAMERA_CLIP_PREVIEW: { + "eventSessionId": EVENT_SESSION_ID, + "previewUrl": "http://example", + }, + }, + parameters={"eventThreadState": "ENDED"}, + ) + ) + await hass.async_block_till_done() + + state = hass.states.get("event.front_chime") + assert ( + state.state == "2024-08-24T12:00:02.000+00:00" + ) # A second event is not received + assert state.attributes == { + "device_class": "doorbell", + "event_types": ["doorbell_chime"], + "friendly_name": "Front Chime", + "event_type": "doorbell_chime", + "nest_event_id": ENCODED_EVENT_ID, + } + + # An additional doorbell press event happens (with an updated session id) + freezer.tick(datetime.timedelta(seconds=2)) + await subscriber.async_receive_event( + create_event_messages( + { + EventType.DOORBELL_CHIME: { + "eventSessionId": EVENT_SESSION_ID2, + "eventId": EVENT_ID2, + }, + EventType.CAMERA_CLIP_PREVIEW: { + "eventSessionId": EVENT_SESSION_ID2, + "previewUrl": "http://example", + }, + }, + parameters={"eventThreadState": "ENDED"}, + ) + ) + await hass.async_block_till_done() + + state = hass.states.get("event.front_chime") + assert state.state == "2024-08-24T12:00:06.000+00:00" # Third event is received + assert state.attributes == { + "device_class": "doorbell", + "event_types": ["doorbell_chime"], + "friendly_name": "Front Chime", + "event_type": "doorbell_chime", + "nest_event_id": ENCODED_EVENT_ID2, + } diff --git a/tests/components/nest/test_events.py b/tests/components/nest/test_events.py index 08cf9f775b7081..e746e5f263f8d2 100644 --- a/tests/components/nest/test_events.py +++ b/tests/components/nest/test_events.py @@ -122,28 +122,28 @@ def create_events(events, device_id=DEVICE_ID, timestamp=None): [ ( "sdm.devices.types.DOORBELL", - ["sdm.devices.traits.DoorbellChime"], + ["sdm.devices.traits.DoorbellChime", "sdm.devices.traits.CameraEventImage"], "sdm.devices.events.DoorbellChime.Chime", "Doorbell", "doorbell_chime", ), ( "sdm.devices.types.CAMERA", - ["sdm.devices.traits.CameraMotion"], + ["sdm.devices.traits.CameraMotion", "sdm.devices.traits.CameraEventImage"], "sdm.devices.events.CameraMotion.Motion", "Camera", "camera_motion", ), ( "sdm.devices.types.CAMERA", - ["sdm.devices.traits.CameraPerson"], + ["sdm.devices.traits.CameraPerson", "sdm.devices.traits.CameraEventImage"], "sdm.devices.events.CameraPerson.Person", "Camera", "camera_person", ), ( "sdm.devices.types.CAMERA", - ["sdm.devices.traits.CameraSound"], + ["sdm.devices.traits.CameraSound", "sdm.devices.traits.CameraEventImage"], "sdm.devices.events.CameraSound.Sound", "Camera", "camera_sound", @@ -186,6 +186,8 @@ async def test_event( "type": expected_type, "timestamp": event_time, } + assert "image" in events[0].data["attachment"] + assert "video" not in events[0].data["attachment"] @pytest.mark.parametrize( @@ -232,6 +234,41 @@ async def test_camera_multiple_event( } +@pytest.mark.parametrize( + "device_traits", + [(["sdm.devices.traits.CameraMotion"])], +) +async def test_media_not_supported( + hass: HomeAssistant, entity_registry: er.EntityRegistry, subscriber, setup_platform +) -> None: + """Test a pubsub message for a camera person event.""" + events = async_capture_events(hass, NEST_EVENT) + await setup_platform() + entry = entity_registry.async_get("camera.front") + assert entry is not None + + event_map = { + "sdm.devices.events.CameraMotion.Motion": { + "eventSessionId": EVENT_SESSION_ID, + "eventId": EVENT_ID, + }, + } + + timestamp = utcnow() + await subscriber.async_receive_event(create_events(event_map, timestamp=timestamp)) + await hass.async_block_till_done() + + event_time = timestamp.replace(microsecond=0) + assert len(events) == 1 + assert event_view(events[0].data) == { + "device_id": entry.device_id, + "type": "camera_motion", + "timestamp": event_time, + } + # Media fetching not supported by this device + assert "attachment" not in events[0].data + + async def test_unknown_event(hass: HomeAssistant, subscriber, setup_platform) -> None: """Test a pubsub message for an unknown event type.""" events = async_capture_events(hass, NEST_EVENT) @@ -344,6 +381,8 @@ async def test_doorbell_event_thread( "type": "camera_motion", "timestamp": timestamp1.replace(microsecond=0), } + assert "image" in events[0].data["attachment"] + assert "video" in events[0].data["attachment"] @pytest.mark.parametrize( diff --git a/tests/components/nest/test_media_source.py b/tests/components/nest/test_media_source.py index 3cfa4ee6687219..4bc3559e308510 100644 --- a/tests/components/nest/test_media_source.py +++ b/tests/components/nest/test_media_source.py @@ -74,7 +74,6 @@ } IMAGE_BYTES_FROM_EVENT = b"test url image bytes" IMAGE_AUTHORIZATION_HEADERS = {"Authorization": "Basic g.0.eventToken"} -NEST_EVENT = "nest_event" def frame_image_data(frame_i, total_frames): @@ -1461,3 +1460,111 @@ async def test_camera_image_resize( assert browse.title == "Front: Recent Events" assert not browse.thumbnail assert len(browse.children) == 1 + + +async def test_event_media_attachment( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + device_registry: dr.DeviceRegistry, + subscriber, + auth, + setup_platform, +) -> None: + """Verify that an event media attachment is successfully resolved.""" + await setup_platform() + + assert len(hass.states.async_all()) == 1 + camera = hass.states.get("camera.front") + assert camera is not None + + device = device_registry.async_get_device(identifiers={(DOMAIN, DEVICE_ID)}) + assert device + assert device.name == DEVICE_NAME + + # Capture any events published + received_events = async_capture_events(hass, NEST_EVENT) + + # Set up fake media, and publish image events + auth.responses = [ + aiohttp.web.json_response(GENERATE_IMAGE_URL_RESPONSE), + aiohttp.web.Response(body=IMAGE_BYTES_FROM_EVENT), + ] + event_timestamp = dt_util.now() + await subscriber.async_receive_event( + create_event( + EVENT_SESSION_ID, + EVENT_ID, + PERSON_EVENT, + timestamp=event_timestamp, + ) + ) + await hass.async_block_till_done() + + assert len(received_events) == 1 + received_event = received_events[0] + attachment = received_event.data.get("attachment") + assert attachment + assert list(attachment.keys()) == ["image"] + assert attachment["image"].startswith("/api/nest/event_media") + assert attachment["image"].endswith("/thumbnail") + + # Download the attachment content and verify it works + client = await hass_client() + response = await client.get(attachment["image"]) + assert response.status == HTTPStatus.OK, f"Response not matched: {response}" + await response.read() + + +@pytest.mark.parametrize("device_traits", [BATTERY_CAMERA_TRAITS]) +async def test_event_clip_media_attachment( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + device_registry: dr.DeviceRegistry, + subscriber, + auth, + setup_platform, + mp4, +) -> None: + """Verify that an event media attachment is successfully resolved.""" + await setup_platform() + + assert len(hass.states.async_all()) == 1 + camera = hass.states.get("camera.front") + assert camera is not None + + device = device_registry.async_get_device(identifiers={(DOMAIN, DEVICE_ID)}) + assert device + assert device.name == DEVICE_NAME + + # Capture any events published + received_events = async_capture_events(hass, NEST_EVENT) + + # Set up fake media, and publish clip events + auth.responses = [ + aiohttp.web.Response(body=mp4.getvalue()), + ] + event_timestamp = dt_util.now() + await subscriber.async_receive_event( + create_event_message( + create_battery_event_data(MOTION_EVENT), + timestamp=event_timestamp, + ) + ) + await hass.async_block_till_done() + + assert len(received_events) == 1 + received_event = received_events[0] + attachment = received_event.data.get("attachment") + assert attachment + assert list(attachment.keys()) == ["image", "video"] + assert attachment["image"].startswith("/api/nest/event_media") + assert attachment["image"].endswith("/thumbnail") + assert attachment["video"].startswith("/api/nest/event_media") + assert not attachment["video"].endswith("/thumbnail") + + # Download the attachment content and verify it works + for content_path in attachment.values(): + client = await hass_client() + response = await client.get(content_path) + assert response.status == HTTPStatus.OK, f"Response not matched: {response}" + await response.read() diff --git a/tests/components/netatmo/snapshots/test_sensor.ambr b/tests/components/netatmo/snapshots/test_sensor.ambr index bc2a18d918d93a..0d13a88cd674ea 100644 --- a/tests/components/netatmo/snapshots/test_sensor.ambr +++ b/tests/components/netatmo/snapshots/test_sensor.ambr @@ -1159,7 +1159,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': 'unavailable', + 'state': 'True', }) # --- # name: test_entity[sensor.cold_water_power-entry] @@ -1508,7 +1508,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': 'unavailable', + 'state': 'True', }) # --- # name: test_entity[sensor.gas_power-entry] @@ -3257,7 +3257,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': 'unavailable', + 'state': 'True', }) # --- # name: test_entity[sensor.hot_water_power-entry] @@ -3896,7 +3896,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': 'unavailable', + 'state': 'True', }) # --- # name: test_entity[sensor.line_1_power-entry] @@ -3995,7 +3995,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': 'unavailable', + 'state': 'True', }) # --- # name: test_entity[sensor.line_2_power-entry] @@ -4094,7 +4094,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': 'unavailable', + 'state': 'True', }) # --- # name: test_entity[sensor.line_3_power-entry] @@ -4193,7 +4193,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': 'unavailable', + 'state': 'True', }) # --- # name: test_entity[sensor.line_4_power-entry] @@ -4292,7 +4292,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': 'unavailable', + 'state': 'True', }) # --- # name: test_entity[sensor.line_5_power-entry] @@ -5622,7 +5622,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': 'unavailable', + 'state': 'True', }) # --- # name: test_entity[sensor.total_power-entry] diff --git a/tests/components/nextcloud/__init__.py b/tests/components/nextcloud/__init__.py index e2102ed8c25071..4bc5a0416505ba 100644 --- a/tests/components/nextcloud/__init__.py +++ b/tests/components/nextcloud/__init__.py @@ -1 +1,38 @@ """Tests for the Nextcloud integration.""" + +from unittest.mock import Mock, patch + +from homeassistant.components.nextcloud.const import DOMAIN +from homeassistant.const import CONF_URL +from homeassistant.core import HomeAssistant + +from .const import MOCKED_ENTRY_ID + +from tests.common import MockConfigEntry + + +def mock_config_entry(config: dict) -> MockConfigEntry: + """Return a mocked config entry.""" + return MockConfigEntry( + domain=DOMAIN, title=config[CONF_URL], data=config, entry_id=MOCKED_ENTRY_ID + ) + + +async def init_integration( + hass: HomeAssistant, config: dict, data: dict +) -> MockConfigEntry: + """Set up the nextcloud integration.""" + entry = mock_config_entry(config) + entry.add_to_hass(hass) + + with ( + patch( + "homeassistant.components.nextcloud.NextcloudMonitor", + ) as mock_nextcloud_monitor, + ): + mock_nextcloud_monitor.update = Mock(return_value=True) + mock_nextcloud_monitor.return_value.data = data + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + return entry diff --git a/tests/components/nextcloud/conftest.py b/tests/components/nextcloud/conftest.py index cf3eda55fe15a3..3234e3773b8b5e 100644 --- a/tests/components/nextcloud/conftest.py +++ b/tests/components/nextcloud/conftest.py @@ -1,19 +1,11 @@ """Fixtrues for the Nextcloud integration tests.""" from collections.abc import Generator -from unittest.mock import AsyncMock, Mock, patch +from unittest.mock import AsyncMock, patch import pytest -@pytest.fixture -def mock_nextcloud_monitor() -> Mock: - """Mock of NextcloudMonitor.""" - return Mock( - update=Mock(return_value=True), - ) - - @pytest.fixture def mock_setup_entry() -> Generator[AsyncMock]: """Override async_setup_entry.""" diff --git a/tests/components/nextcloud/const.py b/tests/components/nextcloud/const.py new file mode 100644 index 00000000000000..2d328292b6f5b1 --- /dev/null +++ b/tests/components/nextcloud/const.py @@ -0,0 +1,182 @@ +"""Constants for nextcloud tests.""" + +from homeassistant.const import CONF_PASSWORD, CONF_URL, CONF_USERNAME, CONF_VERIFY_SSL + +MOCKED_ENTRY_ID = "1234567890abcdef" + +VALID_CONFIG = { + CONF_URL: "https://my.nc_url.local", + CONF_USERNAME: "nc_user", + CONF_PASSWORD: "nc_pass", + CONF_VERIFY_SSL: True, +} + +NC_DATA = { + "nextcloud": { + "system": { + "version": "28.0.4.1", + "theme": "", + "enable_avatars": "yes", + "enable_previews": "yes", + "memcache.local": "\\OC\\Memcache\\APCu", + "memcache.distributed": "none", + "filelocking.enabled": "yes", + "memcache.locking": "none", + "debug": "no", + "freespace": 32769138688, + "cpuload": [2.06640625, 1.58447265625, 1.45263671875], + "mem_total": 30728192, + "mem_free": 6753280, + "swap_total": 10484736, + "swap_free": 10484736, + "apps": { + "num_installed": 41, + "num_updates_available": 0, + "app_updates": [], + }, + "update": {"lastupdatedat": 1713048517, "available": False}, + }, + "storage": { + "num_users": 2, + "num_files": 6783, + "num_storages": 4, + "num_storages_local": 1, + "num_storages_home": 2, + "num_storages_other": 1, + }, + "shares": { + "num_shares": 2, + "num_shares_user": 0, + "num_shares_groups": 0, + "num_shares_link": 2, + "num_shares_mail": 0, + "num_shares_room": 0, + "num_shares_link_no_password": 2, + "num_fed_shares_sent": 0, + "num_fed_shares_received": 1, + "permissions_3_17": 1, + "permissions_3_31": 1, + }, + }, + "server": { + "webserver": "Apache/2.4.57 (Debian)", + "php": { + "version": "8.2.18", + "memory_limit": 536870912, + "max_execution_time": 3600, + "upload_max_filesize": 536870912, + "opcache_revalidate_freq": 60, + "opcache": { + "opcache_enabled": True, + "cache_full": False, + "restart_pending": False, + "restart_in_progress": False, + "memory_usage": { + "used_memory": 72027112, + "free_memory": 62190616, + "wasted_memory": 0, + "current_wasted_percentage": 0, + }, + "interned_strings_usage": { + "buffer_size": 33554432, + "used_memory": 12630360, + "free_memory": 20924072, + "number_of_strings": 69242, + }, + "opcache_statistics": { + "num_cached_scripts": 1406, + "num_cached_keys": 2654, + "max_cached_keys": 16229, + "hits": 9739971, + "start_time": 1722222008, + "last_restart_time": 0, + "oom_restarts": 0, + "hash_restarts": 0, + "manual_restarts": 0, + "misses": 1406, + "blacklist_misses": 0, + "blacklist_miss_ratio": 0, + "opcache_hit_rate": 99.9855667222406, + }, + "jit": { + "enabled": True, + "on": True, + "kind": 5, + "opt_level": 5, + "opt_flags": 6, + "buffer_size": 134217712, + "buffer_free": 133190688, + }, + }, + "apcu": { + "cache": { + "num_slots": 4099, + "ttl": 0, + "num_hits": 590911, + "num_misses": 55250, + "num_inserts": 55421, + "num_entries": 102, + "expunges": 0, + "start_time": 1722222008, + "mem_size": 175296, + "memory_type": "mmap", + }, + "sma": {"num_seg": 1, "seg_size": 33554312, "avail_mem": 33342368}, + }, + "extensions": [ + "Core", + "date", + "libxml", + "openssl", + "pcre", + "sqlite3", + "zlib", + "ctype", + "curl", + "dom", + "fileinfo", + "filter", + "hash", + "iconv", + "json", + "mbstring", + "SPL", + "session", + "PDO", + "pdo_sqlite", + "standard", + "posix", + "random", + "Reflection", + "Phar", + "SimpleXML", + "tokenizer", + "xml", + "xmlreader", + "xmlwriter", + "mysqlnd", + "apache2handler", + "apcu", + "bcmath", + "exif", + "ftp", + "gd", + "gmp", + "imagick", + "intl", + "ldap", + "memcached", + "pcntl", + "pdo_mysql", + "pdo_pgsql", + "redis", + "sodium", + "sysvsem", + "zip", + "Zend OPcache", + ], + }, + "database": {"type": "sqlite3", "version": "3.40.1", "size": "4784128"}, + }, + "activeUsers": {"last5minutes": 0, "last1hour": 0, "last24hours": 0}, +} diff --git a/tests/components/nextcloud/snapshots/test_binary_sensor.ambr b/tests/components/nextcloud/snapshots/test_binary_sensor.ambr new file mode 100644 index 00000000000000..1831419af520f8 --- /dev/null +++ b/tests/components/nextcloud/snapshots/test_binary_sensor.ambr @@ -0,0 +1,277 @@ +# serializer version: 1 +# name: test_async_setup_entry[binary_sensor.my_nc_url_local_avatars_enabled-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.my_nc_url_local_avatars_enabled', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Avatars enabled', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_system_enable_avatars', + 'unique_id': '1234567890abcdef#system_enable_avatars', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[binary_sensor.my_nc_url_local_avatars_enabled-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Avatars enabled', + }), + 'context': , + 'entity_id': 'binary_sensor.my_nc_url_local_avatars_enabled', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_async_setup_entry[binary_sensor.my_nc_url_local_debug_enabled-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.my_nc_url_local_debug_enabled', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Debug enabled', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_system_debug', + 'unique_id': '1234567890abcdef#system_debug', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[binary_sensor.my_nc_url_local_debug_enabled-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Debug enabled', + }), + 'context': , + 'entity_id': 'binary_sensor.my_nc_url_local_debug_enabled', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_async_setup_entry[binary_sensor.my_nc_url_local_filelocking_enabled-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.my_nc_url_local_filelocking_enabled', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Filelocking enabled', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_system_filelocking_enabled', + 'unique_id': '1234567890abcdef#system_filelocking.enabled', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[binary_sensor.my_nc_url_local_filelocking_enabled-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Filelocking enabled', + }), + 'context': , + 'entity_id': 'binary_sensor.my_nc_url_local_filelocking_enabled', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_async_setup_entry[binary_sensor.my_nc_url_local_jit_active-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.my_nc_url_local_jit_active', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'JIT active', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_jit_on', + 'unique_id': '1234567890abcdef#jit_on', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[binary_sensor.my_nc_url_local_jit_active-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local JIT active', + }), + 'context': , + 'entity_id': 'binary_sensor.my_nc_url_local_jit_active', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_async_setup_entry[binary_sensor.my_nc_url_local_jit_enabled-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.my_nc_url_local_jit_enabled', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'JIT enabled', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_jit_enabled', + 'unique_id': '1234567890abcdef#jit_enabled', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[binary_sensor.my_nc_url_local_jit_enabled-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local JIT enabled', + }), + 'context': , + 'entity_id': 'binary_sensor.my_nc_url_local_jit_enabled', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_async_setup_entry[binary_sensor.my_nc_url_local_previews_enabled-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.my_nc_url_local_previews_enabled', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Previews enabled', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_system_enable_previews', + 'unique_id': '1234567890abcdef#system_enable_previews', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[binary_sensor.my_nc_url_local_previews_enabled-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Previews enabled', + }), + 'context': , + 'entity_id': 'binary_sensor.my_nc_url_local_previews_enabled', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- diff --git a/tests/components/nextcloud/snapshots/test_config_flow.ambr b/tests/components/nextcloud/snapshots/test_config_flow.ambr index 06c4ce216db37a..e87db0a25c0d54 100644 --- a/tests/components/nextcloud/snapshots/test_config_flow.ambr +++ b/tests/components/nextcloud/snapshots/test_config_flow.ambr @@ -2,7 +2,7 @@ # name: test_reauth dict({ 'password': 'other_password', - 'url': 'nc_url', + 'url': 'https://my.nc_url.local', 'username': 'other_user', 'verify_ssl': True, }) @@ -10,7 +10,7 @@ # name: test_user_create_entry dict({ 'password': 'nc_pass', - 'url': 'nc_url', + 'url': 'https://my.nc_url.local', 'username': 'nc_user', 'verify_ssl': True, }) diff --git a/tests/components/nextcloud/snapshots/test_sensor.ambr b/tests/components/nextcloud/snapshots/test_sensor.ambr new file mode 100644 index 00000000000000..c49ba3496dabbf --- /dev/null +++ b/tests/components/nextcloud/snapshots/test_sensor.ambr @@ -0,0 +1,3973 @@ +# serializer version: 1 +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_active_users_last_5_minutes-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_amount_of_active_users_last_5_minutes', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Amount of active users last 5 minutes', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_activeusers_last5minutes', + 'unique_id': '1234567890abcdef#activeUsers_last5minutes', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_active_users_last_5_minutes-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Amount of active users last 5 minutes', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_amount_of_active_users_last_5_minutes', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_active_users_last_day-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_amount_of_active_users_last_day', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Amount of active users last day', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_activeusers_last24hours', + 'unique_id': '1234567890abcdef#activeUsers_last24hours', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_active_users_last_day-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Amount of active users last day', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_amount_of_active_users_last_day', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_active_users_last_hour-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_amount_of_active_users_last_hour', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Amount of active users last hour', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_activeusers_last1hour', + 'unique_id': '1234567890abcdef#activeUsers_last1hour', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_active_users_last_hour-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Amount of active users last hour', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_amount_of_active_users_last_hour', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_files-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.my_nc_url_local_amount_of_files', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Amount of files', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_storage_num_files', + 'unique_id': '1234567890abcdef#storage_num_files', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_files-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Amount of files', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_amount_of_files', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '6783', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_group_shares-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_amount_of_group_shares', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Amount of group shares', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_shares_num_shares_groups', + 'unique_id': '1234567890abcdef#shares_num_shares_groups', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_group_shares-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Amount of group shares', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_amount_of_group_shares', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_link_shares-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_amount_of_link_shares', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Amount of link shares', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_shares_num_shares_link', + 'unique_id': '1234567890abcdef#shares_num_shares_link', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_link_shares-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Amount of link shares', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_amount_of_link_shares', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '2', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_local_storages-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_amount_of_local_storages', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Amount of local storages', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_storage_num_storages_local', + 'unique_id': '1234567890abcdef#storage_num_storages_local', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_local_storages-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Amount of local storages', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_amount_of_local_storages', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '1', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_mail_shares-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_amount_of_mail_shares', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Amount of mail shares', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_shares_num_shares_mail', + 'unique_id': '1234567890abcdef#shares_num_shares_mail', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_mail_shares-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Amount of mail shares', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_amount_of_mail_shares', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_other_storages-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_amount_of_other_storages', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Amount of other storages', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_storage_num_storages_other', + 'unique_id': '1234567890abcdef#storage_num_storages_other', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_other_storages-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Amount of other storages', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_amount_of_other_storages', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '1', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_passwordless_link_shares-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_amount_of_passwordless_link_shares', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Amount of passwordless link shares', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_shares_num_shares_link_no_password', + 'unique_id': '1234567890abcdef#shares_num_shares_link_no_password', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_passwordless_link_shares-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Amount of passwordless link shares', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_amount_of_passwordless_link_shares', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '2', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_room_shares-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_amount_of_room_shares', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Amount of room shares', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_shares_num_shares_room', + 'unique_id': '1234567890abcdef#shares_num_shares_room', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_room_shares-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Amount of room shares', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_amount_of_room_shares', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_shares-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.my_nc_url_local_amount_of_shares', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Amount of shares', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_shares_num_shares', + 'unique_id': '1234567890abcdef#shares_num_shares', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_shares-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Amount of shares', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_amount_of_shares', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '2', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_shares_received-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_amount_of_shares_received', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Amount of shares received', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_shares_num_fed_shares_received', + 'unique_id': '1234567890abcdef#shares_num_fed_shares_received', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_shares_received-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Amount of shares received', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_amount_of_shares_received', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '1', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_shares_sent-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_amount_of_shares_sent', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Amount of shares sent', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_shares_num_fed_shares_sent', + 'unique_id': '1234567890abcdef#shares_num_fed_shares_sent', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_shares_sent-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Amount of shares sent', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_amount_of_shares_sent', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_storages-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.my_nc_url_local_amount_of_storages', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Amount of storages', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_storage_num_storages', + 'unique_id': '1234567890abcdef#storage_num_storages', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_storages-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Amount of storages', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_amount_of_storages', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '4', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_storages_at_home-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_amount_of_storages_at_home', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Amount of storages at home', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_storage_num_storages_home', + 'unique_id': '1234567890abcdef#storage_num_storages_home', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_storages_at_home-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Amount of storages at home', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_amount_of_storages_at_home', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '2', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_user-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.my_nc_url_local_amount_of_user', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Amount of user', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_storage_num_users', + 'unique_id': '1234567890abcdef#storage_num_users', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_user-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Amount of user', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_amount_of_user', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '2', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_user_shares-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_amount_of_user_shares', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Amount of user shares', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_shares_num_shares_user', + 'unique_id': '1234567890abcdef#shares_num_shares_user', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_amount_of_user_shares-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Amount of user shares', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_amount_of_user_shares', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_apps_installed-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.my_nc_url_local_apps_installed', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Apps installed', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_system_apps_num_installed', + 'unique_id': '1234567890abcdef#system_apps_num_installed', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_apps_installed-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Apps installed', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_apps_installed', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '41', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_cache_expunges-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_cache_expunges', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Cache expunges', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_cache_expunges', + 'unique_id': '1234567890abcdef#cache_expunges', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_cache_expunges-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Cache expunges', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_cache_expunges', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_cache_memory-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_cache_memory', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Cache memory', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_cache_memory_type', + 'unique_id': '1234567890abcdef#cache_memory_type', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_cache_memory-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Cache memory', + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_cache_memory', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'mmap', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_cache_memory_size-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_cache_memory_size', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 1, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Cache memory size', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_cache_mem_size', + 'unique_id': '1234567890abcdef#cache_mem_size', + 'unit_of_measurement': , + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_cache_memory_size-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'data_size', + 'friendly_name': 'my.nc_url.local Cache memory size', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_cache_memory_size', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0.175296', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_cache_number_of_entires-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_cache_number_of_entires', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Cache number of entires', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_cache_num_entries', + 'unique_id': '1234567890abcdef#cache_num_entries', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_cache_number_of_entires-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Cache number of entires', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_cache_number_of_entires', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '102', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_cache_number_of_hits-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_cache_number_of_hits', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Cache number of hits', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_cache_num_hits', + 'unique_id': '1234567890abcdef#cache_num_hits', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_cache_number_of_hits-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Cache number of hits', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_cache_number_of_hits', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '590911', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_cache_number_of_inserts-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_cache_number_of_inserts', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Cache number of inserts', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_cache_num_inserts', + 'unique_id': '1234567890abcdef#cache_num_inserts', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_cache_number_of_inserts-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Cache number of inserts', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_cache_number_of_inserts', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '55421', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_cache_number_of_misses-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_cache_number_of_misses', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Cache number of misses', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_cache_num_misses', + 'unique_id': '1234567890abcdef#cache_num_misses', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_cache_number_of_misses-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Cache number of misses', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_cache_number_of_misses', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '55250', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_cache_number_of_slots-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_cache_number_of_slots', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Cache number of slots', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_cache_num_slots', + 'unique_id': '1234567890abcdef#cache_num_slots', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_cache_number_of_slots-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Cache number of slots', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_cache_number_of_slots', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '4099', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_cache_start_time-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_cache_start_time', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Cache start time', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_cache_start_time', + 'unique_id': '1234567890abcdef#cache_start_time', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_cache_start_time-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'timestamp', + 'friendly_name': 'my.nc_url.local Cache start time', + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_cache_start_time', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '2024-07-29T03:00:08+00:00', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_cache_ttl-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_cache_ttl', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Cache ttl', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_cache_ttl', + 'unique_id': '1234567890abcdef#cache_ttl', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_cache_ttl-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Cache ttl', + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_cache_ttl', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_cpu_load_last_15_minutes-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.my_nc_url_local_cpu_load_last_15_minutes', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 2, + }), + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'CPU Load last 15 minutes', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_system_cpuload_15', + 'unique_id': '1234567890abcdef#system_cpuload_15', + 'unit_of_measurement': 'load', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_cpu_load_last_15_minutes-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local CPU Load last 15 minutes', + 'unit_of_measurement': 'load', + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_cpu_load_last_15_minutes', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '1.45263671875', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_cpu_load_last_1_minute-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.my_nc_url_local_cpu_load_last_1_minute', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 2, + }), + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'CPU Load last 1 minute', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_system_cpuload_1', + 'unique_id': '1234567890abcdef#system_cpuload_1', + 'unit_of_measurement': 'load', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_cpu_load_last_1_minute-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local CPU Load last 1 minute', + 'unit_of_measurement': 'load', + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_cpu_load_last_1_minute', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '2.06640625', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_cpu_load_last_5_minutes-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.my_nc_url_local_cpu_load_last_5_minutes', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 2, + }), + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'CPU Load last 5 minutes', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_system_cpuload_5', + 'unique_id': '1234567890abcdef#system_cpuload_5', + 'unit_of_measurement': 'load', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_cpu_load_last_5_minutes-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local CPU Load last 5 minutes', + 'unit_of_measurement': 'load', + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_cpu_load_last_5_minutes', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '1.58447265625', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_database_size-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.my_nc_url_local_database_size', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 1, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Database size', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_database_size', + 'unique_id': '1234567890abcdef#database_size', + 'unit_of_measurement': , + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_database_size-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'data_size', + 'friendly_name': 'my.nc_url.local Database size', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_database_size', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '4.784128', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_database_type-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_database_type', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Database type', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_database_type', + 'unique_id': '1234567890abcdef#database_type', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_database_type-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Database type', + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_database_type', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'sqlite3', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_database_version-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_database_version', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Database version', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_database_version', + 'unique_id': '1234567890abcdef#database_version', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_database_version-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Database version', + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_database_version', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '3.40.1', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_free_memory-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.my_nc_url_local_free_memory', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 2, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Free memory', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_system_mem_free', + 'unique_id': '1234567890abcdef#system_mem_free', + 'unit_of_measurement': , + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_free_memory-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'data_size', + 'friendly_name': 'my.nc_url.local Free memory', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_free_memory', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '6.75328', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_free_space-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.my_nc_url_local_free_space', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 2, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Free space', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_system_freespace', + 'unique_id': '1234567890abcdef#system_freespace', + 'unit_of_measurement': , + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_free_space-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'data_size', + 'friendly_name': 'my.nc_url.local Free space', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_free_space', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '32.769138688', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_free_swap_memory-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.my_nc_url_local_free_swap_memory', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 2, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Free swap memory', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_system_swap_free', + 'unique_id': '1234567890abcdef#system_swap_free', + 'unit_of_measurement': , + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_free_swap_memory-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'data_size', + 'friendly_name': 'my.nc_url.local Free swap memory', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_free_swap_memory', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '10.484736', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_interned_buffer_size-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_interned_buffer_size', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 1, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Interned buffer size', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_interned_strings_usage_buffer_size', + 'unique_id': '1234567890abcdef#interned_strings_usage_buffer_size', + 'unit_of_measurement': , + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_interned_buffer_size-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'data_size', + 'friendly_name': 'my.nc_url.local Interned buffer size', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_interned_buffer_size', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '33.554432', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_interned_free_memory-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_interned_free_memory', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 1, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Interned free memory', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_interned_strings_usage_free_memory', + 'unique_id': '1234567890abcdef#interned_strings_usage_free_memory', + 'unit_of_measurement': , + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_interned_free_memory-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'data_size', + 'friendly_name': 'my.nc_url.local Interned free memory', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_interned_free_memory', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '20.924072', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_interned_number_of_strings-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_interned_number_of_strings', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Interned number of strings', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_interned_strings_usage_number_of_strings', + 'unique_id': '1234567890abcdef#interned_strings_usage_number_of_strings', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_interned_number_of_strings-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Interned number of strings', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_interned_number_of_strings', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '69242', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_interned_used_memory-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_interned_used_memory', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 1, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Interned used memory', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_interned_strings_usage_used_memory', + 'unique_id': '1234567890abcdef#interned_strings_usage_used_memory', + 'unit_of_measurement': , + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_interned_used_memory-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'data_size', + 'friendly_name': 'my.nc_url.local Interned used memory', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_interned_used_memory', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '12.63036', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_jit_buffer_free-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_jit_buffer_free', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 1, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'JIT buffer free', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_jit_buffer_free', + 'unique_id': '1234567890abcdef#jit_buffer_free', + 'unit_of_measurement': , + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_jit_buffer_free-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'data_size', + 'friendly_name': 'my.nc_url.local JIT buffer free', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_jit_buffer_free', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '133.190688', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_jit_buffer_size-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_jit_buffer_size', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 1, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'JIT buffer size', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_jit_buffer_size', + 'unique_id': '1234567890abcdef#jit_buffer_size', + 'unit_of_measurement': , + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_jit_buffer_size-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'data_size', + 'friendly_name': 'my.nc_url.local JIT buffer size', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_jit_buffer_size', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '134.217712', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_jit_kind-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_jit_kind', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'JIT kind', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_jit_kind', + 'unique_id': '1234567890abcdef#jit_kind', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_jit_kind-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local JIT kind', + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_jit_kind', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '5', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_jit_opt_flags-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_jit_opt_flags', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'JIT opt flags', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_jit_opt_flags', + 'unique_id': '1234567890abcdef#jit_opt_flags', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_jit_opt_flags-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local JIT opt flags', + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_jit_opt_flags', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '6', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_jit_opt_level-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_jit_opt_level', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'JIT opt level', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_jit_opt_level', + 'unique_id': '1234567890abcdef#jit_opt_level', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_jit_opt_level-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local JIT opt level', + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_jit_opt_level', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '5', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_opcache_blacklist_miss_ratio-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_opcache_blacklist_miss_ratio', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Opcache blacklist miss ratio', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_opcache_statistics_blacklist_miss_ratio', + 'unique_id': '1234567890abcdef#opcache_statistics_blacklist_miss_ratio', + 'unit_of_measurement': '%', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_opcache_blacklist_miss_ratio-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Opcache blacklist miss ratio', + 'state_class': , + 'unit_of_measurement': '%', + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_opcache_blacklist_miss_ratio', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_opcache_blacklist_misses-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_opcache_blacklist_misses', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Opcache blacklist misses', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_opcache_statistics_blacklist_misses', + 'unique_id': '1234567890abcdef#opcache_statistics_blacklist_misses', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_opcache_blacklist_misses-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Opcache blacklist misses', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_opcache_blacklist_misses', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_opcache_cached_keys-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_opcache_cached_keys', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Opcache cached keys', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_opcache_statistics_num_cached_keys', + 'unique_id': '1234567890abcdef#opcache_statistics_num_cached_keys', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_opcache_cached_keys-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Opcache cached keys', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_opcache_cached_keys', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '2654', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_opcache_cached_scripts-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_opcache_cached_scripts', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Opcache cached scripts', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_opcache_statistics_num_cached_scripts', + 'unique_id': '1234567890abcdef#opcache_statistics_num_cached_scripts', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_opcache_cached_scripts-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Opcache cached scripts', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_opcache_cached_scripts', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '1406', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_opcache_current_wasted_percentage-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_opcache_current_wasted_percentage', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 1, + }), + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Opcache current wasted percentage', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_server_php_opcache_memory_usage_current_wasted_percentage', + 'unique_id': '1234567890abcdef#server_php_opcache_memory_usage_current_wasted_percentage', + 'unit_of_measurement': '%', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_opcache_current_wasted_percentage-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Opcache current wasted percentage', + 'unit_of_measurement': '%', + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_opcache_current_wasted_percentage', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_opcache_free_memory-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_opcache_free_memory', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 1, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Opcache free memory', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_server_php_opcache_memory_usage_free_memory', + 'unique_id': '1234567890abcdef#server_php_opcache_memory_usage_free_memory', + 'unit_of_measurement': , + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_opcache_free_memory-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'data_size', + 'friendly_name': 'my.nc_url.local Opcache free memory', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_opcache_free_memory', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '62.190616', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_opcache_hash_restarts-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_opcache_hash_restarts', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Opcache hash restarts', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_opcache_statistics_hash_restarts', + 'unique_id': '1234567890abcdef#opcache_statistics_hash_restarts', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_opcache_hash_restarts-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Opcache hash restarts', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_opcache_hash_restarts', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_opcache_hit_rate-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_opcache_hit_rate', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 1, + }), + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Opcache hit rate', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_opcache_statistics_opcache_hit_rate', + 'unique_id': '1234567890abcdef#opcache_statistics_opcache_hit_rate', + 'unit_of_measurement': '%', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_opcache_hit_rate-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Opcache hit rate', + 'unit_of_measurement': '%', + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_opcache_hit_rate', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '99.9855667222406', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_opcache_hits-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_opcache_hits', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Opcache hits', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_opcache_statistics_hits', + 'unique_id': '1234567890abcdef#opcache_statistics_hits', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_opcache_hits-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Opcache hits', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_opcache_hits', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '9739971', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_opcache_last_restart_time-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_opcache_last_restart_time', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Opcache last restart time', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_opcache_statistics_last_restart_time', + 'unique_id': '1234567890abcdef#opcache_statistics_last_restart_time', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_opcache_last_restart_time-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'timestamp', + 'friendly_name': 'my.nc_url.local Opcache last restart time', + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_opcache_last_restart_time', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '1970-01-01T00:00:00+00:00', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_opcache_manual_restarts-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_opcache_manual_restarts', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Opcache manual restarts', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_opcache_statistics_manual_restarts', + 'unique_id': '1234567890abcdef#opcache_statistics_manual_restarts', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_opcache_manual_restarts-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Opcache manual restarts', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_opcache_manual_restarts', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_opcache_max_cached_keys-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_opcache_max_cached_keys', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Opcache max cached keys', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_opcache_statistics_max_cached_keys', + 'unique_id': '1234567890abcdef#opcache_statistics_max_cached_keys', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_opcache_max_cached_keys-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Opcache max cached keys', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_opcache_max_cached_keys', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '16229', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_opcache_misses-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_opcache_misses', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Opcache misses', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_opcache_statistics_misses', + 'unique_id': '1234567890abcdef#opcache_statistics_misses', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_opcache_misses-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Opcache misses', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_opcache_misses', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '1406', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_opcache_out_of_memory_restarts-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_opcache_out_of_memory_restarts', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Opcache out of memory restarts', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_opcache_statistics_oom_restarts', + 'unique_id': '1234567890abcdef#opcache_statistics_oom_restarts', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_opcache_out_of_memory_restarts-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Opcache out of memory restarts', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_opcache_out_of_memory_restarts', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_opcache_start_time-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_opcache_start_time', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Opcache start time', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_opcache_statistics_start_time', + 'unique_id': '1234567890abcdef#opcache_statistics_start_time', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_opcache_start_time-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'timestamp', + 'friendly_name': 'my.nc_url.local Opcache start time', + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_opcache_start_time', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '2024-07-29T03:00:08+00:00', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_opcache_used_memory-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_opcache_used_memory', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 1, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Opcache used memory', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_server_php_opcache_memory_usage_used_memory', + 'unique_id': '1234567890abcdef#server_php_opcache_memory_usage_used_memory', + 'unit_of_measurement': , + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_opcache_used_memory-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'data_size', + 'friendly_name': 'my.nc_url.local Opcache used memory', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_opcache_used_memory', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '72.027112', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_opcache_wasted_memory-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_opcache_wasted_memory', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 1, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Opcache wasted memory', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_server_php_opcache_memory_usage_wasted_memory', + 'unique_id': '1234567890abcdef#server_php_opcache_memory_usage_wasted_memory', + 'unit_of_measurement': , + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_opcache_wasted_memory-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'data_size', + 'friendly_name': 'my.nc_url.local Opcache wasted memory', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_opcache_wasted_memory', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0.0', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_php_max_execution_time-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_php_max_execution_time', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'PHP max execution time', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_server_php_max_execution_time', + 'unique_id': '1234567890abcdef#server_php_max_execution_time', + 'unit_of_measurement': , + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_php_max_execution_time-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'duration', + 'friendly_name': 'my.nc_url.local PHP max execution time', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_php_max_execution_time', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '3600', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_php_memory_limit-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_php_memory_limit', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 1, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'PHP memory limit', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_server_php_memory_limit', + 'unique_id': '1234567890abcdef#server_php_memory_limit', + 'unit_of_measurement': , + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_php_memory_limit-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'data_size', + 'friendly_name': 'my.nc_url.local PHP memory limit', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_php_memory_limit', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '536.870912', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_php_upload_maximum_filesize-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_php_upload_maximum_filesize', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 1, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'PHP upload maximum filesize', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_server_php_upload_max_filesize', + 'unique_id': '1234567890abcdef#server_php_upload_max_filesize', + 'unit_of_measurement': , + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_php_upload_maximum_filesize-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'data_size', + 'friendly_name': 'my.nc_url.local PHP upload maximum filesize', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_php_upload_maximum_filesize', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '536.870912', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_php_version-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_php_version', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'PHP version', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_server_php_version', + 'unique_id': '1234567890abcdef#server_php_version', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_php_version-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local PHP version', + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_php_version', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '8.2.18', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_sma_available_memory-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_sma_available_memory', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 1, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'SMA available memory', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_sma_avail_mem', + 'unique_id': '1234567890abcdef#sma_avail_mem', + 'unit_of_measurement': , + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_sma_available_memory-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'data_size', + 'friendly_name': 'my.nc_url.local SMA available memory', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_sma_available_memory', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '33.342368', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_sma_number_of_segments-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_sma_number_of_segments', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'SMA number of segments', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_sma_num_seg', + 'unique_id': '1234567890abcdef#sma_num_seg', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_sma_number_of_segments-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local SMA number of segments', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_sma_number_of_segments', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '1', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_sma_segment_size-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_sma_segment_size', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 1, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'SMA segment size', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_sma_seg_size', + 'unique_id': '1234567890abcdef#sma_seg_size', + 'unit_of_measurement': , + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_sma_segment_size-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'data_size', + 'friendly_name': 'my.nc_url.local SMA segment size', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_sma_segment_size', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '33.554312', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_system_memcache_distributed-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_system_memcache_distributed', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'System memcache distributed', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_system_memcache_distributed', + 'unique_id': '1234567890abcdef#system_memcache.distributed', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_system_memcache_distributed-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local System memcache distributed', + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_system_memcache_distributed', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'none', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_system_memcache_local-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_system_memcache_local', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'System memcache local', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_system_memcache_local', + 'unique_id': '1234567890abcdef#system_memcache.local', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_system_memcache_local-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local System memcache local', + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_system_memcache_local', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '\\OC\\Memcache\\APCu', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_system_memcache_locking-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_system_memcache_locking', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'System memcache locking', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_system_memcache_locking', + 'unique_id': '1234567890abcdef#system_memcache.locking', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_system_memcache_locking-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local System memcache locking', + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_system_memcache_locking', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'none', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_system_theme-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.my_nc_url_local_system_theme', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'System theme', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_system_theme', + 'unique_id': '1234567890abcdef#system_theme', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_system_theme-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local System theme', + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_system_theme', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_system_version-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.my_nc_url_local_system_version', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'System version', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_system_version', + 'unique_id': '1234567890abcdef#system_version', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_system_version-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local System version', + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_system_version', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '28.0.4.1', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_total_memory-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.my_nc_url_local_total_memory', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 2, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Total memory', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_system_mem_total', + 'unique_id': '1234567890abcdef#system_mem_total', + 'unit_of_measurement': , + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_total_memory-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'data_size', + 'friendly_name': 'my.nc_url.local Total memory', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_total_memory', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '30.728192', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_total_swap_memory-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.my_nc_url_local_total_swap_memory', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 2, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Total swap memory', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_system_swap_total', + 'unique_id': '1234567890abcdef#system_swap_total', + 'unit_of_measurement': , + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_total_swap_memory-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'data_size', + 'friendly_name': 'my.nc_url.local Total swap memory', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_total_swap_memory', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '10.484736', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_updates_available-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.my_nc_url_local_updates_available', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Updates available', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_system_apps_num_updates_available', + 'unique_id': '1234567890abcdef#system_apps_num_updates_available', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_updates_available-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Updates available', + 'state_class': , + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_updates_available', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0', + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_webserver-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.my_nc_url_local_webserver', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Webserver', + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'nextcloud_server_webserver', + 'unique_id': '1234567890abcdef#server_webserver', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[sensor.my_nc_url_local_webserver-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'my.nc_url.local Webserver', + }), + 'context': , + 'entity_id': 'sensor.my_nc_url_local_webserver', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'Apache/2.4.57 (Debian)', + }) +# --- diff --git a/tests/components/nextcloud/snapshots/test_update.ambr b/tests/components/nextcloud/snapshots/test_update.ambr new file mode 100644 index 00000000000000..1ee6264c204bf9 --- /dev/null +++ b/tests/components/nextcloud/snapshots/test_update.ambr @@ -0,0 +1,57 @@ +# serializer version: 1 +# name: test_async_setup_entry[update.my_nc_url_local_none-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'update', + 'entity_category': , + 'entity_id': 'update.my_nc_url_local_none', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': None, + 'platform': 'nextcloud', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '1234567890abcdef#update', + 'unit_of_measurement': None, + }) +# --- +# name: test_async_setup_entry[update.my_nc_url_local_none-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'auto_update': False, + 'entity_picture': 'https://brands.home-assistant.io/_/nextcloud/icon.png', + 'friendly_name': 'my.nc_url.local None', + 'in_progress': False, + 'installed_version': '28.0.4.1', + 'latest_version': '28.0.4.1', + 'release_summary': None, + 'release_url': 'https://nextcloud.com/changelog/#28-0-4', + 'skipped_version': None, + 'supported_features': , + 'title': None, + }), + 'context': , + 'entity_id': 'update.my_nc_url_local_none', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- diff --git a/tests/components/nextcloud/test_binary_sensor.py b/tests/components/nextcloud/test_binary_sensor.py new file mode 100644 index 00000000000000..ff121c53ec39f3 --- /dev/null +++ b/tests/components/nextcloud/test_binary_sensor.py @@ -0,0 +1,33 @@ +"""Tests for the Nextcloud binary sensors.""" + +from unittest.mock import patch + +import pytest +from syrupy.assertion import SnapshotAssertion + +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from . import init_integration +from .const import NC_DATA, VALID_CONFIG + +from tests.common import snapshot_platform + + +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_async_setup_entry( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + snapshot: SnapshotAssertion, +) -> None: + """Test a successful setup entry.""" + with patch( + "homeassistant.components.nextcloud.PLATFORMS", [Platform.BINARY_SENSOR] + ): + entry = await init_integration(hass, VALID_CONFIG, NC_DATA) + + states = hass.states.async_all() + assert len(states) == 6 + + await snapshot_platform(hass, entity_registry, snapshot, entry.entry_id) diff --git a/tests/components/nextcloud/test_config_flow.py b/tests/components/nextcloud/test_config_flow.py index c02516fdc99cbb..16b6bf3bc0469e 100644 --- a/tests/components/nextcloud/test_config_flow.py +++ b/tests/components/nextcloud/test_config_flow.py @@ -1,6 +1,6 @@ """Tests for the Nextcloud config flow.""" -from unittest.mock import Mock, patch +from unittest.mock import patch from nextcloudmonitor import ( NextcloudMonitorAuthorizationError, @@ -11,25 +11,20 @@ from syrupy.assertion import SnapshotAssertion from homeassistant.components.nextcloud.const import DOMAIN -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER -from homeassistant.const import CONF_PASSWORD, CONF_URL, CONF_USERNAME, CONF_VERIFY_SSL +from homeassistant.config_entries import SOURCE_USER +from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType +from .const import VALID_CONFIG + from tests.common import MockConfigEntry pytestmark = pytest.mark.usefixtures("mock_setup_entry") -VALID_CONFIG = { - CONF_URL: "nc_url", - CONF_USERNAME: "nc_user", - CONF_PASSWORD: "nc_pass", - CONF_VERIFY_SSL: True, -} - async def test_user_create_entry( - hass: HomeAssistant, mock_nextcloud_monitor: Mock, snapshot: SnapshotAssertion + hass: HomeAssistant, snapshot: SnapshotAssertion ) -> None: """Test that the user step works.""" # start user flow @@ -85,7 +80,7 @@ async def test_user_create_entry( # test success with patch( "homeassistant.components.nextcloud.config_flow.NextcloudMonitor", - return_value=mock_nextcloud_monitor, + return_value=True, ): result = await hass.config_entries.flow.async_configure( result["flow_id"], @@ -94,17 +89,15 @@ async def test_user_create_entry( await hass.async_block_till_done() assert result["type"] is FlowResultType.CREATE_ENTRY - assert result["title"] == "nc_url" + assert result["title"] == "https://my.nc_url.local" assert result["data"] == snapshot -async def test_user_already_configured( - hass: HomeAssistant, mock_nextcloud_monitor: Mock -) -> None: +async def test_user_already_configured(hass: HomeAssistant) -> None: """Test that errors are shown when duplicates are added.""" entry = MockConfigEntry( domain=DOMAIN, - title="nc_url", + title="https://my.nc_url.local", unique_id="nc_url", data=VALID_CONFIG, ) @@ -119,7 +112,7 @@ async def test_user_already_configured( with patch( "homeassistant.components.nextcloud.config_flow.NextcloudMonitor", - return_value=mock_nextcloud_monitor, + return_value=True, ): result = await hass.config_entries.flow.async_configure( result["flow_id"], @@ -131,24 +124,18 @@ async def test_user_already_configured( assert result["reason"] == "already_configured" -async def test_reauth( - hass: HomeAssistant, mock_nextcloud_monitor: Mock, snapshot: SnapshotAssertion -) -> None: +async def test_reauth(hass: HomeAssistant, snapshot: SnapshotAssertion) -> None: """Test that the re-auth flow works.""" entry = MockConfigEntry( domain=DOMAIN, - title="nc_url", + title="https://my.nc_url.local", unique_id="nc_url", data=VALID_CONFIG, ) entry.add_to_hass(hass) # start reauth flow - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH, "entry_id": entry.entry_id}, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -206,7 +193,7 @@ async def test_reauth( # test success with patch( "homeassistant.components.nextcloud.config_flow.NextcloudMonitor", - return_value=mock_nextcloud_monitor, + return_value=True, ): result = await hass.config_entries.flow.async_configure( result["flow_id"], diff --git a/tests/components/nextcloud/test_coordinator.py b/tests/components/nextcloud/test_coordinator.py new file mode 100644 index 00000000000000..91f7e7967a3c18 --- /dev/null +++ b/tests/components/nextcloud/test_coordinator.py @@ -0,0 +1,69 @@ +"""Tests for the Nextcloud coordinator.""" + +from unittest.mock import Mock, patch + +from freezegun.api import FrozenDateTimeFactory +from nextcloudmonitor import ( + NextcloudMonitor, + NextcloudMonitorAuthorizationError, + NextcloudMonitorConnectionError, + NextcloudMonitorError, + NextcloudMonitorRequestError, +) +import pytest + +from homeassistant.components.nextcloud.const import DEFAULT_SCAN_INTERVAL +from homeassistant.const import STATE_UNAVAILABLE +from homeassistant.core import HomeAssistant + +from . import mock_config_entry +from .const import NC_DATA, VALID_CONFIG + +from tests.common import async_fire_time_changed + + +@pytest.mark.parametrize( + ("error"), + [ + (NextcloudMonitorAuthorizationError), + (NextcloudMonitorConnectionError), + (NextcloudMonitorRequestError), + ], +) +async def test_data_update( + hass: HomeAssistant, freezer: FrozenDateTimeFactory, error: NextcloudMonitorError +) -> None: + """Test a coordinator data updates.""" + entry = mock_config_entry(VALID_CONFIG) + entry.add_to_hass(hass) + + with ( + patch( + "homeassistant.components.nextcloud.NextcloudMonitor", spec=NextcloudMonitor + ) as mock_nextcloud_monitor, + ): + mock_nextcloud_monitor.return_value.update = Mock( + return_value=True, + side_effect=[None, error, None], + ) + mock_nextcloud_monitor.return_value.data = NC_DATA + assert await hass.config_entries.async_setup(entry.entry_id) + + # Test successful setup and first data fetch + await hass.async_block_till_done(wait_background_tasks=True) + states = hass.states.async_all() + assert (state != STATE_UNAVAILABLE for state in states) + + # Test states get unavailable on error + freezer.tick(DEFAULT_SCAN_INTERVAL) + async_fire_time_changed(hass) + await hass.async_block_till_done(wait_background_tasks=True) + states = hass.states.async_all() + assert (state == STATE_UNAVAILABLE for state in states) + + # Test successful data fetch + freezer.tick(DEFAULT_SCAN_INTERVAL) + async_fire_time_changed(hass) + await hass.async_block_till_done(wait_background_tasks=True) + states = hass.states.async_all() + assert (state != STATE_UNAVAILABLE for state in states) diff --git a/tests/components/nextcloud/test_init.py b/tests/components/nextcloud/test_init.py new file mode 100644 index 00000000000000..70c8f545c6b95b --- /dev/null +++ b/tests/components/nextcloud/test_init.py @@ -0,0 +1,95 @@ +"""Tests for the Nextcloud init.""" + +from unittest.mock import Mock, patch + +from nextcloudmonitor import ( + NextcloudMonitorAuthorizationError, + NextcloudMonitorConnectionError, + NextcloudMonitorError, + NextcloudMonitorRequestError, +) +import pytest + +from homeassistant.components.nextcloud.const import DOMAIN +from homeassistant.config_entries import ConfigEntryState +from homeassistant.const import CONF_URL, Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from . import init_integration, mock_config_entry +from .const import MOCKED_ENTRY_ID, NC_DATA, VALID_CONFIG + + +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_async_setup_entry( + hass: HomeAssistant, +) -> None: + """Test a successful setup entry.""" + assert await init_integration(hass, VALID_CONFIG, NC_DATA) + + +async def test_unique_id_migration( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, +) -> None: + """Test migration of unique ids to stable ones.""" + + object_id = "my_nc_url_local_system_version" + entity_id = f"{Platform.SENSOR}.{object_id}" + + entry = mock_config_entry(VALID_CONFIG) + entry.add_to_hass(hass) + + entity = entity_registry.async_get_or_create( + Platform.SENSOR, + DOMAIN, + f"{VALID_CONFIG[CONF_URL]}#nextcloud_system_version", + suggested_object_id=object_id, + config_entry=entry, + ) + + # test old unique id + assert entity.entity_id == entity_id + assert entity.unique_id == f"{VALID_CONFIG[CONF_URL]}#nextcloud_system_version" + + with ( + patch( + "homeassistant.components.nextcloud.NextcloudMonitor" + ) as mock_nextcloud_monitor, + ): + mock_nextcloud_monitor.update = Mock(return_value=True) + mock_nextcloud_monitor.return_value.data = NC_DATA + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + # test migrated unique id + reg_entry = entity_registry.async_get(entity_id) + assert reg_entry.unique_id == f"{MOCKED_ENTRY_ID}#system_version" + + +@pytest.mark.parametrize( + ("exception", "expcted_entry_state"), + [ + (NextcloudMonitorAuthorizationError, ConfigEntryState.SETUP_ERROR), + (NextcloudMonitorConnectionError, ConfigEntryState.SETUP_RETRY), + (NextcloudMonitorRequestError, ConfigEntryState.SETUP_RETRY), + ], +) +async def test_setup_entry_errors( + hass: HomeAssistant, + exception: NextcloudMonitorError, + expcted_entry_state: ConfigEntryState, +) -> None: + """Test a successful setup entry.""" + + entry = mock_config_entry(VALID_CONFIG) + entry.add_to_hass(hass) + + with ( + patch( + "homeassistant.components.nextcloud.NextcloudMonitor", side_effect=exception + ), + ): + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + assert entry.state == expcted_entry_state diff --git a/tests/components/nextcloud/test_sensor.py b/tests/components/nextcloud/test_sensor.py new file mode 100644 index 00000000000000..1ea2c87db114bd --- /dev/null +++ b/tests/components/nextcloud/test_sensor.py @@ -0,0 +1,31 @@ +"""Tests for the Nextcloud sensors.""" + +from unittest.mock import patch + +import pytest +from syrupy.assertion import SnapshotAssertion + +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from . import init_integration +from .const import NC_DATA, VALID_CONFIG + +from tests.common import snapshot_platform + + +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_async_setup_entry( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + snapshot: SnapshotAssertion, +) -> None: + """Test a successful setup entry.""" + with patch("homeassistant.components.nextcloud.PLATFORMS", [Platform.SENSOR]): + entry = await init_integration(hass, VALID_CONFIG, NC_DATA) + + states = hass.states.async_all() + assert len(states) == 80 + + await snapshot_platform(hass, entity_registry, snapshot, entry.entry_id) diff --git a/tests/components/nextcloud/test_update.py b/tests/components/nextcloud/test_update.py new file mode 100644 index 00000000000000..d47c9f1df530cb --- /dev/null +++ b/tests/components/nextcloud/test_update.py @@ -0,0 +1,80 @@ +"""Tests for the Nextcloud update entity.""" + +from copy import deepcopy +from unittest.mock import patch + +import pytest +from syrupy.assertion import SnapshotAssertion + +from homeassistant.const import STATE_OFF, STATE_ON, Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from . import init_integration +from .const import NC_DATA, VALID_CONFIG + +from tests.common import snapshot_platform + + +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_async_setup_entry( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + snapshot: SnapshotAssertion, +) -> None: + """Test a successful setup entry.""" + with patch("homeassistant.components.nextcloud.PLATFORMS", [Platform.UPDATE]): + entry = await init_integration(hass, VALID_CONFIG, NC_DATA) + + states = hass.states.async_all() + assert len(states) == 1 + + await snapshot_platform(hass, entity_registry, snapshot, entry.entry_id) + + +async def test_setup_entity_without_update( + hass: HomeAssistant, snapshot: SnapshotAssertion +) -> None: + """Test update entity is created w/o available update.""" + with patch("homeassistant.components.nextcloud.PLATFORMS", [Platform.UPDATE]): + await init_integration(hass, VALID_CONFIG, NC_DATA) + + states = hass.states.async_all() + assert len(states) == 1 + assert states[0].state == STATE_OFF + assert states[0].attributes["installed_version"] == "28.0.4.1" + assert states[0].attributes["latest_version"] == "28.0.4.1" + assert ( + states[0].attributes["release_url"] == "https://nextcloud.com/changelog/#28-0-4" + ) + + +async def test_setup_entity_with_update( + hass: HomeAssistant, snapshot: SnapshotAssertion +) -> None: + """Test update entity is created with available update.""" + data = deepcopy(NC_DATA) + data["nextcloud"]["system"]["update"]["available"] = True + data["nextcloud"]["system"]["update"]["available_version"] = "30.0.0.0" + with patch("homeassistant.components.nextcloud.PLATFORMS", [Platform.UPDATE]): + await init_integration(hass, VALID_CONFIG, data) + + states = hass.states.async_all() + assert len(states) == 1 + assert states[0].state == STATE_ON + assert states[0].attributes["installed_version"] == "28.0.4.1" + assert states[0].attributes["latest_version"] == "30.0.0.0" + assert ( + states[0].attributes["release_url"] == "https://nextcloud.com/changelog/#30-0-0" + ) + + +async def test_setup_no_entity(hass: HomeAssistant) -> None: + """Test no update entity is created, when no data available.""" + data = deepcopy(NC_DATA) + data["nextcloud"]["system"].pop("update") # only nc<28.0.0 + with patch("homeassistant.components.nextcloud.PLATFORMS", [Platform.UPDATE]): + await init_integration(hass, VALID_CONFIG, data) + + states = hass.states.async_all() + assert len(states) == 0 diff --git a/tests/components/nextdns/test_config_flow.py b/tests/components/nextdns/test_config_flow.py index 7571eef347ec70..27a6cf1e7e0736 100644 --- a/tests/components/nextdns/test_config_flow.py +++ b/tests/components/nextdns/test_config_flow.py @@ -12,7 +12,7 @@ from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType -from . import PROFILES, init_integration +from . import PROFILES, init_integration, mock_nextdns async def test_form_create_entry(hass: HomeAssistant) -> None: @@ -101,3 +101,60 @@ async def test_form_already_configured(hass: HomeAssistant) -> None: assert result["type"] is FlowResultType.ABORT assert result["reason"] == "already_configured" + + +async def test_reauth_successful(hass: HomeAssistant) -> None: + """Test starting a reauthentication flow.""" + entry = await init_integration(hass) + + result = await entry.start_reauth_flow(hass) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reauth_confirm" + + with ( + patch( + "homeassistant.components.nextdns.NextDns.get_profiles", + return_value=PROFILES, + ), + mock_nextdns(), + ): + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={CONF_API_KEY: "new_api_key"}, + ) + await hass.async_block_till_done() + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "reauth_successful" + + +@pytest.mark.parametrize( + ("exc", "base_error"), + [ + (ApiError("API Error"), "cannot_connect"), + (InvalidApiKeyError, "invalid_api_key"), + (RetryError("Retry Error"), "cannot_connect"), + (TimeoutError, "cannot_connect"), + (ValueError, "unknown"), + ], +) +async def test_reauth_errors( + hass: HomeAssistant, exc: Exception, base_error: str +) -> None: + """Test reauthentication flow with errors.""" + entry = await init_integration(hass) + + result = await entry.start_reauth_flow(hass) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reauth_confirm" + + with patch( + "homeassistant.components.nextdns.NextDns.get_profiles", side_effect=exc + ): + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={CONF_API_KEY: "new_api_key"}, + ) + await hass.async_block_till_done() + + assert result["errors"] == {"base": base_error} diff --git a/tests/components/nextdns/test_coordinator.py b/tests/components/nextdns/test_coordinator.py new file mode 100644 index 00000000000000..9613a6b423fabd --- /dev/null +++ b/tests/components/nextdns/test_coordinator.py @@ -0,0 +1,46 @@ +"""Tests for NextDNS coordinator.""" + +from datetime import timedelta +from unittest.mock import patch + +from freezegun.api import FrozenDateTimeFactory +from nextdns import InvalidApiKeyError + +from homeassistant.components.nextdns.const import DOMAIN +from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState +from homeassistant.core import HomeAssistant + +from . import init_integration + +from tests.common import async_fire_time_changed + + +async def test_auth_error( + hass: HomeAssistant, + freezer: FrozenDateTimeFactory, +) -> None: + """Test authentication error when polling data.""" + entry = await init_integration(hass) + + assert entry.state is ConfigEntryState.LOADED + + freezer.tick(timedelta(minutes=10)) + with patch( + "homeassistant.components.nextdns.NextDns.connection_status", + side_effect=InvalidApiKeyError, + ): + async_fire_time_changed(hass) + await hass.async_block_till_done() + + assert entry.state is ConfigEntryState.LOADED + + flows = hass.config_entries.flow.async_progress() + assert len(flows) == 1 + + flow = flows[0] + assert flow.get("step_id") == "reauth_confirm" + assert flow.get("handler") == DOMAIN + + assert "context" in flow + assert flow["context"].get("source") == SOURCE_REAUTH + assert flow["context"].get("entry_id") == entry.entry_id diff --git a/tests/components/nextdns/test_init.py b/tests/components/nextdns/test_init.py index 61a487d917c651..0a0bf3fc487cea 100644 --- a/tests/components/nextdns/test_init.py +++ b/tests/components/nextdns/test_init.py @@ -2,12 +2,12 @@ from unittest.mock import patch -from nextdns import ApiError +from nextdns import ApiError, InvalidApiKeyError import pytest from tenacity import RetryError from homeassistant.components.nextdns.const import CONF_PROFILE_ID, DOMAIN -from homeassistant.config_entries import ConfigEntryState +from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState from homeassistant.const import CONF_API_KEY, STATE_UNAVAILABLE from homeassistant.core import HomeAssistant @@ -59,3 +59,33 @@ async def test_unload_entry(hass: HomeAssistant) -> None: assert entry.state is ConfigEntryState.NOT_LOADED assert not hass.data.get(DOMAIN) + + +async def test_config_auth_failed(hass: HomeAssistant) -> None: + """Test for setup failure if the auth fails.""" + entry = MockConfigEntry( + domain=DOMAIN, + title="Fake Profile", + unique_id="xyz12", + data={CONF_API_KEY: "fake_api_key", CONF_PROFILE_ID: "xyz12"}, + ) + entry.add_to_hass(hass) + + with patch( + "homeassistant.components.nextdns.NextDns.get_profiles", + side_effect=InvalidApiKeyError, + ): + await hass.config_entries.async_setup(entry.entry_id) + + assert entry.state is ConfigEntryState.SETUP_ERROR + + flows = hass.config_entries.flow.async_progress() + assert len(flows) == 1 + + flow = flows[0] + assert flow.get("step_id") == "reauth_confirm" + assert flow.get("handler") == DOMAIN + + assert "context" in flow + assert flow["context"].get("source") == SOURCE_REAUTH + assert flow["context"].get("entry_id") == entry.entry_id diff --git a/tests/components/nice_go/conftest.py b/tests/components/nice_go/conftest.py index 31b21083c05208..9ed3d0d19cf755 100644 --- a/tests/components/nice_go/conftest.py +++ b/tests/components/nice_go/conftest.py @@ -1,6 +1,7 @@ """Common fixtures for the Nice G.O. tests.""" from collections.abc import Generator +from datetime import datetime from unittest.mock import AsyncMock, patch from nice_go import Barrier, BarrierState, ConnectionState @@ -71,7 +72,7 @@ def mock_config_entry() -> MockConfigEntry: CONF_EMAIL: "test-email", CONF_PASSWORD: "test-password", CONF_REFRESH_TOKEN: "test-refresh-token", - CONF_REFRESH_TOKEN_CREATION_TIME: 1722184160.738171, + CONF_REFRESH_TOKEN_CREATION_TIME: datetime.now().timestamp(), }, version=1, unique_id="test-email", diff --git a/tests/components/nice_go/snapshots/test_cover.ambr b/tests/components/nice_go/snapshots/test_cover.ambr index 8f85fea27264fb..391d91584bfa68 100644 --- a/tests/components/nice_go/snapshots/test_cover.ambr +++ b/tests/components/nice_go/snapshots/test_cover.ambr @@ -28,7 +28,7 @@ 'previous_unique_id': None, 'supported_features': , 'translation_key': None, - 'unique_id': '1-cover', + 'unique_id': '1', 'unit_of_measurement': None, }) # --- @@ -76,7 +76,7 @@ 'previous_unique_id': None, 'supported_features': , 'translation_key': None, - 'unique_id': '2-cover', + 'unique_id': '2', 'unit_of_measurement': None, }) # --- diff --git a/tests/components/nice_go/snapshots/test_diagnostics.ambr b/tests/components/nice_go/snapshots/test_diagnostics.ambr index abd3b3103d1d71..6f9428ed2462b6 100644 --- a/tests/components/nice_go/snapshots/test_diagnostics.ambr +++ b/tests/components/nice_go/snapshots/test_diagnostics.ambr @@ -26,7 +26,6 @@ 'email': '**REDACTED**', 'password': '**REDACTED**', 'refresh_token': '**REDACTED**', - 'refresh_token_creation_time': 1722184160.738171, }), 'disabled_by': None, 'domain': 'nice_go', diff --git a/tests/components/nice_go/snapshots/test_light.ambr b/tests/components/nice_go/snapshots/test_light.ambr index 294488e3d46c22..2e29d9589dd987 100644 --- a/tests/components/nice_go/snapshots/test_light.ambr +++ b/tests/components/nice_go/snapshots/test_light.ambr @@ -32,7 +32,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'light', - 'unique_id': '1-light', + 'unique_id': '1', 'unit_of_measurement': None, }) # --- @@ -87,7 +87,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'light', - 'unique_id': '2-light', + 'unique_id': '2', 'unit_of_measurement': None, }) # --- diff --git a/tests/components/nice_go/test_diagnostics.py b/tests/components/nice_go/test_diagnostics.py index 1c88c6a8dc6a42..f91f5748792032 100644 --- a/tests/components/nice_go/test_diagnostics.py +++ b/tests/components/nice_go/test_diagnostics.py @@ -26,4 +26,6 @@ async def test_entry_diagnostics( result = await get_diagnostics_for_config_entry( hass, hass_client, mock_config_entry ) - assert result == snapshot(exclude=props("created_at", "modified_at")) + assert result == snapshot( + exclude=props("created_at", "modified_at", "refresh_token_creation_time") + ) diff --git a/tests/components/nice_go/test_init.py b/tests/components/nice_go/test_init.py index 249622d23b0ecb..5568a7ea62aa36 100644 --- a/tests/components/nice_go/test_init.py +++ b/tests/components/nice_go/test_init.py @@ -110,7 +110,7 @@ async def test_update_refresh_token( assert mock_nice_go.authenticate.call_count == 0 mock_nice_go.authenticate.return_value = "new-refresh-token" - freezer.tick(timedelta(days=30)) + freezer.tick(timedelta(days=30, seconds=1)) async_fire_time_changed(hass) assert await hass.config_entries.async_reload(mock_config_entry.entry_id) await hass.async_block_till_done() diff --git a/tests/components/nina/test_config_flow.py b/tests/components/nina/test_config_flow.py index 23ee8cbf7973aa..6bc17cdf674942 100644 --- a/tests/components/nina/test_config_flow.py +++ b/tests/components/nina/test_config_flow.py @@ -188,7 +188,7 @@ async def test_options_flow_init(hass: HomeAssistant) -> None: ) assert result["type"] is FlowResultType.CREATE_ENTRY - assert result["data"] is None + assert result["data"] == {} assert dict(config_entry.data) == { CONF_HEADLINE_FILTER: deepcopy(DUMMY_DATA[CONF_HEADLINE_FILTER]), diff --git a/tests/components/notion/test_config_flow.py b/tests/components/notion/test_config_flow.py index 2cc5e3f04b7ae6..15c211c19cb35a 100644 --- a/tests/components/notion/test_config_flow.py +++ b/tests/components/notion/test_config_flow.py @@ -6,13 +6,15 @@ import pytest from homeassistant.components.notion import CONF_REFRESH_TOKEN, CONF_USER_UUID, DOMAIN -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_USER from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType from .conftest import TEST_PASSWORD, TEST_REFRESH_TOKEN, TEST_USER_UUID, TEST_USERNAME +from tests.common import MockConfigEntry + pytestmark = pytest.mark.usefixtures("mock_setup_entry") @@ -90,21 +92,13 @@ async def test_duplicate_error(hass: HomeAssistant, config, config_entry) -> Non async def test_reauth( hass: HomeAssistant, config, - config_entry, + config_entry: MockConfigEntry, errors, get_client_with_exception, mock_aionotion, ) -> None: """Test that re-auth works.""" - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "entry_id": config_entry.entry_id, - "unique_id": config_entry.unique_id, - }, - data=config, - ) + result = await config_entry.start_reauth_flow(hass) assert result["step_id"] == "reauth_confirm" # Test errors that can arise when getting a Notion API client: diff --git a/tests/components/nuki/test_config_flow.py b/tests/components/nuki/test_config_flow.py index cdd429c40c542c..d4ddc261f1e7c9 100644 --- a/tests/components/nuki/test_config_flow.py +++ b/tests/components/nuki/test_config_flow.py @@ -210,9 +210,7 @@ async def test_reauth_success(hass: HomeAssistant) -> None: """Test starting a reauthentication flow.""" entry = await setup_nuki_integration(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_REAUTH}, data=entry.data - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -241,9 +239,7 @@ async def test_reauth_invalid_auth(hass: HomeAssistant) -> None: """Test starting a reauthentication flow with invalid auth.""" entry = await setup_nuki_integration(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_REAUTH}, data=entry.data - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -265,9 +261,7 @@ async def test_reauth_cannot_connect(hass: HomeAssistant) -> None: """Test starting a reauthentication flow with cannot connect.""" entry = await setup_nuki_integration(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_REAUTH}, data=entry.data - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -289,9 +283,7 @@ async def test_reauth_unknown_exception(hass: HomeAssistant) -> None: """Test starting a reauthentication flow with an unknown exception.""" entry = await setup_nuki_integration(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_REAUTH}, data=entry.data - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/octoprint/test_config_flow.py b/tests/components/octoprint/test_config_flow.py index 738fbea088713c..e069648671889a 100644 --- a/tests/components/octoprint/test_config_flow.py +++ b/tests/components/octoprint/test_config_flow.py @@ -580,15 +580,7 @@ async def test_reauth_form(hass: HomeAssistant) -> None: unique_id="1234", ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "entry_id": entry.entry_id, - "source": config_entries.SOURCE_REAUTH, - "unique_id": entry.unique_id, - }, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert not result["errors"] diff --git a/tests/components/ollama/conftest.py b/tests/components/ollama/conftest.py index b28b8850cd5a58..7658d1cbfab973 100644 --- a/tests/components/ollama/conftest.py +++ b/tests/components/ollama/conftest.py @@ -1,5 +1,6 @@ """Tests Ollama integration.""" +from typing import Any from unittest.mock import patch import pytest @@ -16,12 +17,20 @@ @pytest.fixture -def mock_config_entry(hass: HomeAssistant) -> MockConfigEntry: +def mock_config_entry_options() -> dict[str, Any]: + """Fixture for configuration entry options.""" + return TEST_OPTIONS + + +@pytest.fixture +def mock_config_entry( + hass: HomeAssistant, mock_config_entry_options: dict[str, Any] +) -> MockConfigEntry: """Mock a config entry.""" entry = MockConfigEntry( domain=ollama.DOMAIN, data=TEST_USER_DATA, - options=TEST_OPTIONS, + options=mock_config_entry_options, ) entry.add_to_hass(hass) return entry diff --git a/tests/components/ollama/test_config_flow.py b/tests/components/ollama/test_config_flow.py index b1b74197139027..7755f2208b43d6 100644 --- a/tests/components/ollama/test_config_flow.py +++ b/tests/components/ollama/test_config_flow.py @@ -164,13 +164,18 @@ async def test_options( ) options = await hass.config_entries.options.async_configure( options_flow["flow_id"], - {ollama.CONF_PROMPT: "test prompt", ollama.CONF_MAX_HISTORY: 100}, + { + ollama.CONF_PROMPT: "test prompt", + ollama.CONF_MAX_HISTORY: 100, + ollama.CONF_NUM_CTX: 32768, + }, ) await hass.async_block_till_done() assert options["type"] is FlowResultType.CREATE_ENTRY assert options["data"] == { ollama.CONF_PROMPT: "test prompt", ollama.CONF_MAX_HISTORY: 100, + ollama.CONF_NUM_CTX: 32768, } diff --git a/tests/components/ollama/test_conversation.py b/tests/components/ollama/test_conversation.py index cb56b3983428f8..6c34b8e005288a 100644 --- a/tests/components/ollama/test_conversation.py +++ b/tests/components/ollama/test_conversation.py @@ -482,8 +482,10 @@ async def test_message_history_unlimited( "ollama.AsyncClient.chat", return_value={"message": {"role": "assistant", "content": "test response"}}, ), - patch.object(mock_config_entry, "options", {ollama.CONF_MAX_HISTORY: 0}), ): + hass.config_entries.async_update_entry( + mock_config_entry, options={ollama.CONF_MAX_HISTORY: 0} + ) for i in range(100): result = await conversation.async_converse( hass, @@ -578,3 +580,34 @@ async def test_conversation_agent_with_assist( state.attributes[ATTR_SUPPORTED_FEATURES] == conversation.ConversationEntityFeature.CONTROL ) + + +@pytest.mark.parametrize( + ("mock_config_entry_options", "expected_options"), + [ + ({}, {"num_ctx": 8192}), + ({"num_ctx": 16384}, {"num_ctx": 16384}), + ], +) +async def test_options( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_init_component, + expected_options: dict[str, Any], +) -> None: + """Test that options are passed correctly to ollama client.""" + with patch( + "ollama.AsyncClient.chat", + return_value={"message": {"role": "assistant", "content": "test response"}}, + ) as mock_chat: + await conversation.async_converse( + hass, + "test message", + None, + Context(), + agent_id="conversation.mock_title", + ) + + assert mock_chat.call_count == 1 + args = mock_chat.call_args.kwargs + assert args.get("options") == expected_options diff --git a/tests/components/onvif/test_config_flow.py b/tests/components/onvif/test_config_flow.py index c0e5a6fe545b08..f7200aa7a00dd8 100644 --- a/tests/components/onvif/test_config_flow.py +++ b/tests/components/onvif/test_config_flow.py @@ -769,11 +769,7 @@ async def test_form_reauth(hass: HomeAssistant) -> None: """Test reauthenticate.""" entry, _, _ = await setup_onvif_integration(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_REAUTH, "entry_id": entry.entry_id}, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" assert ( diff --git a/tests/components/openexchangerates/test_config_flow.py b/tests/components/openexchangerates/test_config_flow.py index ec06c662201cc8..0d4744c057a8c8 100644 --- a/tests/components/openexchangerates/test_config_flow.py +++ b/tests/components/openexchangerates/test_config_flow.py @@ -200,16 +200,7 @@ async def test_reauth( ) -> None: """Test we can reauthenticate the config entry.""" mock_config_entry.add_to_hass(hass) - flow_context = { - "source": config_entries.SOURCE_REAUTH, - "entry_id": mock_config_entry.entry_id, - "title_placeholders": {"name": mock_config_entry.title}, - "unique_id": mock_config_entry.unique_id, - } - - result = await hass.config_entries.flow.async_init( - DOMAIN, context=flow_context, data=mock_config_entry.data - ) + result = await mock_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["errors"] is None diff --git a/tests/components/opentherm_gw/conftest.py b/tests/components/opentherm_gw/conftest.py new file mode 100644 index 00000000000000..9c90c74b04bc3c --- /dev/null +++ b/tests/components/opentherm_gw/conftest.py @@ -0,0 +1,62 @@ +"""Test configuration for opentherm_gw.""" + +from collections.abc import Generator +from unittest.mock import AsyncMock, MagicMock, patch + +from pyotgw.vars import OTGW, OTGW_ABOUT +import pytest + +from homeassistant.components.opentherm_gw import DOMAIN +from homeassistant.const import CONF_DEVICE, CONF_ID, CONF_NAME + +from tests.common import MockConfigEntry + +VERSION_TEST = "4.2.5" +MINIMAL_STATUS = {OTGW: {OTGW_ABOUT: f"OpenTherm Gateway {VERSION_TEST}"}} +MOCK_GATEWAY_ID = "mock_gateway" + + +@pytest.fixture +def mock_setup_entry() -> Generator[AsyncMock]: + """Override async_setup_entry.""" + with patch( + "homeassistant.components.opentherm_gw.async_setup_entry", + return_value=True, + ) as mock_setup_entry: + yield mock_setup_entry + + +@pytest.fixture +def mock_pyotgw() -> Generator[MagicMock]: + """Mock a pyotgw.OpenThermGateway object.""" + with ( + patch( + "homeassistant.components.opentherm_gw.OpenThermGateway", + return_value=MagicMock( + connect=AsyncMock(return_value=MINIMAL_STATUS), + set_control_setpoint=AsyncMock(), + set_max_relative_mod=AsyncMock(), + disconnect=AsyncMock(), + ), + ) as mock_gateway, + patch( + "homeassistant.components.opentherm_gw.config_flow.pyotgw.OpenThermGateway", + new=mock_gateway, + ), + ): + yield mock_gateway + + +@pytest.fixture +def mock_config_entry() -> MockConfigEntry: + """Mock an OpenTherm Gateway config entry.""" + return MockConfigEntry( + domain=DOMAIN, + title="Mock Gateway", + data={ + CONF_NAME: "Mock Gateway", + CONF_DEVICE: "/dev/null", + CONF_ID: MOCK_GATEWAY_ID, + }, + options={}, + ) diff --git a/tests/components/opentherm_gw/test_button.py b/tests/components/opentherm_gw/test_button.py new file mode 100644 index 00000000000000..b02a9d9fef0738 --- /dev/null +++ b/tests/components/opentherm_gw/test_button.py @@ -0,0 +1,50 @@ +"""Test opentherm_gw buttons.""" + +from unittest.mock import AsyncMock, MagicMock + +from pyotgw.vars import OTGW_MODE_RESET + +from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS +from homeassistant.components.opentherm_gw import DOMAIN as OPENTHERM_DOMAIN +from homeassistant.components.opentherm_gw.const import OpenThermDeviceIdentifier +from homeassistant.const import ATTR_ENTITY_ID, CONF_ID +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from .conftest import MINIMAL_STATUS + +from tests.common import MockConfigEntry + + +async def test_restart_button( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + mock_config_entry: MockConfigEntry, + mock_pyotgw: MagicMock, +) -> None: + """Test restart button.""" + + mock_pyotgw.return_value.set_mode = AsyncMock(return_value=MINIMAL_STATUS) + mock_config_entry.add_to_hass(hass) + + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + assert ( + button_entity_id := entity_registry.async_get_entity_id( + BUTTON_DOMAIN, + OPENTHERM_DOMAIN, + f"{mock_config_entry.data[CONF_ID]}-{OpenThermDeviceIdentifier.GATEWAY}-restart_button", + ) + ) is not None + + await hass.services.async_call( + BUTTON_DOMAIN, + SERVICE_PRESS, + { + ATTR_ENTITY_ID: button_entity_id, + }, + blocking=True, + ) + + mock_pyotgw.return_value.set_mode.assert_awaited_once_with(OTGW_MODE_RESET) diff --git a/tests/components/opentherm_gw/test_config_flow.py b/tests/components/opentherm_gw/test_config_flow.py index e61a87bb55e1b5..4f4a6cfce31f31 100644 --- a/tests/components/opentherm_gw/test_config_flow.py +++ b/tests/components/opentherm_gw/test_config_flow.py @@ -1,14 +1,12 @@ """Test the Opentherm Gateway config flow.""" -from unittest.mock import patch +from unittest.mock import AsyncMock, MagicMock -from pyotgw.vars import OTGW, OTGW_ABOUT from serial import SerialException from homeassistant import config_entries from homeassistant.components.opentherm_gw.const import ( CONF_FLOOR_TEMP, - CONF_PRECISION, CONF_READ_PRECISION, CONF_SET_PRECISION, CONF_TEMPORARY_OVRD_MODE, @@ -26,10 +24,12 @@ from tests.common import MockConfigEntry -MINIMAL_STATUS = {OTGW: {OTGW_ABOUT: "OpenTherm Gateway 4.2.5"}} - -async def test_form_user(hass: HomeAssistant) -> None: +async def test_form_user( + hass: HomeAssistant, + mock_pyotgw: MagicMock, + mock_setup_entry: AsyncMock, +) -> None: """Test we get the form.""" result = await hass.config_entries.flow.async_init( @@ -38,27 +38,10 @@ async def test_form_user(hass: HomeAssistant) -> None: assert result["type"] is FlowResultType.FORM assert result["errors"] == {} - with ( - patch( - "homeassistant.components.opentherm_gw.async_setup", - return_value=True, - ) as mock_setup, - patch( - "homeassistant.components.opentherm_gw.async_setup_entry", - return_value=True, - ) as mock_setup_entry, - patch( - "pyotgw.OpenThermGateway.connect", return_value=MINIMAL_STATUS - ) as mock_pyotgw_connect, - patch( - "pyotgw.OpenThermGateway.disconnect", return_value=None - ) as mock_pyotgw_disconnect, - patch("pyotgw.status.StatusManager._process_updates", return_value=None), - ): - result2 = await hass.config_entries.flow.async_configure( - result["flow_id"], {CONF_NAME: "Test Entry 1", CONF_DEVICE: "/dev/ttyUSB0"} - ) - await hass.async_block_till_done() + result2 = await hass.config_entries.flow.async_configure( + result["flow_id"], {CONF_NAME: "Test Entry 1", CONF_DEVICE: "/dev/ttyUSB0"} + ) + await hass.async_block_till_done() assert result2["type"] is FlowResultType.CREATE_ENTRY assert result2["title"] == "Test Entry 1" @@ -67,37 +50,21 @@ async def test_form_user(hass: HomeAssistant) -> None: CONF_DEVICE: "/dev/ttyUSB0", CONF_ID: "test_entry_1", } - assert len(mock_setup.mock_calls) == 1 - assert len(mock_setup_entry.mock_calls) == 1 - assert len(mock_pyotgw_connect.mock_calls) == 1 - assert len(mock_pyotgw_disconnect.mock_calls) == 1 + assert mock_pyotgw.return_value.connect.await_count == 1 + assert mock_pyotgw.return_value.disconnect.await_count == 1 -async def test_form_import(hass: HomeAssistant) -> None: +async def test_form_import( + hass: HomeAssistant, + mock_pyotgw: MagicMock, + mock_setup_entry: AsyncMock, +) -> None: """Test import from existing config.""" - - with ( - patch( - "homeassistant.components.opentherm_gw.async_setup", - return_value=True, - ) as mock_setup, - patch( - "homeassistant.components.opentherm_gw.async_setup_entry", - return_value=True, - ) as mock_setup_entry, - patch( - "pyotgw.OpenThermGateway.connect", return_value=MINIMAL_STATUS - ) as mock_pyotgw_connect, - patch( - "pyotgw.OpenThermGateway.disconnect", return_value=None - ) as mock_pyotgw_disconnect, - patch("pyotgw.status.StatusManager._process_updates", return_value=None), - ): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_IMPORT}, - data={CONF_ID: "legacy_gateway", CONF_DEVICE: "/dev/ttyUSB1"}, - ) + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": config_entries.SOURCE_IMPORT}, + data={CONF_ID: "legacy_gateway", CONF_DEVICE: "/dev/ttyUSB1"}, + ) assert result["type"] is FlowResultType.CREATE_ENTRY assert result["title"] == "legacy_gateway" @@ -106,13 +73,15 @@ async def test_form_import(hass: HomeAssistant) -> None: CONF_DEVICE: "/dev/ttyUSB1", CONF_ID: "legacy_gateway", } - assert len(mock_setup.mock_calls) == 1 - assert len(mock_setup_entry.mock_calls) == 1 - assert len(mock_pyotgw_connect.mock_calls) == 1 - assert len(mock_pyotgw_disconnect.mock_calls) == 1 + assert mock_pyotgw.return_value.connect.await_count == 1 + assert mock_pyotgw.return_value.disconnect.await_count == 1 -async def test_form_duplicate_entries(hass: HomeAssistant) -> None: +async def test_form_duplicate_entries( + hass: HomeAssistant, + mock_pyotgw: MagicMock, + mock_setup_entry: AsyncMock, +) -> None: """Test duplicate device or id errors.""" flow1 = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} @@ -124,139 +93,76 @@ async def test_form_duplicate_entries(hass: HomeAssistant) -> None: DOMAIN, context={"source": config_entries.SOURCE_USER} ) - with ( - patch( - "homeassistant.components.opentherm_gw.async_setup", - return_value=True, - ) as mock_setup, - patch( - "homeassistant.components.opentherm_gw.async_setup_entry", - return_value=True, - ) as mock_setup_entry, - patch( - "pyotgw.OpenThermGateway.connect", return_value=MINIMAL_STATUS - ) as mock_pyotgw_connect, - patch( - "pyotgw.OpenThermGateway.disconnect", return_value=None - ) as mock_pyotgw_disconnect, - patch("pyotgw.status.StatusManager._process_updates", return_value=None), - ): - result1 = await hass.config_entries.flow.async_configure( - flow1["flow_id"], {CONF_NAME: "Test Entry 1", CONF_DEVICE: "/dev/ttyUSB0"} - ) - result2 = await hass.config_entries.flow.async_configure( - flow2["flow_id"], {CONF_NAME: "Test Entry 1", CONF_DEVICE: "/dev/ttyUSB1"} - ) - result3 = await hass.config_entries.flow.async_configure( - flow3["flow_id"], {CONF_NAME: "Test Entry 2", CONF_DEVICE: "/dev/ttyUSB0"} - ) + result1 = await hass.config_entries.flow.async_configure( + flow1["flow_id"], {CONF_NAME: "Test Entry 1", CONF_DEVICE: "/dev/ttyUSB0"} + ) assert result1["type"] is FlowResultType.CREATE_ENTRY + + result2 = await hass.config_entries.flow.async_configure( + flow2["flow_id"], {CONF_NAME: "Test Entry 1", CONF_DEVICE: "/dev/ttyUSB1"} + ) assert result2["type"] is FlowResultType.FORM assert result2["errors"] == {"base": "id_exists"} + + result3 = await hass.config_entries.flow.async_configure( + flow3["flow_id"], {CONF_NAME: "Test Entry 2", CONF_DEVICE: "/dev/ttyUSB0"} + ) assert result3["type"] is FlowResultType.FORM assert result3["errors"] == {"base": "already_configured"} - assert len(mock_setup.mock_calls) == 1 - assert len(mock_setup_entry.mock_calls) == 1 - assert len(mock_pyotgw_connect.mock_calls) == 1 - assert len(mock_pyotgw_disconnect.mock_calls) == 1 + assert mock_pyotgw.return_value.connect.await_count == 1 + assert mock_pyotgw.return_value.disconnect.await_count == 1 -async def test_form_connection_timeout(hass: HomeAssistant) -> None: + +async def test_form_connection_timeout( + hass: HomeAssistant, + mock_pyotgw: MagicMock, + mock_setup_entry: AsyncMock, +) -> None: """Test we handle connection timeout.""" - result = await hass.config_entries.flow.async_init( + flow = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} ) - with ( - patch( - "pyotgw.OpenThermGateway.connect", side_effect=(TimeoutError) - ) as mock_connect, - patch("pyotgw.status.StatusManager._process_updates", return_value=None), - ): - result2 = await hass.config_entries.flow.async_configure( - result["flow_id"], - {CONF_NAME: "Test Entry 1", CONF_DEVICE: "socket://192.0.2.254:1234"}, - ) + mock_pyotgw.return_value.connect.side_effect = TimeoutError - assert result2["type"] is FlowResultType.FORM - assert result2["errors"] == {"base": "timeout_connect"} - assert len(mock_connect.mock_calls) == 1 + result = await hass.config_entries.flow.async_configure( + flow["flow_id"], + {CONF_NAME: "Test Entry 1", CONF_DEVICE: "socket://192.0.2.254:1234"}, + ) + + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {"base": "timeout_connect"} + assert mock_pyotgw.return_value.connect.await_count == 1 -async def test_form_connection_error(hass: HomeAssistant) -> None: + +async def test_form_connection_error( + hass: HomeAssistant, + mock_pyotgw: MagicMock, + mock_setup_entry: AsyncMock, +) -> None: """Test we handle serial connection error.""" - result = await hass.config_entries.flow.async_init( + flow = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} ) - with ( - patch( - "pyotgw.OpenThermGateway.connect", side_effect=(SerialException) - ) as mock_connect, - patch("pyotgw.status.StatusManager._process_updates", return_value=None), - ): - result2 = await hass.config_entries.flow.async_configure( - result["flow_id"], {CONF_NAME: "Test Entry 1", CONF_DEVICE: "/dev/ttyUSB0"} - ) + mock_pyotgw.return_value.connect.side_effect = SerialException - assert result2["type"] is FlowResultType.FORM - assert result2["errors"] == {"base": "cannot_connect"} - assert len(mock_connect.mock_calls) == 1 + result = await hass.config_entries.flow.async_configure( + flow["flow_id"], {CONF_NAME: "Test Entry 1", CONF_DEVICE: "/dev/ttyUSB0"} + ) + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {"base": "cannot_connect"} + assert mock_pyotgw.return_value.connect.await_count == 1 -async def test_options_migration(hass: HomeAssistant) -> None: - """Test migration of precision option after update.""" - entry = MockConfigEntry( - domain=DOMAIN, - title="Mock Gateway", - data={ - CONF_NAME: "Test Entry 1", - CONF_DEVICE: "/dev/ttyUSB0", - CONF_ID: "test_entry_1", - }, - options={ - CONF_FLOOR_TEMP: True, - CONF_PRECISION: PRECISION_TENTHS, - }, - ) - entry.add_to_hass(hass) - with ( - patch( - "homeassistant.components.opentherm_gw.OpenThermGatewayHub.connect_and_subscribe", - return_value=True, - ), - patch( - "homeassistant.components.opentherm_gw.async_setup", - return_value=True, - ), - patch( - "pyotgw.status.StatusManager._process_updates", - return_value=None, - ), - ): - await hass.config_entries.async_setup(entry.entry_id) - await hass.async_block_till_done() - - result = await hass.config_entries.options.async_init( - entry.entry_id, context={"source": config_entries.SOURCE_USER}, data=None - ) - - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "init" - - result = await hass.config_entries.options.async_configure( - result["flow_id"], - user_input={}, - ) - - assert result["type"] is FlowResultType.CREATE_ENTRY - assert result["data"][CONF_READ_PRECISION] == PRECISION_TENTHS - assert result["data"][CONF_SET_PRECISION] == PRECISION_TENTHS - assert result["data"][CONF_FLOOR_TEMP] is True - - -async def test_options_form(hass: HomeAssistant) -> None: +async def test_options_form( + hass: HomeAssistant, + mock_pyotgw: MagicMock, + mock_setup_entry: AsyncMock, +) -> None: """Test the options form.""" entry = MockConfigEntry( domain=DOMAIN, @@ -270,23 +176,17 @@ async def test_options_form(hass: HomeAssistant) -> None: ) entry.add_to_hass(hass) - with ( - patch("homeassistant.components.opentherm_gw.async_setup", return_value=True), - patch( - "homeassistant.components.opentherm_gw.async_setup_entry", return_value=True - ), - ): - await hass.config_entries.async_setup(entry.entry_id) - await hass.async_block_till_done() + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() - result = await hass.config_entries.options.async_init( + flow = await hass.config_entries.options.async_init( entry.entry_id, context={"source": "test"}, data=None ) - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "init" + assert flow["type"] is FlowResultType.FORM + assert flow["step_id"] == "init" result = await hass.config_entries.options.async_configure( - result["flow_id"], + flow["flow_id"], user_input={ CONF_FLOOR_TEMP: True, CONF_READ_PRECISION: PRECISION_HALVES, @@ -301,12 +201,12 @@ async def test_options_form(hass: HomeAssistant) -> None: assert result["data"][CONF_TEMPORARY_OVRD_MODE] is True assert result["data"][CONF_FLOOR_TEMP] is True - result = await hass.config_entries.options.async_init( + flow = await hass.config_entries.options.async_init( entry.entry_id, context={"source": "test"}, data=None ) result = await hass.config_entries.options.async_configure( - result["flow_id"], user_input={CONF_READ_PRECISION: 0} + flow["flow_id"], user_input={CONF_READ_PRECISION: 0} ) assert result["type"] is FlowResultType.CREATE_ENTRY @@ -315,12 +215,12 @@ async def test_options_form(hass: HomeAssistant) -> None: assert result["data"][CONF_TEMPORARY_OVRD_MODE] is True assert result["data"][CONF_FLOOR_TEMP] is True - result = await hass.config_entries.options.async_init( + flow = await hass.config_entries.options.async_init( entry.entry_id, context={"source": "test"}, data=None ) result = await hass.config_entries.options.async_configure( - result["flow_id"], + flow["flow_id"], user_input={ CONF_FLOOR_TEMP: False, CONF_READ_PRECISION: PRECISION_TENTHS, diff --git a/tests/components/opentherm_gw/test_init.py b/tests/components/opentherm_gw/test_init.py index a466f788f1a28d..4085e25c614d77 100644 --- a/tests/components/opentherm_gw/test_init.py +++ b/tests/components/opentherm_gw/test_init.py @@ -1,84 +1,150 @@ """Test Opentherm Gateway init.""" -from unittest.mock import patch +from unittest.mock import MagicMock from pyotgw.vars import OTGW, OTGW_ABOUT -import pytest -from homeassistant import setup -from homeassistant.components.opentherm_gw.const import DOMAIN -from homeassistant.const import CONF_DEVICE, CONF_ID, CONF_NAME +from homeassistant.components.opentherm_gw.const import ( + DOMAIN, + OpenThermDeviceIdentifier, +) +from homeassistant.const import CONF_ID from homeassistant.core import HomeAssistant -from homeassistant.helpers import device_registry as dr +from homeassistant.helpers import device_registry as dr, entity_registry as er + +from .conftest import MOCK_GATEWAY_ID, VERSION_TEST from tests.common import MockConfigEntry -VERSION_OLD = "4.2.5" VERSION_NEW = "4.2.8.1" -MINIMAL_STATUS = {OTGW: {OTGW_ABOUT: f"OpenTherm Gateway {VERSION_OLD}"}} MINIMAL_STATUS_UPD = {OTGW: {OTGW_ABOUT: f"OpenTherm Gateway {VERSION_NEW}"}} -MOCK_GATEWAY_ID = "mock_gateway" -MOCK_CONFIG_ENTRY = MockConfigEntry( - domain=DOMAIN, - title="Mock Gateway", - data={ - CONF_NAME: "Mock Gateway", - CONF_DEVICE: "/dev/null", - CONF_ID: MOCK_GATEWAY_ID, - }, - options={}, -) -# This tests needs to be adjusted to remove lingering tasks -@pytest.mark.parametrize("expected_lingering_tasks", [True]) async def test_device_registry_insert( - hass: HomeAssistant, device_registry: dr.DeviceRegistry + hass: HomeAssistant, + device_registry: dr.DeviceRegistry, + mock_config_entry: MockConfigEntry, + mock_pyotgw: MagicMock, ) -> None: """Test that the device registry is initialized correctly.""" - MOCK_CONFIG_ENTRY.add_to_hass(hass) - - with ( - patch( - "homeassistant.components.opentherm_gw.OpenThermGatewayHub.cleanup", - return_value=None, - ), - patch("pyotgw.OpenThermGateway.connect", return_value=MINIMAL_STATUS), - ): - await setup.async_setup_component(hass, DOMAIN, {}) + mock_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(mock_config_entry.entry_id) await hass.async_block_till_done() - gw_dev = device_registry.async_get_device(identifiers={(DOMAIN, MOCK_GATEWAY_ID)}) - assert gw_dev.sw_version == VERSION_OLD + gw_dev = device_registry.async_get_device( + identifiers={(DOMAIN, f"{MOCK_GATEWAY_ID}-{OpenThermDeviceIdentifier.GATEWAY}")} + ) + assert gw_dev is not None + assert gw_dev.sw_version == VERSION_TEST -# This tests needs to be adjusted to remove lingering tasks -@pytest.mark.parametrize("expected_lingering_tasks", [True]) async def test_device_registry_update( - hass: HomeAssistant, device_registry: dr.DeviceRegistry + hass: HomeAssistant, + device_registry: dr.DeviceRegistry, + mock_config_entry: MockConfigEntry, + mock_pyotgw: MagicMock, ) -> None: """Test that the device registry is updated correctly.""" - MOCK_CONFIG_ENTRY.add_to_hass(hass) + mock_config_entry.add_to_hass(hass) device_registry.async_get_or_create( - config_entry_id=MOCK_CONFIG_ENTRY.entry_id, - identifiers={(DOMAIN, MOCK_GATEWAY_ID)}, + config_entry_id=mock_config_entry.entry_id, + identifiers={ + (DOMAIN, f"{MOCK_GATEWAY_ID}-{OpenThermDeviceIdentifier.GATEWAY}") + }, name="Mock Gateway", manufacturer="Schelte Bron", model="OpenTherm Gateway", - sw_version=VERSION_OLD, + sw_version=VERSION_TEST, ) - with ( - patch( - "homeassistant.components.opentherm_gw.OpenThermGatewayHub.cleanup", - return_value=None, - ), - patch("pyotgw.OpenThermGateway.connect", return_value=MINIMAL_STATUS_UPD), - ): - await setup.async_setup_component(hass, DOMAIN, {}) + mock_pyotgw.return_value.connect.return_value = MINIMAL_STATUS_UPD + await hass.config_entries.async_setup(mock_config_entry.entry_id) await hass.async_block_till_done() - gw_dev = device_registry.async_get_device(identifiers={(DOMAIN, MOCK_GATEWAY_ID)}) + + gw_dev = device_registry.async_get_device( + identifiers={(DOMAIN, f"{MOCK_GATEWAY_ID}-{OpenThermDeviceIdentifier.GATEWAY}")} + ) + assert gw_dev is not None assert gw_dev.sw_version == VERSION_NEW + + +# Device migration test can be removed in 2025.4.0 +async def test_device_migration( + hass: HomeAssistant, + device_registry: dr.DeviceRegistry, + mock_config_entry: MockConfigEntry, + mock_pyotgw: MagicMock, +) -> None: + """Test that the device registry is updated correctly.""" + mock_config_entry.add_to_hass(hass) + + device_registry.async_get_or_create( + config_entry_id=mock_config_entry.entry_id, + identifiers={ + (DOMAIN, MOCK_GATEWAY_ID), + }, + name="Mock Gateway", + manufacturer="Schelte Bron", + model="OpenTherm Gateway", + sw_version=VERSION_TEST, + ) + + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + assert ( + device_registry.async_get_device(identifiers={(DOMAIN, MOCK_GATEWAY_ID)}) + is None + ) + + gw_dev = device_registry.async_get_device( + identifiers={(DOMAIN, f"{MOCK_GATEWAY_ID}-{OpenThermDeviceIdentifier.GATEWAY}")} + ) + assert gw_dev is not None + + assert ( + device_registry.async_get_device( + identifiers={ + (DOMAIN, f"{MOCK_GATEWAY_ID}-{OpenThermDeviceIdentifier.BOILER}") + } + ) + is not None + ) + + assert ( + device_registry.async_get_device( + identifiers={ + (DOMAIN, f"{MOCK_GATEWAY_ID}-{OpenThermDeviceIdentifier.THERMOSTAT}") + } + ) + is not None + ) + + +# Entity migration test can be removed in 2025.4.0 +async def test_climate_entity_migration( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + mock_config_entry: MockConfigEntry, + mock_pyotgw: MagicMock, +) -> None: + """Test that the climate entity unique_id gets migrated correctly.""" + mock_config_entry.add_to_hass(hass) + entry = entity_registry.async_get_or_create( + domain="climate", + platform="opentherm_gw", + unique_id=mock_config_entry.data[CONF_ID], + ) + + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + updated_entry = entity_registry.async_get(entry.entity_id) + assert updated_entry is not None + assert ( + updated_entry.unique_id + == f"{mock_config_entry.data[CONF_ID]}-{OpenThermDeviceIdentifier.THERMOSTAT}-thermostat_entity" + ) diff --git a/tests/components/openuv/test_config_flow.py b/tests/components/openuv/test_config_flow.py index 3d31cf53250235..182f66c887f3cb 100644 --- a/tests/components/openuv/test_config_flow.py +++ b/tests/components/openuv/test_config_flow.py @@ -7,7 +7,7 @@ import voluptuous as vol from homeassistant.components.openuv import CONF_FROM_WINDOW, CONF_TO_WINDOW, DOMAIN -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_USER from homeassistant.const import ( CONF_API_KEY, CONF_ELEVATION, @@ -19,6 +19,8 @@ from .conftest import TEST_API_KEY, TEST_ELEVATION, TEST_LATITUDE, TEST_LONGITUDE +from tests.common import MockConfigEntry + pytestmark = pytest.mark.usefixtures("mock_setup_entry") @@ -105,12 +107,10 @@ def get_schema_marker(data_schema: vol.Schema, key: str) -> vol.Marker: async def test_step_reauth( - hass: HomeAssistant, config, config_entry, setup_config_entry + hass: HomeAssistant, config, config_entry: MockConfigEntry, setup_config_entry ) -> None: """Test that the reauth step works.""" - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_REAUTH}, data=config - ) + result = await config_entry.start_reauth_flow(hass) assert result["step_id"] == "reauth_confirm" result = await hass.config_entries.flow.async_configure(result["flow_id"]) diff --git a/tests/components/osoenergy/test_config_flow.py b/tests/components/osoenergy/test_config_flow.py index d9db5888cc3573..0b7a3c30cf277e 100644 --- a/tests/components/osoenergy/test_config_flow.py +++ b/tests/components/osoenergy/test_config_flow.py @@ -65,15 +65,7 @@ async def test_reauth_flow(hass: HomeAssistant) -> None: "homeassistant.components.osoenergy.config_flow.OSOEnergy.get_user_email", return_value=None, ): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": mock_config.unique_id, - "entry_id": mock_config.entry_id, - }, - data=mock_config.data, - ) + result = await mock_config.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["errors"] == {"base": "invalid_auth"} diff --git a/tests/components/otbr/__init__.py b/tests/components/otbr/__init__.py index 2c9daa127c2832..7d52318b477f83 100644 --- a/tests/components/otbr/__init__.py +++ b/tests/components/otbr/__init__.py @@ -31,6 +31,7 @@ TEST_BORDER_AGENT_EXTENDED_ADDRESS = bytes.fromhex("AEEB2F594B570BBF") TEST_BORDER_AGENT_ID = bytes.fromhex("230C6A1AC57F6F4BE262ACF32E5EF52C") +TEST_BORDER_AGENT_ID_2 = bytes.fromhex("230C6A1AC57F6F4BE262ACF32E5EF52D") ROUTER_DISCOVERY_HASS = { "type_": "_meshcop._udp.local.", diff --git a/tests/components/otbr/conftest.py b/tests/components/otbr/conftest.py index 3811ff66ebb224..5ab3e4421830ac 100644 --- a/tests/components/otbr/conftest.py +++ b/tests/components/otbr/conftest.py @@ -77,16 +77,18 @@ async def otbr_config_entry_multipan_fixture( get_active_dataset_tlvs: AsyncMock, get_border_agent_id: AsyncMock, get_extended_address: AsyncMock, -) -> None: +) -> str: """Mock Open Thread Border Router config entry.""" config_entry = MockConfigEntry( data=CONFIG_ENTRY_DATA_MULTIPAN, domain=otbr.DOMAIN, options={}, title="Open Thread Border Router", + unique_id=TEST_BORDER_AGENT_EXTENDED_ADDRESS.hex(), ) config_entry.add_to_hass(hass) assert await hass.config_entries.async_setup(config_entry.entry_id) + return config_entry.entry_id @pytest.fixture(name="otbr_config_entry_thread") @@ -102,6 +104,7 @@ async def otbr_config_entry_thread_fixture( domain=otbr.DOMAIN, options={}, title="Open Thread Border Router", + unique_id=TEST_BORDER_AGENT_EXTENDED_ADDRESS.hex(), ) config_entry.add_to_hass(hass) assert await hass.config_entries.async_setup(config_entry.entry_id) diff --git a/tests/components/otbr/test_config_flow.py b/tests/components/otbr/test_config_flow.py index c4972bb5f836ca..edd92591b1bce9 100644 --- a/tests/components/otbr/test_config_flow.py +++ b/tests/components/otbr/test_config_flow.py @@ -13,7 +13,7 @@ from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType -from . import DATASET_CH15, DATASET_CH16 +from . import DATASET_CH15, DATASET_CH16, TEST_BORDER_AGENT_ID, TEST_BORDER_AGENT_ID_2 from tests.common import MockConfigEntry, MockModule, mock_integration from tests.test_util.aiohttp import AiohttpClientMocker @@ -57,12 +57,91 @@ def addon_info_fixture(): "http://custom_url:1234//", ], ) +@pytest.mark.usefixtures( + "get_active_dataset_tlvs", + "get_border_agent_id", +) async def test_user_flow( hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, url: str ) -> None: """Test the user flow.""" + await _finish_user_flow(hass, url) + + +@pytest.mark.usefixtures( + "get_active_dataset_tlvs", + "get_extended_address", +) +async def test_user_flow_additional_entry( + hass: HomeAssistant, aioclient_mock: AiohttpClientMocker +) -> None: + """Test more than a single entry is allowed.""" + url1 = "http://custom_url:1234" + url2 = "http://custom_url_2:1234" + aioclient_mock.get(f"{url1}/node/ba-id", json=TEST_BORDER_AGENT_ID.hex()) + aioclient_mock.get(f"{url2}/node/ba-id", json=TEST_BORDER_AGENT_ID_2.hex()) + + mock_integration(hass, MockModule("hassio")) + + # Setup a config entry + config_entry = MockConfigEntry( + data={"url": url2}, + domain=otbr.DOMAIN, + options={}, + title="Open Thread Border Router", + unique_id=TEST_BORDER_AGENT_ID_2.hex(), + ) + config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(config_entry.entry_id) + + # Do a user flow + await _finish_user_flow(hass) + + +@pytest.mark.usefixtures( + "get_active_dataset_tlvs", + "get_extended_address", +) +async def test_user_flow_additional_entry_fail_get_address( + hass: HomeAssistant, + aioclient_mock: AiohttpClientMocker, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test more than a single entry is allowed. + + This tets the behavior when we can't read the extended address from the existing + config entry. + """ + url1 = "http://custom_url:1234" + url2 = "http://custom_url_2:1234" + aioclient_mock.get(f"{url2}/node/ba-id", json=TEST_BORDER_AGENT_ID_2.hex()) + + mock_integration(hass, MockModule("hassio")) + + # Setup a config entry + config_entry = MockConfigEntry( + data={"url": url2}, + domain=otbr.DOMAIN, + options={}, + title="Open Thread Border Router", + unique_id=TEST_BORDER_AGENT_ID_2.hex(), + ) + config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(config_entry.entry_id) + + # Do a user flow + aioclient_mock.clear_requests() + aioclient_mock.get(f"{url1}/node/ba-id", json=TEST_BORDER_AGENT_ID.hex()) + aioclient_mock.get(f"{url2}/node/ba-id", status=HTTPStatus.NOT_FOUND) + await _finish_user_flow(hass) + assert f"Could not read border agent id from {url2}" in caplog.text + + +async def _finish_user_flow( + hass: HomeAssistant, url: str = "http://custom_url:1234" +) -> None: + """Finish a user flow.""" stripped_url = "http://custom_url:1234" - aioclient_mock.get(f"{stripped_url}/node/dataset/active", text="aa") result = await hass.config_entries.flow.async_init( otbr.DOMAIN, context={"source": "user"} ) @@ -88,13 +167,56 @@ async def test_user_flow( assert result["options"] == {} assert len(mock_setup_entry.mock_calls) == 1 - config_entry = hass.config_entries.async_entries(otbr.DOMAIN)[0] + config_entry = result["result"] assert config_entry.data == expected_data assert config_entry.options == {} assert config_entry.title == "Open Thread Border Router" - assert config_entry.unique_id == otbr.DOMAIN + assert config_entry.unique_id == TEST_BORDER_AGENT_ID.hex() + + +@pytest.mark.usefixtures( + "get_active_dataset_tlvs", + "get_border_agent_id", + "get_extended_address", +) +async def test_user_flow_additional_entry_same_address( + hass: HomeAssistant, aioclient_mock: AiohttpClientMocker +) -> None: + """Test more than a single entry is allowed.""" + mock_integration(hass, MockModule("hassio")) + + # Setup a config entry + config_entry = MockConfigEntry( + data={"url": "http://custom_url:1234"}, + domain=otbr.DOMAIN, + options={}, + title="Open Thread Border Router", + unique_id=TEST_BORDER_AGENT_ID.hex(), + ) + config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(config_entry.entry_id) + # Start user flow + url = "http://custom_url:1234" + aioclient_mock.get(f"{url}/node/dataset/active", text="aa") + result = await hass.config_entries.flow.async_init( + otbr.DOMAIN, context={"source": "user"} + ) + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {} + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + { + "url": url, + }, + ) + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {"base": "already_configured"} + + +@pytest.mark.usefixtures("get_border_agent_id") async def test_user_flow_router_not_setup( hass: HomeAssistant, aioclient_mock: AiohttpClientMocker ) -> None: @@ -158,10 +280,11 @@ async def test_user_flow_router_not_setup( assert config_entry.data == expected_data assert config_entry.options == {} assert config_entry.title == "Open Thread Border Router" - assert config_entry.unique_id == otbr.DOMAIN + assert config_entry.unique_id == TEST_BORDER_AGENT_ID.hex() -async def test_user_flow_404( +@pytest.mark.usefixtures("get_border_agent_id") +async def test_user_flow_get_dataset_404( hass: HomeAssistant, aioclient_mock: AiohttpClientMocker ) -> None: """Test the user flow.""" @@ -192,7 +315,30 @@ async def test_user_flow_404( aiohttp.ClientError, ], ) -async def test_user_flow_connect_error(hass: HomeAssistant, error) -> None: +async def test_user_flow_get_ba_id_connect_error( + hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, error +) -> None: + """Test the user flow.""" + await _test_user_flow_connect_error(hass, "get_border_agent_id", error) + + +@pytest.mark.usefixtures("get_border_agent_id") +@pytest.mark.parametrize( + "error", + [ + TimeoutError, + python_otbr_api.OTBRError, + aiohttp.ClientError, + ], +) +async def test_user_flow_get_dataset_connect_error( + hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, error +) -> None: + """Test the user flow.""" + await _test_user_flow_connect_error(hass, "get_active_dataset_tlvs", error) + + +async def _test_user_flow_connect_error(hass: HomeAssistant, func, error) -> None: """Test the user flow.""" result = await hass.config_entries.flow.async_init( otbr.DOMAIN, context={"source": "user"} @@ -201,7 +347,7 @@ async def test_user_flow_connect_error(hass: HomeAssistant, error) -> None: assert result["type"] is FlowResultType.FORM assert result["errors"] == {} - with patch("python_otbr_api.OTBR.get_active_dataset_tlvs", side_effect=error): + with patch(f"python_otbr_api.OTBR.{func}", side_effect=error): result = await hass.config_entries.flow.async_configure( result["flow_id"], { @@ -212,6 +358,7 @@ async def test_user_flow_connect_error(hass: HomeAssistant, error) -> None: assert result["errors"] == {"base": "cannot_connect"} +@pytest.mark.usefixtures("get_border_agent_id") async def test_hassio_discovery_flow( hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, addon_info ) -> None: @@ -244,6 +391,7 @@ async def test_hassio_discovery_flow( assert config_entry.unique_id == HASSIO_DATA.uuid +@pytest.mark.usefixtures("get_border_agent_id") async def test_hassio_discovery_flow_yellow( hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, addon_info ) -> None: @@ -301,6 +449,7 @@ async def test_hassio_discovery_flow_yellow( ), ], ) +@pytest.mark.usefixtures("get_border_agent_id") async def test_hassio_discovery_flow_sky_connect( device: str, title: str, @@ -346,6 +495,7 @@ async def test_hassio_discovery_flow_sky_connect( assert config_entry.unique_id == HASSIO_DATA.uuid +@pytest.mark.usefixtures("get_active_dataset_tlvs", "get_extended_address") async def test_hassio_discovery_flow_2x_addons( hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, addon_info ) -> None: @@ -354,6 +504,8 @@ async def test_hassio_discovery_flow_2x_addons( url2 = "http://core-silabs-multiprotocol_2:8081" aioclient_mock.get(f"{url1}/node/dataset/active", text="aa") aioclient_mock.get(f"{url2}/node/dataset/active", text="bb") + aioclient_mock.get(f"{url1}/node/ba-id", json=TEST_BORDER_AGENT_ID.hex()) + aioclient_mock.get(f"{url2}/node/ba-id", json=TEST_BORDER_AGENT_ID_2.hex()) async def _addon_info(hass: HomeAssistant, slug: str) -> dict[str, Any]: await asyncio.sleep(0) @@ -387,18 +539,107 @@ async def _addon_info(hass: HomeAssistant, slug: str) -> dict[str, Any]: addon_info.side_effect = _addon_info - with patch( - "homeassistant.components.otbr.async_setup_entry", - return_value=True, - ) as mock_setup_entry: - result1 = await hass.config_entries.flow.async_init( - otbr.DOMAIN, context={"source": "hassio"}, data=HASSIO_DATA - ) - result2 = await hass.config_entries.flow.async_init( - otbr.DOMAIN, context={"source": "hassio"}, data=HASSIO_DATA_2 - ) + result1 = await hass.config_entries.flow.async_init( + otbr.DOMAIN, context={"source": "hassio"}, data=HASSIO_DATA + ) + result2 = await hass.config_entries.flow.async_init( + otbr.DOMAIN, context={"source": "hassio"}, data=HASSIO_DATA_2 + ) - results = [result1, result2] + results = [result1, result2] + + expected_data = { + "url": f"http://{HASSIO_DATA.config['host']}:{HASSIO_DATA.config['port']}", + } + expected_data_2 = { + "url": f"http://{HASSIO_DATA_2.config['host']}:{HASSIO_DATA_2.config['port']}", + } + + assert results[0]["type"] is FlowResultType.CREATE_ENTRY + assert ( + results[0]["title"] == "Home Assistant SkyConnect (Silicon Labs Multiprotocol)" + ) + assert results[0]["data"] == expected_data + assert results[0]["options"] == {} + + assert results[1]["type"] is FlowResultType.CREATE_ENTRY + assert ( + results[1]["title"] == "Home Assistant SkyConnect (Silicon Labs Multiprotocol)" + ) + assert results[1]["data"] == expected_data_2 + assert results[1]["options"] == {} + + assert len(hass.config_entries.async_entries(otbr.DOMAIN)) == 2 + + config_entry = hass.config_entries.async_entries(otbr.DOMAIN)[0] + assert config_entry.data == expected_data + assert config_entry.options == {} + assert ( + config_entry.title == "Home Assistant SkyConnect (Silicon Labs Multiprotocol)" + ) + assert config_entry.unique_id == HASSIO_DATA.uuid + + config_entry = hass.config_entries.async_entries(otbr.DOMAIN)[1] + assert config_entry.data == expected_data_2 + assert config_entry.options == {} + assert ( + config_entry.title == "Home Assistant SkyConnect (Silicon Labs Multiprotocol)" + ) + assert config_entry.unique_id == HASSIO_DATA_2.uuid + + +@pytest.mark.usefixtures("get_active_dataset_tlvs", "get_extended_address") +async def test_hassio_discovery_flow_2x_addons_same_ext_address( + hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, addon_info +) -> None: + """Test the hassio discovery flow when the user has 2 addons with otbr support.""" + url1 = "http://core-silabs-multiprotocol:8081" + url2 = "http://core-silabs-multiprotocol_2:8081" + aioclient_mock.get(f"{url1}/node/dataset/active", text="aa") + aioclient_mock.get(f"{url2}/node/dataset/active", text="bb") + aioclient_mock.get(f"{url1}/node/ba-id", json=TEST_BORDER_AGENT_ID.hex()) + aioclient_mock.get(f"{url2}/node/ba-id", json=TEST_BORDER_AGENT_ID.hex()) + + async def _addon_info(hass: HomeAssistant, slug: str) -> dict[str, Any]: + await asyncio.sleep(0) + if slug == "otbr": + return { + "available": True, + "hostname": None, + "options": { + "device": ( + "/dev/serial/by-id/usb-Nabu_Casa_SkyConnect_v1.0_" + "9e2adbd75b8beb119fe564a0f320645d-if00-port0" + ) + }, + "state": None, + "update_available": False, + "version": None, + } + return { + "available": True, + "hostname": None, + "options": { + "device": ( + "/dev/serial/by-id/usb-Nabu_Casa_SkyConnect_v1.0_" + "9e2adbd75b8beb119fe564a0f320645d-if00-port1" + ) + }, + "state": None, + "update_available": False, + "version": None, + } + + addon_info.side_effect = _addon_info + + result1 = await hass.config_entries.flow.async_init( + otbr.DOMAIN, context={"source": "hassio"}, data=HASSIO_DATA + ) + result2 = await hass.config_entries.flow.async_init( + otbr.DOMAIN, context={"source": "hassio"}, data=HASSIO_DATA_2 + ) + + results = [result1, result2] expected_data = { "url": f"http://{HASSIO_DATA.config['host']}:{HASSIO_DATA.config['port']}", @@ -411,9 +652,8 @@ async def _addon_info(hass: HomeAssistant, slug: str) -> dict[str, Any]: assert results[0]["data"] == expected_data assert results[0]["options"] == {} assert results[1]["type"] is FlowResultType.ABORT - assert results[1]["reason"] == "single_instance_allowed" + assert results[1]["reason"] == "already_configured" assert len(hass.config_entries.async_entries(otbr.DOMAIN)) == 1 - assert len(mock_setup_entry.mock_calls) == 1 config_entry = hass.config_entries.async_entries(otbr.DOMAIN)[0] assert config_entry.data == expected_data @@ -424,6 +664,7 @@ async def _addon_info(hass: HomeAssistant, slug: str) -> dict[str, Any]: assert config_entry.unique_id == HASSIO_DATA.uuid +@pytest.mark.usefixtures("get_border_agent_id") async def test_hassio_discovery_flow_router_not_setup( hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, addon_info ) -> None: @@ -481,6 +722,7 @@ async def test_hassio_discovery_flow_router_not_setup( assert config_entry.unique_id == HASSIO_DATA.uuid +@pytest.mark.usefixtures("get_border_agent_id") async def test_hassio_discovery_flow_router_not_setup_has_preferred( hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, addon_info ) -> None: @@ -533,6 +775,7 @@ async def test_hassio_discovery_flow_router_not_setup_has_preferred( assert config_entry.unique_id == HASSIO_DATA.uuid +@pytest.mark.usefixtures("get_border_agent_id") async def test_hassio_discovery_flow_router_not_setup_has_preferred_2( hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, @@ -596,6 +839,7 @@ async def test_hassio_discovery_flow_router_not_setup_has_preferred_2( assert config_entry.unique_id == HASSIO_DATA.uuid +@pytest.mark.usefixtures("get_border_agent_id") async def test_hassio_discovery_flow_404( hass: HomeAssistant, aioclient_mock: AiohttpClientMocker ) -> None: @@ -610,6 +854,7 @@ async def test_hassio_discovery_flow_404( assert result["reason"] == "unknown" +@pytest.mark.usefixtures("get_border_agent_id") async def test_hassio_discovery_flow_new_port_missing_unique_id( hass: HomeAssistant, ) -> None: @@ -633,7 +878,7 @@ async def test_hassio_discovery_flow_new_port_missing_unique_id( ) assert result["type"] is FlowResultType.ABORT - assert result["reason"] == "single_instance_allowed" + assert result["reason"] == "already_configured" expected_data = { "url": f"http://{HASSIO_DATA.config['host']}:{HASSIO_DATA.config['port']}", @@ -642,6 +887,7 @@ async def test_hassio_discovery_flow_new_port_missing_unique_id( assert config_entry.data == expected_data +@pytest.mark.usefixtures("get_border_agent_id") async def test_hassio_discovery_flow_new_port(hass: HomeAssistant) -> None: """Test the port can be updated.""" mock_integration(hass, MockModule("hassio")) @@ -664,7 +910,7 @@ async def test_hassio_discovery_flow_new_port(hass: HomeAssistant) -> None: ) assert result["type"] is FlowResultType.ABORT - assert result["reason"] == "single_instance_allowed" + assert result["reason"] == "already_configured" expected_data = { "url": f"http://{HASSIO_DATA.config['host']}:{HASSIO_DATA.config['port']}", @@ -673,6 +919,12 @@ async def test_hassio_discovery_flow_new_port(hass: HomeAssistant) -> None: assert config_entry.data == expected_data +@pytest.mark.usefixtures( + "addon_info", + "get_active_dataset_tlvs", + "get_border_agent_id", + "get_extended_address", +) async def test_hassio_discovery_flow_new_port_other_addon(hass: HomeAssistant) -> None: """Test the port is not updated if we get data for another addon hosting OTBR.""" mock_integration(hass, MockModule("hassio")) @@ -691,22 +943,34 @@ async def test_hassio_discovery_flow_new_port_other_addon(hass: HomeAssistant) - otbr.DOMAIN, context={"source": "hassio"}, data=HASSIO_DATA ) - assert result["type"] is FlowResultType.ABORT - assert result["reason"] == "single_instance_allowed" + # Another entry will be created + assert result["type"] is FlowResultType.CREATE_ENTRY - # Make sure the data was not updated + # Make sure the data of the existing entry was not updated expected_data = { "url": f"http://openthread_border_router:{HASSIO_DATA.config['port']+1}", } - config_entry = hass.config_entries.async_entries(otbr.DOMAIN)[0] + config_entry = hass.config_entries.async_get_entry(config_entry.entry_id) assert config_entry.data == expected_data -@pytest.mark.parametrize(("source", "data"), [("hassio", HASSIO_DATA), ("user", None)]) -async def test_config_flow_single_entry( - hass: HomeAssistant, source: str, data: Any +@pytest.mark.parametrize( + ("source", "data", "expected_result"), + [ + ("hassio", HASSIO_DATA, FlowResultType.CREATE_ENTRY), + ("user", None, FlowResultType.FORM), + ], +) +@pytest.mark.usefixtures( + "addon_info", + "get_active_dataset_tlvs", + "get_border_agent_id", + "get_extended_address", +) +async def test_config_flow_additional_entry( + hass: HomeAssistant, source: str, data: Any, expected_result: FlowResultType ) -> None: - """Test only a single entry is allowed.""" + """Test more than a single entry is allowed.""" mock_integration(hass, MockModule("hassio")) # Setup the config entry @@ -719,13 +983,11 @@ async def test_config_flow_single_entry( config_entry.add_to_hass(hass) with patch( - "homeassistant.components.homeassistant_yellow.async_setup_entry", + "homeassistant.components.otbr.async_setup_entry", return_value=True, - ) as mock_setup_entry: + ): result = await hass.config_entries.flow.async_init( otbr.DOMAIN, context={"source": source}, data=data ) - assert result["type"] is FlowResultType.ABORT - assert result["reason"] == "single_instance_allowed" - mock_setup_entry.assert_not_called() + assert result["type"] is expected_result diff --git a/tests/components/otbr/test_init.py b/tests/components/otbr/test_init.py index 86bab71cbdab79..ca1cbd6483b616 100644 --- a/tests/components/otbr/test_init.py +++ b/tests/components/otbr/test_init.py @@ -11,6 +11,7 @@ from homeassistant.components import otbr, thread from homeassistant.components.thread import discovery +from homeassistant.config_entries import SOURCE_HASSIO, SOURCE_USER from homeassistant.core import HomeAssistant from homeassistant.helpers import issue_registry as ir from homeassistant.setup import async_setup_component @@ -18,7 +19,6 @@ from . import ( BASE_URL, CONFIG_ENTRY_DATA_MULTIPAN, - CONFIG_ENTRY_DATA_THREAD, DATASET_CH15, DATASET_CH16, DATASET_INSECURE_NW_KEY, @@ -71,6 +71,7 @@ async def mock_add_service_listener(type_: str, listener: Any): domain=otbr.DOMAIN, options={}, title="My OTBR", + unique_id=TEST_BORDER_AGENT_EXTENDED_ADDRESS.hex(), ) config_entry.add_to_hass(hass) @@ -138,6 +139,7 @@ async def test_import_share_radio_channel_collision( domain=otbr.DOMAIN, options={}, title="My OTBR", + unique_id=TEST_BORDER_AGENT_EXTENDED_ADDRESS.hex(), ) config_entry.add_to_hass(hass) with ( @@ -177,6 +179,7 @@ async def test_import_share_radio_no_channel_collision( domain=otbr.DOMAIN, options={}, title="My OTBR", + unique_id=TEST_BORDER_AGENT_EXTENDED_ADDRESS.hex(), ) config_entry.add_to_hass(hass) with ( @@ -214,6 +217,7 @@ async def test_import_insecure_dataset( domain=otbr.DOMAIN, options={}, title="My OTBR", + unique_id=TEST_BORDER_AGENT_EXTENDED_ADDRESS.hex(), ) config_entry.add_to_hass(hass) with ( @@ -252,6 +256,7 @@ async def test_config_entry_not_ready( domain=otbr.DOMAIN, options={}, title="My OTBR", + unique_id=TEST_BORDER_AGENT_EXTENDED_ADDRESS.hex(), ) config_entry.add_to_hass(hass) get_active_dataset_tlvs.side_effect = error @@ -268,6 +273,7 @@ async def test_border_agent_id_not_supported( domain=otbr.DOMAIN, options={}, title="My OTBR", + unique_id=TEST_BORDER_AGENT_EXTENDED_ADDRESS.hex(), ) config_entry.add_to_hass(hass) get_border_agent_id.side_effect = python_otbr_api.GetBorderAgentIdNotSupportedError @@ -281,6 +287,7 @@ async def test_config_entry_update(hass: HomeAssistant) -> None: domain=otbr.DOMAIN, options={}, title="My OTBR", + unique_id=TEST_BORDER_AGENT_EXTENDED_ADDRESS.hex(), ) config_entry.add_to_hass(hass) mock_api = MagicMock() @@ -314,25 +321,33 @@ async def test_remove_entry( await hass.config_entries.async_remove(config_entry.entry_id) -async def test_remove_extra_entries( - hass: HomeAssistant, aioclient_mock: AiohttpClientMocker +@pytest.mark.parametrize( + ("source", "unique_id", "updated_unique_id"), + [ + (SOURCE_HASSIO, None, None), + (SOURCE_HASSIO, "abcd", "abcd"), + (SOURCE_USER, None, TEST_BORDER_AGENT_ID.hex()), + (SOURCE_USER, "abcd", TEST_BORDER_AGENT_ID.hex()), + ], +) +async def test_update_unique_id( + hass: HomeAssistant, + aioclient_mock: AiohttpClientMocker, + source: str, + unique_id: str | None, + updated_unique_id: str | None, ) -> None: - """Test we remove additional config entries.""" + """Test we update the unique id if extended address has changed.""" - config_entry1 = MockConfigEntry( + config_entry = MockConfigEntry( data=CONFIG_ENTRY_DATA_MULTIPAN, domain=otbr.DOMAIN, options={}, + source=source, title="Open Thread Border Router", + unique_id=unique_id, ) - config_entry2 = MockConfigEntry( - data=CONFIG_ENTRY_DATA_THREAD, - domain=otbr.DOMAIN, - options={}, - title="Open Thread Border Router", - ) - config_entry1.add_to_hass(hass) - config_entry2.add_to_hass(hass) - assert len(hass.config_entries.async_entries(otbr.DOMAIN)) == 2 + config_entry.add_to_hass(hass) assert await async_setup_component(hass, otbr.DOMAIN, {}) - assert len(hass.config_entries.async_entries(otbr.DOMAIN)) == 1 + config_entry = hass.config_entries.async_get_entry(config_entry.entry_id) + assert config_entry.unique_id == updated_unique_id diff --git a/tests/components/otbr/test_silabs_multiprotocol.py b/tests/components/otbr/test_silabs_multiprotocol.py index e842f40ad4c596..01b1ab63f56cb5 100644 --- a/tests/components/otbr/test_silabs_multiprotocol.py +++ b/tests/components/otbr/test_silabs_multiprotocol.py @@ -5,7 +5,6 @@ import pytest from python_otbr_api import ActiveDataSet, tlv_parser -from homeassistant.components import otbr from homeassistant.components.otbr import ( silabs_multiprotocol as otbr_silabs_multiprotocol, ) @@ -127,10 +126,11 @@ async def test_async_change_channel_no_otbr(hass: HomeAssistant) -> None: async def test_async_change_channel_non_matching_url( - hass: HomeAssistant, otbr_config_entry_multipan + hass: HomeAssistant, otbr_config_entry_multipan: str ) -> None: """Test async_change_channel when otbr is not configured.""" - hass.data[otbr.DATA_OTBR].url = OTBR_NON_MULTIPAN_URL + config_entry = hass.config_entries.async_get_entry(otbr_config_entry_multipan) + config_entry.runtime_data.url = OTBR_NON_MULTIPAN_URL with patch("python_otbr_api.OTBR.set_channel") as mock_set_channel: await otbr_silabs_multiprotocol.async_change_channel(hass, 16, delay=0) mock_set_channel.assert_not_awaited() @@ -184,10 +184,11 @@ async def test_async_get_channel_no_otbr(hass: HomeAssistant) -> None: async def test_async_get_channel_non_matching_url( - hass: HomeAssistant, otbr_config_entry_multipan + hass: HomeAssistant, otbr_config_entry_multipan: str ) -> None: """Test async_change_channel when otbr is not configured.""" - hass.data[otbr.DATA_OTBR].url = OTBR_NON_MULTIPAN_URL + config_entry = hass.config_entries.async_get_entry(otbr_config_entry_multipan) + config_entry.runtime_data.url = OTBR_NON_MULTIPAN_URL with patch("python_otbr_api.OTBR.get_active_dataset") as mock_get_active_dataset: assert await otbr_silabs_multiprotocol.async_get_channel(hass) is None mock_get_active_dataset.assert_not_awaited() @@ -198,10 +199,11 @@ async def test_async_get_channel_non_matching_url( [(OTBR_MULTIPAN_URL, True), (OTBR_NON_MULTIPAN_URL, False)], ) async def test_async_using_multipan( - hass: HomeAssistant, otbr_config_entry_multipan, url: str, expected: bool + hass: HomeAssistant, otbr_config_entry_multipan: str, url: str, expected: bool ) -> None: """Test async_change_channel when otbr is not configured.""" - hass.data[otbr.DATA_OTBR].url = url + config_entry = hass.config_entries.async_get_entry(otbr_config_entry_multipan) + config_entry.runtime_data.url = url assert await otbr_silabs_multiprotocol.async_using_multipan(hass) is expected @@ -213,8 +215,9 @@ async def test_async_using_multipan_no_otbr(hass: HomeAssistant) -> None: async def test_async_using_multipan_non_matching_url( - hass: HomeAssistant, otbr_config_entry_multipan + hass: HomeAssistant, otbr_config_entry_multipan: str ) -> None: """Test async_change_channel when otbr is not configured.""" - hass.data[otbr.DATA_OTBR].url = OTBR_NON_MULTIPAN_URL + config_entry = hass.config_entries.async_get_entry(otbr_config_entry_multipan) + config_entry.runtime_data.url = OTBR_NON_MULTIPAN_URL assert await otbr_silabs_multiprotocol.async_using_multipan(hass) is False diff --git a/tests/components/otbr/test_util.py b/tests/components/otbr/test_util.py index ec325b8819e896..0ed3041bea84ce 100644 --- a/tests/components/otbr/test_util.py +++ b/tests/components/otbr/test_util.py @@ -1,6 +1,6 @@ """Test OTBR Utility functions.""" -from unittest.mock import patch +from unittest.mock import AsyncMock, patch import pytest import python_otbr_api @@ -31,24 +31,37 @@ async def test_get_allowed_channel( assert await otbr.util.get_allowed_channel(hass, OTBR_NON_MULTIPAN_URL) is None -async def test_factory_reset(hass: HomeAssistant, otbr_config_entry_multipan) -> None: +async def test_factory_reset( + hass: HomeAssistant, + otbr_config_entry_multipan: str, + get_border_agent_id: AsyncMock, +) -> None: """Test factory_reset.""" + new_ba_id = b"new_ba_id" + get_border_agent_id.return_value = new_ba_id + config_entry = hass.config_entries.async_get_entry(otbr_config_entry_multipan) + assert config_entry.unique_id != new_ba_id.hex() with ( patch("python_otbr_api.OTBR.factory_reset") as factory_reset_mock, patch( "python_otbr_api.OTBR.delete_active_dataset" ) as delete_active_dataset_mock, ): - await hass.data[otbr.DATA_OTBR].factory_reset() + await config_entry.runtime_data.factory_reset(hass) delete_active_dataset_mock.assert_not_called() factory_reset_mock.assert_called_once_with() + # Check the unique_id is updated + config_entry = hass.config_entries.async_get_entry(otbr_config_entry_multipan) + assert config_entry.unique_id == new_ba_id.hex() + async def test_factory_reset_not_supported( - hass: HomeAssistant, otbr_config_entry_multipan + hass: HomeAssistant, otbr_config_entry_multipan: str ) -> None: """Test factory_reset.""" + config_entry = hass.config_entries.async_get_entry(otbr_config_entry_multipan) with ( patch( "python_otbr_api.OTBR.factory_reset", @@ -58,16 +71,17 @@ async def test_factory_reset_not_supported( "python_otbr_api.OTBR.delete_active_dataset" ) as delete_active_dataset_mock, ): - await hass.data[otbr.DATA_OTBR].factory_reset() + await config_entry.runtime_data.factory_reset(hass) delete_active_dataset_mock.assert_called_once_with() factory_reset_mock.assert_called_once_with() async def test_factory_reset_error_1( - hass: HomeAssistant, otbr_config_entry_multipan + hass: HomeAssistant, otbr_config_entry_multipan: str ) -> None: """Test factory_reset.""" + config_entry = hass.config_entries.async_get_entry(otbr_config_entry_multipan) with ( patch( "python_otbr_api.OTBR.factory_reset", @@ -80,16 +94,17 @@ async def test_factory_reset_error_1( HomeAssistantError, ), ): - await hass.data[otbr.DATA_OTBR].factory_reset() + await config_entry.runtime_data.factory_reset(hass) delete_active_dataset_mock.assert_not_called() factory_reset_mock.assert_called_once_with() async def test_factory_reset_error_2( - hass: HomeAssistant, otbr_config_entry_multipan + hass: HomeAssistant, otbr_config_entry_multipan: str ) -> None: """Test factory_reset.""" + config_entry = hass.config_entries.async_get_entry(otbr_config_entry_multipan) with ( patch( "python_otbr_api.OTBR.factory_reset", @@ -103,7 +118,7 @@ async def test_factory_reset_error_2( HomeAssistantError, ), ): - await hass.data[otbr.DATA_OTBR].factory_reset() + await config_entry.runtime_data.factory_reset(hass) delete_active_dataset_mock.assert_called_once_with() factory_reset_mock.assert_called_once_with() diff --git a/tests/components/overkiz/test_config_flow.py b/tests/components/overkiz/test_config_flow.py index 50870ae85fea3c..cef5ef350a93b2 100644 --- a/tests/components/overkiz/test_config_flow.py +++ b/tests/components/overkiz/test_config_flow.py @@ -573,15 +573,7 @@ async def test_cloud_reauth_success(hass: HomeAssistant) -> None: ) mock_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": mock_entry.unique_id, - "entry_id": mock_entry.entry_id, - }, - data=mock_entry.data, - ) + result = await mock_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "cloud" @@ -623,15 +615,7 @@ async def test_cloud_reauth_wrong_account(hass: HomeAssistant) -> None: ) mock_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": mock_entry.unique_id, - "entry_id": mock_entry.entry_id, - }, - data=mock_entry.data, - ) + result = await mock_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "cloud" @@ -672,15 +656,7 @@ async def test_local_reauth_success(hass: HomeAssistant) -> None: ) mock_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": mock_entry.unique_id, - "entry_id": mock_entry.entry_id, - }, - data=mock_entry.data, - ) + result = await mock_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "local_or_cloud" @@ -731,15 +707,7 @@ async def test_local_reauth_wrong_account(hass: HomeAssistant) -> None: ) mock_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": mock_entry.unique_id, - "entry_id": mock_entry.entry_id, - }, - data=mock_entry.data, - ) + result = await mock_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "local_or_cloud" diff --git a/tests/components/ovo_energy/test_config_flow.py b/tests/components/ovo_energy/test_config_flow.py index 00899e745b93dd..c3f77ca5007cce 100644 --- a/tests/components/ovo_energy/test_config_flow.py +++ b/tests/components/ovo_energy/test_config_flow.py @@ -121,15 +121,15 @@ async def test_full_flow_implementation(hass: HomeAssistant) -> None: async def test_reauth_authorization_error(hass: HomeAssistant) -> None: """Test we show user form on authorization error.""" + mock_config = MockConfigEntry( + domain=DOMAIN, unique_id=UNIQUE_ID, data=FIXTURE_USER_INPUT + ) + mock_config.add_to_hass(hass) with patch( "homeassistant.components.ovo_energy.config_flow.OVOEnergy.authenticate", return_value=False, ): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_REAUTH}, - data=FIXTURE_USER_INPUT, - ) + result = await mock_config.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth" @@ -147,15 +147,15 @@ async def test_reauth_authorization_error(hass: HomeAssistant) -> None: async def test_reauth_connection_error(hass: HomeAssistant) -> None: """Test we show user form on connection error.""" + mock_config = MockConfigEntry( + domain=DOMAIN, unique_id=UNIQUE_ID, data=FIXTURE_USER_INPUT + ) + mock_config.add_to_hass(hass) with patch( "homeassistant.components.ovo_energy.config_flow.OVOEnergy.authenticate", side_effect=aiohttp.ClientError, ): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_REAUTH}, - data=FIXTURE_USER_INPUT, - ) + result = await mock_config.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth" @@ -173,20 +173,15 @@ async def test_reauth_connection_error(hass: HomeAssistant) -> None: async def test_reauth_flow(hass: HomeAssistant) -> None: """Test reauth works.""" + mock_config = MockConfigEntry( + domain=DOMAIN, unique_id=UNIQUE_ID, data=FIXTURE_USER_INPUT + ) + mock_config.add_to_hass(hass) with patch( "homeassistant.components.ovo_energy.config_flow.OVOEnergy.authenticate", return_value=False, ): - mock_config = MockConfigEntry( - domain=DOMAIN, unique_id=UNIQUE_ID, data=FIXTURE_USER_INPUT - ) - mock_config.add_to_hass(hass) - - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_REAUTH}, - data=FIXTURE_USER_INPUT, - ) + result = await mock_config.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth" diff --git a/tests/components/permobil/test_config_flow.py b/tests/components/permobil/test_config_flow.py index ea39e678459f71..4474340f811dca 100644 --- a/tests/components/permobil/test_config_flow.py +++ b/tests/components/permobil/test_config_flow.py @@ -287,6 +287,7 @@ async def test_config_flow_reauth_success( result = await hass.config_entries.flow.async_init( config_flow.DOMAIN, context={"source": "reauth", "entry_id": mock_entry.entry_id}, + data=mock_entry.data, ) assert result["type"] is FlowResultType.FORM @@ -329,6 +330,7 @@ async def test_config_flow_reauth_fail_invalid_code( result = await hass.config_entries.flow.async_init( config_flow.DOMAIN, context={"source": "reauth", "entry_id": mock_entry.entry_id}, + data=mock_entry.data, ) assert result["type"] is FlowResultType.FORM @@ -366,6 +368,7 @@ async def test_config_flow_reauth_fail_code_request( result = await hass.config_entries.flow.async_init( config_flow.DOMAIN, context={"source": "reauth", "entry_id": reauth_entry.entry_id}, + data=mock_entry.data, ) assert result["type"] is FlowResultType.ABORT diff --git a/tests/components/philips_js/test_config_flow.py b/tests/components/philips_js/test_config_flow.py index d7f539db9cf220..80d059618133d5 100644 --- a/tests/components/philips_js/test_config_flow.py +++ b/tests/components/philips_js/test_config_flow.py @@ -60,7 +60,7 @@ async def test_form(hass: HomeAssistant, mock_setup_entry) -> None: async def test_reauth( - hass: HomeAssistant, mock_setup_entry, mock_config_entry, mock_tv + hass: HomeAssistant, mock_setup_entry, mock_config_entry: MockConfigEntry, mock_tv ) -> None: """Test we get the form.""" @@ -69,15 +69,7 @@ async def test_reauth( assert await hass.config_entries.async_setup(mock_config_entry.entry_id) assert len(mock_setup_entry.mock_calls) == 1 - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": mock_config_entry.unique_id, - "entry_id": mock_config_entry.entry_id, - }, - data=mock_config_entry.data, - ) + result = await mock_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "user" diff --git a/tests/components/picnic/test_config_flow.py b/tests/components/picnic/test_config_flow.py index 9ba18dac9a9c60..8d668b28c16d9e 100644 --- a/tests/components/picnic/test_config_flow.py +++ b/tests/components/picnic/test_config_flow.py @@ -170,16 +170,15 @@ async def test_step_reauth(hass: HomeAssistant, picnic_api) -> None: # Create a mocked config entry conf = {CONF_ACCESS_TOKEN: "a3p98fsen.a39p3fap", CONF_COUNTRY_CODE: "NL"} - MockConfigEntry( + entry = MockConfigEntry( domain=DOMAIN, unique_id=picnic_api().get_user()["user_id"], data=conf, - ).add_to_hass(hass) + ) + entry.add_to_hass(hass) # Init a re-auth flow - result_init = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_REAUTH}, data=conf - ) + result_init = await entry.start_reauth_flow(hass) assert result_init["type"] is FlowResultType.FORM assert result_init["step_id"] == "user" @@ -210,16 +209,15 @@ async def test_step_reauth_failed(hass: HomeAssistant) -> None: user_id = "f29-2a6-o32n" conf = {CONF_ACCESS_TOKEN: "a3p98fsen.a39p3fap", CONF_COUNTRY_CODE: "NL"} - MockConfigEntry( + entry = MockConfigEntry( domain=DOMAIN, unique_id=user_id, data=conf, - ).add_to_hass(hass) + ) + entry.add_to_hass(hass) # Init a re-auth flow - result_init = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_REAUTH}, data=conf - ) + result_init = await entry.start_reauth_flow(hass) assert result_init["type"] is FlowResultType.FORM assert result_init["step_id"] == "user" @@ -249,16 +247,15 @@ async def test_step_reauth_different_account(hass: HomeAssistant, picnic_api) -> # Create a mocked config entry, unique_id should be different that the user id in the api response conf = {CONF_ACCESS_TOKEN: "a3p98fsen.a39p3fap", CONF_COUNTRY_CODE: "NL"} - MockConfigEntry( + entry = MockConfigEntry( domain=DOMAIN, unique_id="3fpawh-ues-af3ho", data=conf, - ).add_to_hass(hass) + ) + entry.add_to_hass(hass) # Init a re-auth flow - result_init = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_REAUTH}, data=conf - ) + result_init = await entry.start_reauth_flow(hass) assert result_init["type"] is FlowResultType.FORM assert result_init["step_id"] == "user" diff --git a/tests/components/plex/test_config_flow.py b/tests/components/plex/test_config_flow.py index 202d62d70e0bd7..c4ec108bb6bf8c 100644 --- a/tests/components/plex/test_config_flow.py +++ b/tests/components/plex/test_config_flow.py @@ -26,7 +26,6 @@ ) from homeassistant.config_entries import ( SOURCE_INTEGRATION_DISCOVERY, - SOURCE_REAUTH, SOURCE_USER, ConfigEntryState, ) @@ -744,11 +743,7 @@ async def test_reauth( """Test setup and reauthorization of a Plex token.""" entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH}, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) flow_id = result["flow_id"] with ( @@ -795,11 +790,7 @@ async def test_reauth_multiple_servers_available( entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH}, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) flow_id = result["flow_id"] diff --git a/tests/components/powerwall/test_config_flow.py b/tests/components/powerwall/test_config_flow.py index db0ef2e988432a..5074a289d191e9 100644 --- a/tests/components/powerwall/test_config_flow.py +++ b/tests/components/powerwall/test_config_flow.py @@ -336,11 +336,7 @@ async def test_form_reauth(hass: HomeAssistant) -> None: ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_REAUTH, "entry_id": entry.entry_id}, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["errors"] == {} diff --git a/tests/components/prosegur/test_config_flow.py b/tests/components/prosegur/test_config_flow.py index 9362cecc2899ea..7c3f399ee094ff 100644 --- a/tests/components/prosegur/test_config_flow.py +++ b/tests/components/prosegur/test_config_flow.py @@ -143,15 +143,7 @@ async def test_reauth_flow(hass: HomeAssistant, mock_list_contracts) -> None: ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": entry.unique_id, - "entry_id": entry.entry_id, - }, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) assert result["step_id"] == "reauth_confirm" assert result["type"] is FlowResultType.FORM assert result["errors"] == {} @@ -208,15 +200,7 @@ async def test_reauth_flow_error(hass: HomeAssistant, exception, base_error) -> ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": entry.unique_id, - "entry_id": entry.entry_id, - }, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) with patch( "homeassistant.components.prosegur.config_flow.Installation.list", diff --git a/tests/components/proximity/test_config_flow.py b/tests/components/proximity/test_config_flow.py index 3ed9f5cba27ff0..626565146d15d1 100644 --- a/tests/components/proximity/test_config_flow.py +++ b/tests/components/proximity/test_config_flow.py @@ -10,8 +10,8 @@ CONF_TRACKED_ENTITIES, DOMAIN, ) -from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER -from homeassistant.const import CONF_NAME, CONF_UNIT_OF_MEASUREMENT, CONF_ZONE +from homeassistant.config_entries import SOURCE_USER +from homeassistant.const import CONF_ZONE from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -120,42 +120,6 @@ async def test_options_flow(hass: HomeAssistant) -> None: } -async def test_import_flow(hass: HomeAssistant) -> None: - """Test import of yaml configuration.""" - with patch( - "homeassistant.components.proximity.async_setup_entry", return_value=True - ) as mock_setup_entry: - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_IMPORT}, - data={ - CONF_NAME: "home", - CONF_ZONE: "zone.home", - CONF_TRACKED_ENTITIES: ["device_tracker.test1"], - CONF_IGNORED_ZONES: ["zone.work"], - CONF_TOLERANCE: 10, - CONF_UNIT_OF_MEASUREMENT: "km", - }, - ) - - assert result["type"] is FlowResultType.CREATE_ENTRY - assert result["data"] == { - CONF_NAME: "home", - CONF_ZONE: "zone.home", - CONF_TRACKED_ENTITIES: ["device_tracker.test1"], - CONF_IGNORED_ZONES: ["zone.work"], - CONF_TOLERANCE: 10, - CONF_UNIT_OF_MEASUREMENT: "km", - } - - zone = hass.states.get("zone.home") - assert result["title"] == zone.name - - await hass.async_block_till_done() - - assert mock_setup_entry.called - - async def test_abort_duplicated_entry(hass: HomeAssistant) -> None: """Test if we abort on duplicate user input data.""" DATA = { diff --git a/tests/components/purpleair/test_config_flow.py b/tests/components/purpleair/test_config_flow.py index 2345d98b5e10bd..998cb2b7878873 100644 --- a/tests/components/purpleair/test_config_flow.py +++ b/tests/components/purpleair/test_config_flow.py @@ -6,13 +6,15 @@ import pytest from homeassistant.components.purpleair import DOMAIN -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_USER from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType from homeassistant.helpers import device_registry as dr from .conftest import TEST_API_KEY, TEST_SENSOR_INDEX1, TEST_SENSOR_INDEX2 +from tests.common import MockConfigEntry + TEST_LATITUDE = 51.5285582 TEST_LONGITUDE = -0.2416796 @@ -127,19 +129,11 @@ async def test_reauth( mock_aiopurpleair, check_api_key_errors, check_api_key_mock, - config_entry, + config_entry: MockConfigEntry, setup_config_entry, ) -> None: """Test re-auth (including errors).""" - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "entry_id": config_entry.entry_id, - "unique_id": config_entry.unique_id, - }, - data={"api_key": TEST_API_KEY}, - ) + result = await config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/pushover/test_config_flow.py b/tests/components/pushover/test_config_flow.py index 14347084288da1..58485bfb427f72 100644 --- a/tests/components/pushover/test_config_flow.py +++ b/tests/components/pushover/test_config_flow.py @@ -149,14 +149,7 @@ async def test_reauth_success(hass: HomeAssistant) -> None: ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - }, - data=MOCK_CONFIG, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -180,14 +173,7 @@ async def test_reauth_failed(hass: HomeAssistant, mock_pushover: MagicMock) -> N ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - }, - data=MOCK_CONFIG, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -223,14 +209,7 @@ async def test_reauth_with_existing_config(hass: HomeAssistant) -> None: ) entry2.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - }, - data=MOCK_CONFIG, - ) + result = await entry2.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/pvoutput/test_config_flow.py b/tests/components/pvoutput/test_config_flow.py index 20e99f8e4977d4..fc4335de00dfba 100644 --- a/tests/components/pvoutput/test_config_flow.py +++ b/tests/components/pvoutput/test_config_flow.py @@ -5,7 +5,7 @@ from pvo import PVOutputAuthenticationError, PVOutputConnectionError from homeassistant.components.pvoutput.const import CONF_SYSTEM_ID, DOMAIN -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_USER from homeassistant.const import CONF_API_KEY from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -150,15 +150,7 @@ async def test_reauth_flow( """Test the reauthentication configuration flow.""" mock_config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": mock_config_entry.unique_id, - "entry_id": mock_config_entry.entry_id, - }, - data=mock_config_entry.data, - ) + result = await mock_config_entry.start_reauth_flow(hass) assert result.get("type") is FlowResultType.FORM assert result.get("step_id") == "reauth_confirm" @@ -192,15 +184,7 @@ async def test_reauth_with_authentication_error( """ mock_config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": mock_config_entry.unique_id, - "entry_id": mock_config_entry.entry_id, - }, - data=mock_config_entry.data, - ) + result = await mock_config_entry.start_reauth_flow(hass) assert result.get("type") is FlowResultType.FORM assert result.get("step_id") == "reauth_confirm" @@ -244,15 +228,7 @@ async def test_reauth_api_error( """Test API error during reauthentication.""" mock_config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": mock_config_entry.unique_id, - "entry_id": mock_config_entry.entry_id, - }, - data=mock_config_entry.data, - ) + result = await mock_config_entry.start_reauth_flow(hass) assert result.get("type") is FlowResultType.FORM assert result.get("step_id") == "reauth_confirm" diff --git a/tests/components/pyload/test_config_flow.py b/tests/components/pyload/test_config_flow.py index 8c7754123717f3..b4ff63e79f92eb 100644 --- a/tests/components/pyload/test_config_flow.py +++ b/tests/components/pyload/test_config_flow.py @@ -6,12 +6,7 @@ import pytest from homeassistant.components.pyload.const import DEFAULT_NAME, DOMAIN -from homeassistant.config_entries import ( - SOURCE_IMPORT, - SOURCE_REAUTH, - SOURCE_RECONFIGURE, - SOURCE_USER, -) +from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_RECONFIGURE, SOURCE_USER from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -180,14 +175,7 @@ async def test_reauth( config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "entry_id": config_entry.entry_id, - "unique_id": config_entry.unique_id, - }, - ) + result = await config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -222,14 +210,7 @@ async def test_reauth_errors( config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "entry_id": config_entry.entry_id, - "unique_id": config_entry.unique_id, - }, - ) + result = await config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/radarr/test_config_flow.py b/tests/components/radarr/test_config_flow.py index 407b7b50c48cca..0ff9353695748f 100644 --- a/tests/components/radarr/test_config_flow.py +++ b/tests/components/radarr/test_config_flow.py @@ -6,7 +6,7 @@ import pytest from homeassistant.components.radarr.const import DEFAULT_NAME, DOMAIN -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_USER from homeassistant.const import CONF_API_KEY, CONF_SOURCE, CONF_URL, CONF_VERIFY_SSL from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -143,15 +143,7 @@ async def test_full_reauth_flow_implementation( ) -> None: """Test the manual reauth flow from start to finish.""" entry = await setup_integration(hass, aioclient_mock) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - CONF_SOURCE: SOURCE_REAUTH, - "entry_id": entry.entry_id, - "unique_id": entry.unique_id, - }, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/recorder/db_schema_30.py b/tests/components/recorder/db_schema_30.py index 2668f610dfdea8..97c33334111e24 100644 --- a/tests/components/recorder/db_schema_30.py +++ b/tests/components/recorder/db_schema_30.py @@ -9,7 +9,6 @@ from collections.abc import Callable from datetime import datetime, timedelta import logging -import time from typing import Any, Self, TypedDict, cast, overload import ciso8601 @@ -381,7 +380,7 @@ class States(Base): # type: ignore[misc,valid-type] ) # *** Not originally in v30, only added for recorder to startup ok last_updated = Column(DATETIME_TYPE, default=dt_util.utcnow, index=True) last_updated_ts = Column( - TIMESTAMP_TYPE, default=time.time, index=True + TIMESTAMP_TYPE, index=True ) # *** Not originally in v30, only added for recorder to startup ok old_state_id = Column(Integer, ForeignKey("states.state_id"), index=True) attributes_id = Column( diff --git a/tests/components/recorder/db_schema_32.py b/tests/components/recorder/db_schema_32.py index 60f4f733ec0b80..6da0272da87667 100644 --- a/tests/components/recorder/db_schema_32.py +++ b/tests/components/recorder/db_schema_32.py @@ -224,7 +224,7 @@ class Events(Base): # type: ignore[misc,valid-type] data_id = Column(Integer, ForeignKey("event_data.data_id"), index=True) context_id_bin = Column( LargeBinary(CONTEXT_ID_BIN_MAX_LENGTH) - ) # *** Not originally in v3v320, only added for recorder to startup ok + ) # *** Not originally in v32, only added for recorder to startup ok context_user_id_bin = Column( LargeBinary(CONTEXT_ID_BIN_MAX_LENGTH) ) # *** Not originally in v32, only added for recorder to startup ok @@ -565,6 +565,7 @@ class StatisticsBase: id = Column(Integer, Identity(), primary_key=True) created = Column(DATETIME_TYPE, default=dt_util.utcnow) + # *** Not originally in v32, only added for recorder to startup ok created_ts = Column(TIMESTAMP_TYPE, default=time.time) metadata_id = Column( Integer, @@ -572,11 +573,13 @@ class StatisticsBase: index=True, ) start = Column(DATETIME_TYPE, index=True) + # *** Not originally in v32, only added for recorder to startup ok start_ts = Column(TIMESTAMP_TYPE, index=True) mean = Column(DOUBLE_TYPE) min = Column(DOUBLE_TYPE) max = Column(DOUBLE_TYPE) last_reset = Column(DATETIME_TYPE) + # *** Not originally in v32, only added for recorder to startup ok last_reset_ts = Column(TIMESTAMP_TYPE) state = Column(DOUBLE_TYPE) sum = Column(DOUBLE_TYPE) diff --git a/tests/components/recorder/test_init.py b/tests/components/recorder/test_init.py index 72d47515edd6ed..3bbc78e21cec61 100644 --- a/tests/components/recorder/test_init.py +++ b/tests/components/recorder/test_init.py @@ -166,11 +166,10 @@ async def test_shutdown_before_startup_finishes( await hass.async_block_till_done() await hass.async_stop() - def _run_information_with_session(): - instance.recorder_and_worker_thread_ids.add(threading.get_ident()) - return run_information_with_session(session) - - run_info = await instance.async_add_executor_job(_run_information_with_session) + # The database executor is shutdown so we must run the + # query in the main thread for testing + instance.recorder_and_worker_thread_ids.add(threading.get_ident()) + run_info = run_information_with_session(session) assert run_info.run_id == 1 assert run_info.start is not None @@ -216,8 +215,7 @@ async def test_shutdown_closes_connections( instance = recorder.get_instance(hass) await instance.async_db_ready await hass.async_block_till_done() - pool = instance.engine.pool - pool.shutdown = Mock() + pool = instance.engine def _ensure_connected(): with session_scope(hass=hass, read_only=True) as session: @@ -225,10 +223,11 @@ def _ensure_connected(): await instance.async_add_executor_job(_ensure_connected) - hass.bus.async_fire(EVENT_HOMEASSISTANT_FINAL_WRITE) - await hass.async_block_till_done() + with patch.object(pool, "dispose", wraps=pool.dispose) as dispose: + hass.bus.async_fire(EVENT_HOMEASSISTANT_FINAL_WRITE) + await hass.async_block_till_done() - assert len(pool.shutdown.mock_calls) == 1 + assert len(dispose.mock_calls) == 1 with pytest.raises(RuntimeError): assert instance.get_session() diff --git a/tests/components/recorder/test_migrate.py b/tests/components/recorder/test_migrate.py index 05d59facf09ec9..0e473b702efb06 100644 --- a/tests/components/recorder/test_migrate.py +++ b/tests/components/recorder/test_migrate.py @@ -1028,6 +1028,287 @@ def find_constraints( engine.dispose() +@pytest.mark.skip_on_db_engine(["sqlite"]) +@pytest.mark.usefixtures("skip_by_db_engine") +def test_restore_foreign_key_constraints_twice(recorder_db_url: str) -> None: + """Test we can drop and then restore foreign keys. + + This is not supported on SQLite + """ + + constraints_to_recreate = ( + ("events", "data_id", "event_data", "data_id"), + ("states", "event_id", None, None), # This won't be found + ("states", "old_state_id", "states", "state_id"), + ) + + db_engine = recorder_db_url.partition("://")[0] + + expected_dropped_constraints = { + "mysql": [ + ( + "events", + "data_id", + { + "constrained_columns": ["data_id"], + "name": ANY, + "options": {}, + "referred_columns": ["data_id"], + "referred_schema": None, + "referred_table": "event_data", + }, + ), + ( + "states", + "old_state_id", + { + "constrained_columns": ["old_state_id"], + "name": ANY, + "options": {}, + "referred_columns": ["state_id"], + "referred_schema": None, + "referred_table": "states", + }, + ), + ], + "postgresql": [ + ( + "events", + "data_id", + { + "comment": None, + "constrained_columns": ["data_id"], + "name": "events_data_id_fkey", + "options": {}, + "referred_columns": ["data_id"], + "referred_schema": None, + "referred_table": "event_data", + }, + ), + ( + "states", + "old_state_id", + { + "comment": None, + "constrained_columns": ["old_state_id"], + "name": "states_old_state_id_fkey", + "options": {}, + "referred_columns": ["state_id"], + "referred_schema": None, + "referred_table": "states", + }, + ), + ], + } + + def find_constraints( + engine: Engine, table: str, column: str + ) -> list[tuple[str, str, ReflectedForeignKeyConstraint]]: + inspector = inspect(engine) + return [ + (table, column, foreign_key) + for foreign_key in inspector.get_foreign_keys(table) + if foreign_key["name"] and foreign_key["constrained_columns"] == [column] + ] + + engine = create_engine(recorder_db_url) + db_schema.Base.metadata.create_all(engine) + + matching_constraints_1 = [ + dropped_constraint + for table, column, _, _ in constraints_to_recreate + for dropped_constraint in find_constraints(engine, table, column) + ] + assert matching_constraints_1 == expected_dropped_constraints[db_engine] + + with Session(engine) as session: + session_maker = Mock(return_value=session) + for table, column, _, _ in constraints_to_recreate: + migration._drop_foreign_key_constraints( + session_maker, engine, table, column + ) + + # Check we don't find the constrained columns again (they are removed) + matching_constraints_2 = [ + dropped_constraint + for table, column, _, _ in constraints_to_recreate + for dropped_constraint in find_constraints(engine, table, column) + ] + assert matching_constraints_2 == [] + + # Restore the constraints + with Session(engine) as session: + session_maker = Mock(return_value=session) + migration._restore_foreign_key_constraints( + session_maker, engine, constraints_to_recreate + ) + + # Restore the constraints again + with Session(engine) as session: + session_maker = Mock(return_value=session) + migration._restore_foreign_key_constraints( + session_maker, engine, constraints_to_recreate + ) + + # Check we do find a single the constrained columns again (they are restored + # only once, even though we called _restore_foreign_key_constraints twice) + matching_constraints_3 = [ + dropped_constraint + for table, column, _, _ in constraints_to_recreate + for dropped_constraint in find_constraints(engine, table, column) + ] + assert matching_constraints_3 == expected_dropped_constraints[db_engine] + + engine.dispose() + + +@pytest.mark.skip_on_db_engine(["sqlite"]) +@pytest.mark.usefixtures("skip_by_db_engine") +def test_drop_duplicated_foreign_key_constraints(recorder_db_url: str) -> None: + """Test we can drop and then restore foreign keys. + + This is not supported on SQLite + """ + + constraints_to_recreate = ( + ("events", "data_id", "event_data", "data_id"), + ("states", "event_id", None, None), # This won't be found + ("states", "old_state_id", "states", "state_id"), + ) + + db_engine = recorder_db_url.partition("://")[0] + + expected_dropped_constraints = { + "mysql": [ + ( + "events", + "data_id", + { + "constrained_columns": ["data_id"], + "name": ANY, + "options": {}, + "referred_columns": ["data_id"], + "referred_schema": None, + "referred_table": "event_data", + }, + ), + ( + "states", + "old_state_id", + { + "constrained_columns": ["old_state_id"], + "name": ANY, + "options": {}, + "referred_columns": ["state_id"], + "referred_schema": None, + "referred_table": "states", + }, + ), + ], + "postgresql": [ + ( + "events", + "data_id", + { + "comment": None, + "constrained_columns": ["data_id"], + "name": ANY, + "options": {}, + "referred_columns": ["data_id"], + "referred_schema": None, + "referred_table": "event_data", + }, + ), + ( + "states", + "old_state_id", + { + "comment": None, + "constrained_columns": ["old_state_id"], + "name": ANY, + "options": {}, + "referred_columns": ["state_id"], + "referred_schema": None, + "referred_table": "states", + }, + ), + ], + } + + def find_constraints( + engine: Engine, table: str, column: str + ) -> list[tuple[str, str, ReflectedForeignKeyConstraint]]: + inspector = inspect(engine) + return [ + (table, column, foreign_key) + for foreign_key in inspector.get_foreign_keys(table) + if foreign_key["name"] and foreign_key["constrained_columns"] == [column] + ] + + engine = create_engine(recorder_db_url) + db_schema.Base.metadata.create_all(engine) + + # Create a duplicate of the constraints + inspector = Mock(name="inspector") + inspector.get_foreign_keys = Mock(name="get_foreign_keys", return_value=[]) + with ( + patch( + "homeassistant.components.recorder.migration.sqlalchemy.inspect", + return_value=inspector, + ), + Session(engine) as session, + ): + session_maker = Mock(return_value=session) + migration._restore_foreign_key_constraints( + session_maker, engine, constraints_to_recreate + ) + + matching_constraints_1 = [ + dropped_constraint + for table, column, _, _ in constraints_to_recreate + for dropped_constraint in find_constraints(engine, table, column) + ] + _expected_dropped_constraints = [ + _dropped_constraint + for dropped_constraint in expected_dropped_constraints[db_engine] + for _dropped_constraint in (dropped_constraint, dropped_constraint) + ] + assert matching_constraints_1 == _expected_dropped_constraints + + with Session(engine) as session: + session_maker = Mock(return_value=session) + for table, column, _, _ in constraints_to_recreate: + migration._drop_foreign_key_constraints( + session_maker, engine, table, column + ) + + # Check we don't find the constrained columns again (they are removed) + matching_constraints_2 = [ + dropped_constraint + for table, column, _, _ in constraints_to_recreate + for dropped_constraint in find_constraints(engine, table, column) + ] + assert matching_constraints_2 == [] + + # Restore the constraints + with Session(engine) as session: + session_maker = Mock(return_value=session) + migration._restore_foreign_key_constraints( + session_maker, engine, constraints_to_recreate + ) + + # Check we do find a single the constrained columns again (they are restored + # only once, even though we called _restore_foreign_key_constraints twice) + matching_constraints_3 = [ + dropped_constraint + for table, column, _, _ in constraints_to_recreate + for dropped_constraint in find_constraints(engine, table, column) + ] + assert matching_constraints_3 == expected_dropped_constraints[db_engine] + + engine.dispose() + + def test_restore_foreign_key_constraints_with_error( caplog: pytest.LogCaptureFixture, ) -> None: @@ -1045,6 +1326,9 @@ def test_restore_foreign_key_constraints_with_error( instance = Mock() instance.get_session = Mock(return_value=session) engine = Mock() + inspector = Mock(name="inspector") + inspector.get_foreign_keys = Mock(name="get_foreign_keys", return_value=[]) + engine._sa_instance_state = inspector session_maker = Mock(return_value=session) with pytest.raises(InternalError): diff --git a/tests/components/recorder/test_migration_from_schema_32.py b/tests/components/recorder/test_migration_from_schema_32.py index b2a83ae8313b46..40d18ab51fd4eb 100644 --- a/tests/components/recorder/test_migration_from_schema_32.py +++ b/tests/components/recorder/test_migration_from_schema_32.py @@ -32,9 +32,9 @@ get_migration_changes, select_event_type_ids, ) -from homeassistant.components.recorder.tasks import EntityIDPostMigrationTask from homeassistant.components.recorder.util import ( execute_stmt_lambda_element, + get_index_by_name, session_scope, ) from homeassistant.core import HomeAssistant @@ -48,6 +48,7 @@ async_wait_recording_done, ) +from tests.common import async_test_home_assistant from tests.typing import RecorderInstanceGenerator CREATE_ENGINE_TARGET = "homeassistant.components.recorder.core.create_engine" @@ -94,7 +95,7 @@ def _create_engine_test(*args, **kwargs): return engine -@pytest.fixture(autouse=True) +@pytest.fixture def db_schema_32(): """Fixture to initialize the db with the old schema.""" importlib.import_module(SCHEMA_MODULE) @@ -118,6 +119,7 @@ def db_schema_32(): @pytest.mark.parametrize("enable_migrate_context_ids", [True]) +@pytest.mark.usefixtures("db_schema_32") async def test_migrate_events_context_ids( hass: HomeAssistant, recorder_mock: Recorder ) -> None: @@ -331,8 +333,13 @@ def _fetch_migrated_events(): == migration.EventsContextIDMigration.migration_version ) + # Check the index which will be removed by the migrator no longer exists + with session_scope(hass=hass) as session: + assert get_index_by_name(session, "states", "ix_states_context_id") is None + @pytest.mark.parametrize("enable_migrate_context_ids", [True]) +@pytest.mark.usefixtures("db_schema_32") async def test_migrate_states_context_ids( hass: HomeAssistant, recorder_mock: Recorder ) -> None: @@ -528,8 +535,13 @@ def _fetch_migrated_states(): == migration.StatesContextIDMigration.migration_version ) + # Check the index which will be removed by the migrator no longer exists + with session_scope(hass=hass) as session: + assert get_index_by_name(session, "states", "ix_states_context_id") is None + @pytest.mark.parametrize("enable_migrate_event_type_ids", [True]) +@pytest.mark.usefixtures("db_schema_32") async def test_migrate_event_type_ids( hass: HomeAssistant, recorder_mock: Recorder ) -> None: @@ -621,6 +633,7 @@ def _get_many(): @pytest.mark.parametrize("enable_migrate_entity_ids", [True]) +@pytest.mark.usefixtures("db_schema_32") async def test_migrate_entity_ids(hass: HomeAssistant, recorder_mock: Recorder) -> None: """Test we can migrate entity_ids to the StatesMeta table.""" await async_wait_recording_done(hass) @@ -697,6 +710,7 @@ def _fetch_migrated_states(): @pytest.mark.parametrize("enable_migrate_entity_ids", [True]) +@pytest.mark.usefixtures("db_schema_32") async def test_post_migrate_entity_ids( hass: HomeAssistant, recorder_mock: Recorder ) -> None: @@ -731,7 +745,7 @@ def _insert_events(): await _async_wait_migration_done(hass) # This is a threadsafe way to add a task to the recorder - recorder_mock.queue_task(EntityIDPostMigrationTask()) + recorder_mock.queue_task(migration.EntityIDPostMigrationTask()) await _async_wait_migration_done(hass) def _fetch_migrated_states(): @@ -750,6 +764,7 @@ def _fetch_migrated_states(): @pytest.mark.parametrize("enable_migrate_entity_ids", [True]) +@pytest.mark.usefixtures("db_schema_32") async def test_migrate_null_entity_ids( hass: HomeAssistant, recorder_mock: Recorder ) -> None: @@ -833,6 +848,7 @@ def _get_migration_id(): @pytest.mark.parametrize("enable_migrate_event_type_ids", [True]) +@pytest.mark.usefixtures("db_schema_32") async def test_migrate_null_event_type_ids( hass: HomeAssistant, recorder_mock: Recorder ) -> None: @@ -918,6 +934,7 @@ def _get_migration_id(): ) +@pytest.mark.usefixtures("db_schema_32") async def test_stats_timestamp_conversion_is_reentrant( hass: HomeAssistant, recorder_mock: Recorder ) -> None: @@ -1070,6 +1087,7 @@ def _get_all_short_term_stats() -> list[dict[str, Any]]: ] +@pytest.mark.usefixtures("db_schema_32") async def test_stats_timestamp_with_one_by_one( hass: HomeAssistant, recorder_mock: Recorder ) -> None: @@ -1289,6 +1307,7 @@ def _insert_and_do_migration(): ] +@pytest.mark.usefixtures("db_schema_32") async def test_stats_timestamp_with_one_by_one_removes_duplicates( hass: HomeAssistant, recorder_mock: Recorder ) -> None: @@ -1483,3 +1502,158 @@ def _insert_and_do_migration(): "sum": None, }, ] + + +@pytest.mark.parametrize("persistent_database", [True]) +@pytest.mark.usefixtures("hass_storage") # Prevent test hass from writing to storage +async def test_migrate_times( + async_test_recorder: RecorderInstanceGenerator, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test we can migrate times in the statistics tables.""" + importlib.import_module(SCHEMA_MODULE) + old_db_schema = sys.modules[SCHEMA_MODULE] + now = dt_util.utcnow() + now_timestamp = now.timestamp() + + statistics_kwargs = { + "created": now, + "mean": 0, + "metadata_id": 1, + "min": 0, + "max": 0, + "last_reset": now, + "start": now, + "state": 0, + "sum": 0, + } + mock_metadata = old_db_schema.StatisticMetaData( + has_mean=False, + has_sum=False, + name="Test", + source="sensor", + statistic_id="sensor.test", + unit_of_measurement="cats", + ) + number_of_migrations = 5 + + def _get_index_names(table): + with session_scope(hass=hass) as session: + return inspect(session.connection()).get_indexes(table) + + with ( + patch.object(recorder, "db_schema", old_db_schema), + patch.object(migration, "SCHEMA_VERSION", old_db_schema.SCHEMA_VERSION), + patch(CREATE_ENGINE_TARGET, new=_create_engine_test), + ): + async with ( + async_test_home_assistant() as hass, + async_test_recorder(hass) as instance, + ): + await hass.async_block_till_done() + await async_wait_recording_done(hass) + await async_wait_recording_done(hass) + + def _add_data(): + with session_scope(hass=hass) as session: + session.add(old_db_schema.StatisticsMeta.from_meta(mock_metadata)) + with session_scope(hass=hass) as session: + session.add(old_db_schema.Statistics(**statistics_kwargs)) + session.add(old_db_schema.StatisticsShortTerm(**statistics_kwargs)) + + await instance.async_add_executor_job(_add_data) + await hass.async_block_till_done() + await instance.async_block_till_done() + + statistics_indexes = await instance.async_add_executor_job( + _get_index_names, "statistics" + ) + statistics_short_term_indexes = await instance.async_add_executor_job( + _get_index_names, "statistics_short_term" + ) + statistics_index_names = {index["name"] for index in statistics_indexes} + statistics_short_term_index_names = { + index["name"] for index in statistics_short_term_indexes + } + + await hass.async_stop() + await hass.async_block_till_done() + + assert "ix_statistics_statistic_id_start" in statistics_index_names + assert ( + "ix_statistics_short_term_statistic_id_start" + in statistics_short_term_index_names + ) + + # Test that the times are migrated during migration from schema 32 + async with ( + async_test_home_assistant() as hass, + async_test_recorder(hass) as instance, + ): + await hass.async_block_till_done() + + # We need to wait for all the migration tasks to complete + # before we can check the database. + for _ in range(number_of_migrations): + await instance.async_block_till_done() + await async_wait_recording_done(hass) + + def _get_test_data_from_db(): + with session_scope(hass=hass) as session: + statistics_result = list( + session.query(recorder.db_schema.Statistics) + .join( + recorder.db_schema.StatisticsMeta, + recorder.db_schema.Statistics.metadata_id + == recorder.db_schema.StatisticsMeta.id, + ) + .where( + recorder.db_schema.StatisticsMeta.statistic_id == "sensor.test" + ) + ) + statistics_short_term_result = list( + session.query(recorder.db_schema.StatisticsShortTerm) + .join( + recorder.db_schema.StatisticsMeta, + recorder.db_schema.StatisticsShortTerm.metadata_id + == recorder.db_schema.StatisticsMeta.id, + ) + .where( + recorder.db_schema.StatisticsMeta.statistic_id == "sensor.test" + ) + ) + session.expunge_all() + return statistics_result, statistics_short_term_result + + ( + statistics_result, + statistics_short_term_result, + ) = await instance.async_add_executor_job(_get_test_data_from_db) + + for results in (statistics_result, statistics_short_term_result): + assert len(results) == 1 + assert results[0].created is None + assert results[0].created_ts == now_timestamp + assert results[0].last_reset is None + assert results[0].last_reset_ts == now_timestamp + assert results[0].start is None + assert results[0].start_ts == now_timestamp + + statistics_indexes = await instance.async_add_executor_job( + _get_index_names, "statistics" + ) + statistics_short_term_indexes = await instance.async_add_executor_job( + _get_index_names, "statistics_short_term" + ) + statistics_index_names = {index["name"] for index in statistics_indexes} + statistics_short_term_index_names = { + index["name"] for index in statistics_short_term_indexes + } + + assert "ix_statistics_statistic_id_start" not in statistics_index_names + assert ( + "ix_statistics_short_term_statistic_id_start" + not in statistics_short_term_index_names + ) + + await hass.async_stop() diff --git a/tests/components/recorder/test_v32_migration.py b/tests/components/recorder/test_v32_migration.py index 1006a03f4ecc9c..60f223aaa911ee 100644 --- a/tests/components/recorder/test_v32_migration.py +++ b/tests/components/recorder/test_v32_migration.py @@ -1,5 +1,6 @@ """The tests for recorder platform migrating data from v30.""" +from collections.abc import Callable from datetime import timedelta import importlib import sys @@ -25,29 +26,38 @@ from tests.typing import RecorderInstanceGenerator CREATE_ENGINE_TARGET = "homeassistant.components.recorder.core.create_engine" -SCHEMA_MODULE = "tests.components.recorder.db_schema_32" +SCHEMA_MODULE_30 = "tests.components.recorder.db_schema_30" +SCHEMA_MODULE_32 = "tests.components.recorder.db_schema_32" -def _create_engine_test(*args, **kwargs): +def _create_engine_test(schema_module: str) -> Callable: """Test version of create_engine that initializes with old schema. This simulates an existing db with the old schema. """ - importlib.import_module(SCHEMA_MODULE) - old_db_schema = sys.modules[SCHEMA_MODULE] - engine = create_engine(*args, **kwargs) - old_db_schema.Base.metadata.create_all(engine) - with Session(engine) as session: - session.add( - recorder.db_schema.StatisticsRuns(start=statistics.get_start_time()) - ) - session.add( - recorder.db_schema.SchemaChanges( - schema_version=old_db_schema.SCHEMA_VERSION + + def _create_engine_test(*args, **kwargs): + """Test version of create_engine that initializes with old schema. + + This simulates an existing db with the old schema. + """ + importlib.import_module(schema_module) + old_db_schema = sys.modules[schema_module] + engine = create_engine(*args, **kwargs) + old_db_schema.Base.metadata.create_all(engine) + with Session(engine) as session: + session.add( + recorder.db_schema.StatisticsRuns(start=statistics.get_start_time()) ) - ) - session.commit() - return engine + session.add( + recorder.db_schema.SchemaChanges( + schema_version=old_db_schema.SCHEMA_VERSION + ) + ) + session.commit() + return engine + + return _create_engine_test @pytest.mark.parametrize("enable_migrate_context_ids", [True]) @@ -59,9 +69,9 @@ async def test_migrate_times( async_test_recorder: RecorderInstanceGenerator, caplog: pytest.LogCaptureFixture, ) -> None: - """Test we can migrate times.""" - importlib.import_module(SCHEMA_MODULE) - old_db_schema = sys.modules[SCHEMA_MODULE] + """Test we can migrate times in the events and states tables.""" + importlib.import_module(SCHEMA_MODULE_30) + old_db_schema = sys.modules[SCHEMA_MODULE_30] now = dt_util.utcnow() one_second_past = now - timedelta(seconds=1) now_timestamp = now.timestamp() @@ -99,20 +109,18 @@ def _get_states_index_names(): with ( patch.object(recorder, "db_schema", old_db_schema), patch.object(migration, "SCHEMA_VERSION", old_db_schema.SCHEMA_VERSION), + patch.object(migration, "post_migrate_entity_ids", return_value=False), patch.object(migration.EventsContextIDMigration, "migrate_data"), patch.object(migration.StatesContextIDMigration, "migrate_data"), patch.object(migration.EventTypeIDMigration, "migrate_data"), patch.object(migration.EntityIDMigration, "migrate_data"), + patch.object(migration.EventIDPostMigration, "migrate_data"), patch.object(core, "StatesMeta", old_db_schema.StatesMeta), patch.object(core, "EventTypes", old_db_schema.EventTypes), patch.object(core, "EventData", old_db_schema.EventData), patch.object(core, "States", old_db_schema.States), patch.object(core, "Events", old_db_schema.Events), - patch(CREATE_ENGINE_TARGET, new=_create_engine_test), - patch("homeassistant.components.recorder.Recorder._post_migrate_entity_ids"), - patch( - "homeassistant.components.recorder.migration.cleanup_legacy_states_event_ids" - ), + patch(CREATE_ENGINE_TARGET, new=_create_engine_test(SCHEMA_MODULE_30)), ): async with ( async_test_home_assistant() as hass, @@ -182,9 +190,12 @@ def _get_test_data_from_db(): assert len(events_result) == 1 assert events_result[0].time_fired_ts == now_timestamp + assert events_result[0].time_fired is None assert len(states_result) == 1 assert states_result[0].last_changed_ts == one_second_past_timestamp assert states_result[0].last_updated_ts == now_timestamp + assert states_result[0].last_changed is None + assert states_result[0].last_updated is None def _get_events_index_names(): with session_scope(hass=hass) as session: @@ -208,6 +219,7 @@ def _get_events_index_names(): await hass.async_stop() +@pytest.mark.parametrize("enable_migrate_entity_ids", [True]) @pytest.mark.parametrize("persistent_database", [True]) @pytest.mark.usefixtures("hass_storage") # Prevent test hass from writing to storage async def test_migrate_can_resume_entity_id_post_migration( @@ -216,8 +228,8 @@ async def test_migrate_can_resume_entity_id_post_migration( recorder_db_url: str, ) -> None: """Test we resume the entity id post migration after a restart.""" - importlib.import_module(SCHEMA_MODULE) - old_db_schema = sys.modules[SCHEMA_MODULE] + importlib.import_module(SCHEMA_MODULE_32) + old_db_schema = sys.modules[SCHEMA_MODULE_32] now = dt_util.utcnow() one_second_past = now - timedelta(seconds=1) mock_state = State( @@ -251,19 +263,15 @@ def _get_states_index_names(): with ( patch.object(recorder, "db_schema", old_db_schema), - patch.object( - recorder.migration, "SCHEMA_VERSION", old_db_schema.SCHEMA_VERSION - ), + patch.object(migration, "SCHEMA_VERSION", old_db_schema.SCHEMA_VERSION), + patch.object(migration.EventIDPostMigration, "migrate_data"), + patch.object(migration, "post_migrate_entity_ids", return_value=False), patch.object(core, "StatesMeta", old_db_schema.StatesMeta), patch.object(core, "EventTypes", old_db_schema.EventTypes), patch.object(core, "EventData", old_db_schema.EventData), patch.object(core, "States", old_db_schema.States), patch.object(core, "Events", old_db_schema.Events), - patch(CREATE_ENGINE_TARGET, new=_create_engine_test), - patch("homeassistant.components.recorder.Recorder._post_migrate_entity_ids"), - patch( - "homeassistant.components.recorder.migration.cleanup_legacy_states_event_ids" - ), + patch(CREATE_ENGINE_TARGET, new=_create_engine_test(SCHEMA_MODULE_32)), ): async with ( async_test_home_assistant() as hass, @@ -314,6 +322,7 @@ def _add_data(): await hass.async_stop() +@pytest.mark.parametrize("enable_migrate_entity_ids", [True]) @pytest.mark.parametrize("enable_migrate_event_ids", [True]) @pytest.mark.parametrize("persistent_database", [True]) @pytest.mark.usefixtures("hass_storage") # Prevent test hass from writing to storage @@ -327,8 +336,8 @@ async def test_migrate_can_resume_ix_states_event_id_removed( This case tests the migration still happens if ix_states_event_id is removed from the states table. """ - importlib.import_module(SCHEMA_MODULE) - old_db_schema = sys.modules[SCHEMA_MODULE] + importlib.import_module(SCHEMA_MODULE_32) + old_db_schema = sys.modules[SCHEMA_MODULE_32] now = dt_util.utcnow() one_second_past = now - timedelta(seconds=1) mock_state = State( @@ -373,19 +382,15 @@ def _get_states_index_names(): with ( patch.object(recorder, "db_schema", old_db_schema), - patch.object( - recorder.migration, "SCHEMA_VERSION", old_db_schema.SCHEMA_VERSION - ), + patch.object(migration, "SCHEMA_VERSION", old_db_schema.SCHEMA_VERSION), + patch.object(migration.EventIDPostMigration, "migrate_data"), + patch.object(migration, "post_migrate_entity_ids", return_value=False), patch.object(core, "StatesMeta", old_db_schema.StatesMeta), patch.object(core, "EventTypes", old_db_schema.EventTypes), patch.object(core, "EventData", old_db_schema.EventData), patch.object(core, "States", old_db_schema.States), patch.object(core, "Events", old_db_schema.Events), - patch(CREATE_ENGINE_TARGET, new=_create_engine_test), - patch("homeassistant.components.recorder.Recorder._post_migrate_entity_ids"), - patch( - "homeassistant.components.recorder.migration.cleanup_legacy_states_event_ids" - ), + patch(CREATE_ENGINE_TARGET, new=_create_engine_test(SCHEMA_MODULE_32)), ): async with ( async_test_home_assistant() as hass, @@ -463,8 +468,8 @@ async def test_out_of_disk_space_while_rebuild_states_table( This case tests the migration still happens if ix_states_event_id is removed from the states table. """ - importlib.import_module(SCHEMA_MODULE) - old_db_schema = sys.modules[SCHEMA_MODULE] + importlib.import_module(SCHEMA_MODULE_32) + old_db_schema = sys.modules[SCHEMA_MODULE_32] now = dt_util.utcnow() one_second_past = now - timedelta(seconds=1) mock_state = State( @@ -509,19 +514,15 @@ def _get_states_index_names(): with ( patch.object(recorder, "db_schema", old_db_schema), - patch.object( - recorder.migration, "SCHEMA_VERSION", old_db_schema.SCHEMA_VERSION - ), + patch.object(migration, "SCHEMA_VERSION", old_db_schema.SCHEMA_VERSION), + patch.object(migration.EventIDPostMigration, "migrate_data"), + patch.object(migration, "post_migrate_entity_ids", return_value=False), patch.object(core, "StatesMeta", old_db_schema.StatesMeta), patch.object(core, "EventTypes", old_db_schema.EventTypes), patch.object(core, "EventData", old_db_schema.EventData), patch.object(core, "States", old_db_schema.States), patch.object(core, "Events", old_db_schema.Events), - patch(CREATE_ENGINE_TARGET, new=_create_engine_test), - patch("homeassistant.components.recorder.Recorder._post_migrate_entity_ids"), - patch( - "homeassistant.components.recorder.migration.cleanup_legacy_states_event_ids" - ), + patch(CREATE_ENGINE_TARGET, new=_create_engine_test(SCHEMA_MODULE_32)), ): async with ( async_test_home_assistant() as hass, @@ -626,6 +627,7 @@ def _add_data(): @pytest.mark.usefixtures("skip_by_db_engine") @pytest.mark.skip_on_db_engine(["sqlite"]) +@pytest.mark.parametrize("enable_migrate_entity_ids", [True]) @pytest.mark.parametrize("enable_migrate_event_ids", [True]) @pytest.mark.parametrize("persistent_database", [True]) @pytest.mark.usefixtures("hass_storage") # Prevent test hass from writing to storage @@ -641,10 +643,10 @@ async def test_out_of_disk_space_while_removing_foreign_key( Note that the test is somewhat forced; the states.event_id foreign key constraint is removed when migrating to schema version 46, inspecting the schema in - cleanup_legacy_states_event_ids is not likely to fail. + EventIDPostMigration.migrate_data, is not likely to fail. """ - importlib.import_module(SCHEMA_MODULE) - old_db_schema = sys.modules[SCHEMA_MODULE] + importlib.import_module(SCHEMA_MODULE_32) + old_db_schema = sys.modules[SCHEMA_MODULE_32] now = dt_util.utcnow() one_second_past = now - timedelta(seconds=1) mock_state = State( @@ -689,19 +691,15 @@ def _get_states_index_names(): with ( patch.object(recorder, "db_schema", old_db_schema), - patch.object( - recorder.migration, "SCHEMA_VERSION", old_db_schema.SCHEMA_VERSION - ), + patch.object(migration, "SCHEMA_VERSION", old_db_schema.SCHEMA_VERSION), + patch.object(migration.EventIDPostMigration, "migrate_data"), + patch.object(migration, "post_migrate_entity_ids", return_value=False), patch.object(core, "StatesMeta", old_db_schema.StatesMeta), patch.object(core, "EventTypes", old_db_schema.EventTypes), patch.object(core, "EventData", old_db_schema.EventData), patch.object(core, "States", old_db_schema.States), patch.object(core, "Events", old_db_schema.Events), - patch(CREATE_ENGINE_TARGET, new=_create_engine_test), - patch("homeassistant.components.recorder.Recorder._post_migrate_entity_ids"), - patch( - "homeassistant.components.recorder.migration.cleanup_legacy_states_event_ids" - ), + patch(CREATE_ENGINE_TARGET, new=_create_engine_test(SCHEMA_MODULE_32)), ): async with ( async_test_home_assistant() as hass, diff --git a/tests/components/renault/snapshots/test_services.ambr b/tests/components/renault/snapshots/test_services.ambr new file mode 100644 index 00000000000000..df4269c7430fd7 --- /dev/null +++ b/tests/components/renault/snapshots/test_services.ambr @@ -0,0 +1,460 @@ +# serializer version: 1 +# name: test_service_set_charge_schedule[zoe_40] + list([ + dict({ + 'activated': True, + 'friday': dict({ + 'duration': 450, + 'raw_data': dict({ + 'duration': 450, + 'startTime': 'T00:00Z', + }), + 'startTime': 'T00:00Z', + }), + 'id': 1, + 'monday': dict({ + 'duration': 450, + 'raw_data': dict({ + 'duration': 450, + 'startTime': 'T00:00Z', + }), + 'startTime': 'T00:00Z', + }), + 'raw_data': dict({ + 'activated': True, + 'friday': dict({ + 'duration': 450, + 'startTime': 'T00:00Z', + }), + 'id': 1, + 'monday': dict({ + 'duration': 450, + 'startTime': 'T00:00Z', + }), + 'saturday': dict({ + 'duration': 450, + 'startTime': 'T00:00Z', + }), + 'sunday': dict({ + 'duration': 450, + 'startTime': 'T00:00Z', + }), + 'thursday': dict({ + 'duration': 450, + 'startTime': 'T00:00Z', + }), + 'tuesday': dict({ + 'duration': 450, + 'startTime': 'T00:00Z', + }), + 'wednesday': dict({ + 'duration': 450, + 'startTime': 'T00:00Z', + }), + }), + 'saturday': dict({ + 'duration': 450, + 'raw_data': dict({ + 'duration': 450, + 'startTime': 'T00:00Z', + }), + 'startTime': 'T00:00Z', + }), + 'sunday': dict({ + 'duration': 450, + 'raw_data': dict({ + 'duration': 450, + 'startTime': 'T00:00Z', + }), + 'startTime': 'T00:00Z', + }), + 'thursday': dict({ + 'duration': 450, + 'raw_data': dict({ + 'duration': 450, + 'startTime': 'T00:00Z', + }), + 'startTime': 'T00:00Z', + }), + 'tuesday': dict({ + 'duration': 450, + 'raw_data': dict({ + 'duration': 450, + 'startTime': 'T00:00Z', + }), + 'startTime': 'T00:00Z', + }), + 'wednesday': dict({ + 'duration': 450, + 'raw_data': dict({ + 'duration': 450, + 'startTime': 'T00:00Z', + }), + 'startTime': 'T00:00Z', + }), + }), + dict({ + 'activated': True, + 'friday': dict({ + 'duration': 15, + 'raw_data': dict({ + 'duration': 15, + 'startTime': 'T23:30Z', + }), + 'startTime': 'T23:30Z', + }), + 'id': 2, + 'monday': dict({ + 'duration': 15, + 'raw_data': dict({ + 'duration': 15, + 'startTime': 'T23:30Z', + }), + 'startTime': 'T23:30Z', + }), + 'raw_data': dict({ + 'activated': True, + 'friday': dict({ + 'duration': 15, + 'startTime': 'T23:30Z', + }), + 'id': 2, + 'monday': dict({ + 'duration': 15, + 'startTime': 'T23:30Z', + }), + 'saturday': dict({ + 'duration': 15, + 'startTime': 'T23:30Z', + }), + 'sunday': dict({ + 'duration': 15, + 'startTime': 'T23:30Z', + }), + 'thursday': dict({ + 'duration': 15, + 'startTime': 'T23:30Z', + }), + 'tuesday': dict({ + 'duration': 15, + 'startTime': 'T23:30Z', + }), + 'wednesday': dict({ + 'duration': 15, + 'startTime': 'T23:30Z', + }), + }), + 'saturday': dict({ + 'duration': 15, + 'raw_data': dict({ + 'duration': 15, + 'startTime': 'T23:30Z', + }), + 'startTime': 'T23:30Z', + }), + 'sunday': dict({ + 'duration': 15, + 'raw_data': dict({ + 'duration': 15, + 'startTime': 'T23:30Z', + }), + 'startTime': 'T23:30Z', + }), + 'thursday': dict({ + 'duration': 15, + 'raw_data': dict({ + 'duration': 15, + 'startTime': 'T23:30Z', + }), + 'startTime': 'T23:30Z', + }), + 'tuesday': dict({ + 'duration': 15, + 'raw_data': dict({ + 'duration': 15, + 'startTime': 'T23:30Z', + }), + 'startTime': 'T23:30Z', + }), + 'wednesday': dict({ + 'duration': 15, + 'raw_data': dict({ + 'duration': 15, + 'startTime': 'T23:30Z', + }), + 'startTime': 'T23:30Z', + }), + }), + dict({ + 'activated': False, + 'friday': None, + 'id': 3, + 'monday': None, + 'raw_data': dict({ + 'activated': False, + 'id': 3, + }), + 'saturday': None, + 'sunday': None, + 'thursday': None, + 'tuesday': None, + 'wednesday': None, + }), + dict({ + 'activated': False, + 'friday': None, + 'id': 4, + 'monday': None, + 'raw_data': dict({ + 'activated': False, + 'id': 4, + }), + 'saturday': None, + 'sunday': None, + 'thursday': None, + 'tuesday': None, + 'wednesday': None, + }), + dict({ + 'activated': False, + 'friday': None, + 'id': 5, + 'monday': None, + 'raw_data': dict({ + 'activated': False, + 'id': 5, + }), + 'saturday': None, + 'sunday': None, + 'thursday': None, + 'tuesday': None, + 'wednesday': None, + }), + ]) +# --- +# name: test_service_set_charge_schedule_multi[zoe_40] + list([ + dict({ + 'activated': True, + 'friday': dict({ + 'duration': 450, + 'raw_data': dict({ + 'duration': 450, + 'startTime': 'T00:00Z', + }), + 'startTime': 'T00:00Z', + }), + 'id': 1, + 'monday': dict({ + 'duration': 450, + 'raw_data': dict({ + 'duration': 450, + 'startTime': 'T00:00Z', + }), + 'startTime': 'T00:00Z', + }), + 'raw_data': dict({ + 'activated': True, + 'friday': dict({ + 'duration': 450, + 'startTime': 'T00:00Z', + }), + 'id': 1, + 'monday': dict({ + 'duration': 450, + 'startTime': 'T00:00Z', + }), + 'saturday': dict({ + 'duration': 450, + 'startTime': 'T00:00Z', + }), + 'sunday': dict({ + 'duration': 450, + 'startTime': 'T00:00Z', + }), + 'thursday': dict({ + 'duration': 450, + 'startTime': 'T00:00Z', + }), + 'tuesday': dict({ + 'duration': 450, + 'startTime': 'T00:00Z', + }), + 'wednesday': dict({ + 'duration': 450, + 'startTime': 'T00:00Z', + }), + }), + 'saturday': dict({ + 'duration': 450, + 'raw_data': dict({ + 'duration': 450, + 'startTime': 'T00:00Z', + }), + 'startTime': 'T00:00Z', + }), + 'sunday': dict({ + 'duration': 450, + 'raw_data': dict({ + 'duration': 450, + 'startTime': 'T00:00Z', + }), + 'startTime': 'T00:00Z', + }), + 'thursday': dict({ + 'duration': 450, + 'raw_data': dict({ + 'duration': 450, + 'startTime': 'T00:00Z', + }), + 'startTime': 'T00:00Z', + }), + 'tuesday': dict({ + 'duration': 450, + 'raw_data': dict({ + 'duration': 450, + 'startTime': 'T00:00Z', + }), + 'startTime': 'T00:00Z', + }), + 'wednesday': dict({ + 'duration': 450, + 'raw_data': dict({ + 'duration': 450, + 'startTime': 'T00:00Z', + }), + 'startTime': 'T00:00Z', + }), + }), + dict({ + 'activated': True, + 'friday': dict({ + 'duration': 30, + 'raw_data': dict({ + 'duration': 30, + 'startTime': 'T12:00Z', + }), + 'startTime': 'T12:00Z', + }), + 'id': 2, + 'monday': dict({ + 'duration': 30, + 'raw_data': dict({ + 'duration': 30, + 'startTime': 'T12:00Z', + }), + 'startTime': 'T12:00Z', + }), + 'raw_data': dict({ + 'activated': True, + 'friday': dict({ + 'duration': 15, + 'startTime': 'T23:30Z', + }), + 'id': 2, + 'monday': dict({ + 'duration': 15, + 'startTime': 'T23:30Z', + }), + 'saturday': dict({ + 'duration': 15, + 'startTime': 'T23:30Z', + }), + 'sunday': dict({ + 'duration': 15, + 'startTime': 'T23:30Z', + }), + 'thursday': dict({ + 'duration': 15, + 'startTime': 'T23:30Z', + }), + 'tuesday': dict({ + 'duration': 15, + 'startTime': 'T23:30Z', + }), + 'wednesday': dict({ + 'duration': 15, + 'startTime': 'T23:30Z', + }), + }), + 'saturday': dict({ + 'duration': 30, + 'raw_data': dict({ + 'duration': 30, + 'startTime': 'T12:00Z', + }), + 'startTime': 'T12:00Z', + }), + 'sunday': dict({ + 'duration': 30, + 'raw_data': dict({ + 'duration': 30, + 'startTime': 'T12:00Z', + }), + 'startTime': 'T12:00Z', + }), + 'thursday': dict({ + 'duration': 15, + 'raw_data': dict({ + 'duration': 15, + 'startTime': 'T23:30Z', + }), + 'startTime': 'T23:30Z', + }), + 'tuesday': dict({ + 'duration': 30, + 'raw_data': dict({ + 'duration': 30, + 'startTime': 'T12:00Z', + }), + 'startTime': 'T12:00Z', + }), + 'wednesday': None, + }), + dict({ + 'activated': False, + 'friday': None, + 'id': 3, + 'monday': None, + 'raw_data': dict({ + 'activated': False, + 'id': 3, + }), + 'saturday': None, + 'sunday': None, + 'thursday': None, + 'tuesday': None, + 'wednesday': None, + }), + dict({ + 'activated': False, + 'friday': None, + 'id': 4, + 'monday': None, + 'raw_data': dict({ + 'activated': False, + 'id': 4, + }), + 'saturday': None, + 'sunday': None, + 'thursday': None, + 'tuesday': None, + 'wednesday': None, + }), + dict({ + 'activated': False, + 'friday': None, + 'id': 5, + 'monday': None, + 'raw_data': dict({ + 'activated': False, + 'id': 5, + }), + 'saturday': None, + 'sunday': None, + 'thursday': None, + 'tuesday': None, + 'wednesday': None, + }), + ]) +# --- diff --git a/tests/components/renault/test_config_flow.py b/tests/components/renault/test_config_flow.py index 7d40cf69314847..69bfdf0842e336 100644 --- a/tests/components/renault/test_config_flow.py +++ b/tests/components/renault/test_config_flow.py @@ -13,15 +13,12 @@ CONF_LOCALE, DOMAIN, ) -from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType from homeassistant.helpers import aiohttp_client -from .const import MOCK_CONFIG - -from tests.common import load_fixture +from tests.common import MockConfigEntry, load_fixture pytestmark = pytest.mark.usefixtures("mock_setup_entry") @@ -220,19 +217,11 @@ async def test_config_flow_duplicate( assert len(mock_setup_entry.mock_calls) == 0 -async def test_reauth(hass: HomeAssistant, config_entry: ConfigEntry) -> None: +async def test_reauth(hass: HomeAssistant, config_entry: MockConfigEntry) -> None: """Test the start of the config flow.""" assert len(hass.config_entries.async_entries(DOMAIN)) == 1 - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": config_entry.entry_id, - "unique_id": config_entry.unique_id, - }, - data=MOCK_CONFIG, - ) + result = await config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["description_placeholders"] == {CONF_USERNAME: "email@test.com"} diff --git a/tests/components/renault/test_services.py b/tests/components/renault/test_services.py index 4e3460b9afa662..831204c59b4caa 100644 --- a/tests/components/renault/test_services.py +++ b/tests/components/renault/test_services.py @@ -8,6 +8,7 @@ from renault_api.exceptions import RenaultException from renault_api.kamereon import schemas from renault_api.kamereon.models import ChargeSchedule +from syrupy import SnapshotAssertion from homeassistant.components.renault.const import DOMAIN from homeassistant.components.renault.services import ( @@ -143,7 +144,7 @@ async def test_service_set_ac_start_with_date( async def test_service_set_charge_schedule( - hass: HomeAssistant, config_entry: ConfigEntry + hass: HomeAssistant, config_entry: ConfigEntry, snapshot: SnapshotAssertion ) -> None: """Test that service invokes renault_api with correct data.""" await hass.config_entries.async_setup(config_entry.entry_id) @@ -176,11 +177,11 @@ async def test_service_set_charge_schedule( ) assert len(mock_action.mock_calls) == 1 mock_call_data: list[ChargeSchedule] = mock_action.mock_calls[0][1][0] - assert mock_action.mock_calls[0][1] == (mock_call_data,) + assert mock_call_data == snapshot async def test_service_set_charge_schedule_multi( - hass: HomeAssistant, config_entry: ConfigEntry + hass: HomeAssistant, config_entry: ConfigEntry, snapshot: SnapshotAssertion ) -> None: """Test that service invokes renault_api with correct data.""" await hass.config_entries.async_setup(config_entry.entry_id) @@ -225,7 +226,7 @@ async def test_service_set_charge_schedule_multi( ) assert len(mock_action.mock_calls) == 1 mock_call_data: list[ChargeSchedule] = mock_action.mock_calls[0][1][0] - assert mock_action.mock_calls[0][1] == (mock_call_data,) + assert mock_call_data == snapshot # Monday updated with new values assert mock_call_data[1].monday.startTime == "T12:00Z" diff --git a/tests/components/reolink/conftest.py b/tests/components/reolink/conftest.py index ddea36cb292b56..c14a5ee0c32547 100644 --- a/tests/components/reolink/conftest.py +++ b/tests/components/reolink/conftest.py @@ -6,8 +6,8 @@ import pytest from reolink_aio.api import Chime -from homeassistant.components.reolink import const from homeassistant.components.reolink.config_flow import DEFAULT_PROTOCOL +from homeassistant.components.reolink.const import CONF_USE_HTTPS, DOMAIN from homeassistant.const import ( CONF_HOST, CONF_PASSWORD, @@ -33,11 +33,14 @@ TEST_UID_CAM = "DEF7654321D89GHT" TEST_PORT = 1234 TEST_NVR_NAME = "test_reolink_name" +TEST_CAM_NAME = "test_reolink_cam" TEST_NVR_NAME2 = "test2_reolink_name" +TEST_CAM_NAME = "test_reolink_cam" TEST_USE_HTTPS = True TEST_HOST_MODEL = "RLN8-410" TEST_ITEM_NUMBER = "P000" TEST_CAM_MODEL = "RLC-123" +TEST_DUO_MODEL = "Reolink Duo PoE" @pytest.fixture @@ -134,14 +137,14 @@ def reolink_platforms() -> Generator[None]: def config_entry(hass: HomeAssistant) -> MockConfigEntry: """Add the reolink mock config entry to hass.""" config_entry = MockConfigEntry( - domain=const.DOMAIN, + domain=DOMAIN, unique_id=format_mac(TEST_MAC), data={ CONF_HOST: TEST_HOST, CONF_USERNAME: TEST_USERNAME, CONF_PASSWORD: TEST_PASSWORD, CONF_PORT: TEST_PORT, - const.CONF_USE_HTTPS: TEST_USE_HTTPS, + CONF_USE_HTTPS: TEST_USE_HTTPS, }, options={ CONF_PROTOCOL: DEFAULT_PROTOCOL, diff --git a/tests/components/reolink/test_binary_sensor.py b/tests/components/reolink/test_binary_sensor.py index e02742afe1d7fb..893e58a95124f0 100644 --- a/tests/components/reolink/test_binary_sensor.py +++ b/tests/components/reolink/test_binary_sensor.py @@ -4,13 +4,14 @@ from freezegun.api import FrozenDateTimeFactory -from homeassistant.components.reolink import DEVICE_UPDATE_INTERVAL, const +from homeassistant.components.reolink import DEVICE_UPDATE_INTERVAL +from homeassistant.components.reolink.const import DOMAIN from homeassistant.config_entries import ConfigEntryState from homeassistant.const import STATE_OFF, STATE_ON, Platform from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er -from .conftest import TEST_NVR_NAME, TEST_UID +from .conftest import TEST_DUO_MODEL, TEST_NVR_NAME, TEST_UID from tests.common import MockConfigEntry, async_fire_time_changed from tests.typing import ClientSessionGenerator @@ -25,7 +26,7 @@ async def test_motion_sensor( entity_registry: er.EntityRegistry, ) -> None: """Test binary sensor entity with motion sensor.""" - reolink_connect.model = "Reolink Duo PoE" + reolink_connect.model = TEST_DUO_MODEL reolink_connect.motion_detected.return_value = True with patch("homeassistant.components.reolink.PLATFORMS", [Platform.BINARY_SENSOR]): assert await hass.config_entries.async_setup(config_entry.entry_id) @@ -45,7 +46,7 @@ async def test_motion_sensor( # test webhook callback reolink_connect.motion_detected.return_value = True reolink_connect.ONVIF_event_callback.return_value = [0] - webhook_id = f"{const.DOMAIN}_{TEST_UID.replace(':', '')}_ONVIF" + webhook_id = f"{DOMAIN}_{TEST_UID.replace(':', '')}_ONVIF" client = await hass_client_no_auth() await client.post(f"/api/webhook/{webhook_id}", data="test_data") diff --git a/tests/components/reolink/test_button.py b/tests/components/reolink/test_button.py new file mode 100644 index 00000000000000..7c91051c66e9a2 --- /dev/null +++ b/tests/components/reolink/test_button.py @@ -0,0 +1,112 @@ +"""Test the Reolink button platform.""" + +from unittest.mock import MagicMock, patch + +import pytest +from reolink_aio.exceptions import ReolinkError + +from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS +from homeassistant.components.reolink.button import ATTR_SPEED, SERVICE_PTZ_MOVE +from homeassistant.components.reolink.const import DOMAIN +from homeassistant.config_entries import ConfigEntryState +from homeassistant.const import ATTR_ENTITY_ID, Platform +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError + +from .conftest import TEST_NVR_NAME + +from tests.common import MockConfigEntry + + +async def test_button( + hass: HomeAssistant, + config_entry: MockConfigEntry, + reolink_connect: MagicMock, +) -> None: + """Test button entity with ptz up.""" + with patch("homeassistant.components.reolink.PLATFORMS", [Platform.BUTTON]): + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + entity_id = f"{Platform.BUTTON}.{TEST_NVR_NAME}_ptz_up" + + await hass.services.async_call( + BUTTON_DOMAIN, + SERVICE_PRESS, + {ATTR_ENTITY_ID: entity_id}, + blocking=True, + ) + reolink_connect.set_ptz_command.assert_called_once() + + reolink_connect.set_ptz_command.side_effect = ReolinkError("Test error") + with pytest.raises(HomeAssistantError): + await hass.services.async_call( + BUTTON_DOMAIN, + SERVICE_PRESS, + {ATTR_ENTITY_ID: entity_id}, + blocking=True, + ) + + +async def test_ptz_move_service( + hass: HomeAssistant, + config_entry: MockConfigEntry, + reolink_connect: MagicMock, +) -> None: + """Test ptz_move entity service using PTZ button entity.""" + with patch("homeassistant.components.reolink.PLATFORMS", [Platform.BUTTON]): + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + entity_id = f"{Platform.BUTTON}.{TEST_NVR_NAME}_ptz_up" + + await hass.services.async_call( + DOMAIN, + SERVICE_PTZ_MOVE, + {ATTR_ENTITY_ID: entity_id, ATTR_SPEED: 5}, + blocking=True, + ) + reolink_connect.set_ptz_command.assert_called_with(0, command="Up", speed=5) + + reolink_connect.set_ptz_command.side_effect = ReolinkError("Test error") + with pytest.raises(HomeAssistantError): + await hass.services.async_call( + DOMAIN, + SERVICE_PTZ_MOVE, + {ATTR_ENTITY_ID: entity_id, ATTR_SPEED: 5}, + blocking=True, + ) + + +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_host_button( + hass: HomeAssistant, + config_entry: MockConfigEntry, + reolink_connect: MagicMock, +) -> None: + """Test host button entity with reboot.""" + with patch("homeassistant.components.reolink.PLATFORMS", [Platform.BUTTON]): + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + entity_id = f"{Platform.BUTTON}.{TEST_NVR_NAME}_restart" + + await hass.services.async_call( + BUTTON_DOMAIN, + SERVICE_PRESS, + {ATTR_ENTITY_ID: entity_id}, + blocking=True, + ) + reolink_connect.reboot.assert_called_once() + + reolink_connect.reboot.side_effect = ReolinkError("Test error") + with pytest.raises(HomeAssistantError): + await hass.services.async_call( + BUTTON_DOMAIN, + SERVICE_PRESS, + {ATTR_ENTITY_ID: entity_id}, + blocking=True, + ) diff --git a/tests/components/reolink/test_camera.py b/tests/components/reolink/test_camera.py new file mode 100644 index 00000000000000..96bb5a099c9018 --- /dev/null +++ b/tests/components/reolink/test_camera.py @@ -0,0 +1,63 @@ +"""Test the Reolink camera platform.""" + +from unittest.mock import MagicMock, patch + +import pytest +from reolink_aio.exceptions import ReolinkError + +from homeassistant.components.camera import async_get_image, async_get_stream_source +from homeassistant.config_entries import ConfigEntryState +from homeassistant.const import STATE_IDLE, Platform +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError + +from .conftest import TEST_DUO_MODEL, TEST_NVR_NAME + +from tests.common import MockConfigEntry +from tests.typing import ClientSessionGenerator + + +async def test_camera( + hass: HomeAssistant, + hass_client_no_auth: ClientSessionGenerator, + config_entry: MockConfigEntry, + reolink_connect: MagicMock, +) -> None: + """Test camera entity with fluent.""" + with patch("homeassistant.components.reolink.PLATFORMS", [Platform.CAMERA]): + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + entity_id = f"{Platform.CAMERA}.{TEST_NVR_NAME}_fluent" + assert hass.states.get(entity_id).state == STATE_IDLE + + # check getting a image from the camera + reolink_connect.get_snapshot.return_value = b"image" + assert (await async_get_image(hass, entity_id)).content == b"image" + + reolink_connect.get_snapshot.side_effect = ReolinkError("Test error") + with pytest.raises(HomeAssistantError): + await async_get_image(hass, entity_id) + + # check getting the stream source + assert await async_get_stream_source(hass, entity_id) is not None + + +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_camera_no_stream_source( + hass: HomeAssistant, + config_entry: MockConfigEntry, + reolink_connect: MagicMock, +) -> None: + """Test camera entity with no stream source.""" + reolink_connect.model = TEST_DUO_MODEL + reolink_connect.get_stream_source.return_value = None + + with patch("homeassistant.components.reolink.PLATFORMS", [Platform.CAMERA]): + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + entity_id = f"{Platform.CAMERA}.{TEST_NVR_NAME}_snapshots_fluent_lens_0" + assert hass.states.get(entity_id).state == STATE_IDLE diff --git a/tests/components/reolink/test_config_flow.py b/tests/components/reolink/test_config_flow.py index 55dd0d4fea9ab8..40695861aafc2b 100644 --- a/tests/components/reolink/test_config_flow.py +++ b/tests/components/reolink/test_config_flow.py @@ -10,8 +10,9 @@ from homeassistant import config_entries from homeassistant.components import dhcp -from homeassistant.components.reolink import DEVICE_UPDATE_INTERVAL, const +from homeassistant.components.reolink import DEVICE_UPDATE_INTERVAL from homeassistant.components.reolink.config_flow import DEFAULT_PROTOCOL +from homeassistant.components.reolink.const import CONF_USE_HTTPS, DOMAIN from homeassistant.components.reolink.exceptions import ReolinkWebhookException from homeassistant.components.reolink.host import DEFAULT_TIMEOUT from homeassistant.config_entries import ConfigEntryState @@ -50,7 +51,7 @@ async def test_config_flow_manual_success( ) -> None: """Successful flow manually initialized by the user.""" result = await hass.config_entries.flow.async_init( - const.DOMAIN, context={"source": config_entries.SOURCE_USER} + DOMAIN, context={"source": config_entries.SOURCE_USER} ) assert result["type"] is FlowResultType.FORM @@ -73,7 +74,7 @@ async def test_config_flow_manual_success( CONF_USERNAME: TEST_USERNAME, CONF_PASSWORD: TEST_PASSWORD, CONF_PORT: TEST_PORT, - const.CONF_USE_HTTPS: TEST_USE_HTTPS, + CONF_USE_HTTPS: TEST_USE_HTTPS, } assert result["options"] == { CONF_PROTOCOL: DEFAULT_PROTOCOL, @@ -85,7 +86,7 @@ async def test_config_flow_errors( ) -> None: """Successful flow manually initialized by the user after some errors.""" result = await hass.config_entries.flow.async_init( - const.DOMAIN, context={"source": config_entries.SOURCE_USER} + DOMAIN, context={"source": config_entries.SOURCE_USER} ) assert result["type"] is FlowResultType.FORM @@ -94,6 +95,8 @@ async def test_config_flow_errors( reolink_connect.is_admin = False reolink_connect.user_level = "guest" + reolink_connect.unsubscribe.side_effect = ReolinkError("Test error") + reolink_connect.logout.side_effect = ReolinkError("Test error") result = await hass.config_entries.flow.async_configure( result["flow_id"], { @@ -204,7 +207,7 @@ async def test_config_flow_errors( CONF_PASSWORD: TEST_PASSWORD, CONF_HOST: TEST_HOST, CONF_PORT: TEST_PORT, - const.CONF_USE_HTTPS: TEST_USE_HTTPS, + CONF_USE_HTTPS: TEST_USE_HTTPS, }, ) @@ -215,7 +218,7 @@ async def test_config_flow_errors( CONF_USERNAME: TEST_USERNAME, CONF_PASSWORD: TEST_PASSWORD, CONF_PORT: TEST_PORT, - const.CONF_USE_HTTPS: TEST_USE_HTTPS, + CONF_USE_HTTPS: TEST_USE_HTTPS, } assert result["options"] == { CONF_PROTOCOL: DEFAULT_PROTOCOL, @@ -225,14 +228,14 @@ async def test_config_flow_errors( async def test_options_flow(hass: HomeAssistant, mock_setup_entry: MagicMock) -> None: """Test specifying non default settings using options flow.""" config_entry = MockConfigEntry( - domain=const.DOMAIN, + domain=DOMAIN, unique_id=format_mac(TEST_MAC), data={ CONF_HOST: TEST_HOST, CONF_USERNAME: TEST_USERNAME, CONF_PASSWORD: TEST_PASSWORD, CONF_PORT: TEST_PORT, - const.CONF_USE_HTTPS: TEST_USE_HTTPS, + CONF_USE_HTTPS: TEST_USE_HTTPS, }, options={ CONF_PROTOCOL: "rtsp", @@ -265,14 +268,14 @@ async def test_change_connection_settings( ) -> None: """Test changing connection settings by issuing a second user config flow.""" config_entry = MockConfigEntry( - domain=const.DOMAIN, + domain=DOMAIN, unique_id=format_mac(TEST_MAC), data={ CONF_HOST: TEST_HOST, CONF_USERNAME: TEST_USERNAME, CONF_PASSWORD: TEST_PASSWORD, CONF_PORT: TEST_PORT, - const.CONF_USE_HTTPS: TEST_USE_HTTPS, + CONF_USE_HTTPS: TEST_USE_HTTPS, }, options={ CONF_PROTOCOL: DEFAULT_PROTOCOL, @@ -282,7 +285,7 @@ async def test_change_connection_settings( config_entry.add_to_hass(hass) result = await hass.config_entries.flow.async_init( - const.DOMAIN, context={"source": config_entries.SOURCE_USER} + DOMAIN, context={"source": config_entries.SOURCE_USER} ) assert result["type"] is FlowResultType.FORM @@ -308,14 +311,14 @@ async def test_change_connection_settings( async def test_reauth(hass: HomeAssistant, mock_setup_entry: MagicMock) -> None: """Test a reauth flow.""" config_entry = MockConfigEntry( - domain=const.DOMAIN, + domain=DOMAIN, unique_id=format_mac(TEST_MAC), data={ CONF_HOST: TEST_HOST, CONF_USERNAME: TEST_USERNAME, CONF_PASSWORD: TEST_PASSWORD, CONF_PORT: TEST_PORT, - const.CONF_USE_HTTPS: TEST_USE_HTTPS, + CONF_USE_HTTPS: TEST_USE_HTTPS, }, options={ CONF_PROTOCOL: DEFAULT_PROTOCOL, @@ -327,16 +330,7 @@ async def test_reauth(hass: HomeAssistant, mock_setup_entry: MagicMock) -> None: assert await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() - result = await hass.config_entries.flow.async_init( - const.DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": config_entry.entry_id, - "title_placeholders": {"name": TEST_NVR_NAME}, - "unique_id": format_mac(TEST_MAC), - }, - data=config_entry.data, - ) + result = await config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -374,7 +368,7 @@ async def test_dhcp_flow(hass: HomeAssistant, mock_setup_entry: MagicMock) -> No ) result = await hass.config_entries.flow.async_init( - const.DOMAIN, context={"source": config_entries.SOURCE_DHCP}, data=dhcp_data + DOMAIN, context={"source": config_entries.SOURCE_DHCP}, data=dhcp_data ) assert result["type"] is FlowResultType.FORM @@ -396,7 +390,7 @@ async def test_dhcp_flow(hass: HomeAssistant, mock_setup_entry: MagicMock) -> No CONF_USERNAME: TEST_USERNAME, CONF_PASSWORD: TEST_PASSWORD, CONF_PORT: TEST_PORT, - const.CONF_USE_HTTPS: TEST_USE_HTTPS, + CONF_USE_HTTPS: TEST_USE_HTTPS, } assert result["options"] == { CONF_PROTOCOL: DEFAULT_PROTOCOL, @@ -449,14 +443,14 @@ async def test_dhcp_ip_update( ) -> None: """Test dhcp discovery aborts if already configured where the IP is updated if appropriate.""" config_entry = MockConfigEntry( - domain=const.DOMAIN, + domain=DOMAIN, unique_id=format_mac(TEST_MAC), data={ CONF_HOST: TEST_HOST, CONF_USERNAME: TEST_USERNAME, CONF_PASSWORD: TEST_PASSWORD, CONF_PORT: TEST_PORT, - const.CONF_USE_HTTPS: TEST_USE_HTTPS, + CONF_USE_HTTPS: TEST_USE_HTTPS, }, options={ CONF_PROTOCOL: DEFAULT_PROTOCOL, @@ -471,7 +465,7 @@ async def test_dhcp_ip_update( if not last_update_success: # ensure the last_update_succes is False for the device_coordinator. - reolink_connect.get_states = AsyncMock(side_effect=ReolinkError("Test error")) + reolink_connect.get_states.side_effect = ReolinkError("Test error") freezer.tick(DEVICE_UPDATE_INTERVAL) async_fire_time_changed(hass) await hass.async_block_till_done() @@ -486,7 +480,7 @@ async def test_dhcp_ip_update( setattr(reolink_connect, attr, value) result = await hass.config_entries.flow.async_init( - const.DOMAIN, context={"source": config_entries.SOURCE_DHCP}, data=dhcp_data + DOMAIN, context={"source": config_entries.SOURCE_DHCP}, data=dhcp_data ) for host in host_call_list: diff --git a/tests/components/reolink/test_host.py b/tests/components/reolink/test_host.py index 690bfd035f8425..64c3fe5c1b7000 100644 --- a/tests/components/reolink/test_host.py +++ b/tests/components/reolink/test_host.py @@ -1,28 +1,43 @@ """Test the Reolink host.""" from asyncio import CancelledError -from unittest.mock import AsyncMock, MagicMock +from datetime import timedelta +from unittest.mock import AsyncMock, MagicMock, patch from aiohttp import ClientResponseError +from freezegun.api import FrozenDateTimeFactory import pytest +from reolink_aio.enums import SubType +from reolink_aio.exceptions import NotSupportedError, ReolinkError, SubscriptionError -from homeassistant.components.reolink import const +from homeassistant.components.reolink import DEVICE_UPDATE_INTERVAL +from homeassistant.components.reolink.const import DOMAIN +from homeassistant.components.reolink.host import ( + FIRST_ONVIF_LONG_POLL_TIMEOUT, + FIRST_ONVIF_TIMEOUT, + LONG_POLL_COOLDOWN, + LONG_POLL_ERROR_COOLDOWN, + POLL_INTERVAL_NO_PUSH, +) from homeassistant.components.webhook import async_handle_webhook from homeassistant.config_entries import ConfigEntryState from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er from homeassistant.helpers.dispatcher import async_dispatcher_connect +from homeassistant.helpers.network import NoURLAvailableError from homeassistant.util.aiohttp import MockRequest from .conftest import TEST_UID -from tests.common import MockConfigEntry +from tests.common import MockConfigEntry, async_fire_time_changed +from tests.components.diagnostics import get_diagnostics_for_config_entry from tests.typing import ClientSessionGenerator async def test_webhook_callback( hass: HomeAssistant, hass_client_no_auth: ClientSessionGenerator, + freezer: FrozenDateTimeFactory, config_entry: MockConfigEntry, reolink_connect: MagicMock, entity_registry: er.EntityRegistry, @@ -32,7 +47,7 @@ async def test_webhook_callback( await hass.async_block_till_done() assert config_entry.state is ConfigEntryState.LOADED - webhook_id = f"{const.DOMAIN}_{TEST_UID.replace(':', '')}_ONVIF" + webhook_id = f"{DOMAIN}_{TEST_UID.replace(':', '')}_ONVIF" signal_all = MagicMock() signal_ch = MagicMock() @@ -46,6 +61,10 @@ async def test_webhook_callback( await client.post(f"/api/webhook/{webhook_id}") signal_all.assert_called_once() + freezer.tick(timedelta(seconds=FIRST_ONVIF_TIMEOUT)) + async_fire_time_changed(hass) + await hass.async_block_till_done() + # test webhook callback all channels with failure to read motion_state signal_all.reset_mock() reolink_connect.get_motion_state_all_ch.return_value = False @@ -83,3 +102,285 @@ async def test_webhook_callback( with pytest.raises(CancelledError): await async_handle_webhook(hass, webhook_id, request) signal_all.assert_not_called() + + +async def test_no_mac( + hass: HomeAssistant, + config_entry: MockConfigEntry, + reolink_connect: MagicMock, +) -> None: + """Test setup of host with no mac.""" + reolink_connect.mac_address = None + assert not await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.SETUP_RETRY + + +async def test_subscribe_error( + hass: HomeAssistant, + config_entry: MockConfigEntry, + reolink_connect: MagicMock, +) -> None: + """Test error when subscribing to ONVIF does not block startup.""" + reolink_connect.subscribe.side_effect = ReolinkError("Test Error") + reolink_connect.subscribed.return_value = False + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + +async def test_subscribe_unsuccesfull( + hass: HomeAssistant, + config_entry: MockConfigEntry, + reolink_connect: MagicMock, +) -> None: + """Test that a unsuccessful ONVIF subscription does not block startup.""" + reolink_connect.subscribed.return_value = False + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + +async def test_initial_ONVIF_not_supported( + hass: HomeAssistant, + config_entry: MockConfigEntry, + reolink_connect: MagicMock, +) -> None: + """Test setup when initial ONVIF is not supported.""" + + def test_supported(ch, key): + """Test supported function.""" + if key == "initial_ONVIF_state": + return False + return True + + reolink_connect.supported = test_supported + + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + +async def test_ONVIF_not_supported( + hass: HomeAssistant, + config_entry: MockConfigEntry, + reolink_connect: MagicMock, +) -> None: + """Test setup is not blocked when ONVIF API returns NotSupportedError.""" + + def test_supported(ch, key): + """Test supported function.""" + if key == "initial_ONVIF_state": + return False + return True + + reolink_connect.supported = test_supported + reolink_connect.subscribed.return_value = False + reolink_connect.subscribe.side_effect = NotSupportedError("Test error") + + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + +async def test_renew( + hass: HomeAssistant, + freezer: FrozenDateTimeFactory, + config_entry: MockConfigEntry, + reolink_connect: MagicMock, +) -> None: + """Test renew of the ONVIF subscription.""" + reolink_connect.renewtimer.return_value = 1 + + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + freezer.tick(DEVICE_UPDATE_INTERVAL) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + reolink_connect.renew.assert_called() + + reolink_connect.renew.side_effect = SubscriptionError("Test error") + + freezer.tick(DEVICE_UPDATE_INTERVAL) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + reolink_connect.subscribe.assert_called() + + reolink_connect.subscribe.reset_mock() + reolink_connect.subscribe.side_effect = SubscriptionError("Test error") + + freezer.tick(DEVICE_UPDATE_INTERVAL) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + reolink_connect.subscribe.assert_called() + + +async def test_long_poll_renew_fail( + hass: HomeAssistant, + freezer: FrozenDateTimeFactory, + config_entry: MockConfigEntry, + reolink_connect: MagicMock, +) -> None: + """Test ONVIF long polling errors while renewing.""" + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + reolink_connect.subscribe.side_effect = NotSupportedError("Test error") + + freezer.tick(timedelta(seconds=FIRST_ONVIF_TIMEOUT)) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + # ensure long polling continues + reolink_connect.pull_point_request.assert_called() + + +async def test_register_webhook_errors( + hass: HomeAssistant, + config_entry: MockConfigEntry, + reolink_connect: MagicMock, +) -> None: + """Test errors while registering the webhook.""" + with patch( + "homeassistant.components.reolink.host.get_url", + side_effect=NoURLAvailableError("Test error"), + ): + assert await hass.config_entries.async_setup(config_entry.entry_id) is False + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.SETUP_RETRY + + +async def test_long_poll_stop_when_push( + hass: HomeAssistant, + hass_client_no_auth: ClientSessionGenerator, + freezer: FrozenDateTimeFactory, + config_entry: MockConfigEntry, + reolink_connect: MagicMock, +) -> None: + """Test ONVIF long polling stops when ONVIF push comes in.""" + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + # start ONVIF long polling because ONVIF push did not came in + freezer.tick(timedelta(seconds=FIRST_ONVIF_TIMEOUT)) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + # simulate ONVIF push callback + client = await hass_client_no_auth() + reolink_connect.ONVIF_event_callback.return_value = None + webhook_id = f"{DOMAIN}_{TEST_UID.replace(':', '')}_ONVIF" + await client.post(f"/api/webhook/{webhook_id}") + + freezer.tick(DEVICE_UPDATE_INTERVAL) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + reolink_connect.unsubscribe.assert_called_with(sub_type=SubType.long_poll) + + +async def test_long_poll_errors( + hass: HomeAssistant, + freezer: FrozenDateTimeFactory, + config_entry: MockConfigEntry, + reolink_connect: MagicMock, +) -> None: + """Test errors during ONVIF long polling.""" + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + reolink_connect.pull_point_request.side_effect = ReolinkError("Test error") + + # start ONVIF long polling because ONVIF push did not came in + freezer.tick(timedelta(seconds=FIRST_ONVIF_TIMEOUT)) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + reolink_connect.pull_point_request.assert_called_once() + reolink_connect.pull_point_request.side_effect = Exception("Test error") + + freezer.tick(timedelta(seconds=LONG_POLL_ERROR_COOLDOWN)) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + freezer.tick(timedelta(seconds=LONG_POLL_COOLDOWN)) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + reolink_connect.unsubscribe.assert_called_with(sub_type=SubType.long_poll) + + +async def test_fast_polling_errors( + hass: HomeAssistant, + freezer: FrozenDateTimeFactory, + config_entry: MockConfigEntry, + reolink_connect: MagicMock, +) -> None: + """Test errors during ONVIF fast polling.""" + reolink_connect.get_motion_state_all_ch.side_effect = ReolinkError("Test error") + reolink_connect.pull_point_request.side_effect = ReolinkError("Test error") + + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + # start ONVIF long polling because ONVIF push did not came in + freezer.tick(timedelta(seconds=FIRST_ONVIF_TIMEOUT)) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + # start ONVIF fast polling because ONVIF long polling did not came in + freezer.tick(timedelta(seconds=FIRST_ONVIF_LONG_POLL_TIMEOUT)) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + assert reolink_connect.get_motion_state_all_ch.call_count == 1 + + freezer.tick(timedelta(seconds=POLL_INTERVAL_NO_PUSH)) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + # fast polling continues despite errors + assert reolink_connect.get_motion_state_all_ch.call_count == 2 + + +async def test_diagnostics_event_connection( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + hass_client_no_auth: ClientSessionGenerator, + freezer: FrozenDateTimeFactory, + reolink_connect: MagicMock, + config_entry: MockConfigEntry, +) -> None: + """Test Reolink diagnostics event connection return values.""" + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + diag = await get_diagnostics_for_config_entry(hass, hass_client, config_entry) + assert diag["event connection"] == "Fast polling" + + # start ONVIF long polling because ONVIF push did not came in + freezer.tick(timedelta(seconds=FIRST_ONVIF_TIMEOUT)) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + diag = await get_diagnostics_for_config_entry(hass, hass_client, config_entry) + assert diag["event connection"] == "ONVIF long polling" + + # simulate ONVIF push callback + client = await hass_client_no_auth() + reolink_connect.ONVIF_event_callback.return_value = None + webhook_id = f"{DOMAIN}_{TEST_UID.replace(':', '')}_ONVIF" + await client.post(f"/api/webhook/{webhook_id}") + + diag = await get_diagnostics_for_config_entry(hass, hass_client, config_entry) + assert diag["event connection"] == "ONVIF push" diff --git a/tests/components/reolink/test_init.py b/tests/components/reolink/test_init.py index f5cd56a05d2821..765b3426249602 100644 --- a/tests/components/reolink/test_init.py +++ b/tests/components/reolink/test_init.py @@ -13,8 +13,8 @@ DEVICE_UPDATE_INTERVAL, FIRMWARE_UPDATE_INTERVAL, NUM_CRED_ERRORS, - const, ) +from homeassistant.components.reolink.const import DOMAIN from homeassistant.config import async_process_ha_core_config from homeassistant.config_entries import ConfigEntryState from homeassistant.const import STATE_OFF, STATE_UNAVAILABLE, Platform @@ -108,9 +108,7 @@ async def test_firmware_error_twice( config_entry: MockConfigEntry, ) -> None: """Test when the firmware update fails 2 times.""" - reolink_connect.check_new_firmware = AsyncMock( - side_effect=ReolinkError("Test error") - ) + reolink_connect.check_new_firmware.side_effect = ReolinkError("Test error") with patch("homeassistant.components.reolink.PLATFORMS", [Platform.UPDATE]): assert await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() @@ -140,11 +138,9 @@ async def test_credential_error_three( await hass.async_block_till_done() assert config_entry.state is ConfigEntryState.LOADED - reolink_connect.get_states = AsyncMock( - side_effect=CredentialsInvalidError("Test error") - ) + reolink_connect.get_states.side_effect = CredentialsInvalidError("Test error") - issue_id = f"config_entry_reauth_{const.DOMAIN}_{config_entry.entry_id}" + issue_id = f"config_entry_reauth_{DOMAIN}_{config_entry.entry_id}" for _ in range(NUM_CRED_ERRORS): assert (HOMEASSISTANT_DOMAIN, issue_id) not in issue_registry.issues freezer.tick(DEVICE_UPDATE_INTERVAL) @@ -418,14 +414,14 @@ def mock_supported(ch, capability): reolink_connect.supported = mock_supported dev_entry = device_registry.async_get_or_create( - identifiers={(const.DOMAIN, original_dev_id)}, + identifiers={(DOMAIN, original_dev_id)}, config_entry_id=config_entry.entry_id, disabled_by=None, ) entity_registry.async_get_or_create( domain=domain, - platform=const.DOMAIN, + platform=DOMAIN, unique_id=original_id, config_entry=config_entry, suggested_object_id=original_id, @@ -433,16 +429,13 @@ def mock_supported(ch, capability): device_id=dev_entry.id, ) - assert entity_registry.async_get_entity_id(domain, const.DOMAIN, original_id) - assert entity_registry.async_get_entity_id(domain, const.DOMAIN, new_id) is None + assert entity_registry.async_get_entity_id(domain, DOMAIN, original_id) + assert entity_registry.async_get_entity_id(domain, DOMAIN, new_id) is None - assert device_registry.async_get_device( - identifiers={(const.DOMAIN, original_dev_id)} - ) + assert device_registry.async_get_device(identifiers={(DOMAIN, original_dev_id)}) if new_dev_id != original_dev_id: assert ( - device_registry.async_get_device(identifiers={(const.DOMAIN, new_dev_id)}) - is None + device_registry.async_get_device(identifiers={(DOMAIN, new_dev_id)}) is None ) # setup CH 0 and host entities/device @@ -450,19 +443,15 @@ def mock_supported(ch, capability): assert await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() - assert ( - entity_registry.async_get_entity_id(domain, const.DOMAIN, original_id) is None - ) - assert entity_registry.async_get_entity_id(domain, const.DOMAIN, new_id) + assert entity_registry.async_get_entity_id(domain, DOMAIN, original_id) is None + assert entity_registry.async_get_entity_id(domain, DOMAIN, new_id) if new_dev_id != original_dev_id: assert ( - device_registry.async_get_device( - identifiers={(const.DOMAIN, original_dev_id)} - ) + device_registry.async_get_device(identifiers={(DOMAIN, original_dev_id)}) is None ) - assert device_registry.async_get_device(identifiers={(const.DOMAIN, new_dev_id)}) + assert device_registry.async_get_device(identifiers={(DOMAIN, new_dev_id)}) async def test_no_repair_issue( @@ -476,11 +465,11 @@ async def test_no_repair_issue( assert await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() - assert (const.DOMAIN, "https_webhook") not in issue_registry.issues - assert (const.DOMAIN, "webhook_url") not in issue_registry.issues - assert (const.DOMAIN, "enable_port") not in issue_registry.issues - assert (const.DOMAIN, "firmware_update") not in issue_registry.issues - assert (const.DOMAIN, "ssl") not in issue_registry.issues + assert (DOMAIN, "https_webhook") not in issue_registry.issues + assert (DOMAIN, "webhook_url") not in issue_registry.issues + assert (DOMAIN, "enable_port") not in issue_registry.issues + assert (DOMAIN, "firmware_update") not in issue_registry.issues + assert (DOMAIN, "ssl") not in issue_registry.issues async def test_https_repair_issue( @@ -507,7 +496,7 @@ async def test_https_repair_issue( assert await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() - assert (const.DOMAIN, "https_webhook") in issue_registry.issues + assert (DOMAIN, "https_webhook") in issue_registry.issues async def test_ssl_repair_issue( @@ -537,7 +526,7 @@ async def test_ssl_repair_issue( assert await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() - assert (const.DOMAIN, "ssl") in issue_registry.issues + assert (DOMAIN, "ssl") in issue_registry.issues @pytest.mark.parametrize("protocol", ["rtsp", "rtmp"]) @@ -549,7 +538,7 @@ async def test_port_repair_issue( issue_registry: ir.IssueRegistry, ) -> None: """Test repairs issue is raised when auto enable of ports fails.""" - reolink_connect.set_net_port = AsyncMock(side_effect=ReolinkError("Test error")) + reolink_connect.set_net_port.side_effect = ReolinkError("Test error") reolink_connect.onvif_enabled = False reolink_connect.rtsp_enabled = False reolink_connect.rtmp_enabled = False @@ -557,7 +546,7 @@ async def test_port_repair_issue( assert await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() - assert (const.DOMAIN, "enable_port") in issue_registry.issues + assert (DOMAIN, "enable_port") in issue_registry.issues async def test_webhook_repair_issue( @@ -580,7 +569,7 @@ async def test_webhook_repair_issue( assert await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() - assert (const.DOMAIN, "webhook_url") in issue_registry.issues + assert (DOMAIN, "webhook_url") in issue_registry.issues async def test_firmware_repair_issue( @@ -594,4 +583,4 @@ async def test_firmware_repair_issue( assert await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() - assert (const.DOMAIN, "firmware_update_host") in issue_registry.issues + assert (DOMAIN, "firmware_update_host") in issue_registry.issues diff --git a/tests/components/reolink/test_light.py b/tests/components/reolink/test_light.py new file mode 100644 index 00000000000000..c495a0ff25e2e3 --- /dev/null +++ b/tests/components/reolink/test_light.py @@ -0,0 +1,146 @@ +"""Test the Reolink light platform.""" + +from unittest.mock import MagicMock, call, patch + +import pytest +from reolink_aio.exceptions import InvalidParameterError, ReolinkError + +from homeassistant.components.light import ATTR_BRIGHTNESS, DOMAIN as LIGHT_DOMAIN +from homeassistant.config_entries import ConfigEntryState +from homeassistant.const import ( + ATTR_ENTITY_ID, + SERVICE_TURN_OFF, + SERVICE_TURN_ON, + STATE_ON, + Platform, +) +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError + +from .conftest import TEST_NVR_NAME + +from tests.common import MockConfigEntry + + +async def test_light_state( + hass: HomeAssistant, + config_entry: MockConfigEntry, + reolink_connect: MagicMock, +) -> None: + """Test light entity state with floodlight.""" + reolink_connect.whiteled_state.return_value = True + reolink_connect.whiteled_brightness.return_value = 100 + + with patch("homeassistant.components.reolink.PLATFORMS", [Platform.LIGHT]): + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + entity_id = f"{Platform.LIGHT}.{TEST_NVR_NAME}_floodlight" + + state = hass.states.get(entity_id) + assert state.state == STATE_ON + assert state.attributes["brightness"] == 255 + + +async def test_light_brightness_none( + hass: HomeAssistant, + config_entry: MockConfigEntry, + reolink_connect: MagicMock, +) -> None: + """Test light entity with floodlight and brightness returning None.""" + reolink_connect.whiteled_state.return_value = True + reolink_connect.whiteled_brightness.return_value = None + + with patch("homeassistant.components.reolink.PLATFORMS", [Platform.LIGHT]): + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + entity_id = f"{Platform.LIGHT}.{TEST_NVR_NAME}_floodlight" + + state = hass.states.get(entity_id) + assert state.state == STATE_ON + assert state.attributes["brightness"] is None + + +async def test_light_turn_off( + hass: HomeAssistant, + config_entry: MockConfigEntry, + reolink_connect: MagicMock, +) -> None: + """Test light turn off service.""" + with patch("homeassistant.components.reolink.PLATFORMS", [Platform.LIGHT]): + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + entity_id = f"{Platform.LIGHT}.{TEST_NVR_NAME}_floodlight" + + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: entity_id}, + blocking=True, + ) + reolink_connect.set_whiteled.assert_called_with(0, state=False) + + reolink_connect.set_whiteled.side_effect = ReolinkError("Test error") + with pytest.raises(HomeAssistantError): + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: entity_id}, + blocking=True, + ) + + +async def test_light_turn_on( + hass: HomeAssistant, + config_entry: MockConfigEntry, + reolink_connect: MagicMock, +) -> None: + """Test light turn on service.""" + with patch("homeassistant.components.reolink.PLATFORMS", [Platform.LIGHT]): + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + entity_id = f"{Platform.LIGHT}.{TEST_NVR_NAME}_floodlight" + + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: entity_id, ATTR_BRIGHTNESS: 51}, + blocking=True, + ) + reolink_connect.set_whiteled.assert_has_calls( + [call(0, brightness=20), call(0, state=True)] + ) + + reolink_connect.set_whiteled.side_effect = ReolinkError("Test error") + with pytest.raises(HomeAssistantError): + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: entity_id}, + blocking=True, + ) + + reolink_connect.set_whiteled.side_effect = ReolinkError("Test error") + with pytest.raises(HomeAssistantError): + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: entity_id, ATTR_BRIGHTNESS: 51}, + blocking=True, + ) + + reolink_connect.set_whiteled.side_effect = InvalidParameterError("Test error") + with pytest.raises(HomeAssistantError): + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: entity_id, ATTR_BRIGHTNESS: 51}, + blocking=True, + ) diff --git a/tests/components/reolink/test_media_source.py b/tests/components/reolink/test_media_source.py index b09c267fcfd8f0..6351f683545ec4 100644 --- a/tests/components/reolink/test_media_source.py +++ b/tests/components/reolink/test_media_source.py @@ -14,9 +14,8 @@ async_resolve_media, ) from homeassistant.components.media_source.error import Unresolvable -from homeassistant.components.reolink import const from homeassistant.components.reolink.config_flow import DEFAULT_PROTOCOL -from homeassistant.components.reolink.const import DOMAIN +from homeassistant.components.reolink.const import CONF_USE_HTTPS, DOMAIN from homeassistant.components.stream import DOMAIN as MEDIA_STREAM_DOMAIN from homeassistant.const import ( CONF_HOST, @@ -130,7 +129,7 @@ async def test_browsing( ) -> None: """Test browsing the Reolink three.""" entry_id = config_entry.entry_id - reolink_connect.api_version.return_value = 1 + reolink_connect.supported.return_value = 1 reolink_connect.model = "Reolink TrackMix PoE" with patch("homeassistant.components.reolink.PLATFORMS", [Platform.CAMERA]): @@ -162,7 +161,7 @@ async def test_browsing( browse_res_AT_sub_id = f"RES|{entry_id}|{TEST_CHANNEL}|autotrack_sub" browse_res_AT_main_id = f"RES|{entry_id}|{TEST_CHANNEL}|autotrack_main" assert browse.domain == DOMAIN - assert browse.title == TEST_NVR_NAME + assert browse.title == f"{TEST_NVR_NAME} lens 0" assert browse.identifier == browse_resolution_id assert browse.children[0].identifier == browse_res_sub_id assert browse.children[1].identifier == browse_res_main_id @@ -178,19 +177,19 @@ async def test_browsing( browse = await async_browse_media(hass, f"{URI_SCHEME}{DOMAIN}/{browse_res_sub_id}") assert browse.domain == DOMAIN - assert browse.title == f"{TEST_NVR_NAME} Low res." + assert browse.title == f"{TEST_NVR_NAME} lens 0 Low res." browse = await async_browse_media( hass, f"{URI_SCHEME}{DOMAIN}/{browse_res_AT_sub_id}" ) assert browse.domain == DOMAIN - assert browse.title == f"{TEST_NVR_NAME} Autotrack low res." + assert browse.title == f"{TEST_NVR_NAME} lens 0 Autotrack low res." browse = await async_browse_media( hass, f"{URI_SCHEME}{DOMAIN}/{browse_res_AT_main_id}" ) assert browse.domain == DOMAIN - assert browse.title == f"{TEST_NVR_NAME} Autotrack high res." + assert browse.title == f"{TEST_NVR_NAME} lens 0 Autotrack high res." browse = await async_browse_media( hass, f"{URI_SCHEME}{DOMAIN}/{browse_res_main_id}" @@ -200,7 +199,7 @@ async def test_browsing( browse_day_0_id = f"DAY|{entry_id}|{TEST_CHANNEL}|{TEST_STREAM}|{TEST_YEAR}|{TEST_MONTH}|{TEST_DAY}" browse_day_1_id = f"DAY|{entry_id}|{TEST_CHANNEL}|{TEST_STREAM}|{TEST_YEAR}|{TEST_MONTH}|{TEST_DAY2}" assert browse.domain == DOMAIN - assert browse.title == f"{TEST_NVR_NAME} High res." + assert browse.title == f"{TEST_NVR_NAME} lens 0 High res." assert browse.identifier == browse_days_id assert browse.children[0].identifier == browse_day_0_id assert browse.children[1].identifier == browse_day_1_id @@ -220,7 +219,8 @@ async def test_browsing( browse_file_id = f"FILE|{entry_id}|{TEST_CHANNEL}|{TEST_STREAM}|{TEST_FILE_NAME}" assert browse.domain == DOMAIN assert ( - browse.title == f"{TEST_NVR_NAME} High res. {TEST_YEAR}/{TEST_MONTH}/{TEST_DAY}" + browse.title + == f"{TEST_NVR_NAME} lens 0 High res. {TEST_YEAR}/{TEST_MONTH}/{TEST_DAY}" ) assert browse.identifier == browse_files_id assert browse.children[0].identifier == browse_file_id @@ -272,7 +272,7 @@ async def test_browsing_rec_playback_unsupported( config_entry: MockConfigEntry, ) -> None: """Test browsing a Reolink camera which does not support playback of recordings.""" - reolink_connect.api_version.return_value = 0 + reolink_connect.supported.return_value = 0 with patch("homeassistant.components.reolink.PLATFORMS", [Platform.CAMERA]): assert await hass.config_entries.async_setup(config_entry.entry_id) @@ -293,7 +293,7 @@ async def test_browsing_errors( config_entry: MockConfigEntry, ) -> None: """Test browsing a Reolink camera errors.""" - reolink_connect.api_version.return_value = 1 + reolink_connect.supported.return_value = 1 with patch("homeassistant.components.reolink.PLATFORMS", [Platform.CAMERA]): assert await hass.config_entries.async_setup(config_entry.entry_id) @@ -312,22 +312,22 @@ async def test_browsing_not_loaded( config_entry: MockConfigEntry, ) -> None: """Test browsing a Reolink camera integration which is not loaded.""" - reolink_connect.api_version.return_value = 1 + reolink_connect.supported.return_value = 1 with patch("homeassistant.components.reolink.PLATFORMS", [Platform.CAMERA]): assert await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() - reolink_connect.get_host_data = AsyncMock(side_effect=ReolinkError("Test error")) + reolink_connect.get_host_data.side_effect = ReolinkError("Test error") config_entry2 = MockConfigEntry( - domain=const.DOMAIN, + domain=DOMAIN, unique_id=format_mac(TEST_MAC2), data={ CONF_HOST: TEST_HOST2, CONF_USERNAME: TEST_USERNAME2, CONF_PASSWORD: TEST_PASSWORD2, CONF_PORT: TEST_PORT, - const.CONF_USE_HTTPS: TEST_USE_HTTPS, + CONF_USE_HTTPS: TEST_USE_HTTPS, }, options={ CONF_PROTOCOL: DEFAULT_PROTOCOL, diff --git a/tests/components/reolink/test_number.py b/tests/components/reolink/test_number.py new file mode 100644 index 00000000000000..e9abcec946c0df --- /dev/null +++ b/tests/components/reolink/test_number.py @@ -0,0 +1,111 @@ +"""Test the Reolink number platform.""" + +from unittest.mock import AsyncMock, MagicMock, patch + +import pytest +from reolink_aio.api import Chime +from reolink_aio.exceptions import InvalidParameterError, ReolinkError + +from homeassistant.components.number import ( + ATTR_VALUE, + DOMAIN as NUMBER_DOMAIN, + SERVICE_SET_VALUE, +) +from homeassistant.config_entries import ConfigEntryState +from homeassistant.const import ATTR_ENTITY_ID, Platform +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError + +from .conftest import TEST_NVR_NAME + +from tests.common import MockConfigEntry + + +async def test_number( + hass: HomeAssistant, + config_entry: MockConfigEntry, + reolink_connect: MagicMock, +) -> None: + """Test number entity with volume.""" + reolink_connect.volume.return_value = 80 + + with patch("homeassistant.components.reolink.PLATFORMS", [Platform.NUMBER]): + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + entity_id = f"{Platform.NUMBER}.{TEST_NVR_NAME}_volume" + + assert hass.states.get(entity_id).state == "80" + + await hass.services.async_call( + NUMBER_DOMAIN, + SERVICE_SET_VALUE, + {ATTR_ENTITY_ID: entity_id, ATTR_VALUE: 50}, + blocking=True, + ) + reolink_connect.set_volume.assert_called_with(0, volume=50) + + reolink_connect.set_volume.side_effect = ReolinkError("Test error") + with pytest.raises(HomeAssistantError): + await hass.services.async_call( + NUMBER_DOMAIN, + SERVICE_SET_VALUE, + {ATTR_ENTITY_ID: entity_id, ATTR_VALUE: 50}, + blocking=True, + ) + + reolink_connect.set_volume.side_effect = InvalidParameterError("Test error") + with pytest.raises(HomeAssistantError): + await hass.services.async_call( + NUMBER_DOMAIN, + SERVICE_SET_VALUE, + {ATTR_ENTITY_ID: entity_id, ATTR_VALUE: 50}, + blocking=True, + ) + + +async def test_chime_number( + hass: HomeAssistant, + config_entry: MockConfigEntry, + reolink_connect: MagicMock, + test_chime: Chime, +) -> None: + """Test number entity of a chime with chime volume.""" + test_chime.volume = 3 + + with patch("homeassistant.components.reolink.PLATFORMS", [Platform.NUMBER]): + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + entity_id = f"{Platform.NUMBER}.test_chime_volume" + + assert hass.states.get(entity_id).state == "3" + + test_chime.set_option = AsyncMock() + await hass.services.async_call( + NUMBER_DOMAIN, + SERVICE_SET_VALUE, + {ATTR_ENTITY_ID: entity_id, ATTR_VALUE: 2}, + blocking=True, + ) + test_chime.set_option.assert_called_with(volume=2) + + test_chime.set_option.side_effect = ReolinkError("Test error") + with pytest.raises(HomeAssistantError): + await hass.services.async_call( + NUMBER_DOMAIN, + SERVICE_SET_VALUE, + {ATTR_ENTITY_ID: entity_id, ATTR_VALUE: 1}, + blocking=True, + ) + + test_chime.set_option.side_effect = InvalidParameterError("Test error") + with pytest.raises(HomeAssistantError): + await hass.services.async_call( + NUMBER_DOMAIN, + SERVICE_SET_VALUE, + {ATTR_ENTITY_ID: entity_id, ATTR_VALUE: 1}, + blocking=True, + ) diff --git a/tests/components/reolink/test_select.py b/tests/components/reolink/test_select.py index 5536e85afb93f8..0534f36f4c5f56 100644 --- a/tests/components/reolink/test_select.py +++ b/tests/components/reolink/test_select.py @@ -41,7 +41,6 @@ async def test_floodlight_mode_select( entity_id = f"{Platform.SELECT}.{TEST_NVR_NAME}_floodlight_mode" assert hass.states.get(entity_id).state == "auto" - reolink_connect.set_whiteled = AsyncMock() await hass.services.async_call( SELECT_DOMAIN, SERVICE_SELECT_OPTION, @@ -50,7 +49,7 @@ async def test_floodlight_mode_select( ) reolink_connect.set_whiteled.assert_called_once() - reolink_connect.set_whiteled = AsyncMock(side_effect=ReolinkError("Test error")) + reolink_connect.set_whiteled.side_effect = ReolinkError("Test error") with pytest.raises(HomeAssistantError): await hass.services.async_call( SELECT_DOMAIN, @@ -59,9 +58,7 @@ async def test_floodlight_mode_select( blocking=True, ) - reolink_connect.set_whiteled = AsyncMock( - side_effect=InvalidParameterError("Test error") - ) + reolink_connect.set_whiteled.side_effect = InvalidParameterError("Test error") with pytest.raises(ServiceValidationError): await hass.services.async_call( SELECT_DOMAIN, @@ -94,7 +91,6 @@ async def test_play_quick_reply_message( entity_id = f"{Platform.SELECT}.{TEST_NVR_NAME}_play_quick_reply_message" assert hass.states.get(entity_id).state == STATE_UNKNOWN - reolink_connect.play_quick_reply = AsyncMock() await hass.services.async_call( SELECT_DOMAIN, SERVICE_SELECT_OPTION, @@ -122,6 +118,7 @@ async def test_chime_select( entity_id = f"{Platform.SELECT}.test_chime_visitor_ringtone" assert hass.states.get(entity_id).state == "pianokey" + # Test selecting chime ringtone option test_chime.set_tone = AsyncMock() await hass.services.async_call( SELECT_DOMAIN, @@ -131,7 +128,7 @@ async def test_chime_select( ) test_chime.set_tone.assert_called_once() - test_chime.set_tone = AsyncMock(side_effect=ReolinkError("Test error")) + test_chime.set_tone.side_effect = ReolinkError("Test error") with pytest.raises(HomeAssistantError): await hass.services.async_call( SELECT_DOMAIN, @@ -140,7 +137,7 @@ async def test_chime_select( blocking=True, ) - test_chime.set_tone = AsyncMock(side_effect=InvalidParameterError("Test error")) + test_chime.set_tone.side_effect = InvalidParameterError("Test error") with pytest.raises(ServiceValidationError): await hass.services.async_call( SELECT_DOMAIN, @@ -149,6 +146,7 @@ async def test_chime_select( blocking=True, ) + # Test unavailable test_chime.event_info = {} freezer.tick(DEVICE_UPDATE_INTERVAL) async_fire_time_changed(hass) diff --git a/tests/components/reolink/test_sensor.py b/tests/components/reolink/test_sensor.py new file mode 100644 index 00000000000000..df16463435573c --- /dev/null +++ b/tests/components/reolink/test_sensor.py @@ -0,0 +1,62 @@ +"""Test the Reolink sensor platform.""" + +from unittest.mock import MagicMock, patch + +import pytest + +from homeassistant.config_entries import ConfigEntryState +from homeassistant.const import STATE_UNAVAILABLE, Platform +from homeassistant.core import HomeAssistant + +from .conftest import TEST_NVR_NAME + +from tests.common import MockConfigEntry + + +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_sensors( + hass: HomeAssistant, + config_entry: MockConfigEntry, + reolink_connect: MagicMock, +) -> None: + """Test sensor entities.""" + reolink_connect.ptz_pan_position.return_value = 1200 + reolink_connect.wifi_connection = True + reolink_connect.wifi_signal = 3 + reolink_connect.hdd_list = [0] + reolink_connect.hdd_storage.return_value = 95 + + with patch("homeassistant.components.reolink.PLATFORMS", [Platform.SENSOR]): + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + entity_id = f"{Platform.SENSOR}.{TEST_NVR_NAME}_ptz_pan_position" + assert hass.states.get(entity_id).state == "1200" + + entity_id = f"{Platform.SENSOR}.{TEST_NVR_NAME}_wi_fi_signal" + assert hass.states.get(entity_id).state == "3" + + entity_id = f"{Platform.SENSOR}.{TEST_NVR_NAME}_sd_0_storage" + assert hass.states.get(entity_id).state == "95" + + +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_hdd_sensors( + hass: HomeAssistant, + config_entry: MockConfigEntry, + reolink_connect: MagicMock, +) -> None: + """Test hdd sensor entity.""" + reolink_connect.hdd_list = [0] + reolink_connect.hdd_type.return_value = "HDD" + reolink_connect.hdd_storage.return_value = 85 + reolink_connect.hdd_available.return_value = False + + with patch("homeassistant.components.reolink.PLATFORMS", [Platform.SENSOR]): + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + entity_id = f"{Platform.SENSOR}.{TEST_NVR_NAME}_hdd_0_storage" + assert hass.states.get(entity_id).state == STATE_UNAVAILABLE diff --git a/tests/components/reolink/test_services.py b/tests/components/reolink/test_services.py new file mode 100644 index 00000000000000..a4b7d8f0da4eb1 --- /dev/null +++ b/tests/components/reolink/test_services.py @@ -0,0 +1,116 @@ +"""Test the Reolink services.""" + +from unittest.mock import AsyncMock, MagicMock, patch + +import pytest +from reolink_aio.api import Chime +from reolink_aio.exceptions import InvalidParameterError, ReolinkError + +from homeassistant.components.reolink.const import DOMAIN as REOLINK_DOMAIN +from homeassistant.components.reolink.services import ATTR_RINGTONE +from homeassistant.config_entries import ConfigEntryState +from homeassistant.const import ATTR_DEVICE_ID, Platform +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError, ServiceValidationError +from homeassistant.helpers import entity_registry as er + +from tests.common import MockConfigEntry + + +async def test_play_chime_service_entity( + hass: HomeAssistant, + config_entry: MockConfigEntry, + reolink_connect: MagicMock, + test_chime: Chime, + entity_registry: er.EntityRegistry, +) -> None: + """Test chime play service.""" + with patch("homeassistant.components.reolink.PLATFORMS", [Platform.SELECT]): + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + assert config_entry.state is ConfigEntryState.LOADED + + entity_id = f"{Platform.SELECT}.test_chime_visitor_ringtone" + entity = entity_registry.async_get(entity_id) + assert entity is not None + device_id = entity.device_id + + # Test chime play service with device + test_chime.play = AsyncMock() + await hass.services.async_call( + REOLINK_DOMAIN, + "play_chime", + {ATTR_DEVICE_ID: [device_id], ATTR_RINGTONE: "attraction"}, + blocking=True, + ) + test_chime.play.assert_called_once() + + # Test errors + with pytest.raises(ServiceValidationError): + await hass.services.async_call( + REOLINK_DOMAIN, + "play_chime", + {ATTR_DEVICE_ID: ["invalid_id"], ATTR_RINGTONE: "attraction"}, + blocking=True, + ) + + test_chime.play = AsyncMock(side_effect=ReolinkError("Test error")) + with pytest.raises(HomeAssistantError): + await hass.services.async_call( + REOLINK_DOMAIN, + "play_chime", + {ATTR_DEVICE_ID: [device_id], ATTR_RINGTONE: "attraction"}, + blocking=True, + ) + + test_chime.play = AsyncMock(side_effect=InvalidParameterError("Test error")) + with pytest.raises(ServiceValidationError): + await hass.services.async_call( + REOLINK_DOMAIN, + "play_chime", + {ATTR_DEVICE_ID: [device_id], ATTR_RINGTONE: "attraction"}, + blocking=True, + ) + + reolink_connect.chime.return_value = None + with pytest.raises(ServiceValidationError): + await hass.services.async_call( + REOLINK_DOMAIN, + "play_chime", + {ATTR_DEVICE_ID: [device_id], ATTR_RINGTONE: "attraction"}, + blocking=True, + ) + + +async def test_play_chime_service_unloaded( + hass: HomeAssistant, + config_entry: MockConfigEntry, + reolink_connect: MagicMock, + test_chime: Chime, + entity_registry: er.EntityRegistry, +) -> None: + """Test chime play service when config entry is unloaded.""" + with patch("homeassistant.components.reolink.PLATFORMS", [Platform.SELECT]): + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + entity_id = f"{Platform.SELECT}.test_chime_visitor_ringtone" + entity = entity_registry.async_get(entity_id) + assert entity is not None + device_id = entity.device_id + + # Unload the config entry + assert await hass.config_entries.async_unload(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.NOT_LOADED + + # Test chime play service + with pytest.raises(ServiceValidationError): + await hass.services.async_call( + REOLINK_DOMAIN, + "play_chime", + {ATTR_DEVICE_ID: [device_id], ATTR_RINGTONE: "attraction"}, + blocking=True, + ) diff --git a/tests/components/reolink/test_siren.py b/tests/components/reolink/test_siren.py new file mode 100644 index 00000000000000..0d9d3e0b8000fc --- /dev/null +++ b/tests/components/reolink/test_siren.py @@ -0,0 +1,134 @@ +"""Test the Reolink siren platform.""" + +from typing import Any +from unittest.mock import AsyncMock, MagicMock, patch + +import pytest +from reolink_aio.exceptions import InvalidParameterError, ReolinkError + +from homeassistant.components.siren import ( + ATTR_DURATION, + ATTR_VOLUME_LEVEL, + DOMAIN as SIREN_DOMAIN, +) +from homeassistant.config_entries import ConfigEntryState +from homeassistant.const import ( + ATTR_ENTITY_ID, + SERVICE_TURN_OFF, + SERVICE_TURN_ON, + STATE_UNKNOWN, + Platform, +) +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError, ServiceValidationError + +from .conftest import TEST_NVR_NAME + +from tests.common import MockConfigEntry + + +async def test_siren( + hass: HomeAssistant, + config_entry: MockConfigEntry, + reolink_connect: MagicMock, +) -> None: + """Test siren entity.""" + with patch("homeassistant.components.reolink.PLATFORMS", [Platform.SIREN]): + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + entity_id = f"{Platform.SIREN}.{TEST_NVR_NAME}_siren" + assert hass.states.get(entity_id).state == STATE_UNKNOWN + + # test siren turn on + await hass.services.async_call( + SIREN_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: entity_id}, + blocking=True, + ) + reolink_connect.set_volume.assert_not_called() + reolink_connect.set_siren.assert_called_with(0, True, None) + + await hass.services.async_call( + SIREN_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: entity_id, ATTR_VOLUME_LEVEL: 0.85, ATTR_DURATION: 2}, + blocking=True, + ) + reolink_connect.set_volume.assert_called_with(0, volume=85) + reolink_connect.set_siren.assert_called_with(0, True, 2) + + # test siren turn off + reolink_connect.set_siren.side_effect = None + await hass.services.async_call( + SIREN_DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: entity_id}, + blocking=True, + ) + reolink_connect.set_siren.assert_called_with(0, False, None) + + +@pytest.mark.parametrize("attr", ["set_volume", "set_siren"]) +@pytest.mark.parametrize( + ("value", "expected"), + [ + ( + AsyncMock(side_effect=ReolinkError("Test error")), + HomeAssistantError, + ), + ( + AsyncMock(side_effect=InvalidParameterError("Test error")), + ServiceValidationError, + ), + ], +) +async def test_siren_turn_on_errors( + hass: HomeAssistant, + config_entry: MockConfigEntry, + reolink_connect: MagicMock, + attr: str, + value: Any, + expected: Any, +) -> None: + """Test errors when calling siren turn on service.""" + with patch("homeassistant.components.reolink.PLATFORMS", [Platform.SIREN]): + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + entity_id = f"{Platform.SIREN}.{TEST_NVR_NAME}_siren" + + setattr(reolink_connect, attr, value) + with pytest.raises(expected): + await hass.services.async_call( + SIREN_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: entity_id, ATTR_VOLUME_LEVEL: 0.85, ATTR_DURATION: 2}, + blocking=True, + ) + + +async def test_siren_turn_off_errors( + hass: HomeAssistant, + config_entry: MockConfigEntry, + reolink_connect: MagicMock, +) -> None: + """Test errors when calling siren turn off service.""" + with patch("homeassistant.components.reolink.PLATFORMS", [Platform.SIREN]): + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + entity_id = f"{Platform.SIREN}.{TEST_NVR_NAME}_siren" + + reolink_connect.set_siren.side_effect = ReolinkError("Test error") + with pytest.raises(HomeAssistantError): + await hass.services.async_call( + SIREN_DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: entity_id}, + blocking=True, + ) diff --git a/tests/components/reolink/test_switch.py b/tests/components/reolink/test_switch.py index ebf805b593db47..7f8d606555d975 100644 --- a/tests/components/reolink/test_switch.py +++ b/tests/components/reolink/test_switch.py @@ -1,15 +1,31 @@ """Test the Reolink switch platform.""" -from unittest.mock import MagicMock, patch +from unittest.mock import AsyncMock, MagicMock, patch -from homeassistant.components.reolink import const -from homeassistant.const import Platform +from freezegun.api import FrozenDateTimeFactory +import pytest +from reolink_aio.api import Chime +from reolink_aio.exceptions import ReolinkError + +from homeassistant.components.reolink import DEVICE_UPDATE_INTERVAL +from homeassistant.components.reolink.const import DOMAIN +from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN +from homeassistant.config_entries import ConfigEntryState +from homeassistant.const import ( + ATTR_ENTITY_ID, + SERVICE_TURN_OFF, + SERVICE_TURN_ON, + STATE_OFF, + STATE_ON, + Platform, +) from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import entity_registry as er, issue_registry as ir -from .conftest import TEST_UID +from .conftest import TEST_CAM_NAME, TEST_NVR_NAME, TEST_UID -from tests.common import MockConfigEntry +from tests.common import MockConfigEntry, async_fire_time_changed async def test_cleanup_hdr_switch_( @@ -27,23 +43,21 @@ async def test_cleanup_hdr_switch_( entity_registry.async_get_or_create( domain=domain, - platform=const.DOMAIN, + platform=DOMAIN, unique_id=original_id, config_entry=config_entry, suggested_object_id=original_id, disabled_by=er.RegistryEntryDisabler.USER, ) - assert entity_registry.async_get_entity_id(domain, const.DOMAIN, original_id) + assert entity_registry.async_get_entity_id(domain, DOMAIN, original_id) # setup CH 0 and host entities/device with patch("homeassistant.components.reolink.PLATFORMS", [domain]): assert await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() - assert ( - entity_registry.async_get_entity_id(domain, const.DOMAIN, original_id) is None - ) + assert entity_registry.async_get_entity_id(domain, DOMAIN, original_id) is None async def test_hdr_switch_deprecated_repair_issue( @@ -62,20 +76,206 @@ async def test_hdr_switch_deprecated_repair_issue( entity_registry.async_get_or_create( domain=domain, - platform=const.DOMAIN, + platform=DOMAIN, unique_id=original_id, config_entry=config_entry, suggested_object_id=original_id, disabled_by=None, ) - assert entity_registry.async_get_entity_id(domain, const.DOMAIN, original_id) + assert entity_registry.async_get_entity_id(domain, DOMAIN, original_id) # setup CH 0 and host entities/device with patch("homeassistant.components.reolink.PLATFORMS", [domain]): assert await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() - assert entity_registry.async_get_entity_id(domain, const.DOMAIN, original_id) + assert entity_registry.async_get_entity_id(domain, DOMAIN, original_id) + + assert (DOMAIN, "hdr_switch_deprecated") in issue_registry.issues + + +async def test_switch( + hass: HomeAssistant, + config_entry: MockConfigEntry, + freezer: FrozenDateTimeFactory, + reolink_connect: MagicMock, +) -> None: + """Test switch entity.""" + reolink_connect.camera_name.return_value = TEST_CAM_NAME + + with patch("homeassistant.components.reolink.PLATFORMS", [Platform.SWITCH]): + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + entity_id = f"{Platform.SWITCH}.{TEST_CAM_NAME}_record" + assert hass.states.get(entity_id).state == STATE_ON + + reolink_connect.recording_enabled.return_value = False + freezer.tick(DEVICE_UPDATE_INTERVAL) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + assert hass.states.get(entity_id).state == STATE_OFF + + # test switch turn on + await hass.services.async_call( + SWITCH_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: entity_id}, + blocking=True, + ) + reolink_connect.set_recording.assert_called_with(0, True) + + reolink_connect.set_recording.side_effect = ReolinkError("Test error") + with pytest.raises(HomeAssistantError): + await hass.services.async_call( + SWITCH_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: entity_id}, + blocking=True, + ) + + # test switch turn off + reolink_connect.set_recording.side_effect = None + await hass.services.async_call( + SWITCH_DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: entity_id}, + blocking=True, + ) + reolink_connect.set_recording.assert_called_with(0, False) + + reolink_connect.set_recording.side_effect = ReolinkError("Test error") + with pytest.raises(HomeAssistantError): + await hass.services.async_call( + SWITCH_DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: entity_id}, + blocking=True, + ) + + +async def test_host_switch( + hass: HomeAssistant, + config_entry: MockConfigEntry, + freezer: FrozenDateTimeFactory, + reolink_connect: MagicMock, +) -> None: + """Test host switch entity.""" + reolink_connect.camera_name.return_value = TEST_CAM_NAME + + with patch("homeassistant.components.reolink.PLATFORMS", [Platform.SWITCH]): + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + entity_id = f"{Platform.SWITCH}.{TEST_NVR_NAME}_record" + assert hass.states.get(entity_id).state == STATE_ON + + reolink_connect.recording_enabled.return_value = False + freezer.tick(DEVICE_UPDATE_INTERVAL) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + assert hass.states.get(entity_id).state == STATE_OFF + + # test switch turn on + await hass.services.async_call( + SWITCH_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: entity_id}, + blocking=True, + ) + reolink_connect.set_recording.assert_called_with(None, True) + + reolink_connect.set_recording.side_effect = ReolinkError("Test error") + with pytest.raises(HomeAssistantError): + await hass.services.async_call( + SWITCH_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: entity_id}, + blocking=True, + ) + + # test switch turn off + reolink_connect.set_recording.side_effect = None + await hass.services.async_call( + SWITCH_DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: entity_id}, + blocking=True, + ) + reolink_connect.set_recording.assert_called_with(None, False) + + reolink_connect.set_recording.side_effect = ReolinkError("Test error") + with pytest.raises(HomeAssistantError): + await hass.services.async_call( + SWITCH_DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: entity_id}, + blocking=True, + ) + + +async def test_chime_switch( + hass: HomeAssistant, + config_entry: MockConfigEntry, + freezer: FrozenDateTimeFactory, + reolink_connect: MagicMock, + test_chime: Chime, +) -> None: + """Test host switch entity.""" + with patch("homeassistant.components.reolink.PLATFORMS", [Platform.SWITCH]): + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + entity_id = f"{Platform.SWITCH}.test_chime_led" + assert hass.states.get(entity_id).state == STATE_ON + + test_chime.led_state = False + freezer.tick(DEVICE_UPDATE_INTERVAL) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + assert hass.states.get(entity_id).state == STATE_OFF + + # test switch turn on + test_chime.set_option = AsyncMock() + await hass.services.async_call( + SWITCH_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: entity_id}, + blocking=True, + ) + test_chime.set_option.assert_called_with(led=True) + + test_chime.set_option.side_effect = ReolinkError("Test error") + with pytest.raises(HomeAssistantError): + await hass.services.async_call( + SWITCH_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: entity_id}, + blocking=True, + ) + + # test switch turn off + test_chime.set_option.side_effect = None + await hass.services.async_call( + SWITCH_DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: entity_id}, + blocking=True, + ) + test_chime.set_option.assert_called_with(led=False) - assert (const.DOMAIN, "hdr_switch_deprecated") in issue_registry.issues + test_chime.set_option.side_effect = ReolinkError("Test error") + with pytest.raises(HomeAssistantError): + await hass.services.async_call( + SWITCH_DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: entity_id}, + blocking=True, + ) diff --git a/tests/components/reolink/test_update.py b/tests/components/reolink/test_update.py new file mode 100644 index 00000000000000..3ad10a11499536 --- /dev/null +++ b/tests/components/reolink/test_update.py @@ -0,0 +1,131 @@ +"""Test the Reolink update platform.""" + +from unittest.mock import MagicMock, patch + +from freezegun.api import FrozenDateTimeFactory +import pytest +from reolink_aio.exceptions import ReolinkError +from reolink_aio.software_version import NewSoftwareVersion + +from homeassistant.components.reolink.update import POLL_AFTER_INSTALL +from homeassistant.components.update import DOMAIN as UPDATE_DOMAIN, SERVICE_INSTALL +from homeassistant.config_entries import ConfigEntryState +from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON, Platform +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError + +from .conftest import TEST_CAM_NAME, TEST_NVR_NAME + +from tests.common import MockConfigEntry, async_fire_time_changed +from tests.typing import WebSocketGenerator + +TEST_DOWNLOAD_URL = "https://reolink.com/test" +TEST_RELEASE_NOTES = "bugfix 1, bugfix 2" + + +@pytest.mark.parametrize("entity_name", [TEST_NVR_NAME, TEST_CAM_NAME]) +async def test_no_update( + hass: HomeAssistant, + config_entry: MockConfigEntry, + reolink_connect: MagicMock, + entity_name: str, +) -> None: + """Test update state when no update available.""" + reolink_connect.camera_name.return_value = TEST_CAM_NAME + + with patch("homeassistant.components.reolink.PLATFORMS", [Platform.UPDATE]): + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + entity_id = f"{Platform.UPDATE}.{entity_name}_firmware" + assert hass.states.get(entity_id).state == STATE_OFF + + +@pytest.mark.parametrize("entity_name", [TEST_NVR_NAME, TEST_CAM_NAME]) +async def test_update_str( + hass: HomeAssistant, + config_entry: MockConfigEntry, + reolink_connect: MagicMock, + entity_name: str, +) -> None: + """Test update state when update available with string from API.""" + reolink_connect.camera_name.return_value = TEST_CAM_NAME + reolink_connect.firmware_update_available.return_value = "New firmware available" + + with patch("homeassistant.components.reolink.PLATFORMS", [Platform.UPDATE]): + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + entity_id = f"{Platform.UPDATE}.{entity_name}_firmware" + assert hass.states.get(entity_id).state == STATE_ON + + +@pytest.mark.parametrize("entity_name", [TEST_NVR_NAME, TEST_CAM_NAME]) +async def test_update_firm( + hass: HomeAssistant, + config_entry: MockConfigEntry, + reolink_connect: MagicMock, + hass_ws_client: WebSocketGenerator, + freezer: FrozenDateTimeFactory, + entity_name: str, +) -> None: + """Test update state when update available with firmware info from reolink.com.""" + reolink_connect.camera_name.return_value = TEST_CAM_NAME + new_firmware = NewSoftwareVersion( + version_string="v3.3.0.226_23031644", + download_url=TEST_DOWNLOAD_URL, + release_notes=TEST_RELEASE_NOTES, + ) + reolink_connect.firmware_update_available.return_value = new_firmware + + with patch("homeassistant.components.reolink.PLATFORMS", [Platform.UPDATE]): + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + entity_id = f"{Platform.UPDATE}.{entity_name}_firmware" + assert hass.states.get(entity_id).state == STATE_ON + + # release notes + client = await hass_ws_client(hass) + await hass.async_block_till_done() + + await client.send_json( + { + "id": 1, + "type": "update/release_notes", + "entity_id": entity_id, + } + ) + result = await client.receive_json() + assert TEST_DOWNLOAD_URL in result["result"] + assert TEST_RELEASE_NOTES in result["result"] + + # test install + await hass.services.async_call( + UPDATE_DOMAIN, + SERVICE_INSTALL, + {ATTR_ENTITY_ID: entity_id}, + blocking=True, + ) + reolink_connect.update_firmware.assert_called() + + reolink_connect.update_firmware.side_effect = ReolinkError("Test error") + with pytest.raises(HomeAssistantError): + await hass.services.async_call( + UPDATE_DOMAIN, + SERVICE_INSTALL, + {ATTR_ENTITY_ID: entity_id}, + blocking=True, + ) + + # test _async_update_future + reolink_connect.camera_sw_version.return_value = "v3.3.0.226_23031644" + reolink_connect.firmware_update_available.return_value = False + freezer.tick(POLL_AFTER_INSTALL) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + assert hass.states.get(entity_id).state == STATE_OFF diff --git a/tests/components/ridwell/test_config_flow.py b/tests/components/ridwell/test_config_flow.py index 601ac18267012d..6dd00344c5b9da 100644 --- a/tests/components/ridwell/test_config_flow.py +++ b/tests/components/ridwell/test_config_flow.py @@ -13,6 +13,8 @@ from .conftest import TEST_PASSWORD, TEST_USERNAME +from tests.common import MockConfigEntry + @pytest.mark.parametrize( ("get_client_response", "errors"), @@ -65,12 +67,10 @@ async def test_duplicate_error(hass: HomeAssistant, config, setup_config_entry) async def test_step_reauth( - hass: HomeAssistant, config, config_entry, setup_config_entry + hass: HomeAssistant, config, config_entry: MockConfigEntry, setup_config_entry ) -> None: """Test a full reauth flow.""" - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_REAUTH}, data=config - ) + result = await config_entry.start_reauth_flow(hass) result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={CONF_PASSWORD: "new_password"}, diff --git a/tests/components/ring/conftest.py b/tests/components/ring/conftest.py index cd4447c1a9a4c0..4456a9daa26278 100644 --- a/tests/components/ring/conftest.py +++ b/tests/components/ring/conftest.py @@ -26,13 +26,23 @@ def mock_setup_entry() -> Generator[AsyncMock]: yield mock_setup_entry +@pytest.fixture +def mock_ring_init_auth_class(): + """Mock ring_doorbell.Auth in init and return the mock class.""" + with patch("homeassistant.components.ring.Auth", autospec=True) as mock_ring_auth: + mock_ring_auth.return_value.async_fetch_token.return_value = { + "access_token": "mock-token" + } + yield mock_ring_auth + + @pytest.fixture def mock_ring_auth(): """Mock ring_doorbell.Auth.""" with patch( "homeassistant.components.ring.config_flow.Auth", autospec=True ) as mock_ring_auth: - mock_ring_auth.return_value.fetch_token.return_value = { + mock_ring_auth.return_value.async_fetch_token.return_value = { "access_token": "mock-token" } yield mock_ring_auth.return_value diff --git a/tests/components/ring/device_mocks.py b/tests/components/ring/device_mocks.py index 88ad37bdd3656d..d2671c3896db09 100644 --- a/tests/components/ring/device_mocks.py +++ b/tests/components/ring/device_mocks.py @@ -10,7 +10,7 @@ from copy import deepcopy from datetime import datetime from time import time -from unittest.mock import MagicMock +from unittest.mock import AsyncMock, MagicMock from ring_doorbell import ( RingCapability, @@ -132,18 +132,18 @@ def update_history_data(fixture): # Configure common methods mock_device.has_capability.side_effect = has_capability - mock_device.update_health_data.side_effect = lambda: update_health_data( + mock_device.async_update_health_data.side_effect = lambda: update_health_data( DOORBOT_HEALTH if device_family != "chimes" else CHIME_HEALTH ) # Configure methods based on capability if has_capability(RingCapability.HISTORY): mock_device.configure_mock(last_history=[]) - mock_device.history.side_effect = lambda *_, **__: update_history_data( + mock_device.async_history.side_effect = lambda *_, **__: update_history_data( DOORBOT_HISTORY if device_family != "other" else INTERCOM_HISTORY ) if has_capability(RingCapability.VIDEO): - mock_device.recording_url = MagicMock(return_value="http://dummy.url") + mock_device.async_recording_url = AsyncMock(return_value="http://dummy.url") if has_capability(RingCapability.MOTION_DETECTION): mock_device.configure_mock( diff --git a/tests/components/ring/test_button.py b/tests/components/ring/test_button.py index 6fef3295159ee1..946a893c8ad944 100644 --- a/tests/components/ring/test_button.py +++ b/tests/components/ring/test_button.py @@ -28,11 +28,11 @@ async def test_button_opens_door( await setup_platform(hass, Platform.BUTTON) mock_intercom = mock_ring_devices.get_device(185036587) - mock_intercom.open_door.assert_not_called() + mock_intercom.async_open_door.assert_not_called() await hass.services.async_call( "button", "press", {"entity_id": "button.ingress_open_door"}, blocking=True ) await hass.async_block_till_done(wait_background_tasks=True) - mock_intercom.open_door.assert_called_once() + mock_intercom.async_open_door.assert_called_once() diff --git a/tests/components/ring/test_camera.py b/tests/components/ring/test_camera.py index 49b7dc10f059ed..619fb52846c39a 100644 --- a/tests/components/ring/test_camera.py +++ b/tests/components/ring/test_camera.py @@ -1,6 +1,6 @@ """The tests for the Ring switch platform.""" -from unittest.mock import AsyncMock, MagicMock, PropertyMock, patch +from unittest.mock import AsyncMock, patch from aiohttp.test_utils import make_mocked_request from freezegun.api import FrozenDateTimeFactory @@ -180,8 +180,7 @@ async def test_motion_detection_errors_when_turned_on( assert not any(config_entry.async_get_active_flows(hass, {SOURCE_REAUTH})) front_camera_mock = mock_ring_devices.get_device(765432) - p = PropertyMock(side_effect=exception_type) - type(front_camera_mock).motion_detection = p + front_camera_mock.async_set_motion_detection.side_effect = exception_type with pytest.raises(HomeAssistantError): await hass.services.async_call( @@ -191,7 +190,7 @@ async def test_motion_detection_errors_when_turned_on( blocking=True, ) await hass.async_block_till_done() - p.assert_called_once() + front_camera_mock.async_set_motion_detection.assert_called_once() assert ( any( flow @@ -212,7 +211,7 @@ async def test_camera_handle_mjpeg_stream( await setup_platform(hass, Platform.CAMERA) front_camera_mock = mock_ring_devices.get_device(765432) - front_camera_mock.recording_url.return_value = None + front_camera_mock.async_recording_url.return_value = None state = hass.states.get("camera.front") assert state is not None @@ -220,8 +219,8 @@ async def test_camera_handle_mjpeg_stream( mock_request = make_mocked_request("GET", "/", headers={"token": "x"}) # history not updated yet - front_camera_mock.history.assert_not_called() - front_camera_mock.recording_url.assert_not_called() + front_camera_mock.async_history.assert_not_called() + front_camera_mock.async_recording_url.assert_not_called() stream = await camera.async_get_mjpeg_stream(hass, mock_request, "camera.front") assert stream is None @@ -229,30 +228,30 @@ async def test_camera_handle_mjpeg_stream( freezer.tick(SCAN_INTERVAL) async_fire_time_changed(hass) await hass.async_block_till_done(wait_background_tasks=True) - front_camera_mock.history.assert_called_once() - front_camera_mock.recording_url.assert_called_once() + front_camera_mock.async_history.assert_called_once() + front_camera_mock.async_recording_url.assert_called_once() stream = await camera.async_get_mjpeg_stream(hass, mock_request, "camera.front") assert stream is None # Stop the history updating so we can update the values manually - front_camera_mock.history = MagicMock() + front_camera_mock.async_history = AsyncMock() front_camera_mock.last_history[0]["recording"]["status"] = "not ready" freezer.tick(SCAN_INTERVAL) async_fire_time_changed(hass) await hass.async_block_till_done(wait_background_tasks=True) - front_camera_mock.recording_url.assert_called_once() + front_camera_mock.async_recording_url.assert_called_once() stream = await camera.async_get_mjpeg_stream(hass, mock_request, "camera.front") assert stream is None # If the history id hasn't changed the camera will not check again for the video url # until the FORCE_REFRESH_INTERVAL has passed front_camera_mock.last_history[0]["recording"]["status"] = "ready" - front_camera_mock.recording_url = MagicMock(return_value="http://dummy.url") + front_camera_mock.async_recording_url = AsyncMock(return_value="http://dummy.url") freezer.tick(SCAN_INTERVAL) async_fire_time_changed(hass) await hass.async_block_till_done(wait_background_tasks=True) - front_camera_mock.recording_url.assert_not_called() + front_camera_mock.async_recording_url.assert_not_called() stream = await camera.async_get_mjpeg_stream(hass, mock_request, "camera.front") assert stream is None @@ -260,7 +259,7 @@ async def test_camera_handle_mjpeg_stream( freezer.tick(FORCE_REFRESH_INTERVAL) async_fire_time_changed(hass) await hass.async_block_till_done(wait_background_tasks=True) - front_camera_mock.recording_url.assert_called_once() + front_camera_mock.async_recording_url.assert_called_once() # Now the stream should be returned stream_reader = MockStreamReader(SMALLEST_VALID_JPEG_BYTES) @@ -290,8 +289,8 @@ async def test_camera_image( assert state is not None # history not updated yet - front_camera_mock.history.assert_not_called() - front_camera_mock.recording_url.assert_not_called() + front_camera_mock.async_history.assert_not_called() + front_camera_mock.async_recording_url.assert_not_called() with ( patch( "homeassistant.components.ring.camera.ffmpeg.async_get_image", @@ -305,8 +304,8 @@ async def test_camera_image( async_fire_time_changed(hass) await hass.async_block_till_done(wait_background_tasks=True) # history updated so image available - front_camera_mock.history.assert_called_once() - front_camera_mock.recording_url.assert_called_once() + front_camera_mock.async_history.assert_called_once() + front_camera_mock.async_recording_url.assert_called_once() with patch( "homeassistant.components.ring.camera.ffmpeg.async_get_image", diff --git a/tests/components/ring/test_config_flow.py b/tests/components/ring/test_config_flow.py index 2420bb9cc50a32..d27c4878aea5ed 100644 --- a/tests/components/ring/test_config_flow.py +++ b/tests/components/ring/test_config_flow.py @@ -57,7 +57,7 @@ async def test_form_error( result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} ) - mock_ring_auth.fetch_token.side_effect = error_type + mock_ring_auth.async_fetch_token.side_effect = error_type result2 = await hass.config_entries.flow.async_configure( result["flow_id"], {"username": "hello@home-assistant.io", "password": "test-password"}, @@ -79,7 +79,7 @@ async def test_form_2fa( assert result["type"] is FlowResultType.FORM assert result["errors"] == {} - mock_ring_auth.fetch_token.side_effect = ring_doorbell.Requires2FAError + mock_ring_auth.async_fetch_token.side_effect = ring_doorbell.Requires2FAError result2 = await hass.config_entries.flow.async_configure( result["flow_id"], { @@ -88,20 +88,20 @@ async def test_form_2fa( }, ) await hass.async_block_till_done() - mock_ring_auth.fetch_token.assert_called_once_with( + mock_ring_auth.async_fetch_token.assert_called_once_with( "foo@bar.com", "fake-password", None ) assert result2["type"] is FlowResultType.FORM assert result2["step_id"] == "2fa" - mock_ring_auth.fetch_token.reset_mock(side_effect=True) - mock_ring_auth.fetch_token.return_value = "new-foobar" + mock_ring_auth.async_fetch_token.reset_mock(side_effect=True) + mock_ring_auth.async_fetch_token.return_value = "new-foobar" result3 = await hass.config_entries.flow.async_configure( result2["flow_id"], user_input={"2fa": "123456"}, ) - mock_ring_auth.fetch_token.assert_called_once_with( + mock_ring_auth.async_fetch_token.assert_called_once_with( "foo@bar.com", "fake-password", "123456" ) assert result3["type"] is FlowResultType.CREATE_ENTRY @@ -128,7 +128,7 @@ async def test_reauth( [result] = flows assert result["step_id"] == "reauth_confirm" - mock_ring_auth.fetch_token.side_effect = ring_doorbell.Requires2FAError + mock_ring_auth.async_fetch_token.side_effect = ring_doorbell.Requires2FAError result2 = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={ @@ -136,19 +136,19 @@ async def test_reauth( }, ) - mock_ring_auth.fetch_token.assert_called_once_with( + mock_ring_auth.async_fetch_token.assert_called_once_with( "foo@bar.com", "other_fake_password", None ) assert result2["type"] is FlowResultType.FORM assert result2["step_id"] == "2fa" - mock_ring_auth.fetch_token.reset_mock(side_effect=True) - mock_ring_auth.fetch_token.return_value = "new-foobar" + mock_ring_auth.async_fetch_token.reset_mock(side_effect=True) + mock_ring_auth.async_fetch_token.return_value = "new-foobar" result3 = await hass.config_entries.flow.async_configure( result2["flow_id"], user_input={"2fa": "123456"}, ) - mock_ring_auth.fetch_token.assert_called_once_with( + mock_ring_auth.async_fetch_token.assert_called_once_with( "foo@bar.com", "other_fake_password", "123456" ) assert result3["type"] is FlowResultType.ABORT @@ -185,7 +185,7 @@ async def test_reauth_error( [result] = flows assert result["step_id"] == "reauth_confirm" - mock_ring_auth.fetch_token.side_effect = error_type + mock_ring_auth.async_fetch_token.side_effect = error_type result2 = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={ @@ -194,15 +194,15 @@ async def test_reauth_error( ) await hass.async_block_till_done() - mock_ring_auth.fetch_token.assert_called_once_with( + mock_ring_auth.async_fetch_token.assert_called_once_with( "foo@bar.com", "error_fake_password", None ) assert result2["type"] is FlowResultType.FORM assert result2["errors"] == {"base": errors_msg} # Now test reauth can go on to succeed - mock_ring_auth.fetch_token.reset_mock(side_effect=True) - mock_ring_auth.fetch_token.return_value = "new-foobar" + mock_ring_auth.async_fetch_token.reset_mock(side_effect=True) + mock_ring_auth.async_fetch_token.return_value = "new-foobar" result3 = await hass.config_entries.flow.async_configure( result2["flow_id"], user_input={ @@ -210,7 +210,7 @@ async def test_reauth_error( }, ) - mock_ring_auth.fetch_token.assert_called_once_with( + mock_ring_auth.async_fetch_token.assert_called_once_with( "foo@bar.com", "other_fake_password", None ) assert result3["type"] is FlowResultType.ABORT @@ -220,3 +220,25 @@ async def test_reauth_error( "token": "new-foobar", } assert len(mock_setup_entry.mock_calls) == 1 + + +async def test_account_configured( + hass: HomeAssistant, + mock_setup_entry: AsyncMock, + mock_added_config_entry: Mock, +) -> None: + """Test that user cannot configure the same account twice.""" + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} + ) + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {} + + result2 = await hass.config_entries.flow.async_configure( + result["flow_id"], + {"username": "foo@bar.com", "password": "test-password"}, + ) + + assert result2["type"] is FlowResultType.ABORT + assert result2["reason"] == "already_configured" diff --git a/tests/components/ring/test_init.py b/tests/components/ring/test_init.py index d8529e874b929a..97392e0c93bdb0 100644 --- a/tests/components/ring/test_init.py +++ b/tests/components/ring/test_init.py @@ -10,7 +10,7 @@ from homeassistant.components.ring import DOMAIN from homeassistant.components.ring.const import SCAN_INTERVAL from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState -from homeassistant.const import CONF_USERNAME +from homeassistant.const import CONF_TOKEN, CONF_USERNAME from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er, issue_registry as ir from homeassistant.setup import async_setup_component @@ -42,11 +42,11 @@ async def test_setup_entry_device_update( """Test devices are updating after setup entry.""" front_door_doorbell = mock_ring_devices.get_device(987654) - front_door_doorbell.history.assert_not_called() + front_door_doorbell.async_history.assert_not_called() freezer.tick(SCAN_INTERVAL) async_fire_time_changed(hass) await hass.async_block_till_done(wait_background_tasks=True) - front_door_doorbell.history.assert_called_once() + front_door_doorbell.async_history.assert_called_once() async def test_auth_failed_on_setup( @@ -56,7 +56,7 @@ async def test_auth_failed_on_setup( ) -> None: """Test auth failure on setup entry.""" mock_config_entry.add_to_hass(hass) - mock_ring_client.update_data.side_effect = AuthenticationError + mock_ring_client.async_update_data.side_effect = AuthenticationError assert not any(mock_config_entry.async_get_active_flows(hass, {SOURCE_REAUTH})) await hass.config_entries.async_setup(mock_config_entry.entry_id) @@ -90,7 +90,7 @@ async def test_error_on_setup( """Test non-auth errors on setup entry.""" mock_config_entry.add_to_hass(hass) - mock_ring_client.update_data.side_effect = error_type + mock_ring_client.async_update_data.side_effect = error_type await hass.config_entries.async_setup(mock_config_entry.entry_id) await hass.async_block_till_done() @@ -113,7 +113,7 @@ async def test_auth_failure_on_global_update( await hass.async_block_till_done() assert not any(mock_config_entry.async_get_active_flows(hass, {SOURCE_REAUTH})) - mock_ring_client.update_devices.side_effect = AuthenticationError + mock_ring_client.async_update_devices.side_effect = AuthenticationError freezer.tick(SCAN_INTERVAL) async_fire_time_changed(hass) @@ -139,7 +139,7 @@ async def test_auth_failure_on_device_update( assert not any(mock_config_entry.async_get_active_flows(hass, {SOURCE_REAUTH})) front_door_doorbell = mock_ring_devices.get_device(987654) - front_door_doorbell.history.side_effect = AuthenticationError + front_door_doorbell.async_history.side_effect = AuthenticationError freezer.tick(SCAN_INTERVAL) async_fire_time_changed(hass) @@ -178,7 +178,7 @@ async def test_error_on_global_update( await hass.config_entries.async_setup(mock_config_entry.entry_id) await hass.async_block_till_done() - mock_ring_client.update_devices.side_effect = error_type + mock_ring_client.async_update_devices.side_effect = error_type freezer.tick(SCAN_INTERVAL) async_fire_time_changed(hass) @@ -186,7 +186,7 @@ async def test_error_on_global_update( assert log_msg in caplog.text - assert mock_config_entry.entry_id in hass.data[DOMAIN] + assert hass.config_entries.async_get_entry(mock_config_entry.entry_id) @pytest.mark.parametrize( @@ -219,14 +219,14 @@ async def test_error_on_device_update( await hass.async_block_till_done() front_door_doorbell = mock_ring_devices.get_device(765432) - front_door_doorbell.history.side_effect = error_type + front_door_doorbell.async_history.side_effect = error_type freezer.tick(SCAN_INTERVAL) async_fire_time_changed(hass) await hass.async_block_till_done(wait_background_tasks=True) assert log_msg in caplog.text - assert mock_config_entry.entry_id in hass.data[DOMAIN] + assert hass.config_entries.async_get_entry(mock_config_entry.entry_id) async def test_issue_deprecated_service_ring_update( @@ -386,3 +386,30 @@ async def test_update_unique_id_no_update( assert entity_migrated assert entity_migrated.unique_id == correct_unique_id assert "Fixing non string unique id" not in caplog.text + + +async def test_token_updated( + hass: HomeAssistant, + freezer: FrozenDateTimeFactory, + mock_config_entry: MockConfigEntry, + mock_ring_client, + mock_ring_init_auth_class, +) -> None: + """Test that the token value is updated in the config entry. + + This simulates the api calling the callback. + """ + mock_config_entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(mock_config_entry.entry_id) + + assert mock_ring_init_auth_class.call_count == 1 + token_updater = mock_ring_init_auth_class.call_args.args[2] + assert mock_config_entry.data[CONF_TOKEN] == {"access_token": "mock-token"} + + mock_ring_client.async_update_devices.side_effect = lambda: token_updater( + {"access_token": "new-mock-token"} + ) + freezer.tick(SCAN_INTERVAL) + async_fire_time_changed(hass) + await hass.async_block_till_done() + assert mock_config_entry.data[CONF_TOKEN] == {"access_token": "new-mock-token"} diff --git a/tests/components/ring/test_light.py b/tests/components/ring/test_light.py index c2d21a2295188c..22ed4a31cf819e 100644 --- a/tests/components/ring/test_light.py +++ b/tests/components/ring/test_light.py @@ -1,7 +1,5 @@ """The tests for the Ring light platform.""" -from unittest.mock import PropertyMock - import pytest import ring_doorbell @@ -109,15 +107,14 @@ async def test_light_errors_when_turned_on( assert not any(config_entry.async_get_active_flows(hass, {SOURCE_REAUTH})) front_light_mock = mock_ring_devices.get_device(765432) - p = PropertyMock(side_effect=exception_type) - type(front_light_mock).lights = p + front_light_mock.async_set_lights.side_effect = exception_type with pytest.raises(HomeAssistantError): await hass.services.async_call( "light", "turn_on", {"entity_id": "light.front_light"}, blocking=True ) await hass.async_block_till_done() - p.assert_called_once() + front_light_mock.async_set_lights.assert_called_once() assert ( any( diff --git a/tests/components/ring/test_siren.py b/tests/components/ring/test_siren.py index 695b54c39716f6..e71dd1e6e774ef 100644 --- a/tests/components/ring/test_siren.py +++ b/tests/components/ring/test_siren.py @@ -49,7 +49,7 @@ async def test_default_ding_chime_can_be_played( await hass.async_block_till_done() downstairs_chime_mock = mock_ring_devices.get_device(123456) - downstairs_chime_mock.test_sound.assert_called_once_with(kind="ding") + downstairs_chime_mock.async_test_sound.assert_called_once_with(kind="ding") state = hass.states.get("siren.downstairs_siren") assert state.state == "unknown" @@ -71,7 +71,7 @@ async def test_turn_on_plays_default_chime( await hass.async_block_till_done() downstairs_chime_mock = mock_ring_devices.get_device(123456) - downstairs_chime_mock.test_sound.assert_called_once_with(kind="ding") + downstairs_chime_mock.async_test_sound.assert_called_once_with(kind="ding") state = hass.states.get("siren.downstairs_siren") assert state.state == "unknown" @@ -95,7 +95,7 @@ async def test_explicit_ding_chime_can_be_played( await hass.async_block_till_done() downstairs_chime_mock = mock_ring_devices.get_device(123456) - downstairs_chime_mock.test_sound.assert_called_once_with(kind="ding") + downstairs_chime_mock.async_test_sound.assert_called_once_with(kind="ding") state = hass.states.get("siren.downstairs_siren") assert state.state == "unknown" @@ -117,7 +117,7 @@ async def test_motion_chime_can_be_played( await hass.async_block_till_done() downstairs_chime_mock = mock_ring_devices.get_device(123456) - downstairs_chime_mock.test_sound.assert_called_once_with(kind="motion") + downstairs_chime_mock.async_test_sound.assert_called_once_with(kind="motion") state = hass.states.get("siren.downstairs_siren") assert state.state == "unknown" @@ -146,7 +146,7 @@ async def test_siren_errors_when_turned_on( assert not any(config_entry.async_get_active_flows(hass, {SOURCE_REAUTH})) downstairs_chime_mock = mock_ring_devices.get_device(123456) - downstairs_chime_mock.test_sound.side_effect = exception_type + downstairs_chime_mock.async_test_sound.side_effect = exception_type with pytest.raises(HomeAssistantError): await hass.services.async_call( @@ -155,7 +155,8 @@ async def test_siren_errors_when_turned_on( {"entity_id": "siren.downstairs_siren", "tone": "motion"}, blocking=True, ) - downstairs_chime_mock.test_sound.assert_called_once_with(kind="motion") + downstairs_chime_mock.async_test_sound.assert_called_once_with(kind="motion") + await hass.async_block_till_done() assert ( any( flow diff --git a/tests/components/ring/test_switch.py b/tests/components/ring/test_switch.py index 405f20420b746c..f7aa885342ad51 100644 --- a/tests/components/ring/test_switch.py +++ b/tests/components/ring/test_switch.py @@ -1,7 +1,5 @@ """The tests for the Ring switch platform.""" -from unittest.mock import PropertyMock - import pytest import ring_doorbell @@ -116,15 +114,14 @@ async def test_switch_errors_when_turned_on( assert not any(config_entry.async_get_active_flows(hass, {SOURCE_REAUTH})) front_siren_mock = mock_ring_devices.get_device(765432) - p = PropertyMock(side_effect=exception_type) - type(front_siren_mock).siren = p + front_siren_mock.async_set_siren.side_effect = exception_type with pytest.raises(HomeAssistantError): await hass.services.async_call( "switch", "turn_on", {"entity_id": "switch.front_siren"}, blocking=True ) await hass.async_block_till_done() - p.assert_called_once() + front_siren_mock.async_set_siren.assert_called_once() assert ( any( flow diff --git a/tests/components/risco/test_config_flow.py b/tests/components/risco/test_config_flow.py index 9fade18ea96bfc..cff5f80e6c43f2 100644 --- a/tests/components/risco/test_config_flow.py +++ b/tests/components/risco/test_config_flow.py @@ -154,14 +154,12 @@ async def test_form_cloud_already_exists(hass: HomeAssistant) -> None: assert result3["reason"] == "already_configured" -async def test_form_reauth(hass: HomeAssistant, cloud_config_entry) -> None: +async def test_form_reauth( + hass: HomeAssistant, cloud_config_entry: MockConfigEntry +) -> None: """Test reauthenticate.""" - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_REAUTH}, - data=cloud_config_entry.data, - ) + result = await cloud_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["errors"] == {} @@ -194,15 +192,11 @@ async def test_form_reauth(hass: HomeAssistant, cloud_config_entry) -> None: async def test_form_reauth_with_new_username( - hass: HomeAssistant, cloud_config_entry + hass: HomeAssistant, cloud_config_entry: MockConfigEntry ) -> None: """Test reauthenticate with new username.""" - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_REAUTH}, - data=cloud_config_entry.data, - ) + result = await cloud_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["errors"] == {} diff --git a/tests/components/ruckus_unleashed/test_config_flow.py b/tests/components/ruckus_unleashed/test_config_flow.py index 5bfe2d941d5e91..89bd72d99e4c14 100644 --- a/tests/components/ruckus_unleashed/test_config_flow.py +++ b/tests/components/ruckus_unleashed/test_config_flow.py @@ -83,15 +83,7 @@ async def test_form_user_reauth(hass: HomeAssistant) -> None: entry = mock_config_entry() entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - "unique_id": entry.unique_id, - }, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) flows = hass.config_entries.flow.async_progress() assert len(flows) == 1 @@ -121,15 +113,7 @@ async def test_form_user_reauth_different_unique_id(hass: HomeAssistant) -> None entry = mock_config_entry() entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - "unique_id": entry.unique_id, - }, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) flows = hass.config_entries.flow.async_progress() assert len(flows) == 1 @@ -161,15 +145,7 @@ async def test_form_user_reauth_invalid_auth(hass: HomeAssistant) -> None: entry = mock_config_entry() entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - "unique_id": entry.unique_id, - }, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) flows = hass.config_entries.flow.async_progress() assert len(flows) == 1 @@ -201,15 +177,7 @@ async def test_form_user_reauth_cannot_connect(hass: HomeAssistant) -> None: entry = mock_config_entry() entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - "unique_id": entry.unique_id, - }, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) flows = hass.config_entries.flow.async_progress() assert len(flows) == 1 @@ -241,15 +209,7 @@ async def test_form_user_reauth_general_exception(hass: HomeAssistant) -> None: entry = mock_config_entry() entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - "unique_id": entry.unique_id, - }, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) flows = hass.config_entries.flow.async_progress() assert len(flows) == 1 diff --git a/tests/components/rympro/test_config_flow.py b/tests/components/rympro/test_config_flow.py index e92b7c233572c4..7770889bdeb33b 100644 --- a/tests/components/rympro/test_config_flow.py +++ b/tests/components/rympro/test_config_flow.py @@ -160,17 +160,10 @@ async def test_form_already_exists(hass: HomeAssistant, config_entry) -> None: assert result2["reason"] == "already_configured" -async def test_form_reauth(hass: HomeAssistant, config_entry) -> None: +async def test_form_reauth(hass: HomeAssistant, config_entry: MockConfigEntry) -> None: """Test reauthentication.""" - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": config_entry.entry_id, - }, - data=config_entry.data, - ) + result = await config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["errors"] is None @@ -203,17 +196,12 @@ async def test_form_reauth(hass: HomeAssistant, config_entry) -> None: assert len(mock_setup_entry.mock_calls) == 1 -async def test_form_reauth_with_new_account(hass: HomeAssistant, config_entry) -> None: +async def test_form_reauth_with_new_account( + hass: HomeAssistant, config_entry: MockConfigEntry +) -> None: """Test reauthentication with new account.""" - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": config_entry.entry_id, - }, - data=config_entry.data, - ) + result = await config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["errors"] is None diff --git a/tests/components/samsungtv/test_config_flow.py b/tests/components/samsungtv/test_config_flow.py index 6c325ae3b0429e..43d8c81d00028d 100644 --- a/tests/components/samsungtv/test_config_flow.py +++ b/tests/components/samsungtv/test_config_flow.py @@ -1749,11 +1749,7 @@ async def test_form_reauth_legacy(hass: HomeAssistant) -> None: """Test reauthenticate legacy.""" entry = MockConfigEntry(domain=DOMAIN, data=MOCK_OLD_ENTRY) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"entry_id": entry.entry_id, "source": config_entries.SOURCE_REAUTH}, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["errors"] == {} @@ -1773,11 +1769,7 @@ async def test_form_reauth_websocket(hass: HomeAssistant) -> None: entry.add_to_hass(hass) assert entry.state is ConfigEntryState.NOT_LOADED - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"entry_id": entry.entry_id, "source": config_entries.SOURCE_REAUTH}, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["errors"] == {} @@ -1798,11 +1790,7 @@ async def test_form_reauth_websocket_cannot_connect( """Test reauthenticate websocket when we cannot connect on the first attempt.""" entry = MockConfigEntry(domain=DOMAIN, data=MOCK_ENTRYDATA_WS) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"entry_id": entry.entry_id, "source": config_entries.SOURCE_REAUTH}, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["errors"] == {} @@ -1830,11 +1818,7 @@ async def test_form_reauth_websocket_not_supported(hass: HomeAssistant) -> None: """Test reauthenticate websocket when the device is not supported.""" entry = MockConfigEntry(domain=DOMAIN, data=MOCK_ENTRYDATA_WS) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"entry_id": entry.entry_id, "source": config_entries.SOURCE_REAUTH}, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["errors"] == {} @@ -1863,11 +1847,7 @@ async def test_form_reauth_encrypted(hass: HomeAssistant) -> None: entry.add_to_hass(hass) assert entry.state is ConfigEntryState.NOT_LOADED - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"entry_id": entry.entry_id, "source": config_entries.SOURCE_REAUTH}, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["errors"] == {} diff --git a/tests/components/script/test_blueprint.py b/tests/components/script/test_blueprint.py index aef22b93bcfc23..160b330c10990b 100644 --- a/tests/components/script/test_blueprint.py +++ b/tests/components/script/test_blueprint.py @@ -109,7 +109,6 @@ async def test_confirmable_notification( assert len(mock_call_action.mock_calls) == 1 _hass, config, variables, _context = mock_call_action.mock_calls[0][1] - template.attach(hass, config) rendered_config = template.render_complex(config, variables) assert rendered_config == { diff --git a/tests/components/sense/test_config_flow.py b/tests/components/sense/test_config_flow.py index e564603ea87203..0ba8d94e17b0e3 100644 --- a/tests/components/sense/test_config_flow.py +++ b/tests/components/sense/test_config_flow.py @@ -268,9 +268,7 @@ async def test_reauth_no_form(hass: HomeAssistant, mock_sense) -> None: "homeassistant.config_entries.ConfigEntries.async_reload", return_value=True, ): - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_REAUTH}, data=MOCK_CONFIG - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.ABORT assert result["reason"] == "reauth_successful" @@ -288,9 +286,7 @@ async def test_reauth_password(hass: HomeAssistant, mock_sense) -> None: mock_sense.return_value.authenticate.side_effect = SenseAuthenticationException # Reauth success without user input - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_REAUTH}, data=entry.data - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM mock_sense.return_value.authenticate.side_effect = None diff --git a/tests/components/sensibo/test_config_flow.py b/tests/components/sensibo/test_config_flow.py index e994402b09fbd7..3f53495f0f246f 100644 --- a/tests/components/sensibo/test_config_flow.py +++ b/tests/components/sensibo/test_config_flow.py @@ -192,15 +192,7 @@ async def test_reauth_flow(hass: HomeAssistant) -> None: ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": entry.unique_id, - "entry_id": entry.entry_id, - }, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) assert result["step_id"] == "reauth_confirm" assert result["type"] is FlowResultType.FORM assert result["errors"] == {} @@ -254,15 +246,7 @@ async def test_reauth_flow_error( ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": entry.unique_id, - "entry_id": entry.entry_id, - }, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) with patch( "homeassistant.components.sensibo.util.SensiboClient.async_get_devices", @@ -338,15 +322,7 @@ async def test_flow_reauth_no_username_or_device( ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": entry.unique_id, - "entry_id": entry.entry_id, - }, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/seventeentrack/conftest.py b/tests/components/seventeentrack/conftest.py index e2493319b69b49..0d02a7ab5f1763 100644 --- a/tests/components/seventeentrack/conftest.py +++ b/tests/components/seventeentrack/conftest.py @@ -40,6 +40,11 @@ "Returned": 1, } +ARCHIVE_PACKAGE_NUMBER = "123" +CONFIG_ENTRY_ID_KEY = "config_entry_id" +PACKAGE_TRACKING_NUMBER_KEY = "package_tracking_number" +PACKAGE_STATE_KEY = "package_state" + VALID_CONFIG = { CONF_USERNAME: "test", CONF_PASSWORD: "test", diff --git a/tests/components/seventeentrack/test_services.py b/tests/components/seventeentrack/test_services.py index 4347189a5c0d5b..54c9349c121fdc 100644 --- a/tests/components/seventeentrack/test_services.py +++ b/tests/components/seventeentrack/test_services.py @@ -5,14 +5,24 @@ import pytest from syrupy import SnapshotAssertion -from homeassistant.components.seventeentrack import DOMAIN, SERVICE_GET_PACKAGES +from homeassistant.components.seventeentrack import DOMAIN +from homeassistant.components.seventeentrack.const import ( + SERVICE_ARCHIVE_PACKAGE, + SERVICE_GET_PACKAGES, +) from homeassistant.config_entries import ConfigEntryState from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import device_registry as dr from . import init_integration -from .conftest import get_package +from .conftest import ( + ARCHIVE_PACKAGE_NUMBER, + CONFIG_ENTRY_ID_KEY, + PACKAGE_STATE_KEY, + PACKAGE_TRACKING_NUMBER_KEY, + get_package, +) from tests.common import MockConfigEntry @@ -30,8 +40,8 @@ async def test_get_packages_from_list( DOMAIN, SERVICE_GET_PACKAGES, { - "config_entry_id": mock_config_entry.entry_id, - "package_state": ["in_transit", "delivered"], + CONFIG_ENTRY_ID_KEY: mock_config_entry.entry_id, + PACKAGE_STATE_KEY: ["in_transit", "delivered"], }, blocking=True, return_response=True, @@ -53,7 +63,7 @@ async def test_get_all_packages( DOMAIN, SERVICE_GET_PACKAGES, { - "config_entry_id": mock_config_entry.entry_id, + CONFIG_ENTRY_ID_KEY: mock_config_entry.entry_id, }, blocking=True, return_response=True, @@ -76,7 +86,7 @@ async def test_service_called_with_unloaded_entry( DOMAIN, SERVICE_GET_PACKAGES, { - "config_entry_id": mock_config_entry.entry_id, + CONFIG_ENTRY_ID_KEY: mock_config_entry.entry_id, }, blocking=True, return_response=True, @@ -110,13 +120,36 @@ async def test_service_called_with_non_17track_device( DOMAIN, SERVICE_GET_PACKAGES, { - "config_entry_id": device_entry.id, + CONFIG_ENTRY_ID_KEY: device_entry.id, }, blocking=True, return_response=True, ) +async def test_archive_package( + hass: HomeAssistant, + mock_seventeentrack: AsyncMock, + mock_config_entry: MockConfigEntry, + snapshot: SnapshotAssertion, +) -> None: + """Ensure service archives package.""" + await _mock_packages(mock_seventeentrack) + await init_integration(hass, mock_config_entry) + await hass.services.async_call( + DOMAIN, + SERVICE_ARCHIVE_PACKAGE, + { + CONFIG_ENTRY_ID_KEY: mock_config_entry.entry_id, + PACKAGE_TRACKING_NUMBER_KEY: ARCHIVE_PACKAGE_NUMBER, + }, + blocking=True, + ) + mock_seventeentrack.return_value.profile.archive_package.assert_called_once_with( + ARCHIVE_PACKAGE_NUMBER + ) + + async def _mock_packages(mock_seventeentrack): package1 = get_package(status=10) package2 = get_package( diff --git a/tests/components/sfr_box/test_config_flow.py b/tests/components/sfr_box/test_config_flow.py index 08c12e9817bd98..6bf610de661c79 100644 --- a/tests/components/sfr_box/test_config_flow.py +++ b/tests/components/sfr_box/test_config_flow.py @@ -207,15 +207,7 @@ async def test_reauth(hass: HomeAssistant, config_entry_with_auth: ConfigEntry) """Test the start of the config flow.""" assert len(hass.config_entries.async_entries(DOMAIN)) == 1 - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": config_entry_with_auth.entry_id, - "unique_id": config_entry_with_auth.unique_id, - }, - data=config_entry_with_auth.data, - ) + result = await config_entry_with_auth.start_reauth_flow(hass) assert result.get("type") is FlowResultType.FORM assert result.get("errors") == {} diff --git a/tests/components/sharkiq/test_config_flow.py b/tests/components/sharkiq/test_config_flow.py index cf75bff1686a0e..22a77678c0d701 100644 --- a/tests/components/sharkiq/test_config_flow.py +++ b/tests/components/sharkiq/test_config_flow.py @@ -96,18 +96,18 @@ async def test_form_error(hass: HomeAssistant, exc: Exception, base_error: str) async def test_reauth_success(hass: HomeAssistant) -> None: """Test reauth flow.""" - with patch("sharkiq.AylaApi.async_sign_in", return_value=True): - mock_config = MockConfigEntry(domain=DOMAIN, unique_id=UNIQUE_ID, data=CONFIG) - mock_config.add_to_hass(hass) + mock_config = MockConfigEntry(domain=DOMAIN, unique_id=UNIQUE_ID, data=CONFIG) + mock_config.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_REAUTH, "unique_id": UNIQUE_ID}, - data=CONFIG, + result = await mock_config.start_reauth_flow(hass) + + with patch("sharkiq.AylaApi.async_sign_in", return_value=True): + result = await hass.config_entries.flow.async_configure( + result["flow_id"], user_input=CONFIG ) - assert result["type"] is FlowResultType.ABORT - assert result["reason"] == "reauth_successful" + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "reauth_successful" @pytest.mark.parametrize( @@ -127,13 +127,15 @@ async def test_reauth( msg: str, ) -> None: """Test reauth failures.""" + mock_config = MockConfigEntry(domain=DOMAIN, unique_id=UNIQUE_ID, data=CONFIG) + mock_config.add_to_hass(hass) + + result = await mock_config.start_reauth_flow(hass) + with patch("sharkiq.AylaApi.async_sign_in", side_effect=side_effect): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_REAUTH, "unique_id": UNIQUE_ID}, - data=CONFIG, + result = await hass.config_entries.flow.async_configure( + result["flow_id"], user_input=CONFIG ) - msg_value = result[msg_field] if msg_field == "errors": msg_value = msg_value.get("base") diff --git a/tests/components/shelly/conftest.py b/tests/components/shelly/conftest.py index a2629d213626ad..a983cbbcda9ad9 100644 --- a/tests/components/shelly/conftest.py +++ b/tests/components/shelly/conftest.py @@ -166,8 +166,20 @@ def mock_white_light_set_state( MOCK_CONFIG = { "input:0": {"id": 0, "name": "Test name input 0", "type": "button"}, - "input:1": {"id": 1, "type": "analog", "enable": True}, - "input:2": {"id": 2, "name": "Gas", "type": "count", "enable": True}, + "input:1": { + "id": 1, + "type": "analog", + "enable": True, + "xpercent": {"expr": None, "unit": None}, + }, + "input:2": { + "id": 2, + "name": "Gas", + "type": "count", + "enable": True, + "xcounts": {"expr": None, "unit": None}, + "xfreq": {"expr": None, "unit": None}, + }, "light:0": {"name": "test light_0"}, "light:1": {"name": "test light_1"}, "light:2": {"name": "test light_2"}, @@ -186,6 +198,7 @@ def mock_white_light_set_state( "device": {"name": "Test name"}, }, "wifi": {"sta": {"enable": True}, "sta1": {"enable": False}}, + "ws": {"enable": False, "server": None}, } MOCK_SHELLY_COAP = { diff --git a/tests/components/shelly/test_binary_sensor.py b/tests/components/shelly/test_binary_sensor.py index 18f65deb907ef7..fadfe28db3e455 100644 --- a/tests/components/shelly/test_binary_sensor.py +++ b/tests/components/shelly/test_binary_sensor.py @@ -169,9 +169,14 @@ async def test_block_restored_sleeping_binary_sensor( ) -> None: """Test block restored sleeping binary sensor.""" entry = await init_integration(hass, 1, sleep_period=1000, skip_setup=True) - register_device(device_registry, entry) + device = register_device(device_registry, entry) entity_id = register_entity( - hass, BINARY_SENSOR_DOMAIN, "test_name_motion", "sensor_0-motion", entry + hass, + BINARY_SENSOR_DOMAIN, + "test_name_motion", + "sensor_0-motion", + entry, + device_id=device.id, ) mock_restore_cache(hass, [State(entity_id, STATE_ON)]) monkeypatch.setattr(mock_block_device, "initialized", False) @@ -196,9 +201,14 @@ async def test_block_restored_sleeping_binary_sensor_no_last_state( ) -> None: """Test block restored sleeping binary sensor missing last state.""" entry = await init_integration(hass, 1, sleep_period=1000, skip_setup=True) - register_device(device_registry, entry) + device = register_device(device_registry, entry) entity_id = register_entity( - hass, BINARY_SENSOR_DOMAIN, "test_name_motion", "sensor_0-motion", entry + hass, + BINARY_SENSOR_DOMAIN, + "test_name_motion", + "sensor_0-motion", + entry, + device_id=device.id, ) monkeypatch.setattr(mock_block_device, "initialized", False) await hass.config_entries.async_setup(entry.entry_id) @@ -305,9 +315,14 @@ async def test_rpc_restored_sleeping_binary_sensor( ) -> None: """Test RPC restored binary sensor.""" entry = await init_integration(hass, 2, sleep_period=1000, skip_setup=True) - register_device(device_registry, entry) + device = register_device(device_registry, entry) entity_id = register_entity( - hass, BINARY_SENSOR_DOMAIN, "test_name_cloud", "cloud-cloud", entry + hass, + BINARY_SENSOR_DOMAIN, + "test_name_cloud", + "cloud-cloud", + entry, + device_id=device.id, ) mock_restore_cache(hass, [State(entity_id, STATE_ON)]) @@ -334,9 +349,14 @@ async def test_rpc_restored_sleeping_binary_sensor_no_last_state( ) -> None: """Test RPC restored sleeping binary sensor missing last state.""" entry = await init_integration(hass, 2, sleep_period=1000, skip_setup=True) - register_device(device_registry, entry) + device = register_device(device_registry, entry) entity_id = register_entity( - hass, BINARY_SENSOR_DOMAIN, "test_name_cloud", "cloud-cloud", entry + hass, + BINARY_SENSOR_DOMAIN, + "test_name_cloud", + "cloud-cloud", + entry, + device_id=device.id, ) monkeypatch.setattr(mock_rpc_device, "initialized", False) diff --git a/tests/components/shelly/test_climate.py b/tests/components/shelly/test_climate.py index fea46b1d2d1e80..1156d7e0ed59df 100644 --- a/tests/components/shelly/test_climate.py +++ b/tests/components/shelly/test_climate.py @@ -254,13 +254,14 @@ async def test_block_restored_climate( monkeypatch.setattr(mock_block_device.blocks[DEVICE_BLOCK_ID], "valveError", 0) monkeypatch.delattr(mock_block_device.blocks[EMETER_BLOCK_ID], "targetTemp") entry = await init_integration(hass, 1, sleep_period=1000, skip_setup=True) - register_device(device_registry, entry) + device = register_device(device_registry, entry) entity_id = register_entity( hass, CLIMATE_DOMAIN, "test_name", "sensor_0", entry, + device_id=device.id, ) attrs = {"current_temperature": 20.5, "temperature": 4.0} extra_data = {"last_target_temp": 22.0} @@ -321,13 +322,14 @@ async def test_block_restored_climate_us_customery( monkeypatch.setattr(mock_block_device.blocks[DEVICE_BLOCK_ID], "valveError", 0) monkeypatch.delattr(mock_block_device.blocks[EMETER_BLOCK_ID], "targetTemp") entry = await init_integration(hass, 1, sleep_period=1000, skip_setup=True) - register_device(device_registry, entry) + device = register_device(device_registry, entry) entity_id = register_entity( hass, CLIMATE_DOMAIN, "test_name", "sensor_0", entry, + device_id=device.id, ) attrs = {"current_temperature": 67, "temperature": 39} extra_data = {"last_target_temp": 10.0} @@ -390,13 +392,14 @@ async def test_block_restored_climate_unavailable( monkeypatch.delattr(mock_block_device.blocks[DEVICE_BLOCK_ID], "targetTemp") monkeypatch.setattr(mock_block_device.blocks[DEVICE_BLOCK_ID], "valveError", 0) entry = await init_integration(hass, 1, sleep_period=1000, skip_setup=True) - register_device(device_registry, entry) + device = register_device(device_registry, entry) entity_id = register_entity( hass, CLIMATE_DOMAIN, "test_name", "sensor_0", entry, + device_id=device.id, ) mock_restore_cache(hass, [State(entity_id, STATE_UNAVAILABLE)]) @@ -417,13 +420,14 @@ async def test_block_restored_climate_set_preset_before_online( monkeypatch.delattr(mock_block_device.blocks[DEVICE_BLOCK_ID], "targetTemp") monkeypatch.setattr(mock_block_device.blocks[DEVICE_BLOCK_ID], "valveError", 0) entry = await init_integration(hass, 1, sleep_period=1000, skip_setup=True) - register_device(device_registry, entry) + device = register_device(device_registry, entry) entity_id = register_entity( hass, CLIMATE_DOMAIN, "test_name", "sensor_0", entry, + device_id=device.id, ) mock_restore_cache(hass, [State(entity_id, HVACMode.HEAT)]) @@ -518,13 +522,14 @@ async def test_block_restored_climate_auth_error( monkeypatch.delattr(mock_block_device.blocks[DEVICE_BLOCK_ID], "targetTemp") monkeypatch.setattr(mock_block_device.blocks[DEVICE_BLOCK_ID], "valveError", 0) entry = await init_integration(hass, 1, sleep_period=1000, skip_setup=True) - register_device(device_registry, entry) + device = register_device(device_registry, entry) entity_id = register_entity( hass, CLIMATE_DOMAIN, "test_name", "sensor_0", entry, + device_id=device.id, ) mock_restore_cache(hass, [State(entity_id, HVACMode.HEAT)]) diff --git a/tests/components/shelly/test_config_flow.py b/tests/components/shelly/test_config_flow.py index 0c574a33e0cf4f..f03d90dbabb4b7 100644 --- a/tests/components/shelly/test_config_flow.py +++ b/tests/components/shelly/test_config_flow.py @@ -4,7 +4,7 @@ from datetime import timedelta from ipaddress import ip_address from typing import Any -from unittest.mock import AsyncMock, Mock, patch +from unittest.mock import AsyncMock, Mock, call, patch from aioshelly.const import DEFAULT_HTTP_PORT, MODEL_1, MODEL_PLUS_2PM from aioshelly.exceptions import ( @@ -23,7 +23,7 @@ BLEScannerMode, ) from homeassistant.components.shelly.coordinator import ENTRY_RELOAD_COOLDOWN -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_RECONFIGURE +from homeassistant.config_entries import SOURCE_RECONFIGURE from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType from homeassistant.setup import async_setup_component @@ -819,20 +819,15 @@ async def test_reauth_successful( domain="shelly", unique_id="test-mac", data={"host": "0.0.0.0", "gen": gen} ) entry.add_to_hass(hass) + result = await entry.start_reauth_flow(hass) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reauth_confirm" with patch( "homeassistant.components.shelly.config_flow.get_info", return_value={"mac": "test-mac", "type": MODEL_1, "auth": True, "gen": gen}, ): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH, "entry_id": entry.entry_id}, - data=entry.data, - ) - - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "reauth_confirm" - result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input=user_input, @@ -858,6 +853,9 @@ async def test_reauth_unsuccessful( domain="shelly", unique_id="test-mac", data={"host": "0.0.0.0", "gen": gen} ) entry.add_to_hass(hass) + result = await entry.start_reauth_flow(hass) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reauth_confirm" with ( patch( @@ -873,15 +871,6 @@ async def test_reauth_unsuccessful( new=AsyncMock(side_effect=InvalidAuthError), ), ): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH, "entry_id": entry.entry_id}, - data=entry.data, - ) - - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "reauth_confirm" - result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input=user_input, @@ -897,20 +886,14 @@ async def test_reauth_get_info_error(hass: HomeAssistant) -> None: domain="shelly", unique_id="test-mac", data={"host": "0.0.0.0", "gen": 2} ) entry.add_to_hass(hass) + result = await entry.start_reauth_flow(hass) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reauth_confirm" with patch( "homeassistant.components.shelly.config_flow.get_info", side_effect=DeviceConnectionError, ): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH, "entry_id": entry.entry_id}, - data=entry.data, - ) - - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "reauth_confirm" - result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={"password": "test2 password"}, @@ -1153,6 +1136,182 @@ async def test_zeroconf_sleeping_device_not_triggers_refresh( assert "device did not update" not in caplog.text +async def test_zeroconf_sleeping_device_attempts_configure( + hass: HomeAssistant, + mock_rpc_device: Mock, + monkeypatch: pytest.MonkeyPatch, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test zeroconf discovery configures a sleeping device outbound websocket.""" + monkeypatch.setattr(mock_rpc_device, "connected", False) + monkeypatch.setattr(mock_rpc_device, "initialized", False) + monkeypatch.setitem(mock_rpc_device.status["sys"], "wakeup_period", 1000) + entry = MockConfigEntry( + domain="shelly", + unique_id="AABBCCDDEEFF", + data={"host": "1.1.1.1", "gen": 2, "sleep_period": 1000, "model": MODEL_1}, + ) + entry.add_to_hass(hass) + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + mock_rpc_device.mock_disconnected() + await hass.async_block_till_done() + + mock_rpc_device.mock_online() + await hass.async_block_till_done(wait_background_tasks=True) + + assert "online, resuming setup" in caplog.text + assert len(mock_rpc_device.initialize.mock_calls) == 1 + + with patch( + "homeassistant.components.shelly.config_flow.get_info", + return_value={"mac": "AABBCCDDEEFF", "type": MODEL_1, "auth": False}, + ): + result = await hass.config_entries.flow.async_init( + DOMAIN, + data=DISCOVERY_INFO, + context={"source": config_entries.SOURCE_ZEROCONF}, + ) + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "already_configured" + + assert mock_rpc_device.update_outbound_websocket.mock_calls == [] + + monkeypatch.setattr(mock_rpc_device, "connected", True) + monkeypatch.setattr(mock_rpc_device, "initialized", True) + mock_rpc_device.mock_initialized() + async_fire_time_changed( + hass, dt_util.utcnow() + timedelta(seconds=ENTRY_RELOAD_COOLDOWN) + ) + await hass.async_block_till_done() + assert "device did not update" not in caplog.text + + monkeypatch.setattr(mock_rpc_device, "connected", False) + mock_rpc_device.mock_disconnected() + assert mock_rpc_device.update_outbound_websocket.mock_calls == [ + call("ws://10.10.10.10:8123/api/shelly/ws") + ] + + +async def test_zeroconf_sleeping_device_attempts_configure_ws_disabled( + hass: HomeAssistant, + mock_rpc_device: Mock, + monkeypatch: pytest.MonkeyPatch, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test zeroconf discovery configures a sleeping device outbound websocket when its disabled.""" + monkeypatch.setattr(mock_rpc_device, "connected", False) + monkeypatch.setattr(mock_rpc_device, "initialized", False) + monkeypatch.setitem(mock_rpc_device.status["sys"], "wakeup_period", 1000) + monkeypatch.setitem( + mock_rpc_device.config, "ws", {"enable": False, "server": "ws://oldha"} + ) + entry = MockConfigEntry( + domain="shelly", + unique_id="AABBCCDDEEFF", + data={"host": "1.1.1.1", "gen": 2, "sleep_period": 1000, "model": MODEL_1}, + ) + entry.add_to_hass(hass) + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + mock_rpc_device.mock_disconnected() + await hass.async_block_till_done() + + mock_rpc_device.mock_online() + await hass.async_block_till_done(wait_background_tasks=True) + + assert "online, resuming setup" in caplog.text + assert len(mock_rpc_device.initialize.mock_calls) == 1 + + with patch( + "homeassistant.components.shelly.config_flow.get_info", + return_value={"mac": "AABBCCDDEEFF", "type": MODEL_1, "auth": False}, + ): + result = await hass.config_entries.flow.async_init( + DOMAIN, + data=DISCOVERY_INFO, + context={"source": config_entries.SOURCE_ZEROCONF}, + ) + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "already_configured" + + assert mock_rpc_device.update_outbound_websocket.mock_calls == [] + + monkeypatch.setattr(mock_rpc_device, "connected", True) + monkeypatch.setattr(mock_rpc_device, "initialized", True) + mock_rpc_device.mock_initialized() + async_fire_time_changed( + hass, dt_util.utcnow() + timedelta(seconds=ENTRY_RELOAD_COOLDOWN) + ) + await hass.async_block_till_done() + assert "device did not update" not in caplog.text + + monkeypatch.setattr(mock_rpc_device, "connected", False) + mock_rpc_device.mock_disconnected() + assert mock_rpc_device.update_outbound_websocket.mock_calls == [ + call("ws://10.10.10.10:8123/api/shelly/ws") + ] + + +async def test_zeroconf_sleeping_device_attempts_configure_no_url_available( + hass: HomeAssistant, + mock_rpc_device: Mock, + monkeypatch: pytest.MonkeyPatch, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test zeroconf discovery for sleeping device with no hass url.""" + hass.config.internal_url = None + hass.config.external_url = None + hass.config.api = None + monkeypatch.setattr(mock_rpc_device, "connected", False) + monkeypatch.setattr(mock_rpc_device, "initialized", False) + monkeypatch.setitem(mock_rpc_device.status["sys"], "wakeup_period", 1000) + entry = MockConfigEntry( + domain="shelly", + unique_id="AABBCCDDEEFF", + data={"host": "1.1.1.1", "gen": 2, "sleep_period": 1000, "model": MODEL_1}, + ) + entry.add_to_hass(hass) + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + mock_rpc_device.mock_disconnected() + await hass.async_block_till_done() + + mock_rpc_device.mock_online() + await hass.async_block_till_done(wait_background_tasks=True) + + assert "online, resuming setup" in caplog.text + assert len(mock_rpc_device.initialize.mock_calls) == 1 + + with patch( + "homeassistant.components.shelly.config_flow.get_info", + return_value={"mac": "AABBCCDDEEFF", "type": MODEL_1, "auth": False}, + ): + result = await hass.config_entries.flow.async_init( + DOMAIN, + data=DISCOVERY_INFO, + context={"source": config_entries.SOURCE_ZEROCONF}, + ) + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "already_configured" + + assert mock_rpc_device.update_outbound_websocket.mock_calls == [] + + monkeypatch.setattr(mock_rpc_device, "connected", True) + monkeypatch.setattr(mock_rpc_device, "initialized", True) + mock_rpc_device.mock_initialized() + async_fire_time_changed( + hass, dt_util.utcnow() + timedelta(seconds=ENTRY_RELOAD_COOLDOWN) + ) + await hass.async_block_till_done() + assert "device did not update" not in caplog.text + + monkeypatch.setattr(mock_rpc_device, "connected", False) + mock_rpc_device.mock_disconnected() + # No url available so no attempt to configure the device + assert mock_rpc_device.update_outbound_websocket.mock_calls == [] + + async def test_sleeping_device_gen2_with_new_firmware( hass: HomeAssistant, mock_rpc_device: Mock, monkeypatch: pytest.MonkeyPatch ) -> None: diff --git a/tests/components/shelly/test_coordinator.py b/tests/components/shelly/test_coordinator.py index 1140c93775b1bb..47c338e3fadb68 100644 --- a/tests/components/shelly/test_coordinator.py +++ b/tests/components/shelly/test_coordinator.py @@ -16,6 +16,7 @@ ATTR_DEVICE, ATTR_GENERATION, CONF_BLE_SCANNER_MODE, + CONF_SLEEP_PERIOD, DOMAIN, ENTRY_RELOAD_COOLDOWN, MAX_PUSH_UPDATE_FAILURES, @@ -677,7 +678,7 @@ async def test_rpc_polling_auth_error( monkeypatch.setattr( mock_rpc_device, - "update_status", + "poll", AsyncMock( side_effect=InvalidAuthError, ), @@ -767,7 +768,7 @@ async def test_rpc_polling_connection_error( monkeypatch.setattr( mock_rpc_device, - "update_status", + "poll", AsyncMock( side_effect=DeviceConnectionError, ), @@ -886,9 +887,14 @@ async def test_block_sleeping_device_connection_error( """Test block sleeping device connection error during initialize.""" sleep_period = 1000 entry = await init_integration(hass, 1, sleep_period=sleep_period, skip_setup=True) - register_device(device_registry, entry) + device = register_device(device_registry, entry) entity_id = register_entity( - hass, BINARY_SENSOR_DOMAIN, "test_name_motion", "sensor_0-motion", entry + hass, + BINARY_SENSOR_DOMAIN, + "test_name_motion", + "sensor_0-motion", + entry, + device_id=device.id, ) mock_restore_cache(hass, [State(entity_id, STATE_ON)]) monkeypatch.setattr(mock_block_device, "initialized", False) @@ -931,9 +937,14 @@ async def test_rpc_sleeping_device_connection_error( """Test RPC sleeping device connection error during initialize.""" sleep_period = 1000 entry = await init_integration(hass, 2, sleep_period=1000, skip_setup=True) - register_device(device_registry, entry) + device = register_device(device_registry, entry) entity_id = register_entity( - hass, BINARY_SENSOR_DOMAIN, "test_name_cloud", "cloud-cloud", entry + hass, + BINARY_SENSOR_DOMAIN, + "test_name_cloud", + "cloud-cloud", + entry, + device_id=device.id, ) mock_restore_cache(hass, [State(entity_id, STATE_ON)]) monkeypatch.setattr(mock_rpc_device, "connected", False) @@ -966,6 +977,31 @@ async def test_rpc_sleeping_device_connection_error( assert get_entity_state(hass, entity_id) == STATE_UNAVAILABLE +async def test_rpc_sleeping_device_late_setup( + hass: HomeAssistant, + device_registry: dr.DeviceRegistry, + mock_rpc_device: Mock, + monkeypatch: pytest.MonkeyPatch, +) -> None: + """Test RPC sleeping device creates entities if they do not exist yet.""" + entry = await init_integration(hass, 2, sleep_period=1000, skip_setup=True) + monkeypatch.setitem(mock_rpc_device.status["sys"], "wakeup_period", 1000) + assert entry.data[CONF_SLEEP_PERIOD] == 1000 + register_device(device_registry, entry) + monkeypatch.setattr(mock_rpc_device, "connected", False) + monkeypatch.setattr(mock_rpc_device, "initialized", False) + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + monkeypatch.setattr(mock_rpc_device, "initialized", True) + mock_rpc_device.mock_online() + await hass.async_block_till_done(wait_background_tasks=True) + monkeypatch.setattr(mock_rpc_device, "connected", True) + mock_rpc_device.mock_initialized() + await hass.async_block_till_done(wait_background_tasks=True) + assert hass.states.get("sensor.test_name_temperature") is not None + + async def test_rpc_already_connected( hass: HomeAssistant, freezer: FrozenDateTimeFactory, diff --git a/tests/components/shelly/test_diagnostics.py b/tests/components/shelly/test_diagnostics.py index 4fc8ea6ca8f446..395c7ccfeaf00b 100644 --- a/tests/components/shelly/test_diagnostics.py +++ b/tests/components/shelly/test_diagnostics.py @@ -1,5 +1,6 @@ """Tests for Shelly diagnostics platform.""" +from copy import deepcopy from unittest.mock import ANY, Mock, PropertyMock from aioshelly.ble.const import BLE_SCAN_RESULT_EVENT @@ -151,7 +152,7 @@ async def test_rpc_config_entry_diagnostics( "model": MODEL_25, "sw_version": "some fw string", }, - "device_settings": {}, + "device_settings": {"ws_outbound_enabled": False}, "device_status": { "sys": { "available_updates": { @@ -164,3 +165,30 @@ async def test_rpc_config_entry_diagnostics( }, "last_error": "DeviceConnectionError()", } + + +@pytest.mark.parametrize( + ("ws_outbound_server", "ws_outbound_server_valid"), + [("ws://10.10.10.10:8123/api/shelly/ws", True), ("wrong_url", False)], +) +async def test_rpc_config_entry_diagnostics_ws_outbound( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + mock_rpc_device: Mock, + monkeypatch: pytest.MonkeyPatch, + ws_outbound_server: str, + ws_outbound_server_valid: bool, +) -> None: + """Test config entry diagnostics for rpc device with websocket outbound.""" + config = deepcopy(mock_rpc_device.config) + config["ws"] = {"enable": True, "server": ws_outbound_server} + monkeypatch.setattr(mock_rpc_device, "config", config) + + entry = await init_integration(hass, 2, sleep_period=60) + + result = await get_diagnostics_for_config_entry(hass, hass_client, entry) + + assert ( + result["device_settings"]["ws_outbound_server_valid"] + == ws_outbound_server_valid + ) diff --git a/tests/components/shelly/test_init.py b/tests/components/shelly/test_init.py index 46698c23c0a6de..b5516485501424 100644 --- a/tests/components/shelly/test_init.py +++ b/tests/components/shelly/test_init.py @@ -310,6 +310,52 @@ async def test_sleeping_rpc_device_online_new_firmware( assert entry.data["sleep_period"] == 1500 +async def test_sleeping_rpc_device_online_during_setup( + hass: HomeAssistant, + mock_rpc_device: Mock, + monkeypatch: pytest.MonkeyPatch, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test sleeping device Gen2 woke up by user during setup.""" + monkeypatch.setattr(mock_rpc_device, "connected", False) + monkeypatch.setitem(mock_rpc_device.status["sys"], "wakeup_period", 1000) + await init_integration(hass, 2, sleep_period=1000) + await hass.async_block_till_done(wait_background_tasks=True) + + assert "will resume when device is online" in caplog.text + assert "is online (source: setup)" in caplog.text + assert hass.states.get("sensor.test_name_temperature") is not None + + +async def test_sleeping_rpc_device_offline_during_setup( + hass: HomeAssistant, + mock_rpc_device: Mock, + monkeypatch: pytest.MonkeyPatch, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test sleeping device Gen2 woke up by user during setup.""" + monkeypatch.setattr(mock_rpc_device, "connected", False) + monkeypatch.setitem(mock_rpc_device.status["sys"], "wakeup_period", 1000) + monkeypatch.setattr( + mock_rpc_device, "initialize", AsyncMock(side_effect=DeviceConnectionError) + ) + + # Init integration, should fail since device is offline + await init_integration(hass, 2, sleep_period=1000) + await hass.async_block_till_done(wait_background_tasks=True) + + assert "will resume when device is online" in caplog.text + assert "is online (source: setup)" in caplog.text + assert hass.states.get("sensor.test_name_temperature") is None + + # Create an online event and verify that device is init successfully + monkeypatch.setattr(mock_rpc_device, "initialize", AsyncMock()) + mock_rpc_device.mock_online() + await hass.async_block_till_done(wait_background_tasks=True) + + assert hass.states.get("sensor.test_name_temperature") is not None + + @pytest.mark.parametrize( ("gen", "entity_id"), [ diff --git a/tests/components/shelly/test_number.py b/tests/components/shelly/test_number.py index 73f432094b92bc..6c1cc394b64fce 100644 --- a/tests/components/shelly/test_number.py +++ b/tests/components/shelly/test_number.py @@ -72,7 +72,7 @@ async def test_block_restored_number( ) -> None: """Test block restored number.""" entry = await init_integration(hass, 1, sleep_period=1000, skip_setup=True) - register_device(device_registry, entry) + device = register_device(device_registry, entry) capabilities = { "min": 0, "max": 100, @@ -86,6 +86,7 @@ async def test_block_restored_number( "device_0-valvePos", entry, capabilities, + device_id=device.id, ) extra_data = { "native_max_value": 100, @@ -118,7 +119,7 @@ async def test_block_restored_number_no_last_state( ) -> None: """Test block restored number missing last state.""" entry = await init_integration(hass, 1, sleep_period=1000, skip_setup=True) - register_device(device_registry, entry) + device = register_device(device_registry, entry) capabilities = { "min": 0, "max": 100, @@ -132,6 +133,7 @@ async def test_block_restored_number_no_last_state( "device_0-valvePos", entry, capabilities, + device_id=device.id, ) monkeypatch.setattr(mock_block_device, "initialized", False) await hass.config_entries.async_setup(entry.entry_id) diff --git a/tests/components/shelly/test_sensor.py b/tests/components/shelly/test_sensor.py index 843270107e0a0e..ef8a609998a97c 100644 --- a/tests/components/shelly/test_sensor.py +++ b/tests/components/shelly/test_sensor.py @@ -47,7 +47,7 @@ register_entity, ) -from tests.common import mock_restore_cache_with_extra_data +from tests.common import async_fire_time_changed, mock_restore_cache_with_extra_data RELAY_BLOCK_ID = 0 SENSOR_BLOCK_ID = 3 @@ -193,9 +193,14 @@ async def test_block_restored_sleeping_sensor( ) -> None: """Test block restored sleeping sensor.""" entry = await init_integration(hass, 1, sleep_period=1000, skip_setup=True) - register_device(device_registry, entry) + device = register_device(device_registry, entry) entity_id = register_entity( - hass, SENSOR_DOMAIN, "test_name_temperature", "sensor_0-temp", entry + hass, + SENSOR_DOMAIN, + "test_name_temperature", + "sensor_0-temp", + entry, + device_id=device.id, ) extra_data = {"native_value": "20.4", "native_unit_of_measurement": "°C"} @@ -226,9 +231,14 @@ async def test_block_restored_sleeping_sensor_no_last_state( ) -> None: """Test block restored sleeping sensor missing last state.""" entry = await init_integration(hass, 1, sleep_period=1000, skip_setup=True) - register_device(device_registry, entry) + device = register_device(device_registry, entry) entity_id = register_entity( - hass, SENSOR_DOMAIN, "test_name_temperature", "sensor_0-temp", entry + hass, + SENSOR_DOMAIN, + "test_name_temperature", + "sensor_0-temp", + entry, + device_id=device.id, ) monkeypatch.setattr(mock_block_device, "initialized", False) await hass.config_entries.async_setup(entry.entry_id) @@ -293,9 +303,14 @@ async def test_block_not_matched_restored_sleeping_sensor( ) -> None: """Test block not matched to restored sleeping sensor.""" entry = await init_integration(hass, 1, sleep_period=1000, skip_setup=True) - register_device(device_registry, entry) + device = register_device(device_registry, entry) entity_id = register_entity( - hass, SENSOR_DOMAIN, "test_name_temperature", "sensor_0-temp", entry + hass, + SENSOR_DOMAIN, + "test_name_temperature", + "sensor_0-temp", + entry, + device_id=device.id, ) extra_data = {"native_value": "20.4", "native_unit_of_measurement": "°C"} @@ -489,13 +504,14 @@ async def test_rpc_restored_sleeping_sensor( ) -> None: """Test RPC restored sensor.""" entry = await init_integration(hass, 2, sleep_period=1000, skip_setup=True) - register_device(device_registry, entry) + device = register_device(device_registry, entry) entity_id = register_entity( hass, SENSOR_DOMAIN, "test_name_temperature", "temperature:0-temperature_0", entry, + device_id=device.id, ) extra_data = {"native_value": "21.0", "native_unit_of_measurement": "°C"} @@ -527,13 +543,14 @@ async def test_rpc_restored_sleeping_sensor_no_last_state( ) -> None: """Test RPC restored sensor missing last state.""" entry = await init_integration(hass, 2, sleep_period=1000, skip_setup=True) - register_device(device_registry, entry) + device = register_device(device_registry, entry) entity_id = register_entity( hass, SENSOR_DOMAIN, "test_name_temperature", "temperature:0-temperature_0", entry, + device_id=device.id, ) monkeypatch.setattr(mock_rpc_device, "initialized", False) @@ -689,10 +706,27 @@ async def test_block_sleeping_update_entity_service( ) +@pytest.mark.parametrize( + ("original_unit", "expected_unit"), + [ + ("m/s", "m/s"), + (None, None), + ("", None), + ], +) async def test_rpc_analog_input_sensors( - hass: HomeAssistant, mock_rpc_device: Mock, entity_registry: EntityRegistry + hass: HomeAssistant, + mock_rpc_device: Mock, + entity_registry: EntityRegistry, + monkeypatch: pytest.MonkeyPatch, + original_unit: str | None, + expected_unit: str | None, ) -> None: """Test RPC analog input xpercent sensor.""" + config = deepcopy(mock_rpc_device.config) + config["input:1"]["xpercent"] = {"expr": "x*0.2995", "unit": original_unit} + monkeypatch.setattr(mock_rpc_device, "config", config) + await init_integration(hass, 2) entity_id = f"{SENSOR_DOMAIN}.test_name_analog_input" @@ -703,7 +737,10 @@ async def test_rpc_analog_input_sensors( assert entry.unique_id == "123456789ABC-input:1-analoginput" entity_id = f"{SENSOR_DOMAIN}.test_name_analog_value" - assert hass.states.get(entity_id).state == "8.9" + state = hass.states.get(entity_id) + assert state + assert state.state == "8.9" + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == expected_unit entry = entity_registry.async_get(entity_id) assert entry @@ -747,13 +784,27 @@ async def test_rpc_disabled_xpercent( assert hass.states.get(entity_id) is None +@pytest.mark.parametrize( + ("original_unit", "expected_unit"), + [ + ("l/h", "l/h"), + (None, None), + ("", None), + ], +) async def test_rpc_pulse_counter_sensors( hass: HomeAssistant, mock_rpc_device: Mock, entity_registry: EntityRegistry, monkeypatch: pytest.MonkeyPatch, + original_unit: str | None, + expected_unit: str | None, ) -> None: """Test RPC counter sensor.""" + config = deepcopy(mock_rpc_device.config) + config["input:2"]["xcounts"] = {"expr": "x/10", "unit": original_unit} + monkeypatch.setattr(mock_rpc_device, "config", config) + await init_integration(hass, 2) entity_id = f"{SENSOR_DOMAIN}.gas_pulse_counter" @@ -767,7 +818,10 @@ async def test_rpc_pulse_counter_sensors( assert entry.unique_id == "123456789ABC-input:2-pulse_counter" entity_id = f"{SENSOR_DOMAIN}.gas_counter_value" - assert hass.states.get(entity_id).state == "561.74" + state = hass.states.get(entity_id) + assert state + assert state.state == "561.74" + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == expected_unit entry = entity_registry.async_get(entity_id) assert entry @@ -811,12 +865,27 @@ async def test_rpc_disabled_xtotal_counter( assert hass.states.get(entity_id) is None +@pytest.mark.parametrize( + ("original_unit", "expected_unit"), + [ + ("W", "W"), + (None, None), + ("", None), + ], +) async def test_rpc_pulse_counter_frequency_sensors( hass: HomeAssistant, mock_rpc_device: Mock, entity_registry: EntityRegistry, + monkeypatch: pytest.MonkeyPatch, + original_unit: str | None, + expected_unit: str | None, ) -> None: """Test RPC counter sensor.""" + config = deepcopy(mock_rpc_device.config) + config["input:2"]["xfreq"] = {"expr": "x**2", "unit": original_unit} + monkeypatch.setattr(mock_rpc_device, "config", config) + await init_integration(hass, 2) entity_id = f"{SENSOR_DOMAIN}.gas_pulse_counter_frequency" @@ -830,7 +899,10 @@ async def test_rpc_pulse_counter_frequency_sensors( assert entry.unique_id == "123456789ABC-input:2-counter_frequency" entity_id = f"{SENSOR_DOMAIN}.gas_pulse_counter_frequency_value" - assert hass.states.get(entity_id).state == "6.11" + state = hass.states.get(entity_id) + assert state + assert state.state == "6.11" + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == expected_unit entry = entity_registry.async_get(entity_id) assert entry @@ -1279,3 +1351,35 @@ async def test_rpc_rgbw_sensors( entry = entity_registry.async_get(entity_id) assert entry assert entry.unique_id == f"123456789ABC-{light_type}:0-temperature_{light_type}" + + +async def test_rpc_device_sensor_goes_unavailable_on_disconnect( + hass: HomeAssistant, + mock_rpc_device: Mock, + monkeypatch: pytest.MonkeyPatch, + freezer: FrozenDateTimeFactory, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test RPC device with sensor goes unavailable on disconnect.""" + await init_integration(hass, 2) + temp_sensor_state = hass.states.get("sensor.test_name_temperature") + assert temp_sensor_state is not None + assert temp_sensor_state.state != STATE_UNAVAILABLE + monkeypatch.setattr(mock_rpc_device, "connected", False) + monkeypatch.setattr(mock_rpc_device, "initialized", False) + mock_rpc_device.mock_disconnected() + await hass.async_block_till_done() + temp_sensor_state = hass.states.get("sensor.test_name_temperature") + assert temp_sensor_state.state == STATE_UNAVAILABLE + + freezer.tick(60) + async_fire_time_changed(hass) + await hass.async_block_till_done() + assert "NotInitialized" not in caplog.text + + monkeypatch.setattr(mock_rpc_device, "connected", True) + monkeypatch.setattr(mock_rpc_device, "initialized", True) + mock_rpc_device.mock_initialized() + await hass.async_block_till_done() + temp_sensor_state = hass.states.get("sensor.test_name_temperature") + assert temp_sensor_state.state != STATE_UNAVAILABLE diff --git a/tests/components/shelly/test_switch.py b/tests/components/shelly/test_switch.py index 124562be8d542b..c891d1d7b2d896 100644 --- a/tests/components/shelly/test_switch.py +++ b/tests/components/shelly/test_switch.py @@ -118,13 +118,14 @@ async def test_block_restored_motion_switch( entry = await init_integration( hass, 1, sleep_period=1000, model=model, skip_setup=True ) - register_device(device_registry, entry) + device = register_device(device_registry, entry) entity_id = register_entity( hass, SWITCH_DOMAIN, "test_name_motion_detection", "sensor_0-motionActive", entry, + device_id=device.id, ) mock_restore_cache(hass, [State(entity_id, STATE_OFF)]) @@ -154,13 +155,14 @@ async def test_block_restored_motion_switch_no_last_state( entry = await init_integration( hass, 1, sleep_period=1000, model=model, skip_setup=True ) - register_device(device_registry, entry) + device = register_device(device_registry, entry) entity_id = register_entity( hass, SWITCH_DOMAIN, "test_name_motion_detection", "sensor_0-motionActive", entry, + device_id=device.id, ) monkeypatch.setattr(mock_block_device, "initialized", False) await hass.config_entries.async_setup(entry.entry_id) diff --git a/tests/components/shelly/test_update.py b/tests/components/shelly/test_update.py index 721e86559a3331..c6434c0b98819e 100644 --- a/tests/components/shelly/test_update.py +++ b/tests/components/shelly/test_update.py @@ -385,13 +385,14 @@ async def test_rpc_restored_sleeping_update( ) -> None: """Test RPC restored update entity.""" entry = await init_integration(hass, 2, sleep_period=1000, skip_setup=True) - register_device(device_registry, entry) + device = register_device(device_registry, entry) entity_id = register_entity( hass, UPDATE_DOMAIN, "test_name_firmware_update", "sys-fwupdate", entry, + device_id=device.id, ) attr = {ATTR_INSTALLED_VERSION: "1", ATTR_LATEST_VERSION: "2"} @@ -443,13 +444,14 @@ async def test_rpc_restored_sleeping_update_no_last_state( }, ) entry = await init_integration(hass, 2, sleep_period=1000, skip_setup=True) - register_device(device_registry, entry) + device = register_device(device_registry, entry) entity_id = register_entity( hass, UPDATE_DOMAIN, "test_name_firmware_update", "sys-fwupdate", entry, + device_id=device.id, ) monkeypatch.setattr(mock_rpc_device, "initialized", False) diff --git a/tests/components/simplefin/snapshots/test_binary_sensor.ambr b/tests/components/simplefin/snapshots/test_binary_sensor.ambr new file mode 100644 index 00000000000000..be26ae1a03dd0e --- /dev/null +++ b/tests/components/simplefin/snapshots/test_binary_sensor.ambr @@ -0,0 +1,769 @@ +# serializer version: 1 +# name: test_all_entities[binary_sensor.investments_dr_evil_possible_error-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.investments_dr_evil_possible_error', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Possible error', + 'platform': 'simplefin', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'possible_error', + 'unique_id': 'account_ACT-4k5l6m7n-8o9p-1q2r-3s4t_possible_error', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_entities[binary_sensor.investments_dr_evil_possible_error-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by SimpleFIN API', + 'device_class': 'problem', + 'friendly_name': 'Investments Dr Evil Possible error', + }), + 'context': , + 'entity_id': 'binary_sensor.investments_dr_evil_possible_error', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_all_entities[binary_sensor.investments_dr_evil_problem-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.investments_dr_evil_problem', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Problem', + 'platform': 'simplefin', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'possible_error', + 'unique_id': 'account_ACT-4k5l6m7n-8o9p-1q2r-3s4t_possible_error', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_entities[binary_sensor.investments_dr_evil_problem-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by SimpleFIN API', + 'device_class': 'problem', + 'friendly_name': 'Investments Dr Evil Problem', + }), + 'context': , + 'entity_id': 'binary_sensor.investments_dr_evil_problem', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_all_entities[binary_sensor.investments_my_checking_possible_error-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.investments_my_checking_possible_error', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Possible error', + 'platform': 'simplefin', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'possible_error', + 'unique_id': 'account_ACT-1k2l3m4n-5o6p-7q8r-9s0t_possible_error', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_entities[binary_sensor.investments_my_checking_possible_error-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by SimpleFIN API', + 'device_class': 'problem', + 'friendly_name': 'Investments My Checking Possible error', + }), + 'context': , + 'entity_id': 'binary_sensor.investments_my_checking_possible_error', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_all_entities[binary_sensor.investments_my_checking_problem-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.investments_my_checking_problem', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Problem', + 'platform': 'simplefin', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'possible_error', + 'unique_id': 'account_ACT-1k2l3m4n-5o6p-7q8r-9s0t_possible_error', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_entities[binary_sensor.investments_my_checking_problem-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by SimpleFIN API', + 'device_class': 'problem', + 'friendly_name': 'Investments My Checking Problem', + }), + 'context': , + 'entity_id': 'binary_sensor.investments_my_checking_problem', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_all_entities[binary_sensor.investments_nerdcorp_series_b_possible_error-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.investments_nerdcorp_series_b_possible_error', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Possible error', + 'platform': 'simplefin', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'possible_error', + 'unique_id': 'account_ACT-5k6l7m8n-9o0p-1q2r-3s4t_possible_error', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_entities[binary_sensor.investments_nerdcorp_series_b_possible_error-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by SimpleFIN API', + 'device_class': 'problem', + 'friendly_name': 'Investments NerdCorp Series B Possible error', + }), + 'context': , + 'entity_id': 'binary_sensor.investments_nerdcorp_series_b_possible_error', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_all_entities[binary_sensor.investments_nerdcorp_series_b_problem-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.investments_nerdcorp_series_b_problem', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Problem', + 'platform': 'simplefin', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'possible_error', + 'unique_id': 'account_ACT-5k6l7m8n-9o0p-1q2r-3s4t_possible_error', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_entities[binary_sensor.investments_nerdcorp_series_b_problem-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by SimpleFIN API', + 'device_class': 'problem', + 'friendly_name': 'Investments NerdCorp Series B Problem', + }), + 'context': , + 'entity_id': 'binary_sensor.investments_nerdcorp_series_b_problem', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_all_entities[binary_sensor.mythical_randomsavings_castle_mortgage_possible_error-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.mythical_randomsavings_castle_mortgage_possible_error', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Possible error', + 'platform': 'simplefin', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'possible_error', + 'unique_id': 'account_ACT-7a8b9c0d-1e2f-3g4h-5i6j_possible_error', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_entities[binary_sensor.mythical_randomsavings_castle_mortgage_possible_error-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by SimpleFIN API', + 'device_class': 'problem', + 'friendly_name': 'Mythical RandomSavings Castle Mortgage Possible error', + }), + 'context': , + 'entity_id': 'binary_sensor.mythical_randomsavings_castle_mortgage_possible_error', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_all_entities[binary_sensor.mythical_randomsavings_castle_mortgage_problem-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.mythical_randomsavings_castle_mortgage_problem', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Problem', + 'platform': 'simplefin', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'possible_error', + 'unique_id': 'account_ACT-7a8b9c0d-1e2f-3g4h-5i6j_possible_error', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_entities[binary_sensor.mythical_randomsavings_castle_mortgage_problem-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by SimpleFIN API', + 'device_class': 'problem', + 'friendly_name': 'Mythical RandomSavings Castle Mortgage Problem', + }), + 'context': , + 'entity_id': 'binary_sensor.mythical_randomsavings_castle_mortgage_problem', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_all_entities[binary_sensor.mythical_randomsavings_unicorn_pot_possible_error-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.mythical_randomsavings_unicorn_pot_possible_error', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Possible error', + 'platform': 'simplefin', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'possible_error', + 'unique_id': 'account_ACT-6a7b8c9d-0e1f-2g3h-4i5j_possible_error', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_entities[binary_sensor.mythical_randomsavings_unicorn_pot_possible_error-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by SimpleFIN API', + 'device_class': 'problem', + 'friendly_name': 'Mythical RandomSavings Unicorn Pot Possible error', + }), + 'context': , + 'entity_id': 'binary_sensor.mythical_randomsavings_unicorn_pot_possible_error', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_all_entities[binary_sensor.mythical_randomsavings_unicorn_pot_problem-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.mythical_randomsavings_unicorn_pot_problem', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Problem', + 'platform': 'simplefin', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'possible_error', + 'unique_id': 'account_ACT-6a7b8c9d-0e1f-2g3h-4i5j_possible_error', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_entities[binary_sensor.mythical_randomsavings_unicorn_pot_problem-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by SimpleFIN API', + 'device_class': 'problem', + 'friendly_name': 'Mythical RandomSavings Unicorn Pot Problem', + }), + 'context': , + 'entity_id': 'binary_sensor.mythical_randomsavings_unicorn_pot_problem', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_all_entities[binary_sensor.random_bank_costco_anywhere_visa_r_card_possible_error-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.random_bank_costco_anywhere_visa_r_card_possible_error', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Possible error', + 'platform': 'simplefin', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'possible_error', + 'unique_id': 'account_ACT-3a4b5c6d-7e8f-9g0h-1i2j_possible_error', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_entities[binary_sensor.random_bank_costco_anywhere_visa_r_card_possible_error-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by SimpleFIN API', + 'device_class': 'problem', + 'friendly_name': 'Random Bank Costco Anywhere Visa® Card Possible error', + }), + 'context': , + 'entity_id': 'binary_sensor.random_bank_costco_anywhere_visa_r_card_possible_error', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_all_entities[binary_sensor.random_bank_costco_anywhere_visa_r_card_problem-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.random_bank_costco_anywhere_visa_r_card_problem', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Problem', + 'platform': 'simplefin', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'possible_error', + 'unique_id': 'account_ACT-3a4b5c6d-7e8f-9g0h-1i2j_possible_error', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_entities[binary_sensor.random_bank_costco_anywhere_visa_r_card_problem-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by SimpleFIN API', + 'device_class': 'problem', + 'friendly_name': 'Random Bank Costco Anywhere Visa® Card Problem', + }), + 'context': , + 'entity_id': 'binary_sensor.random_bank_costco_anywhere_visa_r_card_problem', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_all_entities[binary_sensor.the_bank_of_go_prime_savings_possible_error-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.the_bank_of_go_prime_savings_possible_error', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Possible error', + 'platform': 'simplefin', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'possible_error', + 'unique_id': 'account_ACT-2a3b4c5d-6e7f-8g9h-0i1j_possible_error', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_entities[binary_sensor.the_bank_of_go_prime_savings_possible_error-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by SimpleFIN API', + 'device_class': 'problem', + 'friendly_name': 'The Bank of Go PRIME SAVINGS Possible error', + }), + 'context': , + 'entity_id': 'binary_sensor.the_bank_of_go_prime_savings_possible_error', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_all_entities[binary_sensor.the_bank_of_go_prime_savings_problem-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.the_bank_of_go_prime_savings_problem', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Problem', + 'platform': 'simplefin', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'possible_error', + 'unique_id': 'account_ACT-2a3b4c5d-6e7f-8g9h-0i1j_possible_error', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_entities[binary_sensor.the_bank_of_go_prime_savings_problem-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by SimpleFIN API', + 'device_class': 'problem', + 'friendly_name': 'The Bank of Go PRIME SAVINGS Problem', + }), + 'context': , + 'entity_id': 'binary_sensor.the_bank_of_go_prime_savings_problem', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_all_entities[binary_sensor.the_bank_of_go_the_bank_possible_error-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.the_bank_of_go_the_bank_possible_error', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Possible error', + 'platform': 'simplefin', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'possible_error', + 'unique_id': 'account_ACT-1a2b3c4d-5e6f-7g8h-9i0j_possible_error', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_entities[binary_sensor.the_bank_of_go_the_bank_possible_error-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by SimpleFIN API', + 'device_class': 'problem', + 'friendly_name': 'The Bank of Go The Bank Possible error', + }), + 'context': , + 'entity_id': 'binary_sensor.the_bank_of_go_the_bank_possible_error', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_all_entities[binary_sensor.the_bank_of_go_the_bank_problem-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.the_bank_of_go_the_bank_problem', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Problem', + 'platform': 'simplefin', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'possible_error', + 'unique_id': 'account_ACT-1a2b3c4d-5e6f-7g8h-9i0j_possible_error', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_entities[binary_sensor.the_bank_of_go_the_bank_problem-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by SimpleFIN API', + 'device_class': 'problem', + 'friendly_name': 'The Bank of Go The Bank Problem', + }), + 'context': , + 'entity_id': 'binary_sensor.the_bank_of_go_the_bank_problem', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- diff --git a/tests/components/simplefin/test_binary_sensor.py b/tests/components/simplefin/test_binary_sensor.py new file mode 100644 index 00000000000000..40c6882153d699 --- /dev/null +++ b/tests/components/simplefin/test_binary_sensor.py @@ -0,0 +1,29 @@ +"""Test SimpleFin Sensor with Snapshot data.""" + +from unittest.mock import AsyncMock, patch + +from syrupy import SnapshotAssertion + +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from . import setup_integration + +from tests.common import MockConfigEntry, snapshot_platform + + +async def test_all_entities( + hass: HomeAssistant, + snapshot: SnapshotAssertion, + mock_config_entry: MockConfigEntry, + entity_registry: er.EntityRegistry, + mock_simplefin_client: AsyncMock, +) -> None: + """Test all entities.""" + with patch( + "homeassistant.components.simplefin.PLATFORMS", [Platform.BINARY_SENSOR] + ): + await setup_integration(hass, mock_config_entry) + + await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) diff --git a/tests/components/simplisafe/test_config_flow.py b/tests/components/simplisafe/test_config_flow.py index dde7e37b891146..9270fc43c3031f 100644 --- a/tests/components/simplisafe/test_config_flow.py +++ b/tests/components/simplisafe/test_config_flow.py @@ -8,11 +8,13 @@ from homeassistant.components.simplisafe import DOMAIN from homeassistant.components.simplisafe.config_flow import CONF_AUTH_CODE -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_USER from homeassistant.const import CONF_CODE, CONF_TOKEN, CONF_USERNAME from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType +from tests.common import MockConfigEntry + VALID_AUTH_CODE = "code12345123451234512345123451234512345123451" @@ -90,13 +92,11 @@ async def test_options_flow(config_entry, hass: HomeAssistant) -> None: assert config_entry.options == {CONF_CODE: "4321"} -async def test_step_reauth(config_entry, hass: HomeAssistant, setup_simplisafe) -> None: +async def test_step_reauth( + config_entry: MockConfigEntry, hass: HomeAssistant, setup_simplisafe +) -> None: """Test the re-auth step.""" - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH}, - data={CONF_USERNAME: "12345", CONF_TOKEN: "token123"}, - ) + result = await config_entry.start_reauth_flow(hass) assert result["step_id"] == "user" with ( @@ -118,14 +118,10 @@ async def test_step_reauth(config_entry, hass: HomeAssistant, setup_simplisafe) @pytest.mark.parametrize("unique_id", ["some_other_id"]) async def test_step_reauth_wrong_account( - config_entry, hass: HomeAssistant, setup_simplisafe + config_entry: MockConfigEntry, hass: HomeAssistant, setup_simplisafe ) -> None: """Test the re-auth step where the wrong account is used during login.""" - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH}, - data={CONF_USERNAME: "12345", CONF_TOKEN: "token123"}, - ) + result = await config_entry.start_reauth_flow(hass) assert result["step_id"] == "user" with ( diff --git a/tests/components/skybell/test_config_flow.py b/tests/components/skybell/test_config_flow.py index cb62f808efc7a6..f415fef077e6b4 100644 --- a/tests/components/skybell/test_config_flow.py +++ b/tests/components/skybell/test_config_flow.py @@ -5,10 +5,9 @@ from aioskybell import exceptions import pytest -from homeassistant import config_entries from homeassistant.components.skybell.const import DOMAIN from homeassistant.config_entries import SOURCE_USER -from homeassistant.const import CONF_PASSWORD, CONF_SOURCE +from homeassistant.const import CONF_PASSWORD from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -104,15 +103,7 @@ async def test_step_reauth(hass: HomeAssistant) -> None: entry = MockConfigEntry(domain=DOMAIN, unique_id=USER_ID, data=CONF_DATA) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - CONF_SOURCE: config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - "unique_id": entry.unique_id, - }, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -130,15 +121,7 @@ async def test_step_reauth_failed(hass: HomeAssistant, skybell_mock) -> None: entry = MockConfigEntry(domain=DOMAIN, unique_id=USER_ID, data=CONF_DATA) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - CONF_SOURCE: config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - "unique_id": entry.unique_id, - }, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/sleepiq/test_config_flow.py b/tests/components/sleepiq/test_config_flow.py index af08f5aa9feef6..26007d42e7dad8 100644 --- a/tests/components/sleepiq/test_config_flow.py +++ b/tests/components/sleepiq/test_config_flow.py @@ -101,19 +101,7 @@ async def test_reauth_password(hass: HomeAssistant) -> None: # set up initially entry = await setup_platform(hass) - with patch( - "homeassistant.components.sleepiq.config_flow.AsyncSleepIQ.login", - side_effect=SleepIQLoginException, - ): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - "unique_id": entry.unique_id, - }, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) with patch( "homeassistant.components.sleepiq.config_flow.AsyncSleepIQ.login", diff --git a/tests/components/smartthings/test_climate.py b/tests/components/smartthings/test_climate.py index e4b8cb6d373a2e..d39ee2d6bed1c4 100644 --- a/tests/components/smartthings/test_climate.py +++ b/tests/components/smartthings/test_climate.py @@ -88,6 +88,26 @@ def basic_thermostat_fixture(device_factory): return device +@pytest.fixture(name="minimal_thermostat") +def minimal_thermostat_fixture(device_factory): + """Fixture returns a minimal thermostat without cooling.""" + device = device_factory( + "Minimal Thermostat", + capabilities=[ + Capability.temperature_measurement, + Capability.thermostat_heating_setpoint, + Capability.thermostat_mode, + ], + status={ + Attribute.heating_setpoint: 68, + Attribute.thermostat_mode: "off", + Attribute.supported_thermostat_modes: ["off", "heat"], + }, + ) + device.status.attributes[Attribute.temperature] = Status(70, "F", None) + return device + + @pytest.fixture(name="thermostat") def thermostat_fixture(device_factory): """Fixture returns a fully-featured thermostat.""" @@ -310,6 +330,28 @@ async def test_basic_thermostat_entity_state( assert state.attributes[ATTR_CURRENT_TEMPERATURE] == 21.1 # celsius +async def test_minimal_thermostat_entity_state( + hass: HomeAssistant, minimal_thermostat +) -> None: + """Tests the state attributes properly match the thermostat type.""" + await setup_platform(hass, CLIMATE_DOMAIN, devices=[minimal_thermostat]) + state = hass.states.get("climate.minimal_thermostat") + assert state.state == HVACMode.OFF + assert ( + state.attributes[ATTR_SUPPORTED_FEATURES] + == ClimateEntityFeature.TARGET_TEMPERATURE_RANGE + | ClimateEntityFeature.TARGET_TEMPERATURE + | ClimateEntityFeature.TURN_OFF + | ClimateEntityFeature.TURN_ON + ) + assert ATTR_HVAC_ACTION not in state.attributes + assert sorted(state.attributes[ATTR_HVAC_MODES]) == [ + HVACMode.HEAT, + HVACMode.OFF, + ] + assert state.attributes[ATTR_CURRENT_TEMPERATURE] == 21.1 # celsius + + async def test_thermostat_entity_state(hass: HomeAssistant, thermostat) -> None: """Tests the state attributes properly match the thermostat type.""" await setup_platform(hass, CLIMATE_DOMAIN, devices=[thermostat]) diff --git a/tests/components/smarttub/test_config_flow.py b/tests/components/smarttub/test_config_flow.py index c625f217405558..5832841641cd24 100644 --- a/tests/components/smarttub/test_config_flow.py +++ b/tests/components/smarttub/test_config_flow.py @@ -66,15 +66,7 @@ async def test_reauth_success(hass: HomeAssistant, smarttub_api, account) -> Non ) mock_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": mock_entry.unique_id, - "entry_id": mock_entry.entry_id, - }, - data=mock_entry.data, - ) + result = await mock_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -107,15 +99,7 @@ async def test_reauth_wrong_account(hass: HomeAssistant, smarttub_api, account) # we try to reauth account #2, and the user successfully authenticates to account #1 account.id = mock_entry1.unique_id - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": mock_entry2.unique_id, - "entry_id": mock_entry2.entry_id, - }, - data=mock_entry2.data, - ) + result = await mock_entry2.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/smlight/conftest.py b/tests/components/smlight/conftest.py index 0338bf4b672be5..a86c7b4c27a49a 100644 --- a/tests/components/smlight/conftest.py +++ b/tests/components/smlight/conftest.py @@ -1,13 +1,14 @@ """Common fixtures for the SMLIGHT Zigbee tests.""" -from collections.abc import Generator +from collections.abc import AsyncGenerator, Generator from unittest.mock import AsyncMock, MagicMock, patch -from pysmlight.web import Info, Sensors +from pysmlight.web import CmdWrapper, Info, Sensors import pytest +from homeassistant.components.smlight import PLATFORMS from homeassistant.components.smlight.const import DOMAIN -from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME +from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME, Platform from homeassistant.core import HomeAssistant from tests.common import MockConfigEntry, load_json_object_fixture @@ -32,7 +33,32 @@ def mock_config_entry() -> MockConfigEntry: @pytest.fixture -def mock_setup_entry() -> Generator[AsyncMock, None, None]: +def mock_config_entry_host() -> MockConfigEntry: + """Return the default mocked config entry, no credentials.""" + return MockConfigEntry( + domain=DOMAIN, + data={ + CONF_HOST: MOCK_HOST, + }, + unique_id="aa:bb:cc:dd:ee:ff", + ) + + +@pytest.fixture +def platforms() -> list[Platform]: + """Platforms, which should be loaded during the test.""" + return PLATFORMS + + +@pytest.fixture(autouse=True) +async def mock_patch_platforms(platforms: list[str]) -> AsyncGenerator[None]: + """Fixture to set up platforms for tests.""" + with patch(f"homeassistant.components.{DOMAIN}.PLATFORMS", platforms): + yield + + +@pytest.fixture +def mock_setup_entry() -> Generator[AsyncMock]: """Override async_setup_entry.""" with patch( "homeassistant.components.smlight.async_setup_entry", return_value=True @@ -61,10 +87,15 @@ def mock_smlight_client(request: pytest.FixtureRequest) -> Generator[MagicMock]: api.check_auth_needed.return_value = False api.authenticate.return_value = True + api.cmds = AsyncMock(spec_set=CmdWrapper) + yield api -async def setup_integration(hass: HomeAssistant, mock_config_entry: MockConfigEntry): +async def setup_integration( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, +) -> MockConfigEntry: """Set up the integration.""" mock_config_entry.add_to_hass(hass) diff --git a/tests/components/smlight/snapshots/test_sensor.ambr b/tests/components/smlight/snapshots/test_sensor.ambr index 0ff3d37b735b60..6895a8473bd2f4 100644 --- a/tests/components/smlight/snapshots/test_sensor.ambr +++ b/tests/components/smlight/snapshots/test_sensor.ambr @@ -53,6 +53,53 @@ 'state': '35.0', }) # --- +# name: test_sensors[sensor.mock_title_core_uptime-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.mock_title_core_uptime', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Core uptime', + 'platform': 'smlight', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'core_uptime', + 'unique_id': 'aa:bb:cc:dd:ee:ff_core_uptime', + 'unit_of_measurement': None, + }) +# --- +# name: test_sensors[sensor.mock_title_core_uptime-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'timestamp', + 'friendly_name': 'Mock Title Core uptime', + }), + 'context': , + 'entity_id': 'sensor.mock_title_core_uptime', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '2024-06-25T02:51:15+00:00', + }) +# --- # name: test_sensors[sensor.mock_title_filesystem_usage-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ @@ -149,6 +196,100 @@ 'state': '99', }) # --- +# name: test_sensors[sensor.mock_title_timestamp-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.mock_title_timestamp', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Timestamp', + 'platform': 'smlight', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'core_uptime', + 'unique_id': 'aa:bb:cc:dd:ee:ff_core_uptime', + 'unit_of_measurement': None, + }) +# --- +# name: test_sensors[sensor.mock_title_timestamp-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'timestamp', + 'friendly_name': 'Mock Title Timestamp', + }), + 'context': , + 'entity_id': 'sensor.mock_title_timestamp', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '2024-06-25T02:51:15+00:00', + }) +# --- +# name: test_sensors[sensor.mock_title_timestamp_2-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.mock_title_timestamp_2', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Timestamp', + 'platform': 'smlight', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'socket_uptime', + 'unique_id': 'aa:bb:cc:dd:ee:ff_socket_uptime', + 'unit_of_measurement': None, + }) +# --- +# name: test_sensors[sensor.mock_title_timestamp_2-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'timestamp', + 'friendly_name': 'Mock Title Timestamp', + }), + 'context': , + 'entity_id': 'sensor.mock_title_timestamp_2', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '2024-06-30T23:57:53+00:00', + }) +# --- # name: test_sensors[sensor.mock_title_zigbee_chip_temp-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ @@ -203,6 +344,53 @@ 'state': '32.7', }) # --- +# name: test_sensors[sensor.mock_title_zigbee_uptime-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.mock_title_zigbee_uptime', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Zigbee uptime', + 'platform': 'smlight', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'socket_uptime', + 'unique_id': 'aa:bb:cc:dd:ee:ff_socket_uptime', + 'unit_of_measurement': None, + }) +# --- +# name: test_sensors[sensor.mock_title_zigbee_uptime-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'timestamp', + 'friendly_name': 'Mock Title Zigbee uptime', + }), + 'context': , + 'entity_id': 'sensor.mock_title_zigbee_uptime', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '2024-06-30T23:57:53+00:00', + }) +# --- # name: test_sensors[sensor.slzb_06_core_chip_temp-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ diff --git a/tests/components/smlight/test_button.py b/tests/components/smlight/test_button.py new file mode 100644 index 00000000000000..487351acdea18a --- /dev/null +++ b/tests/components/smlight/test_button.py @@ -0,0 +1,77 @@ +"""Tests for SMLIGHT SLZB-06 button entities.""" + +from unittest.mock import MagicMock + +import pytest + +from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS +from homeassistant.const import ATTR_ENTITY_ID, STATE_UNKNOWN, Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from .conftest import setup_integration + +from tests.common import MockConfigEntry + + +@pytest.fixture +def platforms() -> Platform | list[Platform]: + """Platforms, which should be loaded during the test.""" + return [Platform.BUTTON] + + +@pytest.mark.parametrize( + ("entity_id", "method"), + [ + ("core_restart", "reboot"), + ("zigbee_flash_mode", "zb_bootloader"), + ("zigbee_restart", "zb_restart"), + ], +) +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_buttons( + hass: HomeAssistant, + entity_id: str, + entity_registry: er.EntityRegistry, + method: str, + mock_config_entry: MockConfigEntry, + mock_smlight_client: MagicMock, +) -> None: + """Test creation of button entities.""" + await setup_integration(hass, mock_config_entry) + + state = hass.states.get(f"button.mock_title_{entity_id}") + assert state is not None + assert state.state == STATE_UNKNOWN + + entry = entity_registry.async_get(f"button.mock_title_{entity_id}") + assert entry is not None + assert entry.unique_id == f"aa:bb:cc:dd:ee:ff-{entity_id}" + + mock_method = getattr(mock_smlight_client.cmds, method) + + await hass.services.async_call( + BUTTON_DOMAIN, + SERVICE_PRESS, + {ATTR_ENTITY_ID: f"button.mock_title_{entity_id}"}, + blocking=True, + ) + + assert len(mock_method.mock_calls) == 1 + mock_method.assert_called_with() + + +@pytest.mark.usefixtures("mock_smlight_client") +async def test_disabled_by_default_button( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + mock_config_entry: MockConfigEntry, +) -> None: + """Test the disabled by default flash mode button.""" + await setup_integration(hass, mock_config_entry) + + assert not hass.states.get("button.mock_title_zigbee_flash_mode") + + assert (entry := entity_registry.async_get("button.mock_title_zigbee_flash_mode")) + assert entry.disabled + assert entry.disabled_by is er.RegistryEntryDisabler.INTEGRATION diff --git a/tests/components/smlight/test_config_flow.py b/tests/components/smlight/test_config_flow.py index 9a23a8de753216..fb07e29edd486a 100644 --- a/tests/components/smlight/test_config_flow.py +++ b/tests/components/smlight/test_config_flow.py @@ -363,3 +363,116 @@ async def test_zeroconf_legacy_mac( assert len(mock_setup_entry.mock_calls) == 1 assert len(mock_smlight_client.get_info.mock_calls) == 2 + + +async def test_reauth_flow( + hass: HomeAssistant, + mock_smlight_client: MagicMock, + mock_config_entry: MockConfigEntry, + mock_setup_entry: AsyncMock, +) -> None: + """Test reauth flow completes successfully.""" + mock_smlight_client.check_auth_needed.return_value = True + mock_config_entry.add_to_hass(hass) + + result = await mock_config_entry.start_reauth_flow(hass) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reauth_confirm" + + result2 = await hass.config_entries.flow.async_configure( + result["flow_id"], + { + CONF_USERNAME: MOCK_USERNAME, + CONF_PASSWORD: MOCK_PASSWORD, + }, + ) + + assert result2["type"] is FlowResultType.ABORT + assert result2["reason"] == "reauth_successful" + assert mock_config_entry.data == { + CONF_USERNAME: MOCK_USERNAME, + CONF_PASSWORD: MOCK_PASSWORD, + CONF_HOST: MOCK_HOST, + } + + assert len(mock_smlight_client.authenticate.mock_calls) == 1 + assert len(hass.config_entries.async_entries(DOMAIN)) == 1 + + +async def test_reauth_auth_error( + hass: HomeAssistant, + mock_smlight_client: MagicMock, + mock_config_entry: MockConfigEntry, + mock_setup_entry: AsyncMock, +) -> None: + """Test reauth flow with authentication error.""" + mock_smlight_client.check_auth_needed.return_value = True + mock_smlight_client.authenticate.side_effect = SmlightAuthError + + mock_config_entry.add_to_hass(hass) + result = await mock_config_entry.start_reauth_flow(hass) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reauth_confirm" + + result2 = await hass.config_entries.flow.async_configure( + result["flow_id"], + { + CONF_USERNAME: MOCK_USERNAME, + CONF_PASSWORD: "test-bad", + }, + ) + + assert result2["type"] is FlowResultType.FORM + assert result2["step_id"] == "reauth_confirm" + + mock_smlight_client.authenticate.side_effect = None + result3 = await hass.config_entries.flow.async_configure( + result["flow_id"], + { + CONF_USERNAME: MOCK_USERNAME, + CONF_PASSWORD: MOCK_PASSWORD, + }, + ) + + assert result3["type"] is FlowResultType.ABORT + assert result3["reason"] == "reauth_successful" + + assert mock_config_entry.data == { + CONF_USERNAME: MOCK_USERNAME, + CONF_PASSWORD: MOCK_PASSWORD, + CONF_HOST: MOCK_HOST, + } + + assert len(mock_smlight_client.authenticate.mock_calls) == 2 + assert len(hass.config_entries.async_entries(DOMAIN)) == 1 + + +async def test_reauth_connect_error( + hass: HomeAssistant, + mock_smlight_client: MagicMock, + mock_config_entry: MockConfigEntry, +) -> None: + """Test reauth flow with error.""" + mock_smlight_client.check_auth_needed.return_value = True + mock_smlight_client.authenticate.side_effect = SmlightConnectionError + + mock_config_entry.add_to_hass(hass) + + result = await mock_config_entry.start_reauth_flow(hass) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reauth_confirm" + + result2 = await hass.config_entries.flow.async_configure( + result["flow_id"], + { + CONF_USERNAME: MOCK_USERNAME, + CONF_PASSWORD: MOCK_PASSWORD, + }, + ) + + assert result2["type"] is FlowResultType.ABORT + assert result2["reason"] == "cannot_connect" + assert len(mock_smlight_client.authenticate.mock_calls) == 1 diff --git a/tests/components/smlight/test_init.py b/tests/components/smlight/test_init.py index 682993cb9430e0..1323c93e6bf36e 100644 --- a/tests/components/smlight/test_init.py +++ b/tests/components/smlight/test_init.py @@ -3,7 +3,7 @@ from unittest.mock import MagicMock from freezegun.api import FrozenDateTimeFactory -from pysmlight.exceptions import SmlightAuthError, SmlightConnectionError +from pysmlight.exceptions import SmlightAuthError, SmlightConnectionError, SmlightError import pytest from syrupy.assertion import SnapshotAssertion @@ -55,19 +55,37 @@ async def test_async_setup_auth_failed( assert entry.state is ConfigEntryState.NOT_LOADED +async def test_async_setup_missing_credentials( + hass: HomeAssistant, + mock_config_entry_host: MockConfigEntry, + mock_smlight_client: MagicMock, +) -> None: + """Test we trigger reauth when credentials are missing.""" + mock_smlight_client.check_auth_needed.return_value = True + + await setup_integration(hass, mock_config_entry_host) + + progress = hass.config_entries.flow.async_progress() + assert len(progress) == 1 + assert progress[0]["step_id"] == "reauth_confirm" + assert progress[0]["context"]["unique_id"] == "aa:bb:cc:dd:ee:ff" + + +@pytest.mark.parametrize("error", [SmlightConnectionError, SmlightAuthError]) async def test_update_failed( hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_smlight_client: MagicMock, freezer: FrozenDateTimeFactory, + error: SmlightError, ) -> None: - """Test update failed due to connection error.""" + """Test update failed due to error.""" await setup_integration(hass, mock_config_entry) entity = hass.states.get("sensor.mock_title_core_chip_temp") assert entity.state is not STATE_UNAVAILABLE - mock_smlight_client.get_info.side_effect = SmlightConnectionError + mock_smlight_client.get_info.side_effect = error freezer.tick(SCAN_INTERVAL) async_fire_time_changed(hass) diff --git a/tests/components/smlight/test_sensor.py b/tests/components/smlight/test_sensor.py index 4d16a73a0a77a3..f130d7ccf30ea6 100644 --- a/tests/components/smlight/test_sensor.py +++ b/tests/components/smlight/test_sensor.py @@ -1,9 +1,12 @@ """Tests for the SMLIGHT sensor platform.""" +from unittest.mock import MagicMock + +from pysmlight import Sensors import pytest from syrupy.assertion import SnapshotAssertion -from homeassistant.const import Platform +from homeassistant.const import STATE_UNKNOWN, Platform from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr, entity_registry as er @@ -19,12 +22,13 @@ @pytest.fixture -def platforms() -> Platform | list[Platform]: +def platforms() -> list[Platform]: """Platforms, which should be loaded during the test.""" - return Platform.SENSOR + return [Platform.SENSOR] @pytest.mark.usefixtures("entity_registry_enabled_by_default") +@pytest.mark.freeze_time("2024-07-01 00:00:00+00:00") async def test_sensors( hass: HomeAssistant, device_registry: dr.DeviceRegistry, @@ -46,9 +50,26 @@ async def test_disabled_by_default_sensors( """Test the disabled by default SMLIGHT sensors.""" await setup_integration(hass, mock_config_entry) - for sensor in ("ram_usage", "filesystem_usage"): + for sensor in ("core_uptime", "filesystem_usage", "ram_usage", "zigbee_uptime"): assert not hass.states.get(f"sensor.mock_title_{sensor}") assert (entry := entity_registry.async_get(f"sensor.mock_title_{sensor}")) assert entry.disabled assert entry.disabled_by is er.RegistryEntryDisabler.INTEGRATION + + +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_zigbee_uptime_disconnected( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_smlight_client: MagicMock, +) -> None: + """Test for uptime when zigbee socket is disconnected. + + In this case zigbee uptime state should be unknown. + """ + mock_smlight_client.get_sensors.return_value = Sensors(socket_uptime=0) + await setup_integration(hass, mock_config_entry) + + state = hass.states.get("sensor.mock_title_zigbee_uptime") + assert state.state == STATE_UNKNOWN diff --git a/tests/components/solarlog/__init__.py b/tests/components/solarlog/__init__.py index 74b19bd297ea57..c2c0296d9e2c08 100644 --- a/tests/components/solarlog/__init__.py +++ b/tests/components/solarlog/__init__.py @@ -17,3 +17,5 @@ async def setup_platform( with patch("homeassistant.components.solarlog.PLATFORMS", platforms): await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() + + return config_entry diff --git a/tests/components/solarlog/conftest.py b/tests/components/solarlog/conftest.py index c34d0c011a3a32..b363f655c57800 100644 --- a/tests/components/solarlog/conftest.py +++ b/tests/components/solarlog/conftest.py @@ -4,6 +4,7 @@ from unittest.mock import AsyncMock, patch import pytest +from solarlog_cli.solarlog_models import InverterData, SolarlogData from homeassistant.components.solarlog.const import DOMAIN as SOLARLOG_DOMAIN from homeassistant.const import CONF_HOST, CONF_NAME @@ -12,6 +13,19 @@ from tests.common import MockConfigEntry, load_json_object_fixture +DEVICE_LIST = { + 0: InverterData(name="Inverter 1", enabled=True), + 1: InverterData(name="Inverter 2", enabled=True), +} +INVERTER_DATA = { + 0: InverterData( + name="Inverter 1", enabled=True, consumption_year=354687, current_power=5 + ), + 1: InverterData( + name="Inverter 2", enabled=True, consumption_year=354, current_power=6 + ), +} + @pytest.fixture def mock_config_entry() -> MockConfigEntry: @@ -33,11 +47,19 @@ def mock_config_entry() -> MockConfigEntry: def mock_solarlog_connector(): """Build a fixture for the SolarLog API that connects successfully and returns one device.""" - mock_solarlog_api = AsyncMock() - mock_solarlog_api.test_connection = AsyncMock(return_value=True) - mock_solarlog_api.update_data.return_value = load_json_object_fixture( - "solarlog_data.json", SOLARLOG_DOMAIN + data = SolarlogData.from_dict( + load_json_object_fixture("solarlog_data.json", SOLARLOG_DOMAIN) ) + data.inverter_data = INVERTER_DATA + + mock_solarlog_api = AsyncMock() + mock_solarlog_api.test_connection.return_value = True + mock_solarlog_api.update_data.return_value = data + mock_solarlog_api.update_device_list.return_value = INVERTER_DATA + mock_solarlog_api.update_inverter_data.return_value = INVERTER_DATA + mock_solarlog_api.device_name = {0: "Inverter 1", 1: "Inverter 2"}.get + mock_solarlog_api.device_enabled = {0: True, 1: False}.get + with ( patch( "homeassistant.components.solarlog.coordinator.SolarLogConnector", diff --git a/tests/components/solarlog/fixtures/solarlog_data.json b/tests/components/solarlog/fixtures/solarlog_data.json index 4976f4fa8b7da6..339ab4a4dfcd0b 100644 --- a/tests/components/solarlog/fixtures/solarlog_data.json +++ b/tests/components/solarlog/fixtures/solarlog_data.json @@ -17,8 +17,9 @@ "total_power": 120, "self_consumption_year": 545, "alternator_loss": 2, - "efficiency": 0.9804, - "usage": 0.5487, + "efficiency": 98.1, + "usage": 54.8, "power_available": 45.13, - "capacity": 0.85 + "capacity": 85.5, + "last_updated": "2024-08-01T15:20:45Z" } diff --git a/tests/components/solarlog/snapshots/test_diagnostics.ambr b/tests/components/solarlog/snapshots/test_diagnostics.ambr new file mode 100644 index 00000000000000..09ff3a333ee06b --- /dev/null +++ b/tests/components/solarlog/snapshots/test_diagnostics.ambr @@ -0,0 +1,64 @@ +# serializer version: 1 +# name: test_entry_diagnostics + dict({ + 'config_entry': dict({ + 'data': dict({ + 'extended_data': True, + 'host': '**REDACTED**', + 'name': 'Solarlog test 1 2 3', + }), + 'disabled_by': None, + 'domain': 'solarlog', + 'entry_id': 'ce5f5431554d101905d31797e1232da8', + 'minor_version': 2, + 'options': dict({ + }), + 'pref_disable_new_entities': False, + 'pref_disable_polling': False, + 'source': 'user', + 'title': 'solarlog', + 'unique_id': None, + 'version': 1, + }), + 'solarlog_data': dict({ + 'alternator_loss': 2.0, + 'capacity': 85.5, + 'consumption_ac': 54.87, + 'consumption_day': 5.31, + 'consumption_month': 758.0, + 'consumption_total': 354687.0, + 'consumption_year': 4587.0, + 'consumption_yesterday': 7.34, + 'efficiency': 98.1, + 'inverter_data': dict({ + '0': dict({ + 'consumption_year': 354687, + 'current_power': 5, + 'enabled': True, + 'name': 'Inverter 1', + }), + '1': dict({ + 'consumption_year': 354, + 'current_power': 6, + 'enabled': True, + 'name': 'Inverter 2', + }), + }), + 'last_updated': '2024-08-01T15:20:45+00:00', + 'power_ac': 100.0, + 'power_available': 45.13, + 'power_dc': 102.0, + 'production_year': None, + 'self_consumption_year': 545.0, + 'total_power': 120.0, + 'usage': 54.8, + 'voltage_ac': 100.0, + 'voltage_dc': 100.0, + 'yield_day': 4.21, + 'yield_month': 515.0, + 'yield_total': 56513.0, + 'yield_year': 1023.0, + 'yield_yesterday': 5.21, + }), + }) +# --- diff --git a/tests/components/solarlog/snapshots/test_sensor.ambr b/tests/components/solarlog/snapshots/test_sensor.ambr index df154a5eb9bee5..9f95e04a38fa09 100644 --- a/tests/components/solarlog/snapshots/test_sensor.ambr +++ b/tests/components/solarlog/snapshots/test_sensor.ambr @@ -1,4 +1,265 @@ # serializer version: 1 +# name: test_all_entities[sensor.inverter_1_consumption_total-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.inverter_1_consumption_total', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Consumption total', + 'platform': 'solarlog', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'consumption_total', + 'unique_id': 'ce5f5431554d101905d31797e1232da8-inverter_1-consumption_total', + 'unit_of_measurement': , + }) +# --- +# name: test_all_entities[sensor.inverter_1_consumption_total-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'energy', + 'friendly_name': 'Inverter 1 Consumption total', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.inverter_1_consumption_total', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '354.687', + }) +# --- +# name: test_all_entities[sensor.inverter_1_consumption_year-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.inverter_1_consumption_year', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 3, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Consumption year', + 'platform': 'solarlog', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'consumption_year', + 'unique_id': 'ce5f5431554d101905d31797e1232da8-inverter_1-consumption_year', + 'unit_of_measurement': , + }) +# --- +# name: test_all_entities[sensor.inverter_1_consumption_year-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'energy', + 'friendly_name': 'Inverter 1 Consumption year', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.inverter_1_consumption_year', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '354.687', + }) +# --- +# name: test_all_entities[sensor.inverter_1_power-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.inverter_1_power', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Power', + 'platform': 'solarlog', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'current_power', + 'unique_id': 'ce5f5431554d101905d31797e1232da8-inverter_1-current_power', + 'unit_of_measurement': , + }) +# --- +# name: test_all_entities[sensor.inverter_1_power-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'power', + 'friendly_name': 'Inverter 1 Power', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.inverter_1_power', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '5', + }) +# --- +# name: test_all_entities[sensor.inverter_2_consumption_year-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.inverter_2_consumption_year', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 3, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Consumption year', + 'platform': 'solarlog', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'consumption_year', + 'unique_id': 'ce5f5431554d101905d31797e1232da8-inverter_2-consumption_year', + 'unit_of_measurement': , + }) +# --- +# name: test_all_entities[sensor.inverter_2_consumption_year-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'energy', + 'friendly_name': 'Inverter 2 Consumption year', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.inverter_2_consumption_year', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '0.354', + }) +# --- +# name: test_all_entities[sensor.inverter_2_power-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.inverter_2_power', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Power', + 'platform': 'solarlog', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'current_power', + 'unique_id': 'ce5f5431554d101905d31797e1232da8-inverter_2-current_power', + 'unit_of_measurement': , + }) +# --- +# name: test_all_entities[sensor.inverter_2_power-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'power', + 'friendly_name': 'Inverter 2 Power', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.inverter_2_power', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '6', + }) +# --- # name: test_all_entities[sensor.solarlog_alternator_loss-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ @@ -30,7 +291,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'alternator_loss', - 'unique_id': 'ce5f5431554d101905d31797e1232da8_alternator_loss', + 'unique_id': 'ce5f5431554d101905d31797e1232da8-alternator_loss', 'unit_of_measurement': , }) # --- @@ -47,7 +308,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': '2', + 'state': '2.0', }) # --- # name: test_all_entities[sensor.solarlog_capacity-entry] @@ -73,6 +334,9 @@ }), 'name': None, 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 1, + }), }), 'original_device_class': , 'original_icon': None, @@ -81,7 +345,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'capacity', - 'unique_id': 'ce5f5431554d101905d31797e1232da8_capacity', + 'unique_id': 'ce5f5431554d101905d31797e1232da8-capacity', 'unit_of_measurement': '%', }) # --- @@ -98,7 +362,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': '85.0', + 'state': '85.5', }) # --- # name: test_all_entities[sensor.solarlog_consumption_ac-entry] @@ -132,7 +396,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'consumption_ac', - 'unique_id': 'ce5f5431554d101905d31797e1232da8_consumption_ac', + 'unique_id': 'ce5f5431554d101905d31797e1232da8-consumption_ac', 'unit_of_measurement': , }) # --- @@ -173,6 +437,12 @@ }), 'name': None, 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 3, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), }), 'original_device_class': , 'original_icon': None, @@ -181,7 +451,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'consumption_day', - 'unique_id': 'ce5f5431554d101905d31797e1232da8_consumption_day', + 'unique_id': 'ce5f5431554d101905d31797e1232da8-consumption_day', 'unit_of_measurement': , }) # --- @@ -197,7 +467,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': '0.005', + 'state': '0.00531', }) # --- # name: test_all_entities[sensor.solarlog_consumption_month-entry] @@ -221,6 +491,12 @@ }), 'name': None, 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 3, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), }), 'original_device_class': , 'original_icon': None, @@ -229,7 +505,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'consumption_month', - 'unique_id': 'ce5f5431554d101905d31797e1232da8_consumption_month', + 'unique_id': 'ce5f5431554d101905d31797e1232da8-consumption_month', 'unit_of_measurement': , }) # --- @@ -271,6 +547,12 @@ }), 'name': None, 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 3, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), }), 'original_device_class': , 'original_icon': None, @@ -279,7 +561,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'consumption_total', - 'unique_id': 'ce5f5431554d101905d31797e1232da8_consumption_total', + 'unique_id': 'ce5f5431554d101905d31797e1232da8-consumption_total', 'unit_of_measurement': , }) # --- @@ -320,6 +602,12 @@ }), 'name': None, 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 3, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), }), 'original_device_class': , 'original_icon': None, @@ -328,7 +616,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'consumption_year', - 'unique_id': 'ce5f5431554d101905d31797e1232da8_consumption_year', + 'unique_id': 'ce5f5431554d101905d31797e1232da8-consumption_year', 'unit_of_measurement': , }) # --- @@ -368,6 +656,12 @@ }), 'name': None, 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 3, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), }), 'original_device_class': , 'original_icon': None, @@ -376,7 +670,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'consumption_yesterday', - 'unique_id': 'ce5f5431554d101905d31797e1232da8_consumption_yesterday', + 'unique_id': 'ce5f5431554d101905d31797e1232da8-consumption_yesterday', 'unit_of_measurement': , }) # --- @@ -392,7 +686,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': '0.007', + 'state': '0.00734', }) # --- # name: test_all_entities[sensor.solarlog_efficiency-entry] @@ -418,6 +712,9 @@ }), 'name': None, 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 1, + }), }), 'original_device_class': , 'original_icon': None, @@ -426,7 +723,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'efficiency', - 'unique_id': 'ce5f5431554d101905d31797e1232da8_efficiency', + 'unique_id': 'ce5f5431554d101905d31797e1232da8-efficiency', 'unit_of_measurement': '%', }) # --- @@ -443,7 +740,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': '98.0', + 'state': '98.1', }) # --- # name: test_all_entities[sensor.solarlog_installed_peak_power-entry] @@ -475,7 +772,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'total_power', - 'unique_id': 'ce5f5431554d101905d31797e1232da8_total_power', + 'unique_id': 'ce5f5431554d101905d31797e1232da8-total_power', 'unit_of_measurement': , }) # --- @@ -491,7 +788,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': '120', + 'state': '120.0', }) # --- # name: test_all_entities[sensor.solarlog_last_update-entry] @@ -523,7 +820,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'last_update', - 'unique_id': 'ce5f5431554d101905d31797e1232da8_last_updated', + 'unique_id': 'ce5f5431554d101905d31797e1232da8-last_updated', 'unit_of_measurement': None, }) # --- @@ -538,7 +835,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': 'unknown', + 'state': '2024-08-01T15:20:45+00:00', }) # --- # name: test_all_entities[sensor.solarlog_power_ac-entry] @@ -572,7 +869,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'power_ac', - 'unique_id': 'ce5f5431554d101905d31797e1232da8_power_ac', + 'unique_id': 'ce5f5431554d101905d31797e1232da8-power_ac', 'unit_of_measurement': , }) # --- @@ -589,7 +886,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': '100', + 'state': '100.0', }) # --- # name: test_all_entities[sensor.solarlog_power_available-entry] @@ -623,7 +920,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'power_available', - 'unique_id': 'ce5f5431554d101905d31797e1232da8_power_available', + 'unique_id': 'ce5f5431554d101905d31797e1232da8-power_available', 'unit_of_measurement': , }) # --- @@ -674,7 +971,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'power_dc', - 'unique_id': 'ce5f5431554d101905d31797e1232da8_power_dc', + 'unique_id': 'ce5f5431554d101905d31797e1232da8-power_dc', 'unit_of_measurement': , }) # --- @@ -691,7 +988,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': '102', + 'state': '102.0', }) # --- # name: test_all_entities[sensor.solarlog_self_consumption_year-entry] @@ -725,7 +1022,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'self_consumption_year', - 'unique_id': 'ce5f5431554d101905d31797e1232da8_self_consumption_year', + 'unique_id': 'ce5f5431554d101905d31797e1232da8-self_consumption_year', 'unit_of_measurement': , }) # --- @@ -742,7 +1039,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': '545', + 'state': '545.0', }) # --- # name: test_all_entities[sensor.solarlog_usage-entry] @@ -768,6 +1065,9 @@ }), 'name': None, 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 1, + }), }), 'original_device_class': , 'original_icon': None, @@ -776,7 +1076,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'usage', - 'unique_id': 'ce5f5431554d101905d31797e1232da8_usage', + 'unique_id': 'ce5f5431554d101905d31797e1232da8-usage', 'unit_of_measurement': '%', }) # --- @@ -793,7 +1093,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': '54.9', + 'state': '54.8', }) # --- # name: test_all_entities[sensor.solarlog_voltage_ac-entry] @@ -827,7 +1127,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'voltage_ac', - 'unique_id': 'ce5f5431554d101905d31797e1232da8_voltage_ac', + 'unique_id': 'ce5f5431554d101905d31797e1232da8-voltage_ac', 'unit_of_measurement': , }) # --- @@ -844,7 +1144,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': '100', + 'state': '100.0', }) # --- # name: test_all_entities[sensor.solarlog_voltage_dc-entry] @@ -878,7 +1178,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'voltage_dc', - 'unique_id': 'ce5f5431554d101905d31797e1232da8_voltage_dc', + 'unique_id': 'ce5f5431554d101905d31797e1232da8-voltage_dc', 'unit_of_measurement': , }) # --- @@ -895,7 +1195,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': '100', + 'state': '100.0', }) # --- # name: test_all_entities[sensor.solarlog_yield_day-entry] @@ -919,6 +1219,12 @@ }), 'name': None, 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 3, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), }), 'original_device_class': , 'original_icon': None, @@ -927,7 +1233,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'yield_day', - 'unique_id': 'ce5f5431554d101905d31797e1232da8_yield_day', + 'unique_id': 'ce5f5431554d101905d31797e1232da8-yield_day', 'unit_of_measurement': , }) # --- @@ -943,7 +1249,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': '0.004', + 'state': '0.00421', }) # --- # name: test_all_entities[sensor.solarlog_yield_month-entry] @@ -967,6 +1273,12 @@ }), 'name': None, 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 3, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), }), 'original_device_class': , 'original_icon': None, @@ -975,7 +1287,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'yield_month', - 'unique_id': 'ce5f5431554d101905d31797e1232da8_yield_month', + 'unique_id': 'ce5f5431554d101905d31797e1232da8-yield_month', 'unit_of_measurement': , }) # --- @@ -1017,6 +1329,12 @@ }), 'name': None, 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 3, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), }), 'original_device_class': , 'original_icon': None, @@ -1025,7 +1343,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'yield_total', - 'unique_id': 'ce5f5431554d101905d31797e1232da8_yield_total', + 'unique_id': 'ce5f5431554d101905d31797e1232da8-yield_total', 'unit_of_measurement': , }) # --- @@ -1066,6 +1384,9 @@ }), 'name': None, 'options': dict({ + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), }), 'original_device_class': , 'original_icon': None, @@ -1074,7 +1395,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'yield_year', - 'unique_id': 'ce5f5431554d101905d31797e1232da8_yield_year', + 'unique_id': 'ce5f5431554d101905d31797e1232da8-yield_year', 'unit_of_measurement': , }) # --- @@ -1090,7 +1411,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': '1.023', + 'state': '1.0230', }) # --- # name: test_all_entities[sensor.solarlog_yield_yesterday-entry] @@ -1114,6 +1435,12 @@ }), 'name': None, 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 3, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), }), 'original_device_class': , 'original_icon': None, @@ -1122,7 +1449,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'yield_yesterday', - 'unique_id': 'ce5f5431554d101905d31797e1232da8_yield_yesterday', + 'unique_id': 'ce5f5431554d101905d31797e1232da8-yield_yesterday', 'unit_of_measurement': , }) # --- @@ -1138,6 +1465,6 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': '0.005', + 'state': '0.00521', }) # --- diff --git a/tests/components/solarlog/test_config_flow.py b/tests/components/solarlog/test_config_flow.py index f71282a7c9b300..b7ae6119893cad 100644 --- a/tests/components/solarlog/test_config_flow.py +++ b/tests/components/solarlog/test_config_flow.py @@ -5,9 +5,9 @@ import pytest from solarlog_cli.solarlog_exceptions import SolarLogConnectionError, SolarLogError -from homeassistant import config_entries from homeassistant.components.solarlog import config_flow -from homeassistant.components.solarlog.const import DEFAULT_HOST, DOMAIN +from homeassistant.components.solarlog.const import DOMAIN +from homeassistant.config_entries import SOURCE_RECONFIGURE, SOURCE_USER from homeassistant.const import CONF_HOST, CONF_NAME from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -21,7 +21,7 @@ async def test_form(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None: """Test we get the form.""" result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_USER} + DOMAIN, context={"source": SOURCE_USER} ) assert result["type"] is FlowResultType.FORM assert result["errors"] == {} @@ -60,14 +60,14 @@ async def test_user( ) -> None: """Test user config.""" result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_USER} + DOMAIN, context={"source": SOURCE_USER} ) assert result["type"] is FlowResultType.FORM assert result["errors"] == {} # tests with all provided result = await hass.config_entries.flow.async_configure( - result["flow_id"], {CONF_HOST: HOST, CONF_NAME: NAME, "extended_data": False} + result["flow_id"], {CONF_HOST: HOST, CONF_NAME: NAME, "extended_data": True} ) await hass.async_block_till_done() @@ -123,71 +123,27 @@ async def test_form_exceptions( assert result["data"]["extended_data"] is False -async def test_import(hass: HomeAssistant, test_connect) -> None: - """Test import step.""" - flow = init_config_flow(hass) - - # import with only host - result = await flow.async_step_import({CONF_HOST: HOST}) - await hass.async_block_till_done() - - assert result["type"] is FlowResultType.CREATE_ENTRY - assert result["title"] == "solarlog" - assert result["data"][CONF_HOST] == HOST - - # import with only name - result = await flow.async_step_import({CONF_NAME: NAME}) - await hass.async_block_till_done() - - assert result["type"] is FlowResultType.CREATE_ENTRY - assert result["title"] == "solarlog_test_1_2_3" - assert result["data"][CONF_HOST] == DEFAULT_HOST - - # import with host and name - result = await flow.async_step_import({CONF_HOST: HOST, CONF_NAME: NAME}) - await hass.async_block_till_done() - - assert result["type"] is FlowResultType.CREATE_ENTRY - assert result["title"] == "solarlog_test_1_2_3" - assert result["data"][CONF_HOST] == HOST - - -async def test_abort_if_already_setup(hass: HomeAssistant, test_connect) -> None: +async def test_abort_if_already_setup(hass: HomeAssistant, test_connect: None) -> None: """Test we abort if the device is already setup.""" - flow = init_config_flow(hass) - MockConfigEntry( - domain="solarlog", data={CONF_NAME: NAME, CONF_HOST: HOST} - ).add_to_hass(hass) - # Should fail, same HOST different NAME (default) - result = await flow.async_step_import( - {CONF_HOST: HOST, CONF_NAME: "solarlog_test_7_8_9", "extended_data": False} + MockConfigEntry(domain=DOMAIN, data={CONF_NAME: NAME, CONF_HOST: HOST}).add_to_hass( + hass ) - assert result["type"] is FlowResultType.ABORT - assert result["reason"] == "already_configured" - - # Should fail, same HOST and NAME - result = await flow.async_step_user({CONF_HOST: HOST, CONF_NAME: NAME}) - assert result["type"] is FlowResultType.FORM - assert result["errors"] == {CONF_HOST: "already_configured"} - # SHOULD pass, diff HOST (without http://), different NAME - result = await flow.async_step_import( - {CONF_HOST: "2.2.2.2", CONF_NAME: "solarlog_test_7_8_9", "extended_data": False} + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} ) - assert result["type"] is FlowResultType.CREATE_ENTRY - assert result["title"] == "solarlog_test_7_8_9" - assert result["data"][CONF_HOST] == "http://2.2.2.2" - # SHOULD pass, diff HOST, same NAME - result = await flow.async_step_import( - {CONF_HOST: "http://2.2.2.2", CONF_NAME: NAME, "extended_data": False} - ) - await hass.async_block_till_done() + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + assert result["errors"] == {} - assert result["type"] is FlowResultType.CREATE_ENTRY - assert result["title"] == "solarlog_test_1_2_3" - assert result["data"][CONF_HOST] == "http://2.2.2.2" + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {CONF_HOST: HOST, CONF_NAME: "solarlog_test_7_8_9", "extended_data": False}, + ) + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "already_configured" async def test_reconfigure_flow( @@ -207,7 +163,7 @@ async def test_reconfigure_flow( result = await hass.config_entries.flow.async_init( DOMAIN, context={ - "source": config_entries.SOURCE_RECONFIGURE, + "source": SOURCE_RECONFIGURE, "entry_id": entry.entry_id, }, ) diff --git a/tests/components/solarlog/test_diagnostics.py b/tests/components/solarlog/test_diagnostics.py new file mode 100644 index 00000000000000..bc0b020462dab5 --- /dev/null +++ b/tests/components/solarlog/test_diagnostics.py @@ -0,0 +1,32 @@ +"""Test Solarlog diagnostics.""" + +from unittest.mock import AsyncMock + +from syrupy import SnapshotAssertion +from syrupy.filters import props + +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant + +from . import setup_platform + +from tests.common import MockConfigEntry +from tests.components.diagnostics import get_diagnostics_for_config_entry +from tests.typing import ClientSessionGenerator + + +async def test_entry_diagnostics( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + mock_config_entry: MockConfigEntry, + mock_solarlog_connector: AsyncMock, + snapshot: SnapshotAssertion, +) -> None: + """Test config entry diagnostics.""" + await setup_platform(hass, mock_config_entry, [Platform.SENSOR]) + + result = await get_diagnostics_for_config_entry( + hass, hass_client, mock_config_entry + ) + + assert result == snapshot(exclude=props("created_at", "modified_at")) diff --git a/tests/components/soma/test_config_flow.py b/tests/components/soma/test_config_flow.py index 8b8548bfe3eba7..67109e37c6db65 100644 --- a/tests/components/soma/test_config_flow.py +++ b/tests/components/soma/test_config_flow.py @@ -5,7 +5,8 @@ from api.soma_api import SomaApi from requests import RequestException -from homeassistant.components.soma import DOMAIN, config_flow +from homeassistant.components.soma import DOMAIN +from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -17,57 +18,66 @@ async def test_form(hass: HomeAssistant) -> None: """Test user form showing.""" - flow = config_flow.SomaFlowHandler() - flow.hass = hass - result = await flow.async_step_user() + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) assert result["type"] is FlowResultType.FORM async def test_import_abort(hass: HomeAssistant) -> None: """Test configuration from YAML aborting with existing entity.""" - flow = config_flow.SomaFlowHandler() - flow.hass = hass MockConfigEntry(domain=DOMAIN).add_to_hass(hass) - result = await flow.async_step_import() + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_IMPORT} + ) assert result["type"] is FlowResultType.ABORT assert result["reason"] == "already_setup" async def test_import_create(hass: HomeAssistant) -> None: """Test configuration from YAML.""" - flow = config_flow.SomaFlowHandler() - flow.hass = hass with patch.object(SomaApi, "list_devices", return_value={"result": "success"}): - result = await flow.async_step_import({"host": MOCK_HOST, "port": MOCK_PORT}) + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_IMPORT}, + data={"host": MOCK_HOST, "port": MOCK_PORT}, + ) assert result["type"] is FlowResultType.CREATE_ENTRY async def test_error_status(hass: HomeAssistant) -> None: """Test Connect successfully returning error status.""" - flow = config_flow.SomaFlowHandler() - flow.hass = hass with patch.object(SomaApi, "list_devices", return_value={"result": "error"}): - result = await flow.async_step_import({"host": MOCK_HOST, "port": MOCK_PORT}) + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_IMPORT}, + data={"host": MOCK_HOST, "port": MOCK_PORT}, + ) assert result["type"] is FlowResultType.ABORT assert result["reason"] == "result_error" async def test_key_error(hass: HomeAssistant) -> None: """Test Connect returning empty string.""" - flow = config_flow.SomaFlowHandler() - flow.hass = hass + with patch.object(SomaApi, "list_devices", return_value={}): - result = await flow.async_step_import({"host": MOCK_HOST, "port": MOCK_PORT}) + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_IMPORT}, + data={"host": MOCK_HOST, "port": MOCK_PORT}, + ) assert result["type"] is FlowResultType.ABORT assert result["reason"] == "connection_error" async def test_exception(hass: HomeAssistant) -> None: """Test if RequestException fires when no connection can be made.""" - flow = config_flow.SomaFlowHandler() - flow.hass = hass with patch.object(SomaApi, "list_devices", side_effect=RequestException()): - result = await flow.async_step_import({"host": MOCK_HOST, "port": MOCK_PORT}) + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_IMPORT}, + data={"host": MOCK_HOST, "port": MOCK_PORT}, + ) assert result["type"] is FlowResultType.ABORT assert result["reason"] == "connection_error" @@ -75,8 +85,10 @@ async def test_exception(hass: HomeAssistant) -> None: async def test_full_flow(hass: HomeAssistant) -> None: """Check classic use case.""" hass.data[DOMAIN] = {} - flow = config_flow.SomaFlowHandler() - flow.hass = hass with patch.object(SomaApi, "list_devices", return_value={"result": "success"}): - result = await flow.async_step_user({"host": MOCK_HOST, "port": MOCK_PORT}) + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_USER}, + data={"host": MOCK_HOST, "port": MOCK_PORT}, + ) assert result["type"] is FlowResultType.CREATE_ENTRY diff --git a/tests/components/sonarr/test_config_flow.py b/tests/components/sonarr/test_config_flow.py index 6bd14e8b581df8..118d5020cba3ee 100644 --- a/tests/components/sonarr/test_config_flow.py +++ b/tests/components/sonarr/test_config_flow.py @@ -11,7 +11,7 @@ DEFAULT_WANTED_MAX_ITEMS, DOMAIN, ) -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_USER from homeassistant.const import CONF_API_KEY, CONF_SOURCE, CONF_URL, CONF_VERIFY_SSL from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -96,15 +96,7 @@ async def test_full_reauth_flow_implementation( """Test the manual reauth flow from start to finish.""" entry = init_integration - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - CONF_SOURCE: SOURCE_REAUTH, - "entry_id": entry.entry_id, - "unique_id": entry.unique_id, - }, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/sonos/conftest.py b/tests/components/sonos/conftest.py index 996fd4c6663f10..04b35e2c021497 100644 --- a/tests/components/sonos/conftest.py +++ b/tests/components/sonos/conftest.py @@ -10,7 +10,12 @@ import pytest from soco import SoCo from soco.alarms import Alarms -from soco.data_structures import DidlFavorite, DidlPlaylistContainer, SearchResult +from soco.data_structures import ( + DidlFavorite, + DidlMusicTrack, + DidlPlaylistContainer, + SearchResult, +) from soco.events_base import Event as SonosEvent from homeassistant.components import ssdp, zeroconf @@ -185,6 +190,7 @@ def __init__( battery_info, alarm_clock, sonos_playlists: SearchResult, + sonos_queue: list[DidlMusicTrack], ) -> None: """Initialize the mock factory.""" self.mock_list: dict[str, MockSoCo] = {} @@ -194,6 +200,7 @@ def __init__( self.battery_info = battery_info self.alarm_clock = alarm_clock self.sonos_playlists = sonos_playlists + self.sonos_queue = sonos_queue def cache_mock( self, mock_soco: MockSoCo, ip_address: str, name: str = "Zone A" @@ -207,6 +214,7 @@ def cache_mock( mock_soco.get_current_track_info.return_value = self.current_track_info mock_soco.music_source_from_uri = SoCo.music_source_from_uri mock_soco.get_sonos_playlists.return_value = self.sonos_playlists + mock_soco.get_queue.return_value = self.sonos_queue my_speaker_info = self.speaker_info.copy() my_speaker_info["zone_name"] = name my_speaker_info["uid"] = mock_soco.uid @@ -255,6 +263,19 @@ def soco_sharelink(): yield mock_instance +@pytest.fixture(name="sonos_websocket") +def sonos_websocket(): + """Fixture to mock SonosWebSocket.""" + with patch( + "homeassistant.components.sonos.speaker.SonosWebsocket" + ) as mock_sonos_ws: + mock_instance = AsyncMock() + mock_instance.play_clip = AsyncMock() + mock_instance.play_clip.return_value = [{"success": 1}, {}] + mock_sonos_ws.return_value = mock_instance + yield mock_instance + + @pytest.fixture(name="soco_factory") def soco_factory( music_library, @@ -263,6 +284,8 @@ def soco_factory( battery_info, alarm_clock, sonos_playlists: SearchResult, + sonos_websocket, + sonos_queue: list[DidlMusicTrack], ): """Create factory for instantiating SoCo mocks.""" factory = SoCoMockFactory( @@ -272,6 +295,7 @@ def soco_factory( battery_info, alarm_clock, sonos_playlists, + sonos_queue=sonos_queue, ) with ( patch("homeassistant.components.sonos.SoCo", new=factory.get_mock), @@ -356,6 +380,13 @@ def sonos_playlists_fixture() -> SearchResult: return SearchResult(playlists_list, "sonos_playlists", 1, 1, 0) +@pytest.fixture(name="sonos_queue") +def sonos_queue() -> list[DidlMusicTrack]: + """Create sonos queue fixture.""" + queue = load_json_value_fixture("sonos_queue.json", "sonos") + return [DidlMusicTrack.from_dict(track) for track in queue] + + class MockMusicServiceItem: """Mocks a Soco MusicServiceItem.""" diff --git a/tests/components/sonos/fixtures/sonos_queue.json b/tests/components/sonos/fixtures/sonos_queue.json new file mode 100644 index 00000000000000..50689a00e1d36c --- /dev/null +++ b/tests/components/sonos/fixtures/sonos_queue.json @@ -0,0 +1,30 @@ +[ + { + "title": "Something", + "album": "Abbey Road", + "creator": "The Beatles", + "item_id": "Q:0/1", + "parent_id": "Q:0", + "original_track_number": 3, + "resources": [ + { + "uri": "x-file-cifs://192.168.42.10/music/The%20Beatles/Abbey%20Road/03%20Something.mp3", + "protocol_info": "file:*:audio/mpegurl:*" + } + ] + }, + { + "title": "Come Together", + "album": "Abbey Road", + "creator": "The Beatles", + "item_id": "Q:0/2", + "parent_id": "Q:0", + "original_track_number": 1, + "resources": [ + { + "uri": "x-file-cifs://192.168.42.10/music/The%20Beatles/Abbey%20Road/01%20Come%20Together.mp3", + "protocol_info": "file:*:audio/mpegurl:*" + } + ] + } +] diff --git a/tests/components/sonos/snapshots/test_media_player.ambr b/tests/components/sonos/snapshots/test_media_player.ambr index 9c43bceb43bd8f..f382d341de6674 100644 --- a/tests/components/sonos/snapshots/test_media_player.ambr +++ b/tests/components/sonos/snapshots/test_media_player.ambr @@ -56,3 +56,21 @@ 'state': 'idle', }) # --- +# name: test_media_get_queue + dict({ + 'media_player.zone_a': list([ + dict({ + 'media_album_name': 'Abbey Road', + 'media_artist': 'The Beatles', + 'media_content_id': 'x-file-cifs://192.168.42.10/music/The%20Beatles/Abbey%20Road/03%20Something.mp3', + 'media_title': 'Something', + }), + dict({ + 'media_album_name': 'Abbey Road', + 'media_artist': 'The Beatles', + 'media_content_id': 'x-file-cifs://192.168.42.10/music/The%20Beatles/Abbey%20Road/01%20Come%20Together.mp3', + 'media_title': 'Come Together', + }), + ]), + }) +# --- diff --git a/tests/components/sonos/test_media_player.py b/tests/components/sonos/test_media_player.py index b29ae1b6cfb423..ae3928c5ff6df0 100644 --- a/tests/components/sonos/test_media_player.py +++ b/tests/components/sonos/test_media_player.py @@ -5,13 +5,16 @@ import pytest from soco.data_structures import SearchResult +from sonos_websocket.exception import SonosWebsocketError from syrupy import SnapshotAssertion from homeassistant.components.media_player import ( ATTR_INPUT_SOURCE, + ATTR_MEDIA_ANNOUNCE, ATTR_MEDIA_CONTENT_ID, ATTR_MEDIA_CONTENT_TYPE, ATTR_MEDIA_ENQUEUE, + ATTR_MEDIA_EXTRA, ATTR_MEDIA_REPEAT, ATTR_MEDIA_SHUFFLE, ATTR_MEDIA_VOLUME_LEVEL, @@ -29,6 +32,7 @@ ) from homeassistant.components.sonos.media_player import ( LONG_SERVICE_TIMEOUT, + SERVICE_GET_QUEUE, SERVICE_RESTORE, SERVICE_SNAPSHOT, VOLUME_INCREMENT, @@ -47,7 +51,7 @@ SERVICE_VOLUME_UP, ) from homeassistant.core import HomeAssistant -from homeassistant.exceptions import ServiceValidationError +from homeassistant.exceptions import HomeAssistantError, ServiceValidationError from homeassistant.helpers import entity_registry as er from homeassistant.helpers.device_registry import ( CONNECTION_NETWORK_MAC, @@ -228,6 +232,45 @@ async def test_play_media_library( ) +@pytest.mark.parametrize( + ("media_content_type", "media_content_id", "message"), + [ + ( + "artist", + "A:ALBUM/UnknowAlbum", + "Could not find media in library: A:ALBUM/UnknowAlbum", + ), + ( + "UnknownContent", + "A:ALBUM/UnknowAlbum", + "Sonos does not support media content type: UnknownContent", + ), + ], +) +async def test_play_media_library_content_error( + hass: HomeAssistant, + async_autosetup_sonos, + media_content_type, + media_content_id, + message, +) -> None: + """Test playing local library errors on content and content type.""" + with pytest.raises( + ServiceValidationError, + match=message, + ): + await hass.services.async_call( + MP_DOMAIN, + SERVICE_PLAY_MEDIA, + { + ATTR_ENTITY_ID: "media_player.zone_a", + ATTR_MEDIA_CONTENT_TYPE: media_content_type, + ATTR_MEDIA_CONTENT_ID: media_content_id, + }, + blocking=True, + ) + + _track_url = "S://192.168.42.100/music/iTunes/The%20Beatles/A%20Hard%20Day%2fs%I%20Should%20Have%20Known%20Better.mp3" @@ -1050,3 +1093,93 @@ async def test_media_transport( blocking=True, ) assert getattr(soco, client_call).call_count == 1 + + +async def test_play_media_announce( + hass: HomeAssistant, + soco: MockSoCo, + async_autosetup_sonos, + sonos_websocket, +) -> None: + """Test playing media with the announce.""" + content_id: str = "http://10.0.0.1:8123/local/sounds/doorbell.mp3" + volume: float = 0.30 + + # Test the success path + await hass.services.async_call( + MP_DOMAIN, + SERVICE_PLAY_MEDIA, + { + ATTR_ENTITY_ID: "media_player.zone_a", + ATTR_MEDIA_CONTENT_TYPE: "music", + ATTR_MEDIA_CONTENT_ID: content_id, + ATTR_MEDIA_ANNOUNCE: True, + ATTR_MEDIA_EXTRA: {"volume": volume}, + }, + blocking=True, + ) + assert sonos_websocket.play_clip.call_count == 1 + sonos_websocket.play_clip.assert_called_with(content_id, volume=volume) + + # Test receiving a websocket exception + sonos_websocket.play_clip.reset_mock() + sonos_websocket.play_clip.side_effect = SonosWebsocketError("Error Message") + with pytest.raises( + HomeAssistantError, match="Error when calling Sonos websocket: Error Message" + ): + await hass.services.async_call( + MP_DOMAIN, + SERVICE_PLAY_MEDIA, + { + ATTR_ENTITY_ID: "media_player.zone_a", + ATTR_MEDIA_CONTENT_TYPE: "music", + ATTR_MEDIA_CONTENT_ID: content_id, + ATTR_MEDIA_ANNOUNCE: True, + }, + blocking=True, + ) + assert sonos_websocket.play_clip.call_count == 1 + sonos_websocket.play_clip.assert_called_with(content_id, volume=None) + + # Test receiving a non success result + sonos_websocket.play_clip.reset_mock() + sonos_websocket.play_clip.side_effect = None + retval = {"success": 0} + sonos_websocket.play_clip.return_value = [retval, {}] + with pytest.raises( + HomeAssistantError, match=f"Announcing clip {content_id} failed {retval}" + ): + await hass.services.async_call( + MP_DOMAIN, + SERVICE_PLAY_MEDIA, + { + ATTR_ENTITY_ID: "media_player.zone_a", + ATTR_MEDIA_CONTENT_TYPE: "music", + ATTR_MEDIA_CONTENT_ID: content_id, + ATTR_MEDIA_ANNOUNCE: True, + }, + blocking=True, + ) + assert sonos_websocket.play_clip.call_count == 1 + + +async def test_media_get_queue( + hass: HomeAssistant, + soco: MockSoCo, + async_autosetup_sonos, + soco_factory, + snapshot: SnapshotAssertion, +) -> None: + """Test getting the media queue.""" + soco_mock = soco_factory.mock_list.get("192.168.42.2") + result = await hass.services.async_call( + SONOS_DOMAIN, + SERVICE_GET_QUEUE, + { + ATTR_ENTITY_ID: "media_player.zone_a", + }, + blocking=True, + return_response=True, + ) + soco_mock.get_queue.assert_called_with(max_items=0) + assert result == snapshot diff --git a/tests/components/spotify/conftest.py b/tests/components/spotify/conftest.py new file mode 100644 index 00000000000000..3f248b54529446 --- /dev/null +++ b/tests/components/spotify/conftest.py @@ -0,0 +1,128 @@ +"""Common test fixtures.""" + +from collections.abc import Generator +from typing import Any +from unittest.mock import MagicMock, patch + +import pytest + +from homeassistant.components.application_credentials import ( + ClientCredential, + async_import_client_credential, +) +from homeassistant.components.spotify import DOMAIN +from homeassistant.core import HomeAssistant +from homeassistant.setup import async_setup_component + +from tests.common import MockConfigEntry + + +@pytest.fixture +def mock_config_entry_1() -> MockConfigEntry: + """Mock a config entry with an upper case entry id.""" + return MockConfigEntry( + domain=DOMAIN, + title="spotify_1", + data={ + "auth_implementation": "spotify_c95e4090d4d3438b922331e7428f8171", + "token": { + "access_token": "AccessToken", + "token_type": "Bearer", + "expires_in": 3600, + "refresh_token": "RefreshToken", + "scope": "playlist-read-private ...", + "expires_at": 1724198975.8829377, + }, + "id": "32oesphrnacjcf7vw5bf6odx3oiu", + "name": "spotify_account_1", + }, + unique_id="84fce612f5b8", + entry_id="01J5TX5A0FF6G5V0QJX6HBC94T", + ) + + +@pytest.fixture +def mock_config_entry_2() -> MockConfigEntry: + """Mock a config entry with a lower case entry id.""" + return MockConfigEntry( + domain=DOMAIN, + title="spotify_2", + data={ + "auth_implementation": "spotify_c95e4090d4d3438b922331e7428f8171", + "token": { + "access_token": "AccessToken", + "token_type": "Bearer", + "expires_in": 3600, + "refresh_token": "RefreshToken", + "scope": "playlist-read-private ...", + "expires_at": 1724198975.8829377, + }, + "id": "55oesphrnacjcf7vw5bf6odx3oiu", + "name": "spotify_account_2", + }, + unique_id="99fce612f5b8", + entry_id="32oesphrnacjcf7vw5bf6odx3", + ) + + +@pytest.fixture +def spotify_playlists() -> dict[str, Any]: + """Mock the return from getting a list of playlists.""" + return { + "href": "https://api.spotify.com/v1/users/31oesphrnacjcf7vw5bf6odx3oiu/playlists?offset=0&limit=48", + "limit": 48, + "next": None, + "offset": 0, + "previous": None, + "total": 1, + "items": [ + { + "collaborative": False, + "description": "", + "id": "unique_identifier_00", + "name": "Playlist1", + "type": "playlist", + "uri": "spotify:playlist:unique_identifier_00", + } + ], + } + + +@pytest.fixture +def spotify_mock(spotify_playlists: dict[str, Any]) -> Generator[MagicMock]: + """Mock the Spotify API.""" + with patch("homeassistant.components.spotify.Spotify") as spotify_mock: + mock = MagicMock() + mock.current_user_playlists.return_value = spotify_playlists + spotify_mock.return_value = mock + yield spotify_mock + + +@pytest.fixture +async def spotify_setup( + hass: HomeAssistant, + spotify_mock: MagicMock, + mock_config_entry_1: MockConfigEntry, + mock_config_entry_2: MockConfigEntry, +): + """Set up the spotify integration.""" + with patch( + "homeassistant.components.spotify.OAuth2Session.async_ensure_token_valid" + ): + await async_setup_component(hass, "application_credentials", {}) + await hass.async_block_till_done() + await async_import_client_credential( + hass, + DOMAIN, + ClientCredential("CLIENT_ID", "CLIENT_SECRET"), + "spotify_c95e4090d4d3438b922331e7428f8171", + ) + await hass.async_block_till_done() + mock_config_entry_1.add_to_hass(hass) + await hass.config_entries.async_setup(mock_config_entry_1.entry_id) + mock_config_entry_2.add_to_hass(hass) + await hass.config_entries.async_setup(mock_config_entry_2.entry_id) + await hass.async_block_till_done(wait_background_tasks=True) + await async_setup_component(hass, DOMAIN, {}) + await hass.async_block_till_done(wait_background_tasks=True) + yield diff --git a/tests/components/spotify/snapshots/test_media_browser.ambr b/tests/components/spotify/snapshots/test_media_browser.ambr new file mode 100644 index 00000000000000..4236fcb2e7997a --- /dev/null +++ b/tests/components/spotify/snapshots/test_media_browser.ambr @@ -0,0 +1,236 @@ +# serializer version: 1 +# name: test_browse_media_categories + dict({ + 'can_expand': True, + 'can_play': False, + 'children': list([ + dict({ + 'can_expand': True, + 'can_play': False, + 'children_media_class': , + 'media_class': , + 'media_content_id': 'spotify://01j5tx5a0ff6g5v0qjx6hbc94t/current_user_playlists', + 'media_content_type': 'spotify://current_user_playlists', + 'thumbnail': None, + 'title': 'Playlists', + }), + dict({ + 'can_expand': True, + 'can_play': False, + 'children_media_class': , + 'media_class': , + 'media_content_id': 'spotify://01j5tx5a0ff6g5v0qjx6hbc94t/current_user_followed_artists', + 'media_content_type': 'spotify://current_user_followed_artists', + 'thumbnail': None, + 'title': 'Artists', + }), + dict({ + 'can_expand': True, + 'can_play': False, + 'children_media_class': , + 'media_class': , + 'media_content_id': 'spotify://01j5tx5a0ff6g5v0qjx6hbc94t/current_user_saved_albums', + 'media_content_type': 'spotify://current_user_saved_albums', + 'thumbnail': None, + 'title': 'Albums', + }), + dict({ + 'can_expand': True, + 'can_play': False, + 'children_media_class': , + 'media_class': , + 'media_content_id': 'spotify://01j5tx5a0ff6g5v0qjx6hbc94t/current_user_saved_tracks', + 'media_content_type': 'spotify://current_user_saved_tracks', + 'thumbnail': None, + 'title': 'Tracks', + }), + dict({ + 'can_expand': True, + 'can_play': False, + 'children_media_class': , + 'media_class': , + 'media_content_id': 'spotify://01j5tx5a0ff6g5v0qjx6hbc94t/current_user_saved_shows', + 'media_content_type': 'spotify://current_user_saved_shows', + 'thumbnail': None, + 'title': 'Podcasts', + }), + dict({ + 'can_expand': True, + 'can_play': False, + 'children_media_class': , + 'media_class': , + 'media_content_id': 'spotify://01j5tx5a0ff6g5v0qjx6hbc94t/current_user_recently_played', + 'media_content_type': 'spotify://current_user_recently_played', + 'thumbnail': None, + 'title': 'Recently played', + }), + dict({ + 'can_expand': True, + 'can_play': False, + 'children_media_class': , + 'media_class': , + 'media_content_id': 'spotify://01j5tx5a0ff6g5v0qjx6hbc94t/current_user_top_artists', + 'media_content_type': 'spotify://current_user_top_artists', + 'thumbnail': None, + 'title': 'Top Artists', + }), + dict({ + 'can_expand': True, + 'can_play': False, + 'children_media_class': , + 'media_class': , + 'media_content_id': 'spotify://01j5tx5a0ff6g5v0qjx6hbc94t/current_user_top_tracks', + 'media_content_type': 'spotify://current_user_top_tracks', + 'thumbnail': None, + 'title': 'Top Tracks', + }), + dict({ + 'can_expand': True, + 'can_play': False, + 'children_media_class': , + 'media_class': , + 'media_content_id': 'spotify://01j5tx5a0ff6g5v0qjx6hbc94t/categories', + 'media_content_type': 'spotify://categories', + 'thumbnail': None, + 'title': 'Categories', + }), + dict({ + 'can_expand': True, + 'can_play': False, + 'children_media_class': , + 'media_class': , + 'media_content_id': 'spotify://01j5tx5a0ff6g5v0qjx6hbc94t/featured_playlists', + 'media_content_type': 'spotify://featured_playlists', + 'thumbnail': None, + 'title': 'Featured Playlists', + }), + dict({ + 'can_expand': True, + 'can_play': False, + 'children_media_class': , + 'media_class': , + 'media_content_id': 'spotify://01j5tx5a0ff6g5v0qjx6hbc94t/new_releases', + 'media_content_type': 'spotify://new_releases', + 'thumbnail': None, + 'title': 'New Releases', + }), + ]), + 'children_media_class': , + 'media_class': , + 'media_content_id': 'spotify://01j5tx5a0ff6g5v0qjx6hbc94t/library', + 'media_content_type': 'spotify://library', + 'not_shown': 0, + 'thumbnail': None, + 'title': 'Media Library', + }) +# --- +# name: test_browse_media_playlists + dict({ + 'can_expand': True, + 'can_play': False, + 'children': list([ + dict({ + 'can_expand': True, + 'can_play': True, + 'children_media_class': , + 'media_class': , + 'media_content_id': 'spotify://01j5tx5a0ff6g5v0qjx6hbc94t/spotify:playlist:unique_identifier_00', + 'media_content_type': 'spotify://playlist', + 'thumbnail': None, + 'title': 'Playlist1', + }), + ]), + 'children_media_class': , + 'media_class': , + 'media_content_id': 'spotify://01j5tx5a0ff6g5v0qjx6hbc94t/current_user_playlists', + 'media_content_type': 'spotify://current_user_playlists', + 'not_shown': 0, + 'thumbnail': None, + 'title': 'Playlists', + }) +# --- +# name: test_browse_media_playlists[01J5TX5A0FF6G5V0QJX6HBC94T] + dict({ + 'can_expand': True, + 'can_play': False, + 'children': list([ + dict({ + 'can_expand': True, + 'can_play': True, + 'children_media_class': , + 'media_class': , + 'media_content_id': 'spotify://01j5tx5a0ff6g5v0qjx6hbc94t/spotify:playlist:unique_identifier_00', + 'media_content_type': 'spotify://playlist', + 'thumbnail': None, + 'title': 'Playlist1', + }), + ]), + 'children_media_class': , + 'media_class': , + 'media_content_id': 'spotify://01j5tx5a0ff6g5v0qjx6hbc94t/current_user_playlists', + 'media_content_type': 'spotify://current_user_playlists', + 'not_shown': 0, + 'thumbnail': None, + 'title': 'Playlists', + }) +# --- +# name: test_browse_media_playlists[32oesphrnacjcf7vw5bf6odx3] + dict({ + 'can_expand': True, + 'can_play': False, + 'children': list([ + dict({ + 'can_expand': True, + 'can_play': True, + 'children_media_class': , + 'media_class': , + 'media_content_id': 'spotify://32oesphrnacjcf7vw5bf6odx3/spotify:playlist:unique_identifier_00', + 'media_content_type': 'spotify://playlist', + 'thumbnail': None, + 'title': 'Playlist1', + }), + ]), + 'children_media_class': , + 'media_class': , + 'media_content_id': 'spotify://32oesphrnacjcf7vw5bf6odx3/current_user_playlists', + 'media_content_type': 'spotify://current_user_playlists', + 'not_shown': 0, + 'thumbnail': None, + 'title': 'Playlists', + }) +# --- +# name: test_browse_media_root + dict({ + 'can_expand': True, + 'can_play': False, + 'children': list([ + dict({ + 'can_expand': True, + 'can_play': False, + 'children_media_class': None, + 'media_class': , + 'media_content_id': 'spotify://01J5TX5A0FF6G5V0QJX6HBC94T', + 'media_content_type': 'spotify://library', + 'thumbnail': 'https://brands.home-assistant.io/_/spotify/logo.png', + 'title': 'spotify_1', + }), + dict({ + 'can_expand': True, + 'can_play': False, + 'children_media_class': None, + 'media_class': , + 'media_content_id': 'spotify://32oesphrnacjcf7vw5bf6odx3', + 'media_content_type': 'spotify://library', + 'thumbnail': 'https://brands.home-assistant.io/_/spotify/logo.png', + 'title': 'spotify_2', + }), + ]), + 'children_media_class': , + 'media_class': , + 'media_content_id': 'spotify://', + 'media_content_type': 'spotify', + 'not_shown': 0, + 'thumbnail': 'https://brands.home-assistant.io/_/spotify/logo.png', + 'title': 'Spotify', + }) +# --- diff --git a/tests/components/spotify/test_config_flow.py b/tests/components/spotify/test_config_flow.py index 6040fcd84f27b8..09feb4a6e83d38 100644 --- a/tests/components/spotify/test_config_flow.py +++ b/tests/components/spotify/test_config_flow.py @@ -13,7 +13,7 @@ async_import_client_credential, ) from homeassistant.components.spotify.const import DOMAIN -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER, SOURCE_ZEROCONF +from homeassistant.config_entries import SOURCE_USER, SOURCE_ZEROCONF from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType from homeassistant.helpers import config_entry_oauth2_flow @@ -201,15 +201,7 @@ async def test_reauthentication( ) old_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": old_entry.unique_id, - "entry_id": old_entry.entry_id, - }, - data=old_entry.data, - ) + result = await old_entry.start_reauth_flow(hass) flows = hass.config_entries.flow.async_progress() assert len(flows) == 1 @@ -269,15 +261,7 @@ async def test_reauth_account_mismatch( ) old_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": old_entry.unique_id, - "entry_id": old_entry.entry_id, - }, - data=old_entry.data, - ) + result = await old_entry.start_reauth_flow(hass) flows = hass.config_entries.flow.async_progress() result = await hass.config_entries.flow.async_configure(flows[0]["flow_id"], {}) diff --git a/tests/components/spotify/test_media_browser.py b/tests/components/spotify/test_media_browser.py new file mode 100644 index 00000000000000..2b47aed9ee3ea6 --- /dev/null +++ b/tests/components/spotify/test_media_browser.py @@ -0,0 +1,61 @@ +"""Test the media browser interface.""" + +import pytest +from syrupy import SnapshotAssertion + +from homeassistant.components.spotify import DOMAIN +from homeassistant.components.spotify.browse_media import async_browse_media +from homeassistant.core import HomeAssistant +from homeassistant.setup import async_setup_component + +from tests.common import MockConfigEntry + + +async def setup_integration(hass: HomeAssistant, config_entry: MockConfigEntry) -> None: + """Fixture for setting up the component.""" + config_entry.add_to_hass(hass) + + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done(wait_background_tasks=True) + await async_setup_component(hass, DOMAIN, {}) + await hass.async_block_till_done(wait_background_tasks=True) + + +async def test_browse_media_root( + hass: HomeAssistant, + snapshot: SnapshotAssertion, + spotify_setup, +) -> None: + """Test browsing the root.""" + response = await async_browse_media(hass, None, None) + assert response.as_dict() == snapshot + + +async def test_browse_media_categories( + hass: HomeAssistant, + snapshot: SnapshotAssertion, + spotify_setup, +) -> None: + """Test browsing categories.""" + response = await async_browse_media( + hass, "spotify://library", "spotify://01J5TX5A0FF6G5V0QJX6HBC94T" + ) + assert response.as_dict() == snapshot + + +@pytest.mark.parametrize( + ("config_entry_id"), [("01J5TX5A0FF6G5V0QJX6HBC94T"), ("32oesphrnacjcf7vw5bf6odx3")] +) +async def test_browse_media_playlists( + hass: HomeAssistant, + snapshot: SnapshotAssertion, + config_entry_id: str, + spotify_setup, +) -> None: + """Test browsing playlists for the two config entries.""" + response = await async_browse_media( + hass, + "spotify://current_user_playlists", + f"spotify://{config_entry_id}/current_user_playlists", + ) + assert response.as_dict() == snapshot diff --git a/tests/components/squeezebox/conftest.py b/tests/components/squeezebox/conftest.py new file mode 100644 index 00000000000000..4a4bdc6ae73573 --- /dev/null +++ b/tests/components/squeezebox/conftest.py @@ -0,0 +1,133 @@ +"""Setup the squeezebox tests.""" + +from collections.abc import Generator +from unittest.mock import AsyncMock, MagicMock, patch + +import pytest + +from homeassistant.components.media_player import MediaType +from homeassistant.components.squeezebox import const +from homeassistant.components.squeezebox.browse_media import ( + MEDIA_TYPE_TO_SQUEEZEBOX, + SQUEEZEBOX_ID_BY_TYPE, +) +from homeassistant.const import CONF_HOST, CONF_PORT +from homeassistant.core import HomeAssistant +from homeassistant.helpers.device_registry import format_mac + +from tests.common import MockConfigEntry + +TEST_HOST = "1.2.3.4" +TEST_PORT = "9000" +TEST_USE_HTTPS = False +SERVER_UUID = "12345678-1234-1234-1234-123456789012" +TEST_MAC = "aa:bb:cc:dd:ee:ff" + + +@pytest.fixture +def mock_setup_entry() -> Generator[AsyncMock]: + """Override async_setup_entry.""" + with patch( + "homeassistant.components.squeezebox.async_setup_entry", return_value=True + ) as mock_setup_entry: + yield mock_setup_entry + + +@pytest.fixture +def config_entry(hass: HomeAssistant) -> MockConfigEntry: + """Add the squeezebox mock config entry to hass.""" + config_entry = MockConfigEntry( + domain=const.DOMAIN, + unique_id=SERVER_UUID, + data={ + CONF_HOST: TEST_HOST, + CONF_PORT: TEST_PORT, + const.CONF_HTTPS: TEST_USE_HTTPS, + }, + ) + config_entry.add_to_hass(hass) + return config_entry + + +async def mock_async_browse( + media_type: MediaType, limit: int, browse_id: tuple | None = None +) -> dict | None: + """Mock the async_browse method of pysqueezebox.Player.""" + child_types = { + "favorites": "favorites", + "albums": "album", + "album": "track", + "genres": "genre", + "genre": "album", + "artists": "artist", + "artist": "album", + "titles": "title", + "title": "title", + "playlists": "playlist", + "playlist": "title", + } + fake_items = [ + { + "title": "Fake Item 1", + "id": "1234", + "hasitems": False, + "item_type": child_types[media_type], + "artwork_track_id": "b35bb9e9", + }, + { + "title": "Fake Item 2", + "id": "12345", + "hasitems": media_type == "favorites", + "item_type": child_types[media_type], + "image_url": "http://lms.internal:9000/html/images/favorites.png", + }, + { + "title": "Fake Item 3", + "id": "123456", + "hasitems": media_type == "favorites", + "album_id": "123456" if media_type == "favorites" else None, + }, + ] + + if browse_id: + search_type, search_id = browse_id + if search_id: + if search_type in SQUEEZEBOX_ID_BY_TYPE.values(): + for item in fake_items: + if item["id"] == search_id: + return { + "title": item["title"], + "items": [item], + } + return None + if search_type in SQUEEZEBOX_ID_BY_TYPE.values(): + return { + "title": search_type, + "items": fake_items, + } + return None + if media_type in MEDIA_TYPE_TO_SQUEEZEBOX.values(): + return { + "title": media_type, + "items": fake_items, + } + return None + + +@pytest.fixture +def lms() -> MagicMock: + """Mock a Lyrion Media Server with one mock player attached.""" + lms = MagicMock() + player = MagicMock() + player.player_id = TEST_MAC + player.name = "Test Player" + player.power = False + player.async_browse = AsyncMock(side_effect=mock_async_browse) + player.async_load_playlist = AsyncMock() + player.async_update = AsyncMock() + player.generate_image_url_from_track_id = MagicMock( + return_value="http://lms.internal:9000/html/images/favorites.png" + ) + lms.async_get_players = AsyncMock(return_value=[player]) + lms.async_query = AsyncMock(return_value={"uuid": format_mac(TEST_MAC)}) + return lms diff --git a/tests/components/squeezebox/test_media_browser.py b/tests/components/squeezebox/test_media_browser.py new file mode 100644 index 00000000000000..62d668ca57bb74 --- /dev/null +++ b/tests/components/squeezebox/test_media_browser.py @@ -0,0 +1,205 @@ +"""Test the media browser interface.""" + +from unittest.mock import MagicMock, patch + +import pytest + +from homeassistant.components.media_player import ( + ATTR_MEDIA_CONTENT_ID, + ATTR_MEDIA_CONTENT_TYPE, + DOMAIN as MEDIA_PLAYER_DOMAIN, + SERVICE_PLAY_MEDIA, + BrowseError, + MediaType, +) +from homeassistant.components.squeezebox.browse_media import ( + LIBRARY, + MEDIA_TYPE_TO_SQUEEZEBOX, +) +from homeassistant.const import ATTR_ENTITY_ID +from homeassistant.core import HomeAssistant + +from tests.common import MockConfigEntry +from tests.typing import WebSocketGenerator + + +@pytest.fixture(autouse=True) +async def setup_integration( + hass: HomeAssistant, config_entry: MockConfigEntry, lms: MagicMock +) -> None: + """Fixture for setting up the component.""" + with ( + patch("homeassistant.components.squeezebox.Server", return_value=lms), + patch( + "homeassistant.components.squeezebox.media_player.start_server_discovery" + ), + ): + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done(wait_background_tasks=True) + + +async def test_async_browse_media_root( + hass: HomeAssistant, + config_entry: MockConfigEntry, + hass_ws_client: WebSocketGenerator, +) -> None: + """Test the async_browse_media function at the root level.""" + + client = await hass_ws_client() + await client.send_json( + { + "id": 1, + "type": "media_player/browse_media", + "entity_id": "media_player.test_player", + "media_content_id": "", + "media_content_type": "library", + } + ) + response = await client.receive_json() + assert response["success"] + result = response["result"] + for idx, item in enumerate(result["children"]): + assert item["title"] == LIBRARY[idx] + + +async def test_async_browse_media_with_subitems( + hass: HomeAssistant, + config_entry: MockConfigEntry, + hass_ws_client: WebSocketGenerator, +) -> None: + """Test each category with subitems.""" + for category in ("Favorites", "Artists", "Albums", "Playlists", "Genres"): + with patch( + "homeassistant.components.squeezebox.browse_media.is_internal_request", + return_value=False, + ): + client = await hass_ws_client() + await client.send_json( + { + "id": 1, + "type": "media_player/browse_media", + "entity_id": "media_player.test_player", + "media_content_id": "", + "media_content_type": category, + } + ) + response = await client.receive_json() + assert response["success"] + category_level = response["result"] + assert category_level["title"] == MEDIA_TYPE_TO_SQUEEZEBOX[category] + assert category_level["children"][0]["title"] == "Fake Item 1" + + # Look up a subitem + search_type = category_level["children"][0]["media_content_type"] + search_id = category_level["children"][0]["media_content_id"] + await client.send_json( + { + "id": 2, + "type": "media_player/browse_media", + "entity_id": "media_player.test_player", + "media_content_id": search_id, + "media_content_type": search_type, + } + ) + response = await client.receive_json() + assert response["success"] + search = response["result"] + assert search["title"] == "Fake Item 1" + + +async def test_async_browse_tracks( + hass: HomeAssistant, + config_entry: MockConfigEntry, + hass_ws_client: WebSocketGenerator, +) -> None: + """Test tracks (no subitems).""" + with patch( + "homeassistant.components.squeezebox.browse_media.is_internal_request", + return_value=True, + ): + client = await hass_ws_client() + await client.send_json( + { + "id": 1, + "type": "media_player/browse_media", + "entity_id": "media_player.test_player", + "media_content_id": "", + "media_content_type": "Tracks", + } + ) + response = await client.receive_json() + assert response["success"] + tracks = response["result"] + assert tracks["title"] == "titles" + assert len(tracks["children"]) == 3 + + +async def test_async_browse_error( + hass: HomeAssistant, + config_entry: MockConfigEntry, + hass_ws_client: WebSocketGenerator, +) -> None: + """Search for a non-existent item and assert error.""" + client = await hass_ws_client() + await client.send_json( + { + "id": 1, + "type": "media_player/browse_media", + "entity_id": "media_player.test_player", + "media_content_id": "0", + "media_content_type": MediaType.ALBUM, + } + ) + response = await client.receive_json() + assert not response["success"] + + +async def test_play_browse_item( + hass: HomeAssistant, + config_entry: MockConfigEntry, +) -> None: + """Test play browse item.""" + await hass.services.async_call( + MEDIA_PLAYER_DOMAIN, + SERVICE_PLAY_MEDIA, + { + ATTR_ENTITY_ID: "media_player.test_player", + ATTR_MEDIA_CONTENT_ID: "1234", + ATTR_MEDIA_CONTENT_TYPE: "album", + }, + ) + + +async def test_play_browse_item_nonexistent( + hass: HomeAssistant, config_entry: MockConfigEntry +) -> None: + """Test trying to play an item that doesn't exist.""" + with pytest.raises(BrowseError): + await hass.services.async_call( + MEDIA_PLAYER_DOMAIN, + SERVICE_PLAY_MEDIA, + { + ATTR_ENTITY_ID: "media_player.test_player", + ATTR_MEDIA_CONTENT_ID: "0", + ATTR_MEDIA_CONTENT_TYPE: "album", + }, + blocking=True, + ) + + +async def test_play_browse_item_bad_category( + hass: HomeAssistant, + config_entry: MockConfigEntry, +) -> None: + """Test trying to play an item whose category doesn't exist.""" + with pytest.raises(BrowseError): + await hass.services.async_call( + MEDIA_PLAYER_DOMAIN, + SERVICE_PLAY_MEDIA, + { + ATTR_ENTITY_ID: "media_player.test_player", + ATTR_MEDIA_CONTENT_ID: "1234", + ATTR_MEDIA_CONTENT_TYPE: "bad_category", + }, + blocking=True, + ) diff --git a/tests/components/steam_online/test_config_flow.py b/tests/components/steam_online/test_config_flow.py index a5bce80d890b1f..140a8309ff9131 100644 --- a/tests/components/steam_online/test_config_flow.py +++ b/tests/components/steam_online/test_config_flow.py @@ -5,8 +5,8 @@ import steam from homeassistant.components.steam_online.const import CONF_ACCOUNTS, DOMAIN -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER -from homeassistant.const import CONF_API_KEY, CONF_SOURCE +from homeassistant.config_entries import SOURCE_USER +from homeassistant.const import CONF_API_KEY from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType from homeassistant.helpers import entity_registry as er @@ -111,18 +111,10 @@ async def test_flow_user_already_configured(hass: HomeAssistant) -> None: async def test_flow_reauth(hass: HomeAssistant) -> None: """Test reauth step.""" entry = create_entry(hass) + result = await entry.start_reauth_flow(hass) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reauth_confirm" with patch_interface(): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - CONF_SOURCE: SOURCE_REAUTH, - "entry_id": entry.entry_id, - "unique_id": entry.unique_id, - }, - data=CONF_DATA, - ) - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "reauth_confirm" result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={}, diff --git a/tests/components/stt/common.py b/tests/components/stt/common.py index e6c36c5b3508d3..f964fca6b67727 100644 --- a/tests/components/stt/common.py +++ b/tests/components/stt/common.py @@ -2,11 +2,22 @@ from __future__ import annotations -from collections.abc import Callable, Coroutine +from collections.abc import AsyncIterable, Callable, Coroutine from pathlib import Path from typing import Any -from homeassistant.components.stt import Provider +from homeassistant.components.stt import ( + AudioBitRates, + AudioChannels, + AudioCodecs, + AudioFormats, + AudioSampleRates, + Provider, + SpeechMetadata, + SpeechResult, + SpeechResultState, + SpeechToTextEntity, +) from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback @@ -14,6 +25,80 @@ from tests.common import MockPlatform, mock_platform +TEST_DOMAIN = "test" + + +class BaseProvider: + """Mock STT provider.""" + + fail_process_audio = False + + def __init__( + self, *, supported_languages: list[str] | None = None, text: str = "test_result" + ) -> None: + """Init test provider.""" + self._supported_languages = supported_languages or ["de", "de-CH", "en"] + self.calls: list[tuple[SpeechMetadata, AsyncIterable[bytes]]] = [] + self.received: list[bytes] = [] + self.text = text + + @property + def supported_languages(self) -> list[str]: + """Return a list of supported languages.""" + return self._supported_languages + + @property + def supported_formats(self) -> list[AudioFormats]: + """Return a list of supported formats.""" + return [AudioFormats.WAV, AudioFormats.OGG] + + @property + def supported_codecs(self) -> list[AudioCodecs]: + """Return a list of supported codecs.""" + return [AudioCodecs.PCM, AudioCodecs.OPUS] + + @property + def supported_bit_rates(self) -> list[AudioBitRates]: + """Return a list of supported bitrates.""" + return [AudioBitRates.BITRATE_16] + + @property + def supported_sample_rates(self) -> list[AudioSampleRates]: + """Return a list of supported samplerates.""" + return [AudioSampleRates.SAMPLERATE_16000] + + @property + def supported_channels(self) -> list[AudioChannels]: + """Return a list of supported channels.""" + return [AudioChannels.CHANNEL_MONO] + + async def async_process_audio_stream( + self, metadata: SpeechMetadata, stream: AsyncIterable[bytes] + ) -> SpeechResult: + """Process an audio stream.""" + self.calls.append((metadata, stream)) + async for data in stream: + if not data: + break + self.received.append(data) + if self.fail_process_audio: + return SpeechResult(None, SpeechResultState.ERROR) + + return SpeechResult(self.text, SpeechResultState.SUCCESS) + + +class MockSTTProvider(BaseProvider, Provider): + """Mock provider.""" + + url_path = TEST_DOMAIN + + +class MockSTTProviderEntity(BaseProvider, SpeechToTextEntity): + """Mock provider entity.""" + + url_path = "stt.test" + _attr_name = "test" + class MockSTTPlatform(MockPlatform): """Help to set up test stt service.""" diff --git a/tests/components/stt/test_init.py b/tests/components/stt/test_init.py index ca2685ff827eac..92225123995a6c 100644 --- a/tests/components/stt/test_init.py +++ b/tests/components/stt/test_init.py @@ -1,6 +1,7 @@ """Test STT component setup.""" -from collections.abc import AsyncIterable, Generator +from collections.abc import Generator, Iterable +from contextlib import ExitStack from http import HTTPStatus from pathlib import Path from unittest.mock import AsyncMock @@ -9,16 +10,6 @@ from homeassistant.components.stt import ( DOMAIN, - AudioBitRates, - AudioChannels, - AudioCodecs, - AudioFormats, - AudioSampleRates, - Provider, - SpeechMetadata, - SpeechResult, - SpeechResultState, - SpeechToTextEntity, async_default_engine, async_get_provider, async_get_speech_to_text_engine, @@ -28,7 +19,13 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.setup import async_setup_component -from .common import mock_stt_entity_platform, mock_stt_platform +from .common import ( + TEST_DOMAIN, + MockSTTProvider, + MockSTTProviderEntity, + mock_stt_entity_platform, + mock_stt_platform, +) from tests.common import ( MockConfigEntry, @@ -40,102 +37,40 @@ ) from tests.typing import ClientSessionGenerator, WebSocketGenerator -TEST_DOMAIN = "test" - - -class BaseProvider: - """Mock provider.""" - - fail_process_audio = False - - def __init__(self) -> None: - """Init test provider.""" - self.calls: list[tuple[SpeechMetadata, AsyncIterable[bytes]]] = [] - - @property - def supported_languages(self) -> list[str]: - """Return a list of supported languages.""" - return ["de", "de-CH", "en"] - - @property - def supported_formats(self) -> list[AudioFormats]: - """Return a list of supported formats.""" - return [AudioFormats.WAV, AudioFormats.OGG] - - @property - def supported_codecs(self) -> list[AudioCodecs]: - """Return a list of supported codecs.""" - return [AudioCodecs.PCM, AudioCodecs.OPUS] - - @property - def supported_bit_rates(self) -> list[AudioBitRates]: - """Return a list of supported bitrates.""" - return [AudioBitRates.BITRATE_16] - - @property - def supported_sample_rates(self) -> list[AudioSampleRates]: - """Return a list of supported samplerates.""" - return [AudioSampleRates.SAMPLERATE_16000] - - @property - def supported_channels(self) -> list[AudioChannels]: - """Return a list of supported channels.""" - return [AudioChannels.CHANNEL_MONO] - - async def async_process_audio_stream( - self, metadata: SpeechMetadata, stream: AsyncIterable[bytes] - ) -> SpeechResult: - """Process an audio stream.""" - self.calls.append((metadata, stream)) - if self.fail_process_audio: - return SpeechResult(None, SpeechResultState.ERROR) - - return SpeechResult("test_result", SpeechResultState.SUCCESS) - - -class MockProvider(BaseProvider, Provider): - """Mock provider.""" - - url_path = TEST_DOMAIN - - -class MockProviderEntity(BaseProvider, SpeechToTextEntity): - """Mock provider entity.""" - - url_path = "stt.test" - _attr_name = "test" - @pytest.fixture -def mock_provider() -> MockProvider: +def mock_provider() -> MockSTTProvider: """Test provider fixture.""" - return MockProvider() + return MockSTTProvider() @pytest.fixture -def mock_provider_entity() -> MockProviderEntity: +def mock_provider_entity() -> MockSTTProviderEntity: """Test provider entity fixture.""" - return MockProviderEntity() + return MockSTTProviderEntity() class STTFlow(ConfigFlow): """Test flow.""" -@pytest.fixture(name="config_flow_test_domain") -def config_flow_test_domain_fixture() -> str: +@pytest.fixture(name="config_flow_test_domains") +def config_flow_test_domain_fixture() -> Iterable[str]: """Test domain fixture.""" - return TEST_DOMAIN + return (TEST_DOMAIN,) @pytest.fixture(autouse=True) def config_flow_fixture( - hass: HomeAssistant, config_flow_test_domain: str + hass: HomeAssistant, config_flow_test_domains: Iterable[str] ) -> Generator[None]: """Mock config flow.""" - mock_platform(hass, f"{config_flow_test_domain}.config_flow") + for domain in config_flow_test_domains: + mock_platform(hass, f"{domain}.config_flow") - with mock_config_flow(config_flow_test_domain, STTFlow): + with ExitStack() as stack: + for domain in config_flow_test_domains: + stack.enter_context(mock_config_flow(domain, STTFlow)) yield @@ -144,14 +79,14 @@ async def setup_fixture( hass: HomeAssistant, tmp_path: Path, request: pytest.FixtureRequest, -) -> MockProvider | MockProviderEntity: +) -> MockSTTProvider | MockSTTProviderEntity: """Set up the test environment.""" - provider: MockProvider | MockProviderEntity + provider: MockSTTProvider | MockSTTProviderEntity if request.param == "mock_setup": - provider = MockProvider() + provider = MockSTTProvider() await mock_setup(hass, tmp_path, provider) elif request.param == "mock_config_entry_setup": - provider = MockProviderEntity() + provider = MockSTTProviderEntity() await mock_config_entry_setup(hass, tmp_path, provider) else: raise RuntimeError("Invalid setup fixture") @@ -162,7 +97,7 @@ async def setup_fixture( async def mock_setup( hass: HomeAssistant, tmp_path: Path, - mock_provider: MockProvider, + mock_provider: MockSTTProvider, ) -> None: """Set up a test provider.""" mock_stt_platform( @@ -178,7 +113,7 @@ async def mock_setup( async def mock_config_entry_setup( hass: HomeAssistant, tmp_path: Path, - mock_provider_entity: MockProviderEntity, + mock_provider_entity: MockSTTProviderEntity, test_domain: str = TEST_DOMAIN, ) -> MockConfigEntry: """Set up a test provider via config entry.""" @@ -230,7 +165,7 @@ async def async_setup_entry_platform( async def test_get_provider_info( hass: HomeAssistant, hass_client: ClientSessionGenerator, - setup: MockProvider | MockProviderEntity, + setup: MockSTTProvider | MockSTTProviderEntity, ) -> None: """Test engine that doesn't exist.""" client = await hass_client() @@ -252,7 +187,7 @@ async def test_get_provider_info( async def test_non_existing_provider( hass: HomeAssistant, hass_client: ClientSessionGenerator, - setup: MockProvider | MockProviderEntity, + setup: MockSTTProvider | MockSTTProviderEntity, ) -> None: """Test streaming to engine that doesn't exist.""" client = await hass_client() @@ -278,7 +213,7 @@ async def test_non_existing_provider( async def test_stream_audio( hass: HomeAssistant, hass_client: ClientSessionGenerator, - setup: MockProvider | MockProviderEntity, + setup: MockSTTProvider | MockSTTProviderEntity, ) -> None: """Test streaming audio and getting response.""" client = await hass_client() @@ -339,7 +274,7 @@ async def test_metadata_errors( header: str | None, status: int, error: str, - setup: MockProvider | MockProviderEntity, + setup: MockSTTProvider | MockSTTProviderEntity, ) -> None: """Test metadata errors.""" client = await hass_client() @@ -355,7 +290,7 @@ async def test_metadata_errors( async def test_get_provider( hass: HomeAssistant, tmp_path: Path, - mock_provider: MockProvider, + mock_provider: MockSTTProvider, ) -> None: """Test we can get STT providers.""" await mock_setup(hass, tmp_path, mock_provider) @@ -366,7 +301,7 @@ async def test_get_provider( async def test_config_entry_unload( - hass: HomeAssistant, tmp_path: Path, mock_provider_entity: MockProviderEntity + hass: HomeAssistant, tmp_path: Path, mock_provider_entity: MockSTTProviderEntity ) -> None: """Test we can unload config entry.""" config_entry = await mock_config_entry_setup(hass, tmp_path, mock_provider_entity) @@ -378,7 +313,7 @@ async def test_config_entry_unload( async def test_restore_state( hass: HomeAssistant, tmp_path: Path, - mock_provider_entity: MockProviderEntity, + mock_provider_entity: MockSTTProviderEntity, ) -> None: """Test we restore state in the integration.""" entity_id = f"{DOMAIN}.{TEST_DOMAIN}" @@ -395,15 +330,19 @@ async def test_restore_state( @pytest.mark.parametrize( - ("setup", "engine_id"), - [("mock_setup", "test"), ("mock_config_entry_setup", "stt.test")], + ("setup", "engine_id", "extra_data"), + [ + ("mock_setup", "test", {"name": "test"}), + ("mock_config_entry_setup", "stt.test", {}), + ], indirect=["setup"], ) async def test_ws_list_engines( hass: HomeAssistant, hass_ws_client: WebSocketGenerator, - setup: MockProvider | MockProviderEntity, + setup: MockSTTProvider | MockSTTProviderEntity, engine_id: str, + extra_data: dict[str, str], ) -> None: """Test listing speech-to-text engines.""" client = await hass_ws_client() @@ -415,6 +354,7 @@ async def test_ws_list_engines( assert msg["result"] == { "providers": [ {"engine_id": engine_id, "supported_languages": ["de", "de-CH", "en"]} + | extra_data ] } @@ -423,7 +363,7 @@ async def test_ws_list_engines( msg = await client.receive_json() assert msg["success"] assert msg["result"] == { - "providers": [{"engine_id": engine_id, "supported_languages": []}] + "providers": [{"engine_id": engine_id, "supported_languages": []} | extra_data] } await client.send_json_auto_id({"type": "stt/engine/list", "language": "en"}) @@ -431,7 +371,9 @@ async def test_ws_list_engines( msg = await client.receive_json() assert msg["success"] assert msg["result"] == { - "providers": [{"engine_id": engine_id, "supported_languages": ["en"]}] + "providers": [ + {"engine_id": engine_id, "supported_languages": ["en"]} | extra_data + ] } await client.send_json_auto_id({"type": "stt/engine/list", "language": "en-UK"}) @@ -439,7 +381,9 @@ async def test_ws_list_engines( msg = await client.receive_json() assert msg["success"] assert msg["result"] == { - "providers": [{"engine_id": engine_id, "supported_languages": ["en"]}] + "providers": [ + {"engine_id": engine_id, "supported_languages": ["en"]} | extra_data + ] } await client.send_json_auto_id({"type": "stt/engine/list", "language": "de"}) @@ -447,7 +391,10 @@ async def test_ws_list_engines( assert msg["type"] == "result" assert msg["success"] assert msg["result"] == { - "providers": [{"engine_id": engine_id, "supported_languages": ["de", "de-CH"]}] + "providers": [ + {"engine_id": engine_id, "supported_languages": ["de", "de-CH"]} + | extra_data + ] } await client.send_json_auto_id( @@ -457,7 +404,10 @@ async def test_ws_list_engines( assert msg["type"] == "result" assert msg["success"] assert msg["result"] == { - "providers": [{"engine_id": engine_id, "supported_languages": ["de-CH", "de"]}] + "providers": [ + {"engine_id": engine_id, "supported_languages": ["de-CH", "de"]} + | extra_data + ] } @@ -472,7 +422,7 @@ async def test_default_engine_none(hass: HomeAssistant, tmp_path: Path) -> None: async def test_default_engine( hass: HomeAssistant, tmp_path: Path, - mock_provider: MockProvider, + mock_provider: MockSTTProvider, ) -> None: """Test async_default_engine.""" mock_stt_platform( @@ -488,7 +438,7 @@ async def test_default_engine( async def test_default_engine_entity( - hass: HomeAssistant, tmp_path: Path, mock_provider_entity: MockProviderEntity + hass: HomeAssistant, tmp_path: Path, mock_provider_entity: MockSTTProviderEntity ) -> None: """Test async_default_engine.""" await mock_config_entry_setup(hass, tmp_path, mock_provider_entity) @@ -496,21 +446,25 @@ async def test_default_engine_entity( assert async_default_engine(hass) == f"{DOMAIN}.{TEST_DOMAIN}" -@pytest.mark.parametrize("config_flow_test_domain", ["new_test"]) -async def test_default_engine_prefer_provider( +@pytest.mark.parametrize("config_flow_test_domains", [("new_test",)]) +async def test_default_engine_prefer_entity( hass: HomeAssistant, tmp_path: Path, - mock_provider_entity: MockProviderEntity, - mock_provider: MockProvider, - config_flow_test_domain: str, + mock_provider_entity: MockSTTProviderEntity, + mock_provider: MockSTTProvider, + config_flow_test_domains: str, ) -> None: - """Test async_default_engine.""" + """Test async_default_engine. + + In this tests there's an entity and a legacy provider. + The test asserts async_default_engine returns the entity. + """ mock_provider_entity.url_path = "stt.new_test" mock_provider_entity._attr_name = "New test" await mock_setup(hass, tmp_path, mock_provider) await mock_config_entry_setup( - hass, tmp_path, mock_provider_entity, test_domain=config_flow_test_domain + hass, tmp_path, mock_provider_entity, test_domain=config_flow_test_domains[0] ) await hass.async_block_till_done() @@ -520,11 +474,53 @@ async def test_default_engine_prefer_provider( provider_engine = async_get_speech_to_text_engine(hass, "test") assert provider_engine is not None assert provider_engine.name == "test" - assert async_default_engine(hass) == "test" + assert async_default_engine(hass) == "stt.new_test" + + +@pytest.mark.parametrize( + "config_flow_test_domains", + [ + # Test different setup order to ensure the default is not influenced + # by setup order. + ("cloud", "new_test"), + ("new_test", "cloud"), + ], +) +async def test_default_engine_prefer_cloud_entity( + hass: HomeAssistant, + tmp_path: Path, + mock_provider: MockSTTProvider, + config_flow_test_domains: str, +) -> None: + """Test async_default_engine. + + In this tests there's an entity from domain cloud, an entity from domain new_test + and a legacy provider. + The test asserts async_default_engine returns the entity from domain cloud. + """ + await mock_setup(hass, tmp_path, mock_provider) + for domain in config_flow_test_domains: + entity = MockSTTProviderEntity() + entity.url_path = f"stt.{domain}" + entity._attr_name = f"{domain} STT entity" + await mock_config_entry_setup(hass, tmp_path, entity, test_domain=domain) + await hass.async_block_till_done() + + for domain in config_flow_test_domains: + entity_engine = async_get_speech_to_text_engine( + hass, f"stt.{domain}_stt_entity" + ) + assert entity_engine is not None + assert entity_engine.name == f"{domain} STT entity" + + provider_engine = async_get_speech_to_text_engine(hass, "test") + assert provider_engine is not None + assert provider_engine.name == "test" + assert async_default_engine(hass) == "stt.cloud_stt_entity" async def test_get_engine_legacy( - hass: HomeAssistant, tmp_path: Path, mock_provider: MockProvider + hass: HomeAssistant, tmp_path: Path, mock_provider: MockSTTProvider ) -> None: """Test async_get_speech_to_text_engine.""" mock_stt_platform( @@ -549,7 +545,7 @@ async def test_get_engine_legacy( async def test_get_engine_entity( - hass: HomeAssistant, tmp_path: Path, mock_provider_entity: MockProviderEntity + hass: HomeAssistant, tmp_path: Path, mock_provider_entity: MockSTTProviderEntity ) -> None: """Test async_get_speech_to_text_engine.""" await mock_config_entry_setup(hass, tmp_path, mock_provider_entity) diff --git a/tests/components/sunweg/test_config_flow.py b/tests/components/sunweg/test_config_flow.py index 80b6a946749844..8103003d7fbbcf 100644 --- a/tests/components/sunweg/test_config_flow.py +++ b/tests/components/sunweg/test_config_flow.py @@ -69,14 +69,7 @@ async def test_reauth(hass: HomeAssistant, plant_fixture, inverter_fixture) -> N assert entries[0].data[CONF_USERNAME] == SUNWEG_MOCK_ENTRY.data[CONF_USERNAME] assert entries[0].data[CONF_PASSWORD] == SUNWEG_MOCK_ENTRY.data[CONF_PASSWORD] - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": mock_entry.entry_id, - }, - data=mock_entry.data, - ) + result = await mock_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/surepetcare/test_config_flow.py b/tests/components/surepetcare/test_config_flow.py index c3c13195acac8b..c4055ebe658579 100644 --- a/tests/components/surepetcare/test_config_flow.py +++ b/tests/components/surepetcare/test_config_flow.py @@ -155,15 +155,7 @@ async def test_reauthentication(hass: HomeAssistant) -> None: ) old_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": old_entry.unique_id, - "entry_id": old_entry.entry_id, - }, - data=old_entry.data, - ) + result = await old_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["errors"] == {} @@ -192,15 +184,7 @@ async def test_reauthentication_failure(hass: HomeAssistant) -> None: ) old_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": old_entry.unique_id, - "entry_id": old_entry.entry_id, - }, - data=old_entry.data, - ) + result = await old_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["errors"] == {} @@ -230,15 +214,7 @@ async def test_reauthentication_cannot_connect(hass: HomeAssistant) -> None: ) old_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": old_entry.unique_id, - "entry_id": old_entry.entry_id, - }, - data=old_entry.data, - ) + result = await old_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["errors"] == {} @@ -268,15 +244,7 @@ async def test_reauthentication_unknown_failure(hass: HomeAssistant) -> None: ) old_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": old_entry.unique_id, - "entry_id": old_entry.entry_id, - }, - data=old_entry.data, - ) + result = await old_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["errors"] == {} diff --git a/tests/components/switchbot/test_config_flow.py b/tests/components/switchbot/test_config_flow.py index 182e9457f223ba..b0fba2a5f18ab5 100644 --- a/tests/components/switchbot/test_config_flow.py +++ b/tests/components/switchbot/test_config_flow.py @@ -7,6 +7,7 @@ from homeassistant.components.switchbot.const import ( CONF_ENCRYPTION_KEY, CONF_KEY_ID, + CONF_LOCK_NIGHTLATCH, CONF_RETRY_COUNT, ) from homeassistant.config_entries import SOURCE_BLUETOOTH, SOURCE_USER @@ -782,3 +783,65 @@ async def test_options_flow(hass: HomeAssistant) -> None: assert len(mock_setup_entry.mock_calls) == 1 assert entry.options[CONF_RETRY_COUNT] == 6 + + +async def test_options_flow_lock_pro(hass: HomeAssistant) -> None: + """Test updating options.""" + entry = MockConfigEntry( + domain=DOMAIN, + data={ + CONF_ADDRESS: "aa:bb:cc:dd:ee:ff", + CONF_NAME: "test-name", + CONF_PASSWORD: "test-password", + CONF_SENSOR_TYPE: "lock_pro", + }, + options={CONF_RETRY_COUNT: 10}, + unique_id="aabbccddeeff", + ) + entry.add_to_hass(hass) + + # Test Force night_latch should be disabled by default. + with patch_async_setup_entry() as mock_setup_entry: + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + result = await hass.config_entries.options.async_init(entry.entry_id) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "init" + assert result["errors"] is None + + result = await hass.config_entries.options.async_configure( + result["flow_id"], + user_input={ + CONF_RETRY_COUNT: 3, + }, + ) + await hass.async_block_till_done() + + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["data"][CONF_LOCK_NIGHTLATCH] is False + + assert len(mock_setup_entry.mock_calls) == 1 + + # Test Set force night_latch to be enabled. + + with patch_async_setup_entry() as mock_setup_entry: + result = await hass.config_entries.options.async_init(entry.entry_id) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "init" + assert result["errors"] is None + + result = await hass.config_entries.options.async_configure( + result["flow_id"], + user_input={ + CONF_LOCK_NIGHTLATCH: True, + }, + ) + await hass.async_block_till_done() + + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["data"][CONF_LOCK_NIGHTLATCH] is True + + assert len(mock_setup_entry.mock_calls) == 0 + + assert entry.options[CONF_LOCK_NIGHTLATCH] is True diff --git a/tests/components/synology_dsm/test_config_flow.py b/tests/components/synology_dsm/test_config_flow.py index 1574526a7014c8..e5494b7179f5d0 100644 --- a/tests/components/synology_dsm/test_config_flow.py +++ b/tests/components/synology_dsm/test_config_flow.py @@ -21,12 +21,7 @@ DEFAULT_SNAPSHOT_QUALITY, DOMAIN, ) -from homeassistant.config_entries import ( - SOURCE_REAUTH, - SOURCE_SSDP, - SOURCE_USER, - SOURCE_ZEROCONF, -) +from homeassistant.config_entries import SOURCE_SSDP, SOURCE_USER, SOURCE_ZEROCONF from homeassistant.const import ( CONF_HOST, CONF_MAC, @@ -297,24 +292,7 @@ async def test_reauth(hass: HomeAssistant, service: MagicMock) -> None: ) entry.add_to_hass(hass) - with patch( - "homeassistant.config_entries.ConfigEntries.async_reload", - return_value=True, - ): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "entry_id": entry.entry_id, - "unique_id": entry.unique_id, - "title_placeholders": {"name": entry.title}, - }, - data={ - CONF_HOST: HOST, - CONF_USERNAME: USERNAME, - CONF_PASSWORD: PASSWORD, - }, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/tailscale/test_config_flow.py b/tests/components/tailscale/test_config_flow.py index 86daa40d8dcb87..3a67f46a49629b 100644 --- a/tests/components/tailscale/test_config_flow.py +++ b/tests/components/tailscale/test_config_flow.py @@ -5,7 +5,7 @@ from tailscale import TailscaleAuthenticationError, TailscaleConnectionError from homeassistant.components.tailscale.const import CONF_TAILNET, DOMAIN -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_USER from homeassistant.const import CONF_API_KEY from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -128,15 +128,7 @@ async def test_reauth_flow( """Test the reauthentication configuration flow.""" mock_config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": mock_config_entry.unique_id, - "entry_id": mock_config_entry.entry_id, - }, - data=mock_config_entry.data, - ) + result = await mock_config_entry.start_reauth_flow(hass) assert result.get("type") is FlowResultType.FORM assert result.get("step_id") == "reauth_confirm" @@ -170,15 +162,7 @@ async def test_reauth_with_authentication_error( """ mock_config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": mock_config_entry.unique_id, - "entry_id": mock_config_entry.entry_id, - }, - data=mock_config_entry.data, - ) + result = await mock_config_entry.start_reauth_flow(hass) assert result.get("type") is FlowResultType.FORM assert result.get("step_id") == "reauth_confirm" @@ -222,15 +206,7 @@ async def test_reauth_api_error( """Test API error during reauthentication.""" mock_config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": mock_config_entry.unique_id, - "entry_id": mock_config_entry.entry_id, - }, - data=mock_config_entry.data, - ) + result = await mock_config_entry.start_reauth_flow(hass) assert result.get("type") is FlowResultType.FORM assert result.get("step_id") == "reauth_confirm" diff --git a/tests/components/tailwind/test_config_flow.py b/tests/components/tailwind/test_config_flow.py index f70ab6e27ff352..d2d1517271851f 100644 --- a/tests/components/tailwind/test_config_flow.py +++ b/tests/components/tailwind/test_config_flow.py @@ -14,12 +14,7 @@ from homeassistant.components import zeroconf from homeassistant.components.dhcp import DhcpServiceInfo from homeassistant.components.tailwind.const import DOMAIN -from homeassistant.config_entries import ( - SOURCE_DHCP, - SOURCE_REAUTH, - SOURCE_USER, - SOURCE_ZEROCONF, -) +from homeassistant.config_entries import SOURCE_DHCP, SOURCE_USER, SOURCE_ZEROCONF from homeassistant.const import CONF_HOST, CONF_TOKEN from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -311,15 +306,7 @@ async def test_reauth_flow( mock_config_entry.add_to_hass(hass) assert mock_config_entry.data[CONF_TOKEN] == "123456" - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": mock_config_entry.unique_id, - "entry_id": mock_config_entry.entry_id, - }, - data=mock_config_entry.data, - ) + result = await mock_config_entry.start_reauth_flow(hass) assert result.get("type") is FlowResultType.FORM assert result.get("step_id") == "reauth_confirm" @@ -354,15 +341,7 @@ async def test_reauth_flow_errors( mock_config_entry.add_to_hass(hass) mock_tailwind.status.side_effect = side_effect - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": mock_config_entry.unique_id, - "entry_id": mock_config_entry.entry_id, - }, - data=mock_config_entry.data, - ) + result = await mock_config_entry.start_reauth_flow(hass) result2 = await hass.config_entries.flow.async_configure( result["flow_id"], diff --git a/tests/components/tankerkoenig/test_config_flow.py b/tests/components/tankerkoenig/test_config_flow.py index 022b49fd3f8740..bb1e943bbb994d 100644 --- a/tests/components/tankerkoenig/test_config_flow.py +++ b/tests/components/tankerkoenig/test_config_flow.py @@ -9,7 +9,7 @@ CONF_STATIONS, DOMAIN, ) -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_USER from homeassistant.const import ( CONF_API_KEY, CONF_LATITUDE, @@ -162,6 +162,10 @@ async def test_user_no_stations(hass: HomeAssistant) -> None: async def test_reauth(hass: HomeAssistant, config_entry: MockConfigEntry) -> None: """Test starting a flow by user to re-auth.""" config_entry.add_to_hass(hass) + # re-auth initialized + result = await config_entry.start_reauth_flow(hass) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reauth_confirm" with ( patch( @@ -171,15 +175,6 @@ async def test_reauth(hass: HomeAssistant, config_entry: MockConfigEntry) -> Non "homeassistant.components.tankerkoenig.config_flow.Tankerkoenig.nearby_stations", ) as mock_nearby_stations, ): - # re-auth initialized - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH, "entry_id": config_entry.entry_id}, - data=config_entry.data, - ) - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "reauth_confirm" - # re-auth unsuccessful mock_nearby_stations.side_effect = TankerkoenigInvalidKeyError("Booom!") result = await hass.config_entries.flow.async_configure( diff --git a/tests/components/tautulli/test_config_flow.py b/tests/components/tautulli/test_config_flow.py index ca563cfad77cb6..722fd0a7616406 100644 --- a/tests/components/tautulli/test_config_flow.py +++ b/tests/components/tautulli/test_config_flow.py @@ -5,7 +5,7 @@ from pytautulli import exceptions from homeassistant.components.tautulli.const import DOMAIN -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_USER from homeassistant.const import CONF_API_KEY, CONF_SOURCE, CONF_URL, CONF_VERIFY_SSL from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -156,15 +156,7 @@ async def test_flow_reauth( """Test reauth flow.""" with patch("homeassistant.components.tautulli.PLATFORMS", []): entry = await setup_integration(hass, aioclient_mock) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - CONF_SOURCE: SOURCE_REAUTH, - "entry_id": entry.entry_id, - "unique_id": entry.unique_id, - }, - data=CONF_DATA, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" assert result["errors"] == {} @@ -193,14 +185,7 @@ async def test_flow_reauth_error( """Test reauth flow with invalid authentication.""" with patch("homeassistant.components.tautulli.PLATFORMS", []): entry = await setup_integration(hass, aioclient_mock) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "entry_id": entry.entry_id, - "unique_id": entry.unique_id, - }, - ) + result = await entry.start_reauth_flow(hass) with patch_config_flow_tautulli(AsyncMock()) as tautullimock: tautullimock.side_effect = exceptions.PyTautulliAuthenticationException result = await hass.config_entries.flow.async_configure( diff --git a/tests/components/technove/fixtures/station_charging.json b/tests/components/technove/fixtures/station_charging.json index 63e68d0db0e866..4f50bf1a6459cd 100644 --- a/tests/components/technove/fixtures/station_charging.json +++ b/tests/components/technove/fixtures/station_charging.json @@ -6,7 +6,7 @@ "current": 23.75, "network_ssid": "Connecting...", "id": "AA:AA:AA:AA:AA:BB", - "auto_charge": true, + "auto_charge": false, "highChargePeriodActive": false, "normalPeriodActive": false, "maxChargePourcentage": 0.9, diff --git a/tests/components/technove/snapshots/test_diagnostics.ambr b/tests/components/technove/snapshots/test_diagnostics.ambr index 2e81f124ba561e..175e8f2022a27d 100644 --- a/tests/components/technove/snapshots/test_diagnostics.ambr +++ b/tests/components/technove/snapshots/test_diagnostics.ambr @@ -1,7 +1,7 @@ # serializer version: 1 # name: test_diagnostics dict({ - 'auto_charge': True, + 'auto_charge': False, 'conflict_in_sharing_config': False, 'current': 23.75, 'energy_session': 12.34, diff --git a/tests/components/technove/snapshots/test_switch.ambr b/tests/components/technove/snapshots/test_switch.ambr index 1a707971fc8c68..6febc8c768c9a6 100644 --- a/tests/components/technove/snapshots/test_switch.ambr +++ b/tests/components/technove/snapshots/test_switch.ambr @@ -42,6 +42,52 @@ 'last_changed': , 'last_reported': , 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_switches[switch.technove_station_charging_enabled-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': , + 'entity_id': 'switch.technove_station_charging_enabled', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Charging Enabled', + 'platform': 'technove', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'session_active', + 'unique_id': 'AA:AA:AA:AA:AA:BB_session_active', + 'unit_of_measurement': None, + }) +# --- +# name: test_switches[switch.technove_station_charging_enabled-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'TechnoVE Station Charging Enabled', + }), + 'context': , + 'entity_id': 'switch.technove_station_charging_enabled', + 'last_changed': , + 'last_reported': , + 'last_updated': , 'state': 'on', }) # --- diff --git a/tests/components/technove/test_binary_sensor.py b/tests/components/technove/test_binary_sensor.py index 0ee4f3f3db7a8f..0a90093779e796 100644 --- a/tests/components/technove/test_binary_sensor.py +++ b/tests/components/technove/test_binary_sensor.py @@ -8,7 +8,7 @@ from syrupy import SnapshotAssertion from technove import TechnoVEError -from homeassistant.const import STATE_ON, STATE_UNAVAILABLE, Platform +from homeassistant.const import STATE_OFF, STATE_UNAVAILABLE, Platform from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er @@ -43,7 +43,10 @@ async def test_sensors( @pytest.mark.parametrize( "entity_id", - ["binary_sensor.technove_station_static_ip"], + [ + "binary_sensor.technove_station_static_ip", + "binary_sensor.technove_station_charging", + ], ) @pytest.mark.usefixtures("init_integration") async def test_disabled_by_default_binary_sensors( @@ -64,9 +67,9 @@ async def test_binary_sensor_update_failure( freezer: FrozenDateTimeFactory, ) -> None: """Test coordinator update failure.""" - entity_id = "binary_sensor.technove_station_charging" + entity_id = "binary_sensor.technove_station_power_sharing_mode" - assert hass.states.get(entity_id).state == STATE_ON + assert hass.states.get(entity_id).state == STATE_OFF mock_technove.update.side_effect = TechnoVEError("Test error") freezer.tick(timedelta(minutes=5, seconds=1)) diff --git a/tests/components/technove/test_switch.py b/tests/components/technove/test_switch.py index b1a66607f66049..dc0293b6443ee7 100644 --- a/tests/components/technove/test_switch.py +++ b/tests/components/technove/test_switch.py @@ -15,7 +15,7 @@ Platform, ) from homeassistant.core import HomeAssistant -from homeassistant.exceptions import HomeAssistantError +from homeassistant.exceptions import HomeAssistantError, ServiceValidationError from homeassistant.helpers import entity_registry as er from . import setup_with_selected_platforms @@ -53,6 +53,12 @@ async def test_switches( {"enabled": True}, {"enabled": False}, ), + ( + "switch.technove_station_charging_enabled", + "set_charging_enabled", + {"enabled": True}, + {"enabled": False}, + ), ], ) @pytest.mark.usefixtures("init_integration") @@ -96,6 +102,10 @@ async def test_switch_on_off( "switch.technove_station_auto_charge", "set_auto_charge", ), + ( + "switch.technove_station_charging_enabled", + "set_charging_enabled", + ), ], ) @pytest.mark.usefixtures("init_integration") @@ -130,6 +140,10 @@ async def test_invalid_response( "switch.technove_station_auto_charge", "set_auto_charge", ), + ( + "switch.technove_station_charging_enabled", + "set_charging_enabled", + ), ], ) @pytest.mark.usefixtures("init_integration") @@ -157,3 +171,31 @@ async def test_connection_error( assert method_mock.call_count == 1 assert (state := hass.states.get(state.entity_id)) assert state.state == STATE_UNAVAILABLE + + +@pytest.mark.usefixtures("init_integration") +async def test_disable_charging_auto_charge( + hass: HomeAssistant, + mock_technove: MagicMock, +) -> None: + """Test failure to disable charging when the station is in auto charge mode.""" + entity_id = "switch.technove_station_charging_enabled" + state = hass.states.get(entity_id) + + # Enable auto-charge mode + device = mock_technove.update.return_value + device.info.auto_charge = True + + with pytest.raises( + ServiceValidationError, + match="auto-charge is enabled", + ): + await hass.services.async_call( + SWITCH_DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: entity_id}, + blocking=True, + ) + + assert (state := hass.states.get(state.entity_id)) + assert state.state != STATE_UNAVAILABLE diff --git a/tests/components/tedee/test_config_flow.py b/tests/components/tedee/test_config_flow.py index d5dc5d4efcfe7f..0fa3d62c26e6ae 100644 --- a/tests/components/tedee/test_config_flow.py +++ b/tests/components/tedee/test_config_flow.py @@ -10,7 +10,7 @@ import pytest from homeassistant.components.tedee.const import CONF_LOCAL_ACCESS_TOKEN, DOMAIN -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_RECONFIGURE, SOURCE_USER +from homeassistant.config_entries import SOURCE_RECONFIGURE, SOURCE_USER from homeassistant.const import CONF_HOST, CONF_WEBHOOK_ID from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -122,18 +122,7 @@ async def test_reauth_flow( mock_config_entry.add_to_hass(hass) - reauth_result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": mock_config_entry.unique_id, - "entry_id": mock_config_entry.entry_id, - }, - data={ - CONF_LOCAL_ACCESS_TOKEN: LOCAL_ACCESS_TOKEN, - CONF_HOST: "192.168.1.42", - }, - ) + reauth_result = await mock_config_entry.start_reauth_flow(hass) result = await hass.config_entries.flow.async_configure( reauth_result["flow_id"], diff --git a/tests/components/telegram_bot/test_telegram_bot.py b/tests/components/telegram_bot/test_telegram_bot.py index aad758827ca294..bdf6ba72fccf79 100644 --- a/tests/components/telegram_bot/test_telegram_bot.py +++ b/tests/components/telegram_bot/test_telegram_bot.py @@ -1,8 +1,11 @@ """Tests for the telegram_bot component.""" -from unittest.mock import AsyncMock, patch +from typing import Any +from unittest.mock import AsyncMock, MagicMock, patch +import pytest from telegram import Update +from telegram.error import NetworkError, RetryAfter, TelegramError, TimedOut from homeassistant.components.telegram_bot import ( ATTR_MESSAGE, @@ -11,6 +14,7 @@ SERVICE_SEND_MESSAGE, ) from homeassistant.components.telegram_bot.webhooks import TELEGRAM_WEBHOOK_URL +from homeassistant.const import EVENT_HOMEASSISTANT_START from homeassistant.core import Context, HomeAssistant from homeassistant.setup import async_setup_component @@ -188,6 +192,103 @@ async def test_polling_platform_message_text_update( assert isinstance(events[0].context, Context) +@pytest.mark.parametrize( + ("error", "log_message"), + [ + ( + TelegramError("Telegram error"), + 'caused error: "Telegram error"', + ), + (NetworkError("Network error"), ""), + (RetryAfter(42), ""), + (TimedOut("TimedOut error"), ""), + ], +) +async def test_polling_platform_add_error_handler( + hass: HomeAssistant, + config_polling: dict[str, Any], + update_message_text: dict[str, Any], + caplog: pytest.LogCaptureFixture, + error: Exception, + log_message: str, +) -> None: + """Test polling add error handler.""" + with patch( + "homeassistant.components.telegram_bot.polling.ApplicationBuilder" + ) as application_builder_class: + await async_setup_component( + hass, + DOMAIN, + config_polling, + ) + await hass.async_block_till_done() + + application = ( + application_builder_class.return_value.bot.return_value.build.return_value + ) + application.updater.stop = AsyncMock() + application.stop = AsyncMock() + application.shutdown = AsyncMock() + process_error = application.add_error_handler.call_args[0][0] + application.bot.defaults.tzinfo = None + update = Update.de_json(update_message_text, application.bot) + + await process_error(update, MagicMock(error=error)) + + assert log_message in caplog.text + + +@pytest.mark.parametrize( + ("error", "log_message"), + [ + ( + TelegramError("Telegram error"), + "TelegramError: Telegram error", + ), + (NetworkError("Network error"), ""), + (RetryAfter(42), ""), + (TimedOut("TimedOut error"), ""), + ], +) +async def test_polling_platform_start_polling_error_callback( + hass: HomeAssistant, + config_polling: dict[str, Any], + caplog: pytest.LogCaptureFixture, + error: Exception, + log_message: str, +) -> None: + """Test polling add error handler.""" + with patch( + "homeassistant.components.telegram_bot.polling.ApplicationBuilder" + ) as application_builder_class: + await async_setup_component( + hass, + DOMAIN, + config_polling, + ) + await hass.async_block_till_done() + + application = ( + application_builder_class.return_value.bot.return_value.build.return_value + ) + application.initialize = AsyncMock() + application.updater.start_polling = AsyncMock() + application.start = AsyncMock() + application.updater.stop = AsyncMock() + application.stop = AsyncMock() + application.shutdown = AsyncMock() + + hass.bus.async_fire(EVENT_HOMEASSISTANT_START) + await hass.async_block_till_done() + error_callback = application.updater.start_polling.call_args.kwargs[ + "error_callback" + ] + + error_callback(error) + + assert log_message in caplog.text + + async def test_webhook_endpoint_unauthorized_update_doesnt_generate_telegram_text_event( hass: HomeAssistant, webhook_platform, diff --git a/tests/components/template/test_config_flow.py b/tests/components/template/test_config_flow.py index a62370f4261db6..f8ab190e664dae 100644 --- a/tests/components/template/test_config_flow.py +++ b/tests/components/template/test_config_flow.py @@ -101,11 +101,21 @@ "min": "{{ 0 }}", "max": "{{ 100 }}", "step": "{{ 0.1 }}", + "set_value": { + "action": "input_number.set_value", + "target": {"entity_id": "input_number.test"}, + "data": {"value": "{{ value }}"}, + }, }, { "min": "{{ 0 }}", "max": "{{ 100 }}", "step": "{{ 0.1 }}", + "set_value": { + "action": "input_number.set_value", + "target": {"entity_id": "input_number.test"}, + "data": {"value": "{{ value }}"}, + }, }, {}, ), @@ -444,11 +454,21 @@ def get_suggested(schema, key): "min": "{{ 0 }}", "max": "{{ 100 }}", "step": "{{ 0.1 }}", + "set_value": { + "action": "input_number.set_value", + "target": {"entity_id": "input_number.test"}, + "data": {"value": "{{ value }}"}, + }, }, { "min": "{{ 0 }}", "max": "{{ 100 }}", "step": "{{ 0.1 }}", + "set_value": { + "action": "input_number.set_value", + "target": {"entity_id": "input_number.test"}, + "data": {"value": "{{ value }}"}, + }, }, "state", ), diff --git a/tests/components/template/test_init.py b/tests/components/template/test_init.py index 06d59d4d1760fc..3b4db4bf668c6a 100644 --- a/tests/components/template/test_init.py +++ b/tests/components/template/test_init.py @@ -322,12 +322,22 @@ async def async_yaml_patch_helper(hass: HomeAssistant, filename: str) -> None: "min": "{{ 0 }}", "max": "{{ 100 }}", "step": "{{ 0.1 }}", + "set_value": { + "action": "input_number.set_value", + "target": {"entity_id": "input_number.test"}, + "data": {"value": "{{ value }}"}, + }, }, { "state": "{{ 11 }}", "min": "{{ 0 }}", "max": "{{ 100 }}", "step": "{{ 0.1 }}", + "set_value": { + "action": "input_number.set_value", + "target": {"entity_id": "input_number.test"}, + "data": {"value": "{{ value }}"}, + }, }, ), ( diff --git a/tests/components/template/test_number.py b/tests/components/template/test_number.py index c8befc2b8f8515..fdca94d9fa424e 100644 --- a/tests/components/template/test_number.py +++ b/tests/components/template/test_number.py @@ -61,6 +61,11 @@ async def test_setup_config_entry( "min": "{{ 0 }}", "max": "{{ 100 }}", "step": "{{ 0.1 }}", + "set_value": { + "action": "input_number.set_value", + "target": {"entity_id": "input_number.test"}, + "data": {"value": "{{ value }}"}, + }, }, title="My template", ) @@ -522,6 +527,11 @@ async def test_device_id( "min": "{{ 0 }}", "max": "{{ 100 }}", "step": "{{ 0.1 }}", + "set_value": { + "action": "input_number.set_value", + "target": {"entity_id": "input_number.test"}, + "data": {"value": "{{ value }}"}, + }, "device_id": device_entry.id, }, title="My template", diff --git a/tests/components/tesla_fleet/conftest.py b/tests/components/tesla_fleet/conftest.py index 49f0be9cca7f66..cc580212233e58 100644 --- a/tests/components/tesla_fleet/conftest.py +++ b/tests/components/tesla_fleet/conftest.py @@ -9,10 +9,18 @@ import jwt import pytest +from tesla_fleet_api.const import Scope from homeassistant.components.tesla_fleet.const import DOMAIN, SCOPES -from .const import LIVE_STATUS, PRODUCTS, SITE_INFO, VEHICLE_DATA, VEHICLE_ONLINE +from .const import ( + COMMAND_OK, + LIVE_STATUS, + PRODUCTS, + SITE_INFO, + VEHICLE_DATA, + VEHICLE_ONLINE, +) from tests.common import MockConfigEntry @@ -25,16 +33,8 @@ def mock_expires_at() -> int: return time.time() + 3600 -@pytest.fixture(name="scopes") -def mock_scopes() -> list[str]: - """Fixture to set the scopes present in the OAuth token.""" - return SCOPES - - -@pytest.fixture -def normal_config_entry(expires_at: int, scopes: list[str]) -> MockConfigEntry: +def create_config_entry(expires_at: int, scopes: list[Scope]) -> MockConfigEntry: """Create Tesla Fleet entry in Home Assistant.""" - access_token = jwt.encode( { "sub": UID, @@ -64,6 +64,32 @@ def normal_config_entry(expires_at: int, scopes: list[str]) -> MockConfigEntry: ) +@pytest.fixture +def normal_config_entry(expires_at: int) -> MockConfigEntry: + """Create Tesla Fleet entry in Home Assistant.""" + return create_config_entry(expires_at, SCOPES) + + +@pytest.fixture +def noscope_config_entry(expires_at: int) -> MockConfigEntry: + """Create Tesla Fleet entry in Home Assistant without scopes.""" + return create_config_entry(expires_at, [Scope.OPENID, Scope.OFFLINE_ACCESS]) + + +@pytest.fixture +def readonly_config_entry(expires_at: int) -> MockConfigEntry: + """Create Tesla Fleet entry in Home Assistant without scopes.""" + return create_config_entry( + expires_at, + [ + Scope.OPENID, + Scope.OFFLINE_ACCESS, + Scope.VEHICLE_DEVICE_DATA, + Scope.ENERGY_DEVICE_DATA, + ], + ) + + @pytest.fixture(autouse=True) def mock_products() -> Generator[AsyncMock]: """Mock Tesla Fleet Api products method.""" @@ -124,10 +150,20 @@ def mock_site_info() -> Generator[AsyncMock]: yield mock_live_status -@pytest.fixture(autouse=True) +@pytest.fixture def mock_find_server() -> Generator[AsyncMock]: """Mock Tesla Fleet find server method.""" with patch( "homeassistant.components.tesla_fleet.TeslaFleetApi.find_server", ) as mock_find_server: yield mock_find_server + + +@pytest.fixture +def mock_request(): + """Mock all Tesla Fleet API requests.""" + with patch( + "homeassistant.components.tesla_fleet.TeslaFleetApi._request", + return_value=COMMAND_OK, + ) as mock_request: + yield mock_request diff --git a/tests/components/tesla_fleet/snapshots/test_climate.ambr b/tests/components/tesla_fleet/snapshots/test_climate.ambr new file mode 100644 index 00000000000000..696f8c37f08fae --- /dev/null +++ b/tests/components/tesla_fleet/snapshots/test_climate.ambr @@ -0,0 +1,422 @@ +# serializer version: 1 +# name: test_climate[climate.test_cabin_overheat_protection-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'hvac_modes': list([ + , + , + , + ]), + 'max_temp': 40, + 'min_temp': 30, + 'target_temp_step': 5, + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'climate', + 'entity_category': None, + 'entity_id': 'climate.test_cabin_overheat_protection', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Cabin overheat protection', + 'platform': 'tesla_fleet', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': 'climate_state_cabin_overheat_protection', + 'unique_id': 'LRWXF7EK4KC700000-climate_state_cabin_overheat_protection', + 'unit_of_measurement': None, + }) +# --- +# name: test_climate[climate.test_cabin_overheat_protection-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'current_temperature': 30, + 'friendly_name': 'Test Cabin overheat protection', + 'hvac_modes': list([ + , + , + , + ]), + 'max_temp': 40, + 'min_temp': 30, + 'supported_features': , + 'target_temp_step': 5, + 'temperature': 40, + }), + 'context': , + 'entity_id': 'climate.test_cabin_overheat_protection', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'cool', + }) +# --- +# name: test_climate[climate.test_climate-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'hvac_modes': list([ + , + , + ]), + 'max_temp': 28.0, + 'min_temp': 15.0, + 'preset_modes': list([ + 'off', + 'keep', + 'dog', + 'camp', + ]), + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'climate', + 'entity_category': None, + 'entity_id': 'climate.test_climate', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Climate', + 'platform': 'tesla_fleet', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': , + 'unique_id': 'LRWXF7EK4KC700000-driver_temp', + 'unit_of_measurement': None, + }) +# --- +# name: test_climate[climate.test_climate-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'current_temperature': 30.0, + 'friendly_name': 'Test Climate', + 'hvac_modes': list([ + , + , + ]), + 'max_temp': 28.0, + 'min_temp': 15.0, + 'preset_mode': 'keep', + 'preset_modes': list([ + 'off', + 'keep', + 'dog', + 'camp', + ]), + 'supported_features': , + 'temperature': 22.0, + }), + 'context': , + 'entity_id': 'climate.test_climate', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'heat_cool', + }) +# --- +# name: test_climate_alt[climate.test_cabin_overheat_protection-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'hvac_modes': list([ + , + , + , + ]), + 'max_temp': 40, + 'min_temp': 30, + 'target_temp_step': 5, + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'climate', + 'entity_category': None, + 'entity_id': 'climate.test_cabin_overheat_protection', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Cabin overheat protection', + 'platform': 'tesla_fleet', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': 'climate_state_cabin_overheat_protection', + 'unique_id': 'LRWXF7EK4KC700000-climate_state_cabin_overheat_protection', + 'unit_of_measurement': None, + }) +# --- +# name: test_climate_alt[climate.test_cabin_overheat_protection-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'current_temperature': 30, + 'friendly_name': 'Test Cabin overheat protection', + 'hvac_modes': list([ + , + , + , + ]), + 'max_temp': 40, + 'min_temp': 30, + 'supported_features': , + 'target_temp_step': 5, + }), + 'context': , + 'entity_id': 'climate.test_cabin_overheat_protection', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_climate_alt[climate.test_climate-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'hvac_modes': list([ + , + , + ]), + 'max_temp': 28.0, + 'min_temp': 15.0, + 'preset_modes': list([ + 'off', + 'keep', + 'dog', + 'camp', + ]), + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'climate', + 'entity_category': None, + 'entity_id': 'climate.test_climate', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Climate', + 'platform': 'tesla_fleet', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': , + 'unique_id': 'LRWXF7EK4KC700000-driver_temp', + 'unit_of_measurement': None, + }) +# --- +# name: test_climate_alt[climate.test_climate-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'current_temperature': 30.0, + 'friendly_name': 'Test Climate', + 'hvac_modes': list([ + , + , + ]), + 'max_temp': 28.0, + 'min_temp': 15.0, + 'preset_mode': 'off', + 'preset_modes': list([ + 'off', + 'keep', + 'dog', + 'camp', + ]), + 'supported_features': , + 'temperature': 22.0, + }), + 'context': , + 'entity_id': 'climate.test_climate', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_climate_offline[climate.test_cabin_overheat_protection-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'hvac_modes': list([ + , + , + , + ]), + 'max_temp': 40, + 'min_temp': 30, + 'target_temp_step': 5, + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'climate', + 'entity_category': None, + 'entity_id': 'climate.test_cabin_overheat_protection', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Cabin overheat protection', + 'platform': 'tesla_fleet', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': 'climate_state_cabin_overheat_protection', + 'unique_id': 'LRWXF7EK4KC700000-climate_state_cabin_overheat_protection', + 'unit_of_measurement': None, + }) +# --- +# name: test_climate_offline[climate.test_cabin_overheat_protection-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'current_temperature': None, + 'friendly_name': 'Test Cabin overheat protection', + 'hvac_modes': list([ + , + , + , + ]), + 'max_temp': 40, + 'min_temp': 30, + 'supported_features': , + 'target_temp_step': 5, + }), + 'context': , + 'entity_id': 'climate.test_cabin_overheat_protection', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'unknown', + }) +# --- +# name: test_climate_offline[climate.test_climate-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'hvac_modes': list([ + , + , + ]), + 'max_temp': 28.0, + 'min_temp': 15.0, + 'preset_modes': list([ + 'off', + 'keep', + 'dog', + 'camp', + ]), + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'climate', + 'entity_category': None, + 'entity_id': 'climate.test_climate', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Climate', + 'platform': 'tesla_fleet', + 'previous_unique_id': None, + 'supported_features': , + 'translation_key': , + 'unique_id': 'LRWXF7EK4KC700000-driver_temp', + 'unit_of_measurement': None, + }) +# --- +# name: test_climate_offline[climate.test_climate-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'current_temperature': None, + 'friendly_name': 'Test Climate', + 'hvac_modes': list([ + , + , + ]), + 'max_temp': 28.0, + 'min_temp': 15.0, + 'preset_mode': None, + 'preset_modes': list([ + 'off', + 'keep', + 'dog', + 'camp', + ]), + 'supported_features': , + 'temperature': None, + }), + 'context': , + 'entity_id': 'climate.test_climate', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'unknown', + }) +# --- diff --git a/tests/components/tesla_fleet/test_climate.py b/tests/components/tesla_fleet/test_climate.py new file mode 100644 index 00000000000000..902faaba922860 --- /dev/null +++ b/tests/components/tesla_fleet/test_climate.py @@ -0,0 +1,450 @@ +"""Test the Tesla Fleet climate platform.""" + +from unittest.mock import AsyncMock, patch + +from freezegun.api import FrozenDateTimeFactory +import pytest +from syrupy.assertion import SnapshotAssertion +from tesla_fleet_api.exceptions import InvalidCommand, VehicleOffline + +from homeassistant.components.climate import ( + ATTR_HVAC_MODE, + ATTR_PRESET_MODE, + ATTR_TARGET_TEMP_HIGH, + ATTR_TARGET_TEMP_LOW, + ATTR_TEMPERATURE, + DOMAIN as CLIMATE_DOMAIN, + SERVICE_SET_HVAC_MODE, + SERVICE_SET_PRESET_MODE, + SERVICE_SET_TEMPERATURE, + SERVICE_TURN_OFF, + SERVICE_TURN_ON, + HVACMode, +) +from homeassistant.components.tesla_fleet.coordinator import VEHICLE_INTERVAL +from homeassistant.const import ATTR_ENTITY_ID, Platform +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError, ServiceValidationError +from homeassistant.helpers import entity_registry as er + +from . import assert_entities, setup_platform +from .const import ( + COMMAND_ERRORS, + COMMAND_IGNORED_REASON, + VEHICLE_ASLEEP, + VEHICLE_DATA_ALT, + VEHICLE_ONLINE, +) + +from tests.common import MockConfigEntry, async_fire_time_changed + + +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_climate( + hass: HomeAssistant, + snapshot: SnapshotAssertion, + entity_registry: er.EntityRegistry, + normal_config_entry: MockConfigEntry, +) -> None: + """Tests that the climate entities are correct.""" + + await setup_platform(hass, normal_config_entry, [Platform.CLIMATE]) + assert_entities(hass, normal_config_entry.entry_id, entity_registry, snapshot) + + +async def test_climate_services( + hass: HomeAssistant, + snapshot: SnapshotAssertion, + entity_registry: er.EntityRegistry, + normal_config_entry: MockConfigEntry, + mock_request: AsyncMock, +) -> None: + """Tests that the climate services work.""" + + await setup_platform(hass, normal_config_entry, [Platform.CLIMATE]) + entity_id = "climate.test_climate" + + # Turn On and Set Temp + await hass.services.async_call( + CLIMATE_DOMAIN, + SERVICE_SET_TEMPERATURE, + { + ATTR_ENTITY_ID: [entity_id], + ATTR_TEMPERATURE: 20, + ATTR_HVAC_MODE: HVACMode.HEAT_COOL, + }, + blocking=True, + ) + state = hass.states.get(entity_id) + assert state.attributes[ATTR_TEMPERATURE] == 20 + assert state.state == HVACMode.HEAT_COOL + + # Set Temp + await hass.services.async_call( + CLIMATE_DOMAIN, + SERVICE_SET_TEMPERATURE, + { + ATTR_ENTITY_ID: [entity_id], + ATTR_TEMPERATURE: 21, + }, + blocking=True, + ) + state = hass.states.get(entity_id) + assert state.attributes[ATTR_TEMPERATURE] == 21 + + # Set Preset + await hass.services.async_call( + CLIMATE_DOMAIN, + SERVICE_SET_PRESET_MODE, + {ATTR_ENTITY_ID: [entity_id], ATTR_PRESET_MODE: "keep"}, + blocking=True, + ) + state = hass.states.get(entity_id) + assert state.attributes[ATTR_PRESET_MODE] == "keep" + + # Set Preset + await hass.services.async_call( + CLIMATE_DOMAIN, + SERVICE_SET_PRESET_MODE, + {ATTR_ENTITY_ID: [entity_id], ATTR_PRESET_MODE: "off"}, + blocking=True, + ) + state = hass.states.get(entity_id) + assert state.attributes[ATTR_PRESET_MODE] == "off" + + # Turn Off + await hass.services.async_call( + CLIMATE_DOMAIN, + SERVICE_SET_HVAC_MODE, + {ATTR_ENTITY_ID: [entity_id], ATTR_HVAC_MODE: HVACMode.OFF}, + blocking=True, + ) + state = hass.states.get(entity_id) + assert state.state == HVACMode.OFF + + +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_climate_overheat_protection_services( + hass: HomeAssistant, + snapshot: SnapshotAssertion, + entity_registry: er.EntityRegistry, + normal_config_entry: MockConfigEntry, + mock_request: AsyncMock, +) -> None: + """Tests that the climate overheat protection services work.""" + + await setup_platform(hass, normal_config_entry, [Platform.CLIMATE]) + entity_id = "climate.test_cabin_overheat_protection" + + # Turn On and Set Low + await hass.services.async_call( + CLIMATE_DOMAIN, + SERVICE_SET_TEMPERATURE, + { + ATTR_ENTITY_ID: [entity_id], + ATTR_TEMPERATURE: 30, + ATTR_HVAC_MODE: HVACMode.FAN_ONLY, + }, + blocking=True, + ) + state = hass.states.get(entity_id) + assert state.attributes[ATTR_TEMPERATURE] == 30 + assert state.state == HVACMode.FAN_ONLY + + # Set Temp Medium + await hass.services.async_call( + CLIMATE_DOMAIN, + SERVICE_SET_TEMPERATURE, + { + ATTR_ENTITY_ID: [entity_id], + ATTR_TEMPERATURE: 35, + }, + blocking=True, + ) + state = hass.states.get(entity_id) + assert state.attributes[ATTR_TEMPERATURE] == 35 + + # Set Temp High + await hass.services.async_call( + CLIMATE_DOMAIN, + SERVICE_SET_TEMPERATURE, + { + ATTR_ENTITY_ID: [entity_id], + ATTR_TEMPERATURE: 40, + }, + blocking=True, + ) + state = hass.states.get(entity_id) + assert state.attributes[ATTR_TEMPERATURE] == 40 + + # Turn Off + await hass.services.async_call( + CLIMATE_DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: [entity_id]}, + blocking=True, + ) + state = hass.states.get(entity_id) + assert state.state == HVACMode.OFF + + # Turn On + await hass.services.async_call( + CLIMATE_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: [entity_id]}, + blocking=True, + ) + state = hass.states.get(entity_id) + assert state.state == HVACMode.COOL + + # Call set temp with invalid temperature + with pytest.raises( + ServiceValidationError, + match="Cabin overheat protection does not support that temperature", + ): + # Invalid Temp + await hass.services.async_call( + CLIMATE_DOMAIN, + SERVICE_SET_TEMPERATURE, + {ATTR_ENTITY_ID: [entity_id], ATTR_TEMPERATURE: 34}, + blocking=True, + ) + + +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_climate_alt( + hass: HomeAssistant, + snapshot: SnapshotAssertion, + entity_registry: er.EntityRegistry, + mock_vehicle_data: AsyncMock, + normal_config_entry: MockConfigEntry, +) -> None: + """Tests that the climate entity is correct.""" + + mock_vehicle_data.return_value = VEHICLE_DATA_ALT + await setup_platform(hass, normal_config_entry, [Platform.CLIMATE]) + assert_entities(hass, normal_config_entry.entry_id, entity_registry, snapshot) + + +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_climate_offline( + hass: HomeAssistant, + snapshot: SnapshotAssertion, + entity_registry: er.EntityRegistry, + mock_vehicle_data: AsyncMock, + normal_config_entry: MockConfigEntry, +) -> None: + """Tests that the climate entity is correct.""" + + mock_vehicle_data.side_effect = VehicleOffline + await setup_platform(hass, normal_config_entry, [Platform.CLIMATE]) + assert_entities(hass, normal_config_entry.entry_id, entity_registry, snapshot) + + +async def test_invalid_error( + hass: HomeAssistant, + normal_config_entry: MockConfigEntry, +) -> None: + """Tests service error is handled.""" + + await setup_platform(hass, normal_config_entry, platforms=[Platform.CLIMATE]) + entity_id = "climate.test_climate" + + with ( + patch( + "homeassistant.components.tesla_fleet.VehicleSpecific.auto_conditioning_start", + side_effect=InvalidCommand, + ) as mock_on, + pytest.raises( + HomeAssistantError, + match="Command failed: The data request or command is unknown.", + ), + ): + await hass.services.async_call( + CLIMATE_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: [entity_id]}, + blocking=True, + ) + mock_on.assert_called_once() + + +@pytest.mark.parametrize("response", COMMAND_ERRORS) +async def test_errors( + hass: HomeAssistant, response: str, normal_config_entry: MockConfigEntry +) -> None: + """Tests service reason is handled.""" + + await setup_platform(hass, normal_config_entry, [Platform.CLIMATE]) + entity_id = "climate.test_climate" + + with ( + patch( + "homeassistant.components.tesla_fleet.VehicleSpecific.auto_conditioning_start", + return_value=response, + ) as mock_on, + pytest.raises(HomeAssistantError), + ): + await hass.services.async_call( + CLIMATE_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: [entity_id]}, + blocking=True, + ) + mock_on.assert_called_once() + + +async def test_ignored_error( + hass: HomeAssistant, + normal_config_entry: MockConfigEntry, +) -> None: + """Tests ignored error is handled.""" + + await setup_platform(hass, normal_config_entry, [Platform.CLIMATE]) + entity_id = "climate.test_climate" + with patch( + "homeassistant.components.tesla_fleet.VehicleSpecific.auto_conditioning_start", + return_value=COMMAND_IGNORED_REASON, + ) as mock_on: + await hass.services.async_call( + CLIMATE_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: [entity_id]}, + blocking=True, + ) + mock_on.assert_called_once() + + +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_asleep_or_offline( + hass: HomeAssistant, + mock_vehicle_data: AsyncMock, + mock_wake_up: AsyncMock, + mock_vehicle_state: AsyncMock, + freezer: FrozenDateTimeFactory, + normal_config_entry: MockConfigEntry, + mock_request: AsyncMock, +) -> None: + """Tests asleep is handled.""" + + await setup_platform(hass, normal_config_entry, [Platform.CLIMATE]) + entity_id = "climate.test_climate" + mock_vehicle_data.assert_called_once() + + # Put the vehicle alseep + mock_vehicle_data.reset_mock() + mock_vehicle_data.side_effect = VehicleOffline + freezer.tick(VEHICLE_INTERVAL) + async_fire_time_changed(hass) + await hass.async_block_till_done() + mock_vehicle_data.assert_called_once() + mock_wake_up.reset_mock() + + # Run a command but fail trying to wake up the vehicle + mock_wake_up.side_effect = InvalidCommand + with pytest.raises( + HomeAssistantError, match="The data request or command is unknown." + ): + await hass.services.async_call( + CLIMATE_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: [entity_id]}, + blocking=True, + ) + mock_wake_up.assert_called_once() + + mock_wake_up.side_effect = None + mock_wake_up.reset_mock() + + # Run a command but timeout trying to wake up the vehicle + mock_wake_up.return_value = VEHICLE_ASLEEP + mock_vehicle_state.return_value = VEHICLE_ASLEEP + with ( + patch("homeassistant.components.tesla_fleet.helpers.asyncio.sleep"), + pytest.raises(HomeAssistantError, match="Could not wake up vehicle"), + ): + await hass.services.async_call( + CLIMATE_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: [entity_id]}, + blocking=True, + ) + mock_wake_up.assert_called_once() + mock_vehicle_state.assert_called() + + mock_wake_up.reset_mock() + mock_vehicle_state.reset_mock() + mock_wake_up.return_value = VEHICLE_ONLINE + mock_vehicle_state.return_value = VEHICLE_ONLINE + + # Run a command and wake up the vehicle immediately + await hass.services.async_call( + CLIMATE_DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: [entity_id]}, blocking=True + ) + await hass.async_block_till_done() + mock_wake_up.assert_called_once() + + +async def test_climate_noscope( + hass: HomeAssistant, + readonly_config_entry: MockConfigEntry, + snapshot: SnapshotAssertion, +) -> None: + """Tests with no command scopes.""" + await setup_platform(hass, readonly_config_entry, [Platform.CLIMATE]) + entity_id = "climate.test_climate" + + with pytest.raises( + ServiceValidationError, match="Climate mode off is not supported" + ): + await hass.services.async_call( + CLIMATE_DOMAIN, + SERVICE_SET_HVAC_MODE, + {ATTR_ENTITY_ID: [entity_id], ATTR_HVAC_MODE: HVACMode.OFF}, + blocking=True, + ) + + with pytest.raises( + HomeAssistantError, + match="Entity climate.test_climate does not support this service.", + ): + await hass.services.async_call( + CLIMATE_DOMAIN, + SERVICE_SET_TEMPERATURE, + {ATTR_ENTITY_ID: [entity_id], ATTR_TEMPERATURE: 20}, + blocking=True, + ) + + +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +@pytest.mark.parametrize( + ("entity_id", "high", "low"), + [ + ("climate.test_climate", 16, 28), + ("climate.test_cabin_overheat_protection", 30, 40), + ], +) +async def test_climate_notemp( + hass: HomeAssistant, + normal_config_entry: MockConfigEntry, + entity_id: str, + high: int, + low: int, +) -> None: + """Tests that set temp fails without a temp attribute.""" + + await setup_platform(hass, normal_config_entry, [Platform.CLIMATE]) + + with pytest.raises( + ServiceValidationError, match="Temperature is required for this action" + ): + await hass.services.async_call( + CLIMATE_DOMAIN, + SERVICE_SET_TEMPERATURE, + { + ATTR_ENTITY_ID: [entity_id], + ATTR_TARGET_TEMP_HIGH: high, + ATTR_TARGET_TEMP_LOW: low, + }, + blocking=True, + ) diff --git a/tests/components/tesla_fleet/test_config_flow.py b/tests/components/tesla_fleet/test_config_flow.py index 81ba92f1e9c112..b49e090cd5d9e1 100644 --- a/tests/components/tesla_fleet/test_config_flow.py +++ b/tests/components/tesla_fleet/test_config_flow.py @@ -16,7 +16,7 @@ SCOPES, TOKEN_URL, ) -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_USER from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType from homeassistant.helpers import config_entry_oauth2_flow @@ -211,15 +211,7 @@ async def test_reauthentication( ) old_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": old_entry.unique_id, - "entry_id": old_entry.entry_id, - }, - data=old_entry.data, - ) + result = await old_entry.start_reauth_flow(hass) flows = hass.config_entries.flow.async_progress() assert len(flows) == 1 @@ -267,15 +259,7 @@ async def test_reauth_account_mismatch( old_entry = MockConfigEntry(domain=DOMAIN, unique_id="baduid", version=1, data={}) old_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": old_entry.unique_id, - "entry_id": old_entry.entry_id, - }, - data=old_entry.data, - ) + result = await old_entry.start_reauth_flow(hass) flows = hass.config_entries.flow.async_progress() result = await hass.config_entries.flow.async_configure(flows[0]["flow_id"], {}) diff --git a/tests/components/tesla_fleet/test_init.py b/tests/components/tesla_fleet/test_init.py index b5eb21d1cdd523..9dcac4ec388c72 100644 --- a/tests/components/tesla_fleet/test_init.py +++ b/tests/components/tesla_fleet/test_init.py @@ -1,7 +1,9 @@ """Test the Tesla Fleet init.""" -from unittest.mock import AsyncMock +from unittest.mock import AsyncMock, patch +from aiohttp import RequestInfo +from aiohttp.client_exceptions import ClientResponseError from freezegun.api import FrozenDateTimeFactory import pytest from syrupy.assertion import SnapshotAssertion @@ -16,6 +18,7 @@ VehicleOffline, ) +from homeassistant.components.tesla_fleet.const import AUTHORIZE_URL from homeassistant.components.tesla_fleet.coordinator import ( ENERGY_INTERVAL, ENERGY_INTERVAL_SECONDS, @@ -72,6 +75,50 @@ async def test_init_error( assert normal_config_entry.state is state +async def test_oauth_refresh_expired( + hass: HomeAssistant, + normal_config_entry: MockConfigEntry, + mock_products: AsyncMock, +) -> None: + """Test init with expired Oauth token.""" + + # Patch the token refresh to raise an error + with patch( + "homeassistant.components.tesla_fleet.OAuth2Session.async_ensure_token_valid", + side_effect=ClientResponseError( + RequestInfo(AUTHORIZE_URL, "POST", {}, AUTHORIZE_URL), None, status=401 + ), + ) as mock_async_ensure_token_valid: + # Trigger an unmocked function call + mock_products.side_effect = InvalidRegion + await setup_platform(hass, normal_config_entry) + + mock_async_ensure_token_valid.assert_called_once() + assert normal_config_entry.state is ConfigEntryState.SETUP_ERROR + + +async def test_oauth_refresh_error( + hass: HomeAssistant, + normal_config_entry: MockConfigEntry, + mock_products: AsyncMock, +) -> None: + """Test init with Oauth refresh failure.""" + + # Patch the token refresh to raise an error + with patch( + "homeassistant.components.tesla_fleet.OAuth2Session.async_ensure_token_valid", + side_effect=ClientResponseError( + RequestInfo(AUTHORIZE_URL, "POST", {}, AUTHORIZE_URL), None, status=400 + ), + ) as mock_async_ensure_token_valid: + # Trigger an unmocked function call + mock_products.side_effect = InvalidRegion + await setup_platform(hass, normal_config_entry) + + mock_async_ensure_token_valid.assert_called_once() + assert normal_config_entry.state is ConfigEntryState.SETUP_RETRY + + # Test devices async def test_devices( hass: HomeAssistant, diff --git a/tests/components/teslemetry/snapshots/test_climate.ambr b/tests/components/teslemetry/snapshots/test_climate.ambr index b65796fe10e1d2..f5a95c7e3f21de 100644 --- a/tests/components/teslemetry/snapshots/test_climate.ambr +++ b/tests/components/teslemetry/snapshots/test_climate.ambr @@ -280,6 +280,85 @@ 'state': 'off', }) # --- +# name: test_climate_noscope[climate.test_cabin_overheat_protection-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'hvac_modes': list([ + , + ]), + 'max_temp': 40, + 'min_temp': 30, + 'target_temp_step': 5, + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': , + 'domain': 'climate', + 'entity_category': None, + 'entity_id': 'climate.test_cabin_overheat_protection', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Cabin overheat protection', + 'platform': 'teslemetry', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'climate_state_cabin_overheat_protection', + 'unique_id': 'LRWXF7EK4KC700000-climate_state_cabin_overheat_protection', + 'unit_of_measurement': None, + }) +# --- +# name: test_climate_noscope[climate.test_climate-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'hvac_modes': list([ + , + ]), + 'max_temp': 28.0, + 'min_temp': 15.0, + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'climate', + 'entity_category': None, + 'entity_id': 'climate.test_climate', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Climate', + 'platform': 'teslemetry', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': , + 'unique_id': 'LRWXF7EK4KC700000-driver_temp', + 'unit_of_measurement': None, + }) +# --- # name: test_climate_offline[climate.test_cabin_overheat_protection-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ diff --git a/tests/components/teslemetry/test_climate.py b/tests/components/teslemetry/test_climate.py index 31a39f1f21ae04..3cb4b67dc54fa3 100644 --- a/tests/components/teslemetry/test_climate.py +++ b/tests/components/teslemetry/test_climate.py @@ -1,6 +1,6 @@ """Test the Teslemetry climate platform.""" -from unittest.mock import patch +from unittest.mock import AsyncMock, patch from freezegun.api import FrozenDateTimeFactory import pytest @@ -371,12 +371,21 @@ async def test_asleep_or_offline( async def test_climate_noscope( hass: HomeAssistant, - mock_metadata, + snapshot: SnapshotAssertion, + entity_registry: er.EntityRegistry, + mock_metadata: AsyncMock, ) -> None: """Tests that the climate entity is correct.""" mock_metadata.return_value = METADATA_NOSCOPE - await setup_platform(hass, [Platform.CLIMATE]) + entry = await setup_platform(hass, [Platform.CLIMATE]) + + entity_entries = er.async_entries_for_config_entry(entity_registry, entry.entry_id) + + assert entity_entries + for entity_entry in entity_entries: + assert entity_entry == snapshot(name=f"{entity_entry.entity_id}-entry") + entity_id = "climate.test_climate" with pytest.raises(ServiceValidationError): diff --git a/tests/components/teslemetry/test_config_flow.py b/tests/components/teslemetry/test_config_flow.py index fa35142dc07e15..03e46c6a8be6e3 100644 --- a/tests/components/teslemetry/test_config_flow.py +++ b/tests/components/teslemetry/test_config_flow.py @@ -94,14 +94,7 @@ async def test_reauth(hass: HomeAssistant, mock_metadata) -> None: ) mock_entry.add_to_hass(hass) - result1 = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": mock_entry.entry_id, - }, - data=BAD_CONFIG, - ) + result1 = await mock_entry.start_reauth_flow(hass) assert result1["type"] is FlowResultType.FORM assert result1["step_id"] == "reauth_confirm" @@ -144,15 +137,7 @@ async def test_reauth_errors( ) mock_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": mock_entry.unique_id, - "entry_id": mock_entry.entry_id, - }, - data=BAD_CONFIG, - ) + result = await mock_entry.start_reauth_flow(hass) mock_metadata.side_effect = side_effect result2 = await hass.config_entries.flow.async_configure( diff --git a/tests/components/tessie/test_config_flow.py b/tests/components/tessie/test_config_flow.py index 043086971fad12..d51d467002d71d 100644 --- a/tests/components/tessie/test_config_flow.py +++ b/tests/components/tessie/test_config_flow.py @@ -143,14 +143,7 @@ async def test_reauth( ) mock_entry.add_to_hass(hass) - result1 = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": mock_entry.entry_id, - }, - data=TEST_CONFIG, - ) + result1 = await mock_entry.start_reauth_flow(hass) assert result1["type"] is FlowResultType.FORM assert result1["step_id"] == "reauth_confirm" @@ -194,15 +187,7 @@ async def test_reauth_errors( ) mock_entry.add_to_hass(hass) - result1 = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": mock_entry.unique_id, - "entry_id": mock_entry.entry_id, - }, - data=TEST_CONFIG, - ) + result1 = await mock_entry.start_reauth_flow(hass) result2 = await hass.config_entries.flow.async_configure( result1["flow_id"], diff --git a/tests/components/thethingsnetwork/test_config_flow.py b/tests/components/thethingsnetwork/test_config_flow.py index 107d84e099be4a..99c4a080e177a3 100644 --- a/tests/components/thethingsnetwork/test_config_flow.py +++ b/tests/components/thethingsnetwork/test_config_flow.py @@ -4,7 +4,7 @@ from ttn_client import TTNAuthError from homeassistant.components.thethingsnetwork.const import CONF_APP_ID, DOMAIN -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_USER from homeassistant.const import CONF_API_KEY, CONF_HOST from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -12,6 +12,8 @@ from . import init_integration from .conftest import API_KEY, APP_ID, HOST +from tests.common import MockConfigEntry + USER_DATA = {CONF_HOST: HOST, CONF_APP_ID: APP_ID, CONF_API_KEY: API_KEY} @@ -92,21 +94,13 @@ async def test_duplicate_entry( async def test_step_reauth( - hass: HomeAssistant, mock_ttnclient, mock_config_entry + hass: HomeAssistant, mock_ttnclient, mock_config_entry: MockConfigEntry ) -> None: """Test that the reauth step works.""" await init_integration(hass, mock_config_entry) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": APP_ID, - "entry_id": mock_config_entry.entry_id, - }, - data=USER_DATA, - ) + result = await mock_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" assert not result["errors"] diff --git a/tests/components/tibber/test_config_flow.py b/tests/components/tibber/test_config_flow.py index 28b590a29d2407..0c12c4a247b669 100644 --- a/tests/components/tibber/test_config_flow.py +++ b/tests/components/tibber/test_config_flow.py @@ -5,7 +5,11 @@ from aiohttp import ClientError import pytest -from tibber import FatalHttpException, InvalidLogin, RetryableHttpException +from tibber import ( + FatalHttpExceptionError, + InvalidLoginError, + RetryableHttpExceptionError, +) from homeassistant import config_entries from homeassistant.components.recorder import Recorder @@ -66,9 +70,9 @@ async def test_create_entry(recorder_mock: Recorder, hass: HomeAssistant) -> Non [ (TimeoutError, ERR_TIMEOUT), (ClientError, ERR_CLIENT), - (InvalidLogin(401), ERR_TOKEN), - (RetryableHttpException(503), ERR_CLIENT), - (FatalHttpException(404), ERR_CLIENT), + (InvalidLoginError(401), ERR_TOKEN), + (RetryableHttpExceptionError(503), ERR_CLIENT), + (FatalHttpExceptionError(404), ERR_CLIENT), ], ) async def test_create_entry_exceptions( diff --git a/tests/components/tile/test_config_flow.py b/tests/components/tile/test_config_flow.py index 87fe976ca3fa99..849be41d560db2 100644 --- a/tests/components/tile/test_config_flow.py +++ b/tests/components/tile/test_config_flow.py @@ -6,13 +6,15 @@ from pytile.errors import InvalidAuthError, TileError from homeassistant.components.tile import DOMAIN -from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType from .conftest import TEST_PASSWORD, TEST_USERNAME +from tests.common import MockConfigEntry + @pytest.mark.parametrize( ("mock_login_response", "errors"), @@ -77,12 +79,10 @@ async def test_import_entry(hass: HomeAssistant, config, mock_pytile) -> None: async def test_step_reauth( - hass: HomeAssistant, config, config_entry, setup_config_entry + hass: HomeAssistant, config, config_entry: MockConfigEntry, setup_config_entry ) -> None: """Test that the reauth step works.""" - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_REAUTH}, data=config - ) + result = await config_entry.start_reauth_flow(hass) assert result["step_id"] == "reauth_confirm" result = await hass.config_entries.flow.async_configure(result["flow_id"]) diff --git a/tests/components/totalconnect/test_config_flow.py b/tests/components/totalconnect/test_config_flow.py index 98de748faeacaa..a0be52afb3b15c 100644 --- a/tests/components/totalconnect/test_config_flow.py +++ b/tests/components/totalconnect/test_config_flow.py @@ -9,7 +9,7 @@ CONF_USERCODES, DOMAIN, ) -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_USER from homeassistant.const import CONF_PASSWORD from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -141,9 +141,7 @@ async def test_reauth(hass: HomeAssistant) -> None: ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_REAUTH}, data=entry.data - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/touchline_sl/__init__.py b/tests/components/touchline_sl/__init__.py new file mode 100644 index 00000000000000..c22e9d329db364 --- /dev/null +++ b/tests/components/touchline_sl/__init__.py @@ -0,0 +1 @@ +"""Tests for the Roth Touchline SL integration.""" diff --git a/tests/components/touchline_sl/conftest.py b/tests/components/touchline_sl/conftest.py new file mode 100644 index 00000000000000..4edeb048f5bd48 --- /dev/null +++ b/tests/components/touchline_sl/conftest.py @@ -0,0 +1,61 @@ +"""Common fixtures for the Roth Touchline SL tests.""" + +from collections.abc import Generator +from typing import NamedTuple +from unittest.mock import AsyncMock, patch + +import pytest + +from homeassistant.components.touchline_sl.const import DOMAIN +from homeassistant.const import CONF_PASSWORD, CONF_USERNAME + +from tests.common import MockConfigEntry + + +class FakeModule(NamedTuple): + """Fake Module used for unit testing only.""" + + name: str + id: str + + +@pytest.fixture +def mock_setup_entry() -> Generator[AsyncMock]: + """Override async_setup_entry.""" + with patch( + "homeassistant.components.touchline_sl.async_setup_entry", return_value=True + ) as mock_setup_entry: + yield mock_setup_entry + + +@pytest.fixture +def mock_touchlinesl_client() -> Generator[AsyncMock]: + """Mock a pytouchlinesl client.""" + with ( + patch( + "homeassistant.components.touchline_sl.TouchlineSL", + autospec=True, + ) as mock_client, + patch( + "homeassistant.components.touchline_sl.config_flow.TouchlineSL", + new=mock_client, + ), + ): + client = mock_client.return_value + client.user_id.return_value = 12345 + client.modules.return_value = [FakeModule(name="Foobar", id="deadbeef")] + yield client + + +@pytest.fixture +def mock_config_entry() -> MockConfigEntry: + """Mock a config entry.""" + return MockConfigEntry( + domain=DOMAIN, + title="TouchlineSL", + data={ + CONF_USERNAME: "test-username", + CONF_PASSWORD: "test-password", + }, + unique_id="12345", + ) diff --git a/tests/components/touchline_sl/test_config_flow.py b/tests/components/touchline_sl/test_config_flow.py new file mode 100644 index 00000000000000..992fa2bdb3e86b --- /dev/null +++ b/tests/components/touchline_sl/test_config_flow.py @@ -0,0 +1,113 @@ +"""Test the Roth Touchline SL config flow.""" + +from unittest.mock import AsyncMock + +import pytest +from pytouchlinesl.client import RothAPIError + +from homeassistant.components.touchline_sl.const import DOMAIN +from homeassistant.config_entries import SOURCE_USER +from homeassistant.const import CONF_PASSWORD, CONF_USERNAME +from homeassistant.core import HomeAssistant +from homeassistant.data_entry_flow import FlowResultType + +from tests.common import MockConfigEntry + +RESULT_UNIQUE_ID = "12345" + +CONFIG_DATA = { + CONF_USERNAME: "test-username", + CONF_PASSWORD: "test-password", +} + + +async def test_config_flow_success( + hass: HomeAssistant, mock_setup_entry: AsyncMock, mock_touchlinesl_client: AsyncMock +) -> None: + """Test the happy path where the provided username/password result in a new entry.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + assert result["errors"] == {} + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], CONFIG_DATA + ) + + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["title"] == "test-username" + assert result["data"] == CONFIG_DATA + assert result["result"].unique_id == RESULT_UNIQUE_ID + assert len(mock_setup_entry.mock_calls) == 1 + + +@pytest.mark.parametrize( + ("exception", "error_base"), + [ + (RothAPIError(status=401), "invalid_auth"), + (RothAPIError(status=502), "cannot_connect"), + (Exception, "unknown"), + ], +) +async def test_config_flow_failure_api_exceptions( + hass: HomeAssistant, + exception: Exception, + error_base: str, + mock_setup_entry: AsyncMock, + mock_touchlinesl_client: AsyncMock, +) -> None: + """Test for invalid credentials or API connection errors, and that the form can recover.""" + mock_touchlinesl_client.user_id.side_effect = exception + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + assert result["errors"] == {} + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], CONFIG_DATA + ) + + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {"base": error_base} + + # "Fix" the problem, and try again. + mock_touchlinesl_client.user_id.side_effect = None + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], CONFIG_DATA + ) + + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["title"] == "test-username" + assert result["data"] == CONFIG_DATA + assert result["result"].unique_id == RESULT_UNIQUE_ID + assert len(mock_setup_entry.mock_calls) == 1 + + +async def test_config_flow_failure_adding_non_unique_account( + hass: HomeAssistant, + mock_setup_entry: AsyncMock, + mock_touchlinesl_client: AsyncMock, + mock_config_entry: MockConfigEntry, +) -> None: + """Test that the config flow fails when user tries to add duplicate accounts.""" + mock_config_entry.add_to_hass(hass) + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + assert result["errors"] == {} + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], CONFIG_DATA + ) + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "already_configured" diff --git a/tests/components/tplink/fixtures/features.json b/tests/components/tplink/fixtures/features.json index 7cfe979ea255e3..6d4afd98d15ded 100644 --- a/tests/components/tplink/fixtures/features.json +++ b/tests/components/tplink/fixtures/features.json @@ -150,6 +150,11 @@ "type": "Sensor", "category": "Debug" }, + "check_latest_firmware": { + "value": "", + "type": "Action", + "category": "Info" + }, "thermostat_mode": { "value": "off", "type": "Sensor", diff --git a/tests/components/tplink/test_config_flow.py b/tests/components/tplink/test_config_flow.py index ddd67f249e6729..f90eb985d3814a 100644 --- a/tests/components/tplink/test_config_flow.py +++ b/tests/components/tplink/test_config_flow.py @@ -1289,6 +1289,33 @@ async def test_discovery_timeout_connect( assert mock_connect["connect"].call_count == 1 +async def test_discovery_timeout_connect_legacy_error( + hass: HomeAssistant, + mock_discovery: AsyncMock, + mock_connect: AsyncMock, + mock_init, +) -> None: + """Test discovery tries legacy connect on timeout.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} + ) + mock_discovery["discover_single"].side_effect = TimeoutError + mock_connect["connect"].side_effect = KasaException + await hass.async_block_till_done() + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + assert not result["errors"] + assert mock_connect["connect"].call_count == 0 + + result2 = await hass.config_entries.flow.async_configure( + result["flow_id"], {CONF_HOST: IP_ADDRESS} + ) + await hass.async_block_till_done() + assert result2["type"] is FlowResultType.FORM + assert result2["errors"] == {"base": "cannot_connect"} + assert mock_connect["connect"].call_count == 1 + + async def test_reauth_update_other_flows( hass: HomeAssistant, mock_discovery: AsyncMock, diff --git a/tests/components/tplink_omada/test_config_flow.py b/tests/components/tplink_omada/test_config_flow.py index 08606fe126c2c6..28ef0da170f810 100644 --- a/tests/components/tplink_omada/test_config_flow.py +++ b/tests/components/tplink_omada/test_config_flow.py @@ -251,14 +251,7 @@ async def test_async_step_reauth_success(hass: HomeAssistant) -> None: ) mock_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": mock_entry.entry_id, - }, - data=mock_entry.data, - ) + result = await mock_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -298,14 +291,7 @@ async def test_async_step_reauth_invalid_auth(hass: HomeAssistant) -> None: ) mock_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": mock_entry.entry_id, - }, - data=mock_entry.data, - ) + result = await mock_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/tractive/test_config_flow.py b/tests/components/tractive/test_config_flow.py index 5cedb51e5afc68..691bf671afd2ec 100644 --- a/tests/components/tractive/test_config_flow.py +++ b/tests/components/tractive/test_config_flow.py @@ -110,15 +110,7 @@ async def test_reauthentication(hass: HomeAssistant) -> None: ) old_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": old_entry.unique_id, - "entry_id": old_entry.entry_id, - }, - data=old_entry.data, - ) + result = await old_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["errors"] == {} @@ -151,15 +143,7 @@ async def test_reauthentication_failure(hass: HomeAssistant) -> None: ) old_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": old_entry.unique_id, - "entry_id": old_entry.entry_id, - }, - data=old_entry.data, - ) + result = await old_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["errors"] == {} @@ -189,15 +173,7 @@ async def test_reauthentication_unknown_failure(hass: HomeAssistant) -> None: ) old_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": old_entry.unique_id, - "entry_id": old_entry.entry_id, - }, - data=old_entry.data, - ) + result = await old_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["errors"] == {} @@ -227,15 +203,7 @@ async def test_reauthentication_failure_no_existing_entry(hass: HomeAssistant) - ) old_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": old_entry.unique_id, - "entry_id": old_entry.entry_id, - }, - data=old_entry.data, - ) + result = await old_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["errors"] == {} diff --git a/tests/components/trafikverket_camera/test_config_flow.py b/tests/components/trafikverket_camera/test_config_flow.py index 2e9e34f4c357f9..dd75f5e6838d14 100644 --- a/tests/components/trafikverket_camera/test_config_flow.py +++ b/tests/components/trafikverket_camera/test_config_flow.py @@ -208,15 +208,7 @@ async def test_reauth_flow(hass: HomeAssistant) -> None: ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": entry.unique_id, - "entry_id": entry.entry_id, - }, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) assert result["step_id"] == "reauth_confirm" assert result["type"] is FlowResultType.FORM assert result["errors"] == {} @@ -280,15 +272,7 @@ async def test_reauth_flow_error( entry.add_to_hass(hass) await hass.async_block_till_done() - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": entry.unique_id, - "entry_id": entry.entry_id, - }, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) with patch( "homeassistant.components.trafikverket_camera.config_flow.TrafikverketCamera.async_get_cameras", diff --git a/tests/components/trafikverket_ferry/test_config_flow.py b/tests/components/trafikverket_ferry/test_config_flow.py index 1c170a917cce1c..916f9c9f2ec80a 100644 --- a/tests/components/trafikverket_ferry/test_config_flow.py +++ b/tests/components/trafikverket_ferry/test_config_flow.py @@ -128,15 +128,7 @@ async def test_reauth_flow(hass: HomeAssistant) -> None: ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": entry.unique_id, - "entry_id": entry.entry_id, - }, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) assert result["step_id"] == "reauth_confirm" assert result["type"] is FlowResultType.FORM assert result["errors"] == {} @@ -203,15 +195,7 @@ async def test_reauth_flow_error( ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": entry.unique_id, - "entry_id": entry.entry_id, - }, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) with patch( "homeassistant.components.trafikverket_ferry.config_flow.TrafikverketFerry.async_get_next_ferry_stop", diff --git a/tests/components/trafikverket_train/test_config_flow.py b/tests/components/trafikverket_train/test_config_flow.py index 83cc5a89016ebe..3090a9fe3373b9 100644 --- a/tests/components/trafikverket_train/test_config_flow.py +++ b/tests/components/trafikverket_train/test_config_flow.py @@ -246,15 +246,7 @@ async def test_reauth_flow(hass: HomeAssistant) -> None: ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": entry.unique_id, - "entry_id": entry.entry_id, - }, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) assert result["step_id"] == "reauth_confirm" assert result["type"] is FlowResultType.FORM assert result["errors"] == {} @@ -328,15 +320,7 @@ async def test_reauth_flow_error( ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": entry.unique_id, - "entry_id": entry.entry_id, - }, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) with ( patch( @@ -418,15 +402,7 @@ async def test_reauth_flow_error_departures( ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": entry.unique_id, - "entry_id": entry.entry_id, - }, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) with ( patch( diff --git a/tests/components/trafikverket_weatherstation/test_config_flow.py b/tests/components/trafikverket_weatherstation/test_config_flow.py index 771336301ffe6c..738d6a8ceac505 100644 --- a/tests/components/trafikverket_weatherstation/test_config_flow.py +++ b/tests/components/trafikverket_weatherstation/test_config_flow.py @@ -116,14 +116,7 @@ async def test_reauth_flow(hass: HomeAssistant) -> None: ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - }, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) assert result["step_id"] == "reauth_confirm" assert result["type"] is FlowResultType.FORM assert result["errors"] == {} @@ -182,14 +175,7 @@ async def test_reauth_flow_fails( ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - }, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) assert result["step_id"] == "reauth_confirm" assert result["type"] is FlowResultType.FORM assert result["errors"] == {} diff --git a/tests/components/transmission/test_config_flow.py b/tests/components/transmission/test_config_flow.py index e6c523bf1f6eba..b318862047efdc 100644 --- a/tests/components/transmission/test_config_flow.py +++ b/tests/components/transmission/test_config_flow.py @@ -160,14 +160,7 @@ async def test_reauth_success(hass: HomeAssistant) -> None: entry = MockConfigEntry(domain=transmission.DOMAIN, data=MOCK_CONFIG_DATA) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - transmission.DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - }, - data=MOCK_CONFIG_DATA, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -197,14 +190,7 @@ async def test_reauth_failed(hass: HomeAssistant, mock_api: MagicMock) -> None: ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - transmission.DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - }, - data=MOCK_CONFIG_DATA, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -232,14 +218,7 @@ async def test_reauth_failed_connection_error( ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - transmission.DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - }, - data=MOCK_CONFIG_DATA, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/tts/common.py b/tests/components/tts/common.py index 1331f441940b91..b1eae12d694b72 100644 --- a/tests/components/tts/common.py +++ b/tests/components/tts/common.py @@ -130,6 +130,8 @@ class BaseProvider: def __init__(self, lang: str) -> None: """Initialize test provider.""" self._lang = lang + self._supported_languages = SUPPORT_LANGUAGES + self._supported_options = ["voice", "age"] @property def default_language(self) -> str: @@ -139,7 +141,7 @@ def default_language(self) -> str: @property def supported_languages(self) -> list[str]: """Return list of supported languages.""" - return SUPPORT_LANGUAGES + return self._supported_languages @callback def async_get_supported_voices(self, language: str) -> list[Voice] | None: @@ -154,7 +156,7 @@ def async_get_supported_voices(self, language: str) -> list[Voice] | None: @property def supported_options(self) -> list[str]: """Return list of supported options like voice, emotions.""" - return ["voice", "age"] + return self._supported_options def get_tts_audio( self, message: str, language: str, options: dict[str, Any] @@ -163,7 +165,7 @@ def get_tts_audio( return ("mp3", b"") -class MockProvider(BaseProvider, Provider): +class MockTTSProvider(BaseProvider, Provider): """Test speech API provider.""" def __init__(self, lang: str) -> None: @@ -175,10 +177,7 @@ def __init__(self, lang: str) -> None: class MockTTSEntity(BaseProvider, TextToSpeechEntity): """Test speech API provider.""" - @property - def name(self) -> str: - """Return the name of the entity.""" - return "Test" + _attr_name = "Test" class MockTTS(MockPlatform): @@ -188,7 +187,7 @@ class MockTTS(MockPlatform): {vol.Optional(CONF_LANG, default=DEFAULT_LANG): vol.In(SUPPORT_LANGUAGES)} ) - def __init__(self, provider: MockProvider, **kwargs: Any) -> None: + def __init__(self, provider: MockTTSProvider, **kwargs: Any) -> None: """Initialize.""" super().__init__(**kwargs) self._provider = provider @@ -205,7 +204,7 @@ async def async_get_engine( async def mock_setup( hass: HomeAssistant, - mock_provider: MockProvider, + mock_provider: MockTTSProvider, ) -> None: """Set up a test provider.""" mock_integration(hass, MockModule(domain=TEST_DOMAIN)) @@ -218,7 +217,9 @@ async def mock_setup( async def mock_config_entry_setup( - hass: HomeAssistant, tts_entity: MockTTSEntity + hass: HomeAssistant, + tts_entity: MockTTSEntity, + test_domain: str = TEST_DOMAIN, ) -> MockConfigEntry: """Set up a test tts platform via config entry.""" @@ -239,7 +240,7 @@ async def async_unload_entry_init( mock_integration( hass, MockModule( - TEST_DOMAIN, + test_domain, async_setup_entry=async_setup_entry_init, async_unload_entry=async_unload_entry_init, ), @@ -254,9 +255,9 @@ async def async_setup_entry_platform( async_add_entities([tts_entity]) loaded_platform = MockPlatform(async_setup_entry=async_setup_entry_platform) - mock_platform(hass, f"{TEST_DOMAIN}.{TTS_DOMAIN}", loaded_platform) + mock_platform(hass, f"{test_domain}.{TTS_DOMAIN}", loaded_platform) - config_entry = MockConfigEntry(domain=TEST_DOMAIN) + config_entry = MockConfigEntry(domain=test_domain) config_entry.add_to_hass(hass) assert await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() diff --git a/tests/components/tts/conftest.py b/tests/components/tts/conftest.py index d9a4499f544dbe..16c24f006d733f 100644 --- a/tests/components/tts/conftest.py +++ b/tests/components/tts/conftest.py @@ -3,7 +3,8 @@ From http://doc.pytest.org/en/latest/example/simple.html#making-test-result-information-available-in-fixtures """ -from collections.abc import Generator +from collections.abc import Generator, Iterable +from contextlib import ExitStack from pathlib import Path from unittest.mock import MagicMock @@ -16,9 +17,9 @@ from .common import ( DEFAULT_LANG, TEST_DOMAIN, - MockProvider, MockTTS, MockTTSEntity, + MockTTSProvider, mock_config_entry_setup, mock_setup, ) @@ -66,9 +67,9 @@ async def mock_tts(hass: HomeAssistant, mock_provider) -> None: @pytest.fixture -def mock_provider() -> MockProvider: +def mock_provider() -> MockTTSProvider: """Test TTS provider.""" - return MockProvider(DEFAULT_LANG) + return MockTTSProvider(DEFAULT_LANG) @pytest.fixture @@ -81,12 +82,23 @@ class TTSFlow(ConfigFlow): """Test flow.""" +@pytest.fixture(name="config_flow_test_domains") +def config_flow_test_domain_fixture() -> Iterable[str]: + """Test domain fixture.""" + return (TEST_DOMAIN,) + + @pytest.fixture(autouse=True) -def config_flow_fixture(hass: HomeAssistant) -> Generator[None]: +def config_flow_fixture( + hass: HomeAssistant, config_flow_test_domains: Iterable[str] +) -> Generator[None]: """Mock config flow.""" - mock_platform(hass, f"{TEST_DOMAIN}.config_flow") + for domain in config_flow_test_domains: + mock_platform(hass, f"{domain}.config_flow") - with mock_config_flow(TEST_DOMAIN, TTSFlow): + with ExitStack() as stack: + for domain in config_flow_test_domains: + stack.enter_context(mock_config_flow(domain, TTSFlow)) yield @@ -94,7 +106,7 @@ def config_flow_fixture(hass: HomeAssistant) -> Generator[None]: async def setup_fixture( hass: HomeAssistant, request: pytest.FixtureRequest, - mock_provider: MockProvider, + mock_provider: MockTTSProvider, mock_tts_entity: MockTTSEntity, ) -> None: """Set up the test environment.""" diff --git a/tests/components/tts/test_init.py b/tests/components/tts/test_init.py index 7a54ecc26b0128..cf04fbb175ba87 100644 --- a/tests/components/tts/test_init.py +++ b/tests/components/tts/test_init.py @@ -30,15 +30,22 @@ DEFAULT_LANG, SUPPORT_LANGUAGES, TEST_DOMAIN, - MockProvider, + MockTTS, MockTTSEntity, + MockTTSProvider, get_media_source_url, mock_config_entry_setup, mock_setup, retrieve_media, ) -from tests.common import async_mock_service, mock_restore_cache +from tests.common import ( + MockModule, + async_mock_service, + mock_integration, + mock_platform, + mock_restore_cache, +) from tests.typing import ClientSessionGenerator, WebSocketGenerator ORIG_WRITE_TAGS = tts.SpeechManager.write_tags @@ -213,7 +220,7 @@ async def test_service( @pytest.mark.parametrize( ("mock_provider", "mock_tts_entity"), - [(MockProvider("de_DE"), MockTTSEntity("de_DE"))], + [(MockTTSProvider("de_DE"), MockTTSEntity("de_DE"))], ) @pytest.mark.parametrize( ("setup", "tts_service", "service_data", "expected_url_suffix"), @@ -274,7 +281,7 @@ async def test_service_default_language( @pytest.mark.parametrize( ("mock_provider", "mock_tts_entity"), - [(MockProvider("en_US"), MockTTSEntity("en_US"))], + [(MockTTSProvider("en_US"), MockTTSEntity("en_US"))], ) @pytest.mark.parametrize( ("setup", "tts_service", "service_data", "expected_url_suffix"), @@ -504,7 +511,7 @@ async def test_service_options( ).is_file() -class MockProviderWithDefaults(MockProvider): +class MockProviderWithDefaults(MockTTSProvider): """Mock provider with default options.""" @property @@ -847,7 +854,7 @@ async def test_service_receive_voice( @pytest.mark.parametrize( ("mock_provider", "mock_tts_entity"), - [(MockProvider("de_DE"), MockTTSEntity("de_DE"))], + [(MockTTSProvider("de_DE"), MockTTSEntity("de_DE"))], ) @pytest.mark.parametrize( ("setup", "tts_service", "service_data", "expected_url_suffix"), @@ -1008,7 +1015,7 @@ async def test_service_without_cache( ).is_file() -class MockProviderBoom(MockProvider): +class MockProviderBoom(MockTTSProvider): """Mock provider that blows up.""" def get_tts_audio( @@ -1034,7 +1041,7 @@ def get_tts_audio( async def test_setup_legacy_cache_dir( hass: HomeAssistant, mock_tts_cache_dir: Path, - mock_provider: MockProvider, + mock_provider: MockTTSProvider, ) -> None: """Set up a TTS platform with cache and call service without cache.""" calls = async_mock_service(hass, DOMAIN_MP, SERVICE_PLAY_MEDIA) @@ -1099,7 +1106,7 @@ async def test_setup_cache_dir( await hass.async_block_till_done() -class MockProviderEmpty(MockProvider): +class MockProviderEmpty(MockTTSProvider): """Mock provider with empty get_tts_audio.""" def get_tts_audio( @@ -1171,7 +1178,7 @@ async def test_service_get_tts_error( async def test_load_cache_legacy_retrieve_without_mem_cache( hass: HomeAssistant, - mock_provider: MockProvider, + mock_provider: MockTTSProvider, mock_tts_cache_dir: Path, hass_client: ClientSessionGenerator, ) -> None: @@ -1389,9 +1396,6 @@ def test_resolve_engine(hass: HomeAssistant, setup: str, engine_id: str) -> None ): assert tts.async_resolve_engine(hass, None) is None - with patch.dict(hass.data[tts.DATA_TTS_MANAGER].providers, {"cloud": object()}): - assert tts.async_resolve_engine(hass, None) == "cloud" - @pytest.mark.parametrize( ("setup", "engine_id"), @@ -1422,7 +1426,7 @@ async def test_legacy_fetching_in_async( """Test async fetching of data for a legacy provider.""" tts_audio: asyncio.Future[bytes] = asyncio.Future() - class ProviderWithAsyncFetching(MockProvider): + class ProviderWithAsyncFetching(MockTTSProvider): """Provider that supports audio output option.""" @property @@ -1561,15 +1565,19 @@ async def async_get_tts_audio( @pytest.mark.parametrize( - ("setup", "engine_id"), + ("setup", "engine_id", "extra_data"), [ - ("mock_setup", "test"), - ("mock_config_entry_setup", "tts.test"), + ("mock_setup", "test", {"name": "Test"}), + ("mock_config_entry_setup", "tts.test", {}), ], indirect=["setup"], ) async def test_ws_list_engines( - hass: HomeAssistant, hass_ws_client: WebSocketGenerator, setup: str, engine_id: str + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, + setup: str, + engine_id: str, + extra_data: dict[str, str], ) -> None: """Test listing tts engines and supported languages.""" client = await hass_ws_client() @@ -1584,6 +1592,7 @@ async def test_ws_list_engines( "engine_id": engine_id, "supported_languages": ["de_CH", "de_DE", "en_GB", "en_US"], } + | extra_data ] } @@ -1592,7 +1601,7 @@ async def test_ws_list_engines( msg = await client.receive_json() assert msg["success"] assert msg["result"] == { - "providers": [{"engine_id": engine_id, "supported_languages": []}] + "providers": [{"engine_id": engine_id, "supported_languages": []} | extra_data] } await client.send_json_auto_id({"type": "tts/engine/list", "language": "en"}) @@ -1602,6 +1611,7 @@ async def test_ws_list_engines( assert msg["result"] == { "providers": [ {"engine_id": engine_id, "supported_languages": ["en_US", "en_GB"]} + | extra_data ] } @@ -1612,6 +1622,7 @@ async def test_ws_list_engines( assert msg["result"] == { "providers": [ {"engine_id": engine_id, "supported_languages": ["en_GB", "en_US"]} + | extra_data ] } @@ -1622,6 +1633,7 @@ async def test_ws_list_engines( assert msg["result"] == { "providers": [ {"engine_id": engine_id, "supported_languages": ["de_DE", "de_CH"]} + | extra_data ] } @@ -1634,20 +1646,74 @@ async def test_ws_list_engines( assert msg["result"] == { "providers": [ {"engine_id": engine_id, "supported_languages": ["de_CH", "de_DE"]} + | extra_data + ] + } + + +async def test_ws_list_engines_deprecated( + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, + mock_tts_entity: MockTTSEntity, +) -> None: + """Test listing tts engines. + + This test asserts the deprecated flag is set on a legacy engine whose integration + also provides tts entities. + """ + + mock_provider = MockTTSProvider(DEFAULT_LANG) + mock_provider_2 = MockTTSProvider(DEFAULT_LANG) + mock_integration(hass, MockModule(domain="test")) + mock_platform(hass, "test.tts", MockTTS(mock_provider)) + mock_integration(hass, MockModule(domain="test_2")) + mock_platform(hass, "test_2.tts", MockTTS(mock_provider_2)) + await async_setup_component( + hass, "tts", {"tts": [{"platform": "test"}, {"platform": "test_2"}]} + ) + await mock_config_entry_setup(hass, mock_tts_entity) + + client = await hass_ws_client() + + await client.send_json_auto_id({"type": "tts/engine/list"}) + + msg = await client.receive_json() + assert msg["success"] + assert msg["result"] == { + "providers": [ + { + "engine_id": "tts.test", + "supported_languages": ["de_CH", "de_DE", "en_GB", "en_US"], + }, + { + "deprecated": True, + "engine_id": "test", + "name": "Test", + "supported_languages": ["de_CH", "de_DE", "en_GB", "en_US"], + }, + { + "engine_id": "test_2", + "name": "Test", + "supported_languages": ["de_CH", "de_DE", "en_GB", "en_US"], + }, ] } @pytest.mark.parametrize( - ("setup", "engine_id"), + ("setup", "engine_id", "extra_data"), [ - ("mock_setup", "test"), - ("mock_config_entry_setup", "tts.test"), + ("mock_setup", "test", {"name": "Test"}), + ("mock_config_entry_setup", "tts.test", {}), ], indirect=["setup"], ) async def test_ws_get_engine( - hass: HomeAssistant, hass_ws_client: WebSocketGenerator, setup: str, engine_id: str + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, + setup: str, + engine_id: str, + extra_data: dict[str, str], ) -> None: """Test getting an tts engine.""" client = await hass_ws_client() @@ -1661,6 +1727,7 @@ async def test_ws_get_engine( "engine_id": engine_id, "supported_languages": ["de_CH", "de_DE", "en_GB", "en_US"], } + | extra_data } @@ -1838,3 +1905,61 @@ def supported_languages(self) -> list[str]: if record.exc_info is not None ] ) + + +async def test_default_engine_prefer_entity( + hass: HomeAssistant, + mock_tts_entity: MockTTSEntity, + mock_provider: MockTTSProvider, +) -> None: + """Test async_default_engine. + + In this tests there's an entity and a legacy provider. + The test asserts async_default_engine returns the entity. + """ + mock_tts_entity._attr_name = "New test" + + await mock_setup(hass, mock_provider) + await mock_config_entry_setup(hass, mock_tts_entity) + await hass.async_block_till_done() + + entity_engine = tts.async_resolve_engine(hass, "tts.new_test") + assert entity_engine == "tts.new_test" + provider_engine = tts.async_resolve_engine(hass, "test") + assert provider_engine == "test" + assert tts.async_default_engine(hass) == "tts.new_test" + + +@pytest.mark.parametrize( + "config_flow_test_domains", + [ + # Test different setup order to ensure the default is not influenced + # by setup order. + ("cloud", "new_test"), + ("new_test", "cloud"), + ], +) +async def test_default_engine_prefer_cloud_entity( + hass: HomeAssistant, + mock_provider: MockTTSProvider, + config_flow_test_domains: str, +) -> None: + """Test async_default_engine. + + In this tests there's an entity from domain cloud, an entity from domain new_test + and a legacy provider. + The test asserts async_default_engine returns the entity from domain cloud. + """ + await mock_setup(hass, mock_provider) + for domain in config_flow_test_domains: + entity = MockTTSEntity(DEFAULT_LANG) + entity._attr_name = f"{domain} TTS entity" + await mock_config_entry_setup(hass, entity, test_domain=domain) + await hass.async_block_till_done() + + for domain in config_flow_test_domains: + entity_engine = tts.async_resolve_engine(hass, f"tts.{domain}_tts_entity") + assert entity_engine == f"tts.{domain}_tts_entity" + provider_engine = tts.async_resolve_engine(hass, "test") + assert provider_engine == "test" + assert tts.async_default_engine(hass) == "tts.cloud_tts_entity" diff --git a/tests/components/tts/test_legacy.py b/tests/components/tts/test_legacy.py index 0d7f99e8cd112e..22e8ac35f16171 100644 --- a/tests/components/tts/test_legacy.py +++ b/tests/components/tts/test_legacy.py @@ -17,7 +17,7 @@ from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.setup import async_setup_component -from .common import SUPPORT_LANGUAGES, MockProvider, MockTTS +from .common import SUPPORT_LANGUAGES, MockTTS, MockTTSProvider from tests.common import ( MockModule, @@ -75,7 +75,9 @@ async def test_invalid_platform( async def test_platform_setup_without_provider( - hass: HomeAssistant, caplog: pytest.LogCaptureFixture, mock_provider: MockProvider + hass: HomeAssistant, + caplog: pytest.LogCaptureFixture, + mock_provider: MockTTSProvider, ) -> None: """Test platform setup without provider returned.""" @@ -109,7 +111,7 @@ async def async_get_engine( async def test_platform_setup_with_error( hass: HomeAssistant, caplog: pytest.LogCaptureFixture, - mock_provider: MockProvider, + mock_provider: MockTTSProvider, ) -> None: """Test platform setup with an error during setup.""" diff --git a/tests/components/tts/test_media_source.py b/tests/components/tts/test_media_source.py index 4c10d8f0b08b88..ba856fd9622293 100644 --- a/tests/components/tts/test_media_source.py +++ b/tests/components/tts/test_media_source.py @@ -12,8 +12,8 @@ from .common import ( DEFAULT_LANG, - MockProvider, MockTTSEntity, + MockTTSProvider, mock_config_entry_setup, mock_setup, retrieve_media, @@ -28,7 +28,7 @@ class MSEntity(MockTTSEntity): get_tts_audio = MagicMock(return_value=("mp3", b"")) -class MSProvider(MockProvider): +class MSProvider(MockTTSProvider): """Test speech API provider.""" get_tts_audio = MagicMock(return_value=("mp3", b"")) diff --git a/tests/components/tuya/test_config_flow.py b/tests/components/tuya/test_config_flow.py index 6e971262bc8b20..247aec02cd1098 100644 --- a/tests/components/tuya/test_config_flow.py +++ b/tests/components/tuya/test_config_flow.py @@ -8,7 +8,7 @@ from syrupy.assertion import SnapshotAssertion from homeassistant.components.tuya.const import CONF_APP_TYPE, CONF_USER_CODE, DOMAIN -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_USER from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -145,15 +145,7 @@ async def test_reauth_flow( """Test the reauthentication configuration flow.""" mock_config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": mock_config_entry.unique_id, - "entry_id": mock_config_entry.entry_id, - }, - data=mock_config_entry.data, - ) + result = await mock_config_entry.start_reauth_flow(hass) assert result.get("type") is FlowResultType.FORM assert result.get("step_id") == "scan" @@ -185,15 +177,7 @@ async def test_reauth_flow_migration( assert CONF_APP_TYPE in mock_old_config_entry.data assert CONF_USER_CODE not in mock_old_config_entry.data - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": mock_old_config_entry.unique_id, - "entry_id": mock_old_config_entry.entry_id, - }, - data=mock_old_config_entry.data, - ) + result = await mock_old_config_entry.start_reauth_flow(hass) assert result.get("type") is FlowResultType.FORM assert result.get("step_id") == "reauth_user_code" @@ -229,15 +213,7 @@ async def test_reauth_flow_failed_qr_code( """Test an error occurring while retrieving the QR code.""" mock_old_config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": mock_old_config_entry.unique_id, - "entry_id": mock_old_config_entry.entry_id, - }, - data=mock_old_config_entry.data, - ) + result = await mock_old_config_entry.start_reauth_flow(hass) # Something went wrong getting the QR code (like an invalid user code) mock_tuya_login_control.qr_code.return_value["success"] = False diff --git a/tests/components/twitch/test_config_flow.py b/tests/components/twitch/test_config_flow.py index 6935943a4d37f9..fc53b17551c684 100644 --- a/tests/components/twitch/test_config_flow.py +++ b/tests/components/twitch/test_config_flow.py @@ -10,7 +10,7 @@ DOMAIN, OAUTH2_AUTHORIZE, ) -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_USER from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResult, FlowResultType from homeassistant.helpers import config_entry_oauth2_flow @@ -109,14 +109,7 @@ async def test_reauth( ) -> None: """Check reauth flow.""" await setup_integration(hass, config_entry) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "entry_id": config_entry.entry_id, - }, - data=config_entry.data, - ) + result = await config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -184,14 +177,7 @@ async def test_reauth_wrong_account( twitch_mock.return_value.get_users = lambda *args, **kwargs: get_generator( "get_users_2.json", TwitchUser ) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "entry_id": config_entry.entry_id, - }, - data=config_entry.data, - ) + result = await config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/unifi/test_config_flow.py b/tests/components/unifi/test_config_flow.py index 1d745511dc59c8..71b196550daf9c 100644 --- a/tests/components/unifi/test_config_flow.py +++ b/tests/components/unifi/test_config_flow.py @@ -24,7 +24,6 @@ CONF_TRACK_WIRED_CLIENTS, DOMAIN as UNIFI_DOMAIN, ) -from homeassistant.config_entries import SOURCE_REAUTH from homeassistant.const import ( CONF_HOST, CONF_PASSWORD, @@ -302,15 +301,7 @@ async def test_reauth_flow_update_configuration( """Verify reauth flow can update hub configuration.""" config_entry = config_entry_setup - result = await hass.config_entries.flow.async_init( - UNIFI_DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": config_entry.unique_id, - "entry_id": config_entry.entry_id, - }, - data=config_entry.data, - ) + result = await config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "user" @@ -344,15 +335,7 @@ async def test_reauth_flow_update_configuration_on_not_loaded_entry( with patch("aiounifi.Controller.login", side_effect=aiounifi.errors.RequestError): config_entry = await config_entry_factory() - result = await hass.config_entries.flow.async_init( - UNIFI_DOMAIN, - context={ - "source": SOURCE_REAUTH, - "unique_id": config_entry.unique_id, - "entry_id": config_entry.entry_id, - }, - data=config_entry.data, - ) + result = await config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "user" diff --git a/tests/components/unifiprotect/test_binary_sensor.py b/tests/components/unifiprotect/test_binary_sensor.py index af8ce015955d05..31669aa62bb5b3 100644 --- a/tests/components/unifiprotect/test_binary_sensor.py +++ b/tests/components/unifiprotect/test_binary_sensor.py @@ -575,3 +575,149 @@ async def test_binary_sensor_package_detected( ufp.ws_msg(mock_msg) await hass.async_block_till_done() assert len(state_changes) == 2 + + +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_binary_sensor_person_detected( + hass: HomeAssistant, + ufp: MockUFPFixture, + doorbell: Camera, + unadopted_camera: Camera, + fixed_now: datetime, +) -> None: + """Test binary_sensor person detected detection entity.""" + + await init_entry(hass, ufp, [doorbell, unadopted_camera]) + assert_entity_counts(hass, Platform.BINARY_SENSOR, 15, 15) + + doorbell.smart_detect_settings.object_types.append(SmartDetectObjectType.PERSON) + + _, entity_id = ids_from_device_description( + Platform.BINARY_SENSOR, doorbell, EVENT_SENSORS[3] + ) + + events = async_capture_events(hass, EVENT_STATE_CHANGED) + + event = Event( + model=ModelType.EVENT, + id="test_event_id", + type=EventType.SMART_DETECT, + start=fixed_now - timedelta(seconds=1), + end=None, + score=50, + smart_detect_types=[], + smart_detect_event_ids=[], + camera_id=doorbell.id, + api=ufp.api, + ) + + new_camera = doorbell.copy() + new_camera.is_smart_detected = True + + ufp.api.bootstrap.cameras = {new_camera.id: new_camera} + ufp.api.bootstrap.events = {event.id: event} + + mock_msg = Mock() + mock_msg.changed_data = {} + mock_msg.new_obj = event + ufp.ws_msg(mock_msg) + + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert state + assert state.state == STATE_OFF + + event = Event( + model=ModelType.EVENT, + id="test_event_id", + type=EventType.SMART_DETECT, + start=fixed_now - timedelta(seconds=1), + end=fixed_now + timedelta(seconds=1), + score=65, + smart_detect_types=[SmartDetectObjectType.PERSON], + smart_detect_event_ids=[], + camera_id=doorbell.id, + api=ufp.api, + ) + + new_camera = doorbell.copy() + new_camera.is_smart_detected = True + new_camera.last_smart_detect_event_ids[SmartDetectObjectType.PERSON] = event.id + + ufp.api.bootstrap.cameras = {new_camera.id: new_camera} + ufp.api.bootstrap.events = {event.id: event} + + mock_msg = Mock() + mock_msg.changed_data = {} + mock_msg.new_obj = event + ufp.ws_msg(mock_msg) + + await hass.async_block_till_done() + + entity_events = [event for event in events if event.data["entity_id"] == entity_id] + assert len(entity_events) == 3 + assert entity_events[0].data["new_state"].state == STATE_OFF + assert entity_events[1].data["new_state"].state == STATE_ON + assert entity_events[2].data["new_state"].state == STATE_OFF + + # Event is already seen and has end, should now be off + state = hass.states.get(entity_id) + assert state + assert state.state == STATE_OFF + + # Now send an event that has an end right away + event = Event( + model=ModelType.EVENT, + id="new_event_id", + type=EventType.SMART_DETECT, + start=fixed_now - timedelta(seconds=1), + end=fixed_now + timedelta(seconds=1), + score=80, + smart_detect_types=[SmartDetectObjectType.PERSON], + smart_detect_event_ids=[], + camera_id=doorbell.id, + api=ufp.api, + ) + + new_camera = doorbell.copy() + new_camera.is_smart_detected = True + new_camera.last_smart_detect_event_ids[SmartDetectObjectType.PERSON] = event.id + + ufp.api.bootstrap.cameras = {new_camera.id: new_camera} + ufp.api.bootstrap.events = {event.id: event} + + mock_msg = Mock() + mock_msg.changed_data = {} + mock_msg.new_obj = event + + state_changes: list[HAEvent[EventStateChangedData]] = async_capture_events( + hass, EVENT_STATE_CHANGED + ) + ufp.ws_msg(mock_msg) + + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert state + assert state.state == STATE_OFF + + assert len(state_changes) == 2 + + on_event = state_changes[0] + state = on_event.data["new_state"] + assert state + assert state.state == STATE_ON + assert state.attributes[ATTR_ATTRIBUTION] == DEFAULT_ATTRIBUTION + assert state.attributes[ATTR_EVENT_SCORE] == 80 + + off_event = state_changes[1] + state = off_event.data["new_state"] + assert state + assert state.state == STATE_OFF + assert ATTR_EVENT_SCORE not in state.attributes + + # replay and ensure ignored + ufp.ws_msg(mock_msg) + await hass.async_block_till_done() + assert len(state_changes) == 2 diff --git a/tests/components/unifiprotect/test_config_flow.py b/tests/components/unifiprotect/test_config_flow.py index 5d02e1cf09891f..8bfdc004092503 100644 --- a/tests/components/unifiprotect/test_config_flow.py +++ b/tests/components/unifiprotect/test_config_flow.py @@ -224,13 +224,7 @@ async def test_form_reauth_auth( ) mock_config.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": mock_config.entry_id, - }, - ) + result = await mock_config.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert not result["errors"] flows = hass.config_entries.flow.async_progress_by_handler(DOMAIN) diff --git a/tests/components/upb/test_config_flow.py b/tests/components/upb/test_config_flow.py index 5f28f1d9b17994..59a4e97d22b460 100644 --- a/tests/components/upb/test_config_flow.py +++ b/tests/components/upb/test_config_flow.py @@ -114,42 +114,3 @@ async def test_form_user_with_already_configured(hass: HomeAssistant) -> None: assert result2["type"] is FlowResultType.ABORT assert result2["reason"] == "already_configured" await hass.async_block_till_done() - - -async def test_form_import(hass: HomeAssistant) -> None: - """Test we get the form with import source.""" - - with ( - mocked_upb(), - patch( - "homeassistant.components.upb.async_setup_entry", return_value=True - ) as mock_setup_entry, - ): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_IMPORT}, - data={"host": "tcp://42.4.2.42", "file_path": "upb.upe"}, - ) - await hass.async_block_till_done() - - assert result["type"] is FlowResultType.CREATE_ENTRY - assert result["title"] == "UPB" - - assert result["data"] == {"host": "tcp://42.4.2.42", "file_path": "upb.upe"} - assert len(mock_setup_entry.mock_calls) == 1 - - -async def test_form_junk_input(hass: HomeAssistant) -> None: - """Test we get the form with import source.""" - - with mocked_upb(): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_IMPORT}, - data={"foo": "goo", "goo": "foo"}, - ) - - assert result["type"] is FlowResultType.FORM - assert result["errors"] == {"base": "unknown"} - - await hass.async_block_till_done() diff --git a/tests/components/uptimerobot/test_config_flow.py b/tests/components/uptimerobot/test_config_flow.py index 1cf0a358a87dda..3ba5ad696a6864 100644 --- a/tests/components/uptimerobot/test_config_flow.py +++ b/tests/components/uptimerobot/test_config_flow.py @@ -168,15 +168,7 @@ async def test_reauthentication( old_entry = MockConfigEntry(**MOCK_UPTIMEROBOT_CONFIG_ENTRY_DATA) old_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": old_entry.unique_id, - "entry_id": old_entry.entry_id, - }, - data=old_entry.data, - ) + result = await old_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["errors"] is None @@ -209,15 +201,7 @@ async def test_reauthentication_failure( old_entry = MockConfigEntry(**MOCK_UPTIMEROBOT_CONFIG_ENTRY_DATA) old_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": old_entry.unique_id, - "entry_id": old_entry.entry_id, - }, - data=old_entry.data, - ) + result = await old_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["errors"] is None @@ -253,15 +237,7 @@ async def test_reauthentication_failure_no_existing_entry( ) old_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": old_entry.unique_id, - "entry_id": old_entry.entry_id, - }, - data=old_entry.data, - ) + result = await old_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["errors"] is None @@ -294,15 +270,7 @@ async def test_reauthentication_failure_account_not_matching( old_entry = MockConfigEntry(**MOCK_UPTIMEROBOT_CONFIG_ENTRY_DATA) old_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": old_entry.unique_id, - "entry_id": old_entry.entry_id, - }, - data=old_entry.data, - ) + result = await old_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["errors"] is None diff --git a/tests/components/verisure/test_config_flow.py b/tests/components/verisure/test_config_flow.py index cf478b093c07c1..e6dd11669d1c17 100644 --- a/tests/components/verisure/test_config_flow.py +++ b/tests/components/verisure/test_config_flow.py @@ -352,15 +352,7 @@ async def test_reauth_flow( """Test a reauthentication flow.""" mock_config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": mock_config_entry.unique_id, - "entry_id": mock_config_entry.entry_id, - }, - data=mock_config_entry.data, - ) + result = await mock_config_entry.start_reauth_flow(hass) assert result.get("step_id") == "reauth_confirm" assert result.get("type") is FlowResultType.FORM assert result.get("errors") == {} @@ -395,15 +387,7 @@ async def test_reauth_flow_with_mfa( """Test a reauthentication flow.""" mock_config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": mock_config_entry.unique_id, - "entry_id": mock_config_entry.entry_id, - }, - data=mock_config_entry.data, - ) + result = await mock_config_entry.start_reauth_flow(hass) assert result.get("step_id") == "reauth_confirm" assert result.get("type") is FlowResultType.FORM assert result.get("errors") == {} @@ -466,15 +450,7 @@ async def test_reauth_flow_errors( """Test a reauthentication flow.""" mock_config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": mock_config_entry.unique_id, - "entry_id": mock_config_entry.entry_id, - }, - data=mock_config_entry.data, - ) + result = await mock_config_entry.start_reauth_flow(hass) mock_verisure_config_flow.login.side_effect = side_effect result2 = await hass.config_entries.flow.async_configure( diff --git a/tests/components/vicare/fixtures/ViAir300F.json b/tests/components/vicare/fixtures/ViAir300F.json index b1ec747e127815..090c7a81ddf9e2 100644 --- a/tests/components/vicare/fixtures/ViAir300F.json +++ b/tests/components/vicare/fixtures/ViAir300F.json @@ -50,7 +50,7 @@ "properties": { "value": { "type": "string", - "value": "################" + "value": "deviceSerialViAir300F" } }, "timestamp": "2024-03-20T01:29:35.549Z", diff --git a/tests/components/vicare/fixtures/Vitodens300W.json b/tests/components/vicare/fixtures/Vitodens300W.json index 4cf67ebe0f7dc8..d183146e94dce0 100644 --- a/tests/components/vicare/fixtures/Vitodens300W.json +++ b/tests/components/vicare/fixtures/Vitodens300W.json @@ -1,5 +1,22 @@ { "data": [ + { + "apiVersion": 1, + "commands": {}, + "deviceId": "0", + "feature": "device.serial", + "gatewayId": "################", + "isEnabled": true, + "isReady": true, + "properties": { + "value": { + "type": "string", + "value": "deviceSerialVitodens300W" + } + }, + "timestamp": "2024-07-30T20:03:40.073Z", + "uri": "https://api.viessmann.com/iot/v1/features/installations/#######/gateways/################/devices/0/features/device.serial" + }, { "properties": {}, "commands": {}, diff --git a/tests/components/vicare/snapshots/test_binary_sensor.ambr b/tests/components/vicare/snapshots/test_binary_sensor.ambr index a03a6150c45a04..f3e4d4e1c843f3 100644 --- a/tests/components/vicare/snapshots/test_binary_sensor.ambr +++ b/tests/components/vicare/snapshots/test_binary_sensor.ambr @@ -28,7 +28,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'burner', - 'unique_id': 'gateway0-burner_active-0', + 'unique_id': 'gateway0_deviceSerialVitodens300W-burner_active-0', 'unit_of_measurement': None, }) # --- @@ -75,7 +75,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'circulation_pump', - 'unique_id': 'gateway0-circulationpump_active-0', + 'unique_id': 'gateway0_deviceSerialVitodens300W-circulationpump_active-0', 'unit_of_measurement': None, }) # --- @@ -122,7 +122,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'circulation_pump', - 'unique_id': 'gateway0-circulationpump_active-1', + 'unique_id': 'gateway0_deviceSerialVitodens300W-circulationpump_active-1', 'unit_of_measurement': None, }) # --- @@ -169,7 +169,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'domestic_hot_water_charging', - 'unique_id': 'gateway0-charging_active', + 'unique_id': 'gateway0_deviceSerialVitodens300W-charging_active', 'unit_of_measurement': None, }) # --- @@ -216,7 +216,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'domestic_hot_water_circulation_pump', - 'unique_id': 'gateway0-dhw_circulationpump_active', + 'unique_id': 'gateway0_deviceSerialVitodens300W-dhw_circulationpump_active', 'unit_of_measurement': None, }) # --- @@ -263,7 +263,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'domestic_hot_water_pump', - 'unique_id': 'gateway0-dhw_pump_active', + 'unique_id': 'gateway0_deviceSerialVitodens300W-dhw_pump_active', 'unit_of_measurement': None, }) # --- @@ -310,7 +310,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'frost_protection', - 'unique_id': 'gateway0-frost_protection_active-0', + 'unique_id': 'gateway0_deviceSerialVitodens300W-frost_protection_active-0', 'unit_of_measurement': None, }) # --- @@ -356,7 +356,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'frost_protection', - 'unique_id': 'gateway0-frost_protection_active-1', + 'unique_id': 'gateway0_deviceSerialVitodens300W-frost_protection_active-1', 'unit_of_measurement': None, }) # --- diff --git a/tests/components/vicare/snapshots/test_button.ambr b/tests/components/vicare/snapshots/test_button.ambr index 01120b8b0d680c..9fadc6a983f223 100644 --- a/tests/components/vicare/snapshots/test_button.ambr +++ b/tests/components/vicare/snapshots/test_button.ambr @@ -28,7 +28,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'activate_onetimecharge', - 'unique_id': 'gateway0-activate_onetimecharge', + 'unique_id': 'gateway0_deviceSerialVitodens300W-activate_onetimecharge', 'unit_of_measurement': None, }) # --- diff --git a/tests/components/vicare/snapshots/test_climate.ambr b/tests/components/vicare/snapshots/test_climate.ambr index a01d1c43bea739..aea0ea879c2eae 100644 --- a/tests/components/vicare/snapshots/test_climate.ambr +++ b/tests/components/vicare/snapshots/test_climate.ambr @@ -40,7 +40,7 @@ 'previous_unique_id': None, 'supported_features': , 'translation_key': 'heating', - 'unique_id': 'gateway0-0', + 'unique_id': 'gateway0_deviceSerialVitodens300W-heating-0', 'unit_of_measurement': None, }) # --- @@ -123,7 +123,7 @@ 'previous_unique_id': None, 'supported_features': , 'translation_key': 'heating', - 'unique_id': 'gateway0-1', + 'unique_id': 'gateway0_deviceSerialVitodens300W-heating-1', 'unit_of_measurement': None, }) # --- diff --git a/tests/components/vicare/snapshots/test_diagnostics.ambr b/tests/components/vicare/snapshots/test_diagnostics.ambr index dfc29d46cc243f..120bdf7a333559 100644 --- a/tests/components/vicare/snapshots/test_diagnostics.ambr +++ b/tests/components/vicare/snapshots/test_diagnostics.ambr @@ -4,6 +4,24 @@ 'data': list([ dict({ 'data': list([ + dict({ + 'apiVersion': 1, + 'commands': dict({ + }), + 'deviceId': '0', + 'feature': 'device.serial', + 'gatewayId': '################', + 'isEnabled': True, + 'isReady': True, + 'properties': dict({ + 'value': dict({ + 'type': 'string', + 'value': 'deviceSerialVitodens300W', + }), + }), + 'timestamp': '2024-07-30T20:03:40.073Z', + 'uri': 'https://api.viessmann.com/iot/v1/features/installations/#######/gateways/################/devices/0/features/device.serial', + }), dict({ 'apiVersion': 1, 'commands': dict({ diff --git a/tests/components/vicare/snapshots/test_fan.ambr b/tests/components/vicare/snapshots/test_fan.ambr index 48c8d72856924f..8ec4bc41d8d33f 100644 --- a/tests/components/vicare/snapshots/test_fan.ambr +++ b/tests/components/vicare/snapshots/test_fan.ambr @@ -35,7 +35,7 @@ 'previous_unique_id': None, 'supported_features': , 'translation_key': 'ventilation', - 'unique_id': 'gateway0-ventilation', + 'unique_id': 'gateway0_deviceSerialViAir300F-ventilation', 'unit_of_measurement': None, }) # --- diff --git a/tests/components/vicare/snapshots/test_number.ambr b/tests/components/vicare/snapshots/test_number.ambr index a55c29ab8c165d..5a030fc0213937 100644 --- a/tests/components/vicare/snapshots/test_number.ambr +++ b/tests/components/vicare/snapshots/test_number.ambr @@ -33,7 +33,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'comfort_temperature', - 'unique_id': 'gateway0-comfort_temperature-0', + 'unique_id': 'gateway0_deviceSerialVitodens300W-comfort_temperature-0', 'unit_of_measurement': , }) # --- @@ -90,7 +90,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'comfort_temperature', - 'unique_id': 'gateway0-comfort_temperature-1', + 'unique_id': 'gateway0_deviceSerialVitodens300W-comfort_temperature-1', 'unit_of_measurement': , }) # --- @@ -147,7 +147,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'heating_curve_shift', - 'unique_id': 'gateway0-heating curve shift-0', + 'unique_id': 'gateway0_deviceSerialVitodens300W-heating curve shift-0', 'unit_of_measurement': , }) # --- @@ -204,7 +204,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'heating_curve_shift', - 'unique_id': 'gateway0-heating curve shift-1', + 'unique_id': 'gateway0_deviceSerialVitodens300W-heating curve shift-1', 'unit_of_measurement': , }) # --- @@ -261,7 +261,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'heating_curve_slope', - 'unique_id': 'gateway0-heating curve slope-0', + 'unique_id': 'gateway0_deviceSerialVitodens300W-heating curve slope-0', 'unit_of_measurement': None, }) # --- @@ -316,7 +316,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'heating_curve_slope', - 'unique_id': 'gateway0-heating curve slope-1', + 'unique_id': 'gateway0_deviceSerialVitodens300W-heating curve slope-1', 'unit_of_measurement': None, }) # --- @@ -371,7 +371,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'normal_temperature', - 'unique_id': 'gateway0-normal_temperature-0', + 'unique_id': 'gateway0_deviceSerialVitodens300W-normal_temperature-0', 'unit_of_measurement': , }) # --- @@ -428,7 +428,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'normal_temperature', - 'unique_id': 'gateway0-normal_temperature-1', + 'unique_id': 'gateway0_deviceSerialVitodens300W-normal_temperature-1', 'unit_of_measurement': , }) # --- @@ -485,7 +485,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'reduced_temperature', - 'unique_id': 'gateway0-reduced_temperature-0', + 'unique_id': 'gateway0_deviceSerialVitodens300W-reduced_temperature-0', 'unit_of_measurement': , }) # --- @@ -542,7 +542,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'reduced_temperature', - 'unique_id': 'gateway0-reduced_temperature-1', + 'unique_id': 'gateway0_deviceSerialVitodens300W-reduced_temperature-1', 'unit_of_measurement': , }) # --- @@ -565,3 +565,60 @@ 'state': 'unavailable', }) # --- +# name: test_all_entities[number.model0_dhw_temperature-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'max': 100.0, + 'min': 0.0, + 'mode': , + 'step': 1, + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'number', + 'entity_category': , + 'entity_id': 'number.model0_dhw_temperature', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'DHW temperature', + 'platform': 'vicare', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'dhw_temperature', + 'unique_id': 'gateway0_deviceSerialVitodens300W-dhw_temperature', + 'unit_of_measurement': , + }) +# --- +# name: test_all_entities[number.model0_dhw_temperature-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'temperature', + 'friendly_name': 'model0 DHW temperature', + 'max': 100.0, + 'min': 0.0, + 'mode': , + 'step': 1, + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'number.model0_dhw_temperature', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'unavailable', + }) +# --- diff --git a/tests/components/vicare/snapshots/test_sensor.ambr b/tests/components/vicare/snapshots/test_sensor.ambr index 7bbac75bedc373..43e5b713293f55 100644 --- a/tests/components/vicare/snapshots/test_sensor.ambr +++ b/tests/components/vicare/snapshots/test_sensor.ambr @@ -30,7 +30,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'boiler_temperature', - 'unique_id': 'gateway0-boiler_temperature', + 'unique_id': 'gateway0_deviceSerialVitodens300W-boiler_temperature', 'unit_of_measurement': , }) # --- @@ -81,7 +81,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'burner_hours', - 'unique_id': 'gateway0-burner_hours-0', + 'unique_id': 'gateway0_deviceSerialVitodens300W-burner_hours-0', 'unit_of_measurement': , }) # --- @@ -131,7 +131,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'burner_modulation', - 'unique_id': 'gateway0-burner_modulation-0', + 'unique_id': 'gateway0_deviceSerialVitodens300W-burner_modulation-0', 'unit_of_measurement': '%', }) # --- @@ -181,7 +181,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'burner_starts', - 'unique_id': 'gateway0-burner_starts-0', + 'unique_id': 'gateway0_deviceSerialVitodens300W-burner_starts-0', 'unit_of_measurement': None, }) # --- @@ -230,7 +230,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'hotwater_gas_consumption_heating_this_month', - 'unique_id': 'gateway0-hotwater_gas_consumption_heating_this_month', + 'unique_id': 'gateway0_deviceSerialVitodens300W-hotwater_gas_consumption_heating_this_month', 'unit_of_measurement': None, }) # --- @@ -279,7 +279,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'hotwater_gas_consumption_heating_this_week', - 'unique_id': 'gateway0-hotwater_gas_consumption_heating_this_week', + 'unique_id': 'gateway0_deviceSerialVitodens300W-hotwater_gas_consumption_heating_this_week', 'unit_of_measurement': None, }) # --- @@ -328,7 +328,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'hotwater_gas_consumption_heating_this_year', - 'unique_id': 'gateway0-hotwater_gas_consumption_heating_this_year', + 'unique_id': 'gateway0_deviceSerialVitodens300W-hotwater_gas_consumption_heating_this_year', 'unit_of_measurement': None, }) # --- @@ -377,7 +377,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'hotwater_gas_consumption_today', - 'unique_id': 'gateway0-hotwater_gas_consumption_today', + 'unique_id': 'gateway0_deviceSerialVitodens300W-hotwater_gas_consumption_today', 'unit_of_measurement': None, }) # --- @@ -426,7 +426,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'hotwater_max_temperature', - 'unique_id': 'gateway0-hotwater_max_temperature', + 'unique_id': 'gateway0_deviceSerialVitodens300W-hotwater_max_temperature', 'unit_of_measurement': , }) # --- @@ -477,7 +477,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'hotwater_min_temperature', - 'unique_id': 'gateway0-hotwater_min_temperature', + 'unique_id': 'gateway0_deviceSerialVitodens300W-hotwater_min_temperature', 'unit_of_measurement': , }) # --- @@ -528,7 +528,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'power consumption this month', - 'unique_id': 'gateway0-power consumption this month', + 'unique_id': 'gateway0_deviceSerialVitodens300W-power consumption this month', 'unit_of_measurement': , }) # --- @@ -579,7 +579,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'power_consumption_this_year', - 'unique_id': 'gateway0-power consumption this year', + 'unique_id': 'gateway0_deviceSerialVitodens300W-power consumption this year', 'unit_of_measurement': , }) # --- @@ -630,7 +630,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'power_consumption_today', - 'unique_id': 'gateway0-power consumption today', + 'unique_id': 'gateway0_deviceSerialVitodens300W-power consumption today', 'unit_of_measurement': , }) # --- @@ -681,7 +681,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'gas_consumption_heating_this_month', - 'unique_id': 'gateway0-gas_consumption_heating_this_month', + 'unique_id': 'gateway0_deviceSerialVitodens300W-gas_consumption_heating_this_month', 'unit_of_measurement': None, }) # --- @@ -730,7 +730,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'gas_consumption_heating_this_week', - 'unique_id': 'gateway0-gas_consumption_heating_this_week', + 'unique_id': 'gateway0_deviceSerialVitodens300W-gas_consumption_heating_this_week', 'unit_of_measurement': None, }) # --- @@ -779,7 +779,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'gas_consumption_heating_this_year', - 'unique_id': 'gateway0-gas_consumption_heating_this_year', + 'unique_id': 'gateway0_deviceSerialVitodens300W-gas_consumption_heating_this_year', 'unit_of_measurement': None, }) # --- @@ -828,7 +828,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'gas_consumption_heating_today', - 'unique_id': 'gateway0-gas_consumption_heating_today', + 'unique_id': 'gateway0_deviceSerialVitodens300W-gas_consumption_heating_today', 'unit_of_measurement': None, }) # --- @@ -877,7 +877,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'outside_temperature', - 'unique_id': 'gateway0-outside_temperature', + 'unique_id': 'gateway0_deviceSerialVitodens300W-outside_temperature', 'unit_of_measurement': , }) # --- @@ -928,7 +928,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'power_consumption_this_week', - 'unique_id': 'gateway0-power consumption this week', + 'unique_id': 'gateway0_deviceSerialVitodens300W-power consumption this week', 'unit_of_measurement': , }) # --- @@ -979,7 +979,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'supply_temperature', - 'unique_id': 'gateway0-supply_temperature-0', + 'unique_id': 'gateway0_deviceSerialVitodens300W-supply_temperature-0', 'unit_of_measurement': , }) # --- @@ -1030,7 +1030,7 @@ 'previous_unique_id': None, 'supported_features': 0, 'translation_key': 'supply_temperature', - 'unique_id': 'gateway0-supply_temperature-1', + 'unique_id': 'gateway0_deviceSerialVitodens300W-supply_temperature-1', 'unit_of_measurement': , }) # --- diff --git a/tests/components/vicare/snapshots/test_water_heater.ambr b/tests/components/vicare/snapshots/test_water_heater.ambr index 5ab4fcc78bdb2a..bca04b1bbfaf03 100644 --- a/tests/components/vicare/snapshots/test_water_heater.ambr +++ b/tests/components/vicare/snapshots/test_water_heater.ambr @@ -31,7 +31,7 @@ 'previous_unique_id': None, 'supported_features': , 'translation_key': 'domestic_hot_water', - 'unique_id': 'gateway0-0', + 'unique_id': 'gateway0_deviceSerialVitodens300W-0', 'unit_of_measurement': None, }) # --- @@ -87,7 +87,7 @@ 'previous_unique_id': None, 'supported_features': , 'translation_key': 'domestic_hot_water', - 'unique_id': 'gateway0-1', + 'unique_id': 'gateway0_deviceSerialVitodens300W-1', 'unit_of_measurement': None, }) # --- diff --git a/tests/components/vicare/test_config_flow.py b/tests/components/vicare/test_config_flow.py index b823bb72dc9742..a522cf75d5dd4b 100644 --- a/tests/components/vicare/test_config_flow.py +++ b/tests/components/vicare/test_config_flow.py @@ -11,7 +11,7 @@ from homeassistant.components import dhcp from homeassistant.components.vicare.const import DOMAIN -from homeassistant.config_entries import SOURCE_DHCP, SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_DHCP, SOURCE_USER from homeassistant.const import CONF_CLIENT_ID, CONF_PASSWORD, CONF_USERNAME from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -104,11 +104,7 @@ async def test_step_reauth(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> ) config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH, "entry_id": config_entry.entry_id}, - data=VALID_CONFIG, - ) + result = await config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/vicare/test_init.py b/tests/components/vicare/test_init.py new file mode 100644 index 00000000000000..fea7b5985f1cb7 --- /dev/null +++ b/tests/components/vicare/test_init.py @@ -0,0 +1,99 @@ +"""Test ViCare migration.""" + +from unittest.mock import patch + +from homeassistant.components.vicare.const import DOMAIN +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers import device_registry as dr, entity_registry as er + +from . import MODULE +from .conftest import Fixture, MockPyViCare + +from tests.common import MockConfigEntry + + +# Device migration test can be removed in 2025.4.0 +async def test_device_migration( + hass: HomeAssistant, + device_registry: dr.DeviceRegistry, + mock_config_entry: MockConfigEntry, +) -> None: + """Test that the device registry is updated correctly.""" + fixtures: list[Fixture] = [Fixture({"type:boiler"}, "vicare/Vitodens300W.json")] + with ( + patch(f"{MODULE}.vicare_login", return_value=MockPyViCare(fixtures)), + patch(f"{MODULE}.PLATFORMS", [Platform.CLIMATE]), + ): + mock_config_entry.add_to_hass(hass) + + device_registry.async_get_or_create( + config_entry_id=mock_config_entry.entry_id, + identifiers={ + (DOMAIN, "gateway0"), + }, + ) + + await hass.config_entries.async_setup(mock_config_entry.entry_id) + + await hass.async_block_till_done() + + assert device_registry.async_get_device(identifiers={(DOMAIN, "gateway0")}) is None + + assert ( + device_registry.async_get_device( + identifiers={(DOMAIN, "gateway0_deviceSerialVitodens300W")} + ) + is not None + ) + + +# Entity migration test can be removed in 2025.4.0 +async def test_climate_entity_migration( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + mock_config_entry: MockConfigEntry, +) -> None: + """Test that the climate entity unique_id gets migrated correctly.""" + fixtures: list[Fixture] = [Fixture({"type:boiler"}, "vicare/Vitodens300W.json")] + with ( + patch(f"{MODULE}.vicare_login", return_value=MockPyViCare(fixtures)), + patch(f"{MODULE}.PLATFORMS", [Platform.CLIMATE]), + ): + mock_config_entry.add_to_hass(hass) + + entry1 = entity_registry.async_get_or_create( + domain=Platform.CLIMATE, + platform=DOMAIN, + config_entry=mock_config_entry, + unique_id="gateway0-0", + translation_key="heating", + ) + entry2 = entity_registry.async_get_or_create( + domain=Platform.CLIMATE, + platform=DOMAIN, + config_entry=mock_config_entry, + unique_id="gateway0_deviceSerialVitodens300W-heating-1", + translation_key="heating", + ) + entry3 = entity_registry.async_get_or_create( + domain=Platform.CLIMATE, + platform=DOMAIN, + config_entry=mock_config_entry, + unique_id="gateway1-0", + translation_key="heating", + ) + + await hass.config_entries.async_setup(mock_config_entry.entry_id) + + await hass.async_block_till_done() + + assert ( + entity_registry.async_get(entry1.entity_id).unique_id + == "gateway0_deviceSerialVitodens300W-heating-0" + ) + assert ( + entity_registry.async_get(entry2.entity_id).unique_id + == "gateway0_deviceSerialVitodens300W-heating-1" + ) + assert entity_registry.async_get(entry3.entity_id).unique_id == "gateway1-0" diff --git a/tests/components/vicare/test_types.py b/tests/components/vicare/test_types.py index 575e549f0d9aa2..13d8255cf8d9ba 100644 --- a/tests/components/vicare/test_types.py +++ b/tests/components/vicare/test_types.py @@ -3,7 +3,8 @@ import pytest from homeassistant.components.climate import PRESET_COMFORT, PRESET_SLEEP -from homeassistant.components.vicare.types import HeatingProgram, VentilationMode +from homeassistant.components.vicare.fan import VentilationMode +from homeassistant.components.vicare.types import HeatingProgram @pytest.mark.parametrize( diff --git a/tests/components/vlc_telnet/test_config_flow.py b/tests/components/vlc_telnet/test_config_flow.py index 54edafab14a74d..d29a2c06beb623 100644 --- a/tests/components/vlc_telnet/test_config_flow.py +++ b/tests/components/vlc_telnet/test_config_flow.py @@ -153,15 +153,7 @@ async def test_reauth_flow(hass: HomeAssistant) -> None: entry = MockConfigEntry(domain=DOMAIN, data=entry_data) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - "unique_id": entry.unique_id, - }, - data=entry_data, - ) + result = await entry.start_reauth_flow(hass) with ( patch("homeassistant.components.vlc_telnet.config_flow.Client.connect"), @@ -209,15 +201,7 @@ async def test_reauth_errors( entry = MockConfigEntry(domain=DOMAIN, data=entry_data) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - "unique_id": entry.unique_id, - }, - data=entry_data, - ) + result = await entry.start_reauth_flow(hass) with ( patch( diff --git a/tests/components/vodafone_station/test_config_flow.py b/tests/components/vodafone_station/test_config_flow.py index 0492d32070fb0b..3a54f250871666 100644 --- a/tests/components/vodafone_station/test_config_flow.py +++ b/tests/components/vodafone_station/test_config_flow.py @@ -7,7 +7,7 @@ from homeassistant.components.device_tracker import CONF_CONSIDER_HOME from homeassistant.components.vodafone_station.const import DOMAIN -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_USER from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -124,6 +124,9 @@ async def test_reauth_successful(hass: HomeAssistant) -> None: mock_config = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_DATA) mock_config.add_to_hass(hass) + result = await mock_config.start_reauth_flow(hass) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reauth_confirm" with ( patch( @@ -136,15 +139,6 @@ async def test_reauth_successful(hass: HomeAssistant) -> None: "homeassistant.components.vodafone_station.async_setup_entry", ), ): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH, "entry_id": mock_config.entry_id}, - data=mock_config.data, - ) - - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "reauth_confirm" - result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={ @@ -172,6 +166,10 @@ async def test_reauth_not_successful(hass: HomeAssistant, side_effect, error) -> mock_config = MockConfigEntry(domain=DOMAIN, data=MOCK_USER_DATA) mock_config.add_to_hass(hass) + result = await mock_config.start_reauth_flow(hass) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reauth_confirm" + with ( patch( "homeassistant.components.vodafone_station.config_flow.VodafoneStationSercommApi.login", @@ -184,15 +182,6 @@ async def test_reauth_not_successful(hass: HomeAssistant, side_effect, error) -> "homeassistant.components.vodafone_station.async_setup_entry", ), ): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH, "entry_id": mock_config.entry_id}, - data=mock_config.data, - ) - - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "reauth_confirm" - result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={ diff --git a/tests/components/volvooncall/test_config_flow.py b/tests/components/volvooncall/test_config_flow.py index 8bf8bcc7412ff7..5268432c17e500 100644 --- a/tests/components/volvooncall/test_config_flow.py +++ b/tests/components/volvooncall/test_config_flow.py @@ -153,13 +153,7 @@ async def test_reauth(hass: HomeAssistant) -> None: ) first_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": first_entry.entry_id, - }, - ) + result = await first_entry.start_reauth_flow(hass) # the first form is just the confirmation prompt assert result["type"] is FlowResultType.FORM diff --git a/tests/components/vulcan/test_config_flow.py b/tests/components/vulcan/test_config_flow.py index 3311f3c71b2935..a72e77b32e8e31 100644 --- a/tests/components/vulcan/test_config_flow.py +++ b/tests/components/vulcan/test_config_flow.py @@ -137,14 +137,13 @@ async def test_config_flow_reauth_success( mock_student.return_value = [ Student.load(load_fixture("fake_student_1.json", "vulcan")) ] - MockConfigEntry( + entry = MockConfigEntry( domain=const.DOMAIN, unique_id="0", data={"student_id": "0"}, - ).add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - const.DOMAIN, context={"source": config_entries.SOURCE_REAUTH} ) + entry.add_to_hass(hass) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -176,14 +175,13 @@ async def test_config_flow_reauth_without_matching_entries( mock_student.return_value = [ Student.load(load_fixture("fake_student_1.json", "vulcan")) ] - MockConfigEntry( + entry = MockConfigEntry( domain=const.DOMAIN, unique_id="0", data={"student_id": "1"}, - ).add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - const.DOMAIN, context={"source": config_entries.SOURCE_REAUTH} ) + entry.add_to_hass(hass) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -206,9 +204,13 @@ async def test_config_flow_reauth_with_errors( """Test reauth config flow with errors.""" mock_keystore.return_value = fake_keystore mock_account.return_value = fake_account - result = await hass.config_entries.flow.async_init( - const.DOMAIN, context={"source": config_entries.SOURCE_REAUTH} + entry = MockConfigEntry( + domain=const.DOMAIN, + unique_id="0", + data={"student_id": "0"}, ) + entry.add_to_hass(hass) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" assert result["errors"] == {} diff --git a/tests/components/wallbox/__init__.py b/tests/components/wallbox/__init__.py index f21e895b3a7f6d..f4258ea0d4901d 100644 --- a/tests/components/wallbox/__init__.py +++ b/tests/components/wallbox/__init__.py @@ -14,11 +14,15 @@ CHARGER_CURRENT_VERSION_KEY, CHARGER_DATA_KEY, CHARGER_ENERGY_PRICE_KEY, + CHARGER_FEATURES_KEY, CHARGER_LOCKED_UNLOCKED_KEY, CHARGER_MAX_AVAILABLE_POWER_KEY, CHARGER_MAX_CHARGING_CURRENT_KEY, + CHARGER_MAX_ICP_CURRENT_KEY, CHARGER_NAME_KEY, CHARGER_PART_NUMBER_KEY, + CHARGER_PLAN_KEY, + CHARGER_POWER_BOOST_KEY, CHARGER_SERIAL_NUMBER_KEY, CHARGER_SOFTWARE_KEY, CHARGER_STATUS_ID_KEY, @@ -45,6 +49,8 @@ CHARGER_PART_NUMBER_KEY: "PLP1-0-2-4-9-002-E", CHARGER_SOFTWARE_KEY: {CHARGER_CURRENT_VERSION_KEY: "5.5.10"}, CHARGER_CURRENCY_KEY: {"code": "EUR/kWh"}, + CHARGER_MAX_ICP_CURRENT_KEY: 20, + CHARGER_PLAN_KEY: {CHARGER_FEATURES_KEY: [CHARGER_POWER_BOOST_KEY]}, }, } @@ -64,6 +70,8 @@ CHARGER_PART_NUMBER_KEY: "QSP1-0-2-4-9-002-E", CHARGER_SOFTWARE_KEY: {CHARGER_CURRENT_VERSION_KEY: "5.5.10"}, CHARGER_CURRENCY_KEY: {"code": "EUR/kWh"}, + CHARGER_MAX_ICP_CURRENT_KEY: 20, + CHARGER_PLAN_KEY: {CHARGER_FEATURES_KEY: [CHARGER_POWER_BOOST_KEY]}, }, } diff --git a/tests/components/wallbox/const.py b/tests/components/wallbox/const.py index 452b3af0af842d..a86ae9fc3b95c9 100644 --- a/tests/components/wallbox/const.py +++ b/tests/components/wallbox/const.py @@ -9,6 +9,7 @@ MOCK_NUMBER_ENTITY_ID = "number.wallbox_wallboxname_maximum_charging_current" MOCK_NUMBER_ENTITY_ENERGY_PRICE_ID = "number.wallbox_wallboxname_energy_price" +MOCK_NUMBER_ENTITY_ICP_CURRENT_ID = "number.wallbox_wallboxname_maximum_icp_current" MOCK_LOCK_ENTITY_ID = "lock.wallbox_wallboxname_lock" MOCK_SENSOR_CHARGING_SPEED_ID = "sensor.wallbox_wallboxname_charging_speed" MOCK_SENSOR_CHARGING_POWER_ID = "sensor.wallbox_wallboxname_charging_power" diff --git a/tests/components/wallbox/test_config_flow.py b/tests/components/wallbox/test_config_flow.py index c0ff0b19c94d34..cc38576eb2f19e 100644 --- a/tests/components/wallbox/test_config_flow.py +++ b/tests/components/wallbox/test_config_flow.py @@ -160,13 +160,7 @@ async def test_form_reauth(hass: HomeAssistant, entry: MockConfigEntry) -> None: status_code=200, ) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - }, - ) + result = await entry.start_reauth_flow(hass) result2 = await hass.config_entries.flow.async_configure( result["flow_id"], @@ -201,13 +195,7 @@ async def test_form_reauth_invalid(hass: HomeAssistant, entry: MockConfigEntry) status_code=200, ) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - }, - ) + result = await entry.start_reauth_flow(hass) result2 = await hass.config_entries.flow.async_configure( result["flow_id"], diff --git a/tests/components/wallbox/test_number.py b/tests/components/wallbox/test_number.py index 5d782224ce5b1a..0a8b1aa120716b 100644 --- a/tests/components/wallbox/test_number.py +++ b/tests/components/wallbox/test_number.py @@ -6,9 +6,12 @@ import requests_mock from homeassistant.components.input_number import ATTR_VALUE, SERVICE_SET_VALUE +from homeassistant.components.number import DOMAIN as NUMBER_DOMAIN +from homeassistant.components.wallbox import InvalidAuth from homeassistant.components.wallbox.const import ( CHARGER_ENERGY_PRICE_KEY, CHARGER_MAX_CHARGING_CURRENT_KEY, + CHARGER_MAX_ICP_CURRENT_KEY, ) from homeassistant.const import ATTR_ENTITY_ID from homeassistant.core import HomeAssistant @@ -20,7 +23,11 @@ setup_integration_bidir, setup_integration_platform_not_ready, ) -from .const import MOCK_NUMBER_ENTITY_ENERGY_PRICE_ID, MOCK_NUMBER_ENTITY_ID +from .const import ( + MOCK_NUMBER_ENTITY_ENERGY_PRICE_ID, + MOCK_NUMBER_ENTITY_ICP_CURRENT_ID, + MOCK_NUMBER_ENTITY_ID, +) from tests.common import MockConfigEntry @@ -212,3 +219,99 @@ async def test_wallbox_number_class_platform_not_ready( assert state is None await hass.config_entries.async_unload(entry.entry_id) + + +async def test_wallbox_number_class_icp_energy( + hass: HomeAssistant, entry: MockConfigEntry +) -> None: + """Test wallbox sensor class.""" + + await setup_integration(hass, entry) + + with requests_mock.Mocker() as mock_request: + mock_request.get( + "https://user-api.wall-box.com/users/signin", + json=authorisation_response, + status_code=200, + ) + + mock_request.post( + "https://api.wall-box.com/chargers/config/12345", + json={CHARGER_MAX_ICP_CURRENT_KEY: 10}, + status_code=200, + ) + + await hass.services.async_call( + NUMBER_DOMAIN, + SERVICE_SET_VALUE, + { + ATTR_ENTITY_ID: MOCK_NUMBER_ENTITY_ICP_CURRENT_ID, + ATTR_VALUE: 10, + }, + blocking=True, + ) + await hass.config_entries.async_unload(entry.entry_id) + + +async def test_wallbox_number_class_icp_energy_auth_error( + hass: HomeAssistant, entry: MockConfigEntry +) -> None: + """Test wallbox sensor class.""" + + await setup_integration(hass, entry) + + with requests_mock.Mocker() as mock_request: + mock_request.get( + "https://user-api.wall-box.com/users/signin", + json=authorisation_response, + status_code=200, + ) + mock_request.post( + "https://api.wall-box.com/chargers/config/12345", + json={CHARGER_MAX_ICP_CURRENT_KEY: 10}, + status_code=403, + ) + + with pytest.raises(InvalidAuth): + await hass.services.async_call( + NUMBER_DOMAIN, + SERVICE_SET_VALUE, + { + ATTR_ENTITY_ID: MOCK_NUMBER_ENTITY_ICP_CURRENT_ID, + ATTR_VALUE: 10, + }, + blocking=True, + ) + await hass.config_entries.async_unload(entry.entry_id) + + +async def test_wallbox_number_class_icp_energy_connection_error( + hass: HomeAssistant, entry: MockConfigEntry +) -> None: + """Test wallbox sensor class.""" + + await setup_integration(hass, entry) + + with requests_mock.Mocker() as mock_request: + mock_request.get( + "https://user-api.wall-box.com/users/signin", + json=authorisation_response, + status_code=200, + ) + mock_request.post( + "https://api.wall-box.com/chargers/config/12345", + json={CHARGER_MAX_ICP_CURRENT_KEY: 10}, + status_code=404, + ) + + with pytest.raises(ConnectionError): + await hass.services.async_call( + NUMBER_DOMAIN, + SERVICE_SET_VALUE, + { + ATTR_ENTITY_ID: MOCK_NUMBER_ENTITY_ICP_CURRENT_ID, + ATTR_VALUE: 10, + }, + blocking=True, + ) + await hass.config_entries.async_unload(entry.entry_id) diff --git a/tests/components/watttime/test_config_flow.py b/tests/components/watttime/test_config_flow.py index f8eee6b48bfadc..5087717491f655 100644 --- a/tests/components/watttime/test_config_flow.py +++ b/tests/components/watttime/test_config_flow.py @@ -25,6 +25,8 @@ from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType +from tests.common import MockConfigEntry + @pytest.mark.parametrize( ("exc", "error"), @@ -144,21 +146,16 @@ async def test_show_form_user(hass: HomeAssistant) -> None: async def test_step_reauth( - hass: HomeAssistant, config_auth, config_coordinates, config_entry, setup_watttime + hass: HomeAssistant, + config_entry: MockConfigEntry, + setup_watttime, ) -> None: """Test a full reauth flow.""" + result = await config_entry.start_reauth_flow(hass) with patch( "homeassistant.components.watttime.async_setup_entry", return_value=True, ): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_REAUTH}, - data={ - **config_auth, - **config_coordinates, - }, - ) result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={CONF_PASSWORD: "password"}, diff --git a/tests/components/weatherflow_cloud/test_config_flow.py b/tests/components/weatherflow_cloud/test_config_flow.py index 7ade007ceacf71..9dc5ad1322d401 100644 --- a/tests/components/weatherflow_cloud/test_config_flow.py +++ b/tests/components/weatherflow_cloud/test_config_flow.py @@ -4,7 +4,7 @@ from homeassistant import config_entries from homeassistant.components.weatherflow_cloud.const import DOMAIN -from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState +from homeassistant.config_entries import ConfigEntryState from homeassistant.const import CONF_API_TOKEN from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -111,15 +111,14 @@ async def test_reauth(hass: HomeAssistant, mock_get_stations_401_error) -> None: assert not await hass.config_entries.async_setup(entry.entry_id) assert entry.state is ConfigEntryState.SETUP_ERROR - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_REAUTH, "entry_id": entry.entry_id}, data=None - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_REAUTH, "entry_id": entry.entry_id}, - data={CONF_API_TOKEN: "SAME_SAME"}, + assert result["step_id"] == "reauth_confirm" + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], user_input={CONF_API_TOKEN: "SAME_SAME"} ) assert result["reason"] == "reauth_successful" assert result["type"] is FlowResultType.ABORT + assert entry.data[CONF_API_TOKEN] == "SAME_SAME" diff --git a/tests/components/webostv/test_config_flow.py b/tests/components/webostv/test_config_flow.py index 406bb9c88041b8..9b2983aab47a50 100644 --- a/tests/components/webostv/test_config_flow.py +++ b/tests/components/webostv/test_config_flow.py @@ -302,11 +302,7 @@ async def test_reauth_successful( entry = await setup_webostv(hass) assert client - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_REAUTH, "entry_id": entry.entry_id}, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) assert result["step_id"] == "reauth_confirm" result = await hass.config_entries.flow.async_configure(result["flow_id"]) @@ -339,11 +335,7 @@ async def test_reauth_errors( entry = await setup_webostv(hass) assert client - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_REAUTH, "entry_id": entry.entry_id}, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) assert result["step_id"] == "reauth_confirm" result = await hass.config_entries.flow.async_configure(result["flow_id"]) diff --git a/tests/components/websocket_api/test_commands.py b/tests/components/websocket_api/test_commands.py index 772a8ee793e601..54a87e033dc792 100644 --- a/tests/components/websocket_api/test_commands.py +++ b/tests/components/websocket_api/test_commands.py @@ -1262,6 +1262,54 @@ async def test_subscribe_unsubscribe_entities_specific_entities( } +async def test_subscribe_unsubscribe_entities_with_filter( + hass: HomeAssistant, + websocket_client: MockHAClientWebSocket, + hass_admin_user: MockUser, +) -> None: + """Test subscribe/unsubscribe entities with an entity filter.""" + hass.states.async_set("switch.not_included", "off") + hass.states.async_set("light.include", "off") + await websocket_client.send_json( + {"id": 7, "type": "subscribe_entities", "include": {"domains": ["light"]}} + ) + + msg = await websocket_client.receive_json() + assert msg["id"] == 7 + assert msg["type"] == const.TYPE_RESULT + assert msg["success"] + + msg = await websocket_client.receive_json() + assert msg["id"] == 7 + assert msg["type"] == "event" + assert msg["event"] == { + "a": { + "light.include": { + "a": {}, + "c": ANY, + "lc": ANY, + "s": "off", + } + } + } + hass.states.async_set("switch.not_included", "on") + hass.states.async_set("light.include", "on") + msg = await websocket_client.receive_json() + assert msg["id"] == 7 + assert msg["type"] == "event" + assert msg["event"] == { + "c": { + "light.include": { + "+": { + "c": ANY, + "lc": ANY, + "s": "on", + } + } + } + } + + async def test_render_template_renders_template( hass: HomeAssistant, websocket_client ) -> None: diff --git a/tests/components/whirlpool/test_config_flow.py b/tests/components/whirlpool/test_config_flow.py index e3896a436d4c4b..1240e1303e10da 100644 --- a/tests/components/whirlpool/test_config_flow.py +++ b/tests/components/whirlpool/test_config_flow.py @@ -235,15 +235,7 @@ async def test_reauth_flow(hass: HomeAssistant, region, brand) -> None: ) mock_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": mock_entry.unique_id, - "entry_id": mock_entry.entry_id, - }, - data=CONFIG_INPUT | {"region": region[0], "brand": brand[0]}, - ) + result = await mock_entry.start_reauth_flow(hass) assert result["step_id"] == "reauth_confirm" assert result["type"] is FlowResultType.FORM @@ -294,21 +286,7 @@ async def test_reauth_flow_auth_error(hass: HomeAssistant, region, brand) -> Non ) mock_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": mock_entry.unique_id, - "entry_id": mock_entry.entry_id, - }, - data={ - CONF_USERNAME: "test-username", - CONF_PASSWORD: "new-password", - "region": region[0], - "brand": brand[0], - }, - ) - + result = await mock_entry.start_reauth_flow(hass) assert result["step_id"] == "reauth_confirm" assert result["type"] is FlowResultType.FORM assert result["errors"] == {} @@ -345,15 +323,7 @@ async def test_reauth_flow_connnection_error( ) mock_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": mock_entry.unique_id, - "entry_id": mock_entry.entry_id, - }, - data=CONFIG_INPUT | {"region": region[0], "brand": brand[0]}, - ) + result = await mock_entry.start_reauth_flow(hass) assert result["step_id"] == "reauth_confirm" assert result["type"] is FlowResultType.FORM diff --git a/tests/components/withings/test_config_flow.py b/tests/components/withings/test_config_flow.py index 20bef90a31e562..39c8340a78ed5a 100644 --- a/tests/components/withings/test_config_flow.py +++ b/tests/components/withings/test_config_flow.py @@ -5,7 +5,7 @@ import pytest from homeassistant.components.withings.const import DOMAIN -from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER +from homeassistant.config_entries import SOURCE_USER from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType from homeassistant.helpers import config_entry_oauth2_flow @@ -145,14 +145,7 @@ async def test_config_reauth_profile( """Test reauth an existing profile reauthenticates the config entry.""" await setup_integration(hass, polling_config_entry) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "entry_id": polling_config_entry.entry_id, - }, - data=polling_config_entry.data, - ) + result = await polling_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" @@ -207,14 +200,7 @@ async def test_config_reauth_wrong_account( """Test reauth with wrong account.""" await setup_integration(hass, polling_config_entry) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": SOURCE_REAUTH, - "entry_id": polling_config_entry.entry_id, - }, - data=polling_config_entry.data, - ) + result = await polling_config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/xiaomi_ble/test_config_flow.py b/tests/components/xiaomi_ble/test_config_flow.py index b61615e0f79eac..f690665608b6d3 100644 --- a/tests/components/xiaomi_ble/test_config_flow.py +++ b/tests/components/xiaomi_ble/test_config_flow.py @@ -1083,16 +1083,7 @@ async def test_async_step_reauth_abort_early(hass: HomeAssistant) -> None: device = DeviceData() - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "entry_id": entry.entry_id, - "title_placeholders": {"name": entry.title}, - "unique_id": entry.unique_id, - }, - data=entry.data | {"device": device}, - ) + result = await entry.start_reauth_flow(hass, data={"device": device}) assert result["type"] is FlowResultType.ABORT assert result["reason"] == "reauth_successful" diff --git a/tests/components/xiaomi_ble/test_sensor.py b/tests/components/xiaomi_ble/test_sensor.py index 4d9a29e31117ea..11a20a62d02aaa 100644 --- a/tests/components/xiaomi_ble/test_sensor.py +++ b/tests/components/xiaomi_ble/test_sensor.py @@ -11,6 +11,7 @@ from homeassistant.const import ( ATTR_FRIENDLY_NAME, ATTR_UNIT_OF_MEASUREMENT, + STATE_ON, STATE_UNAVAILABLE, ) from homeassistant.core import HomeAssistant @@ -465,6 +466,115 @@ async def test_xiaomi_hhccjcy01_only_some_sources_connectable( await hass.async_block_till_done() +async def test_xiaomi_xmosb01xs(hass: HomeAssistant) -> None: + """Test XMOSB01XS multiple advertisements. + + This device has multiple advertisements before all sensors are visible. + """ + entry = MockConfigEntry( + domain=DOMAIN, + unique_id="DC:8E:95:23:07:B7", + data={"bindkey": "272b1c920ef435417c49228b8ab9a563"}, + ) + entry.add_to_hass(hass) + + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + assert len(hass.states.async_all()) == 0 + inject_bluetooth_service_info_bleak( + hass, + make_advertisement( + "DC:8E:95:23:07:B7", + ( + b"\x58\x59\x83\x46\x91\xb7\x07\x23\x95\x8e\xdc\xc7\x17\x61\xc1" + b"\x24\x03\x00\x25\x44\xb0\x65" + ), + connectable=False, + ), + ) + inject_bluetooth_service_info_bleak( + hass, + make_advertisement( + "DC:8E:95:23:07:B7", + b"\x10\x59\x83\x46\x90\xb7\x07\x23\x95\x8e\xdc", + connectable=False, + ), + ) + inject_bluetooth_service_info_bleak( + hass, + make_advertisement( + "DC:8E:95:23:07:B7", + b"\x48\x59\x83\x46\x9d\x34\x45\xec\xab\xda\x93\xf9\x24\x03\x00\x9e\x01\x6d\x3d", + connectable=False, + ), + ) + inject_bluetooth_service_info_bleak( + hass, + make_advertisement( + "DC:8E:95:23:07:B7", + ( + b"\x58\x59\x83\x46\xa9\xb7\x07\x23\x95\x8e\xdc\xc6\x59\xa2\xdc\xc5" + b"\x24\x03\x00\xa0\x4d\x0d\x45" + ), + connectable=False, + ), + ) + inject_bluetooth_service_info_bleak( + hass, + make_advertisement( + "DC:8E:95:23:07:B7", + ( + b"\x58\x59\x83\x46\xa4\xb7\x07\x23\x95\x8e\xdc\x77\x2a\xe2\x5c\x11" + b"\x24\x03\x00\xab\x87\x7b\xd7" + ), + connectable=False, + ), + ) + await hass.async_block_till_done() + assert len(hass.states.async_all()) == 4 + + occupancy_sensor = hass.states.get("binary_sensor.occupancy_sensor_07b7_occupancy") + occupancy_sensor_attribtes = occupancy_sensor.attributes + assert occupancy_sensor.state == STATE_ON + assert ( + occupancy_sensor_attribtes[ATTR_FRIENDLY_NAME] + == "Occupancy Sensor 07B7 Occupancy" + ) + + illum_sensor = hass.states.get("sensor.occupancy_sensor_07b7_illuminance") + illum_sensor_attr = illum_sensor.attributes + assert illum_sensor.state == "111.0" + assert illum_sensor_attr[ATTR_FRIENDLY_NAME] == "Occupancy Sensor 07B7 Illuminance" + assert illum_sensor_attr[ATTR_UNIT_OF_MEASUREMENT] == "lx" + assert illum_sensor_attr[ATTR_STATE_CLASS] == "measurement" + + illum_sensor = hass.states.get("sensor.occupancy_sensor_07b7_duration_detected") + illum_sensor_attr = illum_sensor.attributes + assert illum_sensor.state == "2" + assert ( + illum_sensor_attr[ATTR_FRIENDLY_NAME] + == "Occupancy Sensor 07B7 Duration detected" + ) + assert illum_sensor_attr[ATTR_UNIT_OF_MEASUREMENT] == "min" + assert illum_sensor_attr[ATTR_STATE_CLASS] == "measurement" + + illum_sensor = hass.states.get("sensor.occupancy_sensor_07b7_duration_cleared") + illum_sensor_attr = illum_sensor.attributes + assert illum_sensor.state == "2" + assert ( + illum_sensor_attr[ATTR_FRIENDLY_NAME] + == "Occupancy Sensor 07B7 Duration cleared" + ) + assert illum_sensor_attr[ATTR_UNIT_OF_MEASUREMENT] == "min" + assert illum_sensor_attr[ATTR_STATE_CLASS] == "measurement" + + assert await hass.config_entries.async_unload(entry.entry_id) + await hass.async_block_till_done() + + assert entry.data[CONF_SLEEPY_DEVICE] is True + + async def test_xiaomi_cgdk2_bind_key(hass: HomeAssistant) -> None: """Test CGDK2 bind key. diff --git a/tests/components/xiaomi_miio/test_config_flow.py b/tests/components/xiaomi_miio/test_config_flow.py index 707da4bff12ea8..146526c69a5a40 100644 --- a/tests/components/xiaomi_miio/test_config_flow.py +++ b/tests/components/xiaomi_miio/test_config_flow.py @@ -976,11 +976,7 @@ async def test_reauth(hass: HomeAssistant) -> None: assert await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() - result = await hass.config_entries.flow.async_init( - const.DOMAIN, - context={"source": config_entries.SOURCE_REAUTH}, - data=config_entry.data, - ) + result = await config_entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" diff --git a/tests/components/yale/__init__.py b/tests/components/yale/__init__.py new file mode 100644 index 00000000000000..7f72d348042d14 --- /dev/null +++ b/tests/components/yale/__init__.py @@ -0,0 +1 @@ +"""Tests for the yale component.""" diff --git a/tests/components/yale/conftest.py b/tests/components/yale/conftest.py new file mode 100644 index 00000000000000..3e633430846f81 --- /dev/null +++ b/tests/components/yale/conftest.py @@ -0,0 +1,72 @@ +"""Yale tests conftest.""" + +from unittest.mock import patch + +import pytest +from yalexs.manager.ratelimit import _RateLimitChecker + +from homeassistant.components.yale.const import DOMAIN +from homeassistant.core import HomeAssistant + +from .mocks import mock_client_credentials, mock_config_entry + +from tests.common import MockConfigEntry, load_fixture + + +@pytest.fixture(name="mock_discovery", autouse=True) +def mock_discovery_fixture(): + """Mock discovery to avoid loading the whole bluetooth stack.""" + with patch( + "homeassistant.components.yale.data.discovery_flow.async_create_flow" + ) as mock_discovery: + yield mock_discovery + + +@pytest.fixture(name="disable_ratelimit_checks", autouse=True) +def disable_ratelimit_checks_fixture(): + """Disable rate limit checks.""" + with patch.object(_RateLimitChecker, "register_wakeup"): + yield + + +@pytest.fixture(name="mock_config_entry") +def mock_config_entry_fixture(jwt: str) -> MockConfigEntry: + """Return the default mocked config entry.""" + return mock_config_entry(jwt=jwt) + + +@pytest.fixture(name="jwt") +def load_jwt_fixture() -> str: + """Load Fixture data.""" + return load_fixture("jwt", DOMAIN).strip("\n") + + +@pytest.fixture(name="reauth_jwt") +def load_reauth_jwt_fixture() -> str: + """Load Fixture data.""" + return load_fixture("reauth_jwt", DOMAIN).strip("\n") + + +@pytest.fixture(name="reauth_jwt_wrong_account") +def load_reauth_jwt_wrong_account_fixture() -> str: + """Load Fixture data.""" + return load_fixture("reauth_jwt_wrong_account", DOMAIN).strip("\n") + + +@pytest.fixture(name="client_credentials", autouse=True) +async def mock_client_credentials_fixture(hass: HomeAssistant) -> None: + """Mock client credentials.""" + await mock_client_credentials(hass) + + +@pytest.fixture(name="skip_cloud", autouse=True) +def skip_cloud_fixture(): + """Skip setting up cloud. + + Cloud already has its own tests for account link. + + We do not need to test it here as we only need to test our + usage of the oauth2 helpers. + """ + with patch("homeassistant.components.cloud.async_setup", return_value=True): + yield diff --git a/tests/components/yale/fixtures/get_activity.bridge_offline.json b/tests/components/yale/fixtures/get_activity.bridge_offline.json new file mode 100644 index 00000000000000..9c2ded966650de --- /dev/null +++ b/tests/components/yale/fixtures/get_activity.bridge_offline.json @@ -0,0 +1,36 @@ +[ + { + "entities": { + "activity": "mockActivity2", + "house": "123", + "device": "online_with_doorsense", + "callingUser": "mockUserId2", + "otherUser": "deleted" + }, + "callingUser": { + "LastName": "elven princess", + "UserID": "mockUserId2", + "FirstName": "Your favorite" + }, + "otherUser": { + "LastName": "User", + "UserName": "deleteduser", + "FirstName": "Unknown", + "UserID": "deleted", + "PhoneNo": "deleted" + }, + "deviceType": "lock", + "deviceName": "MockHouseTDoor", + "action": "associated_bridge_offline", + "dateTime": 1582007218000, + "info": { + "remote": true, + "DateLogActionID": "ABC+Time" + }, + "deviceID": "online_with_doorsense", + "house": { + "houseName": "MockHouse", + "houseID": "123" + } + } +] diff --git a/tests/components/yale/fixtures/get_activity.bridge_online.json b/tests/components/yale/fixtures/get_activity.bridge_online.json new file mode 100644 index 00000000000000..6f8b5e6a4a6449 --- /dev/null +++ b/tests/components/yale/fixtures/get_activity.bridge_online.json @@ -0,0 +1,36 @@ +[ + { + "entities": { + "activity": "mockActivity2", + "house": "123", + "device": "online_with_doorsense", + "callingUser": "mockUserId2", + "otherUser": "deleted" + }, + "callingUser": { + "LastName": "elven princess", + "UserID": "mockUserId2", + "FirstName": "Your favorite" + }, + "otherUser": { + "LastName": "User", + "UserName": "deleteduser", + "FirstName": "Unknown", + "UserID": "deleted", + "PhoneNo": "deleted" + }, + "deviceType": "lock", + "deviceName": "MockHouseTDoor", + "action": "associated_bridge_online", + "dateTime": 1582007218000, + "info": { + "remote": true, + "DateLogActionID": "ABC+Time" + }, + "deviceID": "online_with_doorsense", + "house": { + "houseName": "MockHouse", + "houseID": "123" + } + } +] diff --git a/tests/components/yale/fixtures/get_activity.doorbell_motion.json b/tests/components/yale/fixtures/get_activity.doorbell_motion.json new file mode 100644 index 00000000000000..cf0f231a49adb4 --- /dev/null +++ b/tests/components/yale/fixtures/get_activity.doorbell_motion.json @@ -0,0 +1,58 @@ +[ + { + "otherUser": { + "FirstName": "Unknown", + "UserName": "deleteduser", + "LastName": "User", + "UserID": "deleted", + "PhoneNo": "deleted" + }, + "dateTime": 1582663119959, + "deviceID": "K98GiDT45GUL", + "info": { + "videoUploadProgress": "in_progress", + "image": { + "resource_type": "image", + "etag": "fdsf", + "created_at": "2020-02-25T20:38:39Z", + "type": "upload", + "format": "jpg", + "version": 1582663119, + "secure_url": "https://res.cloudinary.com/updated_image.jpg", + "signature": "fdfdfd", + "url": "http://res.cloudinary.com/updated_image.jpg", + "bytes": 48545, + "placeholder": false, + "original_filename": "file", + "width": 720, + "tags": [], + "public_id": "xnsj5gphpzij9brifpf4", + "height": 576 + }, + "dvrID": "dvr", + "videoAvailable": false, + "hasSubscription": false + }, + "callingUser": { + "LastName": "User", + "UserName": "deleteduser", + "FirstName": "Unknown", + "UserID": "deleted", + "PhoneNo": "deleted" + }, + "house": { + "houseName": "K98GiDT45GUL", + "houseID": "na" + }, + "action": "doorbell_motion_detected", + "deviceType": "doorbell", + "entities": { + "otherUser": "deleted", + "house": "na", + "device": "K98GiDT45GUL", + "activity": "de5585cfd4eae900bb5ba3dc", + "callingUser": "deleted" + }, + "deviceName": "Front Door" + } +] diff --git a/tests/components/yale/fixtures/get_activity.jammed.json b/tests/components/yale/fixtures/get_activity.jammed.json new file mode 100644 index 00000000000000..782a13f9c73421 --- /dev/null +++ b/tests/components/yale/fixtures/get_activity.jammed.json @@ -0,0 +1,36 @@ +[ + { + "entities": { + "activity": "mockActivity2", + "house": "123", + "device": "online_with_doorsense", + "callingUser": "mockUserId2", + "otherUser": "deleted" + }, + "callingUser": { + "LastName": "elven princess", + "UserID": "mockUserId2", + "FirstName": "Your favorite" + }, + "otherUser": { + "LastName": "User", + "UserName": "deleteduser", + "FirstName": "Unknown", + "UserID": "deleted", + "PhoneNo": "deleted" + }, + "deviceType": "lock", + "deviceName": "MockHouseTDoor", + "action": "jammed", + "dateTime": 1582007218000, + "info": { + "remote": true, + "DateLogActionID": "ABC+Time" + }, + "deviceID": "online_with_doorsense", + "house": { + "houseName": "MockHouse", + "houseID": "123" + } + } +] diff --git a/tests/components/yale/fixtures/get_activity.lock.json b/tests/components/yale/fixtures/get_activity.lock.json new file mode 100644 index 00000000000000..b40e7d61ccfa9c --- /dev/null +++ b/tests/components/yale/fixtures/get_activity.lock.json @@ -0,0 +1,36 @@ +[ + { + "entities": { + "activity": "mockActivity2", + "house": "123", + "device": "online_with_doorsense", + "callingUser": "mockUserId2", + "otherUser": "deleted" + }, + "callingUser": { + "LastName": "elven princess", + "UserID": "mockUserId2", + "FirstName": "Your favorite" + }, + "otherUser": { + "LastName": "User", + "UserName": "deleteduser", + "FirstName": "Unknown", + "UserID": "deleted", + "PhoneNo": "deleted" + }, + "deviceType": "lock", + "deviceName": "MockHouseTDoor", + "action": "lock", + "dateTime": 1582007218000, + "info": { + "remote": true, + "DateLogActionID": "ABC+Time" + }, + "deviceID": "online_with_doorsense", + "house": { + "houseName": "MockHouse", + "houseID": "123" + } + } +] diff --git a/tests/components/yale/fixtures/get_activity.lock_from_autorelock.json b/tests/components/yale/fixtures/get_activity.lock_from_autorelock.json new file mode 100644 index 00000000000000..38c26ffb7dda96 --- /dev/null +++ b/tests/components/yale/fixtures/get_activity.lock_from_autorelock.json @@ -0,0 +1,36 @@ +[ + { + "entities": { + "activity": "mockActivity2", + "house": "123", + "device": "online_with_doorsense", + "callingUser": "mockUserId2", + "otherUser": "deleted" + }, + "callingUser": { + "LastName": "Relock", + "UserID": "automaticrelock", + "FirstName": "Auto" + }, + "otherUser": { + "LastName": "User", + "UserName": "deleteduser", + "FirstName": "Unknown", + "UserID": "deleted", + "PhoneNo": "deleted" + }, + "deviceType": "lock", + "deviceName": "MockHouseTDoor", + "action": "lock", + "dateTime": 1582007218000, + "info": { + "remote": false, + "DateLogActionID": "ABC+Time" + }, + "deviceID": "online_with_doorsense", + "house": { + "houseName": "MockHouse", + "houseID": "123" + } + } +] diff --git a/tests/components/yale/fixtures/get_activity.lock_from_bluetooth.json b/tests/components/yale/fixtures/get_activity.lock_from_bluetooth.json new file mode 100644 index 00000000000000..bfbc621e06497b --- /dev/null +++ b/tests/components/yale/fixtures/get_activity.lock_from_bluetooth.json @@ -0,0 +1,36 @@ +[ + { + "entities": { + "activity": "mockActivity2", + "house": "123", + "device": "online_with_doorsense", + "callingUser": "mockUserId2", + "otherUser": "deleted" + }, + "callingUser": { + "LastName": "elven princess", + "UserID": "mockUserId2", + "FirstName": "Your favorite" + }, + "otherUser": { + "LastName": "User", + "UserName": "deleteduser", + "FirstName": "Unknown", + "UserID": "deleted", + "PhoneNo": "deleted" + }, + "deviceType": "lock", + "deviceName": "MockHouseTDoor", + "action": "lock", + "dateTime": 1582007218000, + "info": { + "remote": false, + "DateLogActionID": "ABC+Time" + }, + "deviceID": "online_with_doorsense", + "house": { + "houseName": "MockHouse", + "houseID": "123" + } + } +] diff --git a/tests/components/yale/fixtures/get_activity.lock_from_keypad.json b/tests/components/yale/fixtures/get_activity.lock_from_keypad.json new file mode 100644 index 00000000000000..1b1e13e67ddeb2 --- /dev/null +++ b/tests/components/yale/fixtures/get_activity.lock_from_keypad.json @@ -0,0 +1,37 @@ +[ + { + "entities": { + "activity": "mockActivity2", + "house": "123", + "device": "online_with_doorsense", + "callingUser": "mockUserId2", + "otherUser": "deleted" + }, + "callingUser": { + "LastName": "elven princess", + "UserID": "mockUserId2", + "FirstName": "Your favorite" + }, + "otherUser": { + "LastName": "User", + "UserName": "deleteduser", + "FirstName": "Unknown", + "UserID": "deleted", + "PhoneNo": "deleted" + }, + "deviceType": "lock", + "deviceName": "MockHouseTDoor", + "action": "lock", + "dateTime": 1582007218000, + "info": { + "remote": false, + "keypad": true, + "DateLogActionID": "ABC+Time" + }, + "deviceID": "online_with_doorsense", + "house": { + "houseName": "MockHouse", + "houseID": "123" + } + } +] diff --git a/tests/components/yale/fixtures/get_activity.lock_from_manual.json b/tests/components/yale/fixtures/get_activity.lock_from_manual.json new file mode 100644 index 00000000000000..e2fc195cfda31d --- /dev/null +++ b/tests/components/yale/fixtures/get_activity.lock_from_manual.json @@ -0,0 +1,39 @@ +[ + { + "entities": { + "activity": "mockActivity2", + "house": "123", + "device": "online_with_doorsense", + "callingUser": "mockUserId2", + "otherUser": "deleted" + }, + "callingUser": { + "LastName": "elven princess", + "UserID": "mockUserId2", + "FirstName": "Your favorite" + }, + "otherUser": { + "LastName": "User", + "UserName": "deleteduser", + "FirstName": "Unknown", + "UserID": "deleted", + "PhoneNo": "deleted" + }, + "deviceType": "lock", + "deviceName": "MockHouseTDoor", + "action": "lock", + "dateTime": 1582007218000, + "info": { + "remote": false, + "keypad": false, + "manual": true, + "tag": false, + "DateLogActionID": "ABC+Time" + }, + "deviceID": "online_with_doorsense", + "house": { + "houseName": "MockHouse", + "houseID": "123" + } + } +] diff --git a/tests/components/yale/fixtures/get_activity.locking.json b/tests/components/yale/fixtures/get_activity.locking.json new file mode 100644 index 00000000000000..ad2df6f7e91019 --- /dev/null +++ b/tests/components/yale/fixtures/get_activity.locking.json @@ -0,0 +1,36 @@ +[ + { + "entities": { + "activity": "mockActivity2", + "house": "123", + "device": "online_with_doorsense", + "callingUser": "mockUserId2", + "otherUser": "deleted" + }, + "callingUser": { + "LastName": "elven princess", + "UserID": "mockUserId2", + "FirstName": "Your favorite" + }, + "otherUser": { + "LastName": "User", + "UserName": "deleteduser", + "FirstName": "Unknown", + "UserID": "deleted", + "PhoneNo": "deleted" + }, + "deviceType": "lock", + "deviceName": "MockHouseTDoor", + "action": "locking", + "dateTime": 1582007218000, + "info": { + "remote": true, + "DateLogActionID": "ABC+Time" + }, + "deviceID": "online_with_doorsense", + "house": { + "houseName": "MockHouse", + "houseID": "123" + } + } +] diff --git a/tests/components/yale/fixtures/get_activity.unlock_from_manual.json b/tests/components/yale/fixtures/get_activity.unlock_from_manual.json new file mode 100644 index 00000000000000..e8bf95818ced0d --- /dev/null +++ b/tests/components/yale/fixtures/get_activity.unlock_from_manual.json @@ -0,0 +1,39 @@ +[ + { + "entities": { + "activity": "mockActivity2", + "house": "123", + "device": "online_with_doorsense", + "callingUser": "mockUserId2", + "otherUser": "deleted" + }, + "callingUser": { + "LastName": "elven princess", + "UserID": "mockUserId2", + "FirstName": "Your favorite" + }, + "otherUser": { + "LastName": "User", + "UserName": "deleteduser", + "FirstName": "Unknown", + "UserID": "deleted", + "PhoneNo": "deleted" + }, + "deviceType": "lock", + "deviceName": "MockHouseTDoor", + "action": "unlock", + "dateTime": 1582007218000, + "info": { + "remote": false, + "keypad": false, + "manual": true, + "tag": false, + "DateLogActionID": "ABC+Time" + }, + "deviceID": "online_with_doorsense", + "house": { + "houseName": "MockHouse", + "houseID": "123" + } + } +] diff --git a/tests/components/yale/fixtures/get_activity.unlock_from_tag.json b/tests/components/yale/fixtures/get_activity.unlock_from_tag.json new file mode 100644 index 00000000000000..57876428677a2b --- /dev/null +++ b/tests/components/yale/fixtures/get_activity.unlock_from_tag.json @@ -0,0 +1,39 @@ +[ + { + "entities": { + "activity": "mockActivity2", + "house": "123", + "device": "online_with_doorsense", + "callingUser": "mockUserId2", + "otherUser": "deleted" + }, + "callingUser": { + "LastName": "elven princess", + "UserID": "mockUserId2", + "FirstName": "Your favorite" + }, + "otherUser": { + "LastName": "User", + "UserName": "deleteduser", + "FirstName": "Unknown", + "UserID": "deleted", + "PhoneNo": "deleted" + }, + "deviceType": "lock", + "deviceName": "MockHouseTDoor", + "action": "unlock", + "dateTime": 1582007218000, + "info": { + "remote": false, + "keypad": false, + "manual": false, + "tag": true, + "DateLogActionID": "ABC+Time" + }, + "deviceID": "online_with_doorsense", + "house": { + "houseName": "MockHouse", + "houseID": "123" + } + } +] diff --git a/tests/components/yale/fixtures/get_activity.unlocking.json b/tests/components/yale/fixtures/get_activity.unlocking.json new file mode 100644 index 00000000000000..0fbd0be3eb8998 --- /dev/null +++ b/tests/components/yale/fixtures/get_activity.unlocking.json @@ -0,0 +1,36 @@ +[ + { + "entities": { + "activity": "mockActivity2", + "house": "123", + "device": "online_with_doorsense", + "callingUser": "mockUserId2", + "otherUser": "deleted" + }, + "callingUser": { + "LastName": "elven princess", + "UserID": "mockUserId2", + "FirstName": "Your favorite" + }, + "otherUser": { + "LastName": "User", + "UserName": "deleteduser", + "FirstName": "Unknown", + "UserID": "deleted", + "PhoneNo": "deleted" + }, + "deviceType": "lock", + "deviceName": "MockHouseTDoor", + "action": "unlocking", + "dateTime": 1582007218000, + "info": { + "remote": true, + "DateLogActionID": "ABC+Time" + }, + "deviceID": "online_with_doorsense", + "house": { + "houseName": "MockHouse", + "houseID": "123" + } + } +] diff --git a/tests/components/yale/fixtures/get_doorbell.json b/tests/components/yale/fixtures/get_doorbell.json new file mode 100644 index 00000000000000..32714211618dae --- /dev/null +++ b/tests/components/yale/fixtures/get_doorbell.json @@ -0,0 +1,81 @@ +{ + "status_timestamp": 1512811834532, + "appID": "august-iphone", + "LockID": "BBBB1F5F11114C24CCCC97571DD6AAAA", + "recentImage": { + "original_filename": "file", + "placeholder": false, + "bytes": 24476, + "height": 640, + "format": "jpg", + "width": 480, + "version": 1512892814, + "resource_type": "image", + "etag": "54966926be2e93f77d498a55f247661f", + "tags": [], + "public_id": "qqqqt4ctmxwsysylaaaa", + "url": "http://image.com/vmk16naaaa7ibuey7sar.jpg", + "created_at": "2017-12-10T08:01:35Z", + "signature": "75z47ca21b5e8ffda21d2134e478a2307c4625da", + "secure_url": "https://image.com/vmk16naaaa7ibuey7sar.jpg", + "type": "upload" + }, + "settings": { + "keepEncoderRunning": true, + "videoResolution": "640x480", + "minACNoScaling": 40, + "irConfiguration": 8448272, + "directLink": true, + "overlayEnabled": true, + "notify_when_offline": true, + "micVolume": 100, + "bitrateCeiling": 512000, + "initialBitrate": 384000, + "IVAEnabled": false, + "turnOffCamera": false, + "ringSoundEnabled": true, + "JPGQuality": 70, + "motion_notifications": true, + "speakerVolume": 92, + "buttonpush_notifications": true, + "ABREnabled": true, + "debug": false, + "batteryLowThreshold": 3.1, + "batteryRun": false, + "IREnabled": true, + "batteryUseThreshold": 3.4 + }, + "doorbellServerURL": "https://doorbells.august.com", + "name": "Front Door", + "createdAt": "2016-11-26T22:27:11.176Z", + "installDate": "2016-11-26T22:27:11.176Z", + "serialNumber": "tBXZR0Z35E", + "dvrSubscriptionSetupDone": true, + "caps": ["reconnect"], + "doorbellID": "K98GiDT45GUL", + "HouseID": "mockhouseid1", + "telemetry": { + "signal_level": -56, + "date": "2017-12-10 08:05:12", + "battery_soc": 96, + "battery": 4.061763, + "steady_ac_in": 22.196405, + "BSSID": "88:ee:00:dd:aa:11", + "SSID": "foo_ssid", + "updated_at": "2017-12-10T08:05:13.650Z", + "temperature": 28.25, + "wifi_freq": 5745, + "load_average": "0.50 0.47 0.35 1/154 9345", + "link_quality": 54, + "battery_soh": 95, + "uptime": "16168.75 13830.49", + "ip_addr": "10.0.1.11", + "doorbell_low_battery": false, + "ac_in": 23.856874 + }, + "installUserID": "c3b2a94e-373e-aaaa-bbbb-36e996827777", + "status": "doorbell_call_status_online", + "firmwareVersion": "2.3.0-RC153+201711151527", + "pubsubChannel": "7c7a6672-59c8-3333-ffff-dcd98705cccc", + "updatedAt": "2017-12-10T08:05:13.650Z" +} diff --git a/tests/components/yale/fixtures/get_doorbell.nobattery.json b/tests/components/yale/fixtures/get_doorbell.nobattery.json new file mode 100644 index 00000000000000..2a7f1e2d3b2327 --- /dev/null +++ b/tests/components/yale/fixtures/get_doorbell.nobattery.json @@ -0,0 +1,78 @@ +{ + "status_timestamp": 1512811834532, + "appID": "august-iphone", + "LockID": "BBBB1F5F11114C24CCCC97571DD6AAAA", + "recentImage": { + "original_filename": "file", + "placeholder": false, + "bytes": 24476, + "height": 640, + "format": "jpg", + "width": 480, + "version": 1512892814, + "resource_type": "image", + "etag": "54966926be2e93f77d498a55f247661f", + "tags": [], + "public_id": "qqqqt4ctmxwsysylaaaa", + "url": "http://image.com/vmk16naaaa7ibuey7sar.jpg", + "created_at": "2017-12-10T08:01:35Z", + "signature": "75z47ca21b5e8ffda21d2134e478a2307c4625da", + "secure_url": "https://image.com/vmk16naaaa7ibuey7sar.jpg", + "type": "upload" + }, + "settings": { + "keepEncoderRunning": true, + "videoResolution": "640x480", + "minACNoScaling": 40, + "irConfiguration": 8448272, + "directLink": true, + "overlayEnabled": true, + "notify_when_offline": true, + "micVolume": 100, + "bitrateCeiling": 512000, + "initialBitrate": 384000, + "IVAEnabled": false, + "turnOffCamera": false, + "ringSoundEnabled": true, + "JPGQuality": 70, + "motion_notifications": true, + "speakerVolume": 92, + "buttonpush_notifications": true, + "ABREnabled": true, + "debug": false, + "batteryLowThreshold": 3.1, + "batteryRun": false, + "IREnabled": true, + "batteryUseThreshold": 3.4 + }, + "doorbellServerURL": "https://doorbells.august.com", + "name": "Front Door", + "createdAt": "2016-11-26T22:27:11.176Z", + "installDate": "2016-11-26T22:27:11.176Z", + "serialNumber": "tBXZR0Z35E", + "dvrSubscriptionSetupDone": true, + "caps": ["reconnect"], + "doorbellID": "K98GiDT45GUL", + "HouseID": "3dd2accaea08", + "telemetry": { + "signal_level": -56, + "date": "2017-12-10 08:05:12", + "steady_ac_in": 22.196405, + "BSSID": "88:ee:00:dd:aa:11", + "SSID": "foo_ssid", + "updated_at": "2017-12-10T08:05:13.650Z", + "temperature": 28.25, + "wifi_freq": 5745, + "load_average": "0.50 0.47 0.35 1/154 9345", + "link_quality": 54, + "uptime": "16168.75 13830.49", + "ip_addr": "10.0.1.11", + "doorbell_low_battery": false, + "ac_in": 23.856874 + }, + "installUserID": "c3b2a94e-373e-aaaa-bbbb-36e996827777", + "status": "doorbell_call_status_online", + "firmwareVersion": "2.3.0-RC153+201711151527", + "pubsubChannel": "7c7a6672-59c8-3333-ffff-dcd98705cccc", + "updatedAt": "2017-12-10T08:05:13.650Z" +} diff --git a/tests/components/yale/fixtures/get_doorbell.offline.json b/tests/components/yale/fixtures/get_doorbell.offline.json new file mode 100644 index 00000000000000..13a8483c995735 --- /dev/null +++ b/tests/components/yale/fixtures/get_doorbell.offline.json @@ -0,0 +1,126 @@ +{ + "recentImage": { + "tags": [], + "height": 576, + "public_id": "fdsfds", + "bytes": 50013, + "resource_type": "image", + "original_filename": "file", + "version": 1582242766, + "format": "jpg", + "signature": "fdsfdsf", + "created_at": "2020-02-20T23:52:46Z", + "type": "upload", + "placeholder": false, + "url": "http://res.cloudinary.com/august-com/image/upload/ccc/ccccc.jpg", + "secure_url": "https://res.cloudinary.com/august-com/image/upload/cc/cccc.jpg", + "etag": "zds", + "width": 720 + }, + "firmwareVersion": "3.1.0-HYDRC75+201909251139", + "doorbellServerURL": "https://doorbells.august.com", + "installUserID": "mock", + "caps": ["reconnect", "webrtc", "tcp_wakeup"], + "messagingProtocol": "pubnub", + "createdAt": "2020-02-12T03:52:28.719Z", + "invitations": [], + "appID": "august-iphone-v5", + "HouseID": "houseid1", + "doorbellID": "tmt100", + "name": "Front Door", + "settings": { + "batteryUseThreshold": 3.4, + "brightness": 50, + "batteryChargeCurrent": 60, + "overCurrentThreshold": -250, + "irLedBrightness": 40, + "videoResolution": "720x576", + "pirPulseCounter": 1, + "contrast": 50, + "micVolume": 50, + "directLink": true, + "auto_contrast_mode": 0, + "saturation": 50, + "motion_notifications": true, + "pirSensitivity": 20, + "pirBlindTime": 7, + "notify_when_offline": false, + "nightModeAlsThreshold": 10, + "minACNoScaling": 40, + "DVRRecordingTimeout": 15, + "turnOffCamera": false, + "debug": false, + "keepEncoderRunning": true, + "pirWindowTime": 0, + "bitrateCeiling": 2000000, + "backlight_comp": false, + "buttonpush_notifications": true, + "buttonpush_notifications_partners": false, + "minimumSnapshotInterval": 30, + "pirConfiguration": 272, + "batteryLowThreshold": 3.1, + "sharpness": 50, + "ABREnabled": true, + "hue": 50, + "initialBitrate": 1000000, + "ringSoundEnabled": true, + "IVAEnabled": false, + "overlayEnabled": true, + "speakerVolume": 92, + "ringRepetitions": 3, + "powerProfilePreset": -1, + "irConfiguration": 16836880, + "JPGQuality": 70, + "IREnabled": true + }, + "updatedAt": "2020-02-20T23:58:21.580Z", + "serialNumber": "abc", + "installDate": "2019-02-12T03:52:28.719Z", + "dvrSubscriptionSetupDone": true, + "pubsubChannel": "mock", + "chimes": [ + { + "updatedAt": "2020-02-12T03:55:38.805Z", + "_id": "cccc", + "type": 1, + "serialNumber": "ccccc", + "doorbellID": "tmt100", + "name": "Living Room", + "chimeID": "cccc", + "createdAt": "2020-02-12T03:55:38.805Z", + "firmware": "3.1.16" + } + ], + "telemetry": { + "battery": 3.985, + "battery_soc": 81, + "load_average": "0.45 0.18 0.07 4/98 831", + "ip_addr": "192.168.100.174", + "BSSID": "snp", + "uptime": "96.55 70.59", + "SSID": "bob", + "updated_at": "2020-02-20T23:53:09.586Z", + "dtim_period": 0, + "wifi_freq": 2462, + "date": "2020-02-20 11:47:36", + "BSSIDManufacturer": "Ubiquiti - Ubiquiti Networks Inc.", + "battery_temp": 22, + "battery_avg_cur": -291, + "beacon_interval": 0, + "signal_level": -49, + "battery_soh": 95, + "doorbell_low_battery": false + }, + "secChipCertSerial": "", + "tcpKeepAlive": { + "keepAliveUUID": "mock", + "wakeUp": { + "token": "wakemeup", + "lastUpdated": 1582242723931 + } + }, + "statusUpdatedAtMs": 1582243101579, + "status": "doorbell_offline", + "type": "hydra1", + "HouseName": "housename" +} diff --git a/tests/components/yale/fixtures/get_lock.doorsense_init.json b/tests/components/yale/fixtures/get_lock.doorsense_init.json new file mode 100644 index 00000000000000..1132cc61a8d8bb --- /dev/null +++ b/tests/components/yale/fixtures/get_lock.doorsense_init.json @@ -0,0 +1,92 @@ +{ + "LockName": "Front Door Lock", + "Type": 2, + "Created": "2017-12-10T03:12:09.210Z", + "Updated": "2017-12-10T03:12:09.210Z", + "LockID": "A6697750D607098BAE8D6BAA11EF8063", + "HouseID": "000000000000", + "HouseName": "My House", + "Calibrated": false, + "skuNumber": "AUG-SL02-M02-S02", + "timeZone": "America/Vancouver", + "battery": 0.88, + "SerialNumber": "X2FSW05DGA", + "LockStatus": { + "status": "locked", + "doorState": "init", + "dateTime": "2017-12-10T04:48:30.272Z", + "isLockStatusChanged": false, + "valid": true + }, + "currentFirmwareVersion": "109717e9-3.0.44-3.0.30", + "homeKitEnabled": false, + "zWaveEnabled": false, + "isGalileo": false, + "Bridge": { + "_id": "aaacab87f7efxa0015884999", + "mfgBridgeID": "AAGPP102XX", + "deviceModel": "august-doorbell", + "firmwareVersion": "2.3.0-RC153+201711151527", + "operative": true + }, + "keypad": { + "_id": "5bc65c24e6ef2a263e1450a8", + "serialNumber": "K1GXB0054Z", + "lockID": "92412D1B44004595B5DEB134E151A8D3", + "currentFirmwareVersion": "2.27.0", + "battery": {}, + "batteryLevel": "Medium", + "batteryRaw": 170 + }, + "OfflineKeys": { + "created": [], + "loaded": [], + "deleted": [], + "loadedhk": [ + { + "key": "kkk01d4300c1dcxxx1c330f794941222", + "slot": 256, + "UserID": "cccca94e-373e-aaaa-bbbb-333396827777", + "created": "2017-12-10T03:12:09.218Z", + "loaded": "2017-12-10T03:12:55.563Z" + } + ] + }, + "parametersToSet": {}, + "users": { + "cccca94e-373e-aaaa-bbbb-333396827777": { + "UserType": "superuser", + "FirstName": "Foo", + "LastName": "Bar", + "identifiers": ["email:foo@bar.com", "phone:+177777777777"], + "imageInfo": { + "original": { + "width": 948, + "height": 949, + "format": "jpg", + "url": "http://www.image.com/foo.jpeg", + "secure_url": "https://www.image.com/foo.jpeg" + }, + "thumbnail": { + "width": 128, + "height": 128, + "format": "jpg", + "url": "http://www.image.com/foo.jpeg", + "secure_url": "https://www.image.com/foo.jpeg" + } + } + } + }, + "pubsubChannel": "3333a674-ffff-aaaa-b351-b3a4473f3333", + "ruleHash": {}, + "cameras": [], + "geofenceLimits": { + "ios": { + "debounceInterval": 90, + "gpsAccuracyMultiplier": 2.5, + "maximumGeofence": 5000, + "minimumGeofence": 100, + "minGPSAccuracyRequired": 80 + } + } +} diff --git a/tests/components/yale/fixtures/get_lock.low_keypad_battery.json b/tests/components/yale/fixtures/get_lock.low_keypad_battery.json new file mode 100644 index 00000000000000..43b5513a5271c0 --- /dev/null +++ b/tests/components/yale/fixtures/get_lock.low_keypad_battery.json @@ -0,0 +1,92 @@ +{ + "LockName": "Front Door Lock", + "Type": 2, + "Created": "2017-12-10T03:12:09.210Z", + "Updated": "2017-12-10T03:12:09.210Z", + "LockID": "A6697750D607098BAE8D6BAA11EF8063", + "HouseID": "000000000000", + "HouseName": "My House", + "Calibrated": false, + "skuNumber": "AUG-SL02-M02-S02", + "timeZone": "America/Vancouver", + "battery": 0.88, + "SerialNumber": "X2FSW05DGA", + "LockStatus": { + "status": "locked", + "doorState": "closed", + "dateTime": "2017-12-10T04:48:30.272Z", + "isLockStatusChanged": true, + "valid": true + }, + "currentFirmwareVersion": "109717e9-3.0.44-3.0.30", + "homeKitEnabled": false, + "zWaveEnabled": false, + "isGalileo": false, + "Bridge": { + "_id": "aaacab87f7efxa0015884999", + "mfgBridgeID": "AAGPP102XX", + "deviceModel": "august-doorbell", + "firmwareVersion": "2.3.0-RC153+201711151527", + "operative": true + }, + "keypad": { + "_id": "5bc65c24e6ef2a263e1450a8", + "serialNumber": "K1GXB0054Z", + "lockID": "92412D1B44004595B5DEB134E151A8D3", + "currentFirmwareVersion": "2.27.0", + "battery": {}, + "batteryLevel": "Low", + "batteryRaw": 128 + }, + "OfflineKeys": { + "created": [], + "loaded": [], + "deleted": [], + "loadedhk": [ + { + "key": "kkk01d4300c1dcxxx1c330f794941222", + "slot": 256, + "UserID": "cccca94e-373e-aaaa-bbbb-333396827777", + "created": "2017-12-10T03:12:09.218Z", + "loaded": "2017-12-10T03:12:55.563Z" + } + ] + }, + "parametersToSet": {}, + "users": { + "cccca94e-373e-aaaa-bbbb-333396827777": { + "UserType": "superuser", + "FirstName": "Foo", + "LastName": "Bar", + "identifiers": ["email:foo@bar.com", "phone:+177777777777"], + "imageInfo": { + "original": { + "width": 948, + "height": 949, + "format": "jpg", + "url": "http://www.image.com/foo.jpeg", + "secure_url": "https://www.image.com/foo.jpeg" + }, + "thumbnail": { + "width": 128, + "height": 128, + "format": "jpg", + "url": "http://www.image.com/foo.jpeg", + "secure_url": "https://www.image.com/foo.jpeg" + } + } + } + }, + "pubsubChannel": "3333a674-ffff-aaaa-b351-b3a4473f3333", + "ruleHash": {}, + "cameras": [], + "geofenceLimits": { + "ios": { + "debounceInterval": 90, + "gpsAccuracyMultiplier": 2.5, + "maximumGeofence": 5000, + "minimumGeofence": 100, + "minGPSAccuracyRequired": 80 + } + } +} diff --git a/tests/components/yale/fixtures/get_lock.offline.json b/tests/components/yale/fixtures/get_lock.offline.json new file mode 100644 index 00000000000000..50d3d345ef8ec2 --- /dev/null +++ b/tests/components/yale/fixtures/get_lock.offline.json @@ -0,0 +1,57 @@ +{ + "Calibrated": false, + "Created": "2000-00-00T00:00:00.447Z", + "HouseID": "houseid", + "HouseName": "MockName", + "LockID": "ABC", + "LockName": "Test", + "LockStatus": { + "status": "unknown" + }, + "OfflineKeys": { + "created": [], + "createdhk": [ + { + "UserID": "mock-user-id", + "created": "2000-00-00T00:00:00.447Z", + "key": "mockkey", + "slot": 12 + } + ], + "deleted": [], + "loaded": [] + }, + "SerialNumber": "ABC", + "Type": 3, + "Updated": "2000-00-00T00:00:00.447Z", + "battery": -1, + "cameras": [], + "currentFirmwareVersion": "undefined-1.59.0-1.13.2", + "geofenceLimits": { + "ios": { + "debounceInterval": 90, + "gpsAccuracyMultiplier": 2.5, + "maximumGeofence": 5000, + "minGPSAccuracyRequired": 80, + "minimumGeofence": 100 + } + }, + "homeKitEnabled": false, + "isGalileo": false, + "macAddress": "a:b:c", + "parametersToSet": {}, + "pubsubChannel": "mockpubsub", + "ruleHash": {}, + "skuNumber": "AUG-X", + "supportsEntryCodes": false, + "users": { + "mockuserid": { + "FirstName": "MockName", + "LastName": "House", + "UserType": "superuser", + "identifiers": ["phone:+15558675309", "email:mockme@mock.org"] + } + }, + "zWaveDSK": "1-2-3-4", + "zWaveEnabled": true +} diff --git a/tests/components/yale/fixtures/get_lock.online.json b/tests/components/yale/fixtures/get_lock.online.json new file mode 100644 index 00000000000000..7abadeef4b67d6 --- /dev/null +++ b/tests/components/yale/fixtures/get_lock.online.json @@ -0,0 +1,92 @@ +{ + "LockName": "Front Door Lock", + "Type": 2, + "Created": "2017-12-10T03:12:09.210Z", + "Updated": "2017-12-10T03:12:09.210Z", + "LockID": "A6697750D607098BAE8D6BAA11EF8063", + "HouseID": "000000000000", + "HouseName": "My House", + "Calibrated": false, + "skuNumber": "AUG-SL02-M02-S02", + "timeZone": "America/Vancouver", + "battery": 0.88, + "SerialNumber": "X2FSW05DGA", + "LockStatus": { + "status": "locked", + "doorState": "closed", + "dateTime": "2017-12-10T04:48:30.272Z", + "isLockStatusChanged": true, + "valid": true + }, + "currentFirmwareVersion": "109717e9-3.0.44-3.0.30", + "homeKitEnabled": false, + "zWaveEnabled": false, + "isGalileo": false, + "Bridge": { + "_id": "aaacab87f7efxa0015884999", + "mfgBridgeID": "AAGPP102XX", + "deviceModel": "august-doorbell", + "firmwareVersion": "2.3.0-RC153+201711151527", + "operative": true + }, + "keypad": { + "_id": "5bc65c24e6ef2a263e1450a8", + "serialNumber": "K1GXB0054Z", + "lockID": "92412D1B44004595B5DEB134E151A8D3", + "currentFirmwareVersion": "2.27.0", + "battery": {}, + "batteryLevel": "Medium", + "batteryRaw": 170 + }, + "OfflineKeys": { + "created": [], + "loaded": [], + "deleted": [], + "loadedhk": [ + { + "key": "kkk01d4300c1dcxxx1c330f794941222", + "slot": 256, + "UserID": "cccca94e-373e-aaaa-bbbb-333396827777", + "created": "2017-12-10T03:12:09.218Z", + "loaded": "2017-12-10T03:12:55.563Z" + } + ] + }, + "parametersToSet": {}, + "users": { + "cccca94e-373e-aaaa-bbbb-333396827777": { + "UserType": "superuser", + "FirstName": "Foo", + "LastName": "Bar", + "identifiers": ["email:foo@bar.com", "phone:+177777777777"], + "imageInfo": { + "original": { + "width": 948, + "height": 949, + "format": "jpg", + "url": "http://www.image.com/foo.jpeg", + "secure_url": "https://www.image.com/foo.jpeg" + }, + "thumbnail": { + "width": 128, + "height": 128, + "format": "jpg", + "url": "http://www.image.com/foo.jpeg", + "secure_url": "https://www.image.com/foo.jpeg" + } + } + } + }, + "pubsubChannel": "3333a674-ffff-aaaa-b351-b3a4473f3333", + "ruleHash": {}, + "cameras": [], + "geofenceLimits": { + "ios": { + "debounceInterval": 90, + "gpsAccuracyMultiplier": 2.5, + "maximumGeofence": 5000, + "minimumGeofence": 100, + "minGPSAccuracyRequired": 80 + } + } +} diff --git a/tests/components/yale/fixtures/get_lock.online.unknown_state.json b/tests/components/yale/fixtures/get_lock.online.unknown_state.json new file mode 100644 index 00000000000000..abc8b40a132aa2 --- /dev/null +++ b/tests/components/yale/fixtures/get_lock.online.unknown_state.json @@ -0,0 +1,59 @@ +{ + "LockName": "Side Door", + "Type": 1001, + "Created": "2019-10-07T01:49:06.831Z", + "Updated": "2019-10-07T01:49:06.831Z", + "LockID": "BROKENID", + "HouseID": "abc", + "HouseName": "dog", + "Calibrated": false, + "timeZone": "America/Chicago", + "battery": 0.9524716174964851, + "hostLockInfo": { + "serialNumber": "YR", + "manufacturer": "yale", + "productID": 1536, + "productTypeID": 32770 + }, + "supportsEntryCodes": true, + "skuNumber": "AUG-MD01", + "macAddress": "MAC", + "SerialNumber": "M1FXZ00EZ9", + "LockStatus": { + "status": "unknown_error_during_connect", + "dateTime": "2020-02-22T02:48:11.741Z", + "isLockStatusChanged": true, + "valid": true, + "doorState": "closed" + }, + "currentFirmwareVersion": "undefined-4.3.0-1.8.14", + "homeKitEnabled": true, + "zWaveEnabled": false, + "isGalileo": false, + "Bridge": { + "_id": "id", + "mfgBridgeID": "id", + "deviceModel": "august-connect", + "firmwareVersion": "2.2.1", + "operative": true, + "status": { + "current": "online", + "updated": "2020-02-21T15:06:47.001Z", + "lastOnline": "2020-02-21T15:06:47.001Z", + "lastOffline": "2020-02-06T17:33:21.265Z" + }, + "hyperBridge": true + }, + "parametersToSet": {}, + "ruleHash": {}, + "cameras": [], + "geofenceLimits": { + "ios": { + "debounceInterval": 90, + "gpsAccuracyMultiplier": 2.5, + "maximumGeofence": 5000, + "minimumGeofence": 100, + "minGPSAccuracyRequired": 80 + } + } +} diff --git a/tests/components/yale/fixtures/get_lock.online_missing_doorsense.json b/tests/components/yale/fixtures/get_lock.online_missing_doorsense.json new file mode 100644 index 00000000000000..84822df9b89b5c --- /dev/null +++ b/tests/components/yale/fixtures/get_lock.online_missing_doorsense.json @@ -0,0 +1,50 @@ +{ + "Bridge": { + "_id": "bridgeid", + "deviceModel": "august-connect", + "firmwareVersion": "2.2.1", + "hyperBridge": true, + "mfgBridgeID": "C5WY200WSH", + "operative": true, + "status": { + "current": "online", + "lastOffline": "2000-00-00T00:00:00.447Z", + "lastOnline": "2000-00-00T00:00:00.447Z", + "updated": "2000-00-00T00:00:00.447Z" + } + }, + "Calibrated": false, + "Created": "2000-00-00T00:00:00.447Z", + "HouseID": "123", + "HouseName": "Test", + "LockID": "missing_doorsense_id", + "LockName": "Online door missing doorsense", + "LockStatus": { + "dateTime": "2017-12-10T04:48:30.272Z", + "isLockStatusChanged": false, + "status": "locked", + "valid": true + }, + "SerialNumber": "XY", + "Type": 1001, + "Updated": "2000-00-00T00:00:00.447Z", + "battery": 0.922, + "currentFirmwareVersion": "undefined-4.3.0-1.8.14", + "homeKitEnabled": true, + "hostLockInfo": { + "manufacturer": "yale", + "productID": 1536, + "productTypeID": 32770, + "serialNumber": "ABC" + }, + "isGalileo": false, + "macAddress": "12:22", + "pins": { + "created": [], + "loaded": [] + }, + "skuNumber": "AUG-MD01", + "supportsEntryCodes": true, + "timeZone": "Pacific/Hawaii", + "zWaveEnabled": false +} diff --git a/tests/components/yale/fixtures/get_lock.online_with_doorsense.json b/tests/components/yale/fixtures/get_lock.online_with_doorsense.json new file mode 100644 index 00000000000000..d9b413708ca225 --- /dev/null +++ b/tests/components/yale/fixtures/get_lock.online_with_doorsense.json @@ -0,0 +1,52 @@ +{ + "Bridge": { + "_id": "bridgeid", + "deviceModel": "august-connect", + "firmwareVersion": "2.2.1", + "hyperBridge": true, + "mfgBridgeID": "C5WY200WSH", + "operative": true, + "status": { + "current": "online", + "lastOffline": "2000-00-00T00:00:00.447Z", + "lastOnline": "2000-00-00T00:00:00.447Z", + "updated": "2000-00-00T00:00:00.447Z" + } + }, + "pubsubChannel": "pubsub", + "Calibrated": false, + "Created": "2000-00-00T00:00:00.447Z", + "HouseID": "mockhouseid1", + "HouseName": "Test", + "LockID": "online_with_doorsense", + "LockName": "Online door with doorsense", + "LockStatus": { + "dateTime": "2017-12-10T04:48:30.272Z", + "doorState": "open", + "isLockStatusChanged": false, + "status": "locked", + "valid": true + }, + "SerialNumber": "XY", + "Type": 1001, + "Updated": "2000-00-00T00:00:00.447Z", + "battery": 0.922, + "currentFirmwareVersion": "undefined-4.3.0-1.8.14", + "homeKitEnabled": true, + "hostLockInfo": { + "manufacturer": "yale", + "productID": 1536, + "productTypeID": 32770, + "serialNumber": "ABC" + }, + "isGalileo": false, + "macAddress": "12:22", + "pins": { + "created": [], + "loaded": [] + }, + "skuNumber": "AUG-MD01", + "supportsEntryCodes": true, + "timeZone": "Pacific/Hawaii", + "zWaveEnabled": false +} diff --git a/tests/components/yale/fixtures/get_lock.online_with_keys.json b/tests/components/yale/fixtures/get_lock.online_with_keys.json new file mode 100644 index 00000000000000..4efcba44d097d9 --- /dev/null +++ b/tests/components/yale/fixtures/get_lock.online_with_keys.json @@ -0,0 +1,100 @@ +{ + "LockName": "Front Door Lock", + "Type": 2, + "Created": "2017-12-10T03:12:09.210Z", + "Updated": "2017-12-10T03:12:09.210Z", + "LockID": "A6697750D607098BAE8D6BAA11EF8064", + "HouseID": "000000000000", + "HouseName": "My House", + "Calibrated": false, + "skuNumber": "AUG-SL02-M02-S02", + "timeZone": "America/Vancouver", + "battery": 0.88, + "SerialNumber": "X2FSW05DGA", + "LockStatus": { + "status": "locked", + "doorState": "closed", + "dateTime": "2017-12-10T04:48:30.272Z", + "isLockStatusChanged": true, + "valid": true + }, + "currentFirmwareVersion": "109717e9-3.0.44-3.0.30", + "homeKitEnabled": false, + "zWaveEnabled": false, + "isGalileo": false, + "Bridge": { + "_id": "aaacab87f7efxa0015884999", + "mfgBridgeID": "AAGPP102XX", + "deviceModel": "august-doorbell", + "firmwareVersion": "2.3.0-RC153+201711151527", + "operative": true + }, + "keypad": { + "_id": "5bc65c24e6ef2a263e1450a9", + "serialNumber": "K1GXB0054L", + "lockID": "92412D1B44004595B5DEB134E151A8D4", + "currentFirmwareVersion": "2.27.0", + "battery": {}, + "batteryLevel": "Medium", + "batteryRaw": 170 + }, + "OfflineKeys": { + "created": [], + "loaded": [ + { + "UserID": "cccca94e-373e-aaaa-bbbb-333396827777", + "slot": 1, + "key": "kkk01d4300c1dcxxx1c330f794941111", + "created": "2017-12-10T03:12:09.215Z", + "loaded": "2017-12-10T03:12:54.391Z" + } + ], + "deleted": [], + "loadedhk": [ + { + "key": "kkk01d4300c1dcxxx1c330f794941222", + "slot": 256, + "UserID": "cccca94e-373e-aaaa-bbbb-333396827777", + "created": "2017-12-10T03:12:09.218Z", + "loaded": "2017-12-10T03:12:55.563Z" + } + ] + }, + "parametersToSet": {}, + "users": { + "cccca94e-373e-aaaa-bbbb-333396827777": { + "UserType": "superuser", + "FirstName": "Foo", + "LastName": "Bar", + "identifiers": ["email:foo@bar.com", "phone:+177777777777"], + "imageInfo": { + "original": { + "width": 948, + "height": 949, + "format": "jpg", + "url": "http://www.image.com/foo.jpeg", + "secure_url": "https://www.image.com/foo.jpeg" + }, + "thumbnail": { + "width": 128, + "height": 128, + "format": "jpg", + "url": "http://www.image.com/foo.jpeg", + "secure_url": "https://www.image.com/foo.jpeg" + } + } + } + }, + "pubsubChannel": "3333a674-ffff-aaaa-b351-b3a4473f3333", + "ruleHash": {}, + "cameras": [], + "geofenceLimits": { + "ios": { + "debounceInterval": 90, + "gpsAccuracyMultiplier": 2.5, + "maximumGeofence": 5000, + "minimumGeofence": 100, + "minGPSAccuracyRequired": 80 + } + } +} diff --git a/tests/components/yale/fixtures/get_lock.online_with_unlatch.json b/tests/components/yale/fixtures/get_lock.online_with_unlatch.json new file mode 100644 index 00000000000000..288ab1a2f28599 --- /dev/null +++ b/tests/components/yale/fixtures/get_lock.online_with_unlatch.json @@ -0,0 +1,94 @@ +{ + "LockName": "Lock online with unlatch supported", + "Type": 17, + "Created": "2024-03-14T18:03:09.003Z", + "Updated": "2024-03-14T18:03:09.003Z", + "LockID": "online_with_unlatch", + "HouseID": "mockhouseid1", + "HouseName": "Zuhause", + "Calibrated": false, + "timeZone": "Europe/Berlin", + "battery": 0.61, + "batteryInfo": { + "level": 0.61, + "warningState": "lock_state_battery_warning_none", + "infoUpdatedDate": "2024-04-30T17:55:09.045Z", + "lastChangeDate": "2024-03-15T07:04:00.000Z", + "lastChangeVoltage": 8350, + "state": "Mittel", + "icon": "https://app-resources.aaecosystem.com/images/lock_battery_state_medium.png" + }, + "hostHardwareID": "xxx", + "supportsEntryCodes": true, + "remoteOperateSecret": "xxxx", + "skuNumber": "NONE", + "macAddress": "DE:AD:BE:00:00:00", + "SerialNumber": "LPOC000000", + "LockStatus": { + "status": "locked", + "dateTime": "2024-04-30T18:41:25.673Z", + "isLockStatusChanged": false, + "valid": true, + "doorState": "init" + }, + "currentFirmwareVersion": "1.0.4", + "homeKitEnabled": false, + "zWaveEnabled": false, + "isGalileo": false, + "Bridge": { + "_id": "65f33445529187c78a100000", + "mfgBridgeID": "LPOCH0004Y", + "deviceModel": "august-lock", + "firmwareVersion": "1.0.4", + "operative": true, + "status": { + "current": "online", + "lastOnline": "2024-04-30T18:41:27.971Z", + "updated": "2024-04-30T18:41:27.971Z", + "lastOffline": "2024-04-25T14:41:40.118Z" + }, + "locks": [ + { + "_id": "656858c182e6c7c555faf758", + "LockID": "68895DD075A1444FAD4C00B273EEEF28", + "macAddress": "DE:AD:BE:EF:0B:BC" + } + ], + "hyperBridge": true + }, + "OfflineKeys": { + "created": [], + "loaded": [ + { + "created": "2024-03-14T18:03:09.034Z", + "key": "055281d4aa9bd7b68c7b7bb78e2f34ca", + "slot": 1, + "UserID": "b4b44424-0000-0000-0000-25c224dad337", + "loaded": "2024-03-14T18:03:33.470Z" + } + ], + "deleted": [] + }, + "parametersToSet": {}, + "users": { + "b4b44424-0000-0000-0000-25c224dad337": { + "UserType": "superuser", + "FirstName": "m10x", + "LastName": "m10x", + "identifiers": ["phone:+494444444", "email:m10x@example.com"] + } + }, + "pubsubChannel": "pubsub", + "ruleHash": {}, + "cameras": [], + "geofenceLimits": { + "ios": { + "debounceInterval": 90, + "gpsAccuracyMultiplier": 2.5, + "maximumGeofence": 5000, + "minimumGeofence": 100, + "minGPSAccuracyRequired": 80 + } + }, + "accessSchedulesAllowed": true +} diff --git a/tests/components/yale/fixtures/get_locks.json b/tests/components/yale/fixtures/get_locks.json new file mode 100644 index 00000000000000..3fab55f82c9602 --- /dev/null +++ b/tests/components/yale/fixtures/get_locks.json @@ -0,0 +1,16 @@ +{ + "A6697750D607098BAE8D6BAA11EF8063": { + "LockName": "Front Door Lock", + "UserType": "superuser", + "macAddress": "2E:BA:C4:14:3F:09", + "HouseID": "000000000000", + "HouseName": "A House" + }, + "A6697750D607098BAE8D6BAA11EF9999": { + "LockName": "Back Door Lock", + "UserType": "user", + "macAddress": "2E:BA:C4:14:3F:88", + "HouseID": "000000000011", + "HouseName": "A House" + } +} diff --git a/tests/components/yale/fixtures/jwt b/tests/components/yale/fixtures/jwt new file mode 100644 index 00000000000000..d64f31b9bb27f2 --- /dev/null +++ b/tests/components/yale/fixtures/jwt @@ -0,0 +1 @@ +eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpbnN0YWxsSWQiOiIiLCJyZWdpb24iOiJpcmVsYW5kLXByb2QtYXdzIiwiYXBwbGljYXRpb25JZCI6IiIsInVzZXJJZCI6ImE3NmMyNWU1LTQ5YWEtNGMxNC1jZDBjLTQ4YTY5MzFlMjA4MSIsInZJbnN0YWxsSWQiOmZhbHNlLCJ2UGFzc3dvcmQiOnRydWUsInZFbWFpbCI6dHJ1ZSwidlBob25lIjp0cnVlLCJoYXNJbnN0YWxsSWQiOmZhbHNlLCJoYXNQYXNzd29yZCI6ZmFsc2UsImhhc0VtYWlsIjpmYWxzZSwiaGFzUGhvbmUiOmZhbHNlLCJpc0xvY2tlZE91dCI6ZmFsc2UsImNhcHRjaGEiOiIiLCJlbWFpbCI6W10sInBob25lIjpbXSwiZXhwaXJlc0F0IjoiMjAyNC0xMi0xOFQxMzo1NDowNS4xMzRaIiwidGVtcG9yYXJ5QWNjb3VudENyZWF0aW9uUGFzc3dvcmRMaW5rIjoiIiwiaWF0IjoxNzI0MTYyMDQ1LCJleHAiOjE3MzQ1MzAwNDUsIm9hdXRoIjp7ImFwcF9uYW1lIjoiSG9tZSBBc3Npc3RhbnQiLCJjbGllbnRfaWQiOiJiM2NkM2YwYi1mYjk3LTRkNmMtYmVlOS1hZjdhYjA0NzU4YzciLCJyZWRpcmVjdF91cmkiOiJodHRwczovL2FjY291bnQtbGluay5uYWJ1Y2FzYS5jb20vYXV0aG9yaXplX2NhbGxiYWNrIiwicGFydG5lcl9pZCI6IjY1Nzk3NDg4MTA2NmNhNDhjOTljMDgyNiJ9fQ.BdRo-dEr-osbDQGB2XzlI-mIj4gqULtapODt-sj-eA8 diff --git a/tests/components/yale/fixtures/lock_open.json b/tests/components/yale/fixtures/lock_open.json new file mode 100644 index 00000000000000..b6cfe3c90fc6cc --- /dev/null +++ b/tests/components/yale/fixtures/lock_open.json @@ -0,0 +1,26 @@ +{ + "status": "kAugLockState_Locked", + "resultsFromOperationCache": false, + "retryCount": 1, + "info": { + "wlanRSSI": -54, + "lockType": "lock_version_1001", + "lockStatusChanged": false, + "serialNumber": "ABC", + "serial": "123", + "action": "lock", + "context": { + "startDate": "2020-02-19T01:59:39.516Z", + "retryCount": 1, + "transactionID": "mock" + }, + "bridgeID": "mock", + "wlanSNR": 41, + "startTime": "2020-02-19T01:59:39.517Z", + "duration": 5149, + "lockID": "ABC", + "rssi": -77 + }, + "totalTime": 5162, + "doorState": "kAugDoorState_Open" +} diff --git a/tests/components/yale/fixtures/lock_with_doorbell.online.json b/tests/components/yale/fixtures/lock_with_doorbell.online.json new file mode 100644 index 00000000000000..bb2367d111197c --- /dev/null +++ b/tests/components/yale/fixtures/lock_with_doorbell.online.json @@ -0,0 +1,100 @@ +{ + "LockName": "Front Door Lock", + "Type": 7, + "Created": "2017-12-10T03:12:09.210Z", + "Updated": "2017-12-10T03:12:09.210Z", + "LockID": "A6697750D607098BAE8D6BAA11EF8063", + "HouseID": "000000000000", + "HouseName": "My House", + "Calibrated": false, + "skuNumber": "AUG-SL02-M02-S02", + "timeZone": "America/Vancouver", + "battery": 0.88, + "SerialNumber": "X2FSW05DGA", + "LockStatus": { + "status": "locked", + "doorState": "closed", + "dateTime": "2017-12-10T04:48:30.272Z", + "isLockStatusChanged": true, + "valid": true + }, + "currentFirmwareVersion": "109717e9-3.0.44-3.0.30", + "homeKitEnabled": false, + "zWaveEnabled": false, + "isGalileo": false, + "Bridge": { + "_id": "aaacab87f7efxa0015884999", + "mfgBridgeID": "AAGPP102XX", + "deviceModel": "august-doorbell", + "firmwareVersion": "2.3.0-RC153+201711151527", + "operative": true + }, + "keypad": { + "_id": "5bc65c24e6ef2a263e1450a8", + "serialNumber": "K1GXB0054Z", + "lockID": "92412D1B44004595B5DEB134E151A8D3", + "currentFirmwareVersion": "2.27.0", + "battery": {}, + "batteryLevel": "Medium", + "batteryRaw": 170 + }, + "OfflineKeys": { + "created": [], + "loaded": [ + { + "UserID": "cccca94e-373e-aaaa-bbbb-333396827777", + "slot": 1, + "key": "kkk01d4300c1dcxxx1c330f794941111", + "created": "2017-12-10T03:12:09.215Z", + "loaded": "2017-12-10T03:12:54.391Z" + } + ], + "deleted": [], + "loadedhk": [ + { + "key": "kkk01d4300c1dcxxx1c330f794941222", + "slot": 256, + "UserID": "cccca94e-373e-aaaa-bbbb-333396827777", + "created": "2017-12-10T03:12:09.218Z", + "loaded": "2017-12-10T03:12:55.563Z" + } + ] + }, + "parametersToSet": {}, + "users": { + "cccca94e-373e-aaaa-bbbb-333396827777": { + "UserType": "superuser", + "FirstName": "Foo", + "LastName": "Bar", + "identifiers": ["email:foo@bar.com", "phone:+177777777777"], + "imageInfo": { + "original": { + "width": 948, + "height": 949, + "format": "jpg", + "url": "http://www.image.com/foo.jpeg", + "secure_url": "https://www.image.com/foo.jpeg" + }, + "thumbnail": { + "width": 128, + "height": 128, + "format": "jpg", + "url": "http://www.image.com/foo.jpeg", + "secure_url": "https://www.image.com/foo.jpeg" + } + } + } + }, + "pubsubChannel": "3333a674-ffff-aaaa-b351-b3a4473f3333", + "ruleHash": {}, + "cameras": [], + "geofenceLimits": { + "ios": { + "debounceInterval": 90, + "gpsAccuracyMultiplier": 2.5, + "maximumGeofence": 5000, + "minimumGeofence": 100, + "minGPSAccuracyRequired": 80 + } + } +} diff --git a/tests/components/yale/fixtures/reauth_jwt b/tests/components/yale/fixtures/reauth_jwt new file mode 100644 index 00000000000000..4db8d061b68605 --- /dev/null +++ b/tests/components/yale/fixtures/reauth_jwt @@ -0,0 +1 @@ +eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpbnN0YWxsSWQiOiIiLCJyZWdpb24iOiJpcmVsYW5kLXByb2QtYXdzIiwiYXBwbGljYXRpb25JZCI6IiIsInVzZXJJZCI6ImE3NmMyNWU1LTQ5YWEtNGMxNC1jZDBjLTQ4YTY5MzFlMjA4MSIsInZJbnN0YWxsSWQiOmZhbHNlLCJ2UGFzc3dvcmQiOnRydWUsInZFbWFpbCI6dHJ1ZSwidlBob25lIjp0cnVlLCJoYXNJbnN0YWxsSWQiOmZhbHNlLCJoYXNQYXNzd29yZCI6ZmFsc2UsImhhc0VtYWlsIjpmYWxzZSwiaGFzUGhvbmUiOmZhbHNlLCJpc0xvY2tlZE91dCI6ZmFsc2UsImNhcHRjaGEiOiIiLCJlbWFpbCI6W10sInBob25lIjpbXSwiZXhwaXJlc0F0IjoiMjAyNC0xMi0xOFQxMzo1NDowNS4xMzRaIiwidGVtcG9yYXJ5QWNjb3VudENyZWF0aW9uUGFzc3dvcmRMaW5rIjoiIiwiaWF0IjoxNzI0MTYyMDQ1LCJleHAiOjI3MzQ1MzAwNDUsIm9hdXRoIjp7ImFwcF9uYW1lIjoiSG9tZSBBc3Npc3RhbnQiLCJjbGllbnRfaWQiOiJiM2NkM2YwYi1mYjk3LTRkNmMtYmVlOS1hZjdhYjA0NzU4YzciLCJyZWRpcmVjdF91cmkiOiJodHRwczovL2FjY291bnQtbGluay5uYWJ1Y2FzYS5jb20vYXV0aG9yaXplX2NhbGxiYWNrIiwicGFydG5lcl9pZCI6IjY1Nzk3NDg4MTA2NmNhNDhjOTljMDgyNiJ9fQ.DtkHscsvbTE-SyKW3RxwXFQIKMf0xJwfPZN1X3JesqA diff --git a/tests/components/yale/fixtures/reauth_jwt_wrong_account b/tests/components/yale/fixtures/reauth_jwt_wrong_account new file mode 100644 index 00000000000000..b0b624381781ed --- /dev/null +++ b/tests/components/yale/fixtures/reauth_jwt_wrong_account @@ -0,0 +1 @@ +eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpbnN0YWxsSWQiOiIiLCJyZWdpb24iOiJpcmVsYW5kLXByb2QtYXdzIiwiYXBwbGljYXRpb25JZCI6IiIsInVzZXJJZCI6IjQ0NDQ0NDQ0LTQ5YWEtNGMxNC1jZDBjLTQ4YTY5MzFlMjA4MSIsInZJbnN0YWxsSWQiOmZhbHNlLCJ2UGFzc3dvcmQiOnRydWUsInZFbWFpbCI6dHJ1ZSwidlBob25lIjp0cnVlLCJoYXNJbnN0YWxsSWQiOmZhbHNlLCJoYXNQYXNzd29yZCI6ZmFsc2UsImhhc0VtYWlsIjpmYWxzZSwiaGFzUGhvbmUiOmZhbHNlLCJpc0xvY2tlZE91dCI6ZmFsc2UsImNhcHRjaGEiOiIiLCJlbWFpbCI6W10sInBob25lIjpbXSwiZXhwaXJlc0F0IjoiMjAyNC0xMi0xOFQxMzo1NDowNS4xMzRaIiwidGVtcG9yYXJ5QWNjb3VudENyZWF0aW9uUGFzc3dvcmRMaW5rIjoiIiwiaWF0IjoxNzI0MTYyMDQ1LCJleHAiOjE3MzQ1MzAwNDUsIm9hdXRoIjp7ImFwcF9uYW1lIjoiSG9tZSBBc3Npc3RhbnQiLCJjbGllbnRfaWQiOiJiM2NkM2YwYi1mYjk3LTRkNmMtYmVlOS1hZjdhYjA0NzU4YzciLCJyZWRpcmVjdF91cmkiOiJodHRwczovL2FjY291bnQtbGluay5uYWJ1Y2FzYS5jb20vYXV0aG9yaXplX2NhbGxiYWNrIiwicGFydG5lcl9pZCI6IjY1Nzk3NDg4MTA2NmNhNDhjOTljMDgyNiJ9fQ.PenDp4JUIBQZEx2BFxaCqV1-6yMuUPtmnB6jq1wpoX8 diff --git a/tests/components/yale/fixtures/unlock_closed.json b/tests/components/yale/fixtures/unlock_closed.json new file mode 100644 index 00000000000000..f676c005a171c9 --- /dev/null +++ b/tests/components/yale/fixtures/unlock_closed.json @@ -0,0 +1,26 @@ +{ + "status": "kAugLockState_Unlocked", + "resultsFromOperationCache": false, + "retryCount": 1, + "info": { + "wlanRSSI": -54, + "lockType": "lock_version_1001", + "lockStatusChanged": false, + "serialNumber": "ABC", + "serial": "123", + "action": "lock", + "context": { + "startDate": "2020-02-19T01:59:39.516Z", + "retryCount": 1, + "transactionID": "mock" + }, + "bridgeID": "mock", + "wlanSNR": 41, + "startTime": "2020-02-19T01:59:39.517Z", + "duration": 5149, + "lockID": "ABC", + "rssi": -77 + }, + "totalTime": 5162, + "doorState": "kAugDoorState_Closed" +} diff --git a/tests/components/yale/mocks.py b/tests/components/yale/mocks.py new file mode 100644 index 00000000000000..03ab36090024c3 --- /dev/null +++ b/tests/components/yale/mocks.py @@ -0,0 +1,515 @@ +"""Mocks for the yale component.""" + +from __future__ import annotations + +from collections.abc import Iterable +from contextlib import contextmanager +import json +import os +import time +from typing import Any +from unittest.mock import AsyncMock, MagicMock, PropertyMock, patch + +from yalexs.activity import ( + ACTIVITY_ACTIONS_BRIDGE_OPERATION, + ACTIVITY_ACTIONS_DOOR_OPERATION, + ACTIVITY_ACTIONS_DOORBELL_DING, + ACTIVITY_ACTIONS_DOORBELL_MOTION, + ACTIVITY_ACTIONS_DOORBELL_VIEW, + ACTIVITY_ACTIONS_LOCK_OPERATION, + SOURCE_LOCK_OPERATE, + SOURCE_LOG, + Activity, + BridgeOperationActivity, + DoorbellDingActivity, + DoorbellMotionActivity, + DoorbellViewActivity, + DoorOperationActivity, + LockOperationActivity, +) +from yalexs.api_async import ApiAsync +from yalexs.authenticator_common import Authentication, AuthenticationState +from yalexs.const import Brand +from yalexs.doorbell import Doorbell, DoorbellDetail +from yalexs.lock import Lock, LockDetail +from yalexs.manager.ratelimit import _RateLimitChecker +from yalexs.manager.socketio import SocketIORunner + +from homeassistant.components.application_credentials import ( + ClientCredential, + async_import_client_credential, +) +from homeassistant.components.yale.const import DOMAIN +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant +from homeassistant.setup import async_setup_component + +from tests.common import MockConfigEntry, load_fixture + +USER_ID = "a76c25e5-49aa-4c14-cd0c-48a6931e2081" + + +def _mock_get_config( + brand: Brand = Brand.YALE_GLOBAL, jwt: str | None = None +) -> dict[str, Any]: + """Return a default yale config.""" + return { + DOMAIN: { + "auth_implementation": "yale", + "token": { + "access_token": jwt or "access_token", + "expires_in": 1, + "refresh_token": "refresh_token", + "expires_at": time.time() + 3600, + "service": "yale", + }, + } + } + + +def _mock_authenticator(auth_state: AuthenticationState) -> Authentication: + """Mock an yale authenticator.""" + authenticator = MagicMock() + type(authenticator).state = PropertyMock(return_value=auth_state) + return authenticator + + +def _timetoken() -> str: + return str(time.time_ns())[:-2] + + +async def mock_yale_config_entry( + hass: HomeAssistant, +) -> MockConfigEntry: + """Mock yale config entry and client credentials.""" + entry = mock_config_entry() + entry.add_to_hass(hass) + return entry + + +def mock_config_entry(jwt: str | None = None) -> MockConfigEntry: + """Return the default mocked config entry.""" + return MockConfigEntry( + domain=DOMAIN, + data=_mock_get_config(jwt=jwt)[DOMAIN], + options={}, + unique_id=USER_ID, + ) + + +async def mock_client_credentials(hass: HomeAssistant) -> ClientCredential: + """Mock client credentials.""" + assert await async_setup_component(hass, "application_credentials", {}) + await async_import_client_credential( + hass, + DOMAIN, + ClientCredential("1", "2"), + DOMAIN, + ) + + +@contextmanager +def patch_yale_setup(): + """Patch yale setup process.""" + with ( + patch("yalexs.manager.gateway.ApiAsync") as api_mock, + patch.object(_RateLimitChecker, "register_wakeup") as authenticate_mock, + patch("yalexs.manager.data.SocketIORunner") as socketio_mock, + patch.object(socketio_mock, "run"), + patch( + "homeassistant.components.yale.config_entry_oauth2_flow.async_get_config_entry_implementation" + ), + ): + yield api_mock, authenticate_mock, socketio_mock + + +async def _mock_setup_yale( + hass: HomeAssistant, + api_instance: ApiAsync, + socketio_mock: SocketIORunner, + authenticate_side_effect: MagicMock, +) -> ConfigEntry: + """Set up yale integration.""" + entry = await mock_yale_config_entry(hass) + with patch_yale_setup() as patched_setup: + api_mock, authenticate_mock, sockio_mock_ = patched_setup + authenticate_mock.side_effect = authenticate_side_effect + sockio_mock_.return_value = socketio_mock + api_mock.return_value = api_instance + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + return entry + + +async def _create_yale_with_devices( + hass: HomeAssistant, + devices: Iterable[LockDetail | DoorbellDetail] | None = None, + api_call_side_effects: dict[str, Any] | None = None, + activities: list[Any] | None = None, + brand: Brand = Brand.YALE_GLOBAL, + authenticate_side_effect: MagicMock | None = None, +) -> tuple[ConfigEntry, SocketIORunner]: + entry, _, socketio = await _create_yale_api_with_devices( + hass, + devices, + api_call_side_effects, + activities, + brand, + authenticate_side_effect, + ) + return entry, socketio + + +async def _create_yale_api_with_devices( + hass: HomeAssistant, + devices: Iterable[LockDetail | DoorbellDetail] | None = None, + api_call_side_effects: dict[str, Any] | None = None, + activities: dict[str, Any] | None = None, + brand: Brand = Brand.YALE_GLOBAL, + authenticate_side_effect: MagicMock | None = None, +) -> tuple[ConfigEntry, ApiAsync, SocketIORunner]: + if api_call_side_effects is None: + api_call_side_effects = {} + if devices is None: + devices = () + + update_api_call_side_effects(api_call_side_effects, devices, activities) + + api_instance = await make_mock_api(api_call_side_effects, brand) + socketio = SocketIORunner( + MagicMock( + api=api_instance, async_get_access_token=AsyncMock(return_value="token") + ) + ) + socketio.run = AsyncMock() + + entry = await _mock_setup_yale( + hass, + api_instance, + socketio, + authenticate_side_effect=authenticate_side_effect, + ) + + return entry, api_instance, socketio + + +def update_api_call_side_effects( + api_call_side_effects: dict[str, Any], + devices: Iterable[LockDetail | DoorbellDetail], + activities: dict[str, Any] | None = None, +) -> None: + """Update side effects dict from devices and activities.""" + + device_data = {"doorbells": [], "locks": []} + for device in devices or (): + if isinstance(device, LockDetail): + device_data["locks"].append( + {"base": _mock_yale_lock(device.device_id), "detail": device} + ) + elif isinstance(device, DoorbellDetail): + device_data["doorbells"].append( + { + "base": _mock_yale_doorbell( + deviceid=device.device_id, + brand=device._data.get("brand", Brand.YALE_GLOBAL), + ), + "detail": device, + } + ) + else: + raise ValueError # noqa: TRY004 + + def _get_device_detail(device_type, device_id): + for device in device_data[device_type]: + if device["detail"].device_id == device_id: + return device["detail"] + raise ValueError + + def _get_base_devices(device_type): + return [device["base"] for device in device_data[device_type]] + + def get_lock_detail_side_effect(access_token, device_id): + return _get_device_detail("locks", device_id) + + def get_doorbell_detail_side_effect(access_token, device_id): + return _get_device_detail("doorbells", device_id) + + def get_operable_locks_side_effect(access_token): + return _get_base_devices("locks") + + def get_doorbells_side_effect(access_token): + return _get_base_devices("doorbells") + + def get_house_activities_side_effect(access_token, house_id, limit=10): + if activities is not None: + return activities + return [] + + def lock_return_activities_side_effect(access_token, device_id): + lock = _get_device_detail("locks", device_id) + return [ + # There is a check to prevent out of order events + # so we set the doorclosed & lock event in the future + # to prevent a race condition where we reject the event + # because it happened before the dooropen & unlock event. + _mock_lock_operation_activity(lock, "lock", 2000), + _mock_door_operation_activity(lock, "doorclosed", 2000), + ] + + def unlock_return_activities_side_effect(access_token, device_id): + lock = _get_device_detail("locks", device_id) + return [ + _mock_lock_operation_activity(lock, "unlock", 0), + _mock_door_operation_activity(lock, "dooropen", 0), + ] + + api_call_side_effects.setdefault("get_lock_detail", get_lock_detail_side_effect) + api_call_side_effects.setdefault( + "get_doorbell_detail", get_doorbell_detail_side_effect + ) + api_call_side_effects.setdefault( + "get_operable_locks", get_operable_locks_side_effect + ) + api_call_side_effects.setdefault("get_doorbells", get_doorbells_side_effect) + api_call_side_effects.setdefault( + "get_house_activities", get_house_activities_side_effect + ) + api_call_side_effects.setdefault( + "lock_return_activities", lock_return_activities_side_effect + ) + api_call_side_effects.setdefault( + "unlock_return_activities", unlock_return_activities_side_effect + ) + api_call_side_effects.setdefault( + "async_unlatch_return_activities", unlock_return_activities_side_effect + ) + + +async def make_mock_api( + api_call_side_effects: dict[str, Any], + brand: Brand = Brand.YALE_GLOBAL, +) -> ApiAsync: + """Make a mock ApiAsync instance.""" + api_instance = MagicMock(name="Api", brand=brand) + + if api_call_side_effects["get_lock_detail"]: + type(api_instance).async_get_lock_detail = AsyncMock( + side_effect=api_call_side_effects["get_lock_detail"] + ) + + if api_call_side_effects["get_operable_locks"]: + type(api_instance).async_get_operable_locks = AsyncMock( + side_effect=api_call_side_effects["get_operable_locks"] + ) + + if api_call_side_effects["get_doorbells"]: + type(api_instance).async_get_doorbells = AsyncMock( + side_effect=api_call_side_effects["get_doorbells"] + ) + + if api_call_side_effects["get_doorbell_detail"]: + type(api_instance).async_get_doorbell_detail = AsyncMock( + side_effect=api_call_side_effects["get_doorbell_detail"] + ) + + if api_call_side_effects["get_house_activities"]: + type(api_instance).async_get_house_activities = AsyncMock( + side_effect=api_call_side_effects["get_house_activities"] + ) + + if api_call_side_effects["lock_return_activities"]: + type(api_instance).async_lock_return_activities = AsyncMock( + side_effect=api_call_side_effects["lock_return_activities"] + ) + + if api_call_side_effects["unlock_return_activities"]: + type(api_instance).async_unlock_return_activities = AsyncMock( + side_effect=api_call_side_effects["unlock_return_activities"] + ) + + if api_call_side_effects["async_unlatch_return_activities"]: + type(api_instance).async_unlatch_return_activities = AsyncMock( + side_effect=api_call_side_effects["async_unlatch_return_activities"] + ) + + api_instance.async_unlock_async = AsyncMock() + api_instance.async_lock_async = AsyncMock() + api_instance.async_status_async = AsyncMock() + api_instance.async_get_user = AsyncMock(return_value={"UserID": "abc"}) + api_instance.async_unlatch_async = AsyncMock() + api_instance.async_unlatch = AsyncMock() + api_instance.async_add_websocket_subscription = AsyncMock() + + return api_instance + + +def _mock_yale_authentication( + token_text: str, token_timestamp: float, state: AuthenticationState +) -> Authentication: + authentication = MagicMock(name="yalexs.authentication") + type(authentication).state = PropertyMock(return_value=state) + type(authentication).access_token = PropertyMock(return_value=token_text) + type(authentication).access_token_expires = PropertyMock( + return_value=token_timestamp + ) + return authentication + + +def _mock_yale_lock(lockid: str = "mocklockid1", houseid: str = "mockhouseid1") -> Lock: + return Lock(lockid, _mock_yale_lock_data(lockid=lockid, houseid=houseid)) + + +def _mock_yale_doorbell( + deviceid="mockdeviceid1", houseid="mockhouseid1", brand=Brand.YALE_GLOBAL +) -> Doorbell: + return Doorbell( + deviceid, + _mock_yale_doorbell_data(deviceid=deviceid, houseid=houseid, brand=brand), + ) + + +def _mock_yale_doorbell_data( + deviceid: str = "mockdeviceid1", + houseid: str = "mockhouseid1", + brand: Brand = Brand.YALE_GLOBAL, +) -> dict[str, Any]: + return { + "_id": deviceid, + "DeviceID": deviceid, + "name": f"{deviceid} Name", + "HouseID": houseid, + "UserType": "owner", + "serialNumber": "mockserial", + "battery": 90, + "status": "standby", + "currentFirmwareVersion": "mockfirmware", + "Bridge": { + "_id": "bridgeid1", + "firmwareVersion": "mockfirm", + "operative": True, + }, + "LockStatus": {"doorState": "open"}, + } + + +def _mock_yale_lock_data( + lockid: str = "mocklockid1", houseid: str = "mockhouseid1" +) -> dict[str, Any]: + return { + "_id": lockid, + "LockID": lockid, + "LockName": f"{lockid} Name", + "HouseID": houseid, + "UserType": "owner", + "SerialNumber": "mockserial", + "battery": 90, + "currentFirmwareVersion": "mockfirmware", + "Bridge": { + "_id": "bridgeid1", + "firmwareVersion": "mockfirm", + "operative": True, + }, + "LockStatus": {"doorState": "open"}, + } + + +async def _mock_operative_yale_lock_detail(hass: HomeAssistant) -> LockDetail: + return await _mock_lock_from_fixture(hass, "get_lock.online.json") + + +async def _mock_lock_with_offline_key(hass: HomeAssistant) -> LockDetail: + return await _mock_lock_from_fixture(hass, "get_lock.online_with_keys.json") + + +async def _mock_inoperative_yale_lock_detail(hass: HomeAssistant) -> LockDetail: + return await _mock_lock_from_fixture(hass, "get_lock.offline.json") + + +async def _mock_activities_from_fixture( + hass: HomeAssistant, path: str +) -> list[Activity]: + json_dict = await _load_json_fixture(hass, path) + activities = [] + for activity_json in json_dict: + activity = _activity_from_dict(activity_json) + if activity: + activities.append(activity) + + return activities + + +async def _mock_lock_from_fixture(hass: HomeAssistant, path: str) -> LockDetail: + json_dict = await _load_json_fixture(hass, path) + return LockDetail(json_dict) + + +async def _mock_doorbell_from_fixture(hass: HomeAssistant, path: str) -> LockDetail: + json_dict = await _load_json_fixture(hass, path) + return DoorbellDetail(json_dict) + + +async def _load_json_fixture(hass: HomeAssistant, path: str) -> dict[str, Any]: + fixture = await hass.async_add_executor_job( + load_fixture, os.path.join("yale", path) + ) + return json.loads(fixture) + + +async def _mock_doorsense_enabled_yale_lock_detail(hass: HomeAssistant) -> LockDetail: + return await _mock_lock_from_fixture(hass, "get_lock.online_with_doorsense.json") + + +async def _mock_doorsense_missing_yale_lock_detail(hass: HomeAssistant) -> LockDetail: + return await _mock_lock_from_fixture(hass, "get_lock.online_missing_doorsense.json") + + +async def _mock_lock_with_unlatch(hass: HomeAssistant) -> LockDetail: + return await _mock_lock_from_fixture(hass, "get_lock.online_with_unlatch.json") + + +def _mock_lock_operation_activity( + lock: Lock, action: str, offset: float +) -> LockOperationActivity: + return LockOperationActivity( + SOURCE_LOCK_OPERATE, + { + "dateTime": (time.time() + offset) * 1000, + "deviceID": lock.device_id, + "deviceType": "lock", + "action": action, + }, + ) + + +def _mock_door_operation_activity( + lock: Lock, action: str, offset: float +) -> DoorOperationActivity: + return DoorOperationActivity( + SOURCE_LOCK_OPERATE, + { + "dateTime": (time.time() + offset) * 1000, + "deviceID": lock.device_id, + "deviceType": "lock", + "action": action, + }, + ) + + +def _activity_from_dict(activity_dict: dict[str, Any]) -> Activity | None: + action = activity_dict.get("action") + + activity_dict["dateTime"] = time.time() * 1000 + + if action in ACTIVITY_ACTIONS_DOORBELL_DING: + return DoorbellDingActivity(SOURCE_LOG, activity_dict) + if action in ACTIVITY_ACTIONS_DOORBELL_MOTION: + return DoorbellMotionActivity(SOURCE_LOG, activity_dict) + if action in ACTIVITY_ACTIONS_DOORBELL_VIEW: + return DoorbellViewActivity(SOURCE_LOG, activity_dict) + if action in ACTIVITY_ACTIONS_LOCK_OPERATION: + return LockOperationActivity(SOURCE_LOG, activity_dict) + if action in ACTIVITY_ACTIONS_DOOR_OPERATION: + return DoorOperationActivity(SOURCE_LOG, activity_dict) + if action in ACTIVITY_ACTIONS_BRIDGE_OPERATION: + return BridgeOperationActivity(SOURCE_LOG, activity_dict) + return None diff --git a/tests/components/yale/snapshots/test_binary_sensor.ambr b/tests/components/yale/snapshots/test_binary_sensor.ambr new file mode 100644 index 00000000000000..e294cb7c76c1ce --- /dev/null +++ b/tests/components/yale/snapshots/test_binary_sensor.ambr @@ -0,0 +1,33 @@ +# serializer version: 1 +# name: test_doorbell_device_registry + DeviceRegistryEntrySnapshot({ + 'area_id': 'tmt100_name', + 'config_entries': , + 'configuration_url': 'https://account.aaecosystem.com', + 'connections': set({ + }), + 'disabled_by': None, + 'entry_type': None, + 'hw_version': None, + 'id': , + 'identifiers': set({ + tuple( + 'yale', + 'tmt100', + ), + }), + 'is_new': False, + 'labels': set({ + }), + 'manufacturer': 'Yale Home Inc.', + 'model': 'hydra1', + 'model_id': None, + 'name': 'tmt100 Name', + 'name_by_user': None, + 'primary_config_entry': , + 'serial_number': None, + 'suggested_area': 'tmt100 Name', + 'sw_version': '3.1.0-HYDRC75+201909251139', + 'via_device_id': None, + }) +# --- diff --git a/tests/components/yale/snapshots/test_diagnostics.ambr b/tests/components/yale/snapshots/test_diagnostics.ambr new file mode 100644 index 00000000000000..c3d8d8e2aaac4a --- /dev/null +++ b/tests/components/yale/snapshots/test_diagnostics.ambr @@ -0,0 +1,125 @@ +# serializer version: 1 +# name: test_diagnostics + dict({ + 'brand': 'yale_global', + 'doorbells': dict({ + 'K98GiDT45GUL': dict({ + 'HouseID': '**REDACTED**', + 'LockID': 'BBBB1F5F11114C24CCCC97571DD6AAAA', + 'appID': 'august-iphone', + 'caps': list([ + 'reconnect', + ]), + 'createdAt': '2016-11-26T22:27:11.176Z', + 'doorbellID': 'K98GiDT45GUL', + 'doorbellServerURL': 'https://doorbells.august.com', + 'dvrSubscriptionSetupDone': True, + 'firmwareVersion': '2.3.0-RC153+201711151527', + 'installDate': '2016-11-26T22:27:11.176Z', + 'installUserID': '**REDACTED**', + 'name': 'Front Door', + 'pubsubChannel': '**REDACTED**', + 'recentImage': '**REDACTED**', + 'serialNumber': 'tBXZR0Z35E', + 'settings': dict({ + 'ABREnabled': True, + 'IREnabled': True, + 'IVAEnabled': False, + 'JPGQuality': 70, + 'batteryLowThreshold': 3.1, + 'batteryRun': False, + 'batteryUseThreshold': 3.4, + 'bitrateCeiling': 512000, + 'buttonpush_notifications': True, + 'debug': False, + 'directLink': True, + 'initialBitrate': 384000, + 'irConfiguration': 8448272, + 'keepEncoderRunning': True, + 'micVolume': 100, + 'minACNoScaling': 40, + 'motion_notifications': True, + 'notify_when_offline': True, + 'overlayEnabled': True, + 'ringSoundEnabled': True, + 'speakerVolume': 92, + 'turnOffCamera': False, + 'videoResolution': '640x480', + }), + 'status': 'doorbell_call_status_online', + 'status_timestamp': 1512811834532, + 'telemetry': dict({ + 'BSSID': '88:ee:00:dd:aa:11', + 'SSID': 'foo_ssid', + 'ac_in': 23.856874, + 'battery': 4.061763, + 'battery_soc': 96, + 'battery_soh': 95, + 'date': '2017-12-10 08:05:12', + 'doorbell_low_battery': False, + 'ip_addr': '10.0.1.11', + 'link_quality': 54, + 'load_average': '0.50 0.47 0.35 1/154 9345', + 'signal_level': -56, + 'steady_ac_in': 22.196405, + 'temperature': 28.25, + 'updated_at': '2017-12-10T08:05:13.650Z', + 'uptime': '16168.75 13830.49', + 'wifi_freq': 5745, + }), + 'updatedAt': '2017-12-10T08:05:13.650Z', + }), + }), + 'locks': dict({ + 'online_with_doorsense': dict({ + 'Bridge': dict({ + '_id': 'bridgeid', + 'deviceModel': 'august-connect', + 'firmwareVersion': '2.2.1', + 'hyperBridge': True, + 'mfgBridgeID': 'C5WY200WSH', + 'operative': True, + 'status': dict({ + 'current': 'online', + 'lastOffline': '2000-00-00T00:00:00.447Z', + 'lastOnline': '2000-00-00T00:00:00.447Z', + 'updated': '2000-00-00T00:00:00.447Z', + }), + }), + 'Calibrated': False, + 'Created': '2000-00-00T00:00:00.447Z', + 'HouseID': '**REDACTED**', + 'HouseName': 'Test', + 'LockID': 'online_with_doorsense', + 'LockName': 'Online door with doorsense', + 'LockStatus': dict({ + 'dateTime': '2017-12-10T04:48:30.272Z', + 'doorState': 'open', + 'isLockStatusChanged': False, + 'status': 'locked', + 'valid': True, + }), + 'SerialNumber': 'XY', + 'Type': 1001, + 'Updated': '2000-00-00T00:00:00.447Z', + 'battery': 0.922, + 'currentFirmwareVersion': 'undefined-4.3.0-1.8.14', + 'homeKitEnabled': True, + 'hostLockInfo': dict({ + 'manufacturer': 'yale', + 'productID': 1536, + 'productTypeID': 32770, + 'serialNumber': 'ABC', + }), + 'isGalileo': False, + 'macAddress': '12:22', + 'pins': '**REDACTED**', + 'pubsubChannel': '**REDACTED**', + 'skuNumber': 'AUG-MD01', + 'supportsEntryCodes': True, + 'timeZone': 'Pacific/Hawaii', + 'zWaveEnabled': False, + }), + }), + }) +# --- diff --git a/tests/components/yale/snapshots/test_lock.ambr b/tests/components/yale/snapshots/test_lock.ambr new file mode 100644 index 00000000000000..b1a9f6a4d86c8a --- /dev/null +++ b/tests/components/yale/snapshots/test_lock.ambr @@ -0,0 +1,37 @@ +# serializer version: 1 +# name: test_lock_device_registry + DeviceRegistryEntrySnapshot({ + 'area_id': 'online_with_doorsense_name', + 'config_entries': , + 'configuration_url': 'https://account.aaecosystem.com', + 'connections': set({ + tuple( + 'bluetooth', + '12:22', + ), + }), + 'disabled_by': None, + 'entry_type': None, + 'hw_version': None, + 'id': , + 'identifiers': set({ + tuple( + 'yale', + 'online_with_doorsense', + ), + }), + 'is_new': False, + 'labels': set({ + }), + 'manufacturer': 'Yale Home Inc.', + 'model': 'AUG-MD01', + 'model_id': None, + 'name': 'online_with_doorsense Name', + 'name_by_user': None, + 'primary_config_entry': , + 'serial_number': None, + 'suggested_area': 'online_with_doorsense Name', + 'sw_version': 'undefined-4.3.0-1.8.14', + 'via_device_id': None, + }) +# --- diff --git a/tests/components/yale/snapshots/test_sensor.ambr b/tests/components/yale/snapshots/test_sensor.ambr new file mode 100644 index 00000000000000..a425cfa90de5d3 --- /dev/null +++ b/tests/components/yale/snapshots/test_sensor.ambr @@ -0,0 +1,95 @@ +# serializer version: 1 +# name: test_lock_operator_autorelock + ReadOnlyDict({ + 'autorelock': True, + 'friendly_name': 'online_with_doorsense Name Operator', + 'keypad': False, + 'manual': False, + 'method': 'autorelock', + 'remote': False, + 'tag': False, + }) +# --- +# name: test_lock_operator_keypad + ReadOnlyDict({ + 'autorelock': False, + 'friendly_name': 'online_with_doorsense Name Operator', + 'keypad': True, + 'manual': False, + 'method': 'keypad', + 'remote': False, + 'tag': False, + }) +# --- +# name: test_lock_operator_manual + ReadOnlyDict({ + 'autorelock': False, + 'friendly_name': 'online_with_doorsense Name Operator', + 'keypad': False, + 'manual': True, + 'method': 'manual', + 'remote': False, + 'tag': False, + }) +# --- +# name: test_lock_operator_remote + ReadOnlyDict({ + 'autorelock': False, + 'friendly_name': 'online_with_doorsense Name Operator', + 'keypad': False, + 'manual': False, + 'method': 'remote', + 'remote': True, + 'tag': False, + }) +# --- +# name: test_restored_state + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'autorelock': False, + 'entity_picture': 'image.png', + 'friendly_name': 'online_with_doorsense Name Operator', + 'keypad': False, + 'manual': False, + 'method': 'tag', + 'remote': False, + 'tag': True, + }), + 'context': , + 'entity_id': 'sensor.online_with_doorsense_name_operator', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'Tag Unlock', + }) +# --- +# name: test_unlock_operator_manual + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'autorelock': False, + 'friendly_name': 'online_with_doorsense Name Operator', + 'keypad': False, + 'manual': True, + 'method': 'manual', + 'remote': False, + 'tag': False, + }), + 'context': , + 'entity_id': 'sensor.online_with_doorsense_name_operator', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'Your favorite elven princess', + }) +# --- +# name: test_unlock_operator_tag + ReadOnlyDict({ + 'autorelock': False, + 'friendly_name': 'online_with_doorsense Name Operator', + 'keypad': False, + 'manual': False, + 'method': 'tag', + 'remote': False, + 'tag': True, + }) +# --- diff --git a/tests/components/yale/test_binary_sensor.py b/tests/components/yale/test_binary_sensor.py new file mode 100644 index 00000000000000..811c845e3592a1 --- /dev/null +++ b/tests/components/yale/test_binary_sensor.py @@ -0,0 +1,308 @@ +"""The binary_sensor tests for the yale platform.""" + +import datetime + +from freezegun.api import FrozenDateTimeFactory +from syrupy import SnapshotAssertion + +from homeassistant.components.lock import DOMAIN as LOCK_DOMAIN +from homeassistant.const import ( + ATTR_ENTITY_ID, + SERVICE_LOCK, + SERVICE_UNLOCK, + STATE_OFF, + STATE_ON, + STATE_UNAVAILABLE, +) +from homeassistant.core import HomeAssistant +from homeassistant.helpers import device_registry as dr +import homeassistant.util.dt as dt_util + +from .mocks import ( + _create_yale_with_devices, + _mock_activities_from_fixture, + _mock_doorbell_from_fixture, + _mock_doorsense_enabled_yale_lock_detail, + _mock_lock_from_fixture, +) + +from tests.common import async_fire_time_changed + + +async def test_doorsense(hass: HomeAssistant) -> None: + """Test creation of a lock with doorsense and bridge.""" + lock_one = await _mock_lock_from_fixture( + hass, "get_lock.online_with_doorsense.json" + ) + await _create_yale_with_devices(hass, [lock_one]) + states = hass.states + assert states.get("binary_sensor.online_with_doorsense_name_door").state == STATE_ON + + data = {ATTR_ENTITY_ID: "lock.online_with_doorsense_name"} + await hass.services.async_call(LOCK_DOMAIN, SERVICE_UNLOCK, data, blocking=True) + + assert states.get("binary_sensor.online_with_doorsense_name_door").state == STATE_ON + + await hass.services.async_call(LOCK_DOMAIN, SERVICE_LOCK, data, blocking=True) + + assert ( + states.get("binary_sensor.online_with_doorsense_name_door").state == STATE_OFF + ) + + +async def test_lock_bridge_offline(hass: HomeAssistant) -> None: + """Test creation of a lock with doorsense and bridge that goes offline.""" + lock_one = await _mock_lock_from_fixture( + hass, "get_lock.online_with_doorsense.json" + ) + activities = await _mock_activities_from_fixture( + hass, "get_activity.bridge_offline.json" + ) + await _create_yale_with_devices(hass, [lock_one], activities=activities) + states = hass.states + assert ( + states.get("binary_sensor.online_with_doorsense_name_door").state + == STATE_UNAVAILABLE + ) + + +async def test_create_doorbell(hass: HomeAssistant) -> None: + """Test creation of a doorbell.""" + doorbell_one = await _mock_doorbell_from_fixture(hass, "get_doorbell.json") + await _create_yale_with_devices(hass, [doorbell_one]) + states = hass.states + assert states.get("binary_sensor.k98gidt45gul_name_motion").state == STATE_OFF + assert ( + states.get("binary_sensor.k98gidt45gul_name_image_capture").state == STATE_OFF + ) + assert states.get("binary_sensor.k98gidt45gul_name_connectivity").state == STATE_ON + assert ( + states.get("binary_sensor.k98gidt45gul_name_doorbell_ding").state == STATE_OFF + ) + assert states.get("binary_sensor.k98gidt45gul_name_motion").state == STATE_OFF + assert ( + states.get("binary_sensor.k98gidt45gul_name_image_capture").state == STATE_OFF + ) + + +async def test_create_doorbell_offline(hass: HomeAssistant) -> None: + """Test creation of a doorbell that is offline.""" + doorbell_one = await _mock_doorbell_from_fixture(hass, "get_doorbell.offline.json") + await _create_yale_with_devices(hass, [doorbell_one]) + states = hass.states + assert states.get("binary_sensor.tmt100_name_motion").state == STATE_UNAVAILABLE + assert states.get("binary_sensor.tmt100_name_connectivity").state == STATE_OFF + assert ( + states.get("binary_sensor.tmt100_name_doorbell_ding").state == STATE_UNAVAILABLE + ) + + +async def test_create_doorbell_with_motion( + hass: HomeAssistant, freezer: FrozenDateTimeFactory +) -> None: + """Test creation of a doorbell.""" + doorbell_one = await _mock_doorbell_from_fixture(hass, "get_doorbell.json") + activities = await _mock_activities_from_fixture( + hass, "get_activity.doorbell_motion.json" + ) + await _create_yale_with_devices(hass, [doorbell_one], activities=activities) + states = hass.states + assert states.get("binary_sensor.k98gidt45gul_name_motion").state == STATE_ON + assert states.get("binary_sensor.k98gidt45gul_name_connectivity").state == STATE_ON + assert ( + states.get("binary_sensor.k98gidt45gul_name_doorbell_ding").state == STATE_OFF + ) + freezer.tick(40) + async_fire_time_changed(hass) + await hass.async_block_till_done() + assert states.get("binary_sensor.k98gidt45gul_name_motion").state == STATE_OFF + + +async def test_doorbell_update_via_socketio( + hass: HomeAssistant, freezer: FrozenDateTimeFactory +) -> None: + """Test creation of a doorbell that can be updated via socketio.""" + doorbell_one = await _mock_doorbell_from_fixture(hass, "get_doorbell.json") + + _, socketio = await _create_yale_with_devices(hass, [doorbell_one]) + assert doorbell_one.pubsub_channel == "7c7a6672-59c8-3333-ffff-dcd98705cccc" + states = hass.states + assert states.get("binary_sensor.k98gidt45gul_name_motion").state == STATE_OFF + assert ( + states.get("binary_sensor.k98gidt45gul_name_doorbell_ding").state == STATE_OFF + ) + + listener = list(socketio._listeners)[0] + listener( + doorbell_one.device_id, + dt_util.utcnow(), + { + "status": "imagecapture", + "data": { + "result": { + "created_at": "2021-03-16T01:07:08.817Z", + "secure_url": ( + "https://dyu7azbnaoi74.cloudfront.net/zip/images/zip.jpeg" + ), + }, + }, + }, + ) + + await hass.async_block_till_done() + + assert states.get("binary_sensor.k98gidt45gul_name_image_capture").state == STATE_ON + + listener( + doorbell_one.device_id, + dt_util.utcnow(), + { + "status": "doorbell_motion_detected", + "data": { + "event": "doorbell_motion_detected", + "image": { + "height": 640, + "width": 480, + "format": "jpg", + "created_at": "2021-03-16T02:36:26.886Z", + "bytes": 14061, + "secure_url": ( + "https://dyu7azbnaoi74.cloudfront.net/images/1f8.jpeg" + ), + "url": "https://dyu7azbnaoi74.cloudfront.net/images/1f8.jpeg", + "etag": "09e839331c4ea59eef28081f2caa0e90", + }, + "doorbellName": "Front Door", + "callID": None, + "origin": "mars-api", + "mutableContent": True, + }, + }, + ) + + await hass.async_block_till_done() + + assert states.get("binary_sensor.k98gidt45gul_name_motion").state == STATE_ON + assert ( + states.get("binary_sensor.k98gidt45gul_name_doorbell_ding").state == STATE_OFF + ) + + freezer.tick(40) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + assert ( + states.get("binary_sensor.k98gidt45gul_name_image_capture").state == STATE_OFF + ) + + listener( + doorbell_one.device_id, + dt_util.utcnow(), + { + "status": "buttonpush", + }, + ) + + await hass.async_block_till_done() + + assert states.get("binary_sensor.k98gidt45gul_name_doorbell_ding").state == STATE_ON + + freezer.tick(40) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + assert ( + states.get("binary_sensor.k98gidt45gul_name_doorbell_ding").state == STATE_OFF + ) + + +async def test_doorbell_device_registry( + hass: HomeAssistant, + device_registry: dr.DeviceRegistry, + snapshot: SnapshotAssertion, +) -> None: + """Test creation of a lock with doorsense and bridge ands up in the registry.""" + doorbell_one = await _mock_doorbell_from_fixture(hass, "get_doorbell.offline.json") + await _create_yale_with_devices(hass, [doorbell_one]) + + reg_device = device_registry.async_get_device(identifiers={("yale", "tmt100")}) + assert reg_device == snapshot + + +async def test_door_sense_update_via_socketio(hass: HomeAssistant) -> None: + """Test creation of a lock with doorsense and bridge.""" + lock_one = await _mock_doorsense_enabled_yale_lock_detail(hass) + assert lock_one.pubsub_channel == "pubsub" + + activities = await _mock_activities_from_fixture(hass, "get_activity.lock.json") + config_entry, socketio = await _create_yale_with_devices( + hass, [lock_one], activities=activities + ) + states = hass.states + assert states.get("binary_sensor.online_with_doorsense_name_door").state == STATE_ON + + listener = list(socketio._listeners)[0] + listener( + lock_one.device_id, + dt_util.utcnow(), + {"status": "kAugLockState_Unlocking", "doorState": "closed"}, + ) + + await hass.async_block_till_done() + + assert ( + states.get("binary_sensor.online_with_doorsense_name_door").state == STATE_OFF + ) + + listener( + lock_one.device_id, + dt_util.utcnow(), + {"status": "kAugLockState_Locking", "doorState": "open"}, + ) + + await hass.async_block_till_done() + + assert states.get("binary_sensor.online_with_doorsense_name_door").state == STATE_ON + + async_fire_time_changed(hass, dt_util.utcnow() + datetime.timedelta(seconds=30)) + await hass.async_block_till_done() + assert states.get("binary_sensor.online_with_doorsense_name_door").state == STATE_ON + + socketio.connected = True + async_fire_time_changed(hass, dt_util.utcnow() + datetime.timedelta(seconds=30)) + await hass.async_block_till_done() + assert states.get("binary_sensor.online_with_doorsense_name_door").state == STATE_ON + + # Ensure socketio status is always preserved + async_fire_time_changed(hass, dt_util.utcnow() + datetime.timedelta(hours=2)) + await hass.async_block_till_done() + assert states.get("binary_sensor.online_with_doorsense_name_door").state == STATE_ON + + listener( + lock_one.device_id, + dt_util.utcnow(), + {"status": "kAugLockState_Unlocking", "doorState": "open"}, + ) + + await hass.async_block_till_done() + assert states.get("binary_sensor.online_with_doorsense_name_door").state == STATE_ON + + async_fire_time_changed(hass, dt_util.utcnow() + datetime.timedelta(hours=4)) + await hass.async_block_till_done() + assert states.get("binary_sensor.online_with_doorsense_name_door").state == STATE_ON + + await hass.config_entries.async_unload(config_entry.entry_id) + await hass.async_block_till_done() + + +async def test_create_lock_with_doorbell(hass: HomeAssistant) -> None: + """Test creation of a lock with a doorbell.""" + lock_one = await _mock_lock_from_fixture(hass, "lock_with_doorbell.online.json") + await _create_yale_with_devices(hass, [lock_one]) + states = hass.states + assert ( + states.get( + "binary_sensor.a6697750d607098bae8d6baa11ef8063_name_doorbell_ding" + ).state + == STATE_OFF + ) diff --git a/tests/components/yale/test_button.py b/tests/components/yale/test_button.py new file mode 100644 index 00000000000000..92d3ecef85925c --- /dev/null +++ b/tests/components/yale/test_button.py @@ -0,0 +1,23 @@ +"""The button tests for the yale platform.""" + +from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS +from homeassistant.const import ATTR_ENTITY_ID +from homeassistant.core import HomeAssistant + +from .mocks import _create_yale_api_with_devices, _mock_lock_from_fixture + + +async def test_wake_lock(hass: HomeAssistant) -> None: + """Test creation of a lock and wake it.""" + lock_one = await _mock_lock_from_fixture( + hass, "get_lock.online_with_doorsense.json" + ) + _, api_instance, _ = await _create_yale_api_with_devices(hass, [lock_one]) + entity_id = "button.online_with_doorsense_name_wake" + binary_sensor_online_with_doorsense_name = hass.states.get(entity_id) + assert binary_sensor_online_with_doorsense_name is not None + api_instance.async_status_async.reset_mock() + await hass.services.async_call( + BUTTON_DOMAIN, SERVICE_PRESS, {ATTR_ENTITY_ID: entity_id}, blocking=True + ) + api_instance.async_status_async.assert_called_once() diff --git a/tests/components/yale/test_camera.py b/tests/components/yale/test_camera.py new file mode 100644 index 00000000000000..502945b19c1a07 --- /dev/null +++ b/tests/components/yale/test_camera.py @@ -0,0 +1,93 @@ +"""The camera tests for the yale platform.""" + +from http import HTTPStatus +from unittest.mock import patch + +from yalexs.const import Brand +from yalexs.doorbell import ContentTokenExpired + +from homeassistant.const import STATE_IDLE +from homeassistant.core import HomeAssistant + +from .mocks import _create_yale_with_devices, _mock_doorbell_from_fixture + +from tests.typing import ClientSessionGenerator + + +async def test_create_doorbell( + hass: HomeAssistant, hass_client_no_auth: ClientSessionGenerator +) -> None: + """Test creation of a doorbell.""" + doorbell_one = await _mock_doorbell_from_fixture(hass, "get_doorbell.json") + + with patch.object( + doorbell_one, "async_get_doorbell_image", create=False, return_value="image" + ): + await _create_yale_with_devices(hass, [doorbell_one], brand=Brand.YALE_GLOBAL) + + camera_k98gidt45gul_name_camera = hass.states.get( + "camera.k98gidt45gul_name_camera" + ) + assert camera_k98gidt45gul_name_camera.state == STATE_IDLE + + url = hass.states.get("camera.k98gidt45gul_name_camera").attributes[ + "entity_picture" + ] + + client = await hass_client_no_auth() + resp = await client.get(url) + assert resp.status == HTTPStatus.OK + body = await resp.text() + assert body == "image" + + +async def test_doorbell_refresh_content_token_recover( + hass: HomeAssistant, hass_client_no_auth: ClientSessionGenerator +) -> None: + """Test camera image content token expired.""" + doorbell_two = await _mock_doorbell_from_fixture(hass, "get_doorbell.json") + with patch.object( + doorbell_two, + "async_get_doorbell_image", + create=False, + side_effect=[ContentTokenExpired, "image"], + ): + await _create_yale_with_devices( + hass, + [doorbell_two], + brand=Brand.YALE_GLOBAL, + ) + url = hass.states.get("camera.k98gidt45gul_name_camera").attributes[ + "entity_picture" + ] + + client = await hass_client_no_auth() + resp = await client.get(url) + assert resp.status == HTTPStatus.OK + body = await resp.text() + assert body == "image" + + +async def test_doorbell_refresh_content_token_fail( + hass: HomeAssistant, hass_client_no_auth: ClientSessionGenerator +) -> None: + """Test camera image content token expired.""" + doorbell_two = await _mock_doorbell_from_fixture(hass, "get_doorbell.json") + with patch.object( + doorbell_two, + "async_get_doorbell_image", + create=False, + side_effect=ContentTokenExpired, + ): + await _create_yale_with_devices( + hass, + [doorbell_two], + brand=Brand.YALE_GLOBAL, + ) + url = hass.states.get("camera.k98gidt45gul_name_camera").attributes[ + "entity_picture" + ] + + client = await hass_client_no_auth() + resp = await client.get(url) + assert resp.status == HTTPStatus.INTERNAL_SERVER_ERROR diff --git a/tests/components/yale/test_config_flow.py b/tests/components/yale/test_config_flow.py new file mode 100644 index 00000000000000..004162c0ebfa3c --- /dev/null +++ b/tests/components/yale/test_config_flow.py @@ -0,0 +1,275 @@ +"""Test the yale config flow.""" + +from collections.abc import Generator +from unittest.mock import ANY, Mock, patch + +import pytest + +from homeassistant.components.yale.application_credentials import ( + OAUTH2_AUTHORIZE, + OAUTH2_TOKEN, +) +from homeassistant.components.yale.const import DOMAIN +from homeassistant.config_entries import SOURCE_USER +from homeassistant.core import HomeAssistant +from homeassistant.data_entry_flow import FlowResultType +from homeassistant.helpers import config_entry_oauth2_flow + +from .mocks import USER_ID + +from tests.common import MockConfigEntry +from tests.test_util.aiohttp import AiohttpClientMocker +from tests.typing import ClientSessionGenerator + +CLIENT_ID = "1" + + +@pytest.fixture +def mock_setup_entry() -> Generator[Mock]: + """Patch setup entry.""" + with patch( + "homeassistant.components.yale.async_setup_entry", return_value=True + ) as mock_setup_entry: + yield mock_setup_entry + + +@pytest.mark.usefixtures("client_credentials") +@pytest.mark.usefixtures("current_request_with_host") +async def test_full_flow( + hass: HomeAssistant, + hass_client_no_auth: ClientSessionGenerator, + aioclient_mock: AiohttpClientMocker, + jwt: str, + mock_setup_entry: Mock, +) -> None: + """Check full flow.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + state = config_entry_oauth2_flow._encode_jwt( + hass, + { + "flow_id": result["flow_id"], + "redirect_uri": "https://example.com/auth/external/callback", + }, + ) + + assert result["url"] == ( + f"{OAUTH2_AUTHORIZE}?response_type=code&client_id={CLIENT_ID}" + "&redirect_uri=https://example.com/auth/external/callback" + f"&state={state}" + ) + + client = await hass_client_no_auth() + resp = await client.get(f"/auth/external/callback?code=abcd&state={state}") + assert resp.status == 200 + assert resp.headers["content-type"] == "text/html; charset=utf-8" + + aioclient_mock.clear_requests() + aioclient_mock.post( + OAUTH2_TOKEN, + json={ + "access_token": jwt, + "scope": "any", + "expires_in": 86399, + "refresh_token": "mock-refresh-token", + "user_id": "mock-user-id", + "expires_at": 1697753347, + }, + ) + + result2 = await hass.config_entries.flow.async_configure(result["flow_id"]) + + assert len(hass.config_entries.async_entries(DOMAIN)) == 1 + assert len(mock_setup_entry.mock_calls) == 1 + entry = hass.config_entries.async_entries(DOMAIN)[0] + assert entry.unique_id == USER_ID + + assert result2["type"] is FlowResultType.CREATE_ENTRY + assert result2["result"].unique_id == USER_ID + assert entry.data == { + "auth_implementation": "yale", + "token": { + "access_token": jwt, + "expires_at": ANY, + "expires_in": ANY, + "refresh_token": "mock-refresh-token", + "scope": "any", + "user_id": "mock-user-id", + }, + } + + +@pytest.mark.usefixtures("client_credentials") +@pytest.mark.usefixtures("current_request_with_host") +async def test_full_flow_already_exists( + hass: HomeAssistant, + hass_client_no_auth: ClientSessionGenerator, + aioclient_mock: AiohttpClientMocker, + jwt: str, + mock_setup_entry: Mock, + mock_config_entry: MockConfigEntry, +) -> None: + """Check full flow for a user that already exists.""" + + mock_config_entry.add_to_hass(hass) + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + state = config_entry_oauth2_flow._encode_jwt( + hass, + { + "flow_id": result["flow_id"], + "redirect_uri": "https://example.com/auth/external/callback", + }, + ) + + assert result["url"] == ( + f"{OAUTH2_AUTHORIZE}?response_type=code&client_id={CLIENT_ID}" + "&redirect_uri=https://example.com/auth/external/callback" + f"&state={state}" + ) + + client = await hass_client_no_auth() + resp = await client.get(f"/auth/external/callback?code=abcd&state={state}") + assert resp.status == 200 + assert resp.headers["content-type"] == "text/html; charset=utf-8" + + aioclient_mock.clear_requests() + aioclient_mock.post( + OAUTH2_TOKEN, + json={ + "access_token": jwt, + "scope": "any", + "expires_in": 86399, + "refresh_token": "mock-refresh-token", + "user_id": "mock-user-id", + "expires_at": 1697753347, + }, + ) + + result2 = await hass.config_entries.flow.async_configure(result["flow_id"]) + assert result2["type"] is FlowResultType.ABORT + assert result2["reason"] == "already_configured" + + +@pytest.mark.usefixtures("client_credentials") +@pytest.mark.usefixtures("current_request_with_host") +async def test_reauth( + hass: HomeAssistant, + hass_client_no_auth: ClientSessionGenerator, + aioclient_mock: AiohttpClientMocker, + mock_config_entry: MockConfigEntry, + reauth_jwt: str, + mock_setup_entry: Mock, +) -> None: + """Test the reauthentication case updates the existing config entry.""" + + mock_config_entry.add_to_hass(hass) + + mock_config_entry.async_start_reauth(hass) + await hass.async_block_till_done() + + flows = hass.config_entries.flow.async_progress() + assert len(flows) == 1 + result = flows[0] + assert result["step_id"] == "auth" + + state = config_entry_oauth2_flow._encode_jwt( + hass, + { + "flow_id": result["flow_id"], + "redirect_uri": "https://example.com/auth/external/callback", + }, + ) + client = await hass_client_no_auth() + resp = await client.get(f"/auth/external/callback?code=abcd&state={state}") + assert resp.status == 200 + assert resp.headers["content-type"] == "text/html; charset=utf-8" + + aioclient_mock.post( + OAUTH2_TOKEN, + json={ + "access_token": reauth_jwt, + "expires_in": 86399, + "refresh_token": "mock-refresh-token", + "user_id": USER_ID, + "token_type": "Bearer", + "expires_at": 1697753347, + }, + ) + + result = await hass.config_entries.flow.async_configure(result["flow_id"]) + await hass.async_block_till_done() + + assert len(hass.config_entries.async_entries(DOMAIN)) == 1 + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "reauth_successful" + + assert mock_config_entry.unique_id == USER_ID + assert "token" in mock_config_entry.data + # Verify access token is refreshed + assert mock_config_entry.data["token"]["access_token"] == reauth_jwt + + +@pytest.mark.usefixtures("client_credentials") +@pytest.mark.usefixtures("current_request_with_host") +async def test_reauth_wrong_account( + hass: HomeAssistant, + hass_client_no_auth: ClientSessionGenerator, + aioclient_mock: AiohttpClientMocker, + mock_config_entry: MockConfigEntry, + reauth_jwt_wrong_account: str, + jwt: str, + mock_setup_entry: Mock, +) -> None: + """Test the reauthentication aborts, if user tries to reauthenticate with another account.""" + assert mock_config_entry.data["token"]["access_token"] == jwt + + mock_config_entry.add_to_hass(hass) + + mock_config_entry.async_start_reauth(hass) + await hass.async_block_till_done() + + flows = hass.config_entries.flow.async_progress() + assert len(flows) == 1 + result = flows[0] + assert result["step_id"] == "auth" + + state = config_entry_oauth2_flow._encode_jwt( + hass, + { + "flow_id": result["flow_id"], + "redirect_uri": "https://example.com/auth/external/callback", + }, + ) + client = await hass_client_no_auth() + resp = await client.get(f"/auth/external/callback?code=abcd&state={state}") + assert resp.status == 200 + assert resp.headers["content-type"] == "text/html; charset=utf-8" + + aioclient_mock.post( + OAUTH2_TOKEN, + json={ + "access_token": reauth_jwt_wrong_account, + "expires_in": 86399, + "refresh_token": "mock-refresh-token", + "token_type": "Bearer", + "expires_at": 1697753347, + }, + ) + + result = await hass.config_entries.flow.async_configure(result["flow_id"]) + await hass.async_block_till_done() + + assert len(hass.config_entries.async_entries(DOMAIN)) == 1 + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "reauth_invalid_user" + + assert mock_config_entry.unique_id == USER_ID + assert "token" in mock_config_entry.data + # Verify access token is like before + assert mock_config_entry.data["token"]["access_token"] == jwt diff --git a/tests/components/yale/test_diagnostics.py b/tests/components/yale/test_diagnostics.py new file mode 100644 index 00000000000000..e5fd6b1c1a7eea --- /dev/null +++ b/tests/components/yale/test_diagnostics.py @@ -0,0 +1,31 @@ +"""Test yale diagnostics.""" + +from syrupy import SnapshotAssertion + +from homeassistant.core import HomeAssistant + +from .mocks import ( + _create_yale_api_with_devices, + _mock_doorbell_from_fixture, + _mock_lock_from_fixture, +) + +from tests.components.diagnostics import get_diagnostics_for_config_entry +from tests.typing import ClientSessionGenerator + + +async def test_diagnostics( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + snapshot: SnapshotAssertion, +) -> None: + """Test generating diagnostics for a config entry.""" + lock_one = await _mock_lock_from_fixture( + hass, "get_lock.online_with_doorsense.json" + ) + doorbell_one = await _mock_doorbell_from_fixture(hass, "get_doorbell.json") + + entry, _, _ = await _create_yale_api_with_devices(hass, [lock_one, doorbell_one]) + diag = await get_diagnostics_for_config_entry(hass, hass_client, entry) + + assert diag == snapshot diff --git a/tests/components/yale/test_event.py b/tests/components/yale/test_event.py new file mode 100644 index 00000000000000..7aeb9d8f12b9e9 --- /dev/null +++ b/tests/components/yale/test_event.py @@ -0,0 +1,162 @@ +"""The event tests for the yale.""" + +from freezegun.api import FrozenDateTimeFactory + +from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN +from homeassistant.core import HomeAssistant +import homeassistant.util.dt as dt_util + +from .mocks import ( + _create_yale_with_devices, + _mock_activities_from_fixture, + _mock_doorbell_from_fixture, + _mock_lock_from_fixture, +) + +from tests.common import async_fire_time_changed + + +async def test_create_doorbell(hass: HomeAssistant) -> None: + """Test creation of a doorbell.""" + doorbell_one = await _mock_doorbell_from_fixture(hass, "get_doorbell.json") + await _create_yale_with_devices(hass, [doorbell_one]) + + motion_state = hass.states.get("event.k98gidt45gul_name_motion") + assert motion_state is not None + assert motion_state.state == STATE_UNKNOWN + doorbell_state = hass.states.get("event.k98gidt45gul_name_doorbell") + assert doorbell_state is not None + assert doorbell_state.state == STATE_UNKNOWN + + +async def test_create_doorbell_offline(hass: HomeAssistant) -> None: + """Test creation of a doorbell that is offline.""" + doorbell_one = await _mock_doorbell_from_fixture(hass, "get_doorbell.offline.json") + await _create_yale_with_devices(hass, [doorbell_one]) + motion_state = hass.states.get("event.tmt100_name_motion") + assert motion_state is not None + assert motion_state.state == STATE_UNAVAILABLE + doorbell_state = hass.states.get("event.tmt100_name_doorbell") + assert doorbell_state is not None + assert doorbell_state.state == STATE_UNAVAILABLE + + +async def test_create_doorbell_with_motion( + hass: HomeAssistant, freezer: FrozenDateTimeFactory +) -> None: + """Test creation of a doorbell.""" + doorbell_one = await _mock_doorbell_from_fixture(hass, "get_doorbell.json") + activities = await _mock_activities_from_fixture( + hass, "get_activity.doorbell_motion.json" + ) + await _create_yale_with_devices(hass, [doorbell_one], activities=activities) + + motion_state = hass.states.get("event.k98gidt45gul_name_motion") + assert motion_state is not None + assert motion_state.state != STATE_UNKNOWN + isotime = motion_state.state + doorbell_state = hass.states.get("event.k98gidt45gul_name_doorbell") + assert doorbell_state is not None + assert doorbell_state.state == STATE_UNKNOWN + + freezer.tick(40) + async_fire_time_changed(hass) + await hass.async_block_till_done() + motion_state = hass.states.get("event.k98gidt45gul_name_motion") + assert motion_state.state == isotime + + +async def test_doorbell_update_via_socketio( + hass: HomeAssistant, freezer: FrozenDateTimeFactory +) -> None: + """Test creation of a doorbell that can be updated via socketio.""" + doorbell_one = await _mock_doorbell_from_fixture(hass, "get_doorbell.json") + + _, socketio = await _create_yale_with_devices(hass, [doorbell_one]) + assert doorbell_one.pubsub_channel == "7c7a6672-59c8-3333-ffff-dcd98705cccc" + + motion_state = hass.states.get("event.k98gidt45gul_name_motion") + assert motion_state is not None + assert motion_state.state == STATE_UNKNOWN + doorbell_state = hass.states.get("event.k98gidt45gul_name_doorbell") + assert doorbell_state is not None + assert doorbell_state.state == STATE_UNKNOWN + + listener = list(socketio._listeners)[0] + listener( + doorbell_one.device_id, + dt_util.utcnow(), + { + "status": "doorbell_motion_detected", + "data": { + "event": "doorbell_motion_detected", + "image": { + "height": 640, + "width": 480, + "format": "jpg", + "created_at": "2021-03-16T02:36:26.886Z", + "bytes": 14061, + "secure_url": ( + "https://dyu7azbnaoi74.cloudfront.net/images/1f8.jpeg" + ), + "url": "https://dyu7azbnaoi74.cloudfront.net/images/1f8.jpeg", + "etag": "09e839331c4ea59eef28081f2caa0e90", + }, + "doorbellName": "Front Door", + "callID": None, + "origin": "mars-api", + "mutableContent": True, + }, + }, + ) + + await hass.async_block_till_done() + + motion_state = hass.states.get("event.k98gidt45gul_name_motion") + assert motion_state is not None + assert motion_state.state != STATE_UNKNOWN + isotime = motion_state.state + + freezer.tick(40) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + motion_state = hass.states.get("event.k98gidt45gul_name_motion") + assert motion_state is not None + assert motion_state.state != STATE_UNKNOWN + + listener( + doorbell_one.device_id, + dt_util.utcnow(), + { + "status": "buttonpush", + }, + ) + + await hass.async_block_till_done() + + doorbell_state = hass.states.get("event.k98gidt45gul_name_doorbell") + assert doorbell_state is not None + assert doorbell_state.state != STATE_UNKNOWN + isotime = motion_state.state + + freezer.tick(40) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + doorbell_state = hass.states.get("event.k98gidt45gul_name_doorbell") + assert doorbell_state is not None + assert doorbell_state.state != STATE_UNKNOWN + assert motion_state.state == isotime + + +async def test_create_lock_with_doorbell(hass: HomeAssistant) -> None: + """Test creation of a lock with a doorbell.""" + lock_one = await _mock_lock_from_fixture(hass, "lock_with_doorbell.online.json") + await _create_yale_with_devices(hass, [lock_one]) + + doorbell_state = hass.states.get( + "event.a6697750d607098bae8d6baa11ef8063_name_doorbell" + ) + assert doorbell_state is not None + assert doorbell_state.state == STATE_UNKNOWN diff --git a/tests/components/yale/test_init.py b/tests/components/yale/test_init.py new file mode 100644 index 00000000000000..4f0a853710cdca --- /dev/null +++ b/tests/components/yale/test_init.py @@ -0,0 +1,237 @@ +"""The tests for the yale platform.""" + +from unittest.mock import Mock + +from aiohttp import ClientResponseError +import pytest +from yalexs.exceptions import InvalidAuth, YaleApiError + +from homeassistant.components.lock import DOMAIN as LOCK_DOMAIN +from homeassistant.components.yale.const import DOMAIN +from homeassistant.config_entries import ConfigEntryState +from homeassistant.const import ( + ATTR_ENTITY_ID, + SERVICE_LOCK, + SERVICE_OPEN, + SERVICE_UNLOCK, + STATE_LOCKED, + STATE_ON, +) +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError +from homeassistant.helpers import device_registry as dr, entity_registry as er +from homeassistant.setup import async_setup_component + +from .mocks import ( + _create_yale_with_devices, + _mock_doorsense_enabled_yale_lock_detail, + _mock_doorsense_missing_yale_lock_detail, + _mock_inoperative_yale_lock_detail, + _mock_lock_with_offline_key, + _mock_operative_yale_lock_detail, +) + +from tests.typing import WebSocketGenerator + + +async def test_yale_api_is_failing(hass: HomeAssistant) -> None: + """Config entry state is SETUP_RETRY when yale api is failing.""" + + config_entry, socketio = await _create_yale_with_devices( + hass, + authenticate_side_effect=YaleApiError( + "offline", ClientResponseError(None, None, status=500) + ), + ) + assert config_entry.state is ConfigEntryState.SETUP_RETRY + + +async def test_yale_is_offline(hass: HomeAssistant) -> None: + """Config entry state is SETUP_RETRY when yale is offline.""" + + config_entry, socketio = await _create_yale_with_devices( + hass, authenticate_side_effect=TimeoutError + ) + + assert config_entry.state is ConfigEntryState.SETUP_RETRY + + +async def test_yale_late_auth_failure(hass: HomeAssistant) -> None: + """Test we can detect a late auth failure.""" + config_entry, socketio = await _create_yale_with_devices( + hass, + authenticate_side_effect=InvalidAuth( + "authfailed", ClientResponseError(None, None, status=401) + ), + ) + + assert config_entry.state is ConfigEntryState.SETUP_ERROR + flows = hass.config_entries.flow.async_progress() + + assert flows[0]["step_id"] == "pick_implementation" + + +async def test_unlock_throws_yale_api_http_error(hass: HomeAssistant) -> None: + """Test unlock throws correct error on http error.""" + mocked_lock_detail = await _mock_operative_yale_lock_detail(hass) + aiohttp_client_response_exception = ClientResponseError(None, None, status=400) + + def _unlock_return_activities_side_effect(access_token, device_id): + raise YaleApiError( + "This should bubble up as its user consumable", + aiohttp_client_response_exception, + ) + + await _create_yale_with_devices( + hass, + [mocked_lock_detail], + api_call_side_effects={ + "unlock_return_activities": _unlock_return_activities_side_effect + }, + ) + data = {ATTR_ENTITY_ID: "lock.a6697750d607098bae8d6baa11ef8063_name"} + with pytest.raises( + HomeAssistantError, + match=( + "A6697750D607098BAE8D6BAA11EF8063 Name: This should bubble up as its user" + " consumable" + ), + ): + await hass.services.async_call(LOCK_DOMAIN, SERVICE_UNLOCK, data, blocking=True) + + +async def test_lock_throws_yale_api_http_error(hass: HomeAssistant) -> None: + """Test lock throws correct error on http error.""" + mocked_lock_detail = await _mock_operative_yale_lock_detail(hass) + aiohttp_client_response_exception = ClientResponseError(None, None, status=400) + + def _lock_return_activities_side_effect(access_token, device_id): + raise YaleApiError( + "This should bubble up as its user consumable", + aiohttp_client_response_exception, + ) + + await _create_yale_with_devices( + hass, + [mocked_lock_detail], + api_call_side_effects={ + "lock_return_activities": _lock_return_activities_side_effect + }, + ) + data = {ATTR_ENTITY_ID: "lock.a6697750d607098bae8d6baa11ef8063_name"} + with pytest.raises( + HomeAssistantError, + match=( + "A6697750D607098BAE8D6BAA11EF8063 Name: This should bubble up as its user" + " consumable" + ), + ): + await hass.services.async_call(LOCK_DOMAIN, SERVICE_LOCK, data, blocking=True) + + +async def test_open_throws_hass_service_not_supported_error( + hass: HomeAssistant, +) -> None: + """Test open throws correct error on entity does not support this service error.""" + mocked_lock_detail = await _mock_operative_yale_lock_detail(hass) + await _create_yale_with_devices(hass, [mocked_lock_detail]) + data = {ATTR_ENTITY_ID: "lock.a6697750d607098bae8d6baa11ef8063_name"} + with pytest.raises(HomeAssistantError): + await hass.services.async_call(LOCK_DOMAIN, SERVICE_OPEN, data, blocking=True) + + +async def test_inoperative_locks_are_filtered_out(hass: HomeAssistant) -> None: + """Ensure inoperative locks do not get setup.""" + yale_operative_lock = await _mock_operative_yale_lock_detail(hass) + yale_inoperative_lock = await _mock_inoperative_yale_lock_detail(hass) + await _create_yale_with_devices(hass, [yale_operative_lock, yale_inoperative_lock]) + + lock_abc_name = hass.states.get("lock.abc_name") + assert lock_abc_name is None + lock_a6697750d607098bae8d6baa11ef8063_name = hass.states.get( + "lock.a6697750d607098bae8d6baa11ef8063_name" + ) + assert lock_a6697750d607098bae8d6baa11ef8063_name.state == STATE_LOCKED + + +async def test_lock_has_doorsense(hass: HomeAssistant) -> None: + """Check to see if a lock has doorsense.""" + doorsenselock = await _mock_doorsense_enabled_yale_lock_detail(hass) + nodoorsenselock = await _mock_doorsense_missing_yale_lock_detail(hass) + await _create_yale_with_devices(hass, [doorsenselock, nodoorsenselock]) + + binary_sensor_online_with_doorsense_name_open = hass.states.get( + "binary_sensor.online_with_doorsense_name_door" + ) + assert binary_sensor_online_with_doorsense_name_open.state == STATE_ON + binary_sensor_missing_doorsense_id_name_open = hass.states.get( + "binary_sensor.missing_with_doorsense_name_door" + ) + assert binary_sensor_missing_doorsense_id_name_open is None + + +async def test_load_unload(hass: HomeAssistant) -> None: + """Config entry can be unloaded.""" + + yale_operative_lock = await _mock_operative_yale_lock_detail(hass) + yale_inoperative_lock = await _mock_inoperative_yale_lock_detail(hass) + config_entry, socketio = await _create_yale_with_devices( + hass, [yale_operative_lock, yale_inoperative_lock] + ) + + assert config_entry.state is ConfigEntryState.LOADED + + await hass.config_entries.async_unload(config_entry.entry_id) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.NOT_LOADED + + +async def test_load_triggers_ble_discovery( + hass: HomeAssistant, mock_discovery: Mock +) -> None: + """Test that loading a lock that supports offline ble operation passes the keys to yalexe_ble.""" + + yale_lock_with_key = await _mock_lock_with_offline_key(hass) + yale_lock_without_key = await _mock_operative_yale_lock_detail(hass) + + config_entry, socketio = await _create_yale_with_devices( + hass, [yale_lock_with_key, yale_lock_without_key] + ) + await hass.async_block_till_done() + assert config_entry.state is ConfigEntryState.LOADED + + assert len(mock_discovery.mock_calls) == 1 + assert mock_discovery.mock_calls[0].kwargs["data"] == { + "name": "Front Door Lock", + "address": None, + "serial": "X2FSW05DGA", + "key": "kkk01d4300c1dcxxx1c330f794941111", + "slot": 1, + } + + +async def test_device_remove_devices( + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, + device_registry: dr.DeviceRegistry, + entity_registry: er.EntityRegistry, +) -> None: + """Test we can only remove a device that no longer exists.""" + assert await async_setup_component(hass, "config", {}) + yale_operative_lock = await _mock_operative_yale_lock_detail(hass) + config_entry, socketio = await _create_yale_with_devices( + hass, [yale_operative_lock] + ) + entity = entity_registry.entities["lock.a6697750d607098bae8d6baa11ef8063_name"] + + device_entry = device_registry.async_get(entity.device_id) + client = await hass_ws_client(hass) + response = await client.remove_device(device_entry.id, config_entry.entry_id) + assert not response["success"] + + dead_device_entry = device_registry.async_get_or_create( + config_entry_id=config_entry.entry_id, + identifiers={(DOMAIN, "remove-device-id")}, + ) + response = await client.remove_device(dead_device_entry.id, config_entry.entry_id) + assert response["success"] diff --git a/tests/components/yale/test_lock.py b/tests/components/yale/test_lock.py new file mode 100644 index 00000000000000..2bbb7408953366 --- /dev/null +++ b/tests/components/yale/test_lock.py @@ -0,0 +1,432 @@ +"""The lock tests for the yale platform.""" + +import datetime + +from aiohttp import ClientResponseError +from freezegun.api import FrozenDateTimeFactory +import pytest +from syrupy import SnapshotAssertion +from yalexs.manager.activity import INITIAL_LOCK_RESYNC_TIME + +from homeassistant.components.lock import ( + DOMAIN as LOCK_DOMAIN, + STATE_JAMMED, + STATE_LOCKING, + STATE_UNLOCKING, +) +from homeassistant.const import ( + ATTR_ENTITY_ID, + SERVICE_LOCK, + SERVICE_OPEN, + SERVICE_UNLOCK, + STATE_LOCKED, + STATE_UNAVAILABLE, + STATE_UNKNOWN, + STATE_UNLOCKED, +) +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError +from homeassistant.helpers import device_registry as dr, entity_registry as er +import homeassistant.util.dt as dt_util + +from .mocks import ( + _create_yale_with_devices, + _mock_activities_from_fixture, + _mock_doorsense_enabled_yale_lock_detail, + _mock_lock_from_fixture, + _mock_lock_with_unlatch, + _mock_operative_yale_lock_detail, +) + +from tests.common import async_fire_time_changed + + +async def test_lock_device_registry( + hass: HomeAssistant, device_registry: dr.DeviceRegistry, snapshot: SnapshotAssertion +) -> None: + """Test creation of a lock with doorsense and bridge ands up in the registry.""" + lock_one = await _mock_doorsense_enabled_yale_lock_detail(hass) + await _create_yale_with_devices(hass, [lock_one]) + + reg_device = device_registry.async_get_device( + identifiers={("yale", "online_with_doorsense")} + ) + assert reg_device == snapshot + + +async def test_lock_changed_by(hass: HomeAssistant) -> None: + """Test creation of a lock with doorsense and bridge.""" + lock_one = await _mock_doorsense_enabled_yale_lock_detail(hass) + + activities = await _mock_activities_from_fixture(hass, "get_activity.lock.json") + await _create_yale_with_devices(hass, [lock_one], activities=activities) + + lock_state = hass.states.get("lock.online_with_doorsense_name") + assert lock_state.state == STATE_LOCKED + assert lock_state.attributes["changed_by"] == "Your favorite elven princess" + + +async def test_state_locking(hass: HomeAssistant) -> None: + """Test creation of a lock with doorsense and bridge that is locking.""" + lock_one = await _mock_doorsense_enabled_yale_lock_detail(hass) + + activities = await _mock_activities_from_fixture(hass, "get_activity.locking.json") + await _create_yale_with_devices(hass, [lock_one], activities=activities) + + assert hass.states.get("lock.online_with_doorsense_name").state == STATE_LOCKING + + +async def test_state_unlocking(hass: HomeAssistant) -> None: + """Test creation of a lock with doorsense and bridge that is unlocking.""" + lock_one = await _mock_doorsense_enabled_yale_lock_detail(hass) + + activities = await _mock_activities_from_fixture( + hass, "get_activity.unlocking.json" + ) + await _create_yale_with_devices(hass, [lock_one], activities=activities) + + lock_online_with_doorsense_name = hass.states.get("lock.online_with_doorsense_name") + + assert lock_online_with_doorsense_name.state == STATE_UNLOCKING + + +async def test_state_jammed(hass: HomeAssistant) -> None: + """Test creation of a lock with doorsense and bridge that is jammed.""" + lock_one = await _mock_doorsense_enabled_yale_lock_detail(hass) + + activities = await _mock_activities_from_fixture(hass, "get_activity.jammed.json") + await _create_yale_with_devices(hass, [lock_one], activities=activities) + + assert hass.states.get("lock.online_with_doorsense_name").state == STATE_JAMMED + + +async def test_one_lock_operation( + hass: HomeAssistant, entity_registry: er.EntityRegistry +) -> None: + """Test creation of a lock with doorsense and bridge.""" + lock_one = await _mock_doorsense_enabled_yale_lock_detail(hass) + await _create_yale_with_devices(hass, [lock_one]) + + lock_state = hass.states.get("lock.online_with_doorsense_name") + + assert lock_state.state == STATE_LOCKED + + assert lock_state.attributes["battery_level"] == 92 + assert lock_state.attributes["friendly_name"] == "online_with_doorsense Name" + + data = {ATTR_ENTITY_ID: "lock.online_with_doorsense_name"} + await hass.services.async_call(LOCK_DOMAIN, SERVICE_UNLOCK, data, blocking=True) + + lock_state = hass.states.get("lock.online_with_doorsense_name") + assert lock_state.state == STATE_UNLOCKED + + assert lock_state.attributes["battery_level"] == 92 + assert lock_state.attributes["friendly_name"] == "online_with_doorsense Name" + + await hass.services.async_call(LOCK_DOMAIN, SERVICE_LOCK, data, blocking=True) + + lock_state = hass.states.get("lock.online_with_doorsense_name") + assert lock_state.state == STATE_LOCKED + + # No activity means it will be unavailable until the activity feed has data + assert entity_registry.async_get("sensor.online_with_doorsense_name_operator") + operator_state = hass.states.get("sensor.online_with_doorsense_name_operator") + assert operator_state.state == STATE_UNKNOWN + + +async def test_open_lock_operation(hass: HomeAssistant) -> None: + """Test open lock operation using the open service.""" + lock_with_unlatch = await _mock_lock_with_unlatch(hass) + await _create_yale_with_devices(hass, [lock_with_unlatch]) + + assert hass.states.get("lock.online_with_unlatch_name").state == STATE_LOCKED + + data = {ATTR_ENTITY_ID: "lock.online_with_unlatch_name"} + await hass.services.async_call(LOCK_DOMAIN, SERVICE_OPEN, data, blocking=True) + + assert hass.states.get("lock.online_with_unlatch_name").state == STATE_UNLOCKED + + +async def test_open_lock_operation_socketio_connected( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + freezer: FrozenDateTimeFactory, +) -> None: + """Test open lock operation using the open service when socketio is connected.""" + lock_with_unlatch = await _mock_lock_with_unlatch(hass) + assert lock_with_unlatch.pubsub_channel == "pubsub" + + _, socketio = await _create_yale_with_devices(hass, [lock_with_unlatch]) + socketio.connected = True + + assert hass.states.get("lock.online_with_unlatch_name").state == STATE_LOCKED + + data = {ATTR_ENTITY_ID: "lock.online_with_unlatch_name"} + await hass.services.async_call(LOCK_DOMAIN, SERVICE_OPEN, data, blocking=True) + + listener = list(socketio._listeners)[0] + listener( + lock_with_unlatch.device_id, + dt_util.utcnow() + datetime.timedelta(seconds=2), + { + "status": "kAugLockState_Unlocked", + }, + ) + + await hass.async_block_till_done() + await hass.async_block_till_done() + + assert hass.states.get("lock.online_with_unlatch_name").state == STATE_UNLOCKED + await hass.async_block_till_done() + + +async def test_one_lock_operation_socketio_connected( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + freezer: FrozenDateTimeFactory, +) -> None: + """Test lock and unlock operations are async when socketio is connected.""" + lock_one = await _mock_doorsense_enabled_yale_lock_detail(hass) + assert lock_one.pubsub_channel == "pubsub" + states = hass.states + + _, socketio = await _create_yale_with_devices(hass, [lock_one]) + socketio.connected = True + + lock_state = hass.states.get("lock.online_with_doorsense_name") + assert lock_state.state == STATE_LOCKED + assert lock_state.attributes["battery_level"] == 92 + assert lock_state.attributes["friendly_name"] == "online_with_doorsense Name" + + data = {ATTR_ENTITY_ID: "lock.online_with_doorsense_name"} + await hass.services.async_call(LOCK_DOMAIN, SERVICE_UNLOCK, data, blocking=True) + + listener = list(socketio._listeners)[0] + listener( + lock_one.device_id, + dt_util.utcnow() + datetime.timedelta(seconds=1), + { + "status": "kAugLockState_Unlocked", + }, + ) + + await hass.async_block_till_done() + await hass.async_block_till_done() + + lock_state = states.get("lock.online_with_doorsense_name") + assert lock_state.state == STATE_UNLOCKED + assert lock_state.attributes["battery_level"] == 92 + assert lock_state.attributes["friendly_name"] == "online_with_doorsense Name" + + await hass.services.async_call(LOCK_DOMAIN, SERVICE_LOCK, data, blocking=True) + + listener( + lock_one.device_id, + dt_util.utcnow() + datetime.timedelta(seconds=2), + { + "status": "kAugLockState_Locked", + }, + ) + + await hass.async_block_till_done() + await hass.async_block_till_done() + + assert states.get("lock.online_with_doorsense_name").state == STATE_LOCKED + + # No activity means it will be unavailable until the activity feed has data + assert entity_registry.async_get("sensor.online_with_doorsense_name_operator") + assert ( + states.get("sensor.online_with_doorsense_name_operator").state == STATE_UNKNOWN + ) + + freezer.tick(INITIAL_LOCK_RESYNC_TIME) + + listener( + lock_one.device_id, + dt_util.utcnow() + datetime.timedelta(seconds=2), + { + "status": "kAugLockState_Unlocked", + }, + ) + + await hass.async_block_till_done() + + assert states.get("lock.online_with_doorsense_name").state == STATE_UNLOCKED + + +async def test_lock_jammed(hass: HomeAssistant) -> None: + """Test lock gets jammed on unlock.""" + + def _unlock_return_activities_side_effect(access_token, device_id): + raise ClientResponseError(None, None, status=531) + + lock_one = await _mock_doorsense_enabled_yale_lock_detail(hass) + await _create_yale_with_devices( + hass, + [lock_one], + api_call_side_effects={ + "unlock_return_activities": _unlock_return_activities_side_effect + }, + ) + + states = hass.states + lock_state = states.get("lock.online_with_doorsense_name") + assert lock_state.state == STATE_LOCKED + assert lock_state.attributes["battery_level"] == 92 + assert lock_state.attributes["friendly_name"] == "online_with_doorsense Name" + + data = {ATTR_ENTITY_ID: "lock.online_with_doorsense_name"} + await hass.services.async_call(LOCK_DOMAIN, SERVICE_UNLOCK, data, blocking=True) + + assert states.get("lock.online_with_doorsense_name").state == STATE_JAMMED + + +async def test_lock_throws_exception_on_unknown_status_code( + hass: HomeAssistant, +) -> None: + """Test lock throws exception.""" + + def _unlock_return_activities_side_effect(access_token, device_id): + raise ClientResponseError(None, None, status=500) + + lock_one = await _mock_doorsense_enabled_yale_lock_detail(hass) + await _create_yale_with_devices( + hass, + [lock_one], + api_call_side_effects={ + "unlock_return_activities": _unlock_return_activities_side_effect + }, + ) + + lock_state = hass.states.get("lock.online_with_doorsense_name") + assert lock_state.state == STATE_LOCKED + assert lock_state.attributes["battery_level"] == 92 + assert lock_state.attributes["friendly_name"] == "online_with_doorsense Name" + + data = {ATTR_ENTITY_ID: "lock.online_with_doorsense_name"} + with pytest.raises(ClientResponseError): + await hass.services.async_call(LOCK_DOMAIN, SERVICE_UNLOCK, data, blocking=True) + + +async def test_one_lock_unknown_state(hass: HomeAssistant) -> None: + """Test creation of a lock with doorsense and bridge.""" + lock_one = await _mock_lock_from_fixture( + hass, + "get_lock.online.unknown_state.json", + ) + await _create_yale_with_devices(hass, [lock_one]) + + assert hass.states.get("lock.brokenid_name").state == STATE_UNKNOWN + + +async def test_lock_bridge_offline(hass: HomeAssistant) -> None: + """Test creation of a lock with doorsense and bridge that goes offline.""" + lock_one = await _mock_doorsense_enabled_yale_lock_detail(hass) + + activities = await _mock_activities_from_fixture( + hass, "get_activity.bridge_offline.json" + ) + await _create_yale_with_devices(hass, [lock_one], activities=activities) + + states = hass.states + assert states.get("lock.online_with_doorsense_name").state == STATE_UNAVAILABLE + + +async def test_lock_bridge_online(hass: HomeAssistant) -> None: + """Test creation of a lock with doorsense and bridge that goes offline.""" + lock_one = await _mock_doorsense_enabled_yale_lock_detail(hass) + + activities = await _mock_activities_from_fixture( + hass, "get_activity.bridge_online.json" + ) + await _create_yale_with_devices(hass, [lock_one], activities=activities) + + states = hass.states + assert states.get("lock.online_with_doorsense_name").state == STATE_LOCKED + + +async def test_lock_update_via_socketio(hass: HomeAssistant) -> None: + """Test creation of a lock with doorsense and bridge.""" + lock_one = await _mock_doorsense_enabled_yale_lock_detail(hass) + assert lock_one.pubsub_channel == "pubsub" + + activities = await _mock_activities_from_fixture(hass, "get_activity.lock.json") + config_entry, socketio = await _create_yale_with_devices( + hass, [lock_one], activities=activities + ) + socketio.connected = True + states = hass.states + + assert states.get("lock.online_with_doorsense_name").state == STATE_LOCKED + + listener = list(socketio._listeners)[0] + listener( + lock_one.device_id, + dt_util.utcnow(), + { + "status": "kAugLockState_Unlocking", + }, + ) + + await hass.async_block_till_done() + await hass.async_block_till_done() + + assert states.get("lock.online_with_doorsense_name").state == STATE_UNLOCKING + + listener( + lock_one.device_id, + dt_util.utcnow(), + { + "status": "kAugLockState_Locking", + }, + ) + + await hass.async_block_till_done() + await hass.async_block_till_done() + + assert states.get("lock.online_with_doorsense_name").state == STATE_LOCKING + + async_fire_time_changed(hass, dt_util.utcnow() + datetime.timedelta(seconds=30)) + await hass.async_block_till_done() + assert states.get("lock.online_with_doorsense_name").state == STATE_LOCKING + + socketio.connected = True + async_fire_time_changed(hass, dt_util.utcnow() + datetime.timedelta(seconds=30)) + await hass.async_block_till_done() + assert states.get("lock.online_with_doorsense_name").state == STATE_LOCKING + + # Ensure socketio status is always preserved + async_fire_time_changed(hass, dt_util.utcnow() + datetime.timedelta(hours=2)) + await hass.async_block_till_done() + assert states.get("lock.online_with_doorsense_name").state == STATE_LOCKING + + listener( + lock_one.device_id, + dt_util.utcnow() + datetime.timedelta(seconds=2), + { + "status": "kAugLockState_Unlocking", + }, + ) + + await hass.async_block_till_done() + await hass.async_block_till_done() + + assert states.get("lock.online_with_doorsense_name").state == STATE_UNLOCKING + + async_fire_time_changed(hass, dt_util.utcnow() + datetime.timedelta(hours=4)) + await hass.async_block_till_done() + assert states.get("lock.online_with_doorsense_name").state == STATE_UNLOCKING + + await hass.config_entries.async_unload(config_entry.entry_id) + await hass.async_block_till_done() + + +async def test_open_throws_hass_service_not_supported_error( + hass: HomeAssistant, +) -> None: + """Test open throws correct error on entity does not support this service error.""" + mocked_lock_detail = await _mock_operative_yale_lock_detail(hass) + await _create_yale_with_devices(hass, [mocked_lock_detail]) + data = {ATTR_ENTITY_ID: "lock.a6697750d607098bae8d6baa11ef8063_name"} + with pytest.raises(HomeAssistantError, match="does not support this service"): + await hass.services.async_call(LOCK_DOMAIN, SERVICE_OPEN, data, blocking=True) diff --git a/tests/components/yale/test_sensor.py b/tests/components/yale/test_sensor.py new file mode 100644 index 00000000000000..5d724b4bb9d38a --- /dev/null +++ b/tests/components/yale/test_sensor.py @@ -0,0 +1,320 @@ +"""The sensor tests for the yale platform.""" + +from typing import Any + +from syrupy import SnapshotAssertion + +from homeassistant import core as ha +from homeassistant.const import ( + ATTR_ENTITY_PICTURE, + ATTR_UNIT_OF_MEASUREMENT, + PERCENTAGE, + STATE_UNKNOWN, +) +from homeassistant.core import CoreState, HomeAssistant +from homeassistant.helpers import entity_registry as er + +from .mocks import ( + _create_yale_with_devices, + _mock_activities_from_fixture, + _mock_doorbell_from_fixture, + _mock_doorsense_enabled_yale_lock_detail, + _mock_lock_from_fixture, +) + +from tests.common import mock_restore_cache_with_extra_data + + +async def test_create_doorbell(hass: HomeAssistant) -> None: + """Test creation of a doorbell.""" + doorbell_one = await _mock_doorbell_from_fixture(hass, "get_doorbell.json") + await _create_yale_with_devices(hass, [doorbell_one]) + + battery_state = hass.states.get("sensor.k98gidt45gul_name_battery") + assert battery_state.state == "96" + assert battery_state.attributes["unit_of_measurement"] == PERCENTAGE + + +async def test_create_doorbell_offline( + hass: HomeAssistant, entity_registry: er.EntityRegistry +) -> None: + """Test creation of a doorbell that is offline.""" + doorbell_one = await _mock_doorbell_from_fixture(hass, "get_doorbell.offline.json") + await _create_yale_with_devices(hass, [doorbell_one]) + + battery_state = hass.states.get("sensor.tmt100_name_battery") + assert battery_state.state == "81" + assert battery_state.attributes["unit_of_measurement"] == PERCENTAGE + + entry = entity_registry.async_get("sensor.tmt100_name_battery") + assert entry + assert entry.unique_id == "tmt100_device_battery" + + +async def test_create_doorbell_hardwired(hass: HomeAssistant) -> None: + """Test creation of a doorbell that is hardwired without a battery.""" + doorbell_one = await _mock_doorbell_from_fixture( + hass, "get_doorbell.nobattery.json" + ) + await _create_yale_with_devices(hass, [doorbell_one]) + + sensor_tmt100_name_battery = hass.states.get("sensor.tmt100_name_battery") + assert sensor_tmt100_name_battery is None + + +async def test_create_lock_with_linked_keypad( + hass: HomeAssistant, entity_registry: er.EntityRegistry +) -> None: + """Test creation of a lock with a linked keypad that both have a battery.""" + lock_one = await _mock_lock_from_fixture(hass, "get_lock.doorsense_init.json") + await _create_yale_with_devices(hass, [lock_one]) + + battery_state = hass.states.get( + "sensor.a6697750d607098bae8d6baa11ef8063_name_battery" + ) + assert battery_state.state == "88" + assert battery_state.attributes["unit_of_measurement"] == PERCENTAGE + + entry = entity_registry.async_get( + "sensor.a6697750d607098bae8d6baa11ef8063_name_battery" + ) + assert entry + assert entry.unique_id == "A6697750D607098BAE8D6BAA11EF8063_device_battery" + + keypad_battery_state = hass.states.get("sensor.front_door_lock_keypad_battery") + assert keypad_battery_state.state == "62" + assert keypad_battery_state.attributes[ATTR_UNIT_OF_MEASUREMENT] == PERCENTAGE + entry = entity_registry.async_get("sensor.front_door_lock_keypad_battery") + assert entry + assert entry.unique_id == "5bc65c24e6ef2a263e1450a8_linked_keypad_battery" + + +async def test_create_lock_with_low_battery_linked_keypad( + hass: HomeAssistant, entity_registry: er.EntityRegistry +) -> None: + """Test creation of a lock with a linked keypad that both have a battery.""" + lock_one = await _mock_lock_from_fixture(hass, "get_lock.low_keypad_battery.json") + await _create_yale_with_devices(hass, [lock_one]) + + battery_state = hass.states.get( + "sensor.a6697750d607098bae8d6baa11ef8063_name_battery" + ) + assert battery_state.state == "88" + assert battery_state.attributes["unit_of_measurement"] == PERCENTAGE + entry = entity_registry.async_get( + "sensor.a6697750d607098bae8d6baa11ef8063_name_battery" + ) + assert entry + assert entry.unique_id == "A6697750D607098BAE8D6BAA11EF8063_device_battery" + + state = hass.states.get("sensor.front_door_lock_keypad_battery") + assert state.state == "10" + assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == PERCENTAGE + entry = entity_registry.async_get("sensor.front_door_lock_keypad_battery") + assert entry + assert entry.unique_id == "5bc65c24e6ef2a263e1450a8_linked_keypad_battery" + + # No activity means it will be unavailable until someone unlocks/locks it + lock_operator_sensor = entity_registry.async_get( + "sensor.a6697750d607098bae8d6baa11ef8063_name_operator" + ) + assert ( + lock_operator_sensor.unique_id + == "A6697750D607098BAE8D6BAA11EF8063_lock_operator" + ) + assert ( + hass.states.get("sensor.a6697750d607098bae8d6baa11ef8063_name_operator").state + == STATE_UNKNOWN + ) + + +async def test_lock_operator_bluetooth( + hass: HomeAssistant, entity_registry: er.EntityRegistry +) -> None: + """Test operation of a lock with doorsense and bridge.""" + lock_one = await _mock_doorsense_enabled_yale_lock_detail(hass) + + activities = await _mock_activities_from_fixture( + hass, "get_activity.lock_from_bluetooth.json" + ) + await _create_yale_with_devices(hass, [lock_one], activities=activities) + + lock_operator_sensor = entity_registry.async_get( + "sensor.online_with_doorsense_name_operator" + ) + assert lock_operator_sensor + + state = hass.states.get("sensor.online_with_doorsense_name_operator") + assert state.state == "Your favorite elven princess" + assert state.attributes["manual"] is False + assert state.attributes["tag"] is False + assert state.attributes["remote"] is False + assert state.attributes["keypad"] is False + assert state.attributes["autorelock"] is False + assert state.attributes["method"] == "mobile" + + +async def test_lock_operator_keypad( + hass: HomeAssistant, entity_registry: er.EntityRegistry, snapshot: SnapshotAssertion +) -> None: + """Test operation of a lock with doorsense and bridge.""" + lock_one = await _mock_doorsense_enabled_yale_lock_detail(hass) + + activities = await _mock_activities_from_fixture( + hass, "get_activity.lock_from_keypad.json" + ) + await _create_yale_with_devices(hass, [lock_one], activities=activities) + + lock_operator_sensor = entity_registry.async_get( + "sensor.online_with_doorsense_name_operator" + ) + assert lock_operator_sensor + + state = hass.states.get("sensor.online_with_doorsense_name_operator") + assert state.state == "Your favorite elven princess" + assert state.attributes == snapshot + + +async def test_lock_operator_remote( + hass: HomeAssistant, entity_registry: er.EntityRegistry, snapshot: SnapshotAssertion +) -> None: + """Test operation of a lock with doorsense and bridge.""" + lock_one = await _mock_doorsense_enabled_yale_lock_detail(hass) + + activities = await _mock_activities_from_fixture(hass, "get_activity.lock.json") + await _create_yale_with_devices(hass, [lock_one], activities=activities) + + lock_operator_sensor = entity_registry.async_get( + "sensor.online_with_doorsense_name_operator" + ) + assert lock_operator_sensor + + state = hass.states.get("sensor.online_with_doorsense_name_operator") + assert state.state == "Your favorite elven princess" + assert state.attributes == snapshot + + +async def test_lock_operator_manual( + hass: HomeAssistant, entity_registry: er.EntityRegistry, snapshot: SnapshotAssertion +) -> None: + """Test operation of a lock with doorsense and bridge.""" + lock_one = await _mock_doorsense_enabled_yale_lock_detail(hass) + + activities = await _mock_activities_from_fixture( + hass, "get_activity.lock_from_manual.json" + ) + await _create_yale_with_devices(hass, [lock_one], activities=activities) + + lock_operator_sensor = entity_registry.async_get( + "sensor.online_with_doorsense_name_operator" + ) + assert lock_operator_sensor + state = hass.states.get("sensor.online_with_doorsense_name_operator") + assert state.state == "Your favorite elven princess" + assert state.attributes == snapshot + + +async def test_lock_operator_autorelock( + hass: HomeAssistant, entity_registry: er.EntityRegistry, snapshot: SnapshotAssertion +) -> None: + """Test operation of a lock with doorsense and bridge.""" + lock_one = await _mock_doorsense_enabled_yale_lock_detail(hass) + + activities = await _mock_activities_from_fixture( + hass, "get_activity.lock_from_autorelock.json" + ) + await _create_yale_with_devices(hass, [lock_one], activities=activities) + + lock_operator_sensor = entity_registry.async_get( + "sensor.online_with_doorsense_name_operator" + ) + assert lock_operator_sensor + + state = hass.states.get("sensor.online_with_doorsense_name_operator") + assert state.state == "Auto Relock" + assert state.attributes == snapshot + + +async def test_unlock_operator_manual( + hass: HomeAssistant, entity_registry: er.EntityRegistry, snapshot: SnapshotAssertion +) -> None: + """Test operation of a lock manually.""" + lock_one = await _mock_doorsense_enabled_yale_lock_detail(hass) + + activities = await _mock_activities_from_fixture( + hass, "get_activity.unlock_from_manual.json" + ) + await _create_yale_with_devices(hass, [lock_one], activities=activities) + + lock_operator_sensor = entity_registry.async_get( + "sensor.online_with_doorsense_name_operator" + ) + assert lock_operator_sensor + + state = hass.states.get("sensor.online_with_doorsense_name_operator") + assert state.state == "Your favorite elven princess" + assert state == snapshot + + +async def test_unlock_operator_tag( + hass: HomeAssistant, entity_registry: er.EntityRegistry, snapshot: SnapshotAssertion +) -> None: + """Test operation of a lock with a tag.""" + lock_one = await _mock_doorsense_enabled_yale_lock_detail(hass) + + activities = await _mock_activities_from_fixture( + hass, "get_activity.unlock_from_tag.json" + ) + await _create_yale_with_devices(hass, [lock_one], activities=activities) + + lock_operator_sensor = entity_registry.async_get( + "sensor.online_with_doorsense_name_operator" + ) + assert lock_operator_sensor + + state = hass.states.get("sensor.online_with_doorsense_name_operator") + assert state.state == "Your favorite elven princess" + assert state.attributes == snapshot + + +async def test_restored_state( + hass: HomeAssistant, hass_storage: dict[str, Any], snapshot: SnapshotAssertion +) -> None: + """Test restored state.""" + + entity_id = "sensor.online_with_doorsense_name_operator" + lock_one = await _mock_doorsense_enabled_yale_lock_detail(hass) + + fake_state = ha.State( + entity_id, + state="Tag Unlock", + attributes={ + "method": "tag", + "manual": False, + "remote": False, + "keypad": False, + "tag": True, + "autorelock": False, + ATTR_ENTITY_PICTURE: "image.png", + }, + ) + + # Home assistant is not running yet + hass.set_state(CoreState.not_running) + mock_restore_cache_with_extra_data( + hass, + [ + ( + fake_state, + {"native_value": "Tag Unlock", "native_unit_of_measurement": None}, + ) + ], + ) + + await _create_yale_with_devices(hass, [lock_one]) + + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert state.state == "Tag Unlock" + assert state == snapshot diff --git a/tests/components/yale_smart_alarm/test_config_flow.py b/tests/components/yale_smart_alarm/test_config_flow.py index 4ef201d2122044..d5651503768583 100644 --- a/tests/components/yale_smart_alarm/test_config_flow.py +++ b/tests/components/yale_smart_alarm/test_config_flow.py @@ -132,15 +132,7 @@ async def test_reauth_flow(hass: HomeAssistant) -> None: ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": entry.unique_id, - "entry_id": entry.entry_id, - }, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) assert result["step_id"] == "reauth_confirm" assert result["type"] is FlowResultType.FORM assert result["errors"] == {} @@ -202,15 +194,7 @@ async def test_reauth_flow_error( ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": entry.unique_id, - "entry_id": entry.entry_id, - }, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) with patch( "homeassistant.components.yale_smart_alarm.config_flow.YaleSmartAlarmClient", diff --git a/tests/components/yalexs_ble/test_config_flow.py b/tests/components/yalexs_ble/test_config_flow.py index 15552fdec5f342..5d57095ccd5005 100644 --- a/tests/components/yalexs_ble/test_config_flow.py +++ b/tests/components/yalexs_ble/test_config_flow.py @@ -945,11 +945,7 @@ async def test_reauth(hass: HomeAssistant) -> None: unique_id=YALE_ACCESS_LOCK_DISCOVERY_INFO.address, ) entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_REAUTH, "entry_id": entry.entry_id}, - data=entry.data, - ) + result = await entry.start_reauth_flow(hass) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_validate" diff --git a/tests/components/yolink/test_config_flow.py b/tests/components/yolink/test_config_flow.py index d7ba09e426970f..1dd71368d73750 100644 --- a/tests/components/yolink/test_config_flow.py +++ b/tests/components/yolink/test_config_flow.py @@ -172,15 +172,7 @@ async def test_reauthentication( ) old_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={ - "source": config_entries.SOURCE_REAUTH, - "unique_id": old_entry.unique_id, - "entry_id": old_entry.entry_id, - }, - data=old_entry.data, - ) + result = await old_entry.start_reauth_flow(hass) flows = hass.config_entries.flow.async_progress() assert len(flows) == 1 diff --git a/tests/components/yolink/test_device_trigger.py b/tests/components/yolink/test_device_trigger.py index 6b48b32fd62ac3..c1d3a8acda831c 100644 --- a/tests/components/yolink/test_device_trigger.py +++ b/tests/components/yolink/test_device_trigger.py @@ -6,6 +6,7 @@ from homeassistant.components import automation from homeassistant.components.device_automation import DeviceAutomationType from homeassistant.components.yolink import DOMAIN, YOLINK_EVENT +from homeassistant.components.yolink.const import DEV_MODEL_FLEX_FOB_YS3604_UC from homeassistant.core import HomeAssistant, ServiceCall from homeassistant.helpers import device_registry as dr from homeassistant.setup import async_setup_component @@ -23,6 +24,7 @@ async def test_get_triggers( config_entry_id=config_entry.entry_id, connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, model=ATTR_DEVICE_SMART_REMOTER, + model_id=DEV_MODEL_FLEX_FOB_YS3604_UC, ) expected_triggers = [ @@ -99,6 +101,7 @@ async def test_get_triggers_exception( config_entry_id=config_entry.entry_id, connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, model=ATTR_DEVICE_DIMMER, + model_id=None, ) expected_triggers = [] @@ -123,6 +126,7 @@ async def test_if_fires_on_event( connections={connection}, identifiers={(DOMAIN, mac_address)}, model=ATTR_DEVICE_SMART_REMOTER, + model_id=DEV_MODEL_FLEX_FOB_YS3604_UC, ) assert await async_setup_component( diff --git a/tests/components/youtube/snapshots/test_diagnostics.ambr b/tests/components/youtube/snapshots/test_diagnostics.ambr index a938cb8daad089..50dc2757e8cb97 100644 --- a/tests/components/youtube/snapshots/test_diagnostics.ambr +++ b/tests/components/youtube/snapshots/test_diagnostics.ambr @@ -12,6 +12,7 @@ }), 'subscriber_count': 2290000, 'title': 'Google for Developers', + 'total_views': 214141263, }), }) # --- diff --git a/tests/components/youtube/snapshots/test_sensor.ambr b/tests/components/youtube/snapshots/test_sensor.ambr index cddfa6f6a3d360..dce546b4803d6f 100644 --- a/tests/components/youtube/snapshots/test_sensor.ambr +++ b/tests/components/youtube/snapshots/test_sensor.ambr @@ -30,6 +30,21 @@ 'state': '2290000', }) # --- +# name: test_sensor.2 + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'entity_picture': 'https://yt3.ggpht.com/fca_HuJ99xUxflWdex0XViC3NfctBFreIl8y4i9z411asnGTWY-Ql3MeH_ybA4kNaOjY7kyA=s800-c-k-c0x00ffffff-no-rj', + 'friendly_name': 'Google for Developers Views', + 'unit_of_measurement': 'views', + }), + 'context': , + 'entity_id': 'sensor.google_for_developers_views', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '214141263', + }) +# --- # name: test_sensor_without_uploaded_video StateSnapshot({ 'attributes': ReadOnlyDict({ @@ -58,3 +73,18 @@ 'state': '2290000', }) # --- +# name: test_sensor_without_uploaded_video.2 + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'entity_picture': 'https://yt3.ggpht.com/fca_HuJ99xUxflWdex0XViC3NfctBFreIl8y4i9z411asnGTWY-Ql3MeH_ybA4kNaOjY7kyA=s800-c-k-c0x00ffffff-no-rj', + 'friendly_name': 'Google for Developers Views', + 'unit_of_measurement': 'views', + }), + 'context': , + 'entity_id': 'sensor.google_for_developers_views', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '214141263', + }) +# --- diff --git a/tests/components/youtube/test_sensor.py b/tests/components/youtube/test_sensor.py index ae0c38306e4a6d..e883347c8dbdc6 100644 --- a/tests/components/youtube/test_sensor.py +++ b/tests/components/youtube/test_sensor.py @@ -29,6 +29,9 @@ async def test_sensor( state = hass.states.get("sensor.google_for_developers_subscribers") assert state == snapshot + state = hass.states.get("sensor.google_for_developers_views") + assert state == snapshot + async def test_sensor_without_uploaded_video( hass: HomeAssistant, snapshot: SnapshotAssertion, setup_integration: ComponentSetup @@ -52,6 +55,9 @@ async def test_sensor_without_uploaded_video( state = hass.states.get("sensor.google_for_developers_subscribers") assert state == snapshot + state = hass.states.get("sensor.google_for_developers_views") + assert state == snapshot + async def test_sensor_updating( hass: HomeAssistant, setup_integration: ComponentSetup @@ -95,6 +101,9 @@ async def test_sensor_reauth_trigger( state = hass.states.get("sensor.google_for_developers_subscribers") assert state.state == "2290000" + state = hass.states.get("sensor.google_for_developers_views") + assert state.state == "214141263" + mock.set_thrown_exception(UnauthorizedError()) future = dt_util.utcnow() + timedelta(minutes=15) async_fire_time_changed(hass, future) @@ -121,6 +130,9 @@ async def test_sensor_unavailable( state = hass.states.get("sensor.google_for_developers_subscribers") assert state.state == "2290000" + state = hass.states.get("sensor.google_for_developers_views") + assert state.state == "214141263" + mock.set_thrown_exception(YouTubeBackendError()) future = dt_util.utcnow() + timedelta(minutes=15) async_fire_time_changed(hass, future) @@ -131,3 +143,6 @@ async def test_sensor_unavailable( state = hass.states.get("sensor.google_for_developers_subscribers") assert state.state == "unavailable" + + state = hass.states.get("sensor.google_for_developers_views") + assert state.state == "unavailable" diff --git a/tests/components/zha/snapshots/test_diagnostics.ambr b/tests/components/zha/snapshots/test_diagnostics.ambr index 67655aebc8c6cf..e0da54e2492ded 100644 --- a/tests/components/zha/snapshots/test_diagnostics.ambr +++ b/tests/components/zha/snapshots/test_diagnostics.ambr @@ -162,19 +162,19 @@ '0x0500': dict({ 'attributes': dict({ '0x0000': dict({ - 'attribute': "ZCLAttributeDef(id=0x0000, name='zone_state', type=, access=, mandatory=True, is_manufacturer_specific=False)", + 'attribute': "ZCLAttributeDef(id=0x0000, name='zone_state', type=, zcl_type=, access=, mandatory=True, is_manufacturer_specific=False)", 'value': None, }), '0x0001': dict({ - 'attribute': "ZCLAttributeDef(id=0x0001, name='zone_type', type=, access=, mandatory=True, is_manufacturer_specific=False)", + 'attribute': "ZCLAttributeDef(id=0x0001, name='zone_type', type=, zcl_type=, access=, mandatory=True, is_manufacturer_specific=False)", 'value': None, }), '0x0002': dict({ - 'attribute': "ZCLAttributeDef(id=0x0002, name='zone_status', type=, access=, mandatory=True, is_manufacturer_specific=False)", + 'attribute': "ZCLAttributeDef(id=0x0002, name='zone_status', type=, zcl_type=, access=, mandatory=True, is_manufacturer_specific=False)", 'value': None, }), '0x0010': dict({ - 'attribute': "ZCLAttributeDef(id=0x0010, name='cie_addr', type=, access=, mandatory=True, is_manufacturer_specific=False)", + 'attribute': "ZCLAttributeDef(id=0x0010, name='cie_addr', type=, zcl_type=, access=, mandatory=True, is_manufacturer_specific=False)", 'value': list([ 50, 79, @@ -187,15 +187,15 @@ ]), }), '0x0011': dict({ - 'attribute': "ZCLAttributeDef(id=0x0011, name='zone_id', type=, access=, mandatory=True, is_manufacturer_specific=False)", + 'attribute': "ZCLAttributeDef(id=0x0011, name='zone_id', type=, zcl_type=, access=, mandatory=True, is_manufacturer_specific=False)", 'value': None, }), '0x0012': dict({ - 'attribute': "ZCLAttributeDef(id=0x0012, name='num_zone_sensitivity_levels_supported', type=, access=, mandatory=False, is_manufacturer_specific=False)", + 'attribute': "ZCLAttributeDef(id=0x0012, name='num_zone_sensitivity_levels_supported', type=, zcl_type=, access=, mandatory=False, is_manufacturer_specific=False)", 'value': None, }), '0x0013': dict({ - 'attribute': "ZCLAttributeDef(id=0x0013, name='current_zone_sensitivity_level', type=, access=, mandatory=False, is_manufacturer_specific=False)", + 'attribute': "ZCLAttributeDef(id=0x0013, name='current_zone_sensitivity_level', type=, zcl_type=, access=, mandatory=False, is_manufacturer_specific=False)", 'value': None, }), }), @@ -208,11 +208,11 @@ '0x0501': dict({ 'attributes': dict({ '0xfffd': dict({ - 'attribute': "ZCLAttributeDef(id=0xFFFD, name='cluster_revision', type=, access=, mandatory=True, is_manufacturer_specific=False)", + 'attribute': "ZCLAttributeDef(id=0xFFFD, name='cluster_revision', type=, zcl_type=, access=, mandatory=True, is_manufacturer_specific=False)", 'value': None, }), '0xfffe': dict({ - 'attribute': "ZCLAttributeDef(id=0xFFFE, name='reporting_status', type=, access=, mandatory=False, is_manufacturer_specific=False)", + 'attribute': "ZCLAttributeDef(id=0xFFFE, name='reporting_status', type=, zcl_type=, access=, mandatory=False, is_manufacturer_specific=False)", 'value': None, }), }), diff --git a/tests/components/zha/test_helpers.py b/tests/components/zha/test_helpers.py index 13c03c17cf7a1d..d33926854378d8 100644 --- a/tests/components/zha/test_helpers.py +++ b/tests/components/zha/test_helpers.py @@ -60,16 +60,14 @@ async def test_zcl_schema_conversions(hass: HomeAssistant) -> None: "required": True, }, { - "type": "integer", - "valueMin": 0, - "valueMax": 255, + "type": "multi_select", + "options": ["Execute if off present"], "name": "options_mask", "optional": True, }, { - "type": "integer", - "valueMin": 0, - "valueMax": 255, + "type": "multi_select", + "options": ["Execute if off"], "name": "options_override", "optional": True, }, diff --git a/tests/components/zha/test_update.py b/tests/components/zha/test_update.py index 6a1a19b407f7e7..e2a614915f9587 100644 --- a/tests/components/zha/test_update.py +++ b/tests/components/zha/test_update.py @@ -3,8 +3,11 @@ from unittest.mock import AsyncMock, call, patch import pytest +from zha.application.platforms.update import ( + FirmwareUpdateEntity as ZhaFirmwareUpdateEntity, +) from zigpy.exceptions import DeliveryError -from zigpy.ota import OtaImageWithMetadata +from zigpy.ota import OtaImagesResult, OtaImageWithMetadata import zigpy.ota.image as firmware from zigpy.ota.providers import BaseOtaImageMetadata from zigpy.profiles import zha @@ -43,6 +46,8 @@ from .common import find_entity_id, update_attribute_cache from .conftest import SIG_EP_INPUT, SIG_EP_OUTPUT, SIG_EP_PROFILE, SIG_EP_TYPE +from tests.typing import WebSocketGenerator + @pytest.fixture(autouse=True) def update_platform_only(): @@ -119,8 +124,11 @@ async def setup_test_data( ), ) - cluster.endpoint.device.application.ota.get_ota_image = AsyncMock( - return_value=None if file_not_found else fw_image + cluster.endpoint.device.application.ota.get_ota_images = AsyncMock( + return_value=OtaImagesResult( + upgrades=() if file_not_found else (fw_image,), + downgrades=(), + ) ) zha_device_proxy: ZHADeviceProxy = gateway_proxy.get_device_proxy(zigpy_device.ieee) zha_device_proxy.device.async_update_sw_build_id(installed_fw_version) @@ -544,81 +552,56 @@ async def endpoint_reply(cluster_id, tsn, data, command_id): ) -async def test_firmware_update_no_longer_compatible( +async def test_update_release_notes( hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, setup_zha, zigpy_device_mock, ) -> None: - """Test ZHA update platform - firmware update is no longer valid.""" + """Test ZHA update platform release notes.""" await setup_zha() - zha_device, cluster, fw_image, installed_fw_version = await setup_test_data( - hass, zigpy_device_mock - ) - entity_id = find_entity_id(Platform.UPDATE, zha_device, hass) - assert entity_id is not None - - assert hass.states.get(entity_id).state == STATE_UNKNOWN + gateway = get_zha_gateway(hass) + gateway_proxy: ZHAGatewayProxy = get_zha_gateway_proxy(hass) - # simulate an image available notification - await cluster._handle_query_next_image( - foundation.ZCLHeader.cluster( - tsn=0x12, command_id=general.Ota.ServerCommandDefs.query_next_image.id - ), - general.QueryNextImageCommand( - fw_image.firmware.header.field_control, - zha_device.device.manufacturer_code, - fw_image.firmware.header.image_type, - installed_fw_version, - fw_image.firmware.header.header_version, - ), + zigpy_device = zigpy_device_mock( + { + 1: { + SIG_EP_INPUT: [general.Basic.cluster_id, general.OnOff.cluster_id], + SIG_EP_OUTPUT: [general.Ota.cluster_id], + SIG_EP_TYPE: zha.DeviceType.ON_OFF_SWITCH, + SIG_EP_PROFILE: zha.PROFILE_ID, + } + }, + node_descriptor=b"\x02@\x84_\x11\x7fd\x00\x00,d\x00\x00", ) - await hass.async_block_till_done() - state = hass.states.get(entity_id) - assert state.state == STATE_ON - attrs = state.attributes - assert attrs[ATTR_INSTALLED_VERSION] == f"0x{installed_fw_version:08x}" - assert not attrs[ATTR_IN_PROGRESS] - assert ( - attrs[ATTR_LATEST_VERSION] == f"0x{fw_image.firmware.header.file_version:08x}" - ) + gateway.get_or_create_device(zigpy_device) + await gateway.async_device_initialized(zigpy_device) + await hass.async_block_till_done(wait_background_tasks=True) - new_version = 0x99999999 + zha_device: ZHADeviceProxy = gateway_proxy.get_device_proxy(zigpy_device.ieee) + zha_lib_entity = next( + e + for e in zha_device.device.platform_entities.values() + if isinstance(e, ZhaFirmwareUpdateEntity) + ) + zha_lib_entity._attr_release_notes = "Some lengthy release notes" + zha_lib_entity.maybe_emit_state_changed_event() + await hass.async_block_till_done() - async def endpoint_reply(cluster_id, tsn, data, command_id): - if cluster_id == general.Ota.cluster_id: - hdr, cmd = cluster.deserialize(data) - if isinstance(cmd, general.Ota.ImageNotifyCommand): - zha_device.device.device.packet_received( - make_packet( - zha_device.device.device, - cluster, - general.Ota.ServerCommandDefs.query_next_image.name, - field_control=general.Ota.QueryNextImageCommand.FieldControl.HardwareVersion, - manufacturer_code=fw_image.firmware.header.manufacturer_id, - image_type=fw_image.firmware.header.image_type, - # The device reports that it is no longer compatible! - current_file_version=new_version, - hardware_version=1, - ) - ) + entity_id = find_entity_id(Platform.UPDATE, zha_device, hass) + assert entity_id is not None - cluster.endpoint.reply = AsyncMock(side_effect=endpoint_reply) - with pytest.raises(HomeAssistantError): - await hass.services.async_call( - UPDATE_DOMAIN, - SERVICE_INSTALL, - { - ATTR_ENTITY_ID: entity_id, - }, - blocking=True, - ) + ws_client = await hass_ws_client(hass) + await ws_client.send_json( + { + "id": 1, + "type": "update/release_notes", + "entity_id": entity_id, + } + ) - # We updated the currently installed firmware version, as it is no longer valid - state = hass.states.get(entity_id) - assert state.state == STATE_OFF - attrs = state.attributes - assert attrs[ATTR_INSTALLED_VERSION] == f"0x{new_version:08x}" - assert not attrs[ATTR_IN_PROGRESS] - assert attrs[ATTR_LATEST_VERSION] == f"0x{new_version:08x}" + result = await ws_client.receive_json() + assert result["success"] is True + assert result["result"] == "Some lengthy release notes" diff --git a/tests/components/zwave_js/conftest.py b/tests/components/zwave_js/conftest.py index 60deb7dbce8856..a6bbe554f9a591 100644 --- a/tests/components/zwave_js/conftest.py +++ b/tests/components/zwave_js/conftest.py @@ -1,11 +1,9 @@ """Provide common Z-Wave JS fixtures.""" import asyncio -from collections.abc import Generator import copy import io import json -from typing import Any from unittest.mock import DEFAULT, AsyncMock, patch import pytest @@ -18,242 +16,6 @@ from tests.common import MockConfigEntry, load_fixture -# Add-on fixtures - - -@pytest.fixture(name="addon_info_side_effect") -def addon_info_side_effect_fixture() -> Any | None: - """Return the add-on info side effect.""" - return None - - -@pytest.fixture(name="addon_info") -def mock_addon_info(addon_info_side_effect: Any | None) -> Generator[AsyncMock]: - """Mock Supervisor add-on info.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_get_addon_info", - side_effect=addon_info_side_effect, - ) as addon_info: - addon_info.return_value = { - "available": False, - "hostname": None, - "options": {}, - "state": None, - "update_available": False, - "version": None, - } - yield addon_info - - -@pytest.fixture(name="addon_store_info_side_effect") -def addon_store_info_side_effect_fixture() -> Any | None: - """Return the add-on store info side effect.""" - return None - - -@pytest.fixture(name="addon_store_info") -def mock_addon_store_info( - addon_store_info_side_effect: Any | None, -) -> Generator[AsyncMock]: - """Mock Supervisor add-on info.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_get_addon_store_info", - side_effect=addon_store_info_side_effect, - ) as addon_store_info: - addon_store_info.return_value = { - "available": False, - "installed": None, - "state": None, - "version": "1.0.0", - } - yield addon_store_info - - -@pytest.fixture(name="addon_running") -def mock_addon_running(addon_store_info: AsyncMock, addon_info: AsyncMock) -> AsyncMock: - """Mock add-on already running.""" - addon_store_info.return_value = { - "available": True, - "installed": "1.0.0", - "state": "started", - "version": "1.0.0", - } - addon_info.return_value["available"] = True - addon_info.return_value["state"] = "started" - addon_info.return_value["version"] = "1.0.0" - return addon_info - - -@pytest.fixture(name="addon_installed") -def mock_addon_installed( - addon_store_info: AsyncMock, addon_info: AsyncMock -) -> AsyncMock: - """Mock add-on already installed but not running.""" - addon_store_info.return_value = { - "available": True, - "installed": "1.0.0", - "state": "stopped", - "version": "1.0.0", - } - addon_info.return_value["available"] = True - addon_info.return_value["state"] = "stopped" - addon_info.return_value["version"] = "1.0.0" - return addon_info - - -@pytest.fixture(name="addon_not_installed") -def mock_addon_not_installed( - addon_store_info: AsyncMock, addon_info: AsyncMock -) -> AsyncMock: - """Mock add-on not installed.""" - addon_store_info.return_value["available"] = True - return addon_info - - -@pytest.fixture(name="addon_options") -def mock_addon_options(addon_info: AsyncMock): - """Mock add-on options.""" - return addon_info.return_value["options"] - - -@pytest.fixture(name="set_addon_options_side_effect") -def set_addon_options_side_effect_fixture( - addon_options: dict[str, Any], -) -> Any | None: - """Return the set add-on options side effect.""" - - async def set_addon_options(hass: HomeAssistant, slug: str, options: dict) -> None: - """Mock set add-on options.""" - addon_options.update(options["options"]) - - return set_addon_options - - -@pytest.fixture(name="set_addon_options") -def mock_set_addon_options( - set_addon_options_side_effect: Any | None, -) -> Generator[AsyncMock]: - """Mock set add-on options.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_set_addon_options", - side_effect=set_addon_options_side_effect, - ) as set_options: - yield set_options - - -@pytest.fixture(name="install_addon_side_effect") -def install_addon_side_effect_fixture( - addon_store_info: AsyncMock, addon_info: AsyncMock -) -> Any | None: - """Return the install add-on side effect.""" - - async def install_addon(hass: HomeAssistant, slug): - """Mock install add-on.""" - addon_store_info.return_value = { - "available": True, - "installed": "1.0.0", - "state": "stopped", - "version": "1.0.0", - } - addon_info.return_value["available"] = True - addon_info.return_value["state"] = "stopped" - addon_info.return_value["version"] = "1.0.0" - - return install_addon - - -@pytest.fixture(name="install_addon") -def mock_install_addon(install_addon_side_effect: Any | None) -> Generator[AsyncMock]: - """Mock install add-on.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_install_addon", - side_effect=install_addon_side_effect, - ) as install_addon: - yield install_addon - - -@pytest.fixture(name="update_addon") -def mock_update_addon() -> Generator[AsyncMock]: - """Mock update add-on.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_update_addon" - ) as update_addon: - yield update_addon - - -@pytest.fixture(name="start_addon_side_effect") -def start_addon_side_effect_fixture( - addon_store_info: AsyncMock, addon_info: AsyncMock -) -> Any | None: - """Return the start add-on options side effect.""" - - async def start_addon(hass: HomeAssistant, slug): - """Mock start add-on.""" - addon_store_info.return_value = { - "available": True, - "installed": "1.0.0", - "state": "started", - "version": "1.0.0", - } - addon_info.return_value["available"] = True - addon_info.return_value["state"] = "started" - - return start_addon - - -@pytest.fixture(name="start_addon") -def mock_start_addon(start_addon_side_effect: Any | None) -> Generator[AsyncMock]: - """Mock start add-on.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_start_addon", - side_effect=start_addon_side_effect, - ) as start_addon: - yield start_addon - - -@pytest.fixture(name="stop_addon") -def stop_addon_fixture() -> Generator[AsyncMock]: - """Mock stop add-on.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_stop_addon" - ) as stop_addon: - yield stop_addon - - -@pytest.fixture(name="restart_addon_side_effect") -def restart_addon_side_effect_fixture() -> Any | None: - """Return the restart add-on options side effect.""" - return None - - -@pytest.fixture(name="restart_addon") -def mock_restart_addon(restart_addon_side_effect: Any | None) -> Generator[AsyncMock]: - """Mock restart add-on.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_restart_addon", - side_effect=restart_addon_side_effect, - ) as restart_addon: - yield restart_addon - - -@pytest.fixture(name="uninstall_addon") -def uninstall_addon_fixture() -> Generator[AsyncMock]: - """Mock uninstall add-on.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_uninstall_addon" - ) as uninstall_addon: - yield uninstall_addon - - -@pytest.fixture(name="create_backup") -def create_backup_fixture() -> Generator[AsyncMock]: - """Mock create backup.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_create_backup" - ) as create_backup: - yield create_backup - - # State fixtures diff --git a/tests/components/zwave_js/test_config_flow.py b/tests/components/zwave_js/test_config_flow.py index 46172f72b2ff5f..a3affb6b97794c 100644 --- a/tests/components/zwave_js/test_config_flow.py +++ b/tests/components/zwave_js/test_config_flow.py @@ -5,7 +5,7 @@ from copy import copy from ipaddress import ip_address from typing import Any -from unittest.mock import DEFAULT, AsyncMock, MagicMock, call, patch +from unittest.mock import AsyncMock, MagicMock, call, patch import aiohttp import pytest @@ -77,31 +77,6 @@ def mock_supervisor_fixture() -> Generator[None]: yield -@pytest.fixture(name="discovery_info") -def discovery_info_fixture() -> dict[str, Any]: - """Return the discovery info from the supervisor.""" - return DEFAULT - - -@pytest.fixture(name="discovery_info_side_effect") -def discovery_info_side_effect_fixture() -> Any | None: - """Return the discovery info from the supervisor.""" - return None - - -@pytest.fixture(name="get_addon_discovery_info") -def mock_get_addon_discovery_info( - discovery_info: dict[str, Any], discovery_info_side_effect: Any | None -) -> Generator[AsyncMock]: - """Mock get add-on discovery info.""" - with patch( - "homeassistant.components.hassio.addon_manager.async_get_addon_discovery_info", - side_effect=discovery_info_side_effect, - return_value=discovery_info, - ) as get_addon_discovery_info: - yield get_addon_discovery_info - - @pytest.fixture(name="server_version_side_effect") def server_version_side_effect_fixture() -> Any | None: """Return the server version side effect.""" @@ -2751,104 +2726,6 @@ async def test_options_addon_not_installed( assert client.disconnect.call_count == 1 -@pytest.mark.parametrize("discovery_info", [{"config": ADDON_DISCOVERY_INFO}]) -async def test_import_addon_installed( - hass: HomeAssistant, - supervisor, - addon_installed, - addon_options, - set_addon_options, - start_addon, - get_addon_discovery_info, - serial_port, -) -> None: - """Test import step while add-on already installed on Supervisor.""" - serial_port.device = "/test/imported" - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_IMPORT}, - data={"usb_path": "/test/imported", "network_key": "imported123"}, - ) - - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "on_supervisor" - - result = await hass.config_entries.flow.async_configure( - result["flow_id"], {"use_addon": True} - ) - - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "configure_addon" - - # the default input should be the imported data - default_input = result["data_schema"]({}) - - assert default_input == { - "usb_path": "/test/imported", - "s0_legacy_key": "imported123", - "s2_access_control_key": "", - "s2_authenticated_key": "", - "s2_unauthenticated_key": "", - "lr_s2_access_control_key": "", - "lr_s2_authenticated_key": "", - } - - result = await hass.config_entries.flow.async_configure( - result["flow_id"], default_input - ) - - assert set_addon_options.call_args == call( - hass, - "core_zwave_js", - { - "options": { - "device": "/test/imported", - "s0_legacy_key": "imported123", - "s2_access_control_key": "", - "s2_authenticated_key": "", - "s2_unauthenticated_key": "", - "lr_s2_access_control_key": "", - "lr_s2_authenticated_key": "", - } - }, - ) - - assert result["type"] is FlowResultType.SHOW_PROGRESS - assert result["step_id"] == "start_addon" - - with ( - patch( - "homeassistant.components.zwave_js.async_setup", return_value=True - ) as mock_setup, - patch( - "homeassistant.components.zwave_js.async_setup_entry", - return_value=True, - ) as mock_setup_entry, - ): - await hass.async_block_till_done() - result = await hass.config_entries.flow.async_configure(result["flow_id"]) - await hass.async_block_till_done() - - assert start_addon.call_args == call(hass, "core_zwave_js") - - assert result["type"] is FlowResultType.CREATE_ENTRY - assert result["title"] == TITLE - assert result["data"] == { - "url": "ws://host1:3001", - "usb_path": "/test/imported", - "s0_legacy_key": "imported123", - "s2_access_control_key": "", - "s2_authenticated_key": "", - "s2_unauthenticated_key": "", - "lr_s2_access_control_key": "", - "lr_s2_authenticated_key": "", - "use_addon": True, - "integration_created_addon": False, - } - assert len(mock_setup.mock_calls) == 1 - assert len(mock_setup_entry.mock_calls) == 1 - - async def test_zeroconf(hass: HomeAssistant) -> None: """Test zeroconf discovery.""" diff --git a/tests/components/zwave_js/test_light.py b/tests/components/zwave_js/test_light.py index 376bd700a2a1b9..4c725c6dc291b0 100644 --- a/tests/components/zwave_js/test_light.py +++ b/tests/components/zwave_js/test_light.py @@ -8,6 +8,7 @@ ATTR_BRIGHTNESS, ATTR_COLOR_MODE, ATTR_COLOR_TEMP, + ATTR_HS_COLOR, ATTR_MAX_MIREDS, ATTR_MIN_MIREDS, ATTR_RGB_COLOR, @@ -37,8 +38,8 @@ ZEN_31_ENTITY, ) -HSM200_V1_ENTITY = "light.hsm200" ZDB5100_ENTITY = "light.matrix_office" +HSM200_V1_ENTITY = "light.hsm200" async def test_light( @@ -510,14 +511,388 @@ async def test_light_none_color_value( assert state.attributes[ATTR_SUPPORTED_COLOR_MODES] == ["hs"] -async def test_black_is_off( +async def test_light_on_off_color( + hass: HomeAssistant, client, logic_group_zdb5100, integration +) -> None: + """Test the light entity for RGB lights without dimming support.""" + node = logic_group_zdb5100 + state = hass.states.get(ZDB5100_ENTITY) + assert state.state == STATE_OFF + + async def update_color(red: int, green: int, blue: int) -> None: + event = Event( + type="value updated", + data={ + "source": "node", + "event": "value updated", + "nodeId": node.node_id, + "args": { + "commandClassName": "Color Switch", + "commandClass": 51, + "endpoint": 1, + "property": "currentColor", + "propertyKey": 2, # red + "newValue": red, + "prevValue": None, + "propertyName": "currentColor", + "propertyKeyName": "red", + }, + }, + ) + node.receive_event(event) + await hass.async_block_till_done() + + event = Event( + type="value updated", + data={ + "source": "node", + "event": "value updated", + "nodeId": node.node_id, + "args": { + "commandClassName": "Color Switch", + "commandClass": 51, + "endpoint": 1, + "property": "currentColor", + "propertyKey": 3, # green + "newValue": green, + "prevValue": None, + "propertyName": "currentColor", + "propertyKeyName": "green", + }, + }, + ) + node.receive_event(event) + await hass.async_block_till_done() + + event = Event( + type="value updated", + data={ + "source": "node", + "event": "value updated", + "nodeId": node.node_id, + "args": { + "commandClassName": "Color Switch", + "commandClass": 51, + "endpoint": 1, + "property": "currentColor", + "propertyKey": 4, # blue + "newValue": blue, + "prevValue": None, + "propertyName": "currentColor", + "propertyKeyName": "blue", + }, + }, + ) + node.receive_event(event) + await hass.async_block_till_done() + + event = Event( + type="value updated", + data={ + "source": "node", + "event": "value updated", + "nodeId": node.node_id, + "args": { + "commandClassName": "Color Switch", + "commandClass": 51, + "endpoint": 1, + "property": "currentColor", + "newValue": { + "red": red, + "green": green, + "blue": blue, + }, + "prevValue": None, + "propertyName": "currentColor", + }, + }, + ) + node.receive_event(event) + await hass.async_block_till_done() + + async def update_switch_state(state: bool) -> None: + event = Event( + type="value updated", + data={ + "source": "node", + "event": "value updated", + "nodeId": node.node_id, + "args": { + "commandClassName": "Binary Switch", + "commandClass": 37, + "endpoint": 1, + "property": "currentValue", + "newValue": state, + "prevValue": None, + "propertyName": "currentValue", + }, + }, + ) + node.receive_event(event) + await hass.async_block_till_done() + + # Turn on the light. Since this is the first call, the light should default to white + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: ZDB5100_ENTITY}, + blocking=True, + ) + assert len(client.async_send_command.call_args_list) == 2 + args = client.async_send_command.call_args_list[0][0][0] + assert args["command"] == "node.set_value" + assert args["nodeId"] == node.node_id + assert args["valueId"] == { + "commandClass": 51, + "endpoint": 1, + "property": "targetColor", + } + assert args["value"] == { + "red": 255, + "green": 255, + "blue": 255, + } + + args = client.async_send_command.call_args_list[1][0][0] + assert args["command"] == "node.set_value" + assert args["nodeId"] == node.node_id + assert args["valueId"] == { + "commandClass": 37, + "endpoint": 1, + "property": "targetValue", + } + assert args["value"] is True + + # Force the light to turn off + await update_switch_state(False) + + state = hass.states.get(ZDB5100_ENTITY) + assert state.state == STATE_OFF + + # Force the light to turn on (green) + await update_color(0, 255, 0) + await update_switch_state(True) + + state = hass.states.get(ZDB5100_ENTITY) + assert state.state == STATE_ON + + client.async_send_command.reset_mock() + + # Set the brightness to 128. This should be encoded in the color value + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: ZDB5100_ENTITY, ATTR_BRIGHTNESS: 128}, + blocking=True, + ) + assert len(client.async_send_command.call_args_list) == 2 + args = client.async_send_command.call_args_list[0][0][0] + assert args["command"] == "node.set_value" + assert args["nodeId"] == node.node_id + assert args["valueId"] == { + "commandClass": 51, + "endpoint": 1, + "property": "targetColor", + } + assert args["value"] == { + "red": 0, + "green": 128, + "blue": 0, + } + + args = client.async_send_command.call_args_list[1][0][0] + assert args["command"] == "node.set_value" + assert args["nodeId"] == node.node_id + assert args["valueId"] == { + "commandClass": 37, + "endpoint": 1, + "property": "targetValue", + } + assert args["value"] is True + + client.async_send_command.reset_mock() + + # Force the light to turn on (green, 50%) + await update_color(0, 128, 0) + + # Set the color to red. This should preserve the previous brightness value + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: ZDB5100_ENTITY, ATTR_HS_COLOR: (0, 100)}, + blocking=True, + ) + assert len(client.async_send_command.call_args_list) == 2 + args = client.async_send_command.call_args_list[0][0][0] + assert args["command"] == "node.set_value" + assert args["nodeId"] == node.node_id + assert args["valueId"] == { + "commandClass": 51, + "endpoint": 1, + "property": "targetColor", + } + assert args["value"] == { + "red": 128, + "green": 0, + "blue": 0, + } + + args = client.async_send_command.call_args_list[1][0][0] + assert args["command"] == "node.set_value" + assert args["nodeId"] == node.node_id + assert args["valueId"] == { + "commandClass": 37, + "endpoint": 1, + "property": "targetValue", + } + assert args["value"] is True + + client.async_send_command.reset_mock() + + # Force the light to turn on (red, 50%) + await update_color(128, 0, 0) + + # Turn the device off. This should only affect the binary switch, not the color + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: ZDB5100_ENTITY}, + blocking=True, + ) + assert len(client.async_send_command.call_args_list) == 1 + args = client.async_send_command.call_args_list[0][0][0] + assert args["command"] == "node.set_value" + assert args["nodeId"] == node.node_id + assert args["valueId"] == { + "commandClass": 37, + "endpoint": 1, + "property": "targetValue", + } + assert args["value"] is False + + client.async_send_command.reset_mock() + + # Force the light to turn off + await update_switch_state(False) + + # Turn the device on again. This should only affect the binary switch, not the color + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: ZDB5100_ENTITY}, + blocking=True, + ) + assert len(client.async_send_command.call_args_list) == 1 + args = client.async_send_command.call_args_list[0][0][0] + assert args["command"] == "node.set_value" + assert args["nodeId"] == node.node_id + assert args["valueId"] == { + "commandClass": 37, + "endpoint": 1, + "property": "targetValue", + } + assert args["value"] is True + + +async def test_light_color_only( hass: HomeAssistant, client, express_controls_ezmultipli, integration ) -> None: - """Test the black is off light entity.""" + """Test the light entity for RGB lights with Color Switch CC only.""" node = express_controls_ezmultipli state = hass.states.get(HSM200_V1_ENTITY) assert state.state == STATE_ON + async def update_color(red: int, green: int, blue: int) -> None: + event = Event( + type="value updated", + data={ + "source": "node", + "event": "value updated", + "nodeId": node.node_id, + "args": { + "commandClassName": "Color Switch", + "commandClass": 51, + "endpoint": 0, + "property": "currentColor", + "propertyKey": 2, # red + "newValue": red, + "prevValue": None, + "propertyName": "currentColor", + "propertyKeyName": "red", + }, + }, + ) + node.receive_event(event) + await hass.async_block_till_done() + + event = Event( + type="value updated", + data={ + "source": "node", + "event": "value updated", + "nodeId": node.node_id, + "args": { + "commandClassName": "Color Switch", + "commandClass": 51, + "endpoint": 0, + "property": "currentColor", + "propertyKey": 3, # green + "newValue": green, + "prevValue": None, + "propertyName": "currentColor", + "propertyKeyName": "green", + }, + }, + ) + node.receive_event(event) + await hass.async_block_till_done() + + event = Event( + type="value updated", + data={ + "source": "node", + "event": "value updated", + "nodeId": node.node_id, + "args": { + "commandClassName": "Color Switch", + "commandClass": 51, + "endpoint": 0, + "property": "currentColor", + "propertyKey": 4, # blue + "newValue": blue, + "prevValue": None, + "propertyName": "currentColor", + "propertyKeyName": "blue", + }, + }, + ) + node.receive_event(event) + await hass.async_block_till_done() + + event = Event( + type="value updated", + data={ + "source": "node", + "event": "value updated", + "nodeId": node.node_id, + "args": { + "commandClassName": "Color Switch", + "commandClass": 51, + "endpoint": 0, + "property": "currentColor", + "newValue": { + "red": red, + "green": green, + "blue": blue, + }, + "prevValue": None, + "propertyName": "currentColor", + }, + }, + ) + node.receive_event(event) + await hass.async_block_till_done() + # Attempt to turn on the light and ensure it defaults to white await hass.services.async_call( LIGHT_DOMAIN, @@ -539,64 +914,14 @@ async def test_black_is_off( client.async_send_command.reset_mock() # Force the light to turn off - event = Event( - type="value updated", - data={ - "source": "node", - "event": "value updated", - "nodeId": node.node_id, - "args": { - "commandClassName": "Color Switch", - "commandClass": 51, - "endpoint": 0, - "property": "currentColor", - "newValue": { - "red": 0, - "green": 0, - "blue": 0, - }, - "prevValue": { - "red": 0, - "green": 255, - "blue": 0, - }, - "propertyName": "currentColor", - }, - }, - ) - node.receive_event(event) - await hass.async_block_till_done() + await update_color(0, 0, 0) + state = hass.states.get(HSM200_V1_ENTITY) assert state.state == STATE_OFF - # Force the light to turn on - event = Event( - type="value updated", - data={ - "source": "node", - "event": "value updated", - "nodeId": node.node_id, - "args": { - "commandClassName": "Color Switch", - "commandClass": 51, - "endpoint": 0, - "property": "currentColor", - "newValue": { - "red": 0, - "green": 255, - "blue": 0, - }, - "prevValue": { - "red": 0, - "green": 0, - "blue": 0, - }, - "propertyName": "currentColor", - }, - }, - ) - node.receive_event(event) - await hass.async_block_till_done() + # Force the light to turn on (50% green) + await update_color(0, 128, 0) + state = hass.states.get(HSM200_V1_ENTITY) assert state.state == STATE_ON @@ -619,6 +944,9 @@ async def test_black_is_off( client.async_send_command.reset_mock() + # Force the light to turn off + await update_color(0, 0, 0) + # Assert that the last color is restored await hass.services.async_call( LIGHT_DOMAIN, @@ -635,44 +963,23 @@ async def test_black_is_off( "endpoint": 0, "property": "targetColor", } - assert args["value"] == {"red": 0, "green": 255, "blue": 0} + assert args["value"] == {"red": 0, "green": 128, "blue": 0} client.async_send_command.reset_mock() - # Force the light to turn on - event = Event( - type="value updated", - data={ - "source": "node", - "event": "value updated", - "nodeId": node.node_id, - "args": { - "commandClassName": "Color Switch", - "commandClass": 51, - "endpoint": 0, - "property": "currentColor", - "newValue": None, - "prevValue": { - "red": 0, - "green": 255, - "blue": 0, - }, - "propertyName": "currentColor", - }, - }, - ) - node.receive_event(event) - await hass.async_block_till_done() + # Force the light to turn on (50% green) + await update_color(0, 128, 0) + state = hass.states.get(HSM200_V1_ENTITY) - assert state.state == STATE_UNKNOWN + assert state.state == STATE_ON client.async_send_command.reset_mock() - # Assert that call fails if attribute is added to service call + # Assert that the brightness is preserved when changing colors await hass.services.async_call( LIGHT_DOMAIN, SERVICE_TURN_ON, - {ATTR_ENTITY_ID: HSM200_V1_ENTITY, ATTR_RGBW_COLOR: (255, 76, 255, 0)}, + {ATTR_ENTITY_ID: HSM200_V1_ENTITY, ATTR_RGB_COLOR: (255, 0, 0)}, blocking=True, ) assert len(client.async_send_command.call_args_list) == 1 @@ -684,22 +991,21 @@ async def test_black_is_off( "endpoint": 0, "property": "targetColor", } - assert args["value"] == {"red": 255, "green": 76, "blue": 255} + assert args["value"] == {"red": 128, "green": 0, "blue": 0} + + client.async_send_command.reset_mock() + # Force the light to turn on (50% red) + await update_color(128, 0, 0) -async def test_black_is_off_zdb5100( - hass: HomeAssistant, client, logic_group_zdb5100, integration -) -> None: - """Test the black is off light entity.""" - node = logic_group_zdb5100 - state = hass.states.get(ZDB5100_ENTITY) - assert state.state == STATE_OFF + state = hass.states.get(HSM200_V1_ENTITY) + assert state.state == STATE_ON - # Attempt to turn on the light and ensure it defaults to white + # Assert that the color is preserved when changing brightness await hass.services.async_call( LIGHT_DOMAIN, SERVICE_TURN_ON, - {ATTR_ENTITY_ID: ZDB5100_ENTITY}, + {ATTR_ENTITY_ID: HSM200_V1_ENTITY, ATTR_BRIGHTNESS: 69}, blocking=True, ) assert len(client.async_send_command.call_args_list) == 1 @@ -708,79 +1014,31 @@ async def test_black_is_off_zdb5100( assert args["nodeId"] == node.node_id assert args["valueId"] == { "commandClass": 51, - "endpoint": 1, + "endpoint": 0, "property": "targetColor", } - assert args["value"] == {"red": 255, "green": 255, "blue": 255} + assert args["value"] == {"red": 69, "green": 0, "blue": 0} client.async_send_command.reset_mock() - # Force the light to turn off - event = Event( - type="value updated", - data={ - "source": "node", - "event": "value updated", - "nodeId": node.node_id, - "args": { - "commandClassName": "Color Switch", - "commandClass": 51, - "endpoint": 1, - "property": "currentColor", - "newValue": { - "red": 0, - "green": 0, - "blue": 0, - }, - "prevValue": { - "red": 0, - "green": 255, - "blue": 0, - }, - "propertyName": "currentColor", - }, - }, - ) - node.receive_event(event) - await hass.async_block_till_done() - state = hass.states.get(ZDB5100_ENTITY) - assert state.state == STATE_OFF + await update_color(69, 0, 0) - # Force the light to turn on - event = Event( - type="value updated", - data={ - "source": "node", - "event": "value updated", - "nodeId": node.node_id, - "args": { - "commandClassName": "Color Switch", - "commandClass": 51, - "endpoint": 1, - "property": "currentColor", - "newValue": { - "red": 0, - "green": 255, - "blue": 0, - }, - "prevValue": { - "red": 0, - "green": 0, - "blue": 0, - }, - "propertyName": "currentColor", - }, - }, + # Turn off again + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: HSM200_V1_ENTITY}, + blocking=True, ) - node.receive_event(event) - await hass.async_block_till_done() - state = hass.states.get(ZDB5100_ENTITY) - assert state.state == STATE_ON + await update_color(0, 0, 0) + + client.async_send_command.reset_mock() + # Assert that the color is preserved when turning on with brightness await hass.services.async_call( LIGHT_DOMAIN, - SERVICE_TURN_OFF, - {ATTR_ENTITY_ID: ZDB5100_ENTITY}, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: HSM200_V1_ENTITY, ATTR_BRIGHTNESS: 123}, blocking=True, ) assert len(client.async_send_command.call_args_list) == 1 @@ -789,18 +1047,31 @@ async def test_black_is_off_zdb5100( assert args["nodeId"] == node.node_id assert args["valueId"] == { "commandClass": 51, - "endpoint": 1, + "endpoint": 0, "property": "targetColor", } - assert args["value"] == {"red": 0, "green": 0, "blue": 0} + assert args["value"] == {"red": 123, "green": 0, "blue": 0} client.async_send_command.reset_mock() - # Assert that the last color is restored + await update_color(123, 0, 0) + + # Turn off again + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: HSM200_V1_ENTITY}, + blocking=True, + ) + await update_color(0, 0, 0) + + client.async_send_command.reset_mock() + + # Assert that the brightness is preserved when turning on with color await hass.services.async_call( LIGHT_DOMAIN, SERVICE_TURN_ON, - {ATTR_ENTITY_ID: ZDB5100_ENTITY}, + {ATTR_ENTITY_ID: HSM200_V1_ENTITY, ATTR_HS_COLOR: (240, 100)}, blocking=True, ) assert len(client.async_send_command.call_args_list) == 1 @@ -809,14 +1080,14 @@ async def test_black_is_off_zdb5100( assert args["nodeId"] == node.node_id assert args["valueId"] == { "commandClass": 51, - "endpoint": 1, + "endpoint": 0, "property": "targetColor", } - assert args["value"] == {"red": 0, "green": 255, "blue": 0} + assert args["value"] == {"red": 0, "green": 0, "blue": 123} client.async_send_command.reset_mock() - # Force the light to turn on + # Clear the color value to trigger an unknown state event = Event( type="value updated", data={ @@ -826,21 +1097,18 @@ async def test_black_is_off_zdb5100( "args": { "commandClassName": "Color Switch", "commandClass": 51, - "endpoint": 1, + "endpoint": 0, "property": "currentColor", "newValue": None, - "prevValue": { - "red": 0, - "green": 255, - "blue": 0, - }, + "prevValue": None, "propertyName": "currentColor", }, }, ) node.receive_event(event) await hass.async_block_till_done() - state = hass.states.get(ZDB5100_ENTITY) + + state = hass.states.get(HSM200_V1_ENTITY) assert state.state == STATE_UNKNOWN client.async_send_command.reset_mock() @@ -849,7 +1117,7 @@ async def test_black_is_off_zdb5100( await hass.services.async_call( LIGHT_DOMAIN, SERVICE_TURN_ON, - {ATTR_ENTITY_ID: ZDB5100_ENTITY, ATTR_RGBW_COLOR: (255, 76, 255, 0)}, + {ATTR_ENTITY_ID: HSM200_V1_ENTITY, ATTR_RGBW_COLOR: (255, 76, 255, 0)}, blocking=True, ) assert len(client.async_send_command.call_args_list) == 1 @@ -858,7 +1126,7 @@ async def test_black_is_off_zdb5100( assert args["nodeId"] == node.node_id assert args["valueId"] == { "commandClass": 51, - "endpoint": 1, + "endpoint": 0, "property": "targetColor", } assert args["value"] == {"red": 255, "green": 76, "blue": 255} diff --git a/tests/components/zwave_js/test_sensor.py b/tests/components/zwave_js/test_sensor.py index 02b3df17e228a7..34c50b8d4498df 100644 --- a/tests/components/zwave_js/test_sensor.py +++ b/tests/components/zwave_js/test_sensor.py @@ -23,6 +23,10 @@ SERVICE_RESET_METER, ) from homeassistant.components.zwave_js.helpers import get_valueless_base_unique_id +from homeassistant.components.zwave_js.sensor import ( + CONTROLLER_STATISTICS_KEY_MAP, + NODE_STATISTICS_KEY_MAP, +) from homeassistant.const import ( ATTR_DEVICE_CLASS, ATTR_ENTITY_ID, @@ -55,6 +59,8 @@ VOLTAGE_SENSOR, ) +from tests.common import MockConfigEntry + async def test_numeric_sensor( hass: HomeAssistant, @@ -522,7 +528,7 @@ async def test_reset_meter( "test", 1, "test" ) - with pytest.raises(HomeAssistantError): + with pytest.raises(HomeAssistantError) as err: await hass.services.async_call( DOMAIN, SERVICE_RESET_METER, @@ -530,6 +536,11 @@ async def test_reset_meter( blocking=True, ) + assert str(err.value) == ( + "Failed to reset meters on node Node(node_id=102) endpoint 0: " + "zwave_error: Z-Wave error 1 - test" + ) + async def test_meter_attributes( hass: HomeAssistant, client, aeon_smart_switch_6, integration @@ -751,6 +762,54 @@ async def test_unit_change(hass: HomeAssistant, zp3111, client, integration) -> } +async def test_statistics_sensors_migration( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + zp3111_state, + client, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test statistics migration sensor.""" + node = Node(client, copy.deepcopy(zp3111_state)) + client.driver.controller.nodes[node.node_id] = node + + entry = MockConfigEntry(domain="zwave_js", data={"url": "ws://test.org"}) + entry.add_to_hass(hass) + + controller_base_unique_id = f"{client.driver.controller.home_id}.1.statistics" + node_base_unique_id = f"{client.driver.controller.home_id}.22.statistics" + + # Create entity registry records for the old statistics keys + for base_unique_id, key_map in ( + (controller_base_unique_id, CONTROLLER_STATISTICS_KEY_MAP), + (node_base_unique_id, NODE_STATISTICS_KEY_MAP), + ): + # old key + for key in key_map.values(): + entity_registry.async_get_or_create( + "sensor", DOMAIN, f"{base_unique_id}_{key}" + ) + + # Set up integration + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + # Validate that entity unique ID's have changed + for base_unique_id, key_map in ( + (controller_base_unique_id, CONTROLLER_STATISTICS_KEY_MAP), + (node_base_unique_id, NODE_STATISTICS_KEY_MAP), + ): + for new_key, old_key in key_map.items(): + # If the key has changed, the old entity should not exist + if new_key != old_key: + assert not entity_registry.async_get_entity_id( + "sensor", DOMAIN, f"{base_unique_id}_{old_key}" + ) + assert entity_registry.async_get_entity_id( + "sensor", DOMAIN, f"{base_unique_id}_{new_key}" + ) + + async def test_statistics_sensors_no_last_seen( hass: HomeAssistant, entity_registry: er.EntityRegistry, diff --git a/tests/components/zwave_js/test_switch.py b/tests/components/zwave_js/test_switch.py index c18c0c4359e25a..810ce38cf99823 100644 --- a/tests/components/zwave_js/test_switch.py +++ b/tests/components/zwave_js/test_switch.py @@ -286,7 +286,11 @@ async def test_config_parameter_switch( client.async_send_command.side_effect = FailedZWaveCommand("test", 1, "test") # Test turning off error raises proper exception - with pytest.raises(HomeAssistantError): + with pytest.raises(HomeAssistantError) as err: await hass.services.async_call( DOMAIN, SERVICE_TURN_OFF, {"entity_id": switch_entity_id}, blocking=True ) + + assert str(err.value) == ( + "Unable to set value 32-112-0-20: zwave_error: Z-Wave error 1 - test" + ) diff --git a/tests/hassfest/test_requirements.py b/tests/hassfest/test_requirements.py index f3b008a6113ed5..433e63d904c5f1 100644 --- a/tests/hassfest/test_requirements.py +++ b/tests/hassfest/test_requirements.py @@ -4,7 +4,7 @@ import pytest -from script.hassfest.model import Integration +from script.hassfest.model import Config, Integration from script.hassfest.requirements import validate_requirements_format @@ -13,6 +13,13 @@ def integration(): """Fixture for hassfest integration model.""" return Integration( path=Path("homeassistant/components/test"), + _config=Config( + root=Path(".").absolute(), + specific_integrations=None, + action="validate", + requirements=True, + core_integrations_path=Path("homeassistant/components"), + ), _manifest={ "domain": "test", "documentation": "https://example.com", diff --git a/tests/hassfest/test_version.py b/tests/hassfest/test_version.py index bfe15018fe22de..30677356101e5b 100644 --- a/tests/hassfest/test_version.py +++ b/tests/hassfest/test_version.py @@ -1,5 +1,7 @@ """Tests for hassfest version.""" +from pathlib import Path + import pytest import voluptuous as vol @@ -7,13 +9,22 @@ CUSTOM_INTEGRATION_MANIFEST_SCHEMA, validate_version, ) -from script.hassfest.model import Integration +from script.hassfest.model import Config, Integration @pytest.fixture def integration(): """Fixture for hassfest integration model.""" - integration = Integration("") + integration = Integration( + "", + _config=Config( + root=Path(".").absolute(), + specific_integrations=None, + action="validate", + requirements=True, + core_integrations_path=Path("homeassistant/components"), + ), + ) integration._manifest = { "domain": "test", "documentation": "https://example.com", diff --git a/tests/helpers/snapshots/test_template.ambr b/tests/helpers/snapshots/test_template.ambr new file mode 100644 index 00000000000000..af38433f1a41a3 --- /dev/null +++ b/tests/helpers/snapshots/test_template.ambr @@ -0,0 +1,337 @@ +# serializer version: 1 +# name: test_merge_response[calendar][a_response] + dict({ + 'calendar.local_furry_events': dict({ + 'events': list([ + ]), + }), + 'calendar.sports': dict({ + 'events': list([ + dict({ + 'description': '', + 'end': '2024-02-27T18:00:00-06:00', + 'start': '2024-02-27T17:00:00-06:00', + 'summary': 'Basketball vs. Rockets', + }), + ]), + }), + 'calendar.yap_house_schedules': dict({ + 'events': list([ + dict({ + 'description': '', + 'end': '2024-02-26T09:00:00-06:00', + 'start': '2024-02-26T08:00:00-06:00', + 'summary': 'Dr. Appt', + }), + dict({ + 'description': 'something good', + 'end': '2024-02-28T21:00:00-06:00', + 'start': '2024-02-28T20:00:00-06:00', + 'summary': 'Bake a cake', + }), + ]), + }), + }) +# --- +# name: test_merge_response[calendar][b_rendered] + Wrapper([ + dict({ + 'description': '', + 'end': '2024-02-27T18:00:00-06:00', + 'entity_id': 'calendar.sports', + 'start': '2024-02-27T17:00:00-06:00', + 'summary': 'Basketball vs. Rockets', + 'value_key': 'events', + }), + dict({ + 'description': '', + 'end': '2024-02-26T09:00:00-06:00', + 'entity_id': 'calendar.yap_house_schedules', + 'start': '2024-02-26T08:00:00-06:00', + 'summary': 'Dr. Appt', + 'value_key': 'events', + }), + dict({ + 'description': 'something good', + 'end': '2024-02-28T21:00:00-06:00', + 'entity_id': 'calendar.yap_house_schedules', + 'start': '2024-02-28T20:00:00-06:00', + 'summary': 'Bake a cake', + 'value_key': 'events', + }), + ]) +# --- +# name: test_merge_response[vacuum][a_response] + dict({ + 'vacuum.deebot_n8_plus_1': dict({ + 'header': dict({ + 'ver': '0.0.1', + }), + 'payloadType': 'j', + 'resp': dict({ + 'body': dict({ + 'msg': 'ok', + }), + }), + }), + 'vacuum.deebot_n8_plus_2': dict({ + 'header': dict({ + 'ver': '0.0.1', + }), + 'payloadType': 'j', + 'resp': dict({ + 'body': dict({ + 'msg': 'ok', + }), + }), + }), + }) +# --- +# name: test_merge_response[vacuum][b_rendered] + Wrapper([ + dict({ + 'entity_id': 'vacuum.deebot_n8_plus_1', + 'header': dict({ + 'ver': '0.0.1', + }), + 'payloadType': 'j', + 'resp': dict({ + 'body': dict({ + 'msg': 'ok', + }), + }), + }), + dict({ + 'entity_id': 'vacuum.deebot_n8_plus_2', + 'header': dict({ + 'ver': '0.0.1', + }), + 'payloadType': 'j', + 'resp': dict({ + 'body': dict({ + 'msg': 'ok', + }), + }), + }), + ]) +# --- +# name: test_merge_response[weather][a_response] + dict({ + 'weather.forecast_home': dict({ + 'forecast': list([ + dict({ + 'condition': 'cloudy', + 'datetime': '2024-03-31T10:00:00+00:00', + 'humidity': 71, + 'precipitation': 0, + 'precipitation_probability': 6.6, + 'temperature': 10.9, + 'templow': 6.5, + 'wind_bearing': 71.8, + 'wind_gust_speed': 24.1, + 'wind_speed': 13.7, + }), + dict({ + 'condition': 'cloudy', + 'datetime': '2024-04-01T10:00:00+00:00', + 'humidity': 79, + 'precipitation': 0, + 'precipitation_probability': 8, + 'temperature': 10.2, + 'templow': 3.4, + 'wind_bearing': 350.6, + 'wind_gust_speed': 38.2, + 'wind_speed': 21.6, + }), + dict({ + 'condition': 'snowy', + 'datetime': '2024-04-02T10:00:00+00:00', + 'humidity': 77, + 'precipitation': 2.3, + 'precipitation_probability': 67.4, + 'temperature': 3, + 'templow': 0, + 'wind_bearing': 24.5, + 'wind_gust_speed': 64.8, + 'wind_speed': 37.4, + }), + ]), + }), + 'weather.smhi_home': dict({ + 'forecast': list([ + dict({ + 'cloud_coverage': 100, + 'condition': 'cloudy', + 'datetime': '2024-03-31T16:00:00', + 'humidity': 87, + 'precipitation': 0.2, + 'pressure': 998, + 'temperature': 10, + 'templow': 4, + 'wind_bearing': 79, + 'wind_gust_speed': 21.6, + 'wind_speed': 11.88, + }), + dict({ + 'cloud_coverage': 100, + 'condition': 'rainy', + 'datetime': '2024-04-01T12:00:00', + 'humidity': 88, + 'precipitation': 2.2, + 'pressure': 999, + 'temperature': 6, + 'templow': 1, + 'wind_bearing': 17, + 'wind_gust_speed': 20.52, + 'wind_speed': 8.64, + }), + dict({ + 'cloud_coverage': 100, + 'condition': 'cloudy', + 'datetime': '2024-04-02T12:00:00', + 'humidity': 71, + 'precipitation': 1.3, + 'pressure': 1003, + 'temperature': 0, + 'templow': -3, + 'wind_bearing': 17, + 'wind_gust_speed': 57.24, + 'wind_speed': 30.6, + }), + ]), + }), + }) +# --- +# name: test_merge_response[weather][b_rendered] + Wrapper([ + dict({ + 'cloud_coverage': 100, + 'condition': 'cloudy', + 'datetime': '2024-03-31T16:00:00', + 'entity_id': 'weather.smhi_home', + 'humidity': 87, + 'precipitation': 0.2, + 'pressure': 998, + 'temperature': 10, + 'templow': 4, + 'value_key': 'forecast', + 'wind_bearing': 79, + 'wind_gust_speed': 21.6, + 'wind_speed': 11.88, + }), + dict({ + 'cloud_coverage': 100, + 'condition': 'rainy', + 'datetime': '2024-04-01T12:00:00', + 'entity_id': 'weather.smhi_home', + 'humidity': 88, + 'precipitation': 2.2, + 'pressure': 999, + 'temperature': 6, + 'templow': 1, + 'value_key': 'forecast', + 'wind_bearing': 17, + 'wind_gust_speed': 20.52, + 'wind_speed': 8.64, + }), + dict({ + 'cloud_coverage': 100, + 'condition': 'cloudy', + 'datetime': '2024-04-02T12:00:00', + 'entity_id': 'weather.smhi_home', + 'humidity': 71, + 'precipitation': 1.3, + 'pressure': 1003, + 'temperature': 0, + 'templow': -3, + 'value_key': 'forecast', + 'wind_bearing': 17, + 'wind_gust_speed': 57.24, + 'wind_speed': 30.6, + }), + dict({ + 'condition': 'cloudy', + 'datetime': '2024-03-31T10:00:00+00:00', + 'entity_id': 'weather.forecast_home', + 'humidity': 71, + 'precipitation': 0, + 'precipitation_probability': 6.6, + 'temperature': 10.9, + 'templow': 6.5, + 'value_key': 'forecast', + 'wind_bearing': 71.8, + 'wind_gust_speed': 24.1, + 'wind_speed': 13.7, + }), + dict({ + 'condition': 'cloudy', + 'datetime': '2024-04-01T10:00:00+00:00', + 'entity_id': 'weather.forecast_home', + 'humidity': 79, + 'precipitation': 0, + 'precipitation_probability': 8, + 'temperature': 10.2, + 'templow': 3.4, + 'value_key': 'forecast', + 'wind_bearing': 350.6, + 'wind_gust_speed': 38.2, + 'wind_speed': 21.6, + }), + dict({ + 'condition': 'snowy', + 'datetime': '2024-04-02T10:00:00+00:00', + 'entity_id': 'weather.forecast_home', + 'humidity': 77, + 'precipitation': 2.3, + 'precipitation_probability': 67.4, + 'temperature': 3, + 'templow': 0, + 'value_key': 'forecast', + 'wind_bearing': 24.5, + 'wind_gust_speed': 64.8, + 'wind_speed': 37.4, + }), + ]) +# --- +# name: test_merge_response[workday][a_response] + dict({ + 'binary_sensor.workday': dict({ + 'workday': True, + }), + 'binary_sensor.workday2': dict({ + 'workday': False, + }), + }) +# --- +# name: test_merge_response[workday][b_rendered] + Wrapper([ + dict({ + 'entity_id': 'binary_sensor.workday', + 'workday': True, + }), + dict({ + 'entity_id': 'binary_sensor.workday2', + 'workday': False, + }), + ]) +# --- +# name: test_merge_response_with_empty_response[a_response] + dict({ + 'calendar.local_furry_events': dict({ + 'events': list([ + ]), + }), + 'calendar.sports': dict({ + 'events': list([ + ]), + }), + 'calendar.yap_house_schedules': dict({ + 'events': list([ + ]), + }), + }) +# --- +# name: test_merge_response_with_empty_response[b_rendered] + Wrapper([ + ]) +# --- diff --git a/tests/helpers/test_area_registry.py b/tests/helpers/test_area_registry.py index ad571ac50cc08f..da1947adbc8f18 100644 --- a/tests/helpers/test_area_registry.py +++ b/tests/helpers/test_area_registry.py @@ -242,9 +242,12 @@ async def test_update_area_with_same_name_change_case( async def test_update_area_with_name_already_in_use( area_registry: ar.AreaRegistry, + floor_registry: fr.FloorRegistry, ) -> None: """Make sure that we can't update an area with a name already in use.""" - area1 = area_registry.async_create("mock1") + floor = floor_registry.async_create("mock") + floor_id = floor.floor_id + area1 = area_registry.async_create("mock1", floor_id=floor_id) area2 = area_registry.async_create("mock2") with pytest.raises(ValueError) as e_info: @@ -255,6 +258,8 @@ async def test_update_area_with_name_already_in_use( assert area2.name == "mock2" assert len(area_registry.areas) == 2 + assert area_registry.areas.get_areas_for_floor(floor_id) == [area1] + async def test_update_area_with_normalized_name_already_in_use( area_registry: ar.AreaRegistry, diff --git a/tests/helpers/test_collection.py b/tests/helpers/test_collection.py index f0287218d7fdc9..f564f85ec3b5d0 100644 --- a/tests/helpers/test_collection.py +++ b/tests/helpers/test_collection.py @@ -2,8 +2,10 @@ from __future__ import annotations +from datetime import timedelta import logging +from freezegun.api import FrozenDateTimeFactory import pytest import voluptuous as vol @@ -15,6 +17,7 @@ storage, ) from homeassistant.helpers.typing import ConfigType +from homeassistant.util.dt import utcnow from tests.common import flush_store from tests.typing import WebSocketGenerator @@ -254,6 +257,84 @@ async def test_storage_collection(hass: HomeAssistant) -> None: } +async def test_storage_collection_update_modifiet_at( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + freezer: FrozenDateTimeFactory, +) -> None: + """Test that updating a storage collection will update the modified_at datetime in the entity registry.""" + + entities: dict[str, TestEntity] = {} + + class TestEntity(MockEntity): + """Entity that is config based.""" + + def __init__(self, config: ConfigType) -> None: + """Initialize entity.""" + super().__init__(config) + self._state = "initial" + + @classmethod + def from_storage(cls, config: ConfigType) -> TestEntity: + """Create instance from storage.""" + obj = super().from_storage(config) + entities[obj.unique_id] = obj + return obj + + @property + def state(self) -> str: + """Return state of entity.""" + return self._state + + def set_state(self, value: str) -> None: + """Set value.""" + self._state = value + self.async_write_ha_state() + + store = storage.Store(hass, 1, "test-data") + data = {"id": "mock-1", "name": "Mock 1", "data": 1} + await store.async_save( + { + "items": [ + data, + ] + } + ) + id_manager = collection.IDManager() + ent_comp = entity_component.EntityComponent(_LOGGER, "test", hass) + await ent_comp.async_setup({}) + coll = MockStorageCollection(store, id_manager) + collection.sync_entity_lifecycle(hass, "test", "test", ent_comp, coll, TestEntity) + changes = track_changes(coll) + + await coll.async_load() + assert id_manager.has_id("mock-1") + assert len(changes) == 1 + assert changes[0] == (collection.CHANGE_ADDED, "mock-1", data) + + modified_1 = entity_registry.async_get("test.mock_1").modified_at + assert modified_1 == utcnow() + + freezer.tick(timedelta(minutes=1)) + + updated_item = await coll.async_update_item("mock-1", {"data": 2}) + assert id_manager.has_id("mock-1") + assert updated_item == {"id": "mock-1", "name": "Mock 1", "data": 2} + assert len(changes) == 2 + assert changes[1] == (collection.CHANGE_UPDATED, "mock-1", updated_item) + + modified_2 = entity_registry.async_get("test.mock_1").modified_at + assert modified_2 > modified_1 + assert modified_2 == utcnow() + + freezer.tick(timedelta(minutes=1)) + + entities["mock-1"].set_state("second") + + modified_3 = entity_registry.async_get("test.mock_1").modified_at + assert modified_3 == modified_2 + + async def test_attach_entity_component_collection(hass: HomeAssistant) -> None: """Test attaching collection to entity component.""" ent_comp = entity_component.EntityComponent(_LOGGER, "test", hass) diff --git a/tests/helpers/test_config_validation.py b/tests/helpers/test_config_validation.py index e25d2996370011..0eae0c885817a8 100644 --- a/tests/helpers/test_config_validation.py +++ b/tests/helpers/test_config_validation.py @@ -672,10 +672,12 @@ def test_template(hass: HomeAssistant) -> None: "Hello", "{{ beer }}", "{% if 1 == 1 %}Hello{% else %}World{% endif %}", - # Function added as an extension by Home Assistant + # Function 'expand' added as an extension by Home Assistant "{{ expand('group.foo')|map(attribute='entity_id')|list }}", - # Filter added as an extension by Home Assistant + # Filter 'expand' added as an extension by Home Assistant "{{ ['group.foo']|expand|map(attribute='entity_id')|list }}", + # Non existing function 'no_such_function' is not detected by Jinja2 + "{{ no_such_function('group.foo')|map(attribute='entity_id')|list }}", ) for value in options: schema(value) @@ -701,8 +703,11 @@ async def test_template_no_hass(hass: HomeAssistant) -> None: "Hello", "{{ beer }}", "{% if 1 == 1 %}Hello{% else %}World{% endif %}", - # Function added as an extension by Home Assistant + # Function 'expand' added as an extension by Home Assistant, no error + # because non existing functions are not detected by Jinja2 "{{ expand('group.foo')|map(attribute='entity_id')|list }}", + # Non existing function 'no_such_function' is not detected by Jinja2 + "{{ no_such_function('group.foo')|map(attribute='entity_id')|list }}", ) for value in options: await hass.async_add_executor_job(schema, value) @@ -726,10 +731,12 @@ def test_dynamic_template(hass: HomeAssistant) -> None: options = ( "{{ beer }}", "{% if 1 == 1 %}Hello{% else %}World{% endif %}", - # Function added as an extension by Home Assistant + # Function 'expand' added as an extension by Home Assistant "{{ expand('group.foo')|map(attribute='entity_id')|list }}", - # Filter added as an extension by Home Assistant + # Filter 'expand' added as an extension by Home Assistant "{{ ['group.foo']|expand|map(attribute='entity_id')|list }}", + # Non existing function 'no_such_function' is not detected by Jinja2 + "{{ no_such_function('group.foo')|map(attribute='entity_id')|list }}", ) for value in options: schema(value) @@ -755,8 +762,11 @@ async def test_dynamic_template_no_hass(hass: HomeAssistant) -> None: options = ( "{{ beer }}", "{% if 1 == 1 %}Hello{% else %}World{% endif %}", - # Function added as an extension by Home Assistant + # Function 'expand' added as an extension by Home Assistant, no error + # because non existing functions are not detected by Jinja2 "{{ expand('group.foo')|map(attribute='entity_id')|list }}", + # Non existing function 'no_such_function' is not detected by Jinja2 + "{{ no_such_function('group.foo')|map(attribute='entity_id')|list }}", ) for value in options: await hass.async_add_executor_job(schema, value) @@ -1882,3 +1892,27 @@ async def test_nested_trigger_list_extra() -> None: validated_triggers = TRIGGER_SCHEMA(trigger_config) assert validated_triggers == trigger_config + + +async def test_is_entity_service_schema( + hass: HomeAssistant, +) -> None: + """Test cv.is_entity_service_schema.""" + for schema in ( + vol.Schema({"some": str}), + vol.All(vol.Schema({"some": str})), + vol.Any(vol.Schema({"some": str})), + vol.Any(cv.make_entity_service_schema({"some": str})), + ): + assert cv.is_entity_service_schema(schema) is False + + for schema in ( + cv.make_entity_service_schema({"some": str}), + vol.Schema(cv.make_entity_service_schema({"some": str})), + vol.Schema(vol.All(cv.make_entity_service_schema({"some": str}))), + vol.Schema(vol.Schema(cv.make_entity_service_schema({"some": str}))), + vol.All(cv.make_entity_service_schema({"some": str})), + vol.All(vol.All(cv.make_entity_service_schema({"some": str}))), + vol.All(vol.Schema(cv.make_entity_service_schema({"some": str}))), + ): + assert cv.is_entity_service_schema(schema) is True diff --git a/tests/helpers/test_entity_component.py b/tests/helpers/test_entity_component.py index 5ce0292c2ec5fa..9723b91eb9adce 100644 --- a/tests/helpers/test_entity_component.py +++ b/tests/helpers/test_entity_component.py @@ -23,7 +23,7 @@ callback, ) from homeassistant.exceptions import HomeAssistantError, PlatformNotReady -from homeassistant.helpers import discovery +from homeassistant.helpers import config_validation as cv, discovery from homeassistant.helpers.entity_component import EntityComponent, async_update_entity from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType @@ -557,30 +557,32 @@ def appender(**kwargs): async def test_register_entity_service_non_entity_service_schema( - hass: HomeAssistant, + hass: HomeAssistant, caplog: pytest.LogCaptureFixture ) -> None: - """Test attempting to register a service with an incomplete schema.""" + """Test attempting to register a service with a non entity service schema.""" component = EntityComponent(_LOGGER, DOMAIN, hass) + expected_message = "registers an entity service with a non entity service schema" - with pytest.raises( - HomeAssistantError, - match=( - "The schema does not include all required keys: entity_id, device_id, area_id, " - "floor_id, label_id" - ), + for idx, schema in enumerate( + ( + vol.Schema({"some": str}), + vol.All(vol.Schema({"some": str})), + vol.Any(vol.Schema({"some": str})), + ) ): - component.async_register_entity_service( - "hello", vol.Schema({"some": str}), Mock() + component.async_register_entity_service(f"hello_{idx}", schema, Mock()) + assert expected_message in caplog.text + caplog.clear() + + for idx, schema in enumerate( + ( + cv.make_entity_service_schema({"some": str}), + vol.Schema(cv.make_entity_service_schema({"some": str})), + vol.All(cv.make_entity_service_schema({"some": str})), ) - - # The check currently does not recurse into vol.All or vol.Any allowing these - # non-compliant schemas to pass - component.async_register_entity_service( - "hello", vol.All(vol.Schema({"some": str})), Mock() - ) - component.async_register_entity_service( - "hello", vol.Any(vol.Schema({"some": str})), Mock() - ) + ): + component.async_register_entity_service(f"test_service_{idx}", schema, Mock()) + assert expected_message not in caplog.text async def test_register_entity_service_response_data(hass: HomeAssistant) -> None: diff --git a/tests/helpers/test_entity_platform.py b/tests/helpers/test_entity_platform.py index 2cc3348626cbd0..db83819085bf2c 100644 --- a/tests/helpers/test_entity_platform.py +++ b/tests/helpers/test_entity_platform.py @@ -23,6 +23,7 @@ from homeassistant.exceptions import HomeAssistantError, PlatformNotReady from homeassistant.helpers import ( area_registry as ar, + config_validation as cv, device_registry as dr, entity_platform, entity_registry as er, @@ -1810,33 +1811,36 @@ def handle_service(entity, *_): async def test_register_entity_service_non_entity_service_schema( - hass: HomeAssistant, + hass: HomeAssistant, caplog: pytest.LogCaptureFixture ) -> None: - """Test attempting to register a service with an incomplete schema.""" + """Test attempting to register a service with a non entity service schema.""" entity_platform = MockEntityPlatform( hass, domain="mock_integration", platform_name="mock_platform", platform=None ) + expected_message = "registers an entity service with a non entity service schema" - with pytest.raises( - HomeAssistantError, - match=( - "The schema does not include all required keys: entity_id, device_id, area_id, " - "floor_id, label_id" - ), + for idx, schema in enumerate( + ( + vol.Schema({"some": str}), + vol.All(vol.Schema({"some": str})), + vol.Any(vol.Schema({"some": str})), + ) + ): + entity_platform.async_register_entity_service(f"hello_{idx}", schema, Mock()) + assert expected_message in caplog.text + caplog.clear() + + for idx, schema in enumerate( + ( + cv.make_entity_service_schema({"some": str}), + vol.Schema(cv.make_entity_service_schema({"some": str})), + vol.All(cv.make_entity_service_schema({"some": str})), + ) ): entity_platform.async_register_entity_service( - "hello", - vol.Schema({"some": str}), - Mock(), + f"test_service_{idx}", schema, Mock() ) - # The check currently does not recurse into vol.All or vol.Any allowing these - # non-compliant schemas to pass - entity_platform.async_register_entity_service( - "hello", vol.All(vol.Schema({"some": str})), Mock() - ) - entity_platform.async_register_entity_service( - "hello", vol.Any(vol.Schema({"some": str})), Mock() - ) + assert expected_message not in caplog.text @pytest.mark.parametrize("update_before_add", [True, False]) diff --git a/tests/helpers/test_event.py b/tests/helpers/test_event.py index 6c71f1d8a7c23d..19f1ef5bb761d0 100644 --- a/tests/helpers/test_event.py +++ b/tests/helpers/test_event.py @@ -4938,3 +4938,43 @@ def single_run_callback(event: Event[EventStateReportedData]) -> None: await hass.async_block_till_done() assert len(tracker_called) == 2 unsub() + + +async def test_async_track_template_no_hass_deprecated( + hass: HomeAssistant, caplog: pytest.LogCaptureFixture +) -> None: + """Test async_track_template with a template without hass is deprecated.""" + message = ( + "Detected code that calls async_track_template_result with template without " + "hass, which will stop working in HA Core 2025.10. Please report this issue." + ) + + async_track_template(hass, Template("blah"), lambda x, y, z: None) + assert message in caplog.text + caplog.clear() + + async_track_template(hass, Template("blah", hass), lambda x, y, z: None) + assert message not in caplog.text + caplog.clear() + + +async def test_async_track_template_result_no_hass_deprecated( + hass: HomeAssistant, caplog: pytest.LogCaptureFixture +) -> None: + """Test async_track_template_result with a template without hass is deprecated.""" + message = ( + "Detected code that calls async_track_template_result with template without " + "hass, which will stop working in HA Core 2025.10. Please report this issue." + ) + + async_track_template_result( + hass, [TrackTemplate(Template("blah"), None)], lambda x, y, z: None + ) + assert message in caplog.text + caplog.clear() + + async_track_template_result( + hass, [TrackTemplate(Template("blah", hass), None)], lambda x, y, z: None + ) + assert message not in caplog.text + caplog.clear() diff --git a/tests/helpers/test_icon.py b/tests/helpers/test_icon.py index 732f9971ac0936..e0dc89f53223fe 100644 --- a/tests/helpers/test_icon.py +++ b/tests/helpers/test_icon.py @@ -101,7 +101,7 @@ async def test_get_icons(hass: HomeAssistant) -> None: # Test services icons are available icons = await icon.async_get_icons(hass, "services") assert len(icons) == 1 - assert icons["switch"]["turn_off"] == "mdi:toggle-switch-variant-off" + assert icons["switch"]["turn_off"] == {"service": "mdi:toggle-switch-variant-off"} # Ensure icons file for platform isn't loaded, as that isn't supported icons = await icon.async_get_icons(hass, "entity") @@ -126,7 +126,7 @@ async def test_get_icons(hass: HomeAssistant) -> None: icons = await icon.async_get_icons(hass, "services") assert len(icons) == 2 - assert icons["test_package"]["enable_god_mode"] == "mdi:shield" + assert icons["test_package"]["enable_god_mode"] == {"service": "mdi:shield"} # Load another one hass.config.components.add("test_embedded") diff --git a/tests/helpers/test_service.py b/tests/helpers/test_service.py index 81cc189e1afe65..efe24fe4b8e4c7 100644 --- a/tests/helpers/test_service.py +++ b/tests/helpers/test_service.py @@ -39,7 +39,6 @@ device_registry as dr, entity_registry as er, service, - template, ) import homeassistant.helpers.config_validation as cv from homeassistant.loader import async_get_integration @@ -565,9 +564,6 @@ async def test_not_mutate_input(hass: HomeAssistant) -> None: config = cv.SERVICE_SCHEMA(config) orig = cv.SERVICE_SCHEMA(orig) - # Only change after call is each template getting hass attached - template.attach(hass, orig) - await service.async_call_from_config(hass, config, validate_config=False) assert orig == config diff --git a/tests/helpers/test_template.py b/tests/helpers/test_template.py index 3123c01f50026c..339b372f137356 100644 --- a/tests/helpers/test_template.py +++ b/tests/helpers/test_template.py @@ -15,6 +15,7 @@ from freezegun import freeze_time import orjson import pytest +from syrupy import SnapshotAssertion import voluptuous as vol from homeassistant import config_entries @@ -6236,3 +6237,330 @@ async def test_template_thread_safety_checks(hass: HomeAssistant) -> None: await hass.async_add_executor_job(template_obj.async_render_to_info) assert template_obj.async_render_to_info().result() == 23 + + +@pytest.mark.parametrize( + ("cola", "colb", "expected"), + [ + ([1, 2], [3, 4], [(1, 3), (2, 4)]), + ([1, 2], [3, 4, 5], [(1, 3), (2, 4)]), + ([1, 2, 3, 4], [3, 4], [(1, 3), (2, 4)]), + ], +) +def test_zip(hass: HomeAssistant, cola, colb, expected) -> None: + """Test zip.""" + assert ( + template.Template("{{ zip(cola, colb) | list }}", hass).async_render( + {"cola": cola, "colb": colb} + ) + == expected + ) + assert ( + template.Template( + "[{% for a, b in zip(cola, colb) %}({{a}}, {{b}}), {% endfor %}]", hass + ).async_render({"cola": cola, "colb": colb}) + == expected + ) + + +@pytest.mark.parametrize( + ("col", "expected"), + [ + ([(1, 3), (2, 4)], [(1, 2), (3, 4)]), + (["ax", "by", "cz"], [("a", "b", "c"), ("x", "y", "z")]), + ], +) +def test_unzip(hass: HomeAssistant, col, expected) -> None: + """Test unzipping using zip.""" + assert ( + template.Template("{{ zip(*col) | list }}", hass).async_render({"col": col}) + == expected + ) + assert ( + template.Template( + "{% set a, b = zip(*col) %}[{{a}}, {{b}}]", hass + ).async_render({"col": col}) + == expected + ) + + +def test_template_output_exceeds_maximum_size(hass: HomeAssistant) -> None: + """Test template output exceeds maximum size.""" + tpl = template.Template("{{ 'a' * 1024 * 257 }}", hass) + with pytest.raises(TemplateError): + tpl.async_render() + + +@pytest.mark.parametrize( + ("service_response"), + [ + { + "calendar.sports": { + "events": [ + { + "start": "2024-02-27T17:00:00-06:00", + "end": "2024-02-27T18:00:00-06:00", + "summary": "Basketball vs. Rockets", + "description": "", + } + ] + }, + "calendar.local_furry_events": {"events": []}, + "calendar.yap_house_schedules": { + "events": [ + { + "start": "2024-02-26T08:00:00-06:00", + "end": "2024-02-26T09:00:00-06:00", + "summary": "Dr. Appt", + "description": "", + }, + { + "start": "2024-02-28T20:00:00-06:00", + "end": "2024-02-28T21:00:00-06:00", + "summary": "Bake a cake", + "description": "something good", + }, + ] + }, + }, + { + "binary_sensor.workday": {"workday": True}, + "binary_sensor.workday2": {"workday": False}, + }, + { + "weather.smhi_home": { + "forecast": [ + { + "datetime": "2024-03-31T16:00:00", + "condition": "cloudy", + "wind_bearing": 79, + "cloud_coverage": 100, + "temperature": 10, + "templow": 4, + "pressure": 998, + "wind_gust_speed": 21.6, + "wind_speed": 11.88, + "precipitation": 0.2, + "humidity": 87, + }, + { + "datetime": "2024-04-01T12:00:00", + "condition": "rainy", + "wind_bearing": 17, + "cloud_coverage": 100, + "temperature": 6, + "templow": 1, + "pressure": 999, + "wind_gust_speed": 20.52, + "wind_speed": 8.64, + "precipitation": 2.2, + "humidity": 88, + }, + { + "datetime": "2024-04-02T12:00:00", + "condition": "cloudy", + "wind_bearing": 17, + "cloud_coverage": 100, + "temperature": 0, + "templow": -3, + "pressure": 1003, + "wind_gust_speed": 57.24, + "wind_speed": 30.6, + "precipitation": 1.3, + "humidity": 71, + }, + ] + }, + "weather.forecast_home": { + "forecast": [ + { + "condition": "cloudy", + "precipitation_probability": 6.6, + "datetime": "2024-03-31T10:00:00+00:00", + "wind_bearing": 71.8, + "temperature": 10.9, + "templow": 6.5, + "wind_gust_speed": 24.1, + "wind_speed": 13.7, + "precipitation": 0, + "humidity": 71, + }, + { + "condition": "cloudy", + "precipitation_probability": 8, + "datetime": "2024-04-01T10:00:00+00:00", + "wind_bearing": 350.6, + "temperature": 10.2, + "templow": 3.4, + "wind_gust_speed": 38.2, + "wind_speed": 21.6, + "precipitation": 0, + "humidity": 79, + }, + { + "condition": "snowy", + "precipitation_probability": 67.4, + "datetime": "2024-04-02T10:00:00+00:00", + "wind_bearing": 24.5, + "temperature": 3, + "templow": 0, + "wind_gust_speed": 64.8, + "wind_speed": 37.4, + "precipitation": 2.3, + "humidity": 77, + }, + ] + }, + }, + { + "vacuum.deebot_n8_plus_1": { + "payloadType": "j", + "resp": { + "body": { + "msg": "ok", + } + }, + "header": { + "ver": "0.0.1", + }, + }, + "vacuum.deebot_n8_plus_2": { + "payloadType": "j", + "resp": { + "body": { + "msg": "ok", + } + }, + "header": { + "ver": "0.0.1", + }, + }, + }, + ], + ids=["calendar", "workday", "weather", "vacuum"], +) +async def test_merge_response( + hass: HomeAssistant, + service_response: dict, + snapshot: SnapshotAssertion, +) -> None: + """Test the merge_response function/filter.""" + + _template = "{{ merge_response(" + str(service_response) + ") }}" + + tpl = template.Template(_template, hass) + assert service_response == snapshot(name="a_response") + assert tpl.async_render() == snapshot(name="b_rendered") + + +async def test_merge_response_with_entity_id_in_response( + hass: HomeAssistant, + snapshot: SnapshotAssertion, +) -> None: + """Test the merge_response function/filter with empty lists.""" + + service_response = { + "test.response": {"some_key": True, "entity_id": "test.response"}, + "test.response2": {"some_key": False, "entity_id": "test.response2"}, + } + _template = "{{ merge_response(" + str(service_response) + ") }}" + with pytest.raises( + TemplateError, + match="ValueError: Response dictionary already contains key 'entity_id'", + ): + template.Template(_template, hass).async_render() + + service_response = { + "test.response": { + "happening": [ + { + "start": "2024-02-27T17:00:00-06:00", + "end": "2024-02-27T18:00:00-06:00", + "summary": "Magic day", + "entity_id": "test.response", + } + ] + } + } + _template = "{{ merge_response(" + str(service_response) + ") }}" + with pytest.raises( + TemplateError, + match="ValueError: Response dictionary already contains key 'entity_id'", + ): + template.Template(_template, hass).async_render() + + +async def test_merge_response_with_empty_response( + hass: HomeAssistant, + snapshot: SnapshotAssertion, +) -> None: + """Test the merge_response function/filter with empty lists.""" + + service_response = { + "calendar.sports": {"events": []}, + "calendar.local_furry_events": {"events": []}, + "calendar.yap_house_schedules": {"events": []}, + } + _template = "{{ merge_response(" + str(service_response) + ") }}" + tpl = template.Template(_template, hass) + assert service_response == snapshot(name="a_response") + assert tpl.async_render() == snapshot(name="b_rendered") + + +async def test_response_empty_dict( + hass: HomeAssistant, + snapshot: SnapshotAssertion, +) -> None: + """Test the merge_response function/filter with empty dict.""" + + service_response = {} + _template = "{{ merge_response(" + str(service_response) + ") }}" + tpl = template.Template(_template, hass) + assert tpl.async_render() == [] + + +async def test_response_incorrect_value( + hass: HomeAssistant, + snapshot: SnapshotAssertion, +) -> None: + """Test the merge_response function/filter with incorrect response.""" + + service_response = "incorrect" + _template = "{{ merge_response(" + str(service_response) + ") }}" + with pytest.raises(TemplateError, match="TypeError: Response is not a dictionary"): + template.Template(_template, hass).async_render() + + +async def test_merge_response_with_incorrect_response(hass: HomeAssistant) -> None: + """Test the merge_response function/filter with empty response should raise.""" + + service_response = {"calendar.sports": []} + _template = "{{ merge_response(" + str(service_response) + ") }}" + tpl = template.Template(_template, hass) + with pytest.raises(TemplateError, match="TypeError: Response is not a dictionary"): + tpl.async_render() + + service_response = { + "binary_sensor.workday": [], + } + _template = "{{ merge_response(" + str(service_response) + ") }}" + tpl = template.Template(_template, hass) + with pytest.raises(TemplateError, match="TypeError: Response is not a dictionary"): + tpl.async_render() + + +def test_warn_no_hass(hass: HomeAssistant, caplog: pytest.LogCaptureFixture) -> None: + """Test deprecation warning when instantiating Template without hass.""" + + message = "Detected code that creates a template object without passing hass" + template.Template("blah") + assert message in caplog.text + caplog.clear() + + template.Template("blah", None) + assert message in caplog.text + caplog.clear() + + template.Template("blah", hass) + assert message not in caplog.text + caplog.clear() diff --git a/tests/test_config_entries.py b/tests/test_config_entries.py index dccebff13e5eaf..d01febd69042c5 100644 --- a/tests/test_config_entries.py +++ b/tests/test_config_entries.py @@ -5437,13 +5437,8 @@ async def test_report_direct_mutation_of_config_entry( entry = MockConfigEntry(domain="test") entry.add_to_hass(hass) - setattr(entry, field, "new_value") - - assert ( - f'Detected code that sets "{field}" directly to update a config entry. ' - "This is deprecated and will stop working in Home Assistant 2024.9, " - "it should be updated to use async_update_entry instead. Please report this issue." - ) in caplog.text + with pytest.raises(AttributeError): + setattr(entry, field, "new_value") async def test_updating_non_added_entry_raises(hass: HomeAssistant) -> None: @@ -6001,3 +5996,44 @@ async def test_migration_from_1_2( ] }, } + + +async def test_async_loaded_entries( + hass: HomeAssistant, manager: config_entries.ConfigEntries +) -> None: + """Test that we can get loaded config entries.""" + entry1 = MockConfigEntry(domain="comp") + entry1.add_to_hass(hass) + entry2 = MockConfigEntry(domain="comp", source=config_entries.SOURCE_IGNORE) + entry2.add_to_hass(hass) + entry3 = MockConfigEntry( + domain="comp", disabled_by=config_entries.ConfigEntryDisabler.USER + ) + entry3.add_to_hass(hass) + + mock_setup = AsyncMock(return_value=True) + mock_setup_entry = AsyncMock(return_value=True) + mock_unload_entry = AsyncMock(return_value=True) + + mock_integration( + hass, + MockModule( + "comp", + async_setup=mock_setup, + async_setup_entry=mock_setup_entry, + async_unload_entry=mock_unload_entry, + ), + ) + mock_platform(hass, "comp.config_flow", None) + + assert hass.config_entries.async_loaded_entries("comp") == [] + + assert await manager.async_setup(entry1.entry_id) + assert not await manager.async_setup(entry2.entry_id) + assert not await manager.async_setup(entry3.entry_id) + + assert hass.config_entries.async_loaded_entries("comp") == [entry1] + + assert await hass.config_entries.async_unload(entry1.entry_id) + + assert hass.config_entries.async_loaded_entries("comp") == [] diff --git a/tests/test_data_entry_flow.py b/tests/test_data_entry_flow.py index 967b2565206c38..01b6a530105b7f 100644 --- a/tests/test_data_entry_flow.py +++ b/tests/test_data_entry_flow.py @@ -1098,3 +1098,27 @@ def test_section_in_serializer() -> None: ], "type": "expandable", } + + +def test_nested_section_in_serializer() -> None: + """Test section with custom_serializer.""" + with pytest.raises( + ValueError, match="Nesting expandable sections is not supported" + ): + cv.custom_serializer( + data_entry_flow.section( + vol.Schema( + { + vol.Required("section_1"): data_entry_flow.section( + vol.Schema( + { + vol.Optional("option_1", default=False): bool, + vol.Required("option_2"): int, + } + ) + ) + } + ), + {"collapsed": False}, + ) + ) diff --git a/tests/util/yaml/test_init.py b/tests/util/yaml/test_init.py index ece65504ed65c4..dbd7f1d2e99b13 100644 --- a/tests/util/yaml/test_init.py +++ b/tests/util/yaml/test_init.py @@ -726,3 +726,44 @@ def test_load_yaml_dict_fail() -> None: """Test item without a key.""" with pytest.raises(yaml_loader.YamlTypeError): yaml_loader.load_yaml_dict(YAML_CONFIG_FILE) + + +@pytest.mark.parametrize( + "tag", + [ + "!include", + "!include_dir_named", + "!include_dir_merge_named", + "!include_dir_list", + "!include_dir_merge_list", + ], +) +@pytest.mark.usefixtures("try_both_loaders") +def test_include_without_parameter(tag: str) -> None: + """Test include extensions without parameters.""" + with ( + io.StringIO(f"key: {tag}") as file, + pytest.raises(HomeAssistantError, match=f"{tag} needs an argument"), + ): + yaml_loader.parse_yaml(file) + + +@pytest.mark.parametrize( + ("open_exception", "load_yaml_exception"), + [ + (FileNotFoundError, OSError), + (NotADirectoryError, HomeAssistantError), + (PermissionError, HomeAssistantError), + ], +) +@pytest.mark.usefixtures("try_both_loaders") +def test_load_yaml_wrap_oserror( + open_exception: Exception, + load_yaml_exception: Exception, +) -> None: + """Test load_yaml wraps OSError in HomeAssistantError.""" + with ( + patch("homeassistant.util.yaml.loader.open", side_effect=open_exception), + pytest.raises(load_yaml_exception), + ): + yaml_loader.load_yaml("bla")