Skip to content

Commit

Permalink
fix: disconnect before conect to cleanup incase of failure
Browse files Browse the repository at this point in the history
  • Loading branch information
sopelj committed Jan 15, 2022
1 parent 348b7f1 commit 07e9763
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions custom_components/ember_mug/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,24 @@ def __init__(self, hass: HomeAssistant, config: ConfigEntry) -> None:
"serial_number": None,
"mug_id": None,
"last_read_time": None,
"firmware_info": {},
"firmware_version": None,
"model": "Ember Mug",
"mug_name": self.name,
"mug_name": self.mug.mug_name,
}

async def _run(self):
"""Start the task loop."""
try:
self._loop = True
_LOGGER.info(f"Starting mug loop {self.mac_address}")
# Make sure we're disconnected first
await self.mug.disconnect()
# Start loop
while self._loop:
await self.mug.ensure_connected()
await self.mug.update_all()
self.mug.updates_queued.clear()
await self.async_refresh()

# Maintain connection for 5min seconds until next update
# We will be notified of most changes during this time
for _ in range(150):
Expand All @@ -77,6 +79,7 @@ async def _run(self):

except Exception as e:
_LOGGER.error(f"An unexpected error occurred during loop {e}. Restarting.")
self._loop = False
await self.mug.disconnect()
self.hass.async_create_task(self._run())
finally:
Expand All @@ -88,8 +91,8 @@ async def _async_update_data(self):
"mug_id": self.mug.mug_id,
"serial_number": self.mug.serial_number,
"last_read_time": dt_util.utcnow(),
"firmware_info": self.mug.firmware_info,
"mug_name": self.name,
"firmware_version": str(self.mug.firmware_info.get("version", "")),
"mug_name": self.mug.mug_name,
"model": self.mug.model,
}
_LOGGER.debug(f"{data}")
Expand All @@ -103,7 +106,7 @@ def device_info(self) -> DeviceInfo:
identifiers={(DOMAIN, unique_id)},
name=self.data["mug_name"],
model=self.data["model"],
sw_version=str(self.data["firmware_info"].get("version", "")),
sw_version=self.data["firmware_version"],
manufacturer="Ember",
)

Expand Down

0 comments on commit 07e9763

Please sign in to comment.