Remove beat (internet time) from time_date (#119785)

pull/119777/head^2
G Johansson 2024-06-17 05:36:06 +02:00 committed by GitHub
parent fc3fbc6862
commit bd37ce6e9a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 7 additions and 124 deletions

View File

@ -35,7 +35,7 @@ USER_SCHEMA = vol.Schema(
{
vol.Required(CONF_DISPLAY_OPTIONS): SelectSelector(
SelectSelectorConfig(
options=[option for option in OPTION_TYPES if option != "beat"],
options=OPTION_TYPES,
mode=SelectSelectorMode.DROPDOWN,
translation_key="display_options",
)

View File

@ -18,6 +18,5 @@ OPTION_TYPES = [
"date_time_utc",
"date_time_iso",
"time_date",
"beat",
"time_utc",
]

View File

@ -20,11 +20,10 @@ from homeassistant.core import CALLBACK_TYPE, Event, 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, OPTION_TYPES
from .const import OPTION_TYPES
_LOGGER = logging.getLogger(__name__)
@ -51,23 +50,6 @@ 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(variable) for variable in config[CONF_DISPLAY_OPTIONS]]
)
@ -95,8 +77,7 @@ class TimeDateSensor(SensorEntity):
"""Initialize the sensor."""
self._attr_translation_key = option_type
self.type = option_type
object_id = "internet_time" if option_type == "beat" else option_type
self.entity_id = ENTITY_ID_FORMAT.format(object_id)
self.entity_id = ENTITY_ID_FORMAT.format(option_type)
self._attr_unique_id = option_type if entry_id else None
self._update_internal_state(dt_util.utcnow())
@ -169,13 +150,8 @@ class TimeDateSensor(SensorEntity):
tomorrow = dt_util.as_local(time_date) + timedelta(days=1)
return dt_util.start_of_local_day(tomorrow)
if self.type == "beat":
# Add 1 hour because @0 beats is at 23:00:00 UTC.
timestamp = dt_util.as_timestamp(time_date + timedelta(hours=1))
interval = 86.4
else:
timestamp = dt_util.as_timestamp(time_date)
interval = 60
timestamp = dt_util.as_timestamp(time_date)
interval = 60
delta = interval - (timestamp % interval)
next_interval = time_date + timedelta(seconds=delta)
@ -201,21 +177,6 @@ class TimeDateSensor(SensorEntity):
self._state = f"{time}, {date}"
elif self.type == "time_utc":
self._state = time_utc
elif self.type == "beat":
# Calculate Swatch Internet Time.
time_bmt = time_date + timedelta(hours=1)
delta = timedelta(
hours=time_bmt.hour,
minutes=time_bmt.minute,
seconds=time_bmt.second,
microseconds=time_bmt.microsecond,
)
# Use integers to better handle rounding. For example,
# int(63763.2/86.4) = 737 but 637632//864 = 738.
beat = int(delta.total_seconds() * 10) // 864
self._state = f"@{beat:03d}"
elif self.type == "date_time_iso":
self._state = dt_util.parse_datetime(
f"{date} {time}", raise_on_error=True

View File

@ -66,18 +66,5 @@
"name": "[%key:component::time_date::selector::display_options::options::time_utc%]"
}
}
},
"issues": {
"deprecated_beat": {
"title": "The `{config_key}` Time & Date sensor is being removed",
"fix_flow": {
"step": {
"confirm": {
"title": "[%key:component::time_date::issues::deprecated_beat::title%]",
"description": "Please remove the `{config_key}` key from the {integration} config entry options and click submit to fix this issue."
}
}
}
}
}
}

View File

@ -5,10 +5,9 @@ from unittest.mock import ANY, Mock, patch
from freezegun.api import FrozenDateTimeFactory
import pytest
from homeassistant.components.time_date.const import DOMAIN, OPTION_TYPES
from homeassistant.components.time_date.const import OPTION_TYPES
from homeassistant.core import HomeAssistant
from homeassistant.helpers import event, issue_registry as ir
from homeassistant.setup import async_setup_component
from homeassistant.helpers import event
import homeassistant.util.dt as dt_util
from . import load_int
@ -25,11 +24,6 @@ from tests.common import async_fire_time_changed
dt_util.utc_from_timestamp(45.5),
dt_util.utc_from_timestamp(60),
),
(
"beat",
dt_util.parse_datetime("2020-11-13 00:00:29+01:00"),
dt_util.parse_datetime("2020-11-13 00:01:26.4+01:00"),
),
(
"date_time",
dt_util.utc_from_timestamp(1495068899),
@ -83,9 +77,6 @@ async def test_states(hass: HomeAssistant, freezer: FrozenDateTimeFactory) -> No
state = hass.states.get("sensor.date_time_utc")
assert state.state == "2017-05-18, 00:54"
state = hass.states.get("sensor.internet_time")
assert state.state == "@079"
state = hass.states.get("sensor.date_time_iso")
assert state.state == "2017-05-18T00:54:00"
@ -110,9 +101,6 @@ async def test_states(hass: HomeAssistant, freezer: FrozenDateTimeFactory) -> No
state = hass.states.get("sensor.date_time_utc")
assert state.state == "2020-10-17, 16:42"
state = hass.states.get("sensor.internet_time")
assert state.state == "@738"
state = hass.states.get("sensor.date_time_iso")
assert state.state == "2020-10-17T16:42:00"
@ -143,9 +131,6 @@ async def test_states_non_default_timezone(
state = hass.states.get("sensor.date_time_utc")
assert state.state == "2017-05-18, 00:54"
state = hass.states.get("sensor.internet_time")
assert state.state == "@079"
state = hass.states.get("sensor.date_time_iso")
assert state.state == "2017-05-17T20:54:00"
@ -170,9 +155,6 @@ async def test_states_non_default_timezone(
state = hass.states.get("sensor.date_time_utc")
assert state.state == "2020-10-17, 16:42"
state = hass.states.get("sensor.internet_time")
assert state.state == "@738"
state = hass.states.get("sensor.date_time_iso")
assert state.state == "2020-10-17T12:42:00"
@ -195,9 +177,6 @@ async def test_states_non_default_timezone(
state = hass.states.get("sensor.date_time_utc")
assert state.state == "2020-10-17, 16:42"
state = hass.states.get("sensor.internet_time")
assert state.state == "@738"
state = hass.states.get("sensor.date_time_iso")
assert state.state == "2020-10-17T18:42:00"
@ -280,48 +259,5 @@ async def test_icons(hass: HomeAssistant) -> None:
assert state.attributes["icon"] == "mdi:calendar-clock"
state = hass.states.get("sensor.date_time_utc")
assert state.attributes["icon"] == "mdi:calendar-clock"
state = hass.states.get("sensor.internet_time")
assert state.attributes["icon"] == "mdi:clock"
state = hass.states.get("sensor.date_time_iso")
assert state.attributes["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],
issue_registry: ir.IssueRegistry,
) -> None:
"""Test deprecation warning for swatch beat."""
config = {
"sensor": {
"platform": "time_date",
"display_options": display_options,
}
}
assert 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)
assert len(issue_registry.issues) == len(expected_issues)
for expected_issue in expected_issues:
assert (DOMAIN, expected_issue) in issue_registry.issues