From 56d45c49e92e30f74ec912f091a1ffaddee8f03e Mon Sep 17 00:00:00 2001 From: Martin Hjelmare <marhje52@gmail.com> Date: Fri, 18 Feb 2022 14:03:05 +0100 Subject: [PATCH] Clean webostv notify (#66803) * Replace conf with attr * Test notify service without data parameter * Clean kwargs access * Replace icon constant * Use data from notify --- homeassistant/components/webostv/notify.py | 6 ++-- tests/components/webostv/test_notify.py | 40 ++++++++++++++++------ 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/homeassistant/components/webostv/notify.py b/homeassistant/components/webostv/notify.py index 46f0086e0f6..82e61856187 100644 --- a/homeassistant/components/webostv/notify.py +++ b/homeassistant/components/webostv/notify.py @@ -7,7 +7,7 @@ from typing import Any from aiowebostv import WebOsClient, WebOsTvPairError from homeassistant.components.notify import ATTR_DATA, BaseNotificationService -from homeassistant.const import CONF_ICON +from homeassistant.const import ATTR_ICON from homeassistant.core import HomeAssistant from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType @@ -46,8 +46,8 @@ class LgWebOSNotificationService(BaseNotificationService): if not self._client.is_connected(): await self._client.connect() - data = kwargs.get(ATTR_DATA) - icon_path = data.get(CONF_ICON) if data else None + data = kwargs[ATTR_DATA] + icon_path = data.get(ATTR_ICON) if data else None await self._client.send_message(message, icon_path=icon_path) except WebOsTvPairError: _LOGGER.error("Pairing with TV failed") diff --git a/tests/components/webostv/test_notify.py b/tests/components/webostv/test_notify.py index 7e150c6eb78..92fb151c1b3 100644 --- a/tests/components/webostv/test_notify.py +++ b/tests/components/webostv/test_notify.py @@ -4,9 +4,13 @@ from unittest.mock import Mock, call from aiowebostv import WebOsTvPairError import pytest -from homeassistant.components.notify import ATTR_MESSAGE, DOMAIN as NOTIFY_DOMAIN +from homeassistant.components.notify import ( + ATTR_DATA, + ATTR_MESSAGE, + DOMAIN as NOTIFY_DOMAIN, +) from homeassistant.components.webostv import DOMAIN -from homeassistant.const import CONF_ICON, CONF_SERVICE_DATA +from homeassistant.const import ATTR_ICON from homeassistant.setup import async_setup_component from . import setup_webostv @@ -26,8 +30,8 @@ async def test_notify(hass, client): TV_NAME, { ATTR_MESSAGE: MESSAGE, - CONF_SERVICE_DATA: { - CONF_ICON: ICON_PATH, + ATTR_DATA: { + ATTR_ICON: ICON_PATH, }, }, blocking=True, @@ -41,7 +45,7 @@ async def test_notify(hass, client): TV_NAME, { ATTR_MESSAGE: MESSAGE, - CONF_SERVICE_DATA: { + ATTR_DATA: { "OTHER_DATA": "not_used", }, }, @@ -51,6 +55,20 @@ async def test_notify(hass, client): assert client.connect.call_count == 1 client.send_message.assert_called_with(MESSAGE, icon_path=None) + await hass.services.async_call( + NOTIFY_DOMAIN, + TV_NAME, + { + ATTR_MESSAGE: "only message, no data", + }, + blocking=True, + ) + + assert client.connect.call_count == 1 + assert client.send_message.call_args == call( + "only message, no data", icon_path=None + ) + async def test_notify_not_connected(hass, client, monkeypatch): """Test sending a message when client is not connected.""" @@ -63,8 +81,8 @@ async def test_notify_not_connected(hass, client, monkeypatch): TV_NAME, { ATTR_MESSAGE: MESSAGE, - CONF_SERVICE_DATA: { - CONF_ICON: ICON_PATH, + ATTR_DATA: { + ATTR_ICON: ICON_PATH, }, }, blocking=True, @@ -85,8 +103,8 @@ async def test_icon_not_found(hass, caplog, client, monkeypatch): TV_NAME, { ATTR_MESSAGE: MESSAGE, - CONF_SERVICE_DATA: { - CONF_ICON: ICON_PATH, + ATTR_DATA: { + ATTR_ICON: ICON_PATH, }, }, blocking=True, @@ -116,8 +134,8 @@ async def test_connection_errors(hass, caplog, client, monkeypatch, side_effect, TV_NAME, { ATTR_MESSAGE: MESSAGE, - CONF_SERVICE_DATA: { - CONF_ICON: ICON_PATH, + ATTR_DATA: { + ATTR_ICON: ICON_PATH, }, }, blocking=True, -- GitLab