parent
ce83071900
commit
502fbe65ee
|
@ -46,9 +46,13 @@ from homeassistant.const import (
|
|||
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 (
|
||||
|
@ -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()
|
||||
|
|
|
@ -34,7 +34,6 @@ 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 (
|
||||
|
@ -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]:
|
||||
|
|
|
@ -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
|
|
@ -25,7 +25,6 @@ import voluptuous as vol
|
|||
|
||||
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,
|
||||
|
@ -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")
|
||||
with mock.patch.object(hass_config, "YAML_CONFIG_FILE", yaml_path):
|
||||
await hass.services.async_call(DOMAIN, SERVICE_RELOAD, blocking=True)
|
||||
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=10))
|
||||
await hass.async_block_till_done()
|
||||
for _ in range(4):
|
||||
freezer.tick(timedelta(seconds=1))
|
||||
async_fire_time_changed(hass)
|
||||
|
||||
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.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", [{}])
|
||||
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue