Hi, I’m currently working with the new Tuya V2 integration and I’m looking at implementing a Tuya Alarm system into Home Assistant.
I have most the code done but I’m having an issue with the arming of the Alarm, from what I can see neither the alarm_arm_home or alarm_arm_away functions are being executed in the script yet the alarm_arm_disarm works fine.
This is what I know.
The Home Assistant AlarmControlPanelEntity creates correctly and shows correctly in the lovelace webui
When I change the state of the Alarm (e.g. set it active via the Tuya application) the panel alters to show the new state of the alarm.
When I tried to aim the alarm via Home Assistant it will not arm (this is either via the lovelace panel or developers call services), this is the same for the arm away and arm home
However if the alarm is armed then by pressing the disarm button or service it does disarm the alarm.
Extra info:
I have also altered the send command in the alarm_disarm function of the script to send “home” this allowed me to set the alarm as arm away and then click the disarm button on the panel and it went to arm home proving the command “home” was valid.
I have also traced when calling the arm away and arm home in the developers windows and it appears to be calling the correct functions.
I’ve also put debugging code in the arm away and arm home functions and it doesn’t seem to trigger.
This to me seems to show these 2 functions aren’t being executed in the script but HA is requesting them.
At this stage I have no idea why 2 aren’t being called?
These 2 buttons never seem to run the functions in the script
The disarm does disarm the alarm
in this event 1 worked, event 0 didn’t seem to trigger the script
The alarm_control_panel.py
#!/usr/bin/env python3
"""Support for Tuya Alarm Control."""
import logging
from typing import Callable, List
from tuya_iot import TuyaDevice, TuyaDeviceManager
from homeassistant.components.alarm_control_panel import (
DOMAIN as DEVICE_DOMAIN,
SUPPORT_ALARM_TRIGGER,
AlarmControlPanelEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import STATE_ALARM_DISARMED, STATE_ALARM_ARMED_AWAY, STATE_ALARM_ARMING, STATE_ALARM_TRIGGERED, STATE_UNKNOWN, STATE_ALARM_ARMED_HOME
from homeassistant.core import HomeAssistant
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from .base import TuyaHaDevice
from .const import (
DOMAIN,
TUYA_DEVICE_MANAGER,
TUYA_DISCOVERY_NEW,
TUYA_HA_DEVICES,
TUYA_HA_TUYA_MAP,
)
_LOGGER = logging.getLogger(__name__)
TUYA_SUPPORT_TYPE = [
"mal", # Home Security System
]
DPCODE_MASTER_MODE = "master_mode"
async def async_setup_entry(
hass: HomeAssistant, _entry: ConfigEntry, async_add_entities
):
"""Set up tuya alarm dynamically through tuya discovery."""
_LOGGER.info("alarm init")
hass.data[DOMAIN][TUYA_HA_TUYA_MAP].update({DEVICE_DOMAIN: TUYA_SUPPORT_TYPE})
async def async_discover_device(dev_ids):
"""Discover and add a discovered tuya sensor."""
_LOGGER.info("alarm add->", dev_ids)
if not dev_ids:
return
entities = await hass.async_add_executor_job(_setup_entities, hass, dev_ids)
hass.data[DOMAIN][TUYA_HA_DEVICES].extend(entities)
async_add_entities(entities)
async_dispatcher_connect(
hass, TUYA_DISCOVERY_NEW.format(DEVICE_DOMAIN), async_discover_device
)
device_manager = hass.data[DOMAIN][TUYA_DEVICE_MANAGER]
device_ids = []
for (device_id, device) in device_manager.device_map.items():
if device.category in TUYA_SUPPORT_TYPE:
device_ids.append(device_id)
await async_discover_device(device_ids)
def _setup_entities(hass, device_ids: List):
"""Set up Tuya Switch device."""
device_manager = hass.data[DOMAIN][TUYA_DEVICE_MANAGER]
entities = []
for device_id in device_ids:
device = device_manager.device_map[device_id]
if device is None:
continue
if DPCODE_MASTER_MODE in device.status:
entities.append(
TuyaHaAlarm(
device,
device_manager,
(
lambda d: STATE_ALARM_DISARMED
if d.status.get(DPCODE_MASTER_MODE, 1) == "disarmed"
else STATE_ALARM_ARMED_HOME if d.status.get(DPCODE_MASTER_MODE, 1) == "home"
else STATE_ALARM_ARMED_AWAY if d.status.get(DPCODE_MASTER_MODE, 1) == "arm"
else STATE_UNKNOWN
),
DPCODE_MASTER_MODE,
)
)
return entities
class TuyaHaAlarm(TuyaHaDevice, AlarmControlPanelEntity):
"""Tuya Alarm Device."""
def __init__(
self,
device: TuyaDevice,
device_manager: TuyaDeviceManager,
sensor_is_on: Callable[..., str],
dp_code: str = "",
):
"""Init TuyaHaAlarm."""
super().__init__(device, device_manager)
self._is_on = sensor_is_on
self.dp_code = dp_code
@property
def state(self):
"""Return is alarm on."""
return self._is_on(self.tuya_device)
@property
def code_arm_required(self):
"""Return the polling state."""
return False
@property
def supported_features(self) -> int:
"""Return the list of supported features."""
return SUPPORT_ALARM_TRIGGER
def alarm_arm_home(self, code=None) -> None:
self._send_command([{"code": self.dp_code, "value": "home"}])
def alarm_arm_away(self, code=None) -> None:
self._send_command([{"code": self.dp_code, "value": "arm"}])
def alarm_disarm(self, code=None) -> None:
self._send_command([{"code": self.dp_code, "value": "disarmed"}])
Any idea why alarm_arm_home and alarm_arm_away arn’t being called in this script?