Add unit tests for SNMP integer Switches (#123094)

* Add unit tests for SNMP Switches (integer only)

* Add unit test for SNMP switches (integer unknown)

* log a warning when SNMP response is not a recognised payload

* Use a single configuration for all test_integer_switch tests

* Tweak unknown SNMP response warning

* Apply suggestions from code review

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* import STATE_ consts

* rename tests/components/snmp/test_integer_switch.py to test_switch.py

* check that a warning is logged if the SNMP response payload is unknown

---------

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
Guy Lowe 2024-08-06 21:56:39 +12:00 committed by GitHub
parent 86d8c3b31a
commit a2dd017229
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 72 additions and 0 deletions

View File

@ -277,6 +277,11 @@ class SnmpSwitch(SwitchEntity):
):
self._state = False
else:
_LOGGER.warning(
"Invalid payload '%s' received for entity %s, state is unknown",
resrow[-1],
self.entity_id,
)
self._state = None
@property

View File

@ -0,0 +1,67 @@
"""SNMP switch tests."""
from unittest.mock import patch
from pysnmp.hlapi import Integer32
import pytest
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNKNOWN
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
config = {
SWITCH_DOMAIN: {
"platform": "snmp",
"host": "192.168.1.32",
# ippower-mib::ippoweroutlet1.0
"baseoid": "1.3.6.1.4.1.38107.1.3.1.0",
"payload_on": 1,
"payload_off": 0,
},
}
async def test_snmp_integer_switch_off(hass: HomeAssistant) -> None:
"""Test snmp switch returning int 0 for off."""
mock_data = Integer32(0)
with patch(
"homeassistant.components.snmp.switch.getCmd",
return_value=(None, None, None, [[mock_data]]),
):
assert await async_setup_component(hass, SWITCH_DOMAIN, config)
await hass.async_block_till_done()
state = hass.states.get("switch.snmp")
assert state.state == STATE_OFF
async def test_snmp_integer_switch_on(hass: HomeAssistant) -> None:
"""Test snmp switch returning int 1 for on."""
mock_data = Integer32(1)
with patch(
"homeassistant.components.snmp.switch.getCmd",
return_value=(None, None, None, [[mock_data]]),
):
assert await async_setup_component(hass, SWITCH_DOMAIN, config)
await hass.async_block_till_done()
state = hass.states.get("switch.snmp")
assert state.state == STATE_ON
async def test_snmp_integer_switch_unknown(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test snmp switch returning int 3 (not a configured payload) for unknown."""
mock_data = Integer32(3)
with patch(
"homeassistant.components.snmp.switch.getCmd",
return_value=(None, None, None, [[mock_data]]),
):
assert await async_setup_component(hass, SWITCH_DOMAIN, config)
await hass.async_block_till_done()
state = hass.states.get("switch.snmp")
assert state.state == STATE_UNKNOWN
assert "Invalid payload '3' received for entity" in caplog.text