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

This commit is contained in:
J. Nick Koston 2023-12-28 12:24:36 -10:00 committed by Franck Nijhof
parent 982707afe6
commit 16192cd7f2
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
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 from collections.abc import Callable, Coroutine, Iterable, Mapping, MutableMapping
import dataclasses import dataclasses
from datetime import timedelta from datetime import timedelta
from enum import Enum, auto from enum import Enum, IntFlag, auto
import functools as ft import functools as ft
import logging import logging
import math import math
@ -460,6 +460,9 @@ class Entity(
# If we reported if this entity was slow # If we reported if this entity was slow
_slow_reported = False _slow_reported = False
# If we reported deprecated supported features constants
_deprecated_supported_features_reported = False
# If we reported this entity is updated while disabled # If we reported this entity is updated while disabled
_disabled_reported = False _disabled_reported = False
@ -1496,6 +1499,31 @@ class Entity(
self.hass, integration_domain=platform_name, module=type(self).__module__ 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): class ToggleEntityDescription(EntityDescription, frozen_or_thawed=True):
"""A class that describes toggle entities.""" """A class that describes toggle entities."""

View File

@ -3,6 +3,7 @@ import asyncio
from collections.abc import Iterable from collections.abc import Iterable
import dataclasses import dataclasses
from datetime import timedelta from datetime import timedelta
from enum import IntFlag
import logging import logging
import threading import threading
from typing import Any from typing import Any
@ -2025,3 +2026,28 @@ async def test_cached_entity_property_class_attribute(hass: HomeAssistant) -> No
for ent in entities: for ent in entities:
assert getattr(ent[0], property) == values[1] assert getattr(ent[0], property) == values[1]
assert getattr(ent[1], property) == values[0] 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
)