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

147 lines
4.5 KiB
Python
Raw Normal View History

2019-12-04 16:09:30 +01:00
"""Support for the Mikrotik Router binary sensor service."""
import logging
from typing import Any
2022-02-04 22:06:45 +01:00
from collections.abc import Mapping
from homeassistant.components.binary_sensor import (
BinarySensorEntity,
)
2022-02-02 22:13:39 +01:00
from .helper import format_attribute
2019-12-04 16:09:30 +01:00
from .const import (
CONF_SENSOR_PPP,
DEFAULT_SENSOR_PPP,
2020-12-25 18:24:47 +01:00
CONF_SENSOR_PORT_TRACKER,
DEFAULT_SENSOR_PORT_TRACKER,
2019-12-04 16:09:30 +01:00
)
from .model import model_async_setup_entry, MikrotikEntity
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
)
2019-12-04 16:09:30 +01:00
_LOGGER = logging.getLogger(__name__)
# ---------------------------
# async_setup_entry
# ---------------------------
async def async_setup_entry(hass, config_entry, async_add_entities):
"""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,
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
)
@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
)
@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