Skip to content

Commit

Permalink
Merge pull request #10 from jaroschek/release/1.1.x
Browse files Browse the repository at this point in the history
Release 1.1.0
  • Loading branch information
jaroschek authored Sep 16, 2024
2 parents 6d32a7f + 779c36c commit dcfb9ee
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
5 changes: 4 additions & 1 deletion custom_components/eaton_epdu/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
33 changes: 22 additions & 11 deletions custom_components/eaton_epdu/api.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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(),
Expand All @@ -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]
Expand All @@ -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(),
Expand All @@ -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]
Expand All @@ -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):
Expand Down
9 changes: 7 additions & 2 deletions custom_components/eaton_epdu/coordinator.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -36,15 +39,17 @@
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,
_LOGGER,
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."""
Expand Down
2 changes: 1 addition & 1 deletion custom_components/eaton_epdu/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
Expand Down

0 comments on commit dcfb9ee

Please sign in to comment.