Deprecate 'beat' display option in Time & Date (#106871)

* Deprecate 'beat' display option in Time & Date

* Move deprecation warning

* Update homeassistant/components/time_date/const.py

Co-authored-by: Sander <developer@golles.nl>

---------

Co-authored-by: G Johansson <goran.johansson@shiftit.se>
Co-authored-by: Sander <developer@golles.nl>
This commit is contained in:
Erik Montnemery 2024-01-02 14:11:45 +01:00 committed by GitHub
parent 8f9bd75a36
commit 8f8c0ef13b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 74 additions and 0 deletions

View File

@ -0,0 +1,6 @@
"""Constants for the Time & Date integration."""
from __future__ import annotations
from typing import Final
DOMAIN: Final = "time_date"

View File

@ -12,9 +12,12 @@ from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.event import async_track_point_in_utc_time from homeassistant.helpers.event import async_track_point_in_utc_time
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
from .const import DOMAIN
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
TIME_STR_FORMAT = "%H:%M" TIME_STR_FORMAT = "%H:%M"
@ -50,6 +53,23 @@ async def async_setup_platform(
_LOGGER.error("Timezone is not set in Home Assistant configuration") # type: ignore[unreachable] _LOGGER.error("Timezone is not set in Home Assistant configuration") # type: ignore[unreachable]
return False return False
if "beat" in config[CONF_DISPLAY_OPTIONS]:
async_create_issue(
hass,
DOMAIN,
"deprecated_beat",
breaks_in_ha_version="2024.7.0",
is_fixable=False,
severity=IssueSeverity.WARNING,
translation_key="deprecated_beat",
translation_placeholders={
"config_key": "beat",
"display_options": "display_options",
"integration": DOMAIN,
},
)
_LOGGER.warning("'beat': is deprecated and will be removed in version 2024.7")
async_add_entities( async_add_entities(
[TimeDateSensor(hass, variable) for variable in config[CONF_DISPLAY_OPTIONS]] [TimeDateSensor(hass, variable) for variable in config[CONF_DISPLAY_OPTIONS]]
) )

View File

@ -0,0 +1,8 @@
{
"issues": {
"deprecated_beat": {
"title": "The `{config_key}` Time & Date sensor is being removed",
"description": "Please remove the `{config_key}` key from the `{display_options}` for the {integration} entry in your configuration.yaml file and restart Home Assistant to fix this issue."
}
}
}

View File

@ -1,9 +1,13 @@
"""The tests for time_date sensor platform.""" """The tests for time_date sensor platform."""
from freezegun.api import FrozenDateTimeFactory from freezegun.api import FrozenDateTimeFactory
import pytest
from homeassistant.components.time_date.const import DOMAIN
import homeassistant.components.time_date.sensor as time_date import homeassistant.components.time_date.sensor as time_date
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import issue_registry as ir
from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
@ -172,3 +176,39 @@ async def test_icons(hass: HomeAssistant) -> None:
assert device.icon == "mdi:calendar-clock" assert device.icon == "mdi:calendar-clock"
device = time_date.TimeDateSensor(hass, "date_time_iso") device = time_date.TimeDateSensor(hass, "date_time_iso")
assert device.icon == "mdi:calendar-clock" assert device.icon == "mdi:calendar-clock"
@pytest.mark.parametrize(
(
"display_options",
"expected_warnings",
"expected_issues",
),
[
(["time", "date"], [], []),
(["beat"], ["'beat': is deprecated"], ["deprecated_beat"]),
(["time", "beat"], ["'beat': is deprecated"], ["deprecated_beat"]),
],
)
async def test_deprecation_warning(
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
display_options: list[str],
expected_warnings: list[str],
expected_issues: list[str],
) -> None:
"""Test deprecation warning for swatch beat."""
config = {"sensor": {"platform": "time_date", "display_options": display_options}}
await async_setup_component(hass, "sensor", config)
await hass.async_block_till_done()
warnings = [record for record in caplog.records if record.levelname == "WARNING"]
assert len(warnings) == len(expected_warnings)
for expected_warning in expected_warnings:
assert any(expected_warning in warning.message for warning in warnings)
issue_registry = ir.async_get(hass)
assert len(issue_registry.issues) == len(expected_issues)
for expected_issue in expected_issues:
assert (DOMAIN, expected_issue) in issue_registry.issues