Skip to content

Commit

Permalink
Merge pull request #69 from m4dm4rtig4n/0.7.7
Browse files Browse the repository at this point in the history
0.7.7
  • Loading branch information
m4dm4rtig4n committed Nov 22, 2021
2 parents b6695d3 + 3778ad6 commit 18ccfb1
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 65 deletions.
28 changes: 24 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
COMPOSE=docker-compose -f docker-compose.dev.yml

## Start docker conatiners for dev
up: .env
up:
@echo "Start docker container for dev"
$(COMPOSE) up -d
@echo ""
@echo "\033[0;33mMQTT Explorer:\033[0m \033[0;32mhttp://127.0.0.1:4000\033[0m Auth info: (host: mosquitto)"
@echo "\033[0;33mInflux DB: \033[0m \033[0;32mhttp://127.0.0.1:8086\033[0m Auth info: (user: enedisgateway2mqtt, pawword: enedisgateway2mqtt)"

## Stop docker conatiners for dev
down: .env
down:
@echo "Start docker conatiner for dev"
$(COMPOSE) down

Expand All @@ -21,6 +21,27 @@ start:
bash:
$(COMPOSE) exec enedisgateway2mqtt bash

## Create git branch
version=
git_branch:
git branch $(version) || true
git checkout $(version) || true
echo -n $(version) > app/VERSION

## Create add/commit/push
current_version := $(shell cat app/VERSION)
comment=
.PHONY: git_push
git_push:
set -x
@(echo "git add --all")
git add --all
@if [ "$(comment)" = "" ]; then comment="maj"; fi; \
echo "git commit -m '$${comment}'"
git commit -m "$${comment}"
@(echo "git push origin $(current_version)")
git push origin $(current_version)

.DEFAULT_GOAL := help
.PHONY: help
help:
Expand All @@ -38,5 +59,4 @@ help:
} \
{lastLine = $$0;}' $(MAKEFILE_LIST)\
| sed -e "s/\`/\\\\\`/g"
@echo ""

@echo ""
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

![Supports aarch64 Architecture][aarch64-shield] ![Supports amd64 Architecture][amd64-shield] ![Supports armhf Architecture][armhf-shield] ![Supports armv7 Architecture][armv7-shield]

**Best french discord community about "Domotique & Diy" ?**
**Best french discord community about "Domotique & Diy" :**

=> https://discord.gg/DfVJZme
[![https://discord.gg/DfVJZme](discord.png 'Best french discord community about "Domotique & Diy"')](https://discord.gg/DfVJZme)

****

## IMPORTANT !
**The tool is still under development.
Expand Down Expand Up @@ -271,9 +273,23 @@ make start

- Add **DJU18**
- Add Postgres/MariaDB Connector
- Add max power

## Change log:

### [0.7.7] - 2021-11-22

*UPGRADE Procedure :*
- 0.7.6 -> 0.7.7 : Wipe cache (rm enedisgateway.db)

*Change Log :*
- Fix [Null values don’t mean no values](https://github.com/m4dm4rtig4n/enedisgateway2mqtt/issues/45)
- Fix [Timezone bug](https://github.com/m4dm4rtig4n/enedisgateway2mqtt/issues/48)
- Fix [HP/HC no cost](https://github.com/m4dm4rtig4n/enedisgateway2mqtt/issues/67)
- Fix [HP/HC wrong price](https://github.com/m4dm4rtig4n/enedisgateway2mqtt/issues/54)
- Switch Enedis API Call in UTC Datetime (reduce error)
- Fix offpeak hours in HA Sensor

### [0.7.4] - 2021-11-18

- Fix SQLite closed connection
Expand Down
2 changes: 1 addition & 1 deletion app/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.7.5-dev
0.7.7
6 changes: 3 additions & 3 deletions app/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ def queryApi(url, headers, data, count=0):
offpeak_hours_store += ";"
i += 1

config_query = f"INSERT OR REPLACE INTO config VALUES (?, ?)"
cur.execute(config_query, [f"{pdl}_offpeak_hours", f"HC ({str(offpeak_hours_store)})"])
con.commit()
# config_query = f"INSERT OR REPLACE INTO config VALUES (?, ?)"
# cur.execute(config_query, [f"{pdl}_offpeak_hours", f"HC ({str(offpeak_hours_store)})"])
# con.commit()

else:
ha_discovery = {
Expand Down
17 changes: 11 additions & 6 deletions app/daily.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@
from datetime import datetime, timedelta
from dateutil.relativedelta import *
from pprint import pprint
import locale
import pytz

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

# locale.setlocale(locale.LC_ALL, 'fr_FR.UTF-8')
# timezone = pytz.timezone('Europe/Paris')

def getDaily(headers, cur, con, client, pdl, pdl_config, mode="consumption", last_activation_date=datetime.now()):

def getDaily(headers, cur, con, client, pdl, pdl_config, mode="consumption", last_activation_date=datetime.utcnow()):
max_days = 1095
max_days_date = datetime.now() + relativedelta(days=-max_days)
max_days_date = datetime.utcnow() + relativedelta(days=-max_days)
base_price = pdl_config['consumption_price_base']

url = main.url
Expand All @@ -24,10 +29,10 @@ def getDaily(headers, cur, con, client, pdl, pdl_config, mode="consumption", las
last_activation_date = last_activation_date.split("+")[0]
last_activation_date = datetime.strptime(last_activation_date, '%Y-%m-%d')

lastYears = datetime.now() + relativedelta(years=-1)
lastYears = datetime.utcnow() - relativedelta(years=1)
dateBegin = lastYears.strftime('%Y-%m-%d')
# dateEnded = datetime.now() + relativedelta(days=-1)
dateEnded = datetime.now()
# dateEnded = datetime.now() - relativedelta(days=1)
dateEnded = datetime.utcnow()
dateEnded = dateEnded.strftime('%Y-%m-%d')

lastData = {}
Expand Down Expand Up @@ -60,7 +65,7 @@ def getDaily(headers, cur, con, client, pdl, pdl_config, mode="consumption", las
ha_discovery[pdl][f"{mode}_{key.replace('-', '_')}"]['attributes']["day"] = day.capitalize()
if not "date" in ha_discovery[pdl][f"{mode}_{key.replace('-', '_')}"]['attributes'].keys():
ha_discovery[pdl][f"{mode}_{key.replace('-', '_')}"]['attributes']["date"] = value["date"]
today = datetime.now()
today = datetime.utcnow()
today = today.strftime('%A')
if day == today:
today = True
Expand Down
27 changes: 13 additions & 14 deletions app/detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
date_format = "%Y-%m-%d %H:%M:%S"


def getDetail(headers, cur, con, client, pdl, pdl_config, mode="consumption", last_activation_date=datetime.now(), offpeak_hours=None,
measure_total=None):
def getDetail(headers, cur, con, client, pdl, pdl_config, mode="consumption", last_activation_date=datetime.utcnow(), measure_total=None, offpeak_hours=[]):

max_days = 730
max_days_per_demand = 7
max_days_date = datetime.now() + relativedelta(days=-max_days)
max_days_date = datetime.utcnow() + relativedelta(days=-max_days)
price_base = pdl_config['consumption_price_base']
price_hc = pdl_config['consumption_price_hc']
price_hp = pdl_config['consumption_price_hp']
Expand All @@ -33,13 +32,12 @@ def getDetail(headers, cur, con, client, pdl, pdl_config, mode="consumption", la
last_activation_date = last_activation_date.split("+")[0]
last_activation_date = datetime.strptime(last_activation_date, '%Y-%m-%d')

lastYears = datetime.now() + relativedelta(days=-max_days_per_demand)
lastYears = datetime.utcnow() + relativedelta(days=-max_days_per_demand)
dateBegin = lastYears.strftime('%Y-%m-%d')
dateEnded = datetime.now()
dateEnded = datetime.utcnow()
dateEnded = dateEnded.strftime('%Y-%m-%d')

data = detailBeetwen(headers, cur, con, url, pdl, pdl_config, mode, dateBegin, dateEnded, last_activation_date, max_days_per_demand,
offpeak_hours)
data = detailBeetwen(headers, cur, con, url, pdl, pdl_config, mode, dateBegin, dateEnded, last_activation_date, max_days_per_demand, offpeak_hours)
if "error_code" in data:
f.publish(client, f"{pdl}/{mode}/detail/error", str(1))
for key, value in data.items():
Expand Down Expand Up @@ -128,7 +126,7 @@ def getDetail(headers, cur, con, client, pdl, pdl_config, mode="consumption", la
if price_base != 0:
result[year][month]["measure_base_euro"] = result[year][month]["measure_total_wh"] / 1000 * price_base

if offpeak_hours != None:
if offpeak_hours != []:
if price_hc != 0 and price_hp != 0:
result[year][month]["measure_hp_euro"] = result[year][month]["measure_hp_wh"] / 1000 * price_hp
result[year][month]["measure_hc_euro"] = result[year][month]["measure_hc_wh"] / 1000 * price_hc
Expand All @@ -147,7 +145,7 @@ def getDetail(headers, cur, con, client, pdl, pdl_config, mode="consumption", la
result[year][month]["best_plan"] = f"HC/HP"
result[year][month]["best_plan_percent"] = f"{abs(round(result[year][month]['base_vs_offpeak'], 2))}"

if offpeak_hours != None and price_base != 0 and price_hc != 0 and price_hp != 0:
if offpeak_hours != [] and price_base != 0 and price_hc != 0 and price_hp != 0:
if base_vs_offpeak > 0:
best_plan = f"BASE"
best_plan_percent = f"{abs(round(result[year][month]['base_vs_offpeak'], 2))}"
Expand All @@ -157,7 +155,7 @@ def getDetail(headers, cur, con, client, pdl, pdl_config, mode="consumption", la

year = dateObject.strftime('%Y')
month = dateObject.strftime('%m')
if offpeak_hours != None and offpeak_hours != "":
if offpeak_hours != [] and offpeak_hours != "":
for plan in ["hc", "hp"]:
ha_discovery[pdl].update({
f"{mode}_detail_this_month_{plan}": {
Expand Down Expand Up @@ -189,7 +187,7 @@ def getDetail(headers, cur, con, client, pdl, pdl_config, mode="consumption", la
if price_base != 0:
ha_discovery[pdl][f"{mode}_detail_this_month_base"]["attributes"][f"measure_base_euro"] = result[year][month][f"measure_base_euro"]

if offpeak_hours != None:
if offpeak_hours != []:
if price_base != 0 and price_hc != 0 and price_hp != 0:
ha_discovery[pdl].update({
f"{mode}_detail_this_month_compare": {
Expand All @@ -214,7 +212,8 @@ def getDetail(headers, cur, con, client, pdl, pdl_config, mode="consumption", la
return ha_discovery


def detailBeetwen(headers, cur, con, url, pdl, pdl_config, mode, dateBegin, dateEnded, last_activation_date, max_days_per_demand, offpeak_hours):
def detailBeetwen(headers, cur, con, url, pdl, pdl_config, mode, dateBegin, dateEnded, last_activation_date, max_days_per_demand,
offpeak_hours=[]):

response = {}

Expand Down Expand Up @@ -262,7 +261,7 @@ def is_between(time, time_range):
value = int(interval_reading['value'])
dateObject = datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
dateHourMinute = dateObject.strftime('%H:%M')
if offpeak_hours != None:
if offpeak_hours != []:
measure_type = "HP"
for offpeak_hour in offpeak_hours:
offpeak_begin = offpeak_hour.split("-")[0].replace('h', ':').replace('H', ':')
Expand Down Expand Up @@ -306,7 +305,7 @@ def is_between(time, time_range):
def checkHistoryDetail(cur, con, pdl, mode, dateBegin, dateEnded):

# FORCE THIS WEEK
if datetime.now().strftime('%Y-%m-%d') == dateEnded.strftime('%Y-%m-%d'):
if datetime.utcnow().strftime('%Y-%m-%d') == dateEnded.strftime('%Y-%m-%d'):
result = {
"missing_data": True
}
Expand Down
16 changes: 15 additions & 1 deletion app/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ def on_message(client, userdata, msg):
client.subscribe(client, sub_topic)
client.on_message = on_message

def logo(version):
log(" _____ _ _ ____ _ ")
log("| ____| _ __ ___ __| |(_) ___ / ___| __ _ | |_ ___ __ __ __ _ _ _ ")
log("| _| | '_ \ / _ \ / _` || |/ __| | | _ / _` || __|/ _ \\\ \ /\ / // _` || | | |")
log("| |___ | | | || __/| (_| || |\__ \ | |_| || (_| || |_| __/ \ V V /| (_| || |_| |")
log("|_____||_| |_| \___| \__,_||_||___/ \____| \__,_| \__|\___| \_/\_/ \__,_| \__, |")
log(" ____ __ __ ___ _____ _____ |___/")
log(" |___ \ | \/ | / _ \|_ _||_ _|")
log(" __) || |\/| || | | | | | | |")
log(" / __/ | | | || |_| | | | | | ")
log(" |_____||_| |_| \__\_\ |_| |_| ")
logLine1()
log(f" VERSION : {version}")
logLine1()

def logLine():
log("####################################################################################")
Expand Down Expand Up @@ -108,7 +122,7 @@ def apiRequest(cur, con, pdl, type="POST", url=None, headers=None, data=None):
if not f"call_nb_{pdl}" in query_result:
query_result[f"call_nb_{pdl}"] = 0

log(f"call_number : {query_result[f'call_nb_{pdl}']} (max : {query_result['max_call']})", "debug")
log(f"call_number : {query_result[f'call_nb_{pdl}']} (max : {query_result['max_call']})", "DEBUG")
if query_result["day"] == datetime.now().strftime('%Y-%m-%d'):
if query_result[f"call_nb_{pdl}"] > query_result["max_call"]:
return {
Expand Down
8 changes: 5 additions & 3 deletions app/influxdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
locale.setlocale(locale.LC_ALL, 'fr_FR.UTF-8')
timezone = pytz.timezone('Europe/Paris')

def influxdb_insert(cur, con, pdl, pdl_config, influxdb_api):
def influxdb_insert(cur, con, pdl, pdl_config, influxdb, influxdb_api):

def forceRound(x, n):
import decimal
Expand All @@ -29,6 +29,7 @@ def forceRound(x, n):
"HP": pdl_config['consumption_price_hp']
}

f.log(f" => Import daily")
query = f"SELECT * FROM consumption_daily WHERE pdl = '{pdl}';"
cur.execute(query)
query_result = cur.fetchall()
Expand All @@ -38,7 +39,7 @@ def forceRound(x, n):
value_kwh = value_wh / 1000
current_price = forceRound(value_kwh * price["BASE"], 4)
f.log(f"Insert daily {date} => {value_wh}", "DEBUG")
dateObject = datetime.strptime(date, '%Y-%m-%d') - relativedelta(hours=2)
dateObject = datetime.strptime(date, '%Y-%m-%d')
p = influxdb_client.Point("enedisgateway_daily") \
.tag("pdl", pdl) \
.tag("year", dateObject.strftime("%Y")) \
Expand All @@ -49,6 +50,7 @@ def forceRound(x, n):
.time(dateObject)
influxdb_api.write(bucket=main.config['influxdb']['bucket'], org=main.config['influxdb']['org'], record=p)

f.log(f" => Import detail")
query = f"SELECT * FROM consumption_detail WHERE pdl = '{pdl}';"
cur.execute(query)
query_result = cur.fetchall()
Expand All @@ -62,7 +64,7 @@ def forceRound(x, n):
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)
dateObject = datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
p = influxdb_client.Point("enedisgateway_detail") \
.tag("pdl", pdl) \
.tag("measure_type", measure_type) \
Expand Down
Loading

0 comments on commit 18ccfb1

Please sign in to comment.