예제 #1
0
파일: test_dt.py 프로젝트: bdfoster/blumate
    def test_parse_datetime_converts_correctly(self):
        """Test parse_datetime converts strings."""
        assert \
            datetime(1986, 7, 9, 12, 0, 0, tzinfo=dt_util.UTC) == \
            dt_util.parse_datetime("1986-07-09T12:00:00Z")

        utcnow = dt_util.utcnow()

        assert utcnow == dt_util.parse_datetime(utcnow.isoformat())
예제 #2
0
파일: yr.py 프로젝트: bdfoster/blumate
    def update(self):
        """Get the latest data from yr.no and updates the states."""
        now = dt_util.utcnow()
        # Check if data should be updated
        if self._update is not None and now <= self._update:
            return

        self._weather.update()

        # Find sensor
        for time_entry in self._weather.data['product']['time']:
            valid_from = dt_util.parse_datetime(time_entry['@from'])
            valid_to = dt_util.parse_datetime(time_entry['@to'])

            loc_data = time_entry['location']

            if self.type not in loc_data or now >= valid_to:
                continue

            self._update = valid_to

            if self.type == 'precipitation' and valid_from < now:
                self._state = loc_data[self.type]['@value']
                break
            elif self.type == 'symbol' and valid_from < now:
                self._state = loc_data[self.type]['@number']
                break
            elif self.type in ('temperature', 'pressure', 'humidity',
                               'dewpointTemperature'):
                self._state = loc_data[self.type]['@value']
                break
            elif self.type in ('windSpeed', 'windGust'):
                self._state = loc_data[self.type]['@mps']
                break
            elif self.type == 'windDirection':
                self._state = float(loc_data[self.type]['@deg'])
                break
            elif self.type in ('fog', 'cloudiness', 'lowClouds',
                               'mediumClouds', 'highClouds'):
                self._state = loc_data[self.type]['@percent']
                break
예제 #3
0
파일: yr.py 프로젝트: lumavp/blumate
    def update(self):
        """Get the latest data from yr.no and updates the states."""
        now = dt_util.utcnow()
        # Check if data should be updated
        if self._update is not None and now <= self._update:
            return

        self._weather.update()

        # Find sensor
        for time_entry in self._weather.data['product']['time']:
            valid_from = dt_util.parse_datetime(time_entry['@from'])
            valid_to = dt_util.parse_datetime(time_entry['@to'])

            loc_data = time_entry['location']

            if self.type not in loc_data or now >= valid_to:
                continue

            self._update = valid_to

            if self.type == 'precipitation' and valid_from < now:
                self._state = loc_data[self.type]['@value']
                break
            elif self.type == 'symbol' and valid_from < now:
                self._state = loc_data[self.type]['@number']
                break
            elif self.type in ('temperature', 'pressure', 'humidity',
                               'dewpointTemperature'):
                self._state = loc_data[self.type]['@value']
                break
            elif self.type in ('windSpeed', 'windGust'):
                self._state = loc_data[self.type]['@mps']
                break
            elif self.type == 'windDirection':
                self._state = float(loc_data[self.type]['@deg'])
                break
            elif self.type in ('fog', 'cloudiness', 'lowClouds',
                               'mediumClouds', 'highClouds'):
                self._state = loc_data[self.type]['@percent']
                break
예제 #4
0
    def from_dict(cls, json_dict):
        """Initialize a state from a dict.

        Ensures: state == State.from_json_dict(state.to_json_dict())
        """
        if not (json_dict and 'entity_id' in json_dict and
                'state' in json_dict):
            return None

        last_changed = json_dict.get('last_changed')

        if isinstance(last_changed, str):
            last_changed = dt_util.parse_datetime(last_changed)

        last_updated = json_dict.get('last_updated')

        if isinstance(last_updated, str):
            last_updated = dt_util.parse_datetime(last_updated)

        return cls(json_dict['entity_id'], json_dict['state'],
                   json_dict.get('attributes'), last_changed, last_updated)
예제 #5
0
파일: sun.py 프로젝트: lumavp/blumate
def next_rising_utc(hass, entity_id=None):
    """UTC datetime object of the next sun rising."""
    entity_id = entity_id or ENTITY_ID

    state = hass.states.get(ENTITY_ID)

    try:
        return dt_util.parse_datetime(state.attributes[STATE_ATTR_NEXT_RISING])
    except (AttributeError, KeyError):
        # AttributeError if state is None
        # KeyError if STATE_ATTR_NEXT_RISING does not exist
        return None
예제 #6
0
파일: yr.py 프로젝트: bdfoster/blumate
    def update(self):
        """Get the latest data from yr.no."""
        # Check if new will be available
        if self._nextrun is not None and dt_util.utcnow() <= self._nextrun:
            return
        try:
            with requests.Session() as sess:
                response = sess.get(self._url)
        except requests.RequestException:
            return
        if response.status_code != 200:
            return
        data = response.text

        import xmltodict
        self.data = xmltodict.parse(data)['weatherdata']
        model = self.data['meta']['model']
        if '@nextrun' not in model:
            model = model[0]
        self._nextrun = dt_util.parse_datetime(model['@nextrun'])
예제 #7
0
파일: yr.py 프로젝트: lumavp/blumate
    def update(self):
        """Get the latest data from yr.no."""
        # Check if new will be available
        if self._nextrun is not None and dt_util.utcnow() <= self._nextrun:
            return
        try:
            with requests.Session() as sess:
                response = sess.get(self._url)
        except requests.RequestException:
            return
        if response.status_code != 200:
            return
        data = response.text

        import xmltodict
        self.data = xmltodict.parse(data)['weatherdata']
        model = self.data['meta']['model']
        if '@nextrun' not in model:
            model = model[0]
        self._nextrun = dt_util.parse_datetime(model['@nextrun'])
예제 #8
0
파일: test_dt.py 프로젝트: bdfoster/blumate
 def test_parse_datetime_returns_none_for_incorrect_format(self):
     """Test parse_datetime returns None if incorrect format."""
     self.assertIsNone(dt_util.parse_datetime("not a datetime string"))