Skip to content

Water_heater: add temperature input-validation - #178579

Closed
bouwew wants to merge 13 commits into
home-assistant:devfrom
plugwise:wh-input-checks
Closed

Water_heater: add temperature input-validation#178579
bouwew wants to merge 13 commits into
home-assistant:devfrom
plugwise:wh-input-checks

Conversation

@bouwew

@bouwew bouwew commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Proposed change

Add input-validation to the async_set_temperature() function of the water_heater platform.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:
  • Link to developer documentation pull request:
  • Link to frontend pull request:

Checklist

  • I understand the code I am submitting and can explain how it works.
  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies a diff between library versions and ideally a link to the changelog/release notes is added to the PR description.

To help with the load of incoming pull requests:

@home-assistant

home-assistant Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration (water_heater) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of water_heater can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant mark-draft Mark the pull request as draft.
  • @home-assistant ready-for-review Remove the draft status from the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign water_heater Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant update-branch Update the pull request branch with the base branch.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) on the pull request.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds range validation to water-heater temperature service calls.

Changes:

  • Validates converted temperatures against entity limits.
  • Adds a translated validation error.
  • Tests native and converted temperature scenarios.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
homeassistant/components/water_heater/__init__.py Adds temperature range validation.
homeassistant/components/water_heater/strings.json Adds the validation error message.
tests/components/water_heater/test_init.py Adds out-of-range service tests.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread homeassistant/components/water_heater/__init__.py Outdated
Comment thread homeassistant/components/water_heater/strings.json Outdated
Comment thread tests/components/water_heater/test_init.py Outdated
Copilot AI review requested due to automatic review settings August 9, 2026 06:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Suppressed comments (2)

homeassistant/components/water_heater/strings.json:134

  • Include the temperature unit in this validation message. When Home Assistant and the entity use different units, the reported values are converted to the entity unit (for example, the new test submits 40 °C but reports 104.0, 110, and 140), so the current message leaves users unable to tell that those values are Fahrenheit.
      "message": "Temperature {check_temp} is not valid for {entity_id}. Value must be between {min_temp} and {max_temp}."

tests/components/water_heater/test_init.py:140

  • Parameterize these two cases and share the entity/platform setup. This test duplicates almost the entire preceding test even though only the entity unit, input, and expected converted value differ, making future service-test changes needlessly error-prone.
async def test_set_temperature_raises_out_of_range_with_conversion(
    hass: HomeAssistant, config_flow_fixture: None
) -> None:
    """Test setting temperature outside of range with unit conversion raises validation error."""

Copilot AI review requested due to automatic review settings August 9, 2026 06:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Suppressed comments (4)

tests/components/water_heater/test_init.py:184

  • Exercise the upper-bound branch in the conversion case. The current 40 °C input converts to 104 °F, so both new tests only cover values below the minimum and a regression in the maximum check would pass unnoticed.
    """Test if async turn_off calls sync turn_off."""

tests/components/water_heater/test_init.py:141

  • Parameterize the two out-of-range cases and share their integration setup. These tests duplicate nearly the entire setup and assertion body, differing only in unit, input, and expected converted value.
            "set_temperature",
            data,
            blocking=True,
        )

homeassistant/components/water_heater/init.py:447

  • Skip range validation when an entity has no numeric bounds. tado/water_heater.py:111-125,159-161,196-204 can expose the target-temperature feature while both bounds are None; comparing those values below raises TypeError and prevents a previously valid service call.
    min_temp = entity.min_temp
    max_temp = entity.max_temp

homeassistant/components/water_heater/init.py:435

  • Validate against the same rounded system-unit limits exposed to callers, and clamp boundary values before forwarding them. For a Fahrenheit entity using the default 110 °F minimum on a Celsius system, capability_attributes advertises 43.3 °C, but converting that input here produces 109.94 °F and the subsequent check rejects the advertised minimum.
            check_temp = TemperatureConverter.convert(
                temp, hass.config.units.temperature_unit, entity.temperature_unit
            )

Copilot AI review requested due to automatic review settings August 9, 2026 06:52

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Suppressed comments (1)

homeassistant/components/water_heater/init.py:447

  • Accept displayed boundary values when conversion rounding differs. Capability attributes round via display_temp; for example, a 110 °F minimum is exposed as 43.3 °C, but converting that submitted value back yields 109.94 °F, so this check rejects the UI's advertised minimum. Compare using the displayed bounds or clamp values that equal a displayed boundary before calling the entity, and cover this conversion case in the test.
            if not min_temp <= check_temp <= max_temp:

Copilot AI review requested due to automatic review settings August 9, 2026 07:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Suppressed comments (1)

homeassistant/components/water_heater/init.py:434

  • Rely on the entity base class's non-optional temperature bounds. WaterHeaterEntity.min_temp and max_temp return float and provide defaults when attributes are absent (__init__.py:386-402), so these None branches mask an invalid entity implementation and allow range validation to be skipped; remove the nullable handling and the corresponding no-bounds test.
    min_temp_displayed = (
        show_temp(hass, min_temp, entity.temperature_unit, entity.precision)
        if min_temp is not None
        else None
    )

Comment thread tests/components/water_heater/test_init.py Outdated
Copilot AI review requested due to automatic review settings August 9, 2026 07:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Suppressed comments (1)

homeassistant/components/water_heater/init.py:454

  • Add coverage for the displayed maximum boundary. The new maximum-equality branch is not exercised—the boundary test only verifies the minimum—so the conversion/clamping behavior at the advertised maximum could regress unnoticed; parameterize that test for both boundaries.
            elif (
                max_temp_displayed is not None
                and temp == max_temp_displayed
                and max_temp is not None
            ):
                check_temp = max_temp

Copilot AI review requested due to automatic review settings August 9, 2026 09:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Suppressed comments (2)

tests/components/water_heater/test_init.py:247

  • Remove this unsupported None override and the associated no-bounds path. WaterHeaterEntity.min_temp and max_temp are declared to return float and already provide default bounds, so these overrides violate the entity API (and static type checking) instead of modeling a valid water-heater entity.

async def test_set_temperature_with_no_bounds_skips_validation(
    hass: HomeAssistant, config_flow_fixture: None
) -> None:
    """Test set temperature does not validate when bounds are not available."""

    class NoBoundsWaterHeater(MockWaterHeaterEntity):

homeassistant/components/water_heater/init.py:454

  • Add a test for accepting the displayed maximum boundary. The new maximum-specific correction branch is not exercised by the minimum-boundary test, so a regression here could reject the exact max_temp advertised to callers.
            elif (
                max_temp_displayed is not None
                and temp == max_temp_displayed
                and max_temp is not None
            ):
                check_temp = max_temp

Copilot AI review requested due to automatic review settings August 9, 2026 10:02
Copilot AI review requested due to automatic review settings August 9, 2026 11:20

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

Suppressed comments (1)

homeassistant/components/water_heater/init.py:472

  • Keep the Airzone API-error tests inside the advertised temperature range. tests/components/airzone/test_water_heater.py:226 sends 80 above its 75 maximum, and tests/components/airzone_cloud/test_water_heater.py:180 sends 80 above its 60 maximum, so this new check raises ServiceValidationError (a HomeAssistantError) before either mocked API is called; both tests therefore pass without exercising the failure paths they claim to cover.
            if min_temp is not None and max_temp is not None:
                if not min_temp <= check_temp <= max_temp:
                    raise ServiceValidationError(

@bouwew
bouwew marked this pull request as ready for review August 13, 2026 11:59
@bouwew
bouwew requested review from a team, bdraco, emontnemery and jbouwh as code owners August 13, 2026 11:59
@bouwew

bouwew commented Aug 13, 2026

Copy link
Copy Markdown
Contributor Author

I've started on this PR thinking it could be useful.
If not, let me know and I'll close this PR.

Copilot AI review requested due to automatic review settings August 13, 2026 12:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

)
if min_temp is not None and max_temp is not None:
if not min_temp <= check_temp <= max_temp:
raise ServiceValidationError(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is changing our entity model. Before we can review this PR there needs to be approval in a discussion in our architecture repository.

https://github.com/home-assistant/architecture/discussions

https://developers.home-assistant.io/docs/core/entity#changing-the-entity-model

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok understood, Joost told me something similar :)

@MartinHjelmare

Copy link
Copy Markdown
Member

I'll go ahead and close here for now, since we don't keep pull requests open that are waiting for an architecture discussion.

Thanks for your willingness to contribute!

@github-actions github-actions Bot locked and limited conversation to collaborators Aug 14, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants