8000 Added continue-on-errors, added value template by iamjackg · Pull Request #8971 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Added continue-on-errors, added value template #8971

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

Merged
merged 3 commits into from
Aug 14, 2017
Merged
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
49 changes: 39 additions & 10 deletions homeassistant/components/sensor/snmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.helpers.entity import Entity
from homeassistant.const import (
CONF_HOST, CONF_NAME, CONF_PORT, CONF_UNIT_OF_MEASUREMENT)
CONF_HOST, CONF_NAME, CONF_PORT, CONF_UNIT_OF_MEASUREMENT, STATE_UNKNOWN,
CONF_VALUE_TEMPLATE)

REQUIREMENTS = ['pysnmp==4.3.9']

Expand All @@ -22,6 +23,8 @@
CONF_BASEOID = 'baseoid'
CONF_COMMUNITY = 'community'
CONF_VERSION = 'version'
CONF_ACCEPT_ERRORS = 'accept_errors'
CONF_DEFAULT_VALUE = 'default_value'

DEFAULT_COMMUNITY = 'public'
DEFAULT_HOST = 'localhost'
Expand All @@ -45,6 +48,9 @@
vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
vol.Optional(CONF_VERSION, default=DEFAULT_VERSION):
vol.In(SNMP_VERSIONS),
vol.Optional(CONF_ACCEPT_ERRORS, default=False): cv.boolean,
vol.Optional(CONF_DEFAULT_VALUE): cv.string,
vol.Optional(CONF_VALUE_TEMPLATE): cv.template
})


Expand All @@ -61,6 +67,12 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
baseoid = config.get(CONF_BASEOID)
unit = config.get(CONF_UNIT_OF_MEASUREMENT)
version = config.get(CONF_VERSION)
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

errindication, _, _, _ = next(
getCmd(SnmpEngine(),
Expand All @@ -69,23 +81,27 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
ContextData(),
ObjectType(ObjectIdentity(baseoid))))

if errindication:
if errindication and not accept_errors:
_LOGGER.error("Please check the details in the configuration file")
return False
else:
data = SnmpData(host, port, community, baseoid, version)
add_devices([SnmpSensor(data, name, unit)], True)
data = SnmpData(
host, port, community, baseoid, version, accept_errors,
default_value)
add_devices([SnmpSensor(data, name, unit, value_template)], True)


class SnmpSensor(Entity):
"""Representation of a SNMP sensor."""

def __init__(self, data, name, unit_of_measurement):
def __init__(self, data, name, unit_of_measurement,
value_template):
"""Initialize the sensor."""
self.data = data
self._name = name
self._state = None
self._unit_of_measurement = unit_of_measurement
self._value_template = value_template

@property
def name(self):
Expand All @@ -105,19 +121,30 @@ def unit_of_measurement(self):
def update(self):
"""Get the latest data and updates the states."""
self.data.update()
self._state = self.data.value
value = self.data.value

if value is None:
value = STATE_UNKNOWN
elif self._value_template is not None:
value = self._value_template.render_with_possible_json_value(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have json inside a snmp value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible and entirely device-dependent, as SNMP can return string values. Any manufacturer can decide that one of their OIDs is going to return a JSON representation of some data. I'd rather leave the opportunity and cover the odd edge case.

value, STATE_UNKNOWN)

self._state = value


class SnmpData(object):
"""Get the latest data and update the states."""

def __init__(self, host, port, community, baseoid, version):
def __init__(self, host, port, community, baseoid, version, accept_errors,
default_value):
"""Initialize the data object."""
self._host = host
self._port = port
self._community = community
self._baseoid = baseoid
self._version = SNMP_VERSIONS[version]
self._accept_errors = accept_errors
self._default_value = default_value
self.value = None

def update(self):
Expand All @@ -133,11 +160,13 @@ def update(self):
ObjectType(ObjectIdentity(self._baseoid)))
)

if errindication:
if errindication and not self._accept_errors:
_LOGGER.error("SNMP error: %s", errindication)
elif errstatus:
elif errstatus and not self._accept_errors:
_LOGGER.error("SNMP error: %s at %s", errstatus.prettyPrint(),
errindex and restable[-1][int(errindex) - 1] or '?')
elif (errindication or errstatus) and self._accept_errors:
self.value = self._default_value
else:
for resrow in restable:
self.value = resrow[-1]
self.value = str(resrow[-1])
0