Skip to content

Commit

Permalink
Improve error handling, fixes #23 . Update entso-e default sensor name.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpulakka committed Oct 31, 2022
1 parent 04f035e commit f58ee31
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,15 @@ Install and configure https://github.com/JaccoR/hass-entso-e and/or https://gith
- platform: nordpool_diff
```
The default setup assumes that hass-entso-e provides `sensor.current_price` entity,
The default setup assumes that hass-entso-e provides `sensor.current_electricity_market_pricee` entity,
which it does, if you left optional "Name" empty when configuring hass-entso-e.
Except that it maybe doesn't anymore, https://github.com/JaccoR/hass-entso-e/issues/49 , the default seems to be somewhat volatile, to be updated...

Explicit `entsoe_entity` and/or `nordpool_entity` IDs can also be specified:

```yaml
sensor:
- platform: nordpool_diff
entsoe_entity: sensor.current_price
entsoe_entity: sensor.current_electricity_market_price
nordpool_entity: sensor.nordpool_kwh_fi_eur_3_095_024
```

Expand Down
2 changes: 1 addition & 1 deletion custom_components/nordpool_diff/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"dependencies": [],
"codeowners": ["@jpulakka"],
"iot_class": "calculated",
"version": "0.2.0"
"version": "0.2.1"
}
20 changes: 13 additions & 7 deletions custom_components/nordpool_diff/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
# https://github.com/home-assistant/core/blob/dev/homeassistant/helpers/config_validation.py
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(NORDPOOL_ENTITY, default=""): cv.string, # Is there a way to require EITHER nordpool OR entsoe being valid cv.entity_id?
vol.Optional(ENTSOE_ENTITY, default="sensor.current_price"): cv.string, # hass-entso-e's default entity id
vol.Optional(ENTSOE_ENTITY, default="sensor.current_electricity_market_price"): cv.string, # hass-entso-e's default entity id
vol.Optional(FILTER_LENGTH, default=10): vol.All(vol.Coerce(int), vol.Range(min=2, max=20)),
vol.Optional(FILTER_TYPE, default=TRIANGLE): vol.In([RECTANGLE, TRIANGLE, INTERVAL, RANK]),
vol.Optional(NORMALIZE, default=NO): vol.In([NO, MAX, MAX_MIN]),
Expand Down Expand Up @@ -145,14 +145,20 @@ def _get_next_n_hours(self, n):
prices = []
# Prefer entsoe:
if e := self.hass.states.get(self._entsoe_entity_id):
prices = _get_next_n_hours_from_entsoe(n, e)
_LOGGER.debug(f"{n} prices from entsoe {prices}")
try:
prices = _get_next_n_hours_from_entsoe(n, e)
_LOGGER.debug(f"{n} prices from entsoe {prices}")
except:
_LOGGER.exception("_get_next_n_hours_from_entsoe")
# Fall back to nordpool:
if (len(prices) < n) and (np := self.hass.states.get(self._nordpool_entity_id)):
np_prices = _get_next_n_hours_from_nordpool(n, np)
_LOGGER.debug(f"{n} prices from nordpool {np_prices}")
if len(np_prices) > len(prices):
prices = np_prices
try:
np_prices = _get_next_n_hours_from_nordpool(n, np)
_LOGGER.debug(f"{n} prices from nordpool {np_prices}")
if len(np_prices) > len(prices):
prices = np_prices
except:
_LOGGER.exception("_get_next_n_hours_from_nordpool")
# Fail gracefully if nothing works:
if not prices:
return n * [0]
Expand Down

0 comments on commit f58ee31

Please sign in to comment.