tronikos / opower-hacs

HACS integration for PG&E via Opower
10 stars 2 forks source link

Regex failing with PECO #11

Closed Garulf closed 1 year ago

Garulf commented 1 year ago
Traceback (most recent call last):
  File "/Volumes/homeassistant/.venv/lib/python3.10/site-packages/homeassistant/helpers/update_coordinator.py", line 283, in _async_refresh
    self.data = await self._async_update_data()
  File "/Volumes/HomeAssistant/custom_components/opower/coordinator.py", line 67, in _async_update_data
    await self.api.async_login()
  File "/Volumes/homeassistant/.venv/lib/python3.10/site-packages/opower/opower.py", line 151, in async_login
    self.access_token = await self.utility.async_login(
  File "/Volumes/homeassistant/.venv/lib/python3.10/site-packages/opower/utilities/exelon.py", line 44, in async_login
    settings = json.loads(re.search(r"var SETTINGS = ({.*});", result).group(1))
Garulf commented 1 year ago

I installed the opower package and ran similar code to debug and it works.

Not sure why it's having an issue with this bit of code.

import asyncio
import json
import re

import aiohttp
LOGIN_DOMAIN = "secure.peco.com"
USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"

async def main():
    session = aiohttp.ClientSession()
    async with session.get(
        "https://" + LOGIN_DOMAIN + "/Pages/Login.aspx?/login",
        headers={"User-Agent": USER_AGENT},
        raise_for_status=True,
    ) as resp:
        result = await resp.text()
    settings = json.loads(re.search(r"var SETTINGS = ({.*});", result).group(1))
    print(settings)
    await session.close()

# run
asyncio.run(main())
{
    "allowAutoFocusOnPasswordField": true,
    "api": "CombinedSigninAndSignup",
    "config": {
        "announceVerCompleteMsg": "True",
        "enableRememberMe": "false",
        "forgotPasswordLinkLocation": "AfterInput",
        "forgotPasswordLinkOverride": "ForgotPasswordExchange",
        "includePasswordRequirements": "true",
        "operatingMode": "Email",
        "sendHintOnSignup": "False",
        "showSignupLink": "false"
    },
    "csrf": "XXXXXXXXXXXX",
    "hosts": {
        "policy": "B2C_1A_SignIn",
        "static": "https://secure1.peco.com/static/",
        "tenant": "/euazurepeco.onmicrosoft.com/B2C_1A_SignIn"
    },
    "isPageViewIdSentWithHeader": false,
    "locale": {
        "lang": "en"
    },
    "pageMode": 1,
    "pageViewId": "b2387482-94bb-420e-8191-bc6899dfac98",
    "remoteResource": "https://xzeepb2clookuppsta001.blob.core.windows.net/peco/aadb2ccustomui/unified.html",
    "retryLimit": 3,
    "suppressElementCss": false,
    "transId": "StateProperties=XXXXXXXXXXXXXXXX",
    "trimSpacesInPassword": true,
    "xhrSettings": {
        "retryDelay": 200,
        "retryEnabled": true,
        "retryExponent": 2,
        "retryMaxAttempts": 3,
        "retryOn": [
            "error",
            "timeout"
        ]
    }
}
Garulf commented 1 year ago

I manually edited the opower package the integration is using to log the page its getting when trying to login.

This is what it loads:

<!doctype html>
<html lang="en">
<head>
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  <meta charset="utf-8">

  <title>Accounts</title>
  <base href="/accounts/">

  <meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="/accounts/styles.6d67bd2c5a96a87deee8.css"></head>
<body>

  <app-root></app-root>
  <app-redirect></app-redirect>
<script src="/accounts/runtime-es2015.7d74949e1062d3e292a5.js" type="module"></script><script src="/accounts/runtime-es5.7d74949e1062d3e292a5.js" nomodule defer></script><script src="/accounts/polyfills-es5.bd1b4941665e9ce02e3c.js" nomodule defer></script><script src="/accounts/polyfills-es2015.f683d9b05c0834362594.js" type="module"></script><script src="/accounts/main-es2015.32bde0e5bffc28d6ef06.js" type="module"></script><script src="/accounts/main-es5.32bde0e5bffc28d6ef06.js" nomodule defer></script></body>
</html>
tronikos commented 1 year ago

Thanks for going the extra mile and including the response! That hinted me that the session was already signed in. I fixed the underlying library and I have https://github.com/home-assistant/core/pull/97752 to fix the official integration. I guess I can update this custom integration one last time before I archive it.

tronikos commented 1 year ago

Fixed in the latest release which as I said will likely be the last update of this custom integration.

Garulf commented 1 year ago

Thanks for going the extra mile and including the response!

No problem, thanks for taking the time to create this integration!

That hinted me that the session was already signed in. I fixed the underlying library and I have home-assistant/core#97752 to fix the official integration. I guess I can update this custom integration one last time before I archive it.

So that's what's going on? I couldn't figure out why my test script and the integration would be receiving completely different pages in the same virtual env. I never thought of the session already being logged in. Nice catch!

Fixed in the latest release which as I said will likely be the last update of this custom integration.

Thanks again!

Garulf commented 1 year ago

I upgraded the custom integration and I no longer receive any exceptions regarding regex.

I am however getting 403 errors.

2023-08-04 07:41:52.187 WARNING (MainThread) [homeassistant.config_entries] Config entry 'PECO Energy Company (PECO) (XXXXX@XXXX.com)' for opower integration not ready yet: 403, message='Forbidden', url=URL('https://peco.opower.com/ei/edge/apis/multi-account-v1/cws/peco/customers?offset=0&batchSize=100&addressFilter='); Retrying in background

Since support for this custom integration has stopped where is the best place for issues on this?

EDIT: Looks like the issue might be on PECO's side: (Not sure why it says I am not authorized.) image

tronikos commented 1 year ago

Best place to report issues is at https://github.com/home-assistant/core/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+opower%22

If it's an issue of the underlying library then https://github.com/tronikos/opower/issues

In your particular case the best place seems to be your utility.