Add helper to report deprecated entity supported features magic numbers (#106602)

pull/106609/head^2
J. Nick Koston 2023-12-28 12:24:36 -10:00 committed by GitHub
parent f93b0a4831
commit a46fe94216
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 1 deletions

View File

@ -7,7 +7,7 @@ from collections import deque
from collections.abc import Callable, Coroutine, Iterable, Mapping, MutableMapping
import dataclasses
from datetime import timedelta
from enum import Enum, auto
from enum import Enum, IntFlag, auto
import functools as ft
import logging
import math
@ -460,6 +460,9 @@ class Entity(
# If we reported if this entity was slow
_slow_reported = False
# If we reported deprecated supported features constants
_deprecated_supported_features_reported = False
# If we reported this entity is updated while disabled
_disabled_reported = False
@ -1496,6 +1499,31 @@ class Entity(
self.hass, integration_domain=platform_name, module=type(self).__module__
)
@callback
def _report_deprecated_supported_features_values(
self, replacement: IntFlag
) -> None:
"""Report deprecated supported features values."""
if self._deprecated_supported_features_reported is True:
return
self._deprecated_supported_features_reported = True
report_issue = self._suggest_report_issue()
report_issue += (
" and reference "
"https://developers.home-assistant.io/blog/2023/12/28/support-feature-magic-numbers-deprecation"
)
_LOGGER.warning(
(
"Entity %s (%s) is using deprecated supported features"
" values which will be removed in HA Core 2025.1. Instead it should use"
" %s, please %s"
),
self.entity_id,
type(self),
repr(replacement),
report_issue,
)
class ToggleEntityDescription(EntityDescription, frozen_or_thawed=True):
"""A class that describes toggle entities."""

View File

@ -3,6 +3,7 @@ import asyncio
from collections.abc import Iterable
import dataclasses
from datetime import timedelta
from enum import IntFlag
import logging
import threading
from typing import Any
@ -2025,3 +2026,28 @@ async def test_cached_entity_property_class_attribute(hass: HomeAssistant) -> No
for ent in entities:
assert getattr(ent[0], property) == values[1]
assert getattr(ent[1], property) == values[0]
async def test_entity_report_deprecated_supported_features_values(
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test reporting deprecated supported feature values only happens once."""
ent = entity.Entity()
class MockEntityFeatures(IntFlag):
VALUE1 = 1
VALUE2 = 2
ent._report_deprecated_supported_features_values(MockEntityFeatures(2))
assert (
"is using deprecated supported features values which will be removed"
in caplog.text
)
assert "MockEntityFeatures.VALUE2" in caplog.text
caplog.clear()
ent._report_deprecated_supported_features_values(MockEntityFeatures(2))
assert (
"is using deprecated supported features values which will be removed"
not in caplog.text
)