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