Skip to content

Commit

Permalink
Merge pull request #21 from m4dm4rtig4n/0.5.2
Browse files Browse the repository at this point in the history
0.5.2
  • Loading branch information
m4dm4rtig4n authored Oct 22, 2021
2 parents 1bb5681 + 7ed030d commit 723e109
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 21 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ CARD_MYENEDIS="False"
CURRENT_PLAN="BASE"
INFLUXDB_ENABLE="False"
INFLUXDB_HOST=""
INFLUXDB_PORT=""
INFLUXDB_TOKEN=""
INFLUXDB_ORG=""
INFLUXDB_BUCKET=""
Expand Down Expand Up @@ -202,6 +203,7 @@ docker run -it --restart=unless-stopped \
-e CURRENT_PLAN="$CURRENT_PLAN" \
-e INFLUXDB_ENABLE="$INFLUXDB_ENABLE" \
-e INFLUXDB_HOST="$INFLUXDB_HOST" \
-e INFLUXDB_PORT="$INFLUXDB_PORT" \
-e INFLUXDB_TOKEN="$INFLUXDB_TOKEN" \
-e INFLUXDB_ORG="$INFLUXDB_ORG" \
-e INFLUXDB_BUCKET="$INFLUXDB_BUCKET" \
Expand Down Expand Up @@ -244,11 +246,12 @@ services:
DEBUG: "False"
CARD_MYENEDIS: "False"
CURRENT_PLAN: "BASE"
INFLUXDB_ENABLE: "False"
INFLUXDB_HOST: ""
INFLUXDB_TOKEN: ""
INFLUXDB_ORG: ""
INFLUXDB_BUCKET: ""
INFLUXDB_ENABLE: "False"
INFLUXDB_HOST: ""
INFLUXDB_PORT: ""
INFLUXDB_TOKEN: ""
INFLUXDB_ORG: ""
INFLUXDB_BUCKET: ""
logging:
options:
max-size: "10m"
Expand Down
10 changes: 4 additions & 6 deletions app/daily.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
from datetime import datetime, timedelta
from dateutil.relativedelta import *
from pprint import pprint
import influxdb_client
from influxdb_client.client.write_api import SYNCHRONOUS

from importlib import import_module
main = import_module("main")
f = import_module("function")


def getDaily(cur, con, client, influxdb_api=None, mode="consumption", last_activation_date=datetime.now()):
def getDaily(cur, con, client, mode="consumption", last_activation_date=datetime.now()):
max_days = 1095
max_days_date = datetime.now() + relativedelta(days=-max_days)
pdl = main.pdl
Expand All @@ -31,7 +29,7 @@ def getDaily(cur, con, client, influxdb_api=None, mode="consumption", last_activ
dateEnded = datetime.now()
dateEnded = dateEnded.strftime('%Y-%m-%d')

data = dailyBeetwen(cur, con, influxdb_api, pdl, mode, dateBegin, dateEnded, last_activation_date)
data = dailyBeetwen(cur, con, pdl, mode, dateBegin, dateEnded, last_activation_date)
if "error_code" in data:
f.publish(client, f"{pdl}/{mode}/current_year/error", str(1))
for key, value in data.items():
Expand Down Expand Up @@ -92,7 +90,7 @@ def getDaily(cur, con, client, influxdb_api=None, mode="consumption", last_activ
if last_activation_date > datetime.strptime(dateEnded, '%Y-%m-%d'):
f.log(" - Skip (activation date > dateEnded)")
else:
data = dailyBeetwen(cur, con, influxdb_api, pdl, mode, dateBegin, dateEnded, last_activation_date)
data = dailyBeetwen(cur, con, pdl, mode, dateBegin, dateEnded, last_activation_date)
if "error_code" in data:
f.publish(client, f"{pdl}/{mode}/year-{current_year}/error", str(1))
for key, value in data.items():
Expand Down Expand Up @@ -167,7 +165,7 @@ def getDaily(cur, con, client, influxdb_api=None, mode="consumption", last_activ
return ha_discovery


def dailyBeetwen(cur, con, influxdb_api, pdl, mode, dateBegin, dateEnded, last_activation_date):
def dailyBeetwen(cur, con, pdl, mode, dateBegin, dateEnded, last_activation_date):
response = {}

lastYears = datetime.strptime(dateEnded, '%Y-%m-%d')
Expand Down
4 changes: 1 addition & 3 deletions app/detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
from dateutil.relativedelta import *
from pprint import pprint
import re
import influxdb_client
from influxdb_client.client.write_api import SYNCHRONOUS

from importlib import import_module

Expand All @@ -15,7 +13,7 @@
date_format = "%Y-%m-%d %H:%M:%S"


def getDetail(cur, con, client, influxdb_api=None, mode="consumption", last_activation_date=datetime.now(), offpeak_hours=None,
def getDetail(cur, con, client, mode="consumption", last_activation_date=datetime.now(), offpeak_hours=None,
measure_total=None):

max_days = 730
Expand Down
3 changes: 2 additions & 1 deletion app/influxdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def forceRound(x, n):
value = result[2]
value_kw = value / 1000
current_price = forceRound(value_kw * price["BASE"], 4)
f.log(f"Insert daily {date} => {value}", "DEBUG")
dateObject = datetime.strptime(date, '%Y-%m-%d') - relativedelta(hours=2)
p = influxdb_client.Point("enedisgateway_daily") \
.tag("pdl", pdl) \
Expand All @@ -61,8 +62,8 @@ def forceRound(x, n):
value_wh = value * interval_length / 60
value_kwh = value_wh / 1000
current_price = forceRound(value_kwh * price[measure_type], 4)
f.log(f"Insert detail {date} => {value}", "DEBUG")
dateObject = datetime.strptime(date, '%Y-%m-%d %H:%M:%S') - relativedelta(hours=2)
# pprint(current_price)
p = influxdb_client.Point("enedisgateway_detail") \
.tag("pdl", pdl) \
.tag("measure_type", measure_type) \
Expand Down
24 changes: 18 additions & 6 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@
else:
influxdb_host = ""
########################################################################################################################
# INFLUXDB_PORT
if "INFLUXDB_PORT" in os.environ:
influxdb_port = str(os.environ['INFLUXDB_PORT'])
else:
influxdb_port = ""
########################################################################################################################
# INFLUXDB_TOKEN
if "INFLUXDB_TOKEN" in os.environ:
influxdb_token = str(os.environ['INFLUXDB_TOKEN'])
Expand Down Expand Up @@ -407,7 +413,7 @@ def run():
if get_consumption == True:
f.logLine()
f.log("Get Consumption :")
ha_discovery_consumption = day.getDaily(cur, con, client, influxdb_api, "consumption", last_activation_date)
ha_discovery_consumption = day.getDaily(cur, con, client, "consumption", last_activation_date)
# pprint(ha_discovery_consumption)
if ha_autodiscovery == True:
f.logLine()
Expand Down Expand Up @@ -439,7 +445,7 @@ def run():

if get_consumption_detail == True:
f.log("Get Consumption Detail:")
ha_discovery_consumption = detail.getDetail(cur, con, client, influxdb_api, "consumption", last_activation_date, offpeak_hours)
ha_discovery_consumption = detail.getDetail(cur, con, client, "consumption", last_activation_date, offpeak_hours)
if ha_autodiscovery == True:
f.logLine()
f.log("Home Assistant auto-discovery (Consumption Detail) :")
Expand Down Expand Up @@ -471,7 +477,7 @@ def run():
if get_production == True:
f.logLine()
f.log("Get production :")
ha_discovery_production = day.getDaily(cur, con, client, influxdb_api, "production", last_activation_date)
ha_discovery_production = day.getDaily(cur, con, client, "production", last_activation_date)
if ha_autodiscovery == True:
f.logLine()
f.log("Home Assistant auto-discovery (Production) :")
Expand Down Expand Up @@ -501,7 +507,7 @@ def run():
if get_production_detail == True:
f.logLine()
f.log("Get production Detail:")
ha_discovery_consumption = detail.getDetail(cur, con, client, influxdb_api, "production", last_activation_date, offpeak_hours)
ha_discovery_consumption = detail.getDetail(cur, con, client, "production", last_activation_date, offpeak_hours)
if ha_autodiscovery == True:
f.logLine()
f.log("Home Assistant auto-discovery (Production Detail) :")
Expand Down Expand Up @@ -604,16 +610,22 @@ def run():
client = f.connect_mqtt()
client.loop_start()
except:
f.log("MQTT : Connection failed")
f.log("MQTT : Connection failed", "CRITICIAL")


# INFLUXDB
if influxdb_enable == True:
influxdb = influxdb_client.InfluxDBClient(
url=influxdb_host,
url=f"http://{influxdb_host}:{influxdb_port}",
token=influxdb_token,
org=influxdb_org
)
health = influxdb.health()
if health.status == "pass":
f.log("InfluxDB : Connection success")
else:
f.log("InfluxDB : Connection failed", "CRITICAL")

influxdb_api = influxdb.write_api(write_options=ASYNCHRONOUS)

run()

0 comments on commit 723e109

Please sign in to comment.