core/homeassistant/components/totalconnect/alarm_control_panel.py

268 lines
10 KiB
Python
Raw Normal View History

"""Interfaces with TotalConnect alarm control panels."""
from __future__ import annotations
from total_connect_client import ArmingHelper
from total_connect_client.exceptions import BadResultCodeError, UsercodeInvalid
import homeassistant.components.alarm_control_panel as alarm
from homeassistant.components.alarm_control_panel import AlarmControlPanelEntityFeature
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
2019-07-31 19:25:30 +00:00
STATE_ALARM_ARMED_AWAY,
STATE_ALARM_ARMED_CUSTOM_BYPASS,
2019-07-31 19:25:30 +00:00
STATE_ALARM_ARMED_HOME,
STATE_ALARM_ARMED_NIGHT,
STATE_ALARM_ARMING,
STATE_ALARM_DISARMED,
2019-07-31 19:25:30 +00:00
STATE_ALARM_DISARMING,
STATE_ALARM_TRIGGERED,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_platform
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN
SERVICE_ALARM_ARM_AWAY_INSTANT = "arm_away_instant"
SERVICE_ALARM_ARM_HOME_INSTANT = "arm_home_instant"
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
Add Totalconnect config flow (#32126) * Bump skybellpy to 0.4.0 * Bump skybellpy to 0.4.0 in requirements_all.txt * Added extra states for STATE_ALARM_TRIGGERED to allow users to know if it is a burglar or fire or carbon monoxide so automations can take appropriate actions. Updated TotalConnect component to handle these new states. * Fix const import * Fix const import * Fix const imports * Bump total-connect-client to 0.26. * Catch details of alarm trigger in state attributes. Also bumps total_connect_client to 0.27. * Change state_attributes() to device_state_attributes() * Move totalconnect component toward being a multi-platform integration. Bump total_connect_client to 0.28. * add missing total-connect alarm state mappings * Made recommended changes of MartinHjelmare at https://github.com/home-assistant/home-assistant/pull/24427 * Update __init__.py * Updates per MartinHjelmare comments * flake8/pydocstyle fixes * removed . at end of log message * added blank line between logging and voluptuous * more fixes * Adding totalconnect zones as HA binary_sensors * fix manifest.json * flake8/pydocstyle fixes. Added codeowner. * Update formatting per @springstan guidance. * Fixed pylint * Add zone ID to log message for easier troubleshooting * Account for bypassed zones in update() * More status handling fixes. * Fixed flake8 error * Another attempt at black/isort fixes. * Bump total-connect-client to 0.50. Simplify code using new functions in total-connect-client package instead of importing constants. Run black and isort. * Fix manifest file * Another manifest fix * one more manifest fix * more manifest changes. * sync up * fix indent * one more pylint fix * Hopefully the last pylint fix * make variable names understandable * create and fill dict in one step * Fix name and attributes * rename to logical variable in alarm_control_panel * Remove location_name from alarm_control_panel attributes since it is already the name of the alarm. * Multiple fixes to improve code per @springstan suggestions * Update homeassistant/components/totalconnect/binary_sensor.py Co-Authored-By: springstan <46536646+springstan@users.noreply.github.com> * Multiple changes per @MartinHjelmare review * simplify alarm adding * Fix binary_sensor.py is_on * Move DOMAIN to .const in line with examples. * Move to async_setup * Simplify code using new features of total-connect-client 0.51 * First crack at config flow for totalconnect * bump totalconnect to 0.52 * use client.is_logged_in() to avoid total-connect-client details. * updated generated/config_flow.py * use is_logged_in() * Hopefully final touches for config flow * Add tests for config flow * Updated requirements for test * Fixes to test_config_flow * Removed leftover comments and code * fix const.py flake8 error * Simplify text per @Kane610 https://github.com/home-assistant/home-assistant/pull/32126#pullrequestreview-364652949 * Remove .get() to speed things up since the required items should always be available. * Move CONF_USERNAME and CONF_PASSWORD into .const to eliminate extra I/O to import from homeassistant.const * Fix I/O async issues * Fix flake8 and black errors * Mock the I/O in tests. * Fix isort error * Empty commit to re-start azure pipelines (per discord) * bump total-connect-client to 0.53 * Update homeassistant/components/totalconnect/__init__.py Co-Authored-By: Paulus Schoutsen <paulus@home-assistant.io> * Update homeassistant/components/totalconnect/config_flow.py Co-Authored-By: Paulus Schoutsen <paulus@home-assistant.io> * Fixes per @balloob comments * Fix imports * fix isort error * Fix async_unload_entry It still referenced CONF_USERNAME instead of entry.entity_id * Added async_setup so not breaking change. Fixed imports. * Update tests/components/totalconnect/test_config_flow.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> * Remove TotalConnectSystem() per @MartinHjelmare suggestion * Moved from is_logged_in() to is_valid_credentials() The second is more accurate for what we are checking for, because is_logged_in() could return False due to connection error. * Fix import in test * remove commented code and decorator * Update tests/components/totalconnect/test_config_flow.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> * fix test_config_flow.py * bump total-connect-client to 0.54 * remove un-needed import of mock_coro * bump to total-connect-client 0.54.1 * re-add CONFIG_SCHEMA * disable pylint on line 10 to avoid pylint bug Co-authored-by: springstan <46536646+springstan@users.noreply.github.com> Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io> Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2020-04-13 02:29:57 +00:00
"""Set up TotalConnect alarm panels based on a config entry."""
alarms = []
coordinator = hass.data[DOMAIN][entry.entry_id]
for location_id, location in coordinator.client.locations.items():
Add totalconnect zones as binary sensors (#28712) * Bump skybellpy to 0.4.0 * Bump skybellpy to 0.4.0 in requirements_all.txt * Added extra states for STATE_ALARM_TRIGGERED to allow users to know if it is a burglar or fire or carbon monoxide so automations can take appropriate actions. Updated TotalConnect component to handle these new states. * Fix const import * Fix const import * Fix const imports * Bump total-connect-client to 0.26. * Catch details of alarm trigger in state attributes. Also bumps total_connect_client to 0.27. * Change state_attributes() to device_state_attributes() * Move totalconnect component toward being a multi-platform integration. Bump total_connect_client to 0.28. * add missing total-connect alarm state mappings * Made recommended changes of MartinHjelmare at https://github.com/home-assistant/home-assistant/pull/24427 * Update __init__.py * Updates per MartinHjelmare comments * flake8/pydocstyle fixes * removed . at end of log message * added blank line between logging and voluptuous * more fixes * Adding totalconnect zones as HA binary_sensors * fix manifest.json * flake8/pydocstyle fixes. Added codeowner. * Update formatting per @springstan guidance. * Fixed pylint * Add zone ID to log message for easier troubleshooting * Account for bypassed zones in update() * More status handling fixes. * Fixed flake8 error * Another attempt at black/isort fixes. * Bump total-connect-client to 0.50. Simplify code using new functions in total-connect-client package instead of importing constants. Run black and isort. * Fix manifest file * Another manifest fix * one more manifest fix * more manifest changes. * sync up * fix indent * one more pylint fix * Hopefully the last pylint fix * make variable names understandable * create and fill dict in one step * Fix name and attributes * rename to logical variable in alarm_control_panel * Remove location_name from alarm_control_panel attributes since it is already the name of the alarm. * Multiple fixes to improve code per @springstan suggestions * Update homeassistant/components/totalconnect/binary_sensor.py Co-Authored-By: springstan <46536646+springstan@users.noreply.github.com> * Multiple changes per @MartinHjelmare review * simplify alarm adding * Fix binary_sensor.py is_on Co-authored-by: springstan <46536646+springstan@users.noreply.github.com>
2020-02-01 09:09:52 +00:00
location_name = location.location_name
for partition_id in location.partitions:
alarms.append(
TotalConnectAlarm(
coordinator=coordinator,
name=location_name,
location_id=location_id,
partition_id=partition_id,
)
)
Add Totalconnect config flow (#32126) * Bump skybellpy to 0.4.0 * Bump skybellpy to 0.4.0 in requirements_all.txt * Added extra states for STATE_ALARM_TRIGGERED to allow users to know if it is a burglar or fire or carbon monoxide so automations can take appropriate actions. Updated TotalConnect component to handle these new states. * Fix const import * Fix const import * Fix const imports * Bump total-connect-client to 0.26. * Catch details of alarm trigger in state attributes. Also bumps total_connect_client to 0.27. * Change state_attributes() to device_state_attributes() * Move totalconnect component toward being a multi-platform integration. Bump total_connect_client to 0.28. * add missing total-connect alarm state mappings * Made recommended changes of MartinHjelmare at https://github.com/home-assistant/home-assistant/pull/24427 * Update __init__.py * Updates per MartinHjelmare comments * flake8/pydocstyle fixes * removed . at end of log message * added blank line between logging and voluptuous * more fixes * Adding totalconnect zones as HA binary_sensors * fix manifest.json * flake8/pydocstyle fixes. Added codeowner. * Update formatting per @springstan guidance. * Fixed pylint * Add zone ID to log message for easier troubleshooting * Account for bypassed zones in update() * More status handling fixes. * Fixed flake8 error * Another attempt at black/isort fixes. * Bump total-connect-client to 0.50. Simplify code using new functions in total-connect-client package instead of importing constants. Run black and isort. * Fix manifest file * Another manifest fix * one more manifest fix * more manifest changes. * sync up * fix indent * one more pylint fix * Hopefully the last pylint fix * make variable names understandable * create and fill dict in one step * Fix name and attributes * rename to logical variable in alarm_control_panel * Remove location_name from alarm_control_panel attributes since it is already the name of the alarm. * Multiple fixes to improve code per @springstan suggestions * Update homeassistant/components/totalconnect/binary_sensor.py Co-Authored-By: springstan <46536646+springstan@users.noreply.github.com> * Multiple changes per @MartinHjelmare review * simplify alarm adding * Fix binary_sensor.py is_on * Move DOMAIN to .const in line with examples. * Move to async_setup * Simplify code using new features of total-connect-client 0.51 * First crack at config flow for totalconnect * bump totalconnect to 0.52 * use client.is_logged_in() to avoid total-connect-client details. * updated generated/config_flow.py * use is_logged_in() * Hopefully final touches for config flow * Add tests for config flow * Updated requirements for test * Fixes to test_config_flow * Removed leftover comments and code * fix const.py flake8 error * Simplify text per @Kane610 https://github.com/home-assistant/home-assistant/pull/32126#pullrequestreview-364652949 * Remove .get() to speed things up since the required items should always be available. * Move CONF_USERNAME and CONF_PASSWORD into .const to eliminate extra I/O to import from homeassistant.const * Fix I/O async issues * Fix flake8 and black errors * Mock the I/O in tests. * Fix isort error * Empty commit to re-start azure pipelines (per discord) * bump total-connect-client to 0.53 * Update homeassistant/components/totalconnect/__init__.py Co-Authored-By: Paulus Schoutsen <paulus@home-assistant.io> * Update homeassistant/components/totalconnect/config_flow.py Co-Authored-By: Paulus Schoutsen <paulus@home-assistant.io> * Fixes per @balloob comments * Fix imports * fix isort error * Fix async_unload_entry It still referenced CONF_USERNAME instead of entry.entity_id * Added async_setup so not breaking change. Fixed imports. * Update tests/components/totalconnect/test_config_flow.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> * Remove TotalConnectSystem() per @MartinHjelmare suggestion * Moved from is_logged_in() to is_valid_credentials() The second is more accurate for what we are checking for, because is_logged_in() could return False due to connection error. * Fix import in test * remove commented code and decorator * Update tests/components/totalconnect/test_config_flow.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> * fix test_config_flow.py * bump total-connect-client to 0.54 * remove un-needed import of mock_coro * bump to total-connect-client 0.54.1 * re-add CONFIG_SCHEMA * disable pylint on line 10 to avoid pylint bug Co-authored-by: springstan <46536646+springstan@users.noreply.github.com> Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io> Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2020-04-13 02:29:57 +00:00
async_add_entities(alarms, True)
# Set up services
platform = entity_platform.async_get_current_platform()
platform.async_register_entity_service(
SERVICE_ALARM_ARM_AWAY_INSTANT,
{},
"async_alarm_arm_away_instant",
)
platform.async_register_entity_service(
SERVICE_ALARM_ARM_HOME_INSTANT,
{},
"async_alarm_arm_home_instant",
)
class TotalConnectAlarm(CoordinatorEntity, alarm.AlarmControlPanelEntity):
"""Represent an TotalConnect status."""
_attr_supported_features = (
AlarmControlPanelEntityFeature.ARM_HOME
| AlarmControlPanelEntityFeature.ARM_AWAY
| AlarmControlPanelEntityFeature.ARM_NIGHT
)
def __init__(self, coordinator, name, location_id, partition_id):
"""Initialize the TotalConnect status."""
super().__init__(coordinator)
self._location_id = location_id
self._location = coordinator.client.locations[location_id]
self._partition_id = partition_id
self._partition = self._location.partitions[partition_id]
self._device = self._location.devices[self._location.security_device_id]
self._state = None
self._attr_extra_state_attributes = {}
"""
Set unique_id to location_id for partition 1 to avoid breaking change
for most users with new support for partitions.
Add _# for partition 2 and beyond.
"""
if partition_id == 1:
self._attr_name = name
self._attr_unique_id = f"{location_id}"
else:
self._attr_name = f"{name} partition {partition_id}"
self._attr_unique_id = f"{location_id}_{partition_id}"
@property
def device_info(self) -> DeviceInfo:
"""Return device info."""
return DeviceInfo(
identifiers={(DOMAIN, self._device.serial_number)},
name=self._device.name,
)
@property
def state(self) -> str | None:
"""Return the state of the device."""
attr = {
"location_name": self.name,
2019-07-31 19:25:30 +00:00
"location_id": self._location_id,
"partition": self._partition_id,
"ac_loss": self._location.ac_loss,
"low_battery": self._location.low_battery,
"cover_tampered": self._location.is_cover_tampered(),
2019-07-31 19:25:30 +00:00
"triggered_source": None,
"triggered_zone": None,
}
state = None
if self._partition.arming_state.is_disarmed():
state = STATE_ALARM_DISARMED
elif self._partition.arming_state.is_armed_night():
state = STATE_ALARM_ARMED_NIGHT
elif self._partition.arming_state.is_armed_home():
state = STATE_ALARM_ARMED_HOME
elif self._partition.arming_state.is_armed_away():
state = STATE_ALARM_ARMED_AWAY
elif self._partition.arming_state.is_armed_custom_bypass():
state = STATE_ALARM_ARMED_CUSTOM_BYPASS
elif self._partition.arming_state.is_arming():
state = STATE_ALARM_ARMING
elif self._partition.arming_state.is_disarming():
state = STATE_ALARM_DISARMING
elif self._partition.arming_state.is_triggered_police():
state = STATE_ALARM_TRIGGERED
2019-07-31 19:25:30 +00:00
attr["triggered_source"] = "Police/Medical"
elif self._partition.arming_state.is_triggered_fire():
state = STATE_ALARM_TRIGGERED
2019-07-31 19:25:30 +00:00
attr["triggered_source"] = "Fire/Smoke"
elif self._partition.arming_state.is_triggered_gas():
state = STATE_ALARM_TRIGGERED
2019-07-31 19:25:30 +00:00
attr["triggered_source"] = "Carbon Monoxide"
self._state = state
self._attr_extra_state_attributes = attr
return self._state
async def async_alarm_disarm(self, code: str | None = None) -> None:
"""Send disarm command."""
try:
await self.hass.async_add_executor_job(self._disarm)
except UsercodeInvalid as error:
self.coordinator.config_entry.async_start_reauth(self.hass)
raise HomeAssistantError(
"TotalConnect usercode is invalid. Did not disarm"
) from error
except BadResultCodeError as error:
raise HomeAssistantError(
f"TotalConnect failed to disarm {self.name}."
) from error
await self.coordinator.async_request_refresh()
def _disarm(self, code=None):
"""Disarm synchronous."""
ArmingHelper(self._partition).disarm()
async def async_alarm_arm_home(self, code: str | None = None) -> None:
"""Send arm home command."""
try:
await self.hass.async_add_executor_job(self._arm_home)
except UsercodeInvalid as error:
self.coordinator.config_entry.async_start_reauth(self.hass)
raise HomeAssistantError(
"TotalConnect usercode is invalid. Did not arm home"
) from error
except BadResultCodeError as error:
raise HomeAssistantError(
f"TotalConnect failed to arm home {self.name}."
) from error
await self.coordinator.async_request_refresh()
def _arm_home(self):
"""Arm home synchronous."""
ArmingHelper(self._partition).arm_stay()
async def async_alarm_arm_away(self, code: str | None = None) -> None:
"""Send arm away command."""
try:
await self.hass.async_add_executor_job(self._arm_away)
except UsercodeInvalid as error:
self.coordinator.config_entry.async_start_reauth(self.hass)
raise HomeAssistantError(
"TotalConnect usercode is invalid. Did not arm away"
) from error
except BadResultCodeError as error:
raise HomeAssistantError(
f"TotalConnect failed to arm away {self.name}."
) from error
await self.coordinator.async_request_refresh()
def _arm_away(self, code=None):
"""Arm away synchronous."""
ArmingHelper(self._partition).arm_away()
async def async_alarm_arm_night(self, code: str | None = None) -> None:
"""Send arm night command."""
try:
await self.hass.async_add_executor_job(self._arm_night)
except UsercodeInvalid as error:
self.coordinator.config_entry.async_start_reauth(self.hass)
raise HomeAssistantError(
"TotalConnect usercode is invalid. Did not arm night"
) from error
except BadResultCodeError as error:
raise HomeAssistantError(
f"TotalConnect failed to arm night {self.name}."
) from error
await self.coordinator.async_request_refresh()
def _arm_night(self, code=None):
"""Arm night synchronous."""
ArmingHelper(self._partition).arm_stay_night()
async def async_alarm_arm_home_instant(self, code: str | None = None) -> None:
"""Send arm home instant command."""
try:
await self.hass.async_add_executor_job(self._arm_home_instant)
except UsercodeInvalid as error:
self.coordinator.config_entry.async_start_reauth(self.hass)
raise HomeAssistantError(
"TotalConnect usercode is invalid. Did not arm home instant"
) from error
except BadResultCodeError as error:
raise HomeAssistantError(
f"TotalConnect failed to arm home instant {self.name}."
) from error
await self.coordinator.async_request_refresh()
def _arm_home_instant(self):
"""Arm home instant synchronous."""
ArmingHelper(self._partition).arm_stay_instant()
async def async_alarm_arm_away_instant(self, code: str | None = None) -> None:
"""Send arm away instant command."""
try:
await self.hass.async_add_executor_job(self._arm_away_instant)
except UsercodeInvalid as error:
self.coordinator.config_entry.async_start_reauth(self.hass)
raise HomeAssistantError(
"TotalConnect usercode is invalid. Did not arm away instant"
) from error
except BadResultCodeError as error:
raise HomeAssistantError(
f"TotalConnect failed to arm away instant {self.name}."
) from error
await self.coordinator.async_request_refresh()
def _arm_away_instant(self, code=None):
"""Arm away instant synchronous."""
ArmingHelper(self._partition).arm_away_instant()