def create_light_alarm():
    try:
        light_state = LightState.get_instance()
        light_tasks = get_light_tasks_by_user(Settings.get_instance().user_id)
        if light_tasks is not None:
            enabled_tasks = [task for task in light_tasks if task['enabled']]
            __add_new_alarms(light_state, enabled_tasks)
            __remove_cancelled_alarms(light_state, enabled_tasks)
    except Exception as er:
        logging.error(er)
def write_status_to_file(door_number, time):
    file_name = Settings.get_instance().file_name
    try:
        with open(file_name, 'r', encoding='utf-8') as file:
            content = json.load(file)
            content[door_number] = f'{time:%Y-%m-%d %H:%M:%S%z}'

        with open(file_name, "w") as file:
            json.dump(content, file)
    except FileNotFoundError:
        content = {door_number: f'{time:%Y-%m-%d %H:%M:%S%z}'}
        with open(file_name, "w+") as file:
            json.dump(content, file)
Esempio n. 3
0
def get_light_tasks_by_user(user_id):
    base_url = Settings.get_instance().hub_base_url
    try:
        response = requests.get(f'{base_url}/userId/{user_id}/tasks',
                                timeout=5)
        light_response = response.json()
        for task in light_response:
            task['alarm_time'] = None if task.get(
                'alarm_time') is None else time.fromisoformat(
                    task['alarm_time'])
        return light_response
    except Exception:
        return None
Esempio n. 4
0
def get_light_api_key(username, password):
    body = {'devicetype': Automation().APP_NAME}
    auth = base64.b64encode(
        (username + ':' + password).encode('UTF-8')).decode('UTF-8')
    headers = {'Authorization': 'Basic ' + auth}
    try:
        response = requests.post(Settings.get_instance().light_base_url,
                                 data=json.dumps(body),
                                 headers=headers,
                                 timeout=5)
        api_key = response.json()[0]['success']['username']
        return api_key
    except Exception:
        return None
Esempio n. 5
0
 def setup_method(self):
     os.environ.update({'LIGHT_API_USERNAME': self.LIGHT_API_USER, 'LIGHT_API_PASSWORD': self.LIGHT_API_PASSWORD,
                        'USER_ID': self.USER_ID, 'HUB_BASE_URL': self.HUB_BASE_URL, 'LIGHT_BASE_URL': self.LIGHT_BASE_URL})
     self.SETTINGS = Settings.get_instance()
Esempio n. 6
0
 def setup_method(self):
     self.SETTINGS = Settings.get_instance()
     self.STATE = LightState.get_instance()
     self.STATE.LIGHT_ALARMS = []
 def get_light_api_key(self):
     if self.API_KEY is None:
         settings = Settings.get_instance()
         self.API_KEY = get_light_api_key(settings.light_api_user,
                                          settings.light_api_password)
     return self.API_KEY
 def setup_method(self):
     self.SETTINGS = Settings.get_instance().dev_mode = False
     os.environ.update({'LIGHT_BASE_URL': self.BASE_URL, 'HUB_BASE_URL': self.HUB_BASE_URL})
 def setup_method(self):
     self.SETTINGS = Settings.get_instance()
     self.SETTINGS.dev_mode = True
     self.SETTINGS.settings = {'UserId': self.USER_ID}