2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Canary alarm."""
|
2021-03-17 22:43:55 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-05-22 08:14:59 +00:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from canary.api import (
|
|
|
|
LOCATION_MODE_AWAY,
|
|
|
|
LOCATION_MODE_HOME,
|
|
|
|
LOCATION_MODE_NIGHT,
|
|
|
|
Location,
|
|
|
|
)
|
2019-10-18 23:01:59 +00:00
|
|
|
|
2020-04-25 16:05:28 +00:00
|
|
|
from homeassistant.components.alarm_control_panel import AlarmControlPanelEntity
|
2019-11-25 23:42:53 +00:00
|
|
|
from homeassistant.components.alarm_control_panel.const import (
|
|
|
|
SUPPORT_ALARM_ARM_AWAY,
|
|
|
|
SUPPORT_ALARM_ARM_HOME,
|
|
|
|
SUPPORT_ALARM_ARM_NIGHT,
|
|
|
|
)
|
2020-09-19 04:22:19 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2019-03-21 05:56:46 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
STATE_ALARM_ARMED_AWAY,
|
|
|
|
STATE_ALARM_ARMED_HOME,
|
|
|
|
STATE_ALARM_ARMED_NIGHT,
|
|
|
|
STATE_ALARM_DISARMED,
|
|
|
|
)
|
2021-04-17 10:48:03 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-05-04 21:36:48 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-10-01 08:26:26 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2020-10-01 08:26:26 +00:00
|
|
|
from .const import DATA_COORDINATOR, DOMAIN
|
|
|
|
from .coordinator import CanaryDataUpdateCoordinator
|
2017-12-08 09:40:45 +00:00
|
|
|
|
|
|
|
|
2020-09-19 04:22:19 +00:00
|
|
|
async def async_setup_entry(
|
2021-04-17 10:48:03 +00:00
|
|
|
hass: HomeAssistant,
|
2020-09-19 04:22:19 +00:00
|
|
|
entry: ConfigEntry,
|
2021-05-04 21:36:48 +00:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
2020-09-19 04:22:19 +00:00
|
|
|
) -> None:
|
|
|
|
"""Set up Canary alarm control panels based on a config entry."""
|
2020-10-01 08:26:26 +00:00
|
|
|
coordinator: CanaryDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][
|
|
|
|
DATA_COORDINATOR
|
|
|
|
]
|
|
|
|
alarms = [
|
|
|
|
CanaryAlarm(coordinator, location)
|
|
|
|
for location_id, location in coordinator.data["locations"].items()
|
|
|
|
]
|
2017-12-08 09:40:45 +00:00
|
|
|
|
2020-09-19 04:22:19 +00:00
|
|
|
async_add_entities(alarms, True)
|
2017-12-08 09:40:45 +00:00
|
|
|
|
|
|
|
|
2020-10-01 08:26:26 +00:00
|
|
|
class CanaryAlarm(CoordinatorEntity, AlarmControlPanelEntity):
|
2017-12-08 09:40:45 +00:00
|
|
|
"""Representation of a Canary alarm control panel."""
|
|
|
|
|
2021-05-22 08:14:59 +00:00
|
|
|
coordinator: CanaryDataUpdateCoordinator
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self, coordinator: CanaryDataUpdateCoordinator, location: Location
|
|
|
|
) -> None:
|
2017-12-08 09:40:45 +00:00
|
|
|
"""Initialize a Canary security camera."""
|
2020-10-01 08:26:26 +00:00
|
|
|
super().__init__(coordinator)
|
2021-05-22 08:14:59 +00:00
|
|
|
self._location_id: str = location.location_id
|
|
|
|
self._location_name: str = location.name
|
2020-10-01 08:26:26 +00:00
|
|
|
|
|
|
|
@property
|
2021-05-22 08:14:59 +00:00
|
|
|
def location(self) -> Location:
|
2020-10-01 08:26:26 +00:00
|
|
|
"""Return information about the location."""
|
|
|
|
return self.coordinator.data["locations"][self._location_id]
|
2017-12-08 09:40:45 +00:00
|
|
|
|
|
|
|
@property
|
2021-05-22 08:14:59 +00:00
|
|
|
def name(self) -> str:
|
2017-12-08 09:40:45 +00:00
|
|
|
"""Return the name of the alarm."""
|
2020-10-01 08:26:26 +00:00
|
|
|
return self._location_name
|
2017-12-08 09:40:45 +00:00
|
|
|
|
2020-09-13 23:52:40 +00:00
|
|
|
@property
|
2021-05-22 08:14:59 +00:00
|
|
|
def unique_id(self) -> str:
|
2020-09-13 23:52:40 +00:00
|
|
|
"""Return the unique ID of the alarm."""
|
|
|
|
return str(self._location_id)
|
|
|
|
|
2017-12-08 09:40:45 +00:00
|
|
|
@property
|
2021-05-22 08:14:59 +00:00
|
|
|
def state(self) -> str | None:
|
2017-12-08 09:40:45 +00:00
|
|
|
"""Return the state of the device."""
|
2020-10-01 08:26:26 +00:00
|
|
|
if self.location.is_private:
|
2017-12-08 09:40:45 +00:00
|
|
|
return STATE_ALARM_DISARMED
|
|
|
|
|
2020-10-01 08:26:26 +00:00
|
|
|
mode = self.location.mode
|
2017-12-08 09:40:45 +00:00
|
|
|
if mode.name == LOCATION_MODE_AWAY:
|
|
|
|
return STATE_ALARM_ARMED_AWAY
|
2018-07-23 08:16:05 +00:00
|
|
|
if mode.name == LOCATION_MODE_HOME:
|
2017-12-08 09:40:45 +00:00
|
|
|
return STATE_ALARM_ARMED_HOME
|
2018-07-23 08:16:05 +00:00
|
|
|
if mode.name == LOCATION_MODE_NIGHT:
|
2017-12-08 09:40:45 +00:00
|
|
|
return STATE_ALARM_ARMED_NIGHT
|
2020-10-01 08:26:26 +00:00
|
|
|
|
2018-02-11 17:20:28 +00:00
|
|
|
return None
|
2017-12-08 09:40:45 +00:00
|
|
|
|
2019-11-25 23:42:53 +00:00
|
|
|
@property
|
|
|
|
def supported_features(self) -> int:
|
|
|
|
"""Return the list of supported features."""
|
|
|
|
return SUPPORT_ALARM_ARM_HOME | SUPPORT_ALARM_ARM_AWAY | SUPPORT_ALARM_ARM_NIGHT
|
|
|
|
|
2017-12-08 09:40:45 +00:00
|
|
|
@property
|
2021-05-22 08:14:59 +00:00
|
|
|
def extra_state_attributes(self) -> dict[str, Any]:
|
2017-12-08 09:40:45 +00:00
|
|
|
"""Return the state attributes."""
|
2020-10-01 08:26:26 +00:00
|
|
|
return {"private": self.location.is_private}
|
2017-12-08 09:40:45 +00:00
|
|
|
|
2021-05-22 08:14:59 +00:00
|
|
|
def alarm_disarm(self, code: str | None = None) -> None:
|
2017-12-08 09:40:45 +00:00
|
|
|
"""Send disarm command."""
|
2020-10-01 08:26:26 +00:00
|
|
|
self.coordinator.canary.set_location_mode(
|
|
|
|
self._location_id, self.location.mode.name, True
|
|
|
|
)
|
2017-12-08 09:40:45 +00:00
|
|
|
|
2021-05-22 08:14:59 +00:00
|
|
|
def alarm_arm_home(self, code: str | None = None) -> None:
|
2017-12-08 09:40:45 +00:00
|
|
|
"""Send arm home command."""
|
2020-10-01 08:26:26 +00:00
|
|
|
self.coordinator.canary.set_location_mode(self._location_id, LOCATION_MODE_HOME)
|
2017-12-08 09:40:45 +00:00
|
|
|
|
2021-05-22 08:14:59 +00:00
|
|
|
def alarm_arm_away(self, code: str | None = None) -> None:
|
2017-12-08 09:40:45 +00:00
|
|
|
"""Send arm away command."""
|
2020-10-01 08:26:26 +00:00
|
|
|
self.coordinator.canary.set_location_mode(self._location_id, LOCATION_MODE_AWAY)
|
2017-12-08 09:40:45 +00:00
|
|
|
|
2021-05-22 08:14:59 +00:00
|
|
|
def alarm_arm_night(self, code: str | None = None) -> None:
|
2017-12-08 09:40:45 +00:00
|
|
|
"""Send arm night command."""
|
2020-10-01 08:26:26 +00:00
|
|
|
self.coordinator.canary.set_location_mode(
|
|
|
|
self._location_id, LOCATION_MODE_NIGHT
|
|
|
|
)
|