home-assistant/core · warning · ServiceValidationError

code_arm_required

code_arm_required

Error message

code_arm_required

What it means

ServiceValidationError with translation_key 'code_arm_required' raised by AlarmControlPanelEntity.check_code_arm_required when the provided code is empty/falsy after applying code_or_default_code and the entity's code_arm_required attribute is True. It aborts arm actions (arm_away, arm_home, arm_vacation, arm_night, arm_custom_bypass) before any device call, telling the user a code must be supplied.

Source

Thrown at homeassistant/components/alarm_control_panel/__init__.py:198

        """Code format or None if no code is required."""
        return self._attr_code_format

    @cached_property
    def changed_by(self) -> str | None:
        """Last change triggered by."""
        return self._attr_changed_by

    @cached_property
    def code_arm_required(self) -> bool:
        """Whether the code is required for arm actions."""
        return self._attr_code_arm_required

    @final
    @callback
    def check_code_arm_required(self, code: str | None) -> str | None:
        """Check if arm code is required, raise if no code is given."""
        if not (_code := self.code_or_default_code(code)) and self.code_arm_required:
            raise ServiceValidationError(
                translation_domain=DOMAIN,
                translation_key="code_arm_required",
                translation_placeholders={
                    "entity_id": self.entity_id,
                },
            )
        return _code

    @final
    async def async_handle_alarm_disarm(self, code: str | None = None) -> None:
        """Add default code and disarm."""
        await self.async_alarm_disarm(self.code_or_default_code(code))

    def alarm_disarm(self, code: str | None = None) -> None:
        """Send disarm command."""
        raise NotImplementedError

    async def async_alarm_disarm(self, code: str | None = None) -> None:

View on GitHub (pinned to 58a3fdb3ea)

Solutions

  1. Pass a valid code in the service call data.
  2. If arming without a code is desired, set code_arm_required: false on the entity (or remove the configured code).
  3. Configure a default code on the entity if automations should use it implicitly.
  4. In custom integrations, ensure code_or_default_code and code_arm_required stay consistent.

Example fix

# before
action:
  - service: alarm_control_panel.alarm_arm_away
    target:
      entity_id: alarm_control_panel.home

# after
action:
  - service: alarm_control_panel.alarm_arm_away
    target:
      entity_id: alarm_control_panel.home
    data:
      code: "1234"
Defensive patterns

Strategy: validation

Validate before calling

# Before arming, ensure a code is available when required
if entity.code_arm_required and not entity.code_or_default_code(None):
    # supply code in the service call or set a default code
    ...

Type guard

def can_arm_without_code(entity) -> bool:
    return not entity.code_arm_required or bool(entity.code_or_default_code(None))

Try / catch

from homeassistant.exceptions import ServiceValidationError

try:
    await hass.services.async_call(
        "alarm_control_panel", "alarm_arm_away",
        {"entity_id": "alarm_control_panel.home", "code": "1234"},
    )
except ServiceValidationError as err:
    # translation_key 'code_arm_required': prompt for code
    ...

Prevention

When it happens

Trigger: Calling alarm_control_panel.arm_* without a code while the panel entity sets _attr_code_arm_required = True (default) and has a code or default_code configured. disarm is unaffected (it uses code_or_default_code without this gate).

Common situations: Automations or scripts calling arm actions without passing a code; UI automations missing the code field; custom panels overriding code_arm_required incorrectly; YAML/scripts created before a code was added to the panel.

Related errors


AI-assisted analysis of home-assistant/core@58a3fdb3ea (2026-08-14). Data as JSON: /api/errors/924adc8321c2c5bf. Report an issue: GitHub.