-
-
Notifications
You must be signed in to change notification settings - Fork 37.4k
Add binary sensor to Nord Pool #169684
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
Open
gjohansson-ST
wants to merge
2
commits into
dev
Choose a base branch
from
gj-20260503-01
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add binary sensor to Nord Pool #169684
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,86 @@ | ||||||
| """Binary sensor platform for Nord Pool integration.""" | ||||||
|
|
||||||
| from collections.abc import Callable | ||||||
| from dataclasses import dataclass | ||||||
|
|
||||||
| from homeassistant.components.binary_sensor import ( | ||||||
| BinarySensorEntity, | ||||||
| BinarySensorEntityDescription, | ||||||
| ) | ||||||
| from homeassistant.components.sensor import EntityCategory | ||||||
| from homeassistant.core import HomeAssistant | ||||||
| from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback | ||||||
|
|
||||||
| from . import NordPoolConfigEntry | ||||||
| from .const import CONF_AREAS | ||||||
| from .coordinator import NordPoolDataUpdateCoordinator | ||||||
| from .entity import NordpoolBaseEntity | ||||||
|
|
||||||
| PARALLEL_UPDATES = 0 | ||||||
|
|
||||||
|
|
||||||
| def get_tomorrow_price_available( | ||||||
| entity: NordpoolPriceBinarySensor, | ||||||
| ) -> bool: | ||||||
| """Return tomorrow price availability. | ||||||
|
|
||||||
| Output: True or False | ||||||
| """ | ||||||
| data = entity.coordinator.get_data_tomorrow() | ||||||
| if data and data.entries and entity.area in data.entries[0].entry: | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
A bit more concise imo |
||||||
| return True | ||||||
| return False | ||||||
|
|
||||||
|
|
||||||
| @dataclass(frozen=True, kw_only=True) | ||||||
| class NordpoolBinarySensorEntityDescription(BinarySensorEntityDescription): | ||||||
| """Describes Nord Pool binary sensor entity.""" | ||||||
|
|
||||||
| value_fn: Callable[[NordpoolPriceBinarySensor], bool | None] | ||||||
|
|
||||||
|
|
||||||
| BINARY_SENSOR_TYPES: tuple[NordpoolBinarySensorEntityDescription, ...] = ( | ||||||
| NordpoolBinarySensorEntityDescription( | ||||||
| key="tomorrow_price_available", | ||||||
| translation_key="tomorrow_price_available", | ||||||
| value_fn=get_tomorrow_price_available, | ||||||
| entity_category=EntityCategory.DIAGNOSTIC, | ||||||
| ), | ||||||
| ) | ||||||
|
|
||||||
|
|
||||||
| async def async_setup_entry( | ||||||
| hass: HomeAssistant, | ||||||
| entry: NordPoolConfigEntry, | ||||||
| async_add_entities: AddConfigEntryEntitiesCallback, | ||||||
| ) -> None: | ||||||
| """Set up Nord Pool binary sensor platform.""" | ||||||
|
|
||||||
| coordinator = entry.runtime_data | ||||||
| areas = coordinator.config_entry.data[CONF_AREAS] | ||||||
|
|
||||||
| async_add_entities( | ||||||
| NordpoolPriceBinarySensor(coordinator, description, area) | ||||||
| for description in BINARY_SENSOR_TYPES | ||||||
| for area in areas | ||||||
| ) | ||||||
|
|
||||||
|
|
||||||
| class NordpoolPriceBinarySensor(NordpoolBaseEntity, BinarySensorEntity): | ||||||
| """Representation of a Nord Pool binary sensor.""" | ||||||
|
|
||||||
| entity_description: NordpoolBinarySensorEntityDescription | ||||||
|
|
||||||
| def __init__( | ||||||
| self, | ||||||
| coordinator: NordPoolDataUpdateCoordinator, | ||||||
| entity_description: NordpoolBinarySensorEntityDescription, | ||||||
| area: str, | ||||||
| ) -> None: | ||||||
| """Initiate Nord Pool binary sensor.""" | ||||||
| super().__init__(coordinator, entity_description, area) | ||||||
|
|
||||||
| @property | ||||||
| def is_on(self) -> bool | None: | ||||||
| """Return true if the binary sensor is on.""" | ||||||
| return self.entity_description.value_fn(self) | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
201 changes: 201 additions & 0 deletions
201
tests/components/nordpool/snapshots/test_binary_sensor.ambr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,201 @@ | ||
| # serializer version: 1 | ||
| # name: test_binary_sensor_off[load_platforms0][binary_sensor.nord_pool_se3_tomorrow_price_available-entry] | ||
| EntityRegistryEntrySnapshot({ | ||
| 'aliases': list([ | ||
| None, | ||
| ]), | ||
| 'area_id': None, | ||
| 'capabilities': None, | ||
| 'config_entry_id': <ANY>, | ||
| 'config_subentry_id': <ANY>, | ||
| 'device_class': None, | ||
| 'device_id': <ANY>, | ||
| 'disabled_by': None, | ||
| 'domain': 'binary_sensor', | ||
| 'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>, | ||
| 'entity_id': 'binary_sensor.nord_pool_se3_tomorrow_price_available', | ||
| 'has_entity_name': True, | ||
| 'hidden_by': None, | ||
| 'icon': None, | ||
| 'id': <ANY>, | ||
| 'labels': set({ | ||
| }), | ||
| 'name': None, | ||
| 'object_id_base': 'Tomorrow price available', | ||
| 'options': dict({ | ||
| }), | ||
| 'original_device_class': None, | ||
| 'original_icon': None, | ||
| 'original_name': 'Tomorrow price available', | ||
| 'platform': 'nordpool', | ||
| 'previous_unique_id': None, | ||
| 'suggested_object_id': None, | ||
| 'supported_features': 0, | ||
| 'translation_key': 'tomorrow_price_available', | ||
| 'unique_id': 'SE3-tomorrow_price_available', | ||
| 'unit_of_measurement': None, | ||
| }) | ||
| # --- | ||
| # name: test_binary_sensor_off[load_platforms0][binary_sensor.nord_pool_se3_tomorrow_price_available-state] | ||
| StateSnapshot({ | ||
| 'attributes': ReadOnlyDict({ | ||
| 'friendly_name': 'Nord Pool SE3 Tomorrow price available', | ||
| }), | ||
| 'context': <ANY>, | ||
| 'entity_id': 'binary_sensor.nord_pool_se3_tomorrow_price_available', | ||
| 'last_changed': <ANY>, | ||
| 'last_reported': <ANY>, | ||
| 'last_updated': <ANY>, | ||
| 'state': 'off', | ||
| }) | ||
| # --- | ||
| # name: test_binary_sensor_off[load_platforms0][binary_sensor.nord_pool_se4_tomorrow_price_available-entry] | ||
| EntityRegistryEntrySnapshot({ | ||
| 'aliases': list([ | ||
| None, | ||
| ]), | ||
| 'area_id': None, | ||
| 'capabilities': None, | ||
| 'config_entry_id': <ANY>, | ||
| 'config_subentry_id': <ANY>, | ||
| 'device_class': None, | ||
| 'device_id': <ANY>, | ||
| 'disabled_by': None, | ||
| 'domain': 'binary_sensor', | ||
| 'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>, | ||
| 'entity_id': 'binary_sensor.nord_pool_se4_tomorrow_price_available', | ||
| 'has_entity_name': True, | ||
| 'hidden_by': None, | ||
| 'icon': None, | ||
| 'id': <ANY>, | ||
| 'labels': set({ | ||
| }), | ||
| 'name': None, | ||
| 'object_id_base': 'Tomorrow price available', | ||
| 'options': dict({ | ||
| }), | ||
| 'original_device_class': None, | ||
| 'original_icon': None, | ||
| 'original_name': 'Tomorrow price available', | ||
| 'platform': 'nordpool', | ||
| 'previous_unique_id': None, | ||
| 'suggested_object_id': None, | ||
| 'supported_features': 0, | ||
| 'translation_key': 'tomorrow_price_available', | ||
| 'unique_id': 'SE4-tomorrow_price_available', | ||
| 'unit_of_measurement': None, | ||
| }) | ||
| # --- | ||
| # name: test_binary_sensor_off[load_platforms0][binary_sensor.nord_pool_se4_tomorrow_price_available-state] | ||
| StateSnapshot({ | ||
| 'attributes': ReadOnlyDict({ | ||
| 'friendly_name': 'Nord Pool SE4 Tomorrow price available', | ||
| }), | ||
| 'context': <ANY>, | ||
| 'entity_id': 'binary_sensor.nord_pool_se4_tomorrow_price_available', | ||
| 'last_changed': <ANY>, | ||
| 'last_reported': <ANY>, | ||
| 'last_updated': <ANY>, | ||
| 'state': 'off', | ||
| }) | ||
| # --- | ||
| # name: test_binary_sensor_on[load_platforms0][binary_sensor.nord_pool_se3_tomorrow_price_available-entry] | ||
| EntityRegistryEntrySnapshot({ | ||
| 'aliases': list([ | ||
| None, | ||
| ]), | ||
| 'area_id': None, | ||
| 'capabilities': None, | ||
| 'config_entry_id': <ANY>, | ||
| 'config_subentry_id': <ANY>, | ||
| 'device_class': None, | ||
| 'device_id': <ANY>, | ||
| 'disabled_by': None, | ||
| 'domain': 'binary_sensor', | ||
| 'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>, | ||
| 'entity_id': 'binary_sensor.nord_pool_se3_tomorrow_price_available', | ||
| 'has_entity_name': True, | ||
| 'hidden_by': None, | ||
| 'icon': None, | ||
| 'id': <ANY>, | ||
| 'labels': set({ | ||
| }), | ||
| 'name': None, | ||
| 'object_id_base': 'Tomorrow price available', | ||
| 'options': dict({ | ||
| }), | ||
| 'original_device_class': None, | ||
| 'original_icon': None, | ||
| 'original_name': 'Tomorrow price available', | ||
| 'platform': 'nordpool', | ||
| 'previous_unique_id': None, | ||
| 'suggested_object_id': None, | ||
| 'supported_features': 0, | ||
| 'translation_key': 'tomorrow_price_available', | ||
| 'unique_id': 'SE3-tomorrow_price_available', | ||
| 'unit_of_measurement': None, | ||
| }) | ||
| # --- | ||
| # name: test_binary_sensor_on[load_platforms0][binary_sensor.nord_pool_se3_tomorrow_price_available-state] | ||
| StateSnapshot({ | ||
| 'attributes': ReadOnlyDict({ | ||
| 'friendly_name': 'Nord Pool SE3 Tomorrow price available', | ||
| }), | ||
| 'context': <ANY>, | ||
| 'entity_id': 'binary_sensor.nord_pool_se3_tomorrow_price_available', | ||
| 'last_changed': <ANY>, | ||
| 'last_reported': <ANY>, | ||
| 'last_updated': <ANY>, | ||
| 'state': 'on', | ||
| }) | ||
| # --- | ||
| # name: test_binary_sensor_on[load_platforms0][binary_sensor.nord_pool_se4_tomorrow_price_available-entry] | ||
| EntityRegistryEntrySnapshot({ | ||
| 'aliases': list([ | ||
| None, | ||
| ]), | ||
| 'area_id': None, | ||
| 'capabilities': None, | ||
| 'config_entry_id': <ANY>, | ||
| 'config_subentry_id': <ANY>, | ||
| 'device_class': None, | ||
| 'device_id': <ANY>, | ||
| 'disabled_by': None, | ||
| 'domain': 'binary_sensor', | ||
| 'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>, | ||
| 'entity_id': 'binary_sensor.nord_pool_se4_tomorrow_price_available', | ||
| 'has_entity_name': True, | ||
| 'hidden_by': None, | ||
| 'icon': None, | ||
| 'id': <ANY>, | ||
| 'labels': set({ | ||
| }), | ||
| 'name': None, | ||
| 'object_id_base': 'Tomorrow price available', | ||
| 'options': dict({ | ||
| }), | ||
| 'original_device_class': None, | ||
| 'original_icon': None, | ||
| 'original_name': 'Tomorrow price available', | ||
| 'platform': 'nordpool', | ||
| 'previous_unique_id': None, | ||
| 'suggested_object_id': None, | ||
| 'supported_features': 0, | ||
| 'translation_key': 'tomorrow_price_available', | ||
| 'unique_id': 'SE4-tomorrow_price_available', | ||
| 'unit_of_measurement': None, | ||
| }) | ||
| # --- | ||
| # name: test_binary_sensor_on[load_platforms0][binary_sensor.nord_pool_se4_tomorrow_price_available-state] | ||
| StateSnapshot({ | ||
| 'attributes': ReadOnlyDict({ | ||
| 'friendly_name': 'Nord Pool SE4 Tomorrow price available', | ||
| }), | ||
| 'context': <ANY>, | ||
| 'entity_id': 'binary_sensor.nord_pool_se4_tomorrow_price_available', | ||
| 'last_changed': <ANY>, | ||
| 'last_reported': <ANY>, | ||
| 'last_updated': <ANY>, | ||
| 'state': 'on', | ||
| }) | ||
| # --- |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a bool return type that is obvious