Skip to content

Add native unit types for weather entities#59533

Merged
emontnemery merged 17 commits intohome-assistant:devfrom
rianadon:weather-unit-conversion
Nov 29, 2021
Merged

Add native unit types for weather entities#59533
emontnemery merged 17 commits intohome-assistant:devfrom
rianadon:weather-unit-conversion

Conversation

@rianadon
Copy link
Copy Markdown
Contributor

@rianadon rianadon commented Nov 11, 2021

Proposed change

I'm attempting to port @emontnemery's addition of native_units to SensorEntity to the WeatherEntity class.

This will allow integrations publishing weather data results to convert their data to local units, without needing to write unit conversion routines in each integration. This can also be used as a base to implement unit conversion for weather templates, like in #48641.

It is very unlikely this change should break custom components. However, I did find one component (met_office) that already defined a visibility_unit method but did not return floats in its visibility method. If any other components pull similar shenanigans, they will break.

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)
  • 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:

Checklist

  • 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
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

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 link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 🥈 Silver
  • 🥇 Gold
  • 🏆 Platinum

To help with the load of incoming pull requests:

@probot-home-assistant
Copy link
Copy Markdown

Hey there @fabaff, mind taking a look at this pull request as it has been labeled with an integration (weather) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

@emontnemery
Copy link
Copy Markdown
Contributor

emontnemery commented Nov 11, 2021

I think this looks good!

To prevent the @final decorator from breaking CI, don't add it until all existing integrations are updated.

@emontnemery
Copy link
Copy Markdown
Contributor

Tests are needed too, I just noticed it's missing from sensor tests.
Maybe do something like this: #59546

@rianadon
Copy link
Copy Markdown
Contributor Author

rianadon commented Nov 11, 2021

I've added a bit more. There are tests, and also as a result of cleaning up unit types the precision of temperature values is no longer set by the integration's units but rather the user's units. So I updated the climacell test to add the precision necessary for Celsius values.

Edit: I'm going to add the precision change in a different PR.

@rianadon
Copy link
Copy Markdown
Contributor Author

I'm changing the names of the new methods to no longer have the native_ prefixes in front of them to be more consistent with what we have already. Adding a separate native_temperature also made it difficult to decide whether to use the integration's overridden temperature method or to use the default native_temperature implementation.

Plus, everything gets unit-converted anyways in state_attributes.

The vibility values given by metoffice are formatted into strings,
which means they can't automatically be converted.
@rianadon
Copy link
Copy Markdown
Contributor Author

rianadon commented Nov 19, 2021

The code should be ready to go now!

Copy link
Copy Markdown
Contributor

@emontnemery emontnemery left a comment

Choose a reason for hiding this comment

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

Thanks, @rianadon 👍

if (wind_speed := self.wind_speed) is not None:
if (unit := self.wind_speed_unit) is not None:
wind_speed = round(
self.hass.config.units.wind_speed(wind_speed, unit),
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.

Why is hass config doing the unit conversion?

wind_seed should be imported directly from util.unit_system instead.

The same applies to all other unit conversions happening here.

@home-assistant home-assistant unlocked this conversation Jan 7, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Jan 8, 2022
@home-assistant home-assistant unlocked this conversation Jan 10, 2022
@frenck
Copy link
Copy Markdown
Member

frenck commented Jan 10, 2022

This PR is causing massive breaking changes.

While I think the idea is good, I think the way it is set up is not fitting nor correct.

For that reason:

I think it is cool to support unit conversion on an entity or device level in the future, but we cannot rely on core configuration for e.g., unit of mass or pressure. Which unit to use for those cases heavily relies on the use case.

@github-actions github-actions bot locked and limited conversation to collaborators Jan 11, 2022
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.

5 participants