Prepare MQTT platform tests part7 (#90130)

* Tests select

* Tests sensor

* Deduplicate test code
This commit is contained in:
Jan Bouwhuis 2023-03-24 08:42:00 +01:00 committed by GitHub
parent 0570405a3c
commit f2b4c95a04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 521 additions and 435 deletions

View File

@ -1,4 +1,5 @@
"""The tests for mqtt select component.""" """The tests for mqtt select component."""
from collections.abc import Generator
import copy import copy
import json import json
from typing import Any from typing import Any
@ -21,7 +22,7 @@ from homeassistant.const import (
Platform, Platform,
) )
from homeassistant.core import HomeAssistant, State from homeassistant.core import HomeAssistant, State
from homeassistant.setup import async_setup_component from homeassistant.helpers.typing import ConfigType
from .test_common import ( from .test_common import (
help_test_availability_when_connection_lost, help_test_availability_when_connection_lost,
@ -74,27 +75,35 @@ def select_platform_only():
yield yield
async def test_run_select_setup( def _test_run_select_setup_params(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator topic: str,
) -> None: ) -> Generator[tuple[ConfigType, str], None]:
"""Test that it fetches the given payload.""" yield (
topic = "test/select"
await async_setup_component(
hass,
mqtt.DOMAIN,
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
select.DOMAIN: { select.DOMAIN: {
"state_topic": topic, "state_topic": topic,
"command_topic": topic, "command_topic": "test/select_cmd",
"name": "Test Select", "name": "Test Select",
"options": ["milk", "beer"], "options": ["milk", "beer"],
} }
} }
}, },
topic,
) )
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
@pytest.mark.parametrize(
("hass_config", "topic"),
_test_run_select_setup_params("test/select_stat"),
)
async def test_run_select_setup(
hass: HomeAssistant,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
topic: str,
) -> None:
"""Test that it fetches the given payload."""
await mqtt_mock_entry_no_yaml_config()
async_fire_mqtt_message(hass, topic, "milk") async_fire_mqtt_message(hass, topic, "milk")
@ -111,44 +120,43 @@ async def test_run_select_setup(
assert state.state == "beer" assert state.state == "beer"
async def test_value_template( @pytest.mark.parametrize(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator "hass_config",
) -> None: [
"""Test that it fetches the given payload with a template."""
topic = "test/select"
await async_setup_component(
hass,
mqtt.DOMAIN,
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
select.DOMAIN: { select.DOMAIN: {
"state_topic": topic, "state_topic": "test/select_stat",
"command_topic": topic, "command_topic": "test/select_cmd",
"name": "Test Select", "name": "Test Select",
"options": ["milk", "beer"], "options": ["milk", "beer"],
"value_template": "{{ value_json.val }}", "value_template": "{{ value_json.val }}",
} }
} }
}, }
],
) )
await hass.async_block_till_done() async def test_value_template(
await mqtt_mock_entry_with_yaml_config() hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test that it fetches the given payload with a template."""
await mqtt_mock_entry_no_yaml_config()
async_fire_mqtt_message(hass, topic, '{"val":"milk"}') async_fire_mqtt_message(hass, "test/select_stat", '{"val":"milk"}')
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get("select.test_select") state = hass.states.get("select.test_select")
assert state.state == "milk" assert state.state == "milk"
async_fire_mqtt_message(hass, topic, '{"val":"beer"}') async_fire_mqtt_message(hass, "test/select_stat", '{"val":"beer"}')
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get("select.test_select") state = hass.states.get("select.test_select")
assert state.state == "beer" assert state.state == "beer"
async_fire_mqtt_message(hass, topic, '{"val": null}') async_fire_mqtt_message(hass, "test/select_stat", '{"val": null}')
await hass.async_block_till_done() await hass.async_block_till_done()
@ -156,30 +164,28 @@ async def test_value_template(
assert state.state == STATE_UNKNOWN assert state.state == STATE_UNKNOWN
async def test_run_select_service_optimistic( @pytest.mark.parametrize(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator "hass_config",
) -> None: [
"""Test that set_value service works in optimistic mode."""
topic = "test/select"
fake_state = State("select.test_select", "milk")
mock_restore_cache(hass, (fake_state,))
assert await async_setup_component(
hass,
mqtt.DOMAIN,
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
select.DOMAIN: { select.DOMAIN: {
"command_topic": topic, "command_topic": "test/select_cmd",
"name": "Test Select", "name": "Test Select",
"options": ["milk", "beer"], "options": ["milk", "beer"],
} }
} }
}, }
],
) )
await hass.async_block_till_done() async def test_run_select_service_optimistic(
mqtt_mock = await mqtt_mock_entry_with_yaml_config() hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test that set_value service works in optimistic mode."""
fake_state = State("select.test_select", "milk")
mock_restore_cache(hass, (fake_state,))
mqtt_mock = await mqtt_mock_entry_no_yaml_config()
state = hass.states.get("select.test_select") state = hass.states.get("select.test_select")
assert state.state == "milk" assert state.state == "milk"
@ -192,37 +198,35 @@ async def test_run_select_service_optimistic(
blocking=True, blocking=True,
) )
mqtt_mock.async_publish.assert_called_once_with(topic, "beer", 0, False) mqtt_mock.async_publish.assert_called_once_with("test/select_cmd", "beer", 0, False)
mqtt_mock.async_publish.reset_mock() mqtt_mock.async_publish.reset_mock()
state = hass.states.get("select.test_select") state = hass.states.get("select.test_select")
assert state.state == "beer" assert state.state == "beer"
async def test_run_select_service_optimistic_with_command_template( @pytest.mark.parametrize(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator "hass_config",
) -> None: [
"""Test that set_value service works in optimistic mode and with a command_template."""
topic = "test/select"
fake_state = State("select.test_select", "milk")
mock_restore_cache(hass, (fake_state,))
assert await async_setup_component(
hass,
mqtt.DOMAIN,
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
select.DOMAIN: { select.DOMAIN: {
"command_topic": topic, "command_topic": "test/select_cmd",
"name": "Test Select", "name": "Test Select",
"options": ["milk", "beer"], "options": ["milk", "beer"],
"command_template": '{"option": "{{ value }}"}', "command_template": '{"option": "{{ value }}"}',
} }
} }
}, }
],
) )
await hass.async_block_till_done() async def test_run_select_service_optimistic_with_command_template(
mqtt_mock = await mqtt_mock_entry_with_yaml_config() hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test that set_value service works in optimistic mode and with a command_template."""
fake_state = State("select.test_select", "milk")
mock_restore_cache(hass, (fake_state,))
mqtt_mock = await mqtt_mock_entry_no_yaml_config()
state = hass.states.get("select.test_select") state = hass.states.get("select.test_select")
assert state.state == "milk" assert state.state == "milk"
@ -236,36 +240,36 @@ async def test_run_select_service_optimistic_with_command_template(
) )
mqtt_mock.async_publish.assert_called_once_with( mqtt_mock.async_publish.assert_called_once_with(
topic, '{"option": "beer"}', 0, False "test/select_cmd", '{"option": "beer"}', 0, False
) )
mqtt_mock.async_publish.reset_mock() mqtt_mock.async_publish.reset_mock()
state = hass.states.get("select.test_select") state = hass.states.get("select.test_select")
assert state.state == "beer" assert state.state == "beer"
@pytest.mark.parametrize(
"hass_config",
[
{
mqtt.DOMAIN: {
select.DOMAIN: {
"command_topic": "test/select/set",
"state_topic": "test/select",
"name": "Test Select",
"options": ["milk", "beer"],
}
}
}
],
)
async def test_run_select_service( async def test_run_select_service(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator
) -> None: ) -> None:
"""Test that set_value service works in non optimistic mode.""" """Test that set_value service works in non optimistic mode."""
cmd_topic = "test/select/set" cmd_topic = "test/select/set"
state_topic = "test/select" state_topic = "test/select"
assert await async_setup_component( mqtt_mock = await mqtt_mock_entry_no_yaml_config()
hass,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
select.DOMAIN: {
"command_topic": cmd_topic,
"state_topic": state_topic,
"name": "Test Select",
"options": ["milk", "beer"],
}
}
},
)
await hass.async_block_till_done()
mqtt_mock = await mqtt_mock_entry_with_yaml_config()
async_fire_mqtt_message(hass, state_topic, "beer") async_fire_mqtt_message(hass, state_topic, "beer")
state = hass.states.get("select.test_select") state = hass.states.get("select.test_select")
@ -282,30 +286,30 @@ async def test_run_select_service(
assert state.state == "beer" assert state.state == "beer"
async def test_run_select_service_with_command_template( @pytest.mark.parametrize(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator "hass_config",
) -> None: [
"""Test that set_value service works in non optimistic mode and with a command_template."""
cmd_topic = "test/select/set"
state_topic = "test/select"
assert await async_setup_component(
hass,
mqtt.DOMAIN,
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
select.DOMAIN: { select.DOMAIN: {
"command_topic": cmd_topic, "command_topic": "test/select/set",
"state_topic": state_topic, "state_topic": "test/select",
"name": "Test Select", "name": "Test Select",
"options": ["milk", "beer"], "options": ["milk", "beer"],
"command_template": '{"option": "{{ value }}"}', "command_template": '{"option": "{{ value }}"}',
} }
} }
}, }
],
) )
await hass.async_block_till_done() async def test_run_select_service_with_command_template(
mqtt_mock = await mqtt_mock_entry_with_yaml_config() hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test that set_value service works in non optimistic mode and with a command_template."""
cmd_topic = "test/select/set"
state_topic = "test/select"
mqtt_mock = await mqtt_mock_entry_no_yaml_config()
async_fire_mqtt_message(hass, state_topic, "beer") async_fire_mqtt_message(hass, state_topic, "beer")
state = hass.states.get("select.test_select") state = hass.states.get("select.test_select")
@ -609,60 +613,65 @@ async def test_entity_debug_info_message(
) )
@pytest.mark.parametrize("options", [["milk", "beer"], ["milk"], []]) def _test_options_attributes_options_config(
async def test_options_attributes( request: tuple[list[str]],
hass: HomeAssistant, ) -> Generator[tuple[ConfigType, list[str]], None]:
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, for option in request:
options, yield (
) -> None:
"""Test options attribute."""
topic = "test/select"
await async_setup_component(
hass,
mqtt.DOMAIN,
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
select.DOMAIN: { select.DOMAIN: {
"state_topic": topic, "command_topic": "test/select/set",
"command_topic": topic, "state_topic": "test/select",
"name": "Test select", "name": "Test select",
"options": options, "options": option,
} }
} }
}, },
option,
) )
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
@pytest.mark.parametrize(
("hass_config", "options"),
_test_options_attributes_options_config((["milk", "beer"], ["milk"], [])),
)
async def test_options_attributes(
hass: HomeAssistant,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
options: list[str],
) -> None:
"""Test options attribute."""
await mqtt_mock_entry_no_yaml_config()
state = hass.states.get("select.test_select") state = hass.states.get("select.test_select")
assert state.attributes.get(ATTR_OPTIONS) == options assert state.attributes.get(ATTR_OPTIONS) == options
async def test_mqtt_payload_not_an_option_warning( @pytest.mark.parametrize(
hass: HomeAssistant, "hass_config",
caplog: pytest.LogCaptureFixture, [
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
) -> None:
"""Test warning for MQTT payload which is not a valid option."""
topic = "test/select"
await async_setup_component(
hass,
mqtt.DOMAIN,
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
select.DOMAIN: { select.DOMAIN: {
"state_topic": topic, "state_topic": "test/select_stat",
"command_topic": topic, "command_topic": "test/select_cmd",
"name": "Test Select", "name": "Test Select",
"options": ["milk", "beer"], "options": ["milk", "beer"],
} }
} }
}, }
],
) )
await hass.async_block_till_done() async def test_mqtt_payload_not_an_option_warning(
await mqtt_mock_entry_with_yaml_config() hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
) -> None:
"""Test warning for MQTT payload which is not a valid option."""
await mqtt_mock_entry_no_yaml_config()
async_fire_mqtt_message(hass, topic, "öl") async_fire_mqtt_message(hass, "test/select_stat", "öl")
await hass.async_block_till_done() await hass.async_block_till_done()

View File

@ -18,12 +18,13 @@ from homeassistant.const import (
Platform, Platform,
UnitOfTemperature, UnitOfTemperature,
) )
from homeassistant.core import HomeAssistant, State, callback from homeassistant.core import Event, HomeAssistant, State, callback
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.helpers.typing import ConfigType
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
from .test_common import ( from .test_common import (
help_custom_config,
help_test_availability_when_connection_lost, help_test_availability_when_connection_lost,
help_test_availability_without_topic, help_test_availability_without_topic,
help_test_custom_availability_payload, help_test_custom_availability_payload,
@ -82,13 +83,9 @@ def sensor_platform_only():
yield yield
async def test_setting_sensor_value_via_mqtt_message( @pytest.mark.parametrize(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator "hass_config",
) -> None: [
"""Test the setting of the value via MQTT."""
assert await async_setup_component(
hass,
mqtt.DOMAIN,
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
sensor.DOMAIN: { sensor.DOMAIN: {
@ -98,10 +95,14 @@ async def test_setting_sensor_value_via_mqtt_message(
"suggested_display_precision": 1, "suggested_display_precision": 1,
} }
} }
}, }
],
) )
await hass.async_block_till_done() async def test_setting_sensor_value_via_mqtt_message(
await mqtt_mock_entry_with_yaml_config() hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test the setting of the value via MQTT."""
await mqtt_mock_entry_no_yaml_config()
async_fire_mqtt_message(hass, "test-topic", "100.22") async_fire_mqtt_message(hass, "test-topic", "100.22")
state = hass.states.get("sensor.test") state = hass.states.get("sensor.test")
@ -113,64 +114,118 @@ async def test_setting_sensor_value_via_mqtt_message(
@pytest.mark.parametrize( @pytest.mark.parametrize(
("device_class", "native_value", "state_value", "log"), ("hass_config", "device_class", "native_value", "state_value", "log"),
[ [
(sensor.SensorDeviceClass.DATE, "2021-11-18", "2021-11-18", False),
(sensor.SensorDeviceClass.DATE, "invalid", STATE_UNKNOWN, True),
( (
help_custom_config(
sensor.DOMAIN,
DEFAULT_CONFIG,
({"device_class": sensor.SensorDeviceClass.DATE},),
),
sensor.SensorDeviceClass.DATE,
"2021-11-18",
"2021-11-18",
False,
),
(
help_custom_config(
sensor.DOMAIN,
DEFAULT_CONFIG,
({"device_class": sensor.SensorDeviceClass.DATE},),
),
sensor.SensorDeviceClass.DATE,
"invalid",
STATE_UNKNOWN,
True,
),
(
help_custom_config(
sensor.DOMAIN,
DEFAULT_CONFIG,
({"device_class": sensor.SensorDeviceClass.TIMESTAMP},),
),
sensor.SensorDeviceClass.TIMESTAMP, sensor.SensorDeviceClass.TIMESTAMP,
"2021-11-18T20:25:00+00:00", "2021-11-18T20:25:00+00:00",
"2021-11-18T20:25:00+00:00", "2021-11-18T20:25:00+00:00",
False, False,
), ),
( (
help_custom_config(
sensor.DOMAIN,
DEFAULT_CONFIG,
({"device_class": sensor.SensorDeviceClass.TIMESTAMP},),
),
sensor.SensorDeviceClass.TIMESTAMP, sensor.SensorDeviceClass.TIMESTAMP,
"2021-11-18 20:25:00+00:00", "2021-11-18 20:25:00+00:00",
"2021-11-18T20:25:00+00:00", "2021-11-18T20:25:00+00:00",
False, False,
), ),
( (
help_custom_config(
sensor.DOMAIN,
DEFAULT_CONFIG,
({"device_class": sensor.SensorDeviceClass.TIMESTAMP},),
),
sensor.SensorDeviceClass.TIMESTAMP, sensor.SensorDeviceClass.TIMESTAMP,
"2021-11-18 20:25:00+01:00", "2021-11-18 20:25:00+01:00",
"2021-11-18T19:25:00+00:00", "2021-11-18T19:25:00+00:00",
False, False,
), ),
( (
help_custom_config(
sensor.DOMAIN,
DEFAULT_CONFIG,
({"device_class": sensor.SensorDeviceClass.TIMESTAMP},),
),
sensor.SensorDeviceClass.TIMESTAMP, sensor.SensorDeviceClass.TIMESTAMP,
"2021-13-18T35:25:00+00:00", "2021-13-18T35:25:00+00:00",
STATE_UNKNOWN, STATE_UNKNOWN,
True, True,
), ),
(sensor.SensorDeviceClass.TIMESTAMP, "invalid", STATE_UNKNOWN, True), (
(sensor.SensorDeviceClass.ENUM, "some_value", "some_value", False), help_custom_config(
(None, "some_value", "some_value", False), sensor.DOMAIN,
DEFAULT_CONFIG,
({"device_class": sensor.SensorDeviceClass.TIMESTAMP},),
),
sensor.SensorDeviceClass.TIMESTAMP,
"invalid",
STATE_UNKNOWN,
True,
),
(
help_custom_config(
sensor.DOMAIN,
DEFAULT_CONFIG,
({"device_class": sensor.SensorDeviceClass.ENUM},),
),
sensor.SensorDeviceClass.ENUM,
"some_value",
"some_value",
False,
),
(
help_custom_config(
sensor.DOMAIN, DEFAULT_CONFIG, ({"device_class": None},)
),
None,
"some_value",
"some_value",
False,
),
], ],
) )
async def test_setting_sensor_native_value_handling_via_mqtt_message( async def test_setting_sensor_native_value_handling_via_mqtt_message(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
device_class, device_class: sensor.SensorDeviceClass | None,
native_value, native_value: str,
state_value, state_value: str,
log, log: bool,
) -> None: ) -> None:
"""Test the setting of the value via MQTT.""" """Test the setting of the value via MQTT."""
assert await async_setup_component( await mqtt_mock_entry_no_yaml_config()
hass,
mqtt.DOMAIN,
{
mqtt.DOMAIN: {
sensor.DOMAIN: {
"name": "test",
"state_topic": "test-topic",
"device_class": device_class,
}
}
},
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
async_fire_mqtt_message(hass, "test-topic", native_value) async_fire_mqtt_message(hass, "test-topic", native_value)
state = hass.states.get("sensor.test") state = hass.states.get("sensor.test")
@ -180,14 +235,9 @@ async def test_setting_sensor_native_value_handling_via_mqtt_message(
assert log == ("Invalid state message" in caplog.text) assert log == ("Invalid state message" in caplog.text)
async def test_setting_numeric_sensor_native_value_handling_via_mqtt_message( @pytest.mark.parametrize(
hass: HomeAssistant, "hass_config",
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, [
) -> None:
"""Test the setting of a numeric sensor value via MQTT."""
assert await async_setup_component(
hass,
mqtt.DOMAIN,
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
sensor.DOMAIN: { sensor.DOMAIN: {
@ -198,10 +248,16 @@ async def test_setting_numeric_sensor_native_value_handling_via_mqtt_message(
"unit_of_measurement": "W", "unit_of_measurement": "W",
} }
} }
}, }
],
) )
async def test_setting_numeric_sensor_native_value_handling_via_mqtt_message(
hass: HomeAssistant,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
) -> None:
"""Test the setting of a numeric sensor value via MQTT."""
await hass.async_block_till_done() await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config() await mqtt_mock_entry_no_yaml_config()
# float value # float value
async_fire_mqtt_message(hass, "test-topic", '{ "power": 45.3, "current": 5.24 }') async_fire_mqtt_message(hass, "test-topic", '{ "power": 45.3, "current": 5.24 }')
@ -235,15 +291,9 @@ async def test_setting_numeric_sensor_native_value_handling_via_mqtt_message(
assert state.state == "21" assert state.state == "21"
async def test_setting_sensor_value_expires_availability_topic( @pytest.mark.parametrize(
hass: HomeAssistant, "hass_config",
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, [
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test the expiration of the value."""
assert await async_setup_component(
hass,
mqtt.DOMAIN,
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
sensor.DOMAIN: { sensor.DOMAIN: {
@ -254,10 +304,14 @@ async def test_setting_sensor_value_expires_availability_topic(
"availability_topic": "availability-topic", "availability_topic": "availability-topic",
} }
} }
}, }
],
) )
await hass.async_block_till_done() async def test_setting_sensor_value_expires_availability_topic(
await mqtt_mock_entry_with_yaml_config() hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test the expiration of the value."""
await mqtt_mock_entry_no_yaml_config()
state = hass.states.get("sensor.test") state = hass.states.get("sensor.test")
assert state.state == STATE_UNAVAILABLE assert state.state == STATE_UNAVAILABLE
@ -268,18 +322,12 @@ async def test_setting_sensor_value_expires_availability_topic(
state = hass.states.get("sensor.test") state = hass.states.get("sensor.test")
assert state.state == STATE_UNAVAILABLE assert state.state == STATE_UNAVAILABLE
await expires_helper(hass, caplog) await expires_helper(hass)
async def test_setting_sensor_value_expires( @pytest.mark.parametrize(
hass: HomeAssistant, "hass_config",
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, [
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test the expiration of the value."""
assert await async_setup_component(
hass,
mqtt.DOMAIN,
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
sensor.DOMAIN: { sensor.DOMAIN: {
@ -290,19 +338,23 @@ async def test_setting_sensor_value_expires(
"force_update": True, "force_update": True,
} }
} }
}, }
],
) )
await hass.async_block_till_done() async def test_setting_sensor_value_expires(
await mqtt_mock_entry_with_yaml_config() hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test the expiration of the value."""
await mqtt_mock_entry_no_yaml_config()
# State should be unavailable since expire_after is defined and > 0 # State should be unavailable since expire_after is defined and > 0
state = hass.states.get("sensor.test") state = hass.states.get("sensor.test")
assert state.state == STATE_UNAVAILABLE assert state.state == STATE_UNAVAILABLE
await expires_helper(hass, caplog) await expires_helper(hass)
async def expires_helper(hass: HomeAssistant, caplog) -> None: async def expires_helper(hass: HomeAssistant) -> None:
"""Run the basic expiry code.""" """Run the basic expiry code."""
realnow = dt_util.utcnow() realnow = dt_util.utcnow()
now = datetime(realnow.year + 1, 1, 1, 1, tzinfo=dt_util.UTC) now = datetime(realnow.year + 1, 1, 1, 1, tzinfo=dt_util.UTC)
@ -353,13 +405,9 @@ async def expires_helper(hass: HomeAssistant, caplog) -> None:
assert state.state == STATE_UNAVAILABLE assert state.state == STATE_UNAVAILABLE
async def test_setting_sensor_value_via_mqtt_json_message( @pytest.mark.parametrize(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator "hass_config",
) -> None: [
"""Test the setting of the value via MQTT with JSON payload."""
assert await async_setup_component(
hass,
mqtt.DOMAIN,
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
sensor.DOMAIN: { sensor.DOMAIN: {
@ -368,10 +416,14 @@ async def test_setting_sensor_value_via_mqtt_json_message(
"value_template": "{{ value_json.val }}", "value_template": "{{ value_json.val }}",
} }
} }
}, }
],
) )
await hass.async_block_till_done() async def test_setting_sensor_value_via_mqtt_json_message(
await mqtt_mock_entry_with_yaml_config() hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test the setting of the value via MQTT with JSON payload."""
await mqtt_mock_entry_no_yaml_config()
async_fire_mqtt_message(hass, "test-topic", '{ "val": "100" }') async_fire_mqtt_message(hass, "test-topic", '{ "val": "100" }')
state = hass.states.get("sensor.test") state = hass.states.get("sensor.test")
@ -385,13 +437,9 @@ async def test_setting_sensor_value_via_mqtt_json_message(
assert state.state == "" assert state.state == ""
async def test_setting_sensor_value_via_mqtt_json_message_and_default_current_state( @pytest.mark.parametrize(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator "hass_config",
) -> None: [
"""Test the setting of the value via MQTT with fall back to current state."""
assert await async_setup_component(
hass,
mqtt.DOMAIN,
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
sensor.DOMAIN: { sensor.DOMAIN: {
@ -400,10 +448,14 @@ async def test_setting_sensor_value_via_mqtt_json_message_and_default_current_st
"value_template": "{{ value_json.val | is_defined }}-{{ value_json.par }}", "value_template": "{{ value_json.val | is_defined }}-{{ value_json.par }}",
} }
} }
}, }
],
) )
await hass.async_block_till_done() async def test_setting_sensor_value_via_mqtt_json_message_and_default_current_state(
await mqtt_mock_entry_with_yaml_config() hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test the setting of the value via MQTT with fall back to current state."""
await mqtt_mock_entry_no_yaml_config()
async_fire_mqtt_message( async_fire_mqtt_message(
hass, "test-topic", '{ "val": "valcontent", "par": "parcontent" }' hass, "test-topic", '{ "val": "valcontent", "par": "parcontent" }'
@ -418,15 +470,9 @@ async def test_setting_sensor_value_via_mqtt_json_message_and_default_current_st
assert state.state == "valcontent-parcontent" assert state.state == "valcontent-parcontent"
async def test_setting_sensor_last_reset_via_mqtt_message( @pytest.mark.parametrize(
hass: HomeAssistant, "hass_config",
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, [
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test the setting of the last_reset property via MQTT."""
assert await async_setup_component(
hass,
mqtt.DOMAIN,
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
sensor.DOMAIN: { sensor.DOMAIN: {
@ -437,10 +483,16 @@ async def test_setting_sensor_last_reset_via_mqtt_message(
"last_reset_topic": "last-reset-topic", "last_reset_topic": "last-reset-topic",
} }
} }
}, }
],
) )
await hass.async_block_till_done() async def test_setting_sensor_last_reset_via_mqtt_message(
await mqtt_mock_entry_with_yaml_config() hass: HomeAssistant,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test the setting of the last_reset property via MQTT."""
await mqtt_mock_entry_no_yaml_config()
async_fire_mqtt_message(hass, "last-reset-topic", "2020-01-02 08:11:00") async_fire_mqtt_message(hass, "last-reset-topic", "2020-01-02 08:11:00")
state = hass.states.get("sensor.test") state = hass.states.get("sensor.test")
@ -452,17 +504,9 @@ async def test_setting_sensor_last_reset_via_mqtt_message(
) )
@pytest.mark.parametrize("datestring", ["2020-21-02 08:11:00", "Hello there!"]) @pytest.mark.parametrize(
async def test_setting_sensor_bad_last_reset_via_mqtt_message( "hass_config",
hass: HomeAssistant, [
caplog: pytest.LogCaptureFixture,
datestring,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
) -> None:
"""Test the setting of the last_reset property via MQTT."""
assert await async_setup_component(
hass,
mqtt.DOMAIN,
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
sensor.DOMAIN: { sensor.DOMAIN: {
@ -473,10 +517,18 @@ async def test_setting_sensor_bad_last_reset_via_mqtt_message(
"last_reset_topic": "last-reset-topic", "last_reset_topic": "last-reset-topic",
} }
} }
}, }
],
) )
await hass.async_block_till_done() @pytest.mark.parametrize("datestring", ["2020-21-02 08:11:00", "Hello there!"])
await mqtt_mock_entry_with_yaml_config() async def test_setting_sensor_bad_last_reset_via_mqtt_message(
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
datestring,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
) -> None:
"""Test the setting of the last_reset property via MQTT."""
await mqtt_mock_entry_no_yaml_config()
async_fire_mqtt_message(hass, "last-reset-topic", datestring) async_fire_mqtt_message(hass, "last-reset-topic", datestring)
state = hass.states.get("sensor.test") state = hass.states.get("sensor.test")
@ -484,13 +536,9 @@ async def test_setting_sensor_bad_last_reset_via_mqtt_message(
assert "Invalid last_reset message" in caplog.text assert "Invalid last_reset message" in caplog.text
async def test_setting_sensor_empty_last_reset_via_mqtt_message( @pytest.mark.parametrize(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator "hass_config",
) -> None: [
"""Test the setting of the last_reset property via MQTT."""
assert await async_setup_component(
hass,
mqtt.DOMAIN,
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
sensor.DOMAIN: { sensor.DOMAIN: {
@ -501,23 +549,23 @@ async def test_setting_sensor_empty_last_reset_via_mqtt_message(
"last_reset_topic": "last-reset-topic", "last_reset_topic": "last-reset-topic",
} }
} }
}, }
],
) )
await hass.async_block_till_done() async def test_setting_sensor_empty_last_reset_via_mqtt_message(
await mqtt_mock_entry_with_yaml_config() hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test the setting of the last_reset property via MQTT."""
await mqtt_mock_entry_no_yaml_config()
async_fire_mqtt_message(hass, "last-reset-topic", "") async_fire_mqtt_message(hass, "last-reset-topic", "")
state = hass.states.get("sensor.test") state = hass.states.get("sensor.test")
assert state.attributes.get("last_reset") is None assert state.attributes.get("last_reset") is None
async def test_setting_sensor_last_reset_via_mqtt_json_message( @pytest.mark.parametrize(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator "hass_config",
) -> None: [
"""Test the setting of the value via MQTT with JSON payload."""
assert await async_setup_component(
hass,
mqtt.DOMAIN,
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
sensor.DOMAIN: { sensor.DOMAIN: {
@ -529,10 +577,14 @@ async def test_setting_sensor_last_reset_via_mqtt_json_message(
"last_reset_value_template": "{{ value_json.last_reset }}", "last_reset_value_template": "{{ value_json.last_reset }}",
} }
} }
}, }
],
) )
await hass.async_block_till_done() async def test_setting_sensor_last_reset_via_mqtt_json_message(
await mqtt_mock_entry_with_yaml_config() hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test the setting of the value via MQTT with JSON payload."""
await mqtt_mock_entry_no_yaml_config()
async_fire_mqtt_message( async_fire_mqtt_message(
hass, "last-reset-topic", '{ "last_reset": "2020-01-02 08:11:00" }' hass, "last-reset-topic", '{ "last_reset": "2020-01-02 08:11:00" }'
@ -541,21 +593,12 @@ async def test_setting_sensor_last_reset_via_mqtt_json_message(
assert state.attributes.get("last_reset") == "2020-01-02T08:11:00" assert state.attributes.get("last_reset") == "2020-01-02T08:11:00"
@pytest.mark.parametrize("extra", [{}, {"last_reset_topic": "test-topic"}]) @pytest.mark.parametrize(
async def test_setting_sensor_last_reset_via_mqtt_json_message_2( "hass_config",
hass: HomeAssistant, [
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
extra,
) -> None:
"""Test the setting of the value via MQTT with JSON payload."""
assert await async_setup_component(
hass,
mqtt.DOMAIN,
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
sensor.DOMAIN: { sensor.DOMAIN: {
**{
"name": "test", "name": "test",
"state_class": "total", "state_class": "total",
"state_topic": "test-topic", "state_topic": "test-topic",
@ -563,13 +606,31 @@ async def test_setting_sensor_last_reset_via_mqtt_json_message_2(
"value_template": "{{ value_json.value | float / 60000 }}", "value_template": "{{ value_json.value | float / 60000 }}",
"last_reset_value_template": "{{ utcnow().fromtimestamp(value_json.time / 1000, tz=utcnow().tzinfo) }}", "last_reset_value_template": "{{ utcnow().fromtimestamp(value_json.time / 1000, tz=utcnow().tzinfo) }}",
}, },
**extra,
}
} }
}, },
{
mqtt.DOMAIN: {
sensor.DOMAIN: {
"name": "test",
"state_class": "total",
"state_topic": "test-topic",
"unit_of_measurement": "kWh",
"value_template": "{{ value_json.value | float / 60000 }}",
"last_reset_value_template": "{{ utcnow().fromtimestamp(value_json.time / 1000, tz=utcnow().tzinfo) }}",
"last_reset_topic": "test-topic",
},
}
},
],
) )
async def test_setting_sensor_last_reset_via_mqtt_json_message_2(
hass: HomeAssistant,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test the setting of the value via MQTT with JSON payload."""
await hass.async_block_till_done() await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config() await mqtt_mock_entry_no_yaml_config()
async_fire_mqtt_message( async_fire_mqtt_message(
hass, hass,
@ -586,13 +647,9 @@ async def test_setting_sensor_last_reset_via_mqtt_json_message_2(
) )
async def test_force_update_disabled( @pytest.mark.parametrize(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator "hass_config",
) -> None: [
"""Test force update option."""
assert await async_setup_component(
hass,
mqtt.DOMAIN,
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
sensor.DOMAIN: { sensor.DOMAIN: {
@ -601,15 +658,19 @@ async def test_force_update_disabled(
"unit_of_measurement": "fav unit", "unit_of_measurement": "fav unit",
} }
} }
}, }
],
) )
await hass.async_block_till_done() async def test_force_update_disabled(
await mqtt_mock_entry_with_yaml_config() hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test force update option."""
await mqtt_mock_entry_no_yaml_config()
events = [] events: list[Event] = []
@callback @callback
def test_callback(event) -> None: def test_callback(event: Event) -> None:
events.append(event) events.append(event)
hass.bus.async_listen(EVENT_STATE_CHANGED, test_callback) hass.bus.async_listen(EVENT_STATE_CHANGED, test_callback)
@ -623,13 +684,9 @@ async def test_force_update_disabled(
assert len(events) == 1 assert len(events) == 1
async def test_force_update_enabled( @pytest.mark.parametrize(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator "hass_config",
) -> None: [
"""Test force update option."""
assert await async_setup_component(
hass,
mqtt.DOMAIN,
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
sensor.DOMAIN: { sensor.DOMAIN: {
@ -639,15 +696,19 @@ async def test_force_update_enabled(
"force_update": True, "force_update": True,
} }
} }
}, }
],
) )
await hass.async_block_till_done() async def test_force_update_enabled(
await mqtt_mock_entry_with_yaml_config() hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test force update option."""
await mqtt_mock_entry_no_yaml_config()
events = [] events: list[Event] = []
@callback @callback
def test_callback(event) -> None: def test_callback(event: Event) -> None:
events.append(event) events.append(event)
hass.bus.async_listen(EVENT_STATE_CHANGED, test_callback) hass.bus.async_listen(EVENT_STATE_CHANGED, test_callback)
@ -747,13 +808,9 @@ async def test_discovery_update_availability(
) )
async def test_invalid_device_class( @pytest.mark.parametrize(
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator "hass_config",
) -> None: [
"""Test device_class option with invalid value."""
assert await async_setup_component(
hass,
sensor.DOMAIN,
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
sensor.DOMAIN: { sensor.DOMAIN: {
@ -762,22 +819,25 @@ async def test_invalid_device_class(
"device_class": "foobarnotreal", "device_class": "foobarnotreal",
} }
} }
}, }
],
) )
await hass.async_block_till_done() async def test_invalid_device_class(
await mqtt_mock_entry_no_yaml_config() hass: HomeAssistant,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
state = hass.states.get("sensor.test") caplog: pytest.LogCaptureFixture,
assert state is None
async def test_valid_device_class(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
) -> None: ) -> None:
"""Test device_class option with valid values.""" """Test device_class option with invalid value."""
assert await async_setup_component( with pytest.raises(AssertionError):
hass, await mqtt_mock_entry_no_yaml_config()
mqtt.DOMAIN, assert (
"Invalid config for [mqtt]: expected SensorDeviceClass or one of" in caplog.text
)
@pytest.mark.parametrize(
"hass_config",
[
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
sensor.DOMAIN: [ sensor.DOMAIN: [
@ -794,10 +854,14 @@ async def test_valid_device_class(
}, },
] ]
} }
}, }
],
) )
await hass.async_block_till_done() async def test_valid_device_class(
await mqtt_mock_entry_with_yaml_config() hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test device_class option with valid values."""
await mqtt_mock_entry_no_yaml_config()
state = hass.states.get("sensor.test_1") state = hass.states.get("sensor.test_1")
assert state.attributes["device_class"] == "temperature" assert state.attributes["device_class"] == "temperature"
@ -807,13 +871,9 @@ async def test_valid_device_class(
assert "device_class" not in state.attributes assert "device_class" not in state.attributes
async def test_invalid_state_class( @pytest.mark.parametrize(
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator "hass_config",
) -> None: [
"""Test state_class option with invalid value."""
assert await async_setup_component(
hass,
sensor.DOMAIN,
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
sensor.DOMAIN: { sensor.DOMAIN: {
@ -822,22 +882,25 @@ async def test_invalid_state_class(
"state_class": "foobarnotreal", "state_class": "foobarnotreal",
} }
} }
}, }
],
) )
await hass.async_block_till_done() async def test_invalid_state_class(
await mqtt_mock_entry_no_yaml_config() hass: HomeAssistant,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
state = hass.states.get("sensor.test") caplog: pytest.LogCaptureFixture,
assert state is None
async def test_valid_state_class(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator
) -> None: ) -> None:
"""Test state_class option with valid values.""" """Test state_class option with invalid value."""
assert await async_setup_component( with pytest.raises(AssertionError):
hass, await mqtt_mock_entry_no_yaml_config()
mqtt.DOMAIN, assert (
"Invalid config for [mqtt]: expected SensorStateClass or one of" in caplog.text
)
@pytest.mark.parametrize(
"hass_config",
[
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
sensor.DOMAIN: [ sensor.DOMAIN: [
@ -854,10 +917,14 @@ async def test_valid_state_class(
}, },
] ]
} }
}, }
],
) )
await hass.async_block_till_done() async def test_valid_state_class(
await mqtt_mock_entry_with_yaml_config() hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test state_class option with valid values."""
await mqtt_mock_entry_no_yaml_config()
state = hass.states.get("sensor.test_1") state = hass.states.get("sensor.test_1")
assert state.attributes["state_class"] == "measurement" assert state.attributes["state_class"] == "measurement"
@ -1237,13 +1304,9 @@ async def test_entity_category(
) )
async def test_value_template_with_entity_id( @pytest.mark.parametrize(
hass: HomeAssistant, mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator "hass_config",
) -> None: [
"""Test the access to attributes in value_template via the entity_id."""
assert await async_setup_component(
hass,
mqtt.DOMAIN,
{ {
mqtt.DOMAIN: { mqtt.DOMAIN: {
sensor.DOMAIN: { sensor.DOMAIN: {
@ -1258,10 +1321,14 @@ async def test_value_template_with_entity_id(
{% endif %}', {% endif %}',
} }
} }
}, }
],
) )
await hass.async_block_till_done() async def test_value_template_with_entity_id(
await mqtt_mock_entry_with_yaml_config() hass: HomeAssistant, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator
) -> None:
"""Test the access to attributes in value_template via the entity_id."""
await mqtt_mock_entry_no_yaml_config()
async_fire_mqtt_message(hass, "test-topic", "100") async_fire_mqtt_message(hass, "test-topic", "100")
state = hass.states.get("sensor.test") state = hass.states.get("sensor.test")
@ -1279,38 +1346,43 @@ async def test_reloadable(
await help_test_reloadable(hass, mqtt_client_mock, domain, config) await help_test_reloadable(hass, mqtt_client_mock, domain, config)
@pytest.mark.parametrize(
"hass_config",
[
help_custom_config(
sensor.DOMAIN,
DEFAULT_CONFIG,
(
{
"name": "test1",
"expire_after": 30,
"state_topic": "test-topic1",
"device_class": "temperature",
"unit_of_measurement": UnitOfTemperature.FAHRENHEIT.value,
},
{
"name": "test2",
"expire_after": 5,
"state_topic": "test-topic2",
"device_class": "temperature",
"unit_of_measurement": UnitOfTemperature.CELSIUS.value,
},
),
)
],
)
async def test_cleanup_triggers_and_restoring_state( async def test_cleanup_triggers_and_restoring_state(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
tmp_path: Path, tmp_path: Path,
freezer: FrozenDateTimeFactory, freezer: FrozenDateTimeFactory,
hass_config: ConfigType,
) -> None: ) -> None:
"""Test cleanup old triggers at reloading and restoring the state.""" """Test cleanup old triggers at reloading and restoring the state."""
domain = sensor.DOMAIN
config1 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][domain])
config1["name"] = "test1"
config1["expire_after"] = 30
config1["state_topic"] = "test-topic1"
config1["device_class"] = "temperature"
config1["unit_of_measurement"] = UnitOfTemperature.FAHRENHEIT.value
config2 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][domain])
config2["name"] = "test2"
config2["expire_after"] = 5
config2["state_topic"] = "test-topic2"
config2["device_class"] = "temperature"
config2["unit_of_measurement"] = UnitOfTemperature.CELSIUS.value
freezer.move_to("2022-02-02 12:01:00+01:00") freezer.move_to("2022-02-02 12:01:00+01:00")
assert await async_setup_component( await mqtt_mock_entry_no_yaml_config()
hass,
mqtt.DOMAIN,
{mqtt.DOMAIN: {domain: [config1, config2]}},
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
async_fire_mqtt_message(hass, "test-topic1", "100") async_fire_mqtt_message(hass, "test-topic1", "100")
state = hass.states.get("sensor.test1") state = hass.states.get("sensor.test1")
assert state.state == "38" # 100 °F -> 38 °C assert state.state == "38" # 100 °F -> 38 °C
@ -1321,9 +1393,7 @@ async def test_cleanup_triggers_and_restoring_state(
freezer.move_to("2022-02-02 12:01:10+01:00") freezer.move_to("2022-02-02 12:01:10+01:00")
await help_test_reload_with_config( await help_test_reload_with_config(hass, caplog, tmp_path, hass_config)
hass, caplog, tmp_path, {mqtt.DOMAIN: {domain: [config1, config2]}}
)
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get("sensor.test1") state = hass.states.get("sensor.test1")
@ -1341,19 +1411,30 @@ async def test_cleanup_triggers_and_restoring_state(
assert state.state == "201" assert state.state == "201"
@pytest.mark.parametrize(
"hass_config",
[
help_custom_config(
sensor.DOMAIN,
DEFAULT_CONFIG,
(
{
"name": "test3",
"expire_after": 10,
"state_topic": "test-topic3",
},
),
)
],
)
async def test_skip_restoring_state_with_over_due_expire_trigger( async def test_skip_restoring_state_with_over_due_expire_trigger(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_with_yaml_config: MqttMockHAClientGenerator, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
freezer: FrozenDateTimeFactory, freezer: FrozenDateTimeFactory,
) -> None: ) -> None:
"""Test restoring a state with over due expire timer.""" """Test restoring a state with over due expire timer."""
freezer.move_to("2022-02-02 12:02:00+01:00") freezer.move_to("2022-02-02 12:02:00+01:00")
domain = sensor.DOMAIN
config3 = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN][domain])
config3["name"] = "test3"
config3["expire_after"] = 10
config3["state_topic"] = "test-topic3"
fake_state = State( fake_state = State(
"sensor.test3", "sensor.test3",
"300", "300",
@ -1363,11 +1444,7 @@ async def test_skip_restoring_state_with_over_due_expire_trigger(
fake_extra_data = MagicMock() fake_extra_data = MagicMock()
mock_restore_cache_with_extra_data(hass, ((fake_state, fake_extra_data),)) mock_restore_cache_with_extra_data(hass, ((fake_state, fake_extra_data),))
assert await async_setup_component( await mqtt_mock_entry_no_yaml_config()
hass, mqtt.DOMAIN, {mqtt.DOMAIN: {domain: config3}}
)
await hass.async_block_till_done()
await mqtt_mock_entry_with_yaml_config()
state = hass.states.get("sensor.test3") state = hass.states.get("sensor.test3")
assert state.state == STATE_UNAVAILABLE assert state.state == STATE_UNAVAILABLE