Handle exception in modbus slave sensor (#67472)

This commit is contained in:
jan iversen 2022-03-02 18:49:57 +01:00 committed by GitHub
parent dc24e6505e
commit a4915eb704
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 1 deletions

View File

@ -101,6 +101,9 @@ class ModbusRegisterSensor(BaseStructPlatform, RestoreEntity, SensorEntity):
return
self._lazy_errors = self._lazy_error_count
self._attr_available = False
self._attr_native_value = None
if self._coordinator:
self._coordinator.async_set_updated_data(None)
self.async_write_ha_state()
return

View File

@ -33,6 +33,7 @@ from homeassistant.const import (
CONF_SLAVE,
CONF_STRUCTURE,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
)
from homeassistant.core import State
@ -565,13 +566,14 @@ async def test_all_sensor(hass, mock_do_cycle, expected):
],
)
@pytest.mark.parametrize(
"config_addon,register_words,expected",
"config_addon,register_words,do_exception,expected",
[
(
{
CONF_SLAVE_COUNT: 0,
},
[0x0102, 0x0304],
False,
["16909060"],
),
(
@ -579,6 +581,7 @@ async def test_all_sensor(hass, mock_do_cycle, expected):
CONF_SLAVE_COUNT: 1,
},
[0x0102, 0x0304, 0x0403, 0x0201],
False,
["16909060", "67305985"],
),
(
@ -595,6 +598,7 @@ async def test_all_sensor(hass, mock_do_cycle, expected):
0x0D0E,
0x0F00,
],
False,
[
"16909060",
"84281096",
@ -602,6 +606,22 @@ async def test_all_sensor(hass, mock_do_cycle, expected):
"219025152",
],
),
(
{
CONF_SLAVE_COUNT: 1,
},
[0x0102, 0x0304, 0x0403, 0x0201],
True,
[STATE_UNAVAILABLE, STATE_UNKNOWN],
),
(
{
CONF_SLAVE_COUNT: 1,
},
[],
False,
[STATE_UNAVAILABLE, STATE_UNKNOWN],
),
],
)
async def test_slave_sensor(hass, mock_do_cycle, expected):