From a88b20e172dd4b6774529b1d53da5701640dbff0 Mon Sep 17 00:00:00 2001 From: Michel van de Wetering Date: Fri, 29 Nov 2024 16:47:06 +0100 Subject: [PATCH] Use async contextmanager --- epson_projector/main.py | 2 +- epson_projector/projector_http.py | 4 ++-- epson_projector/projector_serial.py | 4 ++-- epson_projector/projector_tcp.py | 6 +++--- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/epson_projector/main.py b/epson_projector/main.py index 507cc4d..2276ee9 100644 --- a/epson_projector/main.py +++ b/epson_projector/main.py @@ -120,7 +120,7 @@ async def send_request(self, params, timeout, type='json_query', command=False): """Send request to Epson.""" try: - with async_timeout.timeout(timeout): + async with async_timeout.timeout(timeout): url = '{url}{type}'.format( url=self._http_url, type=type) diff --git a/epson_projector/projector_http.py b/epson_projector/projector_http.py index ef70f79..5802352 100644 --- a/epson_projector/projector_http.py +++ b/epson_projector/projector_http.py @@ -78,7 +78,7 @@ async def send_command(self, command, timeout): async def send_request(self, params, timeout, type=JSON_QUERY): """Send request to Epson.""" try: - with async_timeout.timeout(timeout): + async with async_timeout.timeout(timeout): url = "{url}{type}".format(url=self._http_url, type=type) async with self.websession.get( url=url, params=params, headers=self._headers @@ -101,7 +101,7 @@ async def get_serial(self): """Send TCP request for serial to Epson.""" if not self._serial: try: - with async_timeout.timeout(10): + async with async_timeout.timeout(10): power_on = await self.get_property(POWER, get_timeout(POWER)) if power_on == EPSON_CODES[POWER]: reader, writer = await asyncio.open_connection( diff --git a/epson_projector/projector_serial.py b/epson_projector/projector_serial.py index 3292a54..ad20194 100644 --- a/epson_projector/projector_serial.py +++ b/epson_projector/projector_serial.py @@ -42,7 +42,7 @@ async def async_init(self): except: pass try: - with async_timeout.timeout(DEFAULT_TIMEOUT): + async with async_timeout.timeout(DEFAULT_TIMEOUT): ( self._reader, self._writer, @@ -110,7 +110,7 @@ async def send_request(self, timeout, command): await self.async_init() if self._writer and self._isOpen and command: try: - with async_timeout.timeout(timeout): + async with async_timeout.timeout(timeout): _LOGGER.debug("Sent to Epson: %r with timeout %d", command, timeout) self._writer.write(command.encode()) response = await self._reader.readuntil(COLON.encode()) diff --git a/epson_projector/projector_tcp.py b/epson_projector/projector_tcp.py index 736fd3f..08d2207 100644 --- a/epson_projector/projector_tcp.py +++ b/epson_projector/projector_tcp.py @@ -43,7 +43,7 @@ def __init__(self, host, port=3629): async def async_init(self): """Async init to open connection with projector.""" try: - with async_timeout.timeout(10): + async with async_timeout.timeout(10): self._reader, self._writer = await asyncio.open_connection( host=self._host, port=self._port, loop=self._loop ) @@ -97,7 +97,7 @@ async def send_request(self, timeout, command, bytes_to_read=16): await self.async_init() if self._isOpen and command: bytes_to_read = bytes_to_read if bytes_to_read else 16 - with async_timeout.timeout(timeout): + async with async_timeout.timeout(timeout): self._writer.write(command.encode()) response = await self._reader.read(bytes_to_read) response = response.decode().replace(CR_COLON, "") @@ -109,7 +109,7 @@ async def get_serial(self): """Send TCP request for serial to Epson.""" if not self._serial: try: - with async_timeout.timeout(10): + async with async_timeout.timeout(10): power_on = await self.get_property(POWER, get_timeout(POWER)) if power_on == EPSON_CODES[POWER]: reader, writer = await asyncio.open_connection(