Esphome text sensor device class (#111057)

pull/109093/head^2
dougiteixeira 2024-02-25 23:56:52 -03:00 committed by GitHub
parent 5a8779551b
commit 34e9c29ef2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 115 additions and 5 deletions

View File

@ -1,7 +1,7 @@
"""Support for esphome sensors."""
from __future__ import annotations
from datetime import datetime
from datetime import date, datetime
import math
from aioesphomeapi import (
@ -106,9 +106,27 @@ class EsphomeSensor(EsphomeEntity[SensorInfo, SensorState], SensorEntity):
class EsphomeTextSensor(EsphomeEntity[TextSensorInfo, TextSensorState], SensorEntity):
"""A text sensor implementation for ESPHome."""
@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_device_class = try_parse_enum(
SensorDeviceClass, static_info.device_class
)
@property
@esphome_state_property
def native_value(self) -> str | None:
def native_value(self) -> str | datetime | date | None:
"""Return the state of the entity."""
state = self._state
return None if state.missing_state else state.state
if state.missing_state:
return None
if self._attr_device_class is SensorDeviceClass.TIMESTAMP:
return dt_util.parse_datetime(state.state)
if (
self._attr_device_class is SensorDeviceClass.DATE
and (value := dt_util.parse_datetime(state.state)) is not None
):
return value.date()
return state.state

View File

@ -17,8 +17,17 @@ from aioesphomeapi import (
UserService,
)
from homeassistant.components.sensor import ATTR_STATE_CLASS, SensorStateClass
from homeassistant.const import ATTR_ICON, ATTR_UNIT_OF_MEASUREMENT, STATE_UNKNOWN
from homeassistant.components.sensor import (
ATTR_STATE_CLASS,
SensorDeviceClass,
SensorStateClass,
)
from homeassistant.const import (
ATTR_DEVICE_CLASS,
ATTR_ICON,
ATTR_UNIT_OF_MEASUREMENT,
STATE_UNKNOWN,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity import EntityCategory
@ -320,6 +329,89 @@ async def test_generic_text_sensor(
assert state.state == "i am a teapot"
async def test_generic_text_sensor_missing_state(
hass: HomeAssistant, mock_client: APIClient, mock_generic_device_entry
) -> None:
"""Test a generic text sensor that is missing state."""
entity_info = [
TextSensorInfo(
object_id="mysensor",
key=1,
name="my sensor",
unique_id="my_sensor",
)
]
states = [TextSensorState(key=1, state=True, missing_state=True)]
user_service = []
await mock_generic_device_entry(
mock_client=mock_client,
entity_info=entity_info,
user_service=user_service,
states=states,
)
state = hass.states.get("sensor.test_mysensor")
assert state is not None
assert state.state == STATE_UNKNOWN
async def test_generic_text_sensor_device_class_timestamp(
hass: HomeAssistant,
mock_client: APIClient,
mock_generic_device_entry,
) -> None:
"""Test a sensor entity that uses timestamp (datetime)."""
entity_info = [
TextSensorInfo(
object_id="mysensor",
key=1,
name="my sensor",
unique_id="my_sensor",
device_class=SensorDeviceClass.TIMESTAMP,
)
]
states = [TextSensorState(key=1, state="2023-06-22T18:43:52+00:00")]
user_service = []
await mock_generic_device_entry(
mock_client=mock_client,
entity_info=entity_info,
user_service=user_service,
states=states,
)
state = hass.states.get("sensor.test_mysensor")
assert state is not None
assert state.state == "2023-06-22T18:43:52+00:00"
assert state.attributes[ATTR_DEVICE_CLASS] == SensorDeviceClass.TIMESTAMP
async def test_generic_text_sensor_device_class_date(
hass: HomeAssistant,
mock_client: APIClient,
mock_generic_device_entry,
) -> None:
"""Test a sensor entity that uses date (datetime)."""
entity_info = [
TextSensorInfo(
object_id="mysensor",
key=1,
name="my sensor",
unique_id="my_sensor",
device_class=SensorDeviceClass.DATE,
)
]
states = [TextSensorState(key=1, state="2023-06-22T18:43:52+00:00")]
user_service = []
await mock_generic_device_entry(
mock_client=mock_client,
entity_info=entity_info,
user_service=user_service,
states=states,
)
state = hass.states.get("sensor.test_mysensor")
assert state is not None
assert state.state == "2023-06-22"
assert state.attributes[ATTR_DEVICE_CLASS] == SensorDeviceClass.DATE
async def test_generic_numeric_sensor_empty_string_uom(
hass: HomeAssistant, mock_client: APIClient, mock_generic_device_entry
) -> None: