-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
228 lines (181 loc) · 8.36 KB
/
config.py
File metadata and controls
228 lines (181 loc) · 8.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Configuration module for WSPR HA Bridge.
Loads configuration from environment variables with sensible defaults.
"""
import os
import sys
def str_to_bool(value):
"""Convert string to boolean. Handles common string representations."""
if isinstance(value, bool):
return value
if not value:
return False
return value.lower() in ('true', '1', 'yes', 'on', 't', 'y')
def str_to_int(value, default=0):
"""Convert string to integer with fallback to default."""
try:
return int(value) if value else default
except (ValueError, TypeError):
return default
def parse_list(value):
"""Parse comma-separated string into list, stripping whitespace."""
if not value:
return []
if isinstance(value, list):
return value
return [item.strip() for item in value.split(',') if item.strip()]
# ==============================================================================
# --- Core Identity ---
# ==============================================================================
MY_CALLSIGN = os.getenv('MY_CALLSIGN', 'YOUR_CALLSIGN')
# ==============================================================================
# --- Debugging ---
# ==============================================================================
DEBUG_MODE = str_to_bool(os.getenv('DEBUG_MODE', 'False'))
# ==============================================================================
# --- WSPR.live Data Source ---
# ==============================================================================
WSPR_LIVE_URL = os.getenv('WSPR_LIVE_URL', 'http://db1.wspr.live/')
POLL_INTERVAL_SECONDS = str_to_int(
os.getenv('POLL_INTERVAL_SECONDS'),
default=150 # 2.5 minutes - aligns with WSPR 2-min TX cycle
)
# ==============================================================================
# --- Home Assistant MQTT Broker ---
# ==============================================================================
HA_MQTT_BROKER = os.getenv('HA_MQTT_BROKER', 'YOUR_MQTT_BROKER_IP')
HA_MQTT_PORT = str_to_int(os.getenv('HA_MQTT_PORT'), default=1883)
HA_MQTT_USER = os.getenv('HA_MQTT_USER') or None
HA_MQTT_PASS = os.getenv('HA_MQTT_PASS') or None
# ==============================================================================
# --- Script Operation Mode ---
# ==============================================================================
SCRIPT_DIRECTION = os.getenv('SCRIPT_DIRECTION', 'rx')
# ==============================================================================
# --- Statistics Timing ---
# ==============================================================================
STATS_INTERVAL_WINDOW_SECONDS = str_to_int(
os.getenv('STATS_INTERVAL_WINDOW_SECONDS'),
default=900
)
STATS_UPDATE_INTERVAL_SECONDS = str_to_int(
os.getenv('STATS_UPDATE_INTERVAL_SECONDS'),
default=300
)
# ==============================================================================
# --- Spot Sensor Control & Filtering ---
# ==============================================================================
ENABLE_SPOT_SENSORS = str_to_bool(os.getenv('ENABLE_SPOT_SENSORS', 'True'))
SPOT_FILTER_MIN_DISTANCE_KM = str_to_int(
os.getenv('SPOT_FILTER_MIN_DISTANCE_KM'),
default=0
)
# Parse comma-separated lists for callsign and country filtering
SPOT_ALLOW_CALLSIGNS = parse_list(os.getenv('SPOT_ALLOW_CALLSIGNS', ''))
SPOT_FILTERED_CALLSIGNS = parse_list(os.getenv('SPOT_FILTERED_CALLSIGNS', ''))
SPOT_ALLOW_COUNTRIES = parse_list(os.getenv('SPOT_ALLOW_COUNTRIES', ''))
SPOT_FILTERED_COUNTRIES = parse_list(os.getenv('SPOT_FILTERED_COUNTRIES', ''))
# ==============================================================================
# --- Deduplication ---
# ==============================================================================
DEDUP_WINDOW_SECONDS = str_to_int(
os.getenv('DEDUP_WINDOW_SECONDS'),
default=600 # 10 minutes
)
# ==============================================================================
# --- Home Assistant Integration ---
# ==============================================================================
HA_DISCOVERY_PREFIX = os.getenv('HA_DISCOVERY_PREFIX', 'homeassistant')
HA_ENTITY_BASE = os.getenv('HA_ENTITY_BASE', 'wspr')
# ==============================================================================
# --- Configuration Validation ---
# ==============================================================================
def validate_config():
"""
Validate required configuration values and provide helpful error messages.
Called at module import time to fail fast if misconfigured.
"""
errors = []
# Required fields
if MY_CALLSIGN in ['YOUR_CALLSIGN', '', None]:
errors.append(
"MY_CALLSIGN is required. Set the MY_CALLSIGN environment variable "
"to your amateur radio callsign."
)
if HA_MQTT_BROKER in ['YOUR_MQTT_BROKER_IP', '', None]:
errors.append(
"HA_MQTT_BROKER is required. Set the HA_MQTT_BROKER environment variable "
"to your Home Assistant MQTT broker IP address or hostname."
)
# Validate SCRIPT_DIRECTION
valid_directions = ['rx', 'tx', 'dual']
if SCRIPT_DIRECTION.lower() not in valid_directions:
errors.append(
f"SCRIPT_DIRECTION must be one of {valid_directions}. "
f"Got: '{SCRIPT_DIRECTION}'"
)
# Validate port number
if not (1 <= HA_MQTT_PORT <= 65535):
errors.append(
f"HA_MQTT_PORT must be between 1 and 65535. Got: {HA_MQTT_PORT}"
)
# Validate timing values
if STATS_INTERVAL_WINDOW_SECONDS < 60:
errors.append(
f"STATS_INTERVAL_WINDOW_SECONDS should be at least 60 seconds. "
f"Got: {STATS_INTERVAL_WINDOW_SECONDS}"
)
if STATS_UPDATE_INTERVAL_SECONDS < 30:
errors.append(
f"STATS_UPDATE_INTERVAL_SECONDS should be at least 30 seconds. "
f"Got: {STATS_UPDATE_INTERVAL_SECONDS}"
)
if POLL_INTERVAL_SECONDS < 60:
errors.append(
f"POLL_INTERVAL_SECONDS should be at least 60 seconds to respect rate limits. "
f"Got: {POLL_INTERVAL_SECONDS}"
)
# If there are errors, print them and exit
if errors:
print("=" * 80)
print("CONFIGURATION ERRORS")
print("=" * 80)
for i, error in enumerate(errors, 1):
print(f"\n{i}. {error}")
print("\n" + "=" * 80)
print("Please fix the configuration errors above and try again.")
print("=" * 80)
sys.exit(1)
# Run validation when module is imported
validate_config()
# ==============================================================================
# --- Configuration Summary ---
# ==============================================================================
def print_config_summary():
"""Print a summary of the loaded configuration for debugging."""
print("=" * 80)
print("CONFIGURATION SUMMARY")
print("=" * 80)
print(f"MY_CALLSIGN: {MY_CALLSIGN}")
print(f"DEBUG_MODE: {DEBUG_MODE}")
print(f"SCRIPT_DIRECTION: {SCRIPT_DIRECTION.upper()}")
print(f"WSPR_LIVE_URL: {WSPR_LIVE_URL}")
print(f"POLL_INTERVAL: {POLL_INTERVAL_SECONDS}s")
print(f"HA_MQTT_BROKER: {HA_MQTT_BROKER}:{HA_MQTT_PORT}")
print(f"HA_MQTT_AUTH: {'Yes' if HA_MQTT_USER else 'No'}")
print(f"ENABLE_SPOT_SENSORS: {ENABLE_SPOT_SENSORS}")
if ENABLE_SPOT_SENSORS:
print(f"SPOT_FILTER_MIN_DIST: {SPOT_FILTER_MIN_DISTANCE_KM} km")
print(f"SPOT_ALLOW_CALLSIGNS: {SPOT_ALLOW_CALLSIGNS if SPOT_ALLOW_CALLSIGNS else 'Any'}")
print(f"SPOT_FILTERED_CALLSIGNS: {SPOT_FILTERED_CALLSIGNS if SPOT_FILTERED_CALLSIGNS else 'None'}")
print(f"SPOT_ALLOW_COUNTRIES: {SPOT_ALLOW_COUNTRIES if SPOT_ALLOW_COUNTRIES else 'Any'}")
print(f"SPOT_FILTERED_COUNTRIES: {SPOT_FILTERED_COUNTRIES if SPOT_FILTERED_COUNTRIES else 'None'}")
print(f"DEDUP_WINDOW: {DEDUP_WINDOW_SECONDS}s ({DEDUP_WINDOW_SECONDS//60}min)")
print(f"STATS_WINDOW: {STATS_INTERVAL_WINDOW_SECONDS}s ({STATS_INTERVAL_WINDOW_SECONDS//60}min)")
print(f"STATS_UPDATE_INTERVAL: {STATS_UPDATE_INTERVAL_SECONDS}s ({STATS_UPDATE_INTERVAL_SECONDS//60}min)")
print("=" * 80)
# Optionally print config summary if DEBUG_MODE is enabled
if DEBUG_MODE:
print_config_summary()