UNPKG

homebridge-hivehome-control

Version:

Allows control of Hive devices, including water heaters, through Homebridge.

77 lines 3.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getHiveDeviceList = exports.updateHiveData = exports.startHiveSession = exports.translateModeForRequest = void 0; const path_1 = require("path"); const pythonia_1 = require("pythonia"); const log_1 = require("../util/log"); // eslint-disable-next-line max-len const hive_api_1 = require("./hive-api"); // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore: this is the recommended way to exit pythonia process.on('exit', () => pythonia_1.python.exit()); // Translate a mode to the format suitable for a request to the server. function translateModeForRequest(mode) { return (mode === hive_api_1.HeatingMode.kOn ? hive_api_1.HeatingMode.kManual : mode); } exports.translateModeForRequest = translateModeForRequest; // Start a new Hive session using pyhiveapi. async function startHiveSession(config) { // Add our local pylib directory to the python path and import pyhiveapi. await (await (0, pythonia_1.python)('sys')).path.append((0, path_1.join)(__dirname, '..', '..', 'pylib')); const pyhiveapi = await (0, pythonia_1.python)('pyhiveapi'); // Start the Hive session with the specified username and password. const hiveSession = await pyhiveapi.Hive$({ username: config.hiveUsername, password: config.hivePassword, }); // Tell the auth object about the device if we have already registered it. hiveSession.auth[hive_api_1.PyHiveAuth.kDevGroupKey] = config.deviceGroupKey; hiveSession.auth[hive_api_1.PyHiveAuth.kDevKey] = config.deviceKey; hiveSession.auth[hive_api_1.PyHiveAuth.kDevPassword] = config.devicePassword; // Perform the login. const login = await hiveSession.login(); // If the device is already registered, log in using its information. const challengeName = await login.get(hive_api_1.kChallengeName); if (challengeName === hive_api_1.DEVICE_LOGIN_REQUIRED) { await hiveSession.deviceLogin(); } else { log_1.Log.error('Could not log in. Are you sure the device is registered?'); log_1.Log.debug('Login replied with:', challengeName); return null; } // Set the minimum interval between refreshes from the server. await hiveSession.updateInterval(hive_api_1.kScanIntervalSecs); // Return the logged-in session object. await hiveSession.startSession(); return hiveSession; } exports.startHiveSession = startHiveSession; // Update Hive data and catch any exceptions that occur. async function updateHiveData(hiveSession, hiveDevice) { try { return await hiveSession.updateData(hiveDevice); } catch (ex) { log_1.Log.debug('Error updating Hive data:', ex); } return false; } exports.updateHiveData = updateHiveData; // Retrieve a list of all valid hot water and heating devices from Hive. async function getHiveDeviceList(hiveSession) { const deviceList = []; // The PyHiveType enum lists the device categories used by the python library. for (const deviceType of [hive_api_1.PyHiveType.kHeating, hive_api_1.PyHiveType.kHotWater]) { for await (const hiveDevice of await hiveSession.deviceList[deviceType]) { // HiveType specifies Hive native categories, a subset of PyHiveType. // Ensure the device is one of the categories supported by this plugin. if (hive_api_1.HiveTypeName[await hiveDevice[hive_api_1.HiveData.kType]]) { deviceList.push(hiveDevice); } } } return deviceList; } exports.getHiveDeviceList = getHiveDeviceList; //# sourceMappingURL=hive-helpers.js.map