converted binary sensors

This commit is contained in:
Tomaae 2023-08-09 09:33:52 +02:00
parent cace2ae3e5
commit 2ea35dec75
No known key found for this signature in database
GPG key ID: 2F97770867DAA4E6
3 changed files with 140 additions and 137 deletions

View file

@ -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:

View file

@ -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 = {}

View file

@ -3,7 +3,7 @@ from homeassistant.const import Platform
PLATFORMS = [
Platform.SENSOR,
# Platform.BINARY_SENSOR,
Platform.BINARY_SENSOR,
# Platform.DEVICE_TRACKER,
# Platform.SWITCH,
# Platform.BUTTON,