-
Notifications
You must be signed in to change notification settings - Fork 185
feat(entity): optional available on nodered/entity #409
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a72593a
feat(websocket): store contrib version from nodered/version
Swamp-Ig a4c4841
feat(websocket): accept optional available on entity and discovery
Swamp-Ig 7f478c4
feat(entity): apply available with presence-available gating
Swamp-Ig 7e6fbf2
test: availability websocket integration coverage
Swamp-Ig 4473c9a
Merge branch 'main' into feat/entity-available
zachowj 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
| NODERED_DISCOVERY_NEW, | ||
| ) | ||
| from .entity import NodeRedEntity | ||
| from .utils import contrib_supports_presence_available | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
|
|
@@ -71,6 +72,9 @@ def __init__(self, hass: HomeAssistant, config: dict[str, Any]) -> None: | |
| self._attr_native_value = self.convert_state(config.get(CONF_STATE)) | ||
| else: | ||
| self._attr_native_value = None | ||
| # No reading yet: unavailable once presence-available applies | ||
| if contrib_supports_presence_available(hass): | ||
| self._attr_available = False | ||
|
Comment on lines
73
to
+77
Contributor
Author
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. See below. |
||
| self._attr_native_unit_of_measurement = self._config.get( | ||
| CONF_UNIT_OF_MEASUREMENT | ||
| ) | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,13 @@ | ||
| """Version info for Node-RED integration.""" | ||
| """Version info for Node-RED integration. | ||
|
|
||
| __version__ = "4.2.3" | ||
| Single source of truth is ``manifest.json`` (HACS + release-please). | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| from pathlib import Path | ||
|
|
||
| __version__: str = json.loads( | ||
| Path(__file__).with_name("manifest.json").read_text(encoding="utf-8") | ||
| )["version"] |
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 |
|---|---|---|
|
|
@@ -51,8 +51,10 @@ | |
|
|
||
| from .const import ( | ||
| CONF_ATTRIBUTES, | ||
| CONF_AVAILABLE, | ||
| CONF_COMPONENT, | ||
| CONF_CONFIG, | ||
| CONF_CONTRIB_VERSION, | ||
| CONF_DEVICE_INFO, | ||
| CONF_DEVICE_TRIGGER, | ||
| CONF_NODE_ID, | ||
|
|
@@ -192,6 +194,7 @@ async def websocket_device_remove( | |
| vol.Optional(CONF_CONFIG, default={}): dict, | ||
| vol.Optional(CONF_STATE): vol.Any(bool, str, int, float, None), | ||
| vol.Optional(CONF_ATTRIBUTES): dict, | ||
| vol.Optional(CONF_AVAILABLE): bool, | ||
| vol.Optional(CONF_REMOVE): bool, | ||
| vol.Optional(CONF_DEVICE_INFO): dict, | ||
| vol.Optional(CONF_DEVICE_TRIGGER): TRIGGER_SCHEMA, | ||
|
|
@@ -214,8 +217,9 @@ def websocket_discovery( | |
| vol.Required(CONF_TYPE): "nodered/entity", | ||
| vol.Required(CONF_SERVER_ID): cv.string, | ||
| vol.Required(CONF_NODE_ID): cv.string, | ||
| vol.Required(CONF_STATE): vol.Any(bool, str, int, float, None), | ||
| vol.Optional(CONF_ATTRIBUTES, default={}): dict, | ||
| vol.Optional(CONF_STATE): vol.Any(bool, str, int, float, None), | ||
| vol.Optional(CONF_ATTRIBUTES): dict, | ||
| vol.Optional(CONF_AVAILABLE): bool, | ||
|
Comment on lines
+220
to
+222
Contributor
Author
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. The non-sensor entities can't have this yet, they haven't been updated with the new code. |
||
| } | ||
| ) | ||
| def websocket_entity( | ||
|
|
@@ -247,12 +251,48 @@ def websocket_config_update( | |
| connection.send_message(result_message(msg[CONF_ID])) | ||
|
|
||
|
|
||
| def _store_contrib_version(hass: HomeAssistant, contrib_version: str | None) -> None: | ||
| """Persist or clear contrib package version on the Node-RED config entry.""" | ||
| entries = hass.config_entries.async_entries(DOMAIN) | ||
| if not entries: | ||
| return | ||
| entry = entries[0] | ||
| new_data = dict(entry.data) | ||
| if contrib_version: | ||
| new_data[CONF_CONTRIB_VERSION] = contrib_version | ||
| else: | ||
| new_data.pop(CONF_CONTRIB_VERSION, None) | ||
| if new_data != entry.data: | ||
| hass.config_entries.async_update_entry(entry, data=new_data) | ||
|
|
||
|
|
||
| @require_admin | ||
| @websocket_command({vol.Required(CONF_TYPE): "nodered/version"}) | ||
| @websocket_command( | ||
| { | ||
| vol.Required(CONF_TYPE): "nodered/version", | ||
| vol.Optional(CONF_CONTRIB_VERSION): cv.string, | ||
| } | ||
| ) | ||
| def websocket_version( | ||
| hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any] | ||
| ) -> None: | ||
| """Version command.""" | ||
| """Version command. | ||
|
|
||
| Optional ``contrib_version`` is stored on the config entry. Only update when | ||
| the key is present so a version probe without it does not clear a prior | ||
| announce. Empty string clears. On announce, register a disconnect callback | ||
| so reconnect from a contrib that does not announce clears the stored value. | ||
| """ | ||
| if CONF_CONTRIB_VERSION in msg: | ||
|
|
||
| def clear_contrib_version() -> None: | ||
| _store_contrib_version(hass, None) | ||
|
|
||
| contrib_version = msg[CONF_CONTRIB_VERSION] | ||
| _store_contrib_version(hass, contrib_version or None) | ||
| if contrib_version: | ||
| connection.subscriptions[msg[CONF_ID]] = clear_contrib_version | ||
|
|
||
| connection.send_message(result_message(msg[CONF_ID], VERSION)) | ||
|
|
||
|
|
||
|
|
||
Oops, something went wrong.
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.
The issue only arises as a bit of an edge-case when "Resend state, available, and attributes" is set on: create entity -> set available (HA state now unknown/available) -> HA restart -> resend happens without state -> HA state now unknown/unavailable.
I agree it's a bit inconsistent, but it's a very edge case. Discovery without state still defaults to unavailable on purpose (avoid available+unknown on first deploy / empty rediscovery). Availability-only updates after the entity exists already honor available and keep the last reading.
I don't think we should change this for this case, but if we were it would be to disallow setting available to false for resend sensors that have never had a value.