Giter Club home page Giter Club logo

Comments (9)

jasonmadigan avatar jasonmadigan commented on September 26, 2024 1

d'oh, thanks for spotting @jouwdan

@daviss57 this is fixed in v1.0.6, which I've just published.

from ha-hkc.

jouwdan avatar jouwdan commented on September 26, 2024

After more troubleshooting:

I am able to get system status from API

image

But when logging the raw API response for get_all_inputs it seems to give me a 'no panel comms' error (even though it's working fine on the HKC app)

All Inputs:
Raw API Response: {'success': False, 'message': 'No panel comms', 'resultCode': 4}

I'll keep troubleshooting, see if I can figure it out.

from ha-hkc.

jouwdan avatar jouwdan commented on September 26, 2024

Think I have it - changed secureCommAddress from securecomm.hkc.ie to hkc.securecomm.cloud and getting a response! I'll Put in a PR to the repo.

image

from ha-hkc.

jouwdan avatar jouwdan commented on September 26, 2024

PR to pyhkc: jasonmadigan/pyhkc#1

from ha-hkc.

daviss57 avatar daviss57 commented on September 26, 2024

PR to pyhkc: jasonmadigan/pyhkc#1

Hi Jordan,

I'm having the exact same issue. Since the pyton files were updated it does not seem to have fixed my issue, just wondering do I need to do an extra step to get it working. Tried re-downloading the HACS integration but it does not seem to have made a difference.

Do you have any info that would help me debug, my skils are basic enough.

It does seem to be getting some type of response but no entities and just remains Unavailalbe.

2024-04-08 11:16:41.081 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 7.170 seconds (success: True)
2024-04-08 11:17:51.649 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 10.737 seconds (success: True)
2024-04-08 11:19:00.157 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 8.245 seconds (success: True)
2024-04-08 11:20:11.718 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 11.806 seconds (success: True)
2024-04-08 11:21:19.445 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 7.533 seconds (success: True)
2024-04-08 11:22:29.116 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 10.203 seconds (success: True)
2024-04-08 11:23:35.847 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 6.936 seconds (success: True)
2024-04-08 11:24:36.497 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 0.585 seconds (success: True)
2024-04-08 11:25:36.892 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 0.980 seconds (success: True)
2024-04-08 11:26:46.335 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 9.424 seconds (success: True)
2024-04-08 11:27:54.902 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 8.979 seconds (success: True)
2024-04-08 11:29:03.932 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 9.021 seconds (success: True)
2024-04-08 11:30:14.146 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 10.234 seconds (success: True)
2024-04-08 11:31:19.523 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 5.612 seconds (success: True)
2024-04-08 11:32:28.540 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 9.628 seconds (success: True)
2024-04-08 11:33:32.498 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 4.587 seconds (success: True)
2024-04-08 11:34:42.422 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 10.510 seconds (success: True)
2024-04-08 11:35:53.941 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 12.030 seconds (success: True)
2024-04-08 11:36:58.788 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 4.874 seconds (success: True)
2024-04-08 11:37:59.523 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 0.611 seconds (success: True)
2024-04-08 11:39:08.964 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 10.048 seconds (success: True)

from ha-hkc.

jouwdan avatar jouwdan commented on September 26, 2024

PR to pyhkc: jasonmadigan/pyhkc#1

Hi Jordan,

I'm having the exact same issue. Since the pyton files were updated it does not seem to have fixed my issue, just wondering do I need to do an extra step to get it working. Tried re-downloading the HACS integration but it does not seem to have made a difference.

Do you have any info that would help me debug, my skils are basic enough.

It does seem to be getting some type of response but no entities and just remains Unavailalbe.

2024-04-08 11:16:41.081 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 7.170 seconds (success: True) 2024-04-08 11:17:51.649 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 10.737 seconds (success: True) 2024-04-08 11:19:00.157 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 8.245 seconds (success: True) 2024-04-08 11:20:11.718 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 11.806 seconds (success: True) 2024-04-08 11:21:19.445 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 7.533 seconds (success: True) 2024-04-08 11:22:29.116 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 10.203 seconds (success: True) 2024-04-08 11:23:35.847 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 6.936 seconds (success: True) 2024-04-08 11:24:36.497 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 0.585 seconds (success: True) 2024-04-08 11:25:36.892 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 0.980 seconds (success: True) 2024-04-08 11:26:46.335 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 9.424 seconds (success: True) 2024-04-08 11:27:54.902 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 8.979 seconds (success: True) 2024-04-08 11:29:03.932 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 9.021 seconds (success: True) 2024-04-08 11:30:14.146 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 10.234 seconds (success: True) 2024-04-08 11:31:19.523 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 5.612 seconds (success: True) 2024-04-08 11:32:28.540 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 9.628 seconds (success: True) 2024-04-08 11:33:32.498 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 4.587 seconds (success: True) 2024-04-08 11:34:42.422 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 10.510 seconds (success: True) 2024-04-08 11:35:53.941 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 12.030 seconds (success: True) 2024-04-08 11:36:58.788 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 4.874 seconds (success: True) 2024-04-08 11:37:59.523 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 0.611 seconds (success: True) 2024-04-08 11:39:08.964 DEBUG (MainThread) [custom_components.hkc_alarm.alarm_control_panel] Finished fetching hkc_alarm_data data in 10.048 seconds (success: True)

Hey

Just checked and i'm actually importing the python lib locally - from my own debugging. Can you try this and see if this resolves it?

in /homassistant/custom_components/hkc_alarm create hkc_api.py

import requests
import os
from tabulate import tabulate

class HKCAlarm:
  def __init__(self, panel_id, panel_password, user_code, base_url="https://hkc.api.securecomm.cloud"):
    self.base_url = base_url
    self.panel_id = panel_id
    self.panel_password = panel_password
    self.user_code = user_code
    self.headers = {
      "Host": "hkc.api.securecomm.cloud",
      "accept": "application/json, text/plain, */*",
      "content-type": "application/json;charset=utf-8",
      "user-agent": "okhttp/4.9.2"
    }
    self.securecomm_address = ""
    self.hardware_id = ""  
    self._initialize()

  def _initialize(self):
    self.securecomm_address = self.get_system_status().get('secureCommAddress', self.securecomm_address)
    self.hardware_id = self._get_hardware_id()

  def register_mobile(self, app_version="1.0.2", hardware_id="", description=""):
    data = {
      "appType": 5,
      "appVersion": app_version,
      "deviceId": "0",
      "panelList": [{"panelId": self.panel_id, "description": description, "options": 2}],
      "hardwareId": hardware_id,
      "soundlist": []
    }
    return self._mobile_register(data)

  def get_system_status(self):
    data = {
      "panelId": self.panel_id,
      "panelPassword": self.panel_password,
      "userCode": self.user_code,
      "includeDescriptions": True
    }
    response = self._get_status(data)
    return response

  def arm_partset_a(self):
    return self._arm_or_disarm(command=1, block=0)

  def arm_partset_b(self):
    return self._arm_or_disarm(command=2, block=0)

  def arm_fullset(self):
    return self._arm_or_disarm(command=3, block=0)

  def disarm(self):
    return self._arm_or_disarm(command=0, block=0)

  def fetch_logs(self, num_previous_logs=10):
    latest_event_id = self._get_latest_event_id()
    logs = []
    while len(logs) < num_previous_logs:
        if latest_event_id is None:
            break
        start_event_id = latest_event_id - 4  # Since each request fetches 5 logs
        data = {
            "panelId": self.panel_id,
            "panelPassword": self.panel_password,
            "secureCommAddress": self.securecomm_address,
            "panelEventId": start_event_id
        }
        logs_chunk = self._get_logs(data)
        logs.extend(logs_chunk)
        latest_event_id = start_event_id - 1  # Decrement for the next batch
    return logs[:num_previous_logs]  # Return only the desired number of logs

  def get_all_inputs(self):
    all_inputs = []
    more_inputs = True
    first_input = 1

    while more_inputs:
      data = {
        "panelId": self.panel_id,
        "panelPassword": self.panel_password,
        "userCode": self.user_code,
        "firstInput": first_input,
        "secureCommAddress": self.securecomm_address
      }
      inputs_response = self._get_inputs(data)

      current_inputs = inputs_response.get("inputs", [])
      all_inputs.extend(current_inputs)
      more_inputs = inputs_response.get("moreInputs", False)
      
      # If there are more inputs, update the first_input for the next call.
      if more_inputs and current_inputs:
        first_input = current_inputs[-1].get("input", 1) + 1

    return all_inputs

  def check_login(self):
      system_status = self.get_system_status()
      # Check for a successful login
      if 'userOptions' in system_status:
          return True
      # Check for an unsuccessful login
      elif 'success' in system_status and system_status['success'] is False:
          return False
      # In case the response format is neither of the above, 
      # you might want to log an error or raise an exception
      else:
          raise Exception('Unexpected response format from get_system_status')

  def get_panel(self):
      data = {
        "panelId": self.panel_id,
        "panelPassword": self.panel_password,
        "keys": "",
        "isKeypadEnabled": False,
        "secureCommAddress": self.securecomm_address
      }
      url = f"{self.base_url}/Panel/RemoteKeypad/"
      response = requests.post(url, headers=self.headers, json=data)
      keypad_data = response.json()
      return keypad_data

  # Private methods for direct API calls

  def _api_request(self, method, url, data=None):
    try:
        response = requests.request(method, url, headers=self.headers, json=data)
        response.raise_for_status()
        return response.json()
    except requests.exceptions.RequestException as e:
        return {"error": str(e)}

  def _mobile_register(self, data):
    return self._api_request("POST", f"{self.base_url}/Registration/MobileRegister", data)

  def _get_status(self, data):
    return self._api_request("POST", f"{self.base_url}/v2/Panel/Status", data)

  def _arm_or_disarm(self, command, block):
    data = {
      "panelId": self.panel_id,
      "panelPassword": self.panel_password,
      "userCode": self.user_code,
      "command": command,
      "block": block,
      "inhibit": False,
      "hardwareId": self.hardware_id,
      "secureCommAddress": self.securecomm_address
    }
    return self._api_request("POST", f"{self.base_url}/Panel/Arming", data)

  def _get_logs(self, data):
    return self._api_request("POST", f"{self.base_url}/v2/Panel/Logs", data)

  def _get_inputs(self, data):
    return self._api_request("POST", f"{self.base_url}/v2/Device/Inputs", data)

  def _get_hardware_id(self):
    data = {
      "panelId": self.panel_id,
      "panelPassword": self.panel_password,
      "keys": "",
      "isKeypadEnabled": False,
      "secureCommAddress": self.securecomm_address
    }
    url = f"{self.base_url}/Panel/RemoteKeypad/"
    response = requests.post(url, headers=self.headers, json=data)
    keypad_data = response.json()
    return keypad_data.get('hardwareId', '')
  
  def _get_latest_event_id(self):
    return self._api_request("POST", f"{self.base_url}/v2/Panel/Log", {
        "panelId": self.panel_id,
        "panelPassword": self.panel_password,
        "secureCommAddress": self.securecomm_address
    }).get('eventId', None)

if __name__ == '__main__':
  # Sample values for initialization - you would replace these with your actual values.
  panel_id_sample = 100000
  panel_password_sample = "your_site_password"
  user_code_sample = 9999

  # Optional environment variables - use them if available.
  panel_id = int(os.environ.get("HKC_PANEL_ID", panel_id_sample))
  panel_password = os.environ.get("HKC_PANEL_PASSWORD", panel_password_sample)
  user_code = int(os.environ.get("HKC_USER_CODE", user_code_sample))
  alarm_system = HKCAlarm(panel_id, panel_password, user_code)

  panel_data = alarm_system.get_panel()
  print(panel_data)

  # Assuming HKCAlarm is already initialized as alarm_system
  # login_check = alarm_system.check_login()
  # print(login_check)  # Outputs: True or False
  
  status = alarm_system.get_system_status()

  print("System Status:")
  print("+-------------------+--------------------------------+")
  print("| Key               | Value                          |")
  print("+===================+================================+")
  for key, value in status.items():
      if not isinstance(value, (list, dict)):
          print(f"| {key.ljust(17)} | {str(value).ljust(30)} |")
  print("+-------------------+--------------------------------+\n")

  print("\nAll Inputs:")
  inputs = alarm_system.get_all_inputs()
  headers = ["Input", "Input ID", "Description", "Input State", "Input Type", "Timestamp", "Action Inhibit", "Camera ID"]
  table_data = [[input_data[key] for key in ["input", "inputId", "description", "inputState", "inputType", "timestamp", "actionInhibit", "cameraId"]] for input_data in inputs]
  print(tabulate(table_data, headers=headers, tablefmt='grid'))

  print("\nRecent Logs:")
  logs = alarm_system.fetch_logs()
  headers = ["Event ID", "Message", "Alarm", "Fault", "Date", "Verification", "Event Action", "Type", "Number"]
  table_data = [[log[key] for key in ["eventId", "message", "alarm", "fault", "date", "verification", "eventAction", "type", "number"]] for log in logs]
  print(tabulate(table_data, headers=headers, tablefmt='grid'))

  # alarm_system.arm_fullset()
  # alarm_system.disarm()

then update your imports in __init__.py and config_flow.py to from .hkc_api import HKCAlarm and reboot.

from ha-hkc.

daviss57 avatar daviss57 commented on September 26, 2024

Unreal

Generated all the sensors.

Not to push my luck but is it possible to control the alarm from here or only monitoring?

Thanks

image

from ha-hkc.

jouwdan avatar jouwdan commented on September 26, 2024

Nice one. @jasonmadigan looks like the latest code updates aren't pulling for new installs by the looks of the above ^

from ha-hkc.

jouwdan avatar jouwdan commented on September 26, 2024

Unreal

Generated all the sensors.

Not to push my luck but is it possible to control the alarm from here or only monitoring?

Thanks

image

You should be able to arm/disarm using the built in alarm control panel card. It's the only way our family interacts with the alarm now.

from ha-hkc.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.