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

Use async contextmanager for timeouts #23

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion epson_projector/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions epson_projector/projector_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions epson_projector/projector_serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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())
Expand Down
6 changes: 3 additions & 3 deletions epson_projector/projector_tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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, "")
Expand All @@ -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(
Expand Down