Fix Opower utilities that have different ReadResolution than previously assumed (#97823)

This commit is contained in:
tronikos 2023-08-11 04:47:49 -07:00 committed by GitHub
parent 97f3199d6d
commit fe794e2be3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,6 +12,7 @@ from opower import (
InvalidAuth, InvalidAuth,
MeterType, MeterType,
Opower, Opower,
ReadResolution,
) )
from homeassistant.components.recorder import get_instance from homeassistant.components.recorder import get_instance
@ -177,44 +178,55 @@ class OpowerCoordinator(DataUpdateCoordinator[dict[str, Forecast]]):
"""Get all cost reads since account activation but at different resolutions depending on age. """Get all cost reads since account activation but at different resolutions depending on age.
- month resolution for all years (since account activation) - month resolution for all years (since account activation)
- day resolution for past 3 years - day resolution for past 3 years (if account's read resolution supports it)
- hour resolution for past 2 months, only for electricity, not gas - hour resolution for past 2 months (if account's read resolution supports it)
""" """
cost_reads = [] cost_reads = []
start = None start = None
end = datetime.now() - timedelta(days=3 * 365) end = datetime.now()
if account.read_resolution != ReadResolution.BILLING:
end -= timedelta(days=3 * 365)
cost_reads += await self.api.async_get_cost_reads( cost_reads += await self.api.async_get_cost_reads(
account, AggregateType.BILL, start, end account, AggregateType.BILL, start, end
) )
if account.read_resolution == ReadResolution.BILLING:
return cost_reads
start = end if not cost_reads else cost_reads[-1].end_time start = end if not cost_reads else cost_reads[-1].end_time
end = ( end = datetime.now()
datetime.now() - timedelta(days=2 * 30) if account.read_resolution != ReadResolution.DAY:
if account.meter_type == MeterType.ELEC end -= timedelta(days=2 * 30)
else datetime.now()
)
cost_reads += await self.api.async_get_cost_reads( cost_reads += await self.api.async_get_cost_reads(
account, AggregateType.DAY, start, end account, AggregateType.DAY, start, end
) )
if account.meter_type == MeterType.ELEC: if account.read_resolution == ReadResolution.DAY:
start = end if not cost_reads else cost_reads[-1].end_time return cost_reads
end = datetime.now()
cost_reads += await self.api.async_get_cost_reads( start = end if not cost_reads else cost_reads[-1].end_time
account, AggregateType.HOUR, start, end end = datetime.now()
) cost_reads += await self.api.async_get_cost_reads(
account, AggregateType.HOUR, start, end
)
return cost_reads return cost_reads
async def _async_get_recent_cost_reads( async def _async_get_recent_cost_reads(
self, account: Account, last_stat_time: float self, account: Account, last_stat_time: float
) -> list[CostRead]: ) -> list[CostRead]:
"""Get cost reads within the past 30 days to allow corrections in data from utilities. """Get cost reads within the past 30 days to allow corrections in data from utilities."""
if account.read_resolution in [
Hourly for electricity, daily for gas. ReadResolution.HOUR,
""" ReadResolution.HALF_HOUR,
ReadResolution.QUARTER_HOUR,
]:
aggregate_type = AggregateType.HOUR
elif account.read_resolution == ReadResolution.DAY:
aggregate_type = AggregateType.DAY
else:
aggregate_type = AggregateType.BILL
return await self.api.async_get_cost_reads( return await self.api.async_get_cost_reads(
account, account,
AggregateType.HOUR aggregate_type,
if account.meter_type == MeterType.ELEC
else AggregateType.DAY,
datetime.fromtimestamp(last_stat_time) - timedelta(days=30), datetime.fromtimestamp(last_stat_time) - timedelta(days=30),
datetime.now(), datetime.now(),
) )