Skip to content

Commit

Permalink
Bump to 0.0.8 + misc changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Hellowlol committed Nov 10, 2022
1 parent f295d34 commit 6ac27fc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
25 changes: 15 additions & 10 deletions custom_components/nordpool/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@


NAME = DOMAIN
VERSION = "0.0.7"
VERSION = "0.0.8"
ISSUEURL = "https://github.com/custom-components/nordpool/issues"

STARTUP = f"""
Expand Down Expand Up @@ -96,10 +96,11 @@ async def _someday(self, area: str, currency: str, day: str):
await self.update_today(None)
await self.update_tomorrow(None)

return self._data.get(currency, {}).get(day, {}).get(area)
# Send a new data request after new data is updated for this first run
# This way if the user has multiple sensors they will all update
async_dispatcher_send(self._hass, EVENT_NEW_DATA)

def tomorrow_valid(self) -> bool:
return self._tomorrow_valid
return self._data.get(currency, {}).get(day, {}).get(area)

async def today(self, area: str, currency: str) -> dict:
"""Returns todays prices in a area in the requested currency"""
Expand All @@ -114,10 +115,10 @@ async def tomorrow(self, area: str, currency: str):

async def _dry_setup(hass: HomeAssistant, config: Config) -> bool:
"""Set up using yaml config file."""

if DOMAIN not in hass.data:
api = NordpoolData(hass)
hass.data[DOMAIN] = api
_LOGGER.debug("Added %s to hass.data", DOMAIN)

async def new_day_cb(n):
"""Cb to handle some house keeping when it a new day."""
Expand All @@ -142,7 +143,7 @@ async def new_data_cb(n):
"""Callback to fetch new data for tomorrows prices at 1300ish CET
and notify any sensors, about the new data
"""
_LOGGER.debug("Called new_data_cb")
# _LOGGER.debug("Called new_data_cb")
await api.update_tomorrow(n)
async_dispatcher_send(hass, EVENT_NEW_DATA)

Expand All @@ -165,7 +166,7 @@ async def new_data_cb(n):
api.listeners.append(cb_update_tomorrow)
api.listeners.append(cb_new_hr)
api.listeners.append(cb_new_day)

return True


Expand All @@ -181,7 +182,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.config_entries.async_forward_entry_setup(entry, "sensor")
)

# entry.add_update_listener(async_reload_entry)
entry.add_update_listener(async_reload_entry)
return res


Expand All @@ -190,8 +191,12 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
unload_ok = await hass.config_entries.async_forward_entry_unload(entry, "sensor")

if unload_ok:
for unsub in hass.data[DOMAIN].listeners:
unsub()
# This is a issue if you have mulitple sensors as everything related to DOMAIN
# is removed, regardless if you have mulitple sensors or not. Don't seem to
# create a big issue for now #TODO
if DOMAIN in hass.data:
for unsub in hass.data[DOMAIN].listeners:
unsub()
hass.data.pop(DOMAIN)

return True
Expand Down
4 changes: 2 additions & 2 deletions custom_components/nordpool/aio_price.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def join_result_for_correct_time(results, dt):
"""
# utc = datetime.utcnow()
fin = defaultdict(dict)
_LOGGER.debug("join_result_for_correct_time %s", dt)
# _LOGGER.debug("join_result_for_correct_time %s", dt)
utc = dt

for day_ in results:
Expand Down Expand Up @@ -134,7 +134,7 @@ def join_result_for_correct_time(results, dt):
if start_of_day <= local and local <= end_of_day:
fin["areas"][key]["values"].append(val)

_LOGGER.debug("Combines result: %s", fin)
# _LOGGER.debug("Combines result: %s", fin)

return fin

Expand Down
19 changes: 8 additions & 11 deletions custom_components/nordpool/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def inner(*args, **kwargs):
template_value = self._ad_template.async_render(
now=faker(), current_price=price
)
_LOGGER.debug("Template value are %s", template_value)
# _LOGGER.debug("Template value are %s", template_value)
price += template_value

# Convert price to cents if specified by the user.
Expand Down Expand Up @@ -378,11 +378,11 @@ def extra_state_attributes(self) -> dict:
"currency": self._currency,
"country": _REGIONS[self._area][1],
"region": self._area,
"low price": self.low_price,
"low_price": self.low_price,
"price_percent_to_average": self.price_percent_to_average,
"tomorrow_valid": self.tomorrow_valid,
"today": self.today,
"tomorrow": self.tomorrow,
"tomorrow_valid": self.tomorrow_valid,
"raw_today": self.raw_today,
"raw_tomorrow": self.raw_tomorrow,
}
Expand All @@ -408,7 +408,8 @@ def raw_tomorrow(self):

@property
def tomorrow_valid(self):
return self._api.tomorrow_valid()
# this should be checked a better way
return len(self.tomorrow) >= 23

async def _update_current_price(self) -> None:
"""update the current price (price this hour)"""
Expand All @@ -428,19 +429,19 @@ async def _update_current_price(self) -> None:

async def check_stuff(self) -> None:
"""Cb to do some house keeping, called every hour to get the current hours price"""
_LOGGER.debug("called check_stuff")
# _LOGGER.debug("called check_stuff")
if self._last_tick is None:
self._last_tick = dt_utils.now()

if self._data_today is None:
_LOGGER.debug("NordpoolSensor _data_today is none, trying to fetch it.")
_LOGGER.debug("NordpoolSensor _data_today is none, trying to fetch it. %s", self.name)
today = await self._api.today(self._area, self._currency)
if today:
self._data_today = today
self._update(today)

if self._data_tomorrow is None:
_LOGGER.debug("NordpoolSensor _data_tomorrow is none, trying to fetch it.")
_LOGGER.debug("NordpoolSensor _data_tomorrow is none, trying to fetch it. %s", self.name)
tomorrow = await self._api.tomorrow(self._area, self._currency)
if tomorrow:
self._data_tomorrow = tomorrow
Expand Down Expand Up @@ -478,7 +479,3 @@ async def async_added_to_hass(self):

await self.check_stuff()

# async def async_will_remove_from_hass(self):
# """This needs some testing.."""
# for cb in self._cbs:
# self._api._hass.bus._async_remove_listener(EVENT_TIME_CHANGED, cb)

0 comments on commit 6ac27fc

Please sign in to comment.