Skip to content

Commit

Permalink
Linting
Browse files Browse the repository at this point in the history
  • Loading branch information
ic-dev21 committed Nov 22, 2024
1 parent 4e616c4 commit 08e40ce
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
11 changes: 4 additions & 7 deletions pyhilo/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,18 +356,15 @@ async def _async_post_init(self) -> None:
await self._get_fid()
await self._get_device_token()
await self.refresh_ws_token()
#self.websocket = WebsocketClient(self)
# self.websocket = WebsocketClient(self)

#Initialize WebsocketManager ic-dev21
# Initialize WebsocketManager ic-dev21
self.websocket_manager = WebsocketManager(
self.session,
self.async_request,
self._state_yaml,
set_state
self.session, self.async_request, self._state_yaml, set_state
)
await self.websocket_manager.initialize_websockets()

#Create both websocket clients
# Create both websocket clients
self.websocket = WebsocketClient(self, self.websocket_manager.devicehub)
self.websocket2 = WebsocketClient(self, self.websocket_manager.challengehub)

Expand Down
44 changes: 28 additions & 16 deletions pyhilo/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@
from yarl import URL

from pyhilo.const import (
DEFAULT_USER_AGENT,
LOG,
AUTOMATION_CHALLENGE_ENDPOINT,
AUTOMATION_DEVICEHUB_ENDPOINT,
DEFAULT_USER_AGENT,
LOG,
)

from pyhilo.exceptions import (
CannotConnectError,
ConnectionClosedError,
Expand Down Expand Up @@ -388,6 +387,7 @@ async def async_invoke(
@dataclass
class WebsocketConfig:
"""Configuration for a websocket connection"""

endpoint: str
url: Optional[str] = None
token: Optional[str] = None
Expand All @@ -399,11 +399,7 @@ class WebsocketManager:
"""Manages multiple websocket connections for the Hilo API"""

def __init__(
self,
session: ClientSession,
async_request,
state_yaml: str,
set_state_callback
self, session: ClientSession, async_request, state_yaml: str, set_state_callback
) -> None:
"""Initialize the websocket manager.
Expand All @@ -417,7 +413,7 @@ def __init__(
self.async_request = async_request
self._state_yaml = state_yaml
self._set_state = set_state_callback
self._shared_token = None #ic-dev21 need to share the token
self._shared_token = None # ic-dev21 need to share the token

# Initialize websocket configurations
self.devicehub = WebsocketConfig(endpoint=AUTOMATION_DEVICEHUB_ENDPOINT)
Expand All @@ -430,7 +426,9 @@ async def initialize_websockets(self) -> None:
# ic-dev21 reuse it for challenge hub
await self.refresh_token(self.challengehub, get_new_token=False)

async def refresh_token(self, config: WebsocketConfig, get_new_token: bool = True) -> None:
async def refresh_token(
self, config: WebsocketConfig, get_new_token: bool = True
) -> None:
"""Refresh token for a specific websocket configuration.
Args:
Expand All @@ -443,7 +441,7 @@ async def refresh_token(self, config: WebsocketConfig, get_new_token: bool = Tru
# ic-dev21 reuse existing token but get new URL
config.url, _ = await self._negotiate(config)
config.token = self._shared_token

await self._get_websocket_params(config)

async def _negotiate(self, config: WebsocketConfig) -> Tuple[str, str]:
Expand All @@ -461,10 +459,18 @@ async def _negotiate(self, config: WebsocketConfig) -> Tuple[str, str]:

resp = await self.async_request("post", url)
ws_url = resp.get("url")
ws_token = resp.get("accessToken") if self._shared_token is None else self._shared_token
ws_token = (
resp.get("accessToken")
if self._shared_token is None
else self._shared_token
)

# Save state
state_key = "websocket" if config.endpoint == "AUTOMATION_DEVICEHUB_ENDPOINT" else "websocket2"
state_key = (
"websocket"
if config.endpoint == "AUTOMATION_DEVICEHUB_ENDPOINT"
else "websocket2"
)
await self._set_state(
self._state_yaml,
state_key,
Expand Down Expand Up @@ -496,7 +502,9 @@ async def _get_websocket_params(self, config: WebsocketConfig) -> None:
)

config.connection_id = resp.get("connectionId", "")
config.full_url = f"{config.url}&id={config.connection_id}&access_token={config.token}"
config.full_url = (
f"{config.url}&id={config.connection_id}&access_token={config.token}"
)
LOG.debug(f"Getting full ws URL {config.full_url}")

transport_dict = resp.get("availableTransports", [])
Expand All @@ -507,6 +515,10 @@ async def _get_websocket_params(self, config: WebsocketConfig) -> None:
}

# Save state
state_key = "websocket" if config.endpoint == "AUTOMATION_DEVICEHUB_ENDPOINT" else "websocket2"
state_key = (
"websocket"
if config.endpoint == "AUTOMATION_DEVICEHUB_ENDPOINT"
else "websocket2"
)
LOG.debug(f"Calling set_state {state_key}_params")
await self._set_state(self._state_yaml, state_key, websocket_dict)
await self._set_state(self._state_yaml, state_key, websocket_dict)

0 comments on commit 08e40ce

Please sign in to comment.