diff --git a/homeassistant/components/modbus/cover.py b/homeassistant/components/modbus/cover.py index 3a1c1c56536..4b0fa1aee87 100644 --- a/homeassistant/components/modbus/cover.py +++ b/homeassistant/components/modbus/cover.py @@ -2,6 +2,7 @@ from __future__ import annotations from datetime import timedelta +import logging from typing import Any from pymodbus.exceptions import ConnectionException, ModbusException @@ -35,6 +36,8 @@ from .const import ( ) from .modbus import ModbusHub +_LOGGER = logging.getLogger(__name__) + async def async_setup_platform( hass: HomeAssistant, @@ -44,6 +47,11 @@ async def async_setup_platform( ): """Read configuration and create Modbus cover.""" if discovery_info is None: + _LOGGER.warning( + "You're trying to init Modbus Cover in an unsupported way." + " Check https://www.home-assistant.io/integrations/modbus/#configuring-platform-cover" + " and fix your configuration" + ) return covers = [] diff --git a/tests/components/modbus/conftest.py b/tests/components/modbus/conftest.py index 2c83f40546f..761f2c7e141 100644 --- a/tests/components/modbus/conftest.py +++ b/tests/components/modbus/conftest.py @@ -44,6 +44,7 @@ async def base_test( check_config_only=False, config_modbus=None, scan_interval=None, + expect_init_to_fail=False, ): """Run test on device for given config.""" @@ -107,7 +108,10 @@ async def base_test( if config_device is not None: entity_id = f"{entity_domain}.{device_name}" device = hass.states.get(entity_id) - if device is None: + + if expect_init_to_fail: + assert device is None + elif device is None: pytest.fail("CONFIG failed, see output") if check_config_only: return @@ -132,6 +136,7 @@ async def base_config_test( array_name_old_config, method_discovery=False, config_modbus=None, + expect_init_to_fail=False, ): """Check config of device for given config.""" @@ -147,4 +152,5 @@ async def base_config_test( method_discovery=method_discovery, check_config_only=True, config_modbus=config_modbus, + expect_init_to_fail=expect_init_to_fail, ) diff --git a/tests/components/modbus/test_modbus_cover.py b/tests/components/modbus/test_modbus_cover.py index b101c6784d5..eddaa6099d7 100644 --- a/tests/components/modbus/test_modbus_cover.py +++ b/tests/components/modbus/test_modbus_cover.py @@ -1,4 +1,6 @@ """The tests for the Modbus cover component.""" +import logging + import pytest from homeassistant.components.cover import DOMAIN as COVER_DOMAIN @@ -117,7 +119,7 @@ async def test_coil_cover(hass, regs, expected): ), ], ) -async def test_register_COVER(hass, regs, expected): +async def test_register_cover(hass, regs, expected): """Run test for given config.""" cover_name = "modbus_test_cover" state = await base_test( @@ -137,3 +139,32 @@ async def test_register_COVER(hass, regs, expected): scan_interval=5, ) assert state == expected + + +@pytest.mark.parametrize("read_type", [CALL_TYPE_COIL, CONF_REGISTER]) +async def test_unsupported_config_cover(hass, read_type, caplog): + """ + Run test for cover. + + Initialize the Cover in the legacy manner via platform. + This test expects that the Cover won't be initialized, and that we get a config warning. + """ + device_name = "test_cover" + device_config = {CONF_NAME: device_name, read_type: 1234} + + caplog.set_level(logging.WARNING) + caplog.clear() + + await base_config_test( + hass, + device_config, + device_name, + COVER_DOMAIN, + CONF_COVERS, + None, + method_discovery=False, + expect_init_to_fail=True, + ) + + assert len(caplog.records) == 1 + assert caplog.records[0].levelname == "WARNING"