Skip to content

Commit

Permalink
chore: Fixed rates to ignore planned dispatches if unsupported intell…
Browse files Browse the repository at this point in the history
…igent provider
  • Loading branch information
BottlecapDave committed Jan 2, 2024
1 parent 9b36f07 commit 8ed5496
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 14 deletions.
25 changes: 16 additions & 9 deletions custom_components/octopus_energy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from .coordinators.electricity_rates import async_setup_electricity_rates_coordinator
from .coordinators.saving_sessions import async_setup_saving_sessions_coordinators
from .statistics import get_statistic_ids_to_remove
from .intelligent import async_mock_intelligent_data, is_intelligent_tariff, mock_intelligent_device
from .intelligent import async_mock_intelligent_data, get_intelligent_features, is_intelligent_tariff, mock_intelligent_device

from .config.main import async_migrate_main_config
from .config.target_rates import async_migrate_target_config
Expand Down Expand Up @@ -177,16 +177,10 @@ async def async_setup_dependencies(hass, config):
electricity_tariff_code = get_active_tariff_code(now, point["agreements"])
if electricity_tariff_code is not None:
for meter in point["meters"]:
mpan = point["mpan"]
serial_number = meter["serial_number"]
is_export_meter = meter["is_export"]
is_smart_meter = meter["is_smart_meter"]
await async_setup_electricity_rates_coordinator(hass, account_id, mpan, serial_number, is_smart_meter, is_export_meter)

if meter["is_export"] == False:
if is_intelligent_tariff(electricity_tariff_code):
intelligent_mpan = mpan
intelligent_serial_number = serial_number
intelligent_mpan = point["mpan"]
intelligent_serial_number = meter["serial_number"]
has_intelligent_tariff = True

should_mock_intelligent_data = await async_mock_intelligent_data(hass, account_id)
Expand All @@ -200,6 +194,7 @@ async def async_setup_dependencies(hass, config):
intelligent_serial_number = meter["serial_number"]
break

intelligent_device = None
if has_intelligent_tariff or should_mock_intelligent_data:
client: OctopusEnergyApiClient = hass.data[DOMAIN][account_id][DATA_CLIENT]

Expand All @@ -212,6 +207,18 @@ async def async_setup_dependencies(hass, config):
hass.data[DOMAIN][account_id][DATA_INTELLIGENT_MPAN] = intelligent_mpan
hass.data[DOMAIN][account_id][DATA_INTELLIGENT_SERIAL_NUMBER] = intelligent_serial_number

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"])
if electricity_tariff_code is not None:
for meter in point["meters"]:
mpan = point["mpan"]
serial_number = meter["serial_number"]
is_export_meter = meter["is_export"]
is_smart_meter = meter["is_smart_meter"]
planned_dispatches_supported = get_intelligent_features(intelligent_device["provider"]).planned_dispatches_supported if intelligent_device is not None else True
await async_setup_electricity_rates_coordinator(hass, account_id, mpan, serial_number, is_smart_meter, is_export_meter, planned_dispatches_supported)

await async_setup_account_info_coordinator(hass, account_id)

await async_setup_intelligent_dispatches_coordinator(hass, account_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ async def async_refresh_electricity_rates_data(

return existing_rates_result

async def async_setup_electricity_rates_coordinator(hass, account_id: str, target_mpan: str, target_serial_number: str, is_smart_meter: bool, is_export_meter: bool):
async def async_setup_electricity_rates_coordinator(hass, account_id: str, target_mpan: str, target_serial_number: str, is_smart_meter: bool, is_export_meter: bool, planned_dispatches_supported: bool):
key = DATA_ELECTRICITY_RATES_KEY.format(target_mpan, target_serial_number)

# Reset data rates as we might have new information
Expand All @@ -128,7 +128,11 @@ async def async_update_electricity_rates_data():
is_smart_meter,
is_export_meter,
rates,
dispatches.dispatches if dispatches is not None else None,
IntelligentDispatches(
dispatches.dispatches.planned if planned_dispatches_supported == True else [],
dispatches.dispatches.completed
)
if dispatches is not None else None,
hass.bus.async_fire
)

Expand Down
19 changes: 17 additions & 2 deletions custom_components/octopus_energy/intelligent/charge_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from .base import OctopusEnergyIntelligentSensor
from ..api_client import OctopusEnergyApiClient
from ..coordinators.intelligent_settings import IntelligentCoordinatorResult

from ..utils.attributes import dict_to_typed_dict

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -88,4 +88,19 @@ async def async_set_native_value(self, value: float) -> None:
)
self._state = value
self._last_updated = utcnow()
self.async_write_ha_state()
self.async_write_ha_state()

async def async_added_to_hass(self) -> None:
"""Restore last state."""
await super().async_added_to_hass()

if ((last_state := await self.async_get_last_state()) and
(last_number_data := await self.async_get_last_number_data())
):

self._attributes = dict_to_typed_dict(last_state.attributes)
if last_state.state is not "unknown":
self._state = last_number_data.native_value


_LOGGER.debug(f'Restored OctopusEnergyIntelligentChargeLimit state: {self._state}')
1 change: 0 additions & 1 deletion custom_components/octopus_energy/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
CONFIG_MAIN_LIVE_GAS_CONSUMPTION_REFRESH_IN_MINUTES,
CONFIG_MAIN_PREVIOUS_ELECTRICITY_CONSUMPTION_DAYS_OFFSET,
CONFIG_MAIN_PREVIOUS_GAS_CONSUMPTION_DAYS_OFFSET,
DATA_ACCOUNT_ID,
DOMAIN,

CONFIG_MAIN_SUPPORTS_LIVE_CONSUMPTION,
Expand Down

0 comments on commit 8ed5496

Please sign in to comment.