@@ -39,8 +39,8 @@ def __init__(self, adapter, _id, station_id, unit):
3939
4040 self .get_station_info ()
4141
42- self .next_high_tide = None
43- self .next_low_tide = None
42+ self .high_tides = []
43+ self .low_tides = []
4444
4545 if self .have_tide_predictions :
4646 self .properties ['lowTideTime' ] = TideCalendarProperty (
@@ -113,6 +113,23 @@ def __init__(self, adapter, _id, station_id, unit):
113113 False
114114 )
115115
116+ self .properties ['status' ] = TideCalendarProperty (
117+ self ,
118+ 'status' ,
119+ {
120+ 'title' : 'Status' ,
121+ 'type' : 'string' ,
122+ 'enum' : [
123+ 'low' ,
124+ 'high' ,
125+ 'rising' ,
126+ 'falling' ,
127+ ],
128+ 'readOnly' : True ,
129+ },
130+ ''
131+ )
132+
116133 if self .have_water_levels :
117134 self ._type = ['MultiLevelSensor' ]
118135 self .properties ['currentLevel' ] = TideCalendarProperty (
@@ -204,7 +221,7 @@ def poll(self):
204221 'product' : 'predictions' ,
205222 'application' : 'NOS.COOPS.TAC.WL' ,
206223 'begin_date' : '{}{:02d}{:02d}' .format (now .year , now .month , now .day ), # noqa
207- 'range' : '24 ' ,
224+ 'range' : '36 ' ,
208225 'datum' : 'MLLW' ,
209226 'station' : '{}' .format (self .station_id ),
210227 'time_zone' : 'lst_ldt' ,
@@ -224,6 +241,9 @@ def poll(self):
224241 print ('Invalid tide prediction data for station {}'
225242 .format (self .station_id ))
226243 else :
244+ self .high_tides = []
245+ self .low_tides = []
246+
227247 set_high = False
228248 set_low = False
229249
@@ -233,27 +253,42 @@ def poll(self):
233253 '%Y-%m-%d %H:%M'
234254 )
235255
236- if parsed >= now :
237- if prediction ['type' ] == 'H' and not set_high :
238- self .next_high_tide = parsed
239- set_high = True
256+ if parsed < now :
257+ continue
258+
259+ level = round (float (prediction ['v' ]), 1 )
260+ timestamp = prediction ['t' ].split (' ' )[1 ]
261+
262+ if prediction ['type' ] == 'H' :
263+ self .high_tides .append ({
264+ 'datetime' : parsed ,
265+ 'level' : level ,
266+ 'timestamp' : timestamp ,
267+ })
240268
269+ if not set_high :
241270 self .properties ['highTideLevel' ].update (
242- round ( float ( prediction [ 'v' ]), 1 )
271+ level
243272 )
244273 self .properties ['highTideTime' ].update (
245- prediction [ 't' ]. split ( ' ' )[ 1 ]
274+ timestamp
246275 )
247- elif prediction ['type' ] == 'L' and not set_low :
248- self .next_low_tide = parsed
249- set_low = True
250-
276+ set_high = True
277+ elif prediction ['type' ] == 'L' :
278+ self .low_tides .append ({
279+ 'datetime' : parsed ,
280+ 'level' : level ,
281+ 'timestamp' : timestamp ,
282+ })
283+
284+ if not set_low :
251285 self .properties ['lowTideLevel' ].update (
252- round ( float ( prediction [ 'v' ]), 1 )
286+ level
253287 )
254288 self .properties ['lowTideTime' ].update (
255- prediction [ 't' ]. split ( ' ' )[ 1 ]
289+ timestamp
256290 )
291+ set_low = True
257292
258293 if self .have_water_levels :
259294 url = 'https://tidesandcurrents.noaa.gov/api/datagetter'
@@ -300,14 +335,59 @@ def check_events(self):
300335 microsecond = 0
301336 )
302337
303- if self .next_high_tide is not None :
304- if now == self .next_high_tide :
338+ status_set = False
339+
340+ if len (self .high_tides ) > 0 :
341+ next_high_tide = self .high_tides [0 ]['datetime' ]
342+
343+ if now == next_high_tide :
305344 self .properties ['highTide' ].update (True )
345+ self .properties ['status' ].update ('high' )
346+ status_set = True
306347 else :
348+ if next_high_tide < now :
349+ self .high_tides .pop (0 )
350+
351+ if len (self .high_tides ) > 0 :
352+ next_high_tide = self .high_tides [0 ]
353+ self .properties ['highTideLevel' ].update (
354+ next_high_tide ['level' ]
355+ )
356+ self .properties ['highTideTime' ].update (
357+ next_high_tide ['timestamp' ]
358+ )
359+
307360 self .properties ['highTide' ].update (False )
308361
309- if self .next_low_tide is not None :
310- if now == self .next_low_tide :
362+ if len (self .low_tides ) > 0 :
363+ next_low_tide = self .low_tides [0 ]['datetime' ]
364+
365+ if now == next_low_tide :
311366 self .properties ['lowTide' ].update (True )
367+ self .properties ['status' ].update ('low' )
368+ status_set = True
312369 else :
370+ if next_low_tide < now :
371+ self .low_tides .pop (0 )
372+
373+ if len (self .low_tides ) > 0 :
374+ next_low_tide = self .low_tides [0 ]
375+ self .properties ['lowTideLevel' ].update (
376+ next_low_tide ['level' ]
377+ )
378+ self .properties ['lowTideTime' ].update (
379+ next_low_tide ['timestamp' ]
380+ )
381+
313382 self .properties ['lowTide' ].update (False )
383+
384+ if len (self .high_tides ) > 0 and \
385+ len (self .low_tides ) > 0 and \
386+ not status_set :
387+ next_low_tide = self .low_tides [0 ]
388+ next_high_tide = self .high_tides [0 ]
389+
390+ if next_low_tide ['datetime' ] < next_high_tide ['datetime' ]:
391+ self .properties ['status' ].update ('falling' )
392+ else :
393+ self .properties ['status' ].update ('rising' )
0 commit comments