Getting entity state in python

I’m trying to put together my first HA python script.

The intention is that it runs in the background while a button (toggled) is in the ‘on’ state.

My thinking is to have an automation to start the script when the button change to on (I’ve got this working) and for the script to periodically check that the button is still in ‘on’. Once it’s not then it will finish running.

I can’t seem to locate any example code which obtains the state of the button which I can then use in a while loop.

Could someone please provide an example or point me to the pages I can’t seem to find?