diff options
| author | Scott Gasch <[email protected]> | 2021-10-28 11:51:22 -0700 |
|---|---|---|
| committer | Scott Gasch <[email protected]> | 2021-10-28 11:51:22 -0700 |
| commit | 7e6972bc7c8e891dc669645fa5969ed76fe38314 (patch) | |
| tree | eec2f61e92f577e048602e5ced8d4a0cc471da6c /site_config.py | |
| parent | ed47f1a0c31184280a303563237e34c0e53437d7 (diff) | |
Moving smart lights into smart_home to prepare for adding
outlets and cameras.
Diffstat (limited to 'site_config.py')
| -rw-r--r-- | site_config.py | 72 |
1 files changed, 54 insertions, 18 deletions
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() |
