Fix input_datetime.set_datetime not accepting 0 timestamp value (#134489)

This commit is contained in:
Franck Nijhof 2025-01-02 23:45:00 +01:00 committed by GitHub
parent ac26ca2da5
commit cb389d29ea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 1 deletions

View File

@ -385,7 +385,7 @@ class InputDatetime(collection.CollectionEntity, RestoreEntity):
@callback
def async_set_datetime(self, date=None, time=None, datetime=None, timestamp=None):
"""Set a new date / time."""
if timestamp:
if timestamp is not None:
datetime = dt_util.as_local(dt_util.utc_from_timestamp(timestamp))
if datetime:

View File

@ -217,6 +217,34 @@ async def test_set_datetime_3(hass: HomeAssistant) -> None:
assert state.attributes["timestamp"] == dt_obj.timestamp()
async def test_set_datetime_4(hass: HomeAssistant) -> None:
"""Test set_datetime method using timestamp 0."""
await async_setup_component(
hass, DOMAIN, {DOMAIN: {"test_datetime": {"has_time": True, "has_date": True}}}
)
entity_id = "input_datetime.test_datetime"
dt_obj = datetime.datetime(
1969, 12, 31, 16, 00, 00, tzinfo=dt_util.get_time_zone(hass.config.time_zone)
)
await async_set_timestamp(hass, entity_id, 0)
state = hass.states.get(entity_id)
assert state.state == dt_obj.strftime(FORMAT_DATETIME)
assert state.attributes["has_time"]
assert state.attributes["has_date"]
assert state.attributes["year"] == 1969
assert state.attributes["month"] == 12
assert state.attributes["day"] == 31
assert state.attributes["hour"] == 16
assert state.attributes["minute"] == 00
assert state.attributes["second"] == 0
assert state.attributes["timestamp"] == 0
async def test_set_datetime_time(hass: HomeAssistant) -> None:
"""Test set_datetime method with only time."""
await async_setup_component(