mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 16:27:08 +00:00
Use native datetime value inMobile App sensors (#59945)
This commit is contained in:
parent
f7b7786d0d
commit
073bf6d6fd
@ -2,10 +2,17 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from homeassistant.components.sensor import SensorEntity
|
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.core import callback
|
||||||
from homeassistant.helpers import entity_registry as er
|
from homeassistant.helpers import entity_registry as er
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
|
from homeassistant.util import dt as dt_util
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
ATTR_DEVICE_NAME,
|
ATTR_DEVICE_NAME,
|
||||||
@ -81,7 +88,20 @@ class MobileAppSensor(MobileAppEntity, SensorEntity):
|
|||||||
@property
|
@property
|
||||||
def native_value(self):
|
def native_value(self):
|
||||||
"""Return the state of the sensor."""
|
"""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
|
@property
|
||||||
def native_unit_of_measurement(self):
|
def native_unit_of_measurement(self):
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
"""Entity tests for mobile_app."""
|
"""Entity tests for mobile_app."""
|
||||||
from http import HTTPStatus
|
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.const import PERCENTAGE, STATE_UNKNOWN
|
||||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
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")
|
updated_entity = hass.states.get("sensor.test_1_battery_state")
|
||||||
assert updated_entity.state == STATE_UNKNOWN
|
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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user