-
-
Notifications
You must be signed in to change notification settings - Fork 37.6k
Add ness alarm control panel using nessclient #18463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MartinHjelmare
merged 12 commits into
home-assistant:dev
from
nickw444:nwhyte/ness-alarm
Jan 1, 2019
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
30243bc
Add ness alarm control panel using nessclient
nickw444 0407bcc
indenting
nickw444 784e946
.
nickw444 47289d9
Remove availability functionality, will improve and add back in anoth…
nickw444 26537a3
Use call_count
nickw444 0058a2b
lint
nickw444 d326442
lint
nickw444 3c0a853
Review changes
nickw444 a93ae23
Lint
nickw444 f8ea58b
Bump nessclient to 0.9.8
nickw444 a6933ff
Bump nessclient to 0.9.9
nickw444 5cb55c1
Remove from .coveragerc
nickw444 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
homeassistant/components/alarm_control_panel/ness_alarm.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| """ | ||
| Support for Ness D8X/D16X alarm panel. | ||
|
|
||
| For more details about this platform, please refer to the documentation at | ||
| https://home-assistant.io/components/alarm_control_panel.ness_alarm/ | ||
| """ | ||
|
|
||
| import logging | ||
|
|
||
| import homeassistant.components.alarm_control_panel as alarm | ||
| from homeassistant.components.ness_alarm import ( | ||
| DATA_NESS, SIGNAL_ARMING_STATE_CHANGED) | ||
| from homeassistant.const import ( | ||
| STATE_ALARM_ARMED_AWAY, STATE_ALARM_ARMING, | ||
| STATE_ALARM_TRIGGERED, STATE_ALARM_PENDING, STATE_ALARM_DISARMED) | ||
| from homeassistant.core import callback | ||
| from homeassistant.helpers.dispatcher import async_dispatcher_connect | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| DEPENDENCIES = ['ness_alarm'] | ||
|
|
||
|
|
||
| async def async_setup_platform(hass, config, async_add_entities, | ||
| discovery_info=None): | ||
| """Set up the Ness Alarm alarm control panel devices.""" | ||
| if discovery_info is None: | ||
| return | ||
|
|
||
| device = NessAlarmPanel(hass.data[DATA_NESS], 'Alarm Panel') | ||
| async_add_entities([device]) | ||
|
|
||
|
|
||
| class NessAlarmPanel(alarm.AlarmControlPanel): | ||
| """Representation of a Ness alarm panel.""" | ||
|
|
||
| def __init__(self, client, name): | ||
| """Initialize the alarm panel.""" | ||
| self._client = client | ||
| self._name = name | ||
| self._state = None | ||
|
|
||
| async def async_added_to_hass(self): | ||
| """Register callbacks.""" | ||
| async_dispatcher_connect( | ||
| self.hass, SIGNAL_ARMING_STATE_CHANGED, | ||
| self._handle_arming_state_change) | ||
|
|
||
| @property | ||
| def name(self): | ||
| """Return the name of the device.""" | ||
| return self._name | ||
|
|
||
| @property | ||
| def should_poll(self): | ||
| """Return the polling state.""" | ||
| return False | ||
|
|
||
| @property | ||
| def code_format(self): | ||
| """Return the regex for code format or None if no code is required.""" | ||
| return 'Number' | ||
|
|
||
| @property | ||
| def state(self): | ||
| """Return the state of the device.""" | ||
| return self._state | ||
|
|
||
| async def async_alarm_disarm(self, code=None): | ||
| """Send disarm command.""" | ||
| await self._client.disarm(code) | ||
|
|
||
| async def async_alarm_arm_away(self, code=None): | ||
| """Send arm away command.""" | ||
| await self._client.arm_away(code) | ||
|
|
||
| async def async_alarm_arm_home(self, code=None): | ||
| """Send arm home command.""" | ||
| await self._client.arm_home(code) | ||
|
|
||
| async def async_alarm_trigger(self, code=None): | ||
| """Send trigger/panic command.""" | ||
| await self._client.panic(code) | ||
|
|
||
| @callback | ||
| def _handle_arming_state_change(self, arming_state): | ||
| """Handle arming state update.""" | ||
| from nessclient import ArmingState | ||
|
|
||
| if arming_state == ArmingState.UNKNOWN: | ||
| self._state = None | ||
| elif arming_state == ArmingState.DISARMED: | ||
| self._state = STATE_ALARM_DISARMED | ||
| elif arming_state == ArmingState.ARMING: | ||
| self._state = STATE_ALARM_ARMING | ||
| elif arming_state == ArmingState.EXIT_DELAY: | ||
| self._state = STATE_ALARM_ARMING | ||
| elif arming_state == ArmingState.ARMED: | ||
| self._state = STATE_ALARM_ARMED_AWAY | ||
| elif arming_state == ArmingState.ENTRY_DELAY: | ||
| self._state = STATE_ALARM_PENDING | ||
| elif arming_state == ArmingState.TRIGGERED: | ||
| self._state = STATE_ALARM_TRIGGERED | ||
| else: | ||
| _LOGGER.warning("Unhandled arming state: %s", arming_state) | ||
|
|
||
| self.async_schedule_update_ha_state() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| """ | ||
| Support for Ness D8X/D16X zone states - represented as binary sensors. | ||
|
|
||
| For more details about this platform, please refer to the documentation at | ||
| https://home-assistant.io/components/binary_sensor.ness_alarm/ | ||
| """ | ||
| import logging | ||
|
|
||
| from homeassistant.components.binary_sensor import BinarySensorDevice | ||
| from homeassistant.components.ness_alarm import ( | ||
| CONF_ZONES, CONF_ZONE_TYPE, CONF_ZONE_NAME, CONF_ZONE_ID, | ||
| SIGNAL_ZONE_CHANGED, ZoneChangedData) | ||
| from homeassistant.core import callback | ||
| from homeassistant.helpers.dispatcher import async_dispatcher_connect | ||
|
|
||
| DEPENDENCIES = ['ness_alarm'] | ||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| async def async_setup_platform(hass, config, async_add_entities, | ||
| discovery_info=None): | ||
| """Set up the Ness Alarm binary sensor devices.""" | ||
| if not discovery_info: | ||
| return | ||
|
|
||
| configured_zones = discovery_info[CONF_ZONES] | ||
|
|
||
| devices = [] | ||
|
|
||
| for zone_config in configured_zones: | ||
| zone_type = zone_config[CONF_ZONE_TYPE] | ||
| zone_name = zone_config[CONF_ZONE_NAME] | ||
| zone_id = zone_config[CONF_ZONE_ID] | ||
| device = NessZoneBinarySensor(zone_id=zone_id, name=zone_name, | ||
| zone_type=zone_type) | ||
| devices.append(device) | ||
|
|
||
| async_add_entities(devices) | ||
|
|
||
|
|
||
| class NessZoneBinarySensor(BinarySensorDevice): | ||
| """Representation of an Ness alarm zone as a binary sensor.""" | ||
|
|
||
| def __init__(self, zone_id, name, zone_type): | ||
| """Initialize the binary_sensor.""" | ||
| self._zone_id = zone_id | ||
| self._name = name | ||
| self._type = zone_type | ||
| self._state = 0 | ||
|
|
||
| async def async_added_to_hass(self): | ||
| """Register callbacks.""" | ||
| async_dispatcher_connect( | ||
| self.hass, SIGNAL_ZONE_CHANGED, self._handle_zone_change) | ||
|
|
||
| @property | ||
| def name(self): | ||
| """Return the name of the entity.""" | ||
| return self._name | ||
|
|
||
| @property | ||
| def should_poll(self): | ||
| """No polling needed.""" | ||
| return False | ||
|
|
||
| @property | ||
| def is_on(self): | ||
| """Return true if sensor is on.""" | ||
| return self._state == 1 | ||
|
|
||
| @property | ||
| def device_class(self): | ||
| """Return the class of this sensor, from DEVICE_CLASSES.""" | ||
| return self._type | ||
|
|
||
| @callback | ||
| def _handle_zone_change(self, data: ZoneChangedData): | ||
| """Handle zone state update.""" | ||
| if self._zone_id == data.zone_id: | ||
| self._state = data.state | ||
| self.async_schedule_update_ha_state() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| """ | ||
| Support for Ness D8X/D16X devices. | ||
|
|
||
| For more details about this component, please refer to the documentation at | ||
| https://home-assistant.io/components/ness_alarm/ | ||
| """ | ||
| import logging | ||
| from collections import namedtuple | ||
|
|
||
| import voluptuous as vol | ||
|
|
||
| from homeassistant.components.binary_sensor import DEVICE_CLASSES | ||
| from homeassistant.const import EVENT_HOMEASSISTANT_STOP | ||
| from homeassistant.helpers import config_validation as cv | ||
| from homeassistant.helpers.discovery import async_load_platform | ||
| from homeassistant.helpers.dispatcher import async_dispatcher_send | ||
|
|
||
| REQUIREMENTS = ['nessclient==0.9.9'] | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| DOMAIN = 'ness_alarm' | ||
| DATA_NESS = 'ness_alarm' | ||
|
|
||
| CONF_DEVICE_HOST = 'host' | ||
| CONF_DEVICE_PORT = 'port' | ||
| CONF_ZONES = 'zones' | ||
| CONF_ZONE_NAME = 'name' | ||
| CONF_ZONE_TYPE = 'type' | ||
| CONF_ZONE_ID = 'id' | ||
| ATTR_CODE = 'code' | ||
| ATTR_OUTPUT_ID = 'output_id' | ||
| ATTR_STATE = 'state' | ||
| DEFAULT_ZONES = [] | ||
|
|
||
| SIGNAL_ZONE_CHANGED = 'ness_alarm.zone_changed' | ||
| SIGNAL_ARMING_STATE_CHANGED = 'ness_alarm.arming_state_changed' | ||
|
|
||
| ZoneChangedData = namedtuple('ZoneChangedData', ['zone_id', 'state']) | ||
|
|
||
| DEFAULT_ZONE_TYPE = 'motion' | ||
| ZONE_SCHEMA = vol.Schema({ | ||
| vol.Required(CONF_ZONE_NAME): cv.string, | ||
| vol.Required(CONF_ZONE_ID): cv.positive_int, | ||
| vol.Optional(CONF_ZONE_TYPE, default=DEFAULT_ZONE_TYPE): | ||
| vol.In(DEVICE_CLASSES)}) | ||
|
|
||
| CONFIG_SCHEMA = vol.Schema({ | ||
| DOMAIN: vol.Schema({ | ||
| vol.Required(CONF_DEVICE_HOST): cv.string, | ||
| vol.Required(CONF_DEVICE_PORT): cv.port, | ||
| vol.Optional(CONF_ZONES, default=DEFAULT_ZONES): | ||
| vol.All(cv.ensure_list, [ZONE_SCHEMA]), | ||
| }), | ||
| }, extra=vol.ALLOW_EXTRA) | ||
|
|
||
| SERVICE_PANIC = 'panic' | ||
| SERVICE_AUX = 'aux' | ||
|
|
||
| SERVICE_SCHEMA_PANIC = vol.Schema({ | ||
| vol.Required(ATTR_CODE): cv.string, | ||
| }) | ||
| SERVICE_SCHEMA_AUX = vol.Schema({ | ||
| vol.Required(ATTR_OUTPUT_ID): cv.positive_int, | ||
| vol.Optional(ATTR_STATE, default=True): cv.boolean, | ||
| }) | ||
|
|
||
|
|
||
| async def async_setup(hass, config): | ||
| """Set up the Ness Alarm platform.""" | ||
| from nessclient import Client, ArmingState | ||
| conf = config[DOMAIN] | ||
|
|
||
| zones = conf[CONF_ZONES] | ||
| host = conf[CONF_DEVICE_HOST] | ||
| port = conf[CONF_DEVICE_PORT] | ||
|
|
||
| client = Client(host=host, port=port, loop=hass.loop) | ||
| hass.data[DATA_NESS] = client | ||
|
|
||
| async def _close(event): | ||
| await client.close() | ||
|
|
||
| hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _close) | ||
|
|
||
| hass.async_create_task( | ||
| async_load_platform(hass, 'binary_sensor', DOMAIN, {CONF_ZONES: zones}, | ||
| config)) | ||
| hass.async_create_task( | ||
| async_load_platform(hass, 'alarm_control_panel', DOMAIN, {}, config)) | ||
|
|
||
| def on_zone_change(zone_id: int, state: bool): | ||
| """Receives and propagates zone state updates.""" | ||
| async_dispatcher_send(hass, SIGNAL_ZONE_CHANGED, ZoneChangedData( | ||
| zone_id=zone_id, | ||
| state=state, | ||
| )) | ||
|
|
||
| def on_state_change(arming_state: ArmingState): | ||
| """Receives and propagates arming state updates.""" | ||
| async_dispatcher_send(hass, SIGNAL_ARMING_STATE_CHANGED, arming_state) | ||
|
|
||
| client.on_zone_change(on_zone_change) | ||
| client.on_state_change(on_state_change) | ||
|
|
||
| # Force update for current arming status and current zone states | ||
| hass.loop.create_task(client.keepalive()) | ||
| hass.loop.create_task(client.update()) | ||
|
|
||
| async def handle_panic(call): | ||
| await client.panic(call.data[ATTR_CODE]) | ||
|
|
||
| async def handle_aux(call): | ||
| await client.aux(call.data[ATTR_OUTPUT_ID], call.data[ATTR_STATE]) | ||
|
|
||
| hass.services.async_register(DOMAIN, SERVICE_PANIC, handle_panic, | ||
| schema=SERVICE_SCHEMA_PANIC) | ||
| hass.services.async_register(DOMAIN, SERVICE_AUX, handle_aux, | ||
| schema=SERVICE_SCHEMA_AUX) | ||
|
|
||
| return True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.