Skip to content

Commit

Permalink
Add hourly report (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
tetienne authored Dec 25, 2020
1 parent 9b9661d commit ba9daea
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 39 deletions.
45 changes: 36 additions & 9 deletions custom_components/veolia/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,30 @@
import asyncio
from datetime import datetime, timedelta
import logging
import time

from homeassistant.config_entries import ConfigEntry
from homeassistant.core import Config, HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import aiohttp_client
from homeassistant.helpers.typing import HomeAssistantType
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator

from pyolia.client import VeoliaClient

from .const import (
API,
CONF_PASSWORD,
CONF_USERNAME,
COORDINATOR,
DAILY,
DOMAIN,
HOURLY,
LAST_REPORT_TIMESTAMP,
PLATFORMS,
)

SCAN_INTERVAL = timedelta(days=1)
SCAN_INTERVAL = timedelta(seconds=30)


_LOGGER = logging.getLogger(__name__)

Expand All @@ -48,11 +52,34 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
hass.data[DOMAIN][API] = VeoliaClient(username, password, session)

async def _get_consumption():
"""Return the water consumption for each day of the current month."""
now = datetime.now()
if now.day < 4:
now = now - timedelta(days=3)
return await hass.data[DOMAIN][API].get_consumption(now.month, now.year)
"""Return the water consumption."""
api = hass.data[DOMAIN][API]
last_report_date = api.last_report_date
last_report_timestamp = time.mktime(
datetime(
last_report_date.year, last_report_date.month, last_report_date.day
).timetuple()
)

previous_data = hass.data[DOMAIN][COORDINATOR].data
if (
previous_data
and previous_data[LAST_REPORT_TIMESTAMP] == last_report_timestamp
):
return previous_data

daily_consumption = await api.get_consumption(
last_report_date.month, last_report_date.year
)

hourly_consumption = await api.get_consumption(
last_report_date.month, last_report_date.year, last_report_date.day
)
return {
DAILY: daily_consumption,
HOURLY: hourly_consumption,
LAST_REPORT_TIMESTAMP: last_report_timestamp,
}

coordinator = DataUpdateCoordinator(
hass,
Expand All @@ -62,13 +89,13 @@ async def _get_consumption():
update_interval=SCAN_INTERVAL,
)

hass.data[DOMAIN][COORDINATOR] = coordinator

await coordinator.async_refresh()

if not coordinator.last_update_success:
raise ConfigEntryNotReady

hass.data[DOMAIN][COORDINATOR] = coordinator

for platform in PLATFORMS:
hass.async_add_job(
hass.config_entries.async_forward_entry_setup(entry, platform)
Expand Down
4 changes: 4 additions & 0 deletions custom_components/veolia/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@
CONF_PASSWORD = "password"
API = "api"
COORDINATOR = "coordinator"

DAILY = "daily"
HOURLY = "hourly"
LAST_REPORT_TIMESTAMP = "last_report_timestamp"
18 changes: 13 additions & 5 deletions custom_components/veolia/entity.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
"""VeoliaEntity class"""
from datetime import datetime

from homeassistant.const import VOLUME_LITERS
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import DOMAIN, NAME
from .const import DOMAIN, LAST_REPORT_TIMESTAMP, NAME


class VeoliaEntity(CoordinatorEntity):
Expand All @@ -27,7 +26,16 @@ def device_info(self):
@property
def device_state_attributes(self):
"""Return the state attributes."""
last_timestamp = list(self.coordinator.data.keys())[-1]
return {
"last_report": datetime.fromtimestamp(last_timestamp),
"last_report": self.coordinator.data[LAST_REPORT_TIMESTAMP],
}

@property
def icon(self) -> str:
"""Return the usage icon."""
return "mdi:water"

@property
def unit_of_measurement(self) -> str:
"""Return liter as the unit measurement for water."""
return VOLUME_LITERS
2 changes: 1 addition & 1 deletion custom_components/veolia/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"@tetienne"
],
"requirements": [
"pyolia==0.1.0"
"pyolia==0.2.0"
]
}
44 changes: 20 additions & 24 deletions custom_components/veolia/sensor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Sensor platform for Veolia."""
from homeassistant.const import VOLUME_LITERS
import datetime

from .const import COORDINATOR, DOMAIN
from .const import COORDINATOR, DAILY, DOMAIN, HOURLY
from .entity import VeoliaEntity


Expand All @@ -10,38 +10,44 @@ async def async_setup_entry(hass, entry, async_add_devices):
coordinator = hass.data[DOMAIN][COORDINATOR]
async_add_devices(
[
VeoliaHourlyUsageSensor(coordinator, entry),
VeoliaDailyUsageSensor(coordinator, entry),
VeoliaMonthlyUsageSensor(coordinator, entry),
]
)


class VeoliaDailyUsageSensor(VeoliaEntity):
"""Monitors the daily water usage."""
class VeoliaHourlyUsageSensor(VeoliaEntity):
"""Monitors the hourly water usage."""

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

@property
def state(self):
"""Return the state of the sensor."""
return list(self.coordinator.data.values())[-1]
hour = datetime.datetime.now().hour
return self.coordinator.data[HOURLY][hour - 1]


class VeoliaDailyUsageSensor(VeoliaEntity):
"""Monitors the daily water usage."""

@property
def icon(self) -> str:
"""Return the daily usage icon."""
return "mdi:water"
def name(self):
"""Return the name of the sensor."""
return "veolia_daily_consumption"

@property
def unit_of_measurement(self) -> str:
"""Return liter as the unit measurement for water."""
return VOLUME_LITERS
def state(self):
"""Return the state of the sensor."""
return self.coordinator.data[DAILY][-1]


class VeoliaMonthlyUsageSensor(VeoliaEntity):
"""Monitors the daily water usage."""
"""Monitors the monthly water usage."""

@property
def name(self):
Expand All @@ -51,14 +57,4 @@ def name(self):
@property
def state(self):
"""Return the state of the sensor."""
return sum(self.coordinator.data.values())

@property
def icon(self) -> str:
"""Return the daily usage icon."""
return "mdi:water"

@property
def unit_of_measurement(self) -> str:
"""Return liter as the unit measurement for water."""
return VOLUME_LITERS
return sum(self.coordinator.data[DAILY])

0 comments on commit ba9daea

Please sign in to comment.