Skip to content

Commit

Permalink
feat: Added support to adjust the day offset for previous consumption…
Browse files Browse the repository at this point in the history
… sensors beyond the previous day
  • Loading branch information
BottlecapDave committed Sep 22, 2023
1 parent 6651ebf commit 062ec3f
Show file tree
Hide file tree
Showing 7 changed files with 396 additions and 9 deletions.
17 changes: 15 additions & 2 deletions custom_components/octopus_energy/config/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
from ..const import CONFIG_MAIN_ACCOUNT_ID, CONFIG_MAIN_API_KEY, CONFIG_MAIN_LIVE_ELECTRICITY_CONSUMPTION_REFRESH_IN_MINUTES, CONFIG_MAIN_LIVE_GAS_CONSUMPTION_REFRESH_IN_MINUTES, CONFIG_MAIN_SUPPORTS_LIVE_CONSUMPTION
from ..const import (
CONFIG_MAIN_ACCOUNT_ID,
CONFIG_MAIN_API_KEY,
CONFIG_MAIN_LIVE_ELECTRICITY_CONSUMPTION_REFRESH_IN_MINUTES,
CONFIG_MAIN_LIVE_GAS_CONSUMPTION_REFRESH_IN_MINUTES,
CONFIG_MAIN_PREVIOUS_ELECTRICITY_CONSUMPTION_DAYS_OFFSET,
CONFIG_MAIN_PREVIOUS_GAS_CONSUMPTION_DAYS_OFFSET,
CONFIG_MAIN_SUPPORTS_LIVE_CONSUMPTION
)
from ..api_client import OctopusEnergyApiClient


async def async_validate_main_config(data):
errors = {}

Expand All @@ -19,4 +26,10 @@ async def async_validate_main_config(data):
if data[CONFIG_MAIN_LIVE_GAS_CONSUMPTION_REFRESH_IN_MINUTES] < 1:
errors[CONFIG_MAIN_LIVE_GAS_CONSUMPTION_REFRESH_IN_MINUTES] = "value_greater_than_zero"

if data[CONFIG_MAIN_PREVIOUS_ELECTRICITY_CONSUMPTION_DAYS_OFFSET] < 1:
errors[CONFIG_MAIN_PREVIOUS_ELECTRICITY_CONSUMPTION_DAYS_OFFSET] = "value_greater_than_zero"

if data[CONFIG_MAIN_PREVIOUS_GAS_CONSUMPTION_DAYS_OFFSET] < 1:
errors[CONFIG_MAIN_PREVIOUS_GAS_CONSUMPTION_DAYS_OFFSET] = "value_greater_than_zero"

return errors
19 changes: 17 additions & 2 deletions custom_components/octopus_energy/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@
from .config.target_rates import validate_target_rate_config
from .config.main import async_validate_main_config
from .const import (
CONFIG_DEFAULT_LIVE_ELECTRICITY_CONSUMPTION_REFRESH_IN_MINUTES,
CONFIG_DEFAULT_LIVE_GAS_CONSUMPTION_REFRESH_IN_MINUTES,
CONFIG_DEFAULT_PREVIOUS_CONSUMPTION_OFFSET_IN_DAYS,
CONFIG_MAIN_LIVE_ELECTRICITY_CONSUMPTION_REFRESH_IN_MINUTES,
CONFIG_MAIN_LIVE_GAS_CONSUMPTION_REFRESH_IN_MINUTES,
CONFIG_MAIN_PREVIOUS_ELECTRICITY_CONSUMPTION_DAYS_OFFSET,
CONFIG_MAIN_PREVIOUS_GAS_CONSUMPTION_DAYS_OFFSET,
DOMAIN,

CONFIG_MAIN_API_KEY,
Expand Down Expand Up @@ -227,13 +232,21 @@ async def __async_setup_main_schema(self, config, errors):
if CONFIG_MAIN_SUPPORTS_LIVE_CONSUMPTION in config:
supports_live_consumption = config[CONFIG_MAIN_SUPPORTS_LIVE_CONSUMPTION]

live_electricity_consumption_refresh_in_minutes = 1
live_electricity_consumption_refresh_in_minutes = CONFIG_DEFAULT_LIVE_ELECTRICITY_CONSUMPTION_REFRESH_IN_MINUTES
if CONFIG_MAIN_LIVE_ELECTRICITY_CONSUMPTION_REFRESH_IN_MINUTES in config:
live_electricity_consumption_refresh_in_minutes = config[CONFIG_MAIN_LIVE_ELECTRICITY_CONSUMPTION_REFRESH_IN_MINUTES]

live_gas_consumption_refresh_in_minutes = 2
live_gas_consumption_refresh_in_minutes = CONFIG_DEFAULT_LIVE_GAS_CONSUMPTION_REFRESH_IN_MINUTES
if CONFIG_MAIN_LIVE_GAS_CONSUMPTION_REFRESH_IN_MINUTES in config:
live_gas_consumption_refresh_in_minutes = config[CONFIG_MAIN_LIVE_GAS_CONSUMPTION_REFRESH_IN_MINUTES]

previous_electricity_consumption_days_offset = CONFIG_DEFAULT_PREVIOUS_CONSUMPTION_OFFSET_IN_DAYS
if CONFIG_MAIN_PREVIOUS_ELECTRICITY_CONSUMPTION_DAYS_OFFSET in config:
previous_electricity_consumption_days_offset = config[CONFIG_MAIN_PREVIOUS_ELECTRICITY_CONSUMPTION_DAYS_OFFSET]

previous_gas_consumption_days_offset = CONFIG_DEFAULT_PREVIOUS_CONSUMPTION_OFFSET_IN_DAYS
if CONFIG_MAIN_PREVIOUS_GAS_CONSUMPTION_DAYS_OFFSET in config:
previous_gas_consumption_days_offset = config[CONFIG_MAIN_PREVIOUS_GAS_CONSUMPTION_DAYS_OFFSET]

calorific_value = 40
if CONFIG_MAIN_CALORIFIC_VALUE in config:
Expand All @@ -253,6 +266,8 @@ async def __async_setup_main_schema(self, config, errors):
vol.Required(CONFIG_MAIN_SUPPORTS_LIVE_CONSUMPTION, default=supports_live_consumption): bool,
vol.Required(CONFIG_MAIN_LIVE_ELECTRICITY_CONSUMPTION_REFRESH_IN_MINUTES, default=live_electricity_consumption_refresh_in_minutes): cv.positive_int,
vol.Required(CONFIG_MAIN_LIVE_GAS_CONSUMPTION_REFRESH_IN_MINUTES, default=live_gas_consumption_refresh_in_minutes): cv.positive_int,
vol.Required(CONFIG_MAIN_PREVIOUS_ELECTRICITY_CONSUMPTION_DAYS_OFFSET, default=previous_electricity_consumption_days_offset): cv.positive_int,
vol.Required(CONFIG_MAIN_PREVIOUS_GAS_CONSUMPTION_DAYS_OFFSET, default=previous_gas_consumption_days_offset): cv.positive_int,
vol.Required(CONFIG_MAIN_CALORIFIC_VALUE, default=calorific_value): cv.positive_float,
electricity_price_cap_key: cv.positive_float,
vol.Required(CONFIG_MAIN_CLEAR_ELECTRICITY_PRICE_CAP): bool,
Expand Down
5 changes: 5 additions & 0 deletions custom_components/octopus_energy/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
CONFIG_MAIN_LIVE_ELECTRICITY_CONSUMPTION_REFRESH_IN_MINUTES = "live_electricity_consumption_refresh_in_minutes"
CONFIG_MAIN_LIVE_GAS_CONSUMPTION_REFRESH_IN_MINUTES = "live_gas_consumption_refresh_in_minutes"
CONFIG_MAIN_CALORIFIC_VALUE = "calorific_value"
CONFIG_MAIN_PREVIOUS_ELECTRICITY_CONSUMPTION_DAYS_OFFSET = "previous_electricity_consumption_days_offset"
CONFIG_MAIN_PREVIOUS_GAS_CONSUMPTION_DAYS_OFFSET = "previous_gas_consumption_days_offset"
CONFIG_MAIN_ELECTRICITY_PRICE_CAP = "electricity_price_cap"
CONFIG_MAIN_CLEAR_ELECTRICITY_PRICE_CAP = "clear_electricity_price_cap"
CONFIG_MAIN_GAS_PRICE_CAP = "gas_price_cap"
CONFIG_MAIN_CLEAR_GAS_PRICE_CAP = "clear_gas_price_cap"

CONFIG_DEFAULT_LIVE_ELECTRICITY_CONSUMPTION_REFRESH_IN_MINUTES = 1
CONFIG_DEFAULT_LIVE_GAS_CONSUMPTION_REFRESH_IN_MINUTES = 2
CONFIG_DEFAULT_PREVIOUS_CONSUMPTION_OFFSET_IN_DAYS = 1

CONFIG_TARGET_NAME = "Name"
CONFIG_TARGET_HOURS = "Hours"
Expand Down Expand Up @@ -71,6 +74,8 @@
vol.Required(CONFIG_MAIN_SUPPORTS_LIVE_CONSUMPTION): bool,
vol.Required(CONFIG_MAIN_LIVE_ELECTRICITY_CONSUMPTION_REFRESH_IN_MINUTES, default=CONFIG_DEFAULT_LIVE_ELECTRICITY_CONSUMPTION_REFRESH_IN_MINUTES): cv.positive_int,
vol.Required(CONFIG_MAIN_LIVE_GAS_CONSUMPTION_REFRESH_IN_MINUTES, default=CONFIG_DEFAULT_LIVE_ELECTRICITY_CONSUMPTION_REFRESH_IN_MINUTES): cv.positive_int,
vol.Required(CONFIG_MAIN_PREVIOUS_ELECTRICITY_CONSUMPTION_DAYS_OFFSET, default=CONFIG_DEFAULT_PREVIOUS_CONSUMPTION_OFFSET_IN_DAYS): cv.positive_int,
vol.Required(CONFIG_MAIN_PREVIOUS_GAS_CONSUMPTION_DAYS_OFFSET, default=CONFIG_DEFAULT_PREVIOUS_CONSUMPTION_OFFSET_IN_DAYS): cv.positive_int,
vol.Required(CONFIG_MAIN_CALORIFIC_VALUE, default=40.0): cv.positive_float,
vol.Optional(CONFIG_MAIN_ELECTRICITY_PRICE_CAP): cv.positive_float,
vol.Optional(CONFIG_MAIN_GAS_PRICE_CAP): cv.positive_float
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,16 @@ async def async_create_previous_consumption_and_rates_coordinator(
serial_number: str,
is_electricity: bool,
tariff_code: str,
is_smart_meter: bool):
is_smart_meter: bool,
days_offset: int):
"""Create reading coordinator"""

async def async_update_data():
"""Fetch data from API endpoint."""

previous_consumption_key = f'{identifier}_{serial_number}_previous_consumption_and_rates'
period_from = as_utc((now() - timedelta(days=1)).replace(hour=0, minute=0, second=0, microsecond=0))
period_to = as_utc(now().replace(hour=0, minute=0, second=0, microsecond=0))
period_from = as_utc((now() - timedelta(days=days_offset)).replace(hour=0, minute=0, second=0, microsecond=0))
period_to = period_from + timedelta(days=1)
result = await async_fetch_consumption_and_rates(
hass.data[DOMAIN][previous_consumption_key]
if previous_consumption_key in hass.data[DOMAIN] and
Expand Down
17 changes: 15 additions & 2 deletions custom_components/octopus_energy/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@
from .const import (
CONFIG_DEFAULT_LIVE_ELECTRICITY_CONSUMPTION_REFRESH_IN_MINUTES,
CONFIG_DEFAULT_LIVE_GAS_CONSUMPTION_REFRESH_IN_MINUTES,
CONFIG_DEFAULT_PREVIOUS_CONSUMPTION_OFFSET_IN_DAYS,
CONFIG_MAIN_LIVE_ELECTRICITY_CONSUMPTION_REFRESH_IN_MINUTES,
CONFIG_MAIN_LIVE_GAS_CONSUMPTION_REFRESH_IN_MINUTES,
CONFIG_MAIN_PREVIOUS_ELECTRICITY_CONSUMPTION_DAYS_OFFSET,
CONFIG_MAIN_PREVIOUS_GAS_CONSUMPTION_DAYS_OFFSET,
DOMAIN,

CONFIG_MAIN_API_KEY,
Expand Down Expand Up @@ -97,6 +100,10 @@ async def async_setup_default_sensors(hass: HomeAssistant, entry, async_add_enti
if CONFIG_MAIN_ELECTRICITY_PRICE_CAP in config:
electricity_price_cap = config[CONFIG_MAIN_ELECTRICITY_PRICE_CAP]

previous_electricity_consumption_days_offset = CONFIG_DEFAULT_PREVIOUS_CONSUMPTION_OFFSET_IN_DAYS
if CONFIG_MAIN_PREVIOUS_ELECTRICITY_CONSUMPTION_DAYS_OFFSET in config:
previous_electricity_consumption_days_offset = config[CONFIG_MAIN_PREVIOUS_ELECTRICITY_CONSUMPTION_DAYS_OFFSET]

for point in account_info["electricity_meter_points"]:
# We only care about points that have active agreements
electricity_tariff_code = get_active_tariff_code(now, point["agreements"])
Expand All @@ -115,7 +122,8 @@ async def async_setup_default_sensors(hass: HomeAssistant, entry, async_add_enti
meter["serial_number"],
True,
electricity_tariff_code,
meter["is_smart_meter"]
meter["is_smart_meter"],
previous_electricity_consumption_days_offset
)
entities.append(OctopusEnergyPreviousAccumulativeElectricityConsumption(hass, previous_consumption_coordinator, electricity_tariff_code, meter, point))
entities.append(OctopusEnergyPreviousAccumulativeElectricityConsumptionPeak(hass, previous_consumption_coordinator, electricity_tariff_code, meter, point))
Expand Down Expand Up @@ -159,6 +167,10 @@ async def async_setup_default_sensors(hass: HomeAssistant, entry, async_add_enti
gas_rate_coordinator = await async_create_gas_rate_coordinator(hass, client)
gas_standing_charges_coordinator = await async_setup_gas_standing_charges_coordinator(hass, config[CONFIG_MAIN_ACCOUNT_ID])

previous_gas_consumption_days_offset = CONFIG_DEFAULT_PREVIOUS_CONSUMPTION_OFFSET_IN_DAYS
if CONFIG_MAIN_PREVIOUS_GAS_CONSUMPTION_DAYS_OFFSET in config:
previous_gas_consumption_days_offset = config[CONFIG_MAIN_PREVIOUS_GAS_CONSUMPTION_DAYS_OFFSET]

for point in account_info["gas_meter_points"]:
# We only care about points that have active agreements
gas_tariff_code = get_active_tariff_code(now, point["agreements"])
Expand All @@ -177,7 +189,8 @@ async def async_setup_default_sensors(hass: HomeAssistant, entry, async_add_enti
meter["serial_number"],
False,
gas_tariff_code,
None
None,
previous_gas_consumption_days_offset
)
entities.append(OctopusEnergyPreviousAccumulativeGasConsumption(hass, previous_consumption_coordinator, gas_tariff_code, meter, point, calorific_value))
entities.append(OctopusEnergyPreviousAccumulativeGasConsumptionKwh(hass, previous_consumption_coordinator, gas_tariff_code, meter, point, calorific_value))
Expand Down
4 changes: 4 additions & 0 deletions custom_components/octopus_energy/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"live_consumption_refresh_in_minutes": "Home Mini refresh rate in minutes",
"live_electricity_consumption_refresh_in_minutes": "Home Mini electricity refresh rate in minutes",
"live_gas_consumption_refresh_in_minutes": "Home Mini gas refresh rate in minutes",
"previous_electricity_consumption_days_offset": "Previous consumption electricity days offset",
"previous_gas_consumption_days_offset": "Previous consumption gas days offset",
"calorific_value": "Gas calorific value. This can be found on your gas statement and can change from time to time.",
"electricity_price_cap": "Optional electricity price cap in pence",
"gas_price_cap": "Optional gas price cap in pence"
Expand Down Expand Up @@ -57,6 +59,8 @@
"supports_live_consumption": "I have a Home Mini - https://octopus.energy/blog/octopus-home-mini/",
"live_electricity_consumption_refresh_in_minutes": "Home Mini electricity refresh rate in minutes",
"live_gas_consumption_refresh_in_minutes": "Home Mini gas refresh rate in minutes",
"previous_electricity_consumption_days_offset": "Previous consumption electricity days offset",
"previous_gas_consumption_days_offset": "Previous consumption gas days offset",
"calorific_value": "Gas calorific value. This can be found on your gas statement and can change from time to time.",
"electricity_price_cap": "Optional electricity price cap in pence",
"clear_electricity_price_cap": "Clear electricity price cap",
Expand Down
Loading

0 comments on commit 062ec3f

Please sign in to comment.