From 723175656d171e1c12dc665c2c38a860fcadb5c8 Mon Sep 17 00:00:00 2001 From: Jason Hu Date: Tue, 31 Jul 2018 09:50:53 -0700 Subject: [PATCH 1/3] Avoid exception when loading translation file for unloaded module --- homeassistant/helpers/translation.py | 3 +++ homeassistant/util/json.py | 2 ++ 2 files changed, 5 insertions(+) diff --git a/homeassistant/helpers/translation.py b/homeassistant/helpers/translation.py index 81ec046f2e9966..63c91ffe646d99 100644 --- a/homeassistant/helpers/translation.py +++ b/homeassistant/helpers/translation.py @@ -36,6 +36,9 @@ def component_translation_file(hass, component, language): name = component module = get_component(hass, component) + if module is None: + _LOGGER.warning('component %s is not loaded', component) + return component_path = path.dirname(module.__file__) # If loading translations for the package root, (__init__.py), the diff --git a/homeassistant/util/json.py b/homeassistant/util/json.py index 8ecfebd5b33162..065f5256765c6d 100644 --- a/homeassistant/util/json.py +++ b/homeassistant/util/json.py @@ -23,6 +23,8 @@ def load_json(filename: str, default: Union[List, Dict, None] = None) \ Defaults to returning empty dict if file is not found. """ + if filename is None: + return {} try: with open(filename, encoding='utf-8') as fdesc: return json.loads(fdesc.read()) # type: ignore From d656077de4ca02ac2b47f4c2e8a0d9e91ab129d3 Mon Sep 17 00:00:00 2001 From: Jason Hu Date: Thu, 2 Aug 2018 11:02:30 -0700 Subject: [PATCH 2/3] Try to load component even it has been disabled during setup --- homeassistant/helpers/translation.py | 3 --- homeassistant/loader.py | 4 +++- homeassistant/util/json.py | 2 -- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/homeassistant/helpers/translation.py b/homeassistant/helpers/translation.py index 63c91ffe646d99..81ec046f2e9966 100644 --- a/homeassistant/helpers/translation.py +++ b/homeassistant/helpers/translation.py @@ -36,9 +36,6 @@ def component_translation_file(hass, component, language): name = component module = get_component(hass, component) - if module is None: - _LOGGER.warning('component %s is not loaded', component) - return component_path = path.dirname(module.__file__) # If loading translations for the package root, (__init__.py), the diff --git a/homeassistant/loader.py b/homeassistant/loader.py index 3ac49e354b5d50..c547c94e55bcc6 100644 --- a/homeassistant/loader.py +++ b/homeassistant/loader.py @@ -71,7 +71,9 @@ def get_component(hass, # type: HomeAssistant Async friendly. """ try: - return hass.data[DATA_KEY][comp_or_platform] # type: ignore + comp = hass.data[DATA_KEY][comp_or_platform] # type: ignore + if comp is not None: + return comp except KeyError: pass diff --git a/homeassistant/util/json.py b/homeassistant/util/json.py index 065f5256765c6d..8ecfebd5b33162 100644 --- a/homeassistant/util/json.py +++ b/homeassistant/util/json.py @@ -23,8 +23,6 @@ def load_json(filename: str, default: Union[List, Dict, None] = None) \ Defaults to returning empty dict if file is not found. """ - if filename is None: - return {} try: with open(filename, encoding='utf-8') as fdesc: return json.loads(fdesc.read()) # type: ignore From 24a6c5c230b9b6922b41136ca022803d050a0e7a Mon Sep 17 00:00:00 2001 From: Jason Hu Date: Thu, 2 Aug 2018 11:28:31 -0700 Subject: [PATCH 3/3] Fix typing --- homeassistant/loader.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/homeassistant/loader.py b/homeassistant/loader.py index c547c94e55bcc6..57479cf12d1973 100644 --- a/homeassistant/loader.py +++ b/homeassistant/loader.py @@ -71,9 +71,9 @@ def get_component(hass, # type: HomeAssistant Async friendly. """ try: - comp = hass.data[DATA_KEY][comp_or_platform] # type: ignore + comp = hass.data[DATA_KEY][comp_or_platform] if comp is not None: - return comp + return comp # type: ignore except KeyError: pass