|
| 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