44
55import logging
66import re
7- from collections .abc import Generator
87from dataclasses import dataclass , field
98from typing import TYPE_CHECKING
109
@@ -230,19 +229,17 @@ class TimeRWSensor(RWSensor):
230229
231230 def available_values (self , step_minutes : int , state : InverterState ) -> list [str ]:
232231 """Get the available values for this sensor."""
233- full_day = 24 * 60
234-
235- min_val = (
236- SSTime (string = str (state .get (self .min , "0:00" ))).minutes if self .min else 0
237- )
238- max_val = (
239- SSTime (string = str (state .get (self .max , "24:00" ))).minutes
240- if self .max
241- else full_day
242- )
243- val = SSTime (string = str (state .get (self , "0:00" ))).minutes
244- time_range = self ._range (min_val , max_val , val , step_minutes , full_day )
245- return list (map (lambda m : SSTime (minutes = m ).str_value , time_range ))
232+ day = list (range (0 , 24 * 60 , step_minutes ))
233+ minv = SSTime (strv = str (state .get (self .min , "0:00" ))).minutes if self .min else 0
234+ maxv = SSTime (strv = str (state .get (self .max , "0:00" ))).minutes if self .max else 0
235+ if minv >= maxv :
236+ maxv += 24 * 60
237+ opt = [minv , * [v for v in day if (v > minv and v < maxv )], maxv ]
238+ val = SSTime (strv = str (state .get (self , "0:00" ))).minutes
239+ if val not in opt :
240+ opt .append (val )
241+ opt .sort ()
242+ return [SSTime (minutes = m ).str_value for m in opt ]
246243
247244 @property
248245 def dependencies (self ) -> list [Sensor ]:
@@ -251,22 +248,10 @@ def dependencies(self) -> list[Sensor]:
251248
252249 def reg_to_value (self , regs : RegType ) -> ValType :
253250 """Decode the time from a register."""
254- return SSTime (register = regs [0 ]).str_value
251+ return SSTime (regv = regs [0 ]).str_value
255252
256253 def value_to_reg (self , value : ValType , state : InverterState ) -> RegType :
257254 """Get the reg value from a display value."""
258255 if not self .address :
259256 raise NotImplementedError ("Cannot write to a sensor with no address" )
260- return self .reg (SSTime (string = str (value )).reg_value )
261-
262- @staticmethod
263- def _range (
264- start : int , end : int , val : int , step : int , modulo : int
265- ) -> Generator [int ]:
266- if val % step != 0 :
267- yield val
268- stop = end if start <= end else end + modulo
269- for i in range (start , stop , step ):
270- yield i % modulo
271- if start == end or start != end % modulo :
272- yield end
257+ return self .reg (SSTime (strv = str (value )).reg_value )
0 commit comments