Add restore_state to modbus binary_sensor (#50922)

* Add restore_state to binary_sensor.

* Update return value in State.
This commit is contained in:
jan iversen 2021-05-22 13:38:05 +02:00 committed by GitHub
parent 016abda12e
commit 59ae78e5f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 1 deletions

View File

@ -17,9 +17,11 @@ from homeassistant.const import (
CONF_NAME, CONF_NAME,
CONF_SCAN_INTERVAL, CONF_SCAN_INTERVAL,
CONF_SLAVE, CONF_SLAVE,
STATE_ON,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_validation as cv from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .base_platform import BasePlatform from .base_platform import BasePlatform
@ -96,12 +98,17 @@ async def async_setup_platform(
async_add_entities(sensors) async_add_entities(sensors)
class ModbusBinarySensor(BasePlatform, BinarySensorEntity): class ModbusBinarySensor(BasePlatform, RestoreEntity, BinarySensorEntity):
"""Modbus binary sensor.""" """Modbus binary sensor."""
async def async_added_to_hass(self): async def async_added_to_hass(self):
"""Handle entity which will be added.""" """Handle entity which will be added."""
await self.async_base_added_to_hass() await self.async_base_added_to_hass()
state = await self.async_get_last_state()
if state:
self._value = state.state == STATE_ON
else:
self._value = None
@property @property
def is_on(self): def is_on(self):

View File

@ -18,9 +18,12 @@ from homeassistant.const import (
STATE_ON, STATE_ON,
STATE_UNAVAILABLE, STATE_UNAVAILABLE,
) )
from homeassistant.core import State
from .conftest import ReadResult, base_config_test, base_test, prepare_service_update from .conftest import ReadResult, base_config_test, base_test, prepare_service_update
from tests.common import mock_restore_cache
@pytest.mark.parametrize("do_discovery", [False, True]) @pytest.mark.parametrize("do_discovery", [False, True])
@pytest.mark.parametrize( @pytest.mark.parametrize(
@ -130,3 +133,26 @@ async def test_service_binary_sensor_update(hass, mock_pymodbus):
"homeassistant", "update_entity", {"entity_id": entity_id}, blocking=True "homeassistant", "update_entity", {"entity_id": entity_id}, blocking=True
) )
assert hass.states.get(entity_id).state == STATE_ON assert hass.states.get(entity_id).state == STATE_ON
async def test_restore_state_binary_sensor(hass):
"""Run test for binary sensor restore state."""
sensor_name = "test_binary_sensor"
test_value = STATE_ON
config_sensor = {CONF_NAME: sensor_name, CONF_ADDRESS: 17}
mock_restore_cache(
hass,
(State(f"{SENSOR_DOMAIN}.{sensor_name}", test_value),),
)
await base_config_test(
hass,
config_sensor,
sensor_name,
SENSOR_DOMAIN,
CONF_BINARY_SENSORS,
None,
method_discovery=True,
)
entity_id = f"{SENSOR_DOMAIN}.{sensor_name}"
assert hass.states.get(entity_id).state == test_value