diff --git a/homeassistant/components/modbus/__init__.py b/homeassistant/components/modbus/__init__.py index 74b873eb9e7c..315d7d997e04 100644 --- a/homeassistant/components/modbus/__init__.py +++ b/homeassistant/components/modbus/__init__.py @@ -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) diff --git a/homeassistant/components/modbus/const.py b/homeassistant/components/modbus/const.py index 2b0acc6675c7..18829e4cebe7 100644 --- a/homeassistant/components/modbus/const.py +++ b/homeassistant/components/modbus/const.py @@ -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 = ( diff --git a/homeassistant/components/modbus/modbus.py b/homeassistant/components/modbus/modbus.py index 2b82af348d60..63beec37f57a 100644 --- a/homeassistant/components/modbus/modbus.py +++ b/homeassistant/components/modbus/modbus.py @@ -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 diff --git a/homeassistant/components/modbus/services.py b/homeassistant/components/modbus/services.py new file mode 100644 index 000000000000..910a63a119f4 --- /dev/null +++ b/homeassistant/components/modbus/services.py @@ -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) diff --git a/tests/components/modbus/test_deprecation.py b/tests/components/modbus/test_deprecation.py index abaecb9a1d7c..963c2d0b8b62 100644 --- a/tests/components/modbus/test_deprecation.py +++ b/tests/components/modbus/test_deprecation.py @@ -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