Esempio n. 1
0
    def update(self):
        """ Gets the latest data and updates the states. """

        time_date = dt_util.utcnow()
        time = dt_util.datetime_to_short_time_str(dt_util.as_local(time_date))
        time_utc = dt_util.datetime_to_short_time_str(time_date)
        date = dt_util.datetime_to_short_date_str(dt_util.as_local(time_date))

        # Calculate the beat (Swatch Internet Time) time without date.
        hours, minutes, seconds = time_date.strftime('%H:%M:%S').split(':')
        beat = ((int(seconds) + (int(minutes) * 60) + ((int(hours) + 1) *
                                                       3600)) / 86.4)

        if self.type == 'time':
            self._state = time
        elif self.type == 'date':
            self._state = date
        elif self.type == 'date_time':
            self._state = date + ', ' + time
        elif self.type == 'time_date':
            self._state = time + ', ' + date
        elif self.type == 'time_utc':
            self._state = time_utc
        elif self.type == 'beat':
            self._state = '{0:.2f}'.format(beat)
Esempio n. 2
0
    def update(self):
        """ Gets the latest data and updates the states. """

        time_date = dt_util.utcnow()
        time = dt_util.datetime_to_short_time_str(dt_util.as_local(time_date))
        time_utc = dt_util.datetime_to_short_time_str(time_date)
        date = dt_util.datetime_to_short_date_str(dt_util.as_local(time_date))

        # Calculate the beat (Swatch Internet Time) time without date.
        hours, minutes, seconds = time_date.strftime('%H:%M:%S').split(':')
        beat = ((int(seconds) + (int(minutes) * 60) +
                 ((int(hours) + 1) * 3600)) / 86.4)

        if self.type == 'time':
            self._state = time
        elif self.type == 'date':
            self._state = date
        elif self.type == 'date_time':
            self._state = date + ', ' + time
        elif self.type == 'time_date':
            self._state = time + ', ' + date
        elif self.type == 'time_utc':
            self._state = time_utc
        elif self.type == 'beat':
            self._state = '{0:.2f}'.format(beat)
    def update(self):
        """ Gets the latest data from opendata.ch. """

        response = get(
            _RESOURCE +
            'connections?' +
            'from=' + self.start + '&' +
            'to=' + self.destination + '&' +
            'fields[]=connections/from/departureTimestamp/&' +
            'fields[]=connections/')

        connections = response.json()['connections'][:2]

        try:
            return [
                dt_util.datetime_to_short_time_str(
                    dt_util.as_local(dt_util.utc_from_timestamp(
                        item['from']['departureTimestamp']))
                )
                for item in connections
            ]
        except KeyError:
            return ['n/a']