From 05d8c57212f3895a88d81de267e036923ddc35d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=B8yer=20Iversen?= Date: Thu, 4 Oct 2018 09:29:49 +0200 Subject: [PATCH] Tibber component and notify (#17062) * Refactor tibber, and Tibber notify * update Tibber lib. * tibber * Tibber coveragerc * Tibber upgrade lib * style * comments * use async_get_service * event --- .coveragerc | 4 +- homeassistant/components/notify/tibber.py | 37 ++++++++++++++ homeassistant/components/sensor/tibber.py | 24 +++------ homeassistant/components/tibber/__init__.py | 55 +++++++++++++++++++++ requirements_all.txt | 4 +- 5 files changed, 103 insertions(+), 21 deletions(-) create mode 100644 homeassistant/components/notify/tibber.py create mode 100644 homeassistant/components/tibber/__init__.py diff --git a/.coveragerc b/.coveragerc index 7941bd5708f..1cd4c1774e4 100644 --- a/.coveragerc +++ b/.coveragerc @@ -316,6 +316,9 @@ omit = homeassistant/components/*/thinkingcleaner.py + homeassistant/components/tibber/* + homeassistant/components/*/tibber.py + homeassistant/components/toon.py homeassistant/components/*/toon.py @@ -774,7 +777,6 @@ omit = homeassistant/components/sensor/tank_utility.py homeassistant/components/sensor/ted5000.py homeassistant/components/sensor/temper.py - homeassistant/components/sensor/tibber.py homeassistant/components/sensor/time_date.py homeassistant/components/sensor/torque.py homeassistant/components/sensor/trafikverket_weatherstation.py diff --git a/homeassistant/components/notify/tibber.py b/homeassistant/components/notify/tibber.py new file mode 100644 index 00000000000..ddbcb3f6c65 --- /dev/null +++ b/homeassistant/components/notify/tibber.py @@ -0,0 +1,37 @@ +""" +Tibber platform for notify component. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/notify.tibber/ +""" +import asyncio +import logging + +from homeassistant.components.notify import ( + ATTR_TITLE, ATTR_TITLE_DEFAULT, BaseNotificationService) +from homeassistant.components.tibber import DOMAIN as TIBBER_DOMAIN + + +_LOGGER = logging.getLogger(__name__) + + +async def async_get_service(hass, config, discovery_info=None): + """Get the Tibber notification service.""" + tibber_connection = hass.data[TIBBER_DOMAIN] + return TibberNotificationService(tibber_connection.send_notification) + + +class TibberNotificationService(BaseNotificationService): + """Implement the notification service for Tibber.""" + + def __init__(self, notify): + """Initialize the service.""" + self._notify = notify + + async def async_send_message(self, message=None, **kwargs): + """Send a message to Tibber devices.""" + title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT) + try: + await self._notify(title=title, message=message) + except asyncio.TimeoutError: + _LOGGER.error("Timeout sending message with Tibber") diff --git a/homeassistant/components/sensor/tibber.py b/homeassistant/components/sensor/tibber.py index 0f127d7dd64..1207c8dfe20 100644 --- a/homeassistant/components/sensor/tibber.py +++ b/homeassistant/components/sensor/tibber.py @@ -10,25 +10,15 @@ import logging from datetime import timedelta import aiohttp -import voluptuous as vol -import homeassistant.helpers.config_validation as cv -from homeassistant.components.sensor import PLATFORM_SCHEMA -from homeassistant.const import EVENT_HOMEASSISTANT_STOP, CONF_ACCESS_TOKEN +from homeassistant.components.tibber import DOMAIN as TIBBER_DOMAIN from homeassistant.exceptions import PlatformNotReady -from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.entity import Entity from homeassistant.util import dt as dt_util from homeassistant.util import Throttle -REQUIREMENTS = ['pyTibber==0.6.0'] - _LOGGER = logging.getLogger(__name__) -PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ - vol.Required(CONF_ACCESS_TOKEN): cv.string -}) - ICON = 'mdi:currency-usd' ICON_RT = 'mdi:power-plug' SCAN_INTERVAL = timedelta(minutes=1) @@ -38,16 +28,14 @@ MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=5) async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): """Set up the Tibber sensor.""" - import tibber - tibber_connection = tibber.Tibber(config[CONF_ACCESS_TOKEN], - websession=async_get_clientsession(hass)) + if discovery_info is None: + _LOGGER.error("Tibber sensor configuration has changed." + " Check https://home-assistant.io/components/tibber/") + return - async def _close(*_): - await tibber_connection.rt_disconnect() - hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _close) + tibber_connection = hass.data.get(TIBBER_DOMAIN) try: - await tibber_connection.update_info() dev = [] for home in tibber_connection.get_homes(): await home.update_info() diff --git a/homeassistant/components/tibber/__init__.py b/homeassistant/components/tibber/__init__.py new file mode 100644 index 00000000000..8022902c580 --- /dev/null +++ b/homeassistant/components/tibber/__init__.py @@ -0,0 +1,55 @@ +""" +Support for Tibber. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/tibber/ +""" +import asyncio +import logging + +import aiohttp +import voluptuous as vol + +import homeassistant.helpers.config_validation as cv +from homeassistant.const import (EVENT_HOMEASSISTANT_STOP, CONF_ACCESS_TOKEN, + CONF_NAME) +from homeassistant.helpers import discovery +from homeassistant.helpers.aiohttp_client import async_get_clientsession + +REQUIREMENTS = ['pyTibber==0.7.2'] + +DOMAIN = 'tibber' + +CONFIG_SCHEMA = vol.Schema({ + DOMAIN: vol.Schema({ + vol.Required(CONF_ACCESS_TOKEN): cv.string, + }) +}, extra=vol.ALLOW_EXTRA) + +_LOGGER = logging.getLogger(__name__) + + +async def async_setup(hass, config): + """Set up the Tibber component.""" + conf = config.get(DOMAIN) + + import tibber + tibber_connection = tibber.Tibber(conf[CONF_ACCESS_TOKEN], + websession=async_get_clientsession(hass)) + hass.data[DOMAIN] = tibber_connection + + async def _close(event): + await tibber_connection.rt_disconnect() + + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _close) + + try: + await tibber_connection.update_info() + except (asyncio.TimeoutError, aiohttp.ClientError): + return False + + for component in ['sensor', 'notify']: + discovery.load_platform(hass, component, DOMAIN, + {CONF_NAME: DOMAIN}, config) + + return True diff --git a/requirements_all.txt b/requirements_all.txt index 4cb5097d69e..ead19a57f17 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -767,8 +767,8 @@ pyRFXtrx==0.23 # homeassistant.components.switch.switchmate pySwitchmate==0.4.1 -# homeassistant.components.sensor.tibber -pyTibber==0.6.0 +# homeassistant.components.tibber +pyTibber==0.7.2 # homeassistant.components.switch.dlink pyW215==0.6.0