mirror of
https://github.com/home-assistant/core.git
synced 2025-04-24 09:17:53 +00:00
parent
171b57bf32
commit
88b93546f3
@ -373,6 +373,39 @@ async def test_setting_sensor_value_via_mqtt_message_and_template2(
|
||||
assert "template output: 'ILLEGAL'" in caplog.text
|
||||
|
||||
|
||||
async def test_setting_sensor_value_via_mqtt_message_and_template_and_raw_state_encoding(
|
||||
hass, mqtt_mock, caplog
|
||||
):
|
||||
"""Test processing a raw value via MQTT."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
binary_sensor.DOMAIN,
|
||||
{
|
||||
binary_sensor.DOMAIN: {
|
||||
"platform": "mqtt",
|
||||
"name": "test",
|
||||
"encoding": "",
|
||||
"state_topic": "test-topic",
|
||||
"payload_on": "ON",
|
||||
"payload_off": "OFF",
|
||||
"value_template": "{%if value|unpack('b')-%}ON{%else%}OFF{%-endif-%}",
|
||||
}
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("binary_sensor.test")
|
||||
assert state.state == STATE_OFF
|
||||
|
||||
async_fire_mqtt_message(hass, "test-topic", b"\x01")
|
||||
state = hass.states.get("binary_sensor.test")
|
||||
assert state.state == STATE_ON
|
||||
|
||||
async_fire_mqtt_message(hass, "test-topic", b"\x00")
|
||||
state = hass.states.get("binary_sensor.test")
|
||||
assert state.state == STATE_OFF
|
||||
|
||||
|
||||
async def test_setting_sensor_value_via_mqtt_message_empty_template(
|
||||
hass, mqtt_mock, caplog
|
||||
):
|
||||
|
@ -733,6 +733,100 @@ async def test_discovery_expansion_3(hass, mqtt_mock, caplog):
|
||||
)
|
||||
|
||||
|
||||
async def test_discovery_expansion_without_encoding_and_value_template_1(
|
||||
hass, mqtt_mock, caplog
|
||||
):
|
||||
"""Test expansion of raw availability payload with a template as list."""
|
||||
data = (
|
||||
'{ "~": "some/base/topic",'
|
||||
' "name": "DiscoveryExpansionTest1",'
|
||||
' "stat_t": "test_topic/~",'
|
||||
' "cmd_t": "~/test_topic",'
|
||||
' "encoding":"",'
|
||||
' "availability": [{'
|
||||
' "topic":"~/avail_item1",'
|
||||
' "payload_available": "1",'
|
||||
' "payload_not_available": "0",'
|
||||
' "value_template":"{{value|unpack(\'b\')}}"'
|
||||
" }],"
|
||||
' "dev":{'
|
||||
' "ids":["5706DF"],'
|
||||
' "name":"DiscoveryExpansionTest1 Device",'
|
||||
' "mdl":"Generic",'
|
||||
' "sw":"1.2.3.4",'
|
||||
' "mf":"None",'
|
||||
' "sa":"default_area"'
|
||||
" }"
|
||||
"}"
|
||||
)
|
||||
|
||||
async_fire_mqtt_message(hass, "homeassistant/switch/bla/config", data)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("switch.DiscoveryExpansionTest1")
|
||||
assert state.state == STATE_UNAVAILABLE
|
||||
|
||||
async_fire_mqtt_message(hass, "some/base/topic/avail_item1", b"\x01")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("switch.DiscoveryExpansionTest1")
|
||||
assert state is not None
|
||||
assert state.name == "DiscoveryExpansionTest1"
|
||||
assert ("switch", "bla") in hass.data[ALREADY_DISCOVERED]
|
||||
assert state.state == STATE_OFF
|
||||
|
||||
async_fire_mqtt_message(hass, "some/base/topic/avail_item1", b"\x00")
|
||||
|
||||
state = hass.states.get("switch.DiscoveryExpansionTest1")
|
||||
assert state.state == STATE_UNAVAILABLE
|
||||
|
||||
|
||||
async def test_discovery_expansion_without_encoding_and_value_template_2(
|
||||
hass, mqtt_mock, caplog
|
||||
):
|
||||
"""Test expansion of raw availability payload with a template directly."""
|
||||
data = (
|
||||
'{ "~": "some/base/topic",'
|
||||
' "name": "DiscoveryExpansionTest1",'
|
||||
' "stat_t": "test_topic/~",'
|
||||
' "cmd_t": "~/test_topic",'
|
||||
' "availability_topic":"~/avail_item1",'
|
||||
' "payload_available": "1",'
|
||||
' "payload_not_available": "0",'
|
||||
' "encoding":"",'
|
||||
' "availability_template":"{{ value | unpack(\'b\') }}",'
|
||||
' "dev":{'
|
||||
' "ids":["5706DF"],'
|
||||
' "name":"DiscoveryExpansionTest1 Device",'
|
||||
' "mdl":"Generic",'
|
||||
' "sw":"1.2.3.4",'
|
||||
' "mf":"None",'
|
||||
' "sa":"default_area"'
|
||||
" }"
|
||||
"}"
|
||||
)
|
||||
|
||||
async_fire_mqtt_message(hass, "homeassistant/switch/bla/config", data)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("switch.DiscoveryExpansionTest1")
|
||||
assert state.state == STATE_UNAVAILABLE
|
||||
|
||||
async_fire_mqtt_message(hass, "some/base/topic/avail_item1", b"\x01")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("switch.DiscoveryExpansionTest1")
|
||||
assert state is not None
|
||||
assert state.name == "DiscoveryExpansionTest1"
|
||||
assert ("switch", "bla") in hass.data[ALREADY_DISCOVERED]
|
||||
assert state.state == STATE_OFF
|
||||
|
||||
async_fire_mqtt_message(hass, "some/base/topic/avail_item1", b"\x00")
|
||||
|
||||
state = hass.states.get("switch.DiscoveryExpansionTest1")
|
||||
assert state.state == STATE_UNAVAILABLE
|
||||
|
||||
|
||||
ABBREVIATIONS_WHITE_LIST = [
|
||||
# MQTT client/server/trigger settings
|
||||
"CONF_BIRTH_MESSAGE",
|
||||
|
Loading…
x
Reference in New Issue
Block a user