PM8 code optimization

This commit is contained in:
Andrey Khrolenok 2020-03-21 19:02:28 +03:00
parent f0b4c5e723
commit 7ccaf0d9c3
No known key found for this signature in database
GPG key ID: 9D4D4A25BDFFA655
10 changed files with 557 additions and 536 deletions

View file

@ -1,9 +1,9 @@
{
"config": {
"title": "Mikrotik Роутер",
"title": "Роутер Mikrotik",
"step": {
"user": {
"title": "Mikrotik Роутер",
"title": "Роутер Mikrotik",
"description": "Настройка интеграции роутера Mikrotik.",
"data": {
"name": "Название интеграции",

View file

@ -1,7 +1,7 @@
"""Mikrotik Router integration."""
import logging
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.const import (
CONF_NAME,
CONF_HOST,
@ -11,14 +11,14 @@ from homeassistant.const import (
CONF_PASSWORD,
CONF_SSL,
)
from .mikrotik_controller import MikrotikControllerData
from homeassistant.exceptions import ConfigEntryNotReady
from .const import (
DOMAIN,
DATA_CLIENT,
DEFAULT_TRAFFIC_TYPE,
)
from .mikrotik_controller import MikrotikControllerData
_LOGGER = logging.getLogger(__name__)
@ -50,7 +50,8 @@ async def async_setup_entry(hass, config_entry):
traffic_type = DEFAULT_TRAFFIC_TYPE
mikrotik_controller = MikrotikControllerData(
hass, config_entry, name, host, port, username, password, use_ssl, traffic_type
hass, config_entry, name, host, port, username, password, use_ssl,
traffic_type
)
await mikrotik_controller.hwinfo_update()
await mikrotik_controller.async_update()
@ -65,11 +66,13 @@ async def async_setup_entry(hass, config_entry):
)
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(config_entry, "binary_sensor")
hass.config_entries.async_forward_entry_setup(config_entry,
"binary_sensor")
)
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(config_entry, "device_tracker")
hass.config_entries.async_forward_entry_setup(config_entry,
"device_tracker")
)
hass.async_create_task(
@ -95,8 +98,10 @@ async def async_unload_entry(hass, config_entry):
"""Unload a config entry."""
mikrotik_controller = hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id]
await hass.config_entries.async_forward_entry_unload(config_entry, "sensor")
await hass.config_entries.async_forward_entry_unload(config_entry, "binary_sensor")
await hass.config_entries.async_forward_entry_unload(config_entry, "device_tracker")
await hass.config_entries.async_forward_entry_unload(config_entry,
"binary_sensor")
await hass.config_entries.async_forward_entry_unload(config_entry,
"device_tracker")
await hass.config_entries.async_forward_entry_unload(config_entry, "switch")
await mikrotik_controller.async_reset()
hass.data[DOMAIN][DATA_CLIENT].pop(config_entry.entry_id)

View file

@ -1,13 +1,14 @@
"""Support for the Mikrotik Router binary sensor service."""
import logging
from homeassistant.core import callback
from homeassistant.components.binary_sensor import BinarySensorDevice
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.const import (
CONF_NAME,
ATTR_ATTRIBUTION,
)
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from .const import (
DOMAIN,

View file

@ -1,8 +1,8 @@
"""Config flow to configure Mikrotik Router."""
import logging
import voluptuous as vol
from homeassistant.core import callback
from homeassistant.config_entries import (
CONN_CLASS_LOCAL_POLL,
ConfigFlow,
@ -17,6 +17,7 @@ from homeassistant.const import (
CONF_PASSWORD,
CONF_SSL,
)
from homeassistant.core import callback
from .const import (
DOMAIN,
@ -27,11 +28,11 @@ from .const import (
DEFAULT_TRAFFIC_TYPE,
TRAFFIC_TYPES,
)
from .mikrotikapi import MikrotikAPI
_LOGGER = logging.getLogger(__name__)
# ---------------------------
# configured_instances
# ---------------------------
@ -39,7 +40,8 @@ _LOGGER = logging.getLogger(__name__)
def configured_instances(hass):
"""Return a set of configured instances."""
return set(
entry.data[CONF_NAME] for entry in hass.config_entries.async_entries(DOMAIN)
entry.data[CONF_NAME] for entry in
hass.config_entries.async_entries(DOMAIN)
)

View file

@ -1,15 +1,17 @@
"""Support for the Mikrotik Router device tracker."""
import logging
from homeassistant.core import callback
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.components.device_tracker.config_entry import ScannerEntity
from homeassistant.components.device_tracker.const import SOURCE_TYPE_ROUTER
from homeassistant.const import (
CONF_NAME,
ATTR_ATTRIBUTION,
)
from homeassistant.core import callback
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from .const import (
DOMAIN,
DATA_CLIENT,
@ -172,7 +174,8 @@ class MikrotikControllerPortDeviceTracker(ScannerEntity):
def device_info(self):
"""Return a port description for device registry."""
info = {
"connections": {(CONNECTION_NETWORK_MAC, self._data["port-mac-address"])},
"connections": {
(CONNECTION_NETWORK_MAC, self._data["port-mac-address"])},
"manufacturer": self._ctrl.data["resource"]["platform"],
"model": self._ctrl.data["resource"]["board-name"],
"name": self._data["default-name"],

View file

@ -265,7 +265,8 @@ def fill_vals_proc(data, uid, vals_proc) -> dict:
if _action == "combine":
if "key" in val:
tmp = _data[val["key"]] if val["key"] in _data else "unknown"
tmp = _data[val["key"]]\
if val["key"] in _data else "unknown"
if not _value:
_value = tmp
else:

View file

@ -1,8 +1,8 @@
"""Mikrotik Controller for Mikrotik Router."""
from datetime import timedelta
import asyncio
import logging
from datetime import timedelta
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_send
@ -17,10 +17,9 @@ from .const import (
DEFAULT_SCAN_INTERVAL,
DEFAULT_TRAFFIC_TYPE,
)
from .mikrotikapi import MikrotikAPI
from .helper import from_entry, parse_api
from .exceptions import ApiEntryNotFound
from .helper import from_entry, parse_api
from .mikrotikapi import MikrotikAPI
_LOGGER = logging.getLogger(__name__)
@ -277,8 +276,10 @@ class MikrotikControllerData:
traffic_div = 1
for uid in self.data["interface"]:
self.data["interface"][uid]["rx-bits-per-second-attr"] = traffic_type
self.data["interface"][uid]["tx-bits-per-second-attr"] = traffic_type
self.data["interface"][uid][
"rx-bits-per-second-attr"] = traffic_type
self.data["interface"][uid][
"tx-bits-per-second-attr"] = traffic_type
self.data["interface"][uid]["rx-bits-per-second"] = round(
self.data["interface"][uid]["rx-bits-per-second"] * traffic_div
)
@ -402,7 +403,8 @@ class MikrotikControllerData:
self.data["arp"][uid]["mac-address"] = "multiple"
self.data["arp"][uid]["address"] = "multiple"
else:
self.data["arp"][uid]["mac-address"] = from_entry(entry, "mac-address")
self.data["arp"][uid]["mac-address"] = from_entry(entry,
"mac-address")
self.data["arp"][uid]["address"] = (
mac2ip[self.data["arp"][uid]["mac-address"]]
if self.data["arp"][uid]["mac-address"] in mac2ip
@ -541,8 +543,8 @@ class MikrotikControllerData:
if "status" in self.data["fw-update"]:
self.data["fw-update"]["available"] = (
True
if self.data["fw-update"]["status"] == "New version is available"
True if self.data["fw-update"][
"status"] == "New version is available"
else False
)
else:

View file

@ -1,22 +1,23 @@
"""Mikrotik API for Mikrotik Router."""
import ssl
import importlib
import logging
import os
import ssl
import sys
import time
from threading import Lock
from voluptuous import Optional
from .exceptions import ApiEntryNotFound
from .const import (
DEFAULT_LOGIN_METHOD,
DEFAULT_ENCODING,
)
from .exceptions import ApiEntryNotFound
import os
import sys
import importlib
MODULE_PATH = os.path.join(os.path.dirname(__file__), "librouteros_custom", "__init__.py")
MODULE_PATH = os.path.join(os.path.dirname(__file__), "librouteros_custom",
"__init__.py")
MODULE_NAME = "librouteros_custom"
spec = importlib.util.spec_from_file_location(MODULE_NAME, MODULE_PATH)
librouteros_custom = importlib.util.module_from_spec(spec)
@ -112,7 +113,8 @@ class MikrotikAPI:
) as api_error:
if not self.connection_error_reported:
_LOGGER.error(
"Mikrotik %s error while connecting: %s", self._host, api_error
"Mikrotik %s error while connecting: %s", self._host,
api_error
)
self.connection_error_reported = True
@ -123,7 +125,8 @@ class MikrotikAPI:
except:
if not self.connection_error_reported:
_LOGGER.error(
"Mikrotik %s error while connecting: %s", self._host, "Unknown"
"Mikrotik %s error while connecting: %s", self._host,
"Unknown"
)
self.connection_error_reported = True
@ -196,7 +199,8 @@ class MikrotikAPI:
ValueError,
) as api_error:
if not self.connection_error_reported:
_LOGGER.error("Mikrotik %s error while path %s", self._host, api_error)
_LOGGER.error("Mikrotik %s error while path %s", self._host,
api_error)
self.connection_error_reported = True
self.disconnect()
@ -204,7 +208,8 @@ class MikrotikAPI:
return None
except:
if not self.connection_error_reported:
_LOGGER.error("Mikrotik %s error while path %s", self._host, "unknown")
_LOGGER.error("Mikrotik %s error while path %s", self._host,
"unknown")
self.connection_error_reported = True
self.disconnect()
@ -215,7 +220,8 @@ class MikrotikAPI:
tuple(response)
except librouteros_custom.exceptions.ConnectionClosed as api_error:
if not self.connection_error_reported:
_LOGGER.error("Mikrotik %s error while path %s", self._host, api_error)
_LOGGER.error("Mikrotik %s error while path %s", self._host,
api_error)
self.connection_error_reported = True
self.disconnect()
@ -223,7 +229,8 @@ class MikrotikAPI:
return None
except:
if not self.connection_error_reported:
_LOGGER.error("Mikrotik %s error while path %s", self._host, "unknown")
_LOGGER.error("Mikrotik %s error while path %s", self._host,
"unknown")
self.connection_error_reported = True
self.disconnect()
@ -283,7 +290,8 @@ class MikrotikAPI:
) as api_error:
if not self.connection_error_reported:
_LOGGER.error(
"Mikrotik %s error while update %s", self._host, api_error
"Mikrotik %s error while update %s", self._host,
api_error
)
self.connection_error_reported = True
@ -293,7 +301,8 @@ class MikrotikAPI:
except:
if not self.connection_error_reported:
_LOGGER.error(
"Mikrotik %s error while update %s", self._host, "unknown"
"Mikrotik %s error while update %s", self._host,
"unknown"
)
self.connection_error_reported = True
@ -357,7 +366,8 @@ class MikrotikAPI:
) as api_error:
if not self.connection_error_reported:
_LOGGER.error(
"Mikrotik %s error while run_script %s", self._host, api_error
"Mikrotik %s error while run_script %s", self._host,
api_error
)
self.connection_error_reported = True
@ -367,7 +377,8 @@ class MikrotikAPI:
except:
if not self.connection_error_reported:
_LOGGER.error(
"Mikrotik %s error while run_script %s", self._host, "unknown"
"Mikrotik %s error while run_script %s", self._host,
"unknown"
)
self.connection_error_reported = True
@ -426,7 +437,8 @@ class MikrotikAPI:
) as api_error:
if not self.connection_error_reported:
_LOGGER.error(
"Mikrotik %s error while get_traffic %s", self._host, api_error
"Mikrotik %s error while get_traffic %s", self._host,
api_error
)
self.connection_error_reported = True
@ -436,7 +448,8 @@ class MikrotikAPI:
except:
if not self.connection_error_reported:
_LOGGER.error(
"Mikrotik %s error while get_traffic %s", self._host, "unknown"
"Mikrotik %s error while get_traffic %s", self._host,
"unknown"
)
self.connection_error_reported = True
@ -449,7 +462,8 @@ class MikrotikAPI:
except librouteros_custom.exceptions.ConnectionClosed as api_error:
if not self.connection_error_reported:
_LOGGER.error(
"Mikrotik %s error while get_traffic %s", self._host, api_error
"Mikrotik %s error while get_traffic %s", self._host,
api_error
)
self.connection_error_reported = True
@ -459,7 +473,8 @@ class MikrotikAPI:
except:
if not self.connection_error_reported:
_LOGGER.error(
"Mikrotik %s error while get_traffic %s", self._host, "unknown"
"Mikrotik %s error while get_traffic %s", self._host,
"unknown"
)
self.connection_error_reported = True

View file

@ -1,21 +1,14 @@
"""Support for the Mikrotik Router sensor service."""
import logging
from homeassistant.const import (CONF_NAME, ATTR_ATTRIBUTION, ATTR_DEVICE_CLASS)
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.entity import Entity
from homeassistant.const import (
CONF_NAME,
ATTR_ATTRIBUTION,
ATTR_DEVICE_CLASS,
)
from .const import (
DOMAIN,
DATA_CLIENT,
ATTRIBUTION,
)
from .const import (DOMAIN, DATA_CLIENT, ATTRIBUTION)
_LOGGER = logging.getLogger(__name__)
@ -116,13 +109,15 @@ def update_items(inst, mikrotik_controller, async_add_entities, sensors):
continue
sensors[item_id] = MikrotikControllerSensor(
mikrotik_controller=mikrotik_controller, inst=inst, sensor=sensor
mikrotik_controller=mikrotik_controller, inst=inst,
sensor=sensor
)
new_sensors.append(sensors[item_id])
if "traffic_" in sensor:
for uid in mikrotik_controller.data["interface"]:
if mikrotik_controller.data["interface"][uid]["type"] == "ether":
if mikrotik_controller.data["interface"][uid][
"type"] == "ether":
item_id = f"{inst}-{sensor}-{mikrotik_controller.data['interface'][uid]['default-name']}"
if item_id in sensors:
if sensors[item_id].enabled:
@ -248,7 +243,8 @@ class MikrotikControllerTrafficSensor(MikrotikControllerSensor):
"""Initialize."""
super().__init__(mikrotik_controller, inst, sensor)
self._uid = uid
self._data = mikrotik_controller.data[SENSOR_TYPES[sensor][ATTR_PATH]][uid]
self._data = mikrotik_controller.data[SENSOR_TYPES[sensor][ATTR_PATH]][
uid]
@property
def name(self):
@ -264,7 +260,8 @@ class MikrotikControllerTrafficSensor(MikrotikControllerSensor):
def device_info(self):
"""Return a port description for device registry."""
info = {
"connections": {(CONNECTION_NETWORK_MAC, self._data["port-mac-address"])},
"connections": {
(CONNECTION_NETWORK_MAC, self._data["port-mac-address"])},
"manufacturer": self._ctrl.data["resource"]["platform"],
"model": self._ctrl.data["resource"]["board-name"],
"name": self._data["default-name"],

View file

@ -2,20 +2,13 @@
import logging
from homeassistant.components.switch import SwitchDevice
from homeassistant.const import (CONF_NAME, ATTR_ATTRIBUTION)
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 homeassistant.const import (
CONF_NAME,
ATTR_ATTRIBUTION,
)
from .const import (
DOMAIN,
DATA_CLIENT,
ATTRIBUTION,
)
from .const import (DOMAIN, DATA_CLIENT, ATTRIBUTION)
_LOGGER = logging.getLogger(__name__)
@ -192,7 +185,8 @@ class MikrotikControllerPortSwitch(MikrotikControllerSwitch):
def device_info(self):
"""Return a port description for device registry."""
info = {
"connections": {(CONNECTION_NETWORK_MAC, self._data["port-mac-address"])},
"connections": {
(CONNECTION_NETWORK_MAC, self._data["port-mac-address"])},
"manufacturer": self._ctrl.data["resource"]["platform"],
"model": self._ctrl.data["resource"]["board-name"],
"name": self._data["default-name"],
@ -362,7 +356,8 @@ class MikrotikControllerScriptSwitch(MikrotikControllerSwitch):
async def async_added_to_hass(self):
"""Script switch entity created."""
_LOGGER.debug("New script switch %s (%s)", self._inst, self._data["name"])
_LOGGER.debug("New script switch %s (%s)", self._inst,
self._data["name"])
@property
def name(self) -> str: