Skip to content

Commit

Permalink
Move lcn non-config_entry related code to async_setup (home-assistant…
Browse files Browse the repository at this point in the history
…#130603)

* Move non-config_entry related code to async_setup

* Remove action unload
  • Loading branch information
alengwenus authored Nov 14, 2024
1 parent eea782b commit 3d84e35
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
32 changes: 15 additions & 17 deletions homeassistant/components/lcn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
Platform,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers import config_validation as cv, device_registry as dr
from homeassistant.helpers.typing import ConfigType

from .const import (
ADD_ENTITIES_CALLBACKS,
Expand All @@ -41,15 +42,26 @@
register_lcn_address_devices,
register_lcn_host_device,
)
from .services import SERVICES
from .services import register_services
from .websocket import register_panel_and_ws_api

_LOGGER = logging.getLogger(__name__)

CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)


async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the LCN component."""
hass.data.setdefault(DOMAIN, {})

await register_services(hass)
await register_panel_and_ws_api(hass)

return True


async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
"""Set up a connection to PCHK host from a config entry."""
hass.data.setdefault(DOMAIN, {})
if config_entry.entry_id in hass.data[DOMAIN]:
return False

Expand Down Expand Up @@ -109,15 +121,6 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
)
lcn_connection.register_for_inputs(input_received)

# register service calls
for service_name, service in SERVICES:
if not hass.services.has_service(DOMAIN, service_name):
hass.services.async_register(
DOMAIN, service_name, service(hass).async_call_service, service.schema
)

await register_panel_and_ws_api(hass)

return True


Expand Down Expand Up @@ -168,11 +171,6 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
host = hass.data[DOMAIN].pop(config_entry.entry_id)
await host[CONNECTION].async_close()

# unregister service calls
if unload_ok and not hass.data[DOMAIN]: # check if this is the last entry to unload
for service_name, _ in SERVICES:
hass.services.async_remove(DOMAIN, service_name)

return unload_ok


Expand Down
8 changes: 8 additions & 0 deletions homeassistant/components/lcn/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,3 +429,11 @@ class LcnService(StrEnum):
(LcnService.DYN_TEXT, DynText),
(LcnService.PCK, Pck),
)


async def register_services(hass: HomeAssistant) -> None:
"""Register services for LCN."""
for service_name, service in SERVICES:
hass.services.async_register(
DOMAIN, service_name, service(hass).async_call_service, service.schema
)

0 comments on commit 3d84e35

Please sign in to comment.