redo nat switches

This commit is contained in:
Tomaae 2022-02-04 20:34:51 +01:00
parent 18f2e56691
commit 6206c84cc9
2 changed files with 25 additions and 51 deletions

View file

@ -134,11 +134,10 @@ def update_items(inst, mikrotik_controller, async_add_entities, switches):
# Switch type name # Switch type name
[ [
"interface", "interface",
"nat",
], ],
# Entity function # Entity function
[ [MikrotikControllerPortSwitch, MikrotikControllerNATSwitch],
MikrotikControllerPortSwitch,
],
): ):
uid_switch = SWITCH_TYPES[switch] uid_switch = SWITCH_TYPES[switch]
for uid in mikrotik_controller.data[SWITCH_TYPES[switch].data_path]: for uid in mikrotik_controller.data[SWITCH_TYPES[switch].data_path]:
@ -460,10 +459,6 @@ class MikrotikControllerPortSwitch(MikrotikControllerSwitch):
class MikrotikControllerNATSwitch(MikrotikControllerSwitch): class MikrotikControllerNATSwitch(MikrotikControllerSwitch):
"""Representation of a NAT switch.""" """Representation of a NAT switch."""
def __init__(self, inst, uid, mikrotik_controller, sid_data):
"""Initialize."""
super().__init__(inst, uid, mikrotik_controller, sid_data)
@property @property
def name(self) -> str: def name(self) -> str:
"""Return the name.""" """Return the name."""
@ -472,43 +467,9 @@ class MikrotikControllerNATSwitch(MikrotikControllerSwitch):
return f"{self._inst} NAT {self._data['name']}" return f"{self._inst} NAT {self._data['name']}"
@property
def unique_id(self) -> str:
"""Return a unique id for this entity."""
return f"{self._inst.lower()}-enable_nat-{self._data['uniq-id']}"
@property
def icon(self) -> str:
"""Return the icon."""
if not self._data["enabled"]:
icon = "mdi:network-off-outline"
else:
icon = "mdi:network-outline"
return icon
@property
def device_info(self) -> Dict[str, Any]:
"""Return a description for device registry."""
info = {
"identifiers": {
(
DOMAIN,
"serial-number",
f"{self._ctrl.data['routerboard']['serial-number']}",
"switch",
"NAT",
)
},
"manufacturer": self._ctrl.data["resource"]["platform"],
"model": self._ctrl.data["resource"]["board-name"],
"name": f"{self._inst} NAT",
}
return info
async def async_turn_on(self) -> None: async def async_turn_on(self) -> None:
"""Turn on the switch.""" """Turn on the switch."""
path = "/ip/firewall/nat" path = self.entity_description.data_switch_path
param = ".id" param = ".id"
value = None value = None
for uid in self._ctrl.data["nat"]: for uid in self._ctrl.data["nat"]:
@ -519,14 +480,13 @@ class MikrotikControllerNATSwitch(MikrotikControllerSwitch):
): ):
value = self._ctrl.data["nat"][uid][".id"] value = self._ctrl.data["nat"][uid][".id"]
mod_param = "disabled" mod_param = self.entity_description.data_switch_parameter
mod_value = False self._ctrl.set_value(path, param, value, mod_param, False)
self._ctrl.set_value(path, param, value, mod_param, mod_value)
await self._ctrl.force_update() await self._ctrl.force_update()
async def async_turn_off(self) -> None: async def async_turn_off(self) -> None:
"""Turn off the switch.""" """Turn off the switch."""
path = "/ip/firewall/nat" path = self.entity_description.data_switch_path
param = ".id" param = ".id"
value = None value = None
for uid in self._ctrl.data["nat"]: for uid in self._ctrl.data["nat"]:
@ -537,9 +497,8 @@ class MikrotikControllerNATSwitch(MikrotikControllerSwitch):
): ):
value = self._ctrl.data["nat"][uid][".id"] value = self._ctrl.data["nat"][uid][".id"]
mod_param = "disabled" mod_param = self.entity_description.data_switch_parameter
mod_value = True self._ctrl.set_value(path, param, value, mod_param, True)
self._ctrl.set_value(path, param, value, mod_param, mod_value)
await self._ctrl.async_update() await self._ctrl.async_update()

View file

@ -147,7 +147,6 @@ class MikrotikSwitchEntityDescription(SwitchEntityDescription):
ha_connection: str = "" ha_connection: str = ""
ha_connection_value: str = "" ha_connection_value: str = ""
data_path: str = "" data_path: str = ""
data_attribute: str = ""
data_is_on: str = "enabled" data_is_on: str = "enabled"
data_switch_path: str = "" data_switch_path: str = ""
data_switch_parameter: str = "disabled" data_switch_parameter: str = "disabled"
@ -169,11 +168,27 @@ SWITCH_TYPES = {
ha_connection=CONNECTION_NETWORK_MAC, ha_connection=CONNECTION_NETWORK_MAC,
ha_connection_value="data__port-mac-address", ha_connection_value="data__port-mac-address",
data_path="interface", data_path="interface",
data_attribute="temperature",
data_switch_path="/interface", data_switch_path="/interface",
data_name="name", data_name="name",
data_uid="name", data_uid="name",
data_reference="default-name", data_reference="default-name",
data_attributes_list=DEVICE_ATTRIBUTES_IFACE, data_attributes_list=DEVICE_ATTRIBUTES_IFACE,
), ),
"nat": MikrotikSwitchEntityDescription(
key="nat",
name="NAT",
icon_enabled="network-outline",
icon_disabled="network-off-outline",
entity_category=None,
ha_group="NAT",
ha_connection=DOMAIN,
ha_connection_value="NAT",
data_path="nat",
data_switch_path="/ip/firewall/nat",
data_name="name",
data_name_comment=True,
data_uid="uniq-id",
data_reference="uniq-id",
data_attributes_list=DEVICE_ATTRIBUTES_NAT,
),
} }