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
This commit is contained in:
Daniel Høyer Iversen 2018-10-04 09:29:49 +02:00 committed by Pascal Vizeli
parent 3abdf217bb
commit 05d8c57212
5 changed files with 103 additions and 21 deletions

View file

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

View file

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

View file

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

View file

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

View file

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