toggle-on-air.py 636 B

12345678910111213141516171819202122232425
  1. #!/usr/bin/env python
  2. import os
  3. from requests import post
  4. def get_env(name):
  5. value = os.getenv(name, None)
  6. if value is None:
  7. raise Exception('Missing {} from the environment'.format(name))
  8. return value
  9. if __name__ == '__main__':
  10. endpoint = get_env('HA_ENDPOINT')
  11. token = get_env('HA_TOKEN')
  12. url = '{}/api/services/switch/toggle'.format(endpoint)
  13. data = {'entity_id': 'switch.on_air_switch_switch'}
  14. headers = {
  15. "Authorization": "Bearer {}".format(token),
  16. "content-type": "application/json",
  17. }
  18. response = post(url, headers=headers, json=data)
  19. print(response.text)