Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lingering timer in esphome #92533

Merged
merged 3 commits into from
May 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions homeassistant/components/esphome/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from esphome_dashboard_api import ConfiguredDevice, ESPHomeDashboardAPI

from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntryState
from homeassistant.core import HomeAssistant, callback
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
from homeassistant.core import Event, HomeAssistant, callback
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

Expand All @@ -31,13 +32,13 @@ async def async_set_dashboard_info(
"""Set the dashboard info."""
url = f"http://{host}:{port}"

# Do nothing if we already have this data.
if (
(cur_dashboard := hass.data.get(KEY_DASHBOARD))
and cur_dashboard.addon_slug == addon_slug
and cur_dashboard.url == url
):
return
if cur_dashboard := async_get_dashboard(hass):
if cur_dashboard.addon_slug == addon_slug and cur_dashboard.url == url:
# Do nothing if we already have this data.
return
# Clear and make way for new dashboard
await cur_dashboard.async_shutdown()
del hass.data[KEY_DASHBOARD]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am clearing the dashboard in case the new one fails on async_request_refresh.

Another option would be to only shutdown/clear the old one after the new succeeds but I think this is cleaner.

Copy link
Contributor Author

@epenet epenet May 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also wonder if this should be included in a 2023.5.x patch release, if dashboards are recreated often.
cc @balloob

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dashboards are not recreated often, so not a really big deal.


dashboard = ESPHomeDashboard(hass, addon_slug, url, async_get_clientsession(hass))
try:
Expand All @@ -48,6 +49,11 @@ async def async_set_dashboard_info(

hass.data[KEY_DASHBOARD] = dashboard

async def on_hass_stop(_: Event) -> None:
await dashboard.async_shutdown()

hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, on_hass_stop)

reloads = [
hass.config_entries.async_reload(entry.entry_id)
for entry in hass.config_entries.async_entries(DOMAIN)
Expand Down