Log an error if modbus Cover is not initialized correctly (#48829)

This commit is contained in:
Vladimír Záhradník 2021-04-19 16:52:08 +02:00 committed by GitHub
parent a968dea152
commit 05755c27f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 2 deletions

View File

@ -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 = []

View File

@ -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,
)

View File

@ -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"