|
| 1 | +"""Tide calendar adapter for Mozilla WebThings Gateway.""" |
| 2 | + |
| 3 | +from gateway_addon import Adapter, Database |
| 4 | + |
| 5 | +from .tide_calendar_device import TideCalendarDevice, StationException |
| 6 | + |
| 7 | + |
| 8 | +class TideCalendarAdapter(Adapter): |
| 9 | + """Adapter for NOAA tide calendars.""" |
| 10 | + |
| 11 | + def __init__(self, verbose=False): |
| 12 | + """ |
| 13 | + Initialize the object. |
| 14 | +
|
| 15 | + verbose -- whether or not to enable verbose logging |
| 16 | + """ |
| 17 | + self.name = self.__class__.__name__ |
| 18 | + Adapter.__init__(self, |
| 19 | + 'tide-calendar-adapter', |
| 20 | + 'tide-calendar-adapter', |
| 21 | + verbose=verbose) |
| 22 | + |
| 23 | + self.pairing = False |
| 24 | + self.start_pairing() |
| 25 | + |
| 26 | + def start_pairing(self, timeout=None): |
| 27 | + """ |
| 28 | + Start the pairing process. |
| 29 | +
|
| 30 | + timeout -- Timeout in seconds at which to quit pairing |
| 31 | + """ |
| 32 | + if self.pairing: |
| 33 | + return |
| 34 | + |
| 35 | + self.pairing = True |
| 36 | + |
| 37 | + database = Database('tide-calendar-adapter') |
| 38 | + if not database.open(): |
| 39 | + return |
| 40 | + |
| 41 | + config = database.load_config() |
| 42 | + database.close() |
| 43 | + |
| 44 | + if not config or 'stations' not in config or 'unit' not in config: |
| 45 | + return |
| 46 | + |
| 47 | + unit = config['unit'] |
| 48 | + |
| 49 | + for station in config['stations']: |
| 50 | + _id = 'tide-calendar-{}'.format(station) |
| 51 | + if _id not in self.devices: |
| 52 | + try: |
| 53 | + device = TideCalendarDevice(self, _id, station, unit) |
| 54 | + except StationException as e: |
| 55 | + print(e) |
| 56 | + else: |
| 57 | + self.handle_device_added(device) |
| 58 | + |
| 59 | + self.pairing = False |
| 60 | + |
| 61 | + def cancel_pairing(self): |
| 62 | + """Cancel the pairing process.""" |
| 63 | + self.pairing = False |
0 commit comments