Skip to content

Commit

Permalink
Alias Toggle Action/Service
Browse files Browse the repository at this point in the history
  • Loading branch information
Snuffy2 committed Nov 24, 2024
1 parent a333d50 commit 896cec9
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 0 deletions.
1 change: 1 addition & 0 deletions custom_components/opnsense/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,4 @@
SERVICE_RELOAD_INTERFACE = "reload_interface"
SERVICE_GENERATE_VOUCHERS = "generate_vouchers"
SERVICE_KILL_STATES = "kill_states"
SERVICE_TOGGLE_ALIAS = "toggle_alias"
55 changes: 55 additions & 0 deletions custom_components/opnsense/pyopnsense/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2162,3 +2162,58 @@ async def kill_states(self, ip_addr) -> Mapping[str, Any]:
"success": bool(response.get("result", None) == "ok"),
"dropped_states": response.get("dropped_states", 0),
}

async def toggle_alias(self, alias, toggle_on_off) -> bool:

alias_list_resp: Mapping[str, Any] | list = await self._get(
"/api/firewall/alias/searchItem"
)
if not isinstance(alias_list_resp, Mapping):
return False
alias_list: list = alias_list_resp.get("rows", [])
if not isinstance(alias_list, list):
return False
uuid: str | None = None
for item in alias_list:
if not isinstance(item, Mapping):
continue
if item.get("name") == alias:
uuid = item.get("uuid")
break
if not uuid:
return False
payload: Mapping[str, Any] = {}
url: str = f"/api/firewall/alias/toggleItem/{uuid}"
if toggle_on_off == "on":
url = url + "/1"
elif toggle_on_off == "off":
url = url + "/0"
response: Mapping[str, Any] | list = await self._post(
url,
payload=payload,
)
_LOGGER.debug(
f"[toggle_alias] alias: {alias}, uuid: {uuid}, action: {toggle_on_off}, "
f"url: {url}, response: {response}"
)
if (
not isinstance(response, Mapping)
or "result" not in response
or response.get("result") == "failed"
):
return False

set_resp: Mapping[str, Any] | list = await self._post("/api/firewall/alias/set")
if not isinstance(set_resp, Mapping) or set_resp.get("result") != "saved":
return False

reconfigure_resp: Mapping[str, Any] | list = await self._post(
"/api/firewall/alias/reconfigure"
)
if (
not isinstance(reconfigure_resp, Mapping)
or reconfigure_resp.get("status") != "ok"
):
return False

return True
36 changes: 36 additions & 0 deletions custom_components/opnsense/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
SERVICE_STOP_SERVICE,
SERVICE_SYSTEM_HALT,
SERVICE_SYSTEM_REBOOT,
SERVICE_TOGGLE_ALIAS,
)
from .pyopnsense import VoucherServerError

Expand Down Expand Up @@ -268,6 +269,25 @@ async def service_kill_states(call: ServiceCall) -> ServiceResponse:
if call.return_response:
return return_response

async def service_toggle_alias(call: ServiceCall) -> None:
clients: list = await _get_clients(
call.data.get("device_id", []), call.data.get("entity_id", [])
)
success = None
for client in clients:
response = await client.toggle_alias(
call.data.get("alias"), call.data.get("toggle_on_off")
)
_LOGGER.debug(
f"[service_toggle_alias] client: {client.name}, alias: {call.data.get("alias")}, response: {response}"
)
if success is None or success:
success = response
if success is None or not success:
raise ServiceValidationError(
f"Toggle Alias Failed. client: {client.name}, alias: {call.data.get("alias")}, action: {call.data.get("toggle_on_off")}"
)

hass.services.async_register(
domain=DOMAIN,
service=SERVICE_CLOSE_NOTICE,
Expand Down Expand Up @@ -430,3 +450,19 @@ async def service_kill_states(call: ServiceCall) -> ServiceResponse:
service_func=service_kill_states,
supports_response=SupportsResponse.OPTIONAL,
)

hass.services.async_register(
domain=DOMAIN,
service=SERVICE_TOGGLE_ALIAS,
schema=vol.Schema(
{
vol.Required("alias"): vol.Any(cv.string),
vol.Required("toggle_on_off", default="toggle"): vol.In(
{"toggle": "Toggle", "on": "On", "off": "Off"}
),
vol.Optional("device_id"): vol.Any(cv.string),
vol.Optional("entity_id"): vol.Any(cv.string),
}
),
service_func=service_toggle_alias,
)
54 changes: 54 additions & 0 deletions custom_components/opnsense/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -474,3 +474,57 @@ kill_states:
filter:
- integration: opnsense
domain: sensor

toggle_alias:
name: Toggle Alias
fields:
alias:
name: Alias Name
required: true
advanced: false
example: "iphones"
selector:
text:
toggle_on_off:
name: Alias Action
required: true
advanced: false
default: "toggle"
selector:
select:
multiple: false
custom_value: false
mode: list
options:
- label: "Toggle"
value: "toggle"
- label: "On"
value: "on"
- label: "Off"
value: "off"
multiple_opnsense:
name: Only needed if there is more than one OPNsense Router
collapsed: true
fields:
device_id:
name: OPNsense Device
description: Select the OPNsense Router to call the command on. If not specified, the command will be sent to all OPNsense Routers.
required: false
selector:
device:
multiple: false
filter:
- integration: opnsense
entity:
- domain: sensor
entity_id:
name: OPNsense Entity
description: Pick any sensor in the OPNsense Router you want to call the command on. If not specified, the command will be sent to all OPNsense Routers.
example: "sensor.opnsense_interface_lan_status"
required: false
selector:
entity:
multiple: false
filter:
- integration: opnsense
domain: sensor

0 comments on commit 896cec9

Please sign in to comment.