mirror of
https://github.com/tomaae/homeassistant-mikrotik_router.git
synced 2025-07-23 20:14:30 +02:00
changed string formatting according to development guidelines
This commit is contained in:
parent
7af3ef0bb3
commit
8ec4f9ade5
7 changed files with 31 additions and 39 deletions
|
@ -63,7 +63,7 @@ def update_items(inst, mikrotik_controller, async_add_entities, sensors):
|
||||||
new_sensors = []
|
new_sensors = []
|
||||||
|
|
||||||
for sensor in SENSOR_TYPES:
|
for sensor in SENSOR_TYPES:
|
||||||
item_id = "{}-{}".format(inst, sensor)
|
item_id = f"{inst}-{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()
|
||||||
|
@ -99,7 +99,7 @@ class MikrotikControllerBinarySensor(BinarySensorDevice):
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
"""Return the name."""
|
"""Return the name."""
|
||||||
return "{} {}".format(self._inst, self._type[ATTR_LABEL])
|
return f"{self._inst} {self._type[ATTR_LABEL]}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_state_attributes(self):
|
def device_state_attributes(self):
|
||||||
|
@ -109,7 +109,7 @@ class MikrotikControllerBinarySensor(BinarySensorDevice):
|
||||||
@property
|
@property
|
||||||
def unique_id(self):
|
def unique_id(self):
|
||||||
"""Return a unique_id for this entity."""
|
"""Return a unique_id for this entity."""
|
||||||
return "{}-{}".format(self._inst.lower(), self._sensor.lower())
|
return f"{self._inst.lower()}-{self._sensor.lower()}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def available(self) -> bool:
|
def available(self) -> bool:
|
||||||
|
|
|
@ -80,8 +80,8 @@ def update_items(inst, mikrotik_controller, async_add_entities, 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 = "{}-{}".format(
|
item_id = (
|
||||||
inst, mikrotik_controller.data["interface"][uid]["default-name"]
|
f"{inst}-{mikrotik_controller.data['interface'][uid]['default-name']}"
|
||||||
)
|
)
|
||||||
if item_id in tracked:
|
if item_id in tracked:
|
||||||
if tracked[item_id].enabled:
|
if tracked[item_id].enabled:
|
||||||
|
@ -143,12 +143,12 @@ class MikrotikControllerPortDeviceTracker(ScannerEntity):
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
"""Return the name of the port."""
|
"""Return the name of the port."""
|
||||||
return "{} {}".format(self._inst, self._data["default-name"])
|
return f"{self._inst} {self._data['default-name']}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unique_id(self):
|
def unique_id(self):
|
||||||
"""Return a unique identifier for this port."""
|
"""Return a unique identifier for this port."""
|
||||||
return "{}-{}".format(self._inst.lower(), self._data["port-mac-address"])
|
return f"{self._inst.lower()}-{self._data['port-mac-address']}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def available(self) -> bool:
|
def available(self) -> bool:
|
||||||
|
|
|
@ -267,14 +267,14 @@ def fill_vals_proc(data, uid, vals_proc) -> dict:
|
||||||
if not _value:
|
if not _value:
|
||||||
_value = tmp
|
_value = tmp
|
||||||
else:
|
else:
|
||||||
_value = "{}{}".format(_value, tmp)
|
_value = f"{_value}{tmp}"
|
||||||
|
|
||||||
if "text" in val:
|
if "text" in val:
|
||||||
tmp = val["text"]
|
tmp = val["text"]
|
||||||
if not _value:
|
if not _value:
|
||||||
_value = tmp
|
_value = tmp
|
||||||
else:
|
else:
|
||||||
_value = "{}{}".format(_value, tmp)
|
_value = f"{_value}{tmp}"
|
||||||
|
|
||||||
if _name and _value:
|
if _name and _value:
|
||||||
if uid:
|
if uid:
|
||||||
|
|
|
@ -118,7 +118,7 @@ class MikrotikControllerData:
|
||||||
@property
|
@property
|
||||||
def signal_update(self):
|
def signal_update(self):
|
||||||
"""Event to signal new data."""
|
"""Event to signal new data."""
|
||||||
return "{}-update-{}".format(DOMAIN, self.name)
|
return f"{DOMAIN}-update-{self.name}"
|
||||||
|
|
||||||
# ---------------------------
|
# ---------------------------
|
||||||
# connected
|
# connected
|
||||||
|
|
|
@ -256,7 +256,7 @@ class MikrotikAPI:
|
||||||
|
|
||||||
self.lock.release()
|
self.lock.release()
|
||||||
if not entry_found:
|
if not entry_found:
|
||||||
error = 'Parameter "{}" with value "{}" not found'.format(param, value)
|
error = f'Parameter "{param}" with value "{value}" not found'
|
||||||
raise ApiEntryNotFound(error)
|
raise ApiEntryNotFound(error)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
@ -321,7 +321,7 @@ class MikrotikAPI:
|
||||||
|
|
||||||
self.lock.release()
|
self.lock.release()
|
||||||
if not entry_found:
|
if not entry_found:
|
||||||
error = 'Script "{}" not found'.format(name)
|
error = f'Script "{name}" not found'
|
||||||
raise ApiEntryNotFound(error)
|
raise ApiEntryNotFound(error)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
|
@ -109,7 +109,7 @@ def update_items(inst, mikrotik_controller, async_add_entities, sensors):
|
||||||
|
|
||||||
for sensor in SENSOR_TYPES:
|
for sensor in SENSOR_TYPES:
|
||||||
if "traffic_" not in sensor:
|
if "traffic_" not in sensor:
|
||||||
item_id = "{}-{}".format(inst, sensor)
|
item_id = f"{inst}-{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()
|
||||||
|
@ -123,11 +123,7 @@ def update_items(inst, mikrotik_controller, async_add_entities, sensors):
|
||||||
if "traffic_" in sensor:
|
if "traffic_" in sensor:
|
||||||
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 = "{}-{}-{}".format(
|
item_id = f"{inst}-{sensor}-{mikrotik_controller.data['interface'][uid]['default-name']}"
|
||||||
inst,
|
|
||||||
sensor,
|
|
||||||
mikrotik_controller.data["interface"][uid]["default-name"],
|
|
||||||
)
|
|
||||||
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()
|
||||||
|
@ -169,7 +165,7 @@ class MikrotikControllerSensor(Entity):
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
"""Return the name."""
|
"""Return the name."""
|
||||||
return "{} {}".format(self._inst, self._type[ATTR_LABEL])
|
return f"{self._inst} {self._type[ATTR_LABEL]}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
|
@ -199,7 +195,7 @@ class MikrotikControllerSensor(Entity):
|
||||||
@property
|
@property
|
||||||
def unique_id(self):
|
def unique_id(self):
|
||||||
"""Return a unique_id for this entity."""
|
"""Return a unique_id for this entity."""
|
||||||
return "{}-{}".format(self._inst.lower(), self._sensor.lower())
|
return f"{self._inst.lower()}-{self._sensor.lower()}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unit_of_measurement(self):
|
def unit_of_measurement(self):
|
||||||
|
@ -257,14 +253,12 @@ class MikrotikControllerTrafficSensor(MikrotikControllerSensor):
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
"""Return the name."""
|
"""Return the name."""
|
||||||
return "{} {} {}".format(self._inst, self._data["name"], self._type[ATTR_LABEL])
|
return f"{self._inst} {self._data['name']} {self._type[ATTR_LABEL]}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unique_id(self):
|
def unique_id(self):
|
||||||
"""Return a unique_id for this entity."""
|
"""Return a unique_id for this entity."""
|
||||||
return "{}-{}-{}".format(
|
return f"{self._inst.lower()}-{self._sensor.lower()}-{self._data['default-name'].lower()}"
|
||||||
self._inst.lower(), self._sensor.lower(), self._data["default-name"].lower()
|
|
||||||
)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_info(self):
|
def device_info(self):
|
||||||
|
|
|
@ -103,9 +103,7 @@ def update_items(inst, mikrotik_controller, async_add_entities, switches):
|
||||||
],
|
],
|
||||||
):
|
):
|
||||||
for uid in mikrotik_controller.data[sid]:
|
for uid in mikrotik_controller.data[sid]:
|
||||||
item_id = "{}-{}-{}".format(
|
item_id = f"{inst}-{sid}-{mikrotik_controller.data[sid][uid]['name']}"
|
||||||
inst, sid, mikrotik_controller.data[sid][uid]["name"]
|
|
||||||
)
|
|
||||||
if item_id in switches:
|
if item_id in 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()
|
||||||
|
@ -170,14 +168,12 @@ class MikrotikControllerPortSwitch(MikrotikControllerSwitch):
|
||||||
@property
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
"""Return the name of the port."""
|
"""Return the name of the port."""
|
||||||
return "{} port {}".format(self._inst, self._data["default-name"])
|
return f"{self._inst} port {self._data['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 "{}-enable_switch-{}".format(
|
return f"{self._inst.lower()}-enable_switch-{self._data['port-mac-address']}"
|
||||||
self._inst.lower(), self._data["port-mac-address"]
|
|
||||||
)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def icon(self):
|
def icon(self):
|
||||||
|
@ -262,12 +258,12 @@ class MikrotikControllerNATSwitch(MikrotikControllerSwitch):
|
||||||
@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 "{} NAT {}".format(self._inst, self._data["name"])
|
return f"{self._inst} NAT {self._data['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 "{}-nat_switch-{}".format(self._inst.lower(), self._data["name"])
|
return f"{self._inst.lower()}-nat_switch-{self._data['name']}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def icon(self):
|
def icon(self):
|
||||||
|
@ -315,8 +311,9 @@ class MikrotikControllerNATSwitch(MikrotikControllerSwitch):
|
||||||
param = ".id"
|
param = ".id"
|
||||||
value = None
|
value = None
|
||||||
for uid in self._ctrl.data["nat"]:
|
for uid in self._ctrl.data["nat"]:
|
||||||
if self._ctrl.data["nat"][uid]["name"] == "{}:{}".format(
|
if (
|
||||||
self._data["protocol"], self._data["dst-port"]
|
self._ctrl.data["nat"][uid]["name"]
|
||||||
|
== f"{self._data['protocol']}:{self._data['dst-port']}"
|
||||||
):
|
):
|
||||||
value = self._ctrl.data["nat"][uid][".id"]
|
value = self._ctrl.data["nat"][uid][".id"]
|
||||||
|
|
||||||
|
@ -331,8 +328,9 @@ class MikrotikControllerNATSwitch(MikrotikControllerSwitch):
|
||||||
param = ".id"
|
param = ".id"
|
||||||
value = None
|
value = None
|
||||||
for uid in self._ctrl.data["nat"]:
|
for uid in self._ctrl.data["nat"]:
|
||||||
if self._ctrl.data["nat"][uid]["name"] == "{}:{}".format(
|
if (
|
||||||
self._data["protocol"], self._data["dst-port"]
|
self._ctrl.data["nat"][uid]["name"]
|
||||||
|
== f"{self._data['protocol']}:{self._data['dst-port']}"
|
||||||
):
|
):
|
||||||
value = self._ctrl.data["nat"][uid][".id"]
|
value = self._ctrl.data["nat"][uid][".id"]
|
||||||
|
|
||||||
|
@ -369,12 +367,12 @@ class MikrotikControllerScriptSwitch(MikrotikControllerSwitch):
|
||||||
@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 "{} script {}".format(self._inst, self._data["name"])
|
return f"{self._inst} script {self._data['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 "{}-script_switch-{}".format(self._inst.lower(), self._data["name"])
|
return f"{self._inst.lower()}-script_switch-{self._data['name']}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def icon(self):
|
def icon(self):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue