Skip to content

Commit

Permalink
feat(service): add update_state service to force alarm updates (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
palazzem authored May 31, 2024
1 parent 1cb3dae commit d334560
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions custom_components/econnect_metronet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ async def async_setup_entry(hass: HomeAssistant, config: ConfigEntry) -> bool:
# as we need both to access the integration configurations.
hass.services.async_register(DOMAIN, "arm_sectors", partial(services.arm_sectors, hass, config.entry_id))
hass.services.async_register(DOMAIN, "disarm_sectors", partial(services.disarm_sectors, hass, config.entry_id))
hass.services.async_register(DOMAIN, "update_state", partial(services.update_state, hass, config.entry_id))

for component in PLATFORMS:
hass.async_create_task(hass.config_entries.async_forward_entry_setup(config, component))
Expand Down
3 changes: 2 additions & 1 deletion custom_components/econnect_metronet/icons.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"services": {
"arm_sectors": "mdi:shield-lock",
"disarm_sectors": "mdi:shield-off"
"disarm_sectors": "mdi:shield-off",
"update_state": "mdi:update"
}
}
10 changes: 9 additions & 1 deletion custom_components/econnect_metronet/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from homeassistant.core import HomeAssistant, ServiceCall

from .const import DOMAIN, KEY_DEVICE
from .const import DOMAIN, KEY_COORDINATOR, KEY_DEVICE
from .decorators import retry_refresh_token_service

_LOGGER = logging.getLogger(__name__)
Expand All @@ -26,3 +26,11 @@ async def disarm_sectors(hass: HomeAssistant, config_id: str, call: ServiceCall)
code = call.data.get("code")
_LOGGER.debug(f"Service | Disarming sectors: {sectors}")
await hass.async_add_executor_job(device.disarm, code, sectors)


@retry_refresh_token_service
async def update_state(hass: HomeAssistant, config_id: str, call: ServiceCall):
_LOGGER.debug(f"Service | Triggered action {call.service}")
coordinator = hass.data[DOMAIN][config_id][KEY_COORDINATOR]
_LOGGER.debug("Service | Updating alarm state...")
await coordinator.async_refresh()
4 changes: 4 additions & 0 deletions custom_components/econnect_metronet/services.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Describes the format for available e-Connect services

update_state:
name: Update Alarm State
description: Force an update of the alarm areas and inputs.

arm_sectors:
name: Arm Sectors
description: Arm one or multiple sectors.
Expand Down
18 changes: 18 additions & 0 deletions tests/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,21 @@ async def test_service_disarm_sectors(hass, config_entry, alarm_device, coordina
assert disarm.call_count == 1
assert disarm.call_args[0][0] == "1234"
assert disarm.call_args[0][1] == [1, 3]


async def test_service_update_state(hass, config_entry, alarm_device, coordinator, mocker):
# Ensure `update_state` triggers a full refresh
update = mocker.patch.object(alarm_device, "update")
hass.data[DOMAIN][config_entry.entry_id] = {
"device": alarm_device,
"coordinator": coordinator,
}
call = ServiceCall(
domain=DOMAIN,
service="update_state",
data={},
)
# Test
await services.update_state(hass, config_entry.entry_id, call)
assert update.call_count == 1
assert update.call_args == ()

0 comments on commit d334560

Please sign in to comment.