GrandMoff100/HomeAssistantAPI

Async Cache session expire_after does not set properly

Closed this issue · 7 comments

N4S4 commented

Good dday,

With below code:

import asyncio
from homeassistant_api import Client
from aiohttp_client_cache import CachedSession, FileBackend
from datetime import timedelta
import logging

logging.basicConfig(filename='log.log', encoding='utf-8', level=logging.DEBUG)

hassurl = 'MY URL'
token = 'MY TOKEN'

client = Client(
    hassurl,
    token,
    async_cache_session=CachedSession(cache=FileBackend(),
                                      expire_after=timedelta(seconds=5)),
    use_async=True
)


async def main():
    async with client:
        while True:
            door = await client.async_get_state(entity_id='sensor.testing')
            if door.state == 'on':

                return True
            else:
                print(door)


asyncio.get_event_loop().run_until_complete(main())

whenever I run this code it works properly except for the update of the cache

looking at the debug log i find this string:

DEBUG:aiohttp_client_cache.cache_control:Determining expiration time based on: -1

whatever I set as expire_after in CachedSession it will always be set to -1

does this event occur to me only?

I can reproduce this.

ERROR:aiohttp_client_cache.backends:Dependencies are not installed for this backend
ERROR:aiohttp_client_cache.backends:Dependencies are not installed for this backend
DEBUG:asyncio:Using selector: EpollSelector
DEBUG:homeassistant_api.rawasyncclient:Entering cached async requests session <aiohttp_client_cache.session.CachedSession object at 0x7f3ef34680d0>
DEBUG:aiohttp_client_cache.backends.base:Attempting to get cached response for key: 725473622b0c8f0576722ae2ea191947a0b3188737661ae7a8d59034ee51842f
DEBUG:aiosqlite:executing <function connect.<locals>.connector at 0x7f3ef345bb50>
DEBUG:aiosqlite:operation <function connect.<locals>.connector at 0x7f3ef345bb50> completed
DEBUG:aiosqlite:executing functools.partial(<built-in method execute of sqlite3.Connection object at 0x7f3ef3672440>, 'PRAGMA synchronous = 0;', [])
DEBUG:aiosqlite:operation functools.partial(<built-in method execute of sqlite3.Connection object at 0x7f3ef3672440>, 'PRAGMA synchronous = 0;', []) completed
DEBUG:aiosqlite:executing functools.partial(<built-in method execute of sqlite3.Connection object at 0x7f3ef3672440>, 'CREATE TABLE IF NOT EXISTS `redirects` (key PRIMARY KEY, value)', [])
DEBUG:aiosqlite:operation functools.partial(<built-in method execute of sqlite3.Connection object at 0x7f3ef3672440>, 'CREATE TABLE IF NOT EXISTS `redirects` (key PRIMARY KEY, value)', []) completed
DEBUG:aiosqlite:executing functools.partial(<built-in method execute of sqlite3.Connection object at 0x7f3ef3672440>, 'SELECT value FROM `redirects` WHERE key=?', ('725473622b0c8f0576722ae2ea191947a0b3188737661ae7a8d59034ee51842f',))
DEBUG:aiosqlite:operation functools.partial(<built-in method execute of sqlite3.Connection object at 0x7f3ef3672440>, 'SELECT value FROM `redirects` WHERE key=?', ('725473622b0c8f0576722ae2ea191947a0b3188737661ae7a8d59034ee51842f',)) completed
DEBUG:aiosqlite:executing functools.partial(<built-in method fetchone of sqlite3.Cursor object at 0x7f3ef35cb2c0>)
DEBUG:aiosqlite:operation functools.partial(<built-in method fetchone of sqlite3.Cursor object at 0x7f3ef35cb2c0>) completed
DEBUG:aiosqlite:executing functools.partial(<built-in method close of sqlite3.Connection object at 0x7f3ef3672440>)
DEBUG:aiosqlite:operation functools.partial(<built-in method close of sqlite3.Connection object at 0x7f3ef3672440>) completed
DEBUG:aiohttp_client_cache.backends.base:No cached response found
DEBUG:aiohttp_client_cache.session:Cached response not found; making request to http://localhost:8123/api/
DEBUG:aiohttp_client_cache.backends.base:Pre-cache checks for response from http://localhost:8123/api/: {'disabled cache': False, 'disabled method': False, 'disabled status': False, 'disabled by filter': False, 'disabled by headers or expiration params': False, 'expired': False}
DEBUG:aiohttp_client_cache.backends.base:Saving response for key: 725473622b0c8f0576722ae2ea191947a0b3188737661ae7a8d59034ee51842f
DEBUG:aiohttp_client_cache.cache_control:Determining expiration time based on: -1
DEBUG:homeassistant_api.processing:Using processor <function async_process_json at 0x7f3ef3458820> on <ClientResponse(http://localhost:8123/api/) [200 OK]>
<CIMultiDictProxy('Content-Type': 'application/json', 'Content-Length': '34', 'Content-Encoding': 'deflate', 'Date': 'Sat, 18 Mar 2023 23:03:59 GMT', 'Server': 'Python/3.10 aiohttp/3.8.4')>

DEBUG:aiohttp_client_cache.backends.base:Attempting to get cached response for key: 9690eef0b40d54e7c77544fef814ef9b2896e9c9a689b88246a828fd6700af7a
DEBUG:aiosqlite:executing <function connect.<locals>.connector at 0x7f3ef345bac0>
DEBUG:aiosqlite:operation <function connect.<locals>.connector at 0x7f3ef345bac0> completed
DEBUG:aiosqlite:executing functools.partial(<built-in method execute of sqlite3.Connection object at 0x7f3ef3672640>, 'PRAGMA synchronous = 0;', [])
DEBUG:aiosqlite:operation functools.partial(<built-in method execute of sqlite3.Connection object at 0x7f3ef3672640>, 'PRAGMA synchronous = 0;', []) completed
DEBUG:aiosqlite:executing functools.partial(<built-in method execute of sqlite3.Connection object at 0x7f3ef3672640>, 'SELECT value FROM `redirects` WHERE key=?', ('9690eef0b40d54e7c77544fef814ef9b2896e9c9a689b88246a828fd6700af7a',))
DEBUG:aiosqlite:operation functools.partial(<built-in method execute of sqlite3.Connection object at 0x7f3ef3672640>, 'SELECT value FROM `redirects` WHERE key=?', ('9690eef0b40d54e7c77544fef814ef9b2896e9c9a689b88246a828fd6700af7a',)) completed
DEBUG:aiosqlite:executing functools.partial(<built-in method fetchone of sqlite3.Cursor object at 0x7f3ef35cb340>)
DEBUG:aiosqlite:operation functools.partial(<built-in method fetchone of sqlite3.Cursor object at 0x7f3ef35cb340>) completed
DEBUG:aiosqlite:executing functools.partial(<built-in method close of sqlite3.Connection object at 0x7f3ef3672640>)
DEBUG:aiosqlite:operation functools.partial(<built-in method close of sqlite3.Connection object at 0x7f3ef3672640>) completed
DEBUG:aiohttp_client_cache.backends.base:No cached response found
DEBUG:aiohttp_client_cache.session:Cached response not found; making request to http://localhost:8123/api/states/sensor.testing
DEBUG:aiohttp_client_cache.backends.base:Pre-cache checks for response from http://localhost:8123/api/states/sensor.testing: {'disabled cache': False, 'disabled method': False, 'disabled status': True, 'disabled by filter': False, 'disabled by headers or expiration params': False, 'expired': False}
DEBUG:aiohttp_client_cache.backends.base:Not caching response for key: 9690eef0b40d54e7c77544fef814ef9b2896e9c9a689b88246a828fd6700af7a
DEBUG:homeassistant_api.rawasyncclient:Exiting async requests session <aiohttp_client_cache.session.CachedSession object at 0x7f3ef34680d0>

I've looked around and it looks like the expire_after parameter actually goes to the backend instead.

cache=FileBackend(expire_after=timedelta(seconds=5)),
# ...
DEBUG:aiohttp_client_cache.backends.base:Pre-cache checks for response from http://localhost:8123/api/states/sun.sun: {'disabled cache': False, 'disabled method': False, 'disabled status': False, 'disabled by filter': False, 'disabled by headers or expiration params': False, 'expired': False}
DEBUG:aiohttp_client_cache.backends.base:Saving response for key: c7d55f201e0b72612d1d106e995216db2cdb3f407cf96f5bbbf9d6398b79cfa9
DEBUG:aiohttp_client_cache.cache_control:Determining expiration time based on: 0:00:05
DEBUG:homeassistant_api.processing:Using processor <function async_process_json at 0x7f7ff8e80820> on <ClientResponse(http://localhost:8123/api/states/sun.sun) [200 OK]>
<CIMultiDictProxy('Content-Type': 'application/json', 'Content-Length': '329', 'Content-Encoding': 'deflate', 'Date': 'Sat, 18 Mar 2023 23:24:15 GMT', 'Server': 'Python/3.10 aiohttp/3.8.4')>
# ...

Here's where I found this, on the docs for asyncio_client_cache

N4S4 commented

Yes it works flawlessly, thank you so much!
will this be reflected in docs too?

Oooh yea good point. I will add it to the docs! Thank you for opening an issue about it.

N4S4 commented

thank you for the support!

Happy to help.

Done :)