2019-12-03 03:09:30 +01:00
|
|
|
"""Support for the Mikrotik Router sensor service."""
|
|
|
|
|
|
|
|
import logging
|
2020-03-21 19:02:28 +03:00
|
|
|
|
2020-05-27 20:04:09 +02:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_NAME,
|
|
|
|
ATTR_ATTRIBUTION,
|
|
|
|
ATTR_DEVICE_CLASS,
|
|
|
|
TEMP_CELSIUS,
|
|
|
|
)
|
2020-12-12 12:19:40 +01:00
|
|
|
|
|
|
|
from .const import (
|
|
|
|
CONF_SENSOR_PORT_TRAFFIC,
|
|
|
|
DEFAULT_SENSOR_PORT_TRAFFIC,
|
|
|
|
)
|
|
|
|
|
2019-12-03 03:09:30 +01:00
|
|
|
from homeassistant.core import callback
|
2019-12-08 11:20:02 +01:00
|
|
|
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
|
2019-12-03 03:09:30 +01:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
|
|
|
from homeassistant.helpers.entity import Entity
|
2020-03-21 19:02:28 +03:00
|
|
|
|
2020-04-11 05:45:36 +02:00
|
|
|
from .const import DOMAIN, DATA_CLIENT, ATTRIBUTION
|
2019-12-03 03:09:30 +01:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2020-04-04 19:42:05 +02:00
|
|
|
|
|
|
|
# ---------------------------
|
|
|
|
# format_attribute
|
|
|
|
# ---------------------------
|
|
|
|
def format_attribute(attr):
|
|
|
|
res = attr.replace("-", " ")
|
|
|
|
res = res.capitalize()
|
|
|
|
res = res.replace(" ip ", " IP ")
|
|
|
|
res = res.replace(" mac ", " MAC ")
|
|
|
|
res = res.replace(" mtu", " MTU")
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
2019-12-03 03:09:30 +01:00
|
|
|
ATTR_ICON = "icon"
|
|
|
|
ATTR_LABEL = "label"
|
|
|
|
ATTR_UNIT = "unit"
|
2020-03-16 01:32:13 +01:00
|
|
|
ATTR_UNIT_ATTR = "unit_attr"
|
2019-12-04 15:48:53 +01:00
|
|
|
ATTR_GROUP = "group"
|
|
|
|
ATTR_PATH = "data_path"
|
|
|
|
ATTR_ATTR = "data_attr"
|
2019-12-03 03:09:30 +01:00
|
|
|
|
|
|
|
SENSOR_TYPES = {
|
2020-12-02 10:52:34 +01:00
|
|
|
"system_uptime": {
|
|
|
|
ATTR_DEVICE_CLASS: None,
|
|
|
|
ATTR_ICON: "mdi:clock-outline",
|
|
|
|
ATTR_LABEL: "Uptime",
|
2020-12-13 02:47:03 +01:00
|
|
|
ATTR_UNIT: "h",
|
2020-12-02 10:52:34 +01:00
|
|
|
ATTR_GROUP: "System",
|
|
|
|
ATTR_PATH: "resource",
|
|
|
|
ATTR_ATTR: "uptime",
|
|
|
|
},
|
2020-05-27 20:04:09 +02:00
|
|
|
"system_temperature": {
|
|
|
|
ATTR_DEVICE_CLASS: None,
|
|
|
|
ATTR_ICON: "mdi:thermometer",
|
|
|
|
ATTR_LABEL: "Temperature",
|
|
|
|
ATTR_UNIT: TEMP_CELSIUS,
|
|
|
|
ATTR_GROUP: "System",
|
|
|
|
ATTR_PATH: "health",
|
|
|
|
ATTR_ATTR: "temperature",
|
|
|
|
},
|
2020-03-16 04:51:41 +01:00
|
|
|
"system_cpu-load": {
|
2019-12-03 03:09:30 +01:00
|
|
|
ATTR_DEVICE_CLASS: None,
|
|
|
|
ATTR_ICON: "mdi:speedometer",
|
2020-03-16 04:51:41 +01:00
|
|
|
ATTR_LABEL: "CPU load",
|
2019-12-03 03:09:30 +01:00
|
|
|
ATTR_UNIT: "%",
|
2019-12-04 15:48:53 +01:00
|
|
|
ATTR_GROUP: "System",
|
|
|
|
ATTR_PATH: "resource",
|
|
|
|
ATTR_ATTR: "cpu-load",
|
2019-12-03 03:09:30 +01:00
|
|
|
},
|
2020-03-16 04:51:41 +01:00
|
|
|
"system_memory-usage": {
|
2019-12-03 03:09:30 +01:00
|
|
|
ATTR_DEVICE_CLASS: None,
|
|
|
|
ATTR_ICON: "mdi:memory",
|
2020-03-16 04:51:41 +01:00
|
|
|
ATTR_LABEL: "Memory usage",
|
2019-12-03 03:09:30 +01:00
|
|
|
ATTR_UNIT: "%",
|
2019-12-04 15:48:53 +01:00
|
|
|
ATTR_GROUP: "System",
|
|
|
|
ATTR_PATH: "resource",
|
|
|
|
ATTR_ATTR: "memory-usage",
|
2019-12-03 03:09:30 +01:00
|
|
|
},
|
2020-03-16 04:51:41 +01:00
|
|
|
"system_hdd-usage": {
|
2019-12-03 03:09:30 +01:00
|
|
|
ATTR_DEVICE_CLASS: None,
|
|
|
|
ATTR_ICON: "mdi:harddisk",
|
2020-03-16 04:51:41 +01:00
|
|
|
ATTR_LABEL: "HDD usage",
|
2019-12-03 03:09:30 +01:00
|
|
|
ATTR_UNIT: "%",
|
2019-12-04 15:48:53 +01:00
|
|
|
ATTR_GROUP: "System",
|
|
|
|
ATTR_PATH: "resource",
|
|
|
|
ATTR_ATTR: "hdd-usage",
|
2019-12-03 03:09:30 +01:00
|
|
|
},
|
2020-03-16 04:51:41 +01:00
|
|
|
"traffic_tx": {
|
2019-12-08 11:20:02 +01:00
|
|
|
ATTR_DEVICE_CLASS: None,
|
|
|
|
ATTR_ICON: "mdi:upload-network-outline",
|
2020-03-16 04:51:41 +01:00
|
|
|
ATTR_LABEL: "TX",
|
2020-03-16 01:32:13 +01:00
|
|
|
ATTR_UNIT: "ps",
|
|
|
|
ATTR_UNIT_ATTR: "tx-bits-per-second-attr",
|
2019-12-08 11:20:02 +01:00
|
|
|
ATTR_PATH: "interface",
|
|
|
|
ATTR_ATTR: "tx-bits-per-second",
|
|
|
|
},
|
2020-03-16 04:51:41 +01:00
|
|
|
"traffic_rx": {
|
2019-12-08 11:20:02 +01:00
|
|
|
ATTR_DEVICE_CLASS: None,
|
|
|
|
ATTR_ICON: "mdi:download-network-outline",
|
2020-03-16 04:51:41 +01:00
|
|
|
ATTR_LABEL: "RX",
|
2020-03-16 01:32:13 +01:00
|
|
|
ATTR_UNIT: "ps",
|
|
|
|
ATTR_UNIT_ATTR: "rx-bits-per-second-attr",
|
2019-12-08 11:20:02 +01:00
|
|
|
ATTR_PATH: "interface",
|
|
|
|
ATTR_ATTR: "rx-bits-per-second",
|
|
|
|
},
|
2020-04-04 19:42:05 +02:00
|
|
|
"accounting_lan_tx": {
|
|
|
|
ATTR_DEVICE_CLASS: None,
|
2020-04-08 16:01:27 +02:00
|
|
|
ATTR_ICON: "mdi:upload-network",
|
2020-04-04 19:42:05 +02:00
|
|
|
ATTR_LABEL: "LAN TX",
|
|
|
|
ATTR_UNIT: "ps",
|
2020-04-05 15:03:17 +02:00
|
|
|
ATTR_UNIT_ATTR: "tx-rx-attr",
|
2020-04-10 12:25:37 +02:00
|
|
|
ATTR_PATH: "accounting",
|
2020-04-04 19:42:05 +02:00
|
|
|
ATTR_ATTR: "lan-tx",
|
|
|
|
},
|
|
|
|
"accounting_lan_rx": {
|
|
|
|
ATTR_DEVICE_CLASS: None,
|
2020-04-08 16:01:27 +02:00
|
|
|
ATTR_ICON: "mdi:download-network",
|
2020-04-04 19:42:05 +02:00
|
|
|
ATTR_LABEL: "LAN RX",
|
|
|
|
ATTR_UNIT: "ps",
|
2020-04-05 15:03:17 +02:00
|
|
|
ATTR_UNIT_ATTR: "tx-rx-attr",
|
2020-04-10 12:25:37 +02:00
|
|
|
ATTR_PATH: "accounting",
|
2020-04-04 19:42:05 +02:00
|
|
|
ATTR_ATTR: "lan-rx",
|
|
|
|
},
|
|
|
|
"accounting_wan_tx": {
|
|
|
|
ATTR_DEVICE_CLASS: None,
|
2020-04-08 16:01:27 +02:00
|
|
|
ATTR_ICON: "mdi:upload-network",
|
2020-04-04 19:42:05 +02:00
|
|
|
ATTR_LABEL: "WAN TX",
|
|
|
|
ATTR_UNIT: "ps",
|
2020-04-05 15:03:17 +02:00
|
|
|
ATTR_UNIT_ATTR: "tx-rx-attr",
|
2020-04-10 12:25:37 +02:00
|
|
|
ATTR_PATH: "accounting",
|
2020-04-04 19:42:05 +02:00
|
|
|
ATTR_ATTR: "wan-tx",
|
|
|
|
},
|
|
|
|
"accounting_wan_rx": {
|
|
|
|
ATTR_DEVICE_CLASS: None,
|
2020-04-08 16:01:27 +02:00
|
|
|
ATTR_ICON: "mdi:download-network",
|
2020-04-04 19:42:05 +02:00
|
|
|
ATTR_LABEL: "WAN RX",
|
|
|
|
ATTR_UNIT: "ps",
|
2020-04-05 15:03:17 +02:00
|
|
|
ATTR_UNIT_ATTR: "tx-rx-attr",
|
2020-04-10 12:25:37 +02:00
|
|
|
ATTR_PATH: "accounting",
|
2020-04-04 19:42:05 +02:00
|
|
|
ATTR_ATTR: "wan-rx",
|
|
|
|
},
|
2020-12-13 03:43:41 +01:00
|
|
|
"environment": {
|
|
|
|
ATTR_DEVICE_CLASS: None,
|
|
|
|
ATTR_ICON: "mdi:clipboard-list",
|
|
|
|
ATTR_LABEL: "",
|
|
|
|
ATTR_UNIT: "",
|
|
|
|
# ATTR_UNIT_ATTR: "value",
|
|
|
|
ATTR_GROUP: "Environment",
|
|
|
|
ATTR_PATH: "environment",
|
|
|
|
ATTR_ATTR: "value",
|
|
|
|
},
|
2019-12-03 03:09:30 +01:00
|
|
|
}
|
|
|
|
|
2020-04-11 05:45:36 +02:00
|
|
|
DEVICE_ATTRIBUTES_ACCOUNTING = ["address", "mac-address", "host-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):
|
|
|
|
"""Set up device tracker for Mikrotik Router component."""
|
2019-12-06 01:22:34 +01:00
|
|
|
inst = config_entry.data[CONF_NAME]
|
2019-12-03 03:09:30 +01:00
|
|
|
mikrotik_controller = hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id]
|
|
|
|
sensors = {}
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-03 03:09:30 +01:00
|
|
|
@callback
|
|
|
|
def update_controller():
|
|
|
|
"""Update the values of the controller."""
|
2020-12-12 12:19:40 +01:00
|
|
|
update_items(
|
|
|
|
inst, config_entry, mikrotik_controller, async_add_entities, sensors
|
|
|
|
)
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-03 03:09:30 +01:00
|
|
|
mikrotik_controller.listeners.append(
|
2020-03-16 04:51:41 +01:00
|
|
|
async_dispatcher_connect(
|
|
|
|
hass, mikrotik_controller.signal_update, update_controller
|
|
|
|
)
|
2019-12-03 03:09:30 +01:00
|
|
|
)
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-03 03:09:30 +01:00
|
|
|
update_controller()
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------
|
|
|
|
# update_items
|
|
|
|
# ---------------------------
|
|
|
|
@callback
|
2020-12-12 12:19:40 +01:00
|
|
|
def update_items(inst, config_entry, mikrotik_controller, async_add_entities, sensors):
|
2019-12-03 03:09:30 +01:00
|
|
|
"""Update sensor state from the controller."""
|
|
|
|
new_sensors = []
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-03 03:09:30 +01:00
|
|
|
for sensor in SENSOR_TYPES:
|
2020-04-04 19:42:05 +02:00
|
|
|
if "system_" in sensor:
|
2020-12-14 15:25:00 +01:00
|
|
|
if (
|
|
|
|
mikrotik_controller.data[SENSOR_TYPES[sensor][ATTR_PATH]][
|
|
|
|
SENSOR_TYPES[sensor][ATTR_ATTR]
|
|
|
|
]
|
|
|
|
== "unknown"
|
|
|
|
):
|
|
|
|
continue
|
2020-03-16 19:02:54 +01:00
|
|
|
item_id = f"{inst}-{sensor}"
|
2020-04-04 19:46:40 +02:00
|
|
|
_LOGGER.debug("Updating sensor %s", item_id)
|
2019-12-09 10:07:36 +01:00
|
|
|
if item_id in sensors:
|
|
|
|
if sensors[item_id].enabled:
|
|
|
|
sensors[item_id].async_schedule_update_ha_state()
|
|
|
|
continue
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2020-03-16 04:51:41 +01:00
|
|
|
sensors[item_id] = MikrotikControllerSensor(
|
2020-04-11 05:45:36 +02:00
|
|
|
mikrotik_controller=mikrotik_controller, inst=inst, sensor=sensor
|
2020-03-16 04:51:41 +01:00
|
|
|
)
|
2019-12-09 10:07:36 +01:00
|
|
|
new_sensors.append(sensors[item_id])
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-09 10:07:36 +01:00
|
|
|
if "traffic_" in sensor:
|
2020-12-12 12:19:40 +01:00
|
|
|
if not config_entry.options.get(
|
|
|
|
CONF_SENSOR_PORT_TRAFFIC, DEFAULT_SENSOR_PORT_TRAFFIC
|
|
|
|
):
|
|
|
|
continue
|
|
|
|
|
2020-03-16 04:51:41 +01:00
|
|
|
for uid in mikrotik_controller.data["interface"]:
|
2020-06-26 16:35:40 +02:00
|
|
|
if mikrotik_controller.data["interface"][uid]["type"] != "bridge":
|
2020-03-16 19:02:54 +01:00
|
|
|
item_id = f"{inst}-{sensor}-{mikrotik_controller.data['interface'][uid]['default-name']}"
|
2020-04-04 19:46:40 +02:00
|
|
|
_LOGGER.debug("Updating sensor %s", item_id)
|
2019-12-09 10:07:36 +01:00
|
|
|
if item_id in sensors:
|
|
|
|
if sensors[item_id].enabled:
|
|
|
|
sensors[item_id].async_schedule_update_ha_state()
|
|
|
|
continue
|
|
|
|
|
2020-03-16 04:51:41 +01:00
|
|
|
sensors[item_id] = MikrotikControllerTrafficSensor(
|
|
|
|
mikrotik_controller=mikrotik_controller,
|
|
|
|
inst=inst,
|
|
|
|
sensor=sensor,
|
|
|
|
uid=uid,
|
|
|
|
)
|
2019-12-09 10:07:36 +01:00
|
|
|
new_sensors.append(sensors[item_id])
|
2019-12-08 11:20:02 +01:00
|
|
|
|
2020-04-04 19:42:05 +02:00
|
|
|
if "accounting_" in sensor:
|
|
|
|
for uid in mikrotik_controller.data["accounting"]:
|
2020-04-07 20:31:16 +02:00
|
|
|
item_id = f"{inst}-{sensor}-{mikrotik_controller.data['accounting'][uid]['mac-address']}"
|
2020-04-04 19:42:05 +02:00
|
|
|
if item_id in sensors:
|
|
|
|
if sensors[item_id].enabled:
|
|
|
|
sensors[item_id].async_schedule_update_ha_state()
|
|
|
|
continue
|
|
|
|
|
2020-04-11 05:45:36 +02:00
|
|
|
if (
|
|
|
|
SENSOR_TYPES[sensor][ATTR_ATTR]
|
|
|
|
in mikrotik_controller.data["accounting"][uid].keys()
|
|
|
|
):
|
2020-04-05 15:03:17 +02:00
|
|
|
sensors[item_id] = MikrotikAccountingSensor(
|
|
|
|
mikrotik_controller=mikrotik_controller,
|
|
|
|
inst=inst,
|
|
|
|
sensor=sensor,
|
|
|
|
uid=uid,
|
|
|
|
)
|
|
|
|
new_sensors.append(sensors[item_id])
|
2020-04-05 23:23:07 +02:00
|
|
|
|
2020-12-02 13:56:50 +01:00
|
|
|
for uid in mikrotik_controller.data["environment"]:
|
2020-12-02 15:38:17 +01:00
|
|
|
item_id = (
|
2020-12-13 03:43:41 +01:00
|
|
|
f"{inst}-environment-{mikrotik_controller.data['environment'][uid]['name']}"
|
2020-12-02 15:38:17 +01:00
|
|
|
)
|
2020-12-02 13:56:50 +01:00
|
|
|
_LOGGER.debug("Updating sensor %s", item_id)
|
|
|
|
if item_id in sensors:
|
|
|
|
if sensors[item_id].enabled:
|
|
|
|
sensors[item_id].async_schedule_update_ha_state()
|
|
|
|
continue
|
|
|
|
|
|
|
|
sensors[item_id] = MikrotikControllerEnvironmentSensor(
|
|
|
|
mikrotik_controller=mikrotik_controller,
|
|
|
|
inst=inst,
|
2020-12-13 03:43:41 +01:00
|
|
|
sensor="environment",
|
2020-12-02 13:56:50 +01:00
|
|
|
uid=uid,
|
|
|
|
)
|
|
|
|
new_sensors.append(sensors[item_id])
|
|
|
|
|
2019-12-03 03:09:30 +01:00
|
|
|
if new_sensors:
|
|
|
|
async_add_entities(new_sensors, True)
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-03 03:09:30 +01:00
|
|
|
|
|
|
|
# ---------------------------
|
|
|
|
# MikrotikControllerSensor
|
|
|
|
# ---------------------------
|
|
|
|
class MikrotikControllerSensor(Entity):
|
|
|
|
"""Define an Mikrotik Controller sensor."""
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-06 01:22:34 +01:00
|
|
|
def __init__(self, mikrotik_controller, inst, sensor):
|
2019-12-03 03:09:30 +01:00
|
|
|
"""Initialize."""
|
2019-12-06 01:22:34 +01:00
|
|
|
self._inst = inst
|
|
|
|
self._sensor = sensor
|
|
|
|
self._ctrl = mikrotik_controller
|
|
|
|
self._data = mikrotik_controller.data[SENSOR_TYPES[sensor][ATTR_PATH]]
|
|
|
|
self._type = SENSOR_TYPES[sensor]
|
|
|
|
self._attr = SENSOR_TYPES[sensor][ATTR_ATTR]
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-03 03:09:30 +01:00
|
|
|
self._device_class = None
|
|
|
|
self._state = None
|
|
|
|
self._icon = None
|
|
|
|
self._unit_of_measurement = None
|
|
|
|
self._attrs = {ATTR_ATTRIBUTION: ATTRIBUTION}
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-03 03:09:30 +01:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name."""
|
2020-03-16 19:02:54 +01:00
|
|
|
return f"{self._inst} {self._type[ATTR_LABEL]}"
|
2019-12-03 03:09:30 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state."""
|
|
|
|
val = "unknown"
|
2019-12-06 01:22:34 +01:00
|
|
|
if self._attr in self._data:
|
|
|
|
val = self._data[self._attr]
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-03 03:09:30 +01:00
|
|
|
return val
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
|
|
|
return self._attrs
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon."""
|
2019-12-06 01:22:34 +01:00
|
|
|
self._icon = self._type[ATTR_ICON]
|
2019-12-03 03:09:30 +01:00
|
|
|
return self._icon
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the device_class."""
|
2019-12-06 01:22:34 +01:00
|
|
|
return self._type[ATTR_DEVICE_CLASS]
|
2019-12-03 03:09:30 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique_id for this entity."""
|
2020-03-16 19:02:54 +01:00
|
|
|
return f"{self._inst.lower()}-{self._sensor.lower()}"
|
2019-12-03 03:09:30 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit the value is expressed in."""
|
2020-03-16 01:32:13 +01:00
|
|
|
if ATTR_UNIT_ATTR in self._type:
|
|
|
|
return self._data[SENSOR_TYPES[self._sensor][ATTR_UNIT_ATTR]]
|
|
|
|
|
|
|
|
if ATTR_UNIT in self._type:
|
|
|
|
return self._type[ATTR_UNIT]
|
2019-12-03 03:09:30 +01:00
|
|
|
|
|
|
|
@property
|
2019-12-08 22:42:54 +01:00
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return if controller is available."""
|
|
|
|
return self._ctrl.connected()
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-03 03:09:30 +01:00
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Return a port description for device registry."""
|
|
|
|
info = {
|
2020-04-10 09:22:28 +02:00
|
|
|
"manufacturer": self._ctrl.data["resource"]["platform"],
|
|
|
|
"model": self._ctrl.data["resource"]["board-name"],
|
|
|
|
"name": f"{self._inst} {self._type[ATTR_GROUP]}",
|
|
|
|
}
|
|
|
|
if ATTR_GROUP in self._type:
|
|
|
|
info["identifiers"] = {
|
2020-03-16 04:51:41 +01:00
|
|
|
(
|
|
|
|
DOMAIN,
|
|
|
|
"serial-number",
|
|
|
|
self._ctrl.data["routerboard"]["serial-number"],
|
|
|
|
"switch",
|
2020-04-10 09:22:28 +02:00
|
|
|
self._type[ATTR_GROUP],
|
2020-03-16 04:51:41 +01:00
|
|
|
)
|
2020-04-10 09:22:28 +02:00
|
|
|
}
|
|
|
|
|
2019-12-03 03:09:30 +01:00
|
|
|
return info
|
|
|
|
|
|
|
|
async def async_update(self):
|
|
|
|
"""Synchronize state with controller."""
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-03 03:09:30 +01:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Port entity created."""
|
2019-12-06 01:22:34 +01:00
|
|
|
_LOGGER.debug("New sensor %s (%s)", self._inst, self._sensor)
|
2019-12-08 11:20:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------
|
|
|
|
# MikrotikControllerTrafficSensor
|
|
|
|
# ---------------------------
|
|
|
|
class MikrotikControllerTrafficSensor(MikrotikControllerSensor):
|
|
|
|
"""Define an Mikrotik Controller sensor."""
|
|
|
|
|
|
|
|
def __init__(self, mikrotik_controller, inst, sensor, uid):
|
|
|
|
"""Initialize."""
|
|
|
|
super().__init__(mikrotik_controller, inst, sensor)
|
|
|
|
self._uid = uid
|
2020-04-11 05:45:36 +02:00
|
|
|
self._data = mikrotik_controller.data[SENSOR_TYPES[sensor][ATTR_PATH]][uid]
|
2019-12-08 11:20:02 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name."""
|
2020-03-16 19:02:54 +01:00
|
|
|
return f"{self._inst} {self._data['name']} {self._type[ATTR_LABEL]}"
|
2019-12-08 11:20:02 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique_id for this entity."""
|
2020-03-16 19:02:54 +01:00
|
|
|
return f"{self._inst.lower()}-{self._sensor.lower()}-{self._data['default-name'].lower()}"
|
2019-12-08 11:20:02 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Return a port description for device registry."""
|
|
|
|
info = {
|
2020-04-11 05:45:36 +02:00
|
|
|
"connections": {(CONNECTION_NETWORK_MAC, self._data["port-mac-address"])},
|
2020-03-16 04:51:41 +01:00
|
|
|
"manufacturer": self._ctrl.data["resource"]["platform"],
|
|
|
|
"model": self._ctrl.data["resource"]["board-name"],
|
2020-04-10 09:22:28 +02:00
|
|
|
"name": f"{self._inst} {self._data['default-name']}",
|
2019-12-08 11:20:02 +01:00
|
|
|
}
|
2020-04-10 09:22:28 +02:00
|
|
|
|
2019-12-08 11:20:02 +01:00
|
|
|
return info
|
2019-12-10 22:29:35 +01:00
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Port entity created."""
|
2020-03-16 04:51:41 +01:00
|
|
|
_LOGGER.debug(
|
|
|
|
"New sensor %s (%s %s)",
|
|
|
|
self._inst,
|
|
|
|
self._data["default-name"],
|
|
|
|
self._sensor,
|
|
|
|
)
|
2020-04-04 19:42:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------
|
|
|
|
# MikrotikAccountingSensor
|
|
|
|
# ---------------------------
|
|
|
|
class MikrotikAccountingSensor(MikrotikControllerSensor):
|
|
|
|
"""Define an Mikrotik Accounting sensor."""
|
|
|
|
|
|
|
|
def __init__(self, mikrotik_controller, inst, sensor, uid):
|
|
|
|
"""Initialize."""
|
|
|
|
super().__init__(mikrotik_controller, inst, sensor)
|
|
|
|
self._uid = uid
|
|
|
|
self._data = mikrotik_controller.data[SENSOR_TYPES[sensor][ATTR_PATH]][uid]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name."""
|
2020-04-20 08:00:00 +02:00
|
|
|
return f"{self._data['host-name']} {self._type[ATTR_LABEL]}"
|
2020-04-04 19:42:05 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique_id for this entity."""
|
2020-04-07 13:33:48 +02:00
|
|
|
return f"{self._inst.lower()}-{self._sensor.lower()}-{self._data['mac-address'].lower()}"
|
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
|
|
|
"""
|
2020-04-11 05:45:36 +02:00
|
|
|
if self._attr in ["lan-tx", "lan-rx"]:
|
|
|
|
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"]
|
2020-04-08 13:41:03 +02:00
|
|
|
|
2020-04-04 19:42:05 +02:00
|
|
|
@property
|
|
|
|
def device_info(self):
|
2020-04-07 14:19:22 +02:00
|
|
|
"""Return a accounting description for device registry."""
|
2020-04-04 19:42:05 +02:00
|
|
|
info = {
|
2020-04-11 05:45:36 +02:00
|
|
|
"connections": {(CONNECTION_NETWORK_MAC, self._data["mac-address"])},
|
2020-12-02 14:57:30 +01:00
|
|
|
"default_name": self._data["host-name"],
|
2020-04-04 19:42:05 +02:00
|
|
|
}
|
2020-12-03 16:41:06 +01:00
|
|
|
if self._data["manufacturer"] != "":
|
|
|
|
info["manufacturer"] = self._data["manufacturer"]
|
|
|
|
|
2020-04-04 19:42:05 +02:00
|
|
|
return info
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
|
|
|
attributes = self._attrs
|
|
|
|
for variable in DEVICE_ATTRIBUTES_ACCOUNTING:
|
|
|
|
if variable in self._data:
|
|
|
|
attributes[format_attribute(variable)] = self._data[variable]
|
|
|
|
|
|
|
|
return attributes
|
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Port entity created."""
|
|
|
|
_LOGGER.debug(
|
2020-04-07 20:31:16 +02:00
|
|
|
"New sensor %s (%s [%s] %s)",
|
2020-04-04 19:42:05 +02:00
|
|
|
self._inst,
|
2020-04-07 20:31:16 +02:00
|
|
|
self._data["host-name"],
|
|
|
|
self._data["mac-address"],
|
2020-04-04 19:42:05 +02:00
|
|
|
self._sensor,
|
|
|
|
)
|
2020-12-02 13:56:50 +01:00
|
|
|
|
2020-12-02 15:38:17 +01:00
|
|
|
|
2020-12-02 13:56:50 +01:00
|
|
|
# ---------------------------
|
|
|
|
# MikrotikControllerEnvironmentSensor
|
|
|
|
# ---------------------------
|
|
|
|
class MikrotikControllerEnvironmentSensor(MikrotikControllerSensor):
|
|
|
|
"""Define an Mikrotik Controller sensor."""
|
|
|
|
|
|
|
|
def __init__(self, mikrotik_controller, inst, sensor, uid):
|
|
|
|
"""Initialize."""
|
|
|
|
super().__init__(mikrotik_controller, inst, sensor)
|
|
|
|
self._uid = uid
|
2020-12-02 15:38:17 +01:00
|
|
|
self._data = mikrotik_controller.data["environment"][uid]
|
2020-12-02 13:56:50 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique_id for this entity."""
|
|
|
|
return f"{self._inst.lower()}-environment-{self._uid}"
|
|
|
|
|
2020-12-13 03:43:41 +01:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name."""
|
|
|
|
return f"{self._inst} {self._data['name']}"
|
|
|
|
|
2020-12-02 13:56:50 +01:00
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state."""
|
|
|
|
val = "unknown"
|
|
|
|
if self._attr in self._data:
|
|
|
|
val = self._data[self._attr]
|
|
|
|
|
|
|
|
return val
|
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Port entity created."""
|
|
|
|
_LOGGER.debug(
|
|
|
|
"New sensor %s (%s %s)",
|
|
|
|
self._inst,
|
2020-12-02 15:38:17 +01:00
|
|
|
self._data["name"],
|
2020-12-02 13:56:50 +01:00
|
|
|
self._sensor,
|
2020-12-02 15:38:17 +01:00
|
|
|
)
|