Skip to content

Commit

Permalink
update dependencies and add new endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
vchrisb committed Mar 22, 2024
1 parent 0c41bfd commit b443611
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 49 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.11
uses: actions/setup-python@v4
- uses: actions/checkout@v4
- name: Set up Python 3.12
uses: actions/setup-python@v5
with:
python-version: "3.11"
python-version: "3.12"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand All @@ -26,7 +26,7 @@ jobs:
run: black ./ --check
- name: Docker meta
id: docker_meta
uses: docker/metadata-action@v3
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ github.repository }}
tags: |
Expand All @@ -40,13 +40,13 @@ jobs:
version: latest
install: true
- name: Login to GHCR
uses: docker/login-action@v2
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.CR_PAT }}
- name: Push to GitHub Packages
uses: docker/build-push-action@v4
uses: docker/build-push-action@v5
with:
tags: ${{ steps.docker_meta.outputs.tags }}
labels: ${{ steps.docker_meta.outputs.labels }}
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.12.0-alpine3.18
FROM python:3.12.2-alpine3.19
ADD ./api /app
WORKDIR /app
ADD requirements.txt /
Expand Down
52 changes: 36 additions & 16 deletions api/api.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import datetime
import json
import os
from typing import Any, TypeAlias

from e3dc import E3DC
from flask import Flask, request
from flask_httpauth import HTTPBasicAuth
from flask_restful import Api, Resource
from flask_httpauth import HTTPBasicAuth
from flask_restful import Api, Resource
from json_serialize import to_serializable
from webargs import fields, validate
from webargs.flaskparser import abort, parser, use_args
from webargs.flaskparser import abort, parser, use_args
from werkzeug.security import check_password_hash, generate_password_hash

JSON: TypeAlias = dict[str, "JSON"] | list["JSON"] | str | int | float | bool | None

app = Flask(__name__)
api = Api(app)
auth = HTTPBasicAuth()
Expand All @@ -22,14 +25,12 @@
PASSWORD = os.environ["E3DC_PASSWORD"]
KEY = os.environ["E3DC_KEY"]
ADMIN_PASSWORD = os.environ["ADMIN_PASSWORD"]
CONFIG = os.getenv("E3DC_CONFIG", "{}")
CONFIG = json.loads(os.getenv("E3DC_CONFIG", "{}"))
except KeyError:
raise Exception(
"Environmental Variables E3DC_IP_ADDRESS, E3DC_USERNAME, E3DC_PASSWORD E3DC_KEY and ADMIN_PASSWORD need to be present!"
)

CONFIG = json.loads(CONFIG)

e3dc = E3DC(
E3DC.CONNECT_LOCAL,
username=USERNAME,
Expand All @@ -42,15 +43,15 @@
users = {"admin": generate_password_hash(ADMIN_PASSWORD)}


@auth.verify_password
def verify_password(username, password):
if username in users:
return check_password_hash(users.get(username), password)
@auth.verify_password
def verify_password(username: str, password: str):
if users.get(username) is not None:
return check_password_hash(users.get(username), password)
return False


class Resource(Resource):
method_decorators = [auth.login_required]
method_decorators = [auth.login_required]


class poll(Resource):
Expand All @@ -68,6 +69,11 @@ def get(self):
return e3dc.get_system_status(keepAlive=True)


class batteries(Resource):
def get(self):
return e3dc.get_batteries(keepAlive=True)


class battery_data(Resource):
def get(self):
return e3dc.get_battery_data(keepAlive=True)
Expand All @@ -78,6 +84,11 @@ def get(self):
return e3dc.get_batteries_data(keepAlive=True)


class pvis(Resource):
def get(self):
return e3dc.get_pvis(keepAlive=True)


class pvi_data(Resource):
def get(self):
return e3dc.get_pvi_data(keepAlive=True)
Expand All @@ -88,6 +99,11 @@ def get(self):
return e3dc.get_pvis_data(keepAlive=True)


class powermeters(Resource):
def get(self):
return e3dc.get_powermeters(keepAlive=True)


class powermeter_data(Resource):
def get(self):
return e3dc.get_powermeter_data(keepAlive=True)
Expand All @@ -105,8 +121,8 @@ def get(self):
def post(self):
if not request.is_json:
return {"message": "not an application/json content type"}, 400
content = request.json
if content.keys() & [
content: JSON = request.json
if isinstance(content, dict) and content.keys() & [
"powerLimitsUsed",
"powerSaveEnabled",
"weatherRegulatedChargeEnabled",
Expand Down Expand Up @@ -176,7 +192,7 @@ def post(self):
if not request.is_json:
return {"message": "not an application/json content type"}, 400

content = request.json
content: JSON = request.json

try:
if e3dc.set_idle_periods(content, keepAlive=True):
Expand All @@ -196,11 +212,12 @@ class db_data(Resource):
}

@use_args(dateadd_args, location="query")
def get(self, args):
def get(self, args: dict[str, Any]):
if "startDate" not in args:
startDate = datetime.date.today()
else:
startDate = args["startDate"]

return e3dc.get_db_data(
startDate=startDate, timespan=args["timespan"], keepAlive=True
)
Expand All @@ -218,15 +235,18 @@ def handle_request_parsing_error(err, req, schema, *, error_status_code, error_h
api.add_resource(poll, "/api/poll")
api.add_resource(system_info, "/api/system_info")
api.add_resource(system_status, "/api/system_status")
api.add_resource(batteries, "/api/batteries")
api.add_resource(battery_data, "/api/battery_data")
api.add_resource(batteries_data, "/api/batteries_data")
api.add_resource(pvis, "/api/pvis")
api.add_resource(pvi_data, "/api/pvi_data")
api.add_resource(pvis_data, "/api/pvis_data")
api.add_resource(powermeters, "/api/powermeters")
api.add_resource(powermeter_data, "/api/powermeter_data")
api.add_resource(powermeters_data, "/api/powermeters_data")
api.add_resource(power_settings, "/api/power_settings")
api.add_resource(idle_periods, "/api/idle_periods")
api.add_resource(db_data, "/api/db_data")

if __name__ == "__main__":
app.run(debug=True)
app.run(debug=False)
3 changes: 2 additions & 1 deletion api/json_serialize.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from datetime import datetime
from functools import singledispatch
from typing import Any


@singledispatch
def to_serializable(val):
def to_serializable(val: Any):
"""Used by default."""
return str(val)

Expand Down
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ exclude = '''
'''

[tool.pylint.messages_control]
disable = "C0330, C0326"
disable = "C0330, C0326"

[tool.pyright]
ignore = ["build/*", "dist/*", ".venv/*"]
typeCheckingMode = "strict"
36 changes: 13 additions & 23 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@
#
aniso8601==9.0.1
# via flask-restful
blinker==1.6.3
blinker==1.7.0
# via flask
certifi==2023.7.22
# via requests
charset-normalizer==3.3.0
# via requests
click==8.1.7
# via flask
flask==3.0.0
flask==3.0.2
# via
# flask-httpauth
# flask-restful
Expand All @@ -24,44 +20,38 @@ flask-restful==0.3.10
# via -r requirements.in
gunicorn==21.2.0
# via -r requirements.in
idna==3.4
# via requests
itsdangerous==2.1.2
# via flask
jinja2==3.1.2
jinja2==3.1.3
# via flask
markupsafe==2.1.3
markupsafe==2.1.5
# via
# jinja2
# werkzeug
marshmallow==3.20.1
marshmallow==3.21.1
# via webargs
packaging==23.2
packaging==24.0
# via
# gunicorn
# marshmallow
# webargs
py3rijndael==0.3.3
# via pye3dc
pye3dc==0.8.0
pye3dc==0.9.1
# via -r requirements.in
python-dateutil==2.8.2
python-dateutil==2.9.0.post0
# via pye3dc
pytz==2023.3.post1
pytz==2024.1
# via flask-restful
requests==2.31.0
# via pye3dc
six==1.16.0
# via
# flask-restful
# python-dateutil
tzlocal==5.1
tzlocal==5.2
# via pye3dc
urllib3==2.0.6
# via requests
webargs==8.3.0
webargs==8.4.0
# via -r requirements.in
websocket-client==1.6.4
websocket-client==1.7.0
# via pye3dc
werkzeug==3.0.0
werkzeug==3.0.1
# via flask

0 comments on commit b443611

Please sign in to comment.