8000 Fix time_date beat calculation by amelchio · Pull Request #43142 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix time_date beat calculation #43142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions homeassistant/components/time_date/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,6 @@ def _update_internal_state(self, time_date):
date = dt_util.as_local(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":
self._state = time
elif self.type == "date":
Expand All @@ -135,6 +125,19 @@ def _update_internal_state(self, 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.
A7E9 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}").isoformat()
Expand Down
2 changes: 2 additions & 0 deletions tests/components/time_date/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ async def test_states(hass):
device = time_date.TimeDateSensor(hass, "beat")
device._update_internal_state(now)
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._update_internal_state(now)
Expand Down
0