Skip to content

Commit 8504ab4

Browse files
authored
Update snmp_helper.py
1 parent f9165f9 commit 8504ab4

1 file changed

Lines changed: 15 additions & 10 deletions

File tree

custom_components/switch_port_card_pro/snmp_helper.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
get_cmd,
1616
walk_cmd,
1717
)
18+
from .const import (
19+
CONF_OID_IDESCR,
20+
CONF_OID_IFTYPE,
21+
CONF_OID_IFSPEED,
22+
CONF_OID_IFHIGHSPEED,
23+
CONF_OID_SYSDESCR,
24+
)
1825

1926
_LOGGER = logging.getLogger(__name__)
2027

@@ -221,11 +228,11 @@ async def discover_physical_ports(
221228
"""
222229
mapping: dict[int, dict[str, Any]] = {}
223230
logical_port = 1
224-
231+
225232
try:
226233
# Step 1: Get interface descriptions
227234
descr_data = await async_snmp_walk(
228-
hass, host, community, "1.3.6.1.2.1.2.2.1.2", mp_model=mp_model
235+
hass, host, community,CONF_OID_IDESCR, mp_model=mp_model
229236
)
230237
if not descr_data:
231238
_LOGGER.debug("discover_physical_ports: no ifDescr data from %s", host)
@@ -235,21 +242,21 @@ async def discover_physical_ports(
235242

236243
# Step 2: Get interface types (for reliable SFP detection)
237244
type_data = await async_snmp_walk(
238-
hass, host, community, "1.3.6.1.2.1.2.2.1.3", mp_model=mp_model
245+
hass, host, community, CONF_OID_IFTYPE, mp_model=mp_model
239246
)
240247
_LOGGER.debug("ifType data from %s: %d types found", host, len(type_data))
241248

242249
# Step 3: Get speed data
243250
speed_data = await async_snmp_walk(
244-
hass, host, community, "1.3.6.1.2.1.2.2.1.5", mp_model=mp_model
251+
hass, host, community, CONF_OID_IFSPEED, mp_model=mp_model
245252
)
246253
high_speed_data = await async_snmp_walk(
247-
hass, host, community, "1.3.6.1.2.1.31.1.1.1.15", mp_model=mp_model
254+
hass, host, community, CONF_OID_IFHIGHSPEED, mp_model=mp_model
248255
)
249256

250257
# Step 4: Get sysDescr for manufacturer info
251258
sys_descr_data = await async_snmp_walk(
252-
hass, host, community, "1.3.6.1.2.1.1.1.0", mp_model=mp_model
259+
hass, host, community, CONF_OID_SYSDESCR, mp_model=mp_model
253260
)
254261
sys_descr = list(sys_descr_data.values())[0] if sys_descr_data else "Unknown"
255262
_LOGGER.debug("sysDescr from %s: %s", host, sys_descr)
@@ -404,7 +411,6 @@ def _get_interface_type(type_data: dict, if_index: int) -> int:
404411
except (ValueError, TypeError):
405412
return 0
406413

407-
408414
def _detect_sfp_port(if_type: int, descr_lower: str) -> tuple[bool, str]:
409415
"""Detect if port is SFP/fiber based on type and name."""
410416
# Netgear 10G special case
@@ -434,19 +440,18 @@ def _detect_sfp_port(if_type: int, descr_lower: str) -> tuple[bool, str]:
434440

435441
return False, "default_copper"
436442

437-
438443
def _get_port_speed(speed_data: dict, high_speed_data: dict, if_index: int) -> int:
439444
"""Get port speed in Mbps."""
440445
# Try high-speed first (ifHighSpeed)
441-
raw_high = high_speed_data.get(f"1.3.6.1.2.1.31.1.1.1.15.{if_index}")
446+
raw_high = high_speed_data.get(f"{CONF_OID_IFHIGHSPEED}.{if_index}")
442447
if raw_high:
443448
try:
444449
return int(raw_high)
445450
except (ValueError, TypeError):
446451
pass
447452

448453
# Fall back to regular speed (ifSpeed)
449-
raw_speed = speed_data.get(f"1.3.6.1.2.1.2.2.1.5.{if_index}")
454+
raw_speed = speed_data.get(f"{CONF_OID_IFSPEED}.{if_index}")
450455
if raw_speed:
451456
try:
452457
return int(raw_speed) // 1_000_000

0 commit comments

Comments
 (0)