2017-10-04 10:31:42 +02:00
|
|
|
"""
|
|
|
|
Support for Tibber.
|
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/sensor.tibber/
|
|
|
|
"""
|
|
|
|
import asyncio
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from datetime import timedelta
|
2018-03-01 23:15:27 +01:00
|
|
|
import aiohttp
|
2017-10-04 10:31:42 +02:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
|
|
|
from homeassistant.const import CONF_ACCESS_TOKEN
|
2018-03-01 23:15:27 +01:00
|
|
|
from homeassistant.exceptions import PlatformNotReady
|
2017-10-04 10:31:42 +02:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
from homeassistant.util import dt as dt_util
|
|
|
|
|
2018-03-03 13:18:45 +01:00
|
|
|
REQUIREMENTS = ['pyTibber==0.3.2']
|
2017-10-04 10:31:42 +02:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_ACCESS_TOKEN): cv.string
|
|
|
|
})
|
|
|
|
|
|
|
|
ICON = 'mdi:currency-usd'
|
|
|
|
SCAN_INTERVAL = timedelta(minutes=1)
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
|
|
|
"""Set up the Tibber sensor."""
|
2018-03-01 23:15:27 +01:00
|
|
|
import tibber
|
|
|
|
tibber_connection = tibber.Tibber(config[CONF_ACCESS_TOKEN],
|
|
|
|
websession=async_get_clientsession(hass))
|
|
|
|
|
|
|
|
try:
|
|
|
|
yield from tibber_connection.update_info()
|
|
|
|
dev = []
|
|
|
|
for home in tibber_connection.get_homes():
|
|
|
|
yield from home.update_info()
|
|
|
|
dev.append(TibberSensor(home))
|
|
|
|
except (asyncio.TimeoutError, aiohttp.ClientError):
|
|
|
|
raise PlatformNotReady() from None
|
2017-10-04 10:31:42 +02:00
|
|
|
|
2018-02-15 12:33:49 +01:00
|
|
|
async_add_devices(dev, True)
|
2017-10-04 10:31:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TibberSensor(Entity):
|
|
|
|
"""Representation of an Tibber sensor."""
|
|
|
|
|
|
|
|
def __init__(self, tibber_home):
|
|
|
|
"""Initialize the sensor."""
|
|
|
|
self._tibber_home = tibber_home
|
|
|
|
self._last_updated = None
|
|
|
|
self._state = None
|
|
|
|
self._device_state_attributes = None
|
|
|
|
self._unit_of_measurement = None
|
|
|
|
self._name = 'Electricity price {}'.format(self._tibber_home.address1)
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def async_update(self):
|
|
|
|
"""Get the latest data and updates the states."""
|
|
|
|
if self._tibber_home.current_price_total and self._last_updated and \
|
|
|
|
dt_util.as_utc(dt_util.parse_datetime(self._last_updated)).hour\
|
|
|
|
== dt_util.utcnow().hour:
|
|
|
|
return
|
|
|
|
|
|
|
|
yield from self._tibber_home.update_current_price_info()
|
|
|
|
|
|
|
|
self._state = self._tibber_home.current_price_total
|
|
|
|
self._last_updated = self._tibber_home.current_price_info.\
|
|
|
|
get('startsAt')
|
|
|
|
self._device_state_attributes = self._tibber_home.current_price_info
|
|
|
|
self._unit_of_measurement = self._tibber_home.price_unit
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
|
|
|
return self._device_state_attributes
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the device."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon to use in the frontend."""
|
|
|
|
return ICON
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit of measurement of this entity."""
|
|
|
|
return self._unit_of_measurement
|
2018-03-03 13:18:45 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
2018-03-03 19:23:55 +01:00
|
|
|
"""Return a unique ID."""
|
2018-03-03 13:18:45 +01:00
|
|
|
home = self._tibber_home.info['viewer']['home']
|
|
|
|
return home['meteringPointData']['consumptionEan']
|