3 from dataclasses import dataclass
6 from typing import Callable
8 # Note: this module is fairly early loaded. Be aware of dependencies.
10 from type.locations import Location
12 logger = logging.getLogger(__name__)
14 args = config.add_commandline_args(
15 f'Global Site Config ({__file__})',
16 'Args related to global site-specific configuration',
19 '--site_config_override_location',
23 choices=['HOUSE', 'CABIN', 'NONE'],
24 help='Where are we, HOUSE, CABIN? Overrides standard detection code.',
29 class SiteConfig(object):
34 network_router_ip: str
35 presence_location: Location
36 is_anyone_present: Callable
37 arper_minimum_device_count: int
40 def get_location_name():
44 >>> location = get_location_name()
45 >>> location == 'HOUSE' or location == 'CABIN'
49 return get_config().location_name
54 Returns location as an enum instead of a string.
56 >>> from type.locations import Location
57 >>> location = get_location()
58 >>> location == Location.HOUSE or location == Location.CABIN
62 return get_config().location
65 def is_anyone_present_wrapper(location: Location):
68 p = base_presence.PresenceDetection()
69 return p.is_anyone_in_location_now(location)
74 Get a configuration dataclass with information that is
75 site-specific including the current running location.
77 >>> cfg = get_config()
78 >>> cfg.location_name == 'HOUSE' or cfg.location_name == 'CABIN'
82 hostname = platform.node()
84 location_override = config.config['site_config_override_location']
86 location_override = 'NONE'
87 if location_override == 'NONE':
88 if '.house' in hostname:
90 elif '.cabin' in hostname:
92 if location == 'HOUSE':
94 location_name='HOUSE',
95 location=Location.HOUSE,
96 network='10.0.0.0/24',
97 network_netmask='255.255.255.0',
98 network_router_ip='10.0.0.1',
99 presence_location=Location.HOUSE,
100 is_anyone_present=lambda x=Location.HOUSE: is_anyone_present_wrapper(x),
101 arper_minimum_device_count=50,
103 elif location == 'CABIN':
105 location_name='CABIN',
106 location=Location.CABIN,
107 network='192.168.0.0/24',
108 network_netmask='255.255.255.0',
109 network_router_ip='192.168.0.1',
110 presence_location=Location.CABIN,
111 is_anyone_present=lambda x=Location.CABIN: is_anyone_present_wrapper(x),
112 arper_minimum_device_count=15,
115 raise Exception(f'Unknown site location: {location}')
118 if __name__ == '__main__':