From a009564b7466ab0da989355570a2707c228cc2fe Mon Sep 17 00:00:00 2001 From: Nicolas Duhamel Date: Fri, 15 Jan 2021 15:26:03 +0100 Subject: [PATCH] Structure package and fix ApiError --- .gitignore | 8 ++++++++ pyproject.toml | 4 ++++ setup.cfg | 13 +++++++++++++ setup.py | 2 ++ __init__.py => src/yamaha2mqtt/__init__.py | 0 __main__.py => src/yamaha2mqtt/__main__.py | 3 ++- app.py => src/yamaha2mqtt/app.py | 18 ++++++++---------- error.py => src/yamaha2mqtt/error.py | 0 senario.py => src/yamaha2mqtt/senario.py | 0 9 files changed, 37 insertions(+), 11 deletions(-) create mode 100644 .gitignore create mode 100644 pyproject.toml create mode 100644 setup.cfg create mode 100644 setup.py rename __init__.py => src/yamaha2mqtt/__init__.py (100%) rename __main__.py => src/yamaha2mqtt/__main__.py (96%) rename app.py => src/yamaha2mqtt/app.py (88%) rename error.py => src/yamaha2mqtt/error.py (100%) rename senario.py => src/yamaha2mqtt/senario.py (100%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c10d448 --- /dev/null +++ b/.gitignore @@ -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/ diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..1870a2e --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,4 @@ +[build-system] +requires = ["setuptools", "wheel"] +build-backend = "setuptools.build_meta" + diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..8e25dff --- /dev/null +++ b/setup.cfg @@ -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 + diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..a4f49f9 --- /dev/null +++ b/setup.py @@ -0,0 +1,2 @@ +import setuptools +setuptools.setup() diff --git a/__init__.py b/src/yamaha2mqtt/__init__.py similarity index 100% rename from __init__.py rename to src/yamaha2mqtt/__init__.py diff --git a/__main__.py b/src/yamaha2mqtt/__main__.py similarity index 96% rename from __main__.py rename to src/yamaha2mqtt/__main__.py index 88e1bcb..04c19ad 100644 --- a/__main__.py +++ b/src/yamaha2mqtt/__main__.py @@ -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: diff --git a/app.py b/src/yamaha2mqtt/app.py similarity index 88% rename from app.py rename to src/yamaha2mqtt/app.py index a73fcca..bac2c23 100644 --- a/app.py +++ b/src/yamaha2mqtt/app.py @@ -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) + diff --git a/error.py b/src/yamaha2mqtt/error.py similarity index 100% rename from error.py rename to src/yamaha2mqtt/error.py diff --git a/senario.py b/src/yamaha2mqtt/senario.py similarity index 100% rename from senario.py rename to src/yamaha2mqtt/senario.py