Add a simple test for waitable presence.
authorScott Gasch <[email protected]>
Sun, 6 Feb 2022 21:45:21 +0000 (13:45 -0800)
committerScott Gasch <[email protected]>
Sun, 6 Feb 2022 21:45:21 +0000 (13:45 -0800)
dateparse/dateparse_utils.py
string_utils.py
tests/waitable_presence_test.py [new file with mode: 0755]
waitable_presence.py

index 54a47366b6f6493ca7766d6ff72e52a0ac87cf54..79ce8a3d749046e3e4d61fb07300ca72ce317f3f 100755 (executable)
@@ -1,4 +1,5 @@
 #!/usr/bin/env python3
+# type: ignore
 
 """
 Parse dates in a variety of formats.
index 3ae9eeae96141fce9d4bb17516a5bace6ceb8b6f..ec662d234d8afa788cce2779e51f51622bc2862e 100644 (file)
@@ -142,16 +142,16 @@ MARGIN_RE = re.compile(r"^[^\S\r\n]+")
 ESCAPE_SEQUENCE_RE = re.compile(r"\e\[[^A-Za-z]*[A-Za-z]")
 
 NUM_SUFFIXES = {
-    "Pb": (1024 ** 5),
-    "P": (1024 ** 5),
-    "Tb": (1024 ** 4),
-    "T": (1024 ** 4),
-    "Gb": (1024 ** 3),
-    "G": (1024 ** 3),
-    "Mb": (1024 ** 2),
-    "M": (1024 ** 2),
-    "Kb": (1024 ** 1),
-    "K": (1024 ** 1),
+    "Pb": (1024**5),
+    "P": (1024**5),
+    "Tb": (1024**4),
+    "T": (1024**4),
+    "Gb": (1024**3),
+    "G": (1024**3),
+    "Mb": (1024**2),
+    "M": (1024**2),
+    "Kb": (1024**1),
+    "K": (1024**1),
 }
 
 
@@ -1075,7 +1075,7 @@ def to_date(in_str: str) -> Optional[datetime.date]:
     """
     Parses a date string.  See DateParser docs for details.
     """
-    import dateparse.dateparse_utils as dp  # type: ignore
+    import dateparse.dateparse_utils as dp
 
     try:
         d = dp.DateParser()
diff --git a/tests/waitable_presence_test.py b/tests/waitable_presence_test.py
new file mode 100755 (executable)
index 0000000..ff4225c
--- /dev/null
@@ -0,0 +1,33 @@
+#!/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()
index e1da4310b76a77693aa505452e0f3748c4fcc62d..46d7cbe04898c18cd72019c32c4dd1e5545c322e 100644 (file)
@@ -41,13 +41,17 @@ class WaitablePresenceDetectorWithMemory(state_tracker.WaitableAutomaticStateTra
         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,
@@ -87,14 +91,14 @@ class WaitablePresenceDetectorWithMemory(state_tracker.WaitableAutomaticStateTra
             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)