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

136 lines
4.5 KiB
Python
Raw Normal View History

2023-08-08 00:50:09 +02:00
"""Mikrotik sensor platform."""
from __future__ import annotations
2019-12-03 03:09:30 +01:00
2023-08-08 00:50:09 +02:00
from logging import getLogger
2022-02-01 14:06:33 +01:00
from collections.abc import Mapping
2023-08-08 00:50:09 +02:00
from datetime import date, datetime
from decimal import Decimal
from typing import Any
2022-02-01 10:12:38 +01:00
from homeassistant.components.sensor import SensorEntity
2023-08-08 00:50:09 +02:00
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.typing import StateType
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .coordinator import MikrotikCoordinator
2023-08-09 09:53:48 +02:00
from .entity import MikrotikEntity, async_add_entities
2022-02-02 22:13:39 +01:00
from .helper import format_attribute
2022-02-01 10:12:38 +01:00
from .sensor_types import (
SENSOR_TYPES,
SENSOR_SERVICES,
DEVICE_ATTRIBUTES_IFACE_ETHER,
DEVICE_ATTRIBUTES_IFACE_SFP,
DEVICE_ATTRIBUTES_IFACE_WIRELESS,
2022-02-01 10:12:38 +01:00
)
2019-12-03 03:09:30 +01:00
2023-08-08 00:50:09 +02:00
_LOGGER = getLogger(__name__)
2019-12-03 03:09:30 +01:00
2019-12-03 03:09:30 +01:00
# ---------------------------
# async_setup_entry
# ---------------------------
2023-08-08 00:50:09 +02:00
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
_async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up entry for component"""
dispatcher = {
"MikrotikSensor": MikrotikSensor,
"MikrotikInterfaceTrafficSensor": MikrotikInterfaceTrafficSensor,
2023-09-18 08:48:45 +02:00
"MikrotikClientTrafficSensor": MikrotikClientTrafficSensor,
}
2023-08-08 00:50:09 +02:00
await async_add_entities(hass, config_entry, dispatcher)
2019-12-03 03:09:30 +01:00
# ---------------------------
# MikrotikSensor
2019-12-03 03:09:30 +01:00
# ---------------------------
class MikrotikSensor(MikrotikEntity, SensorEntity):
2023-08-08 00:50:09 +02:00
"""Define an Mikrotik sensor."""
def __init__(
self,
coordinator: MikrotikCoordinator,
entity_description,
uid: str | None = None,
):
super().__init__(coordinator, entity_description, uid)
self._attr_suggested_unit_of_measurement = (
self.entity_description.suggested_unit_of_measurement
)
2019-12-03 03:09:30 +01:00
@property
2023-08-08 00:50:09 +02:00
def native_value(self) -> StateType | date | datetime | Decimal:
"""Return the value reported by the sensor."""
return self._data[self.entity_description.data_attribute]
2019-12-03 03:09:30 +01:00
2022-02-01 14:06:33 +01:00
@property
2023-08-08 00:50:09 +02:00
def native_unit_of_measurement(self) -> str | None:
2022-02-01 14:06:33 +01:00
"""Return the unit the value is expressed in."""
if self.entity_description.native_unit_of_measurement:
if self.entity_description.native_unit_of_measurement.startswith("data__"):
uom = self.entity_description.native_unit_of_measurement[6:]
if uom in self._data:
2023-08-08 00:50:09 +02:00
return self._data[uom]
2022-02-01 14:06:33 +01:00
return self.entity_description.native_unit_of_measurement
return None
# ---------------------------
# MikrotikInterfaceTrafficSensor
# ---------------------------
class MikrotikInterfaceTrafficSensor(MikrotikSensor):
"""Define an Mikrotik MikrotikInterfaceTrafficSensor sensor."""
@property
def extra_state_attributes(self) -> Mapping[str, Any]:
"""Return the state attributes."""
attributes = super().extra_state_attributes
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]
return attributes
2023-09-18 08:48:45 +02:00
# ---------------------------
# MikrotikClientTrafficSensor
# ---------------------------
class MikrotikClientTrafficSensor(MikrotikSensor):
"""Define an Mikrotik MikrotikClientTrafficSensor sensor."""
@property
def custom_name(self) -> str:
"""Return the name for this entity"""
return f"{self.entity_description.name}"
# @property
# def available(self) -> bool:
# """Return if controller and accounting feature in Mikrotik is available.
# Additional check for lan-tx/rx sensors
# """
# if self.entity_description.data_attribute in ["lan-tx", "lan-rx"]:
# return (
# self.coordinator.connected()
# and self._data["available"]
# and self._data["local_accounting"]
# )
# else:
# return self.coordinator.connected() and self._data["available"]