Skip to content

Commit

Permalink
Support YAML Reload
Browse files Browse the repository at this point in the history
  • Loading branch information
Snuffy2 committed Jul 20, 2024
1 parent f2efd10 commit b16b3bf
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions custom_components/variable/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Variable implementation for Home Assistant."""

import contextlib
import copy
import json
import logging
Expand All @@ -10,10 +11,13 @@
CONF_FRIENDLY_NAME,
CONF_ICON,
CONF_NAME,
SERVICE_RELOAD,
Platform,
)
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.reload import async_integration_yaml_config
from homeassistant.helpers.typing import ConfigType
import voluptuous as vol

Expand Down Expand Up @@ -68,7 +72,7 @@
async def async_setup(hass: HomeAssistant, config: ConfigType):
"""Set up the Variable services."""

async def async_set_variable_legacy_service(call):
async def async_set_variable_legacy_service(call: ServiceCall):
"""Handle calls to the set_variable legacy service."""

# _LOGGER.debug(f"[async_set_variable_legacy_service] Pre call data: {call.data}")
Expand All @@ -77,7 +81,7 @@ async def async_set_variable_legacy_service(call):
# _LOGGER.debug(f"[async_set_variable_legacy_service] Post call data: {call.data}")
await _async_set_legacy_service(call, var_ent)

async def async_set_entity_legacy_service(call):
async def async_set_entity_legacy_service(call: ServiceCall):
"""Handle calls to the set_entity legacy service."""

# _LOGGER.debug(f"[async_set_entity_legacy_service] call data: {call.data}")
Expand All @@ -102,6 +106,17 @@ async def _async_set_legacy_service(call, var_ent):
DOMAIN, SERVICE_UPDATE_SENSOR, service_data=update_sensor_data
)

async def _async_reload_service_handler(service: ServiceCall):
"""Handle reload service call."""
_LOGGER.info("Service %s.reload called: reloading YAML integration", DOMAIN)
reload_config = None
with contextlib.suppress(HomeAssistantError):
reload_config = await async_integration_yaml_config(hass, DOMAIN)
if reload_config is None:
return
_LOGGER.debug(f" reload_config: {reload_config}")
await _async_process_yaml(hass, reload_config)

hass.services.async_register(
DOMAIN,
SERVICE_SET_VARIABLE_LEGACY,
Expand All @@ -115,7 +130,12 @@ async def _async_set_legacy_service(call, var_ent):
async_set_entity_legacy_service,
schema=SERVICE_SET_ENTITY_LEGACY_SCHEMA,
)
hass.services.async_register(DOMAIN, SERVICE_RELOAD, _async_reload_service_handler)

return await _async_process_yaml(hass, config)


async def _async_process_yaml(hass: HomeAssistant, config: ConfigType) -> bool:
variables = json.loads(json.dumps(config.get(DOMAIN, {})))

for var, var_fields in variables.items():
Expand Down

0 comments on commit b16b3bf

Please sign in to comment.