Skip to content

Commit

Permalink
try auto reconnect of client
Browse files Browse the repository at this point in the history
  • Loading branch information
gluap committed Jun 1, 2020
1 parent e23c98d commit 0fbbaa4
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions pyess/essmqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sys

from asyncio_mqtt import Client
import asyncio_mqtt.error

from distutils.util import strtobool

Expand Down Expand Up @@ -35,8 +36,16 @@ async def recursive_publish_dict(mqtt_client, publish_root, dictionary):
else:
try:
await mqtt_client.publish(f"{publish_root}/{key}", value)
except asyncio_mqtt.error.MqttCodeError: # pragma: no cover
logger.warning(f"exception while publishing {value} to {publish_root}/{key}, reconnecting mqtt",
exc_info=True)
await mqtt_client.disconnect()
await mqtt_client.connect()
logger.info("reconnected")
except:
logger.exception(f"exception while publishing {value} to {publish_root}/{key}")
logger.exception(f"exception while publishing {value} to {publish_root}/{key}, please report bug",
exc_info=True)
raise


async def send_loop(ess, mqtt_client=None, graphite_client=None, once=False, interval_seconds=10, common_divisor=1):
Expand All @@ -63,7 +72,7 @@ def prepare_description(sensor):
if "power" in sensor:
description["device_class"] = "power"
description["unit_of_measurement"] = "W"
if "enegy" in sensor or "energy" in sensor: # typo in ess json
if "enegy" in sensor or "energy" in sensor: # typo in ess json
description["unit_of_measuremnt"] = "Wh"
description["icon"] = "mdi:gauge"
if "soc" in sensor:
Expand All @@ -82,8 +91,19 @@ async def announce_loop(client, once=False, sensors=None):
return
while True:
for sensor in sensors:
await client.publish(f"homeassistant/sensor/{sensor.replace('/','')}/config",
json.dumps(prepare_description(sensor)))
try:
await client.publish(f"homeassistant/sensor/{sensor.replace('/', '')}/config",
json.dumps(prepare_description(sensor)))
except asyncio_mqtt.error.MqttCodeError: # pragma: no cover
logger.warning(f"exception while publishing hass config, reconnecting mqtt",
exc_info=True)
await client.disconnect()
await client.connect()
logger.info("reconnected")
except: # pragma: no cover
logger.exception(f"exception while publishing hass config, please report bug",
exc_info=True)
raise
if not once:
await asyncio.sleep(120)
else:
Expand Down Expand Up @@ -121,7 +141,6 @@ async def _main(arguments=None):
"for homassistant autconfig")

args = parser.parse_args(arguments)

if args.ess_host is None:
ip, name = autodetect_ess()
else:
Expand Down

0 comments on commit 0fbbaa4

Please sign in to comment.