def from_dict(cls, json_dict): """ Static method to create 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 last_changed: last_changed = date_util.str_to_datetime(last_changed) last_updated = json_dict.get("last_updated") if last_updated: last_updated = date_util.str_to_datetime(last_updated) return cls(json_dict["entity_id"], json_dict["state"], json_dict.get("attributes"), last_changed, last_updated)
def update(self): """ Gets 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.str_to_datetime( time_entry['@from'], "%Y-%m-%dT%H:%M:%SZ") valid_to = dt_util.str_to_datetime( time_entry['@to'], "%Y-%m-%dT%H:%M:%SZ") 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 == ('temperature', 'pressure', 'humidity', 'dewpointTemperature'): self._state = loc_data[self.type]['@value'] break elif self.type == 'windSpeed': 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
def update(self): """ Gets 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.str_to_datetime(time_entry['@from'], "%Y-%m-%dT%H:%M:%SZ") valid_to = dt_util.str_to_datetime(time_entry['@to'], "%Y-%m-%dT%H:%M:%SZ") 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 == ('temperature', 'pressure', 'humidity', 'dewpointTemperature'): self._state = loc_data[self.type]['@value'] break elif self.type == 'windSpeed': 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
def from_dict(cls, json_dict): """ Static method to create 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 last_changed: last_changed = date_util.str_to_datetime(last_changed) last_updated = json_dict.get('last_updated') if last_updated: last_updated = date_util.str_to_datetime(last_updated) return cls(json_dict['entity_id'], json_dict['state'], json_dict.get('attributes'), last_changed, last_updated)
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 last_changed: last_changed = dt_util.str_to_datetime(last_changed) last_updated = json_dict.get('last_updated') if last_updated: last_updated = dt_util.str_to_datetime(last_updated) return cls(json_dict['entity_id'], json_dict['state'], json_dict.get('attributes'), last_changed, last_updated)
def update(self): """ Gets 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.str_to_datetime(time_entry["@from"], "%Y-%m-%dT%H:%M:%SZ") valid_to = dt_util.str_to_datetime(time_entry["@to"], "%Y-%m-%dT%H:%M:%SZ") 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 == ("temperature", "pressure", "humidity", "dewpointTemperature"): self._state = loc_data[self.type]["@value"] break elif self.type == "windSpeed": 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
def next_rising_utc(hass, entity_id=None): """ Returns the 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.str_to_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
def update(self): """ Gets 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.str_to_datetime(model['@nextrun'], "%Y-%m-%dT%H:%M:%SZ")
def test_str_to_datetime_returns_none_for_incorrect_format(self): """Test str_to_datetime returns None if incorrect format.""" self.assertIsNone(dt_util.str_to_datetime("not a datetime string"))
def test_str_to_datetime_converts_correctly(self): """Test str_to_datetime converts strings.""" self.assertEqual( datetime(1986, 7, 9, 12, 0, 0, tzinfo=dt_util.UTC), dt_util.str_to_datetime("12:00:00 09-07-1986"))