# homeassistant-react-hooks

> A library providing hooks to easily integrate data from home assistant

Latest version **0.1.14** (published 2023-07-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install homeassistant-react-hooks
pnpm add homeassistant-react-hooks
yarn add homeassistant-react-hooks
bun add homeassistant-react-hooks
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.14 |
| Published | 2023-07-14 |
| First published | 2021-12-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 17 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Alve Svarén |
| Maintainers | alvesvaren |
| Keywords | homeassistant, react-hooks, react, hass |

## Links

- npm: https://www.npmjs.com/package/homeassistant-react-hooks
- Repository: https://github.com/alvesvaren/homeassistant-react-hooks
- npm.io page: https://npm.io/package/homeassistant-react-hooks

## Dependencies (6)

- [react](https://npm.io/package/react.md) ^18.2.0
- [events](https://npm.io/package/events.md) ^3.3.0
- [react-dom](https://npm.io/package/react-dom.md) ^18.2.0
- [usehooks-ts](https://npm.io/package/usehooks-ts.md) ^2.9.1
- [object-assign](https://npm.io/package/object-assign.md) ^4.1.1
- [homeassistant-ws](https://npm.io/package/homeassistant-ws.md) ^0.2.2

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.1.14 (latest) — 2023-07-14
- 0.1.13 — 2023-06-01
- 0.1.12 — 2022-08-06
- 0.1.11 — 2022-01-10
- 0.1.10 — 2022-01-03
- 0.1.9 — 2022-01-03
- 0.1.8 — 2021-12-27
- 0.1.7 — 2021-12-27
- 0.1.6 — 2021-12-27
- 0.1.5 — 2021-12-27
- 0.1.4 — 2021-12-26
- 0.1.3 — 2021-12-26
- 0.1.2 — 2021-12-26
- 0.1.1 — 2021-12-26
- 0.1.0 — 2021-12-26
- … 4 more at https://npm.io/package/homeassistant-react-hooks/versions

## README

# HomeAssistant React Hooks

This is a collection of react hooks and apis to get state / push events to home assistant using websockets.
It can be used to for example create custom dashboards that integrate with home assistant.

[Package on NPM](https://www.npmjs.com/package/homeassistant-react-hooks)

## Installation

You can use this library by running either `yarn add homeassistant-react-hooks` or `npm i homeassistant-react-hooks`

### Token

To create a token that can be used with this, go to your [user profile](https://my.home-assistant.io/redirect/profile/) and scroll down to the bottom where you can create a token.

## Examples

### Code example

```tsx
import { HassProvider, useHassDevice } from "homeassistant-react-hooks";

// This should be loaded from an external file (to not expose secrets in for example version control)
const config = {
    token: "HOME_ASSISTANT_ACCESS_TOKEN",
    host: "example.com",
};

const MediaPlayer = () => {
    const { title, artist, imgUrl } = useMediaPlayer("kitchen");

    return (
        <div className="media-player">
            <h1>{title} - {artist}</h1>
            <img src={imgUrl} alt="Artwork" />
        </div>
    );
}

const App = () => {
    return (
        <HassProvider token={config.token} connectionOptions={{ host: config.host, port: 443, protocol: "wss" }}>
            <MediaPlayer />
        </HassProvider>
    );
};
```

### Projects using this module

 - [alvesvaren/pihubv3](https://github.com/alvesvaren/pihubv3)

## Hooks

To see what each hook returns, use your editor's intellisense (because I'm too lazy to add all of them to this list)

### `useHassDevice`

Access a "raw" home assistant device, with access to all attributes and data, create using the full entity id, for example `media_player.kitchen`

### `useLight`

Access a light in home assistant. This is probably missing some fields because my lights that I can control with home assistant does not support color so I can't test that. Feel free to create a pull request fixing it if you want.

Do not include the domain in the entity id, for example `bedroom_ceiling_1` instead of `light.bedroom_ceiling_1`

### `useMediaPlayer`

Access a cleaned up / processed media player object containing things like position and artwork url etc.

Do not include the domain in the entity id, for example `bedroom_ceiling_1` instead of `light.bedroom_ceiling_1`

### `useWeather`

Access a weather object with some added fields (like minimum temperature etc.)

This might not work if the weather source isn't setting the first day forecast to today, something I know for example the SMHI source is doing wrong.

---
_Source: https://npm.io/package/homeassistant-react-hooks · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
