From 85c863a76605a0aa5781a4268722dc07fffd9f12 Mon Sep 17 00:00:00 2001 From: jan iversen Date: Thu, 3 Mar 2022 08:46:58 +0100 Subject: [PATCH] Allow reload of modbus (#67390) * Allow reload of modbus. Co-authored-by: Paulus Schoutsen --- homeassistant/components/modbus/modbus.py | 14 ++++++++++++++ homeassistant/components/modbus/services.yaml | 3 +++ 2 files changed, 17 insertions(+) diff --git a/homeassistant/components/modbus/modbus.py b/homeassistant/components/modbus/modbus.py index c980eab2b34..3fa4a74a932 100644 --- a/homeassistant/components/modbus/modbus.py +++ b/homeassistant/components/modbus/modbus.py @@ -35,6 +35,7 @@ import homeassistant.helpers.config_validation as cv from homeassistant.helpers.discovery import async_load_platform from homeassistant.helpers.dispatcher import async_dispatcher_send from homeassistant.helpers.event import async_call_later +from homeassistant.helpers.reload import async_setup_reload_service from homeassistant.helpers.typing import ConfigType from .const import ( @@ -129,6 +130,11 @@ async def async_modbus_setup( ) -> bool: """Set up Modbus component.""" + platform_names = [] + for entry in PLATFORMS: + platform_names.append(entry[1]) + await async_setup_reload_service(hass, DOMAIN, platform_names) + hass.data[DOMAIN] = hub_collect = {} for conf_hub in config[DOMAIN]: my_hub = ModbusHub(hass, conf_hub) @@ -239,6 +245,14 @@ async def async_modbus_setup( return True +async def async_reset_platform(hass: HomeAssistant, integration_name: str) -> None: + """Release modbus resources.""" + _LOGGER.info("Modbus reloading") + for hub in hass.data[DOMAIN]: + await hub.async_close() + del hass.data[DOMAIN] + + class ModbusHub: """Thread safe wrapper class for pymodbus.""" diff --git a/homeassistant/components/modbus/services.yaml b/homeassistant/components/modbus/services.yaml index 373954e25df..07acf0a72df 100644 --- a/homeassistant/components/modbus/services.yaml +++ b/homeassistant/components/modbus/services.yaml @@ -1,3 +1,6 @@ +reload: + name: Reload + description: Reload all modbus entities. write_coil: name: Write coil description: Write to a modbus coil.