Serve the Day Planner card JS and register it as a Lovelace resource.
Source code in custom_components/sunriser/__init__.py
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153 | async def async_setup(hass: HomeAssistant, config: dict[str, Any]) -> bool:
"""Serve the Day Planner card JS and register it as a Lovelace resource."""
await hass.http.async_register_static_paths(
[StaticPathConfig(_CARD_URL, str(_CARD_PATH), cache_headers=False)]
)
async def _register(_event: Event | None = None) -> None:
lovelace = hass.data.get("lovelace")
if lovelace is None:
_LOGGER.warning(
"SunRiser: lovelace not available, falling back to add_extra_js_url"
)
add_extra_js_url(hass, f"{_CARD_URL}?v={_CARD_VERSION}")
return
resources = lovelace.resources
await resources.async_get_info()
url_versioned = f"{_CARD_URL}?v={_CARD_VERSION}"
for item in resources.async_items():
item_url: str = item.get("url", "")
if item_url.split("?")[0] == _CARD_URL:
if item_url != url_versioned and isinstance(
resources, ResourceStorageCollection
):
await resources.async_update_item(
item["id"], {"res_type": "module", "url": url_versioned}
)
return
if isinstance(resources, ResourceStorageCollection):
await resources.async_create_item(
{"res_type": "module", "url": url_versioned}
)
_LOGGER.debug("SunRiser: registered Day Planner card as Lovelace resource")
else:
add_extra_js_url(hass, url_versioned)
if hass.state == CoreState.running:
await _register()
else:
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED, _register)
_register_services(hass)
return True
|