Python script to change brightness

I created the script below however it doesn’t seem to be working.
What am I doing wrong?

import time.sleep

entity_id = data.get("entity_id")
rgb_color = data.get("rgb_color", [255, 219, 0])

if entity_id is not None:
  service_data = {"entity_id": entity_id, "rgb_color": rgb_color, "brightness": 1}
  hass.services.call("light", "turn_on", service_data, False)
  
  for x in range(2, 255):
    sleep(1)
    hass.states.set("light.my_light.attributes.brightness", x)