2019-04-03 15:40:03 +00:00
|
|
|
"""Support for NX584 alarm control panels."""
|
2022-01-03 09:57:25 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-06-09 19:02:08 +00:00
|
|
|
from datetime import timedelta
|
2016-02-05 23:11:47 +00:00
|
|
|
import logging
|
2016-02-19 05:27:50 +00:00
|
|
|
|
2019-12-06 08:08:54 +00:00
|
|
|
from nx584 import client
|
2016-02-05 23:11:47 +00:00
|
|
|
import requests
|
2016-09-08 21:06:57 +00:00
|
|
|
import voluptuous as vol
|
2016-02-05 23:11:47 +00:00
|
|
|
|
|
|
|
import homeassistant.components.alarm_control_panel as alarm
|
2021-05-23 15:51:40 +00:00
|
|
|
from homeassistant.components.alarm_control_panel import (
|
|
|
|
PLATFORM_SCHEMA as PARENT_PLATFORM_SCHEMA,
|
2022-04-07 07:34:29 +00:00
|
|
|
AlarmControlPanelEntityFeature,
|
2019-11-25 23:42:53 +00:00
|
|
|
)
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_HOST,
|
|
|
|
CONF_NAME,
|
|
|
|
CONF_PORT,
|
|
|
|
STATE_ALARM_ARMED_AWAY,
|
|
|
|
STATE_ALARM_ARMED_HOME,
|
|
|
|
STATE_ALARM_DISARMED,
|
|
|
|
STATE_ALARM_TRIGGERED,
|
|
|
|
)
|
2022-01-03 09:57:25 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2020-06-08 13:55:50 +00:00
|
|
|
from homeassistant.exceptions import PlatformNotReady
|
|
|
|
from homeassistant.helpers import config_validation as cv, entity_platform
|
2022-01-03 09:57:25 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2016-02-05 23:11:47 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2020-06-09 19:02:08 +00:00
|
|
|
SCAN_INTERVAL = timedelta(seconds=10)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_HOST = "localhost"
|
|
|
|
DEFAULT_NAME = "NX584"
|
2016-09-08 21:06:57 +00:00
|
|
|
DEFAULT_PORT = 5007
|
2020-06-08 13:55:50 +00:00
|
|
|
SERVICE_BYPASS_ZONE = "bypass_zone"
|
|
|
|
SERVICE_UNBYPASS_ZONE = "unbypass_zone"
|
|
|
|
ATTR_ZONE = "zone"
|
2016-09-08 21:06:57 +00:00
|
|
|
|
2021-05-23 15:51:40 +00:00
|
|
|
PLATFORM_SCHEMA = PARENT_PLATFORM_SCHEMA.extend(
|
2019-07-31 19:25:30 +00:00
|
|
|
{
|
|
|
|
vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
|
|
|
}
|
|
|
|
)
|
2016-09-08 21:06:57 +00:00
|
|
|
|
2016-02-05 23:11:47 +00:00
|
|
|
|
2022-01-03 09:57:25 +00:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Set up the NX584 platform."""
|
2022-06-28 11:42:43 +00:00
|
|
|
name: str = config[CONF_NAME]
|
|
|
|
host: str = config[CONF_HOST]
|
|
|
|
port: int = config[CONF_PORT]
|
2016-09-08 21:06:57 +00:00
|
|
|
|
2019-09-03 18:35:00 +00:00
|
|
|
url = f"http://{host}:{port}"
|
2016-02-05 23:11:47 +00:00
|
|
|
|
|
|
|
try:
|
2020-06-08 13:55:50 +00:00
|
|
|
alarm_client = client.Client(url)
|
|
|
|
await hass.async_add_executor_job(alarm_client.list_zones)
|
2016-02-05 23:11:47 +00:00
|
|
|
except requests.exceptions.ConnectionError as ex:
|
2020-06-08 13:55:50 +00:00
|
|
|
_LOGGER.error(
|
2020-08-27 11:56:20 +00:00
|
|
|
"Unable to connect to %(host)s: %(reason)s",
|
2020-10-06 13:02:23 +00:00
|
|
|
{"host": url, "reason": ex},
|
2020-06-08 13:55:50 +00:00
|
|
|
)
|
2020-08-28 11:50:32 +00:00
|
|
|
raise PlatformNotReady from ex
|
2020-06-08 13:55:50 +00:00
|
|
|
|
|
|
|
entity = NX584Alarm(name, alarm_client, url)
|
|
|
|
async_add_entities([entity])
|
|
|
|
|
2021-05-03 16:34:28 +00:00
|
|
|
platform = entity_platform.async_get_current_platform()
|
2020-06-08 13:55:50 +00:00
|
|
|
|
|
|
|
platform.async_register_entity_service(
|
2020-08-27 11:56:20 +00:00
|
|
|
SERVICE_BYPASS_ZONE,
|
|
|
|
{vol.Required(ATTR_ZONE): cv.positive_int},
|
|
|
|
"alarm_bypass",
|
2020-06-08 13:55:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
platform.async_register_entity_service(
|
|
|
|
SERVICE_UNBYPASS_ZONE,
|
|
|
|
{vol.Required(ATTR_ZONE): cv.positive_int},
|
|
|
|
"alarm_unbypass",
|
|
|
|
)
|
2016-02-05 23:11:47 +00:00
|
|
|
|
|
|
|
|
2020-04-25 16:05:28 +00:00
|
|
|
class NX584Alarm(alarm.AlarmControlPanelEntity):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Representation of a NX584-based alarm panel."""
|
2016-03-07 19:21:43 +00:00
|
|
|
|
2022-06-28 11:42:43 +00:00
|
|
|
_attr_code_format = alarm.CodeFormat.NUMBER
|
|
|
|
_attr_state: str | None
|
2022-04-07 07:34:29 +00:00
|
|
|
_attr_supported_features = (
|
|
|
|
AlarmControlPanelEntityFeature.ARM_HOME
|
|
|
|
| AlarmControlPanelEntityFeature.ARM_AWAY
|
|
|
|
)
|
|
|
|
|
2022-06-28 11:42:43 +00:00
|
|
|
def __init__(self, name: str, alarm_client: client.Client, url: str) -> None:
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Init the nx584 alarm panel."""
|
2022-06-28 11:42:43 +00:00
|
|
|
self._attr_name = name
|
2020-06-08 13:55:50 +00:00
|
|
|
self._alarm = alarm_client
|
|
|
|
self._url = url
|
2016-02-05 23:11:47 +00:00
|
|
|
|
2022-06-28 08:00:23 +00:00
|
|
|
def update(self) -> None:
|
2016-10-21 05:35:25 +00:00
|
|
|
"""Process new events from panel."""
|
2016-02-05 23:11:47 +00:00
|
|
|
try:
|
|
|
|
part = self._alarm.list_partitions()[0]
|
|
|
|
zones = self._alarm.list_zones()
|
|
|
|
except requests.exceptions.ConnectionError as ex:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error(
|
|
|
|
"Unable to connect to %(host)s: %(reason)s",
|
2020-10-06 13:02:23 +00:00
|
|
|
{"host": self._url, "reason": ex},
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2022-06-28 11:42:43 +00:00
|
|
|
self._attr_state = None
|
2017-01-03 07:19:33 +00:00
|
|
|
zones = []
|
2016-02-05 23:11:47 +00:00
|
|
|
except IndexError:
|
2018-01-21 06:35:38 +00:00
|
|
|
_LOGGER.error("NX584 reports no partitions")
|
2022-06-28 11:42:43 +00:00
|
|
|
self._attr_state = None
|
2017-01-03 07:19:33 +00:00
|
|
|
zones = []
|
2016-02-05 23:11:47 +00:00
|
|
|
|
|
|
|
bypassed = False
|
|
|
|
for zone in zones:
|
2019-07-31 19:25:30 +00:00
|
|
|
if zone["bypassed"]:
|
|
|
|
_LOGGER.debug(
|
|
|
|
"Zone %(zone)s is bypassed, assuming HOME",
|
2020-10-06 13:02:23 +00:00
|
|
|
{"zone": zone["number"]},
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2016-02-05 23:11:47 +00:00
|
|
|
bypassed = True
|
|
|
|
break
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if not part["armed"]:
|
2022-06-28 11:42:43 +00:00
|
|
|
self._attr_state = STATE_ALARM_DISARMED
|
2016-02-05 23:11:47 +00:00
|
|
|
elif bypassed:
|
2022-06-28 11:42:43 +00:00
|
|
|
self._attr_state = STATE_ALARM_ARMED_HOME
|
2016-02-05 23:11:47 +00:00
|
|
|
else:
|
2022-06-28 11:42:43 +00:00
|
|
|
self._attr_state = STATE_ALARM_ARMED_AWAY
|
2016-02-05 23:11:47 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
for flag in part["condition_flags"]:
|
2018-12-25 15:29:53 +00:00
|
|
|
if flag == "Siren on":
|
2022-06-28 11:42:43 +00:00
|
|
|
self._attr_state = STATE_ALARM_TRIGGERED
|
2018-12-25 15:29:53 +00:00
|
|
|
|
2022-06-28 08:00:23 +00:00
|
|
|
def alarm_disarm(self, code: str | None = None) -> None:
|
2016-03-07 15:45:21 +00:00
|
|
|
"""Send disarm command."""
|
2016-02-05 23:11:47 +00:00
|
|
|
self._alarm.disarm(code)
|
|
|
|
|
2022-06-28 08:00:23 +00:00
|
|
|
def alarm_arm_home(self, code: str | None = None) -> None:
|
2016-03-07 15:45:21 +00:00
|
|
|
"""Send arm home command."""
|
2019-07-31 19:25:30 +00:00
|
|
|
self._alarm.arm("stay")
|
2016-02-05 23:11:47 +00:00
|
|
|
|
2022-06-28 08:00:23 +00:00
|
|
|
def alarm_arm_away(self, code: str | None = None) -> None:
|
2016-03-07 15:45:21 +00:00
|
|
|
"""Send arm away command."""
|
2019-07-31 19:25:30 +00:00
|
|
|
self._alarm.arm("exit")
|
2020-06-08 13:55:50 +00:00
|
|
|
|
2022-06-28 11:42:43 +00:00
|
|
|
def alarm_bypass(self, zone: int) -> None:
|
2020-06-08 13:55:50 +00:00
|
|
|
"""Send bypass command."""
|
|
|
|
self._alarm.set_bypass(zone, True)
|
|
|
|
|
2022-06-28 11:42:43 +00:00
|
|
|
def alarm_unbypass(self, zone: int) -> None:
|
2020-06-08 13:55:50 +00:00
|
|
|
"""Send bypass command."""
|
|
|
|
self._alarm.set_bypass(zone, False)
|