From 1bac3d84b465d4a2c1a5b5e4725cff82c69e65c4 Mon Sep 17 00:00:00 2001 From: Feng Wang Date: Sat, 11 Jan 2025 13:32:16 +0800 Subject: [PATCH 1/7] move web page to html --- .../miot/resource/oauth_redirect_page.html | 260 ++++++++++++++++++ .../xiaomi_home/miot/web_pages.py | 238 +--------------- 2 files changed, 267 insertions(+), 231 deletions(-) create mode 100644 custom_components/xiaomi_home/miot/resource/oauth_redirect_page.html diff --git a/custom_components/xiaomi_home/miot/resource/oauth_redirect_page.html b/custom_components/xiaomi_home/miot/resource/oauth_redirect_page.html new file mode 100644 index 0000000..293819e --- /dev/null +++ b/custom_components/xiaomi_home/miot/resource/oauth_redirect_page.html @@ -0,0 +1,260 @@ + + + + + + + + + + + + +
+ +
+ + 编组 + Created with Sketch. + + + + + + + + + + + + + + + + + +
+ +
+ +
+ +
+ +
+ + +
+ + + + \ No newline at end of file diff --git a/custom_components/xiaomi_home/miot/web_pages.py b/custom_components/xiaomi_home/miot/web_pages.py index e4cde5a..97f2e6b 100644 --- a/custom_components/xiaomi_home/miot/web_pages.py +++ b/custom_components/xiaomi_home/miot/web_pages.py @@ -46,237 +46,13 @@ off Xiaomi or its affiliates' products. MIoT redirect web pages. """ -# pylint: disable=line-too-long +# preload the template +template = "" +with open("./resource/oauth_redirect_page.html", "r") as f: + template = f.read() def oauth_redirect_page(lang: str, status: str) -> str: """Return oauth redirect page.""" - return ''' - - - - - - - - - - -
- -
- 编组 - Created with Sketch. - - - - - - - - - - - - - - - - -
- -
- -
- -
- -
- - -
- - - - ''' + web_page = template.replace('LANG_PLACEHOLDER', lang) + web_page = web_page.replace('STATUS_PLACEHOLDER', status) + return web_page From dbde931d361f68e5f5d8db228c255fca2ad8b0f6 Mon Sep 17 00:00:00 2001 From: Feng Wang Date: Sat, 11 Jan 2025 13:38:51 +0800 Subject: [PATCH 2/7] move loading into function --- custom_components/xiaomi_home/miot/web_pages.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/custom_components/xiaomi_home/miot/web_pages.py b/custom_components/xiaomi_home/miot/web_pages.py index 97f2e6b..df63478 100644 --- a/custom_components/xiaomi_home/miot/web_pages.py +++ b/custom_components/xiaomi_home/miot/web_pages.py @@ -46,12 +46,12 @@ off Xiaomi or its affiliates' products. MIoT redirect web pages. """ -# preload the template template = "" -with open("./resource/oauth_redirect_page.html", "r") as f: - template = f.read() - def oauth_redirect_page(lang: str, status: str) -> str: + global template + if template == "": + with open("./resource/oauth_redirect_page.html", "r") as f: + template = f.read() """Return oauth redirect page.""" web_page = template.replace('LANG_PLACEHOLDER', lang) web_page = web_page.replace('STATUS_PLACEHOLDER', status) From 2f0c2278cc1b19dac49621e17d67957d09504544 Mon Sep 17 00:00:00 2001 From: Feng Wang Date: Sat, 11 Jan 2025 13:59:07 +0800 Subject: [PATCH 3/7] make the loading async --- .../xiaomi_home/miot/web_pages.py | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/custom_components/xiaomi_home/miot/web_pages.py b/custom_components/xiaomi_home/miot/web_pages.py index df63478..e2ee79b 100644 --- a/custom_components/xiaomi_home/miot/web_pages.py +++ b/custom_components/xiaomi_home/miot/web_pages.py @@ -46,13 +46,25 @@ off Xiaomi or its affiliates' products. MIoT redirect web pages. """ -template = "" -def oauth_redirect_page(lang: str, status: str) -> str: - global template - if template == "": - with open("./resource/oauth_redirect_page.html", "r") as f: - template = f.read() +import os +import asyncio + +_template = "" + +def __load_page_template(): + path = os.path.join( + os.path.dirname(os.path.abspath(__file__)), + 'resource/oauth_redirect_page.html') + global _template + with open(path, "r") as f: + _template = f.read() + +async def oauth_redirect_page(lang: str, status: str) -> str: """Return oauth redirect page.""" - web_page = template.replace('LANG_PLACEHOLDER', lang) + global template + if _template == "": + await asyncio.get_event_loop().run_in_executor( + None, __load_page_template) + web_page = _template.replace('LANG_PLACEHOLDER', lang) web_page = web_page.replace('STATUS_PLACEHOLDER', status) return web_page From 2f804421cf7283d050e84c215e887321471aefb7 Mon Sep 17 00:00:00 2001 From: Feng Wang Date: Sat, 11 Jan 2025 14:04:06 +0800 Subject: [PATCH 4/7] fix usage --- custom_components/xiaomi_home/config_flow.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/custom_components/xiaomi_home/config_flow.py b/custom_components/xiaomi_home/config_flow.py index 8e48849..cb72c03 100644 --- a/custom_components/xiaomi_home/config_flow.py +++ b/custom_components/xiaomi_home/config_flow.py @@ -1991,12 +1991,12 @@ async def _handle_oauth_webhook(hass, webhook_id, request): _LOGGER.info('webhook code: %s', data['code']) return web.Response( - body=oauth_redirect_page( + body=await oauth_redirect_page( hass.config.language, 'success'), content_type='text/html') except MIoTConfigError: return web.Response( - body=oauth_redirect_page(hass.config.language, 'fail'), + body=await oauth_redirect_page(hass.config.language, 'fail'), content_type='text/html') From 43104736a6a9cbe5cbba26d9bd6f0c226c6a035d Mon Sep 17 00:00:00 2001 From: Feng Wang Date: Sat, 11 Jan 2025 14:11:30 +0800 Subject: [PATCH 5/7] Fix function naming --- custom_components/xiaomi_home/miot/web_pages.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/custom_components/xiaomi_home/miot/web_pages.py b/custom_components/xiaomi_home/miot/web_pages.py index e2ee79b..ab11cf4 100644 --- a/custom_components/xiaomi_home/miot/web_pages.py +++ b/custom_components/xiaomi_home/miot/web_pages.py @@ -51,7 +51,7 @@ import asyncio _template = "" -def __load_page_template(): +def _load_page_template(): path = os.path.join( os.path.dirname(os.path.abspath(__file__)), 'resource/oauth_redirect_page.html') @@ -64,7 +64,7 @@ async def oauth_redirect_page(lang: str, status: str) -> str: global template if _template == "": await asyncio.get_event_loop().run_in_executor( - None, __load_page_template) + None, _load_page_template) web_page = _template.replace('LANG_PLACEHOLDER', lang) web_page = web_page.replace('STATUS_PLACEHOLDER', status) return web_page From 7607cc3e32a8b812f7e5ea01dd342bff698af03c Mon Sep 17 00:00:00 2001 From: Feng Wang Date: Sat, 11 Jan 2025 14:19:36 +0800 Subject: [PATCH 6/7] fix lint --- custom_components/xiaomi_home/miot/web_pages.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/custom_components/xiaomi_home/miot/web_pages.py b/custom_components/xiaomi_home/miot/web_pages.py index ab11cf4..e28ee38 100644 --- a/custom_components/xiaomi_home/miot/web_pages.py +++ b/custom_components/xiaomi_home/miot/web_pages.py @@ -54,17 +54,17 @@ _template = "" def _load_page_template(): path = os.path.join( os.path.dirname(os.path.abspath(__file__)), - 'resource/oauth_redirect_page.html') - global _template - with open(path, "r") as f: + "resource/oauth_redirect_page.html") + with open(path, "r", encoding='utf-8') as f: + global _template _template = f.read() async def oauth_redirect_page(lang: str, status: str) -> str: """Return oauth redirect page.""" - global template + global _template if _template == "": await asyncio.get_event_loop().run_in_executor( None, _load_page_template) - web_page = _template.replace('LANG_PLACEHOLDER', lang) - web_page = web_page.replace('STATUS_PLACEHOLDER', status) + web_page = _template.replace("LANG_PLACEHOLDER", lang) + web_page = web_page.replace("STATUS_PLACEHOLDER", status) return web_page From d9a9edd42da54bd43a59dbb8f71d1de0bf2c8b1b Mon Sep 17 00:00:00 2001 From: Feng Wang Date: Sat, 11 Jan 2025 14:27:04 +0800 Subject: [PATCH 7/7] fix lint --- custom_components/xiaomi_home/miot/web_pages.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/custom_components/xiaomi_home/miot/web_pages.py b/custom_components/xiaomi_home/miot/web_pages.py index e28ee38..b12eff1 100644 --- a/custom_components/xiaomi_home/miot/web_pages.py +++ b/custom_components/xiaomi_home/miot/web_pages.py @@ -55,13 +55,12 @@ def _load_page_template(): path = os.path.join( os.path.dirname(os.path.abspath(__file__)), "resource/oauth_redirect_page.html") - with open(path, "r", encoding='utf-8') as f: + with open(path, "r", encoding="utf-8") as f: global _template _template = f.read() async def oauth_redirect_page(lang: str, status: str) -> str: """Return oauth redirect page.""" - global _template if _template == "": await asyncio.get_event_loop().run_in_executor( None, _load_page_template)