-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
210 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
MIT License | ||
Copyright (c) 2017 Egor Tsinko | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
# pymonoprice | ||
Python3 interface implementation for Monoprice 6 zone amplifier | ||
|
||
## Notes | ||
This is for use with [Home-Assistant](http://home-assistant.io) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import re | ||
import serial | ||
from threading import RLock | ||
|
||
ZONE_PATTERN = re.compile('#>(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)') | ||
|
||
EOL = b'\r\n#' | ||
LEN_EOL = len(EOL) | ||
|
||
|
||
class ZoneStatus(object): | ||
def __init__(self, | ||
zone: int, | ||
pa:bool, | ||
power:bool, | ||
mute:bool, | ||
do_not_disturb: bool, | ||
volume:int, # 0 - 38 | ||
treble:int, # 0 -> -7, 14-> +7 | ||
bass:int, # 0 -> -7, 14-> +7 | ||
balance:int, # 00 - left, 10 - center, 20 right | ||
source:int, | ||
keypad:bool): | ||
self.zone = zone | ||
self.pa = bool(pa) | ||
self.power = bool(power) | ||
self.mute = bool(mute) | ||
self.do_not_disturb = bool(do_not_disturb) | ||
self.volume = volume | ||
self.treble = treble | ||
self.bass = bass | ||
self.balance = balance | ||
self.source = source | ||
self.keypad = bool(keypad) | ||
|
||
@classmethod | ||
def from_string(self, string: str): | ||
if not string: | ||
return None | ||
return ZoneStatus(*[int(m) for m in re.search(ZONE_PATTERN, string).groups()]) | ||
|
||
|
||
class Monoprice(object): | ||
def __init__(self, port_url): | ||
self._lock = RLock() | ||
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 = 2 | ||
self._port.write_timeout = 2 | ||
self._port.open() | ||
|
||
def _process_request(self, request): | ||
with self._lock: | ||
# clear | ||
self._port.flush() | ||
self._port.reset_output_buffer() | ||
self._port.reset_input_buffer() | ||
# send | ||
self._port.write(request.encode()) | ||
# receive | ||
result = bytearray() | ||
while True: | ||
c = self._port.read(1) | ||
if not c: | ||
raise serial.SerialTimeoutException('Connection timed out! Last received bytes {}'.format([hex(a) for a in result])) | ||
result += c | ||
# Ignore first 3 bytes as they will identical to EOL! | ||
if len(result) > 3 and result[-LEN_EOL:] == EOL: | ||
break | ||
return bytes(result).decode('ascii') | ||
|
||
def zone_status(self, zone: int): | ||
""" | ||
Get the structure representing the status of the zone | ||
:param zone: zone 11..16, 21..26, 31..36 | ||
:return: status of the zone or None | ||
""" | ||
return ZoneStatus.from_string(self._process_request('?{}\r'.format(zone))) | ||
|
||
def set_power(self, zone: int, power: bool): | ||
""" | ||
Turns zone on or off | ||
:param zone: zone 11..16, 21..26, 31..36 | ||
:param power: True to turn on, False to turn off | ||
""" | ||
self._process_request('<{}PR{}\r'.format(zone, '01' if power else '00')) | ||
|
||
def set_mute(self, zone: int, mute: bool): | ||
""" | ||
Mutes zone on or off | ||
:param zone: zone 11..16, 21..26, 31..36 | ||
:param mute: True to mute, False to unmute | ||
""" | ||
self._process_request('<{}MU{}\r'.format(zone, '01' if mute else '00')) | ||
|
||
def set_volume(self, zone: int, volume: int): | ||
""" | ||
Sets volume for zone | ||
:param zone: zone 11..16, 21..26, 31..36 | ||
:param volume: integer from 0 to 38 inclusive | ||
""" | ||
volume = max(0, min(volume, 38)) | ||
self._process_request('<{}VO{:02}\r'.format(zone, volume)) | ||
|
||
def set_treble(self, zone: int, treble: int): | ||
""" | ||
Sets treble for zone | ||
:param zone: zone 11..16, 21..26, 31..36 | ||
:param treble: integer from 0 to 14 inclusive, where 0 is -7 treble and 14 is +7 | ||
""" | ||
treble = max(0, min(treble, 14)) | ||
self._process_request('<{}TR{:02}\r'.format(zone, treble)) | ||
|
||
def set_bass(self, zone: int, bass: int): | ||
""" | ||
Sets bass for zone | ||
:param zone: zone 11..16, 21..26, 31..36 | ||
:param bass: integer from 0 to 14 inclusive, where 0 is -7 bass and 14 is +7 | ||
""" | ||
bass = max(0, min(bass, 14)) | ||
self._process_request('<{}BS{:02}\r'.format(zone, bass)) | ||
|
||
def set_balance(self, zone: int, balance: int): | ||
""" | ||
Sets balance for zone | ||
:param zone: zone 11..16, 21..26, 31..36 | ||
:param balance: integer from 0 to 20 inclusive, where 0 is -10(left), 0 is center and 20 is +10 (right) | ||
""" | ||
balance = max(0, min(balance, 20)) | ||
self._process_request('<{}BL{:02}\r'.format(zone, balance)) | ||
|
||
def set_source(self, zone: int, source: int): | ||
""" | ||
Sets source for zone | ||
:param zone: zone 11..16, 21..26, 31..36 | ||
:param source: integer from 0 to 6 inclusive | ||
""" | ||
source = max(1, min(source, 6)) | ||
self._process_request('<{}CH{:02}\r'.format(zone, source)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pyserial>=3.4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[metadata] | ||
description-file = README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#!/usr/bin/env python | ||
|
||
import os | ||
import sys | ||
|
||
try: | ||
from setuptools import setup | ||
except ImportError: | ||
from distutils.core import setup | ||
|
||
if sys.argv[-1] == 'publish': | ||
os.system('python setup.py sdist upload') | ||
sys.exit() | ||
|
||
license = """ | ||
MIT License | ||
Copyright (c) 2017 Egor Tsinko | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
""" | ||
|
||
setup(name='pymonoprice', | ||
version='0.1', | ||
description='Python API for talking to Monoprice 6-zone amplifier', | ||
url='https://github.com/etsinko/pymonoprice', | ||
download_url='https://github.com/etsinko/pymonoprice/archive/0.1.tar.gz', | ||
author='Egor Tsinko', | ||
author_email='[email protected]', | ||
license='MIT', | ||
install_requires=['pyserial>=3.4'], | ||
packages=['pymonoprice'], | ||
zip_safe=True) |