"""
Lock device
"""

from .overview import Overview

OVERVIEW_URL = '/remotecontrol'
COMMAND_URL = '/remotecontrol/lockunlock.cmd'
AUTORELOCK_URL = '/settings/autorelock/'
SETAUTORELOCK_URL = '/settings/setautorelock.cmd'


class Lock(object):
    """ Lock device

    Args:
        session (verisure.session): Current session
    """

    def __init__(self, session):
        self._session = session

    def get(self):
        """ Get device overview """
        status = self._session.get(OVERVIEW_URL)
        return [Overview('lock', val) for val in status
                if val['type'] == 'DOOR_LOCK']

    def set(self, code, device_id, state):
        """ set status of alarm component

            Args:
                code (str): Personal alarm code (four digits)
                device_id (str): lock device id
                state (str): 'LOCKED', or 'UNLOCKED'

        """
        data = {
            'code': code,
            'deviceLabel': device_id,
            'state': state
        }
        return not self._session.post(COMMAND_URL, data)

    def get_autorelock(self):
        """ Get autorelock status """
        return self._session.get(AUTORELOCK_URL)

    def set_autorelock(self, device_id, autorelock):
        """ Set autorelock enabled

            Args:
                device_id (str): lock device id
                autorelock (bool): True to set, else False
        """
        if autorelock:
            data = {
                "_csrf": self._session.csrf,
                "_doorLockDevices[0].autoRelockEnabled": "on",
                "doorLockDevices[0].autoRelockEnabled": "true",
                "enabledDoorLocks": device_id
            }
        else:
            data = {
                "_csrf": self._session.csrf,
                "_doorLockDevices[0].autoRelockEnabled": "on",
                "enabledDoorLocks": ""
            }
        return not self._session.post(SETAUTORELOCK_URL, data)

@@ -12,16 +12,28 @@
 vice%0A%22%22%22
+%0Aimport time
 %0A%0Afrom .
@@ -227,16 +227,62 @@
 ck.cmd'%0A
+CHECK_STATE = '/remotecontrol/checkstate.cmd'%0A
 %0A%0Aclass 
@@ -1984,20 +1984,725 @@
 TORELOCK_URL, data)%0A
+%0A    def wait_while_pending(self, max_request_count=100):%0A        %22%22%22 Wait for pending lockstatus to finish%0A%0A            Args:%0A                max_request_count (int): maximum number of post requests%0A%0A            Returns: retries if success else -1%0A%0A        %22%22%22%0A%0A        for counter in range(max_request_count):%0A            data = %7B'counter': counter%7D%0A            response = self._session.json_to_dict(%0A                self._session.post(CHECK_STATE, data))%0A            if 'hasResult' not in response:%0A                break%0A            if 'hasPending' in response and not response%5B'hasPending'%5D:%0A                return counter%0A            counter = counter + 1%0A            time.sleep(1)%0A        return -1%0A

Add wait_while_pending for lock