Fix stop service in modbus (#181915)

This commit is contained in:
epenet
2026-09-11 11:26:42 +02:00
committed by GitHub
parent 7c9fd38643
commit f3442b20b7
4 changed files with 55 additions and 3 deletions
+1 -1
View File
@@ -155,7 +155,7 @@ SERVICE_WRITE_REGISTER = "write_register"
SERVICE_STOP = "stop"
# dispatcher signals
SIGNAL_STOP_ENTITY = "modbus.stop"
SIGNAL_STOP_ENTITY = "modbus.stop_{}"
# integration names
DEFAULT_HUB = "modbus_hub"
+5 -1
View File
@@ -149,7 +149,11 @@ class ModbusBaseEntity(Entity):
)
)
self.async_on_remove(
async_dispatcher_connect(self.hass, SIGNAL_STOP_ENTITY, self.async_disable)
async_dispatcher_connect(
self.hass,
SIGNAL_STOP_ENTITY.format(self._hub.name),
self.async_disable,
)
)
+1 -1
View File
@@ -101,7 +101,7 @@ async def _async_stop_hub(service: ServiceCall) -> None:
"""Stop Modbus hub."""
hass = service.hass
hub = _get_hubs(hass)[service.data[ATTR_HUB]]
async_dispatcher_send(hass, SIGNAL_STOP_ENTITY)
async_dispatcher_send(hass, SIGNAL_STOP_ENTITY.format(hub.name))
await hub.async_close()
+48
View File
@@ -28,6 +28,7 @@ from homeassistant.const import (
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceValidationError
from homeassistant.helpers.entity_platform import async_get_platforms
from homeassistant.setup import async_setup_component
from tests.common import get_fixture_path
@@ -162,3 +163,50 @@ async def test_service_after_failed_reload_raises(
await hass.services.async_call(DOMAIN, service, data, blocking=True)
assert err.value.translation_key == "not_loaded"
TEST_HUB_A = "hub_a"
TEST_HUB_B = "hub_b"
def _two_hub_config() -> dict:
"""Return a config with two hubs, each exposing one sensor."""
return {
DOMAIN: [
{
CONF_NAME: name,
CONF_TYPE: "tcp",
CONF_HOST: "modbusHost",
CONF_PORT: port,
CONF_SENSORS: [{CONF_NAME: f"sensor_{name}", CONF_ADDRESS: 1}],
}
for name, port in ((TEST_HUB_A, 5501), (TEST_HUB_B, 5502))
]
}
@pytest.mark.usefixtures("mock_pymodbus")
async def test_stop_only_disables_the_selected_hub(hass: HomeAssistant) -> None:
"""Test stopping one hub leaves the other hub's entities alone."""
assert await async_setup_component(hass, DOMAIN, _two_hub_config())
await hass.async_block_till_done()
# async_disable only flips availability, it does not write the state, so the
# entity objects rather than the state machine show the effect.
entities = {
entity.entity_id: entity
for platform in async_get_platforms(hass, DOMAIN)
for entity in platform.entities.values()
}
entity_a = entities[f"sensor.sensor_{TEST_HUB_A}"]
entity_b = entities[f"sensor.sensor_{TEST_HUB_B}"]
assert entity_a.available
assert entity_b.available
await hass.services.async_call(
DOMAIN, SERVICE_STOP, {ATTR_HUB: TEST_HUB_A}, blocking=True
)
await hass.async_block_till_done()
assert not entity_a.available
assert entity_b.available