Skip to content

Commit 81c5506

Browse files
Support latest gql (#375)
* Support latest gql Signed-off-by: Daniel Hjelseth Høyer <[email protected]> * format Signed-off-by: Daniel Hjelseth Høyer <[email protected]> * format Signed-off-by: Daniel Hjelseth Høyer <[email protected]> * format Signed-off-by: Daniel Hjelseth Høyer <[email protected]> * Update tibber/realtime.py Co-authored-by: Copilot <[email protected]> * format Signed-off-by: Daniel Hjelseth Høyer <[email protected]> --------- Signed-off-by: Daniel Hjelseth Høyer <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 4b516de commit 81c5506

File tree

3 files changed

+68
-21
lines changed

3 files changed

+68
-21
lines changed

.github/workflows/code_checker.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,36 @@ jobs:
3838
- name: pytest
3939
run: |
4040
pytest
41+
42+
validate-gql-350:
43+
runs-on: "ubuntu-latest"
44+
strategy:
45+
matrix:
46+
python-version:
47+
- "3.12"
48+
- "3.13"
49+
env:
50+
SRC_FOLDER: tibber
51+
steps:
52+
- uses: actions/checkout@v6
53+
- name: Set up Python ${{ matrix.python-version }}
54+
uses: actions/setup-python@v6
55+
with:
56+
python-version: ${{ matrix.python-version }}
57+
cache: 'pip'
58+
59+
- name: Install dependency with gql==3.5.0
60+
run: |
61+
pip install -r requirements.txt
62+
pip install gql==3.5.0
63+
pip install mypy pre-commit pytest pytest-asyncio pytest-cov ruff
64+
- uses: actions/cache@v4
65+
with:
66+
path: ~/.cache/pre-commit
67+
key: pre-commit-3|${{ env.pythonLocation }}|${{ hashFiles('.pre-commit-config.yaml') }}
68+
- name: Run pre-commit
69+
run: pre-commit run --all-files --show-diff-on-failure --color=always
70+
shell: bash
71+
- name: pytest
72+
run: |
73+
pytest

tibber/home.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ def current_price_data(self) -> tuple[float | None, dt.datetime | None, float |
382382
return round(price_total, 3), price_time, price_rank
383383
return None, None, None
384384

385-
async def rt_subscribe(self, callback: Callable[..., Any]) -> None:
385+
async def rt_subscribe(self, callback: Callable[..., Any]) -> None: # noqa: PLR0915
386386
"""Connect to Tibber and subscribe to Tibber real time subscription.
387387
388388
:param callback: The function to call when data is received.
@@ -436,11 +436,15 @@ async def _start() -> None:
436436
return
437437

438438
try:
439-
session = self._tibber_control.realtime.sub_manager.session
440-
if not hasattr(session, "subscribe"):
441-
_LOGGER.error("Session does not support subscribe method")
439+
client = self._tibber_control.realtime.sub_manager
440+
if hasattr(client, "subscribe"):
441+
subscribe_method: Any = client.subscribe
442+
elif hasattr(client, "session") and hasattr(client.session, "subscribe"):
443+
subscribe_method = client.session.subscribe # type: ignore[assignment]
444+
else:
445+
_LOGGER.error("Client does not support subscribe method")
442446
return
443-
async for _data in session.subscribe(
447+
async for _data in subscribe_method( # type: ignore[attr-defined]
444448
gql(LIVE_SUBSCRIBE % self.home_id),
445449
):
446450
data = {"data": _data}

tibber/realtime.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,7 @@ async def disconnect(self) -> None:
5454
home.rt_unsubscribe()
5555
if self.sub_manager is None:
5656
return
57-
try:
58-
if not hasattr(self.sub_manager, "session"):
59-
return
60-
await self.sub_manager.close_async()
61-
finally:
62-
self.sub_manager = None
57+
await self._close_sub_manager()
6358

6459
async def connect(self) -> None:
6560
"""Start subscription manager."""
@@ -136,11 +131,7 @@ async def _watchdog(self) -> None:
136131
self.sub_manager.transport.reconnect_at,
137132
)
138133

139-
try:
140-
if hasattr(self.sub_manager, "session"):
141-
await self.sub_manager.close_async()
142-
except Exception:
143-
_LOGGER.exception("Error in watchdog close")
134+
await self._close_sub_manager()
144135

145136
if not self._watchdog_running:
146137
_LOGGER.debug("Watchdog: Stopping")
@@ -185,11 +176,15 @@ def add_home(self, home: TibberHome) -> bool:
185176
@property
186177
def subscription_running(self) -> bool:
187178
"""Is real time subscription running."""
188-
return (
189-
self.sub_manager is not None
190-
and isinstance(self.sub_manager.transport, TibberWebsocketsTransport)
191-
and self.sub_manager.transport.running
192-
and hasattr(self.sub_manager, "session")
179+
if self.sub_manager is None:
180+
return False
181+
if not isinstance(self.sub_manager.transport, TibberWebsocketsTransport):
182+
return False
183+
if not self.sub_manager.transport.running:
184+
return False
185+
# Check if client supports subscriptions (directly on client or via its session attribute)
186+
return hasattr(self.sub_manager, "subscribe") or (
187+
hasattr(self.sub_manager, "session") and hasattr(self.sub_manager.session, "subscribe")
193188
)
194189

195190
@property
@@ -210,3 +205,18 @@ def sub_endpoint(self, sub_endpoint: str) -> None:
210205
ssl=self._ssl_context,
211206
),
212207
)
208+
209+
async def _close_sub_manager(self) -> None:
210+
"""Close the subscription manager."""
211+
try:
212+
if self.sub_manager is None:
213+
return
214+
if hasattr(self.sub_manager, "close_async"):
215+
await self.sub_manager.close_async()
216+
elif hasattr(self.sub_manager, "close"):
217+
await self.sub_manager.close()
218+
except Exception: # noqa: BLE001
219+
# Catch all other exceptions as the gql client may raise various exceptions
220+
_LOGGER.debug("Error closing sub_manager", exc_info=True)
221+
finally:
222+
self.sub_manager = None

0 commit comments

Comments
 (0)