Added SFP support #94

This commit is contained in:
tomaae 2021-04-12 14:28:39 +02:00
parent 813455cc81
commit 3bf4c56254
4 changed files with 144 additions and 17 deletions

View file

@ -58,6 +58,29 @@ DEVICE_ATTRIBUTES_IFACE = [
"poe-out", "poe-out",
] ]
DEVICE_ATTRIBUTES_IFACE_SFP = [
"status",
"auto-negotiation",
"advertising",
"link-partner-advertising",
"sfp-temperature",
"sfp-supply-voltage",
"sfp-module-present",
"sfp-tx-bias-current",
"sfp-tx-power",
"sfp-rx-power",
"sfp-rx-loss",
"sfp-tx-fault",
"sfp-type",
"sfp-connector-type",
"sfp-vendor-name",
"sfp-vendor-part-number",
"sfp-vendor-revision",
"sfp-vendor-serial",
"sfp-manufacturing-date",
"eeprom-checksum",
]
DEVICE_ATTRIBUTES_PPP_SECRET = [ DEVICE_ATTRIBUTES_PPP_SECRET = [
"connected", "connected",
"service", "service",
@ -430,6 +453,11 @@ class MikrotikControllerPortBinarySensor(MikrotikControllerBinarySensor):
if variable in self._data: if variable in self._data:
attributes[format_attribute(variable)] = self._data[variable] attributes[format_attribute(variable)] = self._data[variable]
if "sfp-shutdown-temperature" in self._data:
for variable in DEVICE_ATTRIBUTES_IFACE_SFP:
if variable in self._data:
attributes[format_attribute(variable)] = self._data[variable]
return attributes return attributes
@property @property

View file

@ -683,26 +683,31 @@ class MikrotikControllerData:
"sfp-shutdown-temperature" in vals "sfp-shutdown-temperature" in vals
and vals["sfp-shutdown-temperature"] != "" and vals["sfp-shutdown-temperature"] != ""
): ):
# _LOGGER.warning("!!!!!!!SFP Port %s", uid)
self.data["interface"] = parse_api( self.data["interface"] = parse_api(
data=self.data["interface"], data=self.data["interface"],
source=self.api.path("/interface/ethernet"), source=self.api.get_sfp(uid),
key="default-name", key_search="name",
key_secondary="name",
vals=[ vals=[
{"name": "default-name"}, {"name": "status", "default": "unknown"},
{"name": "name", "default_val": "default-name"}, {"name": "auto-negotiation", "default": "unknown"},
{"name": "poe-out", "default": "N/A"}, {"name": "advertising", "default": "unknown"},
{"name": "sfp-shutdown-temperature", "default": ""}, {"name": "link-partner-advertising", "default": "unknown"},
], {"name": "sfp-temperature", "default": "unknown"},
skip=[ {"name": "sfp-supply-voltage", "default": "unknown"},
{"name": "type", "value": "bridge"}, {"name": "sfp-module-present", "default": "unknown"},
{"name": "type", "value": "ppp-in"}, {"name": "sfp-tx-bias-current", "default": "unknown"},
{"name": "type", "value": "pptp-in"}, {"name": "sfp-tx-power", "default": "unknown"},
{"name": "type", "value": "sstp-in"}, {"name": "sfp-rx-power", "default": "unknown"},
{"name": "type", "value": "l2tp-in"}, {"name": "sfp-rx-loss", "default": "unknown"},
{"name": "type", "value": "pppoe-in"}, {"name": "sfp-tx-fault", "default": "unknown"},
{"name": "type", "value": "ovpn-in"}, {"name": "sfp-type", "default": "unknown"},
{"name": "sfp-connector-type", "default": "unknown"},
{"name": "sfp-vendor-name", "default": "unknown"},
{"name": "sfp-vendor-part-number", "default": "unknown"},
{"name": "sfp-vendor-revision", "default": "unknown"},
{"name": "sfp-vendor-serial", "default": "unknown"},
{"name": "sfp-manufacturing-date", "default": "unknown"},
{"name": "eeprom-checksum", "default": "unknown"},
], ],
) )

View file

@ -489,6 +489,61 @@ class MikrotikAPI:
self.lock.release() self.lock.release()
return traffic if traffic else None return traffic if traffic else None
# ---------------------------
# get_sfp
# ---------------------------
def get_sfp(self, interfaces) -> Optional(list):
"""Get sfp info"""
if not self.connection_check():
return None
response = self.path("/interface/ethernet", return_list=False)
if response is None:
return None
args = {".id": interfaces, "once": True}
self.lock.acquire()
try:
_LOGGER.debug("API query: %s %s", "/interface/ethernet/monitor", interfaces)
sfpinfo = response("monitor", **args)
except librouteros.exceptions.ConnectionClosed:
self.disconnect()
self.lock.release()
return None
except (
librouteros.exceptions.TrapError,
librouteros.exceptions.MultiTrapError,
librouteros.exceptions.ProtocolError,
librouteros.exceptions.FatalError,
ssl.SSLError,
socket_timeout,
socket_error,
BrokenPipeError,
OSError,
ValueError,
) as api_error:
self.disconnect("get_sfp", api_error)
self.lock.release()
return None
except:
self.disconnect("get_sfp")
self.lock.release()
return None
try:
sfpinfo = list(sfpinfo)
except librouteros.exceptions.ConnectionClosed as api_error:
self.disconnect("get_sfp", api_error)
self.lock.release()
return None
except:
self.disconnect("get_sfp")
self.lock.release()
return None
self.lock.release()
return sfpinfo if sfpinfo else None
# --------------------------- # ---------------------------
# arp_ping # arp_ping
# --------------------------- # ---------------------------

View file

@ -31,6 +31,29 @@ DEVICE_ATTRIBUTES_IFACE = [
"poe-out", "poe-out",
] ]
DEVICE_ATTRIBUTES_IFACE_SFP = [
"status",
"auto-negotiation",
"advertising",
"link-partner-advertising",
"sfp-temperature",
"sfp-supply-voltage",
"sfp-module-present",
"sfp-tx-bias-current",
"sfp-tx-power",
"sfp-rx-power",
"sfp-rx-loss",
"sfp-tx-fault",
"sfp-type",
"sfp-connector-type",
"sfp-vendor-name",
"sfp-vendor-part-number",
"sfp-vendor-revision",
"sfp-vendor-serial",
"sfp-manufacturing-date",
"eeprom-checksum",
]
DEVICE_ATTRIBUTES_NAT = [ DEVICE_ATTRIBUTES_NAT = [
"protocol", "protocol",
"dst-port", "dst-port",
@ -320,6 +343,22 @@ class MikrotikControllerPortSwitch(MikrotikControllerSwitch):
"""Return a unique id for this entity.""" """Return a unique id for this entity."""
return f"{self._inst.lower()}-enable_switch-{self._data['port-mac-address']}_{self._data['default-name']}" return f"{self._inst.lower()}-enable_switch-{self._data['port-mac-address']}_{self._data['default-name']}"
@property
def device_state_attributes(self) -> Dict[str, Any]:
"""Return the state attributes."""
attributes = self._attrs
for variable in self._sid_data["sid_attr"]:
if variable in self._data:
attributes[format_attribute(variable)] = self._data[variable]
if "sfp-shutdown-temperature" in self._data:
for variable in DEVICE_ATTRIBUTES_IFACE_SFP:
if variable in self._data:
attributes[format_attribute(variable)] = self._data[variable]
return attributes
@property @property
def icon(self) -> str: def icon(self) -> str:
"""Return the icon.""" """Return the icon."""