Add posibility to specify snmp protocol version (#7564)

* snmp sensor protocol version configuration option

* fixed lint findings.
This commit is contained in:
jhemzal 2017-05-15 09:34:51 +02:00 committed by Paulus Schoutsen
parent e2e58e6acc
commit 5c4a21efac

View File

@ -22,11 +22,18 @@ _LOGGER = logging.getLogger(__name__)
CONF_BASEOID = 'baseoid' CONF_BASEOID = 'baseoid'
CONF_COMMUNITY = 'community' CONF_COMMUNITY = 'community'
CONF_VERSION = 'version'
DEFAULT_COMMUNITY = 'public' DEFAULT_COMMUNITY = 'public'
DEFAULT_HOST = 'localhost' DEFAULT_HOST = 'localhost'
DEFAULT_NAME = 'SNMP' DEFAULT_NAME = 'SNMP'
DEFAULT_PORT = '161' DEFAULT_PORT = '161'
DEFAULT_VERSION = '1'
SNMP_VERSIONS = {
'1': 0,
'2c': 1
}
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=10) MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=10)
@ -37,6 +44,8 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port, vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string, vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
vol.Optional(CONF_VERSION, default=DEFAULT_VERSION):
vol.In(SNMP_VERSIONS),
}) })
@ -52,10 +61,11 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
community = config.get(CONF_COMMUNITY) community = config.get(CONF_COMMUNITY)
baseoid = config.get(CONF_BASEOID) baseoid = config.get(CONF_BASEOID)
unit = config.get(CONF_UNIT_OF_MEASUREMENT) unit = config.get(CONF_UNIT_OF_MEASUREMENT)
version = config.get(CONF_VERSION)
errindication, _, _, _ = next( errindication, _, _, _ = next(
getCmd(SnmpEngine(), getCmd(SnmpEngine(),
CommunityData(community, mpModel=0), CommunityData(community, mpModel=SNMP_VERSIONS[version]),
UdpTransportTarget((host, port)), UdpTransportTarget((host, port)),
ContextData(), ContextData(),
ObjectType(ObjectIdentity(baseoid)))) ObjectType(ObjectIdentity(baseoid))))
@ -64,7 +74,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
_LOGGER.error("Please check the details in the configuration file") _LOGGER.error("Please check the details in the configuration file")
return False return False
else: else:
data = SnmpData(host, port, community, baseoid) data = SnmpData(host, port, community, baseoid, version)
add_devices([SnmpSensor(data, name, unit)]) add_devices([SnmpSensor(data, name, unit)])
@ -103,12 +113,13 @@ class SnmpSensor(Entity):
class SnmpData(object): class SnmpData(object):
"""Get the latest data and update the states.""" """Get the latest data and update the states."""
def __init__(self, host, port, community, baseoid): def __init__(self, host, port, community, baseoid, version):
"""Initialize the data object.""" """Initialize the data object."""
self._host = host self._host = host
self._port = port self._port = port
self._community = community self._community = community
self._baseoid = baseoid self._baseoid = baseoid
self._version = SNMP_VERSIONS[version]
self.value = None self.value = None
@Throttle(MIN_TIME_BETWEEN_UPDATES) @Throttle(MIN_TIME_BETWEEN_UPDATES)
@ -119,7 +130,7 @@ class SnmpData(object):
ObjectType, ObjectIdentity) ObjectType, ObjectIdentity)
errindication, errstatus, errindex, restable = next( errindication, errstatus, errindex, restable = next(
getCmd(SnmpEngine(), getCmd(SnmpEngine(),
CommunityData(self._community, mpModel=0), CommunityData(self._community, mpModel=self._version),
UdpTransportTarget((self._host, self._port)), UdpTransportTarget((self._host, self._port)),
ContextData(), ContextData(),
ObjectType(ObjectIdentity(self._baseoid))) ObjectType(ObjectIdentity(self._baseoid)))