mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Use native datetime value in MQTT sensors (#59923)
This commit is contained in:
parent
4480e1255a
commit
5c443b626a
@ -21,6 +21,8 @@ from homeassistant.const import (
|
|||||||
CONF_NAME,
|
CONF_NAME,
|
||||||
CONF_UNIT_OF_MEASUREMENT,
|
CONF_UNIT_OF_MEASUREMENT,
|
||||||
CONF_VALUE_TEMPLATE,
|
CONF_VALUE_TEMPLATE,
|
||||||
|
DEVICE_CLASS_DATE,
|
||||||
|
DEVICE_CLASS_TIMESTAMP,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
@ -196,6 +198,18 @@ class MqttSensor(MqttEntity, SensorEntity):
|
|||||||
self._state,
|
self._state,
|
||||||
variables=variables,
|
variables=variables,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if payload is not None and self.device_class in (
|
||||||
|
DEVICE_CLASS_DATE,
|
||||||
|
DEVICE_CLASS_TIMESTAMP,
|
||||||
|
):
|
||||||
|
if (payload := dt_util.parse_datetime(payload)) is None:
|
||||||
|
_LOGGER.warning(
|
||||||
|
"Invalid state message '%s' from '%s'", msg.payload, msg.topic
|
||||||
|
)
|
||||||
|
elif self.device_class == DEVICE_CLASS_DATE:
|
||||||
|
payload = payload.date()
|
||||||
|
|
||||||
self._state = payload
|
self._state = payload
|
||||||
|
|
||||||
def _update_last_reset(msg):
|
def _update_last_reset(msg):
|
||||||
|
@ -8,7 +8,7 @@ import pytest
|
|||||||
|
|
||||||
from homeassistant.components.mqtt.sensor import MQTT_SENSOR_ATTRIBUTES_BLOCKED
|
from homeassistant.components.mqtt.sensor import MQTT_SENSOR_ATTRIBUTES_BLOCKED
|
||||||
import homeassistant.components.sensor as sensor
|
import homeassistant.components.sensor as sensor
|
||||||
from homeassistant.const import EVENT_STATE_CHANGED, STATE_UNAVAILABLE
|
from homeassistant.const import EVENT_STATE_CHANGED, STATE_UNAVAILABLE, STATE_UNKNOWN
|
||||||
import homeassistant.core as ha
|
import homeassistant.core as ha
|
||||||
from homeassistant.helpers import device_registry as dr
|
from homeassistant.helpers import device_registry as dr
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
@ -80,6 +80,58 @@ async def test_setting_sensor_value_via_mqtt_message(hass, mqtt_mock):
|
|||||||
assert state.attributes.get("unit_of_measurement") == "fav unit"
|
assert state.attributes.get("unit_of_measurement") == "fav unit"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"device_class,native_value,state_value,log",
|
||||||
|
[
|
||||||
|
(sensor.DEVICE_CLASS_DATE, "2021-11-18", "2021-11-18", False),
|
||||||
|
(sensor.DEVICE_CLASS_DATE, "invalid", STATE_UNKNOWN, True),
|
||||||
|
(
|
||||||
|
sensor.DEVICE_CLASS_TIMESTAMP,
|
||||||
|
"2021-11-18T20:25:00+00:00",
|
||||||
|
"2021-11-18T20:25:00+00:00",
|
||||||
|
False,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
sensor.DEVICE_CLASS_TIMESTAMP,
|
||||||
|
"2021-11-18 20:25:00+00:00",
|
||||||
|
"2021-11-18T20:25:00+00:00",
|
||||||
|
False,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
sensor.DEVICE_CLASS_TIMESTAMP,
|
||||||
|
"2021-11-18 20:25:00+01:00",
|
||||||
|
"2021-11-18T19:25:00+00:00",
|
||||||
|
False,
|
||||||
|
),
|
||||||
|
(sensor.DEVICE_CLASS_TIMESTAMP, "invalid", STATE_UNKNOWN, True),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_setting_sensor_native_value_handling_via_mqtt_message(
|
||||||
|
hass, mqtt_mock, caplog, device_class, native_value, state_value, log
|
||||||
|
):
|
||||||
|
"""Test the setting of the value via MQTT."""
|
||||||
|
assert await async_setup_component(
|
||||||
|
hass,
|
||||||
|
sensor.DOMAIN,
|
||||||
|
{
|
||||||
|
sensor.DOMAIN: {
|
||||||
|
"platform": "mqtt",
|
||||||
|
"name": "test",
|
||||||
|
"state_topic": "test-topic",
|
||||||
|
"device_class": device_class,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
async_fire_mqtt_message(hass, "test-topic", native_value)
|
||||||
|
state = hass.states.get("sensor.test")
|
||||||
|
|
||||||
|
assert state.state == state_value
|
||||||
|
assert state.attributes.get("device_class") == device_class
|
||||||
|
assert log == ("Invalid state message" in caplog.text)
|
||||||
|
|
||||||
|
|
||||||
async def test_setting_sensor_value_expires_availability_topic(hass, mqtt_mock, caplog):
|
async def test_setting_sensor_value_expires_availability_topic(hass, mqtt_mock, caplog):
|
||||||
"""Test the expiration of the value."""
|
"""Test the expiration of the value."""
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user