-
-
Notifications
You must be signed in to change notification settings - Fork 28.5k
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
sensor.snmp added decoding of "Opaque" types. #11239
Changes from 3 commits
839c2eb
15c07bd
bc8cfce
b674269
31d6fbb
2d82560
b8cf07e
2b80ac3
938f37b
2f1a31e
be12343
678407f
f0f74ef
7ca57f4
29b5b46
d8ace49
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import unittest | ||
from homeassistant.setup import setup_component | ||
from tests.common import get_test_home_assistant | ||
from threading import Thread | ||
|
||
from pysnmp.entity import engine, config | ||
from pysnmp.entity.rfc3413 import cmdrsp, context | ||
from pysnmp.carrier.asyncore.dgram import udp | ||
from pysnmp.proto.api import v2c | ||
from pyasn1.codec.ber import encoder | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'pyasn1.codec.ber.encoder' imported but unused |
||
from pyasn1.type import univ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'pyasn1.type.univ' imported but unused |
||
|
||
import time | ||
|
||
_PORT = 10161 | ||
_BASE_OID = (1, 3, 6, 1, 4, 1, 6574, 4) | ||
_OID1 = _BASE_OID + (1, 1, 1) | ||
_OID2 = _BASE_OID + (2, 1, 1) | ||
_OID3 = _BASE_OID + (3, 1, 1) | ||
|
||
|
||
class TestSnmp(unittest.TestCase): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add docstrings to the whole file. Thanks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
def setUp(self): | ||
self.hass = get_test_home_assistant() | ||
self.hass.start() | ||
self.snmpEngine = None | ||
self._agent_thread = None | ||
|
||
def tearDown(self): | ||
if self.snmpEngine is not None: | ||
self.snmpEngine.transportDispatcher.jobFinished(1) | ||
self.snmpEngine.transportDispatcher.unregisterRecvCbFun( | ||
recvId=None) | ||
self.snmpEngine.transportDispatcher.unregisterTransport( | ||
udp.domainName) | ||
if self._agent_thread is not None: | ||
self._agent_thread.join(0.01) | ||
self.hass.stop() | ||
|
||
def _run_agent(self): | ||
self.snmpEngine = engine.SnmpEngine() | ||
config.addTransport( | ||
self.snmpEngine, | ||
udp.domainName, | ||
udp.UdpTransport().openServerMode(('127.0.0.1', _PORT)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the engine should be mocked. Why do we need a real engine? We only need to test the home assistant side of the logic. |
||
) | ||
config.addV1System(self.snmpEngine, 'my-area', 'public') | ||
config.addVacmUser(self.snmpEngine, 2, 'my-area', 'noAuthNoPriv', | ||
_BASE_OID) | ||
snmpContext = context.SnmpContext(self.snmpEngine) | ||
mibBuilder = snmpContext.getMibInstrum().getMibBuilder() | ||
|
||
MibScalar, MibScalarInstance = mibBuilder.importSymbols( | ||
'SNMPv2-SMI', 'MibScalar', 'MibScalarInstance' | ||
) | ||
|
||
class MyStaticMibScalarInstance1(MibScalarInstance): | ||
# noinspection PyUnusedLocal,PyUnusedLocal | ||
def getValue(self, name, idx): | ||
return self.getSyntax().clone("test string") | ||
|
||
class MyStaticMibScalarInstance2(MibScalarInstance): | ||
# noinspection PyUnusedLocal,PyUnusedLocal | ||
def getValue(self, name, idx): | ||
return self.getSyntax().clone(1234) | ||
|
||
class MyStaticMibScalarInstance3(MibScalarInstance): | ||
# noinspection PyUnusedLocal,PyUnusedLocal | ||
def getValue(self, name, idx): | ||
encoded_value = encoder.encode(univ.Real(1.17)) | ||
return self.getSyntax().clone(encoded_value) | ||
|
||
mibBuilder.exportSymbols( | ||
'__MY_MIB', MibScalar(_OID1, v2c.OctetString()), | ||
MyStaticMibScalarInstance1(_OID1, (0,), v2c.OctetString()) | ||
) | ||
|
||
mibBuilder.exportSymbols( | ||
'__MY_MIB', MibScalar(_OID2, v2c.Integer()), | ||
MyStaticMibScalarInstance2(_OID2, (0,), v2c.Integer()) | ||
) | ||
|
||
mibBuilder.exportSymbols( | ||
'__MY_MIB', MibScalar(_OID3, v2c.Opaque()), | ||
MyStaticMibScalarInstance3(_OID3, (0,), v2c.Opaque()) | ||
) | ||
|
||
cmdrsp.GetCommandResponder(self.snmpEngine, snmpContext) | ||
cmdrsp.NextCommandResponder(self.snmpEngine, snmpContext) | ||
cmdrsp.BulkCommandResponder(self.snmpEngine, snmpContext) | ||
self.snmpEngine.transportDispatcher.jobStarted(1) | ||
|
||
# Run I/O dispatcher which would receive queries and send responses | ||
try: | ||
self.snmpEngine.transportDispatcher.runDispatcher() | ||
except: | ||
self.snmpEngine.transportDispatcher.closeDispatcher() | ||
raise | ||
|
||
def test_read_values(self): | ||
self._agent_thread = Thread(target=self._run_agent) | ||
self._agent_thread.start() | ||
time.sleep(1) | ||
assert setup_component(self.hass, 'sensor', { | ||
'sensor': [ | ||
{ | ||
'platform': 'snmp', | ||
'host': '127.0.0.1', | ||
'baseoid': '.'.join([str(i) for i in _OID1])+'.0', | ||
'name': 'stringvar', | ||
'community': 'public', | ||
'port': _PORT, | ||
'accept_errors': False, | ||
'version': '2c', | ||
}, | ||
{ | ||
'platform': 'snmp', | ||
'host': '127.0.0.1', | ||
'baseoid': '.'.join([str(i) for i in _OID2])+'.0', | ||
'name': 'intvar', | ||
'community': 'public', | ||
'port': _PORT, | ||
'accept_errors': False, | ||
'version': '2c', | ||
}, | ||
{ | ||
'platform': 'snmp', | ||
'host': '127.0.0.1', | ||
'baseoid': '.'.join([str(i) for i in _OID3]) + '.0', | ||
'name': 'floatvar', | ||
'community': 'public', | ||
'port': _PORT, | ||
'accept_errors': False, | ||
'version': '2c', | ||
}, | ||
] | ||
}) | ||
str_state = self.hass.states.get('sensor.stringvar') | ||
self.assertEquals('test string', str_state.state) | ||
|
||
int_state = self.hass.states.get('sensor.intvar') | ||
self.assertEquals('1234', int_state.state) | ||
|
||
int_state = self.hass.states.get('sensor.floatvar') | ||
self.assertEquals('1.17', int_state.state) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docstring is missing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done