Move temperature conversions to sensor base class (1/8)#48261
Move temperature conversions to sensor base class (1/8)#48261emontnemery merged 34 commits intohome-assistant:devfrom
Conversation
homeassistant/helpers/entity.py
Outdated
| if not self._temperature_conversion_reported: | ||
| self._temperature_conversion_reported = True | ||
| _LOGGER.warning( | ||
| "Entity %s reports a temperature, but it doesn't extend " |
There was a problem hiding this comment.
Make sure you document this on the dev blog.
homeassistant/helpers/entity.py
Outdated
| await self.async_turn_on(**kwargs) | ||
|
|
||
|
|
||
| class MeasurableUnitEntity(Entity): |
There was a problem hiding this comment.
Is the plan to create a measurable entity that can handle any unit conversion or should we consider mixins per unit conversion (ie TemperatureEntity)
There was a problem hiding this comment.
I think the former, it's fairly common pattern for integrations to have a shared class for all sensors, then just set the attributes, including unit_of_measurement, based on some input from the lib.
If we want to specialize like this, those integrations would have to be refactored.
There was a problem hiding this comment.
I agree. Besides temperature, I think we should only convert units based on device class. That will allow us to have a look up table for the conversion to apply and a clean implementation in a single class.
There was a problem hiding this comment.
we should only convert units based on device class
I actually implemented it like that first, but there are two issues:
- We will need to be backwards compatible for some time to handle integrations which don't set
device_ classon their sensors. - How to handle integrations such as
input_numberwhich allow configuring aunit_of_measurementbut not adevice_class?
The first issue is not a problem, but I'm not sure about the second one.
There was a problem hiding this comment.
If the unit of measurement is configured by the user we probably shouldn't convert it.
There was a problem hiding this comment.
OK, that makes sense for input_number.
Not doing the conversion could be considered a breaking change since we currently do convert all temperatures, but maybe it would be for the better?
unit_of_measurement is also used by base components geo_location (for distance) and air_quality (concentrations, I think).
There was a problem hiding this comment.
I really don't think device class and unit of measurement should be coupled. I think in the long run it would make more sense to device a measurement class/type.
We are more and more mixing up the object and the measurement it provides. For example, temperature vs garage door.
In the long run, this is going to bite.
There was a problem hiding this comment.
device class and unit of measurement need to be coupled for blueprints and to export it to Homekit/Google/Alexa. It's not possible to make code that can work with everything. If it's a device class, we can categorize the entity and know how to deal with it.
homeassistant/helpers/entity.py
Outdated
| """An abstract class for entities which state is a measurable unit.""" | ||
|
|
||
| @property | ||
| def native_value(self) -> str | None: |
There was a problem hiding this comment.
raw is not so good, I would expect that to be the raw binary value read out from the sensor, before conversion to a standard unit.
|
|
||
|
|
||
| class SensorEntity(Entity): | ||
| class SensorEntity(MeasurableUnitEntity): |
There was a problem hiding this comment.
Should all sensors get this logic or should be applied to the right ones? I guess that is a migration we can't do anymore at this point.
There was a problem hiding this comment.
Right, because it's a migration it's not feasible to do anything else.
We could do something like this though:
class SensorEntity(Entity):
"""Base class for sensor entities"""
class MeasurableSensorEntity(MeasurableUnitEntity, SensorEntity):
"""Base class for sensor entities which represent a measurable unit."""
"""The migration would then be to MeasurableSensorEntity, but integrations can move to SensorEntity for sensors which don't represent a measurable unit?
|
Looks good! Before we merge this I suggest we search for sensor platforms that use a temperature unit and try to have them updated to use device class temperature if it's missing. We do that separately. |
After a quick check it's clear that many, possibly even most, integrations fail to set |
|
Maybe, but it would be even better to add the device class in the platforms. Not having it there will be less explicit and explicit is good. |
|
OK, I'm marking this as draft for now, we need to fix integrations not setting |
|
There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days. |
|
There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days. |
|
There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days. |
|
This should still be done |
2ce8880 to
904adf4
Compare
Breaking change
Although not a breaking change from a user's perspective, this is a significant change from a developer's perspective.
More details here: home-assistant/developers.home-assistant#1024
Proposed change
Move temperature conversions to sensor base class
SensorEntity(1/8)Temperature conversion will only be done for sensors with device class set to
temperature.Temperature conversion will still be done for other sensor entities during a transition period, which will end with Home Assistant Core 2022.3.0.
Temperature conversion for non sensor entities is unaffected by this PR.
This PR migrates sensors in integrations a-c to be compatible with unit conversion in
SensorEntity.Full list of PRs migrating sensor integrations:
#48261 (integrations a-c)
#54468 (integrations d-e)
#54469 (integrations f-h)
#54472 (integrations i-m)
#54475 (integrations n-q)
#54476 (integrations r-s)
#54482 (integrations s-u)
#54483 (integrations v-z)
#54623 (additional integrations which were added or updated)
Type of change
Additional information
Checklist
black --fast homeassistant tests)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest.requirements_all.txt.Updated by running
python3 -m script.gen_requirements_all..coveragerc.The integration reached or maintains the following Integration Quality Scale:
To help with the load of incoming pull requests: