From e7bcfc1ddc982469b1d15ca7bbe99b168c4f8b3c Mon Sep 17 00:00:00 2001 From: Stefan Jaroschek Date: Mon, 16 Sep 2024 18:09:26 +0200 Subject: [PATCH] Migrate from pysnmp-lextudio to pysnmp --- custom_components/eaton_epdu/__init__.py | 5 +++- custom_components/eaton_epdu/api.py | 33 ++++++++++++++------- custom_components/eaton_epdu/coordinator.py | 9 ++++-- custom_components/eaton_epdu/manifest.json | 2 +- 4 files changed, 34 insertions(+), 15 deletions(-) diff --git a/custom_components/eaton_epdu/__init__.py b/custom_components/eaton_epdu/__init__.py index ca1dd22..6162f8a 100644 --- a/custom_components/eaton_epdu/__init__.py +++ b/custom_components/eaton_epdu/__init__.py @@ -1,6 +1,8 @@ """The Eaton ePDU integration.""" + from __future__ import annotations +from homeassistant.components.snmp import async_get_snmp_engine from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.typing import ConfigType @@ -18,7 +20,8 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Eaton ePDU from a config entry.""" - coordinator = SnmpCoordinator(hass=hass, entry=entry) + snmpEngine = await async_get_snmp_engine(hass) + coordinator = SnmpCoordinator(hass=hass, entry=entry, snmpEngine=snmpEngine) await coordinator.async_config_entry_first_refresh() hass.data[DOMAIN][entry.entry_id] = coordinator diff --git a/custom_components/eaton_epdu/api.py b/custom_components/eaton_epdu/api.py index f08836b..951c833 100644 --- a/custom_components/eaton_epdu/api.py +++ b/custom_components/eaton_epdu/api.py @@ -1,10 +1,15 @@ """API for Eaton ePDU.""" + from __future__ import annotations + import logging import pysnmp.hlapi.asyncio as hlapi +from pysnmp.hlapi.asyncio import SnmpEngine +from homeassistant.components.snmp import async_get_snmp_engine from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant from .const import ( ATTR_AUTH_KEY, @@ -45,11 +50,14 @@ _LOGGER = logging.getLogger(__name__) + class SnmpApi: """Provide an api for Eaton ePDU.""" - def __init__(self, entry: ConfigEntry) -> None: + def __init__(self, entry: ConfigEntry, snmpEngine: SnmpEngine) -> None: """Init the SnmpApi.""" + self._snmpEngine = snmpEngine + self._target = hlapi.UdpTransportTarget( ( entry.data.get(ATTR_HOST), @@ -85,7 +93,7 @@ async def get(self, oids) -> dict: _LOGGER.debug("Get OID(s) %s", oids) result = [] error_indication, error_status, error_index, var_binds = await hlapi.getCmd( - hlapi.SnmpEngine(), + self._snmpEngine, self._credentials, self._target, hlapi.ContextData(), @@ -99,9 +107,7 @@ async def get(self, oids) -> dict: result.append(items) else: raise RuntimeError( - "Got SNMP error: {} {} {}".format( - error_indication, error_status, error_index - ) + "Got SNMP error: {error_indication} {error_status} {error_index}" ) return result[0] @@ -117,8 +123,13 @@ async def get_bulk( result = [] var_binds = __class__.construct_object_types(oids) for _i in range(count): - error_indication, error_status, error_index, var_bind_table = await hlapi.bulkCmd( - hlapi.SnmpEngine(), + ( + error_indication, + error_status, + error_index, + var_bind_table, + ) = await hlapi.bulkCmd( + self._snmpEngine, self._credentials, self._target, hlapi.ContextData(), @@ -135,9 +146,7 @@ async def get_bulk( result.append(items) else: raise RuntimeError( - "Got SNMP error: {} {} {}".format( - error_indication, error_status, error_index - ) + f"Got SNMP error: {error_indication} {error_status} {error_index}" ) var_binds = var_bind_table[-1] @@ -151,7 +160,9 @@ async def get_bulk_auto( start_from=1, ) -> list: """Get table data for given OIDs with determined rown count.""" - return await self.get_bulk(oids, await self.get([count_oid])[count_oid], start_from) + return await self.get_bulk( + oids, await self.get([count_oid])[count_oid], start_from + ) @staticmethod def cast(value): diff --git a/custom_components/eaton_epdu/coordinator.py b/custom_components/eaton_epdu/coordinator.py index 5fb2642..010dbbe 100644 --- a/custom_components/eaton_epdu/coordinator.py +++ b/custom_components/eaton_epdu/coordinator.py @@ -1,9 +1,12 @@ """Eaton ePDU coordinator.""" + from __future__ import annotations from datetime import timedelta import logging +from pysnmp.hlapi.asyncio import SnmpEngine + from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed @@ -36,7 +39,9 @@ class SnmpCoordinator(DataUpdateCoordinator): """Data update coordinator.""" - def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None: + def __init__( + self, hass: HomeAssistant, entry: ConfigEntry, snmpEngine: SnmpEngine + ) -> None: """Initialize the coordinator.""" super().__init__( hass, @@ -44,7 +49,7 @@ def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None: name=DOMAIN, update_interval=timedelta(seconds=60), ) - self._api = SnmpApi(entry) + self._api = SnmpApi(entry, snmpEngine) async def _update_data(self) -> dict: """Fetch the latest data from the source.""" diff --git a/custom_components/eaton_epdu/manifest.json b/custom_components/eaton_epdu/manifest.json index 0b8ea43..a4bdd53 100644 --- a/custom_components/eaton_epdu/manifest.json +++ b/custom_components/eaton_epdu/manifest.json @@ -8,7 +8,7 @@ "homekit": {}, "iot_class": "local_polling", "issue_tracker": "https://github.com/jaroschek/home-assistant-eaton-epdu/issues", - "requirements": ["pysnmp-lextudio==6.0.2"], + "requirements": ["pysnmp==6.2.5"], "ssdp": [], "version": "1.1.0", "zeroconf": []