From 073bf6d6fdd95eadf98a76b8ea50dd04b9d638f8 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Fri, 19 Nov 2021 07:36:28 +0100 Subject: [PATCH] Use native datetime value inMobile App sensors (#59945) --- homeassistant/components/mobile_app/sensor.py | 24 +++++++- tests/components/mobile_app/test_sensor.py | 59 +++++++++++++++++++ 2 files changed, 81 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/mobile_app/sensor.py b/homeassistant/components/mobile_app/sensor.py index ff4ca491411..f8b71da7c29 100644 --- a/homeassistant/components/mobile_app/sensor.py +++ b/homeassistant/components/mobile_app/sensor.py @@ -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): diff --git a/tests/components/mobile_app/test_sensor.py b/tests/components/mobile_app/test_sensor.py index 032870ffb8c..2fea00a692a 100644 --- a/tests/components/mobile_app/test_sensor.py +++ b/tests/components/mobile_app/test_sensor.py @@ -1,6 +1,9 @@ """Entity tests for mobile_app.""" from http import HTTPStatus +import pytest + +from homeassistant.components.sensor import DEVICE_CLASS_DATE, DEVICE_CLASS_TIMESTAMP from homeassistant.const import PERCENTAGE, STATE_UNKNOWN from homeassistant.helpers import device_registry as dr, entity_registry as er @@ -276,3 +279,59 @@ async def test_update_sensor_no_state(hass, create_registrations, webhook_client updated_entity = hass.states.get("sensor.test_1_battery_state") assert updated_entity.state == STATE_UNKNOWN + + +@pytest.mark.parametrize( + "device_class,native_value,state_value", + [ + (DEVICE_CLASS_DATE, "2021-11-18", "2021-11-18"), + ( + DEVICE_CLASS_TIMESTAMP, + "2021-11-18T20:25:00", + "2021-11-18T20:25:00", + ), + ( + DEVICE_CLASS_TIMESTAMP, + "2021-11-18 20:25:00", + "2021-11-18T20:25:00", + ), + ( + DEVICE_CLASS_TIMESTAMP, + "2021-11-18 20:25:00+01:00", + "2021-11-18T20:25:00+01:00", + ), + ], +) +async def test_sensor_datetime( + hass, create_registrations, webhook_client, device_class, native_value, state_value +): + """Test that sensors can be registered and updated.""" + webhook_id = create_registrations[1]["webhook_id"] + webhook_url = f"/api/webhook/{webhook_id}" + + reg_resp = await webhook_client.post( + webhook_url, + json={ + "type": "register_sensor", + "data": { + "device_class": device_class, + "name": "Datetime sensor test", + "state": native_value, + "type": "sensor", + "unique_id": "super_unique", + }, + }, + ) + + assert reg_resp.status == HTTPStatus.CREATED + + json = await reg_resp.json() + assert json == {"success": True} + await hass.async_block_till_done() + + entity = hass.states.get("sensor.test_1_datetime_sensor_test") + assert entity is not None + + assert entity.attributes["device_class"] == device_class + assert entity.domain == "sensor" + assert entity.state == state_value