From 0448bd4c7d2355bc087430f6198192f3ea80f585 Mon Sep 17 00:00:00 2001 From: Hellowlol Date: Tue, 3 Jan 2023 21:14:28 +0100 Subject: [PATCH 1/4] Misc fixes. bump --- custom_components/nordpool/__init__.py | 18 ++++---- custom_components/nordpool/manifest.json | 2 +- custom_components/nordpool/sensor.py | 53 ++++++++++++------------ 3 files changed, 37 insertions(+), 36 deletions(-) diff --git a/custom_components/nordpool/__init__.py b/custom_components/nordpool/__init__.py index ecd75c0..8cf7026 100644 --- a/custom_components/nordpool/__init__.py +++ b/custom_components/nordpool/__init__.py @@ -28,7 +28,7 @@ NAME = DOMAIN -VERSION = "0.0.10b0" +VERSION = "0.0.11" ISSUEURL = "https://github.com/custom-components/nordpool/issues" STARTUP = f""" @@ -74,11 +74,13 @@ async def _update(self, type_="today", dt=None): _LOGGER.info("Some crap happend, retrying request later.") async_call_later(hass, 20, partial(self._update, type_=type_, dt=dt)) - async def update_today(self, n: datetime): + async def update_today(self, _: datetime): + """Update todays prices""" _LOGGER.debug("Updating tomorrows prices.") await self._update("today") - async def update_tomorrow(self, n: datetime): + async def update_tomorrow(self, _: datetime): + """Update tomorrows prices.""" _LOGGER.debug("Updating tomorrows prices.") await self._update(type_="tomorrow", dt=dt_utils.now() + timedelta(hours=24)) self._tomorrow_valid = True @@ -115,14 +117,14 @@ async def tomorrow(self, area: str, currency: str): return res -async def _dry_setup(hass: HomeAssistant, config: Config) -> bool: +async def _dry_setup(hass: HomeAssistant, _: 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): + async def new_day_cb(_): """Cb to handle some house keeping when it a new day.""" _LOGGER.debug("Called new_day_cb callback") api._tomorrow_valid = False @@ -136,17 +138,17 @@ async def new_day_cb(n): async_dispatcher_send(hass, EVENT_NEW_DATA) - async def new_hr(n): + async def new_hr(_): """Callback to tell the sensors to update on a new hour.""" _LOGGER.debug("Called new_hr callback") async_dispatcher_send(hass, EVENT_NEW_DATA) - async def new_data_cb(n): + async def new_data_cb(tdo): """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") - await api.update_tomorrow(n) + await api.update_tomorrow(tdo) async_dispatcher_send(hass, EVENT_NEW_DATA) # Handles futures updates diff --git a/custom_components/nordpool/manifest.json b/custom_components/nordpool/manifest.json index 722b5e3..6a27374 100644 --- a/custom_components/nordpool/manifest.json +++ b/custom_components/nordpool/manifest.json @@ -15,5 +15,5 @@ "requirements": [ "nordpool>=0.2" ], - "version": "0.0.10b0" + "version": "0.0.11" } \ No newline at end of file diff --git a/custom_components/nordpool/sensor.py b/custom_components/nordpool/sensor.py index f84ca49..38194db 100644 --- a/custom_components/nordpool/sensor.py +++ b/custom_components/nordpool/sensor.py @@ -21,7 +21,7 @@ from jinja2 import pass_context from . import DOMAIN, EVENT_NEW_DATA -from .misc import extract_attrs, has_junk, is_new, start_of +from .misc import is_new, start_of _LOGGER = logging.getLogger(__name__) @@ -148,8 +148,6 @@ def __init__( ad_template, hass, ) -> None: - # friendly_name is ignored as it never worked. - # rename the sensor in the ui if you dont like the name. self._area = area self._currency = currency or _REGIONS[area][0] self._price_type = price_type @@ -199,7 +197,6 @@ def __init__( # To control the updates. self._last_tick = None - self._cbs = [] @property def name(self) -> str: @@ -220,7 +217,7 @@ def unit(self) -> str: return self._price_type @property - def unit_of_measurement(self) -> str: + def unit_of_measurement(self) -> str: # FIXME """Return the unit of measurement this sensor expresses itself in.""" _currency = self._currency if self._use_cents is True: @@ -249,12 +246,9 @@ def device_info(self): "manufacturer": DOMAIN, } - @property - def state(self) -> float: - return self.current_price - @property def additional_costs(self): + """Additional costs.""" return self._additional_costs_value @property @@ -272,7 +266,8 @@ def price_percent_to_average(self) -> float: """Price in percent to average price""" return ( self.current_price / self._average - if self.current_price and self._average + if isinstance(self.current_price, (int, float)) + and isinstance(self._average, (float, int)) else None ) @@ -286,7 +281,7 @@ def _calc_price(self, value=None, fake_dt=None) -> float: return None def faker(): - def inner(*args, **kwargs): + def inner(*_, **__): return fake_dt or dt_utils.now() return pass_context(inner) @@ -333,19 +328,19 @@ def inner(*args, **kwargs): def _update(self): """Set attrs""" - td = self.today + today = self.today - if not td: + if not today: _LOGGER.debug("No data for today, unable to set attrs") return - self._average = mean(td) - self._min = min(td) - self._max = max(td) - self._off_peak_1 = mean(td[0:8]) - self._off_peak_2 = mean(td[20:]) - self._peak = mean(td[8:20]) - self._mean = median(td) + self._average = mean(today) + self._min = min(today) + self._max = max(today) + self._off_peak_1 = mean(today[0:8]) + self._off_peak_2 = mean(today[20:]) + self._peak = mean(today[8:20]) + self._mean = median(today) @property def current_price(self) -> float: @@ -403,7 +398,6 @@ def tomorrow(self) -> list: @property def extra_state_attributes(self) -> dict: return { - "current_price": self.current_price, "average": self._average, "off_peak_1": self._off_peak_1, "off_peak_2": self._off_peak_2, @@ -422,10 +416,12 @@ def extra_state_attributes(self) -> dict: "tomorrow_valid": self.tomorrow_valid, "raw_today": self.raw_today, "raw_tomorrow": self.raw_tomorrow, + "current_price": self.current_price, "additional_costs_current_hour": self.additional_costs, } def _add_raw(self, data) -> list: + """Helper""" result = [] for res in self._someday(data): item = { @@ -438,10 +434,12 @@ def _add_raw(self, data) -> list: @property def raw_today(self) -> list: + """Raw today""" return self._add_raw(self._data_today) @property def raw_tomorrow(self) -> list: + """Raw tomorrow""" return self._add_raw(self._data_tomorrow) @property @@ -458,7 +456,6 @@ async def _update_current_price(self) -> None: if data: for item in self._someday(data): if item["start"] == start_of(local_now, "hour"): - # _LOGGER.info("start %s local_now %s", item["start"], start_of(local_now, "hour")) self._current_price = item["value"] _LOGGER.debug( "Updated %s _current_price %s", self.name, item["value"] @@ -500,23 +497,25 @@ async def check_stuff(self) -> None: # Just to stop the hourly update if its a new day if dt_utils.now().hour != 0: self._update() - self._data_tomorrow = None else: today = await self._api.today(self._area, self._currency) if today: self._data_today = today - # self._update(today) if dt_utils.now().hour != 0: self._update() - self._data_tomorrow = None - # Updates the current for this hour. - await self._update_current_price() + self._data_tomorrow = None + _LOGGER.debug("Cleared self._data_tomorrow = %s", self._data_tomorrow) tomorrow = await self._api.tomorrow(self._area, self._currency) if tomorrow: self._data_tomorrow = tomorrow + # Updates the current for this hour. + await self._update_current_price() + # This is not to make sure the correct template costs are set. Issue 258 + self._attr_native_value = self.current_price + self._last_tick = dt_utils.now() self.async_write_ha_state() From 545592ff2ea152427831decc4630c483a5619765 Mon Sep 17 00:00:00 2001 From: Hellowlol Date: Tue, 3 Jan 2023 21:17:52 +0100 Subject: [PATCH 2/4] Fix name --- custom_components/nordpool/manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/nordpool/manifest.json b/custom_components/nordpool/manifest.json index 6a27374..ff7edd5 100644 --- a/custom_components/nordpool/manifest.json +++ b/custom_components/nordpool/manifest.json @@ -1,6 +1,6 @@ { "domain": "nordpool", - "name": "nordpool", + "name": "Nord Pool", "documentation": "https://github.com/custom-components/nordpool/", "issue_tracker": "https://github.com/custom-components/nordpool/issues", "dependencies": [], From 94c24788aaecc8d9baf318ad0a881284837394d9 Mon Sep 17 00:00:00 2001 From: Hellowlol Date: Tue, 3 Jan 2023 21:25:38 +0100 Subject: [PATCH 3/4] Update et.json --- custom_components/nordpool/translations/et.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/custom_components/nordpool/translations/et.json b/custom_components/nordpool/translations/et.json index 46328fa..4d1e2cc 100644 --- a/custom_components/nordpool/translations/et.json +++ b/custom_components/nordpool/translations/et.json @@ -15,11 +15,11 @@ "price_in_cents": "Hind sentides", "price_type": "Hinna vorming", "additional_costs": "Lisanduvate hindade mall" - } + } } }, "error": { - "name_exists": "See nimi on juba kasutusel" + "name_exists": "See nimi on juba kasutusel", } } -} +} \ No newline at end of file From 434a81ffaca38668549fa7717b056ade00367fc3 Mon Sep 17 00:00:00 2001 From: Hellowlol Date: Tue, 3 Jan 2023 21:51:16 +0100 Subject: [PATCH 4/4] Debump --- custom_components/nordpool/__init__.py | 2 +- custom_components/nordpool/manifest.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/custom_components/nordpool/__init__.py b/custom_components/nordpool/__init__.py index 8cf7026..7151a67 100644 --- a/custom_components/nordpool/__init__.py +++ b/custom_components/nordpool/__init__.py @@ -28,7 +28,7 @@ NAME = DOMAIN -VERSION = "0.0.11" +VERSION = "0.0.10b2" ISSUEURL = "https://github.com/custom-components/nordpool/issues" STARTUP = f""" diff --git a/custom_components/nordpool/manifest.json b/custom_components/nordpool/manifest.json index ff7edd5..7bbdecc 100644 --- a/custom_components/nordpool/manifest.json +++ b/custom_components/nordpool/manifest.json @@ -15,5 +15,5 @@ "requirements": [ "nordpool>=0.2" ], - "version": "0.0.11" + "version": "0.0.10b2" } \ No newline at end of file