diff --git a/.github/workflows/build_push_docker.yml b/.github/workflows/build_push_docker.yml index 887b3d0d..fec53d12 100755 --- a/.github/workflows/build_push_docker.yml +++ b/.github/workflows/build_push_docker.yml @@ -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: diff --git a/README.md b/README.md index 656ccd1a..bdbb0d0c 100755 --- a/README.md +++ b/README.md @@ -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 : diff --git a/app/VERSION b/app/VERSION index 389faccc..ad83b1b0 100755 --- a/app/VERSION +++ b/app/VERSION @@ -1 +1 @@ -0.5.5 \ No newline at end of file +0.5.6 \ No newline at end of file diff --git a/app/daily.py b/app/daily.py index 7aa32258..beb3d463 100755 --- a/app/daily.py +++ b/app/daily.py @@ -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 :") diff --git a/app/detail.py b/app/detail.py index f1e868b7..e7a1cf6f 100755 --- a/app/detail.py +++ b/app/detail.py @@ -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: @@ -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 @@ -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({ @@ -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"] @@ -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") @@ -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 } diff --git a/app/function.py b/app/function.py index 17a13d39..82dde68c 100755 --- a/app/function.py +++ b/app/function.py @@ -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 @@ -45,6 +44,8 @@ def on_message(client, userdata, msg): def logLine(): log("####################################################################################") +def logLine1(): + log("------------------------------------------------------------------------------------") def log(msg, level="INFO "): diff --git a/app/main.py b/app/main.py index c450438f..d848d7d1 100755 --- a/app/main.py +++ b/app/main.py @@ -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 ( @@ -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)]) @@ -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) :") @@ -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) :") @@ -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) :") @@ -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) :") @@ -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() @@ -567,11 +585,14 @@ 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) @@ -579,15 +600,7 @@ def run(): 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]}") @@ -614,7 +627,6 @@ def run(): # MQTT client = f.connect_mqtt() client.loop_start() - pprint(client) # INFLUXDB if influxdb_enable == True: diff --git a/app/myenedis.py b/app/myenedis.py index 419ebe42..ccf0282a 100755 --- a/app/myenedis.py +++ b/app/myenedis.py @@ -72,6 +72,7 @@ def forceRound(x, n): query = f"SELECT * FROM consumption_daily WHERE pdl = '{pdl}' AND date='{yesterday}';" cur.execute(query) query_result = cur.fetchone() + pprint(query_result) attributes['yesterdayDate'] = yesterday if query_result != None and query_result[2] != 0: attributes['yesterday'] = query_result[2] / 1000 @@ -94,7 +95,7 @@ def forceRound(x, n): query = f"SELECT * FROM consumption_daily WHERE pdl = '{pdl}' AND date='{yesterday_last_year}';" cur.execute(query) query_result = cur.fetchone() - + pprint(query_result) if query_result != None and query_result[2] != 0: attributes['yesterdayLastYear'] = query_result[2] / 1000 else: @@ -149,7 +150,7 @@ def forceRound(x, n): current_week_last_year = attributes['current_week_last_year'] if attributes['current_week_last_year'] == 0: - attributes['current_week_last_year'] = -1 + attributes['current_week_last_year'] = 0 else: attributes['current_week_last_year'] = str(forceRound(attributes['current_week_last_year'], 2)) @@ -176,11 +177,11 @@ def forceRound(x, n): last_month = attributes['last_month'] if attributes['last_month'] == 0: - attributes['last_month'] = -1 + attributes['last_month'] = 0 current_month = attributes['current_month'] if attributes['current_month'] == 0: - attributes['current_month'] = -1 + attributes['current_month'] = 0 else: attributes['current_month'] = str(forceRound(attributes['current_month'], 2)) @@ -201,7 +202,7 @@ def forceRound(x, n): current_month_last_year = attributes['current_month_last_year'] if attributes['current_month_last_year'] == 0: - attributes['current_month_last_year'] = -1 + attributes['current_month_last_year'] = 0 else: attributes['current_month_last_year'] = str(forceRound(attributes['current_month_last_year'], 2)) @@ -220,7 +221,7 @@ def forceRound(x, n): last_month_last_year = attributes['last_month_last_year'] if attributes['last_month_last_year'] == 0: - attributes['last_month_last_year'] = -1 + attributes['last_month_last_year'] = 0 else: attributes['last_month_last_year'] = str(forceRound(last_month_last_year, 2)) @@ -246,7 +247,7 @@ def forceRound(x, n): current_year = attributes['current_year'] if attributes['current_year'] == 0: - attributes['current_year'] = -1 + attributes['current_year'] = 0 else: attributes['current_year'] = str(forceRound(attributes['current_year'], 2)) @@ -264,7 +265,7 @@ def forceRound(x, n): current_year_last_year = attributes['current_year_last_year'] if attributes['current_year_last_year'] == 0: - attributes['current_year_last_year'] = -1 + attributes['current_year_last_year'] = 0 else: attributes['current_year_last_year'] = str(forceRound(attributes['current_year_last_year'], 2)) @@ -288,10 +289,10 @@ def forceRound(x, n): attributes[f'day_{day}_{measure_type}'] = int(0) if query_result == []: if measure_type != "BASE": - attributes[f'day_{day}_{measure_type}'] = -1 - attributes[f'dailyweek_cost{measure_type}'].append(-1) - attributes[f'dailyweek_{measure_type}'].append(-1) - attributes["dailyweek_cost"][day-1] = -1 + attributes[f'day_{day}_{measure_type}'] = 0 + attributes[f'dailyweek_cost{measure_type}'].append(0) + attributes[f'dailyweek_{measure_type}'].append(0) + attributes["dailyweek_cost"][day0] = 0 else: value_wh_total = 0 dailyweek_cost = 0 @@ -346,32 +347,32 @@ def forceRound(x, n): if measure_type != "BASE": attributes['peak_offpeak_percent'] = forceRound(abs(100 * (peak_offpeak_percent['HC'] - peak_offpeak_percent['HP']) / peak_offpeak_percent['HP']), 2) else: - attributes['peak_offpeak_percent'] = -1 + attributes['peak_offpeak_percent'] = 0 def variation(val1, val2): # print(f"100 * ({val1} - {val2}) / {val2}") result = 100 * (val1 - val2) / val2 return result - if last_month != -1 and last_month != 0 and last_month_last_year != -1 and last_month_last_year != 0: + if last_month != 0 and last_month != 0 and last_month_last_year != 0 and last_month_last_year != 0: attributes['monthly_evolution'] = forceRound(variation(last_month, last_month_last_year), 2) else: - attributes['monthly_evolution'] = -1 + attributes['monthly_evolution'] = 0 - if current_week != -1 and current_week != 0 and current_week_last_year != -1 and current_week_last_year != 0: + if current_week != 0 and current_week != 0 and current_week_last_year != 0 and current_week_last_year != 0: attributes['current_week_evolution'] = forceRound(variation(current_week, current_week_last_year), 2) else: - attributes['current_week_evolution'] = -1 + attributes['current_week_evolution'] = 0 - if current_month != -1 and current_month != 0 and current_month_last_year != -1 and current_month_last_year != 0: + if current_month != 0 and current_month != 0 and current_month_last_year != 0 and current_month_last_year != 0: attributes['current_month_evolution'] = forceRound(variation(current_month, current_month_last_year), 2) else: - attributes['current_month_evolution'] = -1 + attributes['current_month_evolution'] = 0 - if state != -1 and state != 0 and yesterdayLastYear != -1 and yesterdayLastYear != 0: + if state != 0 and state != 0 and yesterdayLastYear != 0 and yesterdayLastYear != 0: attributes['yesterday_evolution'] = forceRound(variation(state, yesterdayLastYear), 2) else: - attributes['yesterday_evolution'] = -1 + attributes['yesterday_evolution'] = 0 attributes[f'dailyweek'] = sorted(list(set(attributes[f'dailyweek'])), reverse=True) attributes[f'friendly_name'] = f"EnedisGateway.{pdl}" @@ -402,8 +403,8 @@ def variation(val1, val2): offpeak_hours.append(current) attributes[f'offpeak_hours'] = offpeak_hours else: - attributes[f'offpeak_hours_enedis'] = -1 - attributes[f'offpeak_hours'] = -1 + attributes[f'offpeak_hours_enedis'] = 0 + attributes[f'offpeak_hours'] = 0 query = f"SELECT * FROM config WHERE key = '{pdl}_subscribed_power'" cur.execute(query)