diff --git a/homeassistant/components/time_date/const.py b/homeassistant/components/time_date/const.py new file mode 100644 index 00000000000..4d0ff354a6c --- /dev/null +++ b/homeassistant/components/time_date/const.py @@ -0,0 +1,6 @@ +"""Constants for the Time & Date integration.""" +from __future__ import annotations + +from typing import Final + +DOMAIN: Final = "time_date" diff --git a/homeassistant/components/time_date/sensor.py b/homeassistant/components/time_date/sensor.py index eb0f291ad3f..a1d5024e9b1 100644 --- a/homeassistant/components/time_date/sensor.py +++ b/homeassistant/components/time_date/sensor.py @@ -12,9 +12,12 @@ from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity_platform import AddEntitiesCallback 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 import homeassistant.util.dt as dt_util +from .const import DOMAIN + _LOGGER = logging.getLogger(__name__) 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] 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( [TimeDateSensor(hass, variable) for variable in config[CONF_DISPLAY_OPTIONS]] ) diff --git a/homeassistant/components/time_date/strings.json b/homeassistant/components/time_date/strings.json new file mode 100644 index 00000000000..582fd44a45b --- /dev/null +++ b/homeassistant/components/time_date/strings.json @@ -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." + } + } +} diff --git a/tests/components/time_date/test_sensor.py b/tests/components/time_date/test_sensor.py index f9ef8a7cfe9..150ea4e8225 100644 --- a/tests/components/time_date/test_sensor.py +++ b/tests/components/time_date/test_sensor.py @@ -1,9 +1,13 @@ """The tests for time_date sensor platform.""" from freezegun.api import FrozenDateTimeFactory +import pytest +from homeassistant.components.time_date.const import DOMAIN import homeassistant.components.time_date.sensor as time_date 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 @@ -172,3 +176,39 @@ async def test_icons(hass: HomeAssistant) -> None: assert device.icon == "mdi:calendar-clock" device = time_date.TimeDateSensor(hass, "date_time_iso") 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