From: Scott Gasch Date: Thu, 28 Oct 2021 18:51:22 +0000 (-0700) Subject: Moving smart lights into smart_home to prepare for adding X-Git-Url: https://wannabe.guru.org/gitweb/?a=commitdiff_plain;h=7e6972bc7c8e891dc669645fa5969ed76fe38314;p=python_utils.git Moving smart lights into smart_home to prepare for adding outlets and cameras. --- diff --git a/datetime_utils.py b/datetime_utils.py index 310ffe1..7c5516b 100644 --- a/datetime_utils.py +++ b/datetime_utils.py @@ -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: """ diff --git a/decorator_utils.py b/decorator_utils.py index 76faec6..e19759f 100644 --- a/decorator_utils.py +++ b/decorator_utils.py @@ -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): diff --git a/directory_filter.py b/directory_filter.py index 5504609..d14dce7 100644 --- a/directory_filter.py +++ b/directory_filter.py @@ -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. """ diff --git a/presence.py b/presence.py index 947ff08..e5bc64f 100755 --- a/presence.py +++ b/presence.py @@ -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 scott@meerkat.cabin '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 diff --git a/site_config.py b/site_config.py index 95ff5d4..3327312 100644 --- a/site_config.py +++ b/site_config.py @@ -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() diff --git a/light_utils.py b/smart_home/lights.py similarity index 100% rename from light_utils.py rename to smart_home/lights.py