TP-Link Smart Home API

5.0.0 · active · verified Tue Apr 21

This library provides a programmatic interface for controlling TP-Link Kasa smart home devices directly over the local network. It supports a wide range of devices including smart plugs (HS100, HS110, KP303), smart bulbs (LB100, KL125), and light strips (KL430), enabling local control without reliance on the TP-Link cloud services. The current stable version is 5.0.0, which targets Node.js environments version 16 and higher. While no explicit release cadence is documented, major version bumps suggest significant updates and potential breaking changes. A key differentiator is its focus on local LAN communication, offering increased privacy and potentially faster response times compared to cloud-dependent solutions. It ships with full TypeScript type definitions for an enhanced development experience, but importantly, does not support TP-Link Tapo devices.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate the client, connect to a specific Kasa smart device by IP address, retrieve its system information, toggle its power state, and adjust brightness for bulbs. It also shows how to use the client's discovery feature to find devices on the local network and react to new devices coming online.

import { Client } from 'tplink-smarthome-api';

const client = new Client();
const deviceIp = process.env.KASA_DEVICE_IP ?? '192.168.1.100'; // Replace with your device's IP

async function controlDevice() {
  console.log(`Attempting to connect to device at ${deviceIp}...`);
  try {
    const device = await client.getDevice({ host: deviceIp });
    const sysInfo = await device.getSysInfo();
    console.log(`Connected to ${sysInfo.alias} (${sysInfo.model})`);

    const initialState = await device.getPowerState();
    console.log(`Current power state: ${initialState ? 'ON' : 'OFF'}`);

    const newState = !initialState;
    await device.setPowerState(newState);
    console.log(`Set power state to: ${newState ? 'ON' : 'OFF'}`);

    if (device.deviceType === 'bulb' && 'setBrightness' in device) {
      const currentBrightness = await (device as any).getBrightness();
      console.log(`Current brightness: ${currentBrightness}%`);
      await (device as any).setBrightness(50);
      console.log('Set brightness to 50%');
    }
  } catch (error) {
    console.error('Error controlling device:', error instanceof Error ? error.message : error);
  }
}

async function discoverDevices() {
  console.log('Starting device discovery for 10 seconds...');
  client.startDiscovery({ discoveryInterval: 1000, discoveryTimeout: 10000 });

  client.on('device-new', async (device) => {
    const sysInfo = await device.getSysInfo();
    console.log(`[Discovery] Found new device: ${sysInfo.alias} (${sysInfo.host})`);
    // Example: Turn on newly discovered smart plugs
    if (device.deviceType === 'plug') {
      await device.setPowerState(true);
      console.log(`[Discovery] Turned on plug: ${sysInfo.alias}`);
    }
  });

  client.on('device-online', (device) => console.log(`[Discovery] Device online: ${device.host}`));
  client.on('device-offline', (device) => console.log(`[Discovery] Device offline: ${device.host}`));

  setTimeout(() => {
    client.stopDiscovery();
    console.log('Discovery stopped.');
  }, 10000);
}

// Run both examples
controlDevice();
discoverDevices();

view raw JSON →