Hang up phone via Alexa routine [AppDaemon, Obihai]

This is my first foray into AppDaemon and my first real work with Python, so the code might not be pretty, but this has been working for me for a few weeks.

I have CID info announced in my house through TTS. When I want to reject a call, I can either say “Alexa, hang up the phone”, or press one of a couple Flic buttons that I have conveniently placed. Both the Alexa routine and the Flic press call a Home Assistant script - that was the easiest way I found to kick things off from Alexa.

My AppDaemon app watches an input_boolean flipped by the script. It then parses the Obihai status page, finds the “reject” url and taps it. This doesn’t really hang up the call - it just stops the phone ringing and lets the call go to voicemail.

You can also have AppDaemon look for words in the CID info and auto-reject calls based on keywords. You can do that via AppDaemon or an automation triggered by a template trigger.

Here’s my app. I’ve isolated this from a more-complicated app, but I’m pretty sure this should work as-is. You’ll need to update the user/pass and the IP for your Obi in the statusurl and create the input_boolean, script and Alexa routine…

Hope someone finds this useful!

import appdaemon.plugins.hass.hassapi as hass
import requests
import xml.etree.ElementTree


class hangupCall(hass.Hass):

  def initialize(self):
    self.listen_state(self.hangup_call_if_boolean_changes, "input_boolean.hang_up_call")
              
  def hangup_call_if_boolean_changes(self, entity, attribute, old, new, kwargs):
    #watching a boolean so that I can trigger from HA via Alexa, etc
    #make sure to only catch the ON so that it doesn't fire again when
    #this code turns the bool back off
    if new == "on":
      self.log("Processing boolean change")
      self.hangup_call()
              
  def hangup_call(self):
    self._username = 'admin'
    self._password = 'PASSWORD'
    statusurl = 'http://10.0.0.7/callstatus.htm'
    #self.log("Attempting to hang up call")
    
    try:
      resp = requests.get(statusurl, auth=requests.auth.HTTPDigestAuth(self._username,self._password), timeout=2)
    
      lines = resp.text
      start = lines.find("form action")
      if start != -1:
        temp_str = lines[start + 14:]
    
        end = temp_str.find("' method")
        if end != -1:
          rejecturl = 'http://' + str(temp_str[:end])
          self.log("Hanging up call!")
          #self.log("rejecturl: "+rejecturl)
          resp = requests.get(rejecturl, auth=requests.auth.HTTPDigestAuth(self._username,self._password), timeout=2)
        
    except requests.exceptions.RequestException as e:
      self.log(e)
      
    self.turn_off("input_boolean.hang_up_call")
1 Like

now that amazon discontinued the echo connect, I used your code and modified this to still announce the callers. thanks!