diff --git a/tests/components/rflink/test_init.py b/tests/components/rflink/test_init.py index f93c9703d30..73def144aba 100644 --- a/tests/components/rflink/test_init.py +++ b/tests/components/rflink/test_init.py @@ -7,15 +7,24 @@ from voluptuous.error import MultipleInvalid from homeassistant.bootstrap import async_setup_component from homeassistant.components.rflink import ( + CONF_KEEPALIVE_IDLE, CONF_RECONNECT_INTERVAL, DATA_ENTITY_LOOKUP, + DEFAULT_TCP_KEEPALIVE_IDLE_TIMER, + DOMAIN as RFLINK_DOMAIN, EVENT_KEY_COMMAND, EVENT_KEY_SENSOR, SERVICE_SEND_COMMAND, TMP_ENTITY, RflinkCommand, ) -from homeassistant.const import ATTR_ENTITY_ID, SERVICE_STOP_COVER, SERVICE_TURN_OFF +from homeassistant.const import ( + ATTR_ENTITY_ID, + CONF_HOST, + CONF_PORT, + SERVICE_STOP_COVER, + SERVICE_TURN_OFF, +) async def mock_rflink( @@ -380,3 +389,90 @@ async def test_not_connected(hass, monkeypatch): RflinkCommand.set_rflink_protocol(None) with pytest.raises(HomeAssistantError): await test_device._async_handle_command("turn_on") + + +async def test_keepalive(hass, monkeypatch, caplog): + """Validate negative keepalive values.""" + keepalive_value = -3 + domain = RFLINK_DOMAIN + config = { + RFLINK_DOMAIN: { + CONF_HOST: "10.10.0.1", + CONF_PORT: 1234, + CONF_KEEPALIVE_IDLE: keepalive_value, + } + } + + # setup mocking rflink module + _, mock_create, _, _ = await mock_rflink(hass, config, domain, monkeypatch) + + assert mock_create.call_args_list[0][1]["host"] == "10.10.0.1" + assert mock_create.call_args_list[0][1]["port"] == 1234 + assert ( + mock_create.call_args_list[0][1]["keepalive"] is None + ) # negative keepalive is not allowed + assert ( + f"A bogus TCP Keepalive IDLE timer was provided ({keepalive_value} secs)" + in caplog.text + ) + + +async def test2_keepalive(hass, monkeypatch, caplog): + """Validate very short keepalive values.""" + keepalive_value = 30 + domain = RFLINK_DOMAIN + config = { + RFLINK_DOMAIN: { + CONF_HOST: "10.10.0.1", + CONF_PORT: 1234, + CONF_KEEPALIVE_IDLE: keepalive_value, + } + } + + # setup mocking rflink module + _, mock_create, _, _ = await mock_rflink(hass, config, domain, monkeypatch) + + assert mock_create.call_args_list[0][1]["host"] == "10.10.0.1" + assert mock_create.call_args_list[0][1]["port"] == 1234 + assert ( + mock_create.call_args_list[0][1]["keepalive"] == keepalive_value + ) # very short keepalive is allowed but warned + assert ( + f"A very short TCP Keepalive IDLE timer was provided ({keepalive_value} secs)" + in caplog.text + ) + + +async def test3_keepalive(hass, monkeypatch, caplog): + """Validate keepalive=0 value.""" + domain = RFLINK_DOMAIN + config = { + RFLINK_DOMAIN: {CONF_HOST: "10.10.0.1", CONF_PORT: 1234, CONF_KEEPALIVE_IDLE: 0} + } + + # setup mocking rflink module + _, mock_create, _, _ = await mock_rflink(hass, config, domain, monkeypatch) + + assert mock_create.call_args_list[0][1]["host"] == "10.10.0.1" + assert mock_create.call_args_list[0][1]["port"] == 1234 + assert ( + mock_create.call_args_list[0][1]["keepalive"] is None + ) # keepalive=0 will disable it + assert "TCP Keepalive IDLE timer was provided" not in caplog.text + + +async def test_default_keepalive(hass, monkeypatch, caplog): + """Validate keepalive=0 value.""" + domain = RFLINK_DOMAIN + config = {RFLINK_DOMAIN: {CONF_HOST: "10.10.0.1", CONF_PORT: 1234}} + + # setup mocking rflink module + _, mock_create, _, _ = await mock_rflink(hass, config, domain, monkeypatch) + + assert mock_create.call_args_list[0][1]["host"] == "10.10.0.1" + assert mock_create.call_args_list[0][1]["port"] == 1234 + assert ( + mock_create.call_args_list[0][1]["keepalive"] + == DEFAULT_TCP_KEEPALIVE_IDLE_TIMER + ) # no keepalive config will default it + assert "TCP Keepalive IDLE timer was provided" not in caplog.text