Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass areas down to aio_price.py to avoid errors unrelated to selected area #341

Merged
merged 2 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions custom_components/nordpool/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,34 +55,41 @@ def __init__(self, hass: HomeAssistant):
self._data = defaultdict(dict)
self.currency = []
self.listeners = []
self.areas = []

async def _update(self, type_="today", dt=None):
_LOGGER.debug("calling _update %s %s", type_, dt)
async def _update(self, type_="today", dt=None, areas=None):
_LOGGER.debug("calling _update %s %s %s", type_, dt, areas)
hass = self._hass
client = async_get_clientsession(hass)

if dt is None:
dt = dt_utils.now()

if areas is not None:
self.areas += [area for area in areas if area not in self.areas]
# We dont really need today and morrow
# when the region is in another timezone
# as we request data for 3 days anyway.
# Keeping this for now, but this should be changed.
for currency in self.currency:
spot = AioPrices(currency, client)
data = await spot.hourly(end_date=dt)
data = await spot.hourly(end_date=dt, areas=self.areas if len(self.areas) > 0 else None)
if data:
self._data[currency][type_] = data["areas"]

async def update_today(self):
async def update_today(self, areas=None):
"""Update today's prices"""
_LOGGER.debug("Updating today's prices.")
await self._update("today")
if areas is not None:
self.areas += [area for area in areas if area not in self.areas]
await self._update("today", areas=self.areas if len(self.areas) > 0 else None)

async def update_tomorrow(self):
async def update_tomorrow(self, areas=None):
"""Update tomorrows prices."""
_LOGGER.debug("Updating tomorrows prices.")
await self._update(type_="tomorrow", dt=dt_utils.now() + timedelta(hours=24))
if areas is not None:
self.areas += [area for area in areas if area not in self.areas]
await self._update(type_="tomorrow", dt=dt_utils.now() + timedelta(hours=24), areas=self.areas if len(self.areas) > 0 else None)

async def _someday(self, area: str, currency: str, day: str):
"""Returns today's or tomorrow's prices in an area in the currency"""
Expand All @@ -92,13 +99,18 @@ async def _someday(self, area: str, currency: str, day: str):
% (currency, ", ".join(_CURRENCY_LIST))
)

if area not in self.areas:
self.areas.append(area);
# This is needed as the currency is
# set in the sensor.
if currency not in self.currency:
self.currency.append(currency)
await self.update_today()
try:
await self.update_tomorrow()
await self.update_today(areas=self.areas)
except InvalidValueException:
_LOGGER.debug("No data available for today, retrying later")
try:
await self.update_tomorrow(areas=self.areas)
except InvalidValueException:
_LOGGER.debug("No data available for tomorrow, retrying later")

Expand All @@ -118,7 +130,7 @@ async def tomorrow(self, area: str, currency: str):
return await self._someday(area, currency, "tomorrow")


async def _dry_setup(hass: HomeAssistant, _: Config) -> bool:
async def _dry_setup(hass: HomeAssistant, config: Config) -> bool:
"""Set up using yaml config file."""
if DOMAIN not in hass.data:
api = NordpoolData(hass)
Expand Down
2 changes: 1 addition & 1 deletion custom_components/nordpool/aio_price.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def join_result_for_correct_time(results, dt):
local_end = val["end"].astimezone(zone)
if start_of_day <= local and local <= end_of_day:
if val['value'] in INVALID_VALUES:
raise InvalidValueException()
raise InvalidValueException(f"Invalid value in {val} for area '{key}'")
if local == local_end:
_LOGGER.info(
"Hour has the same start and end, most likly due to dst change %s exluded this hour",
Expand Down