Skip to content

Commit

Permalink
Merge pull request #486 from MyElectricalData/fix/tempo_days
Browse files Browse the repository at this point in the history
Fix/tempo days
  • Loading branch information
m4dm4rtig4n committed Feb 5, 2024
2 parents a2562d7 + bdee3e1 commit 2ed1ca6
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 5 deletions.
1 change: 1 addition & 0 deletions .github/workflows/semantic_prerelease.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ jobs:
PRE_RELEASE: true

Trig-Generate-Pre-Release:
needs: ["Semantic-Pre-Release"]
runs-on: ubuntu-latest
steps:
- name: Repository Dispatch
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/semantic_release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ jobs:
PRE_RELEASE: false

Trig-Generate-Release:
needs: ["Semantic-Release"]
runs-on: ubuntu-latest
steps:
- name: Repository Dispatch
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ debug: init enable_debug up bootstrap down

## Start all external ressource necessary to debug (MQTT, InfluxDB,...)
up:
docker compose -f dev/docker-compose.dev.yaml start
cd dev; docker compose up --force-recreate --detach; cd -

## Stop all external ressource necessary to debug (MQTT, InfluxDB,...)
down:
Expand Down
11 changes: 11 additions & 0 deletions src/models/export_home_assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,17 @@ def tempo_days(self):
self.tempo_days_sensor(f"{color}", days)

def tempo_days_sensor(self, color, days):
"""
Add a sensor to Home Assistant with the given name and state.
Args:
color (str): The color of the tempo (e.g. blue, white, red).
days (int): The number of days in the tempo.
Returns:
None
"""
uniq_id = f"myelectricaldata_tempo_days_{color}"
self.sensor(
topic=f"myelectricaldata_edf/tempo_days_{color}",
Expand Down
4 changes: 2 additions & 2 deletions src/models/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,8 @@ def get_tempo(self):
try:
title(f"Récupération des données Tempo :")
Tempo().fetch()
title(f"Récupération des jours Tempo :")
Tempo().fetch_day()
title(f"Calcul des jours Tempo :")
Tempo().calc_day()
title(f"Récupération des tarifs Tempo :")
Tempo().fetch_price()
export_finish()
Expand Down
32 changes: 31 additions & 1 deletion src/models/query_tempo.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
import logging
import traceback
from datetime import datetime
from datetime import datetime, timedelta

from dateutil.relativedelta import relativedelta

Expand All @@ -18,6 +18,11 @@ def __init__(self):
self.url = URL
self.valid_date = datetime.combine(datetime.now() + relativedelta(days=1), datetime.min.time())
self.nb_check_day = 31
self.total_tempo_days = {
"red": 22,
"white": 43,
"blue": 300,
}

def run(self):
start = (datetime.now() - relativedelta(years=3)).strftime("%Y-%m-%d")
Expand Down Expand Up @@ -79,6 +84,31 @@ def fetch(self):
return "OK"
return result

def calc_day(self):
"""
Calculates the number of days left for each color based on the current date.
Args:
None
Returns:
A dictionary containing the number of days left for each color.
"""
now = datetime.now()
begin = datetime.combine(now.replace(month=9, day=1), datetime.min.time())
if now < begin:
begin = begin.replace(year=int(now.strftime("%Y")) - 1)
end = datetime.combine(begin - timedelta(hours=5), datetime.max.time()).replace(
year=int(begin.strftime("%Y")) + 1
)
current_tempo_day = self.db.get_tempo_range(begin=begin, end=end)
result = self.total_tempo_days
for day in current_tempo_day:
result[day.color.lower()] -= 1
self.db.set_tempo_config("days", result)
return result

def fetch_day(self):
target = f"{self.url}/edf/tempo/days"
query_response = Query(endpoint=target).get()
Expand Down
2 changes: 1 addition & 1 deletion src/models/stat.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def tempo_color(self, index=0):
end = datetime.combine(begin, datetime.max.time())
value = ""
for data in self.db.get_tempo_range(begin, end):
logging.info(f"tempo data: {data}")
logging.debug(f"tempo data: {data}")
value = value + data.color
return {
"value": value,
Expand Down

0 comments on commit 2ed1ca6

Please sign in to comment.