Modbus entity update does not occur until after scan_interval (#56221)
* Secure update is called when integration is started. * Review comments. * Update homeassistant/components/modbus/base_platform.py Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io> * Update homeassistant/components/modbus/base_platform.py Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io> Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
parent
02ba3c6089
commit
6947912fa9
3 changed files with 22 additions and 13 deletions
|
@ -28,6 +28,7 @@ from homeassistant.helpers.event import async_call_later, async_track_time_inter
|
||||||
from homeassistant.helpers.restore_state import RestoreEntity
|
from homeassistant.helpers.restore_state import RestoreEntity
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
|
ACTIVE_SCAN_INTERVAL,
|
||||||
CALL_TYPE_COIL,
|
CALL_TYPE_COIL,
|
||||||
CALL_TYPE_DISCRETE,
|
CALL_TYPE_DISCRETE,
|
||||||
CALL_TYPE_REGISTER_HOLDING,
|
CALL_TYPE_REGISTER_HOLDING,
|
||||||
|
@ -78,6 +79,7 @@ class BasePlatform(Entity):
|
||||||
self._scan_interval = int(entry[CONF_SCAN_INTERVAL])
|
self._scan_interval = int(entry[CONF_SCAN_INTERVAL])
|
||||||
self._call_active = False
|
self._call_active = False
|
||||||
self._cancel_timer: Callable[[], None] | None = None
|
self._cancel_timer: Callable[[], None] | None = None
|
||||||
|
self._cancel_call: Callable[[], None] | None = None
|
||||||
|
|
||||||
self._attr_name = entry[CONF_NAME]
|
self._attr_name = entry[CONF_NAME]
|
||||||
self._attr_should_poll = False
|
self._attr_should_poll = False
|
||||||
|
@ -92,11 +94,11 @@ class BasePlatform(Entity):
|
||||||
"""Virtual function to be overwritten."""
|
"""Virtual function to be overwritten."""
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_remote_start(self) -> None:
|
def async_run(self) -> None:
|
||||||
"""Remote start entity."""
|
"""Remote start entity."""
|
||||||
if self._cancel_timer:
|
self.async_hold(update=False)
|
||||||
self._cancel_timer()
|
if self._scan_interval == 0 or self._scan_interval > ACTIVE_SCAN_INTERVAL:
|
||||||
self._cancel_timer = None
|
self._cancel_call = async_call_later(self.hass, 1, self.async_update)
|
||||||
if self._scan_interval > 0:
|
if self._scan_interval > 0:
|
||||||
self._cancel_timer = async_track_time_interval(
|
self._cancel_timer = async_track_time_interval(
|
||||||
self.hass, self.async_update, timedelta(seconds=self._scan_interval)
|
self.hass, self.async_update, timedelta(seconds=self._scan_interval)
|
||||||
|
@ -105,20 +107,26 @@ class BasePlatform(Entity):
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_remote_stop(self) -> None:
|
def async_hold(self, update=True) -> None:
|
||||||
"""Remote stop entity."""
|
"""Remote stop entity."""
|
||||||
|
if self._cancel_call:
|
||||||
|
self._cancel_call()
|
||||||
|
self._cancel_call = None
|
||||||
if self._cancel_timer:
|
if self._cancel_timer:
|
||||||
self._cancel_timer()
|
self._cancel_timer()
|
||||||
self._cancel_timer = None
|
self._cancel_timer = None
|
||||||
self._attr_available = False
|
if update:
|
||||||
self.async_write_ha_state()
|
self._attr_available = False
|
||||||
|
self.async_write_ha_state()
|
||||||
|
|
||||||
async def async_base_added_to_hass(self):
|
async def async_base_added_to_hass(self):
|
||||||
"""Handle entity which will be added."""
|
"""Handle entity which will be added."""
|
||||||
self.async_remote_start()
|
self.async_run()
|
||||||
async_dispatcher_connect(self.hass, SIGNAL_STOP_ENTITY, self.async_remote_stop)
|
self.async_on_remove(
|
||||||
async_dispatcher_connect(
|
async_dispatcher_connect(self.hass, SIGNAL_STOP_ENTITY, self.async_hold)
|
||||||
self.hass, SIGNAL_START_ENTITY, self.async_remote_start
|
)
|
||||||
|
self.async_on_remove(
|
||||||
|
async_dispatcher_connect(self.hass, SIGNAL_START_ENTITY, self.async_run)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -118,11 +118,11 @@ DEFAULT_HUB = "modbus_hub"
|
||||||
DEFAULT_SCAN_INTERVAL = 15 # seconds
|
DEFAULT_SCAN_INTERVAL = 15 # seconds
|
||||||
DEFAULT_SLAVE = 1
|
DEFAULT_SLAVE = 1
|
||||||
DEFAULT_STRUCTURE_PREFIX = ">f"
|
DEFAULT_STRUCTURE_PREFIX = ">f"
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_TEMP_UNIT = "C"
|
DEFAULT_TEMP_UNIT = "C"
|
||||||
MODBUS_DOMAIN = "modbus"
|
MODBUS_DOMAIN = "modbus"
|
||||||
|
|
||||||
|
ACTIVE_SCAN_INTERVAL = 2 # limit to force an extra update
|
||||||
|
|
||||||
PLATFORMS = (
|
PLATFORMS = (
|
||||||
(BINARY_SENSOR_DOMAIN, CONF_BINARY_SENSORS),
|
(BINARY_SENSOR_DOMAIN, CONF_BINARY_SENSORS),
|
||||||
(CLIMATE_DOMAIN, CONF_CLIMATES),
|
(CLIMATE_DOMAIN, CONF_CLIMATES),
|
||||||
|
|
|
@ -246,6 +246,7 @@ async def test_config_wrong_struct_sensor(hass, error_message, mock_modbus, capl
|
||||||
{
|
{
|
||||||
CONF_NAME: TEST_ENTITY_NAME,
|
CONF_NAME: TEST_ENTITY_NAME,
|
||||||
CONF_ADDRESS: 51,
|
CONF_ADDRESS: 51,
|
||||||
|
CONF_SCAN_INTERVAL: 1,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|
Loading…
Add table
Reference in a new issue