'Why is asyncio session is closed error occurring with my Pymyq script?

I have written a script which uses the Python Pymyq library to interact with Chamberlin smart home devices, in this case a garage door. This script ran successfully for months and then stopped working. When the script is executed, it will run as desired accepting a command to open, close, or provide door status. On the second run and thereafter I am getting a Session is closed error as the response back. I have read everything I can find online regarding aiohttp and asyncio loops. I see no reason why the session or loop should be closed. Anyone know why I am getting this error? I am running the script with Python 3.9 and have tried it on Ubuntu and Windows machines with the same results.

Here is the full script I am working from:

import os
import sys
import datetime
import requests
import json
import time
import re
import asyncio
import pymyq
from aiohttp import ClientSession
import aiohttp
from pymyq import login
from pymyq.errors import MyQError, RequestError
Input = command
Input = Input.lower()
print('Input = '+Input)
#codebox = set_ip()
#creating a place holder global variable for the garage door status
door_status = ''
async def door_position() -> None:
    async with aiohttp.ClientSession() as websession:
        try:
            # Create an API object:
            api = await login(EMAIL, PASSWORD, websession)
            device_id = '###############'
            device = api.devices[device_id]
            #global door_status
            door_status = device.state
            return(door_status)
        except MyQError as err:
            return('error')

async def door_open() -> None:
    async with aiohttp.ClientSession() as websession:
        try:
            # Create an API object:
            api = await login(EMAIL, PASSWORD, websession)
            device_id = '################'
            device = api.devices[device_id]
            await device.open()
        except MyQError as err:
            return('error')

async def door_close() -> None:
    async with aiohttp.ClientSession() as websession:
        try:
            # Create an API object:
            api = await login(EMAIL, PASSWORD, websession)
            device_id = '##############'
            device = api.devices[device_id]
            await device.close()
        except MyQError as err:
            return('error')

if 'garage door status' in Input:
    try:
        #gets the status of the myQ garage door
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        future = asyncio.ensure_future(door_position())
        door_status = asyncio.get_event_loop().run_until_complete(future)
        gen_msg = ('The garage door is '+door_status)
    except Exception as e:
        print(e)
        gen_msg = ('*******API ERROR*****'+str(e))
elif 'open the garage door' in Input:
    #opens the myQ garage door
    try:
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        future = asyncio.ensure_future(door_open())
        asyncio.get_event_loop().run_until_complete(future)
        gen_msg = ('The garage door is now opening')
    except Exception as e:
        print(e)
        gen_msg = ('*******MYQ ERROR: DOOR NOT RESPONDING*****'+str(e))
elif 'close the garage door' in Input:
    #closes the myQ garage door
    try:
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        future = asyncio.ensure_future(door_close())
        asyncio.get_event_loop().run_until_complete(future)
        gen_msg = ('The garage door is now closing')
    except Exception as e:
        print(e)
        gen_msg = ('*******MYQ ERROR: DOOR NOT RESPONDING*****'+str(e))
else:
    gen_msg = ("""command unknown.)""")
return(gen_msg)


Solution 1:[1]

I'm having the same issue, but only when it's used inside Bottle as a web app. Almost the same code works fine from the command line every time. What I have noticed is that the door state gets stuck at either closing or opening after the first task is completed. Thereafter, in the same web service session, subsequent requests fail with "Session is closed". What's interesting is that the command line reports closed or open correctly.

I think this may be a bug in pymyq. Please check https://github.com/arraylabs/pymyq/issues/143.

Solution 2:[2]

This was a bug, and I fixed it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Glyph