forked from home-assistant/core
Adjust ManualTriggerSensorEntity to handle timestap device classes
This commit is contained in:
@@ -10,8 +10,6 @@ from typing import Any
|
|||||||
|
|
||||||
from jsonpath import jsonpath
|
from jsonpath import jsonpath
|
||||||
|
|
||||||
from homeassistant.components.sensor import SensorDeviceClass
|
|
||||||
from homeassistant.components.sensor.helpers import async_parse_date_datetime
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_COMMAND,
|
CONF_COMMAND,
|
||||||
CONF_NAME,
|
CONF_NAME,
|
||||||
@@ -189,17 +187,7 @@ class CommandSensor(ManualTriggerSensorEntity):
|
|||||||
self.entity_id, variables, None
|
self.entity_id, variables, None
|
||||||
)
|
)
|
||||||
|
|
||||||
if self.device_class not in {
|
self._set_value_with_possible_timestamp(value, self.device_class, variables)
|
||||||
SensorDeviceClass.DATE,
|
|
||||||
SensorDeviceClass.TIMESTAMP,
|
|
||||||
}:
|
|
||||||
self._attr_native_value = value
|
|
||||||
elif value is not None:
|
|
||||||
self._attr_native_value = async_parse_date_datetime(
|
|
||||||
value, self.entity_id, self.device_class
|
|
||||||
)
|
|
||||||
|
|
||||||
self._process_manual_data(variables)
|
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
async def async_update(self) -> None:
|
async def async_update(self) -> None:
|
||||||
|
@@ -13,9 +13,7 @@ from homeassistant.components.sensor import (
|
|||||||
CONF_STATE_CLASS,
|
CONF_STATE_CLASS,
|
||||||
DOMAIN as SENSOR_DOMAIN,
|
DOMAIN as SENSOR_DOMAIN,
|
||||||
PLATFORM_SCHEMA as SENSOR_PLATFORM_SCHEMA,
|
PLATFORM_SCHEMA as SENSOR_PLATFORM_SCHEMA,
|
||||||
SensorDeviceClass,
|
|
||||||
)
|
)
|
||||||
from homeassistant.components.sensor.helpers import async_parse_date_datetime
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_DEVICE_CLASS,
|
CONF_DEVICE_CLASS,
|
||||||
CONF_FORCE_UPDATE,
|
CONF_FORCE_UPDATE,
|
||||||
@@ -181,18 +179,5 @@ class RestSensor(ManualTriggerSensorEntity, RestEntity):
|
|||||||
self.entity_id, variables, None
|
self.entity_id, variables, None
|
||||||
)
|
)
|
||||||
|
|
||||||
if value is None or self.device_class not in (
|
self._set_value_with_possible_timestamp(value, self.device_class, variables)
|
||||||
SensorDeviceClass.DATE,
|
|
||||||
SensorDeviceClass.TIMESTAMP,
|
|
||||||
):
|
|
||||||
self._attr_native_value = value
|
|
||||||
self._process_manual_data(variables)
|
|
||||||
self.async_write_ha_state()
|
|
||||||
return
|
|
||||||
|
|
||||||
self._attr_native_value = async_parse_date_datetime(
|
|
||||||
value, self.entity_id, self.device_class
|
|
||||||
)
|
|
||||||
|
|
||||||
self._process_manual_data(variables)
|
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
@@ -7,8 +7,7 @@ from typing import Any, cast
|
|||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.sensor import CONF_STATE_CLASS, SensorDeviceClass
|
from homeassistant.components.sensor import CONF_STATE_CLASS
|
||||||
from homeassistant.components.sensor.helpers import async_parse_date_datetime
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_ATTRIBUTE,
|
CONF_ATTRIBUTE,
|
||||||
CONF_DEVICE_CLASS,
|
CONF_DEVICE_CLASS,
|
||||||
@@ -218,18 +217,7 @@ class ScrapeSensor(CoordinatorEntity[ScrapeCoordinator], ManualTriggerSensorEnti
|
|||||||
self.entity_id, variables, None
|
self.entity_id, variables, None
|
||||||
)
|
)
|
||||||
|
|
||||||
if self.device_class not in {
|
self._set_value_with_possible_timestamp(value, self.device_class, variables)
|
||||||
SensorDeviceClass.DATE,
|
|
||||||
SensorDeviceClass.TIMESTAMP,
|
|
||||||
}:
|
|
||||||
self._attr_native_value = value
|
|
||||||
self._process_manual_data(variables)
|
|
||||||
return
|
|
||||||
|
|
||||||
self._attr_native_value = async_parse_date_datetime(
|
|
||||||
value, self.entity_id, self.device_class
|
|
||||||
)
|
|
||||||
self._process_manual_data(variables)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def available(self) -> bool:
|
def available(self) -> bool:
|
||||||
|
@@ -217,8 +217,7 @@ class SnmpSensor(ManualTriggerSensorEntity):
|
|||||||
self.entity_id, variables, STATE_UNKNOWN
|
self.entity_id, variables, STATE_UNKNOWN
|
||||||
)
|
)
|
||||||
|
|
||||||
self._attr_native_value = value
|
self._set_value_with_possible_timestamp(value, self.device_class, variables)
|
||||||
self._process_manual_data(variables)
|
|
||||||
|
|
||||||
|
|
||||||
class SnmpData:
|
class SnmpData:
|
||||||
|
@@ -401,10 +401,12 @@ class SQLSensor(ManualTriggerSensorEntity):
|
|||||||
if data is not None and self._template is not None:
|
if data is not None and self._template is not None:
|
||||||
variables = self._template_variables_with_value(data)
|
variables = self._template_variables_with_value(data)
|
||||||
if self._render_availability_template(variables):
|
if self._render_availability_template(variables):
|
||||||
self._attr_native_value = self._template.async_render_as_value_template(
|
_value = self._template.async_render_as_value_template(
|
||||||
self.entity_id, variables, None
|
self.entity_id, variables, None
|
||||||
)
|
)
|
||||||
self._process_manual_data(variables)
|
self._set_value_with_possible_timestamp(
|
||||||
|
_value, self.device_class, variables
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
self._attr_native_value = data
|
self._attr_native_value = data
|
||||||
|
|
||||||
|
@@ -13,8 +13,10 @@ from homeassistant.components.sensor import (
|
|||||||
CONF_STATE_CLASS,
|
CONF_STATE_CLASS,
|
||||||
DEVICE_CLASSES_SCHEMA,
|
DEVICE_CLASSES_SCHEMA,
|
||||||
STATE_CLASSES_SCHEMA,
|
STATE_CLASSES_SCHEMA,
|
||||||
|
SensorDeviceClass,
|
||||||
SensorEntity,
|
SensorEntity,
|
||||||
)
|
)
|
||||||
|
from homeassistant.components.sensor.helpers import async_parse_date_datetime
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_ENTITY_PICTURE,
|
ATTR_ENTITY_PICTURE,
|
||||||
ATTR_FRIENDLY_NAME,
|
ATTR_FRIENDLY_NAME,
|
||||||
@@ -389,3 +391,28 @@ class ManualTriggerSensorEntity(ManualTriggerEntity, SensorEntity):
|
|||||||
ManualTriggerEntity.__init__(self, hass, config)
|
ManualTriggerEntity.__init__(self, hass, config)
|
||||||
self._attr_native_unit_of_measurement = config.get(CONF_UNIT_OF_MEASUREMENT)
|
self._attr_native_unit_of_measurement = config.get(CONF_UNIT_OF_MEASUREMENT)
|
||||||
self._attr_state_class = config.get(CONF_STATE_CLASS)
|
self._attr_state_class = config.get(CONF_STATE_CLASS)
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def _set_value_with_possible_timestamp(
|
||||||
|
self,
|
||||||
|
value: Any,
|
||||||
|
device_class: SensorDeviceClass | None,
|
||||||
|
variables: dict[str, Any],
|
||||||
|
) -> None:
|
||||||
|
"""Set value with possible timestamp.
|
||||||
|
|
||||||
|
Implementing class should call this last in update method to set the value
|
||||||
|
and then render all templates.
|
||||||
|
Ex: _set_value_with_possible_timestamp(value, device_class, variables)
|
||||||
|
"""
|
||||||
|
if device_class not in (
|
||||||
|
SensorDeviceClass.DATE,
|
||||||
|
SensorDeviceClass.TIMESTAMP,
|
||||||
|
):
|
||||||
|
self._attr_native_value = value
|
||||||
|
elif value is not None:
|
||||||
|
self._attr_native_value = async_parse_date_datetime(
|
||||||
|
value, self.entity_id, self.device_class
|
||||||
|
)
|
||||||
|
|
||||||
|
super()._process_manual_data(variables)
|
||||||
|
Reference in New Issue
Block a user