Skip to content

Hobbit44/HA-JStoMQTT

Repository files navigation

HA-JStoMQTT

Checks

This repository enables you to write Home Assistant entities in Javascript that can perform arbitrary operations or fetch statuses or values in a familiar language without having to use input booleans or additional helpers.

This library uses MQTT to provide the discovery data to Home Assistant whilst you provide handlers for the operations of the entities your trying to create.

Usage

Connect to MQTT

Its built to be compatible to the MQTT.js library which you initialise and pass as a parameter to the Classes for whichever entity you want to create.

import { connectAsync as connect } from "mqtt"

const mqttClient = await connect("tcp://localhost:1883", {})

Switch

import { Switch } from 'hass-jstomqtt'

const customswitch = new Switch(mqttClient, "atestSwitch")

customswitch.on(() => {
  console.log("This runs when the switch turns ON")
})

customswitch.off(() => {
  console.log("This runs when the switch turns OFF")
})

Binary sensor

import { BinarySensor } from 'hass-jstomqtt'

const customsensor = new BinarySensor(mqttClient, "atestsensor")

// do something
customswitch.on()

// do something else
customswitch.off()

Button

import { Button } from 'hass-jstomqtt'

const customButton = new Button(
  client,
  "atestbutton",
)

customButton.press(() => {
  console.log("Button Pressed")
})

Devices

import { Button, Switch } from 'hass-jstomqtt'

const deviceConfig = {
  name: "atestdevice",
  identifiers: "atestdevice",
}

const customswitch = new Switch(client, "atestSwitch", {
  device: deviceConfig,
})

const customButton = new Button(client, "atestbutton", {
  device: deviceConfig,
})

Todo

Tasks

  • CI
  • Docs
    • API
    • Deployment
    • Development
  • Hass add-on

Entity types

  • Switch
  • Binary sensor
  • Sensors
  • Button
  • Event
  • Light?

Others

  • Device support