Skip to content

[BUG] fetch_price_info and fetch_price_info_range does not work with asynchronous code #65

@Waester

Description

@Waester

Describe the bug

fetch_price_info and fetch_price_info_range throws "RuntimeError: This event loop is already running"

To Reproduce

Steps to reproduce the behavior:

    async def get_tibber_data():
        global tibber_prices
        account = await asyncio.to_thread(tibber.Account, tibber_token, immediate_update=False)
        while True:
            await account.update_async()
            for home in account.homes:
                if home.id == tibber_home_id:
                    tibber_prices = home.current_subscription.fetch_price_info("QUARTER_HOURLY").today
                    prices = [price.energy for price in tibber_prices]
                    print(median_high(prices), flush=True)
                    break
            seconds_to_midnight = 86400 - (datetime.now() - datetime.combine(datetime.now().date(), datetime.min.time())).total_seconds()
            await asyncio.sleep(seconds_to_midnight + 60)
Traceback (most recent call last):
  File "/home/REDACTED/REDACTED.py", line 220, in <module>
    asyncio.run(main())
  File "/usr/lib/python3.9/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File "/home/REDACTED/REDACTED.py", line 208, in main
    await asyncio.gather(
  File "/home/REDACTED/REDACTED.py", line 125, in get_tibber_data
    tibber_prices = home.current_subscription.fetch_price_info("QUARTER_HOURLY").today
  File "/home/REDACTED/venv/lib/python3.9/site-packages/tibber/types/subscription.py", line 64, in fetch_price_info
    full_data = self.tibber_client.execute_query(
  File "/home/REDACTED/venv/lib/python3.9/site-packages/tibber/networking/query_executor.py", line 57, in execute_query
    return loop.run_until_complete(
  File "/usr/lib/python3.9/asyncio/base_events.py", line 618, in run_until_complete
    self._check_running()
  File "/usr/lib/python3.9/asyncio/base_events.py", line 578, in _check_running
    raise RuntimeError('This event loop is already running')
RuntimeError: This event loop is already running
sys:1: RuntimeWarning: coroutine 'QueryExecutor.execute_async' was never awaited

Expected behavior

List tibber_prices is populated with quartely prices using async code.

Screenshots

N/A

Version

0.7.1

Additional context

Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions