)
+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:
"""
)
+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:
"""
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(
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
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()