2019-04-03 15:40:03 +00:00
|
|
|
"""Support for switch controlled using a telnet connection."""
|
2018-01-21 06:35:38 +00:00
|
|
|
from datetime import timedelta
|
2017-09-18 15:35:35 +00:00
|
|
|
import logging
|
|
|
|
import telnetlib
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2018-01-21 06:35:38 +00:00
|
|
|
from homeassistant.components.switch import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ENTITY_ID_FORMAT,
|
|
|
|
PLATFORM_SCHEMA,
|
2020-04-26 16:50:37 +00:00
|
|
|
SwitchEntity,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2017-09-18 15:35:35 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_COMMAND_OFF,
|
|
|
|
CONF_COMMAND_ON,
|
|
|
|
CONF_COMMAND_STATE,
|
|
|
|
CONF_NAME,
|
|
|
|
CONF_PORT,
|
|
|
|
CONF_RESOURCE,
|
|
|
|
CONF_SWITCHES,
|
2019-12-09 13:41:48 +00:00
|
|
|
CONF_TIMEOUT,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_VALUE_TEMPLATE,
|
|
|
|
)
|
2017-09-18 15:35:35 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DEFAULT_PORT = 23
|
2019-07-31 17:59:12 +00:00
|
|
|
DEFAULT_TIMEOUT = 0.2
|
2017-09-18 15:35:35 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SWITCH_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_COMMAND_OFF): cv.string,
|
|
|
|
vol.Required(CONF_COMMAND_ON): cv.string,
|
|
|
|
vol.Required(CONF_RESOURCE): cv.string,
|
|
|
|
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
|
|
|
|
vol.Optional(CONF_COMMAND_STATE): cv.string,
|
|
|
|
vol.Optional(CONF_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
|
|
|
vol.Optional(CONF_TIMEOUT, default=DEFAULT_TIMEOUT): vol.Coerce(float),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{vol.Required(CONF_SWITCHES): cv.schema_with_slug_keys(SWITCH_SCHEMA)}
|
|
|
|
)
|
2017-09-18 15:35:35 +00:00
|
|
|
|
|
|
|
SCAN_INTERVAL = timedelta(seconds=10)
|
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-09-18 15:35:35 +00:00
|
|
|
"""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),
|
2019-07-31 17:59:12 +00:00
|
|
|
value_template,
|
2019-07-31 19:25:30 +00:00
|
|
|
device_config.get(CONF_TIMEOUT),
|
2017-09-18 15:35:35 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
if not switches:
|
|
|
|
_LOGGER.error("No switches added")
|
2018-01-21 06:35:38 +00:00
|
|
|
return
|
2017-09-18 15:35:35 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(switches)
|
2017-09-18 15:35:35 +00:00
|
|
|
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
class TelnetSwitch(SwitchEntity):
|
2017-09-18 15:35:35 +00:00
|
|
|
"""Representation of a switch that can be toggled using telnet commands."""
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
hass,
|
|
|
|
object_id,
|
|
|
|
resource,
|
|
|
|
port,
|
|
|
|
friendly_name,
|
|
|
|
command_on,
|
|
|
|
command_off,
|
|
|
|
command_state,
|
|
|
|
value_template,
|
|
|
|
timeout,
|
|
|
|
):
|
2017-09-18 15:35:35 +00:00
|
|
|
"""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
|
2019-07-31 17:59:12 +00:00
|
|
|
self._timeout = timeout
|
2017-09-18 15:35:35 +00:00
|
|
|
|
|
|
|
def _telnet_command(self, command):
|
|
|
|
try:
|
|
|
|
telnet = telnetlib.Telnet(self._resource, self._port)
|
2019-07-31 19:25:30 +00:00
|
|
|
telnet.write(command.encode("ASCII") + b"\r")
|
|
|
|
response = telnet.read_until(b"\r", timeout=self._timeout)
|
|
|
|
_LOGGER.debug("telnet response: %s", response.decode("ASCII").strip())
|
|
|
|
return response.decode("ASCII").strip()
|
2019-09-04 17:09:24 +00:00
|
|
|
except OSError as error:
|
2017-09-18 15:35:35 +00:00
|
|
|
_LOGGER.error(
|
2019-07-31 19:25:30 +00:00
|
|
|
'Command "%s" failed with exception: %s', command, repr(error)
|
|
|
|
)
|
2017-09-18 15:35:35 +00:00
|
|
|
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):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Return true if no state command is defined, false otherwise."""
|
2017-09-18 15:35:35 +00:00
|
|
|
return self._command_state is None
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update device state."""
|
|
|
|
response = self._telnet_command(self._command_state)
|
|
|
|
if response:
|
2019-07-31 19:25:30 +00:00
|
|
|
rendered = self._value_template.render_with_possible_json_value(response)
|
2017-09-18 15:35:35 +00:00
|
|
|
self._state = rendered == "True"
|
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.warning("Empty response for command: %s", self._command_state)
|
2017-09-18 15:35:35 +00:00
|
|
|
|
|
|
|
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
|