Modbus coil support

pull/233/head^2
Vitor Espíndola 2015-07-29 14:04:32 -03:00
parent dc2ed19105
commit f82b63483a
2 changed files with 94 additions and 53 deletions

View File

@ -18,6 +18,9 @@ sensor:
name: My boolean sensor
2:
name: My other boolean sensor
coils:
0:
name: My coil switch
VARIABLES:
@ -25,6 +28,7 @@ VARIABLES:
- "unit" = unit to attach to value (optional, ignored for boolean sensors)
- "registers" contains a list of relevant registers to read from
it can contain a "bits" section, listing relevant bits
- "coils" contains a list of relevant coils to read from
- each named register will create an integer sensor
- each named bit will create a boolean sensor
@ -49,6 +53,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
_LOGGER.error("No slave number provided for serial Modbus")
return False
registers = config.get("registers")
if registers:
for regnum, register in registers.items():
if register.get("name"):
sensors.append(ModbusSensor(register.get("name"),
@ -64,6 +69,14 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
slave,
regnum,
bitnum))
coils = config.get("coils")
if coils:
for coilnum, coil in coils.items():
sensors.append(ModbusSensor(coil.get("name"),
slave,
coilnum,
coil=True))
add_devices(sensors)
@ -71,13 +84,14 @@ class ModbusSensor(Entity):
# pylint: disable=too-many-arguments
""" Represents a Modbus Sensor """
def __init__(self, name, slave, register, bit=None, unit=None):
def __init__(self, name, slave, register, bit=None, unit=None, coil=False):
self._name = name
self.slave = int(slave) if slave else 1
self.register = int(register)
self.bit = int(bit) if bit else None
self._value = None
self._unit = unit
self._coil = coil
def __str__(self):
return "%s: %s" % (self.name, self.state)
@ -124,6 +138,10 @@ class ModbusSensor(Entity):
return attr
def update(self):
if self._coil:
result = modbus.NETWORK.read_coils(self.register, 1)
self._value = result.bits[0]
else:
result = modbus.NETWORK.read_holding_registers(unit=self.slave,
address=self.register,
count=1)

View File

@ -18,12 +18,16 @@ sensor:
name: My switch
2:
name: My other switch
coils:
0:
name: My coil switch
VARIABLES:
- "slave" = slave number (ignored and can be omitted if not serial Modbus)
- "registers" contains a list of relevant registers to read from
- it must contain a "bits" section, listing relevant bits
- "coils" contains a list of relevant coils to read from/write to
- each named bit will create a switch
"""
@ -44,6 +48,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
_LOGGER.error("No slave number provided for serial Modbus")
return False
registers = config.get("registers")
if registers:
for regnum, register in registers.items():
bits = register.get("bits")
for bitnum, bit in bits.items():
@ -52,17 +57,26 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
slave,
regnum,
bitnum))
coils = config.get("coils")
if coils:
for coilnum, coil in coils.items():
switches.append(ModbusSwitch(coil.get("name"),
slave,
coilnum,
0,
coil=True))
add_devices(switches)
class ModbusSwitch(ToggleEntity):
""" Represents a Modbus switch. """
def __init__(self, name, slave, register, bit):
def __init__(self, name, slave, register, bit, coil=False):
self._name = name
self.slave = int(slave) if slave else 1
self.register = int(register)
self.bit = int(bit)
self._coil = coil
self._is_on = None
self.register_value = None
@ -98,6 +112,9 @@ class ModbusSwitch(ToggleEntity):
return attr
def turn_on(self, **kwargs):
if self._coil:
modbus.NETWORK.write_coil(self.register, True)
else:
if self.register_value is None:
self.update()
val = self.register_value | (0x0001 << self.bit)
@ -106,6 +123,9 @@ class ModbusSwitch(ToggleEntity):
value=val)
def turn_off(self, **kwargs):
if self._coil:
r = modbus.NETWORK.write_coil(self.register, False)
else:
if self.register_value is None:
self.update()
val = self.register_value & ~(0x0001 << self.bit)
@ -114,9 +134,12 @@ class ModbusSwitch(ToggleEntity):
value=val)
def update(self):
result = modbus.NETWORK.read_holding_registers(unit=self.slave,
address=self.register,
count=1)
if self._coil:
result = modbus.NETWORK.read_coils(self.register, 1)
self.register_value = result.bits[0]
self._is_on = self.register_value
else:
result = modbus.NETWORK.read_holding_registers(unit=self.slave, address=self.register, count=1)
val = 0
for i, res in enumerate(result.registers):
val += res * (2**(i*16))