Upgraded aiopylgtv to v0.4.0 (#47014)

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
pull/47767/head
corneyl 2021-03-18 04:59:48 +01:00 committed by GitHub
parent f785cc7d9a
commit 6fb0e49335
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 81 additions and 5 deletions

View File

@ -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():

View File

@ -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"]
}

View File

@ -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

View File

@ -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

View File

@ -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