mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Fix beat calculation (#43142)
This commit is contained in:
parent
e0f1d0ab20
commit
1a888433cb
@ -116,16 +116,6 @@ class TimeDateSensor(Entity):
|
|||||||
date = dt_util.as_local(time_date).date().isoformat()
|
date = dt_util.as_local(time_date).date().isoformat()
|
||||||
date_utc = time_date.date().isoformat()
|
date_utc = time_date.date().isoformat()
|
||||||
|
|
||||||
# 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,
|
|
||||||
)
|
|
||||||
beat = int((delta.seconds + delta.microseconds / 1000000.0) / 86.4)
|
|
||||||
|
|
||||||
if self.type == "time":
|
if self.type == "time":
|
||||||
self._state = time
|
self._state = time
|
||||||
elif self.type == "date":
|
elif self.type == "date":
|
||||||
@ -139,6 +129,19 @@ class TimeDateSensor(Entity):
|
|||||||
elif self.type == "time_utc":
|
elif self.type == "time_utc":
|
||||||
self._state = time_utc
|
self._state = time_utc
|
||||||
elif self.type == "beat":
|
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}"
|
self._state = f"@{beat:03d}"
|
||||||
elif self.type == "date_time_iso":
|
elif self.type == "date_time_iso":
|
||||||
self._state = dt_util.parse_datetime(f"{date} {time}").isoformat()
|
self._state = dt_util.parse_datetime(f"{date} {time}").isoformat()
|
||||||
|
@ -69,6 +69,8 @@ async def test_states(hass):
|
|||||||
device = time_date.TimeDateSensor(hass, "beat")
|
device = time_date.TimeDateSensor(hass, "beat")
|
||||||
device._update_internal_state(now)
|
device._update_internal_state(now)
|
||||||
assert device.state == "@079"
|
assert device.state == "@079"
|
||||||
|
device._update_internal_state(dt_util.utc_from_timestamp(1602952963.2))
|
||||||
|
assert device.state == "@738"
|
||||||
|
|
||||||
device = time_date.TimeDateSensor(hass, "date_time_iso")
|
device = time_date.TimeDateSensor(hass, "date_time_iso")
|
||||||
device._update_internal_state(now)
|
device._update_internal_state(now)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user