8000 InfluxDb: Include the unit_of_measurement attribute as a measurement field by PeteBa · Pull Request #9790 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

InfluxDb: Include the unit_of_measurement attribute as a measurement field #9790

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 19, 2017
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
8000 5 changes: 4 additions & 1 deletion homeassistant/components/influxdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def influx_event_listener(event):
_state = state.state
_state_key = "state"

include_uom = True
measurement = component_config.get(state.entity_id).get(
CONF_OVERRIDE_MEASUREMENT)
if measurement in (None, ''):
Expand All @@ -161,6 +162,8 @@ def influx_event_listener(event):
measurement = default_measurement
else:
measurement = state.entity_id
else:
include_uom = False

json_body = [
{
Expand All @@ -179,7 +182,7 @@ def influx_event_listener(event):
for key, value in state.attributes.items():
if key in tags_attributes:
json_body[0]['tags'][key] = value
elif key != 'unit_of_measurement':
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appreciate thoughts on whether it would be better to just remove the conditional (key != 'unit_of_measurement') on line 182 and provide a uom field in all cases? Consistency with other attributes has some merit and there are advantages to having this in a field in that you can use it in Grafana tables etc.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like consistency 👍 better to always include it imo (but I'm not a user of this component)

elif key != 'unit_of_measurement' or include_uom:
# If the key is already in fields
if key in json_body[0]['fields']:
key = key + "_"
Expand Down
42 changes: 42 additions & 0 deletions tests/components/test_influxdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,48 @@ def test_event_listener_default_measurement(self, mock_client):
self.assertFalse(mock_client.return_value.write_points.called)
mock_client.return_value.write_points.reset_mock()

def test_event_listener_unit_of_measurement_field(self, mock_client):
"""Test the event listener for unit of measurement field."""
config = {
'influxdb': {
'host': 'host',
'username': 'user',
'password': 'pass',
'override_measurement': 'state',
}
}
assert setup_component(self.hass, influxdb.DOMAIN, config)
self.handler_method = self.hass.bus.listen.call_args_list[0][0][1]

attrs = {
'unit_of_measurement': 'foobars',
}
state = mock.MagicMock(
state='foo', domain='fake', entity_id='fake.entity-id',
object_id='entity', attributes=attrs)
event = mock.MagicMock(data={'new_state': state}, time_fired=12345)
body = [{
'measurement': 'state',
'tags': {
'domain': 'fake',
'entity_id': 'entity',
},
'time': 12345,
'fields': {
'state': 'foo',
'unit_of_measurement_str': 'foobars',
},
}]
self.handler_method(event)
self.assertEqual(
mock_client.return_value.write_points.call_count, 1
)
self.assertEqual(
mock_client.return_value.write_points.call_args,
mock.call(body)
)
mock_client.return_value.write_points.reset_mock()

def test_event_listener_tags_attributes(self, < 4730 span class=pl-s1>mock_client):
"""Test the event listener when some attributes should be tags."""
config = {
Expand Down
0