diff --git a/.coveragerc b/.coveragerc index 1dc9dd79904..39395d4667b 100644 --- a/.coveragerc +++ b/.coveragerc @@ -314,7 +314,6 @@ omit = homeassistant/components/esphome/domain_data.py homeassistant/components/esphome/entry_data.py homeassistant/components/esphome/light.py - homeassistant/components/esphome/switch.py homeassistant/components/etherscan/sensor.py homeassistant/components/eufy/* homeassistant/components/eufylife_ble/__init__.py diff --git a/tests/components/esphome/test_switch.py b/tests/components/esphome/test_switch.py new file mode 100644 index 00000000000..39e01a7d07c --- /dev/null +++ b/tests/components/esphome/test_switch.py @@ -0,0 +1,55 @@ +"""Test ESPHome switches.""" + + +from unittest.mock import call + +from aioesphomeapi import APIClient, SwitchInfo, SwitchState + +from homeassistant.components.switch import ( + DOMAIN as SWITCH_DOMAIN, + SERVICE_TURN_OFF, + SERVICE_TURN_ON, +) +from homeassistant.const import ATTR_ENTITY_ID, STATE_ON +from homeassistant.core import HomeAssistant + + +async def test_switch_generic_entity( + hass: HomeAssistant, mock_client: APIClient, mock_generic_device_entry +) -> None: + """Test a generic switch entity.""" + entity_info = [ + SwitchInfo( + object_id="myswitch", + key=1, + name="my switch", + unique_id="my_switch", + ) + ] + states = [SwitchState(key=1, state=True)] + user_service = [] + await mock_generic_device_entry( + mock_client=mock_client, + entity_info=entity_info, + user_service=user_service, + states=states, + ) + state = hass.states.get("switch.test_my_switch") + assert state is not None + assert state.state == STATE_ON + + await hass.services.async_call( + SWITCH_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: "switch.test_my_switch"}, + blocking=True, + ) + mock_client.switch_command.assert_has_calls([call(1, True)]) + + await hass.services.async_call( + SWITCH_DOMAIN, + SERVICE_TURN_OFF, + {ATTR_ENTITY_ID: "switch.test_my_switch"}, + blocking=True, + ) + mock_client.switch_command.assert_has_calls([call(1, False)])