Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to define unique_id in yaml #416

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ mv nordpool-X.Y.Z/custom_components/nordpool/* .
| Price in cents | no | *Default: false* <br> Display price in cents in stead of (for example) Euros.|
| Energy scale | no | *Default: kWh* <br> Price displayed for MWh, kWh or Wh.|
| Additional Cost | no | *default `{{0.0\|float}}`* <br> Template to specify additional cost to be added. See [Additional Costs](#additional-costs) for more details.|
| Entity Id | no | *Default: Generated automatically based on scale, region, currency, precision, low price precentage and vat. |

### Option 1: UI
- Go to `Settings` -> `Devices & Services`
Expand Down Expand Up @@ -108,6 +109,11 @@ sensor:
# The template price is in EUR, DKK, NOK or SEK (not in cents).
# For example: "{{ current_price * 0.19 + 0.023 | float}}"
additional_costs: "{{0.0|float}}"

# Can be used to override default generated entity id.
# Enter only part after "sensor."
# Lower case and no spaces
unique_id: nordpool_custom
```
### Regions
See the [Nord Pool region map](https://www.nordpoolgroup.com/en/maps/) for details
Expand Down
8 changes: 8 additions & 0 deletions custom_components/nordpool/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
vol.Optional("price_type", default="kWh"): vol.In(list(_PRICE_IN.keys())),
vol.Optional("price_in_cents", default=False): cv.boolean,
vol.Optional("additional_costs", default=DEFAULT_TEMPLATE): cv.template,
vol.Optional("unique_id"): cv.string,
}
)

Expand All @@ -101,6 +102,7 @@ def _dry_setup(hass, config, add_devices, discovery_info=None):
use_cents = config.get("price_in_cents")
ad_template = config.get("additional_costs")
api = hass.data[DOMAIN]
unique_id = config.get("unique_id")
sensor = NordpoolSensor(
friendly_name,
region,
Expand All @@ -113,6 +115,7 @@ def _dry_setup(hass, config, add_devices, discovery_info=None):
api,
ad_template,
hass,
unique_id,
)

add_devices([sensor])
Expand Down Expand Up @@ -148,6 +151,7 @@ def __init__(
api,
ad_template,
hass,
unique_id=None,
) -> None:
self._area = area
self._currency = currency or _REGIONS[area][0]
Expand All @@ -161,6 +165,7 @@ def __init__(
self._ad_template = ad_template
self._hass = hass
self._attr_force_update = True
self._user_unique_id = unique_id

if vat is True:
self._vat = _REGIONS[area][2]
Expand Down Expand Up @@ -228,6 +233,9 @@ def unit_of_measurement(self) -> str: # FIXME

@property
def unique_id(self):
if self._user_unique_id:
return self._user_unique_id

name = "nordpool_%s_%s_%s_%s_%s_%s" % (
self._price_type,
self._area,
Expand Down