@switchbot/homebridge-switchbot
Version:
The SwitchBot plugin allows you to access your SwitchBot device(s) from HomeKit.
249 lines • 11 kB
HTML
<p class="text-center">
<img
src="https://raw.githubusercontent.com/OpenWonderLabs/homebridge-switchbot/latest/branding/Homebridge_x_SwitchBot.svg"
alt="homebridge-switchbot logo" style="width: 50%" />
</p>
<div id="pageIntro" style="display: none">
<p class="lead text-center">Thank you for installing <strong>homebridge-switchbot</strong></p>
<p class="lead text-center">Before continuing:</p>
<ol>
<li class="mb-3">
Download the SwitchBot App from either the
<a href="https://apps.apple.com/us/app/switchbot/id1087374760" target="_blank" rel="noreferrer noopener">App
Store</a> or the
<a href="https://play.google.com/store/apps/details?id=com.theswitchbot.switchbot&hl=en_US&gl=US" target="_blank"
rel="noreferrer noopener">Play Store</a>.
</li>
<li class="mb-3">Select <strong>Profile</strong>.</li>
<li class="mb-3">Select <strong>Preferences</strong>.</li>
<li class="mb-3">Tap the <strong>App version</strong> 10 times, this will enable Developer Options.</li>
<li class="mb-3">Select <strong>Developer Options</strong>.</li>
<li class="mb-3">Copy your token by hitting <strong>Copy to Clipboard</strong>.</li>
<li class="mb-3">Paste the Token into the Token parameter on the next page.</li>
</ol>
<div class="text-center">
<button type="button" class="btn btn-primary" id="introLink">Continue →</button>
</div>
</div>
<div id="menuWrapper" class="btn-group w-100 mb-0" role="group" aria-label="UI Menu" style="display: none">
<button type="button" class="btn btn-primary" id="menuSettings">Settings</button>
<button type="button" class="btn btn-primary" id="menuDevices">Devices</button>
<button type="button" class="btn btn-primary mr-0" id="menuHome">Support</button>
</div>
<div id="disabledBanner" class="alert alert-secondary mb-0 mt-3" role="alert" style="display: none">
Plugin is currently disabled
<button id="disabledEnable" type="button" class="btn btn-link p-0 m-0 float-right">Enable</button>
</div>
<div id="pageDevices" class="mt-4" style="display: none">
<div id="deviceInfo">
<form>
<div class="form-group">
<select class="form-control" id="deviceSelect"></select>
</div>
</form>
<table class="table w-100" id="deviceTable" style="display: none">
<thead>
<tr class="table-active">
<th scope="col" style="width: 40%">Device Name</th>
<th scope="col" style="width: 60%" id="displayName"></th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Device ID</th>
<td id="deviceId"></td>
</tr>
<tr>
<th scope="row">Model</th>
<td id="model"></td>
</tr>
<tr>
<th scope="row">Firmware Version</th>
<td id="version"></td>
</tr>
<tr>
<th scope="row">Device Type</th>
<td id="deviceType"></td>
</tr>
<tr>
<th scope="row">Connection Type</th>
<td id="connectionType"></td>
</tr>
</tbody>
</table>
<p class="text-center">External Accessories Will Not Display Here.</p>
</div>
</div>
<div id="pageSupport" class="mt-4" style="display: none">
<p class="text-center lead">Thank you for using <strong>homebridge-switchbot</strong></p>
<p class="text-center">The links below will take you to our GitHub wiki</p>
<h5>Setup</h5>
<ul>
<li>
<a href="https://github.com/OpenWonderLabs/homebridge-switchbot/wiki" target="_blank">Wiki Home</a>
</li>
<li>
<a href="https://github.com/OpenWonderLabs/homebridge-switchbot/wiki/Configuration"
target="_blank">Configuration</a>
</li>
<li>
<a href="https://github.com/OpenWonderLabs/homebridge-switchbot/wiki/Beta-Version" target="_blank">Beta
Version</a>
</li>
<li>
<a href="https://github.com/OpenWonderLabs/homebridge-switchbot/wiki/Node-Version" target="_blank">Node
Version</a>
</li>
<li>
<a href="https://github.com/OpenWonderLabs/homebridge-switchbot/wiki/Uninstallation"
target="_blank">Uninstallation</a>
</li>
</ul>
<h5>Features</h5>
<ul>
<li>
<a href="https://github.com/OpenWonderLabs/homebridge-switchbot/wiki/Supported-Devices" target="_blank">Supported
Devices</a>
</li>
</ul>
<h5>Help/About</h5>
<ul>
<li>
<a href="https://github.com/OpenWonderLabs/homebridge-switchbot/issues/new/choose" target="_blank">Support
Request</a>
</li>
<li>
<a href="https://github.com/OpenWonderLabs/homebridge-switchbot/blob/latest/CHANGELOG.md"
target="_blank">Changelog</a>
</li>
</ul>
</div>
<script>
(async () => {
try {
const currentConfig = await homebridge.getPluginConfig();
showIntro = () => {
const introLink = document.getElementById('introLink');
introLink.addEventListener('click', () => {
homebridge.showSpinner();
document.getElementById('pageIntro').style.display = 'none';
document.getElementById('menuWrapper').style.display = 'inline-flex';
showSettings();
homebridge.hideSpinner();
});
document.getElementById('pageIntro').style.display = 'block';
};
showDevices = async () => {
homebridge.showSpinner();
homebridge.hideSchemaForm();
document.getElementById('menuHome').classList.remove('btn-elegant');
document.getElementById('menuHome').classList.add('btn-primary');
document.getElementById('menuDevices').classList.add('btn-elegant');
document.getElementById('menuDevices').classList.remove('btn-primary');
document.getElementById('menuSettings').classList.remove('btn-elegant');
document.getElementById('menuSettings').classList.add('btn-primary');
document.getElementById('pageSupport').style.display = 'none';
document.getElementById('pageDevices').style.display = 'block';
const cachedAccessories =
typeof homebridge.getCachedAccessories === 'function'
? await homebridge.getCachedAccessories()
: await homebridge.request('/getCachedAccessories');
if (cachedAccessories.length > 0) {
cachedAccessories.sort((a, b) => {
return a.displayName.toLowerCase() > b.displayName.toLowerCase() ? 1 : b.displayName.toLowerCase() > a.displayName.toLowerCase() ? -1 : 0;
});
}
const deviceSelect = document.getElementById('deviceSelect');
deviceSelect.innerHTML = '';
cachedAccessories.forEach((a) => {
const option = document.createElement('option');
option.text = a.displayName;
option.value = a.UUID;
deviceSelect.add(option);
});
showDeviceInfo = async (UUID) => {
homebridge.showSpinner();
const thisAcc = cachedAccessories.find((x) => x.UUID === UUID);
const context = thisAcc.context;
document.getElementById('displayName').innerHTML = thisAcc.displayName;
document.getElementById('deviceId').innerHTML = context.deviceId;
document.getElementById('model').innerHTML = context.model;
document.getElementById('version').innerHTML = context.version;
document.getElementById('deviceType').innerHTML = context.deviceType;
document.getElementById('connectionType').innerHTML = context.connectionType;
document.getElementById('refreshRate').innerHTML = context.refreshRate;
document.getElementById('updateRate').innerHTML = context.updateRate;
document.getElementById('pushRate').innerHTML = context.pushRate;
document.getElementById('deviceTable').style.display = 'inline-table';
homebridge.hideSpinner();
};
deviceSelect.addEventListener('change', (event) => showDeviceInfo(event.target.value));
if (cachedAccessories.length > 0) {
showDeviceInfo(cachedAccessories[0].UUID);
} else {
const option = document.createElement('option');
option.text = 'No Devices';
deviceSelect.add(option);
deviceSelect.disabled = true;
}
homebridge.hideSpinner();
};
showSupport = () => {
homebridge.showSpinner();
homebridge.hideSchemaForm();
document.getElementById('menuHome').classList.add('btn-elegant');
document.getElementById('menuHome').classList.remove('btn-primary');
document.getElementById('menuDevices').classList.remove('btn-elegant');
document.getElementById('menuDevices').classList.add('btn-primary');
document.getElementById('menuSettings').classList.remove('btn-elegant');
document.getElementById('menuSettings').classList.add('btn-primary');
document.getElementById('pageSupport').style.display = 'block';
document.getElementById('pageDevices').style.display = 'none';
homebridge.hideSpinner();
};
showSettings = () => {
homebridge.showSpinner();
document.getElementById('menuHome').classList.remove('btn-elegant');
document.getElementById('menuHome').classList.add('btn-primary');
document.getElementById('menuDevices').classList.remove('btn-elegant');
document.getElementById('menuDevices').classList.add('btn-primary');
document.getElementById('menuSettings').classList.add('btn-elegant');
document.getElementById('menuSettings').classList.remove('btn-primary');
document.getElementById('pageSupport').style.display = 'none';
document.getElementById('pageDevices').style.display = 'none';
homebridge.showSchemaForm();
homebridge.hideSpinner();
};
showDisabledBanner = () => {
document.getElementById('disabledBanner').style.display = 'block';
};
enablePlugin = async () => {
homebridge.showSpinner();
document.getElementById('disabledBanner').style.display = 'none';
currentConfig[0].disablePlugin = false;
await homebridge.updatePluginConfig(currentConfig);
await homebridge.savePluginConfig();
homebridge.hideSpinner();
};
menuHome.addEventListener('click', () => showSupport());
menuDevices.addEventListener('click', () => showDevices());
menuSettings.addEventListener('click', () => showSettings());
disabledEnable.addEventListener('click', () => enablePlugin());
if (currentConfig.length) {
document.getElementById('menuWrapper').style.display = 'inline-flex';
showSettings();
if (currentConfig[0].disablePlugin) {
showDisabledBanner();
}
} else {
currentConfig.push({ name: 'SwitchBot' });
await homebridge.updatePluginConfig(currentConfig);
showIntro();
}
} catch (err) {
homebridge.toast.error(err.message, 'Error');
} finally {
homebridge.hideSpinner();
}
})();
</script>