11"""MEL API access."""
22from datetime import datetime , timedelta
33from typing import Any , Dict , List , Optional
4+ from aiohttp import ClientResponseError
45
56from aiohttp import ClientSession
67
@@ -172,14 +173,23 @@ 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+ data = await resp .json ()
186+ if isinstance (data , dict ):
187+ return data
188+ return None
189+ except ClientResponseError as e :
190+ if e .status == 403 :
191+ return None
192+ raise
183193
184194 async def fetch_device_state (self , device ) -> Optional [Dict [Any , Any ]]:
185195 """Fetch state information of a device.
@@ -197,23 +207,32 @@ async def fetch_device_state(self, device) -> Optional[Dict[Any, Any]]:
197207 return await resp .json ()
198208
199209 async def fetch_energy_report (self , device ) -> Optional [Dict [Any , Any ]]:
200- """Fetch energy report containing today and 1-2 days from the past."""
210+ """Fetch energy report containing today and 1-2 days from the past.
211+ If the request returns 403, then ignore it"""
201212 device_id = device .device_id
202213 from_str = (datetime .today () - timedelta (days = 2 )).strftime ("%Y-%m-%d" )
203214 to_str = (datetime .today () + timedelta (days = 2 )).strftime ("%Y-%m-%d" )
204215
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 ()
216+ try :
217+ async with self ._session .post (
218+ f"{ BASE_URL } /EnergyCost/Report" ,
219+ headers = _headers (self ._token ),
220+ json = {
221+ "DeviceId" : device_id ,
222+ "UseCurrency" : False ,
223+ "FromDate" : f"{ from_str } T00:00:00" ,
224+ "ToDate" : f"{ to_str } T00:00:00"
225+ }
226+ ) as resp :
227+ resp .raise_for_status ()
228+ data = await resp .json ()
229+ if isinstance (data , dict ):
230+ return data
231+ return None
232+ except ClientResponseError as e :
233+ if e .status == 403 :
234+ return None
235+ raise
217236
218237 async def set_device_state (self , device ):
219238 """Update device state.
0 commit comments