Use native datetime value inMobile App sensors (#59945)
This commit is contained in:
parent
f7b7786d0d
commit
073bf6d6fd
2 changed files with 81 additions and 2 deletions
|
@ -2,10 +2,17 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from homeassistant.components.sensor import SensorEntity
|
||||
from homeassistant.const import CONF_NAME, CONF_UNIQUE_ID, CONF_WEBHOOK_ID
|
||||
from homeassistant.const import (
|
||||
CONF_NAME,
|
||||
CONF_UNIQUE_ID,
|
||||
CONF_WEBHOOK_ID,
|
||||
DEVICE_CLASS_DATE,
|
||||
DEVICE_CLASS_TIMESTAMP,
|
||||
)
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from .const import (
|
||||
ATTR_DEVICE_NAME,
|
||||
|
@ -81,7 +88,20 @@ class MobileAppSensor(MobileAppEntity, SensorEntity):
|
|||
@property
|
||||
def native_value(self):
|
||||
"""Return the state of the sensor."""
|
||||
return self._config[ATTR_SENSOR_STATE]
|
||||
if (
|
||||
(state := self._config[ATTR_SENSOR_STATE]) is not None
|
||||
and self.device_class
|
||||
in (
|
||||
DEVICE_CLASS_DATE,
|
||||
DEVICE_CLASS_TIMESTAMP,
|
||||
)
|
||||
and (timestamp := dt_util.parse_datetime(state)) is not None
|
||||
):
|
||||
if self.device_class == DEVICE_CLASS_DATE:
|
||||
return timestamp.date()
|
||||
return timestamp
|
||||
|
||||
return state
|
||||
|
||||
@property
|
||||
def native_unit_of_measurement(self):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue