From 4dfd9ab95a4b89d3bce164076bec2bdda67fccf7 Mon Sep 17 00:00:00 2001 From: jessikitty Date: Wed, 31 Dec 2025 00:41:29 +1100 Subject: [PATCH] Add person-based dynamic device control system --- opnsense_person_control.yaml | 671 +++++++++++++++++++++++++++++++++++ 1 file changed, 671 insertions(+) create mode 100644 opnsense_person_control.yaml diff --git a/opnsense_person_control.yaml b/opnsense_person_control.yaml new file mode 100644 index 0000000..ce5f316 --- /dev/null +++ b/opnsense_person_control.yaml @@ -0,0 +1,671 @@ +################################################################################ +# OPNsense Person-Based Device Control System +# Dynamically links devices to persons and provides per-device or per-person blocking +################################################################################ + +# Person definitions (one toggle to block all devices for that person) +input_boolean: + # Person-level blocks + block_bella_all: + name: "Block All - Bella" + icon: mdi:account-cancel + + block_xander_all: + name: "Block All - Xander" + icon: mdi:account-cancel + + block_william_all: + name: "Block All - William" + icon: mdi:account-cancel + + block_jess_all: + name: "Block All - Jess" + icon: mdi:account-cancel + + block_rob_all: + name: "Block All - Rob" + icon: mdi:account-cancel + + block_system_all: + name: "Block All - System Devices" + icon: mdi:server-network-off + + # Individual device blocks + block_device_bella_phone: + name: "Block Bella's Phone" + icon: mdi:cellphone-off + + block_device_bella_tablet: + name: "Block Bella's Tablet" + icon: mdi:tablet-off + + block_device_xander_phone: + name: "Block Xander's Phone" + icon: mdi:cellphone-off + + block_device_xander_desktop: + name: "Block Xander's Desktop" + icon: mdi:desktop-classic + + block_device_william_phone: + name: "Block William's Phone" + icon: mdi:cellphone-off + + block_device_william_laptop: + name: "Block William's Laptop" + icon: mdi:laptop-off + + block_device_jess_phone: + name: "Block Jess's Phone" + icon: mdi:cellphone-off + + block_device_jess_laptop: + name: "Block Jess's Laptop" + icon: mdi:laptop-off + + block_device_rob_phone: + name: "Block Rob's Phone" + icon: mdi:cellphone-off + + block_device_rob_laptop: + name: "Block Rob's Laptop" + icon: mdi:laptop-off + +# Device owner assignments +input_select: + device_owner_bella_phone: + name: "Bella's Phone - Owner" + options: + - Bella + - Xander + - William + - Jess + - Rob + - System + - Unassigned + initial: Bella + icon: mdi:account-circle + + device_owner_bella_tablet: + name: "Bella's Tablet - Owner" + options: + - Bella + - Xander + - William + - Jess + - Rob + - System + - Unassigned + initial: Bella + icon: mdi:account-circle + + device_owner_xander_phone: + name: "Xander's Phone - Owner" + options: + - Bella + - Xander + - William + - Jess + - Rob + - System + - Unassigned + initial: Xander + icon: mdi:account-circle + + device_owner_xander_desktop: + name: "Xander's Desktop - Owner" + options: + - Bella + - Xander + - William + - Jess + - Rob + - System + - Unassigned + initial: Xander + icon: mdi:account-circle + + device_owner_william_phone: + name: "William's Phone - Owner" + options: + - Bella + - Xander + - William + - Jess + - Rob + - System + - Unassigned + initial: William + icon: mdi:account-circle + + device_owner_william_laptop: + name: "William's Laptop - Owner" + options: + - Bella + - Xander + - William + - Jess + - Rob + - System + - Unassigned + initial: William + icon: mdi:account-circle + + device_owner_jess_phone: + name: "Jess's Phone - Owner" + options: + - Bella + - Xander + - William + - Jess + - Rob + - System + - Unassigned + initial: Jess + icon: mdi:account-circle + + device_owner_jess_laptop: + name: "Jess's Laptop - Owner" + options: + - Bella + - Xander + - William + - Jess + - Rob + - System + - Unassigned + initial: Jess + icon: mdi:account-circle + + device_owner_rob_phone: + name: "Rob's Phone - Owner" + options: + - Bella + - Xander + - William + - Jess + - Rob + - System + - Unassigned + initial: Rob + icon: mdi:account-circle + + device_owner_rob_laptop: + name: "Rob's Laptop - Owner" + options: + - Bella + - Xander + - William + - Jess + - Rob + - System + - Unassigned + initial: Rob + icon: mdi:account-circle + +# Device MAC addresses (add more as needed) +input_text: + mac_bella_phone: + name: "Bella Phone MAC" + icon: mdi:network + + mac_bella_tablet: + name: "Bella Tablet MAC" + icon: mdi:network + + mac_xander_phone: + name: "Xander Phone MAC" + icon: mdi:network + + mac_xander_desktop_wifi: + name: "Xander Desktop WiFi MAC" + icon: mdi:network + + mac_xander_desktop_wired: + name: "Xander Desktop Wired MAC" + icon: mdi:network + + mac_william_phone: + name: "William Phone MAC" + icon: mdi:network + + mac_william_laptop_wifi: + name: "William Laptop WiFi MAC" + icon: mdi:network + + mac_william_laptop_wired: + name: "William Laptop Wired MAC" + icon: mdi:network + + mac_jess_phone: + name: "Jess Phone MAC" + icon: mdi:network + + mac_jess_laptop_wifi: + name: "Jess Laptop WiFi MAC" + icon: mdi:network + + mac_jess_laptop_wired: + name: "Jess Laptop Wired MAC" + icon: mdi:network + + mac_rob_phone: + name: "Rob Phone MAC" + icon: mdi:network + + mac_rob_laptop_wifi: + name: "Rob Laptop WiFi MAC" + icon: mdi:network + + mac_rob_laptop_wired: + name: "Rob Laptop Wired MAC" + icon: mdi:network + +# Template sensors to track device counts per person +template: + - sensor: + - name: "Bella Device Count" + unique_id: bella_device_count + state: > + {% set devices = [ + states('input_select.device_owner_bella_phone'), + states('input_select.device_owner_bella_tablet'), + states('input_select.device_owner_xander_phone'), + states('input_select.device_owner_xander_desktop'), + states('input_select.device_owner_william_phone'), + states('input_select.device_owner_william_laptop'), + states('input_select.device_owner_jess_phone'), + states('input_select.device_owner_jess_laptop'), + states('input_select.device_owner_rob_phone'), + states('input_select.device_owner_rob_laptop') + ] %} + {{ devices | select('eq', 'Bella') | list | count }} + icon: mdi:counter + + - name: "Xander Device Count" + unique_id: xander_device_count + state: > + {% set devices = [ + states('input_select.device_owner_bella_phone'), + states('input_select.device_owner_bella_tablet'), + states('input_select.device_owner_xander_phone'), + states('input_select.device_owner_xander_desktop'), + states('input_select.device_owner_william_phone'), + states('input_select.device_owner_william_laptop'), + states('input_select.device_owner_jess_phone'), + states('input_select.device_owner_jess_laptop'), + states('input_select.device_owner_rob_phone'), + states('input_select.device_owner_rob_laptop') + ] %} + {{ devices | select('eq', 'Xander') | list | count }} + icon: mdi:counter + + - name: "William Device Count" + unique_id: william_device_count + state: > + {% set devices = [ + states('input_select.device_owner_bella_phone'), + states('input_select.device_owner_bella_tablet'), + states('input_select.device_owner_xander_phone'), + states('input_select.device_owner_xander_desktop'), + states('input_select.device_owner_william_phone'), + states('input_select.device_owner_william_laptop'), + states('input_select.device_owner_jess_phone'), + states('input_select.device_owner_jess_laptop'), + states('input_select.device_owner_rob_phone'), + states('input_select.device_owner_rob_laptop') + ] %} + {{ devices | select('eq', 'William') | list | count }} + icon: mdi:counter + + - name: "Jess Device Count" + unique_id: jess_device_count + state: > + {% set devices = [ + states('input_select.device_owner_bella_phone'), + states('input_select.device_owner_bella_tablet'), + states('input_select.device_owner_xander_phone'), + states('input_select.device_owner_xander_desktop'), + states('input_select.device_owner_william_phone'), + states('input_select.device_owner_william_laptop'), + states('input_select.device_owner_jess_phone'), + states('input_select.device_owner_jess_laptop'), + states('input_select.device_owner_rob_phone'), + states('input_select.device_owner_rob_laptop') + ] %} + {{ devices | select('eq', 'Jess') | list | count }} + icon: mdi:counter + + - name: "Rob Device Count" + unique_id: rob_device_count + state: > + {% set devices = [ + states('input_select.device_owner_bella_phone'), + states('input_select.device_owner_bella_tablet'), + states('input_select.device_owner_xander_phone'), + states('input_select.device_owner_xander_desktop'), + states('input_select.device_owner_william_phone'), + states('input_select.device_owner_william_laptop'), + states('input_select.device_owner_jess_phone'), + states('input_select.device_owner_jess_laptop'), + states('input_select.device_owner_rob_phone'), + states('input_select.device_owner_rob_laptop') + ] %} + {{ devices | select('eq', 'Rob') | list | count }} + icon: mdi:counter + + - name: "System Device Count" + unique_id: system_device_count + state: > + {% set devices = [ + states('input_select.device_owner_bella_phone'), + states('input_select.device_owner_bella_tablet'), + states('input_select.device_owner_xander_phone'), + states('input_select.device_owner_xander_desktop'), + states('input_select.device_owner_william_phone'), + states('input_select.device_owner_william_laptop'), + states('input_select.device_owner_jess_phone'), + states('input_select.device_owner_jess_laptop'), + states('input_select.device_owner_rob_phone'), + states('input_select.device_owner_rob_laptop') + ] %} + {{ devices | select('eq', 'System') | list | count }} + icon: mdi:counter + +# Automations for person-level blocking +automation: + # Bella - Block all devices + - id: block_all_bella_devices + alias: "Block All Bella Devices" + trigger: + - platform: state + entity_id: input_boolean.block_bella_all + action: + - service: input_boolean.turn_{{ 'on' if trigger.to_state.state == 'on' else 'off' }} + target: + entity_id: > + {% set devices = [] %} + {% if states('input_select.device_owner_bella_phone') == 'Bella' %} + {% set devices = devices + ['input_boolean.block_device_bella_phone'] %} + {% endif %} + {% if states('input_select.device_owner_bella_tablet') == 'Bella' %} + {% set devices = devices + ['input_boolean.block_device_bella_tablet'] %} + {% endif %} + {% if states('input_select.device_owner_xander_phone') == 'Bella' %} + {% set devices = devices + ['input_boolean.block_device_xander_phone'] %} + {% endif %} + {% if states('input_select.device_owner_xander_desktop') == 'Bella' %} + {% set devices = devices + ['input_boolean.block_device_xander_desktop'] %} + {% endif %} + {% if states('input_select.device_owner_william_phone') == 'Bella' %} + {% set devices = devices + ['input_boolean.block_device_william_phone'] %} + {% endif %} + {% if states('input_select.device_owner_william_laptop') == 'Bella' %} + {% set devices = devices + ['input_boolean.block_device_william_laptop'] %} + {% endif %} + {% if states('input_select.device_owner_jess_phone') == 'Bella' %} + {% set devices = devices + ['input_boolean.block_device_jess_phone'] %} + {% endif %} + {% if states('input_select.device_owner_jess_laptop') == 'Bella' %} + {% set devices = devices + ['input_boolean.block_device_jess_laptop'] %} + {% endif %} + {% if states('input_select.device_owner_rob_phone') == 'Bella' %} + {% set devices = devices + ['input_boolean.block_device_rob_phone'] %} + {% endif %} + {% if states('input_select.device_owner_rob_laptop') == 'Bella' %} + {% set devices = devices + ['input_boolean.block_device_rob_laptop'] %} + {% endif %} + {{ devices }} + + # Xander - Block all devices + - id: block_all_xander_devices + alias: "Block All Xander Devices" + trigger: + - platform: state + entity_id: input_boolean.block_xander_all + action: + - service: input_boolean.turn_{{ 'on' if trigger.to_state.state == 'on' else 'off' }} + target: + entity_id: > + {% set devices = [] %} + {% if states('input_select.device_owner_bella_phone') == 'Xander' %} + {% set devices = devices + ['input_boolean.block_device_bella_phone'] %} + {% endif %} + {% if states('input_select.device_owner_bella_tablet') == 'Xander' %} + {% set devices = devices + ['input_boolean.block_device_bella_tablet'] %} + {% endif %} + {% if states('input_select.device_owner_xander_phone') == 'Xander' %} + {% set devices = devices + ['input_boolean.block_device_xander_phone'] %} + {% endif %} + {% if states('input_select.device_owner_xander_desktop') == 'Xander' %} + {% set devices = devices + ['input_boolean.block_device_xander_desktop'] %} + {% endif %} + {% if states('input_select.device_owner_william_phone') == 'Xander' %} + {% set devices = devices + ['input_boolean.block_device_william_phone'] %} + {% endif %} + {% if states('input_select.device_owner_william_laptop') == 'Xander' %} + {% set devices = devices + ['input_boolean.block_device_william_laptop'] %} + {% endif %} + {% if states('input_select.device_owner_jess_phone') == 'Xander' %} + {% set devices = devices + ['input_boolean.block_device_jess_phone'] %} + {% endif %} + {% if states('input_select.device_owner_jess_laptop') == 'Xander' %} + {% set devices = devices + ['input_boolean.block_device_jess_laptop'] %} + {% endif %} + {% if states('input_select.device_owner_rob_phone') == 'Xander' %} + {% set devices = devices + ['input_boolean.block_device_rob_phone'] %} + {% endif %} + {% if states('input_select.device_owner_rob_laptop') == 'Xander' %} + {% set devices = devices + ['input_boolean.block_device_rob_laptop'] %} + {% endif %} + {{ devices }} + + # William - Block all devices + - id: block_all_william_devices + alias: "Block All William Devices" + trigger: + - platform: state + entity_id: input_boolean.block_william_all + action: + - service: input_boolean.turn_{{ 'on' if trigger.to_state.state == 'on' else 'off' }} + target: + entity_id: > + {% set devices = [] %} + {% if states('input_select.device_owner_bella_phone') == 'William' %} + {% set devices = devices + ['input_boolean.block_device_bella_phone'] %} + {% endif %} + {% if states('input_select.device_owner_bella_tablet') == 'William' %} + {% set devices = devices + ['input_boolean.block_device_bella_tablet'] %} + {% endif %} + {% if states('input_select.device_owner_xander_phone') == 'William' %} + {% set devices = devices + ['input_boolean.block_device_xander_phone'] %} + {% endif %} + {% if states('input_select.device_owner_xander_desktop') == 'William' %} + {% set devices = devices + ['input_boolean.block_device_xander_desktop'] %} + {% endif %} + {% if states('input_select.device_owner_william_phone') == 'William' %} + {% set devices = devices + ['input_boolean.block_device_william_phone'] %} + {% endif %} + {% if states('input_select.device_owner_william_laptop') == 'William' %} + {% set devices = devices + ['input_boolean.block_device_william_laptop'] %} + {% endif %} + {% if states('input_select.device_owner_jess_phone') == 'William' %} + {% set devices = devices + ['input_boolean.block_device_jess_phone'] %} + {% endif %} + {% if states('input_select.device_owner_jess_laptop') == 'William' %} + {% set devices = devices + ['input_boolean.block_device_jess_laptop'] %} + {% endif %} + {% if states('input_select.device_owner_rob_phone') == 'William' %} + {% set devices = devices + ['input_boolean.block_device_rob_phone'] %} + {% endif %} + {% if states('input_select.device_owner_rob_laptop') == 'William' %} + {% set devices = devices + ['input_boolean.block_device_rob_laptop'] %} + {% endif %} + {{ devices }} + + # Jess - Block all devices + - id: block_all_jess_devices + alias: "Block All Jess Devices" + trigger: + - platform: state + entity_id: input_boolean.block_jess_all + action: + - service: input_boolean.turn_{{ 'on' if trigger.to_state.state == 'on' else 'off' }} + target: + entity_id: > + {% set devices = [] %} + {% if states('input_select.device_owner_bella_phone') == 'Jess' %} + {% set devices = devices + ['input_boolean.block_device_bella_phone'] %} + {% endif %} + {% if states('input_select.device_owner_bella_tablet') == 'Jess' %} + {% set devices = devices + ['input_boolean.block_device_bella_tablet'] %} + {% endif %} + {% if states('input_select.device_owner_xander_phone') == 'Jess' %} + {% set devices = devices + ['input_boolean.block_device_xander_phone'] %} + {% endif %} + {% if states('input_select.device_owner_xander_desktop') == 'Jess' %} + {% set devices = devices + ['input_boolean.block_device_xander_desktop'] %} + {% endif %} + {% if states('input_select.device_owner_william_phone') == 'Jess' %} + {% set devices = devices + ['input_boolean.block_device_william_phone'] %} + {% endif %} + {% if states('input_select.device_owner_william_laptop') == 'Jess' %} + {% set devices = devices + ['input_boolean.block_device_william_laptop'] %} + {% endif %} + {% if states('input_select.device_owner_jess_phone') == 'Jess' %} + {% set devices = devices + ['input_boolean.block_device_jess_phone'] %} + {% endif %} + {% if states('input_select.device_owner_jess_laptop') == 'Jess' %} + {% set devices = devices + ['input_boolean.block_device_jess_laptop'] %} + {% endif %} + {% if states('input_select.device_owner_rob_phone') == 'Jess' %} + {% set devices = devices + ['input_boolean.block_device_rob_phone'] %} + {% endif %} + {% if states('input_select.device_owner_rob_laptop') == 'Jess' %} + {% set devices = devices + ['input_boolean.block_device_rob_laptop'] %} + {% endif %} + {{ devices }} + + # Rob - Block all devices + - id: block_all_rob_devices + alias: "Block All Rob Devices" + trigger: + - platform: state + entity_id: input_boolean.block_rob_all + action: + - service: input_boolean.turn_{{ 'on' if trigger.to_state.state == 'on' else 'off' }} + target: + entity_id: > + {% set devices = [] %} + {% if states('input_select.device_owner_bella_phone') == 'Rob' %} + {% set devices = devices + ['input_boolean.block_device_bella_phone'] %} + {% endif %} + {% if states('input_select.device_owner_bella_tablet') == 'Rob' %} + {% set devices = devices + ['input_boolean.block_device_bella_tablet'] %} + {% endif %} + {% if states('input_select.device_owner_xander_phone') == 'Rob' %} + {% set devices = devices + ['input_boolean.block_device_xander_phone'] %} + {% endif %} + {% if states('input_select.device_owner_xander_desktop') == 'Rob' %} + {% set devices = devices + ['input_boolean.block_device_xander_desktop'] %} + {% endif %} + {% if states('input_select.device_owner_william_phone') == 'Rob' %} + {% set devices = devices + ['input_boolean.block_device_william_phone'] %} + {% endif %} + {% if states('input_select.device_owner_william_laptop') == 'Rob' %} + {% set devices = devices + ['input_boolean.block_device_william_laptop'] %} + {% endif %} + {% if states('input_select.device_owner_jess_phone') == 'Rob' %} + {% set devices = devices + ['input_boolean.block_device_jess_phone'] %} + {% endif %} + {% if states('input_select.device_owner_jess_laptop') == 'Rob' %} + {% set devices = devices + ['input_boolean.block_device_jess_laptop'] %} + {% endif %} + {% if states('input_select.device_owner_rob_phone') == 'Rob' %} + {% set devices = devices + ['input_boolean.block_device_rob_phone'] %} + {% endif %} + {% if states('input_select.device_owner_rob_laptop') == 'Rob' %} + {% set devices = devices + ['input_boolean.block_device_rob_laptop'] %} + {% endif %} + {{ devices }} + + # System - Block all devices + - id: block_all_system_devices + alias: "Block All System Devices" + trigger: + - platform: state + entity_id: input_boolean.block_system_all + action: + - service: input_boolean.turn_{{ 'on' if trigger.to_state.state == 'on' else 'off' }} + target: + entity_id: > + {% set devices = [] %} + {% if states('input_select.device_owner_bella_phone') == 'System' %} + {% set devices = devices + ['input_boolean.block_device_bella_phone'] %} + {% endif %} + {% if states('input_select.device_owner_bella_tablet') == 'System' %} + {% set devices = devices + ['input_boolean.block_device_bella_tablet'] %} + {% endif %} + {% if states('input_select.device_owner_xander_phone') == 'System' %} + {% set devices = devices + ['input_boolean.block_device_xander_phone'] %} + {% endif %} + {% if states('input_select.device_owner_xander_desktop') == 'System' %} + {% set devices = devices + ['input_boolean.block_device_xander_desktop'] %} + {% endif %} + {% if states('input_select.device_owner_william_phone') == 'System' %} + {% set devices = devices + ['input_boolean.block_device_william_phone'] %} + {% endif %} + {% if states('input_select.device_owner_william_laptop') == 'System' %} + {% set devices = devices + ['input_boolean.block_device_william_laptop'] %} + {% endif %} + {% if states('input_select.device_owner_jess_phone') == 'System' %} + {% set devices = devices + ['input_boolean.block_device_jess_phone'] %} + {% endif %} + {% if states('input_select.device_owner_jess_laptop') == 'System' %} + {% set devices = devices + ['input_boolean.block_device_jess_laptop'] %} + {% endif %} + {% if states('input_select.device_owner_rob_phone') == 'System' %} + {% set devices = devices + ['input_boolean.block_device_rob_phone'] %} + {% endif %} + {% if states('input_select.device_owner_rob_laptop') == 'System' %} + {% set devices = devices + ['input_boolean.block_device_rob_laptop'] %} + {% endif %} + {{ devices }} + + # Update OPNsense when any device block changes + - id: sync_device_blocks_to_opnsense + alias: "Sync Device Blocks to OPNsense" + trigger: + - platform: state + entity_id: + - input_boolean.block_device_bella_phone + - input_boolean.block_device_bella_tablet + - input_boolean.block_device_xander_phone + - input_boolean.block_device_xander_desktop + - input_boolean.block_device_william_phone + - input_boolean.block_device_william_laptop + - input_boolean.block_device_jess_phone + - input_boolean.block_device_jess_laptop + - input_boolean.block_device_rob_phone + - input_boolean.block_device_rob_laptop + action: + - service: rest_command.update_blocked_macs + data: {} + +# REST commands for OPNsense integration +rest_command: + update_blocked_macs: + url: !secret opnsense_api_url + method: POST + verify_ssl: false + username: !secret opnsense_api_key + password: !secret opnsense_api_secret + content_type: 'application/json' + payload: > + { + "alias": { + "Blocked_Bella": "{{ states('input_text.mac_bella_phone') if is_state('input_boolean.block_device_bella_phone', 'on') and states('input_select.device_owner_bella_phone') == 'Bella' else '' }}\n{{ states('input_text.mac_bella_tablet') if is_state('input_boolean.block_device_bella_tablet', 'on') and states('input_select.device_owner_bella_tablet') == 'Bella' else '' }}", + "Blocked_Xander": "{{ states('input_text.mac_xander_phone') if is_state('input_boolean.block_device_xander_phone', 'on') and states('input_select.device_owner_xander_phone') == 'Xander' else '' }}\n{{ states('input_text.mac_xander_desktop_wifi') if is_state('input_boolean.block_device_xander_desktop', 'on') and states('input_select.device_owner_xander_desktop') == 'Xander' else '' }}\n{{ states('input_text.mac_xander_desktop_wired') if is_state('input_boolean.block_device_xander_desktop', 'on') and states('input_select.device_owner_xander_desktop') == 'Xander' else '' }}", + "Blocked_William": "{{ states('input_text.mac_william_phone') if is_state('input_boolean.block_device_william_phone', 'on') and states('input_select.device_owner_william_phone') == 'William' else '' }}\n{{ states('input_text.mac_william_laptop_wifi') if is_state('input_boolean.block_device_william_laptop', 'on') and states('input_select.device_owner_william_laptop') == 'William' else '' }}\n{{ states('input_text.mac_william_laptop_wired') if is_state('input_boolean.block_device_william_laptop', 'on') and states('input_select.device_owner_william_laptop') == 'William' else '' }}" + } + }