2022-02-01 10:12:38 +01:00
|
|
|
"""Implementation of Mikrotik Router sensor entities."""
|
2019-12-03 03:09:30 +01:00
|
|
|
|
|
|
|
import logging
|
2022-02-01 14:07:21 +01:00
|
|
|
from typing import Any, Optional
|
2022-02-01 14:06:33 +01:00
|
|
|
from collections.abc import Mapping
|
2022-02-01 10:12:38 +01:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
2022-02-02 22:13:39 +01:00
|
|
|
from .helper import format_attribute
|
2022-03-27 03:29:36 +02:00
|
|
|
from .model import model_async_setup_entry, MikrotikEntity
|
2022-02-01 10:12:38 +01:00
|
|
|
from .sensor_types import (
|
|
|
|
SENSOR_TYPES,
|
2022-03-27 03:29:36 +02:00
|
|
|
SENSOR_SERVICES,
|
2022-02-04 22:55:20 +01:00
|
|
|
DEVICE_ATTRIBUTES_IFACE_ETHER,
|
|
|
|
DEVICE_ATTRIBUTES_IFACE_SFP,
|
2022-08-21 22:40:37 +02:00
|
|
|
DEVICE_ATTRIBUTES_IFACE_WIRELESS,
|
2022-02-01 10:12:38 +01:00
|
|
|
)
|
2019-12-03 03:09:30 +01:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2020-04-04 19:42:05 +02:00
|
|
|
|
2019-12-03 03:09:30 +01:00
|
|
|
# ---------------------------
|
|
|
|
# async_setup_entry
|
|
|
|
# ---------------------------
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
2022-03-27 03:29:36 +02:00
|
|
|
"""Set up entry for component"""
|
|
|
|
dispatcher = {
|
|
|
|
"MikrotikSensor": MikrotikSensor,
|
|
|
|
"MikrotikInterfaceTrafficSensor": MikrotikInterfaceTrafficSensor,
|
|
|
|
"MikrotikClientTrafficSensor": MikrotikClientTrafficSensor,
|
|
|
|
}
|
|
|
|
await model_async_setup_entry(
|
|
|
|
hass,
|
|
|
|
config_entry,
|
|
|
|
async_add_entities,
|
|
|
|
SENSOR_SERVICES,
|
|
|
|
SENSOR_TYPES,
|
|
|
|
dispatcher,
|
2019-12-03 03:09:30 +01:00
|
|
|
)
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-03 03:09:30 +01:00
|
|
|
|
|
|
|
# ---------------------------
|
2022-03-27 03:29:36 +02:00
|
|
|
# MikrotikSensor
|
2019-12-03 03:09:30 +01:00
|
|
|
# ---------------------------
|
2022-03-27 03:29:36 +02:00
|
|
|
class MikrotikSensor(MikrotikEntity, SensorEntity):
|
2019-12-03 03:09:30 +01:00
|
|
|
"""Define an Mikrotik Controller sensor."""
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-03 03:09:30 +01:00
|
|
|
@property
|
2022-02-01 10:12:38 +01:00
|
|
|
def state(self) -> Optional[str]:
|
|
|
|
"""Return the state."""
|
|
|
|
if self.entity_description.data_attribute:
|
|
|
|
return self._data[self.entity_description.data_attribute]
|
|
|
|
else:
|
|
|
|
return "unknown"
|
2019-12-03 03:09:30 +01:00
|
|
|
|
2022-02-01 14:06:33 +01:00
|
|
|
@property
|
|
|
|
def native_unit_of_measurement(self):
|
|
|
|
"""Return the unit the value is expressed in."""
|
|
|
|
if self.entity_description.native_unit_of_measurement:
|
|
|
|
if self.entity_description.native_unit_of_measurement.startswith("data__"):
|
|
|
|
uom = self.entity_description.native_unit_of_measurement[6:]
|
|
|
|
if uom in self._data:
|
|
|
|
uom = self._data[uom]
|
|
|
|
return uom
|
|
|
|
|
|
|
|
return self.entity_description.native_unit_of_measurement
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2020-04-04 19:42:05 +02:00
|
|
|
|
2022-02-04 22:55:20 +01:00
|
|
|
# ---------------------------
|
|
|
|
# MikrotikInterfaceTrafficSensor
|
|
|
|
# ---------------------------
|
2022-03-27 03:29:36 +02:00
|
|
|
class MikrotikInterfaceTrafficSensor(MikrotikSensor):
|
2022-02-04 22:55:20 +01:00
|
|
|
"""Define an Mikrotik MikrotikInterfaceTrafficSensor sensor."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def extra_state_attributes(self) -> Mapping[str, Any]:
|
|
|
|
"""Return the state attributes."""
|
|
|
|
attributes = super().extra_state_attributes
|
|
|
|
|
|
|
|
if self._data["type"] == "ether":
|
|
|
|
for variable in DEVICE_ATTRIBUTES_IFACE_ETHER:
|
|
|
|
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]
|
|
|
|
|
2022-08-21 22:40:37 +02:00
|
|
|
elif self._data["type"] == "wlan":
|
|
|
|
for variable in DEVICE_ATTRIBUTES_IFACE_WIRELESS:
|
|
|
|
if variable in self._data:
|
|
|
|
attributes[format_attribute(variable)] = self._data[variable]
|
|
|
|
|
2022-02-04 22:55:20 +01:00
|
|
|
return attributes
|
|
|
|
|
|
|
|
|
2020-04-04 19:42:05 +02:00
|
|
|
# ---------------------------
|
2022-01-01 17:26:40 +00:00
|
|
|
# MikrotikClientTrafficSensor
|
2020-04-04 19:42:05 +02:00
|
|
|
# ---------------------------
|
2022-03-27 03:29:36 +02:00
|
|
|
class MikrotikClientTrafficSensor(MikrotikSensor):
|
2022-01-01 17:26:40 +00:00
|
|
|
"""Define an Mikrotik MikrotikClientTrafficSensor sensor."""
|
2020-04-04 19:42:05 +02:00
|
|
|
|
|
|
|
@property
|
2020-12-25 20:28:36 +01:00
|
|
|
def name(self) -> str:
|
2020-04-04 19:42:05 +02:00
|
|
|
"""Return the name."""
|
2022-08-18 09:29:55 +02:00
|
|
|
return f"{self.entity_description.name}"
|
2020-04-04 19:42:05 +02:00
|
|
|
|
2020-04-08 13:41:03 +02:00
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return if controller and accounting feature in Mikrotik is available.
|
2020-12-02 15:38:17 +01:00
|
|
|
Additional check for lan-tx/rx sensors
|
2020-04-08 13:41:03 +02:00
|
|
|
"""
|
2022-02-01 14:06:33 +01:00
|
|
|
if self.entity_description.data_attribute in ["lan-tx", "lan-rx"]:
|
2020-04-11 05:45:36 +02:00
|
|
|
return (
|
|
|
|
self._ctrl.connected()
|
|
|
|
and self._data["available"]
|
|
|
|
and self._data["local_accounting"]
|
|
|
|
)
|
2020-04-08 13:41:03 +02:00
|
|
|
else:
|
2020-04-11 05:45:36 +02:00
|
|
|
return self._ctrl.connected() and self._data["available"]
|