mirror of
https://github.com/tomaae/homeassistant-mikrotik_router.git
synced 2025-07-14 03:14:30 +02:00
converted binary sensors
This commit is contained in:
parent
cace2ae3e5
commit
2ea35dec75
3 changed files with 140 additions and 137 deletions
|
@ -1,19 +1,15 @@
|
||||||
"""Support for the Mikrotik Router binary sensor service."""
|
"""Support for the Mikrotik Router binary sensor service."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
from logging import getLogger
|
||||||
from typing import Any
|
|
||||||
from collections.abc import Mapping
|
from collections.abc import Mapping
|
||||||
from homeassistant.components.binary_sensor import (
|
from typing import Any
|
||||||
BinarySensorEntity,
|
|
||||||
)
|
from homeassistant.components.binary_sensor import BinarySensorEntity
|
||||||
from .helper import format_attribute
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from .const import (
|
from homeassistant.core import HomeAssistant
|
||||||
CONF_SENSOR_PPP,
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
DEFAULT_SENSOR_PPP,
|
|
||||||
CONF_SENSOR_PORT_TRACKER,
|
|
||||||
DEFAULT_SENSOR_PORT_TRACKER,
|
|
||||||
)
|
|
||||||
from .entity import model_async_setup_entry, MikrotikEntity
|
|
||||||
from .binary_sensor_types import (
|
from .binary_sensor_types import (
|
||||||
SENSOR_TYPES,
|
SENSOR_TYPES,
|
||||||
SENSOR_SERVICES,
|
SENSOR_SERVICES,
|
||||||
|
@ -21,28 +17,33 @@ from .binary_sensor_types import (
|
||||||
DEVICE_ATTRIBUTES_IFACE_SFP,
|
DEVICE_ATTRIBUTES_IFACE_SFP,
|
||||||
DEVICE_ATTRIBUTES_IFACE_WIRELESS,
|
DEVICE_ATTRIBUTES_IFACE_WIRELESS,
|
||||||
)
|
)
|
||||||
|
from .const import (
|
||||||
|
CONF_SENSOR_PPP,
|
||||||
|
DEFAULT_SENSOR_PPP,
|
||||||
|
CONF_SENSOR_PORT_TRACKER,
|
||||||
|
DEFAULT_SENSOR_PORT_TRACKER,
|
||||||
|
)
|
||||||
|
from .entity import MikrotikEntity, async_add_entities
|
||||||
|
from .helper import format_attribute
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
# async_setup_entry
|
# async_setup_entry
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
config_entry: ConfigEntry,
|
||||||
|
_async_add_entities: AddEntitiesCallback,
|
||||||
|
) -> None:
|
||||||
"""Set up entry for component"""
|
"""Set up entry for component"""
|
||||||
dispatcher = {
|
dispatcher = {
|
||||||
"MikrotikBinarySensor": MikrotikBinarySensor,
|
"MikrotikBinarySensor": MikrotikBinarySensor,
|
||||||
"MikrotikPPPSecretBinarySensor": MikrotikPPPSecretBinarySensor,
|
"MikrotikPPPSecretBinarySensor": MikrotikPPPSecretBinarySensor,
|
||||||
"MikrotikPortBinarySensor": MikrotikPortBinarySensor,
|
"MikrotikPortBinarySensor": MikrotikPortBinarySensor,
|
||||||
}
|
}
|
||||||
await model_async_setup_entry(
|
await async_add_entities(hass, config_entry, dispatcher)
|
||||||
hass,
|
|
||||||
config_entry,
|
|
||||||
async_add_entities,
|
|
||||||
SENSOR_SERVICES,
|
|
||||||
SENSOR_TYPES,
|
|
||||||
dispatcher,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
|
@ -86,10 +87,10 @@ class MikrotikPPPSecretBinarySensor(MikrotikBinarySensor):
|
||||||
else False
|
else False
|
||||||
)
|
)
|
||||||
|
|
||||||
@property
|
# @property
|
||||||
def available(self) -> bool:
|
# def available(self) -> bool:
|
||||||
"""Return if controller is available."""
|
# """Return if controller is available."""
|
||||||
return self._ctrl.connected() if self.option_sensor_ppp else False
|
# return self._ctrl.connected() if self.option_sensor_ppp else False
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
|
@ -105,10 +106,10 @@ class MikrotikPortBinarySensor(MikrotikBinarySensor):
|
||||||
CONF_SENSOR_PORT_TRACKER, DEFAULT_SENSOR_PORT_TRACKER
|
CONF_SENSOR_PORT_TRACKER, DEFAULT_SENSOR_PORT_TRACKER
|
||||||
)
|
)
|
||||||
|
|
||||||
@property
|
# @property
|
||||||
def available(self) -> bool:
|
# def available(self) -> bool:
|
||||||
"""Return if controller is available."""
|
# """Return if controller is available."""
|
||||||
return self._ctrl.connected() if self.option_sensor_port_tracker else False
|
# return self._ctrl.connected() if self.option_sensor_port_tracker else False
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def icon(self) -> str:
|
def icon(self) -> str:
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
"""Definitions for Mikrotik Router binary sensor entities."""
|
"""Definitions for Mikrotik Router binary sensor entities."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
from typing import List
|
from typing import List
|
||||||
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
|
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
|
||||||
|
@ -111,23 +113,23 @@ DEVICE_ATTRIBUTES_UPS = [
|
||||||
class MikrotikBinarySensorEntityDescription(BinarySensorEntityDescription):
|
class MikrotikBinarySensorEntityDescription(BinarySensorEntityDescription):
|
||||||
"""Class describing mikrotik entities."""
|
"""Class describing mikrotik entities."""
|
||||||
|
|
||||||
icon_enabled: str = ""
|
icon_enabled: str | None = None
|
||||||
icon_disabled: str = ""
|
icon_disabled: str | None = None
|
||||||
ha_group: str = ""
|
ha_group: str | None = None
|
||||||
ha_connection: str = ""
|
ha_connection: str | None = None
|
||||||
ha_connection_value: str = ""
|
ha_connection_value: str | None = None
|
||||||
data_path: str = ""
|
data_path: str | None = None
|
||||||
data_attribute: str = "available"
|
data_attribute: str = "available"
|
||||||
data_name: str = ""
|
data_name: str | None = None
|
||||||
data_name_comment: bool = False
|
data_name_comment: bool = False
|
||||||
data_uid: str = ""
|
data_uid: str | None = None
|
||||||
data_reference: str = ""
|
data_reference: str | None = None
|
||||||
data_attributes_list: List = field(default_factory=lambda: [])
|
data_attributes_list: List = field(default_factory=lambda: [])
|
||||||
func: str = "MikrotikBinarySensor"
|
func: str = "MikrotikBinarySensor"
|
||||||
|
|
||||||
|
|
||||||
SENSOR_TYPES = {
|
SENSOR_TYPES: tuple[BinarySensorEntityDescription, ...] = (
|
||||||
"system_ups": MikrotikBinarySensorEntityDescription(
|
MikrotikBinarySensorEntityDescription(
|
||||||
key="system_ups",
|
key="system_ups",
|
||||||
name="UPS",
|
name="UPS",
|
||||||
icon_enabled="",
|
icon_enabled="",
|
||||||
|
@ -141,7 +143,7 @@ SENSOR_TYPES = {
|
||||||
data_reference="",
|
data_reference="",
|
||||||
data_attributes_list=DEVICE_ATTRIBUTES_UPS,
|
data_attributes_list=DEVICE_ATTRIBUTES_UPS,
|
||||||
),
|
),
|
||||||
"ppp_tracker": MikrotikBinarySensorEntityDescription(
|
MikrotikBinarySensorEntityDescription(
|
||||||
key="ppp_tracker",
|
key="ppp_tracker",
|
||||||
name="PPP",
|
name="PPP",
|
||||||
icon_enabled="mdi:account-network-outline",
|
icon_enabled="mdi:account-network-outline",
|
||||||
|
@ -158,7 +160,7 @@ SENSOR_TYPES = {
|
||||||
data_attributes_list=DEVICE_ATTRIBUTES_PPP_SECRET,
|
data_attributes_list=DEVICE_ATTRIBUTES_PPP_SECRET,
|
||||||
func="MikrotikPPPSecretBinarySensor",
|
func="MikrotikPPPSecretBinarySensor",
|
||||||
),
|
),
|
||||||
"interface": MikrotikBinarySensorEntityDescription(
|
MikrotikBinarySensorEntityDescription(
|
||||||
key="interface",
|
key="interface",
|
||||||
name="Connection",
|
name="Connection",
|
||||||
icon_enabled="mdi:lan-connect",
|
icon_enabled="mdi:lan-connect",
|
||||||
|
@ -175,6 +177,6 @@ SENSOR_TYPES = {
|
||||||
data_attributes_list=DEVICE_ATTRIBUTES_IFACE,
|
data_attributes_list=DEVICE_ATTRIBUTES_IFACE,
|
||||||
func="MikrotikPortBinarySensor",
|
func="MikrotikPortBinarySensor",
|
||||||
),
|
),
|
||||||
}
|
)
|
||||||
|
|
||||||
SENSOR_SERVICES = {}
|
SENSOR_SERVICES = {}
|
||||||
|
|
|
@ -1,92 +1,92 @@
|
||||||
"""Constants used by the Mikrotik Router component and platforms."""
|
"""Constants used by the Mikrotik Router component and platforms."""
|
||||||
from homeassistant.const import Platform
|
from homeassistant.const import Platform
|
||||||
|
|
||||||
PLATFORMS = [
|
PLATFORMS = [
|
||||||
Platform.SENSOR,
|
Platform.SENSOR,
|
||||||
# Platform.BINARY_SENSOR,
|
Platform.BINARY_SENSOR,
|
||||||
# Platform.DEVICE_TRACKER,
|
# Platform.DEVICE_TRACKER,
|
||||||
# Platform.SWITCH,
|
# Platform.SWITCH,
|
||||||
# Platform.BUTTON,
|
# Platform.BUTTON,
|
||||||
# Platform.UPDATE,
|
# Platform.UPDATE,
|
||||||
]
|
]
|
||||||
|
|
||||||
DOMAIN = "mikrotik_router"
|
DOMAIN = "mikrotik_router"
|
||||||
DEFAULT_NAME = "Mikrotik Router"
|
DEFAULT_NAME = "Mikrotik Router"
|
||||||
ATTRIBUTION = "Data provided by Mikrotik"
|
ATTRIBUTION = "Data provided by Mikrotik"
|
||||||
|
|
||||||
RUN_SCRIPT_COMMAND = "run_script"
|
RUN_SCRIPT_COMMAND = "run_script"
|
||||||
|
|
||||||
DEFAULT_ENCODING = "ISO-8859-1"
|
DEFAULT_ENCODING = "ISO-8859-1"
|
||||||
DEFAULT_LOGIN_METHOD = "plain"
|
DEFAULT_LOGIN_METHOD = "plain"
|
||||||
|
|
||||||
DEFAULT_HOST = "10.0.0.1"
|
DEFAULT_HOST = "10.0.0.1"
|
||||||
DEFAULT_USERNAME = "admin"
|
DEFAULT_USERNAME = "admin"
|
||||||
DEFAULT_PORT = 0
|
DEFAULT_PORT = 0
|
||||||
DEFAULT_DEVICE_NAME = "Mikrotik"
|
DEFAULT_DEVICE_NAME = "Mikrotik"
|
||||||
DEFAULT_SSL = False
|
DEFAULT_SSL = False
|
||||||
|
|
||||||
CONF_SCAN_INTERVAL = "scan_interval"
|
CONF_SCAN_INTERVAL = "scan_interval"
|
||||||
DEFAULT_SCAN_INTERVAL = 30
|
DEFAULT_SCAN_INTERVAL = 30
|
||||||
CONF_TRACK_IFACE_CLIENTS = "track_iface_clients"
|
CONF_TRACK_IFACE_CLIENTS = "track_iface_clients"
|
||||||
DEFAULT_TRACK_IFACE_CLIENTS = True
|
DEFAULT_TRACK_IFACE_CLIENTS = True
|
||||||
CONF_TRACK_HOSTS = "track_network_hosts"
|
CONF_TRACK_HOSTS = "track_network_hosts"
|
||||||
DEFAULT_TRACK_HOSTS = False
|
DEFAULT_TRACK_HOSTS = False
|
||||||
CONF_TRACK_HOSTS_TIMEOUT = "track_network_hosts_timeout"
|
CONF_TRACK_HOSTS_TIMEOUT = "track_network_hosts_timeout"
|
||||||
DEFAULT_TRACK_HOST_TIMEOUT = 180
|
DEFAULT_TRACK_HOST_TIMEOUT = 180
|
||||||
|
|
||||||
CONF_SENSOR_PORT_TRACKER = "sensor_port_tracker"
|
CONF_SENSOR_PORT_TRACKER = "sensor_port_tracker"
|
||||||
DEFAULT_SENSOR_PORT_TRACKER = False
|
DEFAULT_SENSOR_PORT_TRACKER = False
|
||||||
CONF_SENSOR_PORT_TRAFFIC = "sensor_port_traffic"
|
CONF_SENSOR_PORT_TRAFFIC = "sensor_port_traffic"
|
||||||
DEFAULT_SENSOR_PORT_TRAFFIC = False
|
DEFAULT_SENSOR_PORT_TRAFFIC = False
|
||||||
CONF_SENSOR_CLIENT_TRAFFIC = "sensor_client_traffic"
|
CONF_SENSOR_CLIENT_TRAFFIC = "sensor_client_traffic"
|
||||||
DEFAULT_SENSOR_CLIENT_TRAFFIC = False
|
DEFAULT_SENSOR_CLIENT_TRAFFIC = False
|
||||||
CONF_SENSOR_CLIENT_CAPTIVE = "sensor_client_captive"
|
CONF_SENSOR_CLIENT_CAPTIVE = "sensor_client_captive"
|
||||||
DEFAULT_SENSOR_CLIENT_CAPTIVE = False
|
DEFAULT_SENSOR_CLIENT_CAPTIVE = False
|
||||||
CONF_SENSOR_SIMPLE_QUEUES = "sensor_simple_queues"
|
CONF_SENSOR_SIMPLE_QUEUES = "sensor_simple_queues"
|
||||||
DEFAULT_SENSOR_SIMPLE_QUEUES = False
|
DEFAULT_SENSOR_SIMPLE_QUEUES = False
|
||||||
CONF_SENSOR_NAT = "sensor_nat"
|
CONF_SENSOR_NAT = "sensor_nat"
|
||||||
DEFAULT_SENSOR_NAT = False
|
DEFAULT_SENSOR_NAT = False
|
||||||
CONF_SENSOR_MANGLE = "sensor_mangle"
|
CONF_SENSOR_MANGLE = "sensor_mangle"
|
||||||
DEFAULT_SENSOR_MANGLE = False
|
DEFAULT_SENSOR_MANGLE = False
|
||||||
CONF_SENSOR_FILTER = "sensor_filter"
|
CONF_SENSOR_FILTER = "sensor_filter"
|
||||||
DEFAULT_SENSOR_FILTER = False
|
DEFAULT_SENSOR_FILTER = False
|
||||||
CONF_SENSOR_PPP = "sensor_ppp"
|
CONF_SENSOR_PPP = "sensor_ppp"
|
||||||
DEFAULT_SENSOR_PPP = False
|
DEFAULT_SENSOR_PPP = False
|
||||||
CONF_SENSOR_KIDCONTROL = "sensor_kidcontrol"
|
CONF_SENSOR_KIDCONTROL = "sensor_kidcontrol"
|
||||||
DEFAULT_SENSOR_KIDCONTROL = False
|
DEFAULT_SENSOR_KIDCONTROL = False
|
||||||
CONF_SENSOR_SCRIPTS = "sensor_scripts"
|
CONF_SENSOR_SCRIPTS = "sensor_scripts"
|
||||||
DEFAULT_SENSOR_SCRIPTS = False
|
DEFAULT_SENSOR_SCRIPTS = False
|
||||||
CONF_SENSOR_ENVIRONMENT = "sensor_environment"
|
CONF_SENSOR_ENVIRONMENT = "sensor_environment"
|
||||||
DEFAULT_SENSOR_ENVIRONMENT = False
|
DEFAULT_SENSOR_ENVIRONMENT = False
|
||||||
|
|
||||||
TO_REDACT = {
|
TO_REDACT = {
|
||||||
"ip-address",
|
"ip-address",
|
||||||
"client-ip-address",
|
"client-ip-address",
|
||||||
"address",
|
"address",
|
||||||
"active-address",
|
"active-address",
|
||||||
"mac-address",
|
"mac-address",
|
||||||
"active-mac-address",
|
"active-mac-address",
|
||||||
"orig-mac-address",
|
"orig-mac-address",
|
||||||
"port-mac-address",
|
"port-mac-address",
|
||||||
"client-mac-address",
|
"client-mac-address",
|
||||||
"client-id",
|
"client-id",
|
||||||
"active-client-id",
|
"active-client-id",
|
||||||
"eeprom",
|
"eeprom",
|
||||||
"sfp-vendor-serial",
|
"sfp-vendor-serial",
|
||||||
"gateway",
|
"gateway",
|
||||||
"dns-server",
|
"dns-server",
|
||||||
"wins-server",
|
"wins-server",
|
||||||
"ntp-server",
|
"ntp-server",
|
||||||
"caps-manager",
|
"caps-manager",
|
||||||
"serial-number",
|
"serial-number",
|
||||||
"source",
|
"source",
|
||||||
"from-addresses",
|
"from-addresses",
|
||||||
"to-addresses",
|
"to-addresses",
|
||||||
"src-address",
|
"src-address",
|
||||||
"dst-address",
|
"dst-address",
|
||||||
"username",
|
"username",
|
||||||
"password",
|
"password",
|
||||||
"caller-id",
|
"caller-id",
|
||||||
"target",
|
"target",
|
||||||
"ssid",
|
"ssid",
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue