1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00

Deprecate binary sensor device class constants (#105736)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
Robert Resch
2023-12-19 12:45:32 +01:00
committed by GitHub
parent c64c1c8f08
commit a4ccd6e13b
5 changed files with 302 additions and 33 deletions

View File

@@ -1,10 +1,15 @@
"""Test deprecation helpers."""
import logging
import sys
from unittest.mock import MagicMock, Mock, patch
import pytest
from homeassistant.core import HomeAssistant
from homeassistant.helpers.deprecation import (
DeprecatedConstant,
DeprecatedConstantEnum,
check_if_deprecated_constant,
deprecated_class,
deprecated_function,
deprecated_substitute,
@@ -247,3 +252,92 @@ def test_deprecated_function_called_from_custom_integration(
"Use new_function instead, please report it to the author of the "
"'hue' custom integration"
) in caplog.text
@pytest.mark.parametrize(
("deprecated_constant", "extra_msg"),
[
(
DeprecatedConstant("value", "NEW_CONSTANT", None),
". Use NEW_CONSTANT instead",
),
(
DeprecatedConstant(1, "NEW_CONSTANT", "2099.1"),
" which will be removed in HA Core 2099.1. Use NEW_CONSTANT instead",
),
],
)
@pytest.mark.parametrize(
("module_name", "extra_extra_msg"),
[
("homeassistant.components.hue.light", ""), # builtin integration
(
"config.custom_components.hue.light",
", please report it to the author of the 'hue' custom integration",
), # custom component integration
],
)
def test_check_if_deprecated_constant(
caplog: pytest.LogCaptureFixture,
deprecated_constant: DeprecatedConstant | DeprecatedConstantEnum,
extra_msg: str,
module_name: str,
extra_extra_msg: str,
) -> None:
"""Test check_if_deprecated_constant."""
module_globals = {
"__name__": module_name,
"_DEPRECATED_TEST_CONSTANT": deprecated_constant,
}
filename = f"/home/paulus/{module_name.replace('.', '/')}.py"
# mock module for homeassistant/helpers/frame.py#get_integration_frame
sys.modules[module_name] = Mock(__file__=filename)
with patch(
"homeassistant.helpers.frame.extract_stack",
return_value=[
Mock(
filename="/home/paulus/homeassistant/core.py",
lineno="23",
line="do_something()",
),
Mock(
filename=filename,
lineno="23",
line="await session.close()",
),
Mock(
filename="/home/paulus/aiohue/lights.py",
lineno="2",
line="something()",
),
],
):
value = check_if_deprecated_constant("TEST_CONSTANT", module_globals)
assert value == deprecated_constant.value
assert (
module_name,
logging.WARNING,
f"TEST_CONSTANT was used from hue, this is a deprecated constant{extra_msg}{extra_extra_msg}",
) in caplog.record_tuples
def test_test_check_if_deprecated_constant_invalid(
caplog: pytest.LogCaptureFixture
) -> None:
"""Test check_if_deprecated_constant will raise an attribute error and create an log entry on an invalid deprecation type."""
module_name = "homeassistant.components.hue.light"
module_globals = {"__name__": module_name, "_DEPRECATED_TEST_CONSTANT": 1}
name = "TEST_CONSTANT"
excepted_msg = (
f"Value of _DEPRECATED_{name!r} is an instance of <class 'int'> "
"but an instance of DeprecatedConstant or DeprecatedConstantEnum is required"
)
with pytest.raises(AttributeError, match=excepted_msg):
check_if_deprecated_constant(name, module_globals)
assert (module_name, logging.DEBUG, excepted_msg) in caplog.record_tuples