The fundamental issue with your code in that the function
gpio_function does not return the state of the pin but the actual type of pin (input, output, serial port etc). This is documented here
There are a couple of ways you can handle this issue but first thing to note is that the function
GPIO.cleanup() will reset your pin to input with no pull up/down. You need to exclude the pin you are using with
GPIO.cleanup(GPIO_ENCHUFE). This is documented here
As for keeping the state of the pin then there are a few ways of doing this (and I'm sure others will have their own ways):
First up is to create a program that handles both on and off status with a delay between them:
set port to output
set switch on
delay till off time
set switch off
You can then call this program from CRON at the correct on time
A second way is to save the status to a file and use that to action the switch:
Create a file with a zero in it (use nano or similar)
Read the data from the file
If zero turn off the switch and write zero to the file
If one turn on the switch and write one to the file
You then call this at the required time to change the state of the switch.
A third way would be to pass the status you want the switch as a parameter to the Python program and use the sys module to access it:
Check only one argument passed using len(sys.argv)
Check if the argument is on or off using sys.argv for the parameter
Turn the switch on or off as needed
Note the Python module getopt helps with parameters but is not needed for a simple check like this.
You would then call this program with the correct parameter to change as needed.
The last option I will list is to use the same program and just toggle the current pin state using the input function:
GPIO.output(GPIO_ENCHUFE, not GPIO.input(GPIO_ENCHUFE))
Every time the program is called, the current state is found by the GPIO.input, inverted / flipped by the not and set as the new output value.
Please remember that you must not clear down the pin use at the end with the general GPIO.cleanup() command but use the