1 """Files to interact with an ESPHome dashboard."""
3 from __future__
import annotations
16 from .const
import DOMAIN
17 from .coordinator
import ESPHomeDashboardCoordinator
19 _LOGGER = logging.getLogger(__name__)
22 KEY_DASHBOARD_MANAGER =
"esphome_dashboard_manager"
24 STORAGE_KEY =
"esphome.dashboard"
29 """Set up the ESPHome dashboard."""
36 @singleton(KEY_DASHBOARD_MANAGER)
39 ) -> ESPHomeDashboardManager:
40 """Get the dashboard manager or create it."""
42 await manager.async_setup()
47 """Class to manage the dashboard and restore it from storage."""
49 def __init__(self, hass: HomeAssistant) ->
None:
50 """Initialize the dashboard manager."""
52 self._store: Store[dict[str, Any]] =
Store(hass, STORAGE_VERSION, STORAGE_KEY)
53 self.
_data_data: dict[str, Any] |
None =
None
58 """Restore the dashboard from storage."""
60 if (data := self.
_data_data)
and (info := data.get(
"info")):
62 info[
"addon_slug"], info[
"host"], info[
"port"]
66 def async_get(self) -> ESPHomeDashboardCoordinator | None:
67 """Get the current dashboard."""
71 self, addon_slug: str, host: str, port: int
73 """Set the dashboard info."""
74 url = f
"http://{host}:{port}"
75 hass = self.
_hass_hass
78 if cur_dashboard.addon_slug == addon_slug
and cur_dashboard.url == url:
82 await cur_dashboard.async_shutdown()
91 await dashboard.async_request_refresh()
95 async
def on_hass_stop(_: Event) ->
None:
96 await dashboard.async_shutdown()
99 EVENT_HOMEASSISTANT_STOP, on_hass_stop
102 new_data = {
"info": {
"addon_slug": addon_slug,
"host": host,
"port": port}}
103 if self.
_data_data != new_data:
107 hass.config_entries.async_reload(entry.entry_id)
108 for entry
in hass.config_entries.async_entries(DOMAIN)
109 if entry.state
is ConfigEntryState.LOADED
113 if dashboard.last_update_success:
115 hass.config_entries.flow.async_configure(flow[
"flow_id"])
116 for flow
in hass.config_entries.flow.async_progress()
117 if flow[
"handler"] == DOMAIN
118 and flow[
"context"][
"source"] == SOURCE_REAUTH
123 "Dashboard unavailable; skipping reauth: %s", dashboard.last_exception
127 "Reloading %d and re-authenticating %d", len(reloads), len(reauths)
129 if reloads
or reauths:
130 await asyncio.gather(*reloads, *reauths)
135 """Get an instance of the dashboard if set.
137 This is only safe to call after `async_setup` has been completed.
139 It should not be called from the config flow because there is a race
140 where manager can be an asyncio.Event instead of the actual manager
141 because the singleton decorator is not yet done.
143 manager: ESPHomeDashboardManager |
None = hass.data.get(KEY_DASHBOARD_MANAGER)
144 return manager.async_get()
if manager
else None
148 hass: HomeAssistant, addon_slug: str, host: str, port: int
150 """Set the dashboard info."""
152 await manager.async_set_dashboard_info(addon_slug, host, port)
ESPHomeDashboardCoordinator|None async_get(self)
None __init__(self, HomeAssistant hass)
None async_set_dashboard_info(self, str addon_slug, str host, int port)
ESPHomeDashboardManager async_get_or_create_dashboard_manager(HomeAssistant hass)
None async_set_dashboard_info(HomeAssistant hass, str addon_slug, str host, int port)
None async_setup(HomeAssistant hass)
ESPHomeDashboardCoordinator|None async_get_dashboard(HomeAssistant hass)
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)
None async_load(HomeAssistant hass)
None async_save(self, _T data)