Python addon created switch feedback/event to addon/python

Hi,

I have created a small addon (located it in /addons directory). It is a python script (inside Docker) that monitors an EPS device and updates the state of binary entities within HA.

http://supervisor/core/api/states/binary_sensor.mydoor

With the following payload.

{ 'attributes': {                                                                           
         'device_class' : 'door',                   
           'friendly_name': 'my_door'                           
     },                                                                          
          'entity_id': 'binary_sensor.mydoor',                          
          'state': 'on'                                         
    }                                            

All works fine.

Next step is to get events flowing the other way, i.e. from HA to my python script.

Ive created a switch (within python) and it appears in HA. It is just not clear to me how I receive (monitor) the state change of this switch from my python script.

http://supervisor/core/api/states/switch.my_switch                                                   
                                                                                                                              
           {'attributes': {                                                                                                                                                                   
                          'device_class' : 'switch',                                                                          
                          'friendly_name': 'My Switch' 
                        },                                                                                                     
                          'entity_id': 'switch.myswitch,                                                                  
                          'state': 'off'                                                                                      
                       }     

I’ve looked into websocket but this switch does not seem to generate an event.

Any suggestions as to how I can change the state of a swith within HA and that even “appear” in my python script?

Thanks,

Pete.