Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.5.6 #35

Merged
merged 6 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 53 additions & 6 deletions .github/workflows/build_push_docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,76 @@ jobs:
echo "::set-output name=dev::false"
fi

- name: Build and push
- name: Discord notification building state
# if: steps.check-tag.outputs.dev == 'false'
env:
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
uses: Ilshidur/action-discord@master
with:
args: '**EnedisGateway2MQTT** : Version **${{ steps.vars.outputs.version }}** is in building state...'

##################################################################################################################
# VERSION
- name: Build and push ${{ steps.vars.outputs.version }}
uses: docker/build-push-action@v2
if: steps.check-tag.outputs.dev == 'false'
# if: steps.check-tag.outputs.dev == 'false'
with:
context: .
platforms: linux/amd64,linux/arm64,linux/arm/v6,linux/arm/v7
push: true
tags: |
m4dm4rtig4n/enedisgateway2mqtt:latest
m4dm4rtig4n/enedisgateway2mqtt:${{ steps.vars.outputs.version }}
m4dm4rtig4n/enedisgateway2mqtt:latest-dev

- name: Discord notification {{ steps.vars.outputs.version }} image version is up to date
# if: steps.check-tag.outputs.dev == 'false'
env:
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
uses: Ilshidur/action-discord@master
with:
args: '**EnedisGateway2MQTT** : **${{ steps.vars.outputs.version }}** image version is up to date'

##################################################################################################################
# LATEST-DEV
- name: Build and push
uses: docker/build-push-action@v2
if: steps.check-tag.outputs.dev == 'true'
# if: steps.check-tag.outputs.dev == 'true'
with:
context: .
platforms: linux/amd64,linux/arm64,linux/arm/v6,linux/arm/v7
push: true
tags: |
m4dm4rtig4n/enedisgateway2mqtt:${{ steps.vars.outputs.version }}
m4dm4rtig4n/enedisgateway2mqtt:latest-dev

- name: Discord notification
# if: steps.check-tag.outputs.dev == 'true'
env:
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
uses: Ilshidur/action-discord@master
with:
args: '**EnedisGateway2MQTT** : **latest-dev** image version is up to date'

##################################################################################################################
# LATEST
- name: Build and push
uses: docker/build-push-action@v2
if: steps.check-tag.outputs.dev == 'false'
with:
context: .
platforms: linux/amd64,linux/arm64,linux/arm/v6,linux/arm/v7
push: true
tags: |
m4dm4rtig4n/enedisgateway2mqtt:latest

- name: Discord notification
if: steps.check-tag.outputs.dev == 'false'
env:
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
uses: Ilshidur/action-discord@master
with:
args: '**EnedisGateway2MQTT** : **latest** image version is up to date'

##################################################################################################################
# FINISH
- name: Discord notification
# if: steps.check-tag.outputs.dev == 'false'
env:
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,12 @@ make start

## Change log:

### [0.5.6] - 2021-11-01

- Reduce API Call
- Add more log
- Fix bug

### [0.5.5] - 2021-11-01

- Fix log on MQTT connection failed :
Expand Down
2 changes: 1 addition & 1 deletion app/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.5.5
0.5.6
18 changes: 15 additions & 3 deletions app/daily.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,23 @@ def dailyBeetwen(cur, con, pdl, mode, dateBegin, dateEnded, last_activation_date
for date, date_data in current_data['date'].items():
if not date in new_date:
not_found_data.append(date)
current_date = datetime.strptime(date, '%Y-%m-%d').date()
current_day = datetime.today().date()
if date_data['fail'] == 0 and date_data['value'] == 0:
cur.execute(f"INSERT OR REPLACE INTO {mode}_daily VALUES ('{pdl}','{date}','0','1')")
if current_day == current_date:
fail = 0
else:
fail = 1
cur.execute(f"INSERT OR REPLACE INTO {mode}_daily VALUES ('{pdl}','{date}','0','{fail}')")
# cur.execute(f"INSERT OR REPLACE INTO {mode}_daily VALUES ('{pdl}','{date}','0','1')")
else:
cur.execute(
f"UPDATE {mode}_daily SET fail = {date_data['fail'] + 1} WHERE pdl = '{pdl}' and date = '{date}'")
if current_day != current_date:
fail = 0
else:
fail = date_data['fail']
cur.execute(f"UPDATE {mode}_daily SET fail = {fail + 1} WHERE pdl = '{pdl}' and date = '{date}'")
# cur.execute(f"UPDATE {mode}_daily SET fail = {date_data['fail'] + 1} WHERE pdl = '{pdl}' and date = '{date}'")


if not_found_data != []:
f.log("Data not found :")
Expand Down
13 changes: 7 additions & 6 deletions app/detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ def getDetail(cur, con, client, mode="consumption", last_activation_date=datetim
query = f"SELECT * FROM consumption_detail WHERE pdl = '{pdl}' ORDER BY date;"
cur.execute(query)
query_result = cur.fetchall()

result = {}
base_vs_offpeak = 0
for data in query_result:
Expand Down Expand Up @@ -122,7 +121,6 @@ def getDetail(cur, con, client, mode="consumption", last_activation_date=datetim
result[year][month]["measure_ration_hc"] = round(
100 * result[year][month]["measure_hc"] / result[year][month]["measure_total"], 2)


if price_base != 0:
result[year][month]["measure_base_euro"] = result[year][month]["measure_total_wh"] / 1000 * price_base

Expand Down Expand Up @@ -155,7 +153,6 @@ def getDetail(cur, con, client, mode="consumption", last_activation_date=datetim

year = dateObject.strftime('%Y')
month = dateObject.strftime('%m')

if offpeak_hours != None and offpeak_hours != "":
for plan in ["hc", "hp"]:
ha_discovery[pdl].update({
Expand All @@ -167,8 +164,10 @@ def getDetail(cur, con, client, mode="consumption", last_activation_date=datetim
"attributes": {}
}
})
ha_discovery[pdl][f"{mode}_detail_this_month_{plan}"]["attributes"]["ratio"] = result[year][month][f"measure_ration_{plan}"]
ha_discovery[pdl][f"{mode}_detail_this_month_{plan}"]["attributes"]["W"] = result[year][month][f"measure_{plan}"]
if f"measure_ration_{plan}" in result[year][month]:
ha_discovery[pdl][f"{mode}_detail_this_month_{plan}"]["attributes"]["ratio"] = result[year][month][f"measure_ration_{plan}"]
if f"measure_{plan}" in result[year][month]:
ha_discovery[pdl][f"{mode}_detail_this_month_{plan}"]["attributes"]["W"] = result[year][month][f"measure_{plan}"]

if price_hc != 0 and price_hp != 0:
ha_discovery[pdl][f"{mode}_detail_this_month_{plan}"]["attributes"][f"measure_{plan}_euro"] = result[year][month][f"measure_{plan}_euro"]
Expand Down Expand Up @@ -284,6 +283,7 @@ def is_between(time, time_range):
elif detail['error_code'] == 2:
f.log(f"Fetch data error detected beetween {dateBegin} / {dateEnded}", "ERROR")
f.log(f" => {detail['description']}", "ERROR")
# cur.execute(f"UPDATE {mode}_detail SET fail = {date_data['fail'] + 1} WHERE pdl = '{pdl}' and date = '{date}'")
else:
f.log(f"API return error beetween {dateBegin} / {dateEnded}", "ERROR")
f.log(f" => {detail['description']}", "ERROR")
Expand Down Expand Up @@ -312,7 +312,8 @@ def checkHistoryDetail(cur, con, mode, dateBegin, dateEnded):
query = f"SELECT * FROM {mode}_detail WHERE pdl = '{pdl}' AND date BETWEEN '{dateBegin}' AND '{dateEnded}' ORDER BY date"
cur.execute(query)
query_result = cur.fetchall()
if len(query_result) < 160:
# if len(query_result) < 160:
if not query_result:
result = {
"missing_data": True
}
Expand Down
3 changes: 2 additions & 1 deletion app/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def connect_mqtt():
client = mqtt_client.Client(main.client_id)
if main.username != "" and main.password != "":
client.username_pw_set(main.username, main.password)
client.on_connect = on_connect
client.connect(main.broker, main.port)
log("Connected to MQTT Broker!")
return client
Expand Down Expand Up @@ -45,6 +44,8 @@ def on_message(client, userdata, msg):

def logLine():
log("####################################################################################")
def logLine1():
log("------------------------------------------------------------------------------------")


def log(msg, level="INFO "):
Expand Down
42 changes: 27 additions & 15 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,12 @@ def init_database(cur):
fail INTEGER)''')
cur.execute('''CREATE UNIQUE INDEX idx_date_consumption_detail
ON consumption_detail (date)''')
# cur.execute('''CREATE TABLE consumption_detail_try (
# pdl TEXT NOT NULL,
# date TEXT NOT NULL,
# try INTEGER)''')
# cur.execute('''CREATE UNIQUE INDEX idx_date_consumption_detail_try
# ON consumption_detail_try (date)''')
## PRODUCTION
# DAILY
cur.execute('''CREATE TABLE production_daily (
Expand All @@ -306,7 +312,7 @@ def init_database(cur):
config = {
"day": datetime.now().strftime('%Y-%m-%d'),
"call_number": 0,
"max_call": 15
"max_call": 200
}
cur.execute(config_query, ["config", json.dumps(config)])

Expand Down Expand Up @@ -420,7 +426,9 @@ def run():
f.logLine()
f.log("Get Consumption :")
ha_discovery_consumption = day.getDaily(cur, con, client, "consumption", last_activation_date)
# pprint(ha_discovery_consumption)
f.logLine1()
f.log(" SUCCESS : Consumption daily imported")
f.logLine1()
if ha_autodiscovery == True:
f.logLine()
f.log("Home Assistant auto-discovery (Consumption) :")
Expand All @@ -446,12 +454,15 @@ def run():
ha.haAutodiscovery(client=client, type="sensor", pdl=pdl, name=name, value=sensor_data['value'],
attributes=attributes, unit_of_meas=unit_of_meas,
device_class=device_class, state_class=state_class)

f.log(" => HA Sensor updated")
# f.logLine()

if get_consumption_detail == True:
f.log("Get Consumption Detail:")
ha_discovery_consumption = detail.getDetail(cur, con, client, "consumption", last_activation_date, offpeak_hours)
f.logLine1()
f.log(" SUCCESS : Consumption detail imported")
f.logLine1()
if ha_autodiscovery == True:
f.logLine()
f.log("Home Assistant auto-discovery (Consumption Detail) :")
Expand All @@ -477,13 +488,16 @@ def run():
ha.haAutodiscovery(client=client, type="sensor", pdl=pdl, name=name, value=sensor_data['value'],
attributes=attributes, unit_of_meas=unit_of_meas,
device_class=device_class, state_class=state_class)

f.log(" => HA Sensor updated")
# f.logLine()

if get_production == True:
f.logLine()
f.log("Get production :")
ha_discovery_production = day.getDaily(cur, con, client, "production", last_activation_date)
f.logLine1()
f.log(" SUCCESS : Production daily imported")
f.logLine1()
if ha_autodiscovery == True:
f.logLine()
f.log("Home Assistant auto-discovery (Production) :")
Expand All @@ -508,12 +522,15 @@ def run():
ha.haAutodiscovery(client=client, type="sensor", pdl=pdl, name=name, value=sensor_data['value'],
attributes=attributes, unit_of_meas=unit_of_meas,
device_class=device_class, state_class=state_class)
# f.logLine()
f.log(" => HA Sensor updated")

if get_production_detail == True:
f.logLine()
f.log("Get production Detail:")
ha_discovery_consumption = detail.getDetail(cur, con, client, "production", last_activation_date, offpeak_hours)
f.logLine1()
f.log(" SUCCESS : Production detail imported")
f.logLine1()
if ha_autodiscovery == True:
f.logLine()
f.log("Home Assistant auto-discovery (Production Detail) :")
Expand All @@ -539,6 +556,7 @@ def run():
ha.haAutodiscovery(client=client, type="sensor", pdl=pdl, name=name, value=sensor_data['value'],
attributes=attributes, unit_of_meas=unit_of_meas,
device_class=device_class, state_class=state_class)
f.log(" => HA Sensor updated")

if card_myenedis == True:
f.logLine()
Expand Down Expand Up @@ -567,27 +585,22 @@ def run():
value=sensor_data['value'],
attributes=attributes, unit_of_meas=unit_of_meas,
device_class=device_class, state_class=state_class)
f.log(" => Sensor generated")


if influxdb_enable == True:
f.logLine()
f.log("Push data in influxdb")
influx.influxdb_insert(cur, con, influxdb_api)
f.log(" => Data exported")

query = f"SELECT * FROM consumption_daily WHERE pdl == '{pdl}' AND fail > {fail_count} ORDER BY date"
rows = con.execute(query)
if rows.fetchone() is not None:
f.logLine()
f.log(f"Consumption data not found on enedis (after {fail_count} retry) :")
# pprint(rows.fetchall())
for row in rows:
f.log(f"{row[0]} => {row[1]}")

query = f"SELECT * FROM production_daily WHERE pdl == '{pdl}' AND fail > {fail_count} ORDER BY date"
rows = con.execute(query)
if rows.fetchone() is not None:
f.logLine()
f.log(f"Production data not found on enedis (after {fail_count} retry) :")
# pprint(rows.fetchall())
# pprint(rows)
for row in rows:
f.log(f"{row[0]} => {row[1]}")

Expand All @@ -614,7 +627,6 @@ def run():
# MQTT
client = f.connect_mqtt()
client.loop_start()
pprint(client)

# INFLUXDB
if influxdb_enable == True:
Expand Down
Loading