test_device.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. from datetime import datetime
  2. from time import time
  3. from unittest import IsolatedAsyncioTestCase
  4. from unittest.mock import ANY, AsyncMock, Mock, call, patch
  5. from homeassistant.const import EVENT_HOMEASSISTANT_STARTED, EVENT_HOMEASSISTANT_STOP
  6. from custom_components.tuya_local.device import TuyaLocalDevice
  7. from .const import EUROM_600_HEATER_PAYLOAD
  8. class TestDevice(IsolatedAsyncioTestCase):
  9. def setUp(self):
  10. device_patcher = patch("tinytuya.Device")
  11. self.addCleanup(device_patcher.stop)
  12. self.mock_api = device_patcher.start()
  13. hass_patcher = patch("homeassistant.core.HomeAssistant")
  14. self.addCleanup(hass_patcher.stop)
  15. self.hass = hass_patcher.start()
  16. self.hass().is_running = True
  17. self.hass().is_stopping = False
  18. def job(func, *args):
  19. return func(*args)
  20. self.hass().async_add_executor_job = AsyncMock()
  21. self.hass().async_add_executor_job.side_effect = job
  22. sleep_patcher = patch("asyncio.sleep")
  23. self.addCleanup(sleep_patcher.stop)
  24. self.mock_sleep = sleep_patcher.start()
  25. lock_patcher = patch("custom_components.tuya_local.device.Lock")
  26. self.addCleanup(lock_patcher.stop)
  27. self.mock_lock = lock_patcher.start()
  28. self.subject = TuyaLocalDevice(
  29. "Some name",
  30. "some_dev_id",
  31. "some.ip.address",
  32. "some_local_key",
  33. "auto",
  34. None,
  35. self.hass(),
  36. )
  37. # For most tests we want the protocol working
  38. self.subject._api_protocol_version_index = 0
  39. self.subject._api_protocol_working = True
  40. self.subject._protocol_configured = "auto"
  41. def test_configures_tinytuya_correctly(self):
  42. self.mock_api.assert_called_once_with(
  43. "some_dev_id", "some.ip.address", "some_local_key"
  44. )
  45. self.assertIs(self.subject._api, self.mock_api())
  46. def test_name(self):
  47. """Returns the name given at instantiation."""
  48. self.assertEqual(self.subject.name, "Some name")
  49. def test_unique_id(self):
  50. """Returns the unique ID presented by the API class."""
  51. self.assertIs(self.subject.unique_id, self.mock_api().id)
  52. def test_device_info(self):
  53. """Returns generic info plus the unique ID for categorisation."""
  54. self.assertEqual(
  55. self.subject.device_info,
  56. {
  57. "identifiers": {("tuya_local", self.mock_api().id)},
  58. "name": "Some name",
  59. "manufacturer": "Tuya",
  60. },
  61. )
  62. def test_has_returned_state(self):
  63. """Returns True if the device has returned its state."""
  64. self.subject._cached_state = EUROM_600_HEATER_PAYLOAD
  65. self.assertTrue(self.subject.has_returned_state)
  66. self.subject._cached_state = {"updated_at": 0}
  67. self.assertFalse(self.subject.has_returned_state)
  68. async def test_refreshes_state_if_no_cached_state_exists(self):
  69. self.subject._cached_state = {}
  70. self.subject.async_refresh = AsyncMock()
  71. await self.subject.async_inferred_type()
  72. self.subject.async_refresh.assert_awaited()
  73. async def test_detection_returns_none_when_device_type_not_detected(self):
  74. self.subject._cached_state = {"2": False, "updated_at": datetime.now()}
  75. self.assertEqual(await self.subject.async_inferred_type(), None)
  76. async def test_refreshes_when_there_is_no_pending_reset(self):
  77. self.subject._cached_state = {"updated_at": time() - 19}
  78. self.mock_api().status.return_value = {"dps": {"1": "called"}}
  79. await self.subject.async_refresh()
  80. self.mock_api().status.assert_called_once()
  81. self.assertEqual(self.subject._cached_state["1"], "called")
  82. async def test_refreshes_when_there_is_expired_pending_reset(self):
  83. self.subject._cached_state = {"updated_at": time() - 20}
  84. self.mock_api().status.return_value = {"dps": {"1": "called"}}
  85. await self.subject.async_refresh()
  86. self.mock_api().status.assert_called_once()
  87. self.assertEqual(self.subject._cached_state["1"], "called")
  88. async def test_refresh_retries_up_to_eleven_times(self):
  89. self.subject._api_protocol_working = False
  90. self.mock_api().status.side_effect = [
  91. Exception("Error"),
  92. Exception("Error"),
  93. Exception("Error"),
  94. Exception("Error"),
  95. Exception("Error"),
  96. Exception("Error"),
  97. Exception("Error"),
  98. Exception("Error"),
  99. Exception("Error"),
  100. Exception("Error"),
  101. {"dps": {"1": False}},
  102. ]
  103. await self.subject.async_refresh()
  104. self.assertEqual(self.mock_api().status.call_count, 11)
  105. self.assertEqual(self.subject._cached_state["1"], False)
  106. async def test_refresh_clears_cache_after_allowed_failures(self):
  107. self.subject._cached_state = {"1": True}
  108. self.subject._pending_updates = {
  109. "1": {"value": False, "updated_at": datetime.now(), "sent": True}
  110. }
  111. self.mock_api().status.side_effect = [
  112. Exception("Error"),
  113. Exception("Error"),
  114. Exception("Error"),
  115. ]
  116. await self.subject.async_refresh()
  117. self.assertEqual(self.mock_api().status.call_count, 3)
  118. self.assertEqual(self.subject._cached_state, {"updated_at": 0})
  119. self.assertEqual(self.subject._pending_updates, {})
  120. async def test_api_protocol_version_is_rotated_with_each_failure(self):
  121. self.subject._api_protocol_version_index = None
  122. self.subject._api_protocol_working = False
  123. self.mock_api().status.side_effect = [
  124. Exception("Error"),
  125. Exception("Error"),
  126. Exception("Error"),
  127. Exception("Error"),
  128. Exception("Error"),
  129. Exception("Error"),
  130. Exception("Error"),
  131. ]
  132. await self.subject.async_refresh()
  133. self.mock_api().set_version.assert_has_calls(
  134. [call(3.1), call(3.2), call(3.4), call(3.5), call(3.3), call(3.1)]
  135. )
  136. async def test_api_protocol_version_is_stable_once_successful(self):
  137. self.subject._api_protocol_version_index = None
  138. self.subject._api_protocol_working = False
  139. self.mock_api().status.side_effect = [
  140. Exception("Error"),
  141. Exception("Error"),
  142. Exception("Error"),
  143. {"dps": {"1": False}},
  144. {"dps": {"1": False}},
  145. Exception("Error"),
  146. Exception("Error"),
  147. {"dps": {"1": False}},
  148. ]
  149. await self.subject.async_refresh()
  150. self.assertEqual(self.subject._api_protocol_version_index, 3)
  151. self.assertTrue(self.subject._api_protocol_working)
  152. await self.subject.async_refresh()
  153. self.assertEqual(self.subject._api_protocol_version_index, 3)
  154. await self.subject.async_refresh()
  155. self.assertEqual(self.subject._api_protocol_version_index, 3)
  156. self.mock_api().set_version.assert_has_calls(
  157. [
  158. call(3.1),
  159. call(3.2),
  160. call(3.4),
  161. ]
  162. )
  163. async def test_api_protocol_version_is_not_rotated_when_not_auto(self):
  164. # Set up preconditions for the test
  165. self.subject._protocol_configured = 3.4
  166. self.subject._api_protocol_version_index = None
  167. self.subject._api_protocol_working = False
  168. self.mock_api().status.side_effect = [
  169. Exception("Error"),
  170. Exception("Error"),
  171. Exception("Error"),
  172. {"dps": {"1": False}},
  173. {"dps": {"1": False}},
  174. Exception("Error"),
  175. Exception("Error"),
  176. Exception("Error"),
  177. Exception("Error"),
  178. Exception("Error"),
  179. Exception("Error"),
  180. Exception("Error"),
  181. {"dps": {"1": False}},
  182. ]
  183. await self.subject._rotate_api_protocol_version()
  184. self.mock_api().set_version.assert_called_once_with(3.4)
  185. self.mock_api().set_version.reset_mock()
  186. await self.subject.async_refresh()
  187. self.assertEqual(self.subject._api_protocol_version_index, 3)
  188. await self.subject.async_refresh()
  189. self.assertEqual(self.subject._api_protocol_version_index, 3)
  190. await self.subject.async_refresh()
  191. self.assertEqual(self.subject._api_protocol_version_index, 3)
  192. def test_reset_cached_state_clears_cached_state_and_pending_updates(self):
  193. self.subject._cached_state = {"1": True, "updated_at": time()}
  194. self.subject._pending_updates = {
  195. "1": {"value": False, "updated_at": datetime.now(), "sent": True}
  196. }
  197. self.subject._reset_cached_state()
  198. self.assertEqual(self.subject._cached_state, {"updated_at": 0})
  199. self.assertEqual(self.subject._pending_updates, {})
  200. def test_get_property_returns_value_from_cached_state(self):
  201. self.subject._cached_state = {"1": True}
  202. self.assertEqual(self.subject.get_property("1"), True)
  203. def test_get_property_returns_pending_update_value(self):
  204. self.subject._pending_updates = {
  205. "1": {"value": False, "updated_at": time() - 4, "sent": True}
  206. }
  207. self.assertEqual(self.subject.get_property("1"), False)
  208. def test_pending_update_value_overrides_cached_value(self):
  209. self.subject._cached_state = {"1": True}
  210. self.subject._pending_updates = {
  211. "1": {"value": False, "updated_at": time() - 4, "sent": True}
  212. }
  213. self.assertEqual(self.subject.get_property("1"), False)
  214. def test_expired_pending_update_value_does_not_override_cached_value(self):
  215. self.subject._cached_state = {"1": True}
  216. self.subject._pending_updates = {
  217. "1": {"value": False, "updated_at": time() - 5, "sent": True}
  218. }
  219. self.assertEqual(self.subject.get_property("1"), True)
  220. def test_get_property_returns_none_when_value_does_not_exist(self):
  221. self.subject._cached_state = {"1": True}
  222. self.assertIs(self.subject.get_property("2"), None)
  223. async def test_async_set_property_sends_to_api(self):
  224. await self.subject.async_set_property("1", False)
  225. self.mock_api().set_multiple_values.assert_called_once()
  226. async def test_set_property_immediately_stores_pending_updates(self):
  227. self.subject._cached_state = {"1": True}
  228. await self.subject.async_set_property("1", False)
  229. self.assertFalse(self.subject.get_property("1"))
  230. async def test_set_properties_takes_no_action_when_nothing_provided(self):
  231. with patch("asyncio.sleep") as mock:
  232. await self.subject.async_set_properties({})
  233. mock.assert_not_called()
  234. def test_anticipate_property_value_updates_cached_state(self):
  235. self.subject._cached_state = {"1": True}
  236. self.subject.anticipate_property_value("1", False)
  237. self.assertEqual(self.subject._cached_state["1"], False)
  238. def test_get_key_for_value_returns_key_from_object_matching_value(self):
  239. obj = {"key1": "value1", "key2": "value2"}
  240. self.assertEqual(
  241. TuyaLocalDevice.get_key_for_value(obj, "value1"),
  242. "key1",
  243. )
  244. self.assertEqual(
  245. TuyaLocalDevice.get_key_for_value(obj, "value2"),
  246. "key2",
  247. )
  248. def test_get_key_for_value_returns_fallback_when_value_not_found(self):
  249. obj = {"key1": "value1", "key2": "value2"}
  250. self.assertEqual(
  251. TuyaLocalDevice.get_key_for_value(obj, "value3", fallback="fb"),
  252. "fb",
  253. )
  254. def test_refresh_cached_state(self):
  255. # set up preconditions
  256. self.mock_api().status.return_value = {"dps": {"1": "CHANGED"}}
  257. self.subject._cached_state = {"1": "UNCHANGED", "updated_at": 123}
  258. # call the function under test
  259. self.subject._refresh_cached_state()
  260. # Did it call the API as expected?
  261. self.mock_api().status.assert_called_once()
  262. # Did it update the cached state?
  263. self.assertDictEqual(
  264. self.subject._cached_state,
  265. {"1": "CHANGED"} | self.subject._cached_state,
  266. )
  267. # Did it update the timestamp on the cached state?
  268. self.assertAlmostEqual(
  269. self.subject._cached_state["updated_at"],
  270. time(),
  271. delta=2,
  272. )
  273. def test_set_values(self):
  274. # set up preconditions
  275. self.subject._pending_updates = {
  276. "1": {"value": "sample", "updated_at": time() - 2, "sent": False},
  277. }
  278. # call the function under test
  279. self.subject._set_values({"1": "sample"})
  280. # did it send what it was asked?
  281. self.mock_api().set_multiple_values.assert_called_once_with(
  282. {"1": "sample"}, nowait=True
  283. )
  284. # did it mark the pending updates as sent?
  285. self.assertTrue(self.subject._pending_updates["1"]["sent"])
  286. # did it update the time on the pending updates?
  287. self.assertAlmostEqual(
  288. self.subject._pending_updates["1"]["updated_at"],
  289. time(),
  290. delta=2,
  291. )
  292. # did it lock and unlock when sending
  293. self.subject._lock.acquire.assert_called_once()
  294. self.subject._lock.release.assert_called_once()
  295. def test_pending_updates_cleared_on_receipt(self):
  296. # Set up the preconditions
  297. now = time()
  298. self.subject._pending_updates = {
  299. "1": {"value": True, "updated_at": now, "sent": True},
  300. "2": {"value": True, "updated_at": now, "sent": False}, # unsent
  301. "3": {"value": True, "updated_at": now, "sent": True}, # unmatched
  302. "4": {"value": True, "updated_at": now, "sent": True}, # not received
  303. }
  304. self.subject._remove_properties_from_pending_updates(
  305. {"1": True, "2": True, "3": False}
  306. )
  307. self.assertDictEqual(
  308. self.subject._pending_updates,
  309. {
  310. "2": {"value": True, "updated_at": now, "sent": False},
  311. "3": {"value": True, "updated_at": now, "sent": True},
  312. "4": {"value": True, "updated_at": now, "sent": True},
  313. },
  314. )
  315. def test_actually_start(self):
  316. # Set up the preconditions
  317. self.subject.receive_loop = Mock()
  318. self.subject.receive_loop.return_value = "LOOP"
  319. self.hass().bus.async_listen_once.return_value = "LISTENER"
  320. self.subject._running = False
  321. # run the function under test
  322. self.subject.actually_start()
  323. # did it register a listener for EVENT_HOMEASSISTANT_STOP?
  324. self.hass().bus.async_listen_once.assert_called_once_with(
  325. EVENT_HOMEASSISTANT_STOP, self.subject.async_stop
  326. )
  327. self.assertEqual(self.subject._shutdown_listener, "LISTENER")
  328. # did it set the running flag?
  329. self.assertTrue(self.subject._running)
  330. # did it schedule the loop?
  331. self.hass().async_create_task.assert_called_once_with("LOOP")
  332. def test_start_starts_when_ha_running(self):
  333. # Set up preconditions
  334. self.hass().is_running = True
  335. listener = Mock()
  336. self.subject._startup_listener = listener
  337. self.subject.actually_start = Mock()
  338. # Call the function under test
  339. self.subject.start()
  340. # Did it actually start?
  341. self.subject.actually_start.assert_called_once()
  342. # Did it cancel the startup listener?
  343. self.assertIsNone(self.subject._startup_listener)
  344. listener.assert_called_once()
  345. def test_start_schedules_for_later_when_ha_starting(self):
  346. # Set up preconditions
  347. self.hass().is_running = False
  348. self.hass().bus.async_listen_once.return_value = "LISTENER"
  349. self.subject.actually_start = Mock()
  350. # Call the function under test
  351. self.subject.start()
  352. # Did it avoid actually starting?
  353. self.subject.actually_start.assert_not_called()
  354. # Did it register a listener?
  355. self.assertEqual(self.subject._startup_listener, "LISTENER")
  356. self.hass().bus.async_listen_once.assert_called_once_with(
  357. EVENT_HOMEASSISTANT_STARTED, self.subject.actually_start
  358. )
  359. def test_start_does_nothing_when_ha_stopping(self):
  360. # Set up preconditions
  361. self.hass().is_running = True
  362. self.hass().is_stopping = True
  363. self.subject.actually_start = Mock()
  364. # Call the function under test
  365. self.subject.start()
  366. # Did it avoid actually starting?
  367. self.subject.actually_start.assert_not_called()
  368. # Did it avoid registering a listener?
  369. self.hass().bus.async_listen_once.assert_not_called()
  370. self.assertIsNone(self.subject._startup_listener)
  371. async def test_async_stop(self):
  372. # Set up preconditions
  373. listener = Mock()
  374. self.subject._refresh_task = None
  375. self.subject._shutdown_listener = listener
  376. self.subject._children = [1, 2, 3]
  377. # Call the function under test
  378. await self.subject.async_stop()
  379. # Shutdown listener doesn't get cancelled as HA does that
  380. listener.assert_not_called()
  381. # Were the child entities cleared?
  382. self.assertEqual(self.subject._children, [])
  383. # Did it wait for the refresh task to finish then clear it?
  384. # This doesn't work because AsyncMock only mocks awaitable method calls
  385. # but we want an awaitable object
  386. # refresh.assert_awaited_once()
  387. self.assertIsNone(self.subject._refresh_task)
  388. async def test_async_stop_when_not_running(self):
  389. # Set up preconditions
  390. self._refresh_task = None
  391. self.subject._shutdown_listener = None
  392. self.subject._children = []
  393. # Call the function under test
  394. await self.subject.async_stop()
  395. # Was the shutdown listener left empty?
  396. self.assertIsNone(self.subject._shutdown_listener)
  397. # Were the child entities cleared?
  398. self.assertEqual(self.subject._children, [])
  399. # Was the refresh task left empty?
  400. self.assertIsNone(self.subject._refresh_task)
  401. def test_register_first_entity_ha_running(self):
  402. # Set up preconditions
  403. self.subject._children = []
  404. self.subject._running = False
  405. self.subject._startup_listener = None
  406. self.subject.start = Mock()
  407. entity = AsyncMock()
  408. entity._config = Mock()
  409. entity._config.dps.return_value = []
  410. # despite the name, the below HA function is not async and does not need to be awaited
  411. entity.async_schedule_update_ha_state = Mock()
  412. # Call the function under test
  413. self.subject.register_entity(entity)
  414. # Was the entity added to the list?
  415. self.assertEqual(self.subject._children, [entity])
  416. # Did we start the loop?
  417. self.subject.start.assert_called_once()
  418. def test_register_subsequent_entity_ha_running(self):
  419. # Set up preconditions
  420. first = AsyncMock()
  421. second = AsyncMock()
  422. second._config = Mock()
  423. second._config.dps.return_value = []
  424. self.subject._children = [first]
  425. self.subject._running = True
  426. self.subject._startup_listener = None
  427. self.subject.start = Mock()
  428. # Call the function under test
  429. self.subject.register_entity(second)
  430. # Was the entity added to the list?
  431. self.assertCountEqual(self.subject._children, [first, second])
  432. # Did we avoid restarting the loop?
  433. self.subject.start.assert_not_called()
  434. def test_register_subsequent_entity_ha_starting(self):
  435. # Set up preconditions
  436. first = AsyncMock()
  437. second = AsyncMock()
  438. second._config = Mock()
  439. second._config.dps.return_value = []
  440. self.subject._children = [first]
  441. self.subject._running = False
  442. self.subject._startup_listener = Mock()
  443. self.subject.start = Mock()
  444. # Call the function under test
  445. self.subject.register_entity(second)
  446. # Was the entity added to the list?
  447. self.assertCountEqual(self.subject._children, [first, second])
  448. # Did we avoid restarting the loop?
  449. self.subject.start.assert_not_called()
  450. async def test_unregister_one_of_many_entities(self):
  451. # Set up preconditions
  452. self.subject._children = ["First", "Second"]
  453. self.subject.async_stop = AsyncMock()
  454. # Call the function under test
  455. await self.subject.async_unregister_entity("First")
  456. # Was the entity removed from the list?
  457. self.assertCountEqual(self.subject._children, ["Second"])
  458. # Is the loop still running?
  459. self.subject.async_stop.assert_not_called()
  460. async def test_unregister_last_entity(self):
  461. # Set up preconditions
  462. self.subject._children = ["Last"]
  463. self.subject.async_stop = AsyncMock()
  464. # Call the function under test
  465. await self.subject.async_unregister_entity("Last")
  466. # Was the entity removed from the list?
  467. self.assertEqual(self.subject._children, [])
  468. # Was the loop stopped?
  469. self.subject.async_stop.assert_called_once()
  470. async def test_async_receive(self):
  471. # Set up preconditions
  472. self.mock_api().status.return_value = {"dps": {"1": "INIT", "2": 2}}
  473. self.mock_api().receive.return_value = {"1": "UPDATED"}
  474. self.subject._running = True
  475. self.subject._cached_state = {"updated_at": 0}
  476. # Call the function under test
  477. print("starting test loop...")
  478. loop = self.subject.async_receive()
  479. print("getting first iteration...")
  480. result = await loop.__anext__()
  481. # Check that the loop was started, but without persistent connection
  482. # since there was no state returned yet and it might need to negotiate
  483. # version.
  484. self.mock_api().set_socketPersistent.assert_called_once_with(False)
  485. # Check that a full poll was done
  486. self.mock_api().status.assert_called_once()
  487. self.assertDictEqual(result, {"1": "INIT", "2": 2, "full_poll": ANY})
  488. # Prepare for next round
  489. self.subject._cached_state = self.subject._cached_state | result
  490. self.mock_api().set_socketPersistent.reset_mock()
  491. self.mock_api().status.reset_mock()
  492. self.subject._cached_state["updated_at"] = time()
  493. # Call the function under test
  494. print("getting second iteration...")
  495. result = await loop.__anext__()
  496. # Check that a heartbeat poll was done
  497. self.mock_api().status.assert_not_called()
  498. self.mock_api().heartbeat.assert_called_once()
  499. self.mock_api().receive.assert_called_once()
  500. self.assertDictEqual(result, {"1": "UPDATED", "full_poll": ANY})
  501. # Check that the connection was made persistent now that data has been
  502. # returned
  503. self.mock_api().set_socketPersistent.assert_called_once_with(True)
  504. # Prepare for next iteration
  505. self.subject._running = False
  506. self.mock_api().set_socketPersistent.reset_mock()
  507. # Call the function under test
  508. print("getting last iteration...")
  509. try:
  510. result = await loop.__anext__()
  511. self.fail("Should have raised an exception to quit the loop")
  512. # Check that the loop terminated
  513. except StopAsyncIteration:
  514. pass
  515. self.mock_api().set_socketPersistent.assert_called_once_with(False)
  516. def test_should_poll(self):
  517. self.subject._cached_state = {"1": "sample", "updated_at": time()}
  518. self.subject._poll_only = False
  519. self.subject._temporary_poll = False
  520. # Test temporary poll via pause/resume
  521. self.assertFalse(self.subject.should_poll)
  522. self.subject.pause()
  523. self.assertTrue(self.subject.should_poll)
  524. self.subject.resume()
  525. self.assertFalse(self.subject.should_poll)
  526. # Test configured polling
  527. self.subject._poll_only = True
  528. self.assertTrue(self.subject.should_poll)
  529. self.subject._poll_only = False
  530. # Test initial polling
  531. self.subject._cached_state = {}
  532. self.assertTrue(self.subject.should_poll)