Skip to content

Commit

Permalink
rework mqtt data, fix stat, rename sensor,...
Browse files Browse the repository at this point in the history
  • Loading branch information
m4dm4rtig4n committed Jan 3, 2023
1 parent 8c29247 commit e4fd5e8
Show file tree
Hide file tree
Showing 8 changed files with 278 additions and 255 deletions.
2 changes: 1 addition & 1 deletion app/models/ajax.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 16 additions & 0 deletions app/models/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
247 changes: 131 additions & 116 deletions app/models/export_home_assistant.py

Large diffs are not rendered by default.

91 changes: 29 additions & 62 deletions app/models/export_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.")
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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
Expand All @@ -132,19 +130,18 @@ 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
mqtt_data[f"{sub_prefix}/week/{begin_day}/base/kWh"] = round(value / 1000, 2)
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"]
Expand Down Expand Up @@ -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"],
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -283,15 +271,15 @@ 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)
mqtt_data[f"{prefix}/euro"] = round(value_hp / 1000 * price_hp, 2)
# 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"]
Expand All @@ -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)
Expand Down Expand Up @@ -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"],
Expand Down Expand Up @@ -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)

2 changes: 1 addition & 1 deletion app/models/export_mqttv1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
42 changes: 26 additions & 16 deletions app/models/query_address.py
Original file line number Diff line number Diff line change
@@ -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:

Expand All @@ -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."
Expand Down Expand Up @@ -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
Loading

0 comments on commit e4fd5e8

Please sign in to comment.