-
-
Notifications
You must be signed in to change notification settings - Fork 37.6k
Telnet switch #8913
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
Telnet switch #8913
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b204c72
Added telnet switch.
dd75053
Lint.
e9c4a3d
Coverage
47e4be0
Added port parameter to Telnet switch.
0ded0a7
Removed optimistic attribute from Telnet switch.
961601f
Merge remote-tracking branch 'upstream/dev' into feature/telent_switch
a393c16
Code cleanup.
e80fd7d
Merge remote-tracking branch 'upstream/dev' into feature/telent_switch
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| """ | ||
| Support for switch controlled using a telnet connection. | ||
|
|
||
| For more details about this platform, please refer to the documentation at | ||
| https://home-assistant.io/components/switch.telnet/ | ||
| """ | ||
| import logging | ||
| import telnetlib | ||
| from datetime import timedelta | ||
|
|
||
| import voluptuous as vol | ||
|
|
||
| from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA, | ||
| ENTITY_ID_FORMAT) | ||
| from homeassistant.const import ( | ||
| CONF_RESOURCE, CONF_NAME, CONF_SWITCHES, CONF_VALUE_TEMPLATE, | ||
| CONF_COMMAND_OFF, CONF_COMMAND_ON, CONF_COMMAND_STATE, CONF_PORT) | ||
| import homeassistant.helpers.config_validation as cv | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| DEFAULT_PORT = 23 | ||
|
|
||
| SWITCH_SCHEMA = vol.Schema({ | ||
| vol.Required(CONF_COMMAND_ON): cv.string, | ||
| vol.Required(CONF_COMMAND_OFF): cv.string, | ||
| vol.Optional(CONF_COMMAND_STATE): cv.string, | ||
| vol.Optional(CONF_NAME): cv.string, | ||
| vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port, | ||
| vol.Required(CONF_RESOURCE): cv.string, | ||
| vol.Required(CONF_VALUE_TEMPLATE): cv.template, | ||
| }) | ||
|
|
||
| PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | ||
| vol.Required(CONF_SWITCHES): vol.Schema({cv.slug: SWITCH_SCHEMA}), | ||
| }) | ||
|
|
||
| SCAN_INTERVAL = timedelta(seconds=10) | ||
|
|
||
|
|
||
| # pylint: disable=unused-argument | ||
| def setup_platform(hass, config, add_devices, discovery_info=None): | ||
| """Find and return switches controlled by telnet commands.""" | ||
| devices = config.get(CONF_SWITCHES, {}) | ||
| switches = [] | ||
|
|
||
| for object_id, device_config in devices.items(): | ||
| value_template = device_config.get(CONF_VALUE_TEMPLATE) | ||
|
|
||
| if value_template is not None: | ||
| value_template.hass = hass | ||
|
|
||
| switches.append( | ||
| TelnetSwitch( | ||
| hass, | ||
| object_id, | ||
| device_config.get(CONF_RESOURCE), | ||
| device_config.get(CONF_PORT), | ||
| device_config.get(CONF_NAME, object_id), | ||
| device_config.get(CONF_COMMAND_ON), | ||
| device_config.get(CONF_COMMAND_OFF), | ||
| device_config.get(CONF_COMMAND_STATE), | ||
| value_template | ||
| ) | ||
| ) | ||
|
|
||
| if not switches: | ||
| _LOGGER.error("No switches added") | ||
| return False | ||
|
|
||
| add_devices(switches) | ||
|
|
||
|
|
||
| class TelnetSwitch(SwitchDevice): | ||
| """Representation of a switch that can be toggled using telnet commands.""" | ||
|
|
||
| def __init__(self, hass, object_id, resource, port, friendly_name, | ||
| command_on, command_off, command_state, value_template): | ||
| """Initialize the switch.""" | ||
| self._hass = hass | ||
| self.entity_id = ENTITY_ID_FORMAT.format(object_id) | ||
| self._resource = resource | ||
| self._port = port | ||
| self._name = friendly_name | ||
| self._state = False | ||
| self._command_on = command_on | ||
| self._command_off = command_off | ||
| self._command_state = command_state | ||
| self._value_template = value_template | ||
|
|
||
| def _telnet_command(self, command): | ||
| try: | ||
| telnet = telnetlib.Telnet(self._resource, self._port) | ||
| telnet.write(command.encode('ASCII') + b'\r') | ||
| response = telnet.read_until(b'\r', timeout=0.2) | ||
| return response.decode('ASCII').strip() | ||
| except IOError as error: | ||
| _LOGGER.error( | ||
| 'Command "%s" failed with exception: %s', | ||
| command, repr(error)) | ||
| return None | ||
|
|
||
| @property | ||
| def name(self): | ||
| """Return the name of the switch.""" | ||
| return self._name | ||
|
|
||
| @property | ||
| def should_poll(self): | ||
| """Only poll if we have state command.""" | ||
| return self._command_state is not None | ||
|
|
||
| @property | ||
| def is_on(self): | ||
| """Return true if device is on.""" | ||
| return self._state | ||
|
|
||
| @property | ||
| def assumed_state(self): | ||
| """Default ist true if no state command is defined, false otherwise.""" | ||
| return self._command_state is None | ||
|
|
||
| def update(self): | ||
| """Update device state.""" | ||
| response = self._telnet_command(self._command_state) | ||
| if response: | ||
| rendered = self._value_template \ | ||
| .render_with_possible_json_value(response) | ||
| self._state = rendered == "True" | ||
| else: | ||
| _LOGGER.warning( | ||
| "Empty response for command: %s", self._command_state) | ||
|
|
||
| def turn_on(self, **kwargs): | ||
| """Turn the device on.""" | ||
| self._telnet_command(self._command_on) | ||
| if self.assumed_state: | ||
| self._state = True | ||
|
|
||
| def turn_off(self, **kwargs): | ||
| """Turn the device off.""" | ||
| self._telnet_command(self._command_off) | ||
| if self.assumed_state: | ||
| self._state = False | ||
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.
Would be nice to keep it ordered.