Skip to content
Snippets Groups Projects
Commit ec2595e0 authored by jurgenhaas's avatar jurgenhaas
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
"""
Support gathering alert information from an Alerta instance.
"""
import logging
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (
CONF_NAME, CONF_URL, CONF_API_KEY)
from homeassistant.exceptions import PlatformNotReady
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
REQUIREMENTS = ['alerta==7.3.0']
_LOGGER = logging.getLogger(__name__)
CONF_DATA_GROUP = 'data_group'
CONF_ELEMENT = 'element'
DEFAULT_NAME = 'Alerta Demo'
DEFAULT_URL = 'https://alerta-api.herokuapp.com/'
DEFAULT_API_KEY = 'demo-key'
DEFAULT_ICON = 'mdi:desktop-classic'
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_URL, default=DEFAULT_URL): cv.string,
vol.Optional(CONF_API_KEY, default=DEFAULT_API_KEY): cv.string,
})
async def async_setup_platform(
hass, config, async_add_entities, discovery_info=None):
"""Set up the Alerta sensor."""
from alertaclient.api import Client
name = config.get(CONF_NAME)
url = config.get(CONF_URL)
key = config.get(CONF_API_KEY)
alerta = AlertaData(Client(endpoint=url, key=key))
await alerta.async_update()
if not alerta.available:
raise PlatformNotReady
dev = [AlertaSensor(alerta, name)]
async_add_entities(dev, True)
class AlertaSensor(Entity):
"""Implementation of an Alerta sensor."""
def __init__(
self, alerta, name):
"""Initialize the Alerta sensor."""
self.alerta = alerta
self._state = None
self._name = name
@property
def name(self):
"""Return the name of the sensor."""
return '{}'.format(self._name)
@property
def state(self):
"""Return the state of the resources."""
return self._state
@property
def entity_picture(self):
"""Status symbol if type is symbol."""
base = 'https://cdn2.iconfinder.com/data/icons/function_icon_set/'
if self._state == 'ok':
return "{}circle_green.png".format(base)
elif self._state == 'warning':
return "{}warning_48.png".format(base)
elif self._state == 'critical':
return "{}circle_red.png".format(base)
else:
return "{}refresh_48.png".format(base)
@property
def available(self):
"""Could the resource be accessed during the last update call."""
return self.alerta.available
async def async_update(self):
"""Get the latest data from Alerta API."""
await self.alerta.async_update()
self._state = 'ok'
for alert in self.alerta.alerts:
if alert.severity in ['security', 'critical', 'major']:
self._state = 'critical'
return
self._state = 'warning'
class AlertaData:
"""The class for handling the data retrieval."""
def __init__(self, api):
"""Initialize the data object."""
self.api = api
self.available = True
self.endpoint = self.api.endpoint
self.status = []
self.alerts = []
self.keys = []
async def async_update(self):
"""Get the latest data from the Alerta API."""
import requests
try:
self.status = self.api.mgmt_status()
self.alerts = self.api.get_alerts([('status', 'open')])
self.keys = self.api.get_keys()
self.available = True
except requests.exceptions.RequestException:
_LOGGER.error(
"Unable to retrieve data from Alerta host %s" % self.endpoint)
self.available = False
self.status = []
self.alerts = []
self.keys = []
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment