mirror of
https://github.com/home-assistant/core.git
synced 2025-07-28 15:47:12 +00:00
Remove class, just use state_changed, and update the export data
This commit is contained in:
parent
76ac913fc0
commit
38b564e413
@ -16,20 +16,19 @@ influx:
|
|||||||
dbuser_password: DB_USER_PASSWORD
|
dbuser_password: DB_USER_PASSWORD
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
import requests
|
|
||||||
import socket
|
|
||||||
|
|
||||||
import homeassistant.util as util
|
import homeassistant.util as util
|
||||||
from homeassistant.helpers import validate_config
|
from homeassistant.helpers import validate_config
|
||||||
from homeassistant.const import (MATCH_ALL)
|
from homeassistant.const import (EVENT_STATE_CHANGED, STATE_ON, STATE_OFF,
|
||||||
|
STATE_UNLOCKED, STATE_LOCKED, STATE_UNKNOWN)
|
||||||
|
from homeassistant.components.sun import (STATE_ABOVE_HORIZON,
|
||||||
|
STATE_BELOW_HORIZON)
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
DOMAIN = "influx"
|
DOMAIN = "influx"
|
||||||
DEPENDENCIES = ['recorder']
|
DEPENDENCIES = ['recorder']
|
||||||
|
|
||||||
INFLUX_CLIENT = None
|
|
||||||
|
|
||||||
DEFAULT_HOST = 'localhost'
|
DEFAULT_HOST = 'localhost'
|
||||||
DEFAULT_PORT = 8086
|
DEFAULT_PORT = 8086
|
||||||
DEFAULT_DATABASE = 'home_assistant'
|
DEFAULT_DATABASE = 'home_assistant'
|
||||||
@ -46,7 +45,7 @@ CONF_PASSWORD = 'password'
|
|||||||
def setup(hass, config):
|
def setup(hass, config):
|
||||||
""" Setup the Influx component. """
|
""" Setup the Influx component. """
|
||||||
|
|
||||||
from influxdb import exceptions
|
from influxdb import InfluxDBClient, exceptions
|
||||||
|
|
||||||
if not validate_config(config, {DOMAIN: ['host']}, _LOGGER):
|
if not validate_config(config, {DOMAIN: ['host']}, _LOGGER):
|
||||||
return False
|
return False
|
||||||
@ -59,85 +58,63 @@ def setup(hass, config):
|
|||||||
username = util.convert(conf.get(CONF_USERNAME), str)
|
username = util.convert(conf.get(CONF_USERNAME), str)
|
||||||
password = util.convert(conf.get(CONF_PASSWORD), str)
|
password = util.convert(conf.get(CONF_PASSWORD), str)
|
||||||
|
|
||||||
global INFLUX_CLIENT
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
INFLUX_CLIENT = Influx(host, port, username, password, dbname)
|
influx = InfluxDBClient(host=host, port=port, username=username,
|
||||||
except (socket.gaierror, requests.exceptions.ConnectionError):
|
password=password, database=dbname)
|
||||||
_LOGGER.error("Database is not accessible. "
|
databases = [i['name'] for i in influx.get_list_database()]
|
||||||
|
except exceptions.InfluxDBClientError:
|
||||||
|
_LOGGER.error("Database host is not accessible. "
|
||||||
"Please check your entries in the configuration file.")
|
"Please check your entries in the configuration file.")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
try:
|
if dbname not in databases:
|
||||||
INFLUX_CLIENT.create_database(dbname)
|
_LOGGER.error("Database %s doesn't exist", dbname)
|
||||||
except exceptions.InfluxDBClientError:
|
return False
|
||||||
_LOGGER.info("Database '%s' already exists", dbname)
|
|
||||||
|
|
||||||
INFLUX_CLIENT.switch_user(username, password)
|
|
||||||
INFLUX_CLIENT.switch_database(dbname)
|
|
||||||
|
|
||||||
def event_listener(event):
|
def event_listener(event):
|
||||||
""" Listen for new messages on the bus and sends them to Influx. """
|
""" Listen for new messages on the bus and sends them to Influx. """
|
||||||
event_data = event.as_dict()
|
event_data = event.as_dict()
|
||||||
json_body = []
|
|
||||||
|
|
||||||
if event_data['event_type'] is not 'time_changed':
|
if event_data['event_type'] is not EVENT_STATE_CHANGED:
|
||||||
try:
|
return
|
||||||
entity_id = event_data['data']['entity_id']
|
|
||||||
new_state = event_data['data']['new_state']
|
|
||||||
|
|
||||||
json_body = [
|
state = event_data['data']['new_state']
|
||||||
{
|
|
||||||
"measurement": entity_id.split('.')[1],
|
if state.state == STATE_ON or state.state == STATE_LOCKED or \
|
||||||
"tags": {
|
state.state == STATE_ABOVE_HORIZON:
|
||||||
"type": entity_id.split('.')[0],
|
_state = 1
|
||||||
},
|
elif state.state == STATE_OFF or state.state == STATE_UNLOCKED or \
|
||||||
"time": event_data['time_fired'],
|
state.state == STATE_UNKNOWN or \
|
||||||
"fields": {
|
state.state == STATE_BELOW_HORIZON:
|
||||||
"value": new_state.state
|
_state = 0
|
||||||
}
|
else:
|
||||||
}
|
_state = state.state
|
||||||
]
|
|
||||||
except KeyError:
|
try:
|
||||||
pass
|
measurement = state.attributes['unit_of_measurement']
|
||||||
|
except KeyError:
|
||||||
|
measurement = '{}'.format(state.domain)
|
||||||
|
|
||||||
|
json_body = [
|
||||||
|
{
|
||||||
|
'measurement': measurement,
|
||||||
|
'tags': {
|
||||||
|
'domain': state.domain,
|
||||||
|
'entity_id': state.object_id,
|
||||||
|
},
|
||||||
|
'time': event_data['time_fired'],
|
||||||
|
'fields': {
|
||||||
|
'value': _state,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
if json_body:
|
if json_body:
|
||||||
INFLUX_CLIENT.write_data(json_body)
|
try:
|
||||||
|
influx.write_points(json_body)
|
||||||
|
except exceptions.InfluxDBClientError:
|
||||||
|
_LOGGER.error("Field type conflict")
|
||||||
|
|
||||||
hass.bus.listen(MATCH_ALL, event_listener)
|
hass.bus.listen(EVENT_STATE_CHANGED, event_listener)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=too-many-arguments
|
|
||||||
class Influx(object):
|
|
||||||
""" Implements the handling of an connection to an Influx database.. """
|
|
||||||
|
|
||||||
def __init__(self, host, port, username, password, dbname):
|
|
||||||
|
|
||||||
from influxdb import InfluxDBClient
|
|
||||||
|
|
||||||
self._host = host
|
|
||||||
self._port = port
|
|
||||||
self._username = username
|
|
||||||
self._password = password
|
|
||||||
self._dbname = dbname
|
|
||||||
|
|
||||||
self.client = InfluxDBClient(self._host, self._port, self._username,
|
|
||||||
self._password, self._dbname)
|
|
||||||
|
|
||||||
def switch_user(self, username, password):
|
|
||||||
""" Switch the user to the given one. """
|
|
||||||
self.client.switch_user(username, password)
|
|
||||||
|
|
||||||
def create_database(self, dbname):
|
|
||||||
""" Creates a new Influx database. """
|
|
||||||
self.client.create_database(dbname)
|
|
||||||
|
|
||||||
def switch_database(self, dbname):
|
|
||||||
""" Switch the user to the given one. """
|
|
||||||
return self.client.switch_database(dbname)
|
|
||||||
|
|
||||||
def write_data(self, data):
|
|
||||||
""" Writes data to Influx database. """
|
|
||||||
self.client.write_points(data)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user