mirror of
https://github.com/home-assistant/core.git
synced 2026-09-26 17:31:15 -04:00
Move service registration in modbus async_setup (#181273)
This commit is contained in:
@@ -1,19 +1,14 @@
|
||||
"""Support for Modbus."""
|
||||
|
||||
import logging
|
||||
|
||||
from homeassistant.const import SERVICE_RELOAD
|
||||
from homeassistant.core import Event, HomeAssistant, ServiceCall
|
||||
from homeassistant.helpers.entity_platform import async_get_platforms
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.frame import ReportBehavior, report_usage
|
||||
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 .connection import async_get_temporary_unit, async_get_unit
|
||||
from .const import DOMAIN
|
||||
from .modbus import DATA_MODBUS_HUBS, ModbusHub, async_modbus_setup
|
||||
from .const import DATA_MODBUS_HUBS, DOMAIN
|
||||
from .modbus import ModbusHub, async_modbus_setup
|
||||
from .schemas import CONFIG_SCHEMA
|
||||
from .services import async_setup_services
|
||||
|
||||
__all__ = [
|
||||
"CONFIG_SCHEMA",
|
||||
@@ -23,8 +18,6 @@ __all__ = [
|
||||
"get_hub",
|
||||
]
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def get_hub(hass: HomeAssistant, name: str) -> ModbusHub:
|
||||
"""Return modbus hub with name.
|
||||
@@ -51,28 +44,9 @@ def get_hub(hass: HomeAssistant, name: str) -> ModbusHub:
|
||||
|
||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||
"""Set up Modbus component."""
|
||||
async_setup_services(hass)
|
||||
|
||||
if DOMAIN not in config:
|
||||
return True
|
||||
|
||||
async def _reload_config(call: Event | ServiceCall) -> None:
|
||||
"""Reload Modbus."""
|
||||
if DATA_MODBUS_HUBS not in hass.data:
|
||||
_LOGGER.error("Modbus cannot reload, because it was never loaded")
|
||||
return
|
||||
hubs = hass.data[DATA_MODBUS_HUBS]
|
||||
for hub in hubs.values():
|
||||
await hub.async_close()
|
||||
reset_platforms = async_get_platforms(hass, DOMAIN)
|
||||
for reset_platform in reset_platforms:
|
||||
_LOGGER.debug("Reload modbus resetting platform: %s", reset_platform.domain)
|
||||
await reset_platform.async_reset()
|
||||
reload_config = await async_integration_yaml_config(hass, DOMAIN)
|
||||
if not reload_config:
|
||||
_LOGGER.debug("Modbus not present anymore")
|
||||
return
|
||||
_LOGGER.debug("Modbus reloading")
|
||||
await async_modbus_setup(hass, reload_config)
|
||||
|
||||
async_register_admin_service(hass, DOMAIN, SERVICE_RELOAD, _reload_config)
|
||||
|
||||
return await async_modbus_setup(hass, config)
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
from enum import StrEnum
|
||||
import logging
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from homeassistant.const import (
|
||||
CONF_ADDRESS,
|
||||
@@ -12,6 +13,10 @@ from homeassistant.const import (
|
||||
CONF_SWITCHES,
|
||||
Platform,
|
||||
)
|
||||
from homeassistant.util.hass_dict import HassKey
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .modbus import ModbusHub
|
||||
|
||||
# configuration names
|
||||
CONF_BAUDRATE = "baudrate"
|
||||
@@ -163,6 +168,8 @@ DEFAULT_HVAC_OFF_VALUE = 0
|
||||
MODBUS_DOMAIN = "modbus"
|
||||
DOMAIN = "modbus"
|
||||
|
||||
DATA_MODBUS_HUBS: HassKey[dict[str, ModbusHub]] = HassKey(DOMAIN)
|
||||
|
||||
ACTIVE_SCAN_INTERVAL = 2 # limit to force an extra update
|
||||
|
||||
PLATFORMS = (
|
||||
|
||||
@@ -30,7 +30,6 @@ from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers.discovery import async_load_platform
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
from homeassistant.util.hass_dict import HassKey
|
||||
|
||||
from .const import (
|
||||
ATTR_ADDRESS,
|
||||
@@ -51,6 +50,7 @@ from .const import (
|
||||
CONF_MSG_WAIT,
|
||||
CONF_PARITY,
|
||||
CONF_STOPBITS,
|
||||
DATA_MODBUS_HUBS,
|
||||
DEFAULT_HUB,
|
||||
DEVICE_ID,
|
||||
DOMAIN,
|
||||
@@ -67,8 +67,6 @@ from .const import (
|
||||
)
|
||||
from .validators import check_config
|
||||
|
||||
DATA_MODBUS_HUBS: HassKey[dict[str, ModbusHub]] = HassKey(DOMAIN)
|
||||
|
||||
PRIMARY_RECONNECT_DELAY = 60
|
||||
|
||||
ConfEntry = namedtuple("ConfEntry", "call_type attr func_name value_attr_name") # noqa: PYI024
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
"""Support for Modbus services."""
|
||||
|
||||
from homeassistant.const import SERVICE_RELOAD
|
||||
from homeassistant.core import HomeAssistant, ServiceCall, callback
|
||||
from homeassistant.helpers.entity_platform import async_get_platforms
|
||||
from homeassistant.helpers.reload import async_integration_yaml_config
|
||||
from homeassistant.helpers.service import async_register_admin_service
|
||||
|
||||
from .const import DATA_MODBUS_HUBS, DOMAIN, LOGGER
|
||||
from .modbus import async_modbus_setup
|
||||
|
||||
|
||||
async def _async_reload_config(call: ServiceCall) -> None:
|
||||
"""Reload Modbus."""
|
||||
hass = call.hass
|
||||
if DATA_MODBUS_HUBS not in hass.data:
|
||||
LOGGER.error("Modbus cannot reload, because it was never loaded")
|
||||
return
|
||||
hubs = hass.data[DATA_MODBUS_HUBS]
|
||||
for hub in hubs.values():
|
||||
await hub.async_close()
|
||||
reset_platforms = async_get_platforms(hass, DOMAIN)
|
||||
for reset_platform in reset_platforms:
|
||||
LOGGER.debug("Reload modbus resetting platform: %s", reset_platform.domain)
|
||||
await reset_platform.async_reset()
|
||||
reload_config = await async_integration_yaml_config(hass, DOMAIN)
|
||||
if not reload_config:
|
||||
LOGGER.debug("Modbus not present anymore")
|
||||
return
|
||||
LOGGER.debug("Modbus reloading")
|
||||
await async_modbus_setup(hass, reload_config)
|
||||
|
||||
|
||||
@callback
|
||||
def async_setup_services(hass: HomeAssistant) -> None:
|
||||
"""Register the Modbus services."""
|
||||
async_register_admin_service(hass, DOMAIN, SERVICE_RELOAD, _async_reload_config)
|
||||
@@ -8,7 +8,7 @@ from types import ModuleType
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.modbus import get_hub
|
||||
from homeassistant.components.modbus.modbus import DATA_MODBUS_HUBS
|
||||
from homeassistant.components.modbus.const import DATA_MODBUS_HUBS
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user