Skip to content

Commit 92001f3

Browse files
committed
Fix handling of command output missing trailing newline
1 parent f758fb8 commit 92001f3

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

  • custom_components/xiaomi_gateway3/core/shell

custom_components/xiaomi_gateway3/core/shell/base.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,16 @@ async def write(self, data: bytes, until: bytes, timeout: float) -> bytes:
2222

2323
async def exec(self, command: str, as_bytes=False, timeout=10) -> str | bytes:
2424
"""Run command and return it result."""
25-
self.writer.write(command.encode() + b"\n")
26-
coro = self.reader.readuntil(b"# ")
27-
raw = await asyncio.wait_for(coro, timeout=timeout)
28-
return raw[:-2] if as_bytes else raw[:-2].decode()
25+
self.writer.write((command + "\r\n").encode())
26+
raw = bytearray()
27+
while buffer := await asyncio.wait_for(
28+
self.reader.read(1_000_000), timeout=timeout
29+
):
30+
raw += buffer
31+
if raw.endswith(b"# "):
32+
raw = raw[:-2]
33+
break
34+
return bytes(raw) if as_bytes else raw.decode()
2935

3036
async def read_file(self, filename: str, as_base64=False, tail=None):
3137
command = f"tail -c {tail} {filename}" if tail else f"cat {filename}"

0 commit comments

Comments
 (0)