Skip to content

Commit

Permalink
Merge pull request #89 from AndrewSteel/main
Browse files Browse the repository at this point in the history
Currency Convertor with new localization
  • Loading branch information
dinki authored Oct 31, 2024
2 parents 2df0602 + f2d3c46 commit 870cfd0
Show file tree
Hide file tree
Showing 13 changed files with 1,262 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Currency Convertor

## Prequisites

### Pyscript

You must have Pyscript installed [See installation video](https://www.youtube.com/watch?v=jpJxZaisbGQ)

Download
* viewassist-currencyconvertor.py
* viewassist-response_handler.py
* viewassist_currencyconvertor_digits.yaml

and place this files in your **pyscripts** directory on your Home Assistant server. \
The pyscript variables **hass_all_imports** and **hass_is_global** need to be set to true. This can be done in the UI or via an entry
```
pyscript:
allow_all_imports: true
hass_is_global: true
```
in **configuration.yaml**.

### Intent Script

If not exist create a directory **intent_script** in your 'config' directory on your Home Assistant server. \
Download
* viewassist_currenyconvertor.intent.yaml

and place it in this 'intent_script' directory. \
The 'intent_script' directory must be accessed via the entry

> intent_script: !include_dir_merge_named intent_script/
in **configuration.yaml**.

### Custom Sentences

If not exist create a directory **custom_sentences** in your 'config' directory on your Home Assistant server. \
If not exist create your preferred **language directory** (esp. 'en') in this 'custom_sentences' directory. \
Download the
* viewassist_currencyconvertor_intent.yaml
* viewassist_currencyconvertor_response.yaml
* viewassist_currencyconvertor_currencies.yaml

files from your preferred language directory (esp. 'en') and place this files in the corresponding language directory on your Home Assistant server.

## Rapid API Key

You must get an API key from **rapidapi.com** for the [required API](https://rapidapi.com/pwshub-pwshub-default/api/crypto-market-prices) (totally free no credit card needed) \
You must add your API key to the 'rapidapikey' field in the blueprint.

## Blueprint

**DEV Blueprint Link! Remove before push to main!!**

[![Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.](https://my.home-assistant.io/badges/blueprint_import.svg)](https://my.home-assistant.io/redirect/blueprint_import/?blueprint_url=https%3A%2F%2Fraw.githubusercontent.com%2FAndrewSteel%2FView-Assist%2Frefs%2Fheads%2Fmain%2FView_Assist_custom_sentences%2Fcommunity_contributions%2FCurrency_Convertor%2Fblueprint-currencyconvertor.yaml)
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
# Currency Convertor
blueprint:
name: View Assist - Currency Convertor
description: Convert currency from one type to another (View Assist Currency Convertor v 0.2.0)
domain: automation
input:
rapid_apikey:
name: Rapid Api Key
description: Your Rapid Api Key
default: YOURAPIKEY
tts_device:
name: Text to Speech Device
description: Your TTS Device to play media
default: tts.piper
tts_language:
name: Text to Speech language
description: Your preferred language for TTS
default: en_US
tts_model:
name: Text to Speech voice model
description: Your preferred voice model for TTS
default: en_US-amy-medium
group_entity:
name: Group Entity
description: The group that holds the list of View Assist satellites(example
group.viewassist_satellites)
selector:
entity:
filter:
- domain:
- group
multiple: false
default: group.viewassist_satellites
view_path:
name: Dashboard Info view
description: The View Assist dashboard view to use for displaying information (example /dashboard-viewassist/info)
default: /dashboard-viewassist/info
trigger:
- platform: mqtt
topic: viewassist/intent/currencyconvertor
condition: []
action:
- variables:
rapidapikey: !input rapid_apikey
tts_device: !input tts_device
tts_language: !input tts_language
tts_model: !input tts_model
group_entity: !input group_entity
view: !input view_path
target_satellite_device: |-
{% for sat in expand(group_entity) %}
{% if (device_id(sat.attributes.mic_device) in trigger.payload_json['devices'] ) or (device_id(sat.attributes.display_device) in trigger.payload_json['devices'] ) %}
{{ sat.entity_id }}
{% endif %}
{% endfor %}
target_display_device: "{{ device_id(state_attr(target_satellite_device, 'display_device')) }}"
target_mediaplayer_device: "{{ state_attr(target_satellite_device, 'mediaplayer_device') }}"
target_satellite_device_type: "{{ state_attr(target_satellite_device, 'type') }}"
enabled: true
- choose:
- conditions:
- condition: template
value_template: "{{ trigger.payload_json['intent'] == 'exchange'}}"
sequence:
- action: pyscript.currencyconvertor
data:
rapidapikey: "{{ rapidapikey }}"
currencyto: "{{ trigger.payload_json['tocurrency'] }}"
response_variable: conversion_response
- if:
- condition: template
value_template: "{{ conversion_response['error'] == 0 }}"
then:
- action: pyscript.response_handler
data:
intent: ExchangeCurrency
response: speech
custom: viewassist-currencyconvertor
exchange: "{{ conversion_response['data']['value'] | round(2) }}"
exchangecurrency: "{{ trigger.payload_json['tocurrency'] }}"
response_variable: response_speech
- action: media_player.play_media
target:
entity_id: "{{target_mediaplayer_device}}"
data:
media_content_id: >-
media-source://tts/{{
tts_device}}?message={{response_speech['message'] | replace(" ", "+")}}&language={{
tts_language}}&voice={{tts_model}}
media_content_type: provider
enabled: true
- if:
- condition: template
value_template: >-
{% if target_satellite_device_type != 'audio_only'
%}true{% else %}false{% endif %}
then:
- action: pyscript.response_handler
data:
intent: ExchangeCurrency
response: screen
custom: viewassist-currencyconvertor
basecurrency: "{{ conversion_response['data']['from'] }}"
exchange: "{{ conversion_response['data']['value'] | round(2) }}"
exchangecurrency: "{{ conversion_response['data']['to'] }}"
response_variable: response_screen
- action: python_script.set_state
data:
entity_id: "{{ target_satellite_device }}"
title: Currency Convertor
message_font_size: 4vw
message: "{{ response_screen['message'] }}"
enabled: true
- data:
path: "{{ view }}"
target:
device_id: "{{target_display_device}}"
action: browser_mod.navigate
enabled: true
else:
- action: media_player.play_media
target:
entity_id: "{{target_mediaplayer_device}}"
data:
media_content_id: >-
media-source://tts/{{
tts_device}}?message=Error+{{conversion_response['error']}}+Message+{{
conversion_response['message']}}&language={{tts_language}}&voice={{tts_model}}
media_content_type: provider
enabled: true
- if:
- condition: template
value_template: "{{ target_satellite_device_type != 'audio_only' }}"
then:
- data:
entity_id: "{{ target_satellite_device }}"
title: Currency Convertor
message_font_size: 4vw
message: >-
Error {{ conversion_response['error'] }} Message {{
conversion_response['message'] }}
action: python_script.set_state
- data:
path: "{{ view }}"
target:
device_id: "{{target_display_device}}"
action: browser_mod.navigate
enabled: true
- conditions:
- condition: template
value_template: "{{ trigger.payload_json['intent'] == 'convert'}}"
sequence:
- action: pyscript.currencyconvertor
data:
rapidapikey: "{{ rapidapikey }}"
currencyfrom: "{{ trigger.payload_json['fromcurrency'] }}"
currencyto: "{{ trigger.payload_json['tocurrency'] }}"
response_variable: conversion_response
- if:
- condition: template
value_template: "{{ conversion_response['error'] == 0 }}"
then:
- action: pyscript.response_handler
data:
intent: ConvertCurrency
response: speech
custom: viewassist-currencyconvertor
amount: "{{ conversion_response['data']['amount'] }}"
fromcurrency: "{{ trigger.payload_json['fromcurrency'].split(' ',2)[1] }}"
convert: "{{ conversion_response['data']['value'] | round(2) }}"
tocurrency: "{{ trigger.payload_json['tocurrency'] }}"
response_variable: response_speech
- action: media_player.play_media
target:
entity_id: "{{target_mediaplayer_device}}"
data:
media_content_id: >-
media-source://tts/{{
tts_device}}?message={{response_speech['message'] | replace(" ", "+")}}&language={{
tts_language}}&voice={{tts_model}}
media_content_type: provider
enabled: true
- if:
- condition: template
value_template: >-
{% if target_satellite_device_type != 'audio_only'
%}true{% else %}false{% endif %}
then:
- action: pyscript.response_handler
data:
intent: ConvertCurrency
response: screen
custom: viewassist-currencyconvertor
amount: "{{ conversion_response['data']['amount'] }}"
fromcurrency: "{{ conversion_response['data']['from'] }}"
convert: "{{ conversion_response['data']['value'] | round(2) }}"
tocurrency: "{{ conversion_response['data']['to'] }}"
response_variable: response_screen
- data:
entity_id: "{{ target_satellite_device }}"
title: Currency Convertor
message_font_size: 4vw
message: "{{ response_screen['message'] }}"
action: python_script.set_state
- data:
path: "{{ view }}"
target:
device_id: "{{target_display_device}}"
action: browser_mod.navigate
enabled: true
else:
- action: media_player.play_media
target:
entity_id: "{{target_mediaplayer_device}}"
data:
media_content_id: >-
media-source://tts/{{
tts_device}}?message=Error+{{conversion_response['error']}}+Message+{{
conversion_response['message']}}&language={{tts_language}}&voice={{tts_model}}
media_content_type: provider
enabled: true
- if:
- condition: template
value_template: "{{ target_satellite_device_type != 'audio_only' }}"
then:
- data:
entity_id: "{{ target_satellite_device }}"
title: Currency Convertor
message_font_size: 4vw
message: >-
Error {{ conversion_response['error'] }} Message {{
conversion_response['message'] }}
action: python_script.set_state
- data:
path: "{{ view }}"
target:
device_id: "{{target_display_device}}"
action: browser_mod.navigate
enabled: true
mode: single
Loading

0 comments on commit 870cfd0

Please sign in to comment.