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>_**
$
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:
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"
Result:
$ python3 getstate-v1.py