11"""MEL API access."""
22from datetime import datetime , timedelta
33from typing import Any , Dict , List , Optional
4+ from aiohttp import ClientResponseError , ContentTypeError
45
56from aiohttp import ClientSession
67
@@ -172,14 +173,26 @@ async def fetch_device_units(self, device) -> Optional[Dict[Any, Any]]:
172173
173174 User provided info such as indoor/outdoor unit model names and
174175 serial numbers.
176+ If the request returns 403, then ignore it
175177 """
176- async with self ._session .post (
177- f"{ BASE_URL } /Device/ListDeviceUnits" ,
178- headers = _headers (self ._token ),
179- json = {"deviceId" : device .device_id },
180- raise_for_status = True ,
181- ) as resp :
182- return await resp .json ()
178+ try :
179+ async with self ._session .post (
180+ f"{ BASE_URL } /Device/ListDeviceUnits" ,
181+ headers = _headers (self ._token ),
182+ json = {"deviceId" : device .device_id }
183+ ) as resp :
184+ resp .raise_for_status ()
185+ try :
186+ data = await resp .json ()
187+ except ContentTypeError :
188+ return None
189+ if isinstance (data , dict ):
190+ return data
191+ return None
192+ except ClientResponseError as e :
193+ if e .status == 403 :
194+ return None
195+ raise
183196
184197 async def fetch_device_state (self , device ) -> Optional [Dict [Any , Any ]]:
185198 """Fetch state information of a device.
@@ -197,23 +210,35 @@ async def fetch_device_state(self, device) -> Optional[Dict[Any, Any]]:
197210 return await resp .json ()
198211
199212 async def fetch_energy_report (self , device ) -> Optional [Dict [Any , Any ]]:
200- """Fetch energy report containing today and 1-2 days from the past."""
213+ """Fetch energy report containing today and 1-2 days from the past.
214+ If the request returns 403, then ignore it"""
201215 device_id = device .device_id
202216 from_str = (datetime .today () - timedelta (days = 2 )).strftime ("%Y-%m-%d" )
203217 to_str = (datetime .today () + timedelta (days = 2 )).strftime ("%Y-%m-%d" )
204218
205- async with self ._session .post (
206- f"{ BASE_URL } /EnergyCost/Report" ,
207- headers = _headers (self ._token ),
208- json = {
209- "DeviceId" : device_id ,
210- "UseCurrency" : False ,
211- "FromDate" : f"{ from_str } T00:00:00" ,
212- "ToDate" : f"{ to_str } T00:00:00"
213- },
214- raise_for_status = True ,
215- ) as resp :
216- return await resp .json ()
219+ try :
220+ async with self ._session .post (
221+ f"{ BASE_URL } /EnergyCost/Report" ,
222+ headers = _headers (self ._token ),
223+ json = {
224+ "DeviceId" : device_id ,
225+ "UseCurrency" : False ,
226+ "FromDate" : f"{ from_str } T00:00:00" ,
227+ "ToDate" : f"{ to_str } T00:00:00"
228+ }
229+ ) as resp :
230+ resp .raise_for_status ()
231+ try :
232+ data = await resp .json ()
233+ except ContentTypeError :
234+ return None
235+ if isinstance (data , dict ):
236+ return data
237+ return None
238+ except ClientResponseError as e :
239+ if e .status == 403 :
240+ return None
241+ raise
217242
218243 async def set_device_state (self , device ):
219244 """Update device state.
0 commit comments