Fix units not being pulled from source sensor (#63639)

This commit is contained in:
Ryan Steckler 2022-01-24 06:15:34 -08:00 committed by GitHub
parent a15bdbbc4a
commit b541e91885
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 133 additions and 4 deletions

View file

@ -150,17 +150,27 @@ class IntegrationSensor(RestoreEntity, SensorEntity):
old_state = event.data.get("old_state")
new_state = event.data.get("new_state")
# We may want to update our state before an early return,
# based on the source sensor's unit_of_measurement
# or device_class.
update_state = False
if self._unit_of_measurement is None:
unit = new_state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
self._unit_of_measurement = self._unit_template.format(
"" if unit is None else unit
)
if unit is not None:
self._unit_of_measurement = self._unit_template.format(unit)
update_state = True
if (
self.device_class is None
and new_state.attributes.get(ATTR_DEVICE_CLASS)
== SensorDeviceClass.POWER
):
self._attr_device_class = SensorDeviceClass.ENERGY
update_state = True
if update_state:
self.async_write_ha_state()
if (
old_state is None