From 7dcd46ca7011a65558d4c53015915377c3f08a74 Mon Sep 17 00:00:00 2001 From: Francois Thibault Date: Tue, 17 Jun 2025 07:24:00 -0400 Subject: [PATCH 1/3] Disable session cookie quoting to work with aiohttp v3.12.12. Fix issue #684 --- custom_components/hilo/__init__.py | 5 +++-- custom_components/hilo/oauth2.py | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/custom_components/hilo/__init__.py b/custom_components/hilo/__init__.py index 29afca62..1e676b97 100644 --- a/custom_components/hilo/__init__.py +++ b/custom_components/hilo/__init__.py @@ -3,6 +3,7 @@ from __future__ import annotations import asyncio +from aiohttp import CookieJar from collections import OrderedDict from datetime import datetime, timedelta import traceback @@ -25,8 +26,8 @@ ) from homeassistant.core import Context, Event, HomeAssistant, callback from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady +from homeassistant.helpers.aiohttp_client import async_create_clientsession from homeassistant.helpers import ( - aiohttp_client, config_entry_oauth2_flow, device_registry as dr, entity_registry as er, @@ -140,7 +141,7 @@ async def async_setup_entry( # noqa: C901 try: api = await API.async_create( - session=aiohttp_client.async_get_clientsession(hass), + session=async_create_clientsession(hass, cookie_jar=CookieJar(quote_cookie=False)), oauth_session=config_entry_oauth2_flow.OAuth2Session( hass, entry, implementation ), diff --git a/custom_components/hilo/oauth2.py b/custom_components/hilo/oauth2.py index d827a3dd..47379b8e 100644 --- a/custom_components/hilo/oauth2.py +++ b/custom_components/hilo/oauth2.py @@ -4,6 +4,9 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.config_entry_oauth2_flow import LocalOAuth2Implementation + +from homeassistant.helpers.aiohttp_client import async_create_clientsession +from aiohttp import CookieJar from pyhilo.const import AUTH_AUTHORIZE, AUTH_CLIENT_ID, AUTH_TOKEN, DOMAIN from pyhilo.oauth2helper import OAuth2Helper @@ -25,6 +28,7 @@ def __init__( AUTH_TOKEN, ) + self.session = async_create_clientsession(self.hass, cookie_jar=CookieJar(quote_cookie=False)) self.oauth_helper = OAuth2Helper() # ... Override AbstractOAuth2Implementation details @@ -48,3 +52,21 @@ async def async_resolve_external_data(self, external_data: Any) -> dict: ) ), ) + + async def _token_request(self, data: dict) -> dict: + """Make a token request.""" + data["client_id"] = self.client_id + + if self.client_secret: + data["client_secret"] = self.client_secret + + resp = await self.session.post(self.token_url, data=data) + if resp.status >= 400: + try: + error_response = await resp.json() + except (ClientError, JSONDecodeError): + error_response = {} + error_code = error_response.get("error", "unknown") + error_description = error_response.get("error_description", "unknown error") + resp.raise_for_status() + return cast(dict, await resp.json()) From 7d961f1bddab9380dc85c58fb5631f869594e33c Mon Sep 17 00:00:00 2001 From: Francois Thibault Date: Tue, 17 Jun 2025 13:41:39 -0400 Subject: [PATCH 2/3] Remove unnecessary code --- custom_components/hilo/oauth2.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/custom_components/hilo/oauth2.py b/custom_components/hilo/oauth2.py index 47379b8e..e81968da 100644 --- a/custom_components/hilo/oauth2.py +++ b/custom_components/hilo/oauth2.py @@ -7,6 +7,7 @@ from homeassistant.helpers.aiohttp_client import async_create_clientsession from aiohttp import CookieJar + from pyhilo.const import AUTH_AUTHORIZE, AUTH_CLIENT_ID, AUTH_TOKEN, DOMAIN from pyhilo.oauth2helper import OAuth2Helper @@ -61,12 +62,5 @@ async def _token_request(self, data: dict) -> dict: data["client_secret"] = self.client_secret resp = await self.session.post(self.token_url, data=data) - if resp.status >= 400: - try: - error_response = await resp.json() - except (ClientError, JSONDecodeError): - error_response = {} - error_code = error_response.get("error", "unknown") - error_description = error_response.get("error_description", "unknown error") resp.raise_for_status() return cast(dict, await resp.json()) From 3546068532bac9aa89e6a06907c960628897ec1a Mon Sep 17 00:00:00 2001 From: "Ian C." <108159253+ic-dev21@users.noreply.github.com> Date: Tue, 17 Jun 2025 19:32:57 -0400 Subject: [PATCH 3/3] Linting --- custom_components/hilo/__init__.py | 8 +++++--- custom_components/hilo/oauth2.py | 10 +++++----- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/custom_components/hilo/__init__.py b/custom_components/hilo/__init__.py index 1e676b97..554648a4 100644 --- a/custom_components/hilo/__init__.py +++ b/custom_components/hilo/__init__.py @@ -3,12 +3,12 @@ from __future__ import annotations import asyncio -from aiohttp import CookieJar from collections import OrderedDict from datetime import datetime, timedelta import traceback from typing import TYPE_CHECKING, List, Optional +from aiohttp import CookieJar from homeassistant.components.select import ( ATTR_OPTION, DOMAIN as SELECT_DOMAIN, @@ -26,12 +26,12 @@ ) from homeassistant.core import Context, Event, HomeAssistant, callback from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady -from homeassistant.helpers.aiohttp_client import async_create_clientsession from homeassistant.helpers import ( config_entry_oauth2_flow, device_registry as dr, entity_registry as er, ) +from homeassistant.helpers.aiohttp_client import async_create_clientsession from homeassistant.helpers.dispatcher import async_dispatcher_send from homeassistant.helpers.event import async_call_later from homeassistant.helpers.update_coordinator import DataUpdateCoordinator @@ -141,7 +141,9 @@ async def async_setup_entry( # noqa: C901 try: api = await API.async_create( - session=async_create_clientsession(hass, cookie_jar=CookieJar(quote_cookie=False)), + session=async_create_clientsession( + hass, cookie_jar=CookieJar(quote_cookie=False) + ), oauth_session=config_entry_oauth2_flow.OAuth2Session( hass, entry, implementation ), diff --git a/custom_components/hilo/oauth2.py b/custom_components/hilo/oauth2.py index e81968da..78527185 100644 --- a/custom_components/hilo/oauth2.py +++ b/custom_components/hilo/oauth2.py @@ -2,12 +2,10 @@ from typing import Any, cast +from aiohttp import CookieJar from homeassistant.core import HomeAssistant -from homeassistant.helpers.config_entry_oauth2_flow import LocalOAuth2Implementation - from homeassistant.helpers.aiohttp_client import async_create_clientsession -from aiohttp import CookieJar - +from homeassistant.helpers.config_entry_oauth2_flow import LocalOAuth2Implementation from pyhilo.const import AUTH_AUTHORIZE, AUTH_CLIENT_ID, AUTH_TOKEN, DOMAIN from pyhilo.oauth2helper import OAuth2Helper @@ -29,7 +27,9 @@ def __init__( AUTH_TOKEN, ) - self.session = async_create_clientsession(self.hass, cookie_jar=CookieJar(quote_cookie=False)) + self.session = async_create_clientsession( + self.hass, cookie_jar=CookieJar(quote_cookie=False) + ) self.oauth_helper = OAuth2Helper() # ... Override AbstractOAuth2Implementation details