Skip to content
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

Only enrich events if static properties are available #137

Merged
merged 2 commits into from
Feb 12, 2021
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
3 changes: 2 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[flake8]
max-line-length = 127
extend-ignore = E203, W503
extend-ignore = E203, W503
exclude = wheels-custom-integrations
25 changes: 21 additions & 4 deletions custom_components/elasticsearch/es_doc_publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from homeassistant.const import EVENT_STATE_CHANGED
from homeassistant.helpers import state as state_helper
from homeassistant.helpers.typing import HomeAssistantType, StateType
from pytz import utc

from .const import (
Expand All @@ -24,7 +25,7 @@
class DocumentPublisher:
"""Publishes documents to Elasticsearch"""

def __init__(self, config, gateway, index_manager, hass):
def __init__(self, config, gateway, index_manager, hass: HomeAssistantType):
"""Initialize the publisher"""

self.publish_enabled = config.get(CONF_PUBLISH_ENABLED)
Expand Down Expand Up @@ -127,8 +128,18 @@ def enqueue_state(self, entry):
domain = state.domain
entity_id = state.entity_id

if not self.publish_enabled:
LOGGER.warning(
"Attempted to queue a state change for %s.%s, but publish is not enabled. This is a no-op (and a bug).",
domain,
entity_id,
)
return

if domain in self._excluded_domains:
LOGGER.debug("Skipping %s: it belongs to an excluded domain", entity_id)
LOGGER.debug(
"Skipping %s: it belongs to an excluded domain (%s)", entity_id, domain
)
return

if entity_id in self._excluded_entities:
Expand Down Expand Up @@ -199,7 +210,7 @@ async def async_bulk_sync_wrapper(self, actions):
except ElasticsearchException as err:
LOGGER.exception("Error publishing documents to Elasticsearch: %s", err)

def _state_to_bulk_action(self, state, time):
def _state_to_bulk_action(self, state: StateType, time):
"""Creates a bulk action from the given state object"""
try:
_state = state_helper.state_as_number(state)
Expand Down Expand Up @@ -259,7 +270,13 @@ def _state_to_bulk_action(self, state, time):
"@timestamp": time_tz,
}

document_body.update(self._static_doc_properties)
if self._static_doc_properties is None:
LOGGER.warning(
"Event for entity [%s] is missing static doc properties. This is a bug.",
state.entity_id,
)
else:
document_body.update(self._static_doc_properties)

if (
"latitude" in document_body["hass.attributes"]
Expand Down