1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Add support Mqtt switch for unkown state (#65294)

* Mqtt switch allow unkown state

* correct type

* Update discovery tests

* Optimistic mode if not state_topic is configured.

* Default state UNKNOWN in optimistic mode

* fix discovery test
This commit is contained in:
Jan Bouwhuis
2022-02-03 16:47:24 +01:00
committed by GitHub
parent 63459feede
commit 3d434dffc7
3 changed files with 50 additions and 13 deletions

View File

@@ -11,6 +11,7 @@ from homeassistant.const import (
ATTR_DEVICE_CLASS,
STATE_OFF,
STATE_ON,
STATE_UNKNOWN,
)
import homeassistant.core as ha
from homeassistant.setup import async_setup_component
@@ -71,7 +72,7 @@ async def test_controlling_state_via_topic(hass, mqtt_mock):
await hass.async_block_till_done()
state = hass.states.get("switch.test")
assert state.state == STATE_OFF
assert state.state == STATE_UNKNOWN
assert state.attributes.get(ATTR_DEVICE_CLASS) == "switch"
assert not state.attributes.get(ATTR_ASSUMED_STATE)
@@ -85,6 +86,11 @@ async def test_controlling_state_via_topic(hass, mqtt_mock):
state = hass.states.get("switch.test")
assert state.state == STATE_OFF
async_fire_mqtt_message(hass, "state-topic", "None")
state = hass.states.get("switch.test")
assert state.state == STATE_UNKNOWN
async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock):
"""Test the sending MQTT commands in optimistic mode."""
@@ -132,6 +138,26 @@ async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock):
assert state.state == STATE_OFF
async def test_sending_inital_state_and_optimistic(hass, mqtt_mock):
"""Test the initial state in optimistic mode."""
assert await async_setup_component(
hass,
switch.DOMAIN,
{
switch.DOMAIN: {
"platform": "mqtt",
"name": "test",
"command_topic": "command-topic",
}
},
)
await hass.async_block_till_done()
state = hass.states.get("switch.test")
assert state.state == STATE_UNKNOWN
assert state.attributes.get(ATTR_ASSUMED_STATE)
async def test_controlling_state_via_topic_and_json_message(hass, mqtt_mock):
"""Test the controlling state via topic and JSON message."""
assert await async_setup_component(
@@ -152,7 +178,7 @@ async def test_controlling_state_via_topic_and_json_message(hass, mqtt_mock):
await hass.async_block_till_done()
state = hass.states.get("switch.test")
assert state.state == STATE_OFF
assert state.state == STATE_UNKNOWN
async_fire_mqtt_message(hass, "state-topic", '{"val":"beer on"}')
@@ -164,6 +190,11 @@ async def test_controlling_state_via_topic_and_json_message(hass, mqtt_mock):
state = hass.states.get("switch.test")
assert state.state == STATE_OFF
async_fire_mqtt_message(hass, "state-topic", '{"val": null}')
state = hass.states.get("switch.test")
assert state.state == STATE_UNKNOWN
async def test_availability_when_connection_lost(hass, mqtt_mock):
"""Test availability after MQTT disconnection."""
@@ -236,7 +267,7 @@ async def test_custom_state_payload(hass, mqtt_mock):
await hass.async_block_till_done()
state = hass.states.get("switch.test")
assert state.state == STATE_OFF
assert state.state == STATE_UNKNOWN
assert not state.attributes.get(ATTR_ASSUMED_STATE)
async_fire_mqtt_message(hass, "state-topic", "HIGH")