Skip to content

Commit

Permalink
chore: release 2023-09-14
Browse files Browse the repository at this point in the history
  • Loading branch information
alandtse authored Sep 15, 2023
2 parents 1f7eb50 + fe67b76 commit af99698
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 22 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ repos:
- id: commitizen
stages: ["commit-msg"]
repo: https://github.com/commitizen-tools/commitizen
rev: 3.5.2
rev: 3.8.2
# --- Linters ---
# - hooks:
# - id: dockerfile_lint
Expand All @@ -55,7 +55,7 @@ repos:
hooks:
- id: prettier
exclude: ^custom_components/alexa_media/translations|CHANGELOG.md
rev: v3.0.0-alpha.9-for-vscode
rev: v3.0.3
- repo: https://github.com/asottile/pyupgrade
rev: v3.4.0
hooks:
Expand Down
5 changes: 5 additions & 0 deletions custom_components/alexa_media/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@
CONF_INCLUDE_DEVICES,
CONF_OAUTH,
CONF_OTPSECRET,
CONF_PUBLIC_URL,
CONF_QUEUE_DELAY,
DATA_ALEXAMEDIA,
DATA_LISTENER,
DEFAULT_EXTENDED_ENTITY_DISCOVERY,
DEFAULT_PUBLIC_URL,
DEFAULT_QUEUE_DELAY,
DEPENDENT_ALEXA_COMPONENTS,
DOMAIN,
Expand Down Expand Up @@ -290,6 +292,9 @@ async def login_success(event=None) -> None:
CONF_QUEUE_DELAY: config_entry.options.get(
CONF_QUEUE_DELAY, DEFAULT_QUEUE_DELAY
),
CONF_PUBLIC_URL: config_entry.options.get(
CONF_PUBLIC_URL, DEFAULT_PUBLIC_URL
),
CONF_EXTENDED_ENTITY_DISCOVERY: config_entry.options.get(
CONF_EXTENDED_ENTITY_DISCOVERY, DEFAULT_EXTENDED_ENTITY_DISCOVERY
),
Expand Down
8 changes: 8 additions & 0 deletions custom_components/alexa_media/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@
CONF_OAUTH,
CONF_OTPSECRET,
CONF_PROXY_WARNING,
CONF_PUBLIC_URL,
CONF_QUEUE_DELAY,
CONF_SECURITYCODE,
CONF_TOTP_REGISTER,
DATA_ALEXAMEDIA,
DEFAULT_EXTENDED_ENTITY_DISCOVERY,
DEFAULT_PUBLIC_URL,
DEFAULT_QUEUE_DELAY,
DOMAIN,
ISSUE_URL,
Expand Down Expand Up @@ -797,6 +799,12 @@ async def async_step_init(self, user_input=None):
CONF_QUEUE_DELAY, DEFAULT_QUEUE_DELAY
),
): vol.All(vol.Coerce(float), vol.Clamp(min=0)),
vol.Optional(
CONF_PUBLIC_URL,
default=self.config_entry.options.get(
CONF_PUBLIC_URL, DEFAULT_PUBLIC_URL
),
): str,
vol.Required(
CONF_EXTENDED_ENTITY_DISCOVERY,
default=self.config_entry.options.get(
Expand Down
4 changes: 4 additions & 0 deletions custom_components/alexa_media/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
CONF_INCLUDE_DEVICES = "include_devices"
CONF_EXCLUDE_DEVICES = "exclude_devices"
CONF_QUEUE_DELAY = "queue_delay"
CONF_PUBLIC_URL = "public_url"
CONF_EXTENDED_ENTITY_DISCOVERY = "extended_entity_discovery"
CONF_SECURITYCODE = "securitycode"
CONF_OTPSECRET = "otp_secret"
Expand All @@ -59,6 +60,7 @@

DEFAULT_EXTENDED_ENTITY_DISCOVERY = False
DEFAULT_QUEUE_DELAY = 1.5
DEFAULT_PUBLIC_URL = ""
SERVICE_CLEAR_HISTORY = "clear_history"
SERVICE_UPDATE_LAST_CALLED = "update_last_called"
SERVICE_FORCE_LOGOUT = "force_logout"
Expand Down Expand Up @@ -131,3 +133,5 @@
"Alexa.AirQuality.IndoorAirQuality": "mdi:numeric",
}
ALEXA_ICON_DEFAULT = "mdi:molecule"

UPLOAD_PATH = "/config/www/alexa_tts"
100 changes: 89 additions & 11 deletions custom_components/alexa_media/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@
"""
import asyncio
import logging
import os
import re
import subprocess
from typing import List, Optional
import urllib.request

from homeassistant import util
from homeassistant.components import media_source
from homeassistant.components.media_player import (
ATTR_MEDIA_ANNOUNCE,
async_process_play_media_url,
)
from homeassistant.components.media_player.const import (
MEDIA_TYPE_MUSIC,
SUPPORT_NEXT_TRACK,
Expand Down Expand Up @@ -44,8 +52,10 @@
from homeassistant.util import slugify

from . import (
CONF_PUBLIC_URL,
CONF_QUEUE_DELAY,
DATA_ALEXAMEDIA,
DEFAULT_PUBLIC_URL,
DEFAULT_QUEUE_DELAY,
DOMAIN as ALEXA_DOMAIN,
hide_email,
Expand All @@ -57,6 +67,7 @@
MIN_TIME_BETWEEN_FORCED_SCANS,
MIN_TIME_BETWEEN_SCANS,
PLAY_SCAN_INTERVAL,
UPLOAD_PATH,
)
from .helpers import _catch_login_errors, add_devices

Expand Down Expand Up @@ -90,6 +101,9 @@
# @retry_async(limit=5, delay=2, catch_exceptions=True)
async def async_setup_platform(hass, config, add_devices_callback, discovery_info=None):
"""Set up the Alexa media player platform."""
if not os.path.exists(UPLOAD_PATH):
os.mkdir(UPLOAD_PATH)

devices = [] # type: List[AlexaClient]
account = None
if config:
Expand Down Expand Up @@ -1314,25 +1328,89 @@ async def async_send_dropin_notification(self, message, **kwargs):
message, customer_id=self._customer_id, **kwargs
)

@_catch_login_errors
async def async_play_tts_cloud_say(self, public_url, media_id, **kwargs):
file_name = media_id
if media_source.is_media_source_id(media_id):
media = await media_source.async_resolve_media(
self.hass, media_id, self.entity_id
)
file_name = media.url[media.url.rindex("/") : media.url.rindex(".")]
media_id = async_process_play_media_url(self.hass, media.url)

if kwargs.get(ATTR_MEDIA_ANNOUNCE):
input_file_path = f"{UPLOAD_PATH}{file_name}_input.mp3"
output_file_name = f"{file_name}_output.mp3"
output_file_path = f"{UPLOAD_PATH}{output_file_name}"

# file might already exist -> the same tts is cached from previous calls
if not os.path.exists(output_file_path):
await self.hass.async_add_executor_job(
urllib.request.urlretrieve, media_id, input_file_path
)
command = [
"ffmpeg",
"-i",
input_file_path,
"-ac",
"2",
"-codec:a",
"libmp3lame",
"-b:a",
"48k",
"-ar",
"24000",
output_file_path,
]
if subprocess.run(command, check=True).returncode != 0:
_LOGGER.error(
"%s: %s:ffmpeg command FAILED converting %s to %s",
hide_email(self._login.email),
self,
input_file_path,
output_file_path,
)

_LOGGER.debug(
"%s: %s:Playing %slocal/alexa_tts%s",
hide_email(self._login.email),
self,
public_url,
output_file_name,
)
await self.async_send_tts(
f"<audio src='{public_url}local/alexa_tts{output_file_name}' />"
)
else:
await self.async_send_tts(
"To send TTS, please set Announce=true. Music can't be played this way."
)
_LOGGER.warning(
"To send TTS, please set Announce=true. Music can't be played this way."
)

@_catch_login_errors
async def async_play_media(self, media_type, media_id, enqueue=None, **kwargs):
# pylint: disable=unused-argument,too-many-branches
"""Send the play_media command to the media player."""
queue_delay = self.hass.data[DATA_ALEXAMEDIA]["accounts"][self.email][
"options"
].get(CONF_QUEUE_DELAY, DEFAULT_QUEUE_DELAY)
public_url = self.hass.data[DATA_ALEXAMEDIA]["accounts"][self.email][
"options"
].get(CONF_PUBLIC_URL, DEFAULT_PUBLIC_URL)
if media_type == "music":
await self.async_send_tts(
"Sorry, text to speech can only be called"
" with the notify.alexa_media service."
" Please see the alexa_media wiki for details."
)
_LOGGER.warning(
"Sorry, text to speech can only be called"
" with the notify.alexa_media service."
" Please see the alexa_media wiki for details."
"https://github.com/custom-components/alexa_media_player/wiki/Configuration%3A-Notification-Component#use-the-notifyalexa_media-service"
)
if public_url:
await self.async_play_tts_cloud_say(public_url, media_id, **kwargs)
else:
await self.async_send_tts(
"To send TTS, set public url in integration configuration"
)
_LOGGER.warning(
"To send TTS, set public url in integration configuration"
" Please see the alexa_media wiki for details."
"https://github.com/custom-components/alexa_media_player/wiki/Configuration%3A-Notification-Component#use-the-notifyalexa_media-service"
)
elif media_type == "sequence":
_LOGGER.debug(
"%s: %s:Running sequence %s with queue_delay %s",
Expand Down
3 changes: 2 additions & 1 deletion custom_components/alexa_media/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
"init": {
"data": {
"queue_delay": "Seconds to wait to queue commands together",
"extended_entity_discovery": "Include devices connected via Echo"
"extended_entity_discovery": "Include devices connected via Echo",
"public_url": "Public URL to access Home Assistant (including trailing '/')"
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion custom_components/alexa_media/translations/ar.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
"init": {
"data": {
"extended_entity_discovery": "Include devices connected via Echo",
"queue_delay": "Seconds to wait to queue commands together"
"queue_delay": "Seconds to wait to queue commands together",
"public_url": "Public URL to access Home Assistant (including trailing '/')"
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion custom_components/alexa_media/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
"init": {
"data": {
"extended_entity_discovery": "Include devices connected via Echo",
"queue_delay": "Seconds to wait to queue commands together"
"queue_delay": "Seconds to wait to queue commands together",
"public_url": "Public URL to access Home Assistant (including trailing '/')"
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion custom_components/alexa_media/translations/zh-Hans.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
"init": {
"data": {
"extended_entity_discovery": "包括通过 Echo 连接的设备",
"queue_delay": "Seconds to wait to queue commands together"
"queue_delay": "Seconds to wait to queue commands together",
"public_url": "Public URL to access Home Assistant (including trailing '/')"
}
}
}
Expand Down
14 changes: 9 additions & 5 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit af99698

Please sign in to comment.