Add posibility to specify snmp protocol version (#7564)
* snmp sensor protocol version configuration option * fixed lint findings.
This commit is contained in:
parent
e2e58e6acc
commit
5c4a21efac
1 changed files with 15 additions and 4 deletions
|
@ -22,11 +22,18 @@ _LOGGER = logging.getLogger(__name__)
|
|||
|
||||
CONF_BASEOID = 'baseoid'
|
||||
CONF_COMMUNITY = 'community'
|
||||
CONF_VERSION = 'version'
|
||||
|
||||
DEFAULT_COMMUNITY = 'public'
|
||||
DEFAULT_HOST = 'localhost'
|
||||
DEFAULT_NAME = 'SNMP'
|
||||
DEFAULT_PORT = '161'
|
||||
DEFAULT_VERSION = '1'
|
||||
|
||||
SNMP_VERSIONS = {
|
||||
'1': 0,
|
||||
'2c': 1
|
||||
}
|
||||
|
||||
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_PORT, default=DEFAULT_PORT): cv.port,
|
||||
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)
|
||||
baseoid = config.get(CONF_BASEOID)
|
||||
unit = config.get(CONF_UNIT_OF_MEASUREMENT)
|
||||
version = config.get(CONF_VERSION)
|
||||
|
||||
errindication, _, _, _ = next(
|
||||
getCmd(SnmpEngine(),
|
||||
CommunityData(community, mpModel=0),
|
||||
CommunityData(community, mpModel=SNMP_VERSIONS[version]),
|
||||
UdpTransportTarget((host, port)),
|
||||
ContextData(),
|
||||
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")
|
||||
return False
|
||||
else:
|
||||
data = SnmpData(host, port, community, baseoid)
|
||||
data = SnmpData(host, port, community, baseoid, version)
|
||||
add_devices([SnmpSensor(data, name, unit)])
|
||||
|
||||
|
||||
|
@ -103,12 +113,13 @@ class SnmpSensor(Entity):
|
|||
class SnmpData(object):
|
||||
"""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."""
|
||||
self._host = host
|
||||
self._port = port
|
||||
self._community = community
|
||||
self._baseoid = baseoid
|
||||
self._version = SNMP_VERSIONS[version]
|
||||
self.value = None
|
||||
|
||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||
|
@ -119,7 +130,7 @@ class SnmpData(object):
|
|||
ObjectType, ObjectIdentity)
|
||||
errindication, errstatus, errindex, restable = next(
|
||||
getCmd(SnmpEngine(),
|
||||
CommunityData(self._community, mpModel=0),
|
||||
CommunityData(self._community, mpModel=self._version),
|
||||
UdpTransportTarget((self._host, self._port)),
|
||||
ContextData(),
|
||||
ObjectType(ObjectIdentity(self._baseoid)))
|
||||
|
|
Loading…
Add table
Reference in a new issue