diff --git a/src/energomera_hass_mqtt/mqtt_client.py b/src/energomera_hass_mqtt/mqtt_client.py index 5c297b8..b868b12 100644 --- a/src/energomera_hass_mqtt/mqtt_client.py +++ b/src/energomera_hass_mqtt/mqtt_client.py @@ -54,15 +54,21 @@ async def connect(self, *args, **kwargs): """ Connects to MQTT broker. Multiple calls will result only in single call to `connect()` method of - parent class, to allow the method to be called within a process loop - with no risk of constantly reinitializing MQTT broker connection. + parent class if the MQTT client needs a connection (not being connected + or got disconnected), to allow the method to be called within a process + loop with no risk of constantly reinitializing MQTT broker connection. :param args: Pass-through positional arguments for parent class :param kwargs: Pass-through keyword arguments for parent class """ - if not self._connected.done(): - await super().connect(*args, *kwargs) + if self._connected.done() and not self._disconnected.done(): + _LOGGER.info( + 'MQTT client is already connected, skipping subsequent attempt' + ) + return + + await super().connect(*args, *kwargs) def will_set(self, *args, **kwargs): """ diff --git a/tox.ini b/tox.ini index e546747..c781a58 100644 --- a/tox.ini +++ b/tox.ini @@ -16,26 +16,26 @@ isolated_build = true deps = check-manifest >= 0.42 flake8 - pylint - pytest - pytest-cov - pytest-asyncio - mock;python_version<"3.8" - freezegun - docker + pylint + pytest + pytest-cov + pytest-asyncio + mock;python_version<"3.8" + freezegun + docker setenv = - # Ensure the module under test will be found under `src/` directory, in - # case of any test command below will attempt importing it. In particular, - # it helps `coverage` to recognize test traces from the module under `src/` - # directory and report correct (aligned with repository layout) paths, not - # from the module installed by `tox` in the virtual environment (the traces - # will be referencing `tox` specific paths, not aligned with repository) + # Ensure the module under test will be found under `src/` directory, in + # case of any test command below will attempt importing it. In particular, + # it helps `coverage` to recognize test traces from the module under `src/` + # directory and report correct (aligned with repository layout) paths, not + # from the module installed by `tox` in the virtual environment (the traces + # will be referencing `tox` specific paths, not aligned with repository) PYTHONPATH = src passenv = - RUNNER_* - GITHUB_* + RUNNER_* + GITHUB_* allowlist_externals = - cat + cat commands = check-manifest --ignore 'tox.ini,tests/**,docs/**,.pylintrc,.readthedocs.yaml,sonar-project.properties,systemd/**',Dockerfile flake8 --tee --output-file=flake8.txt . @@ -44,8 +44,8 @@ commands = # installed by `tox` (see above for more details) pytest --cov=src/energomera_hass_mqtt --cov-append -v -s tests commands_post = - # Show the `pylint` report to the standard output, to ease fixing the issues reported - cat pylint.txt + # Show the `pylint` report to the standard output, to ease fixing the issues reported + cat pylint.txt [flake8] exclude = .tox,*.egg,build,data,scripts,docs