Coder Social home page Coder Social logo

dangreco / zupre Goto Github PK

View Code? Open in Web Editor NEW
14.0 1.0 9.0 124 KB

HA boilerplate card using Zustand, Preact, Styled Components

TypeScript 83.29% JavaScript 15.79% Shell 0.92%
home-assistant lovelace lovelace-card lovelace-custom-card zustand preact

zupre's Introduction

zupre

A ~55kb boilerplate card for Home Assistant.

Stack:

Hooks

useEntity(entityId: string)


Retrieves an entity by ID from the Home Assistant state. The entity will be undefined if the state of HA is not loaded or if the entity does not exist.

Returns: HassEntity | undefined

Example:

...

const Card = () => {
  const sun = useEntity('sun.sun'); // sun: HassEntity | undefined

  return (
    <div style={{ padding: '1rem' }}>
      <p>{ sun?.attributes.friendly_name }</p>
      <p>
        State:
        {' '}
        { sun?.state }
      </p>
    </div>
  );
};

...

Screenshot-2022-03-10-at-15-57-11-Overview-Home-Assistant.png

useEntities(entityIds: string[])


Retrieves a record of entities by their IDs.

Returns: Record<string, HassEntity | undefined>

Example:

...

const Card = () => {
  const lights = useEntities([
    'light.lamp',
    'light.ceiling',
    'light.outside',
  ]); // lights: Record<string, HassEntity | undefined>

  return (
    <div style={{ padding: '1rem' }}>
      <pre>
        { JSON.stringify(lights, null, 2) }
      </pre>
    </div>
  );
};

...

useHistory(entityId: string, config?: HistoryConfig)


Retrieves the history for an entity's state. The optional config parameter takes two fields:

  • start: Date - the starting time/date to base the history on
  • end: Date - the ending time/date to base the history on

By default, this period is the last 24hrs.

Types:

type Datum = { last_changed: Date, state: string }

interface HistoryConfig = {
  start?: Date;
  end?: Date;
}

Returns: { history: Datum[]; entity: HassEntity | undefined }

Example:

...

const Card = () => {
  const { history, entity } = useConfig(); // { history: Datum[]; entity?: HassEntity }

  return (
    <div style={{ padding: '1rem' }}>
      <p>{ entity?.attributes.friendly_name }:</p>
      <p>{ entity?.state }</p>
      <p>
        Changed
        {' '}
        { history.length }
        {' '}
        times.
      </p>
    </div>
  );
};

...

useUser()


Retrieves the currently signed in user and its corresponding entity.

Returns: (CurrentUser & { entity: HassEntity | undefined }) | undefined

Example:

...

const Card = () => {
  const user = useUser(); // user: (CurrentUser & { entity: HassEntity | undefined }) | undefined

  return (
    <div style={{ padding: '1rem' }}>
      <p>{ user?.name }</p>
      <img src={user?.entity?.attributes.entity_picture || ''} />
    </div>
  );
};

...

useHass()


Retrieves current Home Assistant instance. Useful for API/service calls.

Returns: HomeAssistant | undefined

Example:

...

const Card = () => {
  const hass = useHass(); // hass: HomeAssistant | undefined

  return (
    <div style={{ padding: '1rem' }}>
      <button
        onClick={
          hass
            ? hass.callService('homeassistant', 'restart')
            : () => {}
        }
      >
        Restart
      </button>
    </div>
  );
};

...

useConfig()


Retrieves the current config of the card. Note that Config is a type should be customized to the given card's use case.

Returns: Config | undefined

Example:

...

const Card = () => {
  const config = useConfig(); // config: Config | undefined

  return (
    <div style={{ padding: '1rem' }}>
      <p>{ config?.type }</p>
    </div>
  );
};

...

Screenshot-2022-03-10-at-15-58-27-Overview-Home-Assistant.png

zupre's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

zupre's Issues

Styled Components Issue

Hi,

Attempting to use your boiler plate code but running in to an issue with the styled components, seems to be related to the target={this}.

The error: An error occurred. See https://git.io/JUIaE#17 for more information..

This is on a fresh HA dev install in docker and making no changes to your boilerplate code.

Zustand store collision

Issue

When there's multiple of the same card on one dashboard, the Zustand states interfere with one another, because they are global singletons.

Fix

Switch to React Context and use separate providers for config and hass (to prevent unnecessary config re-renders when hass updates).
I did this in my project and it fixed the issue. If you are interested, I could create another pull request for this change; or correct me if I did something wrong please. Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.