Skip to content

koproductions-code/homeassistant_ws

Repository files navigation

homeassistant_ws

This package provides an interface to communicate with HomeAssistant using the websocket protocol. In addition, it provides a way to integrate this interface into flutter applications using provider

Features

  • Listen for state changes for specific HomeAssistant entities.
  • Integrate into flutter widgets using the provider package.

Getting started

Dependencies:

provider: ^6.1.2

Usage

Usage with Flutter

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:homeassistant_ws/flutter/homeassistant_ws_flutter.dart'; // <-- If you want to use the flutter functionality, you need to import this subpackage.

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        home: HomeAssistantBuilder(
            host: "homeassistant.koproductions.dev",
            port: 443,
            entities: ["[Your Entity IDs]"],
            child: EntityWidget(
              entityId: "[EntityID]",
            )));
  }
}

class EntityWidget extends StatefulWidget {
  final String entityId;

  const EntityWidget({super.key, required this.entityId});

  @override
  EntityWidgetState createState() => EntityWidgetState();
}

class EntityWidgetState extends State<EntityWidget> {
  String state = "unknown";

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    final provider = Provider.of<HomeAssistantProvider>(context);

    provider.socket?.subscribe(widget.entityId, update);
  }

  void update(HAEntityState data) {
    setState(() {
      state = data.state;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Text(state);
  }
}

About

A dart package for interacting with HomeAssistant using WebSocket.

Resources

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages