2018-12-17 00:29:32 +00:00
|
|
|
"""Support for esphome sensors."""
|
2021-03-17 22:49:01 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2018-12-17 00:29:32 +00:00
|
|
|
import math
|
2019-05-29 11:33:49 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
from aioesphomeapi import SensorInfo, SensorState, TextSensorInfo, TextSensorState
|
2021-02-20 19:05:35 +00:00
|
|
|
import voluptuous as vol
|
2018-12-17 00:29:32 +00:00
|
|
|
|
2021-02-20 19:05:35 +00:00
|
|
|
from homeassistant.components.sensor import DEVICE_CLASSES
|
2018-12-17 00:29:32 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-02-20 19:05:35 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2018-12-17 00:29:32 +00:00
|
|
|
from homeassistant.helpers.typing import HomeAssistantType
|
|
|
|
|
2019-05-29 11:33:49 +00:00
|
|
|
from . import EsphomeEntity, esphome_state_property, platform_async_setup_entry
|
2018-12-17 00:29:32 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities
|
|
|
|
) -> 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
|
|
|
|
# pylint: disable=invalid-overridden-method
|
|
|
|
|
|
|
|
|
2018-12-17 00:29:32 +00:00
|
|
|
class EsphomeSensor(EsphomeEntity):
|
|
|
|
"""A sensor implementation for esphome."""
|
|
|
|
|
|
|
|
@property
|
2019-05-29 11:33:49 +00:00
|
|
|
def _static_info(self) -> SensorInfo:
|
2018-12-17 00:29:32 +00:00
|
|
|
return super()._static_info
|
|
|
|
|
|
|
|
@property
|
2021-03-17 22:49:01 +00:00
|
|
|
def _state(self) -> SensorState | None:
|
2018-12-17 00:29:32 +00:00
|
|
|
return super()._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self) -> str:
|
|
|
|
"""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-02-20 19:05:35 +00:00
|
|
|
return vol.Schema(cv.icon)(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
|
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
|
|
|
|
def unit_of_measurement(self) -> str:
|
|
|
|
"""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
|
|
|
|
def device_class(self) -> str:
|
|
|
|
"""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
|
|
|
|
|
2018-12-17 19:51:59 +00:00
|
|
|
|
|
|
|
class EsphomeTextSensor(EsphomeEntity):
|
|
|
|
"""A text sensor implementation for ESPHome."""
|
|
|
|
|
|
|
|
@property
|
2021-02-12 17:54:00 +00:00
|
|
|
def _static_info(self) -> TextSensorInfo:
|
2018-12-17 19:51:59 +00:00
|
|
|
return super()._static_info
|
|
|
|
|
|
|
|
@property
|
2021-03-17 22:49:01 +00:00
|
|
|
def _state(self) -> TextSensorState | None:
|
2018-12-17 19:51:59 +00:00
|
|
|
return super()._state
|
|
|
|
|
|
|
|
@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
|