-
-
Notifications
You must be signed in to change notification settings - Fork 37.8k
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
Add mysensors text platform #84667
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
|
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. Preference: I probably would have named this async_on_unload
Member
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. Yeah, when all the platforms are migrated to config entry setup, I'll remove this and just use the builtin |
||
|
|
||
|
|
||
| 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 | ||
| ) | ||
| 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)" | ||
| ) |
Uh oh!
There was an error while loading. Please reload this page.