Fix Datadog boolean metrics (#37273)

Explicitly cast boolean metrics to integers
This commit is contained in:
Rick Sherman 2020-07-05 12:50:51 -05:00 committed by GitHub
parent 992793295c
commit b56f4bd9f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 2 deletions

View File

@ -82,6 +82,7 @@ def setup(hass, config):
for key, value in states.items():
if isinstance(value, (float, int)):
attribute = f"{metric}.{key.replace(' ', '_')}"
value = int(value) if isinstance(value, bool) else value
statsd.gauge(attribute, value, sample_rate=sample_rate, tags=tags)
_LOGGER.debug("Sent metric %s: %s (tags: %s)", attribute, value, tags)

View File

@ -134,7 +134,7 @@ class TestDatadog(unittest.TestCase):
valid = {"1": 1, "1.0": 1.0, STATE_ON: 1, STATE_OFF: 0}
attributes = {"elevation": 3.2, "temperature": 5.0}
attributes = {"elevation": 3.2, "temperature": 5.0, "up": True, "down": False}
for in_, out in valid.items():
state = mock.MagicMock(
@ -145,9 +145,10 @@ class TestDatadog(unittest.TestCase):
)
handler_method(mock.MagicMock(data={"new_state": state}))
assert mock_client.gauge.call_count == 3
assert mock_client.gauge.call_count == 5
for attribute, value in attributes.items():
value = int(value) if isinstance(value, bool) else value
mock_client.gauge.assert_has_calls(
[
mock.call(