core/tests/components/knx/test_expose.py

92 lines
3.0 KiB
Python
Raw Normal View History

2021-03-26 13:51:36 +00:00
"""Test knx expose."""
from homeassistant.components.knx import CONF_KNX_EXPOSE, KNX_ADDRESS
2021-03-29 10:17:54 +00:00
from homeassistant.const import CONF_ATTRIBUTE, CONF_ENTITY_ID, CONF_TYPE
from . import setup_knx_integration
2021-03-29 10:17:54 +00:00
async def test_binary_expose(hass, knx_ip_interface_mock):
2021-03-26 13:51:36 +00:00
"""Test that a binary expose sends only telegrams on state change."""
2021-03-29 10:17:54 +00:00
entity_id = "fake.entity"
await setup_knx_integration(
hass,
knx_ip_interface_mock,
2021-03-29 10:17:54 +00:00
{
CONF_KNX_EXPOSE: {
CONF_TYPE: "binary",
KNX_ADDRESS: "1/1/8",
CONF_ENTITY_ID: entity_id,
}
},
)
assert not hass.states.async_all()
2021-03-26 13:51:36 +00:00
# Change state to on
knx_ip_interface_mock.reset_mock()
2021-03-29 10:17:54 +00:00
hass.states.async_set(entity_id, "on", {})
2021-03-26 13:51:36 +00:00
await hass.async_block_till_done()
assert (
knx_ip_interface_mock.send_telegram.call_count == 1
), "Expected telegram for state change"
2021-03-26 13:51:36 +00:00
# Change attribute; keep state
knx_ip_interface_mock.reset_mock()
2021-03-29 10:17:54 +00:00
hass.states.async_set(entity_id, "on", {"brightness": 180})
2021-03-26 13:51:36 +00:00
await hass.async_block_till_done()
assert (
knx_ip_interface_mock.send_telegram.call_count == 0
2021-03-26 13:51:36 +00:00
), "Expected no telegram; state not changed"
# Change attribute and state
knx_ip_interface_mock.reset_mock()
2021-03-29 10:17:54 +00:00
hass.states.async_set(entity_id, "off", {"brightness": 0})
2021-03-26 13:51:36 +00:00
await hass.async_block_till_done()
assert (
knx_ip_interface_mock.send_telegram.call_count == 1
), "Expected telegram for state change"
2021-03-26 13:51:36 +00:00
async def test_expose_attribute(hass, knx_ip_interface_mock):
2021-03-26 13:51:36 +00:00
"""Test that an expose sends only telegrams on attribute change."""
2021-03-29 10:17:54 +00:00
entity_id = "fake.entity"
attribute = "fake_attribute"
await setup_knx_integration(
hass,
knx_ip_interface_mock,
2021-03-29 10:17:54 +00:00
{
CONF_KNX_EXPOSE: {
CONF_TYPE: "percentU8",
KNX_ADDRESS: "1/1/8",
CONF_ENTITY_ID: entity_id,
CONF_ATTRIBUTE: attribute,
}
},
)
assert not hass.states.async_all()
2021-03-26 13:51:36 +00:00
# Change state to on; no attribute
knx_ip_interface_mock.reset_mock()
2021-03-29 10:17:54 +00:00
hass.states.async_set(entity_id, "on", {})
2021-03-26 13:51:36 +00:00
await hass.async_block_till_done()
assert knx_ip_interface_mock.send_telegram.call_count == 0
2021-03-26 13:51:36 +00:00
# Change attribute; keep state
knx_ip_interface_mock.reset_mock()
2021-03-29 10:17:54 +00:00
hass.states.async_set(entity_id, "on", {attribute: 1})
2021-03-26 13:51:36 +00:00
await hass.async_block_till_done()
assert knx_ip_interface_mock.send_telegram.call_count == 1
2021-03-26 13:51:36 +00:00
# Change state keep attribute
knx_ip_interface_mock.reset_mock()
2021-03-29 10:17:54 +00:00
hass.states.async_set(entity_id, "off", {attribute: 1})
2021-03-26 13:51:36 +00:00
await hass.async_block_till_done()
assert knx_ip_interface_mock.send_telegram.call_count == 0
2021-03-26 13:51:36 +00:00
# Change state and attribute
knx_ip_interface_mock.reset_mock()
2021-03-29 10:17:54 +00:00
hass.states.async_set(entity_id, "on", {attribute: 0})
2021-03-26 13:51:36 +00:00
await hass.async_block_till_done()
assert knx_ip_interface_mock.send_telegram.call_count == 1