diff --git a/custom_components/apiEnedis/apiEnedis.py b/custom_components/apiEnedis/apiEnedis.py index 4e254f1..5538ecc 100644 --- a/custom_components/apiEnedis/apiEnedis.py +++ b/custom_components/apiEnedis/apiEnedis.py @@ -49,9 +49,10 @@ def __init__(self, token, PDL_ID, delai = 3600, heuresCreuses = None, \ pass def myLog(self, message): - pass #self._log.info(message) #self._log.warning(message) + pass + def myLogWarning(self, message): self._log.warning(message) @@ -392,6 +393,7 @@ def createMultiDaysHCHP(self, data): self._joursHP={} dateDuJour = (datetime.date.today()).strftime("%Y-%m-%d") for x in data["meter_reading"]["interval_reading"]: + self._interval_length = x["interval_length"] date = x["date"][:10] heure = x["date"][11:16] if ( heure == "00:00" ): # alors sur la veille, var c'est la fin de la tranche du jour precedent @@ -410,6 +412,10 @@ def createMultiDaysHCHP(self, data): else: self._joursHC[date] +=int(x["value"]) * self.getCoeffIntervalLength() # car c'est pas en heure + # if ( date == "2020-12-06" ): + # print("ici", x["date"], heure, " ", heurePleine, ", ", x[ "value" ], \ + # self._joursHP[date], self._joursHC[date], self._joursHP[date] + self._joursHC[date]) + #print(self._joursHC) #print(self._joursHP) @@ -440,10 +446,10 @@ def createHCHP(self, data): #print(self._HP) def updateDataYesterdayHCHP(self, data=None): - self.updateLastMethodCall("updateDataHCHP") - self.myLog("--updateDataHCHP --") + self.updateLastMethodCall("updateDataYesterdayHCHP") + self.myLog("--updateDataYesterdayHCHP --") if (data == None): data = self.CallgetDataYesterdayHCHP() - self.myLog("updateDataHCHP : data %s" % (data)) + self.myLog("updateDataYesterdayHCHP : data %s" % (data)) self.checkData(data) self.createHCHP(data) @@ -612,6 +618,7 @@ def updateErrorLastCall(self, errorMessage): def getDelai(self): return self._delai def getDelaiIsGood(self): + self.myLogWarning("TimeLastCall : %s" %(self.getTimeLastCall())) ecartOk = ( datetime.datetime.now() - self.getTimeLastCall()).seconds > self.getDelai() return ecartOk @@ -623,7 +630,8 @@ def update(self): self.updateLastMethodCall("") try: self.setUpdateRealise( True ) - self.myLogWarning( "myEnedis ...%s update lancé, status precedent : %s" % (self.get_PDL_ID(), self.getStatusLastCall())) + self.myLogWarning( "myEnedis ...%s update lancé, status precedent : %s, lastCall :%s" \ + % (self.get_PDL_ID(), self.getStatusLastCall(), self.getLastMethodCallError())) if ( self.isConsommation()): self._niemeAppel += 1 if (self.getStatusLastCall() or self.getLastMethodCallError() == "updateYesterday"): @@ -647,7 +655,18 @@ def update(self): if (self.getStatusLastCall() or self.getLastMethodCallError() == "updateCurrentYear"): self.updateCurrentYear() if (self.getStatusLastCall() or self.getLastMethodCallError() == "updateDataYesterdayHCHP"): - self.updateDataYesterdayHCHP() + try: + self.updateDataYesterdayHCHP() + except Exception as inst: + if ( inst.args[:3] == ('call', 'error', 'no_data_found')): # gestion que c'est pas une erreur de contrat trop recent ? + # si le service ne repond pas, l'erreur pas grave + self.updateErrorLastCall( "%s"%(self.getLastAnswer())) + pass + else: + raise Exception(inst) + + # if (self.getStatusLastCall() or self.getLastMethodCallError() == "updateDataYesterdayHCHP"): + # self.updateDataYesterdayHCHP() if (self.getStatusLastCall() or self.getLastMethodCallError() == "updateLastYear"): self.updateLastYear() if (self.getStatusLastCall() or self.getLastMethodCallError() == "updateLastMonthLastYear"): diff --git a/custom_components/apiEnedis/sensor.py b/custom_components/apiEnedis/sensor.py index 5208daf..06ed607 100644 --- a/custom_components/apiEnedis/sensor.py +++ b/custom_components/apiEnedis/sensor.py @@ -31,7 +31,7 @@ ICON = "mdi:package-variant-closed" -__VERSION__ = "1.0.5.0" +__VERSION__ = "1.0.5.1" SCAN_INTERVAL = timedelta(seconds=1800)# interrogation enedis ? DEFAUT_DELAI_INTERVAL = 7200 # interrogation faite toutes 2 les heures @@ -94,7 +94,7 @@ def __init__(self, session, name, interval, myDataEnedis): self._session = session self._name = name self._myDataEnedis = myDataEnedis - self._attributes = None + self._attributes = {} self._state = None self._unit = "kWh" self.update = Throttle(interval)(self._update) @@ -113,6 +113,8 @@ def setAttributesFromLastAttributes(self): if ( self._lastAttributes != None ) : self._attributes = self._lastAttributes.copy() return self._attributes + else: + return {} @property def name(self): diff --git a/custom_components/apiEnedis/sensorEnedis.py b/custom_components/apiEnedis/sensorEnedis.py index b05fe31..90bdbdb 100644 --- a/custom_components/apiEnedis/sensorEnedis.py +++ b/custom_components/apiEnedis/sensorEnedis.py @@ -9,6 +9,7 @@ def manageSensorState( _myDataEnedis,_LOGGER = None, version = None ): # si pas de mises à jour alors juste return !! return if (_myDataEnedis.getStatusLastCall()): # update avec statut ok + #if( 1 ): # try: status_counts["typeCompteur"] = _myDataEnedis.getTypePDL() if ( _myDataEnedis.isConsommation()): @@ -19,10 +20,10 @@ def manageSensorState( _myDataEnedis,_LOGGER = None, version = None ): status_counts['yesterday'] = _myDataEnedis.getYesterday() status_counts['last_week'] = _myDataEnedis.getLastWeek() - last7days = _myDataEnedis.getLast7Days() - for day in last7days: - status_counts['day_%s' % (day["niemejour"])] = day["value"] - status_counts['daily'] = [("{:.2f}".format(day["value"] * 0.001)) for day in last7days] + #last7days = _myDataEnedis.getLast7Days() + #for day in last7days: + # status_counts['day_%s' % (day["niemejour"])] = day["value"] + #status_counts['daily'] = [("{:.2f}".format(day["value"] * 0.001)) for day in last7days] last7daysHP = _myDataEnedis.get7DaysHP() listeClef = list(last7daysHP.keys()) @@ -80,6 +81,15 @@ def manageSensorState( _myDataEnedis,_LOGGER = None, version = None ): dailyHP.append(last7daysHP[clef]) status_counts['dailyweek_HP'] = [("{:.3f}".format(0.001 * day_HP)) for day_HP in dailyHP] + niemejour = 0 + daily = [] + for clef in listeClef: + niemejour += 1 + somme = last7daysHP[clef] + last7daysHC[clef] + status_counts['day_%s' %(niemejour)] = somme + daily.append(somme) + status_counts['daily'] = [("{:.2f}".format(0.001 * day)) for day in daily] + status_counts["halfhourly"] = [] status_counts["offpeak_hours"] = _myDataEnedis.getYesterdayHC() * 0.001 status_counts["peak_hours"] = _myDataEnedis.getYesterdayHP() * 0.001 @@ -104,9 +114,9 @@ def manageSensorState( _myDataEnedis,_LOGGER = None, version = None ): status_counts['last_year'] = _myDataEnedis.getLastYear() * 0.001 status_counts['current_year'] = _myDataEnedis.getCurrentYear() * 0.001 status_counts['errorLastCall'] = _myDataEnedis.getErrorLastCall() - if ((_myDataEnedis.getLastMonthLastYear() != None) and + if ((_myDataEnedis.getLastMonthLastYear() is not None) and (_myDataEnedis.getLastMonthLastYear() != 0) and - (_myDataEnedis.getLastMonth() != None)): + (_myDataEnedis.getLastMonth() is not None)): status_counts["monthly_evolution"] = \ ((_myDataEnedis.getLastMonth() - _myDataEnedis.getLastMonthLastYear()) / _myDataEnedis.getLastMonthLastYear()) * 100 diff --git a/custom_components/apiEnedis/testEnedis.py b/custom_components/apiEnedis/testEnedis.py index 09089f0..7d6bb0d 100644 --- a/custom_components/apiEnedis/testEnedis.py +++ b/custom_components/apiEnedis/testEnedis.py @@ -90,7 +90,7 @@ def testMulti(): mon_conteneur.read("../../../myCredential/security.txt") #for qui in ["ENEDIS","ENEDIS2","ENEDIS3","ENEDIS4"]: #for qui in ["ENEDIS","ENEDIS7"]: - for qui in ["ENEDIS8"]: + for qui in ["ENEDIS"]: token = mon_conteneur[qui]['TOKEN'] PDL_ID = mon_conteneur[qui]['CODE'] print(token, PDL_ID) @@ -107,7 +107,7 @@ def testMono(): import configparser mon_conteneur = configparser.ConfigParser() mon_conteneur.read("../../../myCredential/security.txt") - qui = "ENEDIS8" + qui = "ENEDIS" token = mon_conteneur[qui]['TOKEN'] PDL_ID = mon_conteneur[qui]['CODE'] print(token, PDL_ID)