-
-
Notifications
You must be signed in to change notification settings - Fork 37.5k
Add mysensors text platform #84667
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
Add mysensors text platform #84667
Changes from all commits
Commits
Show all changes
4 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
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 |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| """Provide a text platform for MySensors.""" | ||
| from __future__ import annotations | ||
|
|
||
| from homeassistant.components.text import TextEntity | ||
| from homeassistant.config_entries import ConfigEntry | ||
| from homeassistant.const import Platform | ||
| from homeassistant.core import HomeAssistant, callback | ||
| from homeassistant.helpers.dispatcher import async_dispatcher_connect | ||
| from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
|
|
||
| from .. import mysensors | ||
| from .const import MYSENSORS_DISCOVERY, DiscoveryInfo | ||
| from .device import MySensorsEntity | ||
| from .helpers import on_unload | ||
|
|
||
|
|
||
| async def async_setup_entry( | ||
| hass: HomeAssistant, | ||
| config_entry: ConfigEntry, | ||
| async_add_entities: AddEntitiesCallback, | ||
| ) -> None: | ||
| """Set up this platform for a specific ConfigEntry(==Gateway).""" | ||
|
|
||
| @callback | ||
| def async_discover(discovery_info: DiscoveryInfo) -> None: | ||
| """Discover and add a MySensors text entity.""" | ||
| mysensors.setup_mysensors_platform( | ||
| hass, | ||
| Platform.TEXT, | ||
| discovery_info, | ||
| MySensorsText, | ||
| async_add_entities=async_add_entities, | ||
| ) | ||
|
|
||
| on_unload( | ||
| hass, | ||
| config_entry.entry_id, | ||
| async_dispatcher_connect( | ||
| hass, | ||
| MYSENSORS_DISCOVERY.format(config_entry.entry_id, Platform.TEXT), | ||
| async_discover, | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| class MySensorsText(MySensorsEntity, TextEntity): | ||
| """Representation of the value of a MySensors Text child node.""" | ||
|
|
||
| _attr_native_max = 25 | ||
|
|
||
| @property | ||
| def native_value(self) -> str | None: | ||
| """Return the value reported by the text.""" | ||
| return self._values.get(self.value_type) | ||
|
|
||
| async def async_set_value(self, value: str) -> None: | ||
| """Change the value.""" | ||
| self.gateway.set_child_value( | ||
| self.node_id, self.child_id, self.value_type, value, ack=1 | ||
| ) | ||
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 |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| """Provide tests for mysensors text platform.""" | ||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Callable | ||
| from unittest.mock import MagicMock, call | ||
|
|
||
| from mysensors.sensor import Sensor | ||
| import pytest | ||
|
|
||
| from homeassistant.components.text import ( | ||
| ATTR_VALUE, | ||
| DOMAIN as TEXT_DOMAIN, | ||
| SERVICE_SET_VALUE, | ||
| ) | ||
| from homeassistant.const import ATTR_ENTITY_ID | ||
| from homeassistant.core import HomeAssistant | ||
|
|
||
|
|
||
| async def test_text_node( | ||
| hass: HomeAssistant, | ||
| text_node: Sensor, | ||
| receive_message: Callable[[str], None], | ||
| transport_write: MagicMock, | ||
| ) -> None: | ||
| """Test a text node.""" | ||
| entity_id = "text.text_node_1_1" | ||
|
|
||
| state = hass.states.get(entity_id) | ||
|
|
||
| assert state | ||
| assert state.state == "test" | ||
|
|
||
| await hass.services.async_call( | ||
| TEXT_DOMAIN, | ||
| SERVICE_SET_VALUE, | ||
| {ATTR_ENTITY_ID: entity_id, ATTR_VALUE: "Hello World"}, | ||
| blocking=True, | ||
| ) | ||
|
|
||
| assert transport_write.call_count == 1 | ||
| assert transport_write.call_args == call("1;1;1;1;47;Hello World\n") | ||
|
|
||
| receive_message("1;1;1;0;47;Hello World\n") | ||
| await hass.async_block_till_done() | ||
|
|
||
| state = hass.states.get(entity_id) | ||
|
|
||
| assert state | ||
| assert state.state == "Hello World" | ||
|
|
||
| transport_write.reset_mock() | ||
|
|
||
| value = "12345678123456781234567812" | ||
|
|
||
| with pytest.raises(ValueError) as err: | ||
| await hass.services.async_call( | ||
| TEXT_DOMAIN, | ||
| SERVICE_SET_VALUE, | ||
| {ATTR_ENTITY_ID: entity_id, ATTR_VALUE: value}, | ||
| blocking=True, | ||
| ) | ||
|
|
||
| assert str(err.value) == ( | ||
| f"Value {value} for Text Node 1 1 is too long (maximum length 25)" | ||
| ) |
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.
Preference: I probably would have named this async_on_unload
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.
Yeah, when all the platforms are migrated to config entry setup, I'll remove this and just use the builtin
entry.async_on_unload.