-
-
Notifications
You must be signed in to change notification settings - Fork 32k
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
Add datadog component #7158
Add datadog component #7158
Conversation
@nunofgs, thanks for your PR! By analyzing the history of the files in this pull request, we identified @postlund, @MartinHjelmare and @balloob to be potential reviewers. |
tests/components/test_datadog.py
Outdated
for attribute, value in attributes.items(): | ||
mock_client.gauge.assert_has_calls([ | ||
mock.call("ha.sensor.%s" % attribute, value, sample_rate=1, | ||
tags=["entity:%s" % state.entity_id]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
continuation line under-indented for visual indent
tests/components/test_datadog.py
Outdated
|
||
for in_, out in valid.items(): | ||
state = mock.MagicMock(domain="sensor", entity_id="sensor.foo.bar", | ||
state=in_, attributes=attributes) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
continuation line under-indented for visual indent
tests/components/test_datadog.py
Outdated
import unittest | ||
import voluptuous as vol | ||
|
||
class TestDatadog(unittest.TestCase): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expected 2 blank lines, found 1
homeassistant/components/logbook.py
Outdated
@@ -17,7 +17,7 @@ | |||
from homeassistant.components import sun | |||
from homeassistant.components.frontend import register_built_in_panel | |||
from homeassistant.components.http import HomeAssistantView | |||
from homeassistant.const import (EVENT_HOMEASSISTANT_START, | |||
from homeassistant.const import (EVENT_HOMEASSISTANT_START, EVENT_LOGBOOK_ENTRY, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
line too long (80 > 79 characters)
homeassistant/components/datadog.py
Outdated
|
||
def setup(hass, config): | ||
"""Setup the Datadog component.""" | ||
from datadog import initialize, statsd, DogStatsd |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'datadog.DogStatsd' imported but unused
1c95a26
to
6d82ac1
Compare
tests/components/test_datadog.py
Outdated
|
||
from tests.common import get_test_home_assistant | ||
|
||
class TestDatadog(unittest.TestCase): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expected 2 blank lines, found 1
homeassistant/components/datadog.py
Outdated
try: | ||
value = state_helper.state_as_number(state) | ||
except ValueError: | ||
_LOGGER.debug('Error sending %s: %s (tags: %s)', metric, state.state, tags) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
line too long (87 > 79 characters)
6d82ac1
to
aa53cf2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type change in the state helper needs some thought. Simplest solution would be to move that to the data dog module. As it is now the change will create an error for current users of Influxdb component.
Otherwise looks good. Some style comments. Use python 3 string formatting consistently. Haven't looked at the tests.
homeassistant/helpers/state.py
Outdated
if state.state in (STATE_ON, STATE_LOCKED, STATE_ABOVE_HORIZON, | ||
STATE_OPEN): | ||
if state.state in (STATE_ON, STATE_LOCKED, STATE_ABOVE_HORIZON, STATE_OPEN, | ||
STATE_HOME, STATE_PLAYING): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change of state type will create a type mismatch error for Influxdb schema.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally feel that, in the context of "assigning" a value to a state, STATE_HOME
and STATE_PLAYING
naturally fall under a "1". If anything, we should be adding more states and mapping them to 1
and 0
.
However, I understand the schema issues this may bring. In fact, it's probably an issue for other components as well (logentries for example). By changing the converted state this PR is introducing a BC and possibly breaking alerts/graphs that people are using.
I'll revert this change but I really think an improved way of converting to a number should be developed, as Datadog (and possibly others) only handle numbers (not strings).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, but we should take more devs opinion and it should go in another PR that targets that topic specifically. It will probably need some discussion on how to handle breakage. We've used migration scripts in the past eg for influxdb. You're welcome to start a PR or RFC issue for discussion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I would prefer it in a new PR too. Also agree that more number representations is better in the long run.
homeassistant/components/datadog.py
Outdated
import voluptuous as vol | ||
|
||
from homeassistant.const import ( | ||
CONF_HOST, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use the whole line width until 79 characters.
homeassistant/components/datadog.py
Outdated
|
||
statsd.event( | ||
title="Home Assistant", | ||
text="%%%%%% \n **%s** %s \n %%%%%%" % (name, message), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use python 3 string formatting, ie 'hello {}'.format('world')
.
aa53cf2
to
ea0f084
Compare
homeassistant/components/datadog.py
Outdated
tags=[ | ||
"entity:{}".format(event.data.get('entity_id'), | ||
"domain:{}".format(event.data.get('domain') | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SyntaxError: invalid syntax
@@ -0,0 +1,118 @@ | |||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TokenError: EOF in multi-line statement
ea0f084
to
d05f83b
Compare
d05f83b
to
4935630
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've looked over the tests now too. Use assert_setup_component
to make sure config is validated when setting up the component.
Aside from that I think it looks good. Please don't rebase/squash your branch/commits during the review process, but just push the new commits. It's a lot easier to see what changed that way. If you want, you can then squash everything when we're ready to merge, although since github now has the squash and merge button, you don't have to do that either.
tests/components/test_datadog.py
Outdated
} | ||
} | ||
|
||
with self.assertRaises(vol.Invalid): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should test this using setup_component
and use the helper context manager assert_setup_component
found in tests/common.py
. See eg: https://github.com/home-assistant/home-assistant/blob/dev/tests/components/alarm_control_panel/test_mqtt.py#L31-L40
Also use assert_setup_component
for the other tests when you setup the component, to make sure the config is validated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call. I grabbed this from the statsd component, which I used for the basis of my component.
Would you accept a commit that improves the test there as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but not in this PR.
tests/components/test_datadog.py
Outdated
|
||
def test_invalid_config(self): | ||
"""Test invalid configuration.""" | ||
with assert_setup_component(0) as config: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
local variable 'config' is assigned to but never used
tests/components/test_datadog.py
Outdated
) | ||
from homeassistant.components import datadog | ||
from homeassistant.setup import setup_component | ||
import homeassistant.components.datadog as datadog |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
redefinition of unused 'datadog' from line 13
tests/components/test_datadog.py
Outdated
from unittest import mock | ||
import unittest | ||
|
||
import voluptuous as vol |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'voluptuous as vol' imported but unused
df68897
to
f36196f
Compare
I think you should use |
f36196f
to
f443185
Compare
f443185
to
aae2ad2
Compare
@MartinHjelmare updated. |
Sorry if I'm not making sense. I meant that you should use the helper |
Nevermind, I think this is fine. 👍 |
Description:
Adds a datadog component. Note that while it is possible to use the existing statsd component to report to Datadog, its true power comes from using tags which requires a datadog-specific library.
I am not affiliated with Datadog in any way. Just a happy user :)
This all started when I saw a tweet by @andre.rb. Since I was already using Datadog, I decided to build a component for HA.
Here's what I've been able to achieve using this component:
With the data in Datadog I'm able to also set up alerts to email/Slack when, for example, a statistical anomaly is detected on a sensor.
Datadog also supports events, which I'm grabbing from the logbook:
Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.io#2452
Example entry for
configuration.yaml
:Note that you need a datadog-agent running. More information about that here.
Checklist:
tox
run successfully. Your PR cannot be merged unless tests passREQUIREMENTS
variable (example).requirements_all.txt
by runningscript/gen_requirements_all.py
.