2016-02-14 00:03:56 +00:00
|
|
|
"""
|
|
|
|
homeassistant.components.sensor.tcp
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
Provides a sensor which gets its values from a TCP socket.
|
|
|
|
"""
|
|
|
|
import logging
|
2016-02-17 17:26:53 +00:00
|
|
|
import socket
|
2016-02-19 17:41:51 +00:00
|
|
|
import select
|
2016-02-14 00:03:56 +00:00
|
|
|
|
2016-02-17 17:26:53 +00:00
|
|
|
from homeassistant.const import CONF_NAME, CONF_HOST
|
2016-02-17 18:12:36 +00:00
|
|
|
from homeassistant.util import template
|
|
|
|
from homeassistant.exceptions import TemplateError
|
2016-02-17 17:26:53 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2016-02-14 00:03:56 +00:00
|
|
|
|
|
|
|
|
2016-02-18 16:57:32 +00:00
|
|
|
# DEPENDENCIES = [DOMAIN]
|
|
|
|
|
|
|
|
DOMAIN = "tcp"
|
|
|
|
|
|
|
|
CONF_PORT = "port"
|
|
|
|
CONF_TIMEOUT = "timeout"
|
|
|
|
CONF_PAYLOAD = "payload"
|
|
|
|
CONF_UNIT = "unit"
|
|
|
|
CONF_VALUE_TEMPLATE = "value_template"
|
|
|
|
CONF_VALUE_ON = "value_on"
|
|
|
|
CONF_BUFFER_SIZE = "buffer_size"
|
|
|
|
|
|
|
|
DEFAULT_TIMEOUT = 10
|
|
|
|
DEFAULT_BUFFER_SIZE = 1024
|
2016-02-14 00:03:56 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
|
|
|
""" Create the Sensor. """
|
2016-02-17 17:26:53 +00:00
|
|
|
if not Sensor.validate_config(config):
|
2016-02-14 00:03:56 +00:00
|
|
|
return False
|
2016-02-17 18:12:36 +00:00
|
|
|
add_entities((Sensor(hass, config),))
|
2016-02-17 17:26:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Sensor(Entity):
|
|
|
|
""" Sensor Entity which gets its value from a TCP socket. """
|
|
|
|
required = tuple()
|
|
|
|
|
2016-02-17 18:12:36 +00:00
|
|
|
def __init__(self, hass, config):
|
2016-02-17 17:26:53 +00:00
|
|
|
""" Set all the config values if they exist and get initial state. """
|
2016-02-17 18:12:36 +00:00
|
|
|
self._hass = hass
|
2016-02-17 17:26:53 +00:00
|
|
|
self._config = {
|
|
|
|
CONF_NAME: config.get(CONF_NAME),
|
|
|
|
CONF_HOST: config[CONF_HOST],
|
|
|
|
CONF_PORT: config[CONF_PORT],
|
|
|
|
CONF_TIMEOUT: config.get(CONF_TIMEOUT, DEFAULT_TIMEOUT),
|
|
|
|
CONF_PAYLOAD: config[CONF_PAYLOAD],
|
|
|
|
CONF_UNIT: config.get(CONF_UNIT),
|
2016-02-17 18:12:36 +00:00
|
|
|
CONF_VALUE_TEMPLATE: config.get(CONF_VALUE_TEMPLATE),
|
2016-02-17 17:26:53 +00:00
|
|
|
CONF_VALUE_ON: config.get(CONF_VALUE_ON),
|
|
|
|
CONF_BUFFER_SIZE: config.get(
|
|
|
|
CONF_BUFFER_SIZE, DEFAULT_BUFFER_SIZE),
|
|
|
|
}
|
|
|
|
self._state = None
|
|
|
|
self.update()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def validate_config(cls, config):
|
|
|
|
""" Ensure the config has all of the necessary values. """
|
|
|
|
always_required = (CONF_HOST, CONF_PORT, CONF_PAYLOAD)
|
|
|
|
for key in always_required + tuple(cls.required):
|
|
|
|
if key not in config:
|
|
|
|
_LOGGER.error(
|
|
|
|
"You must provide %r to create any TCP entity.", key)
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
name = self._config[CONF_NAME]
|
|
|
|
if name is not None:
|
|
|
|
return name
|
|
|
|
return super(Sensor, self).name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
return self._config[CONF_UNIT]
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
""" Get the latest value for this sensor. """
|
2016-02-19 17:41:51 +00:00
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
|
|
|
try:
|
|
|
|
sock.connect(
|
|
|
|
(self._config[CONF_HOST], self._config[CONF_PORT]))
|
|
|
|
except socket.error as err:
|
|
|
|
_LOGGER.error(
|
|
|
|
"Unable to connect to %s on port %s: %s",
|
|
|
|
self._config[CONF_HOST], self._config[CONF_PORT], err)
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
sock.send(self._config[CONF_PAYLOAD].encode())
|
|
|
|
except socket.error as err:
|
|
|
|
_LOGGER.error(
|
|
|
|
"Unable to send payload %r to %s on port %s: %s",
|
|
|
|
self._config[CONF_PAYLOAD], self._config[CONF_HOST],
|
|
|
|
self._config[CONF_PORT], err)
|
|
|
|
return
|
|
|
|
|
|
|
|
readable, _, _ = select.select(
|
|
|
|
[sock], [], [], self._config[CONF_TIMEOUT])
|
|
|
|
if not readable:
|
|
|
|
_LOGGER.warning(
|
|
|
|
"Timeout (%s second(s)) waiting for a response after "
|
|
|
|
"sending %r to %s on port %s.",
|
|
|
|
self._config[CONF_TIMEOUT], self._config[CONF_PAYLOAD],
|
|
|
|
self._config[CONF_HOST], self._config[CONF_PORT])
|
|
|
|
return
|
|
|
|
|
|
|
|
value = sock.recv(self._config[CONF_BUFFER_SIZE]).decode()
|
2016-02-17 18:12:36 +00:00
|
|
|
|
|
|
|
if self._config[CONF_VALUE_TEMPLATE] is not None:
|
2016-02-17 17:26:53 +00:00
|
|
|
try:
|
2016-02-17 18:12:36 +00:00
|
|
|
self._state = template.render(
|
|
|
|
self._hass,
|
|
|
|
self._config[CONF_VALUE_TEMPLATE],
|
|
|
|
value=value)
|
|
|
|
return
|
|
|
|
except TemplateError as err:
|
2016-02-17 17:26:53 +00:00
|
|
|
_LOGGER.error(
|
2016-02-17 18:12:36 +00:00
|
|
|
"Unable to render template of %r with value: %r",
|
|
|
|
self._config[CONF_VALUE_TEMPLATE], value)
|
2016-02-17 17:26:53 +00:00
|
|
|
return
|
2016-02-17 18:12:36 +00:00
|
|
|
|
2016-02-17 17:26:53 +00:00
|
|
|
self._state = value
|