12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- #!/usr/bin/env python
- import os
- import time
- import math
- from requests import post
- def get_env(name):
- value = os.getenv(name, None)
- if value is None:
- raise Exception('Missing {} from the environment'.format(name))
- return value
- def call(service_call, data):
- endpoint = get_env('HA_ENDPOINT')
- token = get_env('HA_TOKEN')
- url = '{}/api/services/light/{}'.format(endpoint, service_call)
- headers = {
- "Authorization": "Bearer {}".format(token),
- "content-type": "application/json",
- }
- response = post(url, headers=headers, json=data)
- print(response.text)
- def set_rgb(r, g, b):
- data = {
- 'entity_id': 'light.led_strip_light',
- 'rgb_color': [r, g, b],
- }
- call('turn_on', data)
- def turn_off():
- data = {
- 'entity_id': 'light.led_strip_light',
- }
- call('turn_off', data)
- if __name__ == '__main__':
- print('Press CTRL+C to interrupt...')
- try:
- while True:
- for i in range(1, 50):
- r = math.sin(0.3*i + 0) * 127 + 128
- g = math.sin(0.3*i + 2) * 127 + 128
- b = math.sin(0.3*i + 4) * 127 + 128
- set_rgb(r, g, b)
- time.sleep(0.5)
- except KeyboardInterrupt:
- pass
- finally:
- set_rgb(0, 0, 255)
- time.sleep(1)
- turn_off()
|