Skip to content

Commit

Permalink
Add reload service
Browse files Browse the repository at this point in the history
  • Loading branch information
arturpragacz committed Aug 17, 2024
1 parent 40b6b56 commit ddaa8e4
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ arturs_labels:

```

Each time you make changes to the configuration, you have to restart Home Assistant.
Each time you make changes to the configuration, you have to reload the component or restart Home Assistant.

### Install prerequisites

Expand Down
2 changes: 1 addition & 1 deletion custom_components/arturs_labels/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ arturs_labels:

```

Each time you make changes to the configuration, you have to restart Home Assistant.
Each time you make changes to the configuration, you have to reload the component or restart Home Assistant.

### Install prerequisites

Expand Down
34 changes: 33 additions & 1 deletion custom_components/arturs_labels/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

import voluptuous as vol

from homeassistant.core import HomeAssistant
from homeassistant.const import SERVICE_RELOAD
from homeassistant.core import HomeAssistant, ServiceCall
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.reload import async_integration_yaml_config
from homeassistant.helpers.service import async_register_admin_service
from homeassistant.helpers.typing import ConfigType

from .overrides import (
Expand Down Expand Up @@ -54,12 +57,41 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
# lr has to be loaded first, because others depend on it
lr.async_load(hass, labels_parents)

# dr has to be loaded before er, so that callbacks
# to lr.EVENT_LABEL_REGISTRY_ANCESTRY_UPDATED fire in correct order
dr.async_load(hass)

er.async_load(hass)
ar.async_load(hass)

con_lr.async_setup(hass)
con_dr.async_setup(hass)
con_er.async_setup(hass)

async def _handle_reload(service_call: ServiceCall) -> None:
await async_reload(hass)
hass.bus.async_fire(f"event_{DOMAIN}_reloaded", context=service_call.context)

async_register_admin_service(
hass,
DOMAIN,
SERVICE_RELOAD,
_handle_reload,
)

return True


async def async_reload(hass: HomeAssistant) -> None:
"""Reload component."""
config = await async_integration_yaml_config(hass, DOMAIN)

if config is None or DOMAIN not in config:
return

labels_parents = {}
for label_id, label_data in config[DOMAIN]["labels"].items():
labels_parents[label_id] = label_data["parents"]

lab_reg = lr.async_get(hass)
lab_reg.async_load_parents(labels_parents)
2 changes: 1 addition & 1 deletion custom_components/arturs_labels/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"domain": "arturs_labels",
"name": "Artur's Labels",
"version": "0.1.0",
"version": "0.2.0",
"codeowners": ["@arturpragacz"],
"integration_type": "system",
"quality_scale": "internal"
Expand Down
1 change: 1 addition & 0 deletions custom_components/arturs_labels/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
reload:

0 comments on commit ddaa8e4

Please sign in to comment.