Skip to content

Instantly share code, notes, and snippets.

@haberda
Last active October 2, 2024 12:44
Show Gist options
  • Save haberda/b65113a1e505759f333a3c5f8feb4fef to your computer and use it in GitHub Desktop.
Save haberda/b65113a1e505759f333a3c5f8feb4fef to your computer and use it in GitHub Desktop.
AppDaemon Reminder app
import hassapi as hass
import datetime
class reminder(hass.Hass):
def initialize(self):
self.set_namespace("reminder")
self.listen_event(self.set_reminder,'set_reminder', namespace='default')
self.listen_event(self.remove_reminder,'remove_reminder', namespace='default')
domain = 'reminder'
reminder_list = list(self.get_state(domain).keys())
if len(reminder_list) > 0:
for reminder in reminder_list:
time = self.parse_datetime(self.get_state(reminder))
att = self.get_state(reminder, attribute='all')
dt = datetime.datetime.now()
if dt.timestamp() > time.timestamp():
self.remove_entity(reminder, namespace='default')
self.remove_entity(reminder, namespace='reminder')
else:
self.run_at(self.send_reminder, time, entity_id = reminder)
self.set_state(**att, namespace='default')
self.log('Subscribed to ' + reminder)
def set_reminder(self, event, data, kwargs):
"""Creates Reminder Entities to Track"""
if 'name' in data and 'time' in data and 'title' in data and 'recipient' in data and 'message' in data:
entity_name = 'reminder.' + data['name']
entity_name = entity_name.replace(" ", "_")
else:
self.log('No reminder name defined, exiting')
return
self.set_state(entity_name, state=data['time'], attributes = {
"message": data['message'],
"title": data['title'],
"recipient": data['recipient']}, namespace='default')
self.add_entity(entity_name, state=data['time'], attributes = {
"message": data['message'],
"title": data['title'],
"recipient": data['recipient']}, namespace='reminder')
self.restart_app(self.name)
def send_reminder(self, kwargs):
"""Sends reminder then deletes entity"""
self.log('send_reminder')
state=self.get_state(kwargs['entity_id'], attribute="all")
attributes = state['attributes']
self.notify(attributes['message'], title = attributes['title'], name = attributes['recipient'], namespace='default')
self.remove_entity(kwargs['entity_id'], namespace='default')
self.remove_entity(kwargs['entity_id'], namespace='reminder')
def remove_reminder (self, event, data, kwargs):
self.remove_entity(data['entity_id'], namespace='default')
self.remove_entity(data['entity_id'], namespace='reminder')
self.restart_app(self.name)
@haberda
Copy link
Author

haberda commented Nov 26, 2022

I added some gists for the UI stuff. There are 2 scripts and an automation. The automation builds and maintains an input select with a list of the active reminders. However in the UI I use a custom card called autoentities that just populates any active reminders.

Automation:
https://gist.github.com/haberda/e107c442dee75c58bcd2db322da3a17e

Sctipts:
https://gist.github.com/haberda/b509816370af6c421b36dc8049a0a47f

UI:
https://gist.github.com/haberda/07a3a28e4bd2efc82754382cc0fe34d0

It could be done better, but this works for now. The automation, scripts, and helpers should probably be in a package for distribution, but none of this is required to make the appdaemon app work so I never packaged it up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment