tomaae.homeassistant-mikrot.../custom_components/mikrotik_router/binary_sensor.py

578 lines
18 KiB
Python
Raw Normal View History

2019-12-04 16:09:30 +01:00
"""Support for the Mikrotik Router binary sensor service."""
import logging
2020-12-25 20:28:36 +01:00
from typing import Any, Dict, Optional
2022-02-04 22:06:45 +01:00
from collections.abc import Mapping
from homeassistant.helpers.entity import DeviceInfo, EntityCategory
from homeassistant.components.binary_sensor import (
BinarySensorEntity,
BinarySensorDeviceClass,
)
2019-12-04 16:09:30 +01:00
from homeassistant.const import (
CONF_NAME,
CONF_HOST,
ATTR_DEVICE_CLASS,
2019-12-04 16:09:30 +01:00
ATTR_ATTRIBUTION,
)
2020-03-21 19:02:28 +03:00
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
2020-12-25 18:24:47 +01:00
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
2022-02-02 22:13:39 +01:00
from .helper import format_attribute
2019-12-04 16:09:30 +01:00
from .const import (
DOMAIN,
DATA_CLIENT,
ATTRIBUTION,
CONF_SENSOR_PPP,
DEFAULT_SENSOR_PPP,
2020-12-25 18:24:47 +01:00
CONF_SENSOR_PORT_TRACKER,
DEFAULT_SENSOR_PORT_TRACKER,
2019-12-04 16:09:30 +01:00
)
2022-02-04 22:06:45 +01:00
from .binary_sensor_types import (
MikrotikBinarySensorEntityDescription,
SENSOR_TYPES,
)
2019-12-04 16:09:30 +01:00
_LOGGER = logging.getLogger(__name__)
2020-12-25 18:24:47 +01:00
DEVICE_ATTRIBUTES_IFACE = [
"running",
"enabled",
"comment",
"client-ip-address",
"client-mac-address",
"port-mac-address",
"last-link-down-time",
"last-link-up-time",
"link-downs",
"actual-mtu",
"type",
"name",
]
DEVICE_ATTRIBUTES_IFACE_ETHER = [
"running",
"enabled",
"comment",
"client-ip-address",
"client-mac-address",
"port-mac-address",
"last-link-down-time",
"last-link-up-time",
"link-downs",
"actual-mtu",
"type",
"name",
"status",
"auto-negotiation",
"rate",
"full-duplex",
2020-12-25 18:24:47 +01:00
"default-name",
"poe-out",
]
2021-04-12 14:28:39 +02:00
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 = [
"connected",
"service",
"profile",
"comment",
"caller-id",
"encoding",
]
2019-12-04 16: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-04 16:09:30 +01:00
mikrotik_controller = hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id]
sensors = {}
2019-12-04 16:09:30 +01:00
@callback
def update_controller():
"""Update the values of the controller."""
update_items(
inst, config_entry, mikrotik_controller, async_add_entities, sensors
)
2019-12-04 16: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-04 16:09:30 +01:00
)
update_controller()
# ---------------------------
# update_items
# ---------------------------
@callback
def update_items(inst, config_entry, mikrotik_controller, async_add_entities, sensors):
2019-12-04 16:09:30 +01:00
"""Update sensor state from the controller."""
new_sensors = []
2022-02-04 22:06:45 +01:00
# for sensor, sid_func in zip(
# # Sensor type name
# [
# "environment",
# ],
# # Entity function
# [
# MikrotikControllerSensor,
# ],
# ):
# if sensor.startswith("traffic_") and not config_entry.options.get(
# CONF_SENSOR_PORT_TRAFFIC, DEFAULT_SENSOR_PORT_TRAFFIC
# ):
# continue
#
# uid_sensor = SENSOR_TYPES[sensor]
# for uid in mikrotik_controller.data[SENSOR_TYPES[sensor].data_path]:
# uid_data = mikrotik_controller.data[SENSOR_TYPES[sensor].data_path]
# if (
# uid_sensor.data_path == "interface"
# and uid_data[uid]["type"] == "bridge"
# ):
# continue
#
# item_id = f"{inst}-{sensor}-{uid_data[uid][uid_sensor.data_reference]}"
# _LOGGER.debug("Updating binary 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] = sid_func(
# inst=inst,
# uid=uid,
# mikrotik_controller=mikrotik_controller,
# entity_description=uid_sensor,
# )
# new_sensors.append(sensors[item_id])
for sensor in SENSOR_TYPES:
if sensor.startswith("system_"):
uid_sensor = SENSOR_TYPES[sensor]
item_id = f"{inst}-{sensor}"
_LOGGER.debug("Updating binary sensor %s", item_id)
if item_id in sensors:
if sensors[item_id].enabled:
sensors[item_id].async_schedule_update_ha_state()
continue
2022-02-04 22:06:45 +01:00
sensors[item_id] = MikrotikControllerBinarySensor(
inst=inst,
uid="",
mikrotik_controller=mikrotik_controller,
entity_description=uid_sensor,
)
new_sensors.append(sensors[item_id])
2022-02-04 22:06:45 +01:00
#
# # Add switches
# for sid, sid_uid, sid_name, sid_ref, sid_attr, sid_func in zip(
# # Data point name
# ["ppp_secret", "interface"],
# # Data point unique id
# ["name", "default-name"],
# # Entry Name
# ["name", "name"],
# # Entry Unique id
# ["name", "port-mac-address"],
# # Attr
# [None, DEVICE_ATTRIBUTES_IFACE],
# # Tracker function
# [
# MikrotikControllerPPPSecretBinarySensor,
# MikrotikControllerPortBinarySensor,
# ],
# ):
# if (
# sid_func == MikrotikControllerPortBinarySensor
# and not config_entry.options.get(
# CONF_SENSOR_PORT_TRACKER, DEFAULT_SENSOR_PORT_TRACKER
# )
# ):
# continue
# for uid in mikrotik_controller.data[sid]:
# if (
# # Skip if interface is wlan
# sid == "interface"
# and mikrotik_controller.data[sid][uid]["type"] == "wlan"
# ):
# continue
# # Update entity
# item_id = f"{inst}-{sid}-{mikrotik_controller.data[sid][uid][sid_uid]}"
# _LOGGER.debug("Updating binary_sensor %s", item_id)
# if item_id in sensors:
# if sensors[item_id].enabled:
# sensors[item_id].async_schedule_update_ha_state()
# continue
#
# # Create new entity
# sid_data = {
# "sid": sid,
# "sid_uid": sid_uid,
# "sid_name": sid_name,
# "sid_ref": sid_ref,
# "sid_attr": sid_attr,
# }
# sensors[item_id] = sid_func(
# inst, uid, mikrotik_controller, config_entry, sid_data
# )
# new_sensors.append(sensors[item_id])
#
# for sensor in SENSOR_TYPES:
# item_id = f"{inst}-{sensor}"
# _LOGGER.debug("Updating binary_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] = MikrotikControllerBinarySensor(
# mikrotik_controller=mikrotik_controller, inst=inst, sid_data=sensor
# )
# new_sensors.append(sensors[item_id])
2019-12-04 16:09:30 +01:00
if new_sensors:
async_add_entities(new_sensors, True)
2019-12-04 16:09:30 +01:00
class MikrotikControllerBinarySensor(BinarySensorEntity):
2019-12-04 16:09:30 +01:00
"""Define an Mikrotik Controller Binary Sensor."""
2022-02-04 22:06:45 +01:00
def __init__(
self,
inst,
uid: "",
mikrotik_controller,
entity_description: MikrotikBinarySensorEntityDescription,
):
2019-12-04 16:09:30 +01:00
"""Initialize."""
2022-02-04 22:06:45 +01:00
self.entity_description = entity_description
2019-12-06 01:22:34 +01:00
self._inst = inst
self._ctrl = mikrotik_controller
2022-02-04 22:06:45 +01:00
self._attr_extra_state_attributes = {ATTR_ATTRIBUTION: ATTRIBUTION}
self._uid = uid
if self._uid:
self._data = mikrotik_controller.data[self.entity_description.data_path][
self._uid
]
2020-12-28 13:11:10 +01:00
else:
2022-02-04 22:06:45 +01:00
self._data = mikrotik_controller.data[self.entity_description.data_path]
2019-12-04 16:09:30 +01:00
@property
2020-12-25 20:28:36 +01:00
def name(self) -> str:
2019-12-04 16:09:30 +01:00
"""Return the name."""
2022-02-04 22:06:45 +01:00
if self._uid:
if self.entity_description.name:
return f"{self._inst} {self._data[self.entity_description.data_name]} {self.entity_description.name}"
2022-02-04 22:06:45 +01:00
return f"{self._inst} {self._data[self.entity_description.data_name]}"
else:
return f"{self._inst} {self.entity_description.name}"
2019-12-04 16:09:30 +01:00
@property
2020-12-25 20:28:36 +01:00
def unique_id(self) -> str:
"""Return a unique id for this entity."""
2022-02-04 22:06:45 +01:00
if self._uid:
return f"{self._inst.lower()}-{self.entity_description.key}-{self._data[self.entity_description.data_reference].lower()}"
else:
return f"{self._inst.lower()}-{self.entity_description.key}"
2019-12-04 16:09:30 +01:00
@property
def available(self) -> bool:
"""Return if controller is available."""
return self._ctrl.connected()
2021-12-13 13:45:07 +01:00
@property
2022-02-04 22:06:45 +01:00
def is_on(self) -> bool:
"""Return true if device is on."""
return self._data[self.entity_description.data_is_on]
2021-12-13 13:45:07 +01:00
2019-12-04 16:09:30 +01:00
@property
2022-02-04 22:06:45 +01:00
def device_info(self) -> DeviceInfo:
2020-12-25 20:28:36 +01:00
"""Return a description for device registry."""
2022-02-04 22:06:45 +01:00
dev_connection = DOMAIN
dev_connection_value = self.entity_description.data_reference
dev_group = self.entity_description.ha_group
if self.entity_description.ha_group == "System":
dev_group = self._ctrl.data["resource"]["board-name"]
dev_connection_value = self._ctrl.data["routerboard"]["serial-number"]
if self.entity_description.ha_group.startswith("data__"):
dev_group = self.entity_description.ha_group[6:]
if dev_group in self._data:
dev_group = self._data[dev_group]
dev_connection_value = dev_group
if self.entity_description.ha_connection:
dev_connection = self.entity_description.ha_connection
if self.entity_description.ha_connection_value:
dev_connection_value = self.entity_description.ha_connection_value
if dev_connection_value.startswith("data__"):
dev_connection_value = dev_connection_value[6:]
dev_connection_value = self._data[dev_connection_value]
info = DeviceInfo(
connections={(dev_connection, f"{dev_connection_value}")},
identifiers={(dev_connection, f"{dev_connection_value}")},
default_name=f"{self._inst} {dev_group}",
model=f"{self._ctrl.data['resource']['board-name']}",
manufacturer=f"{self._ctrl.data['resource']['platform']}",
sw_version=f"{self._ctrl.data['resource']['version']}",
configuration_url=f"http://{self._ctrl.config_entry.data[CONF_HOST]}",
via_device=(DOMAIN, f"{self._ctrl.data['routerboard']['serial-number']}"),
)
if "mac-address" in self.entity_description.data_reference:
dev_group = self._data[self.entity_description.data_name]
dev_manufacturer = ""
if dev_connection_value in self._ctrl.data["host"]:
dev_group = self._ctrl.data["host"][dev_connection_value]["host-name"]
dev_manufacturer = self._ctrl.data["host"][dev_connection_value][
"manufacturer"
]
info = DeviceInfo(
connections={(dev_connection, f"{dev_connection_value}")},
default_name=f"{dev_group}",
manufacturer=f"{dev_manufacturer}",
via_device=(
2020-03-16 04:51:41 +01:00
DOMAIN,
2022-01-21 15:50:25 +01:00
f"{self._ctrl.data['routerboard']['serial-number']}",
2022-02-04 22:06:45 +01:00
),
)
2019-12-04 16:09:30 +01:00
return info
@property
2022-02-04 22:06:45 +01:00
def extra_state_attributes(self) -> Mapping[str, Any]:
"""Return the state attributes."""
attributes = super().extra_state_attributes
for variable in self.entity_description.data_attributes_list:
if variable in self._data:
attributes[format_attribute(variable)] = self._data[variable]
2022-02-04 22:06:45 +01:00
return attributes
2020-12-25 20:28:36 +01:00
async def async_added_to_hass(self):
"""Run when entity about to be added to hass."""
2022-02-04 22:06:45 +01:00
_LOGGER.debug("New binary sensor %s (%s)", self._inst, self.unique_id)
2020-12-25 20:28:36 +01:00
# ---------------------------
# MikrotikControllerPPPSecretBinarySensor
# ---------------------------
class MikrotikControllerPPPSecretBinarySensor(MikrotikControllerBinarySensor):
"""Representation of a network device."""
def __init__(self, inst, uid, mikrotik_controller, config_entry, sid_data):
2020-12-25 20:28:36 +01:00
"""Initialize."""
super().__init__(mikrotik_controller, inst, uid)
self._sid_data = sid_data
self._data = mikrotik_controller.data[self._sid_data["sid"]][uid]
self._config_entry = config_entry
@property
2020-12-25 20:28:36 +01:00
def option_sensor_ppp(self) -> bool:
"""Config entry option."""
return self._config_entry.options.get(CONF_SENSOR_PPP, DEFAULT_SENSOR_PPP)
@property
2020-12-25 20:28:36 +01:00
def name(self) -> str:
"""Return the name."""
return f"{self._inst} PPP {self._data['name']}"
@property
2020-12-25 20:28:36 +01:00
def is_on(self) -> bool:
"""Return true if device is on."""
if not self.option_sensor_ppp:
return False
return self._data["connected"]
@property
2020-12-25 20:28:36 +01:00
def device_class(self) -> Optional[str]:
"""Return the device class."""
return BinarySensorDeviceClass.CONNECTIVITY
@property
def available(self) -> bool:
"""Return if controller is available."""
if not self.option_sensor_ppp:
return False
return self._ctrl.connected()
@property
2020-12-25 20:28:36 +01:00
def unique_id(self) -> str:
"""Return a unique id for this entity."""
return f"{self._inst.lower()}-{self._sid_data['sid']}_tracker-{self._data[self._sid_data['sid_ref']]}"
@property
2020-12-25 20:28:36 +01:00
def icon(self) -> str:
"""Return the icon."""
if self._data["connected"]:
return "mdi:account-network-outline"
else:
return "mdi:account-off-outline"
@property
def extra_state_attributes(self) -> Dict[str, Any]:
2020-12-25 20:28:36 +01:00
"""Return the state attributes."""
attributes = self._attrs
for variable in DEVICE_ATTRIBUTES_PPP_SECRET:
if variable in self._data:
attributes[format_attribute(variable)] = self._data[variable]
return attributes
@property
2020-12-25 20:28:36 +01:00
def device_info(self) -> Dict[str, Any]:
"""Return a description for device registry."""
info = {
"identifiers": {
(
DOMAIN,
"serial-number",
2022-01-21 15:50:25 +01:00
f"{self._ctrl.data['routerboard']['serial-number']}",
"switch",
"PPP",
)
},
"manufacturer": self._ctrl.data["resource"]["platform"],
"model": self._ctrl.data["resource"]["board-name"],
"name": f"{self._inst} PPP",
}
return info
2020-12-25 18:24:47 +01:00
# ---------------------------
# MikrotikControllerPortBinarySensor
# ---------------------------
class MikrotikControllerPortBinarySensor(MikrotikControllerBinarySensor):
"""Representation of a network port."""
def __init__(self, inst, uid, mikrotik_controller, config_entry, sid_data):
2020-12-25 20:28:36 +01:00
"""Initialize."""
2020-12-25 18:24:47 +01:00
super().__init__(mikrotik_controller, inst, uid)
self._sid_data = sid_data
self._data = mikrotik_controller.data[self._sid_data["sid"]][uid]
self._config_entry = config_entry
@property
2020-12-25 20:28:36 +01:00
def option_sensor_port_tracker(self) -> bool:
2020-12-25 18:24:47 +01:00
"""Config entry option to not track ARP."""
return self._config_entry.options.get(
CONF_SENSOR_PORT_TRACKER, DEFAULT_SENSOR_PORT_TRACKER
)
@property
2020-12-25 20:28:36 +01:00
def name(self) -> str:
"""Return the name."""
2020-12-25 18:24:47 +01:00
return f"{self._inst} {self._data[self._sid_data['sid_name']]}"
@property
2020-12-25 20:28:36 +01:00
def unique_id(self) -> str:
"""Return a unique id for this entity."""
2020-12-25 23:31:51 +01:00
return (
f"{self._inst.lower()}-{self._sid_data['sid']}-"
f"{self._data['port-mac-address']}_{self._data['default-name']}"
)
2020-12-25 18:24:47 +01:00
@property
2020-12-25 20:28:36 +01:00
def is_on(self) -> bool:
"""Return true if device is on."""
2020-12-25 18:24:47 +01:00
return self._data["running"]
@property
2020-12-25 20:28:36 +01:00
def device_class(self) -> Optional[str]:
2020-12-25 18:24:47 +01:00
"""Return the device class."""
return BinarySensorDeviceClass.CONNECTIVITY
2020-12-25 18:24:47 +01:00
@property
def available(self) -> bool:
"""Return if controller is available."""
if not self.option_sensor_port_tracker:
return False
return self._ctrl.connected()
@property
2020-12-25 20:28:36 +01:00
def icon(self) -> str:
2020-12-25 18:24:47 +01:00
"""Return the icon."""
if self._data["running"]:
icon = "mdi:lan-connect"
else:
icon = "mdi:lan-pending"
if not self._data["enabled"]:
icon = "mdi:lan-disconnect"
return icon
@property
def extra_state_attributes(self) -> Dict[str, Any]:
2020-12-25 20:28:36 +01:00
"""Return the state attributes."""
2020-12-25 18:24:47 +01:00
attributes = self._attrs
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]
else:
for variable in self._sid_data["sid_attr"]:
2021-04-12 14:28:39 +02:00
if variable in self._data:
attributes[format_attribute(variable)] = self._data[variable]
2020-12-25 18:24:47 +01:00
return attributes
@property
2020-12-25 20:28:36 +01:00
def device_info(self) -> Dict[str, Any]:
2020-12-25 18:24:47 +01:00
"""Return a description for device registry."""
info = {
"connections": {
(CONNECTION_NETWORK_MAC, self._data[self._sid_data["sid_ref"]])
},
"manufacturer": self._ctrl.data["resource"]["platform"],
"model": self._ctrl.data["resource"]["board-name"],
"name": f"{self._inst} {self._data[self._sid_data['sid_name']]}",
}
return info