Advertisement
Guest User

Untitled

a guest
Feb 26th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.03 KB | None | 0 0
  1. """ Simple asynchronous package for interacting with Sector Alarms web panel """
  2.  
  3. from .util import get_json
  4.  
  5.  
  6. class AsyncSector(object):
  7.     """ Class to interact with sector alarm web panel """
  8.  
  9.     Base = 'https://mypagesapi.sectoralarm.net/'
  10.     Login = 'User/Login'
  11.     Alarm = 'Panel/GetOverview'
  12.     Temperatures = 'Panel/GetTempratures/{}'
  13.     History = 'Panel/GetPanelHistory/{}'
  14.     Arm = 'Panel/ArmPanel'
  15.  
  16.     @classmethod
  17.     async def create(cls, session, alarm_id, username, password):
  18.         """ factory """
  19.         self = AsyncSector(session, alarm_id, username, password)
  20.         logged_in = await self.login()
  21.  
  22.         return self if logged_in else None
  23.  
  24.     def __init__(self, session, alarm_id, username, password):
  25.         self.alarm_id = alarm_id
  26.         self._session = session
  27.         self._auth = {'userID': username, 'password': password}
  28.  
  29.     async def login(self):
  30.         """ Tries to Login to Sector Alarm """
  31.  
  32.         response = await self._session.post(
  33.             AsyncSector.Base + AsyncSector.Login, json=self._auth)
  34.  
  35.         if response.status == 200:
  36.             result = await response.text()
  37.             if 'frmLogin' in result:
  38.                 return False
  39.             return True
  40.  
  41.         return False
  42.  
  43.     async def get_status(self):
  44.         """
  45.        Fetches the status of the alarm
  46.        """
  47.         request = self._session.post(
  48.             AsyncSector.Base + AsyncSector.Alarm,
  49.             data={'PanelId': self.alarm_id})
  50.  
  51.         return await get_json(request)
  52.  
  53.     async def get_temperatures(self):
  54.         """
  55.        Fetches a list of all temperature sensors
  56.        """
  57.         request = self._session.get(
  58.             AsyncSector.Base + AsyncSector.Temperatures.format(self.alarm_id))
  59.  
  60.         return await get_json(request)
  61.  
  62.     async def get_history(self):
  63.         """
  64.        Fetches the alarm event log/history
  65.        """
  66.         request = self._session.get(AsyncSector.Base +
  67.                                     AsyncSector.History.format(self.alarm_id))
  68.  
  69.         return await get_json(request)
  70.  
  71.     async def alarm_toggle(self, state, code=None):
  72.         """
  73.        Tries to toggle the state of the alarm
  74.        """
  75.         data = {
  76.             'ArmCmd': state,
  77.             'PanelCode': code,
  78.             'HasLocks': False,
  79.             'id': self.alarm_id
  80.         }
  81.  
  82.         request = self._session.post(
  83.             AsyncSector.Base + AsyncSector.Arm, json=data)
  84.  
  85.         result = await get_json(request)
  86.         if 'status' in result and result['status'] == 'success':
  87.             return True
  88.  
  89.         return False
  90.  
  91.     async def disarm(self, code=None):
  92.         """ Send disarm command """
  93.         return await self.alarm_toggle('Disarm', code=code)
  94.  
  95.     async def arm_home(self, code=None):
  96.         """ Send arm home command """
  97.         return await self.alarm_toggle('Partial', code=code)
  98.  
  99.     async def arm_away(self, code=None):
  100.         """ Send arm away command """
  101.         return await self.alarm_toggle('Total', code=code)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement