Skip to content
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

Unclosed client session when using inside function #127

Closed
ssjoholm opened this issue Nov 6, 2022 · 1 comment · Fixed by #133
Closed

Unclosed client session when using inside function #127

ssjoholm opened this issue Nov 6, 2022 · 1 comment · Fixed by #133
Assignees
Labels
bug Something isn't working

Comments

@ssjoholm
Copy link

ssjoholm commented Nov 6, 2022

Hi,

I am not sure if the issue is because I have missed something or understood wrong.

I fetching status from one sensor using the HomeAssistant API, and receives "Unclosed client session" when the routine is inside a function. When I use the same code outside the function it works without any additional printouts.

Example when it works:

from homeassistant_api import Client

URL = '<url>'
TOKEN = '<token>'

with Client(
    URL,
    TOKEN,
) as client:
    state = client.get_state(entity_id="sensor.kitchen_motion_sensor_temperature_2")
print(state)

Result:

$ python3 getstate-v2.py
entity_id='sensor.kitchen_motion_sensor_temperature_2' state='19.8' attributes={'state_class': 'measurement', 'unit_of_measurement': '°C', 'device_class': 'temperature', 'icon': 'mdi:thermometer', 'friendly_name': 'AC Sensor-2 Temperature'} last_changed=datetime.datetime(2022, 11, 6, 12, 31, 8, 698689, tzinfo=datetime.timezone.utc) last_updated=datetime.datetime(2022, 11, 6, 12, 31, 8, 698689, tzinfo=datetime.timezone.utc) context=Context(id='')
$

Example when it output the "Unclosed client session"

from homeassistant_api import Client

def main():
    URL = '<url>'
    TOKEN = '<token>'

    with Client(
        URL,
        TOKEN,
    ) as client:
        state = client.get_state(entity_id="sensor.kitchen_motion_sensor_temperature_2")
    print(state)

if __name__ == "__main__":
    main()

Result:

$ python3 getstate-v1.py

entity_id='sensor.kitchen_motion_sensor_temperature_2' state='19.8' attributes={'state_class': 'measurement', 'unit_of_measurement': '°C', 'device_class': 'temperature', 'icon': 'mdi:thermometer', 'friendly_name': 'AC Sensor-2 Temperature'} last_changed=datetime.datetime(2022, 11, 6, 12, 31, 8, 698689, tzinfo=datetime.timezone.utc) last_updated=datetime.datetime(2022, 11, 6, 12, 31, 8, 698689, tzinfo=datetime.timezone.utc) context=Context(id='<id>')
**_Unclosed client session
client_session: <aiohttp_client_cache.session.CachedSession object at 0x7f8360cd00>_**
$
@ssjoholm ssjoholm added the bug Something isn't working label Nov 6, 2022
@GrandMoff100
Copy link
Owner

I just reproduced this. Looking into it right now...

@GrandMoff100 GrandMoff100 linked a pull request Nov 25, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants