2019-12-03 03:09:30 +01:00
|
|
|
"""Support for the Mikrotik Router sensor service."""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
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
|
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_NAME,
|
|
|
|
ATTR_ATTRIBUTION,
|
|
|
|
ATTR_DEVICE_CLASS,
|
|
|
|
)
|
|
|
|
|
|
|
|
from .const import (
|
|
|
|
DOMAIN,
|
|
|
|
DATA_CLIENT,
|
|
|
|
ATTRIBUTION,
|
|
|
|
)
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
ATTR_ICON = "icon"
|
|
|
|
ATTR_LABEL = "label"
|
|
|
|
ATTR_UNIT = "unit"
|
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 = {
|
|
|
|
'system_cpu-load': {
|
|
|
|
ATTR_DEVICE_CLASS: None,
|
|
|
|
ATTR_ICON: "mdi:speedometer",
|
|
|
|
ATTR_LABEL: 'CPU load',
|
|
|
|
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
|
|
|
},
|
|
|
|
'system_memory-usage': {
|
|
|
|
ATTR_DEVICE_CLASS: None,
|
|
|
|
ATTR_ICON: "mdi:memory",
|
|
|
|
ATTR_LABEL: 'Memory usage',
|
|
|
|
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
|
|
|
},
|
|
|
|
'system_hdd-usage': {
|
|
|
|
ATTR_DEVICE_CLASS: None,
|
|
|
|
ATTR_ICON: "mdi:harddisk",
|
|
|
|
ATTR_LABEL: 'HDD usage',
|
|
|
|
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
|
|
|
},
|
2019-12-08 11:20:02 +01:00
|
|
|
'traffic_tx': {
|
|
|
|
ATTR_DEVICE_CLASS: None,
|
|
|
|
ATTR_ICON: "mdi:upload-network-outline",
|
|
|
|
ATTR_LABEL: 'TX bps',
|
|
|
|
ATTR_UNIT: "bps",
|
|
|
|
ATTR_PATH: "interface",
|
|
|
|
ATTR_ATTR: "tx-bits-per-second",
|
|
|
|
},
|
|
|
|
'traffic_rx': {
|
|
|
|
ATTR_DEVICE_CLASS: None,
|
|
|
|
ATTR_ICON: "mdi:download-network-outline",
|
|
|
|
ATTR_LABEL: 'RX bps',
|
|
|
|
ATTR_UNIT: "bps",
|
|
|
|
ATTR_PATH: "interface",
|
|
|
|
ATTR_ATTR: "rx-bits-per-second",
|
|
|
|
},
|
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."""
|
2019-12-06 01:22:34 +01:00
|
|
|
update_items(inst, 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(
|
|
|
|
async_dispatcher_connect(hass, mikrotik_controller.signal_update, update_controller)
|
|
|
|
)
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-03 03:09:30 +01:00
|
|
|
update_controller()
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------
|
|
|
|
# update_items
|
|
|
|
# ---------------------------
|
|
|
|
@callback
|
2019-12-06 01:22:34 +01:00
|
|
|
def update_items(inst, 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:
|
2019-12-08 11:20:02 +01:00
|
|
|
if "traffic_" in sensor:
|
|
|
|
continue
|
|
|
|
|
2019-12-06 01:22:34 +01:00
|
|
|
item_id = "{}-{}".format(inst, sensor)
|
2019-12-03 03:09:30 +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
|
|
|
|
2019-12-06 01:22:34 +01:00
|
|
|
sensors[item_id] = MikrotikControllerSensor(mikrotik_controller=mikrotik_controller, inst=inst, sensor=sensor)
|
2019-12-03 03:09:30 +01:00
|
|
|
new_sensors.append(sensors[item_id])
|
2019-12-05 20:45:04 +01:00
|
|
|
|
2019-12-08 11:20:02 +01:00
|
|
|
for sensor in SENSOR_TYPES:
|
|
|
|
if "traffic_" not in sensor:
|
|
|
|
continue
|
|
|
|
|
|
|
|
for uid in mikrotik_controller.data['interface']:
|
|
|
|
if mikrotik_controller.data['interface'][uid]['type'] == "ether":
|
|
|
|
item_id = "{}-{}-{}".format(inst, sensor, mikrotik_controller.data['interface'][uid]['default-name'])
|
|
|
|
if item_id in sensors:
|
|
|
|
if sensors[item_id].enabled:
|
|
|
|
sensors[item_id].async_schedule_update_ha_state()
|
|
|
|
continue
|
|
|
|
|
|
|
|
sensors[item_id] = MikrotikControllerTrafficSensor(mikrotik_controller=mikrotik_controller, inst=inst, sensor=sensor, 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
|
|
|
return
|
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."""
|
2019-12-06 01:22:34 +01:00
|
|
|
return "{} {}".format(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."""
|
2019-12-06 01:22:34 +01:00
|
|
|
return "{}-{}".format(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."""
|
2019-12-06 01:22:34 +01:00
|
|
|
return self._type[ATTR_UNIT]
|
2019-12-03 03:09:30 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Return True if entity is available."""
|
2019-12-06 01:22:34 +01:00
|
|
|
return bool(self._ctrl.data)
|
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 = {
|
2019-12-06 01:22:34 +01:00
|
|
|
"identifiers": {(DOMAIN, "serial-number", self._ctrl.data['routerboard']['serial-number'], "switch", "PORT")},
|
|
|
|
"manufacturer": self._ctrl.data['resource']['platform'],
|
|
|
|
"model": self._ctrl.data['resource']['board-name'],
|
|
|
|
"name": self._type[ATTR_GROUP],
|
2019-12-03 03:09:30 +01:00
|
|
|
}
|
|
|
|
return info
|
|
|
|
|
|
|
|
async def async_update(self):
|
|
|
|
"""Synchronize state with controller."""
|
|
|
|
return
|
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-03 03:09:30 +01:00
|
|
|
return
|
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
|
|
|
|
self._data = mikrotik_controller.data[SENSOR_TYPES[sensor][ATTR_PATH]][uid]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name."""
|
|
|
|
return "{} {} {}".format(self._inst, self._data['name'], self._type[ATTR_LABEL])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique_id for this entity."""
|
|
|
|
return "{}-{}-{}".format(self._inst.lower(), self._sensor.lower(), self._data['name'].lower())
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Return a port description for device registry."""
|
|
|
|
info = {
|
|
|
|
"connections": {(CONNECTION_NETWORK_MAC, self._data['port-mac-address'])},
|
|
|
|
"manufacturer": self._ctrl.data['resource']['platform'],
|
|
|
|
"model": self._ctrl.data['resource']['board-name'],
|
|
|
|
"name": self._data['default-name'],
|
|
|
|
}
|
|
|
|
return info
|