Skip to content

Commit

Permalink
Retry syncing lock after failure
Browse files Browse the repository at this point in the history
  • Loading branch information
raman325 committed Apr 1, 2024
1 parent 87d8ecf commit 81d48a7
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions custom_components/lock_code_manager/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""Sensor for lock_code_manager."""
"""Binary sensor for lock_code_manager."""

from __future__ import annotations

import asyncio
from datetime import datetime
from datetime import datetime, timedelta
import logging

from homeassistant.components.binary_sensor import (
Expand Down Expand Up @@ -46,6 +46,7 @@
from .providers import BaseLock

_LOGGER = logging.getLogger(__name__)
SCAN_INTERVAL = timedelta(seconds=30)


async def async_setup_entry(
Expand Down Expand Up @@ -258,13 +259,14 @@ def __init__(

key_name = ATTR_IN_SYNC.replace("_", " ")
self._attr_name: str | None = f"{super()._attr_name} {key_name}"
self._attr_should_poll = True

@property
def icon(self) -> str | None:
"""Return icon."""
if self.is_on:
return "mdi:sync"
return "mdi:sync-"
return "mdi:sync-off"

@property
def available(self) -> bool:
Expand All @@ -273,6 +275,22 @@ def available(self) -> bool:
int(self.slot_num) in self.coordinator.data
)

async def async_update(self) -> None:
"""Update entity."""
if (
self._lock
and not self._lock.locked()
and self.is_on is False
and (state := self.hass.states.get(self.lock.lock.entity_id))
and state.state not in (STATE_UNAVAILABLE, STATE_UNKNOWN)
):
_LOGGER.error(

Check warning on line 287 in custom_components/lock_code_manager/binary_sensor.py

View check run for this annotation

Codecov / codecov/patch

custom_components/lock_code_manager/binary_sensor.py#L287

Added line #L287 was not covered by tests
"Updating %s code slot %s because it is out of sync",
self.lock.lock.entity_id,
self.slot_num,
)
await self._update_state()

Check warning on line 292 in custom_components/lock_code_manager/binary_sensor.py

View check run for this annotation

Codecov / codecov/patch

custom_components/lock_code_manager/binary_sensor.py#L292

Added line #L292 was not covered by tests

def _get_entity_state(self, key: str) -> str | None:
"""Get entity state."""
if (state := self.hass.states.get(self._entity_id_map[key])) is None:
Expand Down

0 comments on commit 81d48a7

Please sign in to comment.