Use native datetime value inMobile App sensors (#59945)

This commit is contained in:
Franck Nijhof 2021-11-19 07:36:28 +01:00 committed by GitHub
parent f7b7786d0d
commit 073bf6d6fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 81 additions and 2 deletions

View file

@ -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):