Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Switchbot users to force nightlatch #124326

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion homeassistant/components/switchbot/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@
from .const import (
CONF_ENCRYPTION_KEY,
CONF_KEY_ID,
CONF_LOCK_NIGHTLATCH,
CONF_RETRY_COUNT,
CONNECTABLE_SUPPORTED_MODEL_TYPES,
DEFAULT_LOCK_NIGHTLATCH,
DEFAULT_RETRY_COUNT,
DOMAIN,
NON_CONNECTABLE_SUPPORTED_MODEL_TYPES,
SUPPORTED_LOCK_MODELS,
SUPPORTED_MODEL_TYPES,
SupportedModels,
)

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -355,13 +358,24 @@ async def async_step_init(
# Update common entity options for all other entities.
return self.async_create_entry(title="", data=user_input)

options = {
options: dict[vol.Optional, Any] = {
vol.Optional(
CONF_RETRY_COUNT,
default=self.config_entry.options.get(
CONF_RETRY_COUNT, DEFAULT_RETRY_COUNT
),
): int
}
if self.config_entry.data.get(CONF_SENSOR_TYPE) == SupportedModels.LOCK_PRO:
options.update(
{
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
12 changes: 8 additions & 4 deletions homeassistant/components/switchbot/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import CONF_LOCK_NIGHTLATCH, DEFAULT_LOCK_NIGHTLATCH

Check warning on line 12 in homeassistant/components/switchbot/lock.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/switchbot/lock.py#L12

Added line #L12 was not covered by tests
from .coordinator import SwitchbotConfigEntry, SwitchbotDataUpdateCoordinator
from .entity import SwitchbotEntity

Expand All @@ -19,7 +20,8 @@
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))])

Check warning on line 24 in homeassistant/components/switchbot/lock.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/switchbot/lock.py#L23-L24

Added lines #L23 - L24 were not covered by tests
joostlek marked this conversation as resolved.
Show resolved Hide resolved


# noinspection PyAbstractClass
Expand All @@ -30,11 +32,13 @@
_attr_name = None
_device: switchbot.SwitchbotLock

def __init__(self, coordinator: SwitchbotDataUpdateCoordinator) -> None:
def __init__(

Check warning on line 35 in homeassistant/components/switchbot/lock.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/switchbot/lock.py#L35

Added line #L35 was not covered by tests
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:

Check warning on line 41 in homeassistant/components/switchbot/lock.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/switchbot/lock.py#L41

Added line #L41 was not covered by tests
self._attr_supported_features = LockEntityFeature.OPEN

def _async_update_attrs(self) -> None:
Expand All @@ -55,7 +59,7 @@

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):

Check warning on line 62 in homeassistant/components/switchbot/lock.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/switchbot/lock.py#L62

Added line #L62 was not covered by tests
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
63 changes: 63 additions & 0 deletions tests/components/switchbot/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from homeassistant.components.switchbot.const import (
CONF_ENCRYPTION_KEY,
CONF_KEY_ID,
CONF_LOCK_NIGHTLATCH,
CONF_RETRY_COUNT,
)
from homeassistant.config_entries import SOURCE_BLUETOOTH, SOURCE_USER
Expand Down Expand Up @@ -782,3 +783,65 @@ async def test_options_flow(hass: HomeAssistant) -> None:
assert len(mock_setup_entry.mock_calls) == 1

assert entry.options[CONF_RETRY_COUNT] == 6


async def test_options_flow_lock_pro(hass: HomeAssistant) -> None:
"""Test updating options."""
entry = MockConfigEntry(
domain=DOMAIN,
data={
CONF_ADDRESS: "aa:bb:cc:dd:ee:ff",
CONF_NAME: "test-name",
CONF_PASSWORD: "test-password",
CONF_SENSOR_TYPE: "lock_pro",
},
options={CONF_RETRY_COUNT: 10},
unique_id="aabbccddeeff",
)
entry.add_to_hass(hass)

# Test Force night_latch should be disabled by default.
with patch_async_setup_entry() as mock_setup_entry:
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()

result = await hass.config_entries.options.async_init(entry.entry_id)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "init"
assert result["errors"] is None

result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
CONF_RETRY_COUNT: 3,
},
)
await hass.async_block_till_done()

assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["data"][CONF_LOCK_NIGHTLATCH] is False

assert len(mock_setup_entry.mock_calls) == 1

# Test Set force night_latch to be enabled.

with patch_async_setup_entry() as mock_setup_entry:
result = await hass.config_entries.options.async_init(entry.entry_id)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "init"
assert result["errors"] is None

result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
CONF_LOCK_NIGHTLATCH: True,
},
)
await hass.async_block_till_done()

assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["data"][CONF_LOCK_NIGHTLATCH] is True

assert len(mock_setup_entry.mock_calls) == 0

assert entry.options[CONF_LOCK_NIGHTLATCH] is True