2019-04-03 15:40:03 +00:00
|
|
|
"""Support for displaying collected data over SNMP."""
|
2016-06-09 04:16:43 +00:00
|
|
|
from datetime import timedelta
|
2019-05-06 21:31:03 +00:00
|
|
|
import logging
|
2016-08-20 22:40:16 +00:00
|
|
|
|
2016-06-09 04:16:43 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2016-08-20 22:40:16 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
|
|
|
from homeassistant.const import (
|
2019-05-06 21:31:03 +00:00
|
|
|
CONF_HOST, CONF_NAME, CONF_PORT, CONF_UNIT_OF_MEASUREMENT, CONF_USERNAME,
|
|
|
|
CONF_VALUE_TEMPLATE, STATE_UNKNOWN)
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
from homeassistant.helpers.entity import Entity
|
2016-06-09 04:16:43 +00:00
|
|
|
|
2019-05-06 21:31:03 +00:00
|
|
|
from .const import (
|
|
|
|
CONF_ACCEPT_ERRORS, CONF_AUTH_KEY, CONF_AUTH_PROTOCOL, CONF_BASEOID,
|
|
|
|
CONF_COMMUNITY, CONF_DEFAULT_VALUE, CONF_PRIV_KEY, CONF_PRIV_PROTOCOL,
|
|
|
|
CONF_VERSION, DEFAULT_AUTH_PROTOCOL, DEFAULT_COMMUNITY, DEFAULT_HOST,
|
|
|
|
DEFAULT_NAME, DEFAULT_PORT, DEFAULT_PRIV_PROTOCOL, DEFAULT_VERSION,
|
|
|
|
MAP_AUTH_PROTOCOLS, MAP_PRIV_PROTOCOLS, SNMP_VERSIONS)
|
2016-06-09 04:16:43 +00:00
|
|
|
|
2019-05-06 21:31:03 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2016-06-09 04:16:43 +00:00
|
|
|
|
2017-06-15 19:23:55 +00:00
|
|
|
SCAN_INTERVAL = timedelta(seconds=10)
|
2016-06-09 04:16:43 +00:00
|
|
|
|
2016-08-20 22:40:16 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_BASEOID): cv.string,
|
2017-10-26 08:38:27 +00:00
|
|
|
vol.Optional(CONF_ACCEPT_ERRORS, default=False): cv.boolean,
|
2016-08-20 22:40:16 +00:00
|
|
|
vol.Optional(CONF_COMMUNITY, default=DEFAULT_COMMUNITY): cv.string,
|
2017-10-26 08:38:27 +00:00
|
|
|
vol.Optional(CONF_DEFAULT_VALUE): cv.string,
|
2016-08-20 22:40:16 +00:00
|
|
|
vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
|
|
|
vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
|
2017-10-26 08:38:27 +00:00
|
|
|
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
|
|
|
|
vol.Optional(CONF_VERSION, default=DEFAULT_VERSION): vol.In(SNMP_VERSIONS),
|
2018-09-07 18:51:18 +00:00
|
|
|
vol.Optional(CONF_USERNAME): cv.string,
|
|
|
|
vol.Optional(CONF_AUTH_KEY): cv.string,
|
|
|
|
vol.Optional(CONF_AUTH_PROTOCOL, default=DEFAULT_AUTH_PROTOCOL):
|
|
|
|
vol.In(MAP_AUTH_PROTOCOLS),
|
|
|
|
vol.Optional(CONF_PRIV_KEY): cv.string,
|
|
|
|
vol.Optional(CONF_PRIV_PROTOCOL, default=DEFAULT_PRIV_PROTOCOL):
|
|
|
|
vol.In(MAP_PRIV_PROTOCOLS),
|
2016-08-20 22:40:16 +00:00
|
|
|
})
|
|
|
|
|
2016-06-09 04:16:43 +00:00
|
|
|
|
2018-09-07 18:51:18 +00:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass, config, async_add_entities, discovery_info=None):
|
2017-02-05 10:22:32 +00:00
|
|
|
"""Set up the SNMP sensor."""
|
2018-09-07 18:51:18 +00:00
|
|
|
from pysnmp.hlapi.asyncio import (
|
2017-03-26 13:53:53 +00:00
|
|
|
getCmd, CommunityData, SnmpEngine, UdpTransportTarget, ContextData,
|
2018-09-07 18:51:18 +00:00
|
|
|
ObjectType, ObjectIdentity, UsmUserData)
|
2016-06-09 04:16:43 +00:00
|
|
|
|
2016-08-20 22:40:16 +00:00
|
|
|
name = config.get(CONF_NAME)
|
2016-06-09 04:16:43 +00:00
|
|
|
host = config.get(CONF_HOST)
|
2016-08-20 22:40:16 +00:00
|
|
|
port = config.get(CONF_PORT)
|
|
|
|
community = config.get(CONF_COMMUNITY)
|
2016-06-09 04:16:43 +00:00
|
|
|
baseoid = config.get(CONF_BASEOID)
|
2016-08-20 22:40:16 +00:00
|
|
|
unit = config.get(CONF_UNIT_OF_MEASUREMENT)
|
2017-05-15 07:34:51 +00:00
|
|
|
version = config.get(CONF_VERSION)
|
2018-09-07 18:51:18 +00:00
|
|
|
username = config.get(CONF_USERNAME)
|
|
|
|
authkey = config.get(CONF_AUTH_KEY)
|
|
|
|
authproto = config.get(CONF_AUTH_PROTOCOL)
|
|
|
|
privkey = config.get(CONF_PRIV_KEY)
|
|
|
|
privproto = config.get(CONF_PRIV_PROTOCOL)
|
2017-08-14 14:31:06 +00:00
|
|
|
accept_errors = config.get(CONF_ACCEPT_ERRORS)
|
|
|
|
default_value = config.get(CONF_DEFAULT_VALUE)
|
|
|
|
value_template = config.get(CONF_VALUE_TEMPLATE)
|
|
|
|
|
|
|
|
if value_template is not None:
|
|
|
|
value_template.hass = hass
|
2016-06-09 04:16:43 +00:00
|
|
|
|
2018-09-07 18:51:18 +00:00
|
|
|
if version == '3':
|
|
|
|
import pysnmp.hlapi.asyncio as hlapi
|
|
|
|
|
|
|
|
if not authkey:
|
|
|
|
authproto = 'none'
|
|
|
|
if not privkey:
|
|
|
|
privproto = 'none'
|
|
|
|
|
|
|
|
request_args = [
|
|
|
|
SnmpEngine(),
|
|
|
|
UsmUserData(
|
|
|
|
username, authKey=authkey or None, privKey=privkey or None,
|
|
|
|
authProtocol=getattr(hlapi, MAP_AUTH_PROTOCOLS[authproto]),
|
|
|
|
privProtocol=getattr(hlapi, MAP_PRIV_PROTOCOLS[privproto]),),
|
|
|
|
UdpTransportTarget((host, port)),
|
|
|
|
ContextData(),
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
request_args = [
|
|
|
|
SnmpEngine(),
|
|
|
|
CommunityData(community, mpModel=SNMP_VERSIONS[version]),
|
|
|
|
UdpTransportTarget((host, port)),
|
|
|
|
ContextData(),
|
|
|
|
]
|
|
|
|
|
|
|
|
errindication, _, _, _ = await getCmd(
|
|
|
|
*request_args, ObjectType(ObjectIdentity(baseoid)))
|
2016-06-09 04:16:43 +00:00
|
|
|
|
2017-08-14 14:31:06 +00:00
|
|
|
if errindication and not accept_errors:
|
2017-02-05 10:22:32 +00:00
|
|
|
_LOGGER.error("Please check the details in the configuration file")
|
2018-09-07 18:51:18 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
data = SnmpData(request_args, baseoid, accept_errors, default_value)
|
|
|
|
async_add_entities([SnmpSensor(data, name, unit, value_template)], True)
|
2016-06-09 04:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SnmpSensor(Entity):
|
|
|
|
"""Representation of a SNMP sensor."""
|
|
|
|
|
2018-09-07 18:51:18 +00:00
|
|
|
def __init__(self, data, name, unit_of_measurement, value_template):
|
2016-06-09 04:16:43 +00:00
|
|
|
"""Initialize the sensor."""
|
|
|
|
self.data = data
|
|
|
|
self._name = name
|
|
|
|
self._state = None
|
|
|
|
self._unit_of_measurement = unit_of_measurement
|
2017-08-14 14:31:06 +00:00
|
|
|
self._value_template = value_template
|
2016-06-09 04:16:43 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit the value is expressed in."""
|
|
|
|
return self._unit_of_measurement
|
|
|
|
|
2018-09-07 18:51:18 +00:00
|
|
|
async def async_update(self):
|
2016-06-09 04:16:43 +00:00
|
|
|
"""Get the latest data and updates the states."""
|
2018-09-07 18:51:18 +00:00
|
|
|
await self.data.async_update()
|
2017-08-14 14:31:06 +00:00
|
|
|
value = self.data.value
|
|
|
|
|
|
|
|
if value is None:
|
|
|
|
value = STATE_UNKNOWN
|
|
|
|
elif self._value_template is not None:
|
2018-09-19 20:01:54 +00:00
|
|
|
value = self._value_template.async_render_with_possible_json_value(
|
2017-08-14 14:31:06 +00:00
|
|
|
value, STATE_UNKNOWN)
|
|
|
|
|
|
|
|
self._state = value
|
2016-06-09 04:16:43 +00:00
|
|
|
|
|
|
|
|
2018-07-20 08:45:20 +00:00
|
|
|
class SnmpData:
|
2016-06-09 04:16:43 +00:00
|
|
|
"""Get the latest data and update the states."""
|
|
|
|
|
2018-09-07 18:51:18 +00:00
|
|
|
def __init__(self, request_args, baseoid, accept_errors, default_value):
|
2016-06-09 04:16:43 +00:00
|
|
|
"""Initialize the data object."""
|
2018-09-07 18:51:18 +00:00
|
|
|
self._request_args = request_args
|
2016-06-09 04:16:43 +00:00
|
|
|
self._baseoid = baseoid
|
2017-08-14 14:31:06 +00:00
|
|
|
self._accept_errors = accept_errors
|
|
|
|
self._default_value = default_value
|
2016-06-09 04:16:43 +00:00
|
|
|
self.value = None
|
|
|
|
|
2018-09-07 18:51:18 +00:00
|
|
|
async def async_update(self):
|
2016-06-09 04:16:43 +00:00
|
|
|
"""Get the latest data from the remote SNMP capable host."""
|
2019-05-06 21:31:03 +00:00
|
|
|
from pysnmp.hlapi.asyncio import getCmd, ObjectType, ObjectIdentity
|
2018-09-07 18:51:18 +00:00
|
|
|
|
|
|
|
errindication, errstatus, errindex, restable = await getCmd(
|
|
|
|
*self._request_args, ObjectType(ObjectIdentity(self._baseoid)))
|
2016-06-09 04:16:43 +00:00
|
|
|
|
2017-08-14 14:31:06 +00:00
|
|
|
if errindication and not self._accept_errors:
|
2016-06-09 04:16:43 +00:00
|
|
|
_LOGGER.error("SNMP error: %s", errindication)
|
2017-08-14 14:31:06 +00:00
|
|
|
elif errstatus and not self._accept_errors:
|
2017-02-05 10:22:32 +00:00
|
|
|
_LOGGER.error("SNMP error: %s at %s", errstatus.prettyPrint(),
|
2016-06-09 04:16:43 +00:00
|
|
|
errindex and restable[-1][int(errindex) - 1] or '?')
|
2017-08-14 14:31:06 +00:00
|
|
|
elif (errindication or errstatus) and self._accept_errors:
|
|
|
|
self.value = self._default_value
|
2016-06-09 04:16:43 +00:00
|
|
|
else:
|
|
|
|
for resrow in restable:
|
2017-08-14 14:31:06 +00:00
|
|
|
self.value = str(resrow[-1])
|