mirror of
https://github.com/tomaae/homeassistant-mikrotik_router.git
synced 2025-07-15 03:44:31 +02:00
flake8 compliance
This commit is contained in:
parent
d8b9294483
commit
0a6c8d369d
5 changed files with 81 additions and 82 deletions
|
@ -1,8 +1,6 @@
|
||||||
"""Config flow to configure Mikrotik Router."""
|
"""Config flow to configure Mikrotik Router."""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
|
@ -25,6 +23,8 @@ from .const import (
|
||||||
|
|
||||||
from .mikrotikapi import MikrotikAPI
|
from .mikrotikapi import MikrotikAPI
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
# configured_instances
|
# configured_instances
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
|
@ -58,16 +58,16 @@ class MikrotikControllerConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
"""Handle a flow initialized by the user."""
|
"""Handle a flow initialized by the user."""
|
||||||
errors = {}
|
errors = {}
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
## Check if instance with this name already exists
|
# Check if instance with this name already exists
|
||||||
if user_input[CONF_NAME] in configured_instances(self.hass):
|
if user_input[CONF_NAME] in configured_instances(self.hass):
|
||||||
errors["base"] = "name_exists"
|
errors["base"] = "name_exists"
|
||||||
|
|
||||||
## Test connection
|
# Test connection
|
||||||
api = MikrotikAPI(host=user_input["host"], username=user_input["username"], password=user_input["password"], port=user_input["port"], use_ssl=user_input["ssl"])
|
api = MikrotikAPI(host=user_input["host"], username=user_input["username"], password=user_input["password"], port=user_input["port"], use_ssl=user_input["ssl"])
|
||||||
if not api.connect():
|
if not api.connect():
|
||||||
errors[CONF_HOST] = api.error
|
errors[CONF_HOST] = api.error
|
||||||
|
|
||||||
## Save instance
|
# Save instance
|
||||||
if not errors:
|
if not errors:
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
title=user_input[CONF_NAME],
|
title=user_input[CONF_NAME],
|
||||||
|
|
|
@ -49,7 +49,6 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
"""Update the values of the controller."""
|
"""Update the values of the controller."""
|
||||||
update_items(name, mikrotik_controller, async_add_entities, tracked)
|
update_items(name, mikrotik_controller, async_add_entities, tracked)
|
||||||
|
|
||||||
|
|
||||||
mikrotik_controller.listeners.append(
|
mikrotik_controller.listeners.append(
|
||||||
async_dispatcher_connect(hass, mikrotik_controller.signal_update, update_controller)
|
async_dispatcher_connect(hass, mikrotik_controller.signal_update, update_controller)
|
||||||
)
|
)
|
||||||
|
|
|
@ -14,9 +14,9 @@ from .const import (
|
||||||
DEFAULT_SCAN_INTERVAL,
|
DEFAULT_SCAN_INTERVAL,
|
||||||
)
|
)
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
from .mikrotikapi import MikrotikAPI
|
from .mikrotikapi import MikrotikAPI
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
# DEFAULT_SCAN_INTERVAL = timedelta(seconds=DEFAULT_SCAN_INTERVAL)
|
# DEFAULT_SCAN_INTERVAL = timedelta(seconds=DEFAULT_SCAN_INTERVAL)
|
||||||
|
|
||||||
|
|
||||||
|
@ -167,20 +167,20 @@ class MikrotikControllerData():
|
||||||
bridge_used = False
|
bridge_used = False
|
||||||
data = self.api.path("/ip/arp")
|
data = self.api.path("/ip/arp")
|
||||||
for entry in data:
|
for entry in data:
|
||||||
## Ignore invalid entries
|
# Ignore invalid entries
|
||||||
if entry['invalid']:
|
if entry['invalid']:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
## Do not add ARP detected on bridge
|
# Do not add ARP detected on bridge
|
||||||
if entry['interface'] == "bridge":
|
if entry['interface'] == "bridge":
|
||||||
bridge_used = True
|
bridge_used = True
|
||||||
## Build address table on bridge
|
# Build address table on bridge
|
||||||
if 'mac-address' in entry and 'address' in entry:
|
if 'mac-address' in entry and 'address' in entry:
|
||||||
mac2ip[entry['mac-address']] = entry['address']
|
mac2ip[entry['mac-address']] = entry['address']
|
||||||
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
## Get iface default-name from custom name
|
# Get iface default-name from custom name
|
||||||
uid = None
|
uid = None
|
||||||
for ifacename in self.data['interface']:
|
for ifacename in self.data['interface']:
|
||||||
if self.data['interface'][ifacename]['name'] == entry['interface']:
|
if self.data['interface'][ifacename]['name'] == entry['interface']:
|
||||||
|
@ -190,11 +190,11 @@ class MikrotikControllerData():
|
||||||
if not uid:
|
if not uid:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
## Create uid arp dict
|
# Create uid arp dict
|
||||||
if uid not in self.data['arp']:
|
if uid not in self.data['arp']:
|
||||||
self.data['arp'][uid] = {}
|
self.data['arp'][uid] = {}
|
||||||
|
|
||||||
## Add data
|
# Add data
|
||||||
self.data['arp'][uid]['interface'] = uid
|
self.data['arp'][uid]['interface'] = uid
|
||||||
if 'mac-address' in self.data['arp'][uid]:
|
if 'mac-address' in self.data['arp'][uid]:
|
||||||
self.data['arp'][uid]['mac-address'] = "multiple"
|
self.data['arp'][uid]['mac-address'] = "multiple"
|
||||||
|
@ -209,7 +209,7 @@ class MikrotikControllerData():
|
||||||
if bridge_used:
|
if bridge_used:
|
||||||
self.update_bridge_hosts(mac2ip)
|
self.update_bridge_hosts(mac2ip)
|
||||||
|
|
||||||
## Map ARP to ifaces
|
# Map ARP to ifaces
|
||||||
for uid in self.data['interface']:
|
for uid in self.data['interface']:
|
||||||
self.data['interface'][uid]['client-ip-address'] = self.data['arp'][uid]['address'] if uid in self.data['arp'] and 'address' in self.data['arp'][uid] else ""
|
self.data['interface'][uid]['client-ip-address'] = self.data['arp'][uid]['address'] if uid in self.data['arp'] and 'address' in self.data['arp'][uid] else ""
|
||||||
self.data['interface'][uid]['client-mac-address'] = self.data['arp'][uid]['mac-address'] if uid in self.data['arp'] and 'mac-address' in self.data['arp'][uid] else ""
|
self.data['interface'][uid]['client-mac-address'] = self.data['arp'][uid]['mac-address'] if uid in self.data['arp'] and 'mac-address' in self.data['arp'][uid] else ""
|
||||||
|
@ -222,11 +222,11 @@ class MikrotikControllerData():
|
||||||
def update_bridge_hosts(self, mac2ip):
|
def update_bridge_hosts(self, mac2ip):
|
||||||
data = self.api.path("/interface/bridge/host")
|
data = self.api.path("/interface/bridge/host")
|
||||||
for entry in data:
|
for entry in data:
|
||||||
## Ignore port MAC
|
# Ignore port MAC
|
||||||
if entry['local']:
|
if entry['local']:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
## Get iface default-name from custom name
|
# Get iface default-name from custom name
|
||||||
uid = None
|
uid = None
|
||||||
for ifacename in self.data['interface']:
|
for ifacename in self.data['interface']:
|
||||||
if self.data['interface'][ifacename]['name'] == entry['interface']:
|
if self.data['interface'][ifacename]['name'] == entry['interface']:
|
||||||
|
@ -236,11 +236,11 @@ class MikrotikControllerData():
|
||||||
if not uid:
|
if not uid:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
## Create uid arp dict
|
# Create uid arp dict
|
||||||
if uid not in self.data['arp']:
|
if uid not in self.data['arp']:
|
||||||
self.data['arp'][uid] = {}
|
self.data['arp'][uid] = {}
|
||||||
|
|
||||||
## Add data
|
# Add data
|
||||||
self.data['arp'][uid]['interface'] = uid
|
self.data['arp'][uid]['interface'] = uid
|
||||||
if 'mac-address' in self.data['arp'][uid]:
|
if 'mac-address' in self.data['arp'][uid]:
|
||||||
self.data['arp'][uid]['mac-address'] = "multiple"
|
self.data['arp'][uid]['mac-address'] = "multiple"
|
||||||
|
|
|
@ -27,7 +27,7 @@ class MikrotikAPI:
|
||||||
self._connected = False
|
self._connected = False
|
||||||
self.error = ""
|
self.error = ""
|
||||||
|
|
||||||
## Default ports
|
# Default ports
|
||||||
if not self._port:
|
if not self._port:
|
||||||
self._port = 8729 if self._use_ssl else 8728
|
self._port = 8729 if self._use_ssl else 8728
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue