2016-03-09 09:25:50 +00:00
|
|
|
"""The tests for the MQTT sensor platform."""
|
2020-09-10 18:52:23 +00:00
|
|
|
import copy
|
2019-01-27 17:54:52 +00:00
|
|
|
from datetime import datetime, timedelta
|
2018-10-08 10:53:30 +00:00
|
|
|
import json
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import patch
|
2017-03-23 21:55:07 +00:00
|
|
|
|
2020-05-06 21:14:57 +00:00
|
|
|
import pytest
|
|
|
|
|
2015-10-03 06:57:26 +00:00
|
|
|
import homeassistant.components.sensor as sensor
|
2020-07-08 23:20:19 +00:00
|
|
|
from homeassistant.const import EVENT_STATE_CHANGED, STATE_UNAVAILABLE
|
2019-01-27 17:54:52 +00:00
|
|
|
import homeassistant.core as ha
|
2021-03-09 13:32:08 +00:00
|
|
|
from homeassistant.helpers import device_registry as dr
|
2019-04-19 21:52:23 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2017-03-23 21:55:07 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2015-10-03 06:57:26 +00:00
|
|
|
|
2020-03-30 21:26:59 +00:00
|
|
|
from .test_common import (
|
2020-06-07 07:21:16 +00:00
|
|
|
help_test_availability_when_connection_lost,
|
2020-03-17 07:09:19 +00:00
|
|
|
help_test_availability_without_topic,
|
|
|
|
help_test_custom_availability_payload,
|
2020-07-04 15:48:34 +00:00
|
|
|
help_test_default_availability_list_payload,
|
2021-01-11 15:04:22 +00:00
|
|
|
help_test_default_availability_list_payload_all,
|
|
|
|
help_test_default_availability_list_payload_any,
|
2020-07-04 15:48:34 +00:00
|
|
|
help_test_default_availability_list_single,
|
2020-03-17 07:09:19 +00:00
|
|
|
help_test_default_availability_payload,
|
2020-03-09 16:40:00 +00:00
|
|
|
help_test_discovery_broken,
|
|
|
|
help_test_discovery_removal,
|
|
|
|
help_test_discovery_update,
|
|
|
|
help_test_discovery_update_attr,
|
2020-07-04 15:48:34 +00:00
|
|
|
help_test_discovery_update_availability,
|
2020-08-06 08:43:47 +00:00
|
|
|
help_test_discovery_update_unchanged,
|
2020-04-01 17:00:40 +00:00
|
|
|
help_test_entity_debug_info,
|
|
|
|
help_test_entity_debug_info_max_messages,
|
|
|
|
help_test_entity_debug_info_message,
|
|
|
|
help_test_entity_debug_info_remove,
|
|
|
|
help_test_entity_debug_info_update_entity_id,
|
2020-03-12 01:00:47 +00:00
|
|
|
help_test_entity_device_info_remove,
|
2020-03-09 16:40:00 +00:00
|
|
|
help_test_entity_device_info_update,
|
2020-03-17 07:09:19 +00:00
|
|
|
help_test_entity_device_info_with_connection,
|
2020-03-09 16:40:00 +00:00
|
|
|
help_test_entity_device_info_with_identifier,
|
2021-03-29 22:09:14 +00:00
|
|
|
help_test_entity_disabled_by_default,
|
2020-03-30 21:26:59 +00:00
|
|
|
help_test_entity_id_update_discovery_update,
|
|
|
|
help_test_entity_id_update_subscriptions,
|
2020-03-09 16:40:00 +00:00
|
|
|
help_test_setting_attribute_via_mqtt_json_message,
|
2020-03-17 07:09:19 +00:00
|
|
|
help_test_setting_attribute_with_template,
|
2020-03-09 16:40:00 +00:00
|
|
|
help_test_unique_id,
|
|
|
|
help_test_update_with_json_attrs_bad_JSON,
|
|
|
|
help_test_update_with_json_attrs_not_dict,
|
|
|
|
)
|
|
|
|
|
2020-06-15 22:38:56 +00:00
|
|
|
from tests.common import async_fire_mqtt_message, async_fire_time_changed
|
2019-04-19 21:52:23 +00:00
|
|
|
|
2020-03-17 07:09:19 +00:00
|
|
|
DEFAULT_CONFIG = {
|
|
|
|
sensor.DOMAIN: {"platform": "mqtt", "name": "test", "state_topic": "test-topic"}
|
2020-03-11 16:34:19 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 21:52:23 +00:00
|
|
|
|
|
|
|
async def test_setting_sensor_value_via_mqtt_message(hass, mqtt_mock):
|
|
|
|
"""Test the setting of the value via MQTT."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
sensor.DOMAIN,
|
|
|
|
{
|
|
|
|
sensor.DOMAIN: {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "test",
|
|
|
|
"state_topic": "test-topic",
|
|
|
|
"unit_of_measurement": "fav unit",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2020-06-01 05:18:30 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-04-19 21:52:23 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "test-topic", "100")
|
|
|
|
state = hass.states.get("sensor.test")
|
2019-04-19 21:52:23 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.state == "100"
|
|
|
|
assert state.attributes.get("unit_of_measurement") == "fav unit"
|
2019-04-19 21:52:23 +00:00
|
|
|
|
|
|
|
|
2020-10-05 19:39:44 +00:00
|
|
|
async def test_setting_sensor_value_expires_availability_topic(hass, mqtt_mock, caplog):
|
2020-07-08 23:20:19 +00:00
|
|
|
"""Test the expiration of the value."""
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
sensor.DOMAIN,
|
|
|
|
{
|
|
|
|
sensor.DOMAIN: {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "test",
|
|
|
|
"state_topic": "test-topic",
|
|
|
|
"expire_after": 4,
|
|
|
|
"force_update": True,
|
|
|
|
"availability_topic": "availability-topic",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get("sensor.test")
|
|
|
|
assert state.state == STATE_UNAVAILABLE
|
|
|
|
|
|
|
|
async_fire_mqtt_message(hass, "availability-topic", "online")
|
|
|
|
|
|
|
|
# State should be unavailable since expire_after is defined and > 0
|
|
|
|
state = hass.states.get("sensor.test")
|
|
|
|
assert state.state == STATE_UNAVAILABLE
|
|
|
|
|
|
|
|
await expires_helper(hass, mqtt_mock, caplog)
|
|
|
|
|
|
|
|
|
2020-10-05 19:39:44 +00:00
|
|
|
async def test_setting_sensor_value_expires(hass, mqtt_mock, caplog):
|
2019-04-19 21:52:23 +00:00
|
|
|
"""Test the expiration of the value."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
sensor.DOMAIN,
|
|
|
|
{
|
|
|
|
sensor.DOMAIN: {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "test",
|
|
|
|
"state_topic": "test-topic",
|
|
|
|
"unit_of_measurement": "fav unit",
|
|
|
|
"expire_after": "4",
|
|
|
|
"force_update": True,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2020-06-01 05:18:30 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-04-19 21:52:23 +00:00
|
|
|
|
2020-07-08 23:20:19 +00:00
|
|
|
# State should be unavailable since expire_after is defined and > 0
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("sensor.test")
|
2020-07-08 23:20:19 +00:00
|
|
|
assert state.state == STATE_UNAVAILABLE
|
|
|
|
|
|
|
|
await expires_helper(hass, mqtt_mock, caplog)
|
|
|
|
|
2019-04-19 21:52:23 +00:00
|
|
|
|
2020-07-08 23:20:19 +00:00
|
|
|
async def expires_helper(hass, mqtt_mock, caplog):
|
|
|
|
"""Run the basic expiry code."""
|
2020-06-29 16:39:24 +00:00
|
|
|
realnow = dt_util.utcnow()
|
|
|
|
now = datetime(realnow.year + 1, 1, 1, 1, tzinfo=dt_util.UTC)
|
2020-01-02 19:17:10 +00:00
|
|
|
with patch(("homeassistant.helpers.event.dt_util.utcnow"), return_value=now):
|
2019-04-19 21:52:23 +00:00
|
|
|
async_fire_time_changed(hass, now)
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "test-topic", "100")
|
2019-04-19 21:52:23 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# Value was set correctly.
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("sensor.test")
|
|
|
|
assert state.state == "100"
|
2019-04-19 21:52:23 +00:00
|
|
|
|
|
|
|
# Time jump +3s
|
|
|
|
now = now + timedelta(seconds=3)
|
|
|
|
async_fire_time_changed(hass, now)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# Value is not yet expired
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("sensor.test")
|
|
|
|
assert state.state == "100"
|
2019-04-19 21:52:23 +00:00
|
|
|
|
|
|
|
# Next message resets timer
|
2020-01-02 19:17:10 +00:00
|
|
|
with patch(("homeassistant.helpers.event.dt_util.utcnow"), return_value=now):
|
2019-04-19 21:52:23 +00:00
|
|
|
async_fire_time_changed(hass, now)
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "test-topic", "101")
|
2019-04-19 21:52:23 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# Value was updated correctly.
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("sensor.test")
|
|
|
|
assert state.state == "101"
|
2019-04-19 21:52:23 +00:00
|
|
|
|
|
|
|
# Time jump +3s
|
|
|
|
now = now + timedelta(seconds=3)
|
|
|
|
async_fire_time_changed(hass, now)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# Value is not yet expired
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("sensor.test")
|
|
|
|
assert state.state == "101"
|
2019-04-19 21:52:23 +00:00
|
|
|
|
|
|
|
# Time jump +2s
|
|
|
|
now = now + timedelta(seconds=2)
|
|
|
|
async_fire_time_changed(hass, now)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# Value is expired now
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("sensor.test")
|
2020-07-08 23:20:19 +00:00
|
|
|
assert state.state == STATE_UNAVAILABLE
|
2019-04-19 21:52:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_setting_sensor_value_via_mqtt_json_message(hass, mqtt_mock):
|
|
|
|
"""Test the setting of the value via MQTT with JSON payload."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
sensor.DOMAIN,
|
|
|
|
{
|
|
|
|
sensor.DOMAIN: {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "test",
|
|
|
|
"state_topic": "test-topic",
|
|
|
|
"unit_of_measurement": "fav unit",
|
|
|
|
"value_template": "{{ value_json.val }}",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2020-06-01 05:18:30 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-04-19 21:52:23 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "test-topic", '{ "val": "100" }')
|
|
|
|
state = hass.states.get("sensor.test")
|
2019-04-19 21:52:23 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.state == "100"
|
2019-04-19 21:52:23 +00:00
|
|
|
|
|
|
|
|
2021-05-26 09:50:29 +00:00
|
|
|
async def test_setting_sensor_last_reset_via_mqtt_message(hass, mqtt_mock):
|
|
|
|
"""Test the setting of the last_reset property via MQTT."""
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
sensor.DOMAIN,
|
|
|
|
{
|
|
|
|
sensor.DOMAIN: {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "test",
|
|
|
|
"state_topic": "test-topic",
|
|
|
|
"unit_of_measurement": "fav unit",
|
|
|
|
"last_reset_topic": "last-reset-topic",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
async_fire_mqtt_message(hass, "last-reset-topic", "2020-01-02 08:11:00")
|
|
|
|
state = hass.states.get("sensor.test")
|
|
|
|
assert state.attributes.get("last_reset") == "2020-01-02T08:11:00"
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("datestring", ["2020-21-02 08:11:00", "Hello there!"])
|
|
|
|
async def test_setting_sensor_bad_last_reset_via_mqtt_message(
|
|
|
|
hass, caplog, datestring, mqtt_mock
|
|
|
|
):
|
|
|
|
"""Test the setting of the last_reset property via MQTT."""
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
sensor.DOMAIN,
|
|
|
|
{
|
|
|
|
sensor.DOMAIN: {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "test",
|
|
|
|
"state_topic": "test-topic",
|
|
|
|
"unit_of_measurement": "fav unit",
|
|
|
|
"last_reset_topic": "last-reset-topic",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
async_fire_mqtt_message(hass, "last-reset-topic", datestring)
|
|
|
|
state = hass.states.get("sensor.test")
|
|
|
|
assert state.attributes.get("last_reset") is None
|
|
|
|
assert "Invalid last_reset message" in caplog.text
|
|
|
|
|
|
|
|
|
|
|
|
async def test_setting_sensor_empty_last_reset_via_mqtt_message(
|
|
|
|
hass, caplog, mqtt_mock
|
|
|
|
):
|
|
|
|
"""Test the setting of the last_reset property via MQTT."""
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
sensor.DOMAIN,
|
|
|
|
{
|
|
|
|
sensor.DOMAIN: {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "test",
|
|
|
|
"state_topic": "test-topic",
|
|
|
|
"unit_of_measurement": "fav unit",
|
|
|
|
"last_reset_topic": "last-reset-topic",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
async_fire_mqtt_message(hass, "last-reset-topic", "")
|
|
|
|
state = hass.states.get("sensor.test")
|
|
|
|
assert state.attributes.get("last_reset") is None
|
|
|
|
assert "Ignoring empty last_reset message" in caplog.text
|
|
|
|
|
|
|
|
|
|
|
|
async def test_setting_sensor_last_reset_via_mqtt_json_message(hass, mqtt_mock):
|
|
|
|
"""Test the setting of the value via MQTT with JSON payload."""
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
sensor.DOMAIN,
|
|
|
|
{
|
|
|
|
sensor.DOMAIN: {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "test",
|
|
|
|
"state_topic": "test-topic",
|
|
|
|
"unit_of_measurement": "fav unit",
|
|
|
|
"last_reset_topic": "last-reset-topic",
|
|
|
|
"last_reset_value_template": "{{ value_json.last_reset }}",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
async_fire_mqtt_message(
|
|
|
|
hass, "last-reset-topic", '{ "last_reset": "2020-01-02 08:11:00" }'
|
|
|
|
)
|
|
|
|
state = hass.states.get("sensor.test")
|
|
|
|
assert state.attributes.get("last_reset") == "2020-01-02T08:11:00"
|
|
|
|
|
|
|
|
|
2019-04-19 21:52:23 +00:00
|
|
|
async def test_force_update_disabled(hass, mqtt_mock):
|
|
|
|
"""Test force update option."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
sensor.DOMAIN,
|
|
|
|
{
|
|
|
|
sensor.DOMAIN: {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "test",
|
|
|
|
"state_topic": "test-topic",
|
|
|
|
"unit_of_measurement": "fav unit",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2020-06-01 05:18:30 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-04-19 21:52:23 +00:00
|
|
|
|
|
|
|
events = []
|
|
|
|
|
|
|
|
@ha.callback
|
|
|
|
def callback(event):
|
|
|
|
events.append(event)
|
|
|
|
|
|
|
|
hass.bus.async_listen(EVENT_STATE_CHANGED, callback)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "test-topic", "100")
|
2019-04-19 21:52:23 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-04-22 12:48:50 +00:00
|
|
|
assert len(events) == 1
|
2019-04-19 21:52:23 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "test-topic", "100")
|
2019-04-19 21:52:23 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-04-22 12:48:50 +00:00
|
|
|
assert len(events) == 1
|
2019-04-19 21:52:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_force_update_enabled(hass, mqtt_mock):
|
|
|
|
"""Test force update option."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
sensor.DOMAIN,
|
|
|
|
{
|
|
|
|
sensor.DOMAIN: {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "test",
|
|
|
|
"state_topic": "test-topic",
|
|
|
|
"unit_of_measurement": "fav unit",
|
|
|
|
"force_update": True,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2020-06-01 05:18:30 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-04-19 21:52:23 +00:00
|
|
|
|
|
|
|
events = []
|
|
|
|
|
|
|
|
@ha.callback
|
|
|
|
def callback(event):
|
|
|
|
events.append(event)
|
|
|
|
|
|
|
|
hass.bus.async_listen(EVENT_STATE_CHANGED, callback)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "test-topic", "100")
|
2019-04-19 21:52:23 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-04-22 12:48:50 +00:00
|
|
|
assert len(events) == 1
|
2019-04-19 21:52:23 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_fire_mqtt_message(hass, "test-topic", "100")
|
2019-04-19 21:52:23 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-04-22 12:48:50 +00:00
|
|
|
assert len(events) == 2
|
2019-04-19 21:52:23 +00:00
|
|
|
|
|
|
|
|
2020-06-07 07:21:16 +00:00
|
|
|
async def test_availability_when_connection_lost(hass, mqtt_mock):
|
|
|
|
"""Test availability after MQTT disconnection."""
|
|
|
|
await help_test_availability_when_connection_lost(
|
|
|
|
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-03-17 07:09:19 +00:00
|
|
|
async def test_availability_without_topic(hass, mqtt_mock):
|
|
|
|
"""Test availability without defined availability topic."""
|
|
|
|
await help_test_availability_without_topic(
|
|
|
|
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-04-19 21:52:23 +00:00
|
|
|
|
|
|
|
|
2020-03-17 07:09:19 +00:00
|
|
|
async def test_default_availability_payload(hass, mqtt_mock):
|
|
|
|
"""Test availability by default payload with defined topic."""
|
|
|
|
await help_test_default_availability_payload(
|
|
|
|
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
|
|
|
|
)
|
2019-04-19 21:52:23 +00:00
|
|
|
|
|
|
|
|
2020-07-04 15:48:34 +00:00
|
|
|
async def test_default_availability_list_payload(hass, mqtt_mock):
|
|
|
|
"""Test availability by default payload with defined topic."""
|
|
|
|
await help_test_default_availability_list_payload(
|
|
|
|
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-01-11 15:04:22 +00:00
|
|
|
async def test_default_availability_list_payload_all(hass, mqtt_mock):
|
|
|
|
"""Test availability by default payload with defined topic."""
|
|
|
|
await help_test_default_availability_list_payload_all(
|
|
|
|
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_default_availability_list_payload_any(hass, mqtt_mock):
|
|
|
|
"""Test availability by default payload with defined topic."""
|
|
|
|
await help_test_default_availability_list_payload_any(
|
|
|
|
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-07-04 15:48:34 +00:00
|
|
|
async def test_default_availability_list_single(hass, mqtt_mock, caplog):
|
|
|
|
"""Test availability list and availability_topic are mutually exclusive."""
|
|
|
|
await help_test_default_availability_list_single(
|
|
|
|
hass, mqtt_mock, caplog, sensor.DOMAIN, DEFAULT_CONFIG
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-04-19 21:52:23 +00:00
|
|
|
async def test_custom_availability_payload(hass, mqtt_mock):
|
|
|
|
"""Test availability by custom payload with defined topic."""
|
2020-03-17 07:09:19 +00:00
|
|
|
await help_test_custom_availability_payload(
|
|
|
|
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-04-19 21:52:23 +00:00
|
|
|
|
|
|
|
|
2020-07-04 15:48:34 +00:00
|
|
|
async def test_discovery_update_availability(hass, mqtt_mock):
|
|
|
|
"""Test availability discovery update."""
|
|
|
|
await help_test_discovery_update_availability(
|
|
|
|
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-04-19 21:52:23 +00:00
|
|
|
async def test_invalid_device_class(hass, mqtt_mock):
|
|
|
|
"""Test device_class option with invalid value."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
sensor.DOMAIN,
|
|
|
|
{
|
|
|
|
sensor.DOMAIN: {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "test",
|
|
|
|
"state_topic": "test-topic",
|
|
|
|
"device_class": "foobarnotreal",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2020-06-01 05:18:30 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-04-19 21:52:23 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("sensor.test")
|
2019-04-19 21:52:23 +00:00
|
|
|
assert state is None
|
|
|
|
|
|
|
|
|
|
|
|
async def test_valid_device_class(hass, mqtt_mock):
|
|
|
|
"""Test device_class option with valid values."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
"sensor",
|
|
|
|
{
|
|
|
|
"sensor": [
|
|
|
|
{
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "Test 1",
|
|
|
|
"state_topic": "test-topic",
|
|
|
|
"device_class": "temperature",
|
|
|
|
},
|
|
|
|
{"platform": "mqtt", "name": "Test 2", "state_topic": "test-topic"},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
)
|
2019-04-19 21:52:23 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("sensor.test_1")
|
|
|
|
assert state.attributes["device_class"] == "temperature"
|
|
|
|
state = hass.states.get("sensor.test_2")
|
|
|
|
assert "device_class" not in state.attributes
|
2018-08-18 11:34:33 +00:00
|
|
|
|
|
|
|
|
2021-05-21 15:37:26 +00:00
|
|
|
async def test_invalid_state_class(hass, mqtt_mock):
|
|
|
|
"""Test state_class option with invalid value."""
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
sensor.DOMAIN,
|
|
|
|
{
|
|
|
|
sensor.DOMAIN: {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "test",
|
|
|
|
"state_topic": "test-topic",
|
|
|
|
"state_class": "foobarnotreal",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get("sensor.test")
|
|
|
|
assert state is None
|
|
|
|
|
|
|
|
|
|
|
|
async def test_valid_state_class(hass, mqtt_mock):
|
|
|
|
"""Test state_class option with valid values."""
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
"sensor",
|
|
|
|
{
|
|
|
|
"sensor": [
|
|
|
|
{
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "Test 1",
|
|
|
|
"state_topic": "test-topic",
|
|
|
|
"state_class": "measurement",
|
|
|
|
},
|
|
|
|
{"platform": "mqtt", "name": "Test 2", "state_topic": "test-topic"},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get("sensor.test_1")
|
|
|
|
assert state.attributes["state_class"] == "measurement"
|
|
|
|
state = hass.states.get("sensor.test_2")
|
|
|
|
assert "state_class" not in state.attributes
|
|
|
|
|
|
|
|
|
2019-01-17 18:53:52 +00:00
|
|
|
async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock):
|
|
|
|
"""Test the setting of attribute via MQTT with JSON payload."""
|
2020-03-09 16:40:00 +00:00
|
|
|
await help_test_setting_attribute_via_mqtt_json_message(
|
2020-03-17 07:09:19 +00:00
|
|
|
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-01-17 18:53:52 +00:00
|
|
|
|
|
|
|
|
2019-04-19 03:55:10 +00:00
|
|
|
async def test_setting_attribute_with_template(hass, mqtt_mock):
|
|
|
|
"""Test the setting of attribute via MQTT with JSON payload."""
|
2020-03-17 07:09:19 +00:00
|
|
|
await help_test_setting_attribute_with_template(
|
|
|
|
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-04-19 03:55:10 +00:00
|
|
|
|
|
|
|
|
2019-01-17 18:53:52 +00:00
|
|
|
async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog):
|
|
|
|
"""Test attributes get extracted from a JSON result."""
|
2020-03-09 16:40:00 +00:00
|
|
|
await help_test_update_with_json_attrs_not_dict(
|
2020-03-17 07:09:19 +00:00
|
|
|
hass, mqtt_mock, caplog, sensor.DOMAIN, DEFAULT_CONFIG
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-01-17 18:53:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_update_with_json_attrs_bad_JSON(hass, mqtt_mock, caplog):
|
|
|
|
"""Test attributes get extracted from a JSON result."""
|
2020-03-09 16:40:00 +00:00
|
|
|
await help_test_update_with_json_attrs_bad_JSON(
|
2020-03-17 07:09:19 +00:00
|
|
|
hass, mqtt_mock, caplog, sensor.DOMAIN, DEFAULT_CONFIG
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-01-17 18:53:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_discovery_update_attr(hass, mqtt_mock, caplog):
|
|
|
|
"""Test update of discovered MQTTAttributes."""
|
2020-03-09 16:40:00 +00:00
|
|
|
await help_test_discovery_update_attr(
|
2020-03-17 07:09:19 +00:00
|
|
|
hass, mqtt_mock, caplog, sensor.DOMAIN, DEFAULT_CONFIG
|
2020-03-09 16:40:00 +00:00
|
|
|
)
|
2019-01-17 18:53:52 +00:00
|
|
|
|
|
|
|
|
2020-06-22 21:59:50 +00:00
|
|
|
async def test_unique_id(hass, mqtt_mock):
|
2018-08-18 11:34:33 +00:00
|
|
|
"""Test unique id option only creates one sensor per unique_id."""
|
2020-03-09 16:40:00 +00:00
|
|
|
config = {
|
|
|
|
sensor.DOMAIN: [
|
|
|
|
{
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "Test 1",
|
|
|
|
"state_topic": "test-topic",
|
|
|
|
"unique_id": "TOTALLY_UNIQUE",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "Test 2",
|
|
|
|
"state_topic": "test-topic",
|
|
|
|
"unique_id": "TOTALLY_UNIQUE",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
2020-06-22 21:59:50 +00:00
|
|
|
await help_test_unique_id(hass, mqtt_mock, sensor.DOMAIN, config)
|
2018-09-25 17:32:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_discovery_removal_sensor(hass, mqtt_mock, caplog):
|
|
|
|
"""Test removal of discovered sensor."""
|
2020-08-06 08:43:47 +00:00
|
|
|
data = '{ "name": "test", "state_topic": "test_topic" }'
|
2020-03-09 16:40:00 +00:00
|
|
|
await help_test_discovery_removal(hass, mqtt_mock, caplog, sensor.DOMAIN, data)
|
2018-10-08 10:53:30 +00:00
|
|
|
|
|
|
|
|
2020-09-10 18:52:23 +00:00
|
|
|
async def test_discovery_update_sensor_topic_template(hass, mqtt_mock, caplog):
|
2019-01-03 14:51:18 +00:00
|
|
|
"""Test update of discovered sensor."""
|
2020-09-10 18:52:23 +00:00
|
|
|
config = {"name": "test", "state_topic": "test_topic"}
|
|
|
|
config1 = copy.deepcopy(config)
|
|
|
|
config2 = copy.deepcopy(config)
|
|
|
|
config1["name"] = "Beer"
|
|
|
|
config2["name"] = "Milk"
|
|
|
|
config1["state_topic"] = "sensor/state1"
|
|
|
|
config2["state_topic"] = "sensor/state2"
|
|
|
|
config1["value_template"] = "{{ value_json.state | int }}"
|
|
|
|
config2["value_template"] = "{{ value_json.state | int * 2 }}"
|
|
|
|
|
|
|
|
state_data1 = [
|
|
|
|
([("sensor/state1", '{"state":100}')], "100", None),
|
|
|
|
]
|
|
|
|
state_data2 = [
|
|
|
|
([("sensor/state1", '{"state":1000}')], "100", None),
|
|
|
|
([("sensor/state1", '{"state":1000}')], "100", None),
|
|
|
|
([("sensor/state2", '{"state":100}')], "200", None),
|
|
|
|
]
|
|
|
|
|
|
|
|
data1 = json.dumps(config1)
|
|
|
|
data2 = json.dumps(config2)
|
|
|
|
await help_test_discovery_update(
|
|
|
|
hass,
|
|
|
|
mqtt_mock,
|
|
|
|
caplog,
|
|
|
|
sensor.DOMAIN,
|
|
|
|
data1,
|
|
|
|
data2,
|
|
|
|
state_data1=state_data1,
|
|
|
|
state_data2=state_data2,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_discovery_update_sensor_template(hass, mqtt_mock, caplog):
|
|
|
|
"""Test update of discovered sensor."""
|
|
|
|
config = {"name": "test", "state_topic": "test_topic"}
|
|
|
|
config1 = copy.deepcopy(config)
|
|
|
|
config2 = copy.deepcopy(config)
|
|
|
|
config1["name"] = "Beer"
|
|
|
|
config2["name"] = "Milk"
|
|
|
|
config1["state_topic"] = "sensor/state1"
|
|
|
|
config2["state_topic"] = "sensor/state1"
|
|
|
|
config1["value_template"] = "{{ value_json.state | int }}"
|
|
|
|
config2["value_template"] = "{{ value_json.state | int * 2 }}"
|
|
|
|
|
|
|
|
state_data1 = [
|
|
|
|
([("sensor/state1", '{"state":100}')], "100", None),
|
|
|
|
]
|
|
|
|
state_data2 = [
|
|
|
|
([("sensor/state1", '{"state":100}')], "200", None),
|
|
|
|
]
|
|
|
|
|
|
|
|
data1 = json.dumps(config1)
|
|
|
|
data2 = json.dumps(config2)
|
2020-03-09 16:40:00 +00:00
|
|
|
await help_test_discovery_update(
|
2020-09-10 18:52:23 +00:00
|
|
|
hass,
|
|
|
|
mqtt_mock,
|
|
|
|
caplog,
|
|
|
|
sensor.DOMAIN,
|
|
|
|
data1,
|
|
|
|
data2,
|
|
|
|
state_data1=state_data1,
|
|
|
|
state_data2=state_data2,
|
2020-03-09 16:40:00 +00:00
|
|
|
)
|
2018-11-27 10:23:47 +00:00
|
|
|
|
|
|
|
|
2020-08-06 08:43:47 +00:00
|
|
|
async def test_discovery_update_unchanged_sensor(hass, mqtt_mock, caplog):
|
|
|
|
"""Test update of discovered sensor."""
|
|
|
|
data1 = '{ "name": "Beer", "state_topic": "test_topic" }'
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.mqtt.sensor.MqttSensor.discovery_update"
|
|
|
|
) as discovery_update:
|
|
|
|
await help_test_discovery_update_unchanged(
|
|
|
|
hass, mqtt_mock, caplog, sensor.DOMAIN, data1, discovery_update
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-05-06 21:14:57 +00:00
|
|
|
@pytest.mark.no_fail_on_log_exception
|
2019-01-03 14:51:18 +00:00
|
|
|
async def test_discovery_broken(hass, mqtt_mock, caplog):
|
|
|
|
"""Test handling of bad discovery message."""
|
2020-08-06 08:43:47 +00:00
|
|
|
data1 = '{ "name": "Beer", "state_topic": "test_topic#" }'
|
|
|
|
data2 = '{ "name": "Milk", "state_topic": "test_topic" }'
|
2020-03-09 16:40:00 +00:00
|
|
|
await help_test_discovery_broken(
|
|
|
|
hass, mqtt_mock, caplog, sensor.DOMAIN, data1, data2
|
|
|
|
)
|
2019-01-03 14:51:18 +00:00
|
|
|
|
|
|
|
|
2020-03-17 07:09:19 +00:00
|
|
|
async def test_entity_device_info_with_connection(hass, mqtt_mock):
|
|
|
|
"""Test MQTT sensor device registry integration."""
|
|
|
|
await help_test_entity_device_info_with_connection(
|
|
|
|
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-10-08 10:53:30 +00:00
|
|
|
async def test_entity_device_info_with_identifier(hass, mqtt_mock):
|
|
|
|
"""Test MQTT sensor device registry integration."""
|
2020-03-09 16:40:00 +00:00
|
|
|
await help_test_entity_device_info_with_identifier(
|
2020-03-17 07:09:19 +00:00
|
|
|
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
|
2020-03-09 16:40:00 +00:00
|
|
|
)
|
2018-12-19 13:05:24 +00:00
|
|
|
|
|
|
|
|
2019-01-26 18:48:18 +00:00
|
|
|
async def test_entity_device_info_update(hass, mqtt_mock):
|
|
|
|
"""Test device registry update."""
|
2020-03-11 16:34:19 +00:00
|
|
|
await help_test_entity_device_info_update(
|
2020-03-17 07:09:19 +00:00
|
|
|
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
|
2020-03-11 16:34:19 +00:00
|
|
|
)
|
2019-01-26 18:48:18 +00:00
|
|
|
|
|
|
|
|
2020-03-12 01:00:47 +00:00
|
|
|
async def test_entity_device_info_remove(hass, mqtt_mock):
|
|
|
|
"""Test device registry remove."""
|
2020-03-17 07:09:19 +00:00
|
|
|
await help_test_entity_device_info_remove(
|
|
|
|
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
|
|
|
|
)
|
2020-03-12 01:00:47 +00:00
|
|
|
|
|
|
|
|
2020-03-30 21:26:59 +00:00
|
|
|
async def test_entity_id_update_subscriptions(hass, mqtt_mock):
|
2018-12-19 13:05:24 +00:00
|
|
|
"""Test MQTT subscriptions are managed when entity_id is updated."""
|
2020-03-30 21:26:59 +00:00
|
|
|
await help_test_entity_id_update_subscriptions(
|
|
|
|
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_entity_id_update_discovery_update(hass, mqtt_mock):
|
|
|
|
"""Test MQTT discovery update when entity_id is updated."""
|
|
|
|
await help_test_entity_id_update_discovery_update(
|
|
|
|
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
|
|
|
|
)
|
2019-01-11 21:11:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_entity_device_info_with_hub(hass, mqtt_mock):
|
|
|
|
"""Test MQTT sensor device registry integration."""
|
2021-03-09 13:32:08 +00:00
|
|
|
registry = dr.async_get(hass)
|
2019-01-11 21:11:13 +00:00
|
|
|
hub = registry.async_get_or_create(
|
2019-07-31 19:25:30 +00:00
|
|
|
config_entry_id="123",
|
2019-01-11 21:11:13 +00:00
|
|
|
connections=set(),
|
2019-07-31 19:25:30 +00:00
|
|
|
identifiers={("mqtt", "hub-id")},
|
|
|
|
manufacturer="manufacturer",
|
|
|
|
model="hub",
|
2019-01-11 21:11:13 +00:00
|
|
|
)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
data = json.dumps(
|
|
|
|
{
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "Test 1",
|
|
|
|
"state_topic": "test-topic",
|
|
|
|
"device": {"identifiers": ["helloworld"], "via_device": "hub-id"},
|
|
|
|
"unique_id": "veryunique",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
async_fire_mqtt_message(hass, "homeassistant/sensor/bla/config", data)
|
2019-01-11 21:11:13 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2021-01-07 12:49:45 +00:00
|
|
|
device = registry.async_get_device({("mqtt", "helloworld")})
|
2019-01-11 21:11:13 +00:00
|
|
|
assert device is not None
|
2019-06-10 16:10:44 +00:00
|
|
|
assert device.via_device_id == hub.id
|
2020-04-01 17:00:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_entity_debug_info(hass, mqtt_mock):
|
|
|
|
"""Test MQTT sensor debug info."""
|
|
|
|
await help_test_entity_debug_info(hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_entity_debug_info_max_messages(hass, mqtt_mock):
|
|
|
|
"""Test MQTT sensor debug info."""
|
|
|
|
await help_test_entity_debug_info_max_messages(
|
|
|
|
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_entity_debug_info_message(hass, mqtt_mock):
|
|
|
|
"""Test MQTT debug info."""
|
|
|
|
await help_test_entity_debug_info_message(
|
|
|
|
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_entity_debug_info_remove(hass, mqtt_mock):
|
|
|
|
"""Test MQTT sensor debug info."""
|
|
|
|
await help_test_entity_debug_info_remove(
|
|
|
|
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_entity_debug_info_update_entity_id(hass, mqtt_mock):
|
|
|
|
"""Test MQTT sensor debug info."""
|
|
|
|
await help_test_entity_debug_info_update_entity_id(
|
|
|
|
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
|
|
|
|
)
|
2021-03-29 22:09:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_entity_disabled_by_default(hass, mqtt_mock):
|
|
|
|
"""Test entity disabled by default."""
|
|
|
|
await help_test_entity_disabled_by_default(
|
|
|
|
hass, mqtt_mock, sensor.DOMAIN, DEFAULT_CONFIG
|
|
|
|
)
|
2021-05-19 12:38:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_value_template_with_entity_id(hass, mqtt_mock):
|
|
|
|
"""Test the access to attributes in value_template via the entity_id."""
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
sensor.DOMAIN,
|
|
|
|
{
|
|
|
|
sensor.DOMAIN: {
|
|
|
|
"platform": "mqtt",
|
|
|
|
"name": "test",
|
|
|
|
"state_topic": "test-topic",
|
|
|
|
"unit_of_measurement": "fav unit",
|
|
|
|
"value_template": '\
|
|
|
|
{% if state_attr(entity_id, "friendly_name") == "test" %} \
|
|
|
|
{{ value | int + 1 }} \
|
|
|
|
{% else %} \
|
|
|
|
{{ value }} \
|
|
|
|
{% endif %}',
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
async_fire_mqtt_message(hass, "test-topic", "100")
|
|
|
|
state = hass.states.get("sensor.test")
|
|
|
|
|
|
|
|
assert state.state == "101"
|