Skip to content

Commit

Permalink
Merge pull request #9 from hostcc/bugfix/reconnect
Browse files Browse the repository at this point in the history
Fixed (re)connecting to MQTT broker if no active connection exists or one got disconnected
  • Loading branch information
hostcc authored Sep 11, 2022
2 parents 47c9bad + da92677 commit d01dd4c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
14 changes: 10 additions & 4 deletions src/energomera_hass_mqtt/mqtt_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
36 changes: 18 additions & 18 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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 .
Expand All @@ -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
Expand Down

0 comments on commit d01dd4c

Please sign in to comment.