forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_entity.py
216 lines (176 loc) · 7.26 KB
/
test_entity.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
"""Tests for the Bond entities."""
import asyncio
from datetime import timedelta
from unittest.mock import patch
from bond_async import BPUPSubscriptions, DeviceType
from bond_async.bpup import BPUP_ALIVE_TIMEOUT
from homeassistant.components import fan
from homeassistant.components.fan import DOMAIN as FAN_DOMAIN
from homeassistant.const import EVENT_HOMEASSISTANT_STOP, STATE_ON, STATE_UNAVAILABLE
from homeassistant.core import CoreState, HomeAssistant
from homeassistant.util import utcnow
from .common import patch_bond_device_state, setup_platform
from tests.common import async_fire_time_changed
def ceiling_fan(name: str):
"""Create a ceiling fan with given name."""
return {
"name": name,
"type": DeviceType.CEILING_FAN,
"actions": ["SetSpeed", "SetDirection"],
}
async def test_bpup_goes_offline_and_recovers_same_entity(hass: HomeAssistant) -> None:
"""Test that push updates fail and we fallback to polling and then bpup recovers.
The BPUP recovery is triggered by an update for the entity and
we do not fallback to polling because state is in sync.
"""
bpup_subs = BPUPSubscriptions()
with patch(
"homeassistant.components.bond.BPUPSubscriptions",
return_value=bpup_subs,
):
await setup_platform(
hass, FAN_DOMAIN, ceiling_fan("name-1"), bond_device_id="test-device-id"
)
bpup_subs.notify(
{
"s": 200,
"t": "devices/test-device-id/state",
"b": {"power": 1, "speed": 3, "direction": 0},
}
)
await hass.async_block_till_done()
assert hass.states.get("fan.name_1").attributes[fan.ATTR_PERCENTAGE] == 100
# Send a message for the wrong device to make sure its ignored
# we should never get this callback
bpup_subs.notify(
{
"s": 200,
"t": "devices/other-device-id/state",
"b": {"power": 1, "speed": 1, "direction": 0},
}
)
await hass.async_block_till_done()
assert hass.states.get("fan.name_1").attributes[fan.ATTR_PERCENTAGE] == 100
# Test we ignore messages for the wrong topic
bpup_subs.notify(
{
"s": 200,
"t": "devices/test-device-id/other_topic",
"b": {"power": 1, "speed": 1, "direction": 0},
}
)
await hass.async_block_till_done()
assert hass.states.get("fan.name_1").attributes[fan.ATTR_PERCENTAGE] == 100
bpup_subs.notify(
{
"s": 200,
"t": "devices/test-device-id/state",
"b": {"power": 1, "speed": 1, "direction": 0},
}
)
await hass.async_block_till_done()
assert hass.states.get("fan.name_1").attributes[fan.ATTR_PERCENTAGE] == 33
bpup_subs.last_message_time = -BPUP_ALIVE_TIMEOUT
with patch_bond_device_state(side_effect=asyncio.TimeoutError):
async_fire_time_changed(hass, utcnow() + timedelta(seconds=230))
await hass.async_block_till_done()
assert hass.states.get("fan.name_1").state == STATE_UNAVAILABLE
# Ensure we do not poll to get the state
# since bpup has recovered and we know we
# are back in sync
with patch_bond_device_state(side_effect=Exception):
bpup_subs.notify(
{
"s": 200,
"t": "devices/test-device-id/state",
"b": {"power": 1, "speed": 2, "direction": 0},
}
)
await hass.async_block_till_done()
state = hass.states.get("fan.name_1")
assert state.state == STATE_ON
assert state.attributes[fan.ATTR_PERCENTAGE] == 66
async def test_bpup_goes_offline_and_recovers_different_entity(
hass: HomeAssistant,
) -> None:
"""Test that push updates fail and we fallback to polling and then bpup recovers.
The BPUP recovery is triggered by an update for a different entity which
forces a poll since we need to re-get the state.
"""
bpup_subs = BPUPSubscriptions()
with patch(
"homeassistant.components.bond.BPUPSubscriptions",
return_value=bpup_subs,
):
await setup_platform(
hass, FAN_DOMAIN, ceiling_fan("name-1"), bond_device_id="test-device-id"
)
bpup_subs.notify(
{
"s": 200,
"t": "devices/test-device-id/state",
"b": {"power": 1, "speed": 3, "direction": 0},
}
)
await hass.async_block_till_done()
assert hass.states.get("fan.name_1").attributes[fan.ATTR_PERCENTAGE] == 100
bpup_subs.notify(
{
"s": 200,
"t": "devices/test-device-id/state",
"b": {"power": 1, "speed": 1, "direction": 0},
}
)
await hass.async_block_till_done()
assert hass.states.get("fan.name_1").attributes[fan.ATTR_PERCENTAGE] == 33
bpup_subs.last_message_time = -BPUP_ALIVE_TIMEOUT
with patch_bond_device_state(side_effect=asyncio.TimeoutError):
async_fire_time_changed(hass, utcnow() + timedelta(seconds=230))
await hass.async_block_till_done()
assert hass.states.get("fan.name_1").state == STATE_UNAVAILABLE
bpup_subs.notify(
{
"s": 200,
"t": "devices/not-this-device-id/state",
"b": {"power": 1, "speed": 2, "direction": 0},
}
)
await hass.async_block_till_done()
assert hass.states.get("fan.name_1").state == STATE_UNAVAILABLE
with patch_bond_device_state(return_value={"power": 1, "speed": 1}):
async_fire_time_changed(hass, utcnow() + timedelta(seconds=430))
await hass.async_block_till_done()
state = hass.states.get("fan.name_1")
assert state.state == STATE_ON
assert state.attributes[fan.ATTR_PERCENTAGE] == 33
async def test_polling_fails_and_recovers(hass: HomeAssistant) -> None:
"""Test that polling fails and we recover."""
await setup_platform(
hass, FAN_DOMAIN, ceiling_fan("name-1"), bond_device_id="test-device-id"
)
with patch_bond_device_state(side_effect=asyncio.TimeoutError):
async_fire_time_changed(hass, utcnow() + timedelta(seconds=230))
await hass.async_block_till_done()
assert hass.states.get("fan.name_1").state == STATE_UNAVAILABLE
with patch_bond_device_state(return_value={"power": 1, "speed": 1}):
async_fire_time_changed(hass, utcnow() + timedelta(seconds=230))
await hass.async_block_till_done()
state = hass.states.get("fan.name_1")
assert state.state == STATE_ON
assert state.attributes[fan.ATTR_PERCENTAGE] == 33
async def test_polling_stops_at_the_stop_event(hass: HomeAssistant) -> None:
"""Test that polling stops at the stop event."""
await setup_platform(
hass, FAN_DOMAIN, ceiling_fan("name-1"), bond_device_id="test-device-id"
)
with patch_bond_device_state(side_effect=asyncio.TimeoutError):
async_fire_time_changed(hass, utcnow() + timedelta(seconds=230))
await hass.async_block_till_done()
assert hass.states.get("fan.name_1").state == STATE_UNAVAILABLE
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
hass.state = CoreState.stopping
await hass.async_block_till_done()
with patch_bond_device_state(return_value={"power": 1, "speed": 1}):
async_fire_time_changed(hass, utcnow() + timedelta(seconds=230))
await hass.async_block_till_done()
assert hass.states.get("fan.name_1").state == STATE_UNAVAILABLE