removed trailing whitespace from empty lines

This commit is contained in:
tomaae 2019-12-05 20:45:04 +01:00
parent 7d9470133e
commit b851085f48
8 changed files with 212 additions and 212 deletions

View file

@ -42,32 +42,32 @@ async def async_setup_entry(hass, config_entry):
username = config_entry.data[CONF_USERNAME] username = config_entry.data[CONF_USERNAME]
password = config_entry.data[CONF_PASSWORD] password = config_entry.data[CONF_PASSWORD]
use_ssl = config_entry.data[CONF_SSL] use_ssl = config_entry.data[CONF_SSL]
mikrotik_controller = MikrotikControllerData(hass, config_entry, name, host, port, username, password, use_ssl) mikrotik_controller = MikrotikControllerData(hass, config_entry, name, host, port, username, password, use_ssl)
await mikrotik_controller.hwinfo_update() await mikrotik_controller.hwinfo_update()
await mikrotik_controller.async_update() await mikrotik_controller.async_update()
if not mikrotik_controller.data: if not mikrotik_controller.data:
raise ConfigEntryNotReady() raise ConfigEntryNotReady()
hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id] = mikrotik_controller hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id] = mikrotik_controller
hass.async_create_task( hass.async_create_task(
hass.config_entries.async_forward_entry_setup(config_entry, "sensor") hass.config_entries.async_forward_entry_setup(config_entry, "sensor")
) )
hass.async_create_task( 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.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( hass.async_create_task(
hass.config_entries.async_forward_entry_setup(config_entry, "switch") hass.config_entries.async_forward_entry_setup(config_entry, "switch")
) )
device_registry = await hass.helpers.device_registry.async_get_registry() device_registry = await hass.helpers.device_registry.async_get_registry()
device_registry.async_get_or_create( device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id, config_entry_id=config_entry.entry_id,
@ -76,7 +76,7 @@ async def async_setup_entry(hass, config_entry):
name=mikrotik_controller.data['routerboard']['model'], name=mikrotik_controller.data['routerboard']['model'],
sw_version=mikrotik_controller.data['resource']['version'], sw_version=mikrotik_controller.data['resource']['version'],
) )
return True return True

View file

@ -40,16 +40,16 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
name = config_entry.data[CONF_NAME] name = config_entry.data[CONF_NAME]
mikrotik_controller = hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id] mikrotik_controller = hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id]
sensors = {} sensors = {}
@callback @callback
def update_controller(): def update_controller():
"""Update the values of the controller.""" """Update the values of the controller."""
update_items(name, mikrotik_controller, async_add_entities, sensors) update_items(name, mikrotik_controller, async_add_entities, sensors)
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)
) )
update_controller() update_controller()
return return
@ -61,63 +61,63 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
def update_items(name, mikrotik_controller, async_add_entities, sensors): def update_items(name, mikrotik_controller, async_add_entities, sensors):
"""Update sensor state from the controller.""" """Update sensor state from the controller."""
new_sensors = [] new_sensors = []
for sensor in SENSOR_TYPES: for sensor in SENSOR_TYPES:
item_id = name + "-" + sensor item_id = name + "-" + sensor
if item_id in sensors: if item_id in sensors:
if sensors[item_id].enabled: if sensors[item_id].enabled:
sensors[item_id].async_schedule_update_ha_state() sensors[item_id].async_schedule_update_ha_state()
continue continue
sensors[item_id] = MikrotikControllerBinarySensor(mikrotik_controller=mikrotik_controller, name=name, kind=sensor) sensors[item_id] = MikrotikControllerBinarySensor(mikrotik_controller=mikrotik_controller, name=name, kind=sensor)
new_sensors.append(sensors[item_id]) new_sensors.append(sensors[item_id])
if new_sensors: if new_sensors:
async_add_entities(new_sensors, True) async_add_entities(new_sensors, True)
return return
class MikrotikControllerBinarySensor(BinarySensorDevice): class MikrotikControllerBinarySensor(BinarySensorDevice):
"""Define an Mikrotik Controller Binary Sensor.""" """Define an Mikrotik Controller Binary Sensor."""
def __init__(self, mikrotik_controller, name, kind, uid=''): def __init__(self, mikrotik_controller, name, kind, uid=''):
"""Initialize.""" """Initialize."""
self.mikrotik_controller = mikrotik_controller self.mikrotik_controller = mikrotik_controller
self._name = name self._name = name
self.kind = kind self.kind = kind
self.uid = uid self.uid = uid
self._device_class = None self._device_class = None
self._state = None self._state = None
self._icon = None self._icon = None
self._unit_of_measurement = None self._unit_of_measurement = None
self._attrs = {ATTR_ATTRIBUTION: ATTRIBUTION} self._attrs = {ATTR_ATTRIBUTION: ATTRIBUTION}
@property @property
def name(self): def name(self):
"""Return the name.""" """Return the name."""
if self.uid: if self.uid:
return f"{self._name} {self.uid} {SENSOR_TYPES[self.kind][ATTR_LABEL]}" return f"{self._name} {self.uid} {SENSOR_TYPES[self.kind][ATTR_LABEL]}"
return f"{self._name} {SENSOR_TYPES[self.kind][ATTR_LABEL]}" return f"{self._name} {SENSOR_TYPES[self.kind][ATTR_LABEL]}"
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the state attributes.""" """Return the state attributes."""
return self._attrs return self._attrs
@property @property
def unique_id(self): def unique_id(self):
"""Return a unique_id for this entity.""" """Return a unique_id for this entity."""
if self.uid: if self.uid:
return f"{self._name.lower()}-{self.kind.lower()}-{self.uid.lower()}" return f"{self._name.lower()}-{self.kind.lower()}-{self.uid.lower()}"
return f"{self._name.lower()}-{self.kind.lower()}" return f"{self._name.lower()}-{self.kind.lower()}"
@property @property
def available(self): def available(self):
"""Return True if entity is available.""" """Return True if entity is available."""
return bool(self.mikrotik_controller.data) return bool(self.mikrotik_controller.data)
@property @property
def device_info(self): def device_info(self):
"""Return a port description for device registry.""" """Return a port description for device registry."""
@ -133,17 +133,17 @@ class MikrotikControllerBinarySensor(BinarySensorDevice):
"""Synchronize state with controller.""" """Synchronize state with controller."""
# await self.mikrotik_controller.async_update() # await self.mikrotik_controller.async_update()
return return
async def async_added_to_hass(self): async def async_added_to_hass(self):
"""Port entity created.""" """Port entity created."""
_LOGGER.debug("New sensor %s (%s)", self._name, self.kind) _LOGGER.debug("New sensor %s (%s)", self._name, self.kind)
return return
@property @property
def is_on(self): def is_on(self):
"""Return true if sensor is on.""" """Return true if sensor is on."""
val = False val = False
if SENSOR_TYPES[self.kind][ATTR_PATH] in self.mikrotik_controller.data and SENSOR_TYPES[self.kind][ATTR_ATTR] in self.mikrotik_controller.data[SENSOR_TYPES[self.kind][ATTR_PATH]]: if SENSOR_TYPES[self.kind][ATTR_PATH] in self.mikrotik_controller.data and SENSOR_TYPES[self.kind][ATTR_ATTR] in self.mikrotik_controller.data[SENSOR_TYPES[self.kind][ATTR_PATH]]:
val = self.mikrotik_controller.data[SENSOR_TYPES[self.kind][ATTR_PATH]][SENSOR_TYPES[self.kind][ATTR_ATTR]] val = self.mikrotik_controller.data[SENSOR_TYPES[self.kind][ATTR_PATH]][SENSOR_TYPES[self.kind][ATTR_ATTR]]
return val return val

View file

@ -44,17 +44,17 @@ class MikrotikControllerConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
def __init__(self): def __init__(self):
"""Initialize MikrotikControllerConfigFlow.""" """Initialize MikrotikControllerConfigFlow."""
return return
@staticmethod @staticmethod
@callback @callback
def async_get_options_flow(config_entry): def async_get_options_flow(config_entry):
"""Get the options flow for this handler.""" """Get the options flow for this handler."""
return MikrotikControllerOptionsFlowHandler(config_entry) return MikrotikControllerOptionsFlowHandler(config_entry)
async def async_step_import(self, user_input=None): async def async_step_import(self, user_input=None):
"""Occurs when a previously entry setup fails and is re-initiated.""" """Occurs when a previously entry setup fails and is re-initiated."""
return await self.async_step_user(user_input) return await self.async_step_user(user_input)
async def async_step_user(self, user_input=None): async def async_step_user(self, user_input=None):
"""Handle a flow initialized by the user.""" """Handle a flow initialized by the user."""
errors = {} errors = {}
@ -62,7 +62,7 @@ class MikrotikControllerConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
# 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"], api = MikrotikAPI(host=user_input["host"],
username=user_input["username"], username=user_input["username"],
@ -72,14 +72,14 @@ class MikrotikControllerConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
) )
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],
data=user_input data=user_input
) )
return self._show_config_form(host=user_input["host"], return self._show_config_form(host=user_input["host"],
username=user_input["username"], username=user_input["username"],
password=user_input["password"], password=user_input["password"],
@ -88,9 +88,9 @@ class MikrotikControllerConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
use_ssl=user_input["ssl"], use_ssl=user_input["ssl"],
errors=errors errors=errors
) )
return self._show_config_form(errors=errors) return self._show_config_form(errors=errors)
# --------------------------- # ---------------------------
# _show_config_form # _show_config_form
# --------------------------- # ---------------------------
@ -115,22 +115,22 @@ class MikrotikControllerConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
# --------------------------- # ---------------------------
class MikrotikControllerOptionsFlowHandler(config_entries.OptionsFlow): class MikrotikControllerOptionsFlowHandler(config_entries.OptionsFlow):
"""Handle options.""" """Handle options."""
def __init__(self, config_entry): def __init__(self, config_entry):
"""Initialize options flow.""" """Initialize options flow."""
self.config_entry = config_entry self.config_entry = config_entry
self.options = dict(config_entry.options) self.options = dict(config_entry.options)
async def async_step_init(self, user_input=None): async def async_step_init(self, user_input=None):
"""Manage the options.""" """Manage the options."""
return await self.async_step_device_tracker(user_input) return await self.async_step_device_tracker(user_input)
async def async_step_device_tracker(self, user_input=None): async def async_step_device_tracker(self, user_input=None):
"""Manage the device tracker options.""" """Manage the device tracker options."""
if user_input is not None: if user_input is not None:
self.options.update(user_input) self.options.update(user_input)
return self.async_create_entry(title="", data=self.options) return self.async_create_entry(title="", data=self.options)
return self.async_show_form( return self.async_show_form(
step_id="device_tracker", step_id="device_tracker",
data_schema=vol.Schema( data_schema=vol.Schema(

View file

@ -43,16 +43,16 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
name = config_entry.data[CONF_NAME] name = config_entry.data[CONF_NAME]
mikrotik_controller = hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id] mikrotik_controller = hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id]
tracked = {} tracked = {}
@callback @callback
def update_controller(): def update_controller():
"""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)
) )
update_controller() update_controller()
return return
@ -64,7 +64,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
def update_items(name, mikrotik_controller, async_add_entities, tracked): def update_items(name, mikrotik_controller, async_add_entities, tracked):
"""Update tracked device state from the controller.""" """Update tracked device state from the controller."""
new_tracked = [] new_tracked = []
for uid in mikrotik_controller.data['interface']: 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 = name + "-" + mikrotik_controller.data['interface'][uid]['default-name'] item_id = name + "-" + mikrotik_controller.data['interface'][uid]['default-name']
@ -72,13 +72,13 @@ def update_items(name, mikrotik_controller, async_add_entities, tracked):
if tracked[item_id].enabled: if tracked[item_id].enabled:
tracked[item_id].async_schedule_update_ha_state() tracked[item_id].async_schedule_update_ha_state()
continue continue
tracked[item_id] = MikrotikControllerPortDeviceTracker(name, uid, mikrotik_controller) tracked[item_id] = MikrotikControllerPortDeviceTracker(name, uid, mikrotik_controller)
new_tracked.append(tracked[item_id]) new_tracked.append(tracked[item_id])
if new_tracked: if new_tracked:
async_add_entities(new_tracked) async_add_entities(new_tracked)
return return
@ -87,57 +87,57 @@ def update_items(name, mikrotik_controller, async_add_entities, tracked):
# --------------------------- # ---------------------------
class MikrotikControllerPortDeviceTracker(ScannerEntity): class MikrotikControllerPortDeviceTracker(ScannerEntity):
"""Representation of a network port.""" """Representation of a network port."""
def __init__(self, name, uid, mikrotik_controller): def __init__(self, name, uid, mikrotik_controller):
"""Set up tracked port.""" """Set up tracked port."""
self._name = name self._name = name
self._uid = uid self._uid = uid
self.mikrotik_controller = mikrotik_controller self.mikrotik_controller = mikrotik_controller
self._attrs = { self._attrs = {
ATTR_ATTRIBUTION: ATTRIBUTION, ATTR_ATTRIBUTION: ATTRIBUTION,
} }
@property @property
def entity_registry_enabled_default(self): def entity_registry_enabled_default(self):
"""Return if the entity should be enabled when first added to the entity registry.""" """Return if the entity should be enabled when first added to the entity registry."""
return True return True
async def async_added_to_hass(self): async def async_added_to_hass(self):
"""Port entity created.""" """Port entity created."""
_LOGGER.debug("New port tracker %s (%s)", self._name, self.mikrotik_controller.data['interface'][self._uid]['port-mac-address']) _LOGGER.debug("New port tracker %s (%s)", self._name, self.mikrotik_controller.data['interface'][self._uid]['port-mac-address'])
return return
async def async_update(self): async def async_update(self):
"""Synchronize state with controller.""" """Synchronize state with controller."""
# await self.mikrotik_controller.async_update() # await self.mikrotik_controller.async_update()
return return
@property @property
def is_connected(self): def is_connected(self):
"""Return true if the port is connected to the network.""" """Return true if the port is connected to the network."""
return self.mikrotik_controller.data['interface'][self._uid]['running'] return self.mikrotik_controller.data['interface'][self._uid]['running']
@property @property
def source_type(self): def source_type(self):
"""Return the source type of the port.""" """Return the source type of the port."""
return SOURCE_TYPE_ROUTER return SOURCE_TYPE_ROUTER
@property @property
def name(self) -> str: def name(self) -> str:
"""Return the name of the port.""" """Return the name of the port."""
return f"{self._name} {self.mikrotik_controller.data['interface'][self._uid]['default-name']}" return f"{self._name} {self.mikrotik_controller.data['interface'][self._uid]['default-name']}"
@property @property
def unique_id(self) -> str: def unique_id(self) -> str:
"""Return a unique identifier for this port.""" """Return a unique identifier for this port."""
return f"{self._name.lower()}-{self.mikrotik_controller.data['interface'][self._uid]['port-mac-address']}" return f"{self._name.lower()}-{self.mikrotik_controller.data['interface'][self._uid]['port-mac-address']}"
@property @property
def available(self) -> bool: def available(self) -> bool:
"""Return if controller is available.""" """Return if controller is available."""
return self.mikrotik_controller.connected() return self.mikrotik_controller.connected()
@property @property
def icon(self): def icon(self):
"""Return the icon.""" """Return the icon."""
@ -145,12 +145,12 @@ class MikrotikControllerPortDeviceTracker(ScannerEntity):
icon = 'mdi:lan-connect' icon = 'mdi:lan-connect'
else: else:
icon = 'mdi:lan-pending' icon = 'mdi:lan-pending'
if not self.mikrotik_controller.data['interface'][self._uid]['enabled']: if not self.mikrotik_controller.data['interface'][self._uid]['enabled']:
icon = 'mdi:lan-disconnect' icon = 'mdi:lan-disconnect'
return icon return icon
@property @property
def device_info(self): def device_info(self):
"""Return a port description for device registry.""" """Return a port description for device registry."""
@ -161,14 +161,14 @@ class MikrotikControllerPortDeviceTracker(ScannerEntity):
"name": self.mikrotik_controller.data['interface'][self._uid]['default-name'], "name": self.mikrotik_controller.data['interface'][self._uid]['default-name'],
} }
return info return info
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the port state attributes.""" """Return the port state attributes."""
attributes = self._attrs attributes = self._attrs
for variable in DEVICE_ATTRIBUTES: for variable in DEVICE_ATTRIBUTES:
if variable in self.mikrotik_controller.data['interface'][self._uid]: if variable in self.mikrotik_controller.data['interface'][self._uid]:
attributes[variable] = self.mikrotik_controller.data['interface'][self._uid][variable] attributes[variable] = self.mikrotik_controller.data['interface'][self._uid][variable]
return attributes return attributes

View file

@ -30,7 +30,7 @@ class MikrotikControllerData():
self.name = name self.name = name
self.hass = hass self.hass = hass
self.config_entry = config_entry self.config_entry = config_entry
self.data = {} self.data = {}
self.data['routerboard'] = {} self.data['routerboard'] = {}
self.data['resource'] = {} self.data['resource'] = {}
@ -39,18 +39,18 @@ class MikrotikControllerData():
self.data['nat'] = {} self.data['nat'] = {}
self.data['fw-update'] = {} self.data['fw-update'] = {}
self.data['script'] = {} self.data['script'] = {}
self.listeners = [] self.listeners = []
self.api = MikrotikAPI(host, username, password, port, use_ssl) self.api = MikrotikAPI(host, username, password, port, use_ssl)
if not self.api.connect(): if not self.api.connect():
self.api = None self.api = None
async_track_time_interval(self.hass, self.force_update, self.option_scan_interval) async_track_time_interval(self.hass, self.force_update, self.option_scan_interval)
async_track_time_interval(self.hass, self.force_fwupdate_check, timedelta(hours=1)) async_track_time_interval(self.hass, self.force_fwupdate_check, timedelta(hours=1))
return return
# --------------------------- # ---------------------------
# force_update # force_update
# --------------------------- # ---------------------------
@ -58,7 +58,7 @@ class MikrotikControllerData():
"""Trigger update by timer""" """Trigger update by timer"""
await self.async_update() await self.async_update()
return return
# --------------------------- # ---------------------------
# force_fwupdate_check # force_fwupdate_check
# --------------------------- # ---------------------------
@ -66,7 +66,7 @@ class MikrotikControllerData():
"""Trigger hourly update by timer""" """Trigger hourly update by timer"""
await self.async_fwupdate_check() await self.async_fwupdate_check()
return return
# --------------------------- # ---------------------------
# option_track_arp # option_track_arp
# --------------------------- # ---------------------------
@ -74,7 +74,7 @@ class MikrotikControllerData():
def option_track_arp(self): def option_track_arp(self):
"""Config entry option to not track ARP.""" """Config entry option to not track ARP."""
return self.config_entry.options.get(CONF_TRACK_ARP, DEFAULT_TRACK_ARP) return self.config_entry.options.get(CONF_TRACK_ARP, DEFAULT_TRACK_ARP)
# --------------------------- # ---------------------------
# option_scan_interval # option_scan_interval
# --------------------------- # ---------------------------
@ -83,7 +83,7 @@ class MikrotikControllerData():
"""Config entry option scan interval.""" """Config entry option scan interval."""
scan_interval = self.config_entry.options.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL) scan_interval = self.config_entry.options.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
return timedelta(seconds=scan_interval) return timedelta(seconds=scan_interval)
# --------------------------- # ---------------------------
# signal_update # signal_update
# --------------------------- # ---------------------------
@ -91,14 +91,14 @@ class MikrotikControllerData():
def signal_update(self): def signal_update(self):
"""Event 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 state""" """Return connected state"""
return self.api.connected() return self.api.connected()
# --------------------------- # ---------------------------
# hwinfo_update # hwinfo_update
# --------------------------- # ---------------------------
@ -107,37 +107,37 @@ class MikrotikControllerData():
self.get_system_routerboard() self.get_system_routerboard()
self.get_system_resource() self.get_system_resource()
return return
# --------------------------- # ---------------------------
# async_fwupdate_check # async_fwupdate_check
# --------------------------- # ---------------------------
async def async_fwupdate_check(self): async def async_fwupdate_check(self):
"""Update Mikrotik data""" """Update Mikrotik data"""
self.get_firmare_update() self.get_firmare_update()
async_dispatcher_send(self.hass, self.signal_update) async_dispatcher_send(self.hass, self.signal_update)
return return
# --------------------------- # ---------------------------
# async_update # async_update
# --------------------------- # ---------------------------
# @Throttle(DEFAULT_SCAN_INTERVAL) # @Throttle(DEFAULT_SCAN_INTERVAL)
async def async_update(self): async def async_update(self):
"""Update Mikrotik 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_interface() self.get_interface()
self.get_arp() self.get_arp()
self.get_nat() self.get_nat()
self.get_system_resource() self.get_system_resource()
self.get_script() self.get_script()
async_dispatcher_send(self.hass, self.signal_update) async_dispatcher_send(self.hass, self.signal_update)
return return
# --------------------------- # ---------------------------
# async_reset # async_reset
# --------------------------- # ---------------------------
@ -145,24 +145,24 @@ class MikrotikControllerData():
"""Reset dispatchers""" """Reset dispatchers"""
for unsub_dispatcher in self.listeners: for unsub_dispatcher in self.listeners:
unsub_dispatcher() unsub_dispatcher()
self.listeners = [] self.listeners = []
return True return True
# --------------------------- # ---------------------------
# 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""" """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""" """Run script using Mikrotik API"""
return self.api.run_script(name) return self.api.run_script(name)
# --------------------------- # ---------------------------
# get_interface # get_interface
# --------------------------- # ---------------------------
@ -172,11 +172,11 @@ class MikrotikControllerData():
for iface in ifaces: for iface in ifaces:
if 'default-name' not in iface: if 'default-name' not in iface:
continue continue
uid = iface['default-name'] uid = iface['default-name']
if uid not in self.data['interface']: if uid not in self.data['interface']:
self.data['interface'][uid] = {} self.data['interface'][uid] = {}
self.data['interface'][uid]['default-name'] = iface['default-name'] self.data['interface'][uid]['default-name'] = iface['default-name']
self.data['interface'][uid]['name'] = iface['name'] if 'name' in iface else iface['default-name'] self.data['interface'][uid]['name'] = iface['name'] if 'name' in iface else iface['default-name']
self.data['interface'][uid]['type'] = iface['type'] if 'type' in iface else "unknown" self.data['interface'][uid]['type'] = iface['type'] if 'type' in iface else "unknown"
@ -191,15 +191,15 @@ class MikrotikControllerData():
self.data['interface'][uid]['tx-byte'] = iface['tx-byte'] if 'tx-byte' in iface else "" self.data['interface'][uid]['tx-byte'] = iface['tx-byte'] if 'tx-byte' in iface else ""
self.data['interface'][uid]['tx-queue-drop'] = iface['tx-queue-drop'] if 'tx-queue-drop' in iface else "" self.data['interface'][uid]['tx-queue-drop'] = iface['tx-queue-drop'] if 'tx-queue-drop' in iface else ""
self.data['interface'][uid]['actual-mtu'] = iface['actual-mtu'] if 'actual-mtu' in iface else "" self.data['interface'][uid]['actual-mtu'] = iface['actual-mtu'] if 'actual-mtu' in iface else ""
if 'client-ip-address' not in self.data['interface'][uid]: if 'client-ip-address' not in self.data['interface'][uid]:
self.data['interface'][uid]['client-ip-address'] = "" self.data['interface'][uid]['client-ip-address'] = ""
if 'client-mac-address' not in self.data['interface'][uid]: if 'client-mac-address' not in self.data['interface'][uid]:
self.data['interface'][uid]['client-mac-address'] = "" self.data['interface'][uid]['client-mac-address'] = ""
return return
# --------------------------- # ---------------------------
# get_arp # get_arp
# --------------------------- # ---------------------------
@ -211,7 +211,7 @@ class MikrotikControllerData():
self.data['interface'][uid]['client-ip-address'] = "disabled" self.data['interface'][uid]['client-ip-address'] = "disabled"
self.data['interface'][uid]['client-mac-address'] = "disabled" self.data['interface'][uid]['client-mac-address'] = "disabled"
return False return False
mac2ip = {} mac2ip = {}
bridge_used = False bridge_used = False
data = self.api.path("/ip/arp") data = self.api.path("/ip/arp")
@ -219,40 +219,40 @@ class MikrotikControllerData():
# 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 = self.get_iface_from_entry(entry) uid = self.get_iface_from_entry(entry)
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
self.data['arp'][uid]['mac-address'] = "multiple" if 'mac-address' in self.data['arp'][uid] else entry['mac-address'] self.data['arp'][uid]['mac-address'] = "multiple" if 'mac-address' in self.data['arp'][uid] else entry['mac-address']
self.data['arp'][uid]['address'] = "multiple" if 'address' in self.data['arp'][uid] else entry['address'] self.data['arp'][uid]['address'] = "multiple" if 'address' in self.data['arp'][uid] else entry['address']
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 ""
return True return True
# --------------------------- # ---------------------------
# update_bridge_hosts # update_bridge_hosts
# --------------------------- # ---------------------------
@ -263,16 +263,16 @@ class MikrotikControllerData():
# 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 = self.get_iface_from_entry(entry) uid = self.get_iface_from_entry(entry)
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]:
@ -281,12 +281,12 @@ class MikrotikControllerData():
else: else:
self.data['arp'][uid]['mac-address'] = entry['mac-address'] self.data['arp'][uid]['mac-address'] = entry['mac-address']
self.data['arp'][uid]['address'] = "" self.data['arp'][uid]['address'] = ""
if self.data['arp'][uid]['address'] == "" and self.data['arp'][uid]['mac-address'] in mac2ip: if self.data['arp'][uid]['address'] == "" and self.data['arp'][uid]['mac-address'] in mac2ip:
self.data['arp'][uid]['address'] = mac2ip[self.data['arp'][uid]['mac-address']] self.data['arp'][uid]['address'] = mac2ip[self.data['arp'][uid]['mac-address']]
return return
# --------------------------- # ---------------------------
# get_iface_from_entry # get_iface_from_entry
# --------------------------- # ---------------------------
@ -297,9 +297,9 @@ class MikrotikControllerData():
if self.data['interface'][ifacename]['name'] == entry['interface']: if self.data['interface'][ifacename]['name'] == entry['interface']:
uid = self.data['interface'][ifacename]['default-name'] uid = self.data['interface'][ifacename]['default-name']
break break
return uid return uid
# --------------------------- # ---------------------------
# get_nat # get_nat
# --------------------------- # ---------------------------
@ -309,11 +309,11 @@ class MikrotikControllerData():
for entry in data: for entry in data:
if entry['action'] != 'dst-nat': if entry['action'] != 'dst-nat':
continue continue
uid = entry['.id'] uid = entry['.id']
if uid not in self.data['nat']: if uid not in self.data['nat']:
self.data['nat'][uid] = {} self.data['nat'][uid] = {}
self.data['nat'][uid]['name'] = entry['protocol'] + ':' + str(entry['dst-port']) self.data['nat'][uid]['name'] = entry['protocol'] + ':' + str(entry['dst-port'])
self.data['nat'][uid]['protocol'] = entry['protocol'] if 'protocol' in entry else "" self.data['nat'][uid]['protocol'] = entry['protocol'] if 'protocol' in entry else ""
self.data['nat'][uid]['dst-port'] = entry['dst-port'] if 'dst-port' in entry else "" self.data['nat'][uid]['dst-port'] = entry['dst-port'] if 'dst-port' in entry else ""
@ -324,9 +324,9 @@ class MikrotikControllerData():
self.data['nat'][uid]['enabled'] = True self.data['nat'][uid]['enabled'] = True
if 'disabled' in entry and entry['disabled']: if 'disabled' in entry and entry['disabled']:
self.data['nat'][uid]['enabled'] = False self.data['nat'][uid]['enabled'] = False
return return
# --------------------------- # ---------------------------
# get_system_routerboard # get_system_routerboard
# --------------------------- # ---------------------------
@ -338,9 +338,9 @@ class MikrotikControllerData():
self.data['routerboard']['model'] = entry['model'] if 'model' in entry else "unknown" self.data['routerboard']['model'] = entry['model'] if 'model' in entry else "unknown"
self.data['routerboard']['serial-number'] = entry['serial-number'] if 'serial-number' in entry else "unknown" self.data['routerboard']['serial-number'] = entry['serial-number'] if 'serial-number' in entry else "unknown"
self.data['routerboard']['firmware'] = entry['current-firmware'] if 'current-firmware' in entry else "unknown" self.data['routerboard']['firmware'] = entry['current-firmware'] if 'current-firmware' in entry else "unknown"
return return
# --------------------------- # ---------------------------
# get_system_resource # get_system_resource
# --------------------------- # ---------------------------
@ -357,14 +357,14 @@ class MikrotikControllerData():
self.data['resource']['memory-usage'] = round(((entry['total-memory'] - entry['free-memory']) / entry['total-memory']) * 100) self.data['resource']['memory-usage'] = round(((entry['total-memory'] - entry['free-memory']) / entry['total-memory']) * 100)
else: else:
self.data['resource']['memory-usage'] = "unknown" self.data['resource']['memory-usage'] = "unknown"
if 'free-hdd-space' in entry and 'total-hdd-space' in entry: if 'free-hdd-space' in entry and 'total-hdd-space' in entry:
self.data['resource']['hdd-usage'] = round(((entry['total-hdd-space'] - entry['free-hdd-space']) / entry['total-hdd-space']) * 100) self.data['resource']['hdd-usage'] = round(((entry['total-hdd-space'] - entry['free-hdd-space']) / entry['total-hdd-space']) * 100)
else: else:
self.data['resource']['hdd-usage'] = "unknown" self.data['resource']['hdd-usage'] = "unknown"
return return
# --------------------------- # ---------------------------
# get_system_routerboard # get_system_routerboard
# --------------------------- # ---------------------------
@ -376,9 +376,9 @@ class MikrotikControllerData():
self.data['fw-update']['channel'] = entry['channel'] if 'channel' in entry else "unknown" self.data['fw-update']['channel'] = entry['channel'] if 'channel' in entry else "unknown"
self.data['fw-update']['installed-version'] = entry['installed-version'] if 'installed-version' in entry else "unknown" self.data['fw-update']['installed-version'] = entry['installed-version'] if 'installed-version' in entry else "unknown"
self.data['fw-update']['latest-version'] = entry['latest-version'] if 'latest-version' in entry else "unknown" self.data['fw-update']['latest-version'] = entry['latest-version'] if 'latest-version' in entry else "unknown"
return return
# --------------------------- # ---------------------------
# get_script # get_script
# --------------------------- # ---------------------------
@ -388,13 +388,13 @@ class MikrotikControllerData():
for entry in data: for entry in data:
if 'name' not in entry: if 'name' not in entry:
continue continue
uid = entry['name'] uid = entry['name']
if uid not in self.data['script']: if uid not in self.data['script']:
self.data['script'][uid] = {} self.data['script'][uid] = {}
self.data['script'][uid]['name'] = entry['name'] self.data['script'][uid]['name'] = entry['name']
self.data['script'][uid]['last-started'] = entry['last-started'] if 'last-started' in entry else "unknown" self.data['script'][uid]['last-started'] = entry['last-started'] if 'last-started' in entry else "unknown"
self.data['script'][uid]['run-count'] = entry['run-count'] if 'run-count' in entry else "unknown" self.data['script'][uid]['run-count'] = entry['run-count'] if 'run-count' in entry else "unknown"
return return

View file

@ -11,7 +11,7 @@ _LOGGER = logging.getLogger(__name__)
# --------------------------- # ---------------------------
class MikrotikAPI: class MikrotikAPI:
"""Handle all communication with the Mikrotik API.""" """Handle all communication with the Mikrotik API."""
def __init__(self, host, username, password, port=0, use_ssl=True, login_method="plain", encoding="utf-8"): def __init__(self, host, username, password, port=0, use_ssl=True, login_method="plain", encoding="utf-8"):
"""Initialize the Mikrotik Client.""" """Initialize the Mikrotik Client."""
self._host = host self._host = host
@ -22,15 +22,15 @@ class MikrotikAPI:
self._login_method = login_method self._login_method = login_method
self._encoding = encoding self._encoding = encoding
self._ssl_wrapper = None self._ssl_wrapper = None
self._connection = None self._connection = None
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
# --------------------------- # ---------------------------
# connect # connect
# --------------------------- # ---------------------------
@ -38,13 +38,13 @@ class MikrotikAPI:
"""Connect to Mikrotik device.""" """Connect to Mikrotik device."""
self.error = "" self.error = ""
self._connected = False self._connected = False
kwargs = { kwargs = {
"encoding": self._encoding, "encoding": self._encoding,
"login_methods": self._login_method, "login_methods": self._login_method,
"port": self._port, "port": self._port,
} }
if self._use_ssl: if self._use_ssl:
if self._ssl_wrapper is None: if self._ssl_wrapper is None:
ssl_context = ssl.create_default_context() ssl_context = ssl.create_default_context()
@ -52,7 +52,7 @@ class MikrotikAPI:
ssl_context.verify_mode = ssl.CERT_NONE ssl_context.verify_mode = ssl.CERT_NONE
self._ssl_wrapper = ssl_context.wrap_socket self._ssl_wrapper = ssl_context.wrap_socket
kwargs["ssl_wrapper"] = self._ssl_wrapper kwargs["ssl_wrapper"] = self._ssl_wrapper
try: try:
self._connection = librouteros.connect(self._host, self._username, self._password, **kwargs) self._connection = librouteros.connect(self._host, self._username, self._password, **kwargs)
except ( except (
@ -69,9 +69,9 @@ class MikrotikAPI:
else: else:
_LOGGER.info("Mikrotik Connected to %s", self._host) _LOGGER.info("Mikrotik Connected to %s", self._host)
self._connected = True self._connected = True
return self._connected return self._connected
# --------------------------- # ---------------------------
# error_to_strings # error_to_strings
# --------------------------- # ---------------------------
@ -80,16 +80,16 @@ class MikrotikAPI:
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"
return return
# --------------------------- # ---------------------------
# connected # connected
# --------------------------- # ---------------------------
def connected(self): def connected(self):
"""Return connected boolean.""" """Return connected boolean."""
return self._connected return self._connected
# --------------------------- # ---------------------------
# path # path
# --------------------------- # ---------------------------
@ -98,7 +98,7 @@ class MikrotikAPI:
if not self._connected or not self._connection: if not self._connected or not self._connection:
if not self.connect(): if not self.connect():
return None return None
try: try:
response = self._connection.path(path) response = self._connection.path(path)
tuple(response) tuple(response)
@ -115,9 +115,9 @@ class MikrotikAPI:
) as api_error: ) as api_error:
_LOGGER.error("Mikrotik %s connection error %s", self._host, api_error) _LOGGER.error("Mikrotik %s connection error %s", self._host, api_error)
return None return None
return response if response else None return response if response else None
# --------------------------- # ---------------------------
# update # update
# --------------------------- # ---------------------------
@ -126,23 +126,23 @@ class MikrotikAPI:
response = self.path(path) response = self.path(path)
if response is None: if response is None:
return False return False
for tmp in response: for tmp in response:
if param not in tmp: if param not in tmp:
continue continue
if tmp[param] != value: if tmp[param] != value:
continue continue
params = { params = {
'.id': tmp['.id'], '.id': tmp['.id'],
mod_param: mod_value mod_param: mod_value
} }
response.update(**params) response.update(**params)
return True return True
# --------------------------- # ---------------------------
# run_script # run_script
# --------------------------- # ---------------------------
@ -151,15 +151,15 @@ class MikrotikAPI:
response = self.path('/system/script') response = self.path('/system/script')
if response is None: if response is None:
return False return False
for tmp in response: for tmp in response:
if 'name' not in tmp: if 'name' not in tmp:
continue continue
if tmp['name'] != name: if tmp['name'] != name:
continue continue
run = response('run', **{'.id': tmp['.id']}) run = response('run', **{'.id': tmp['.id']})
tuple(run) tuple(run)
return True return True

View file

@ -64,16 +64,16 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
name = config_entry.data[CONF_NAME] name = config_entry.data[CONF_NAME]
mikrotik_controller = hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id] mikrotik_controller = hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id]
sensors = {} sensors = {}
@callback @callback
def update_controller(): def update_controller():
"""Update the values of the controller.""" """Update the values of the controller."""
update_items(name, mikrotik_controller, async_add_entities, sensors) update_items(name, mikrotik_controller, async_add_entities, sensors)
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)
) )
update_controller() update_controller()
return return
@ -85,42 +85,42 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
def update_items(name, mikrotik_controller, async_add_entities, sensors): def update_items(name, mikrotik_controller, async_add_entities, sensors):
"""Update sensor state from the controller.""" """Update sensor state from the controller."""
new_sensors = [] new_sensors = []
for sensor in SENSOR_TYPES: for sensor in SENSOR_TYPES:
item_id = name + "-" + sensor item_id = name + "-" + sensor
if item_id in sensors: if item_id in sensors:
if sensors[item_id].enabled: if sensors[item_id].enabled:
sensors[item_id].async_schedule_update_ha_state() sensors[item_id].async_schedule_update_ha_state()
continue continue
sensors[item_id] = MikrotikControllerSensor(mikrotik_controller=mikrotik_controller, name=name, kind=sensor) sensors[item_id] = MikrotikControllerSensor(mikrotik_controller=mikrotik_controller, name=name, kind=sensor)
new_sensors.append(sensors[item_id]) new_sensors.append(sensors[item_id])
if new_sensors: if new_sensors:
async_add_entities(new_sensors, True) async_add_entities(new_sensors, True)
return return
# --------------------------- # ---------------------------
# MikrotikControllerSensor # MikrotikControllerSensor
# --------------------------- # ---------------------------
class MikrotikControllerSensor(Entity): class MikrotikControllerSensor(Entity):
"""Define an Mikrotik Controller sensor.""" """Define an Mikrotik Controller sensor."""
def __init__(self, mikrotik_controller, name, kind, uid=''): def __init__(self, mikrotik_controller, name, kind, uid=''):
"""Initialize.""" """Initialize."""
self.mikrotik_controller = mikrotik_controller self.mikrotik_controller = mikrotik_controller
self._name = name self._name = name
self.kind = kind self.kind = kind
self.uid = uid self.uid = uid
self._device_class = None self._device_class = None
self._state = None self._state = None
self._icon = None self._icon = None
self._unit_of_measurement = None self._unit_of_measurement = None
self._attrs = {ATTR_ATTRIBUTION: ATTRIBUTION} self._attrs = {ATTR_ATTRIBUTION: ATTRIBUTION}
@property @property
def name(self): def name(self):
"""Return the name.""" """Return the name."""
@ -134,7 +134,7 @@ class MikrotikControllerSensor(Entity):
val = "unknown" val = "unknown"
if SENSOR_TYPES[self.kind][ATTR_PATH] in self.mikrotik_controller.data and SENSOR_TYPES[self.kind][ATTR_ATTR] in self.mikrotik_controller.data[SENSOR_TYPES[self.kind][ATTR_PATH]]: if SENSOR_TYPES[self.kind][ATTR_PATH] in self.mikrotik_controller.data and SENSOR_TYPES[self.kind][ATTR_ATTR] in self.mikrotik_controller.data[SENSOR_TYPES[self.kind][ATTR_PATH]]:
val = self.mikrotik_controller.data[SENSOR_TYPES[self.kind][ATTR_PATH]][SENSOR_TYPES[self.kind][ATTR_ATTR]] val = self.mikrotik_controller.data[SENSOR_TYPES[self.kind][ATTR_PATH]][SENSOR_TYPES[self.kind][ATTR_ATTR]]
return val return val
@property @property
@ -169,7 +169,7 @@ class MikrotikControllerSensor(Entity):
def available(self): def available(self):
"""Return True if entity is available.""" """Return True if entity is available."""
return bool(self.mikrotik_controller.data) return bool(self.mikrotik_controller.data)
@property @property
def device_info(self): def device_info(self):
"""Return a port description for device registry.""" """Return a port description for device registry."""
@ -185,7 +185,7 @@ class MikrotikControllerSensor(Entity):
"""Synchronize state with controller.""" """Synchronize state with controller."""
# await self.mikrotik_controller.async_update() # await self.mikrotik_controller.async_update()
return return
async def async_added_to_hass(self): async def async_added_to_hass(self):
"""Port entity created.""" """Port entity created."""
_LOGGER.debug("New sensor %s (%s)", self._name, self.kind) _LOGGER.debug("New sensor %s (%s)", self._name, self.kind)

View file

@ -58,16 +58,16 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
name = config_entry.data[CONF_NAME] name = config_entry.data[CONF_NAME]
mikrotik_controller = hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id] mikrotik_controller = hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id]
switches = {} switches = {}
@callback @callback
def update_controller(): def update_controller():
"""Update the values of the controller.""" """Update the values of the controller."""
update_items(name, mikrotik_controller, async_add_entities, switches) update_items(name, mikrotik_controller, async_add_entities, switches)
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)
) )
update_controller() update_controller()
return return
@ -79,7 +79,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
def update_items(name, mikrotik_controller, async_add_entities, switches): def update_items(name, mikrotik_controller, async_add_entities, switches):
"""Update device switch state from the controller.""" """Update device switch state from the controller."""
new_switches = [] new_switches = []
# Add interface switches # Add interface switches
for uid in mikrotik_controller.data['interface']: for uid in mikrotik_controller.data['interface']:
if mikrotik_controller.data['interface'][uid]['type'] == "ether": if mikrotik_controller.data['interface'][uid]['type'] == "ether":
@ -88,10 +88,10 @@ def update_items(name, mikrotik_controller, async_add_entities, switches):
if switches[item_id].enabled: if switches[item_id].enabled:
switches[item_id].async_schedule_update_ha_state() switches[item_id].async_schedule_update_ha_state()
continue continue
switches[item_id] = MikrotikControllerPortSwitch(name, uid, mikrotik_controller) switches[item_id] = MikrotikControllerPortSwitch(name, uid, mikrotik_controller)
new_switches.append(switches[item_id]) new_switches.append(switches[item_id])
# Add NAT switches # Add NAT switches
for uid in mikrotik_controller.data['nat']: for uid in mikrotik_controller.data['nat']:
item_id = name + "-nat-" + mikrotik_controller.data['nat'][uid]['name'] item_id = name + "-nat-" + mikrotik_controller.data['nat'][uid]['name']
@ -99,10 +99,10 @@ def update_items(name, mikrotik_controller, async_add_entities, switches):
if switches[item_id].enabled: if switches[item_id].enabled:
switches[item_id].async_schedule_update_ha_state() switches[item_id].async_schedule_update_ha_state()
continue continue
switches[item_id] = MikrotikControllerNATSwitch(name, uid, mikrotik_controller) switches[item_id] = MikrotikControllerNATSwitch(name, uid, mikrotik_controller)
new_switches.append(switches[item_id]) new_switches.append(switches[item_id])
# Add script switches # Add script switches
for uid in mikrotik_controller.data['script']: for uid in mikrotik_controller.data['script']:
item_id = name + "-script-" + mikrotik_controller.data['script'][uid]['name'] item_id = name + "-script-" + mikrotik_controller.data['script'][uid]['name']
@ -110,13 +110,13 @@ def update_items(name, mikrotik_controller, async_add_entities, switches):
if switches[item_id].enabled: if switches[item_id].enabled:
switches[item_id].async_schedule_update_ha_state() switches[item_id].async_schedule_update_ha_state()
continue continue
switches[item_id] = MikrotikControllerScriptSwitch(name, uid, mikrotik_controller) switches[item_id] = MikrotikControllerScriptSwitch(name, uid, mikrotik_controller)
new_switches.append(switches[item_id]) new_switches.append(switches[item_id])
if new_switches: if new_switches:
async_add_entities(new_switches) async_add_entities(new_switches)
return return
@ -125,23 +125,23 @@ def update_items(name, mikrotik_controller, async_add_entities, switches):
# --------------------------- # ---------------------------
class MikrotikControllerSwitch(SwitchDevice, RestoreEntity): class MikrotikControllerSwitch(SwitchDevice, RestoreEntity):
"""Representation of a network port switch.""" """Representation of a network port switch."""
def __init__(self, name, uid, mikrotik_controller): def __init__(self, name, uid, mikrotik_controller):
"""Set up switch.""" """Set up switch."""
self._name = name self._name = name
self._uid = uid self._uid = uid
self.mikrotik_controller = mikrotik_controller self.mikrotik_controller = mikrotik_controller
async def async_added_to_hass(self): async def async_added_to_hass(self):
"""Switch entity created.""" """Switch entity created."""
_LOGGER.debug("New switch %s (%s)", self._name, self._uid) _LOGGER.debug("New switch %s (%s)", self._name, self._uid)
return return
async def async_update(self): async def async_update(self):
"""Synchronize state with controller.""" """Synchronize state with controller."""
# await self.mikrotik_controller.async_update() # await self.mikrotik_controller.async_update()
return return
@property @property
def available(self) -> bool: def available(self) -> bool:
"""Return if controller is available.""" """Return if controller is available."""
@ -153,30 +153,30 @@ class MikrotikControllerSwitch(SwitchDevice, RestoreEntity):
# --------------------------- # ---------------------------
class MikrotikControllerPortSwitch(MikrotikControllerSwitch): class MikrotikControllerPortSwitch(MikrotikControllerSwitch):
"""Representation of a network port switch.""" """Representation of a network port switch."""
def __init__(self, name, uid, mikrotik_controller): def __init__(self, name, uid, mikrotik_controller):
"""Set up tracked port.""" """Set up tracked port."""
super().__init__(name, uid, mikrotik_controller) super().__init__(name, uid, mikrotik_controller)
self._attrs = { self._attrs = {
ATTR_ATTRIBUTION: ATTRIBUTION, ATTR_ATTRIBUTION: ATTRIBUTION,
} }
async def async_added_to_hass(self): async def async_added_to_hass(self):
"""Port entity created.""" """Port entity created."""
_LOGGER.debug("New port switch %s (%s)", self._name, self.mikrotik_controller.data['interface'][self._uid]['port-mac-address']) _LOGGER.debug("New port switch %s (%s)", self._name, self.mikrotik_controller.data['interface'][self._uid]['port-mac-address'])
return return
@property @property
def name(self) -> str: def name(self) -> str:
"""Return the name of the port.""" """Return the name of the port."""
return f"{self._name} port {self.mikrotik_controller.data['interface'][self._uid]['default-name']}" return f"{self._name} port {self.mikrotik_controller.data['interface'][self._uid]['default-name']}"
@property @property
def unique_id(self) -> str: def unique_id(self) -> str:
"""Return a unique identifier for this port.""" """Return a unique identifier for this port."""
return f"{self._name.lower()}-enable_switch-{self.mikrotik_controller.data['interface'][self._uid]['port-mac-address']}" return f"{self._name.lower()}-enable_switch-{self.mikrotik_controller.data['interface'][self._uid]['port-mac-address']}"
@property @property
def icon(self): def icon(self):
"""Return the icon.""" """Return the icon."""
@ -184,12 +184,12 @@ class MikrotikControllerPortSwitch(MikrotikControllerSwitch):
icon = 'mdi:lan-connect' icon = 'mdi:lan-connect'
else: else:
icon = 'mdi:lan-pending' icon = 'mdi:lan-pending'
if not self.mikrotik_controller.data['interface'][self._uid]['enabled']: if not self.mikrotik_controller.data['interface'][self._uid]['enabled']:
icon = 'mdi:lan-disconnect' icon = 'mdi:lan-disconnect'
return icon return icon
@property @property
def device_info(self): def device_info(self):
"""Return a port description for device registry.""" """Return a port description for device registry."""
@ -200,16 +200,16 @@ class MikrotikControllerPortSwitch(MikrotikControllerSwitch):
"name": self.mikrotik_controller.data['interface'][self._uid]['default-name'], "name": self.mikrotik_controller.data['interface'][self._uid]['default-name'],
} }
return info return info
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the port state attributes.""" """Return the port state attributes."""
attributes = self._attrs attributes = self._attrs
for variable in DEVICE_ATTRIBUTES_IFACE: for variable in DEVICE_ATTRIBUTES_IFACE:
if variable in self.mikrotik_controller.data['interface'][self._uid]: if variable in self.mikrotik_controller.data['interface'][self._uid]:
attributes[variable] = self.mikrotik_controller.data['interface'][self._uid][variable] attributes[variable] = self.mikrotik_controller.data['interface'][self._uid][variable]
return attributes return attributes
async def async_turn_on(self): async def async_turn_on(self):
@ -245,30 +245,30 @@ class MikrotikControllerPortSwitch(MikrotikControllerSwitch):
# --------------------------- # ---------------------------
class MikrotikControllerNATSwitch(MikrotikControllerSwitch): class MikrotikControllerNATSwitch(MikrotikControllerSwitch):
"""Representation of a NAT switch.""" """Representation of a NAT switch."""
def __init__(self, name, uid, mikrotik_controller): def __init__(self, name, uid, mikrotik_controller):
"""Set up NAT switch.""" """Set up NAT switch."""
super().__init__(name, uid, mikrotik_controller) super().__init__(name, uid, mikrotik_controller)
self._attrs = { self._attrs = {
ATTR_ATTRIBUTION: ATTRIBUTION, ATTR_ATTRIBUTION: ATTRIBUTION,
} }
async def async_added_to_hass(self): async def async_added_to_hass(self):
"""NAT switch entity created.""" """NAT switch entity created."""
_LOGGER.debug("New port switch %s (%s)", self._name, self.mikrotik_controller.data['nat'][self._uid]['name']) _LOGGER.debug("New port switch %s (%s)", self._name, self.mikrotik_controller.data['nat'][self._uid]['name'])
return return
@property @property
def name(self) -> str: def name(self) -> str:
"""Return the name of the NAT switch.""" """Return the name of the NAT switch."""
return f"{self._name} NAT {self.mikrotik_controller.data['nat'][self._uid]['name']}" return f"{self._name} NAT {self.mikrotik_controller.data['nat'][self._uid]['name']}"
@property @property
def unique_id(self) -> str: def unique_id(self) -> str:
"""Return a unique identifier for this NAT switch.""" """Return a unique identifier for this NAT switch."""
return f"{self._name.lower()}-nat_switch-{self.mikrotik_controller.data['nat'][self._uid]['name']}" return f"{self._name.lower()}-nat_switch-{self.mikrotik_controller.data['nat'][self._uid]['name']}"
@property @property
def icon(self): def icon(self):
"""Return the icon.""" """Return the icon."""
@ -276,9 +276,9 @@ class MikrotikControllerNATSwitch(MikrotikControllerSwitch):
icon = 'mdi:network-off-outline' icon = 'mdi:network-off-outline'
else: else:
icon = 'mdi:network-outline' icon = 'mdi:network-outline'
return icon return icon
@property @property
def device_info(self): def device_info(self):
"""Return a NAT switch description for device registry.""" """Return a NAT switch description for device registry."""
@ -289,16 +289,16 @@ class MikrotikControllerNATSwitch(MikrotikControllerSwitch):
"name": "NAT", "name": "NAT",
} }
return info return info
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the NAT switch state attributes.""" """Return the NAT switch state attributes."""
attributes = self._attrs attributes = self._attrs
for variable in DEVICE_ATTRIBUTES_NAT: for variable in DEVICE_ATTRIBUTES_NAT:
if variable in self.mikrotik_controller.data['nat'][self._uid]: if variable in self.mikrotik_controller.data['nat'][self._uid]:
attributes[variable] = self.mikrotik_controller.data['nat'][self._uid][variable] attributes[variable] = self.mikrotik_controller.data['nat'][self._uid][variable]
return attributes return attributes
async def async_turn_on(self): async def async_turn_on(self):
@ -334,35 +334,35 @@ class MikrotikControllerNATSwitch(MikrotikControllerSwitch):
# --------------------------- # ---------------------------
class MikrotikControllerScriptSwitch(MikrotikControllerSwitch): class MikrotikControllerScriptSwitch(MikrotikControllerSwitch):
"""Representation of a script switch.""" """Representation of a script switch."""
def __init__(self, name, uid, mikrotik_controller): def __init__(self, name, uid, mikrotik_controller):
"""Set up script switch.""" """Set up script switch."""
super().__init__(name, uid, mikrotik_controller) super().__init__(name, uid, mikrotik_controller)
self._attrs = { self._attrs = {
ATTR_ATTRIBUTION: ATTRIBUTION, ATTR_ATTRIBUTION: ATTRIBUTION,
} }
async def async_added_to_hass(self): async def async_added_to_hass(self):
"""Script switch entity created.""" """Script switch entity created."""
_LOGGER.debug("New script switch %s (%s)", self._name, self.mikrotik_controller.data['script'][self._uid]['name']) _LOGGER.debug("New script switch %s (%s)", self._name, self.mikrotik_controller.data['script'][self._uid]['name'])
return return
@property @property
def name(self) -> str: def name(self) -> str:
"""Return the name of the script switch.""" """Return the name of the script switch."""
return f"{self._name} script {self.mikrotik_controller.data['script'][self._uid]['name']}" return f"{self._name} script {self.mikrotik_controller.data['script'][self._uid]['name']}"
@property @property
def unique_id(self) -> str: def unique_id(self) -> str:
"""Return a unique identifier for this script switch.""" """Return a unique identifier for this script switch."""
return f"{self._name.lower()}-script_switch-{self.mikrotik_controller.data['script'][self._uid]['name']}" return f"{self._name.lower()}-script_switch-{self.mikrotik_controller.data['script'][self._uid]['name']}"
@property @property
def icon(self): def icon(self):
"""Return the icon.""" """Return the icon."""
return 'mdi:script-text-outline' return 'mdi:script-text-outline'
@property @property
def device_info(self): def device_info(self):
"""Return a script switch description for device registry.""" """Return a script switch description for device registry."""
@ -373,16 +373,16 @@ class MikrotikControllerScriptSwitch(MikrotikControllerSwitch):
"name": "Scripts", "name": "Scripts",
} }
return info return info
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the script switch state attributes.""" """Return the script switch state attributes."""
attributes = self._attrs attributes = self._attrs
for variable in DEVICE_ATTRIBUTES_SCRIPT: for variable in DEVICE_ATTRIBUTES_SCRIPT:
if variable in self.mikrotik_controller.data['script'][self._uid]: if variable in self.mikrotik_controller.data['script'][self._uid]:
attributes[variable] = self.mikrotik_controller.data['script'][self._uid][variable] attributes[variable] = self.mikrotik_controller.data['script'][self._uid][variable]
return attributes return attributes
async def async_turn_on(self): async def async_turn_on(self):