diff --git a/custom_components/nordpool/__init__.py b/custom_components/nordpool/__init__.py index 694ca9a..37d3141 100644 --- a/custom_components/nordpool/__init__.py +++ b/custom_components/nordpool/__init__.py @@ -28,7 +28,7 @@ NAME = DOMAIN -VERSION = "0.0.7" +VERSION = "0.0.8" ISSUEURL = "https://github.com/custom-components/nordpool/issues" STARTUP = f""" @@ -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""" @@ -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.""" @@ -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) @@ -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 @@ -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 @@ -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 diff --git a/custom_components/nordpool/aio_price.py b/custom_components/nordpool/aio_price.py index f79e4f5..24f116b 100644 --- a/custom_components/nordpool/aio_price.py +++ b/custom_components/nordpool/aio_price.py @@ -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: @@ -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 diff --git a/custom_components/nordpool/sensor.py b/custom_components/nordpool/sensor.py index 8307cfb..5420ed8 100644 --- a/custom_components/nordpool/sensor.py +++ b/custom_components/nordpool/sensor.py @@ -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. @@ -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, } @@ -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)""" @@ -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 @@ -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)