@@ -221,28 +221,31 @@ def request_power_update(self, callback=None):
221221 if self ._energy_last_collected_timestamp < _timestamp_utcnow .replace (
222222 minute = 0 , second = 0 , microsecond = 0
223223 ):
224- _LOGGER .info ("Queue _last_log_address for %s at %s last_collected %s" ,
225- str (self .mac ),str (self ._last_log_address ),
226- self ._energy_last_collected_timestamp
224+ _LOGGER .info (
225+ "Queue _last_log_address for %s at %s last_collected %s" ,
226+ str (self .mac ),
227+ str (self ._last_log_address ),
228+ self ._energy_last_collected_timestamp ,
227229 )
228230 self ._request_info (self .push_last_log_address )
229231
230232 if len (self ._energy_history_failed_address ) > 0 :
231- _mem_address = self ._energy_history_failed_address .pop (0 )
232- if self ._energy_memory .get (_mem_address , 0 ) < 4 :
233- _LOGGER .info ("Collect EnergyCounters for %s at %s" ,
234- str (self .mac ),
235- str (_mem_address ),
236- )
237- self .request_energy_counters (_mem_address )
238- self ._energy_ratelimit_collection_timestamp = _timestamp_utcnow
239- else :
240- _LOGGER .info (
241- "Drop known request_energy_counters for %s at %s and clock sync" ,
242- str (self .mac ),
243- str (_mem_address ),
244- )
245- self .get_clock (self .sync_clock )
233+ _mem_address = self ._energy_history_failed_address .pop (0 )
234+ if self ._energy_memory .get (_mem_address , 0 ) < 4 :
235+ _LOGGER .info (
236+ "Collect EnergyCounters for %s at %s" ,
237+ str (self .mac ),
238+ str (_mem_address ),
239+ )
240+ self .request_energy_counters (_mem_address )
241+ self ._energy_ratelimit_collection_timestamp = _timestamp_utcnow
242+ else :
243+ _LOGGER .info (
244+ "Drop known request_energy_counters for %s at %s and clock sync" ,
245+ str (self .mac ),
246+ str (_mem_address ),
247+ )
248+ self .get_clock (self .sync_clock )
246249 if datetime .now ().day != self ._last_clock_sync_day :
247250 self ._last_clock_sync_day = datetime .now ().day
248251 self .get_clock (self .sync_clock )
@@ -252,8 +255,7 @@ def push_last_log_address(self):
252255 self ._energy_history_failed_address .append (self ._last_log_address )
253256
254257 def message_for_circle (self , message ):
255- """Process received message
256- """
258+ """Process received message"""
257259 if isinstance (message , CirclePowerUsageResponse ):
258260 if self .calibration :
259261 self ._response_power_usage (message )
@@ -387,8 +389,7 @@ def _response_calibration(self, message: CircleCalibrationResponse):
387389 self .calibration = True
388390
389391 def pulses_to_kws (self , pulses , seconds = 1 ):
390- """Converts the amount of pulses to kWs using the calaboration offsets
391- """
392+ """Converts the amount of pulses to kWs using the calaboration offsets"""
392393 if pulses is None :
393394 return None
394395 if pulses == 0 or not self .calibration :
@@ -428,7 +429,7 @@ def _collect_energy_pulses(self, start_utc: datetime, end_utc: datetime):
428429 )
429430 else :
430431 _mem_address = self ._energy_timestamp_memory_address (_log_timestamp )
431- if ( _mem_address is not None and _mem_address >= 0 ) :
432+ if _mem_address is not None and _mem_address >= 0 :
432433 _LOGGER .info (
433434 "_collect_energy_pulses for %s at %s | %s not found" ,
434435 str (self .mac ),
@@ -443,7 +444,6 @@ def _collect_energy_pulses(self, start_utc: datetime, end_utc: datetime):
443444 str (_mem_address ),
444445 )
445446
446-
447447 # Validate all history values where present
448448 if len (self ._energy_history_failed_address ) == 0 :
449449 return _energy_pulses
@@ -480,12 +480,11 @@ def _update_energy_today_now(self):
480480 and self ._energy_pulses_current_hour is not None
481481 ):
482482 _pulses_today_now = (
483- self ._energy_pulses_today_hourly
484- + self ._energy_pulses_current_hour
483+ self ._energy_pulses_today_hourly + self ._energy_pulses_current_hour
485484 )
486485
487486 _utc_hour_timestamp = datetime .utcnow ().replace (
488- minute = 0 , second = 0 , microsecond = 0
487+ minute = 0 , second = 0 , microsecond = 0
489488 )
490489 _local_hour = datetime .now ().hour
491490 _utc_midnight_timestamp = _utc_hour_timestamp - timedelta (hours = _local_hour )
@@ -500,9 +499,9 @@ def _update_energy_today_now(self):
500499 _utc_hour_timestamp ,
501500 )
502501 elif (
503- self ._energy_pulses_today_now is not None
504- and self ._energy_pulses_today_now > _pulses_today_now
505- and self ._energy_pulses_midnight_rollover < _local_midnight_timestamp
502+ self ._energy_pulses_today_now is not None
503+ and self ._energy_pulses_today_now > _pulses_today_now
504+ and self ._energy_pulses_midnight_rollover < _local_midnight_timestamp
506505 ):
507506 _LOGGER .info (
508507 "_update_energy_today_now for %s midnight rollover started old=%s, new=%s" ,
@@ -518,9 +517,14 @@ def _update_energy_today_now(self):
518517 )
519518 self .do_callback (FEATURE_ENERGY_CONSUMPTION_TODAY ["id" ])
520519 elif (
521- self ._energy_pulses_today_now is not None
522- and self ._energy_pulses_today_now > _pulses_today_now
523- and int ((self ._energy_pulses_today_now - _pulses_today_now )/ (self ._energy_pulses_today_now + 1 )* 100 ) > 1
520+ self ._energy_pulses_today_now is not None
521+ and self ._energy_pulses_today_now > _pulses_today_now
522+ and int (
523+ (self ._energy_pulses_today_now - _pulses_today_now )
524+ / (self ._energy_pulses_today_now + 1 )
525+ * 100
526+ )
527+ > 1
524528 ):
525529 _LOGGER .info (
526530 "_update_energy_today_now for %s hour rollover started old=%s, new=%s" ,
@@ -617,12 +621,14 @@ def _update_energy_today_hourly(self, start_today: datetime, end_today: datetime
617621 def request_energy_counters (self , log_address = None , callback = None ):
618622 """Request power log of specified address"""
619623 _LOGGER .debug (
620- "request_energy_counters for %s of address %s" , str (self .mac ), str (log_address )
624+ "request_energy_counters for %s of address %s" ,
625+ str (self .mac ),
626+ str (log_address ),
621627 )
622628 if not self ._available :
623629 _LOGGER .debug (
624- "Skip request_energy_counters for % is unavailable" ,
625- str (self .mac ),
630+ "Skip request_energy_counters for % is unavailable" ,
631+ str (self .mac ),
626632 )
627633 return
628634 if log_address is None :
@@ -687,11 +693,12 @@ def _response_energy_counters(self, message: CircleEnergyCountersResponse):
687693 message , "pulses%d" % (_slot ,)
688694 ).value
689695
690- _LOGGER .info ("push _energy_memory for %s address %s slot %s stamp %s" ,
691- str (self .mac ),
692- str (message .logaddr .value ),
693- str (_slot ),
694- str (_log_timestamp ),
696+ _LOGGER .info (
697+ "push _energy_memory for %s address %s slot %s stamp %s" ,
698+ str (self .mac ),
699+ str (message .logaddr .value ),
700+ str (_slot ),
701+ str (_log_timestamp ),
695702 )
696703
697704 # Store last populated _slot
@@ -710,9 +717,11 @@ def _response_energy_counters(self, message: CircleEnergyCountersResponse):
710717 self ._last_collected_address_timestamp = _log_timestamp
711718
712719 # Trigger history rollover
713- _LOGGER .info ('history_rollover %s %s %s' ,str (_log_timestamp ),
714- str (_utc_hour_timestamp ),
715- str (self ._energy_last_rollover_timestamp ),
720+ _LOGGER .info (
721+ "history_rollover %s %s %s" ,
722+ str (_log_timestamp ),
723+ str (_utc_hour_timestamp ),
724+ str (self ._energy_last_rollover_timestamp ),
716725 )
717726 if (
718727 _log_timestamp == _utc_hour_timestamp
@@ -739,7 +748,7 @@ def _response_energy_counters(self, message: CircleEnergyCountersResponse):
739748 self ._energy_consumption_today_reset = _local_midnight_timestamp
740749 _midnight_rollover = True
741750 if self ._energy_last_collected_timestamp == datetime .utcnow ().replace (
742- minute = 0 , second = 0 , microsecond = 0
751+ minute = 0 , second = 0 , microsecond = 0
743752 ):
744753 self ._update_energy_previous_hour (_utc_hour_timestamp )
745754 self ._update_energy_today_hourly (
@@ -751,24 +760,31 @@ def _response_energy_counters(self, message: CircleEnergyCountersResponse):
751760 _utc_midnight_timestamp ,
752761 )
753762 else :
754- _LOGGER .info ("CircleEnergyCounter failed for %s at %s|%s count %s" ,
755- str (self .mac ),
756- str (message .logaddr .value ),
757- str (self ._last_log_address ),
758- str (self ._energy_last_collected_count ),
763+ _LOGGER .info (
764+ "CircleEnergyCounter failed for %s at %s|%s count %s" ,
765+ str (self .mac ),
766+ str (message .logaddr .value ),
767+ str (self ._last_log_address ),
768+ str (self ._energy_last_collected_count ),
759769 )
760- self ._energy_last_collected_count += 1
770+ self ._energy_last_collected_count += 1
761771
762772 if (
763- message .logaddr .value == self ._last_log_address
764- and self ._energy_last_collected_count > 3
773+ message .logaddr .value == self ._last_log_address
774+ and self ._energy_last_collected_count > 3
765775 ):
766- if self ._energy_history_failed_address .count (self ._last_log_address - 1 ) == 0 :
767- self ._energy_history_failed_address .append (self ._last_log_address - 1 )
776+ if (
777+ self ._energy_history_failed_address .count (
778+ self ._last_log_address - 1
779+ )
780+ == 0
781+ ):
782+ self ._energy_history_failed_address .append (
783+ self ._last_log_address - 1
784+ )
768785 _LOGGER .info ("Resetting CircleEnergyCounter due to logaddress offset" )
769786
770-
771- # Cleanup energy history for more than 48 hours
787+ # Cleanup energy history for more than 48 hours
772788 _48_hours_ago = datetime .utcnow ().replace (
773789 minute = 0 , second = 0 , microsecond = 0
774790 ) - timedelta (hours = 48 )
@@ -814,7 +830,6 @@ def set_clock(self, callback=None):
814830 callback ,
815831 )
816832
817-
818833 def sync_clock (self , max_drift = 0 ):
819834 """Resync clock of node if time has drifted more than MAX_TIME_DRIFT"""
820835 if self ._clock_offset is not None :
@@ -832,7 +847,7 @@ def _energy_timestamp_memory_address(self, utc_timestamp: datetime):
832847 """Return memory address for given energy counter timestamp"""
833848 if self ._last_collected_address is None :
834849 return None
835- #Should already be hour timestamp, but just to be sure.
850+ # Should already be hour timestamp, but just to be sure.
836851 _utc_now_timestamp = self ._last_collected_address_timestamp .replace (
837852 minute = 0 , second = 0 , microsecond = 0
838853 )
@@ -855,12 +870,13 @@ def _energy_timestamp_memory_address(self, utc_timestamp: datetime):
855870 _address -= 1
856871 _slot = 4
857872 _hours += 1
858- _LOGGER .info ("Calculated address %s at %s from %s at %s with %s|%s" ,
859- _address ,
860- utc_timestamp ,
861- self ._last_log_address ,
862- _utc_now_timestamp ,
863- _sslot ,
864- _hours_offset ,
873+ _LOGGER .info (
874+ "Calculated address %s at %s from %s at %s with %s|%s" ,
875+ _address ,
876+ utc_timestamp ,
877+ self ._last_log_address ,
878+ _utc_now_timestamp ,
879+ _sslot ,
880+ _hours_offset ,
865881 )
866882 return _address
0 commit comments