├── LICENSE ├── README.md ├── capture_timestamped_image.py ├── days_until_date.py ├── home_temperature.py ├── hue_remote.py ├── process_deepstack_image.py ├── send_telegram.py └── train_state.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Robin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # python-scripts-for-home-assistant 2 | 3 | A couple of scripts for use with the Home-assistant python scripts component. 4 | 5 | ## References 6 | https://home-assistant.io/components/python_script/ 7 | -------------------------------------------------------------------------------- /capture_timestamped_image.py: -------------------------------------------------------------------------------- 1 | """ 2 | Capture a timestamped camera image using the service camera.snapshot. 3 | """ 4 | BLINK_SLEEP_TIME = 7 # seconds to wait for Blink 5 | HA_SLEEP_TIME = 3 # seconds to wait for HA 6 | CAMERA_ENTITY_ID = 'camera.blink_living_room' 7 | CAMERA_NAME = 'Living_room' 8 | 9 | now = datetime.datetime.now() 10 | time_str = "{}_{}_{}_{}_{}_{}_{}".format( 11 | now.year, now.month, now.day, now.hour, 12 | now.minute, now.second, now.microsecond) 13 | 14 | # Trigger a capture now 15 | hass.services.call( 16 | 'blink', 'trigger_camera', 17 | {'name': CAMERA_NAME}) 18 | time.sleep(BLINK_SLEEP_TIME) 19 | 20 | # Update representation in HA 21 | hass.services.call( 22 | 'blink', 'blink_update') 23 | time.sleep(HA_SLEEP_TIME) 24 | 25 | # Save using snapshot 26 | folder = '/config/www/blink_{}_'.format(CAMERA_NAME) 27 | filename = folder + time_str + '.jpg' 28 | 29 | hass.services.call( 30 | 'camera', 'snapshot', 31 | {'entity_id': CAMERA_ENTITY_ID, 32 | 'filename': filename}) 33 | 34 | ## We need to wait for this file to be created before sending to notify 35 | time.sleep(HA_SLEEP_TIME) 36 | 37 | hass.services.call( 38 | 'telegram_bot', 'send_photo', { 39 | "caption": "New blink capture at {}:{}:{}".format(now.hour, 40 | now.minute, now.second), 41 | "file": filename 42 | }) 43 | -------------------------------------------------------------------------------- /days_until_date.py: -------------------------------------------------------------------------------- 1 | """ 2 | Script to create a sensor displaying the number of days until some event. 3 | Automate to update every day. 4 | """ 5 | 6 | EVENT = 'renew_letsencrypt' 7 | YEAR = 2018 8 | MONTH = 2 9 | DAY = 8 10 | 11 | day_of_interest = datetime.datetime(YEAR, MONTH, DAY) 12 | now = datetime.datetime.now() 13 | diff = day_of_interest - now 14 | 15 | hass.states.set('sensor.' + EVENT, diff.days,{ 16 | 'unit_of_measurement': 'days', 17 | 'friendly_name': 'Days until renew LetsEncrypt', 18 | 'icon': 'mdi:calendar' 19 | }) 20 | -------------------------------------------------------------------------------- /home_temperature.py: -------------------------------------------------------------------------------- 1 | tempEntities = ["sensor.bedroom_temperature", 2 | "sensor.blink_blink_camera_percy_temperature", 3 | "sensor.hall_temperature", 4 | "sensor.living_room_temperature"] 5 | 6 | sensorsTotal = len(tempEntities) 7 | tempsCounted = 0 8 | tempsTotal = 0 9 | 10 | # look up each entity_id 11 | for entity_id in tempEntities: 12 | # copy it's state 13 | state = hass.states.get(entity_id) 14 | 15 | # If not None, add up and increase counter 16 | if state.state is not 'unknown': 17 | tempsCounted = tempsCounted + 1 18 | tempsTotal = tempsTotal + float(state.state) 19 | 20 | # Get average 21 | averageTemp = tempsTotal / tempsCounted 22 | averageTemp = round(averageTemp, 1) 23 | # logger.warning("Ave indoor temp of : {}".format(averageTemp)) 24 | 25 | hass.states.set('sensor.average_indoor_temp', averageTemp, { 26 | 'unit_of_measurement': '°C', 27 | 'friendly_name': 'Indoor Temp', 28 | 'icon': 'mdi:thermometer', 29 | 'temps_counted': tempsCounted, 30 | 'temps_total': sensorsTotal 31 | }) 32 | -------------------------------------------------------------------------------- /hue_remote.py: -------------------------------------------------------------------------------- 1 | button_obj = hass.states.get('sensor.living_room_remote_status') 2 | button = button_obj.state 3 | logger.warning("Hello Robin, button preseed was {}".format(button)) 4 | 5 | if button == '1': 6 | hass.services.call('light', 'turn_on', { "entity_id" : 'light.lamp', 'color_name': 'green' }) 7 | elif button == '2': 8 | hass.services.call('light', 'turn_on', { "entity_id" : 'light.lamp', 'color_name': 'red' }) 9 | elif button == '3': 10 | hass.services.call('light', 'turn_on', { "entity_id" : 'light.lamp', 'color_name': 'purple' }) 11 | elif button == '4': 12 | hass.services.call('light', 'turn_on', { "entity_id" : 'light.lamp', 'color_name': 'yellow' }) 13 | -------------------------------------------------------------------------------- /process_deepstack_image.py: -------------------------------------------------------------------------------- 1 | """ 2 | Pass file_path into this script with automation: 3 | 4 | - action: 5 | - data_template: 6 | file_path: "{{ trigger.event.data.file }}" 7 | service: python_script.process_deepstack_image 8 | alias: Process deepstack 9 | condition: [] 10 | id: '1120092824611' 11 | trigger: 12 | - platform: event 13 | event_type: image_processing.file_saved 14 | 15 | """ 16 | 17 | file_path = data.get('file_path') 18 | file_name = file_path.split('/')[-1] # get just the file name e.g. img.jpg 19 | web_path = "http://192.168.1.164:8123/local/deepstack_images/" + file_name 20 | logger.info("Deepstack image web_path : {}".format(web_path)) 21 | 22 | hass.services.call( 23 | 'telegram_bot', 'send_photo', { 24 | "caption": "{}".format(file_name), 25 | "url": web_path} 26 | ) 27 | -------------------------------------------------------------------------------- /send_telegram.py: -------------------------------------------------------------------------------- 1 | FILE = "/config/www/apple.jpg" 2 | 3 | hass.services.call( 4 | 'telegram_bot', 'send_photo', { 5 | "caption": "{}".format(FILE), 6 | "file": FILE} 7 | ) 8 | -------------------------------------------------------------------------------- /train_state.py: -------------------------------------------------------------------------------- 1 | """ 2 | Script to receive alerts for train of interest (TOI). 3 | """ 4 | SCHEDULED = False 5 | 6 | # Set config 7 | MAIN_ENTITY_ID = 'sensor.next_train_to_wim' 8 | TOI_ENTITY_ID = 'sensor.morning_train' 9 | TOI_TIME = '07:15' 10 | 11 | # Set notification details 12 | MSG_TITLE = "Train Status Update" 13 | NOTIFY_ID = 'robins_and_marias_iphones' 14 | 15 | 16 | if hass.states.get(MAIN_ENTITY_ID).state == "No departures": 17 | logger.warn('No Departures') 18 | else: 19 | attributes = hass.states.get(MAIN_ENTITY_ID).attributes 20 | try: 21 | for train in attributes['next_trains']: 22 | if train['scheduled'] == TOI_TIME: 23 | SCHEDULED = True 24 | train_status = train['status'] 25 | 26 | if train_status != hass.states.get(TOI_ENTITY_ID).state: 27 | hass.states.set(TOI_ENTITY_ID, train_status) 28 | 29 | if train_status == 'LATE': 30 | hass.services.call('notify', NOTIFY_ID, { 31 | "title": MSG_TITLE, 32 | "message": "The {} will be late and its ETA is" 33 | .format(TOI_TIME) + train['estimated']}) 34 | 35 | hass.services.call('light', 'turn_on', { 36 | "entity_id": 'light.lamp', 37 | 'color_name': 'orange'}) 38 | 39 | elif train_status == 'CANCELLED': 40 | hass.services.call('notify', NOTIFY_ID, { 41 | "title": MSG_TITLE, 42 | "message": "The {} has been cancelled." 43 | .format(TOI_TIME)}) 44 | 45 | hass.services.call('light', 'turn_on', { 46 | "entity_id": 'light.lamp', 47 | 'color_name': 'red'}) 48 | break 49 | except: 50 | logger.warn('Error in train_state.py') 51 | 52 | if not SCHEDULED: 53 | hass.states.set(TOI_ENTITY_ID, 'No data') # check no data 54 | --------------------------------------------------------------------------------