Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
m4dm4rtig4n committed Jul 28, 2023
1 parent ffd4873 commit 8519a7e
Show file tree
Hide file tree
Showing 18 changed files with 285 additions and 176 deletions.
2 changes: 1 addition & 1 deletion app/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.9.1-beta9
0.9.1-beta11
4 changes: 2 additions & 2 deletions app/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def filter(self, record: logging.LogRecord) -> bool:
INFLUXB_ENABLE = False
INFLUXDB = None
INFLUXDB_CONFIG = CONFIG.influxdb_config()
if INFLUXDB_CONFIG and "enable" in INFLUXDB_CONFIG and INFLUXDB_CONFIG["enable"]:
if INFLUXDB_CONFIG and "enable" in INFLUXDB_CONFIG and str2bool(INFLUXDB_CONFIG["enable"]):
INFLUXB_ENABLE = True
if "method" in INFLUXDB_CONFIG:
method = INFLUXDB_CONFIG["method"]
Expand All @@ -110,7 +110,7 @@ def filter(self, record: logging.LogRecord) -> bool:
MQTT_ENABLE = False
MQTT = None
MQTT_CONFIG = CONFIG.mqtt_config()
if MQTT_CONFIG and "enable" in MQTT_CONFIG and MQTT_CONFIG["enable"]:
if MQTT_CONFIG and "enable" in MQTT_CONFIG and str2bool(MQTT_CONFIG["enable"]):
MQTT_ENABLE = True
MQTT = Mqtt(
hostname=MQTT_CONFIG["hostname"],
Expand Down
6 changes: 5 additions & 1 deletion app/models/ajax.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ def __init__(self, usage_point_id=None):
self.usage_points_id_list = ""

def gateway_status(self):
title(f"[{self.usage_point_id}] Check de l'état de la passerelle.")
if self.usage_point_id is not None:
msg = f"[{self.usage_point_id}] Check de l'état de la passerelle."
else:
msg = "Check de l'état de la passerelle."
title(msg)
return Status().ping()

def account_status(self):
Expand Down
141 changes: 78 additions & 63 deletions app/models/export_home_assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,11 @@ def export(self):

def sensor(self, **kwargs):
topic = f"{self.discovery_prefix}/sensor/{kwargs['topic']}"
if "unit_of_measurement" not in kwargs:
unit_of_measurement = ""
else:
unit_of_measurement = kwargs["unit_of_measurement"]
config = {
"name": f"myelectricaldata.{kwargs['name']}",
"uniq_id": kwargs['uniq_id'],
"stat_t": f"{topic}/state",
"json_attr_t": f"{topic}/attributes",
"unit_of_measurement": unit_of_measurement,
"device": {
"identifiers": [
f"linky_{self.usage_point_id}"
Expand All @@ -123,6 +118,8 @@ def sensor(self, **kwargs):
"manufacturer": "MyElectricalData"
}
}
if "unit_of_measurement" in kwargs:
config["unit_of_measurement"] = kwargs["unit_of_measurement"]
attributes = {
**kwargs["attributes"],
**{
Expand All @@ -143,7 +140,7 @@ def sensor(self, **kwargs):

def last_x_day(self, days, measurement_direction):
uniq_id = f"myelectricaldata_{measurement_direction}_{self.usage_point_id}_last{days}day"
logging.info(f"- {uniq_id}")
logging.info(f"- sensor.{uniq_id}")
end = datetime.combine(datetime.now() - timedelta(days=1), datetime.max.time())
begin = datetime.combine(end - timedelta(days), datetime.min.time())
range = self.db.get_detail_range(self.usage_point_id, begin, end, measurement_direction)
Expand All @@ -162,7 +159,7 @@ def last_x_day(self, days, measurement_direction):

def history_usage_point_id(self, measurement_direction):
uniq_id = f"myelectricaldata_{measurement_direction}_{self.usage_point_id}_history"
logging.info(f"- {uniq_id}")
logging.info(f"- sensor.{uniq_id}")
stats = Stat(self.usage_point_id, measurement_direction)
state = self.db.get_daily_last(self.usage_point_id, measurement_direction)
if state:
Expand All @@ -184,7 +181,7 @@ def history_usage_point_id(self, measurement_direction):

def tempo(self):
uniq_id = f"myelectricaldata_{self.usage_point_id}_tempo_today"
logging.info(f"- {uniq_id}")
logging.info(f"- sensor.{uniq_id}")
begin = datetime.combine(datetime.now(), datetime.min.time())
end = datetime.combine(datetime.now(), datetime.max.time())
tempo_data = self.db.get_tempo_range(begin, end, "asc")
Expand All @@ -206,7 +203,7 @@ def tempo(self):
)

uniq_id = f"myelectricaldata_{self.usage_point_id}_tempo_tomorrow"
logging.info(f"- {uniq_id}")
logging.info(f"- sensor.{uniq_id}")
begin = begin + timedelta(days=1)
end = end + timedelta(days=1)
tempo_data = self.db.get_tempo_range(begin, end, "asc")
Expand All @@ -229,12 +226,11 @@ def tempo(self):

def ecowatt(self):
uniq_id = f"myelectricaldata_{self.usage_point_id}_ecowatt"
logging.info(f"- {uniq_id}")
logging.info(f"- sensor.{uniq_id}")
end = datetime.combine(datetime.now(), datetime.min.time())
begin = end - timedelta(days=1)
ecowatt_data = self.db.get_ecowatt_range(begin, end, "asc")
if ecowatt_data:
stats = Stat(self.usage_point_id)
max_history = 11
now = datetime.now().replace(minute=0, second=0, microsecond=0)
forecast = {}
Expand All @@ -252,9 +248,7 @@ def ecowatt(self):
forecast[f'{date.strftime("%H")} h'] = value
i = i + 1
attributes = {
"lastSensorCall": datetime.now().strftime(self.date_format_detail),
"forecast": forecast,
"yesterdayDate": stats.daily(0)["begin"],
"begin": begin,
"end": end
}
Expand Down Expand Up @@ -419,28 +413,81 @@ def myelectricaldata_usage_point_id(self, measurement_direction):
# LOG.show(yesterday_last_year)

dailyweek_cost = []

dailyweek_HP = []
dailyweek_costHP = []
dailyweek_HC = []
dailyweek_costHC = []
yesterday_hp_value = 0
yesterday_hp_value_cost = 0
if measurement_direction == "consumption":
daily_cost = 0
if hasattr(self.config, "plan") and self.config.plan.upper() == "HC/HP":
daily_cost = (
convert_kw_to_euro(stats.detail(0, "HC")["value"], self.consumption_price_hc)
+ convert_kw_to_euro(stats.detail(0, "HP")["value"], self.consumption_price_hp)
)
for i in range(7):
value = (
convert_kw_to_euro(stats.detail(i, "HP")["value"], self.consumption_price_hp)
+ convert_kw_to_euro(stats.detail(i, "HC")["value"], self.consumption_price_hc)
hp = stats.detail(i, "HP")["value"]
hc = stats.detail(i, "HC")["value"]
dailyweek_HP.append(convert_kw(hp))
dailyweek_HC.append(convert_kw(hc))
dailyweek_costHP.append(convert_kw_to_euro(hp, self.consumption_price_hp))
dailyweek_costHC.append(convert_kw_to_euro(hc, self.consumption_price_hp))
value = hp + hc
if i == 0:
daily_cost = value
elif i == 1:
yesterday_hp_value_cost = convert_kw_to_euro(hp, self.consumption_price_hp)
dailyweek_cost.append(round(value, 1))
elif hasattr(self.config, "plan") and self.config.plan.upper() == "TEMPO":
tempo_config = CONFIG.tempo_config()
for i in range(7):
tempo_data = stats.tempo(i)["value"]
hp = tempo_data["blue_hp"] + tempo_data["white_hp"] + tempo_data["red_hp"]
hc = tempo_data["blue_hc"] + tempo_data["white_hc"] + tempo_data["red_hc"]
dailyweek_HP.append(convert_kw(hp))
dailyweek_HC.append(convert_kw(hc))
cost_hp = (
convert_kw_to_euro(tempo_data["blue_hp"], tempo_config["price_blue_hp"])
+ convert_kw_to_euro(tempo_data["white_hp"], tempo_config["price_white_hp"])
+ convert_kw_to_euro(tempo_data["red_hp"], tempo_config["price_red_hp"])
)
cost_hc = (
convert_kw_to_euro(tempo_data["blue_hc"], tempo_config["price_blue_hc"])
+ convert_kw_to_euro(tempo_data["white_hc"], tempo_config["price_white_hc"])
+ convert_kw_to_euro(tempo_data["red_hc"], tempo_config["price_red_hc"])
)
dailyweek_costHP.append(cost_hp)
dailyweek_costHC.append(cost_hc)
value = cost_hp + cost_hc
if i == 0:
daily_cost = value
elif i == 1:
yesterday_hp_value_cost = cost_hp
dailyweek_cost.append(round(value, 1))
else:
daily_cost = convert_kw_to_euro(stats.daily(0)["value"], self.consumption_price_base)
for i in range(7):
hp = stats.detail(i, "HP")["value"]
hc = stats.detail(i, "HC")["value"]
dailyweek_HP.append(convert_kw(hp))
dailyweek_HC.append(convert_kw(hc))
dailyweek_costHP.append(convert_kw_to_euro(hp, self.consumption_price_base))
dailyweek_costHC.append(convert_kw_to_euro(hc, self.consumption_price_base))
dailyweek_cost.append(convert_kw_to_euro(stats.daily(i)["value"], self.consumption_price_base))
if i == 0:
daily_cost = convert_kw_to_euro(stats.daily(0)["value"], self.consumption_price_base)
elif i == 1:
yesterday_hp_value_cost = convert_kw_to_euro(hp, self.consumption_price_base)
else:
daily_cost = convert_kw_to_euro(stats.daily(0)["value"], self.production_price)
for i in range(7):
dailyweek_cost.append(convert_kw_to_euro(stats.daily(i)["value"], self.production_price))

if not dailyweek_HP:
dailyweek_HP = [0, 0, 0, 0, 0, 0, 0, 0]
if not dailyweek_costHP:
dailyweek_costHP = [0, 0, 0, 0, 0, 0, 0, 0]
if not dailyweek_HC:
dailyweek_HC = [0, 0, 0, 0, 0, 0, 0, 0]
if not dailyweek_costHC:
dailyweek_costHC = [0, 0, 0, 0, 0, 0, 0, 0]

yesterday_consumption_max_power = 0
if hasattr(self.config, "consumption_max_power") and self.config.consumption_max_power:
yesterday_consumption_max_power = stats.max_power(0)["value"]
Expand Down Expand Up @@ -489,26 +536,12 @@ def myelectricaldata_usage_point_id(self, measurement_direction):
],
"dailyweek_cost": dailyweek_cost,
# TODO : If current_day = 0, dailyweek_hp & dailyweek_hc just next day...
"dailyweek_costHP": [
convert_kw_to_euro(stats.detail(0, "HP")["value"], self.consumption_price_hp),
convert_kw_to_euro(stats.detail(1, "HP")["value"], self.consumption_price_hp),
convert_kw_to_euro(stats.detail(2, "HP")["value"], self.consumption_price_hp),
convert_kw_to_euro(stats.detail(3, "HP")["value"], self.consumption_price_hp),
convert_kw_to_euro(stats.detail(4, "HP")["value"], self.consumption_price_hp),
convert_kw_to_euro(stats.detail(5, "HP")["value"], self.consumption_price_hp),
convert_kw_to_euro(stats.detail(6, "HP")["value"], self.consumption_price_hp),
],
"dailyweek_HP": [
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"]),
],
"dailyweek_costHP": dailyweek_costHP,
"dailyweek_HP": dailyweek_HP,
"dailyweek_costHC": dailyweek_costHC,
"dailyweek_HC": dailyweek_HC,
"daily_cost": daily_cost,
"yesterday_HP_cost": convert_kw_to_euro(yesterday_hp_value, self.consumption_price_hp),
"yesterday_HP_cost": yesterday_hp_value_cost,
"yesterday_HP": convert_kw(yesterday_hp_value),
"day_1_HP": stats.detail(0, "HP")["value"],
"day_2_HP": stats.detail(1, "HP")["value"],
Expand All @@ -517,24 +550,7 @@ def myelectricaldata_usage_point_id(self, measurement_direction):
"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(stats.detail(0, "HC")["value"], self.consumption_price_hc),
convert_kw_to_euro(stats.detail(1, "HC")["value"], self.consumption_price_hc),
convert_kw_to_euro(stats.detail(2, "HC")["value"], self.consumption_price_hc),
convert_kw_to_euro(stats.detail(3, "HC")["value"], self.consumption_price_hc),
convert_kw_to_euro(stats.detail(4, "HC")["value"], self.consumption_price_hc),
convert_kw_to_euro(stats.detail(5, "HC")["value"], self.consumption_price_hc),
convert_kw_to_euro(stats.detail(6, "HC")["value"], self.consumption_price_hc),
],
"dailyweek_HC": [
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.consumption_price_hc),
"yesterday_HC": convert_kw(yesterday_hc_value),
"day_1_HC": stats.detail(0, "HC")["value"],
Expand Down Expand Up @@ -585,12 +601,11 @@ def myelectricaldata_usage_point_id(self, measurement_direction):
"offpeak_hours_enedis": offpeak_hours_enedis,
"offpeak_hours": offpeak_hours,
"subscribed_power": self.subscribed_power,
"info": info
# "info": info
}


uniq_id = f"myelectricaldata_{measurement_direction}_{self.usage_point_id}"
logging.info(f"- {uniq_id}")
logging.info(f"- sensor.{uniq_id}")
self.sensor(
topic=f"myelectricaldata_{measurement_direction}/{self.usage_point_id}",
name=f"{measurement_direction}_{self.usage_point_id}",
Expand All @@ -601,7 +616,7 @@ def myelectricaldata_usage_point_id(self, measurement_direction):
)

uniq_id = f"myelectricaldata_{self.usage_point_id}"
logging.info(f"- {uniq_id}")
logging.info(f"- sensor.{uniq_id}")
self.sensor(
topic=f"myelectricaldata/{self.usage_point_id}",
name=self.usage_point_id,
Expand Down
9 changes: 6 additions & 3 deletions app/models/export_influxdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def daily(self, measurement_direction="consumption"):
price = self.usage_point_config.production_price
logging.info(f'Envoi des données "{measurement_direction.upper()}" dans influxdb')
get_daily_all = self.db.get_daily_all(self.usage_point_id)

get_daily_all_count = len(get_daily_all)
last_data = self.db.get_daily_last_date(self.usage_point_id, measurement_direction)
first_data = self.db.get_daily_first_date(self.usage_point_id, measurement_direction)
if last_data and first_data:
Expand All @@ -48,7 +48,8 @@ def daily(self, measurement_direction="consumption"):
for data in influxdb_data:
for record in data.records:
count += record.get_value()
if len(get_daily_all) != count:
if get_daily_all_count != count:
logging.info(f" Cache : {get_daily_all_count} / InfluxDb : {count}")
for daily in get_daily_all:
date = daily.date
# start = datetime.strftime(date, "%Y-%m-%dT00:00:00Z")
Expand Down Expand Up @@ -85,6 +86,7 @@ def detail(self, measurement_direction="consumption"):
measurement = f"{measurement_direction}_detail"
logging.info(f'Envoi des données "{measurement.upper()}" dans influxdb')
get_detail_all = self.db.get_detail_all(self.usage_point_id, measurement_direction)
get_detail_all_count = len(get_detail_all)
last_data = self.db.get_detail_last_date(self.usage_point_id, measurement_direction)
first_data = self.db.get_detail_first_date(self.usage_point_id, measurement_direction)
if last_data and first_data:
Expand All @@ -98,7 +100,8 @@ def detail(self, measurement_direction="consumption"):

# print(len(get_detail_all))
# print(count)
if len(get_detail_all) != count:
if get_detail_all_count != count:
logging.info(f" Cache : {get_detail_all_count} / InfluxDb : {count}")
for index, detail in enumerate(get_detail_all):
date = detail.date
# start = datetime.strftime(date, self.time_format)
Expand Down
Loading

0 comments on commit 8519a7e

Please sign in to comment.