2018-12-17 00:29:32 +00:00
|
|
|
"""Support for esphome sensors."""
|
2021-03-17 22:49:01 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-07-28 11:56:45 +00:00
|
|
|
from contextlib import suppress
|
|
|
|
from datetime import datetime
|
2018-12-17 00:29:32 +00:00
|
|
|
import math
|
2021-07-12 20:56:10 +00:00
|
|
|
from typing import cast
|
2019-05-29 11:33:49 +00:00
|
|
|
|
2021-05-25 22:21:18 +00:00
|
|
|
from aioesphomeapi import (
|
|
|
|
SensorInfo,
|
|
|
|
SensorState,
|
|
|
|
SensorStateClass,
|
|
|
|
TextSensorInfo,
|
|
|
|
TextSensorState,
|
|
|
|
)
|
2021-07-28 11:56:45 +00:00
|
|
|
from aioesphomeapi.model import LastResetType
|
2021-02-20 19:05:35 +00:00
|
|
|
import voluptuous as vol
|
2018-12-17 00:29:32 +00:00
|
|
|
|
2021-05-08 17:37:09 +00:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
DEVICE_CLASS_TIMESTAMP,
|
|
|
|
DEVICE_CLASSES,
|
2021-05-25 22:21:18 +00:00
|
|
|
STATE_CLASS_MEASUREMENT,
|
2021-05-08 17:37:09 +00:00
|
|
|
SensorEntity,
|
|
|
|
)
|
2018-12-17 00:29:32 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-07-28 11:56:45 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2021-02-20 19:05:35 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2021-07-12 20:56:10 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-07-28 11:56:45 +00:00
|
|
|
from homeassistant.helpers.restore_state import RestoreEntity
|
2021-05-08 17:37:09 +00:00
|
|
|
from homeassistant.util import dt
|
2018-12-17 00:29:32 +00:00
|
|
|
|
2021-05-25 22:21:18 +00:00
|
|
|
from . import (
|
|
|
|
EsphomeEntity,
|
2021-06-22 04:22:38 +00:00
|
|
|
EsphomeEnumMapper,
|
2021-05-25 22:21:18 +00:00
|
|
|
esphome_state_property,
|
|
|
|
platform_async_setup_entry,
|
|
|
|
)
|
2018-12-17 00:29:32 +00:00
|
|
|
|
2021-04-26 10:37:13 +00:00
|
|
|
ICON_SCHEMA = vol.Schema(cv.icon)
|
|
|
|
|
2018-12-17 00:29:32 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_entry(
|
2021-07-12 20:56:10 +00:00
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
2019-07-31 19:25:30 +00:00
|
|
|
) -> None:
|
2018-12-17 00:29:32 +00:00
|
|
|
"""Set up esphome sensors based on a config entry."""
|
|
|
|
await platform_async_setup_entry(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass,
|
|
|
|
entry,
|
|
|
|
async_add_entities,
|
|
|
|
component_key="sensor",
|
|
|
|
info_type=SensorInfo,
|
|
|
|
entity_type=EsphomeSensor,
|
|
|
|
state_type=SensorState,
|
2018-12-17 00:29:32 +00:00
|
|
|
)
|
2018-12-17 19:51:59 +00:00
|
|
|
await platform_async_setup_entry(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass,
|
|
|
|
entry,
|
|
|
|
async_add_entities,
|
|
|
|
component_key="text_sensor",
|
|
|
|
info_type=TextSensorInfo,
|
|
|
|
entity_type=EsphomeTextSensor,
|
|
|
|
state_type=TextSensorState,
|
2018-12-17 19:51:59 +00:00
|
|
|
)
|
2018-12-17 00:29:32 +00:00
|
|
|
|
|
|
|
|
2019-10-07 15:17:39 +00:00
|
|
|
# https://github.com/PyCQA/pylint/issues/3150 for all @esphome_state_property
|
2021-07-15 04:44:57 +00:00
|
|
|
# pylint: disable=invalid-overridden-method
|
2019-10-07 15:17:39 +00:00
|
|
|
|
|
|
|
|
2021-07-12 20:56:10 +00:00
|
|
|
_STATE_CLASSES: EsphomeEnumMapper[SensorStateClass, str | None] = EsphomeEnumMapper(
|
2021-06-22 04:22:38 +00:00
|
|
|
{
|
2021-05-25 22:21:18 +00:00
|
|
|
SensorStateClass.NONE: None,
|
|
|
|
SensorStateClass.MEASUREMENT: STATE_CLASS_MEASUREMENT,
|
|
|
|
}
|
2021-06-22 04:22:38 +00:00
|
|
|
)
|
2021-05-25 22:21:18 +00:00
|
|
|
|
|
|
|
|
2021-07-28 11:56:45 +00:00
|
|
|
class EsphomeSensor(
|
|
|
|
EsphomeEntity[SensorInfo, SensorState], SensorEntity, RestoreEntity
|
|
|
|
):
|
2018-12-17 00:29:32 +00:00
|
|
|
"""A sensor implementation for esphome."""
|
|
|
|
|
2021-07-28 11:56:45 +00:00
|
|
|
_old_state: float | None = None
|
|
|
|
|
|
|
|
async def async_added_to_hass(self) -> None:
|
|
|
|
"""Register callbacks."""
|
|
|
|
await super().async_added_to_hass()
|
|
|
|
|
|
|
|
if self._static_info.last_reset_type != LastResetType.AUTO:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Logic to restore old state for last_reset_type AUTO:
|
|
|
|
last_state = await self.async_get_last_state()
|
|
|
|
if last_state is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
if "last_reset" in last_state.attributes:
|
|
|
|
self._attr_last_reset = dt.as_utc(
|
|
|
|
datetime.fromisoformat(last_state.attributes["last_reset"])
|
|
|
|
)
|
|
|
|
|
|
|
|
with suppress(ValueError):
|
|
|
|
self._old_state = float(last_state.state)
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _on_state_update(self) -> None:
|
|
|
|
"""Check last_reset when new state arrives."""
|
|
|
|
if self._static_info.last_reset_type == LastResetType.NEVER:
|
|
|
|
self._attr_last_reset = dt.utc_from_timestamp(0)
|
|
|
|
|
|
|
|
if self._static_info.last_reset_type != LastResetType.AUTO:
|
|
|
|
super()._on_state_update()
|
|
|
|
return
|
|
|
|
|
|
|
|
# Last reset type AUTO logic for the last_reset property
|
|
|
|
# In this mode we automatically determine if an accumulator reset
|
|
|
|
# has taken place.
|
|
|
|
# We compare the last valid value (_old_state) with the new one.
|
|
|
|
# If the value has reset to 0 or has significantly reduced we say
|
|
|
|
# it has reset.
|
|
|
|
new_state: float | None = None
|
|
|
|
state = cast("str | None", self.state)
|
|
|
|
if state is not None:
|
|
|
|
with suppress(ValueError):
|
|
|
|
new_state = float(state)
|
|
|
|
|
|
|
|
did_reset = False
|
|
|
|
if new_state is None:
|
|
|
|
# New state is not a float - we'll detect the reset once we get valid data again
|
|
|
|
did_reset = False
|
|
|
|
elif self._old_state is None:
|
|
|
|
# First measurement we ever got for this sensor, always a reset
|
|
|
|
did_reset = True
|
|
|
|
elif new_state == 0:
|
|
|
|
# don't set reset if both old and new are 0
|
|
|
|
# we would already have detected the reset on the last state
|
|
|
|
did_reset = self._old_state != 0
|
|
|
|
elif new_state < self._old_state:
|
|
|
|
did_reset = True
|
|
|
|
|
|
|
|
# Set last_reset to now if we detected a reset
|
|
|
|
if did_reset:
|
|
|
|
self._attr_last_reset = dt.utcnow()
|
|
|
|
|
|
|
|
if new_state is not None:
|
|
|
|
# Only write to old_state if the new one contains actual data
|
|
|
|
self._old_state = new_state
|
|
|
|
|
|
|
|
super()._on_state_update()
|
|
|
|
|
2018-12-17 00:29:32 +00:00
|
|
|
@property
|
2021-07-12 20:56:10 +00:00
|
|
|
def icon(self) -> str | None:
|
2018-12-17 00:29:32 +00:00
|
|
|
"""Return the icon."""
|
2021-02-16 19:36:32 +00:00
|
|
|
if not self._static_info.icon or self._static_info.device_class:
|
2021-02-16 06:19:31 +00:00
|
|
|
return None
|
2021-07-12 20:56:10 +00:00
|
|
|
return cast(str, ICON_SCHEMA(self._static_info.icon))
|
2018-12-17 00:29:32 +00:00
|
|
|
|
2019-10-21 14:06:57 +00:00
|
|
|
@property
|
|
|
|
def force_update(self) -> bool:
|
|
|
|
"""Return if this sensor should force a state update."""
|
|
|
|
return self._static_info.force_update
|
|
|
|
|
2019-04-16 20:48:46 +00:00
|
|
|
@esphome_state_property
|
2021-03-17 22:49:01 +00:00
|
|
|
def state(self) -> str | None:
|
2018-12-17 00:29:32 +00:00
|
|
|
"""Return the state of the entity."""
|
|
|
|
if math.isnan(self._state.state):
|
|
|
|
return None
|
2019-11-12 17:26:46 +00:00
|
|
|
if self._state.missing_state:
|
|
|
|
return None
|
2021-05-08 17:37:09 +00:00
|
|
|
if self.device_class == DEVICE_CLASS_TIMESTAMP:
|
|
|
|
return dt.utc_from_timestamp(self._state.state).isoformat()
|
2020-02-24 16:47:52 +00:00
|
|
|
return f"{self._state.state:.{self._static_info.accuracy_decimals}f}"
|
2018-12-17 00:29:32 +00:00
|
|
|
|
|
|
|
@property
|
2021-07-12 20:56:10 +00:00
|
|
|
def unit_of_measurement(self) -> str | None:
|
2018-12-17 00:29:32 +00:00
|
|
|
"""Return the unit the value is expressed in."""
|
2021-02-16 19:36:32 +00:00
|
|
|
if not self._static_info.unit_of_measurement:
|
2021-02-16 06:19:31 +00:00
|
|
|
return None
|
2018-12-17 00:29:32 +00:00
|
|
|
return self._static_info.unit_of_measurement
|
2018-12-17 19:51:59 +00:00
|
|
|
|
2021-02-16 06:19:31 +00:00
|
|
|
@property
|
2021-07-12 20:56:10 +00:00
|
|
|
def device_class(self) -> str | None:
|
2021-02-16 06:19:31 +00:00
|
|
|
"""Return the class of this device, from component DEVICE_CLASSES."""
|
2021-02-20 19:05:35 +00:00
|
|
|
if self._static_info.device_class not in DEVICE_CLASSES:
|
2021-02-16 06:19:31 +00:00
|
|
|
return None
|
|
|
|
return self._static_info.device_class
|
|
|
|
|
2021-05-25 22:21:18 +00:00
|
|
|
@property
|
|
|
|
def state_class(self) -> str | None:
|
|
|
|
"""Return the state class of this entity."""
|
|
|
|
if not self._static_info.state_class:
|
|
|
|
return None
|
2021-06-22 04:22:38 +00:00
|
|
|
return _STATE_CLASSES.from_esphome(self._static_info.state_class)
|
2021-05-25 22:21:18 +00:00
|
|
|
|
2018-12-17 19:51:59 +00:00
|
|
|
|
2021-07-12 20:56:10 +00:00
|
|
|
class EsphomeTextSensor(EsphomeEntity[TextSensorInfo, TextSensorState], SensorEntity):
|
2018-12-17 19:51:59 +00:00
|
|
|
"""A text sensor implementation for ESPHome."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self) -> str:
|
|
|
|
"""Return the icon."""
|
|
|
|
return self._static_info.icon
|
|
|
|
|
2019-04-16 20:48:46 +00:00
|
|
|
@esphome_state_property
|
2021-03-17 22:49:01 +00:00
|
|
|
def state(self) -> str | None:
|
2018-12-17 19:51:59 +00:00
|
|
|
"""Return the state of the entity."""
|
2019-11-12 17:26:46 +00:00
|
|
|
if self._state.missing_state:
|
|
|
|
return None
|
2018-12-17 19:51:59 +00:00
|
|
|
return self._state.state
|