Skip to content

Commit

Permalink
Rename google/tts.py to google_translate/tts.py (home-assistant#23090)
Browse files Browse the repository at this point in the history
* Rename google/tts.py to google_translate/tts.py

* Move config migration before load config file

Migrate default config google tts to google_translate tts

* Revert change in process component config

* Fix tests
  • Loading branch information
awarecan authored and balloob committed Apr 16, 2019
1 parent ca52423 commit 308d1fb
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 24 deletions.
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ homeassistant/components/gearbest/* @HerrHofrat
homeassistant/components/gitter/* @fabaff
homeassistant/components/glances/* @fabaff
homeassistant/components/gntp/* @robbiet480
homeassistant/components/google_translate/* @awarecan
homeassistant/components/google_travel_time/* @robbiet480
homeassistant/components/googlehome/* @ludeeus
homeassistant/components/gpsd/* @fabaff
Expand Down
6 changes: 3 additions & 3 deletions homeassistant/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ async def async_from_config_dict(config: Dict[str, Any],
"Further initialization aborted")
return None

await hass.async_add_executor_job(
conf_util.process_ha_config_upgrade, hass)

# Make a copy because we are mutating it.
config = OrderedDict(config)

Expand Down Expand Up @@ -167,6 +164,9 @@ async def async_from_config_file(config_path: str,
async_enable_logging(hass, verbose, log_rotate_days, log_file,
log_no_color)

await hass.async_add_executor_job(
conf_util.process_ha_config_upgrade, hass)

try:
config_dict = await hass.async_add_executor_job(
conf_util.load_yaml_config_file, config_path)
Expand Down
1 change: 0 additions & 1 deletion homeassistant/components/google/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"name": "Google",
"documentation": "https://www.home-assistant.io/components/google",
"requirements": [
"gTTS-token==1.1.3",
"google-api-python-client==1.6.4",
"httplib2==0.10.3",
"oauth2client==4.0.0"
Expand Down
1 change: 1 addition & 0 deletions homeassistant/components/google_translate/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""The google_translate component."""
12 changes: 12 additions & 0 deletions homeassistant/components/google_translate/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"domain": "google_translate",
"name": "Google Translate",
"documentation": "https://www.home-assistant.io/components/google_translate",
"requirements": [
"gTTS-token==1.1.3"
],
"dependencies": [],
"codeowners": [
"@awarecan"
]
}
File renamed without changes.
21 changes: 18 additions & 3 deletions homeassistant/components/tts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
ATTR_MEDIA_CONTENT_ID, ATTR_MEDIA_CONTENT_TYPE, MEDIA_TYPE_MUSIC,
SERVICE_PLAY_MEDIA)
from homeassistant.components.media_player.const import DOMAIN as DOMAIN_MP
from homeassistant.const import ATTR_ENTITY_ID, ENTITY_MATCH_ALL
from homeassistant.const import ATTR_ENTITY_ID, ENTITY_MATCH_ALL, CONF_PLATFORM
from homeassistant.core import callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import config_per_platform
Expand All @@ -32,11 +32,12 @@
ATTR_OPTIONS = 'options'
ATTR_PLATFORM = 'platform'

CONF_BASE_URL = 'base_url'
CONF_CACHE = 'cache'
CONF_CACHE_DIR = 'cache_dir'
CONF_LANG = 'language'
CONF_SERVICE_NAME = 'service_name'
CONF_TIME_MEMORY = 'time_memory'
CONF_BASE_URL = 'base_url'

DEFAULT_CACHE = True
DEFAULT_CACHE_DIR = 'tts'
Expand All @@ -53,12 +54,24 @@
r"([a-f0-9]{40})_([^_]+)_([^_]+)_([a-z_]+)\.[a-z0-9]{3,4}")
KEY_PATTERN = '{0}_{1}_{2}_{3}'


def _deprecated_platform(value):
"""Validate if platform is deprecated."""
if value == 'google':
raise vol.Invalid(
'google tts service has been renamed to google_translate,'
' please update your configuration.')
return value


PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({
vol.Required(CONF_PLATFORM): vol.All(cv.string, _deprecated_platform),
vol.Optional(CONF_CACHE, default=DEFAULT_CACHE): cv.boolean,
vol.Optional(CONF_CACHE_DIR, default=DEFAULT_CACHE_DIR): cv.string,
vol.Optional(CONF_TIME_MEMORY, default=DEFAULT_TIME_MEMORY):
vol.All(vol.Coerce(int), vol.Range(min=60, max=57600)),
vol.Optional(CONF_BASE_URL): cv.string,
vol.Optional(CONF_SERVICE_NAME): cv.string,
})
PLATFORM_SCHEMA_BASE = cv.PLATFORM_SCHEMA_BASE.extend(PLATFORM_SCHEMA.schema)

Expand Down Expand Up @@ -142,8 +155,10 @@ async def async_say_handle(service):
await hass.services.async_call(
DOMAIN_MP, SERVICE_PLAY_MEDIA, data, blocking=True)

service_name = p_config.get(CONF_SERVICE_NAME, "{}_{}".format(
p_type, SERVICE_SAY))
hass.services.async_register(
DOMAIN, "{}_{}".format(p_type, SERVICE_SAY), async_say_handle,
DOMAIN, service_name, async_say_handle,
schema=SCHEMA_SERVICE_SAY)

setup_tasks = [async_setup_platform(p_type, p_config) for p_type, p_config
Expand Down
27 changes: 25 additions & 2 deletions homeassistant/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
# Text to speech
tts:
- platform: google
- platform: google_translate
group: !include groups.yaml
automation: !include automations.yaml
Expand All @@ -95,6 +95,15 @@
# Learn more at https://home-assistant.io/docs/configuration/secrets/
some_password: welcome
"""
TTS_PRE_92 = """
tts:
- platform: google
"""
TTS_92 = """
tts:
- platform: google_translate
service_name: google_say
"""


def _no_duplicate_auth_provider(configs: Sequence[Dict[str, Any]]) \
Expand Down Expand Up @@ -378,10 +387,24 @@ def process_ha_config_upgrade(hass: HomeAssistant) -> None:
if os.path.isdir(lib_path):
shutil.rmtree(lib_path)

if LooseVersion(conf_version) < LooseVersion('0.92'):
# 0.92 moved google/tts.py to google_translate/tts.py
config_path = find_config_file(hass.config.config_dir)
assert config_path is not None

with open(config_path, 'rt') as config_file:
config_raw = config_file.read()

if TTS_PRE_92 in config_raw:
_LOGGER.info("Migrating google tts to google_translate tts")
config_raw = config_raw.replace(TTS_PRE_92, TTS_92)
with open(config_path, 'wt') as config_file:
config_file.write(config_raw)

with open(version_path, 'wt') as outp:
outp.write(__version__)

_LOGGER.info("Migrating old system configuration files to new locations")
_LOGGER.debug("Migrating old system configuration files to new locations")
for oldf, newf in FILE_MIGRATION:
if os.path.isfile(hass.config.path(oldf)):
_LOGGER.info("Migrating %s to %s", oldf, newf)
Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ freesms==0.1.2
# homeassistant.components.fritzdect
fritzhome==1.0.4

# homeassistant.components.google
# homeassistant.components.google_translate
gTTS-token==1.1.3

# homeassistant.components.gearbest
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ feedparser-homeassistant==5.2.2.dev1
# homeassistant.components.foobot
foobot_async==0.3.1

# homeassistant.components.google
# homeassistant.components.google_translate
gTTS-token==1.1.3

# homeassistant.components.geo_json_events
Expand Down
1 change: 1 addition & 0 deletions tests/components/google_translate/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Tests for the Google Translate integration."""
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_setup_component(self):
"""Test setup component."""
config = {
tts.DOMAIN: {
'platform': 'google',
'platform': 'google_translate',
}
}

Expand All @@ -65,14 +65,14 @@ def test_service_say(self, mock_calculate, aioclient_mock):

config = {
tts.DOMAIN: {
'platform': 'google',
'platform': 'google_translate',
}
}

with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config)

self.hass.services.call(tts.DOMAIN, 'google_say', {
self.hass.services.call(tts.DOMAIN, 'google_translate_say', {
tts.ATTR_MESSAGE: "90% of I person is on front of your door.",
})
self.hass.block_till_done()
Expand All @@ -93,15 +93,15 @@ def test_service_say_german_config(self, mock_calculate, aioclient_mock):

config = {
tts.DOMAIN: {
'platform': 'google',
'platform': 'google_translate',
'language': 'de',
}
}

with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config)

self.hass.services.call(tts.DOMAIN, 'google_say', {
self.hass.services.call(tts.DOMAIN, 'google_translate_say', {
tts.ATTR_MESSAGE: "90% of I person is on front of your door.",
})
self.hass.block_till_done()
Expand All @@ -121,7 +121,8 @@ def test_service_say_german_service(self, mock_calculate, aioclient_mock):

config = {
tts.DOMAIN: {
'platform': 'google',
'platform': 'google_translate',
'service_name': 'google_say',
}
}

Expand All @@ -148,14 +149,14 @@ def test_service_say_error(self, mock_calculate, aioclient_mock):

config = {
tts.DOMAIN: {
'platform': 'google',
'platform': 'google_translate',
}
}

with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config)

self.hass.services.call(tts.DOMAIN, 'google_say', {
self.hass.services.call(tts.DOMAIN, 'google_translate_say', {
tts.ATTR_MESSAGE: "90% of I person is on front of your door.",
})
self.hass.block_till_done()
Expand All @@ -174,14 +175,14 @@ def test_service_say_timeout(self, mock_calculate, aioclient_mock):

config = {
tts.DOMAIN: {
'platform': 'google',
'platform': 'google_translate',
}
}

with assert_setup_component(1, tts.DOMAIN):
setup_component(self.hass, tts.DOMAIN, config)

self.hass.services.call(tts.DOMAIN, 'google_say', {
self.hass.services.call(tts.DOMAIN, 'google_translate_say', {
tts.ATTR_MESSAGE: "90% of I person is on front of your door.",
})
self.hass.block_till_done()
Expand All @@ -205,7 +206,8 @@ def test_service_say_long_size(self, mock_calculate, aioclient_mock):

config = {
tts.DOMAIN: {
'platform': 'google',
'platform': 'google_translate',
'service_name': 'google_say',
}
}

Expand Down
9 changes: 7 additions & 2 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ def test_remove_lib_on_upgrade(self, mock_os, mock_shutil):

def test_process_config_upgrade(self):
"""Test update of version on upgrade."""
ha_version = '0.8.0'
ha_version = '0.92.0'

mock_open = mock.mock_open()
with mock.patch('homeassistant.config.open', mock_open, create=True):
Expand All @@ -342,10 +342,13 @@ def test_config_upgrade_same_version(self):

assert opened_file.write.call_count == 0

@mock.patch('homeassistant.config.find_config_file', mock.Mock())
def test_config_upgrade_no_file(self):
"""Test update of version on upgrade, with no version file."""
mock_open = mock.mock_open()
mock_open.side_effect = [FileNotFoundError(), mock.DEFAULT]
mock_open.side_effect = [FileNotFoundError(),
mock.DEFAULT,
mock.DEFAULT]
with mock.patch('homeassistant.config.open', mock_open, create=True):
opened_file = mock_open.return_value
# pylint: disable=no-member
Expand All @@ -355,6 +358,7 @@ def test_config_upgrade_no_file(self):

@mock.patch('homeassistant.config.shutil')
@mock.patch('homeassistant.config.os')
@mock.patch('homeassistant.config.find_config_file', mock.Mock())
def test_migrate_file_on_upgrade(self, mock_os, mock_shutil):
"""Test migrate of config files on upgrade."""
ha_version = '0.7.0'
Expand All @@ -381,6 +385,7 @@ def _mock_isfile(filename):

@mock.patch('homeassistant.config.shutil')
@mock.patch('homeassistant.config.os')
@mock.patch('homeassistant.config.find_config_file', mock.Mock())
def test_migrate_no_file_on_upgrade(self, mock_os, mock_shutil):
"""Test not migrating config files on upgrade."""
ha_version = '0.7.0'
Expand Down

0 comments on commit 308d1fb

Please sign in to comment.