Poll state when Tasmota device becomes available (#41401)

This commit is contained in:
Erik Montnemery authored and GitHub committed 2020-10-08 11:17:23 +02:00
1 parent b51a160cce
commit 35287828bc
6 files changed
+163 -4

No files matched your search

+72 -2
View File
@@ -59,22 +59,27 @@ DEFAULT_CONFIG = {
async def help_test_availability_when_connection_lost(
hass, mqtt_client_mock, mqtt_mock, domain, config
):
"""Test availability after MQTT disconnection."""
"""Test availability after MQTT disconnection.
This is a test helper for the TasmotaAvailability mixin.
"""
async_fire_mqtt_message(
hass,
f"{DEFAULT_PREFIX}/{config[CONF_MAC]}/config",
json.dumps(config),
)
await hass.async_block_till_done()
# Device online
async_fire_mqtt_message(
hass,
get_topic_tele_will(config),
config_get_state_online(config),
)
state = hass.states.get(f"{domain}.test")
assert state.state != STATE_UNAVAILABLE
# Disconnected from MQTT server -> state changed to unavailable
mqtt_mock.connected = False
await hass.async_add_executor_job(mqtt_client_mock.on_disconnect, None, None, 0)
await hass.async_block_till_done()
@@ -83,12 +88,22 @@ async def help_test_availability_when_connection_lost(
state = hass.states.get(f"{domain}.test")
assert state.state == STATE_UNAVAILABLE
# Reconnected to MQTT server -> state still unavailable
mqtt_mock.connected = True
await hass.async_add_executor_job(mqtt_client_mock.on_connect, None, None, None, 0)
await hass.async_block_till_done()
await hass.async_block_till_done()
await hass.async_block_till_done()
state = hass.states.get(f"{domain}.test")
assert state.state == STATE_UNAVAILABLE
# Receive LWT again
async_fire_mqtt_message(
hass,
get_topic_tele_will(config),
config_get_state_online(config),
)
state = hass.states.get(f"{domain}.test")
assert state.state != STATE_UNAVAILABLE
@@ -194,6 +209,61 @@ async def help_test_availability_discovery_update(
assert state.state != STATE_UNAVAILABLE
async def help_test_availability_poll_state(
hass, mqtt_client_mock, mqtt_mock, domain, config, poll_topic, poll_payload
):
"""Test polling of state when device is available.
This is a test helper for the TasmotaAvailability mixin.
"""
async_fire_mqtt_message(
hass,
f"{DEFAULT_PREFIX}/{config[CONF_MAC]}/config",
json.dumps(config),
)
await hass.async_block_till_done()
mqtt_mock.async_publish.reset_mock()
# Device online, verify poll for state
async_fire_mqtt_message(
hass,
get_topic_tele_will(config),
config_get_state_online(config),
)
await hass.async_block_till_done()
await hass.async_block_till_done()
await hass.async_block_till_done()
mqtt_mock.async_publish.assert_called_once_with(poll_topic, poll_payload, 0, False)
mqtt_mock.async_publish.reset_mock()
# Disconnected from MQTT server
mqtt_mock.connected = False
await hass.async_add_executor_job(mqtt_client_mock.on_disconnect, None, None, 0)
await hass.async_block_till_done()
await hass.async_block_till_done()
await hass.async_block_till_done()
assert not mqtt_mock.async_publish.called
# Reconnected to MQTT server
mqtt_mock.connected = True
await hass.async_add_executor_job(mqtt_client_mock.on_connect, None, None, None, 0)
await hass.async_block_till_done()
await hass.async_block_till_done()
await hass.async_block_till_done()
assert not mqtt_mock.async_publish.called
# Device online, verify poll for state
async_fire_mqtt_message(
hass,
get_topic_tele_will(config),
config_get_state_online(config),
)
await hass.async_block_till_done()
await hass.async_block_till_done()
await hass.async_block_till_done()
mqtt_mock.async_publish.assert_called_once_with(poll_topic, poll_payload, 0, False)
async def help_test_discovery_removal(
hass, mqtt_mock, caplog, domain, config1, config2
):