diff --git a/homeassistant/components/sensor/tibber.py b/homeassistant/components/sensor/tibber.py index dbea54ff353..0f127d7dd64 100644 --- a/homeassistant/components/sensor/tibber.py +++ b/homeassistant/components/sensor/tibber.py @@ -14,14 +14,14 @@ 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 +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__) @@ -30,6 +30,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ }) ICON = 'mdi:currency-usd' +ICON_RT = 'mdi:power-plug' SCAN_INTERVAL = timedelta(minutes=1) MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=5) @@ -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.""" @@ -155,3 +162,71 @@ class TibberSensor(Entity): 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): + """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) diff --git a/requirements_all.txt b/requirements_all.txt index b6f9dd4875e..6c8b86a03f6 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -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