Accessing user name or user id from (python) script

I’ve been trying to get a simple home assistant setup to work that is able to open a lock (by pulsing a relay). This should only be possible for each user at a certain time, so I’ve created a python script to cross-reference the user and their access rights (represented by a dictionary). The script is the following:

users = {"De Nootzaak": ["mon 00:00:00", "sun 23:59:59"], "Elias": ["wed 18:45:00", "wed 19:15:00"]}
days = ["mon","tue","wed","thu","fri","sat","sun"]

user = data.get("name")
daylimits = [days.index(users[user][0].split()[0]), days.index(users[user][1].split()[0])]
timelimits = [users[user][0].split()[1], users[user][1].split()[1]]
now = datetime.datetime.now()
if daylimits[0] <= now.weekday() and now.weekday() <= daylimits[1]:
    if timelimits[0] <= str(now).split()[1] and str(now).split()[1] <= timelimits[1]:
        hass.services.call("switch", "turn_on", {"entity_id": "switch.lock"}, False)
        time.sleep(0.8)
        hass.services.call("switch", "turn_off", {"entity_id": "switch.lock"}, False)

It is activated by a button in the UI which should pass the user name (adapted from Person entity: how to use "user_id"):

show_name: true
show_icon: true
type: button
tap_action:
  action: call-service
  service: python_script.lock
  target:
    entity_id: switch.lock
  data:
    name: >
      {{ states.person|selectattr("attributes.user_id", "==",
      "states.domain.object_id.context.user_id")|map(attribute="attributes.friendly_name")|first
      }}
entity: switch.lock
name: Open slot python
icon: mdi:home-lock-open

I’ve tested every part of the script and button and it seems to work fine, aside from gaining access to the correct user name (or alternatively the user id) so I can check their access rights. I’ve tried using different templates for the user name, an intermediate script that passes the user name parameter, and accessing the user name directly inside the python script but everything seems to result in an empty string or an error claiming “context” is not defined. I’m quite new to home assistant, so maybe there is something obvious I’m missing, any suggestions on how to solve this issue?

For anyone who is having a similar issue, I found a solution to my problem using the button card integration from HACS:

type: custom:button-card
show_name: true
show_icon: true
tap_action:
  action: call-service
  service: python_script.lock
  target:
    entity_id: switch.lock
  data:
    name: '[[[ return user.name; ]]]'
entity: switch.lock
name: Open slot
icon: mdi:home-lock-open

This integration exposes the user object in a javascript template (and as a result the user name using user.name). Hope this is useful.