Skip to content

Commit

Permalink
Merge pull request #129 from DuanXDong/master
Browse files Browse the repository at this point in the history
解决add-on读取环境变量问题
  • Loading branch information
ARC-MX authored Jan 5, 2025
2 parents 3eb32da + bea6c33 commit 4e3202c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 44 deletions.
6 changes: 2 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
ARG BUILD_FROM
FROM $BUILD_FROM

COPY scripts/* /app/
WORKDIR /app

COPY run.sh /app/
RUN chmod a+x /app/run.sh

CMD [ "/bin/bash", "./run.sh" ]
CMD ["python3","main.py"]
54 changes: 27 additions & 27 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,31 @@ init: false
map:
- config:rw
options:
phone: ""
password: ""
ignore_user_id: "xxxx,xxxx"
enable_database_storage: false
db_name: "homeassistant.db"
hass_url: "http://homeassistant.local:8123/"
hass_token: ""
job_start_time: "07:00"
retry_wait_time_offset_unit: 15
data_retention_days: 7
recharge_notify: false
balance: 5.0
pushplus_token: "xxxx,xxxx"
PHONE_NUMBER: ""
PASSWORD: ""
IGNORE_USER_ID: "xxxx,xxxx"
ENABLE_DATABASE_STORAGE: false
DB_NAME: "homeassistant.db"
HASS_URL: "http://homeassistant.local:8123/"
HASS_TOKEN: ""
JOB_START_TIME: "07:00"
RETRY_WAIT_TIME_OFFSET_UNIT: 15
DATA_RETENTION_DAYS: 7
RECHARGE_NOTIFY: false
BALANCE: 5.0
PUSHPLUS_TOKEN: "xxxx,xxxx"
schema:
phone: str
password: password
ignore_user_id: str
enable_database_storage: bool
db_name: str
hass_url: url
hass_token: str
job_start_time: str
retry_wait_time_offset_unit: int(2,30)
log_level: str
data_retention_days: int
recharge_notify: bool
balance: float
pushplus_token: str
PHONE_NUMBER: str
PASSWORD: password
IGNORE_USER_ID: str
ENABLE_DATABASE_STORAGE: bool
DB_NAME: str
HASS_URL: url
HASS_TOKEN: str
JOB_START_TIME: str
RETRY_WAIT_TIME_OFFSET_UNIT: int(2,30)
LOG_LEVEL: str
DATA_RETENTION_DAYS: int
RECHARGE_NOTIFY: bool
BALANCE: float
PUSHPLUS_TOKEN: str
53 changes: 40 additions & 13 deletions scripts/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,56 @@
import sys
import time
import schedule
import json
from datetime import datetime,timedelta
from const import *
from data_fetcher import DataFetcher


def main():
global RETRY_TIMES_LIMIT
if 'PYTHON_IN_DOCKER' not in os.environ:
# 读取 .env 文件
import dotenv
dotenv.load_dotenv(verbose=True)
global RETRY_TIMES_LIMIT
try:
PHONE_NUMBER = os.getenv("PHONE_NUMBER")
PASSWORD = os.getenv("PASSWORD")
HASS_URL = os.getenv("HASS_URL")
JOB_START_TIME = os.getenv("JOB_START_TIME","07:00" )
LOG_LEVEL = os.getenv("LOG_LEVEL","INFO")
VERSION = os.getenv("VERSION")
RETRY_TIMES_LIMIT = int(os.getenv("RETRY_TIMES_LIMIT", 5))
except Exception as e:
logging.error(f"Failing to read the .env file, the program will exit with an error message: {e}.")
sys.exit()

try:
PHONE_NUMBER = os.getenv("PHONE_NUMBER")
PASSWORD = os.getenv("PASSWORD")
HASS_URL = os.getenv("HASS_URL")
JOB_START_TIME = os.getenv("JOB_START_TIME","07:00" )
LOG_LEVEL = os.getenv("LOG_LEVEL","INFO")
VERSION = os.getenv("VERSION")
RETRY_TIMES_LIMIT = int(os.getenv("RETRY_TIMES_LIMIT", 5))
except Exception as e:
logging.error(f"Failing to read the .env file, the program will exit with an error message: {e}.")
sys.exit()
else:
with open('/data/options.json') as f:
options = json.load(f)
try:
PHONE_NUMBER = options.get("PHONE_NUMBER")
PASSWORD = options.get("PASSWORD")
HASS_URL = options.get("HASS_URL")
JOB_START_TIME = options.get("JOB_START_TIME", "07:00")
LOG_LEVEL = options.get("LOG_LEVEL", "INFO")
VERSION = os.getenv("VERSION")
RETRY_TIMES_LIMIT = int(options.get("RETRY_TIMES_LIMIT", 5))
os.environ["HASS_URL"] = options.get("HASS_URL", "http://homeassistant.local:8123/")
os.environ["HASS_TOKEN"] = options.get("HASS_TOKEN", "")
os.environ["ENABLE_DATABASE_STORAGE"] = str(options.get("ENABLE_DATABASE_STORAGE", "false")).lower()
os.environ["IGNORE_USER_ID"] = options.get("IGNORE_USER_ID", "xxxxx,xxxxx")
os.environ["DB_NAME"] = options.get("DB_NAME", "homeassistant.db")
os.environ["RETRY_TIMES_LIMIT"] = str(options.get("RETRY_TIMES_LIMIT", 5))
os.environ["DRIVER_IMPLICITY_WAIT_TIME"] = str(options.get("DRIVER_IMPLICITY_WAIT_TIME", 60))
os.environ["LOGIN_EXPECTED_TIME"] = str(options.get("LOGIN_EXPECTED_TIME", 10))
os.environ["RETRY_WAIT_TIME_OFFSET_UNIT"] = str(options.get("RETRY_WAIT_TIME_OFFSET_UNIT", 10))
os.environ["DATA_RETENTION_DAYS"] = str(options.get("DATA_RETENTION_DAYS", 7))
os.environ["RECHARGE_NOTIFY"] = str(options.get("RECHARGE_NOTIFY", "false")).lower()
os.environ["BALANCE"] = str(options.get("BALANCE", 5.0))
os.environ["PUSHPLUS_TOKEN"] = options.get("PUSHPLUS_TOKEN", "")
except Exception as e:
logging.error(f"Failing to read the options.json file, the program will exit with an error message: {e}.")
sys.exit()
logger_init(LOG_LEVEL)
logging.info(f"The current repository version is {VERSION}, and the repository address is https://github.com/ARC-MX/sgcc_electricity_new.git")
current_datetime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
Expand Down

0 comments on commit 4e3202c

Please sign in to comment.