def weatherStationData(auth, requests_mock): with open("fixtures/weatherstation_data_simple.json") as f: json_fixture = json.load(f) requests_mock.post( pyatmo.weather_station._GETSTATIONDATA_REQ, json=json_fixture, headers={"content-type": "application/json"}, ) return pyatmo.WeatherStationData(auth)
def test_WeatherStationData_no_data(auth, requests_mock): with open("fixtures/home_data_empty.json") as f: json_fixture = json.load(f) requests_mock.post( pyatmo.weather_station._GETSTATIONDATA_REQ, json=json_fixture, headers={"content-type": "application/json"}, ) with pytest.raises(pyatmo.NoDevice): assert pyatmo.WeatherStationData(auth)
def update(self): """Call the Netatmo API to update the data.""" import pyatmo self.station_data = pyatmo.WeatherStationData(self.auth) if self.station is not None: self.data = self.station_data.lastData( station=self.station, exclude=3600) else: self.data = self.station_data.lastData(exclude=3600)
def update(self): """Call the Netatmo API to update the data. This method is not throttled by the builtin Throttle decorator but with a custom logic, which takes into account the time of the last update from the cloud. """ if time() < self._next_update or \ not self._update_in_progress.acquire(False): return try: import pyatmo try: self.station_data = pyatmo.WeatherStationData(self.auth) except TypeError: _LOGGER.error("Failed to connect to NetAtmo") return # finally statement will be executed if self.station is not None: self.data = self.station_data.lastData( station=self.station, exclude=3600) else: self.data = self.station_data.lastData(exclude=3600) newinterval = 0 for module in self.data: if 'When' in self.data[module]: newinterval = self.data[module]['When'] break if newinterval: # Try and estimate when fresh data will be available newinterval += NETATMO_UPDATE_INTERVAL - time() if newinterval > NETATMO_UPDATE_INTERVAL - 30: newinterval = NETATMO_UPDATE_INTERVAL else: if newinterval < NETATMO_UPDATE_INTERVAL / 2: # Never hammer the NetAtmo API more than # twice per update interval newinterval = NETATMO_UPDATE_INTERVAL / 2 _LOGGER.info( "NetAtmo refresh interval reset to %d seconds", newinterval) else: # Last update time not found, fall back to default value newinterval = NETATMO_UPDATE_INTERVAL self._next_update = time() + newinterval finally: self._update_in_progress.release()
def get_values(authorization): values = {} try: weatherData = pyatmo.WeatherStationData(authorization) weatherDataMap = weatherData.lastData() for station in weatherDataMap: for sensor in weatherDataMap[station]: if sensor in sensors_to_use: values["{}/{}".format( station, sensor)] = weatherDataMap[station][sensor] except Exception as e: print("exception {}".format(e)) return values
def test_WeatherStationData_no_response(auth, requests_mock): requests_mock.post(pyatmo.weather_station._GETSTATIONDATA_REQ, text="None") with pytest.raises(pyatmo.NoDevice): assert pyatmo.WeatherStationData(auth)
def fetch_weather_data(): """This method will fetch the weather data from Netatmo""" ssm = boto3.client('ssm') # Need to get the secrets for use with the Netatmo API which are held in AWS Systems Manager. # You will need a client id and client secret from Netatmo which can be obtained when you # register your app with them https://dev.netatmo.com/apps/createanapp#form secrets = ssm.get_parameters(Names=[ 'Netatmo_Client_Id', 'Netatmo_Username', 'Netatmo_Client_Secret', 'Netatmo_Password' ], WithDecryption=True) # Parse the secrets returned from AWS Systems Manager parsed_secrets = parse_secrets_parameters(secrets) # Now authorise against Netatmo authorization = pyatmo.ClientAuth( client_id=parsed_secrets['Netatmo_Client_Id'], client_secret=parsed_secrets['Netatmo_Client_Secret'], username=parsed_secrets['Netatmo_Username'], password=parsed_secrets['Netatmo_Password'], ) # Request the weather station data from Netatmo # The response contains the latest readings from the base station, # Outside modules and all modules indoors. metric_data = [] weather_data = pyatmo.WeatherStationData(authorization) # It is possible to own more than one base station for station_key in weather_data.stations: station = weather_data.stations[station_key] # For each metric associated with the station that we want to send to CloudWatch # we append each metric to the list append_metric_data(metric_data, "Temperature", "Temperature", station) append_metric_data(metric_data, "CO2", "CO2", station) append_metric_data(metric_data, "Humidity", "Humidity", station) append_metric_data(metric_data, "Noise", "Noise", station) append_metric_data(metric_data, "Air_Pressure", "Pressure", station) # Now iterate over the modules; these are the indoors and outdoors modules. # Please note that the wind module has yet to be added. for module in station["modules"]: # Battery is dead, no wifi signal or there is a problem with the module. # In this case the module is not reporting any data so there is nothing to collect. if not module["reachable"]: continue # The rf_status is the signal strength. # Please refer to https://dev.netatmo.com/apidocumentation/weather # for an explaination of the values. append_metric_data(metric_data, "Signal_Strength", "rf_status", module) # The battery_vp is a number to indicate the health of the battery. # Depending on the module, the values will mean a different status. # Please refer to https://dev.netatmo.com/apidocumentation/weather # for an explaination of the values. append_metric_data(metric_data, "Battery_Status", "battery_vp", module) # This is for indoor and outdoor temperature modules if "Temperature" in module["data_type"]: append_metric_data(metric_data, "Temperature", "Temperature", module) append_metric_data(metric_data, "Humidity", "Humidity", module) # Only the indoor modules have CO2 sensor if "CO2" in module["data_type"]: append_metric_data(metric_data, "CO2", "CO2", module) # Rain gauage if "Rain" in module["data_type"]: append_metric_data(metric_data, "Rain", "Rain", module) append_metric_data(metric_data, "Rain_1_hour", "sum_rain_1", module) append_metric_data(metric_data, "Rain_24_hours", "sum_rain_24", module) return metric_data