55from homeassistant .core import HomeAssistant
66from homeassistant .helpers .config_entry_oauth2_flow import LocalOAuth2Implementation
77
8+ from homeassistant .helpers .aiohttp_client import async_create_clientsession
9+ from aiohttp import CookieJar
810from pyhilo .const import AUTH_AUTHORIZE , AUTH_CLIENT_ID , AUTH_TOKEN , DOMAIN
911from pyhilo .oauth2helper import OAuth2Helper
1012
@@ -26,6 +28,7 @@ def __init__(
2628 AUTH_TOKEN ,
2729 )
2830
31+ self .session = async_create_clientsession (self .hass , cookie_jar = CookieJar (quote_cookie = False ))
2932 self .oauth_helper = OAuth2Helper ()
3033
3134 # ... Override AbstractOAuth2Implementation details
@@ -49,3 +52,21 @@ async def async_resolve_external_data(self, external_data: Any) -> dict:
4952 )
5053 ),
5154 )
55+
56+ async def _token_request (self , data : dict ) -> dict :
57+ """Make a token request."""
58+ data ["client_id" ] = self .client_id
59+
60+ if self .client_secret :
61+ data ["client_secret" ] = self .client_secret
62+
63+ resp = await self .session .post (self .token_url , data = data )
64+ if resp .status >= 400 :
65+ try :
66+ error_response = await resp .json ()
67+ except (ClientError , JSONDecodeError ):
68+ error_response = {}
69+ error_code = error_response .get ("error" , "unknown" )
70+ error_description = error_response .get ("error_description" , "unknown error" )
71+ resp .raise_for_status ()
72+ return cast (dict , await resp .json ())
0 commit comments