-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_switch.py
54 lines (46 loc) · 1.78 KB
/
test_switch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""Test MyHeat switch."""
from unittest.mock import call
from unittest.mock import patch
from custom_components.myheat import (
async_setup_entry,
)
from custom_components.myheat.const import (
DEFAULT_NAME,
)
from custom_components.myheat.const import (
DOMAIN,
)
from custom_components.myheat.const import (
SWITCH,
)
from homeassistant.components.switch import SERVICE_TURN_OFF
from homeassistant.components.switch import SERVICE_TURN_ON
from homeassistant.const import ATTR_ENTITY_ID
from pytest_homeassistant_custom_component.common import MockConfigEntry
from .const import MOCK_CONFIG
async def _test_switch_services(hass):
"""Test switch services."""
# Create a mock entry so we don't have to go through config flow
config_entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG, entry_id="test")
assert await async_setup_entry(hass, config_entry)
await hass.async_block_till_done()
# Functions/objects can be patched directly in test code as well and can be used to test
# additional things, like whether a function was called or what arguments it was called with
with patch("custom_components.myheat.MhApiClient.async_set_title") as title_func:
await hass.services.async_call(
SWITCH,
SERVICE_TURN_OFF,
service_data={ATTR_ENTITY_ID: f"{SWITCH}.{DEFAULT_NAME}_{SWITCH}"},
blocking=True,
)
assert title_func.called
assert title_func.call_args == call("foo")
title_func.reset_mock()
await hass.services.async_call(
SWITCH,
SERVICE_TURN_ON,
service_data={ATTR_ENTITY_ID: f"{SWITCH}.{DEFAULT_NAME}_{SWITCH}"},
blocking=True,
)
assert title_func.called
assert title_func.call_args == call("bar")