Skip to content

Commit

Permalink
adds sensor yesterdaycost
Browse files Browse the repository at this point in the history
  • Loading branch information
saniho committed Jan 2, 2021
1 parent 2493044 commit 2c67d5a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
4 changes: 3 additions & 1 deletion custom_components/apiEnedis/myEnedis.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ def createMultiDaysHCHP(self, data):

def getIntervalLength(self):
return self._interval_length

def getCoeffIntervalLength(self):
interval = self.getIntervalLength()
coeff = 1
Expand Down Expand Up @@ -472,7 +473,7 @@ def updateDataYesterdayHCHP(self, data=None):
if (data == None): data = self.CallgetDataYesterdayHCHP()
self.myLog("updateDataYesterdayHCHP : data %s" % (data))
if ( self.checkData( data )):
self.checkData(data)
self.createHCHP(data)
else:
return

Expand Down Expand Up @@ -505,6 +506,7 @@ def checkDataContract(self, dataAnswer ):
if ("error" in dataAnswer.keys()):
#self.myLogWarning( "** %s" %(dataAnswer["error"]))
raise Exception( 'call' , "error", dataAnswer["error"] )
return True

def getLastMonth(self):
return self._lastMonth
Expand Down
11 changes: 8 additions & 3 deletions custom_components/apiEnedis/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
from collections import defaultdict
from datetime import timedelta
import datetime

import voluptuous as vol

Expand Down Expand Up @@ -230,6 +231,7 @@ def __init__(self, coordinator):
interval = timedelta(seconds=120)
self.update = Throttle(interval)(self._update)
self._lastState = None
self._lastCall = None

"""
@property
Expand Down Expand Up @@ -288,12 +290,15 @@ def _update_state(self):
"""Update sensors state."""
self._attributes = {ATTR_ATTRIBUTION: "" }
status_counts, state = self._myDataEnedis.myEnedis.getStatusYesterdayCost()
if ( state != "unavailable"):
if ( state != self._state ):
if state != "unavailable":
today = datetime.datetime.now().strftime("%Y%m%d") # sans les heures et minutes ;)
if ( self._lastCall != today ):
status_counts["timeLastCall"] = today
self._attributes.update(status_counts)
self._state = state
self._lastCall = today
else:
# donnée identique au jour precedent ....# vior faire autrement avec la date ?
# pas de nouvelle donnée
return
else:
# donnée non disponible
Expand Down
8 changes: 2 additions & 6 deletions custom_components/apiEnedis/sensorEnedis.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ def getStatusYesterdayCost(self):
state = "unavailable"
status_counts = defaultdict(int)
status_counts["version"] = self.version
self._LOGGER.info("getStatusYesterdayCost" )
if (self._myDataEnedis.getContract() != None):
if ( self._myDataEnedis.getStatusLastCall()):
status_counts["timeLastCall"] = datetime.datetime.now()
if self._myDataEnedis.getContract() != None:
if self._myDataEnedis.getStatusLastCall():
status_counts["yesterday_HC_cost"] = \
"{:.3f}".format(0.001 * self._myDataEnedis.getHCCost(self._myDataEnedis.getYesterdayHC()))
status_counts["yesterday_HP_cost"] = \
Expand All @@ -48,8 +46,6 @@ def getStatusYesterdayCost(self):
)
status_counts["daily_cost"] = daily_cost
state = daily_cost
self._LOGGER.info("getStatusYesterdayCost - done" )

return status_counts, state

def getStatus(self):
Expand Down
3 changes: 2 additions & 1 deletion custom_components/apiEnedis/testEnedis.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ def testMulti():
#for qui in ["ENEDIS","ENEDIS7"]:
#for qui in ["ENEDIS9"]:
#for qui in ["ENEDIS","ENEDIS2","ENEDIS3","ENEDIS4","ENEDIS15"]:
for qui in ["ENEDIS18"]:
#for qui in ["ENEDIS18"]:
for qui in ["ENEDIS"]:
token = mon_conteneur[qui]['TOKEN']
PDL_ID = mon_conteneur[qui]['CODE']
print(qui , "*", token, PDL_ID)
Expand Down

0 comments on commit 2c67d5a

Please sign in to comment.