forked from thelabexpedition67/MustInverterDataHandler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
210 lines (163 loc) · 5.45 KB
/
Copy pathconfig.py
File metadata and controls
210 lines (163 loc) · 5.45 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
# config.py
# Standard Libraries
import logging
import os
from datetime import datetime, time
from enum import IntEnum
from typing import Final
# Third-Party Libraries
from dotenv import load_dotenv
load_dotenv()
logger = logging.getLogger(__name__)
class EnergyMode(IntEnum):
"""
Inverter energy mode codes.
Replace the codes below with the actual codes used by the inverter.
"""
# 1 = SBU, 2 = SUB, 3 = UTI, 4 = SOL
SBU = 1
SUB = 2
UTI = 3
SOL = 4
def get_env_bool(variable_name: str, default: bool = False) -> bool:
"""
Read and convert a boolean environment variable.
"""
value = os.getenv(variable_name)
if value is None:
return default
normalized_value = value.strip().lower()
if normalized_value in {"true", "1", "yes", "on"}:
return True
if normalized_value in {"false", "0", "no", "off"}:
return False
raise ValueError(
f"Environment variable {variable_name} must contain true or false"
)
def get_env_int(
variable_name: str,
default: int,
min_value: int | None = None,
max_value: int | None = None,
) -> int:
"""
Read an integer environment variable.
Returns the default value if the variable is missing
or contains an invalid value.
Optionally limits the value to a minimum and maximum.
"""
raw_value = os.getenv(variable_name)
if raw_value is None:
return default
try:
value = int(raw_value.strip())
except (TypeError, ValueError):
logger.warning(
"Invalid value for %s: %r. Using default value: %s.",
variable_name,
raw_value,
default,
)
return default
if min_value is not None and value < min_value:
logger.warning(
"%s is below the minimum value of %s. Using %s.",
variable_name,
min_value,
min_value,
)
return min_value
if max_value is not None and value > max_value:
logger.warning(
"%s is above the maximum value of %s. Using %s.",
variable_name,
max_value,
max_value,
)
return max_value
return value
def get_env_time(variable_name: str, default: str) -> time:
"""
Read an environment variable in HH:MM format and convert it to time.
"""
value = os.getenv(variable_name, default)
try:
return datetime.strptime(value, "%H:%M").time()
except ValueError as error:
raise ValueError(
f"Environment variable {variable_name} "
f"must use the HH:MM format"
) from error
def get_env_energy_mode(
variable_name: str,
default: EnergyMode,
) -> EnergyMode:
"""
Read an energy mode name and convert it to EnergyMode.
"""
value = os.getenv(variable_name, default.name).strip().upper()
try:
return EnergyMode[value]
except KeyError as error:
allowed_modes = ", ".join(mode.name for mode in EnergyMode)
raise ValueError(
f"Environment variable {variable_name} contains "
f"unsupported mode {value}. Allowed modes: {allowed_modes}"
) from error
MUST_PORT: Final[str] = os.getenv("MUST_PORT", "COM3")
DATA_GATHER_INTERVAL_SECONDS: Final[int] = get_env_int(
variable_name="DATA_GATHER_INTERVAL_SECONDS",
default=60,
min_value=10,
max_value=3600,
)
ENABLE_AUTO_SWITCH: Final[bool] = get_env_bool(
variable_name="ENABLE_AUTO_SWITCH",
default=False,
)
AUTO_SWITCH_TARGET_TIME: Final[time] = get_env_time(
variable_name="AUTO_SWITCH_TARGET_TIME",
default="20:00",
)
AUTO_SWITCH_TARGET_MODE: Final[EnergyMode] = get_env_energy_mode(
variable_name="AUTO_SWITCH_TARGET_MODE",
default=EnergyMode.SUB,
)
# Grid outage automatic mode switch
ENABLE_GRID_OUTAGE_AUTO_SWITCH: Final[bool] = (
get_env_bool(
variable_name="ENABLE_GRID_OUTAGE_AUTO_SWITCH",
default=False,
)
)
GRID_OUTAGE_TARGET_MODE: Final[EnergyMode] = (
get_env_energy_mode(
variable_name="GRID_OUTAGE_TARGET_MODE",
default=EnergyMode.SUB,
)
)
if __name__ == '__main__':
print("debugging config.py")
# if (
# ENABLE_AUTO_SWITCH
# and is_time_reached(AUTO_SWITCH_TARGET_TIME)
# and current_energy_mode != AUTO_SWITCH_TARGET_MODE.value
# ):
# switch_energy_mode(AUTO_SWITCH_TARGET_MODE.value)
print("Enable auto switch:", ENABLE_AUTO_SWITCH)
print("Object Type:", type(ENABLE_AUTO_SWITCH))
auto_switch_target_mode_name = AUTO_SWITCH_TARGET_MODE.name
print("Auto switch target mode name:", auto_switch_target_mode_name)
print("Object Type:", type(auto_switch_target_mode_name))
auto_switch_target_mode_code = AUTO_SWITCH_TARGET_MODE.value
print("Auto switch target mode code:", auto_switch_target_mode_code)
print("Object Type:", type(auto_switch_target_mode_code))
grid_outage_auto_switch = ENABLE_GRID_OUTAGE_AUTO_SWITCH
print("Grid outage auto switch:", grid_outage_auto_switch)
print("Object Type:", type(grid_outage_auto_switch))
grid_outage_target_mode_name = GRID_OUTAGE_TARGET_MODE.name
grid_outage_target_mode_code = GRID_OUTAGE_TARGET_MODE.value
print("Grid outage target mode:", grid_outage_target_mode_name)
print("Object Type:", type(grid_outage_target_mode_name))
print("Grid outage target mode code:", grid_outage_target_mode_code)
print("Object Type:", type(grid_outage_target_mode_code))