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