Reduce overhead to update esphome entities (#94930)

This commit is contained in:
J. Nick Koston 2023-06-21 10:00:21 +01:00 committed by GitHub
parent 933ae5198e
commit 804a8ef36a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 117 additions and 158 deletions

View file

@ -5,6 +5,7 @@ from datetime import datetime
import math
from aioesphomeapi import (
EntityInfo,
SensorInfo,
SensorState,
SensorStateClass as EsphomeSensorStateClass,
@ -19,7 +20,7 @@ from homeassistant.components.sensor import (
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util import dt as dt_util
from homeassistant.util.enum import try_parse_enum
@ -67,10 +68,27 @@ _STATE_CLASSES: EsphomeEnumMapper[
class EsphomeSensor(EsphomeEntity[SensorInfo, SensorState], SensorEntity):
"""A sensor implementation for esphome."""
@property
def force_update(self) -> bool:
"""Return if this sensor should force a state update."""
return self._static_info.force_update
@callback
def _on_static_info_update(self, static_info: EntityInfo) -> None:
"""Set attrs from static info."""
super()._on_static_info_update(static_info)
static_info = self._static_info
self._attr_force_update = static_info.force_update
self._attr_native_unit_of_measurement = static_info.unit_of_measurement
self._attr_device_class = try_parse_enum(
SensorDeviceClass, static_info.device_class
)
if not (state_class := static_info.state_class):
return
if (
state_class == EsphomeSensorStateClass.MEASUREMENT
and static_info.last_reset_type == LastResetType.AUTO
):
# Legacy, last_reset_type auto was the equivalent to the
# TOTAL_INCREASING state class
self._attr_state_class = SensorStateClass.TOTAL_INCREASING
else:
self._attr_state_class = _STATE_CLASSES.from_esphome(state_class)
@property
@esphome_state_property
@ -80,38 +98,10 @@ class EsphomeSensor(EsphomeEntity[SensorInfo, SensorState], SensorEntity):
return None
if self._state.missing_state:
return None
if self.device_class == SensorDeviceClass.TIMESTAMP:
if self._attr_device_class == SensorDeviceClass.TIMESTAMP:
return dt_util.utc_from_timestamp(self._state.state)
return f"{self._state.state:.{self._static_info.accuracy_decimals}f}"
@property
def native_unit_of_measurement(self) -> str | None:
"""Return the unit the value is expressed in."""
if not self._static_info.unit_of_measurement:
return None
return self._static_info.unit_of_measurement
@property
def device_class(self) -> SensorDeviceClass | None:
"""Return the class of this device, from component DEVICE_CLASSES."""
return try_parse_enum(SensorDeviceClass, self._static_info.device_class)
@property
def state_class(self) -> SensorStateClass | None:
"""Return the state class of this entity."""
if not self._static_info.state_class:
return None
state_class = self._static_info.state_class
reset_type = self._static_info.last_reset_type
if (
state_class == EsphomeSensorStateClass.MEASUREMENT
and reset_type == LastResetType.AUTO
):
# Legacy, last_reset_type auto was the equivalent to the
# TOTAL_INCREASING state class
return SensorStateClass.TOTAL_INCREASING
return _STATE_CLASSES.from_esphome(self._static_info.state_class)
class EsphomeTextSensor(EsphomeEntity[TextSensorInfo, TextSensorState], SensorEntity):
"""A text sensor implementation for ESPHome."""