Skip to content

Commit

Permalink
Use built-in readline() instead of own function
Browse files Browse the repository at this point in the history
We don't need to read partial lines so we can make do with readline()
  • Loading branch information
Jalle19 committed Sep 9, 2024
1 parent ec8f323 commit cbed991
Showing 1 changed file with 2 additions and 12 deletions.
14 changes: 2 additions & 12 deletions custom_components/vinx/lw3.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,6 @@ def __init__(self, hostname: str, port: int, timeout: int = 5):
self._writer: StreamWriter | None = None
self._semaphore = asyncio.Semaphore()

async def _read_until(self, phrase: str) -> str | None:
b = bytearray()

while not self._reader.at_eof():
byte = await self._reader.read(1)
b += byte

if b.endswith(phrase.encode()):
return b.decode()

def connection(self):
return LW3ConnectionContext(self)

Expand All @@ -71,12 +61,12 @@ def parse_response(response: str) -> PropertyResponse | ErrorResponse:
return PropertyResponse(matches.group(1), matches.group(2), matches.group(3))

async def _read_and_parse_response(self) -> PropertyResponse:
response = await self._read_until("\r\n")
response = await self._reader.readline()

if response is None:
raise EOFError("Reached EOF while reading, connection probably lost")

result = self.parse_response(response.strip())
result = self.parse_response(response.decode().strip())

if isinstance(result, ErrorResponse):
raise ValueError(result)
Expand Down

0 comments on commit cbed991

Please sign in to comment.