That’s actually better. I had some old yaml automations, but have been trying mo move all automations to AppDaemon. I only went back because when it didn’t work, I found more config examples in yaml.
Well, pure appademon works:
class TelegramBotEventListener(hass.Hass):
"""
Event listener for Telegram callbacks.
"""
def initialize(self):
"""
Listen to Telegram events of interest.
"""
# self.listen_event(self.receive_telegram_command, 'telegram_command')
self.listen_event(self.receive_telegram_callback, 'telegram_callback')
def receive_telegram_callback(self, event_id, payload_event, *args):
"""
Match telegram commands with actions.
"""
if payload_event['data'] == '/espresso_on':
self.turn_on('switch.switch')
if payload_event['data'] == '/espresso_off':
self.turn_off('switch.switch')
if payload_event['data'] == '/fountain_on':
self.turn_on('switch.fountain')
if payload_event['data'] == '/fountain_off':
self.turn_off('switch.fountain')
if payload_event['data'] == '/lights_off':
self.turn_on('script.scene_all_lights_off')
if payload_event['data'] == '/all_off':
self.turn_on('script.everyone_left_turn_off_everything')
if payload_event['data'] == '/set_alarm_away':
self.turn_on('script.alarm_armed_away')
if payload_event['data'] == '/set_alarm_all_off':
self.turn_on('script.alarm_armed_away')
self.turn_on('script.everyone_left_turn_off_everything')
if payload_event['data'] == '/guest_mode_off':
self.turn_off('input_boolean.guest_mode')
Together with this:
"""
Notify (actionable) me if the 'entity' is left 'on' for more than 'start_after' seconds.
Notify every 'time_between_notifications' seconds.
After 'end_after' seconds, turn off and notify.
Set variables in app config (apps.yaml)
"""
import appdaemon.plugins.hass.hassapi as hass
import datetime
class NotifyStatus(hass.Hass):
def initialize(self):
"""
Initialize the timers and set listen_state.
"""
self.starttime = datetime.datetime.now()
self.timer = None
self.listen_state(self.StartTimer,self.args['entity'])
def StartTimer(self, entity, attribute, old, new, kwargs):
"""
Cancel timer if entity is turned 'off'.
Otherwise note the time, and start the loop (SendNotification)
"""
if new == 'off':
self.cancel_timer(self.timer)
elif new == 'on' and old == 'off':
if self.timer != None:
self.cancel_timer(self.timer)
self.starttime = datetime.datetime.now()
self.timer = self.run_in(self.SendNotification,self.args['start_after'])
def SendNotification(self, kwargs):
"""
Notify me about leaving the 'entity' on. Repeat every 'time_between_notifications' seconds.
After 'end_after' seconds, if 'switch_off' is True, automatically turn off, and notify me.
Remember to cancel timer before each recursive callback, as well as after last action ending the loop.
"""
delta = datetime.datetime.now() - self.starttime
seconds = int(datetime.timedelta.total_seconds(delta))
minutes = round(seconds/60)
keyboard_1 = [[(self.args['button_1'], self.args['command_1']),
(self.args['button_3'], self.args['command_3'])],
[(self.args['button_2'], self.args['command_2'])]]
message_1 = str(self.friendly_name(self.args['entity'])) + " has been " + str(self.args["on_open"]) + " for " + str(minutes) + " minutes"
keyboard_2 = [[(self.args['button_3'], self.args['command_3']),
(self.args['button_1'], self.args['command_1'])],
[(self.args['button_2'], self.args['command_2'])]]
notify = self.args['notify']
if seconds < self.args['end_after']:
self.call_service(self.args['notify'],
title=self.args['title'],
target=self.args['user_id'],
message=message_1,
inline_keyboard=keyboard_1)
# self.log(self.args['entity'] + " has been on for " + str(minutes) + " minutes") # for troubleshooting
self.timer = self.run_in(self.SendNotification,self.args['time_between_notifications'])
else:
if self.args['switch_off']:
self.turn_off(self.args['entity'])
switchofftext = ", i turned it off."
self.turn_off('switch.switch')
else:
switchofftext = "."
message_2 = str(self.friendly_name(self.args['entity'])) + " has been " + str(self.args["on_open"]) + " for " + str(minutes) + " minutes" + switchofftext
self.call_service(self.args['notify'],
title=self.args['title'],
target=self.args['user_id'],
message=message_2,
inline_keyboard=keyboard_2)
That said, do you have something in AppDaemon for substituting a removekeyboard
automation? I’m having issues translating my automation (I’d want to include this so the keyboard is removed once a button is pressed):
- id: 'telegramremoveinline' # https://community.home-assistant.io/t/telegram-bot-notifications-and-communication/50247
alias: 'Telegram callback to remove keyboard'
hide_entity: true
initial_state: True
trigger:
platform: event
event_type: telegram_callback
event_data:
data: '/removekeyboard'
action:
- service: telegram_bot.answer_callback_query
data_template:
callback_query_id: '{{ trigger.event.data.id }}'
message: 'OK'
- service: telegram_bot.edit_replymarkup
data_template:
message_id: '{{ trigger.event.data.message.message_id }}'
chat_id: '{{ trigger.event.data.user_id }}'
inline_keyboard: []