Skip to content

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

Closed
@stopcoder

Description

@stopcoder

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions