mirror of
https://github.com/tomaae/homeassistant-mikrotik_router.git
synced 2025-07-13 19:04:30 +02:00
added docstrings to all functions and classes
This commit is contained in:
parent
d1f8a7f56b
commit
b2c268a1c0
3 changed files with 33 additions and 17 deletions
|
@ -40,8 +40,9 @@ def configured_instances(hass):
|
||||||
# MikrotikControllerConfigFlow
|
# MikrotikControllerConfigFlow
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
class MikrotikControllerConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
class MikrotikControllerConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
|
"""MikrotikControllerConfigFlow class"""
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""Initialize."""
|
"""Initialize MikrotikControllerConfigFlow."""
|
||||||
return
|
return
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
|
@ -24,8 +24,9 @@ _LOGGER = logging.getLogger(__name__)
|
||||||
# MikrotikControllerData
|
# MikrotikControllerData
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
class MikrotikControllerData():
|
class MikrotikControllerData():
|
||||||
|
"""MikrotikController Class"""
|
||||||
def __init__(self, hass, config_entry, name, host, port, username, password, use_ssl):
|
def __init__(self, hass, config_entry, name, host, port, username, password, use_ssl):
|
||||||
"""Initialize."""
|
"""Initialize MikrotikController."""
|
||||||
self.name = name
|
self.name = name
|
||||||
self.hass = hass
|
self.hass = hass
|
||||||
self.config_entry = config_entry
|
self.config_entry = config_entry
|
||||||
|
@ -54,7 +55,7 @@ class MikrotikControllerData():
|
||||||
# force_update
|
# force_update
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
async def force_update(self, now=None):
|
async def force_update(self, now=None):
|
||||||
"""Periodic update."""
|
"""Trigger update by timer"""
|
||||||
await self.async_update()
|
await self.async_update()
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -62,7 +63,7 @@ class MikrotikControllerData():
|
||||||
# force_fwupdate_check
|
# force_fwupdate_check
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
async def force_fwupdate_check(self, now=None):
|
async def force_fwupdate_check(self, now=None):
|
||||||
"""Periodic update."""
|
"""Trigger hourly update by timer"""
|
||||||
await self.async_fwupdate_check()
|
await self.async_fwupdate_check()
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -88,21 +89,21 @@ class MikrotikControllerData():
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
@property
|
@property
|
||||||
def signal_update(self):
|
def signal_update(self):
|
||||||
"""Event specific per UniFi entry to signal new data."""
|
"""Event to signal new data."""
|
||||||
return f"{DOMAIN}-update-{self.name}"
|
return f"{DOMAIN}-update-{self.name}"
|
||||||
|
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
# connected
|
# connected
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
def connected(self):
|
def connected(self):
|
||||||
"""Return connected boolean."""
|
"""Return connected state"""
|
||||||
return self.api.connected()
|
return self.api.connected()
|
||||||
|
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
# hwinfo_update
|
# hwinfo_update
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
async def hwinfo_update(self):
|
async def hwinfo_update(self):
|
||||||
"""Update Mikrotik hardware info."""
|
"""Update Mikrotik hardware info"""
|
||||||
self.get_system_routerboard()
|
self.get_system_routerboard()
|
||||||
self.get_system_resource()
|
self.get_system_resource()
|
||||||
return
|
return
|
||||||
|
@ -111,7 +112,7 @@ class MikrotikControllerData():
|
||||||
# async_fwupdate_check
|
# async_fwupdate_check
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
async def async_fwupdate_check(self):
|
async def async_fwupdate_check(self):
|
||||||
"""Update Mikrotik Controller data."""
|
"""Update Mikrotik data"""
|
||||||
|
|
||||||
self.get_firmare_update()
|
self.get_firmare_update()
|
||||||
|
|
||||||
|
@ -123,12 +124,12 @@ class MikrotikControllerData():
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
# @Throttle(DEFAULT_SCAN_INTERVAL)
|
# @Throttle(DEFAULT_SCAN_INTERVAL)
|
||||||
async def async_update(self):
|
async def async_update(self):
|
||||||
"""Update Mikrotik Controller data."""
|
"""Update Mikrotik data"""
|
||||||
|
|
||||||
if 'available' not in self.data['fw-update']:
|
if 'available' not in self.data['fw-update']:
|
||||||
await self.async_fwupdate_check()
|
await self.async_fwupdate_check()
|
||||||
|
|
||||||
self.get_interfaces()
|
self.get_interface()
|
||||||
self.get_arp()
|
self.get_arp()
|
||||||
self.get_nat()
|
self.get_nat()
|
||||||
self.get_system_resource()
|
self.get_system_resource()
|
||||||
|
@ -141,7 +142,7 @@ class MikrotikControllerData():
|
||||||
# async_reset
|
# async_reset
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
async def async_reset(self):
|
async def async_reset(self):
|
||||||
"""Reset this controller to default state."""
|
"""Reset dispatchers"""
|
||||||
for unsub_dispatcher in self.listeners:
|
for unsub_dispatcher in self.listeners:
|
||||||
unsub_dispatcher()
|
unsub_dispatcher()
|
||||||
|
|
||||||
|
@ -152,18 +153,21 @@ class MikrotikControllerData():
|
||||||
# set_value
|
# set_value
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
def set_value(self, path, param, value, mod_param, mod_value):
|
def set_value(self, path, param, value, mod_param, mod_value):
|
||||||
|
"""Change value using Mikrotik API"""
|
||||||
return self.api.update(path, param, value, mod_param, mod_value)
|
return self.api.update(path, param, value, mod_param, mod_value)
|
||||||
|
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
# run_script
|
# run_script
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
def run_script(self, name):
|
def run_script(self, name):
|
||||||
|
"""Run script using Mikrotik API"""
|
||||||
return self.api.run_script(name)
|
return self.api.run_script(name)
|
||||||
|
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
# get_interfaces
|
# get_interface
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
def get_interfaces(self):
|
def get_interface(self):
|
||||||
|
"""Get all interfaces data from Mikrotik"""
|
||||||
ifaces = self.api.path("/interface")
|
ifaces = self.api.path("/interface")
|
||||||
for iface in ifaces:
|
for iface in ifaces:
|
||||||
if 'default-name' not in iface:
|
if 'default-name' not in iface:
|
||||||
|
@ -200,6 +204,7 @@ class MikrotikControllerData():
|
||||||
# get_arp
|
# get_arp
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
def get_arp(self):
|
def get_arp(self):
|
||||||
|
"""Get ARP data from Mikrotik"""
|
||||||
self.data['arp'] = {}
|
self.data['arp'] = {}
|
||||||
if not self.option_track_arp:
|
if not self.option_track_arp:
|
||||||
for uid in self.data['interface']:
|
for uid in self.data['interface']:
|
||||||
|
@ -225,7 +230,7 @@ class MikrotikControllerData():
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Get iface default-name from custom name
|
# Get iface default-name from custom name
|
||||||
uid = self.get_iface_name(entry)
|
uid = self.get_iface_from_entry(entry)
|
||||||
if not uid:
|
if not uid:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
@ -252,6 +257,7 @@ class MikrotikControllerData():
|
||||||
# update_bridge_hosts
|
# update_bridge_hosts
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
def update_bridge_hosts(self, mac2ip):
|
def update_bridge_hosts(self, mac2ip):
|
||||||
|
"""Get list of hosts in bridge for ARP data from Mikrotik"""
|
||||||
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
|
||||||
|
@ -259,7 +265,7 @@ class MikrotikControllerData():
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Get iface default-name from custom name
|
# Get iface default-name from custom name
|
||||||
uid = self.get_iface_name(entry)
|
uid = self.get_iface_from_entry(entry)
|
||||||
if not uid:
|
if not uid:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
@ -282,9 +288,10 @@ class MikrotikControllerData():
|
||||||
return
|
return
|
||||||
|
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
# get_iface_name
|
# get_iface_from_entry
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
def get_iface_name(self, entry):
|
def get_iface_from_entry(self, entry):
|
||||||
|
"""Get interface name from Mikrotik"""
|
||||||
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']:
|
||||||
|
@ -297,6 +304,7 @@ class MikrotikControllerData():
|
||||||
# get_nat
|
# get_nat
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
def get_nat(self):
|
def get_nat(self):
|
||||||
|
"""Get NAT data from Mikrotik"""
|
||||||
data = self.api.path("/ip/firewall/nat")
|
data = self.api.path("/ip/firewall/nat")
|
||||||
for entry in data:
|
for entry in data:
|
||||||
if entry['action'] != 'dst-nat':
|
if entry['action'] != 'dst-nat':
|
||||||
|
@ -323,6 +331,7 @@ class MikrotikControllerData():
|
||||||
# get_system_routerboard
|
# get_system_routerboard
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
def get_system_routerboard(self):
|
def get_system_routerboard(self):
|
||||||
|
"""Get routerboard data from Mikrotik"""
|
||||||
data = self.api.path("/system/routerboard")
|
data = self.api.path("/system/routerboard")
|
||||||
for entry in data:
|
for entry in data:
|
||||||
self.data['routerboard']['routerboard'] = True if entry['routerboard'] else False
|
self.data['routerboard']['routerboard'] = True if entry['routerboard'] else False
|
||||||
|
@ -336,6 +345,7 @@ class MikrotikControllerData():
|
||||||
# get_system_resource
|
# get_system_resource
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
def get_system_resource(self):
|
def get_system_resource(self):
|
||||||
|
"""Get system resources data from Mikrotik"""
|
||||||
data = self.api.path("/system/resource")
|
data = self.api.path("/system/resource")
|
||||||
for entry in data:
|
for entry in data:
|
||||||
self.data['resource']['platform'] = entry['platform'] if 'platform' in entry else "unknown"
|
self.data['resource']['platform'] = entry['platform'] if 'platform' in entry else "unknown"
|
||||||
|
@ -359,6 +369,7 @@ class MikrotikControllerData():
|
||||||
# get_system_routerboard
|
# get_system_routerboard
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
def get_firmare_update(self):
|
def get_firmare_update(self):
|
||||||
|
"""Check for firmware update on Mikrotik"""
|
||||||
data = self.api.path("/system/package/update")
|
data = self.api.path("/system/package/update")
|
||||||
for entry in data:
|
for entry in data:
|
||||||
self.data['fw-update']['available'] = True if entry['status'] == "New version is available" else False
|
self.data['fw-update']['available'] = True if entry['status'] == "New version is available" else False
|
||||||
|
@ -372,6 +383,7 @@ class MikrotikControllerData():
|
||||||
# get_script
|
# get_script
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
def get_script(self):
|
def get_script(self):
|
||||||
|
"""Get list of all scripts from Mikrotik"""
|
||||||
data = self.api.path("/system/script")
|
data = self.api.path("/system/script")
|
||||||
for entry in data:
|
for entry in data:
|
||||||
if 'name' not in entry:
|
if 'name' not in entry:
|
||||||
|
|
|
@ -76,6 +76,7 @@ class MikrotikAPI:
|
||||||
# error_to_strings
|
# error_to_strings
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
def error_to_strings(self, error):
|
def error_to_strings(self, error):
|
||||||
|
"""Translate error output to error string."""
|
||||||
self.error = "cannot_connect"
|
self.error = "cannot_connect"
|
||||||
if error == "invalid user name or password (6)":
|
if error == "invalid user name or password (6)":
|
||||||
self.error = "wrong_login"
|
self.error = "wrong_login"
|
||||||
|
@ -121,6 +122,7 @@ class MikrotikAPI:
|
||||||
# update
|
# update
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
def update(self, path, param, value, mod_param, mod_value):
|
def update(self, path, param, value, mod_param, mod_value):
|
||||||
|
"""Modify a parameter"""
|
||||||
response = self.path(path)
|
response = self.path(path)
|
||||||
if response is None:
|
if response is None:
|
||||||
return False
|
return False
|
||||||
|
@ -145,6 +147,7 @@ class MikrotikAPI:
|
||||||
# run_script
|
# run_script
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
def run_script(self, name):
|
def run_script(self, name):
|
||||||
|
"""Run script"""
|
||||||
response = self.path('/system/script')
|
response = self.path('/system/script')
|
||||||
if response is None:
|
if response is None:
|
||||||
return False
|
return False
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue