Add support for Sensor state class to ESPHome (#51090)

* Add support for Sensor state class to ESPHome

* Bump aioesphome to 2.8.0
This commit is contained in:
Franck Nijhof 2021-05-26 00:21:18 +02:00 committed by GitHub
parent deb9135707
commit 997a847b5c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 5 deletions

View file

@ -3,12 +3,19 @@ from __future__ import annotations
import math
from aioesphomeapi import SensorInfo, SensorState, TextSensorInfo, TextSensorState
from aioesphomeapi import (
SensorInfo,
SensorState,
SensorStateClass,
TextSensorInfo,
TextSensorState,
)
import voluptuous as vol
from homeassistant.components.sensor import (
DEVICE_CLASS_TIMESTAMP,
DEVICE_CLASSES,
STATE_CLASS_MEASUREMENT,
SensorEntity,
)
from homeassistant.config_entries import ConfigEntry
@ -16,7 +23,12 @@ from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.util import dt
from . import EsphomeEntity, esphome_state_property, platform_async_setup_entry
from . import (
EsphomeEntity,
esphome_map_enum,
esphome_state_property,
platform_async_setup_entry,
)
ICON_SCHEMA = vol.Schema(cv.icon)
@ -49,6 +61,14 @@ async def async_setup_entry(
# pylint: disable=invalid-overridden-method
@esphome_map_enum
def _state_classes():
return {
SensorStateClass.NONE: None,
SensorStateClass.MEASUREMENT: STATE_CLASS_MEASUREMENT,
}
class EsphomeSensor(EsphomeEntity, SensorEntity):
"""A sensor implementation for esphome."""
@ -97,6 +117,13 @@ class EsphomeSensor(EsphomeEntity, SensorEntity):
return None
return self._static_info.device_class
@property
def state_class(self) -> str | None:
"""Return the state class of this entity."""
if not self._static_info.state_class:
return None
return _state_classes.from_esphome(self._static_info.state_class)
class EsphomeTextSensor(EsphomeEntity, SensorEntity):
"""A text sensor implementation for ESPHome."""