Skip to content

Commit

Permalink
#48: Added sensor for year-to-date consumption.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rune A. Juel Mønnike committed Sep 13, 2022
1 parent fe2dbd2 commit 563b9c3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
39 changes: 26 additions & 13 deletions custom_components/eloverblik/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import voluptuous as vol
from homeassistant.util import Throttle
from datetime import timedelta
from datetime import timedelta, date

from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
Expand Down Expand Up @@ -67,28 +67,35 @@ def __init__(self, refresh_token, metering_point):
self._client = Eloverblik(refresh_token)
self._metering_point = metering_point

self._data = None
self._day_data = None
self._year_data = None

def get_total_day(self):
if self._data != None:
return round(self._data.get_total_metering_data(), 3)
if self._day_data != None:
return round(self._day_data.get_total_metering_data(), 3)
else:
return None

def get_total_year(self):
if self._day_data != None:
return round(self._year_data.get_total_metering_data(), 3)
else:
return None

def get_usage_hour(self, hour):
if self._data != None:
if self._day_data != None:
try:
return round(self._data.get_metering_data(hour), 3)
return round(self._day_data.get_metering_data(hour), 3)
except IndexError:
self._data.get_metering_data(23)
self._day_data.get_metering_data(23)
_LOGGER.info(f"Unable to get data for hour {hour}. If siwtch to daylight saving day this is not an error.")
return 0
else:
return None

def get_data_date(self):
if self._data != None:
return self._data.data_date.date().strftime('%Y-%m-%d')
if self._day_data != None:
return self._day_data.data_date.date().strftime('%Y-%m-%d')
else:
return None

Expand All @@ -100,11 +107,17 @@ def update(self):
_LOGGER.debug("Fetching data from Eloverblik")

try:
data = self._client.get_latest(self._metering_point)
if data.status == 200:
self._data = data
day_data = self._client.get_latest(self._metering_point)
if day_data.status == 200:
self._day_data = day_data
else:
_LOGGER.warn(f"Error from eloverblik when getting day data: {day_data.status} - {day_data.detailed_status}")

year_data = self._client.get_per_month(self._metering_point)
if year_data.status == 200:
self._year_data = year_data
else:
_LOGGER.warn(f"Error from eloverblik: {data.status} - {data.detailed_status}")
_LOGGER.warn(f"Error from eloverblik when getting year data: {year_data.status} - {year_data.detailed_status}")
except requests.exceptions.HTTPError as he:
message = None
if he.response.status_code == 401:
Expand Down
14 changes: 11 additions & 3 deletions custom_components/eloverblik/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ async def async_setup_entry(hass, config, async_add_entities):

sensors = []
sensors.append(EloverblikEnergy("Eloverblik Energy Total", 'total', eloverblik))
sensors.append(EloverblikEnergy("Eloverblik Energy Total (Year)", 'year_total', eloverblik))
for x in range(1, 25):
sensors.append(EloverblikEnergy(f"Eloverblik Energy {x-1}-{x}", 'hour', eloverblik, x))
async_add_entities(sensors)
Expand All @@ -35,8 +36,12 @@ def __init__(self, name, sensor_type, client, hour=None):

if sensor_type == 'hour':
self._unique_id = f"{self._data.get_metering_point()}-{hour}"
else:
elif sensor_type == 'total':
self._unique_id = f"{self._data.get_metering_point()}-total"
elif sensor_type == 'year_total':
self._unique_id = f"{self._data.get_metering_point()}-year-total"
else:
raise ValueError(f"Unexpected sensor_type: {sensor_type}.")

@property
def name(self):
Expand Down Expand Up @@ -75,8 +80,11 @@ def update(self):

self._data_date = self._data.get_data_date()

if self._sensor_type == 'hour':
self._state = self._data.get_usage_hour(self._hour)
if self._sensor_type == 'total':
self._state = self._data.get_total_day()
elif self._sensor_type == 'year_total':
self._state = self._data.get_total_year()
else:
self._state = self._data.get_usage_hour(self._hour)

raise ValueError(f"Unexpected sensor_type: {self._sensor_type}.")

0 comments on commit 563b9c3

Please sign in to comment.