Skip to content

Commit

Permalink
Add logging tests and adjust websocket logging level in Tibber integr…
Browse files Browse the repository at this point in the history
…ation (#316)

* Add logging tests and adjust websocket logging level in Tibber integration

* improve type hints and linting

* renamed test function and added annotation
  • Loading branch information
benganellison authored Oct 31, 2024
1 parent c1cd7b4 commit 6694047
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
26 changes: 26 additions & 0 deletions test/test_tibber.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Tests for pyTibber."""

import asyncio
import datetime as dt
import logging

import aiohttp
import pytest
Expand Down Expand Up @@ -165,3 +167,27 @@ async def test_tibber_get_historic_data():
assert len(historic_data) == 5
assert historic_data[0]["from"] == "2024-01-01T00:00:00.000+01:00", "First day must be 2024-01-01"
assert historic_data[4]["from"] == "2024-01-05T00:00:00.000+01:00", "Last day must be 2024-01-05"


@pytest.mark.asyncio
async def test_logging_rt_subscribe(caplog: pytest.LogCaptureFixture) -> None:
caplog.set_level(logging.INFO)
async with aiohttp.ClientSession() as session:
tibber_connection = tibber.Tibber(
websession=session,
user_agent="test",
)
await tibber_connection.update_info()
home = tibber_connection.get_homes()[0]

def _callback(_: dict) -> None:
return None

await home.rt_subscribe(_callback)
await asyncio.sleep(1)
home.rt_unsubscribe()
await tibber_connection.rt_disconnect()
await asyncio.sleep(10)

assert "gql.transport.websockets:websockets_base.py:240" not in caplog.text, "should not show on info logging level"
assert "gql.transport.websockets:websockets_base.py:218" not in caplog.text, "should not show on info logging level"
3 changes: 3 additions & 0 deletions tibber/realtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import Any

from gql import Client
from gql.transport.websockets import log as websockets_logger

from .exceptions import SubscriptionEndpointMissingError
from .home import TibberHome
Expand All @@ -17,6 +18,8 @@

_LOGGER = logging.getLogger(__name__)

websockets_logger.setLevel(logging.WARNING)


class TibberRT:
"""Class to handle real time connection with the Tibber api."""
Expand Down

0 comments on commit 6694047

Please sign in to comment.