From a3e6761c78a09944e2d815f43be742920a4d9c79 Mon Sep 17 00:00:00 2001 From: Tomaae Date: Wed, 2 Feb 2022 22:13:39 +0100 Subject: [PATCH] renamed format_attribute to helper.py --- .../mikrotik_router/binary_sensor.py | 19 +----------- custom_components/mikrotik_router/button.py | 19 +----------- .../mikrotik_router/device_tracker.py | 18 +---------- custom_components/mikrotik_router/helper.py | 17 ++++++++++ custom_components/mikrotik_router/sensor.py | 31 ++++--------------- custom_components/mikrotik_router/switch.py | 18 +---------- 6 files changed, 27 insertions(+), 95 deletions(-) create mode 100644 custom_components/mikrotik_router/helper.py diff --git a/custom_components/mikrotik_router/binary_sensor.py b/custom_components/mikrotik_router/binary_sensor.py index 1cb4648..d575ba9 100644 --- a/custom_components/mikrotik_router/binary_sensor.py +++ b/custom_components/mikrotik_router/binary_sensor.py @@ -2,7 +2,6 @@ import logging from typing import Any, Dict, Optional - from homeassistant.helpers.entity import EntityCategory from homeassistant.components.binary_sensor import ( BinarySensorEntity, @@ -17,7 +16,7 @@ from homeassistant.const import ( from homeassistant.core import callback from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC - +from .helper import format_attribute from .const import ( DOMAIN, DATA_CLIENT, @@ -116,22 +115,6 @@ DEVICE_ATTRIBUTES_PPP_SECRET = [ ] -# --------------------------- -# format_attribute -# --------------------------- -def format_attribute(attr): - res = attr.replace("-", " ") - res = res.capitalize() - res = res.replace(" ip ", " IP ") - res = res.replace(" mac ", " MAC ") - res = res.replace(" mtu", " MTU") - res = res.replace("Sfp", "SFP") - res = res.replace("Poe", "POE") - res = res.replace(" tx", " TX") - res = res.replace(" rx", " RX") - return res - - # --------------------------- # format_value # --------------------------- diff --git a/custom_components/mikrotik_router/button.py b/custom_components/mikrotik_router/button.py index 914717b..30e5144 100644 --- a/custom_components/mikrotik_router/button.py +++ b/custom_components/mikrotik_router/button.py @@ -2,13 +2,12 @@ import logging from typing import Any, Dict - from homeassistant.components.button import ButtonEntity from homeassistant.const import CONF_NAME, ATTR_ATTRIBUTION from homeassistant.core import callback from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.restore_state import RestoreEntity - +from .helper import format_attribute from .const import DOMAIN, DATA_CLIENT, ATTRIBUTION _LOGGER = logging.getLogger(__name__) @@ -19,22 +18,6 @@ DEVICE_ATTRIBUTES_SCRIPT = [ ] -# --------------------------- -# format_attribute -# --------------------------- -def format_attribute(attr): - res = attr.replace("-", " ") - res = res.capitalize() - res = res.replace(" ip ", " IP ") - res = res.replace(" mac ", " MAC ") - res = res.replace(" mtu", " MTU") - res = res.replace("Sfp", "SFP") - res = res.replace("Poe", "POE") - res = res.replace(" tx", " TX") - res = res.replace(" rx", " RX") - return res - - # --------------------------- # async_setup_entry # --------------------------- diff --git a/custom_components/mikrotik_router/device_tracker.py b/custom_components/mikrotik_router/device_tracker.py index 5618432..508e6fc 100644 --- a/custom_components/mikrotik_router/device_tracker.py +++ b/custom_components/mikrotik_router/device_tracker.py @@ -15,7 +15,7 @@ from homeassistant.core import callback from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.util.dt import get_age, utcnow - +from .helper import format_attribute from .const import ( DOMAIN, DATA_CLIENT, @@ -38,22 +38,6 @@ DEVICE_ATTRIBUTES_HOST = [ ] -# --------------------------- -# format_attribute -# --------------------------- -def format_attribute(attr): - res = attr.replace("-", " ") - res = res.capitalize() - res = res.replace(" ip ", " IP ") - res = res.replace(" mac ", " MAC ") - res = res.replace(" mtu", " MTU") - res = res.replace("Sfp", "SFP") - res = res.replace("Poe", "POE") - res = res.replace(" tx", " TX") - res = res.replace(" rx", " RX") - return res - - # --------------------------- # format_value # --------------------------- diff --git a/custom_components/mikrotik_router/helper.py b/custom_components/mikrotik_router/helper.py new file mode 100644 index 0000000..507e254 --- /dev/null +++ b/custom_components/mikrotik_router/helper.py @@ -0,0 +1,17 @@ +"""Helper functions for Mikrotik Router.""" + + +# --------------------------- +# format_attribute +# --------------------------- +def format_attribute(attr): + res = attr.replace("-", " ") + res = res.capitalize() + res = res.replace(" ip ", " IP ") + res = res.replace(" mac ", " MAC ") + res = res.replace(" mtu", " MTU") + res = res.replace("Sfp", "SFP") + res = res.replace("Poe", "POE") + res = res.replace(" tx", " TX") + res = res.replace(" rx", " RX") + return res diff --git a/custom_components/mikrotik_router/sensor.py b/custom_components/mikrotik_router/sensor.py index d169c08..56a5146 100644 --- a/custom_components/mikrotik_router/sensor.py +++ b/custom_components/mikrotik_router/sensor.py @@ -1,28 +1,25 @@ """Implementation of Mikrotik Router sensor entities.""" import logging - from typing import Any, Optional from collections.abc import Mapping - from homeassistant.const import ( CONF_NAME, CONF_HOST, ATTR_ATTRIBUTION, ) - from homeassistant.helpers.entity import DeviceInfo from homeassistant.components.sensor import SensorEntity - +from homeassistant.core import callback +from homeassistant.helpers.dispatcher import async_dispatcher_connect +from .helper import format_attribute from .const import ( CONF_SENSOR_PORT_TRAFFIC, DEFAULT_SENSOR_PORT_TRAFFIC, + DOMAIN, + DATA_CLIENT, + ATTRIBUTION, ) - -from homeassistant.core import callback -from homeassistant.helpers.dispatcher import async_dispatcher_connect - -from .const import DOMAIN, DATA_CLIENT, ATTRIBUTION from .sensor_types import ( MikrotikSensorEntityDescription, SENSOR_TYPES, @@ -31,22 +28,6 @@ from .sensor_types import ( _LOGGER = logging.getLogger(__name__) -# --------------------------- -# format_attribute -# --------------------------- -def format_attribute(attr): - res = attr.replace("-", " ") - res = res.capitalize() - res = res.replace(" ip ", " IP ") - res = res.replace(" mac ", " MAC ") - res = res.replace(" mtu", " MTU") - res = res.replace("Sfp", "SFP") - res = res.replace("Poe", "POE") - res = res.replace(" tx", " TX") - res = res.replace(" rx", " RX") - return res - - # --------------------------- # async_setup_entry # --------------------------- diff --git a/custom_components/mikrotik_router/switch.py b/custom_components/mikrotik_router/switch.py index bef19cd..96592b3 100644 --- a/custom_components/mikrotik_router/switch.py +++ b/custom_components/mikrotik_router/switch.py @@ -9,7 +9,7 @@ from homeassistant.core import callback from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.restore_state import RestoreEntity - +from .helper import format_attribute from .const import DOMAIN, DATA_CLIENT, ATTRIBUTION _LOGGER = logging.getLogger(__name__) @@ -151,22 +151,6 @@ DEVICE_ATTRIBUTES_QUEUE = [ ] -# --------------------------- -# format_attribute -# --------------------------- -def format_attribute(attr): - res = attr.replace("-", " ") - res = res.capitalize() - res = res.replace(" ip ", " IP ") - res = res.replace(" mac ", " MAC ") - res = res.replace(" mtu", " MTU") - res = res.replace("Sfp", "SFP") - res = res.replace("Poe", "POE") - res = res.replace(" tx", " TX") - res = res.replace(" rx", " RX") - return res - - # --------------------------- # async_setup_entry # ---------------------------