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

116 lines
3.7 KiB
Python
Raw Normal View History

2022-02-01 10:12:38 +01:00
"""Implementation of Mikrotik Router sensor entities."""
2019-12-03 03:09:30 +01:00
import logging
2022-02-01 14:07:21 +01:00
from typing import Any, Optional
2022-02-01 14:06:33 +01:00
from collections.abc import Mapping
2022-02-01 10:12:38 +01:00
from homeassistant.components.sensor import SensorEntity
2022-02-02 22:13:39 +01:00
from .helper import format_attribute
from .model import model_async_setup_entry, MikrotikEntity
2022-02-01 10:12:38 +01:00
from .sensor_types import (
SENSOR_TYPES,
SENSOR_SERVICES,
DEVICE_ATTRIBUTES_IFACE_ETHER,
DEVICE_ATTRIBUTES_IFACE_SFP,
2022-02-01 10:12:38 +01:00
)
2019-12-03 03:09:30 +01:00
_LOGGER = logging.getLogger(__name__)
2019-12-03 03:09:30 +01:00
# ---------------------------
# async_setup_entry
# ---------------------------
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up entry for component"""
dispatcher = {
"MikrotikSensor": MikrotikSensor,
"MikrotikInterfaceTrafficSensor": MikrotikInterfaceTrafficSensor,
"MikrotikClientTrafficSensor": MikrotikClientTrafficSensor,
}
await model_async_setup_entry(
hass,
config_entry,
async_add_entities,
SENSOR_SERVICES,
SENSOR_TYPES,
dispatcher,
2019-12-03 03:09:30 +01:00
)
2019-12-03 03:09:30 +01:00
# ---------------------------
# MikrotikSensor
2019-12-03 03:09:30 +01:00
# ---------------------------
class MikrotikSensor(MikrotikEntity, SensorEntity):
2019-12-03 03:09:30 +01:00
"""Define an Mikrotik Controller sensor."""
2019-12-03 03:09:30 +01:00
@property
2022-02-01 10:12:38 +01:00
def state(self) -> Optional[str]:
"""Return the state."""
if self.entity_description.data_attribute:
return self._data[self.entity_description.data_attribute]
else:
return "unknown"
2019-12-03 03:09:30 +01:00
2022-02-01 14:06:33 +01:00
@property
def native_unit_of_measurement(self):
"""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:
uom = self._data[uom]
return uom
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]
return attributes
# ---------------------------
# MikrotikClientTrafficSensor
# ---------------------------
class MikrotikClientTrafficSensor(MikrotikSensor):
"""Define an Mikrotik MikrotikClientTrafficSensor sensor."""
@property
2020-12-25 20:28:36 +01:00
def name(self) -> str:
"""Return the name."""
2022-08-18 09:29:55 +02:00
return f"{self.entity_description.name}"
@property
def available(self) -> bool:
"""Return if controller and accounting feature in Mikrotik is available.
2020-12-02 15:38:17 +01:00
Additional check for lan-tx/rx sensors
"""
2022-02-01 14:06:33 +01:00
if self.entity_description.data_attribute in ["lan-tx", "lan-rx"]:
2020-04-11 05:45:36 +02:00
return (
self._ctrl.connected()
and self._data["available"]
and self._data["local_accounting"]
)
else:
2020-04-11 05:45:36 +02:00
return self._ctrl.connected() and self._data["available"]