Skip to content

Commit

Permalink
Feat: Person Detection
Browse files Browse the repository at this point in the history
This adds person detection into the main codebase for those who want to experiment with that feature.
  • Loading branch information
keatontaylor committed May 15, 2020
1 parent d10b9a8 commit 334b29b
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions lambda_function.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## VERSION 0.5.3
## VERSION 0.6.0

import logging
import urllib3
Expand Down Expand Up @@ -47,7 +47,7 @@ def get_health_check(self):
'{}/api/config'.format(HOME_ASSISTANT_URL),
headers={
'Authorization': 'Bearer {}'.format(self.token),
'Content-Type': 'application/json',
'Content-Type': 'application/json'
},
)

Expand Down Expand Up @@ -77,7 +77,7 @@ def get_ha_state(self):
'{}/api/states/{}'.format(HOME_ASSISTANT_URL, "input_text.alexa_actionable_notification"),
headers={
'Authorization': 'Bearer {}'.format(self.token),
'Content-Type': 'application/json',
'Content-Type': 'application/json'
},
)

Expand All @@ -98,22 +98,30 @@ def get_ha_state(self):

return self.text

def post_ha_event(self, response: str):
def post_ha_event(self, handler_input, response: str):
"""Send event to HA."""

http = urllib3.PoolManager(
cert_reqs='CERT_REQUIRED' if VERIFY_SSL else 'CERT_NONE',
timeout=urllib3.Timeout(connect=10.0, read=10.0)
)

person_id = ""
person_speak_name = ""
try:
person_id = handler_input.request_envelope.context.system.person.person_id
person_speak_name = f'<alexa:name type="first" personId="{person_id}"/>'
except:
print('I could not recognize the person who spoke to me.')

response = http.request(
'POST',
'{}/api/events/alexa_actionable_notification'.format(HOME_ASSISTANT_URL),
headers={
'Authorization': 'Bearer {}'.format(self.token),
'Content-Type': 'application/json',
'Content-Type': 'application/json'
},
body=json.dumps({"event_id": self.event_id, "event_response": response, "text": self.text}).encode('utf-8')
body=json.dumps({"event_id": self.event_id, "event_response": response, "text": self.text, "event_person_id": person_id}).encode('utf-8')
)

if response.status == 401:
Expand All @@ -126,7 +134,7 @@ def post_ha_event(self, response: str):
print(f"{response.status} Error", response.data)
return "Could not communicate with home assistant. Please check the Amazon CloudWatch logs in the custom skill developer console."

return "Okay"
return "Okay" + person_speak_name

def get_value_for_slot(self, handler_input, slot_name):
""""Get value from slot, also know as the (why does amazon make you do this code)"""
Expand Down Expand Up @@ -173,7 +181,7 @@ def handle(self, handler_input):
home_assistant_object = HomeAssistant(handler_input)
home_assistant_object.get_ha_state()

speak_output = home_assistant_object.post_ha_event("ResponseYes")
speak_output = home_assistant_object.post_ha_event(handler_input, "ResponseYes")
return (
handler_input.response_builder
.speak(speak_output)
Expand All @@ -196,7 +204,7 @@ def handle(self, handler_input):
home_assistant_object = HomeAssistant(handler_input)
home_assistant_object.get_ha_state()

speak_output = home_assistant_object.post_ha_event("ResponseNo")
speak_output = home_assistant_object.post_ha_event(handler_input, "ResponseNo")
return (
handler_input.response_builder
.speak(speak_output)
Expand All @@ -220,8 +228,7 @@ def handle(self, handler_input):

selection = home_assistant_object.get_value_for_slot(handler_input, "Selections")

print(handler_input.request_envelope)
home_assistant_object.post_ha_event(selection)
home_assistant_object.post_ha_event(handler_input, selection)
speak_output = "You selected " + selection
return (
handler_input.response_builder
Expand Down Expand Up @@ -285,7 +292,7 @@ def handle(self, handler_input):
home_assistant_object = HomeAssistant(handler_input)
home_assistant_object.get_ha_state()

speak_output = home_assistant_object.post_ha_event("ResponseNone")
speak_output = home_assistant_object.post_ha_event(handler_input, "ResponseNone")
print(handler_input.request_envelope.request.reason)

return handler_input.response_builder.response
Expand Down

0 comments on commit 334b29b

Please sign in to comment.