mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 22:27:07 +00:00
Tibber available (#13865)
* Tibber available * Tibber available * Tibber * Tibber
This commit is contained in:
parent
8ef2abfca7
commit
825f94f47f
@ -19,6 +19,7 @@ from homeassistant.exceptions import PlatformNotReady
|
|||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.util import dt as dt_util
|
from homeassistant.util import dt as dt_util
|
||||||
|
from homeassistant.util import Throttle
|
||||||
|
|
||||||
REQUIREMENTS = ['pyTibber==0.4.1']
|
REQUIREMENTS = ['pyTibber==0.4.1']
|
||||||
|
|
||||||
@ -30,6 +31,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|||||||
|
|
||||||
ICON = 'mdi:currency-usd'
|
ICON = 'mdi:currency-usd'
|
||||||
SCAN_INTERVAL = timedelta(minutes=1)
|
SCAN_INTERVAL = timedelta(minutes=1)
|
||||||
|
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=5)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_platform(hass, config, async_add_devices,
|
async def async_setup_platform(hass, config, async_add_devices,
|
||||||
@ -58,7 +60,9 @@ class TibberSensor(Entity):
|
|||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self._tibber_home = tibber_home
|
self._tibber_home = tibber_home
|
||||||
self._last_updated = None
|
self._last_updated = None
|
||||||
|
self._newest_data_timestamp = None
|
||||||
self._state = None
|
self._state = None
|
||||||
|
self._is_available = False
|
||||||
self._device_state_attributes = {}
|
self._device_state_attributes = {}
|
||||||
self._unit_of_measurement = self._tibber_home.price_unit
|
self._unit_of_measurement = self._tibber_home.price_unit
|
||||||
self._name = 'Electricity price {}'.format(tibber_home.info['viewer']
|
self._name = 'Electricity price {}'.format(tibber_home.info['viewer']
|
||||||
@ -68,50 +72,27 @@ class TibberSensor(Entity):
|
|||||||
"""Get the latest data and updates the states."""
|
"""Get the latest data and updates the states."""
|
||||||
now = dt_util.utcnow()
|
now = dt_util.utcnow()
|
||||||
if self._tibber_home.current_price_total and self._last_updated and \
|
if self._tibber_home.current_price_total and self._last_updated and \
|
||||||
dt_util.as_utc(dt_util.parse_datetime(self._last_updated)).hour\
|
self._last_updated.hour == now.hour and self._newest_data_timestamp:
|
||||||
== now.hour:
|
|
||||||
return
|
return
|
||||||
|
|
||||||
def _find_current_price():
|
if (not self._newest_data_timestamp or
|
||||||
state = None
|
(self._newest_data_timestamp - now).total_seconds()/3600 < 12
|
||||||
max_price = None
|
or not self._is_available):
|
||||||
min_price = None
|
_LOGGER.debug("Asking for new data.")
|
||||||
for key, price_total in self._tibber_home.price_total.items():
|
await self._fetch_data()
|
||||||
price_time = dt_util.as_utc(dt_util.parse_datetime(key))
|
|
||||||
price_total = round(price_total, 3)
|
|
||||||
time_diff = (now - price_time).total_seconds()/60
|
|
||||||
if time_diff >= 0 and time_diff < 60:
|
|
||||||
state = price_total
|
|
||||||
self._last_updated = key
|
|
||||||
if now.date() == price_time.date():
|
|
||||||
if max_price is None or price_total > max_price:
|
|
||||||
max_price = price_total
|
|
||||||
if min_price is None or price_total < min_price:
|
|
||||||
min_price = price_total
|
|
||||||
self._state = state
|
|
||||||
self._device_state_attributes['max_price'] = max_price
|
|
||||||
self._device_state_attributes['min_price'] = min_price
|
|
||||||
return state is not None
|
|
||||||
|
|
||||||
if _find_current_price():
|
self._is_available = self._update_current_price()
|
||||||
return
|
|
||||||
|
|
||||||
_LOGGER.debug("No cached data found, so asking for new data")
|
|
||||||
await self._tibber_home.update_info()
|
|
||||||
await self._tibber_home.update_price_info()
|
|
||||||
data = self._tibber_home.info['viewer']['home']
|
|
||||||
self._device_state_attributes['app_nickname'] = data['appNickname']
|
|
||||||
self._device_state_attributes['grid_company'] =\
|
|
||||||
data['meteringPointData']['gridCompany']
|
|
||||||
self._device_state_attributes['estimated_annual_consumption'] =\
|
|
||||||
data['meteringPointData']['estimatedAnnualConsumption']
|
|
||||||
_find_current_price()
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_state_attributes(self):
|
def device_state_attributes(self):
|
||||||
"""Return the state attributes."""
|
"""Return the state attributes."""
|
||||||
return self._device_state_attributes
|
return self._device_state_attributes
|
||||||
|
|
||||||
|
@property
|
||||||
|
def available(self):
|
||||||
|
"""Return True if entity is available."""
|
||||||
|
return self._is_available
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
"""Return the name of the sensor."""
|
"""Return the name of the sensor."""
|
||||||
@ -137,3 +118,42 @@ class TibberSensor(Entity):
|
|||||||
"""Return a unique ID."""
|
"""Return a unique ID."""
|
||||||
home = self._tibber_home.info['viewer']['home']
|
home = self._tibber_home.info['viewer']['home']
|
||||||
return home['meteringPointData']['consumptionEan']
|
return home['meteringPointData']['consumptionEan']
|
||||||
|
|
||||||
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||||
|
async def _fetch_data(self):
|
||||||
|
try:
|
||||||
|
await self._tibber_home.update_info()
|
||||||
|
await self._tibber_home.update_price_info()
|
||||||
|
except (asyncio.TimeoutError, aiohttp.ClientError):
|
||||||
|
return
|
||||||
|
data = self._tibber_home.info['viewer']['home']
|
||||||
|
self._device_state_attributes['app_nickname'] = data['appNickname']
|
||||||
|
self._device_state_attributes['grid_company'] = \
|
||||||
|
data['meteringPointData']['gridCompany']
|
||||||
|
self._device_state_attributes['estimated_annual_consumption'] = \
|
||||||
|
data['meteringPointData']['estimatedAnnualConsumption']
|
||||||
|
|
||||||
|
def _update_current_price(self):
|
||||||
|
state = None
|
||||||
|
max_price = None
|
||||||
|
min_price = None
|
||||||
|
now = dt_util.utcnow()
|
||||||
|
for key, price_total in self._tibber_home.price_total.items():
|
||||||
|
price_time = dt_util.as_utc(dt_util.parse_datetime(key))
|
||||||
|
price_total = round(price_total, 3)
|
||||||
|
time_diff = (now - price_time).total_seconds()/60
|
||||||
|
if (not self._newest_data_timestamp or
|
||||||
|
price_time > self._newest_data_timestamp):
|
||||||
|
self._newest_data_timestamp = price_time
|
||||||
|
if 0 <= time_diff < 60:
|
||||||
|
state = price_total
|
||||||
|
self._last_updated = price_time
|
||||||
|
if now.date() == price_time.date():
|
||||||
|
if max_price is None or price_total > max_price:
|
||||||
|
max_price = price_total
|
||||||
|
if min_price is None or price_total < min_price:
|
||||||
|
min_price = price_total
|
||||||
|
self._state = state
|
||||||
|
self._device_state_attributes['max_price'] = max_price
|
||||||
|
self._device_state_attributes['min_price'] = min_price
|
||||||
|
return state is not None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user