diff --git a/app/models/ajax.py b/app/models/ajax.py index 6e0a0db8..8fbd33d5 100755 --- a/app/models/ajax.py +++ b/app/models/ajax.py @@ -203,7 +203,7 @@ def fetch(self, target, date): return data def blacklist(self, target, date): - print(target) + # print(target) result = {} if target == "consumption": if hasattr(self.usage_point_config, "consumption") and self.usage_point_config.consumption: diff --git a/app/models/database.py b/app/models/database.py index b4d8e193..a1df1065 100644 --- a/app/models/database.py +++ b/app/models/database.py @@ -1179,6 +1179,22 @@ def get_daily_max_power_all(self, usage_point_id, order="desc"): .order_by(order) ).all() + def get_daily_max_power_range(self, usage_point_id, begin, end): + query = ( + select(ConsumptionDailyMaxPower) + .join(UsagePoints.relation_consumption_daily_max_power) + .where(ConsumptionDailyMaxPower.usage_point_id == usage_point_id) + .where(ConsumptionDailyMaxPower.date >= begin) + .where(ConsumptionDailyMaxPower.date <= end) + .order_by(ConsumptionDailyMaxPower.date.desc()) + ) + LOG.debug(query.compile(compile_kwargs={"literal_binds": True})) + current_data = self.session.scalars(query).all() + if current_data is None: + return False + else: + return current_data + def get_daily_power(self, usage_point_id, begin, end): delta = end - begin result = { diff --git a/app/models/export_home_assistant.py b/app/models/export_home_assistant.py index 864d7222..abdb09df 100644 --- a/app/models/export_home_assistant.py +++ b/app/models/export_home_assistant.py @@ -1,11 +1,10 @@ import __main__ as app import json -from datetime import datetime, timezone, timedelta +from datetime import datetime, timedelta from math import floor import pytz from dateutil.relativedelta import relativedelta - from models.log import Log from models.stat import Stat @@ -26,9 +25,8 @@ def truncate(f, n): class HomeAssistant: - def __init__(self, usage_point_id, measurement_direction="consumption"): + def __init__(self, usage_point_id): self.usage_point_id = usage_point_id - self.measurement_direction = measurement_direction self.date_format = "%Y-%m-%d" self.date_format_detail = "%Y-%m-%d %H:%M:%S" self.config = app.DB.get_usage_point(usage_point_id) @@ -75,25 +73,35 @@ def __init__(self, usage_point_id, measurement_direction="consumption"): else: self.subscribed_power = None self.usage_point = app.DB.get_usage_point(self.usage_point_id) - self.stat = Stat(self.usage_point_id) + def export(self): app.LOG.title(f"[{self.usage_point_id}] Exportation des données dans Home Assistant (via MQTT)") - self.myelectricaldata_usage_point_id() - self.history_usage_point_id() - self.last_x_day(5) - def last_x_day(self, days): - topic = f"{self.discovery_prefix}/sensor/myelectricaldata_last_{days}_day/{self.usage_point_id}" + if (hasattr(self.config, "consumption") and self.config.consumption + or (hasattr(self.config, "consumption_detail") and self.config.consumption_detail)): + self.myelectricaldata_usage_point_id("consumption") + self.last_x_day(5, "consumption") + self.history_usage_point_id("consumption") + + if (hasattr(self.config, "production") and self.config.consumption + or (hasattr(self.config, "production_detail") and self.config.production_detail)): + self.myelectricaldata_usage_point_id("production") + self.last_x_day(5, "production") + self.history_usage_point_id("production") + + + def last_x_day(self, days, measurement_direction): + topic = f"{self.discovery_prefix}/sensor/myelectricaldata_{measurement_direction}_last_{days}_day/{self.usage_point_id}" config = { - "name": f"myelectricaldata_last{days}day_{self.usage_point_id}", - "uniq_id": f"myelectricaldata_last{days}day.{self.usage_point_id}", + "name": f"myelectricaldata_{measurement_direction}_last{days}day_{self.usage_point_id}", + "uniq_id": f"myelectricaldata_{measurement_direction}_last{days}day.{self.usage_point_id}", "stat_t": f"{topic}/state", "json_attr_t": f"{topic}/attributes", "unit_of_measurement": "W", "device": { "identifiers": [ - f"linky_history_{self.usage_point_id}" + f"linky_{measurement_direction}_history_{self.usage_point_id}" ], "name": f"Linky {self.usage_point_id}", "model": "Linky", @@ -108,14 +116,14 @@ def last_x_day(self, days): "lastUpdate": datetime.now().strftime(self.date_format_detail), "timeLastCall": datetime.now().strftime(self.date_format_detail), "time": [], - "consumption": [] + f"{measurement_direction}": [] } end = datetime.now() - timedelta(days=1) begin = end - timedelta(days) - range = app.DB.get_detail_range(self.usage_point_id, begin, end, self.measurement_direction) + range = app.DB.get_detail_range(self.usage_point_id, begin, end, measurement_direction) for data in range: attributes["time"].append(data.date.strftime("%Y-%m-%d %H:%M:%S")) - attributes["consumption"].append(data.value) + attributes[measurement_direction].append(data.value) attributes = json.dumps(attributes) data = { @@ -125,17 +133,18 @@ def last_x_day(self, days): } app.MQTT.publish_multiple(data, topic) - def history_usage_point_id(self): - topic = f"{self.discovery_prefix}/sensor/myelectricaldata_history/{self.usage_point_id}" + def history_usage_point_id(self, measurement_direction): + stats = Stat(self.usage_point_id, measurement_direction) + topic = f"{self.discovery_prefix}/sensor/myelectricaldata_{measurement_direction}_history/{self.usage_point_id}" config = { - "name": f"myelectricaldata_history_{self.usage_point_id}", - "uniq_id": f"myelectricaldata_history.{self.usage_point_id}", + "name": f"myelectricaldata_{measurement_direction}_history_{self.usage_point_id}", + "uniq_id": f"myelectricaldata_{measurement_direction}_history.{self.usage_point_id}", "stat_t": f"{topic}/state", "json_attr_t": f"{topic}/attributes", "unit_of_measurement": "kWh", "device": { "identifiers": [ - f"linky_history_{self.usage_point_id}" + f"linky_{measurement_direction}_history_{self.usage_point_id}" ], "name": f"Linky {self.usage_point_id}", "model": "Linky", @@ -144,7 +153,7 @@ def history_usage_point_id(self): } config = json.dumps(config) - state = app.DB.get_daily_last(self.usage_point_id, self.measurement_direction) + state = app.DB.get_daily_last(self.usage_point_id, measurement_direction) if state: state = state.value else: @@ -156,7 +165,7 @@ def history_usage_point_id(self): "activationDate": self.activation_date, "lastUpdate": datetime.now().strftime(self.date_format_detail), "timeLastCall": datetime.now().strftime(self.date_format_detail), - "yesterdayDate": self.stat.daily(0)["begin"] + "yesterdayDate": stats.daily(0)["begin"] } attributes = json.dumps(attributes) data = { @@ -166,19 +175,20 @@ def history_usage_point_id(self): } app.MQTT.publish_multiple(data, topic) - def myelectricaldata_usage_point_id(self): - topic = f"{self.discovery_prefix}/sensor/myelectricaldata/{self.usage_point_id}" + def myelectricaldata_usage_point_id(self, measurement_direction): + stats = Stat(self.usage_point_id, measurement_direction) + topic = f"{self.discovery_prefix}/sensor/myelectricaldata_{measurement_direction}/{self.usage_point_id}" config = { f"config": json.dumps( { - "name": f"myelectricaldata_{self.usage_point_id}", - "uniq_id": f"myelectricaldata.{self.usage_point_id}", + "name": f"myelectricaldata_{measurement_direction}_{self.usage_point_id}", + "uniq_id": f"myelectricaldata_{measurement_direction}.{self.usage_point_id}", "stat_t": f"{topic}/state", "json_attr_t": f"{topic}/attributes", "unit_of_measurement": "kWh", "device": { "identifiers": [ - f"linky_{self.usage_point_id}" + f"linky_{measurement_direction}_{self.usage_point_id}" ], "name": f"Linky {self.usage_point_id}", "model": "Linky", @@ -188,7 +198,7 @@ def myelectricaldata_usage_point_id(self): } app.MQTT.publish_multiple(config, topic) - state = app.DB.get_daily_last(self.usage_point_id, self.measurement_direction) + state = app.DB.get_daily_last(self.usage_point_id, measurement_direction) if state: state = state.value else: @@ -248,77 +258,77 @@ def myelectricaldata_usage_point_id(self): } # current_week - current_week = self.stat.current_week("consumption") + current_week = stats.current_week() current_week_value = current_week["value"] info["current_week"] = { "begin": current_week["begin"], "end": current_week["end"] } # last_week - last_week = self.stat.last_week("consumption") + last_week = stats.last_week() last_week_value = last_week["value"] info["last_week"] = { "begin": last_week["begin"], "end": last_week["end"] } # current_week_last_year - current_week_last_year = self.stat.current_week_last_year("consumption") + current_week_last_year = stats.current_week_last_year() current_week_last_year_value = current_week_last_year["value"] info["current_week_last_year"] = { "begin": current_week_last_year["begin"], "end": current_week_last_year["end"] } # last_month - last_month = self.stat.last_month("consumption") + last_month = stats.last_month() last_month_value = last_month["value"] info["last_month"] = { "begin": last_month["begin"], "end": last_month["end"] } # current_month - current_month = self.stat.current_month("consumption") + current_month = stats.current_month() current_month_value = current_month["value"] info["current_month"] = { "begin": current_month["begin"], "end": current_month["end"] } # current_month_last_year - current_month_last_year = self.stat.current_month_last_year("consumption") + current_month_last_year = stats.current_month_last_year() current_month_last_year_value = current_month_last_year["value"] info["current_month_last_year"] = { "begin": current_month_last_year["begin"], "end": current_month_last_year["end"] } # last_month_last_year - last_month_last_year = self.stat.last_month_last_year("consumption") + last_month_last_year = stats.last_month_last_year() last_month_last_year_value = last_month_last_year["value"] info["last_month_last_year"] = { "begin": last_month_last_year["begin"], "end": last_month_last_year["end"] } # current_year - current_year = self.stat.current_year("consumption") + current_year = stats.current_year() current_year_value = current_year["value"] info["current_year"] = { "begin": current_year["begin"], "end": current_year["end"] } # current_year_last_year - current_year_last_year = self.stat.current_year_last_year("consumption") + current_year_last_year = stats.current_year_last_year() current_year_last_year_value = current_year_last_year["value"] info["current_year_last_year"] = { "begin": current_year_last_year["begin"], "end": current_year_last_year["end"] } # last_year - last_year = self.stat.last_year("consumption") + last_year = stats.last_year() last_year_value = last_year["value"] info["last_year"] = { "begin": last_year["begin"], "end": last_year["end"] } # yesterday_hc_hp - yesterday_hc_hp = self.stat.yesterday_hc_hp("consumption") + yesterday_hc_hp = stats.yesterday_hc_hp() yesterday_hc_value = yesterday_hc_hp["value"]["hc"] yesterday_hp_value = yesterday_hc_hp["value"]["hp"] info["yesterday_hc_hp"] = { @@ -327,30 +337,33 @@ def myelectricaldata_usage_point_id(self): } # evolution - peak_offpeak_percent = self.stat.peak_offpeak_percent() - current_week_evolution = self.stat.current_week_evolution() - current_month_evolution = self.stat.current_month_evolution() - yesterday_evolution = self.stat.yesterday_evolution() - monthly_evolution = self.stat.monthly_evolution() + peak_offpeak_percent = stats.peak_offpeak_percent() + current_week_evolution = stats.current_week_evolution() + current_month_evolution = stats.current_month_evolution() + yesterday_evolution = stats.yesterday_evolution() + monthly_evolution = stats.monthly_evolution() yesterday_last_year = app.DB.get_daily_date(self.usage_point_id, yesterday_last_year) dailyweek_cost = [] if hasattr(self.config, "plan") and self.config.plan.upper() == "HC/HP": daily_cost = ( - convert_kw_to_euro(self.stat.detail(0, "HC")["value"], self.price_hc) - + convert_kw_to_euro(self.stat.detail(0, "HP")["value"], self.price_hp) + convert_kw_to_euro(stats.detail(0, "HC")["value"], self.price_hc) + + convert_kw_to_euro(stats.detail(0, "HP")["value"], self.price_hp) ) for i in range(7): value = ( - convert_kw_to_euro(self.stat.detail(i, "HP")["value"], self.price_hp) - + convert_kw_to_euro(self.stat.detail(i, "HC")["value"], self.price_hc) + convert_kw_to_euro(stats.detail(i, "HP")["value"], self.price_hp) + + convert_kw_to_euro(stats.detail(i, "HC")["value"], self.price_hc) ) dailyweek_cost.append(round(value, 1)) else: - daily_cost = convert_kw_to_euro(self.stat.daily(0)["value"], self.price_base) + daily_cost = convert_kw_to_euro(stats.daily(0)["value"], self.price_base) for i in range(7): - dailyweek_cost.append(convert_kw_to_euro(self.stat.daily(i)["value"], self.price_base)) + dailyweek_cost.append(convert_kw_to_euro(stats.daily(i)["value"], self.price_base)) + + if hasattr(self.config, "consumption_max_power") and self.config.consumption_max_power: + yesterday_consumption_max_power = stats.max_power(0)["value"] config = { f"attributes": json.dumps( @@ -359,29 +372,30 @@ def myelectricaldata_usage_point_id(self): "activationDate": self.activation_date, "lastUpdate": datetime.now().strftime(self.date_format_detail), "timeLastCall": datetime.now().strftime(self.date_format_detail), - "yesterdayDate": self.stat.daily(0)["begin"], - "yesterday": convert_kw(self.stat.daily(0)["value"]), + "yesterdayDate": stats.daily(0)["begin"], + "yesterday": convert_kw(stats.daily(0)["value"]), + "ServiceEnedis": "myElectricalData", "yesterdayLastYearDate": (datetime.now() - relativedelta(years=1)).strftime(self.date_format), "yesterdayLastYear": convert_kw(yesterday_last_year.value) if hasattr(yesterday_last_year, "value") else 0, "daily": [ - convert_kw(self.stat.daily(0)["value"]), - convert_kw(self.stat.daily(1)["value"]), - convert_kw(self.stat.daily(2)["value"]), - convert_kw(self.stat.daily(3)["value"]), - convert_kw(self.stat.daily(4)["value"]), - convert_kw(self.stat.daily(5)["value"]), - convert_kw(self.stat.daily(6)["value"]) + convert_kw(stats.daily(0)["value"]), + convert_kw(stats.daily(1)["value"]), + convert_kw(stats.daily(2)["value"]), + convert_kw(stats.daily(3)["value"]), + convert_kw(stats.daily(4)["value"]), + convert_kw(stats.daily(5)["value"]), + convert_kw(stats.daily(6)["value"]) ], "current_week": convert_kw(current_week_value), "last_week": convert_kw(last_week_value), - "day_1": convert_kw(self.stat.daily(0)["value"]), - "day_2": convert_kw(self.stat.daily(1)["value"]), - "day_3": convert_kw(self.stat.daily(2)["value"]), - "day_4": convert_kw(self.stat.daily(3)["value"]), - "day_5": convert_kw(self.stat.daily(4)["value"]), - "day_6": convert_kw(self.stat.daily(5)["value"]), - "day_7": convert_kw(self.stat.daily(6)["value"]), + "day_1": convert_kw(stats.daily(0)["value"]), + "day_2": convert_kw(stats.daily(1)["value"]), + "day_3": convert_kw(stats.daily(2)["value"]), + "day_4": convert_kw(stats.daily(3)["value"]), + "day_5": convert_kw(stats.daily(4)["value"]), + "day_6": convert_kw(stats.daily(5)["value"]), + "day_7": convert_kw(stats.daily(6)["value"]), "current_week_last_year": convert_kw(current_week_last_year_value), "last_month": convert_kw(last_month_value), "current_month": convert_kw(current_month_value), @@ -391,72 +405,73 @@ def myelectricaldata_usage_point_id(self): "current_year": convert_kw(current_year_value), "current_year_last_year": convert_kw(current_year_last_year_value), "dailyweek": [ - self.stat.daily(0)["begin"], - self.stat.daily(1)["begin"], - self.stat.daily(2)["begin"], - self.stat.daily(3)["begin"], - self.stat.daily(4)["begin"], - self.stat.daily(5)["begin"], - self.stat.daily(6)["begin"], + stats.daily(0)["begin"], + stats.daily(1)["begin"], + stats.daily(2)["begin"], + stats.daily(3)["begin"], + stats.daily(4)["begin"], + stats.daily(5)["begin"], + stats.daily(6)["begin"], ], "dailyweek_cost": dailyweek_cost, # TODO : If current_day = 0, dailyweek_hp & dailyweek_hc just next day... "dailyweek_costHP": [ - convert_kw_to_euro(self.stat.detail(0, "HP")["value"], self.price_hp), - convert_kw_to_euro(self.stat.detail(1, "HP")["value"], self.price_hp), - convert_kw_to_euro(self.stat.detail(2, "HP")["value"], self.price_hp), - convert_kw_to_euro(self.stat.detail(3, "HP")["value"], self.price_hp), - convert_kw_to_euro(self.stat.detail(4, "HP")["value"], self.price_hp), - convert_kw_to_euro(self.stat.detail(5, "HP")["value"], self.price_hp), - convert_kw_to_euro(self.stat.detail(6, "HP")["value"], self.price_hp), + convert_kw_to_euro(stats.detail(0, "HP")["value"], self.price_hp), + convert_kw_to_euro(stats.detail(1, "HP")["value"], self.price_hp), + convert_kw_to_euro(stats.detail(2, "HP")["value"], self.price_hp), + convert_kw_to_euro(stats.detail(3, "HP")["value"], self.price_hp), + convert_kw_to_euro(stats.detail(4, "HP")["value"], self.price_hp), + convert_kw_to_euro(stats.detail(5, "HP")["value"], self.price_hp), + convert_kw_to_euro(stats.detail(6, "HP")["value"], self.price_hp), ], "dailyweek_HP": [ - convert_kw(self.stat.detail(0, "HP")["value"]), - convert_kw(self.stat.detail(1, "HP")["value"]), - convert_kw(self.stat.detail(2, "HP")["value"]), - convert_kw(self.stat.detail(3, "HP")["value"]), - convert_kw(self.stat.detail(4, "HP")["value"]), - convert_kw(self.stat.detail(5, "HP")["value"]), - convert_kw(self.stat.detail(6, "HP")["value"]), + convert_kw(stats.detail(0, "HP")["value"]), + convert_kw(stats.detail(1, "HP")["value"]), + convert_kw(stats.detail(2, "HP")["value"]), + convert_kw(stats.detail(3, "HP")["value"]), + convert_kw(stats.detail(4, "HP")["value"]), + convert_kw(stats.detail(5, "HP")["value"]), + convert_kw(stats.detail(6, "HP")["value"]), ], "daily_cost": daily_cost, "yesterday_HP_cost": convert_kw_to_euro(yesterday_hp_value, self.price_hp), "yesterday_HP": convert_kw(yesterday_hp_value), - "day_1_HP": self.stat.detail(0, "HP")["value"], - "day_2_HP": self.stat.detail(1, "HP")["value"], - "day_3_HP": self.stat.detail(2, "HP")["value"], - "day_4_HP": self.stat.detail(3, "HP")["value"], - "day_5_HP": self.stat.detail(4, "HP")["value"], - "day_6_HP": self.stat.detail(5, "HP")["value"], - "day_7_HP": self.stat.detail(6, "HP")["value"], + "day_1_HP": stats.detail(0, "HP")["value"], + "day_2_HP": stats.detail(1, "HP")["value"], + "day_3_HP": stats.detail(2, "HP")["value"], + "day_4_HP": stats.detail(3, "HP")["value"], + "day_5_HP": stats.detail(4, "HP")["value"], + "day_6_HP": stats.detail(5, "HP")["value"], + "day_7_HP": stats.detail(6, "HP")["value"], "dailyweek_costHC": [ - convert_kw_to_euro(self.stat.detail(0, "HC")["value"], self.price_hc), - convert_kw_to_euro(self.stat.detail(1, "HC")["value"], self.price_hc), - convert_kw_to_euro(self.stat.detail(2, "HC")["value"], self.price_hc), - convert_kw_to_euro(self.stat.detail(3, "HC")["value"], self.price_hc), - convert_kw_to_euro(self.stat.detail(4, "HC")["value"], self.price_hc), - convert_kw_to_euro(self.stat.detail(5, "HC")["value"], self.price_hc), - convert_kw_to_euro(self.stat.detail(6, "HC")["value"], self.price_hc), + convert_kw_to_euro(stats.detail(0, "HC")["value"], self.price_hc), + convert_kw_to_euro(stats.detail(1, "HC")["value"], self.price_hc), + convert_kw_to_euro(stats.detail(2, "HC")["value"], self.price_hc), + convert_kw_to_euro(stats.detail(3, "HC")["value"], self.price_hc), + convert_kw_to_euro(stats.detail(4, "HC")["value"], self.price_hc), + convert_kw_to_euro(stats.detail(5, "HC")["value"], self.price_hc), + convert_kw_to_euro(stats.detail(6, "HC")["value"], self.price_hc), ], "dailyweek_HC": [ - convert_kw(self.stat.detail(0, "HC")["value"]), - convert_kw(self.stat.detail(1, "HC")["value"]), - convert_kw(self.stat.detail(2, "HC")["value"]), - convert_kw(self.stat.detail(3, "HC")["value"]), - convert_kw(self.stat.detail(4, "HC")["value"]), - convert_kw(self.stat.detail(5, "HC")["value"]), - convert_kw(self.stat.detail(6, "HC")["value"]), + convert_kw(stats.detail(0, "HC")["value"]), + convert_kw(stats.detail(1, "HC")["value"]), + convert_kw(stats.detail(2, "HC")["value"]), + convert_kw(stats.detail(3, "HC")["value"]), + convert_kw(stats.detail(4, "HC")["value"]), + convert_kw(stats.detail(5, "HC")["value"]), + convert_kw(stats.detail(6, "HC")["value"]), ], "yesterday_HC_cost": convert_kw_to_euro(yesterday_hc_value, self.price_hc), "yesterday_HC": convert_kw(yesterday_hc_value), - "day_1_HC": self.stat.detail(0, "HC")["value"], - "day_2_HC": self.stat.detail(1, "HC")["value"], - "day_3_HC": self.stat.detail(2, "HC")["value"], - "day_4_HC": self.stat.detail(3, "HC")["value"], - "day_5_HC": self.stat.detail(4, "HC")["value"], - "day_6_HC": self.stat.detail(5, "HC")["value"], - "day_7_HC": self.stat.detail(6, "HC")["value"], + "day_1_HC": stats.detail(0, "HC")["value"], + "day_2_HC": stats.detail(1, "HC")["value"], + "day_3_HC": stats.detail(2, "HC")["value"], + "day_4_HC": stats.detail(3, "HC")["value"], + "day_5_HC": stats.detail(4, "HC")["value"], + "day_6_HC": stats.detail(5, "HC")["value"], + "day_7_HC": stats.detail(6, "HC")["value"], "peak_offpeak_percent": round(peak_offpeak_percent, 2), + "YesterdayConsumptionMaxPower": yesterday_consumption_max_power, "monthly_evolution": round(monthly_evolution, 2), "current_week_evolution": round(current_week_evolution, 2), "current_month_evolution": round(current_month_evolution, 2), diff --git a/app/models/export_mqtt.py b/app/models/export_mqtt.py index b7073078..8827d5a8 100644 --- a/app/models/export_mqtt.py +++ b/app/models/export_mqtt.py @@ -2,9 +2,7 @@ from datetime import datetime from dateutil.relativedelta import relativedelta - from models.stat import Stat -from models.config import Config class ExportMqtt: @@ -13,7 +11,7 @@ def __init__(self, usage_point_id, measurement_direction="consumption"): self.usage_point_id = usage_point_id self.measurement_direction = measurement_direction self.date_format = "%Y-%m-%d" - self.stat = Stat(self.usage_point_id) + self.stat = Stat(self.usage_point_id, measurement_direction) def status(self): app.LOG.title(f"[{self.usage_point_id}] Statut du compte.") @@ -65,7 +63,7 @@ def status(self): f"{self.usage_point_id}/status/last_call": str(last_call), f"{self.usage_point_id}/status/ban": str(ban) } - print(consentement_expiration) + # print(consentement_expiration) app.MQTT.publish_multiple(consentement_expiration) app.LOG.log(" => Finish") @@ -106,9 +104,9 @@ def daily_annual(self, price): finish = False while not finish: year = int(date_begin_current.strftime('%Y')) - get_daily_year = self.stat.get_year(year=year, measurement_direction=self.measurement_direction) - get_daily_month = self.stat.get_month(year=year, measurement_direction=self.measurement_direction) - get_daily_week = self.stat.get_week(year=year, measurement_direction=self.measurement_direction) + get_daily_year = self.stat.get_year(year=year) + get_daily_month = self.stat.get_month(year=year) + get_daily_week = self.stat.get_week(year=year) sub_prefix = f"{self.usage_point_id}/{self.measurement_direction}/annual/{year}" mqtt_data = { # thisYear @@ -132,10 +130,10 @@ def daily_annual(self, price): } for week in range(7): - begin = self.stat.daily(week, measurement_direction=self.measurement_direction)["begin"] + begin = self.stat.daily(week)["begin"] begin_day = datetime.strptime(self.stat.daily(week)["begin"], self.date_format).strftime("%A") - end = self.stat.daily(week, measurement_direction=self.measurement_direction)["end"] - value = self.stat.daily(week, measurement_direction=self.measurement_direction)["value"] + end = self.stat.daily(week)["end"] + value = self.stat.daily(week)["value"] mqtt_data[f"{sub_prefix}/week/{begin_day}/dateBegin"] = begin mqtt_data[f"{sub_prefix}/week/{begin_day}/dateEnd"] = end mqtt_data[f"{sub_prefix}/week/{begin_day}/base/Wh"] = value @@ -143,8 +141,7 @@ def daily_annual(self, price): mqtt_data[f"{sub_prefix}/week/{begin_day}/base/euro"] = round(value / 1000 * price, 2) for month in range(1, 13): - get_daily_month = self.stat.get_month(year=year, month=month, - measurement_direction=self.measurement_direction) + get_daily_month = self.stat.get_month(year=year, month=month) mqtt_data[f"{sub_prefix}/month/{month}/dateBegin"] = get_daily_month["begin"] mqtt_data[f"{sub_prefix}/month/{month}/dateEnd"] = get_daily_month["end"] mqtt_data[f"{sub_prefix}/month/{month}/base/Wh"] = get_daily_month["value"] @@ -182,12 +179,9 @@ def daily_linear(self, price): else: key = f"year-{idx}" sub_prefix = f"{self.usage_point_id}/{self.measurement_direction}/linear/{key}" - get_daily_year_linear = self.stat.get_year_linear(idx, - measurement_direction=self.measurement_direction) - get_daily_month_linear = self.stat.get_month_linear(idx, - measurement_direction=self.measurement_direction) - get_daily_week_linear = self.stat.get_week_linear(idx, - measurement_direction=self.measurement_direction) + get_daily_year_linear = self.stat.get_year_linear(idx, ) + get_daily_month_linear = self.stat.get_month_linear(idx) + get_daily_week_linear = self.stat.get_week_linear(idx) mqtt_data = { # thisYear f"{sub_prefix}/thisYear/dateBegin": get_daily_year_linear["begin"], @@ -238,18 +232,12 @@ def detail_annual(self, price_hp, price_hc=0): while not finish: year = int(date_begin_current.strftime('%Y')) month = int(datetime.now().strftime('%m')) - get_detail_year_hp = self.stat.get_year(year=year, measure_type="HP", - measurement_direction=self.measurement_direction) - get_detail_year_hc = self.stat.get_year(year=year, measure_type="HC", - measurement_direction=self.measurement_direction) - get_detail_month_hp = self.stat.get_month(year=year, month=month, measure_type="HP", - measurement_direction=self.measurement_direction) - get_detail_month_hc = self.stat.get_month(year=year, month=month, measure_type="HC", - measurement_direction=self.measurement_direction) - get_detail_week_hp = self.stat.get_week(year=year, month=month, measure_type="HP", - measurement_direction=self.measurement_direction) - get_detail_week_hc = self.stat.get_week(year=year, month=month, measure_type="HC", - measurement_direction=self.measurement_direction) + get_detail_year_hp = self.stat.get_year(year=year, measure_type="HP") + get_detail_year_hc = self.stat.get_year(year=year, measure_type="HC") + get_detail_month_hp = self.stat.get_month(year=year, month=month, measure_type="HP") + get_detail_month_hc = self.stat.get_month(year=year, month=month, measure_type="HC") + get_detail_week_hp = self.stat.get_week(year=year, month=month, measure_type="HP", ) + get_detail_week_hc = self.stat.get_week(year=year, month=month, measure_type="HC", ) sub_prefix = f"{self.usage_point_id}/{self.measurement_direction}/annual/{year}" mqtt_data = { # thisYear - HP @@ -283,7 +271,7 @@ def detail_annual(self, price_hp, price_hc=0): begin_hp_day = ( datetime.strptime(self.stat.detail(week, "HP")["begin"], self.date_format).strftime("%A") ) - value_hp = self.stat.detail(week, "HP", measurement_direction=self.measurement_direction)["value"] + value_hp = self.stat.detail(week, "HP")["value"] prefix = f"{sub_prefix}/week/{begin_hp_day}/hp" mqtt_data[f"{prefix}/Wh"] = value_hp mqtt_data[f"{prefix}/kWh"] = round(value_hp / 1000, 2) @@ -291,7 +279,7 @@ def detail_annual(self, price_hp, price_hc=0): # HC begin_hc_day = ( datetime.strptime( - self.stat.detail(week, "HC", measurement_direction=self.measurement_direction)["begin"], + self.stat.detail(week, "HC")["begin"], self.date_format).strftime("%A") ) value_hc = self.stat.detail(week, "HC")["value"] @@ -303,15 +291,13 @@ def detail_annual(self, price_hp, price_hc=0): for month in range(12): month = month + 1 # HP - get_detail_month_hp = self.stat.get_month(year=year, month=month, measure_type="HP", - measurement_direction=self.measurement_direction) + get_detail_month_hp = self.stat.get_month(year=year, month=month, measure_type="HP") prefix = f"{sub_prefix}/month/{month}/hp" mqtt_data[f"{prefix}/Wh"] = get_detail_month_hp["value"] mqtt_data[f"{prefix}/kWh"] = round(get_detail_month_hp["value"] / 1000, 2) mqtt_data[f"{prefix}/euro"] = round(get_detail_month_hp["value"] / 1000 * price_hp, 2) # HC - get_detail_month_hc = self.stat.get_month(year=year, month=month, measure_type="HC", - measurement_direction=self.measurement_direction) + get_detail_month_hc = self.stat.get_month(year=year, month=month, measure_type="HC") prefix = f"{sub_prefix}/month/{month}/hc" mqtt_data[f"{prefix}/Wh"] = get_detail_month_hc["value"] mqtt_data[f"{prefix}/kWh"] = round(get_detail_month_hc["value"] / 1000, 2) @@ -346,30 +332,12 @@ def detail_linear(self, price_hp, price_hc=0): else: key = f"year-{idx}" sub_prefix = f"{self.usage_point_id}/{self.measurement_direction}/linear/{key}" - get_daily_year_linear_hp = self.stat.get_year_linear( - idx, "HP", - measurement_direction=self.measurement_direction - ) - get_daily_year_linear_hc = self.stat.get_year_linear( - idx, "HC", - measurement_direction=self.measurement_direction - ) - get_detail_month_linear_hp = self.stat.get_month_linear( - idx, "HP", - measurement_direction=self.measurement_direction - ) - get_detail_month_linear_hc = self.stat.get_month_linear( - idx, "HC", - measurement_direction=self.measurement_direction - ) - get_detail_week_linear_hp = self.stat.get_week_linear( - idx, "HP", - measurement_direction=self.measurement_direction - ) - get_detail_week_linear_hc = self.stat.get_week_linear( - idx, "HC", - measurement_direction=self.measurement_direction - ) + get_daily_year_linear_hp = self.stat.get_year_linear(idx, "HP") + get_daily_year_linear_hc = self.stat.get_year_linear(idx, "HC") + get_detail_month_linear_hp = self.stat.get_month_linear(idx, "HP") + get_detail_month_linear_hc = self.stat.get_month_linear(idx, "HC") + get_detail_week_linear_hp = self.stat.get_week_linear(idx, "HP") + get_detail_week_linear_hc = self.stat.get_week_linear(idx, "HC", ) mqtt_data = { # thisYear f"{sub_prefix}/thisYear/hp/Wh": get_daily_year_linear_hp["value"], @@ -433,6 +401,5 @@ def max_power(self): mqtt_data[f"{sub_prefix}/threshold_exceeded"] = 1 threshold_usage = int(100 * value_w / max_value) mqtt_data[f"{sub_prefix}/percentage_usage"] = threshold_usage - print(mqtt_data) + # print(mqtt_data) app.MQTT.publish_multiple(mqtt_data) - diff --git a/app/models/export_mqttv1.py b/app/models/export_mqttv1.py index f25b0f92..11def00e 100644 --- a/app/models/export_mqttv1.py +++ b/app/models/export_mqttv1.py @@ -61,7 +61,7 @@ def status(self): f"{self.usage_point_id}/status/last_call": str(last_call), f"{self.usage_point_id}/status/ban": str(ban) } - print(consentement_expiration) + # print(consentement_expiration) app.MQTT.publish_multiple(consentement_expiration) app.LOG.log(" => Finish") diff --git a/app/models/query_address.py b/app/models/query_address.py index c7292b8c..f3eae6b1 100755 --- a/app/models/query_address.py +++ b/app/models/query_address.py @@ -1,10 +1,12 @@ +import __main__ as app import json +import traceback -import __main__ as app -from config import URL from dependencies import * from models.query import Query +from config import URL + class Address: @@ -31,17 +33,28 @@ def run(self): response = response_json["customer"]["usage_points"][0] usage_point = response["usage_point"] usage_point_addresses = usage_point["usage_point_addresses"] + response = usage_point_addresses + response.update(usage_point) self.db.set_addresse(self.usage_point_id, { - "usage_points": str(usage_point["usage_point_id"]) if usage_point["usage_point_id"] is not None else "", - "street": str(usage_point_addresses["street"]) if usage_point_addresses["street"] is not None else "", - "locality": str(usage_point_addresses["locality"]) if usage_point_addresses["locality"] is not None else "", - "postal_code": str(usage_point_addresses["postal_code"]) if usage_point_addresses["postal_code"] is not None else "", - "insee_code": str(usage_point_addresses["insee_code"]) if usage_point_addresses["insee_code"] is not None else "", + "usage_points": str(usage_point["usage_point_id"]) if usage_point[ + "usage_point_id"] is not None else "", + "street": str(usage_point_addresses["street"]) if usage_point_addresses[ + "street"] is not None else "", + "locality": str(usage_point_addresses["locality"]) if usage_point_addresses[ + "locality"] is not None else "", + "postal_code": str(usage_point_addresses["postal_code"]) if usage_point_addresses[ + "postal_code"] is not None else "", + "insee_code": str(usage_point_addresses["insee_code"]) if usage_point_addresses[ + "insee_code"] is not None else "", "city": str(usage_point_addresses["city"]) if usage_point_addresses["city"] is not None else "", - "country": str(usage_point_addresses["country"]) if usage_point_addresses["country"] is not None else "", - "geo_points": str(usage_point_addresses["geo_points"]) if usage_point_addresses["geo_points"] is not None else "", + "country": str(usage_point_addresses["country"]) if usage_point_addresses[ + "country"] is not None else "", + "geo_points": str(usage_point_addresses["geo_points"]) if usage_point_addresses[ + "geo_points"] is not None else "", }) - except LookupError: + except Exception as e: + app.LOG.error(e) + traceback.print_exc() response = { "error": True, "description": "Erreur lors de la récupération du contrat." @@ -75,10 +88,7 @@ def get(self): app.LOG.debug(f" => {result}") if "error" not in result: for key, value in result.items(): - if isinstance(value, dict): - for k, v in value.items(): - app.LOG.log(f" {k}: {v}") - else: - app.LOG.log(f"{key}: {value}") - + app.LOG.log(f"{key}: {value}") + else: + app.LOG.error(result) return result diff --git a/app/models/stat.py b/app/models/stat.py index 0d514175..bfc94a42 100644 --- a/app/models/stat.py +++ b/app/models/stat.py @@ -10,8 +10,9 @@ class Stat: - def __init__(self, usage_point_id): + def __init__(self, usage_point_id, measurement_direction): self.usage_point_id = usage_point_id + self.measurement_direction = measurement_direction self.usage_point_id_config = app.DB.get_usage_point(self.usage_point_id) self.date_format = "%Y-%m-%d" self.date_format_detail = "%Y-%m-%d %H:%M:%S" @@ -40,11 +41,11 @@ def __init__(self, usage_point_id): self.value_peak_offpeak_percent_hp_vs_hc = 0 self.value_monthly_evolution = 0 - def daily(self, index=0, measurement_direction="consumption"): + def daily(self, index=0): begin = datetime.combine(self.yesterday_date - timedelta(days=index), datetime.min.time()) end = datetime.combine(begin, datetime.max.time()) value = 0 - for data in app.DB.get_daily_range(self.usage_point_id, begin, end, measurement_direction): + for data in app.DB.get_daily_range(self.usage_point_id, begin, end, self.measurement_direction): value = value + data.value return { "value": value, @@ -52,11 +53,11 @@ def daily(self, index=0, measurement_direction="consumption"): "end": end.strftime(self.date_format) } - def detail(self, index, measure_type=None, measurement_direction="consumption"): + def detail(self, index, measure_type=None): begin = datetime.combine(self.yesterday_date - timedelta(days=index), datetime.min.time()) end = datetime.combine(begin, datetime.max.time()) value = 0 - for data in app.DB.get_detail_range(self.usage_point_id, begin, end, measurement_direction): + for data in app.DB.get_detail_range(self.usage_point_id, begin, end, self.measurement_direction): if measure_type is None or (measure_type == "HP" and data.measure_type == "HP"): value = value + data.value / (60 / data.interval) elif measure_type is None or (measure_type == "HC" and data.measure_type == "HC"): @@ -67,14 +68,28 @@ def detail(self, index, measure_type=None, measurement_direction="consumption"): "end": end.strftime(self.date_format) } - def current_week_array(self, measurement_direction="consumption"): + def max_power(self, index=0): + begin = datetime.combine(self.yesterday_date - timedelta(days=index), datetime.min.time()) + end = datetime.combine(begin, datetime.max.time()) + value = 0 + # print(app.DB.get_daily_max_power_range(self.usage_point_id, begin, end)) + for data in app.DB.get_daily_max_power_range(self.usage_point_id, begin, end): + # print(data) + value = value + data.value + return { + "value": value, + "begin": begin.strftime(self.date_format), + "end": end.strftime(self.date_format) + } + + def current_week_array(self): begin = datetime.combine(self.yesterday_date, datetime.min.time()) begin_return = begin end = datetime.combine(self.yesterday_date, datetime.max.time()) day_idx = 0 daily_obj = [] while day_idx < 7: - day = app.DB.get_daily_range(self.usage_point_id, begin, end, measurement_direction) + day = app.DB.get_daily_range(self.usage_point_id, begin, end, self.measurement_direction) if day: daily_obj.append({ "date": day[0].date, @@ -94,11 +109,11 @@ def current_week_array(self, measurement_direction="consumption"): "end": end } - def current_week(self, measurement_direction="consumption"): + def current_week(self): app.LOG.log("current_week") begin = datetime.combine(self.now_date - relativedelta(weeks=1), datetime.min.time()) end = datetime.combine(self.yesterday_date, datetime.max.time()) - for data in app.DB.get_daily_range(self.usage_point_id, begin, end, measurement_direction): + for data in app.DB.get_daily_range(self.usage_point_id, begin, end, self.measurement_direction): self.value_current_week = self.value_current_week + data.value app.LOG.log(f" => {self.value_current_week}") return { @@ -107,11 +122,11 @@ def current_week(self, measurement_direction="consumption"): "end": end.strftime(self.date_format) } - # def get_week(self, year, measurement_direction="consumption"): + # def get_week(self, year): # app.LOG.log(f"[{year}] current_week") # begin = datetime.combine(self.now_date - relativedelta(weeks=1), datetime.min.time()) # end = datetime.combine(self.yesterday_date, datetime.max.time()) - # for data in app.DB.get_daily_range(self.usage_point_id, begin, end, measurement_direction): + # for data in app.DB.get_daily_range(self.usage_point_id, begin, end, self.measurement_direction): # self.value_current_week = self.value_current_week + data.value # app.LOG.log(f" => {self.value_current_week}") # return { @@ -120,19 +135,19 @@ def current_week(self, measurement_direction="consumption"): # "end": end.strftime(self.date_format) # } - def last_week(self, measurement_direction="consumption"): + def last_week(self): app.LOG.log("last_week") begin = datetime.combine(self.now_date - relativedelta(weeks=2), datetime.min.time()) end = datetime.combine(self.yesterday_date - relativedelta(weeks=1), datetime.max.time()) # while day_idx < 7: - # day = app.DB.get_daily_range(self.usage_point_id, begin, end, self.measurement_direction) + # day = app.DB.get_daily_range(self.usage_point_id, begin, end, self.self.measurement_direction) # if day: # for data in day: # last_week = last_week + data.value # begin = begin - timedelta(days=1) # end = end - timedelta(days=1) # day_idx = day_idx + 1 - for data in app.DB.get_daily_range(self.usage_point_id, begin, end, measurement_direction): + for data in app.DB.get_daily_range(self.usage_point_id, begin, end, self.measurement_direction): self.value_last_week = self.value_last_week + data.value app.LOG.log(f" => {self.value_last_week}") return { @@ -148,11 +163,11 @@ def current_week_evolution(self): app.LOG.log(f" => {self.value_current_week_evolution}") return self.value_current_week_evolution - def yesterday(self, measurement_direction="consumption"): + def yesterday(self): app.LOG.log("yesterday") begin = datetime.combine(self.yesterday_date, datetime.min.time()) end = datetime.combine(self.yesterday_date, datetime.max.time()) - data = app.DB.get_daily_range(self.usage_point_id, begin, end, measurement_direction) + data = app.DB.get_daily_range(self.usage_point_id, begin, end, self.measurement_direction) if data: self.value_yesterday = data[0].value else: @@ -164,11 +179,11 @@ def yesterday(self, measurement_direction="consumption"): "end": end.strftime(self.date_format) } - def yesterday_1(self, measurement_direction="consumption"): + def yesterday_1(self): app.LOG.log("yesterday_1") begin = datetime.combine(self.yesterday_date - timedelta(days=1), datetime.min.time()) end = datetime.combine(self.yesterday_date - timedelta(days=1), datetime.max.time()) - data = app.DB.get_daily_range(self.usage_point_id, begin, end, measurement_direction) + data = app.DB.get_daily_range(self.usage_point_id, begin, end, self.measurement_direction) if data: self.value_yesterday_1 = data[0].value else: @@ -189,13 +204,13 @@ def yesterday_evolution(self): app.LOG.log(f" => {self.value_yesterday_evolution}") return self.value_yesterday_evolution - def current_week_last_year(self, measurement_direction="consumption"): + def current_week_last_year(self): app.LOG.log("current_week_last_year") # begin = datetime.combine(yesterday - relativedelta(years=1), datetime.min.time()) # end = datetime.combine(yesterday - relativedelta(years=1), datetime.max.time()) # day_idx = 0 # while day_idx < 7: - # day = app.DB.get_daily_range(self.usage_point_id, begin, end, self.measurement_direction) + # day = app.DB.get_daily_range(self.usage_point_id, begin, end, self.self.measurement_direction) # if day: # for data in day: # current_week_last_year = current_week_last_year + data.value @@ -204,7 +219,7 @@ def current_week_last_year(self, measurement_direction="consumption"): # day_idx = day_idx + 1 begin = datetime.combine((self.now_date - timedelta(weeks=1)) - relativedelta(years=1), datetime.min.time()) end = datetime.combine(self.yesterday_date - relativedelta(years=1), datetime.max.time()) - for data in app.DB.get_daily_range(self.usage_point_id, begin, end, measurement_direction): + for data in app.DB.get_daily_range(self.usage_point_id, begin, end, self.measurement_direction): self.value_current_week_last_year = self.value_current_week_last_year + data.value app.LOG.log(f" => {self.value_current_week_last_year}") return { @@ -213,12 +228,12 @@ def current_week_last_year(self, measurement_direction="consumption"): "end": end.strftime(self.date_format) } - def last_month(self, measurement_direction="consumption"): + def last_month(self): app.LOG.log("last_month") begin = datetime.combine((self.now_date.replace(day=1) - timedelta(days=1)).replace(day=1), datetime.min.time()) end = datetime.combine(self.yesterday_date.replace(day=1) - timedelta(days=1), datetime.max.time()) - for day in app.DB.get_daily_range(self.usage_point_id, begin, end, measurement_direction): + for day in app.DB.get_daily_range(self.usage_point_id, begin, end, self.measurement_direction): self.value_last_month = self.value_last_month + day.value app.LOG.log(f" => {self.value_last_month}") return { @@ -227,11 +242,11 @@ def last_month(self, measurement_direction="consumption"): "end": end.strftime(self.date_format) } - def current_month(self, measurement_direction="consumption"): + def current_month(self): app.LOG.log("current_month") begin = datetime.combine(self.now_date.replace(day=1), datetime.min.time()) end = self.yesterday_date - for day in app.DB.get_daily_range(self.usage_point_id, begin, end, measurement_direction): + for day in app.DB.get_daily_range(self.usage_point_id, begin, end, self.measurement_direction): self.value_current_month = self.value_current_month + day.value app.LOG.log(f" => {self.value_current_month}") return { @@ -240,11 +255,11 @@ def current_month(self, measurement_direction="consumption"): "end": end.strftime(self.date_format) } - def current_month_last_year(self, measurement_direction="consumption"): + def current_month_last_year(self): app.LOG.log("current_month_last_year") begin = datetime.combine(self.now_date.replace(day=1), datetime.min.time()) - relativedelta(years=1) end = self.yesterday_date - relativedelta(years=1) - for day in app.DB.get_daily_range(self.usage_point_id, begin, end, measurement_direction): + for day in app.DB.get_daily_range(self.usage_point_id, begin, end, self.measurement_direction): self.value_current_month_last_year = self.value_current_month_last_year + day.value app.LOG.log(f" => {self.value_current_month_last_year}") return { @@ -262,7 +277,7 @@ def current_month_evolution(self): app.LOG.log(f" => {self.value_current_month_evolution}") return self.value_current_month_evolution - def last_month_last_year(self, measurement_direction="consumption"): + def last_month_last_year(self): app.LOG.log("last_month_last_year") begin = datetime.combine( (self.now_date.replace(day=1) - timedelta(days=1)).replace(day=1), @@ -270,7 +285,7 @@ def last_month_last_year(self, measurement_direction="consumption"): end = datetime.combine( self.yesterday_date.replace(day=1) - timedelta(days=1), datetime.max.time()) - relativedelta(years=1) - for day in app.DB.get_daily_range(self.usage_point_id, begin, end, measurement_direction): + for day in app.DB.get_daily_range(self.usage_point_id, begin, end, self.measurement_direction): self.value_last_month_last_year = self.value_last_month_last_year + day.value app.LOG.log(f" => {self.value_last_month_last_year}") return { @@ -288,11 +303,11 @@ def monthly_evolution(self): app.LOG.log(f" => {self.value_monthly_evolution}") return self.value_monthly_evolution - def current_year(self, measurement_direction="consumption"): + def current_year(self): app.LOG.log("current_year") begin = datetime.combine(self.now_date.replace(day=1).replace(month=1), datetime.min.time()) end = self.yesterday_date - for day in app.DB.get_daily_range(self.usage_point_id, begin, end, measurement_direction): + for day in app.DB.get_daily_range(self.usage_point_id, begin, end, self.measurement_direction): self.value_current_year = self.value_current_year + day.value app.LOG.log(f" => {self.value_current_year}") return { @@ -301,14 +316,14 @@ def current_year(self, measurement_direction="consumption"): "end": end.strftime(self.date_format) } - def current_year_last_year(self, measurement_direction="consumption"): + def current_year_last_year(self): app.LOG.log("current_year_last_year") begin = datetime.combine( datetime.combine(self.now_date.replace(day=1).replace(month=1), datetime.min.time()) - relativedelta( years=1), datetime.min.time()) end = self.yesterday_date - relativedelta(years=1) - for day in app.DB.get_daily_range(self.usage_point_id, begin, end, measurement_direction): + for day in app.DB.get_daily_range(self.usage_point_id, begin, end, self.measurement_direction): self.value_current_year_last_year = self.value_current_year_last_year + day.value app.LOG.log(f" => {self.value_current_year_last_year}") return { @@ -317,13 +332,13 @@ def current_year_last_year(self, measurement_direction="consumption"): "end": end.strftime(self.date_format) } - def last_year(self, measurement_direction="consumption"): + def last_year(self): app.LOG.log("last_year") begin = datetime.combine(self.now_date.replace(day=1).replace(month=1) - relativedelta(years=1), datetime.min.time()) last_day_of_month = calendar.monthrange(int(begin.strftime("%Y")), 12)[1] end = datetime.combine(begin.replace(day=last_day_of_month).replace(month=12), datetime.max.time()) - for day in app.DB.get_daily_range(self.usage_point_id, begin, end, measurement_direction): + for day in app.DB.get_daily_range(self.usage_point_id, begin, end, self.measurement_direction): self.value_last_year = self.value_last_year + day.value app.LOG.log(f" => {self.value_last_year}") return { @@ -332,11 +347,11 @@ def last_year(self, measurement_direction="consumption"): "end": end.strftime(self.date_format) } - def yesterday_hc_hp(self, measurement_direction="consumption"): + def yesterday_hc_hp(self): app.LOG.log("yesterday_hp / yesterday_hc") begin = datetime.combine(self.yesterday_date, datetime.min.time()) end = datetime.combine(self.now_date, datetime.max.time()) - for day in app.DB.get_detail_range(self.usage_point_id, begin, end, measurement_direction): + for day in app.DB.get_detail_range(self.usage_point_id, begin, end, self.measurement_direction): if day.measure_type == "HP": self.value_yesterday_hp = self.value_yesterday_hp + (day.value / (60 / day.interval)) if day.measure_type == "HC": @@ -352,11 +367,11 @@ def yesterday_hc_hp(self, measurement_direction="consumption"): "end": end.strftime(self.date_format) } - def peak_offpeak_percent(self, measurement_direction="consumption"): + def peak_offpeak_percent(self): app.LOG.log("peak_offpeak_percent_hp VS peak_offpeak_percent_hc") begin = self.yesterday_date - relativedelta(years=1) end = self.yesterday_date - for day in app.DB.get_detail_range(self.usage_point_id, begin, end, measurement_direction): + for day in app.DB.get_detail_range(self.usage_point_id, begin, end, self.measurement_direction): if day.measure_type == "HP": self.value_peak_offpeak_percent_hp = self.value_peak_offpeak_percent_hp + ( day.value / (60 / day.interval)) @@ -372,16 +387,16 @@ def peak_offpeak_percent(self, measurement_direction="consumption"): return self.value_peak_offpeak_percent_hp_vs_hc # STAT V2 - def get_year(self, year, measure_type=None, measurement_direction="consumption"): + def get_year(self, year, measure_type=None): begin = datetime.combine(self.now_date.replace(year=year).replace(day=1).replace(month=1), datetime.min.time()) last_day_of_month = calendar.monthrange(year, 12)[1] end = datetime.combine(self.now_date.replace(year=year).replace(day=last_day_of_month).replace(month=12), datetime.max.time()) value = 0 if measure_type is None: - for day in app.DB.get_daily_range(self.usage_point_id, begin, end, measurement_direction): + for day in app.DB.get_daily_range(self.usage_point_id, begin, end, self.measurement_direction): value = value + day.value else: - for day in app.DB.get_detail_range(self.usage_point_id, begin, end, measurement_direction): + for day in app.DB.get_detail_range(self.usage_point_id, begin, end, self.measurement_direction): if day.measure_type == measure_type: value = value + (day.value / (60 / day.interval)) return { @@ -390,15 +405,15 @@ def get_year(self, year, measure_type=None, measurement_direction="consumption") "end": end.strftime(self.date_format) } - def get_year_linear(self, idx, measure_type=None, measurement_direction="consumption"): + def get_year_linear(self, idx, measure_type=None): end = datetime.combine(self.yesterday_date - relativedelta(years=idx), datetime.max.time()) begin = datetime.combine(end - relativedelta(years=1), datetime.min.time()) value = 0 if measure_type is None: - for day in app.DB.get_daily_range(self.usage_point_id, begin, end, measurement_direction): + for day in app.DB.get_daily_range(self.usage_point_id, begin, end, self.measurement_direction): value = value + day.value else: - for day in app.DB.get_detail_range(self.usage_point_id, begin, end, measurement_direction): + for day in app.DB.get_detail_range(self.usage_point_id, begin, end, self.measurement_direction): if day.measure_type == measure_type: value = value + (day.value / (60 / day.interval)) return { @@ -407,7 +422,7 @@ def get_year_linear(self, idx, measure_type=None, measurement_direction="consump "end": end.strftime(self.date_format) } - def get_month(self, year, month=None, measure_type=None, measurement_direction="consumption"): + def get_month(self, year, month=None, measure_type=None): if month is None: month = int(datetime.now().strftime('%m')) begin = datetime.combine(self.now_date.replace(year=year).replace(day=1).replace(month=month), datetime.min.time()) @@ -415,10 +430,10 @@ def get_month(self, year, month=None, measure_type=None, measurement_direction=" end = datetime.combine(self.now_date.replace(year=year).replace(day=last_day_of_month).replace(month=month), datetime.max.time()) value = 0 if measure_type is None: - for day in app.DB.get_daily_range(self.usage_point_id, begin, end, measurement_direction): + for day in app.DB.get_daily_range(self.usage_point_id, begin, end, self.measurement_direction): value = value + day.value else: - for day in app.DB.get_detail_range(self.usage_point_id, begin, end, measurement_direction): + for day in app.DB.get_detail_range(self.usage_point_id, begin, end, self.measurement_direction): if day.measure_type == measure_type: value = value + (day.value / (60 / day.interval)) return { @@ -427,15 +442,15 @@ def get_month(self, year, month=None, measure_type=None, measurement_direction=" "end": end.strftime(self.date_format) } - def get_month_linear(self, idx, measure_type=None, measurement_direction="consumption"): + def get_month_linear(self, idx, measure_type=None): end = datetime.combine(self.yesterday_date - relativedelta(years=idx), datetime.max.time()) begin = datetime.combine(end - relativedelta(months=1), datetime.min.time()) value = 0 if measure_type is None: - for day in app.DB.get_daily_range(self.usage_point_id, begin, end, measurement_direction): + for day in app.DB.get_daily_range(self.usage_point_id, begin, end, self.measurement_direction): value = value + day.value else: - for day in app.DB.get_detail_range(self.usage_point_id, begin, end, measurement_direction): + for day in app.DB.get_detail_range(self.usage_point_id, begin, end, self.measurement_direction): if day.measure_type == measure_type: value = value + (day.value / (60 / day.interval)) return { @@ -444,7 +459,7 @@ def get_month_linear(self, idx, measure_type=None, measurement_direction="consum "end": end.strftime(self.date_format) } - def get_week(self, year, month=None, measure_type=None, measurement_direction="consumption"): + def get_week(self, year, month=None, measure_type=None): if month is None: month = int(datetime.now().strftime('%m')) today = date.today() @@ -454,10 +469,10 @@ def get_week(self, year, month=None, measure_type=None, measurement_direction="c end = datetime.combine(self.now_date.replace(year=year).replace(day=int(start.strftime("%d"))).replace(month=month), datetime.max.time()) value = 0 if measure_type is None: - for day in app.DB.get_daily_range(self.usage_point_id, begin, end, measurement_direction): + for day in app.DB.get_daily_range(self.usage_point_id, begin, end, self.measurement_direction): value = value + day.value else: - for day in app.DB.get_detail_range(self.usage_point_id, begin, end, measurement_direction): + for day in app.DB.get_detail_range(self.usage_point_id, begin, end, self.measurement_direction): if day.measure_type == measure_type: value = value + (day.value / (60 / day.interval)) return { @@ -466,15 +481,15 @@ def get_week(self, year, month=None, measure_type=None, measurement_direction="c "end": end.strftime(self.date_format) } - def get_week_linear(self, idx, measure_type=None, measurement_direction="consumption"): + def get_week_linear(self, idx, measure_type=None): end = datetime.combine(self.yesterday_date - relativedelta(years=idx), datetime.max.time()) begin = datetime.combine(end - timedelta(days=7), datetime.min.time()) value = 0 if measure_type is None: - for day in app.DB.get_daily_range(self.usage_point_id, begin, end, measurement_direction): + for day in app.DB.get_daily_range(self.usage_point_id, begin, end, self.measurement_direction): value = value + day.value else: - for day in app.DB.get_detail_range(self.usage_point_id, begin, end, measurement_direction): + for day in app.DB.get_detail_range(self.usage_point_id, begin, end, self.measurement_direction): if day.measure_type == measure_type: value = value + (day.value / (60 / day.interval)) return { diff --git a/app/templates/usage_point_id.py b/app/templates/usage_point_id.py index 8560fdf4..8bf0e31e 100644 --- a/app/templates/usage_point_id.py +++ b/app/templates/usage_point_id.py @@ -403,10 +403,10 @@ def consumption(self): def consumption_max_power(self): if hasattr(self.config, "consumption_max_power") and self.config.consumption_max_power: - if hasattr(self.contract, "subscribed_power"): + if hasattr(self.contract, "subscribed_power") and self.contract.subscribed_power is not None: max_power = self.contract.subscribed_power.split(' ')[0] else: - max_power = 6 + max_power = 999 daily_result = Datatable(self.usage_point_id).html( title="Puissance", tag=f"consumption_max_power",