Skip to content

Commit

Permalink
Fix blocking IO in send_message (#332)
Browse files Browse the repository at this point in the history
* Fix blocking IO in send_message

* Move to a method
  • Loading branch information
thecode authored Jul 4, 2024
1 parent dfb8b51 commit 102b391
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions aiowebostv/webos_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,24 +694,28 @@ async def scroll(self, d_x, d_y):
message = f"type:scroll\ndx:{d_x}\ndy:{d_y}\n\n"
await self.input_command(message)

def read_icon(self, icon_path, message_payload):
"""Read icon & set data in message payload."""
message_payload["iconExtension"] = os.path.splitext(icon_path)[1][1:]
with open(icon_path, "rb") as icon_file:
message_payload["iconData"] = base64.b64encode(icon_file.read()).decode(
"ascii"
)

async def send_message(self, message, icon_path=None):
"""Show a floating message."""
icon_encoded_string = ""
icon_extension = ""
message_payload = {
"message": message,
"iconData": "",
"iconExtension": "",
}

if icon_path is not None:
icon_extension = os.path.splitext(icon_path)[1][1:]
with open(icon_path, "rb") as icon_file:
icon_encoded_string = base64.b64encode(icon_file.read()).decode("ascii")

return await self.request(
ep.SHOW_MESSAGE,
{
"message": message,
"iconData": icon_encoded_string,
"iconExtension": icon_extension,
},
)
await self._loop.run_in_executor(
None, self.read_icon, icon_path, message_payload
)

return await self.request(ep.SHOW_MESSAGE, message_payload)

async def get_power_state(self):
"""Get current power state."""
Expand Down

0 comments on commit 102b391

Please sign in to comment.