mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 20:27:08 +00:00
Fix bool registers in modbus integration (#41506)
When a register is of type bool modbus returns a whole byte, but ONLY the lowest bit determine true/false.
This commit is contained in:
parent
8e3258cdfb
commit
4d9ff13384
@ -122,5 +122,5 @@ class ModbusBinarySensor(BinarySensorEntity):
|
|||||||
self._available = False
|
self._available = False
|
||||||
return
|
return
|
||||||
|
|
||||||
self._value = result.bits[0]
|
self._value = result.bits[0] & 1
|
||||||
self._available = True
|
self._available = True
|
||||||
|
@ -228,7 +228,7 @@ class ModbusCover(CoverEntity, RestoreEntity):
|
|||||||
self._available = False
|
self._available = False
|
||||||
return
|
return
|
||||||
|
|
||||||
value = bool(result.bits[0])
|
value = bool(result.bits[0] & 1)
|
||||||
self._available = True
|
self._available = True
|
||||||
|
|
||||||
return value
|
return value
|
||||||
|
@ -174,7 +174,7 @@ class ModbusCoilSwitch(ToggleEntity, RestoreEntity):
|
|||||||
# bits[0] select the lowest bit in result,
|
# bits[0] select the lowest bit in result,
|
||||||
# is_on for a binary_sensor is true if the bit are 1
|
# is_on for a binary_sensor is true if the bit are 1
|
||||||
# The other bits are not considered.
|
# The other bits are not considered.
|
||||||
return bool(result.bits[0])
|
return bool(result.bits[0] & 1)
|
||||||
|
|
||||||
def _write_coil(self, coil, value):
|
def _write_coil(self, coil, value):
|
||||||
"""Write coil using the Modbus hub slave."""
|
"""Write coil using the Modbus hub slave."""
|
||||||
|
@ -29,6 +29,13 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
[0xFF],
|
[0xFF],
|
||||||
STATE_ON,
|
STATE_ON,
|
||||||
),
|
),
|
||||||
|
(
|
||||||
|
{
|
||||||
|
CONF_INPUT_TYPE: CALL_TYPE_COIL,
|
||||||
|
},
|
||||||
|
[0x01],
|
||||||
|
STATE_ON,
|
||||||
|
),
|
||||||
(
|
(
|
||||||
{
|
{
|
||||||
CONF_INPUT_TYPE: CALL_TYPE_COIL,
|
CONF_INPUT_TYPE: CALL_TYPE_COIL,
|
||||||
@ -36,6 +43,20 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
[0x00],
|
[0x00],
|
||||||
STATE_OFF,
|
STATE_OFF,
|
||||||
),
|
),
|
||||||
|
(
|
||||||
|
{
|
||||||
|
CONF_INPUT_TYPE: CALL_TYPE_COIL,
|
||||||
|
},
|
||||||
|
[0x80],
|
||||||
|
STATE_OFF,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
{
|
||||||
|
CONF_INPUT_TYPE: CALL_TYPE_COIL,
|
||||||
|
},
|
||||||
|
[0xFE],
|
||||||
|
STATE_OFF,
|
||||||
|
),
|
||||||
(
|
(
|
||||||
{
|
{
|
||||||
CONF_INPUT_TYPE: CALL_TYPE_DISCRETE,
|
CONF_INPUT_TYPE: CALL_TYPE_DISCRETE,
|
||||||
|
@ -13,34 +13,6 @@ from .conftest import run_base_read_test, setup_base_test
|
|||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
async def run_sensor_test(hass, use_mock_hub, value, expected):
|
|
||||||
"""Run test for given config."""
|
|
||||||
switch_name = "modbus_test_switch"
|
|
||||||
scan_interval = 5
|
|
||||||
entity_id, now, device = await setup_base_test(
|
|
||||||
switch_name,
|
|
||||||
hass,
|
|
||||||
use_mock_hub,
|
|
||||||
{
|
|
||||||
CONF_COILS: [
|
|
||||||
{CONF_NAME: switch_name, CALL_TYPE_COIL: 1234, CONF_SLAVE: 1},
|
|
||||||
]
|
|
||||||
},
|
|
||||||
SWITCH_DOMAIN,
|
|
||||||
scan_interval,
|
|
||||||
)
|
|
||||||
|
|
||||||
await run_base_read_test(
|
|
||||||
entity_id,
|
|
||||||
hass,
|
|
||||||
use_mock_hub,
|
|
||||||
CALL_TYPE_COIL,
|
|
||||||
value,
|
|
||||||
expected,
|
|
||||||
now + timedelta(seconds=scan_interval + 1),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"regs,expected",
|
"regs,expected",
|
||||||
[
|
[
|
||||||
@ -48,6 +20,14 @@ async def run_sensor_test(hass, use_mock_hub, value, expected):
|
|||||||
[0x00],
|
[0x00],
|
||||||
STATE_OFF,
|
STATE_OFF,
|
||||||
),
|
),
|
||||||
|
(
|
||||||
|
[0x80],
|
||||||
|
STATE_OFF,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
[0xFE],
|
||||||
|
STATE_OFF,
|
||||||
|
),
|
||||||
(
|
(
|
||||||
[0xFF],
|
[0xFF],
|
||||||
STATE_ON,
|
STATE_ON,
|
||||||
@ -59,10 +39,28 @@ async def run_sensor_test(hass, use_mock_hub, value, expected):
|
|||||||
],
|
],
|
||||||
)
|
)
|
||||||
async def test_coil_switch(hass, mock_hub, regs, expected):
|
async def test_coil_switch(hass, mock_hub, regs, expected):
|
||||||
"""Test reading of switch coil."""
|
"""Run test for given config."""
|
||||||
await run_sensor_test(
|
switch_name = "modbus_test_switch"
|
||||||
|
scan_interval = 5
|
||||||
|
entity_id, now, device = await setup_base_test(
|
||||||
|
switch_name,
|
||||||
hass,
|
hass,
|
||||||
mock_hub,
|
mock_hub,
|
||||||
|
{
|
||||||
|
CONF_COILS: [
|
||||||
|
{CONF_NAME: switch_name, CALL_TYPE_COIL: 1234, CONF_SLAVE: 1},
|
||||||
|
]
|
||||||
|
},
|
||||||
|
SWITCH_DOMAIN,
|
||||||
|
scan_interval,
|
||||||
|
)
|
||||||
|
|
||||||
|
await run_base_read_test(
|
||||||
|
entity_id,
|
||||||
|
hass,
|
||||||
|
mock_hub,
|
||||||
|
CALL_TYPE_COIL,
|
||||||
regs,
|
regs,
|
||||||
expected,
|
expected,
|
||||||
|
now + timedelta(seconds=scan_interval + 1),
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user