Skip to content

Commit

Permalink
Added datetime handler (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
DEADSEC-SECURITY committed Dec 16, 2022
1 parent efcbc17 commit f16ab86
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 30 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

---

Alexa Actions is a custom skill that coupled with the [Alexa Media Player](https://github.com/custom-components/alexa_media_player/) custom component allows for actionable notificaitons to your Alexa device from Home Assistant.
Alexa Actions is a custom skill that coupled with the [Alexa Media Player](https://github.com/custom-components/alexa_media_player/) custom component allows for actionable notifications to your Alexa device from Home Assistant.

### Getting Started
To get started, head over to the [Alexa Actions Wiki](https://github.com/keatontaylor/alexa-actions/wiki).
Expand All @@ -17,4 +17,10 @@ Thanks to [@alandtse](https://github.com/alandtse) for his continued worked on t

If you've found this useful and want to support my sleepless nights:

Creator of Alexa Actions Keaton Taylor:

[![coffee](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/ogFeLZl)

Contributor and Supporter DeadSec-Security (AKA: Zeus):

[![coffee](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/zeus500k)
76 changes: 65 additions & 11 deletions lambda/lambda_function.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# VERSION 0.8.2

# UPDATE THESE VARIABLES WITH YOUR CONFIG

HOME_ASSISTANT_URL = 'https://yourinstall.com' # REPLACE WITH THE URL FOR YOUR HOME ASSISTANT
VERIFY_SSL = True # SET TO FALSE IF YOU DO NOT HAVE VALID CERTS
TOKEN = '' # ADD YOUR LONG LIVED TOKEN IF NEEDED OTHERWISE LEAVE BLANK
Expand All @@ -14,6 +13,7 @@
import json
import isodate
import prompts
from datetime import datetime
from typing import Union, Optional
from urllib3 import HTTPResponse

Expand Down Expand Up @@ -50,6 +50,7 @@
RESPONSE_NUMERIC = "ResponseNumeric"
RESPONSE_DURATION = "ResponseDuration"
RESPONSE_STRING = "ResponseString"
RESPONSE_DATE_TIME = "ResponseDateTime"


class Borg:
Expand Down Expand Up @@ -388,26 +389,79 @@ def can_handle(self, handler_input):
def handle(self, handler_input):
"""Handle the Date Time intent."""
logger.info('Date Intent Handler triggered')
ha_obj = HomeAssistant(handler_input)

dates = get_slot_value(handler_input, 'Dates')
times = get_slot_value(handler_input, 'Times')
date = get_slot_value(handler_input, 'Dates')
time = get_slot_value(handler_input, 'Times')

logger.debug(f'Dates: {dates}')
logger.debug(f'Times: {times}')
logger.debug(f'Dates: {date} of type {type(date)}')
logger.debug(f'Times: {time} of type {type(time)}')

if not dates and not times:
if not date and not time:
raise

data = handler_input.attributes_manager.request_attributes["_"]
speak_output = data[prompts.ERROR_SPECIFIC_DATE]
speak_output = ha_obj.post_ha_event(json.dumps({
**self._parse_date(date),
**self._parse_time(time)
}), RESPONSE_DATE_TIME)

return (
handler_input.response_builder
.speak(speak_output)
.ask('')
.response
.speak(speak_output)
.response
)

@staticmethod
def _parse_date(date: str) -> dict:
date_data = {
'day': None,
'month': None,
'year': None,
}

if not date:
return date_data

date = date.split('-')
date_len = len(date)

date_data['day'] = date[2] if date_len >= 3 else None
date_data['month'] = date[1] if date_len >= 2 else None
date_data['year'] = date[0] if date_len >= 1 else None

return date_data

@staticmethod
def _parse_time(time: str) -> dict:
time_data = {
'seconds': None,
'minute': None,
'hour': None,
}

if not time:
return time_data

# If the letter s is present then the hole time represents a second
if 's' in time.lower():
time_data['seconds'] = time.lower().replace('s', '')
return time_data
if 'm' in time.lower():
time_data['minute'] = time.lower().replace('m', '')
return time_data
if 'h' in time.lower():
time_data['hour'] = time.lower().replace('h', '')
return time_data

time = time.split(':')
time_len = len(time)

time_data['seconds'] = time[2] if time_len >= 3 else None
time_data['minute'] = time[1] if time_len >= 2 else None
time_data['hour'] = time[0] if time_len >= 1 else None

return time_data


class CancelOrStopIntentHandler(AbstractRequestHandler):
"""Single handler for Cancel and Stop Intent."""
Expand Down
5 changes: 0 additions & 5 deletions lambda/language_strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"ERROR_400": "Could not communicate with home assistant. Please check the Amazon CloudWatch logs in the custom skill developer console.",
"ERROR_ACOUSTIC": "Sorry I did not catch that... <break time='200ms'/> {}",
"ERROR_CONFIG": "Sorry, I am having trouble, please check your configuration, in the custom skill and try again.",
"ERROR_SPECIFIC_DATE": "Sorry, I can not do specific dates right now, try a duration instead, like... in 5 hours",
"HELP_MESSAGE": "This skill should be only reactively while triggered via home assistant.",
"OKAY": "Okay",
"STRING" : "You selected {}",
Expand All @@ -20,7 +19,6 @@
"ERROR_400": "Ich kann keine Verbindung zum home assistant vornehmen. Bitte prüfe die Amazon CloudWatch logdateien in der Alexa Entwickler Console.",
"ERROR_ACOUSTIC": "Das habe ich leider nicht verstanden... <break time='200ms'/> {}",
"ERROR_CONFIG": "Entschuldige, hier ist etwas schief gelaufen. Bitte prüfe deine Konfiugration im custom skill und versuche es erneut.",
"ERROR_SPECIFIC_DATE": "Ich kann leider noch kein spezifisches Datum verarbeiten. Bitte nutze stattedessen Zeiträume wie beispielsweise... in 5 Stunden.",
"HELP_MESSAGE": "Dieser Skill sollte nur reaktiv genutzt werden, wenn er via home assistant angestossen wird",
"OKAY": "Okay",
"STRING" : "Du hast {} gewählt",
Expand All @@ -35,7 +33,6 @@
"ERROR_400": "Impossible de communiquer avec home assistant. Veuillez verifier les logs Amazon CloudWAtch dans la console de développement de votre skill personnalisé.",
"ERROR_ACOUSTIC": "Désolé je n'ai pas compris... <break time='200ms'/> {}.",
"ERROR_CONFIG": "Désolé, j'ai quelques problèmes, veuillez verifier votre configuration dans votre skill personnalisé et essayez à nouveau.",
"ERROR_SPECIFIC_DATE": "Désolé, je ne peux pas gérer de date specifiques pour le moment. Essayez plutôt avec une période comme par exemple... dans 5 heures",
"HELP_MESSAGE": "Cette skill ne devrait être seulement réactive que depuis un appel de home assistant",
"OKAY": "Okay",
"STRING": "Vous avez choisi {}",
Expand All @@ -56,7 +53,6 @@
"ERROR_400": "Impossibile accedere ad Home Assistant. Controllare i logs su Amazon CloudWatch nella custom skill developer console.",
"ERROR_ACOUSTIC": "Mi dispiace, non ho capito... <break time='200ms'/> {}",
"ERROR_CONFIG": "Mi dispiace, c'e' qualche problema, per favore controlla la configurazione della custom skill e riprova.",
"ERROR_SPECIFIC_DATE": "Mi dispiace, non riesco a usare date specifiche in questo caso, prova con una data relative, ad esempio... tra 5 ore",
"HELP_MESSAGE": "Questa skill dovrebbe rispondere solo quando attivata da Home Assistant.",
"OKAY": "Okay",
"STRING" : "Hai selezionato {}",
Expand All @@ -74,7 +70,6 @@
"ERROR_400": "Não consigo me conectar ao Home Assistant. Por favor, cheque os registros no painel de desenvolvimento desta minha habilidade.",
"ERROR_ACOUSTIC": "Desculpe, não entendi... <break time='200ms'/> {}",
"ERROR_CONFIG": "Desculpe, estou com problemas. Por favor, cheque suas configurações nesta minha habilidade e tente novamente.",
"ERROR_SPECIFIC_DATE": "Desculpe, não consigo lidar com datas específicas por enquanto. <break time='500ms'/> Tente dizer um intervalo de tempo, por exemplo... <break time='200ms'/> em 5 horas",
"HELP_MESSAGE": "Essa habilidade deve ser acionada reativamente quando invocada pelo Home Assistant.",
"OKAY": "Tudo bem",
"STRING": "Você escolheu {}",
Expand Down
25 changes: 12 additions & 13 deletions lambda/prompts.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
# Alexa Prompts Language Constants
ERROR_401 = "ERROR_401"
ERROR_404 = "ERROR_404"
ERROR_400 = "ERROR_400"
ERROR_ACOUSTIC = "ERROR_ACOUSTIC"
ERROR_CONFIG = "ERROR_CONFIG"
ERROR_SPECIFIC_DATE = "ERROR_SPECIFIC_DATE"
HELP_MESSAGE = "HELP_MESSAGE"
OKAY = "OKAY"
STRING = "STRING"
SELECTED = "SELECTED"
SKILL_NAME = "SKILL_NAME"
STOP_MESSAGE = "STOP_MESSAGE"
WELCOME_MESSAGE = "WELCOME_MESSAGE"
ERROR_401 = 'ERROR_401'
ERROR_404 = 'ERROR_404'
ERROR_400 = 'ERROR_400'
ERROR_ACOUSTIC = 'ERROR_ACOUSTIC'
ERROR_CONFIG = 'ERROR_CONFIG'
HELP_MESSAGE = 'HELP_MESSAGE'
OKAY = 'OKAY'
STRING = 'STRING'
SELECTED = 'SELECTED'
SKILL_NAME = 'SKILL_NAME'
STOP_MESSAGE = 'STOP_MESSAGE'
WELCOME_MESSAGE = 'WELCOME_MESSAGE'

0 comments on commit f16ab86

Please sign in to comment.