Structure package and fix ApiError

This commit is contained in:
Nicolas Duhamel 2021-01-15 15:26:03 +01:00
parent 51486518b4
commit a009564b74
9 changed files with 37 additions and 11 deletions

8
.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Source for the following rules: https://raw.githubusercontent.com/github/gitignore/master/Python.gitignore
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
*.egg-info/

4
pyproject.toml Normal file
View File

@ -0,0 +1,4 @@
[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"

13
setup.cfg Normal file
View File

@ -0,0 +1,13 @@
[metadata]
name = yamaha2mqtt
version = 0.0.1
[options]
package_dir =
=src
packages = find_namespace:
# install_requires =
[options.packages.find]
where = src

2
setup.py Normal file
View File

@ -0,0 +1,2 @@
import setuptools
setuptools.setup()

View File

@ -16,7 +16,8 @@ def main(\
mqtt_port: int = typer.Option(... , envvar="YAMAHA_MQTT_PORT"),\
yamaha_host: str = typer.Option(... , envvar="YAMAHA_HOST"),\
senarios_file: Optional[Path] = typer.Option(None, exists=True,
file_okay=True, dir_okay=False, readable=True, resolve_path=True)\
file_okay=True, dir_okay=False, readable=True, resolve_path=True,
envvar="YAMAHA_SENARIOS")\
):
if senarios_file:

View File

@ -25,7 +25,6 @@ class Yamaha:
self._client.subscribe_callback('yamaha/cmnd/senario', self.handle_senario)
self._client.subscribe_callback('yamaha/cmnd/senario/available', self.handle_senario_available)
self._client.subscribe_callback('yamaha/cmnd/volume', self.handle_volume)
self._client.subscribe_callback('yamaha/cmnd/volume/params', self.handle_volume_params)
self._client.subscribe_callback('yamaha/cmnd/power', self.handle_power)
@property
@ -47,7 +46,7 @@ class Yamaha:
raise ConnectionTimeoutError()
r_json = r.json()
if r_json['response_code'] != 0:
raise ApiResponse(r.url, r_json['response_code'])
raise ApiError(r.url, r_json['response_code'])
return r_json
def search_senario(self, name: str):
@ -78,13 +77,6 @@ class Yamaha:
self._client.publish('yamaha/stat/senario/available',
json.dumps([s.__name__.lower() for s in self.available_senarios]))
def handle_volume_params(self, client, userdata, msg):
r = self.request(Zone().get_status('main'))
msg = {
'maximum': r['max_volume']
}
self._client.publish('yamaha/stat/volume/params', json.dumps(msg))
def handle_volume(self, client, userdata, msg):
param =msg.payload.decode().lower()
@ -96,7 +88,12 @@ class Yamaha:
self.request(Zone().set_volume('main', param, ''))
r = self.request(Zone().get_status('main'))
self._client.publish('yamaha/stat/volume', r['volume'])
msg = {
'volume_max': r['max_volume'],
'volume': r['volume'],
}
self._client.publish('yamaha/stat/volume', json.dumps(msg))
def handle_power(self, client, userdata, msg):
param =msg.payload.decode().lower()
@ -110,3 +107,4 @@ class Yamaha:
state = r['power']
state = 'off' if state == 'standby' else state
self._client.publish('yamaha/stat/power', state)