--- /dev/null
+#!/usr/bin/env python3
+
+import re
+import unittest
+from unittest.mock import MagicMock
+
+import base_presence
+import unittest_utils # Needed for --unittests_ignore_perf flag
+import waitable_presence
+from type.locations import Location
+
+
+class TestWaitablePresence(unittest.TestCase):
+ def test_basic_functionality(self):
+ mock_detector = base_presence.PresenceDetection()
+ mock_detector.update = MagicMock()
+ mock_detector.is_anyone_in_location_now = MagicMock(return_value=True)
+ wp = waitable_presence.WaitablePresenceDetectorWithMemory(
+ 1.0, Location.HOUSE, mock_detector
+ )
+ changed = wp.wait()
+ (someone_is_home, since) = wp.is_someone_home()
+ mock_detector.update.assert_called_with()
+ mock_detector.is_anyone_in_location_now.assert_called_with(Location.HOUSE)
+ self.assertTrue(changed)
+ self.assertTrue(someone_is_home)
+ self.assertNotEqual(None, since)
+ wp.reset()
+ wp.shutdown()
+
+
+if __name__ == '__main__':
+ unittest.main()
self,
override_update_interval_sec: float = 60.0,
override_location: Location = site_config.get_location(),
+ injected_presence_detector: Optional[base_presence.PresenceDetection] = None,
) -> None:
self.last_someone_is_home: Optional[bool] = None
self.someone_is_home: Optional[bool] = None
self.everyone_gone_since: Optional[datetime.datetime] = None
self.someone_home_since: Optional[datetime.datetime] = None
self.location = override_location
- self.detector: base_presence.PresenceDetection = base_presence.PresenceDetection()
+ if injected_presence_detector is not None:
+ self.detector: base_presence.PresenceDetection = injected_presence_detector
+ else:
+ self.detector = base_presence.PresenceDetection()
super().__init__(
{
'poll_presence': override_update_interval_sec,
logger.debug('PresenceDetector is incomplete; trying to reinitialize...')
self.detector = base_presence.PresenceDetection()
- def is_someone_home(self) -> Tuple[bool, datetime.datetime]:
+ def is_someone_home(self) -> Optional[Tuple[bool, datetime.datetime]]:
"""Returns a tuple of a bool that indicates whether someone is home
and a datetime that indicates how long either someone has been
home or no one has been home.
"""
if self.someone_is_home is None:
- raise Exception("Too Soon!")
+ return None # checked too soon, wait a bit.
if self.someone_is_home:
assert self.someone_home_since is not None
return (True, self.someone_home_since)