Skip to content

Issue with 'await' after upgrade from 1.6.1 to 1.6.3 #712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
stopcoder opened this issue Apr 3, 2025 · 4 comments
Closed

Issue with 'await' after upgrade from 1.6.1 to 1.6.3 #712

stopcoder opened this issue Apr 3, 2025 · 4 comments

Comments

@stopcoder
Copy link

I have the following code to get the history data from recorder statistics in Home Assistant. The code stopped working after upgrading to 1.6.3 from 1.6.1. I also tested with 1.6.2 and the issue also occurs.

from datetime import datetime, timedelta
from homeassistant.components.recorder import history
from homeassistant.components.recorder import get_instance
from homeassistant.components.recorder.statistics import (
    statistics_during_period,
)

from calendar import monthrange

year = 2025
month = 2

def get_full_charge_days(year, month):
     # First day of the given month at 00:00:00
    first_day_of_month = datetime(year, month, 1, 0, 0, 0)

    # Get the last day of the month
    last_day = monthrange(year, month)[1]
    
    # Last moment of the current month
    last_day_of_month = datetime(year, month, last_day, 23, 59, 59)
    sensor_name = "sensor.solax_battery_capacity"


    sensor_history = await get_instance(hass).async_add_executor_job(
        lambda fd=first_day_of_month, ld=last_day_of_month, sensor=sensor_name: statistics_during_period(
            hass=hass,
            start_time=fd,
            end_time=ld,
            statistic_ids={sensor},
            period="day",
            units=None,
            types={"max"}
        )
    )

    return [datetime.fromtimestamp(entry["start"]).day for entry in sensor_history[sensor_name] if entry["max"] >= 90]

data = get_full_charge_days(year, month)
log.debug(data)
log.debug(len(data))

The error is:
Exception in <jupyter_0> line 47:
return [datetime.fromtimestamp(entry["start"]).day for entry in sensor_history[sensor_name] if entry["max"] >= 90]

TypeError: '_asyncio.Future' object is not subscriptable

It seems like the Future returned from async_add_executor_job can't be awaited to a result. The sensor_history returned from the await is still a Future instance.

@dmamelin
Copy link
Contributor

dmamelin commented Apr 3, 2025

@craigbarratt I think the change in ast_await should help, but I can’t verify it yet:

if coro and (asyncio.iscoroutine(coro) or asyncio.isfuture(coro)):

@craigbarratt
Copy link
Member

@stopcoder can you confirm whether @dmamelin's proposed fix solves your problem?

@stopcoder
Copy link
Author

@craigbarratt I think the change in ast_await should help, but I can’t verify it yet:

if coro and (asyncio.iscoroutine(coro) or asyncio.isfuture(coro)):

My script works again after patching the line with the given code in eval.py.

craigbarratt added a commit that referenced this issue Apr 12, 2025
@craigbarratt
Copy link
Member

Released 1.6.4 with this fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants