Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 80 additions & 5 deletions homeassistant/components/sensor/tibber.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@

import homeassistant.helpers.config_validation as cv
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import CONF_ACCESS_TOKEN
from homeassistant.const import EVENT_HOMEASSISTANT_STOP, CONF_ACCESS_TOKEN
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.5.1']
REQUIREMENTS = ['pyTibber==0.6.0']

_LOGGER = logging.getLogger(__name__)

Expand All @@ -30,6 +30,7 @@
})

ICON = 'mdi:currency-usd'
ICON_RT = 'mdi:power-plug'
SCAN_INTERVAL = timedelta(minutes=1)
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=5)

Expand All @@ -41,20 +42,26 @@ async def async_setup_platform(hass, config, async_add_entities,
tibber_connection = tibber.Tibber(config[CONF_ACCESS_TOKEN],
websession=async_get_clientsession(hass))

async def _close(*_):
await tibber_connection.rt_disconnect()
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _close)

try:
await tibber_connection.update_info()
dev = []
for home in tibber_connection.get_homes():
await home.update_info()
dev.append(TibberSensor(home))
dev.append(TibberSensorElPrice(home))
if home.has_real_time_consumption:
dev.append(TibberSensorRT(home))
except (asyncio.TimeoutError, aiohttp.ClientError):
raise PlatformNotReady()

async_add_entities(dev, True)


class TibberSensor(Entity):
"""Representation of an Tibber sensor."""
class TibberSensorElPrice(Entity):
"""Representation of an Tibber sensor for el price."""

def __init__(self, tibber_home):
"""Initialize the sensor."""
Expand Down Expand Up @@ -155,3 +162,71 @@ def _update_current_price(self):
self._device_state_attributes['max_price'] = max_price
self._device_state_attributes['min_price'] = min_price
return state is not None


class TibberSensorRT(Entity):
"""Representation of an Tibber sensor for real time consumption."""

def __init__(self, tibber_home):
"""Initialize the sensor."""
self._tibber_home = tibber_home
self._state = None
self._device_state_attributes = {}
self._unit_of_measurement = 'W'
nickname = tibber_home.info['viewer']['home']['appNickname']
self._name = 'Real time consumption {}'.format(nickname)

async def async_added_to_hass(self):
"""Start unavailability tracking."""
await self._tibber_home.rt_subscribe(self.hass.loop,
self._async_callback)

async def _async_callback(self, payload):
Comment thread
Danielhiversen marked this conversation as resolved.
"""Handle received data."""
data = payload.get('data', {})
live_measurement = data.get('liveMeasurement', {})
self._state = live_measurement.pop('power', None)
self._device_state_attributes = live_measurement
self.async_schedule_update_ha_state()

@property
def device_state_attributes(self):
"""Return the state attributes."""
return self._device_state_attributes

@property
def available(self):
"""Return True if entity is available."""
return self._tibber_home.rt_subscription_running

@property
def name(self):
"""Return the name of the sensor."""
return self._name

@property
def should_poll(self):
"""Return the polling state."""
return False

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

@property
def unit_of_measurement(self):
"""Return the unit of measurement of this entity."""
return self._unit_of_measurement

@property
def unique_id(self):
"""Return a unique ID."""
home = self._tibber_home.info['viewer']['home']
_id = home['meteringPointData']['consumptionEan']
return'{}_rt_consumption'.format(_id)
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ pyRFXtrx==0.23
pySwitchmate==0.4.1

# homeassistant.components.sensor.tibber
pyTibber==0.5.1
pyTibber==0.6.0

# homeassistant.components.switch.dlink
pyW215==0.6.0
Expand Down