Moving smart lights into smart_home to prepare for adding
authorScott Gasch <[email protected]>
Thu, 28 Oct 2021 18:51:22 +0000 (11:51 -0700)
committerScott Gasch <[email protected]>
Thu, 28 Oct 2021 18:51:22 +0000 (11:51 -0700)
outlets and cameras.

datetime_utils.py
decorator_utils.py
directory_filter.py
presence.py
site_config.py
smart_home/lights.py [moved from light_utils.py with 100% similarity]

index 310ffe154d116348fa65e4c707a421c3c6721de0..7c5516b594b314aed141b4494e7eed5b820770f3 100644 (file)
@@ -62,6 +62,24 @@ def replace_timezone(dt: datetime.datetime,
     )
 
 
+def replace_time_timezone(t: datetime.time,
+                          tz: datetime.tzinfo) -> datetime.time:
+    """
+    Replaces the timezone on a datetime.time directly without performing
+    any translation.
+
+    >>> t = datetime.time(8, 15, 12, 0, pytz.UTC)
+    >>> t.tzname()
+    'UTC'
+
+    >>> t = replace_time_timezone(t, pytz.timezone('US/Pacific'))
+    >>> t.tzname()
+    'US/Pacific'
+
+    """
+    return t.replace(tzinfo=tz)
+
+
 def translate_timezone(dt: datetime.datetime,
                        tz: datetime.tzinfo) -> datetime.datetime:
     """
@@ -115,6 +133,42 @@ def date_to_datetime(date: datetime.date) -> datetime.datetime:
     )
 
 
+def time_to_datetime_today(time: datetime.time) -> datetime.datetime:
+    """
+    Given a time, returns that time as a datetime with a date component
+    set based on the current date.  If the time passed is timezone aware,
+    the resulting datetime will also be (and will use the same tzinfo).
+    If the time is timezone naive, the datetime returned will be too.
+
+    >>> t = datetime.time(13, 14, 0)
+    >>> d = now_pacific().date()
+    >>> dt = time_to_datetime_today(t)
+    >>> dt.date() == d
+    True
+
+    >>> dt.time() == t
+    True
+
+    >>> dt.tzinfo == t.tzinfo
+    True
+
+    >>> dt.tzinfo == None
+    True
+
+    >>> t = datetime.time(8, 15, 12, 0, pytz.UTC)
+    >>> t.tzinfo == None
+    False
+
+    >>> dt = time_to_datetime_today(t)
+    >>> dt.tzinfo == None
+    False
+
+    """
+    now = now_pacific()
+    tz = time.tzinfo
+    return datetime.datetime.combine(now, time, tz)
+
+
 def date_and_time_to_datetime(date: datetime.date,
                               time: datetime.time) -> datetime.datetime:
     """
index 76faec6f7be2e8d52f3ed9c7e05e1dc6048d666b..e19759f92a5e5b06ea425c2a0755d96ea783a7af 100644 (file)
@@ -435,7 +435,6 @@ def timeout(
         use_signals = thread_utils.is_current_thread_main_thread()
 
     def decorate(function):
-
         if use_signals:
 
             def handler(signum, frame):
index 5504609ad6d0e92257052370b4132e353c166338..d14dce7c6eec912e2b2c5b07ebe9b6ef4dfcf716 100644 (file)
@@ -6,7 +6,7 @@ from typing import Any, Optional
 
 
 class DirectoryFileFilter(object):
-    """A predicate that will return False if when a proposed file's
+    """A predicate that will return False if when a proposed file's
     content to-be-written is identical to the contents of the file;
     skip the write.
     """
index 947ff08706851c9d5bfa6b743bd53633afcfe9d6..e5bc64f0ba7b9666947200cdfa0f578d289d8b33 100755 (executable)
@@ -83,7 +83,10 @@ class PresenceDetection(object):
 
     def update(self) -> None:
         from exec_utils import cmd
-        persisted_macs = config.config['presence_macs_file']
+        try:
+            persisted_macs = config.config['presence_macs_file']
+        except KeyError:
+            persisted_macs = '/home/scott/cron/persisted_mac_addresses.txt'
         self.read_persisted_macs_file(persisted_macs, Location.HOUSE)
         raw = cmd(
             "ssh [email protected] 'cat /home/scott/cron/persisted_mac_addresses.txt'"
@@ -166,7 +169,7 @@ class PresenceDetection(object):
             location = dict_utils.key_with_min_value(tiebreaks)
             v = votes.get(location, 0)
             votes[location] = v + credit
-            logger.debug('{name}: {location} gets {credit} votes.')
+            logger.debug(f'{name}: {location} gets {credit} votes.')
             credit = int(
                 credit * 0.667
             )  # Note: list most important devices first
index 95ff5d403dcd900861c3a2c2ff8b37f3c5672c0a..332731277dde2ef16b983da04e749ba19975c496 100644 (file)
@@ -3,59 +3,95 @@
 from dataclasses import dataclass
 import logging
 import platform
-from typing import Optional
+from typing import Callable, Optional
 
 import config
+import presence
 
 logger = logging.getLogger(__name__)
 args = config.add_commandline_args(
     f'({__file__})',
     'Args related to __file__'
 )
-
 args.add_argument(
-    '--site_config_location',
-    default='AUTO',
-    const='AUTO',
+    '--site_config_override_location',
+    default='NONE',
+    const='NONE',
     nargs='?',
-    choices=('HOUSE', 'CABIN', 'AUTO'),
-    help='Where are we, HOUSE, CABIN or AUTO?',
+    choices=('HOUSE', 'CABIN', 'NONE'),
+    help='Where are we, HOUSE, CABIN?',
 )
 
 
 @dataclass
 class SiteConfig(object):
+    location: str
     network: str
     network_netmask: str
     network_router_ip: str
+    presence_location: presence.Location
+    is_anyone_present: Callable[None, bool]
 
 
 def get_location():
-    location = config.config['site_config_location']
-    if location == 'AUTO':
-        hostname = platform.node()
+    """
+    Where are we?
+
+    >>> location = get_location()
+    >>> location == 'HOUSE' or location == 'CABIN'
+    True
+
+    """
+    return get_config().location
+
+
+def is_anyone_present_wrapper(location: presence.Location):
+    p = presence.PresenceDetection()
+    return p.is_anyone_in_location_now(location)
+
+
+def get_config():
+    """
+    Get a configuration dataclass with information that is
+    site-specific including the current running location.
+
+    >>> cfg = get_config()
+    >>> cfg.location == 'HOUSE' or cfg.location == 'CABIN'
+    True
+
+    """
+    hostname = platform.node()
+    try:
+        location_override = config.config['site_config_override_location']
+    except KeyError:
+        location_override = 'NONE'
+    if location_override == 'NONE':
         if '.house' in hostname:
             location = 'HOUSE'
         elif '.cabin' in hostname:
             location = 'CABIN'
-        else:
-            raise Exception(f'Unknown hostname {hostname}, help.')
-    return location
-
-
-def get_config():
-    location = get_location()
     if location == 'HOUSE':
         return SiteConfig(
+            location = 'HOUSE',
             network = '10.0.0.0/24',
             network_netmask = '255.255.255.0',
             network_router_ip = '10.0.0.1',
+            presence_location = presence.Location.HOUSE,
+            is_anyone_present = lambda x=presence.Location.HOUSE: is_anyone_present_wrapper(x),
         )
     elif location == 'CABIN':
         return SiteConfig(
+            location = 'CABIN',
             network = '192.168.0.0/24',
             network_netmask = '255.255.255.0',
             network_router_ip = '192.168.0.1',
+            presence_location = presence.Location.CABIN,
+            is_anyone_present = lambda x=presence.Location.CABIN: is_anyone_present_wrapper(x),
         )
     else:
-        raise Exception('Unknown site location')
+        raise Exception(f'Unknown site location: {location}')
+
+
+if __name__ == '__main__':
+    import doctest
+    doctest.testmod()
similarity index 100%
rename from light_utils.py
rename to smart_home/lights.py