Skip to content

Commit

Permalink
Add option to force nightlatch operation mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal4K committed Aug 20, 2024
1 parent 1d1a6ee commit f6fa796
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
10 changes: 9 additions & 1 deletion homeassistant/components/switchbot/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@
CONF_ENCRYPTION_KEY,
CONF_KEY_ID,
CONF_RETRY_COUNT,
CONF_LOCK_NIGHTLATCH,
CONNECTABLE_SUPPORTED_MODEL_TYPES,
DEFAULT_RETRY_COUNT,
DEFAULT_LOCK_NIGHTLATCH,
DOMAIN,
NON_CONNECTABLE_SUPPORTED_MODEL_TYPES,
SUPPORTED_LOCK_MODELS,
Expand Down Expand Up @@ -361,7 +363,13 @@ async def async_step_init(
default=self.config_entry.options.get(
CONF_RETRY_COUNT, DEFAULT_RETRY_COUNT
),
): int
): int,
vol.Optional(
CONF_LOCK_NIGHTLATCH,
default=self.config_entry.options.get(
CONF_LOCK_NIGHTLATCH, DEFAULT_LOCK_NIGHTLATCH
),
): bool
}

return self.async_show_form(step_id="init", data_schema=vol.Schema(options))
2 changes: 2 additions & 0 deletions homeassistant/components/switchbot/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@ class SupportedModels(StrEnum):

# Config Defaults
DEFAULT_RETRY_COUNT = 3
DEFAULT_LOCK_NIGHTLATCH = False

# Config Options
CONF_RETRY_COUNT = "retry_count"
CONF_KEY_ID = "key_id"
CONF_ENCRYPTION_KEY = "encryption_key"
CONF_LOCK_NIGHTLATCH = "lock_force_nightlatch"

# Deprecated config Entry Options to be removed in 2023.4
CONF_TIME_BETWEEN_UPDATE_COMMAND = "update_time"
Expand Down
14 changes: 10 additions & 4 deletions homeassistant/components/switchbot/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@
from .coordinator import SwitchbotConfigEntry, SwitchbotDataUpdateCoordinator
from .entity import SwitchbotEntity

from .const import (
CONF_LOCK_NIGHTLATCH,
DEFAULT_LOCK_NIGHTLATCH,
)


async def async_setup_entry(
hass: HomeAssistant,
entry: SwitchbotConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up Switchbot lock based on a config entry."""
async_add_entities([(SwitchBotLock(entry.runtime_data))])
force_nightlatch = entry.options.get(CONF_LOCK_NIGHTLATCH, DEFAULT_LOCK_NIGHTLATCH)
async_add_entities([(SwitchBotLock(entry.runtime_data, force_nightlatch))])


# noinspection PyAbstractClass
Expand All @@ -30,11 +36,11 @@ class SwitchBotLock(SwitchbotEntity, LockEntity):
_attr_name = None
_device: switchbot.SwitchbotLock

def __init__(self, coordinator: SwitchbotDataUpdateCoordinator) -> None:
def __init__(self, coordinator: SwitchbotDataUpdateCoordinator, force_nightlatch) -> None:
"""Initialize the entity."""
super().__init__(coordinator)
self._async_update_attrs()
if self._device.is_night_latch_enabled():
if self._device.is_night_latch_enabled() or force_nightlatch:
self._attr_supported_features = LockEntityFeature.OPEN

def _async_update_attrs(self) -> None:
Expand All @@ -55,7 +61,7 @@ async def async_lock(self, **kwargs: Any) -> None:

async def async_unlock(self, **kwargs: Any) -> None:
"""Unlock the lock."""
if self._device.is_night_latch_enabled():
if self._attr_supported_features & (LockEntityFeature.OPEN):
self._last_run_success = await self._device.unlock_without_unlatch()
else:
self._last_run_success = await self._device.unlock()
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/switchbot/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
"step": {
"init": {
"data": {
"retry_count": "Retry count"
"retry_count": "Retry count",
"lock_force_nightlatch": "Force Nightlatch operation mode"
}
}
}
Expand Down

0 comments on commit f6fa796

Please sign in to comment.