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

148 lines
4.7 KiB
Python
Raw Normal View History

2019-12-04 16:09:30 +01:00
"""Support for the Mikrotik Router binary sensor service."""
2023-08-09 09:33:52 +02:00
from __future__ import annotations
2019-12-04 16:09:30 +01:00
2023-08-09 09:33:52 +02:00
from logging import getLogger
2022-02-04 22:06:45 +01:00
from collections.abc import Mapping
2023-08-09 09:33:52 +02:00
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
2022-02-04 22:06:45 +01:00
from .binary_sensor_types import (
SENSOR_TYPES,
SENSOR_SERVICES,
DEVICE_ATTRIBUTES_IFACE_ETHER,
DEVICE_ATTRIBUTES_IFACE_SFP,
DEVICE_ATTRIBUTES_IFACE_WIRELESS,
2022-02-04 22:06:45 +01:00
)
2023-08-09 09:33:52 +02:00
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
2022-02-04 22:06:45 +01:00
2023-08-09 09:33:52 +02:00
_LOGGER = getLogger(__name__)
2019-12-04 16:09:30 +01:00
# ---------------------------
# async_setup_entry
# ---------------------------
2023-08-09 09:33:52 +02:00
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,
}
2023-08-09 09:33:52 +02:00
await async_add_entities(hass, config_entry, dispatcher)
2019-12-04 16:09:30 +01:00
# ---------------------------
# MikrotikBinarySensor
2019-12-04 16:09:30 +01:00
# ---------------------------
class MikrotikBinarySensor(MikrotikEntity, BinarySensorEntity):
2019-12-04 16:09:30 +01:00
"""Define an Mikrotik Controller Binary Sensor."""
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_attribute]
2021-12-13 13:45:07 +01:00
@property
def icon(self) -> str:
"""Return the icon."""
if self.entity_description.icon_enabled:
if self._data[self.entity_description.data_attribute]:
return self.entity_description.icon_enabled
else:
return self.entity_description.icon_disabled
# ---------------------------
# MikrotikPPPSecretBinarySensor
# ---------------------------
class MikrotikPPPSecretBinarySensor(MikrotikBinarySensor):
"""Representation of a network device."""
@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 is_on(self) -> bool:
"""Return true if device is on."""
return (
self._data[self.entity_description.data_attribute]
if self.option_sensor_ppp
else False
)
2023-08-09 09:33:52 +02:00
# @property
# def available(self) -> bool:
# """Return if controller is available."""
# return self._ctrl.connected() if self.option_sensor_ppp else False
2020-12-25 18:24:47 +01:00
# ---------------------------
# MikrotikPortBinarySensor
2020-12-25 18:24:47 +01:00
# ---------------------------
class MikrotikPortBinarySensor(MikrotikBinarySensor):
2020-12-25 18:24:47 +01:00
"""Representation of a network port."""
@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
)
2023-08-09 09:33:52 +02:00
# @property
# def available(self) -> bool:
# """Return if controller is available."""
# return self._ctrl.connected() if self.option_sensor_port_tracker else False
2020-12-25 18:24:47 +01:00
@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[self.entity_description.data_attribute]:
2022-02-04 22:46:18 +01:00
icon = self.entity_description.icon_enabled
2020-12-25 18:24:47 +01:00
else:
2022-02-04 22:46:18 +01:00
icon = self.entity_description.icon_disabled
2020-12-25 18:24:47 +01:00
if not self._data["enabled"]:
icon = "mdi:lan-disconnect"
return icon
@property
2022-02-04 22:46:18 +01:00
def extra_state_attributes(self) -> Mapping[str, Any]:
2020-12-25 20:28:36 +01:00
"""Return the state attributes."""
2022-02-04 22:46:18 +01:00
attributes = super().extra_state_attributes
2020-12-25 18:24:47 +01:00
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]
elif self._data["type"] == "wlan":
for variable in DEVICE_ATTRIBUTES_IFACE_WIRELESS:
if variable in self._data:
attributes[format_attribute(variable)] = self._data[variable]
2020-12-25 18:24:47 +01:00
return attributes