Skip to content

Commit

Permalink
Merge pull request #262 from custom-components/fix-258
Browse files Browse the repository at this point in the history
Misc fixes. bump
  • Loading branch information
Hellowlol authored Jan 3, 2023
2 parents a1c4773 + 434a81f commit 25d8224
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 40 deletions.
18 changes: 10 additions & 8 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.10b0"
VERSION = "0.0.10b2"
ISSUEURL = "https://github.com/custom-components/nordpool/issues"

STARTUP = f"""
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions custom_components/nordpool/manifest.json
Original file line number Diff line number Diff line change
@@ -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": [],
Expand All @@ -15,5 +15,5 @@
"requirements": [
"nordpool>=0.2"
],
"version": "0.0.10b0"
"version": "0.0.10b2"
}
53 changes: 26 additions & 27 deletions custom_components/nordpool/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -199,7 +197,6 @@ def __init__(

# To control the updates.
self._last_tick = None
self._cbs = []

@property
def name(self) -> str:
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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
)

Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand All @@ -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 = {
Expand All @@ -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
Expand All @@ -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"]
Expand Down Expand Up @@ -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()

Expand Down
7 changes: 4 additions & 3 deletions custom_components/nordpool/translations/et.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
"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",
"invalid_template": "See mall on vigane, vaata https://github.com/custom-components/nordpool"

}
}
}
}

0 comments on commit 25d8224

Please sign in to comment.