Skip to content

Commit

Permalink
Fix reload modbus component issue (home-assistant#133820)
Browse files Browse the repository at this point in the history
fix issue 116675
  • Loading branch information
crug80 authored Dec 24, 2024
1 parent 15806c2 commit 5c0659c
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 29 deletions.
39 changes: 27 additions & 12 deletions homeassistant/components/modbus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@
CONF_TYPE,
CONF_UNIQUE_ID,
CONF_UNIT_OF_MEASUREMENT,
SERVICE_RELOAD,
)
from homeassistant.core import HomeAssistant
from homeassistant.core import Event, HomeAssistant, ServiceCall
import homeassistant.helpers.config_validation as cv
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 homeassistant.helpers.typing import ConfigType

from .const import (
Expand Down Expand Up @@ -451,18 +455,29 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up Modbus component."""
if DOMAIN not in config:
return True

async def _reload_config(call: Event | ServiceCall) -> None:
"""Reload Modbus."""
if DOMAIN not in hass.data:
_LOGGER.error("Modbus cannot reload, because it was never loaded")
return
hubs = hass.data[DOMAIN]
for name in hubs:
await hubs[name].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,
)


async def async_reset_platform(hass: HomeAssistant, integration_name: str) -> None:
"""Release modbus resources."""
if DOMAIN not in hass.data:
_LOGGER.error("Modbus cannot reload, because it was never loaded")
return
_LOGGER.debug("Modbus reloading")
hubs = hass.data[DOMAIN]
for name in hubs:
await hubs[name].async_close()
3 changes: 0 additions & 3 deletions homeassistant/components/modbus/modbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
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 (
Expand Down Expand Up @@ -125,8 +124,6 @@ async def async_modbus_setup(
) -> bool:
"""Set up Modbus component."""

await async_setup_reload_service(hass, DOMAIN, [DOMAIN])

if config[DOMAIN]:
config[DOMAIN] = check_config(hass, config[DOMAIN])
if not config[DOMAIN]:
Expand Down
12 changes: 12 additions & 0 deletions tests/components/modbus/fixtures/configuration_2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
modbus:
type: "tcp"
host: "testHost"
port: 5001
name: "testModbus"
sensors:
- name: "dummy"
address: 117
slave: 0
- name: "dummy_2"
address: 118
slave: 1
Empty file.
60 changes: 46 additions & 14 deletions tests/components/modbus/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

from homeassistant import config as hass_config
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
from homeassistant.components.modbus import async_reset_platform
from homeassistant.components.modbus.const import (
ATTR_ADDRESS,
ATTR_HUB,
Expand Down Expand Up @@ -1159,22 +1158,61 @@ async def test_integration_reload(
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
mock_modbus,
freezer: FrozenDateTimeFactory,
) -> None:
"""Run test for integration reload."""

caplog.set_level(logging.DEBUG)
caplog.clear()

yaml_path = get_fixture_path("configuration.yaml", "modbus")
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=10))
await hass.async_block_till_done()

yaml_path = get_fixture_path("configuration.yaml", DOMAIN)
with mock.patch.object(hass_config, "YAML_CONFIG_FILE", yaml_path):
await hass.services.async_call(DOMAIN, SERVICE_RELOAD, blocking=True)
await hass.services.async_call(
DOMAIN,
SERVICE_RELOAD,
{},
blocking=True,
)
await hass.async_block_till_done()
for _ in range(4):
freezer.tick(timedelta(seconds=1))
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert "Modbus reloading" in caplog.text
state_sensor_1 = hass.states.get("sensor.dummy")
state_sensor_2 = hass.states.get("sensor.dummy_2")
assert state_sensor_1
assert not state_sensor_2

caplog.clear()
yaml_path = get_fixture_path("configuration_2.yaml", DOMAIN)
with mock.patch.object(hass_config, "YAML_CONFIG_FILE", yaml_path):
await hass.services.async_call(
DOMAIN,
SERVICE_RELOAD,
{},
blocking=True,
)
await hass.async_block_till_done()
assert "Modbus reloading" in caplog.text
state_sensor_1 = hass.states.get("sensor.dummy")
state_sensor_2 = hass.states.get("sensor.dummy_2")
assert state_sensor_1
assert state_sensor_2

caplog.clear()
yaml_path = get_fixture_path("configuration_empty.yaml", DOMAIN)
with mock.patch.object(hass_config, "YAML_CONFIG_FILE", yaml_path):
await hass.services.async_call(
DOMAIN,
SERVICE_RELOAD,
{},
blocking=True,
)
await hass.async_block_till_done()
assert "Modbus not present anymore" in caplog.text
state_sensor_1 = hass.states.get("sensor.dummy")
state_sensor_2 = hass.states.get("sensor.dummy_2")
assert not state_sensor_1
assert not state_sensor_2


@pytest.mark.parametrize("do_config", [{}])
Expand Down Expand Up @@ -1227,9 +1265,3 @@ async def test_no_entities(hass: HomeAssistant) -> None:
]
}
assert await async_setup_component(hass, DOMAIN, config) is False


async def test_reset_platform(hass: HomeAssistant) -> None:
"""Run test for async_reset_platform."""
await async_reset_platform(hass, "modbus")
assert DOMAIN not in hass.data

0 comments on commit 5c0659c

Please sign in to comment.