Fix date and timestamp device class in Command Line Sensor (#97663)

* Fix date in Command Line sensor

* prettier
This commit is contained in:
G Johansson 2023-08-03 13:59:37 +02:00 committed by GitHub
parent d50b993f64
commit 4c395c0124
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 6 deletions

View file

@ -15,9 +15,11 @@ from homeassistant.components.sensor import (
DOMAIN as SENSOR_DOMAIN,
PLATFORM_SCHEMA,
STATE_CLASSES_SCHEMA,
SensorDeviceClass,
SensorEntity,
SensorStateClass,
)
from homeassistant.components.sensor.helpers import async_parse_date_datetime
from homeassistant.const import (
CONF_COMMAND,
CONF_DEVICE_CLASS,
@ -206,14 +208,24 @@ class CommandSensor(ManualTriggerEntity, SensorEntity):
return
if self._value_template is not None:
self._attr_native_value = (
self._value_template.async_render_with_possible_json_value(
value,
None,
)
value = self._value_template.async_render_with_possible_json_value(
value,
None,
)
else:
if self.device_class not in {
SensorDeviceClass.DATE,
SensorDeviceClass.TIMESTAMP,
}:
self._attr_native_value = value
self._process_manual_data(value)
return
self._attr_native_value = None
if value is not None:
self._attr_native_value = async_parse_date_datetime(
value, self.entity_id, self.device_class
)
self._process_manual_data(value)
self.async_write_ha_state()