Skip to content

Commit 7acd94d

Browse files
authored
Merge pull request #198 from siphu/main
Fix login issue with #196
2 parents 12534d1 + 1b66670 commit 7acd94d

File tree

5 files changed

+27
-15
lines changed

5 files changed

+27
-15
lines changed

custom_components/generac/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
from homeassistant.config_entries import ConfigEntry
1010
from homeassistant.core import HomeAssistant
1111
from homeassistant.exceptions import ConfigEntryNotReady
12-
from homeassistant.helpers.aiohttp_client import async_get_clientsession
1312

1413
from .api import GeneracApiClient
14+
from .utils import async_client_session
1515
from .const import CONF_PASSWORD
1616
from .const import CONF_USERNAME
1717
from .const import DOMAIN
@@ -32,7 +32,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
3232
username = entry.data.get(CONF_USERNAME, "")
3333
password = entry.data.get(CONF_PASSWORD, "")
3434

35-
session = async_get_clientsession(hass)
35+
session = await async_client_session(hass)
3636
client = GeneracApiClient(username, password, session)
3737

3838
coordinator = GeneracDataUpdateCoordinator(hass, client=client, config_entry=entry)

custom_components/generac/config_flow.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import voluptuous as vol
55
from homeassistant import config_entries
66
from homeassistant.core import callback
7-
from homeassistant.helpers.aiohttp_client import async_create_clientsession
87

98
from .api import GeneracApiClient
9+
from .utils import async_client_session
1010
from .api import InvalidCredentialsException
1111
from .const import CONF_OPTIONS
1212
from .const import CONF_PASSWORD
@@ -68,7 +68,7 @@ async def _show_config_form(self, user_input): # pylint: disable=unused-argumen
6868
async def _test_credentials(self, username, password):
6969
"""Return true if credentials is valid."""
7070
try:
71-
session = async_create_clientsession(self.hass)
71+
session = await async_client_session(self.hass)
7272
client = GeneracApiClient(username, password, session)
7373
await client.async_get_data()
7474
return None

custom_components/generac/utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""Helper to create an aiohttp client session for the Generac API."""
2+
3+
from aiohttp import ClientSession, CookieJar
4+
5+
from homeassistant.core import HomeAssistant
6+
from homeassistant.helpers import aiohttp_client
7+
8+
9+
async def async_client_session(hass: HomeAssistant) -> ClientSession:
10+
"""Return a new aiohttp session."""
11+
return aiohttp_client.async_create_clientsession(
12+
hass, cookie_jar=CookieJar(unsafe=True, quote_cookie=False)
13+
)
14+

local_test.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,28 @@
33
import json
44
import logging
55
import os
6-
76
import aiohttp
87
from custom_components.generac.api import GeneracApiClient
98

109
logging.basicConfig(level=logging.DEBUG)
11-
12-
jar = aiohttp.CookieJar(unsafe=True)
13-
14-
10+
# Your custom JSON encoder
1511
class EnhancedJSONEncoder(json.JSONEncoder):
1612
def default(self, o):
1713
if dataclasses.is_dataclass(o):
1814
return dataclasses.asdict(o)
1915
return super().default(o)
2016

21-
17+
# Async main logic
2218
async def main():
19+
jar = aiohttp.CookieJar(unsafe=True,quote_cookie=False)
2320
async with aiohttp.ClientSession(cookie_jar=jar) as session:
2421
api = GeneracApiClient(
2522
os.environ["GENERAC_USER"], os.environ["GENERAC_PASS"], session
2623
)
2724
await api.login()
28-
print(json.dumps(await api.get_device_data(), cls=EnhancedJSONEncoder))
29-
25+
device_data = await api.get_device_data()
26+
print(json.dumps(device_data, cls=EnhancedJSONEncoder))
3027

31-
loop = asyncio.get_event_loop()
32-
loop.run_until_complete(main())
28+
# Run it using asyncio.run (preferred method in Python 3.7+)
29+
if __name__ == "__main__":
30+
asyncio.run(main())

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
aiohttp==3.11.18
21
beautifulsoup4==4.13.4
32
dacite==1.9.2
43
homeassistant==2025.3.3
54
voluptuous==0.15.2
65
pre-commit==4.2.0
76
reorder-python-imports==3.14.0
7+
Brotli==1.1.0

0 commit comments

Comments
 (0)