Skip to content

Commit

Permalink
Merge pull request #296 from keatontaylor/dev
Browse files Browse the repository at this point in the history
Bump to 2.0.0

BREAKING CHANGE
  • Loading branch information
alandtse committed Aug 22, 2019
2 parents 94b5c57 + 8522058 commit 474c30d
Show file tree
Hide file tree
Showing 9 changed files with 392 additions and 327 deletions.
17 changes: 0 additions & 17 deletions custom_components.json

This file was deleted.

292 changes: 169 additions & 123 deletions custom_components/alexa_media/__init__.py

Large diffs are not rendered by default.

75 changes: 40 additions & 35 deletions custom_components/alexa_media/alarm_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
from homeassistant import util
from homeassistant.components.alarm_control_panel import AlarmControlPanel
from homeassistant.const import (STATE_ALARM_ARMED_AWAY,
STATE_ALARM_ARMED_HOME, STATE_ALARM_DISARMED)
STATE_ALARM_DISARMED)
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.event import call_later
from homeassistant.helpers.event import async_call_later

from . import DATA_ALEXAMEDIA
from . import DOMAIN as ALEXA_DOMAIN
Expand All @@ -26,15 +26,17 @@
DEPENDENCIES = [ALEXA_DOMAIN]


def setup_platform(hass, config, add_devices_callback,
discovery_info=None):
async def async_setup_platform(hass,
config,
add_devices_callback,
discovery_info=None) -> bool:
"""Set up the Alexa alarm control panel platform."""
devices = [] # type: List[AlexaAlarmControlPanel]
for account, account_dict in (hass.data[DATA_ALEXAMEDIA]
['accounts'].items()):
alexa_client = AlexaAlarmControlPanel(account_dict['login_obj'],
hass) \
# type: AlexaAlarmControlPanel
alexa_client: AlexaAlarmControlPanel = AlexaAlarmControlPanel(
account_dict['login_obj'])
await alexa_client.init()
if not (alexa_client and alexa_client.unique_id):
_LOGGER.debug("%s: Skipping creation of uninitialized device: %s",
hide_email(account),
Expand Down Expand Up @@ -65,7 +67,7 @@ def setup_platform(hass, config, add_devices_callback,
class AlexaAlarmControlPanel(AlarmControlPanel):
"""Implementation of Alexa Media Player alarm control panel."""

def __init__(self, login, hass):
def __init__(self, login) -> None:
# pylint: disable=unexpected-keyword-arg
"""Initialize the Alexa device."""
from alexapy import AlexaAPI
Expand All @@ -74,7 +76,6 @@ def __init__(self, login, hass):
self.alexa_api = AlexaAPI(self, login)
self.alexa_api_session = login.session
self.account = hide_email(login.email)
self.hass = hass

# Guard info
self._appliance_id = None
Expand All @@ -84,9 +85,10 @@ def __init__(self, login, hass):
self._should_poll = False
self._attrs = {}

async def init(self):
try:
from simplejson import JSONDecodeError
data = self.alexa_api.get_guard_details(self._login)
data = await self.alexa_api.get_guard_details(self._login)
guard_dict = (data['locationDetails']
['locationDetails']['Default_Location']
['amazonBridgeDetails']['amazonBridgeDetails']
Expand All @@ -107,29 +109,33 @@ def __init__(self, login, hass):
if not self._appliance_id:
_LOGGER.debug("%s: No Alexa Guard entity found", self.account)
return None

async def async_added_to_hass(self):
"""Store register state change callback."""
# Register event handler on bus
hass.bus.listen(('{}_{}'.format(ALEXA_DOMAIN,
hide_email(login.email)))[0:32],
self._handle_event)
self.refresh(no_throttle=True)
self.hass.bus.async_listen(('{}_{}'.format(
ALEXA_DOMAIN,
hide_email(self._login.email)))[0:32],
self._handle_event)

def _handle_event(self, event):
"""Handle websocket events.
Used instead of polling.
"""
if 'push_activity' in event.data:
call_later(self.hass, 2, lambda _:
self.refresh(no_throttle=True))
async_call_later(self.hass, 2, lambda _:
self.hass.async_create_task(
self.async_update(no_throttle=True)))

@util.Throttle(MIN_TIME_BETWEEN_SCANS, MIN_TIME_BETWEEN_FORCED_SCANS)
def refresh(self):
async def async_update(self):
"""Update Guard state."""
import json
_LOGGER.debug("%s: Refreshing %s", self.account, self.name)
state = None
state_json = self.alexa_api.get_guard_state(self._login,
self._appliance_id)
state_json = await self.alexa_api.get_guard_state(self._login,
self._appliance_id)
# _LOGGER.debug("%s: state_json %s", self.account, state_json)
if (state_json and 'deviceStates' in state_json
and state_json['deviceStates']):
Expand All @@ -156,33 +162,32 @@ def refresh(self):
else:
self._state = STATE_ALARM_DISARMED
_LOGGER.debug("%s: Alarm State: %s", self.account, self.state)
self.schedule_update_ha_state()
self.async_schedule_update_ha_state()

def alarm_disarm(self, code=None):
async def async_alarm_disarm(self, code=None) -> None:
# pylint: disable=unexpected-keyword-arg
"""Send disarm command.
We use the arm_home state as Alexa does not have disarm state.
"""
self.alarm_arm_home()
self.schedule_update_ha_state()
await self.async_alarm_arm_home()

def alarm_arm_home(self, code=None):
async def async_alarm_arm_home(self, code=None) -> None:
"""Send arm home command."""
self.alexa_api.set_guard_state(self._login,
self._guard_entity_id,
"ARMED_STAY")
self.refresh(no_throttle=True)
self.schedule_update_ha_state()
await self.alexa_api.set_guard_state(self._login,
self._guard_entity_id,
"ARMED_STAY")
await self.async_update(no_throttle=True)
self.async_schedule_update_ha_state()

def alarm_arm_away(self, code=None):
async def async_alarm_arm_away(self, code=None) -> None:
"""Send arm away command."""
# pylint: disable=unexpected-keyword-arg
self.alexa_api.set_guard_state(self._login,
self._guard_entity_id,
"ARMED_AWAY")
self.refresh(no_throttle=True)
self.schedule_update_ha_state()
await self.alexa_api.set_guard_state(self._login,
self._guard_entity_id,
"ARMED_AWAY")
await self.async_update(no_throttle=True)
self.async_schedule_update_ha_state()

@property
def unique_id(self):
Expand Down
2 changes: 1 addition & 1 deletion custom_components/alexa_media/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"""
from datetime import timedelta

__version__ = '1.4.1'
__version__ = '2.0.0'
PROJECT_URL = "https://github.com/keatontaylor/alexa_media_player/"
ISSUE_URL = "{}issues".format(PROJECT_URL)

Expand Down
2 changes: 1 addition & 1 deletion custom_components/alexa_media/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"documentation": "https://github.com/keatontaylor/alexa_media_player/wiki",
"dependencies": [],
"codeowners": ["@keatontaylor", "@alandtse"],
"requirements": ["alexapy==0.7.1"]
"requirements": ["alexapy==1.0.0"]
}
Loading

0 comments on commit 474c30d

Please sign in to comment.