mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Add restore_state to modbus binary_sensor (#50922)
* Add restore_state to binary_sensor. * Update return value in State.
This commit is contained in:
parent
016abda12e
commit
59ae78e5f0
@ -17,9 +17,11 @@ from homeassistant.const import (
|
||||
CONF_NAME,
|
||||
CONF_SCAN_INTERVAL,
|
||||
CONF_SLAVE,
|
||||
STATE_ON,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
|
||||
from .base_platform import BasePlatform
|
||||
@ -96,12 +98,17 @@ async def async_setup_platform(
|
||||
async_add_entities(sensors)
|
||||
|
||||
|
||||
class ModbusBinarySensor(BasePlatform, BinarySensorEntity):
|
||||
class ModbusBinarySensor(BasePlatform, RestoreEntity, BinarySensorEntity):
|
||||
"""Modbus binary sensor."""
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Handle entity which will be added."""
|
||||
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
|
||||
def is_on(self):
|
||||
|
@ -18,9 +18,12 @@ from homeassistant.const import (
|
||||
STATE_ON,
|
||||
STATE_UNAVAILABLE,
|
||||
)
|
||||
from homeassistant.core import State
|
||||
|
||||
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(
|
||||
@ -130,3 +133,26 @@ async def test_service_binary_sensor_update(hass, mock_pymodbus):
|
||||
"homeassistant", "update_entity", {"entity_id": entity_id}, blocking=True
|
||||
)
|
||||
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
|
Loading…
x
Reference in New Issue
Block a user