From 6fb0e493357a683ee4f77c3c636fa61767355f8f Mon Sep 17 00:00:00 2001 From: corneyl Date: Thu, 18 Mar 2021 04:59:48 +0100 Subject: [PATCH] Upgraded aiopylgtv to v0.4.0 (#47014) Co-authored-by: Paulus Schoutsen --- homeassistant/components/webostv/__init__.py | 33 ++++++++++++- .../components/webostv/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- tests/components/webostv/test_media_player.py | 47 ++++++++++++++++++- 5 files changed, 81 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/webostv/__init__.py b/homeassistant/components/webostv/__init__.py index 3a117955bee..34e32dce163 100644 --- a/homeassistant/components/webostv/__init__.py +++ b/homeassistant/components/webostv/__init__.py @@ -1,8 +1,11 @@ """Support for LG webOS Smart TV.""" import asyncio +import json import logging +import os from aiopylgtv import PyLGTVCmdException, PyLGTVPairException, WebOsClient +from sqlitedict import SqliteDict import voluptuous as vol from websockets.exceptions import ConnectionClosed @@ -101,13 +104,41 @@ async def async_setup(hass, config): return True +def convert_client_keys(config_file): + """In case the config file contains JSON, convert it to a Sqlite config file.""" + # Return early if config file is non-existing + if not os.path.isfile(config_file): + return + + # Try to parse the file as being JSON + with open(config_file) as json_file: + try: + json_conf = json.load(json_file) + except (json.JSONDecodeError, UnicodeDecodeError): + json_conf = None + + # If the file contains JSON, convert it to an Sqlite DB + if json_conf: + _LOGGER.warning("LG webOS TV client-key file is being migrated to Sqlite!") + + # Clean the JSON file + os.remove(config_file) + + # Write the data to the Sqlite DB + with SqliteDict(config_file) as conf: + for host, key in json_conf.items(): + conf[host] = key + conf.commit() + + async def async_setup_tv(hass, config, conf): """Set up a LG WebOS TV based on host parameter.""" host = conf[CONF_HOST] config_file = hass.config.path(WEBOSTV_CONFIG_FILE) + await hass.async_add_executor_job(convert_client_keys, config_file) - client = WebOsClient(host, config_file) + client = await WebOsClient.create(host, config_file) hass.data[DOMAIN][host] = {"client": client} if client.is_registered(): diff --git a/homeassistant/components/webostv/manifest.json b/homeassistant/components/webostv/manifest.json index acdee1d9ca9..7773e9c4963 100644 --- a/homeassistant/components/webostv/manifest.json +++ b/homeassistant/components/webostv/manifest.json @@ -2,7 +2,7 @@ "domain": "webostv", "name": "LG webOS Smart TV", "documentation": "https://www.home-assistant.io/integrations/webostv", - "requirements": ["aiopylgtv==0.3.3"], + "requirements": ["aiopylgtv==0.4.0"], "dependencies": ["configurator"], "codeowners": ["@bendavid"] } diff --git a/requirements_all.txt b/requirements_all.txt index 0398f053ec3..40a3e8545be 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -215,7 +215,7 @@ aiopvapi==1.6.14 aiopvpc==2.0.2 # homeassistant.components.webostv -aiopylgtv==0.3.3 +aiopylgtv==0.4.0 # homeassistant.components.recollect_waste aiorecollect==1.0.1 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 47c10b5700c..5a17b05864d 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -134,7 +134,7 @@ aiopvapi==1.6.14 aiopvpc==2.0.2 # homeassistant.components.webostv -aiopylgtv==0.3.3 +aiopylgtv==0.4.0 # homeassistant.components.recollect_waste aiorecollect==1.0.1 diff --git a/tests/components/webostv/test_media_player.py b/tests/components/webostv/test_media_player.py index 3015fcccf9f..716c496d88a 100644 --- a/tests/components/webostv/test_media_player.py +++ b/tests/components/webostv/test_media_player.py @@ -1,8 +1,10 @@ """The tests for the LG webOS media player platform.""" - +import json +import os from unittest.mock import patch import pytest +from sqlitedict import SqliteDict from homeassistant.components import media_player from homeassistant.components.media_player.const import ( @@ -16,6 +18,7 @@ from homeassistant.components.webostv.const import ( DOMAIN, SERVICE_BUTTON, SERVICE_COMMAND, + WEBOSTV_CONFIG_FILE, ) from homeassistant.const import ( ATTR_COMMAND, @@ -36,6 +39,7 @@ def client_fixture(): with patch( "homeassistant.components.webostv.WebOsClient", autospec=True ) as mock_client_class: + mock_client_class.create.return_value = mock_client_class.return_value client = mock_client_class.return_value client.software_info = {"device_id": "a1:b1:c1:d1:e1:f1"} client.client_key = "0123456789" @@ -52,6 +56,13 @@ async def setup_webostv(hass): await hass.async_block_till_done() +@pytest.fixture +def cleanup_config(hass): + """Test cleanup, remove the config file.""" + yield + os.remove(hass.config.path(WEBOSTV_CONFIG_FILE)) + + async def test_mute(hass, client): """Test simple service call.""" @@ -128,3 +139,37 @@ async def test_command_with_optional_arg(hass, client): client.request.assert_called_with( "test", payload={"target": "https://www.google.com"} ) + + +async def test_migrate_keyfile_to_sqlite(hass, client, cleanup_config): + """Test migration from JSON key-file to Sqlite based one.""" + key = "3d5b1aeeb98e" + # Create config file with JSON content + config_file = hass.config.path(WEBOSTV_CONFIG_FILE) + with open(config_file, "w+") as file: + json.dump({"host": key}, file) + + # Run the component setup + await setup_webostv(hass) + + # Assert that the config file is a Sqlite database which contains the key + with SqliteDict(config_file) as conf: + assert conf.get("host") == key + + +async def test_dont_migrate_sqlite_keyfile(hass, client, cleanup_config): + """Test that migration is not performed and setup still succeeds when config file is already an Sqlite DB.""" + key = "3d5b1aeeb98e" + + # Create config file with Sqlite DB + config_file = hass.config.path(WEBOSTV_CONFIG_FILE) + with SqliteDict(config_file) as conf: + conf["host"] = key + conf.commit() + + # Run the component setup + await setup_webostv(hass) + + # Assert that the config file is still an Sqlite database and setup didn't fail + with SqliteDict(config_file) as conf: + assert conf.get("host") == key