Skip to content

Commit

Permalink
Improve reconnect login
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Hjelseth Høyer <[email protected]>
  • Loading branch information
Danielhiversen committed Feb 13, 2023
1 parent 4c2e5c6 commit 0747e7f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
17 changes: 13 additions & 4 deletions tibber/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,14 @@ async def rt_disconnect(self) -> None:
"""Stop subscription manager.
This method simply calls the stop method of the SubscriptionManager if it is defined.
"""
_LOGGER.debug("Stopping subscription manager")
if self._watchdog_runner is not None:
_LOGGER.debug("Stopping watchdog")
self._watchdog_running = False
self._watchdog_runner.cancel()
self._watchdog_runner = None
for home in self.get_homes(False):
home.rt_unsubscribe()
if not hasattr(self.sub_manager, "session"):
return
await self.sub_manager.close_async()
Expand All @@ -112,21 +115,27 @@ async def _rt_watchdog(self) -> None:
await asyncio.sleep(60)

_retry_count = 0
next_is_running_test = dt.datetime.now()
next_test_all_homes_running = dt.datetime.now()
while self._watchdog_running:
if (
self.sub_manager.transport.running
and self.sub_manager.transport.reconnect_at > dt.datetime.now()
):
is_running = True
if next_is_running_test > dt.datetime.now():
if dt.datetime.now() > next_test_all_homes_running:
for home in self.get_homes(False):
_LOGGER.debug(
"Watchdog: Checking if home %s is alive, %s, %s",
home.home_id,
home.has_real_time_consumption,
home.rt_subscription_running,
)
if not home.has_real_time_consumption:
continue
if not home.rt_subscription_running:
is_running = False
next_is_running_test = dt.datetime.now() + dt.timedelta(
seconds=60
next_test_all_homes_running = (
dt.datetime.now() + dt.timedelta(seconds=60)
)
break
if is_running:
Expand Down
2 changes: 1 addition & 1 deletion tibber/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from typing import Final

__version__ = "0.26.12"
__version__ = "0.26.13"

RESOLUTION_HOURLY: Final = "HOURLY"
RESOLUTION_DAILY: Final = "DAILY"
Expand Down
23 changes: 19 additions & 4 deletions tibber/tibber_home.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def __init__(self, home_id: str, tibber_control: Tibber) -> None:
self._last_rt_data_received: dt.datetime = dt.datetime.now()
self._rt_listener: None | asyncio.Task = None
self._rt_callback: Callable | None = None
self._rt_stopped: bool = True

async def _fetch_data(self, hourly_data: HourlyData) -> None:
"""Update hourly consumption or production data asynchronously."""
Expand Down Expand Up @@ -415,6 +416,9 @@ async def _start():
while not self._tibber_control.rt_subscription_running:
_LOGGER.debug("Waiting for rt_connect")
await asyncio.sleep(1)
if self._rt_stopped:
_LOGGER.debug("Stopping rt_subscribe")
return
try:
async for data in self._tibber_control.sub_manager.session.subscribe(
gql(LIVE_SUBSCRIBE % self.home_id)
Expand All @@ -431,25 +435,36 @@ async def _start():
self.home_id,
data,
)
if self._rt_stopped:
_LOGGER.debug("Stopping rt_subscribe loop")
return
except Exception: # pylint: disable=broad-except
_LOGGER.exception("Error in rt_subscribe")
await asyncio.sleep(10)

self._rt_callback = callback
self._rt_listener = asyncio.create_task(_start())
self._rt_stopped = False
await self._tibber_control.rt_connect()

async def rt_resubscribe(self) -> None:
"""Resubscribe to Tibber data."""
_LOGGER.debug("Resubscribe")
_LOGGER.debug("Resubscribe, %s", self.home_id)
self.rt_unsubscribe()
if self._rt_callback is None:
_LOGGER.warning("No callback set for rt_resubscribe")
return
if self._rt_listener is not None:
self._rt_listener.cancel()
self._rt_listener = None
await self.rt_subscribe(self._rt_callback)

def rt_unsubscribe(self) -> None:
"""Unsubscribe to Tibber data."""
_LOGGER.debug("Unsubscribe, %s", self.home_id)
self._rt_stopped = True
if self._rt_listener is None:
return
self._rt_listener.cancel()
self._rt_listener = None

@property
def rt_subscription_running(self) -> bool:
"""Is real time subscription running."""
Expand Down

0 comments on commit 0747e7f

Please sign in to comment.