Skip to content

Commit

Permalink
Lovelace in integration
Browse files Browse the repository at this point in the history
  • Loading branch information
arjenbos committed Oct 3, 2024
1 parent e8e3ea2 commit af7af3a
Show file tree
Hide file tree
Showing 11 changed files with 14,344 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ env
.coverage
coverage.xml
.secrets
.pytest_cache
.pytest_cache
node_modules
70 changes: 69 additions & 1 deletion custom_components/postnl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import urllib3
from aiohttp.client_exceptions import ClientError, ClientResponseError
from gql.transport.exceptions import TransportQueryError
from homeassistant.components.frontend import add_extra_js_url
from homeassistant.components.http import StaticPathConfig
from homeassistant.components.lovelace.resources import ResourceStorageCollection
from homeassistant.config_entries import ConfigEntry, ConfigEntryState
from homeassistant.const import CONF_ACCESS_TOKEN
from homeassistant.core import HomeAssistant
Expand All @@ -14,7 +17,7 @@
from homeassistant.helpers.config_entry_oauth2_flow import (
OAuth2Session, async_get_config_entry_implementation)

from .const import DOMAIN, PLATFORMS
from .const import DOMAIN, PLATFORMS, VERSION
from .graphql import PostNLGraphql
from .login_api import PostNLLoginAPI

Expand Down Expand Up @@ -56,8 +59,73 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> True:

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)


# if not hass.http.app.router.get('/postnl/lovelace'):
# hass.http.register_static_path(
# url_path='/postnl/lovelace',
# path=hass.config.path("custom_components/postnl/lovelace"),
# cache_headers=True
# )

if not await path_registered(hass=hass, url='/postnl/lovelace.js'):
await hass.http.async_register_static_paths([
StaticPathConfig(
url_path='/postnl/lovelace.js',
path=hass.config.path("custom_components/postnl/lovelace.js"),
cache_headers=True
)
])

await init_resource(
hass=hass,
url='/postnl/lovelace.js',
ver=VERSION
)

return True

async def path_registered(hass: HomeAssistant, url: str):
for resource in hass.http.app.router.resources():
if url in resource.canonical:
return True

return False

async def init_resource(hass: HomeAssistant, url: str, ver: str) -> bool:
resources: ResourceStorageCollection = hass.data["lovelace"]["resources"]
# force load storage
await resources.async_get_info()

url2 = f"{url}?v={ver}"

for item in resources.async_items():
if not item.get("url", "").startswith(url):
continue

# no need to update
if item["url"].endswith(ver):
return False

_LOGGER.debug(f"Update lovelace resource to: {url2}")

if isinstance(resources, ResourceStorageCollection):
await resources.async_update_item(
item["id"], {"res_type": "module", "url": url2}
)
else:
# not the best solution, but what else can we do
item["url"] = url2

return True

if isinstance(resources, ResourceStorageCollection):
_LOGGER.debug(f"Add new lovelace resource: {url2}")
await resources.async_create_item({"res_type": "module", "url": url2})
else:
_LOGGER.debug(f"Add extra JS module: {url2}")
add_extra_js_url(hass, url2)

return True

async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload PostNL config entry."""
Expand Down
1 change: 1 addition & 0 deletions custom_components/postnl/const.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from homeassistant.const import Platform

DOMAIN = "postnl"
VERSION = "1.4.0"
POSTNL_CLIENT_ID = "deb0a372-6d72-4e09-83fe-997beacbd137"
POSTNL_AUTH_URL = "https://login.postnl.nl/101112a0-4a0f-4bbb-8176-2f1b2d370d7c/login/authorize"
POSTNL_TOKEN_URL = "https://login.postnl.nl/101112a0-4a0f-4bbb-8176-2f1b2d370d7c/login/token"
Expand Down
368 changes: 368 additions & 0 deletions custom_components/postnl/lovelace.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion custom_components/postnl/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/arjenbos/ha-postnl/issues",
"requirements": ["gql"],
"version": "1.3.0"
"version": "1.4.0"
}
1 change: 1 addition & 0 deletions custom_components/postnl/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.helpers.entity_registry import async_get as async_get_entity_registry
from homeassistant.components.frontend import add_extra_js_url, async_register_built_in_panel

from . import DOMAIN
from .coordinator import PostNLCoordinator
Expand Down
2 changes: 1 addition & 1 deletion hacs.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "PostNL",
"render_readme": true,
"homeassistant": "2023.12.0"
"homeassistant": "2024.9.0"
}
Loading

0 comments on commit af7af3a

Please sign in to comment.