rospogrigio / localtuya

local handling for Tuya devices
GNU General Public License v3.0
2.71k stars 530 forks source link

localtuya passive device? #828

Open rohbags opened 2 years ago

rohbags commented 2 years ago

Any plans to re-add passive device support? I have a number of tuya-based door/window sensors that will turn off 3 seconds after sending an update to HA, as well as motion sensors that act the same way. For the last couple of years I have been manually changing the localtuya code to support this, but after updating to 3.5 from 3.2 today there are significant changes. Instead of re-writing everything again, can we add this to the main repo?

Previously in common.py I have this in class LocalTuyaEntity:

` async def async_added_to_hass(self): """Subscribe localtuya events.""" await super().async_added_to_hass()

    self.debug("Adding %s with configuration: %s", self.entity_id, self._config)

    state = await self.async_get_last_state()
    if state:
        self.status_restored(state)

    def _update_handler(status):
        """Update entity state when status was updated."""
        if status is not None:
            self._status = status
            self._last_seen = None
            self.status_updated()
        elif self._config_entry.data.get(CONF_PASSIVE_DEVICE):
            _LOGGER.debug("Passive device disconnected, keeping old state")
            self._last_seen = datetime.now()
        else:
            self._status = {}
            self._last_seen = None

        self.schedule_update_ha_state()

    signal = f"localtuya_{self._config_entry.data[CONF_DEVICE_ID]}"
    self.async_on_remove(
        async_dispatcher_connect(self.hass, signal, _update_handler)
    )

    state = await self.async_get_last_state()
    if not (state or self._config_entry.data.get(CONF_PASSIVE_DEVICE)):
        return

    last_seen = state.attributes.get(ATTR_LAST_SEEN)
    if last_seen:
        self._last_seen = datetime.fromisoformat(last_seen)
    self._status = state.attributes.get(ATTR_OLD_STATE, {})
    self.status_updated()`

In 3.5 I have started with this, but have not added the section after async_dispatcher_connect(...) because the flow of the first part is not right:

` async def async_added_to_hass(self): """Subscribe localtuya events.""" await super().async_added_to_hass()

    self.debug("Adding %s with configuration: %s", self.entity_id, self._config)

    state = await self.async_get_last_state()
    if state:
        self.status_restored(state)

    def _update_handler(status):
        """Update entity state when status was updated."""
        ## mod
        if self._config_entry.data.get(CONF_PASSIVE_DEVICE):
            _LOGGER.debug("Passive device disconnected, keeping old state")
            self._last_seen = datetime.now()
        elif status is None:
            status = {}
        if self._status != status:
            self._status = status.copy()
            if status:
                self.status_updated()
            self.schedule_update_ha_state()

    signal = f"localtuya_{self._config_entry.data[CONF_DEVICE_ID]}"

    self.async_on_remove(
        async_dispatcher_connect(self.hass, signal, _update_handler)
    )

    signal = f"localtuya_entity_{self._config_entry.data[CONF_DEVICE_ID]}"
    async_dispatcher_send(self.hass, signal, self.entity_id)

`

In config_flow.py I have also added vol.Required(CONF_PASSIVE_DEVICE, default=False): bool, to BASIC_INFO_SCHEMA, DEVICE_SCHEMA and options_schema ... return vol.Schema(){ ... }) as well as CONF_DEVICE_PASSIVE to .const import, and the necessary changes to init.py

Why was passive device support removed at all?

nprez83 commented 2 years ago

I haven't dug into the code at all, but for what is worth, I currently have pir motion sensors and door contact sensors working with localtuya 4.0.0. Took a bit of trial and error to pair them but got them to work in the end, simply by making sure I was triggering them as I was running the config flow. (https://github.com/rospogrigio/localtuya/issues/770).

The problem I'm having right now with the motion sensors is they're taking considerably longer to update using localtuya (5-6 secs) then via the cloud (1-3 secs), so would love to see this improved. That being said, I otherwise love this integration, so kudos to all the developers behind it. It's made my lights and switches respond significantly faster than before, almost instantaneously tbh, which makes it all the more confusing to understand why it's the opposite with the motion detectors.