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.""" """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:

View file

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

View file

@ -3,7 +3,7 @@ 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,