Skip to content
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
27 changes: 14 additions & 13 deletions pymonoprice/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import asyncio
import logging
import re
import serial
import serialx
from dataclasses import dataclass
from functools import wraps
from serial_asyncio_fast import create_serial_connection, SerialTransport
from threading import RLock
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -123,13 +122,15 @@ def __init__(self, port_url: str, lock: RLock) -> None:
Monoprice amplifier interface
"""
self._lock = lock
self._port = serial.serial_for_url(port_url, do_not_open=True)
self._port.baudrate = 9600
self._port.stopbits = serial.STOPBITS_ONE
self._port.bytesize = serial.EIGHTBITS
self._port.parity = serial.PARITY_NONE
self._port.timeout = TIMEOUT
self._port.write_timeout = TIMEOUT
self._port = serialx.Serial(
port_url,
baudrate=9600,
stopbits=serialx.StopBits.ONE,
byte_size=8,
parity=serialx.Parity.NONE,
read_timeout=TIMEOUT,
write_timeout=TIMEOUT,
)
self._port.open()

def _send_request(self, request: bytes) -> None:
Expand Down Expand Up @@ -157,7 +158,7 @@ def _process_request(self, request: bytes, num_eols_to_read: int = 1) -> str:
while True:
c = self._port.read(1)
if not c:
raise serial.SerialTimeoutException(
raise serialx.SerialTimeoutException(
"Connection timed out! Last received bytes {}".format(
[hex(a) for a in result]
)
Expand Down Expand Up @@ -394,11 +395,11 @@ def __init__(self) -> None:
super().__init__()
self._lock = asyncio.Lock()
self._tasks: set[asyncio.Task[None]] = set()
self._transport: SerialTransport = None
self._transport: serialx.SerialTransport = None
self._connected = asyncio.Event()
self.q: asyncio.Queue[bytes] = asyncio.Queue()

def connection_made(self, transport: SerialTransport) -> None:
def connection_made(self, transport: serialx.SerialTransport) -> None:
self._transport = transport
self._connected.set()
_LOGGER.debug("port opened %s", self._transport)
Expand Down Expand Up @@ -518,7 +519,7 @@ async def get_async_monoprice(port_url: str) -> MonopriceAsync:
lock = asyncio.Lock()

loop = asyncio.get_running_loop()
_, protocol = await create_serial_connection(
_, protocol = await serialx.create_serial_connection(
loop, MonopriceProtocol, port_url, baudrate=9600
)
return MonopriceAsync(protocol, lock)
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
pyserial>=3.4
pyserial-asyncio-fast>=0.16
serialx>=1.4.1
7 changes: 1 addition & 6 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ packages =
python_requires >= 3.9
zip_safe = True
install_requires =
pyserial>=3.4
pyserial-asyncio-fast>=0.16
serialx>=1.4.1

[options.package_data]
pymonoprice = py.typed
Expand All @@ -42,7 +41,3 @@ warn_redundant_casts = true
warn_unreachable = true
warn_unused_configs = True
warn_unused_ignores = true
[mypy-serial]
ignore_missing_imports = True
[mypy-serial_asyncio_fast]
ignore_missing_imports = True
5 changes: 2 additions & 3 deletions tests/test_monoprice.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import unittest

import serial

import pymonoprice
import serialx
from pymonoprice import (get_monoprice, get_async_monoprice, ZoneStatus)
from tests import create_dummy_port
import asyncio
Expand Down Expand Up @@ -324,7 +323,7 @@ def test_restore_zone(self):
self.assertEqual(0, len(self.responses))

def test_timeout(self):
with self.assertRaises(serial.SerialTimeoutException):
with self.assertRaises(serialx.SerialTimeoutException):
self.monoprice.set_source(3, 3)


Expand Down