Skip to content

Commit a2dd017

Browse files
gunkaaajoostlek
andauthored
Add unit tests for SNMP integer Switches (home-assistant#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 <[email protected]> * 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 <[email protected]>
1 parent 86d8c3b commit a2dd017

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

homeassistant/components/snmp/switch.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,11 @@ async def async_update(self) -> None:
277277
):
278278
self._state = False
279279
else:
280+
_LOGGER.warning(
281+
"Invalid payload '%s' received for entity %s, state is unknown",
282+
resrow[-1],
283+
self.entity_id,
284+
)
280285
self._state = None
281286

282287
@property
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""SNMP switch tests."""
2+
3+
from unittest.mock import patch
4+
5+
from pysnmp.hlapi import Integer32
6+
import pytest
7+
8+
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
9+
from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNKNOWN
10+
from homeassistant.core import HomeAssistant
11+
from homeassistant.setup import async_setup_component
12+
13+
config = {
14+
SWITCH_DOMAIN: {
15+
"platform": "snmp",
16+
"host": "192.168.1.32",
17+
# ippower-mib::ippoweroutlet1.0
18+
"baseoid": "1.3.6.1.4.1.38107.1.3.1.0",
19+
"payload_on": 1,
20+
"payload_off": 0,
21+
},
22+
}
23+
24+
25+
async def test_snmp_integer_switch_off(hass: HomeAssistant) -> None:
26+
"""Test snmp switch returning int 0 for off."""
27+
28+
mock_data = Integer32(0)
29+
with patch(
30+
"homeassistant.components.snmp.switch.getCmd",
31+
return_value=(None, None, None, [[mock_data]]),
32+
):
33+
assert await async_setup_component(hass, SWITCH_DOMAIN, config)
34+
await hass.async_block_till_done()
35+
state = hass.states.get("switch.snmp")
36+
assert state.state == STATE_OFF
37+
38+
39+
async def test_snmp_integer_switch_on(hass: HomeAssistant) -> None:
40+
"""Test snmp switch returning int 1 for on."""
41+
42+
mock_data = Integer32(1)
43+
with patch(
44+
"homeassistant.components.snmp.switch.getCmd",
45+
return_value=(None, None, None, [[mock_data]]),
46+
):
47+
assert await async_setup_component(hass, SWITCH_DOMAIN, config)
48+
await hass.async_block_till_done()
49+
state = hass.states.get("switch.snmp")
50+
assert state.state == STATE_ON
51+
52+
53+
async def test_snmp_integer_switch_unknown(
54+
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
55+
) -> None:
56+
"""Test snmp switch returning int 3 (not a configured payload) for unknown."""
57+
58+
mock_data = Integer32(3)
59+
with patch(
60+
"homeassistant.components.snmp.switch.getCmd",
61+
return_value=(None, None, None, [[mock_data]]),
62+
):
63+
assert await async_setup_component(hass, SWITCH_DOMAIN, config)
64+
await hass.async_block_till_done()
65+
state = hass.states.get("switch.snmp")
66+
assert state.state == STATE_UNKNOWN
67+
assert "Invalid payload '3' received for entity" in caplog.text

0 commit comments

Comments
 (0)