hass-core/homeassistant/components/webostv/notify.py
Josh Bendavid fc23b4f83f Migrate webostv to new library and make integration async with callback state updates (#29296)
* migrate webostv to new aiopylgtv version of the library and add support
for generic commands, input/button commands, and callback state updates

* update requirements

* cleanup and bump aiopylgtv version

* update webostv unit tests

* make webostv unit tests work with python 3.7

* cleanup for code checks

* cleanup and code review

* make all client request functions coroutines

* make host required for webostv configuration

* remove generic command and button functionality plus related cleanup

* fix previous track function

* update unit tests

* fix imports for unit tests

* update unit test

* further unit test updates

* remove unnecessary setup call in unit tests

* restore previous behaviour with client key config file in hass configuration directory
2020-01-01 00:26:35 +01:00

60 lines
1.8 KiB
Python

"""Support for LG WebOS TV notification service."""
import asyncio
import logging
from aiopylgtv import PyLGTVCmdException, PyLGTVPairException
from websockets.exceptions import ConnectionClosed
from homeassistant.components.notify import ATTR_DATA, BaseNotificationService
from homeassistant.const import CONF_HOST, CONF_ICON
from . import DOMAIN
_LOGGER = logging.getLogger(__name__)
async def async_get_service(hass, config, discovery_info=None):
"""Return the notify service."""
host = discovery_info.get(CONF_HOST)
icon_path = discovery_info.get(CONF_ICON)
client = hass.data[DOMAIN][host]["client"]
svc = LgWebOSNotificationService(client, icon_path)
return svc
class LgWebOSNotificationService(BaseNotificationService):
"""Implement the notification service for LG WebOS TV."""
def __init__(self, client, icon_path):
"""Initialize the service."""
self._client = client
self._icon_path = icon_path
async def async_send_message(self, message="", **kwargs):
"""Send a message to the tv."""
try:
if not self._client.is_connected():
await self._client.connect()
data = kwargs.get(ATTR_DATA)
icon_path = (
data.get(CONF_ICON, self._icon_path) if data else self._icon_path
)
await self._client.send_message(message, icon_path=icon_path)
except PyLGTVPairException:
_LOGGER.error("Pairing with TV failed")
except FileNotFoundError:
_LOGGER.error("Icon %s not found", icon_path)
except (
OSError,
ConnectionClosed,
ConnectionRefusedError,
asyncio.TimeoutError,
asyncio.CancelledError,
PyLGTVCmdException,
):
_LOGGER.error("TV unreachable")