Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ssl_context for the realtime connection #298

Merged
merged 5 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion tibber/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import datetime as dt
import logging
import zoneinfo
from ssl import SSLContext
from typing import Any

import aiohttp
Expand Down Expand Up @@ -35,6 +36,7 @@ def __init__(
websession: aiohttp.ClientSession | None = None,
time_zone: dt.tzinfo | None = None,
user_agent: str | None = None,
ssl: SSLContext | bool = True,
):
"""Initialize the Tibber connection.

Expand All @@ -43,10 +45,11 @@ def __init__(
:param websession: The websession to use when communicating with the Tibber API.
:param time_zone: The time zone to display times in and to use.
:param user_agent: User agent identifier for the platform running this. Required if websession is None.
:param ssl: SSLContext to use.
"""

if websession is None:
websession = aiohttp.ClientSession()
websession = aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=ssl))
elif user_agent is None:
user_agent = websession.headers.get(aiohttp.hdrs.USER_AGENT)
if user_agent is None:
Expand All @@ -60,6 +63,7 @@ def __init__(
self._access_token,
self.timeout,
self._user_agent,
ssl=ssl,
)

self.time_zone: dt.tzinfo = time_zone or zoneinfo.ZoneInfo("UTC")
Expand Down
10 changes: 4 additions & 6 deletions tibber/realtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import datetime as dt
import logging
import random
from ssl import SSLContext
from typing import Any

from gql import Client
Expand All @@ -21,12 +22,7 @@ class TibberRT:
"""Class to handle real time connection with the Tibber api."""

# pylint: disable=too-many-instance-attributes
def __init__(
self,
access_token: str,
timeout: int,
user_agent: str,
):
def __init__(self, access_token: str, timeout: int, user_agent: str, ssl: SSLContext | bool):
"""Initialize the Tibber connection.

:param access_token: The access token to access the Tibber API with.
Expand All @@ -36,6 +32,7 @@ def __init__(
self._access_token: str = access_token
self._timeout: int = timeout
self._user_agent: str = user_agent
self._ssl_context = ssl

self._sub_endpoint: str | None = None
self._homes: list[TibberHome] = []
Expand Down Expand Up @@ -90,6 +87,7 @@ def _create_sub_manager(self) -> None:
self.sub_endpoint,
self._access_token,
self._user_agent,
ssl=self._ssl_context,
),
)

Expand Down
4 changes: 3 additions & 1 deletion tibber/websocket_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import asyncio
import datetime as dt
import logging
from ssl import SSLContext

from gql.transport.exceptions import TransportClosed
from gql.transport.websockets import WebsocketsTransport
Expand All @@ -13,13 +14,14 @@
class TibberWebsocketsTransport(WebsocketsTransport):
"""Tibber websockets transport."""

def __init__(self, url: str, access_token: str, user_agent: str) -> None:
def __init__(self, url: str, access_token: str, user_agent: str, ssl: SSLContext | bool = True) -> None:
"""Initialize TibberWebsocketsTransport."""
super().__init__(
url=url,
init_payload={"token": access_token},
headers={"User-Agent": user_agent},
ping_interval=30,
ssl=ssl,
)
self._user_agent: str = user_agent
self._timeout: int = 90
Expand Down
Loading