mirror of
https://github.com/home-assistant/core.git
synced 2025-07-20 19:57:07 +00:00
Add IPv6 support to snmp sensor (#84607)
Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
parent
e383533ff3
commit
5ebbeff42d
@ -4,6 +4,7 @@ from __future__ import annotations
|
|||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from pysnmp.error import PySnmpError
|
||||||
import pysnmp.hlapi.asyncio as hlapi
|
import pysnmp.hlapi.asyncio as hlapi
|
||||||
from pysnmp.hlapi.asyncio import (
|
from pysnmp.hlapi.asyncio import (
|
||||||
CommunityData,
|
CommunityData,
|
||||||
@ -11,6 +12,7 @@ from pysnmp.hlapi.asyncio import (
|
|||||||
ObjectIdentity,
|
ObjectIdentity,
|
||||||
ObjectType,
|
ObjectType,
|
||||||
SnmpEngine,
|
SnmpEngine,
|
||||||
|
Udp6TransportTarget,
|
||||||
UdpTransportTarget,
|
UdpTransportTarget,
|
||||||
UsmUserData,
|
UsmUserData,
|
||||||
getCmd,
|
getCmd,
|
||||||
@ -106,6 +108,17 @@ async def async_setup_platform(
|
|||||||
default_value = config.get(CONF_DEFAULT_VALUE)
|
default_value = config.get(CONF_DEFAULT_VALUE)
|
||||||
unique_id = config.get(CONF_UNIQUE_ID)
|
unique_id = config.get(CONF_UNIQUE_ID)
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Try IPv4 first.
|
||||||
|
target = UdpTransportTarget((host, port), timeout=DEFAULT_TIMEOUT)
|
||||||
|
except PySnmpError:
|
||||||
|
# Then try IPv6.
|
||||||
|
try:
|
||||||
|
target = Udp6TransportTarget((host, port), timeout=DEFAULT_TIMEOUT)
|
||||||
|
except PySnmpError as err:
|
||||||
|
_LOGGER.error("Invalid SNMP host: %s", err)
|
||||||
|
return
|
||||||
|
|
||||||
if version == "3":
|
if version == "3":
|
||||||
|
|
||||||
if not authkey:
|
if not authkey:
|
||||||
@ -122,14 +135,14 @@ async def async_setup_platform(
|
|||||||
authProtocol=getattr(hlapi, MAP_AUTH_PROTOCOLS[authproto]),
|
authProtocol=getattr(hlapi, MAP_AUTH_PROTOCOLS[authproto]),
|
||||||
privProtocol=getattr(hlapi, MAP_PRIV_PROTOCOLS[privproto]),
|
privProtocol=getattr(hlapi, MAP_PRIV_PROTOCOLS[privproto]),
|
||||||
),
|
),
|
||||||
UdpTransportTarget((host, port), timeout=DEFAULT_TIMEOUT),
|
target,
|
||||||
ContextData(),
|
ContextData(),
|
||||||
]
|
]
|
||||||
else:
|
else:
|
||||||
request_args = [
|
request_args = [
|
||||||
SnmpEngine(),
|
SnmpEngine(),
|
||||||
CommunityData(community, mpModel=SNMP_VERSIONS[version]),
|
CommunityData(community, mpModel=SNMP_VERSIONS[version]),
|
||||||
UdpTransportTarget((host, port), timeout=DEFAULT_TIMEOUT),
|
target,
|
||||||
ContextData(),
|
ContextData(),
|
||||||
]
|
]
|
||||||
get_result = await getCmd(*request_args, ObjectType(ObjectIdentity(baseoid)))
|
get_result = await getCmd(*request_args, ObjectType(ObjectIdentity(baseoid)))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user