Skip to content

Commit 39f52c4

Browse files
author
OSi (Ondrej Sienczak)
committed
Removed deprecated 2.5 onecall API
1 parent 85a725e commit 39f52c4

File tree

1 file changed

+13
-109
lines changed

1 file changed

+13
-109
lines changed

simulator/forecast.py

Lines changed: 13 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,7 @@ class Forecast:
103103
def __init__(self, connection, **kw):
104104
logger.info("Reading forecast data")
105105
self._read1(connection, **kw)
106-
107-
if api["variant"] == 2:
108-
self._read2_short(connection)
109-
else:
110-
self._read2_long(connection, 96)
106+
self._read2(connection, api["variant"] * 24)
111107

112108
self.home = Forecast.Home(kw["in_temp"], kw["in_humi"])
113109
self._get_status()
@@ -117,46 +113,19 @@ def _mk_id(fid, rain):
117113
return fid if fid != 500 or rain < 2 else 520
118114

119115
def _read1(self, connection, **kw):
120-
# Download hourly weather forecast for today
121-
url = "http://api.openweathermap.org/data/2.5/onecall?lat={}&lon={}&APPID={}&mode=json&units={}&lang={}&exclude={}"
122-
fcast = connection.http_get_json(
116+
# Download weather now for today
117+
url = "https://api.openweathermap.org/data/2.5/weather?lat={}&lon={}&appid={}&mode=json&units={}&lang={}"
118+
current = connection.http_get_json(
123119
url.format(
124120
location["locations"][connection.config["location"]]["lat"],
125121
location["locations"][connection.config["location"]]["lon"],
126122
api["apikey"],
127123
api["units"],
128124
api["language"],
129-
"minutely,hourly,daily",
130125
)
131126
)
132127

133128
# Parse todays forecast
134-
try:
135-
if not fcast["cod"] == 0:
136-
logger.info("Server commu8nication error - can not load forecast!")
137-
138-
try:
139-
logger.info("Server reported:")
140-
logger.info(" ", fcast["message"])
141-
logger.info("")
142-
logger.info("Go into configuration mode to set server correctly")
143-
except:
144-
...
145-
146-
play(
147-
(400, 1000),
148-
(200, 1000),
149-
(400, 1000),
150-
(200, 1000),
151-
(400, 1000),
152-
(200, 1000),
153-
)
154-
deepsleep()
155-
except:
156-
...
157-
158-
current = fcast["current"]
159-
160129
try:
161130
rain = current["rain"]["1h"]
162131
except KeyError:
@@ -169,7 +138,7 @@ def _read1(self, connection, **kw):
169138
except KeyError:
170139
snow = 0.0
171140

172-
self.time_zone = fcast["timezone_offset"]
141+
self.time_zone = current["timezone"]
173142

174143
weather = current["weather"][0]
175144
dsc = weather["description"]
@@ -184,17 +153,17 @@ def _mk_id(fid, rain):
184153
id2icon[self._mk_id(weather["id"], rain)], weather["icon"][-1]
185154
),
186155
current["dt"],
187-
current["sunrise"],
188-
current["sunset"],
189-
current["temp"],
156+
current["sys"]["sunrise"],
157+
current["sys"]["sunset"],
158+
current["main"]["temp"],
190159
kw.get("out_temp", None),
191-
current["feels_like"],
160+
current["main"]["feels_like"],
192161
kw.get("out_humi", None) or current["humidity"],
193162
rain,
194163
rpb,
195164
snow,
196-
current["wind_speed"],
197-
current["wind_deg"],
165+
current["wind"]["speed"],
166+
current["wind"]["deg"],
198167
current["clouds"],
199168
)
200169
self.time = Time(self.time_zone)
@@ -204,74 +173,9 @@ def _mk_id(fid, rain):
204173
dt = self.time.get_date_time(self.weather.dt)
205174
rtc.init((dt[0], dt[1], dt[2], 0, dt[3], dt[4], dt[5], 0))
206175

207-
def _read2_short(self, connection):
208-
# Download hourly weather forecast for today
209-
url = "http://api.openweathermap.org/data/2.5/onecall?lat={}&lon={}&APPID={}&mode=json&units={}&lang={}&exclude={}"
210-
fcast = connection.http_get_json(
211-
url.format(
212-
location["locations"][connection.config["location"]]["lat"],
213-
location["locations"][connection.config["location"]]["lon"],
214-
api["apikey"],
215-
api["units"],
216-
"EN",
217-
"current,minutely,daily",
218-
)
219-
)
220-
221-
# Build 2 days forecast
222-
self.forecast = []
223-
self.step = 3600
224-
srt = self.weather.srt
225-
sst = self.weather.sst
226-
227-
for current in fcast["hourly"]:
228-
weather = current["weather"][0]
229-
230-
try:
231-
rain = current["rain"]["1h"]
232-
except KeyError:
233-
rain = 0.0
234-
235-
rpb = current.get("pop", 0) * 100
236-
237-
try:
238-
snow = current["snow"]["1h"]
239-
except KeyError:
240-
snow = 0.0
241-
242-
dt = current["dt"]
243-
if dt > sst:
244-
srt += 86400
245-
sst += 86400
246-
247-
id = (
248-
701
249-
if current.get("visibility", 0) < 500
250-
and weather["id"] in range(800, 802)
251-
else weather["id"]
252-
)
253-
self.forecast.append(
254-
Forecast.Weather(
255-
"{}{}".format(id2icon[self._mk_id(id, rain)], weather["icon"][-1]),
256-
dt,
257-
srt,
258-
sst,
259-
current["temp"],
260-
None,
261-
current["feels_like"],
262-
current["humidity"],
263-
rain,
264-
rpb,
265-
snow,
266-
current["wind_speed"],
267-
current["wind_deg"],
268-
current["clouds"],
269-
)
270-
)
271-
272-
def _read2_long(self, connection, hours):
176+
def _read2(self, connection, hours):
273177
# Download hourly weather forecast for 5 days
274-
url = "http://api.openweathermap.org/data/2.5/forecast?lat={}&lon={}&APPID={}&mode=json&units={}&lang={}&cnt={}"
178+
url = "http://api.openweathermap.org/data/2.5/forecast?lat={}&lon={}&appid={}&mode=json&units={}&lang={}&cnt={}"
275179
fcast = connection.http_get_json(
276180
url.format(
277181
location["locations"][connection.config["location"]]["lat"],

0 commit comments

Comments
 (0)