2019-02-14 04:35:12 +00:00
|
|
|
"""Support for sending data to Logentries webhook endpoint."""
|
2024-03-08 18:15:59 +00:00
|
|
|
|
2016-05-21 18:21:23 +00:00
|
|
|
import json
|
|
|
|
import logging
|
2016-08-24 00:27:54 +00:00
|
|
|
|
2019-12-09 13:22:30 +00:00
|
|
|
import requests
|
2016-08-24 00:27:54 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.const import CONF_TOKEN, EVENT_STATE_CHANGED
|
2022-01-02 15:34:33 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2016-05-21 18:21:23 +00:00
|
|
|
from homeassistant.helpers import state as state_helper
|
2019-12-09 13:22:30 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2022-01-02 15:34:33 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2016-05-21 18:21:23 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN = "logentries"
|
2016-05-21 18:21:23 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_HOST = "https://webhook.logentries.com/noformat/logs/"
|
2016-05-21 18:21:23 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{DOMAIN: vol.Schema({vol.Required(CONF_TOKEN): cv.string})}, extra=vol.ALLOW_EXTRA
|
|
|
|
)
|
2016-05-21 18:21:23 +00:00
|
|
|
|
|
|
|
|
2022-01-02 15:34:33 +00:00
|
|
|
def setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up the Logentries component."""
|
2016-05-21 18:21:23 +00:00
|
|
|
conf = config[DOMAIN]
|
2016-08-24 00:27:54 +00:00
|
|
|
token = conf.get(CONF_TOKEN)
|
2019-09-03 19:14:00 +00:00
|
|
|
le_wh = f"{DEFAULT_HOST}{token}"
|
2016-05-21 18:21:23 +00:00
|
|
|
|
|
|
|
def logentries_event_listener(event):
|
|
|
|
"""Listen for new messages on the bus and sends them to Logentries."""
|
2021-10-21 06:27:42 +00:00
|
|
|
if (state := event.data.get("new_state")) is None:
|
2016-05-21 18:21:23 +00:00
|
|
|
return
|
|
|
|
try:
|
|
|
|
_state = state_helper.state_as_number(state)
|
|
|
|
except ValueError:
|
|
|
|
_state = state.state
|
2019-07-31 19:25:30 +00:00
|
|
|
json_body = [
|
|
|
|
{
|
|
|
|
"domain": state.domain,
|
|
|
|
"entity_id": state.object_id,
|
|
|
|
"attributes": dict(state.attributes),
|
|
|
|
"time": str(event.time_fired),
|
|
|
|
"value": _state,
|
2017-04-30 05:04:49 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
]
|
|
|
|
try:
|
|
|
|
payload = {"host": le_wh, "event": json_body}
|
2016-05-21 18:21:23 +00:00
|
|
|
requests.post(le_wh, data=json.dumps(payload), timeout=10)
|
2024-03-29 06:20:36 +00:00
|
|
|
except requests.exceptions.RequestException:
|
|
|
|
_LOGGER.exception("Error sending to Logentries")
|
2016-05-21 18:21:23 +00:00
|
|
|
|
|
|
|
hass.bus.listen(EVENT_STATE_CHANGED, logentries_event_listener)
|
|
|
|
|
|
|
|
return True
|