diff --git a/custom_components/fpl/fplapi.py b/custom_components/fpl/fplapi.py index eb2796a..320b9fa 100644 --- a/custom_components/fpl/fplapi.py +++ b/custom_components/fpl/fplapi.py @@ -145,85 +145,91 @@ class FplApi(object): dailyAvg = float(projectedBillData["dailyAvg"]) avgHighTemp = int(projectedBillData["avgHighTemp"]) - url = ( - "https://app.fpl.com/wps/PA_ESFPortalWeb/getDailyConsumption" - f"?premiseNumber={premise}" - f"&startDate={currentBillDate.strftime('%Y%m%d')}" - f"&endDate={dt.today().strftime('%Y%m%d')}" - f"&accountNumber={account}" - # "&accountType=ELE" - f"&zipCode={zip_code}" - "&consumption=0.0" - "&usage=0.0" - "&isMultiMeter=false" - f"&lastAvailableDate={dt.today()}" - # "&isAmiMeter=true" - "&userType=EXT" - # "¤tReading=64359" - "&isResidential=true" - # "&isTouUser=false" - "&showGroupData=false" - # "&isNetMeter=false" - "&certifiedDate=1900/01/01" - # "&acctNetMeter=false" - "&tempType=max" - "&viewType=dollar" - "&ecDayHumType=NoHum" - # "&ecHasMoveInRate=false" - # "&ecMoveInRateVal=" - # "&lastAvailableIeeDate=20191230" - ) + try: - async with async_timeout.timeout(TIMEOUT, loop=self._loop): - response = await self._session.get(url) - - if response.status != 200: - self.data = None - return - - malformedXML = await response.read() - - cleanerXML = ( - str(malformedXML) - .replace('', "", 1) - .split("@@", 1)[0] - ) - - total_kw = 0 - total_cost = 0 - days = 0 - - soup = BeautifulSoup(cleanerXML, "html.parser") - items = soup.find("dataset", seriesname="$").find_all("set") - - details = [] - - for item in items: - # - match = re.search( - r"Date: (\w\w\w. \d\d, \d\d\d\d).*\{br\}kWh Usage: (.*?) kWh \{br\}.*Cost:\s\$([\w|.]+).*Temp:\s(\d+)", - str(item), + url = ( + "https://app.fpl.com/wps/PA_ESFPortalWeb/getDailyConsumption" + f"?premiseNumber={premise}" + f"&startDate={currentBillDate.strftime('%Y%m%d')}" + f"&endDate={dt.today().strftime('%Y%m%d')}" + f"&accountNumber={account}" + # "&accountType=ELE" + f"&zipCode={zip_code}" + "&consumption=0.0" + "&usage=0.0" + "&isMultiMeter=false" + f"&lastAvailableDate={dt.today()}" + # "&isAmiMeter=true" + "&userType=EXT" + # "¤tReading=64359" + "&isResidential=true" + # "&isTouUser=false" + "&showGroupData=false" + # "&isNetMeter=false" + "&certifiedDate=1900/01/01" + # "&acctNetMeter=false" + "&tempType=max" + "&viewType=dollar" + "&ecDayHumType=NoHum" + # "&ecHasMoveInRate=false" + # "&ecMoveInRateVal=" + # "&lastAvailableIeeDate=20191230" ) - if match: - g1 = match.group(1).replace(".", "") - date = datetime.strptime(g1, "%b %d, %Y").date() - usage = int(match.group(2)) - cost = float(match.group(3)) - max_temp = int(match.group(4)) - if usage == 0: - cost = 0 - total_kw += usage - total_cost += cost - days += 1 + async with async_timeout.timeout(TIMEOUT, loop=self._loop): + response = await self._session.get(url) - day_detail = {} - day_detail["date"] = str(date) - day_detail["usage"] = usage - day_detail["cost"] = cost - day_detail["max_temperature"] = max_temp + if response.status != 200: + self.data = None + return - details.append(day_detail) + malformedXML = await response.read() + + cleanerXML = ( + str(malformedXML) + .replace('', "", 1) + .split("@@", 1)[0] + ) + + total_kw = 0 + total_cost = 0 + days = 0 + + soup = BeautifulSoup(cleanerXML, "html.parser") + items = soup.find("dataset", seriesname="$").find_all("set") + + details = [] + + for item in items: + # + match = re.search( + r"Date: (\w\w\w. \d\d, \d\d\d\d).*\{br\}kWh Usage: (.*?) kWh \{br\}.*Cost:\s\$([\w|.]+).*Temp:\s(\d+)", + str(item), + ) + if match: + g1 = match.group(1).replace(".", "") + date = datetime.strptime(g1, "%b %d, %Y").date() + usage = int(match.group(2)) + cost = float(match.group(3)) + max_temp = int(match.group(4)) + if usage == 0: + cost = 0 + + total_kw += usage + total_cost += cost + days += 1 + + day_detail = {} + day_detail["date"] = str(date) + day_detail["usage"] = usage + day_detail["cost"] = cost + day_detail["max_temperature"] = max_temp + + details.append(day_detail) + + data["details"] = details + except: + data["details"] = [] remaining_days = serviceDays - asOfDays avg_kw = round(total_kw / days, 0) @@ -239,6 +245,6 @@ class FplApi(object): data["remaining_days"] = remaining_days data["mtd_kwh"] = total_kw data["average_kwh"] = avg_kw - data["details"] = details + return data