-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
90 lines (71 loc) · 2.99 KB
/
config.py
File metadata and controls
90 lines (71 loc) · 2.99 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
import os
from dataclasses import dataclass
from dotenv import load_dotenv
load_dotenv()
@dataclass
class Config:
# EcoFlow developer API credentials
# Obtain from https://developer.ecoflow.com -> Access Management
ecoflow_access_key: str
ecoflow_secret_key: str
# Your device serial number (found on the device label or EcoFlow app)
device_sn: str
# Discord bot token (from https://discord.com/developers/applications)
discord_token: str
# Channel ID to send notifications to (right-click channel -> Copy ID)
discord_channel_id: int
# Optional: also DM this user ID when an event occurs (0 = disabled)
discord_dm_user_id: int
# EcoFlow API host — use "https://api-e.ecoflow.com" for Europe
api_host: str
# Watts threshold to consider AC charging as "started" (avoids noise near 0)
charging_watts_threshold: float
# AC output configuration sent with toggle commands
# Most of the world uses 220–240 V / 50 Hz; North America uses 120 V / 60 Hz (out_freq 2)
ac_out_voltage: int
ac_out_freq: int # 1 = 50 Hz, 2 = 60 Hz
ac_xboost: bool # X-Boost allows running appliances above rated watts
@classmethod
def from_env(cls) -> "Config":
missing = []
def require(key: str) -> str:
val = os.getenv(key, "").strip()
if not val:
missing.append(key)
return val
access_key = require("ECOFLOW_ACCESS_KEY")
secret_key = require("ECOFLOW_SECRET_KEY")
device_sn = require("DEVICE_SN")
discord_token = require("DISCORD_TOKEN")
discord_channel_id_str = require("DISCORD_CHANNEL_ID")
if missing:
raise EnvironmentError(
f"Missing required environment variables: {', '.join(missing)}"
)
try:
channel_id = int(discord_channel_id_str)
except ValueError:
raise EnvironmentError("DISCORD_CHANNEL_ID must be an integer")
dm_user_id_str = os.getenv("DISCORD_DM_USER_ID", "0").strip()
try:
dm_user_id = int(dm_user_id_str)
except ValueError:
dm_user_id = 0
api_host = os.getenv("ECOFLOW_API_HOST", "https://api.ecoflow.com").strip().rstrip("/")
charging_threshold = float(os.getenv("CHARGING_WATTS_THRESHOLD", "10"))
ac_out_voltage = int(os.getenv("AC_OUT_VOLTAGE", "230"))
ac_out_freq = int(os.getenv("AC_OUT_FREQ", "1"))
ac_xboost = os.getenv("AC_XBOOST", "true").strip().lower() not in {"0", "false", "no"}
return cls(
ecoflow_access_key=access_key,
ecoflow_secret_key=secret_key,
device_sn=device_sn,
discord_token=discord_token,
discord_channel_id=channel_id,
discord_dm_user_id=dm_user_id,
api_host=api_host,
charging_watts_threshold=charging_threshold,
ac_out_voltage=ac_out_voltage,
ac_out_freq=ac_out_freq,
ac_xboost=ac_xboost,
)