Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions homeassistant/components/modbus/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

from datetime import timedelta
import logging
from typing import Any

from pymodbus.exceptions import ConnectionException, ModbusException
Expand Down Expand Up @@ -35,6 +36,8 @@
)
from .modbus import ModbusHub

_LOGGER = logging.getLogger(__name__)


async def async_setup_platform(
hass: HomeAssistant,
Expand All @@ -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 = []
Expand Down
8 changes: 7 additions & 1 deletion tests/components/modbus/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down Expand Up @@ -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
Expand All @@ -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."""

Expand All @@ -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,
)
33 changes: 32 additions & 1 deletion tests/components/modbus/test_modbus_cover.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""The tests for the Modbus cover component."""
import logging

import pytest

from homeassistant.components.cover import DOMAIN as COVER_DOMAIN
Expand Down Expand Up @@ -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(
Expand All @@ -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"