Skip to content

Commit

Permalink
Finalized to reinstate old behaviour for code prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Petersson committed Nov 5, 2024
1 parent 47c733c commit 9416d55
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
18 changes: 5 additions & 13 deletions custom_components/sector/alarm_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,29 +85,21 @@ def state(self):
)
return mapped_state

async def async_alarm_arm_away(self, **kwargs):
async def async_alarm_arm_away(self, code=None):
"""Send arm away command."""
code = kwargs.get("code")
is_valid = self._is_valid_code(code)
_LOGGER.debug("Arm away requested. Code: %s, Is valid: %s", code, is_valid)
if not is_valid:
raise HomeAssistantError("Code required to arm the system.")
_LOGGER.debug("Arm away requested. Code: %s", code)
success = await self.coordinator.api.arm_system("total", code=code)
if success:
await self.coordinator.async_request_refresh()

async def async_alarm_arm_home(self, **kwargs):
async def async_alarm_arm_home(self, code=None):
"""Send arm home command."""
code = kwargs.get("code")
is_valid = self._is_valid_code(code)
_LOGGER.debug("Arm home requested. Code: %s, Is valid: %s", code, is_valid)
if not is_valid:
raise HomeAssistantError("Code required to arm the system.")
_LOGGER.debug("Arm home requested. Code: %s", code)
success = await self.coordinator.api.arm_system("partial", code=code)
if success:
await self.coordinator.async_request_refresh()

async def async_alarm_disarm(self, **kwargs):
async def async_alarm_disarm(self, code=None):
"""Send disarm command."""
code = kwargs.get("code")
is_valid = self._is_valid_code(code)
Expand Down
41 changes: 30 additions & 11 deletions custom_components/sector/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import logging

from homeassistant.components.lock import LockEntity
from homeassistant.components.lock import LockEntity, LockEntityDescription
from homeassistant.core import HomeAssistant
from homeassistant.const import ATTR_CODE
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.exceptions import HomeAssistantError

from .coordinator import SectorAlarmConfigEntry, SectorDataUpdateCoordinator
from .entity import SectorAlarmBaseEntity
from .const import CONF_CODE_FORMAT

_LOGGER = logging.getLogger(__name__)

Expand All @@ -21,12 +22,19 @@ async def async_setup_entry(
):
"""Set up Sector Alarm locks."""
coordinator = entry.runtime_data
code_format = entry.options.get(CONF_CODE_FORMAT, 6)
devices = coordinator.data.get("devices", {})
entities = []

for device in devices.values():
if device.get("model") == "Smart Lock":
entities.append(SectorAlarmLock(coordinator, device))
serial_no = device["serial_no"]
description = LockEntityDescription(
key=serial_no,
name=f"Sector {device.get('name', 'Lock')} {serial_no}",
)
entities.append(SectorAlarmLock(coordinator, code_format, description, serial_no))


if entities:
async_add_entities(entities)
Expand All @@ -38,14 +46,29 @@ class SectorAlarmLock(SectorAlarmBaseEntity, LockEntity):
"""Representation of a Sector Alarm lock."""

_attr_name = None
_attr_code_required = True

def __init__(self, coordinator: SectorDataUpdateCoordinator, device_info: dict):
def __init__(
self,
coordinator: SectorDataUpdateCoordinator,
code_format: int,
description: LockEntityDescription,
serial_no: str
):
"""Initialize the lock."""
super().__init__(
coordinator, device_info["serial_no"], device_info, device_info["model"]
coordinator,
serial_no,
{"name": description.name},
"Lock"
)
self._attr_unique_id = f"{self._serial_no}_lock"
self._attr_code_format = rf"^\d{{{code_format}}}$"
self._attr_code_required = False

@property
def code_format(self):
"""Return the numeric code format if a code is required."""
return self._attr_code_format if self._attr_code_required else None

@property
def is_locked(self):
Expand All @@ -59,12 +82,8 @@ def is_locked(self):
async def async_lock(self, **kwargs):
"""Lock the device."""
code = kwargs.get(ATTR_CODE)
is_valid = self._is_valid_code(code)
_LOGGER.debug(
"Lock requested for lock %s. Code: %s, Is valid: %s", self._serial_no, code, is_valid
)
if self._attr_code_required and not is_valid:
raise HomeAssistantError("Code required to lock the system.")
self._attr_code_required = False
_LOGGER.debug("Lock requested for lock %s. Code: %s", self._serial_no, code)
success = await self.coordinator.api.lock_door(self._serial_no, code=code)
if success:
await self.coordinator.async_request_refresh()
Expand Down

0 comments on commit 9416d55

Please sign in to comment.