Skip to content

Commit

Permalink
Fix sending wrong events to HA on display devices (#43)
Browse files Browse the repository at this point in the history
* Fix sending wrong events to HA

+Validates reason as EXCEEDED_MAX_REPROMPTS

* Bump version 0.7.3

* fix conflicts

* deleting old file
  • Loading branch information
dudu631 committed Sep 9, 2020
1 parent af9e6a5 commit ae3d9e4
Showing 1 changed file with 27 additions and 24 deletions.
51 changes: 27 additions & 24 deletions lambda/lambda_function.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## VERSION 0.8.1
## VERSION 0.8.2

# UPDATE THESE VARIABLES WITH YOUR CONFIG
HOME_ASSISTANT_URL = 'https://yourhainstall.com' # REPLACE WITH THE URL FOR YOUR HA FRONTEND
Expand All @@ -19,9 +19,11 @@
from ask_sdk_core.dispatch_components import AbstractExceptionHandler
from ask_sdk_core.dispatch_components import AbstractRequestInterceptor
from ask_sdk_core.handler_input import HandlerInput
from ask_sdk_model import SessionEndedReason
from ask_sdk_model.slu.entityresolution import StatusCode
from ask_sdk_model import Response


logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

Expand All @@ -46,14 +48,14 @@ class HomeAssistant(Borg):
"""HomeAssistant Wrapper Class."""
def __init__(self, handler_input=None):
Borg.__init__(self)
if handler_input:
if handler_input:
self.handler_input = handler_input

self.token = self._fetch_token() if TOKEN == "" else TOKEN

if not hasattr(self, 'ha_state') or self.ha_state is None:
self.get_ha_state()

def _clear_state(self):
self.ha_state = None

Expand All @@ -79,14 +81,14 @@ def _check_response_errors(self, response):

def get_ha_state(self):
"""Get State from HA."""

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

response = http.request(
'GET',
'GET',
'{}/api/states/{}'.format(HOME_ASSISTANT_URL, INPUT_TEXT_ENTITY),
headers={
'Authorization': 'Bearer {}'.format(self.token),
Expand All @@ -102,16 +104,16 @@ def get_ha_state(self):
}

decoded_response = json.loads(response.data.decode('utf-8'))['state']

self.ha_state = {
"error": False,
"event_id": json.loads(decoded_response)['event'],
"text": json.loads(decoded_response)['text']
}

def post_ha_event(self, response: str, response_type: str, **kwargs):
"""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)
Expand All @@ -123,26 +125,26 @@ def post_ha_event(self, response: str, response_type: str, **kwargs):
"event_response_type": response_type
}
request_body.update(kwargs)

if self.handler_input.request_envelope.context.system.person:
person_id = self.handler_input.request_envelope.context.system.person.person_id
request_body['event_person_id'] = person_id

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

error = self._check_response_errors(response)

if error:
return error

data = self.handler_input.attributes_manager.request_attributes["_"]
speak_output = data[prompts.OKAY]
self._clear_state()
Expand All @@ -166,7 +168,7 @@ def can_handle(self, handler_input):

def handle(self, handler_input):
home_assistant_object = HomeAssistant(handler_input)
speak_output = home_assistant_object.ha_state['text']
speak_output = home_assistant_object.ha_state['text']

return (
handler_input.response_builder
Expand Down Expand Up @@ -199,7 +201,7 @@ def can_handle(self, handler_input):

def handle(self, handler_input):
home_assistant_object = HomeAssistant(handler_input)
speak_output = home_assistant_object.post_ha_event(RESPONSE_NO, RESPONSE_NO)
speak_output = home_assistant_object.post_ha_event(RESPONSE_NO, RESPONSE_NO)

return (
handler_input.response_builder
Expand Down Expand Up @@ -273,13 +275,13 @@ def can_handle(self, handler_input):

def handle(self, handler_input):
home_assistant_object = HomeAssistant(handler_input)

dates = ask_utils.get_slot_value(handler_input, "Dates")
times = ask_utils.get_slot_value(handler_input, "Times")

if not dates and not times:
raise

data = handler_input.attributes_manager.request_attributes["_"]
speak_output = data[prompts.ERROR_SPECIFIC_DATE]

Expand Down Expand Up @@ -315,8 +317,9 @@ def can_handle(self, handler_input):
return ask_utils.is_request_type("SessionEndedRequest")(handler_input)

def handle(self, handler_input):
home_assistant_object = HomeAssistant()
speak_output = home_assistant_object.post_ha_event(RESPONSE_NONE, RESPONSE_NONE)
home_assistant_object = HomeAssistant(handler_input)
if handler_input.request_envelope.request.reason == SessionEndedReason.EXCEEDED_MAX_REPROMPTS:
home_assistant_object.post_ha_event(RESPONSE_NONE, RESPONSE_NONE)

return handler_input.response_builder.response

Expand Down Expand Up @@ -352,8 +355,8 @@ def can_handle(self, handler_input, exception):
def handle(self, handler_input, exception):
print("CatchAllExceptionHandler")
logger.error(exception, exc_info=True)
home_assistant_object = HomeAssistant()
home_assistant_object = HomeAssistant()

data = handler_input.attributes_manager.request_attributes["_"]
if hasattr(home_assistant_object, 'ha_state') and home_assistant_object.ha_state != None and 'text' in home_assistant_object.ha_state:
speak_output = data[prompts.ERROR_ACOUSTIC].format(home_assistant_object.ha_state['text'])
Expand All @@ -373,7 +376,7 @@ def handle(self, handler_input, exception):

class LocalizationInterceptor(AbstractRequestInterceptor):
"""Add function to request attributes, that can load locale specific data."""

def process(self, handler_input):
locale = handler_input.request_envelope.request.locale
logger.info("Locale is {}".format(locale[:2]))
Expand Down

0 comments on commit ae3d9e4

Please sign in to comment.