Best Python code snippet using Airtest
test_siren.py
Source:test_siren.py
1"""Tests for the devolo Home Control binary sensors."""2from unittest.mock import patch3import pytest4from homeassistant.components.siren import DOMAIN5from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE6from homeassistant.core import HomeAssistant7from . import configure_integration8from .mocks import HomeControlMock, HomeControlMockSiren9@pytest.mark.usefixtures("mock_zeroconf")10async def test_siren(hass: HomeAssistant):11 """Test setup and state change of a siren device."""12 entry = configure_integration(hass)13 test_gateway = HomeControlMockSiren()14 test_gateway.devices["Test"].status = 015 with patch(16 "homeassistant.components.devolo_home_control.HomeControl",17 side_effect=[test_gateway, HomeControlMock()],18 ):19 await hass.config_entries.async_setup(entry.entry_id)20 await hass.async_block_till_done()21 state = hass.states.get(f"{DOMAIN}.test")22 assert state is not None23 assert state.state == STATE_OFF24 # Emulate websocket message: sensor turned on25 test_gateway.publisher.dispatch("Test", ("devolo.SirenMultiLevelSwitch:Test", 1))26 await hass.async_block_till_done()27 assert hass.states.get(f"{DOMAIN}.test").state == STATE_ON28 # Emulate websocket message: device went offline29 test_gateway.devices["Test"].status = 130 test_gateway.publisher.dispatch("Test", ("Status", False, "status"))31 await hass.async_block_till_done()32 assert hass.states.get(f"{DOMAIN}.test").state == STATE_UNAVAILABLE33@pytest.mark.usefixtures("mock_zeroconf")34async def test_siren_switching(hass: HomeAssistant):35 """Test setup and state change via switching of a siren device."""36 entry = configure_integration(hass)37 test_gateway = HomeControlMockSiren()38 test_gateway.devices["Test"].status = 039 with patch(40 "homeassistant.components.devolo_home_control.HomeControl",41 side_effect=[test_gateway, HomeControlMock()],42 ):43 await hass.config_entries.async_setup(entry.entry_id)44 await hass.async_block_till_done()45 state = hass.states.get(f"{DOMAIN}.test")46 assert state is not None47 assert state.state == STATE_OFF48 with patch(49 "devolo_home_control_api.properties.multi_level_switch_property.MultiLevelSwitchProperty.set"50 ) as set:51 await hass.services.async_call(52 "siren",53 "turn_on",54 {"entity_id": f"{DOMAIN}.test"},55 blocking=True,56 )57 # The real device state is changed by a websocket message58 test_gateway.publisher.dispatch(59 "Test", ("devolo.SirenMultiLevelSwitch:Test", 1)60 )61 await hass.async_block_till_done()62 set.assert_called_once_with(1)63 with patch(64 "devolo_home_control_api.properties.multi_level_switch_property.MultiLevelSwitchProperty.set"65 ) as set:66 await hass.services.async_call(67 "siren",68 "turn_off",69 {"entity_id": f"{DOMAIN}.test"},70 blocking=True,71 )72 # The real device state is changed by a websocket message73 test_gateway.publisher.dispatch(74 "Test", ("devolo.SirenMultiLevelSwitch:Test", 0)75 )76 await hass.async_block_till_done()77 assert hass.states.get(f"{DOMAIN}.test").state == STATE_OFF78 set.assert_called_once_with(0)79@pytest.mark.usefixtures("mock_zeroconf")80async def test_siren_change_default_tone(hass: HomeAssistant):81 """Test changing the default tone on message."""82 entry = configure_integration(hass)83 test_gateway = HomeControlMockSiren()84 test_gateway.devices["Test"].status = 085 with patch(86 "homeassistant.components.devolo_home_control.HomeControl",87 side_effect=[test_gateway, HomeControlMock()],88 ):89 await hass.config_entries.async_setup(entry.entry_id)90 await hass.async_block_till_done()91 state = hass.states.get(f"{DOMAIN}.test")92 assert state is not None93 with patch(94 "devolo_home_control_api.properties.multi_level_switch_property.MultiLevelSwitchProperty.set"95 ) as set:96 test_gateway.publisher.dispatch("Test", ("mss:Test", 2))97 await hass.services.async_call(98 "siren",99 "turn_on",100 {"entity_id": f"{DOMAIN}.test"},101 blocking=True,102 )103 set.assert_called_once_with(2)104@pytest.mark.usefixtures("mock_zeroconf")105async def test_remove_from_hass(hass: HomeAssistant):106 """Test removing entity."""107 entry = configure_integration(hass)108 test_gateway = HomeControlMockSiren()109 with patch(110 "homeassistant.components.devolo_home_control.HomeControl",111 side_effect=[test_gateway, HomeControlMock()],112 ):113 await hass.config_entries.async_setup(entry.entry_id)114 await hass.async_block_till_done()115 state = hass.states.get(f"{DOMAIN}.test")116 assert state is not None117 await hass.config_entries.async_remove(entry.entry_id)118 await hass.async_block_till_done()119 assert len(hass.states.async_all()) == 0...
test_binary_sensor.py
Source:test_binary_sensor.py
1"""Tests for the devolo Home Control binary sensors."""2from unittest.mock import patch3import pytest4from homeassistant.components.binary_sensor import DOMAIN5from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE6from homeassistant.core import HomeAssistant7from homeassistant.helpers import entity_registry8from homeassistant.helpers.entity import EntityCategory9from . import configure_integration10from .mocks import (11 HomeControlMock,12 HomeControlMockBinarySensor,13 HomeControlMockDisabledBinarySensor,14 HomeControlMockRemoteControl,15)16@pytest.mark.usefixtures("mock_zeroconf")17async def test_binary_sensor(hass: HomeAssistant):18 """Test setup and state change of a binary sensor device."""19 entry = configure_integration(hass)20 test_gateway = HomeControlMockBinarySensor()21 test_gateway.devices["Test"].status = 022 with patch(23 "homeassistant.components.devolo_home_control.HomeControl",24 side_effect=[test_gateway, HomeControlMock()],25 ):26 await hass.config_entries.async_setup(entry.entry_id)27 await hass.async_block_till_done()28 state = hass.states.get(f"{DOMAIN}.test")29 assert state is not None30 assert state.state == STATE_OFF31 state = hass.states.get(f"{DOMAIN}.test_2")32 assert state is not None33 er = entity_registry.async_get(hass)34 assert er.async_get(f"{DOMAIN}.test_2").entity_category == EntityCategory.DIAGNOSTIC35 # Emulate websocket message: sensor turned on36 test_gateway.publisher.dispatch("Test", ("Test", True))37 await hass.async_block_till_done()38 assert hass.states.get(f"{DOMAIN}.test").state == STATE_ON39 # Emulate websocket message: device went offline40 test_gateway.devices["Test"].status = 141 test_gateway.publisher.dispatch("Test", ("Status", False, "status"))42 await hass.async_block_till_done()43 assert hass.states.get(f"{DOMAIN}.test").state == STATE_UNAVAILABLE44@pytest.mark.usefixtures("mock_zeroconf")45async def test_remote_control(hass: HomeAssistant):46 """Test setup and state change of a remote control device."""47 entry = configure_integration(hass)48 test_gateway = HomeControlMockRemoteControl()49 test_gateway.devices["Test"].status = 050 with patch(51 "homeassistant.components.devolo_home_control.HomeControl",52 side_effect=[test_gateway, HomeControlMock()],53 ):54 await hass.config_entries.async_setup(entry.entry_id)55 await hass.async_block_till_done()56 state = hass.states.get(f"{DOMAIN}.test")57 assert state is not None58 assert state.state == STATE_OFF59 # Emulate websocket message: button pressed60 test_gateway.publisher.dispatch("Test", ("Test", 1))61 await hass.async_block_till_done()62 assert hass.states.get(f"{DOMAIN}.test").state == STATE_ON63 # Emulate websocket message: button released64 test_gateway.publisher.dispatch("Test", ("Test", 0))65 await hass.async_block_till_done()66 assert hass.states.get(f"{DOMAIN}.test").state == STATE_OFF67 # Emulate websocket message: device went offline68 test_gateway.devices["Test"].status = 169 test_gateway.publisher.dispatch("Test", ("Status", False, "status"))70 await hass.async_block_till_done()71 assert hass.states.get(f"{DOMAIN}.test").state == STATE_UNAVAILABLE72@pytest.mark.usefixtures("mock_zeroconf")73async def test_disabled(hass: HomeAssistant):74 """Test setup of a disabled device."""75 entry = configure_integration(hass)76 with patch(77 "homeassistant.components.devolo_home_control.HomeControl",78 side_effect=[HomeControlMockDisabledBinarySensor(), HomeControlMock()],79 ):80 await hass.config_entries.async_setup(entry.entry_id)81 await hass.async_block_till_done()82 assert hass.states.get(f"{DOMAIN}.devolo.WarningBinaryFI:Test") is None83@pytest.mark.usefixtures("mock_zeroconf")84async def test_remove_from_hass(hass: HomeAssistant):85 """Test removing entity."""86 entry = configure_integration(hass)87 test_gateway = HomeControlMockBinarySensor()88 with patch(89 "homeassistant.components.devolo_home_control.HomeControl",90 side_effect=[test_gateway, HomeControlMock()],91 ):92 await hass.config_entries.async_setup(entry.entry_id)93 await hass.async_block_till_done()94 state = hass.states.get(f"{DOMAIN}.test")95 assert state is not None96 await hass.config_entries.async_remove(entry.entry_id)97 await hass.async_block_till_done()98 assert len(hass.states.async_all()) == 0...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!