Add test coverage for ESPHome switch platform (#95306)

pull/95307/head
J. Nick Koston 2023-06-26 17:49:00 -05:00 committed by GitHub
parent dbe4252d34
commit 9fe24c54e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 1 deletions

View File

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

View File

@ -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)])