From 6694047f0d93bdf0849a9d6d90a0d082bf740c91 Mon Sep 17 00:00:00 2001 From: Bengt Ellison Date: Thu, 31 Oct 2024 07:18:22 +0100 Subject: [PATCH] Add logging tests and adjust websocket logging level in Tibber integration (#316) * Add logging tests and adjust websocket logging level in Tibber integration * improve type hints and linting * renamed test function and added annotation --- test/test_tibber.py | 26 ++++++++++++++++++++++++++ tibber/realtime.py | 3 +++ 2 files changed, 29 insertions(+) diff --git a/test/test_tibber.py b/test/test_tibber.py index 54b5aa1..400b6d9 100644 --- a/test/test_tibber.py +++ b/test/test_tibber.py @@ -1,6 +1,8 @@ """Tests for pyTibber.""" +import asyncio import datetime as dt +import logging import aiohttp import pytest @@ -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" diff --git a/tibber/realtime.py b/tibber/realtime.py index 34d5146..68fd530 100644 --- a/tibber/realtime.py +++ b/tibber/realtime.py @@ -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 @@ -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."""