2019-02-14 04:35:12 +00:00
|
|
|
"""Support for Modbus Register sensors."""
|
2015-04-15 14:47:42 +00:00
|
|
|
import logging
|
2017-04-21 01:28:49 +00:00
|
|
|
import struct
|
2016-10-30 08:58:34 +00:00
|
|
|
|
2016-09-13 20:47:44 +00:00
|
|
|
import voluptuous as vol
|
2015-04-15 14:47:42 +00:00
|
|
|
|
2019-02-11 19:00:37 +00:00
|
|
|
from homeassistant.components.modbus import (
|
|
|
|
CONF_HUB, DEFAULT_HUB, DOMAIN as MODBUS_DOMAIN)
|
2015-04-15 14:47:42 +00:00
|
|
|
from homeassistant.const import (
|
2017-11-13 22:27:15 +00:00
|
|
|
CONF_NAME, CONF_OFFSET, CONF_UNIT_OF_MEASUREMENT, CONF_SLAVE,
|
|
|
|
CONF_STRUCTURE)
|
2019-02-11 19:00:37 +00:00
|
|
|
from homeassistant.helpers.restore_state import RestoreEntity
|
2016-09-13 20:47:44 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv
|
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
2015-04-15 14:47:42 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2017-05-02 16:18:47 +00:00
|
|
|
|
2015-08-09 04:22:34 +00:00
|
|
|
DEPENDENCIES = ['modbus']
|
2015-04-15 14:47:42 +00:00
|
|
|
|
2016-10-30 08:58:34 +00:00
|
|
|
CONF_COUNT = 'count'
|
2017-11-13 22:27:15 +00:00
|
|
|
CONF_REVERSE_ORDER = 'reverse_order'
|
2016-10-30 08:58:34 +00:00
|
|
|
CONF_PRECISION = 'precision'
|
|
|
|
CONF_REGISTER = 'register'
|
|
|
|
CONF_REGISTERS = 'registers'
|
|
|
|
CONF_SCALE = 'scale'
|
2017-04-21 01:28:49 +00:00
|
|
|
CONF_DATA_TYPE = 'data_type'
|
|
|
|
CONF_REGISTER_TYPE = 'register_type'
|
|
|
|
|
|
|
|
REGISTER_TYPE_HOLDING = 'holding'
|
|
|
|
REGISTER_TYPE_INPUT = 'input'
|
|
|
|
|
|
|
|
DATA_TYPE_INT = 'int'
|
2017-11-13 22:27:15 +00:00
|
|
|
DATA_TYPE_UINT = 'uint'
|
2017-04-21 01:28:49 +00:00
|
|
|
DATA_TYPE_FLOAT = 'float'
|
2017-11-13 22:27:15 +00:00
|
|
|
DATA_TYPE_CUSTOM = 'custom'
|
2016-09-13 20:47:44 +00:00
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_REGISTERS): [{
|
2019-02-11 19:00:37 +00:00
|
|
|
vol.Optional(CONF_HUB, default=DEFAULT_HUB): cv.string,
|
2016-09-13 20:47:44 +00:00
|
|
|
vol.Required(CONF_NAME): cv.string,
|
|
|
|
vol.Required(CONF_REGISTER): cv.positive_int,
|
2017-04-21 01:28:49 +00:00
|
|
|
vol.Optional(CONF_REGISTER_TYPE, default=REGISTER_TYPE_HOLDING):
|
|
|
|
vol.In([REGISTER_TYPE_HOLDING, REGISTER_TYPE_INPUT]),
|
2016-09-13 20:47:44 +00:00
|
|
|
vol.Optional(CONF_COUNT, default=1): cv.positive_int,
|
2017-11-13 22:27:15 +00:00
|
|
|
vol.Optional(CONF_REVERSE_ORDER, default=False): cv.boolean,
|
2016-09-13 20:47:44 +00:00
|
|
|
vol.Optional(CONF_OFFSET, default=0): vol.Coerce(float),
|
|
|
|
vol.Optional(CONF_PRECISION, default=0): cv.positive_int,
|
|
|
|
vol.Optional(CONF_SCALE, default=1): vol.Coerce(float),
|
|
|
|
vol.Optional(CONF_SLAVE): cv.positive_int,
|
2017-04-21 01:28:49 +00:00
|
|
|
vol.Optional(CONF_DATA_TYPE, default=DATA_TYPE_INT):
|
2017-11-13 22:27:15 +00:00
|
|
|
vol.In([DATA_TYPE_INT, DATA_TYPE_UINT, DATA_TYPE_FLOAT,
|
|
|
|
DATA_TYPE_CUSTOM]),
|
|
|
|
vol.Optional(CONF_STRUCTURE): cv.string,
|
2019-02-14 04:35:12 +00:00
|
|
|
vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
|
2016-09-13 20:47:44 +00:00
|
|
|
}]
|
|
|
|
})
|
|
|
|
|
2015-04-21 14:40:13 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2016-10-30 08:58:34 +00:00
|
|
|
"""Set up the Modbus sensors."""
|
2015-04-15 14:47:42 +00:00
|
|
|
sensors = []
|
2017-11-13 22:27:15 +00:00
|
|
|
data_types = {DATA_TYPE_INT: {1: 'h', 2: 'i', 4: 'q'}}
|
|
|
|
data_types[DATA_TYPE_UINT] = {1: 'H', 2: 'I', 4: 'Q'}
|
|
|
|
data_types[DATA_TYPE_FLOAT] = {1: 'e', 2: 'f', 4: 'd'}
|
|
|
|
|
2016-09-13 20:47:44 +00:00
|
|
|
for register in config.get(CONF_REGISTERS):
|
2017-11-13 22:27:15 +00:00
|
|
|
structure = '>i'
|
|
|
|
if register.get(CONF_DATA_TYPE) != DATA_TYPE_CUSTOM:
|
|
|
|
try:
|
2019-02-11 19:00:37 +00:00
|
|
|
structure = '>{}'.format(data_types[register.get(
|
|
|
|
CONF_DATA_TYPE)][register.get(CONF_COUNT)])
|
2017-11-13 22:27:15 +00:00
|
|
|
except KeyError:
|
|
|
|
_LOGGER.error("Unable to detect data type for %s sensor, "
|
2019-02-14 04:35:12 +00:00
|
|
|
"try a custom type", register.get(CONF_NAME))
|
2017-11-13 22:27:15 +00:00
|
|
|
continue
|
|
|
|
else:
|
|
|
|
structure = register.get(CONF_STRUCTURE)
|
|
|
|
|
|
|
|
try:
|
|
|
|
size = struct.calcsize(structure)
|
|
|
|
except struct.error as err:
|
|
|
|
_LOGGER.error(
|
|
|
|
"Error in sensor %s structure: %s",
|
|
|
|
register.get(CONF_NAME), err)
|
|
|
|
continue
|
|
|
|
|
|
|
|
if register.get(CONF_COUNT) * 2 != size:
|
|
|
|
_LOGGER.error(
|
|
|
|
"Structure size (%d bytes) mismatch registers count "
|
|
|
|
"(%d words)", size, register.get(CONF_COUNT))
|
|
|
|
continue
|
|
|
|
|
2019-02-11 19:00:37 +00:00
|
|
|
hub_name = register.get(CONF_HUB)
|
|
|
|
hub = hass.data[MODBUS_DOMAIN][hub_name]
|
2016-09-13 20:47:44 +00:00
|
|
|
sensors.append(ModbusRegisterSensor(
|
2019-02-11 19:00:37 +00:00
|
|
|
hub,
|
2016-09-13 20:47:44 +00:00
|
|
|
register.get(CONF_NAME),
|
|
|
|
register.get(CONF_SLAVE),
|
|
|
|
register.get(CONF_REGISTER),
|
2017-04-21 01:28:49 +00:00
|
|
|
register.get(CONF_REGISTER_TYPE),
|
2016-09-13 20:47:44 +00:00
|
|
|
register.get(CONF_UNIT_OF_MEASUREMENT),
|
|
|
|
register.get(CONF_COUNT),
|
2017-11-13 22:27:15 +00:00
|
|
|
register.get(CONF_REVERSE_ORDER),
|
2016-09-13 20:47:44 +00:00
|
|
|
register.get(CONF_SCALE),
|
|
|
|
register.get(CONF_OFFSET),
|
2017-11-13 22:27:15 +00:00
|
|
|
structure,
|
2016-09-13 20:47:44 +00:00
|
|
|
register.get(CONF_PRECISION)))
|
2017-11-13 22:27:15 +00:00
|
|
|
|
|
|
|
if not sensors:
|
|
|
|
return False
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(sensors)
|
2015-04-15 14:47:42 +00:00
|
|
|
|
2015-04-21 14:40:13 +00:00
|
|
|
|
2019-02-11 19:00:37 +00:00
|
|
|
class ModbusRegisterSensor(RestoreEntity):
|
2017-09-23 15:15:46 +00:00
|
|
|
"""Modbus register sensor."""
|
2015-04-15 14:47:42 +00:00
|
|
|
|
2019-02-11 19:00:37 +00:00
|
|
|
def __init__(self, hub, name, slave, register, register_type,
|
2017-11-13 22:27:15 +00:00
|
|
|
unit_of_measurement, count, reverse_order, scale, offset,
|
|
|
|
structure, precision):
|
2016-09-13 20:47:44 +00:00
|
|
|
"""Initialize the modbus register sensor."""
|
2019-02-11 19:00:37 +00:00
|
|
|
self._hub = hub
|
2015-04-15 14:47:42 +00:00
|
|
|
self._name = name
|
2016-09-13 20:47:44 +00:00
|
|
|
self._slave = int(slave) if slave else None
|
|
|
|
self._register = int(register)
|
2017-04-21 01:28:49 +00:00
|
|
|
self._register_type = register_type
|
2016-09-13 20:47:44 +00:00
|
|
|
self._unit_of_measurement = unit_of_measurement
|
|
|
|
self._count = int(count)
|
2017-11-13 22:27:15 +00:00
|
|
|
self._reverse_order = reverse_order
|
2016-08-27 09:49:49 +00:00
|
|
|
self._scale = scale
|
|
|
|
self._offset = offset
|
|
|
|
self._precision = precision
|
2017-11-13 22:27:15 +00:00
|
|
|
self._structure = structure
|
2016-09-13 20:47:44 +00:00
|
|
|
self._value = None
|
2015-04-15 14:47:42 +00:00
|
|
|
|
2019-02-11 19:00:37 +00:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Handle entity which will be added."""
|
|
|
|
state = await self.async_get_last_state()
|
|
|
|
if not state:
|
|
|
|
return
|
|
|
|
self._value = state.state
|
|
|
|
|
2015-04-15 14:47:42 +00:00
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the state of the sensor."""
|
2016-09-13 20:47:44 +00:00
|
|
|
return self._value
|
2015-04-15 14:47:42 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the name of the sensor."""
|
2015-04-15 14:47:42 +00:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2016-09-13 20:47:44 +00:00
|
|
|
"""Return the unit of measurement."""
|
|
|
|
return self._unit_of_measurement
|
2015-04-15 14:47:42 +00:00
|
|
|
|
|
|
|
def update(self):
|
2016-02-23 05:21:49 +00:00
|
|
|
"""Update the state of the sensor."""
|
2017-04-21 01:28:49 +00:00
|
|
|
if self._register_type == REGISTER_TYPE_INPUT:
|
2019-02-11 19:00:37 +00:00
|
|
|
result = self._hub.read_input_registers(
|
2017-04-21 01:28:49 +00:00
|
|
|
self._slave,
|
|
|
|
self._register,
|
|
|
|
self._count)
|
|
|
|
else:
|
2019-02-11 19:00:37 +00:00
|
|
|
result = self._hub.read_holding_registers(
|
2017-04-21 01:28:49 +00:00
|
|
|
self._slave,
|
|
|
|
self._register,
|
|
|
|
self._count)
|
2016-09-13 20:47:44 +00:00
|
|
|
val = 0
|
2017-07-07 05:30:23 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
registers = result.registers
|
2017-11-13 22:27:15 +00:00
|
|
|
if self._reverse_order:
|
|
|
|
registers.reverse()
|
2017-07-07 05:30:23 +00:00
|
|
|
except AttributeError:
|
2019-02-11 19:00:37 +00:00
|
|
|
_LOGGER.error("No response from hub %s, slave %s, register %s",
|
|
|
|
self._hub.name, self._slave, self._register)
|
2016-09-27 03:46:34 +00:00
|
|
|
return
|
2017-11-13 22:27:15 +00:00
|
|
|
byte_string = b''.join(
|
|
|
|
[x.to_bytes(2, byteorder='big') for x in registers]
|
|
|
|
)
|
|
|
|
val = struct.unpack(self._structure, byte_string)[0]
|
2016-09-13 20:47:44 +00:00
|
|
|
self._value = format(
|
2016-10-30 08:58:34 +00:00
|
|
|
self._scale * val + self._offset, '.{}f'.format(self._precision))
|