Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions homeassistant/components/sensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class SensorEntity(Entity):
_attr_native_unit_of_measurement: str | None
_attr_native_value: StateType = None
_attr_state_class: str | None
_attr_state: None = None # Subclasses of SensorEntity should not set this
_last_reset_reported = False
_temperature_conversion_reported = False

Expand Down Expand Up @@ -161,8 +162,7 @@ def state_attributes(self) -> dict[str, Any] | None:
"""Return state attributes."""
if last_reset := self.last_reset:
if (
last_reset is not None
and self.state_class == STATE_CLASS_MEASUREMENT
self.state_class == STATE_CLASS_MEASUREMENT
and not self._last_reset_reported
):
self._last_reset_reported = True
Expand Down Expand Up @@ -197,6 +197,7 @@ def native_unit_of_measurement(self) -> str | None:
return self.entity_description.native_unit_of_measurement
return None

@final
@property
def unit_of_measurement(self) -> str | None:
"""Return the unit of measurement of the entity, after unit conversion."""
Expand All @@ -218,13 +219,10 @@ def unit_of_measurement(self) -> str | None:

return native_unit_of_measurement

@final
@property
def state(self) -> Any:
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.

We should also mark the state method final and re-type _attr_state: None = None.

"""Return the state of the sensor and perform unit conversions, if needed."""
# Test if _attr_state has been set in this instance
if "_attr_state" in self.__dict__:
return self._attr_state

unit_of_measurement = self.native_unit_of_measurement
value = self.native_value

Expand Down