Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Gauge Card to Lovelace #1742

Merged
merged 8 commits into from
Oct 11, 2018
Merged

Conversation

zsarnett
Copy link
Contributor

@zsarnett zsarnett commented Oct 5, 2018

Adds a new card type gauge to the available cards

Card uses the Previously made Custom Card located here

Configuration Variables

Name Type Default Description
type string Required gauge
entity string Required sensor.my_temperature
title string optional Name to display on card
unit_of_measurement string optional If not set, uses the unit_of_measurement on the entity
min number 0 Minimum value for graph
max number 100 Maximum value for graph
severity object optional Severity object. See below

Severity Variables

Name Type Default Description
red number Required Value from which to start red color
green number Required Value from which to start green color
yellow number Required Value from which to start amber color

Example

- type: gauge
  title: Testing
  entity: input_number.slider1
  unit_of_measurement: '%'
  min: 0
  max: 100
  severity:
    red: 85
    green: 0
    amber: 45

Open to any advice on more efficient code and changes to the card. First pull request to any Repo.

@homeassistant
Copy link
Contributor

Hi @zsarnett,

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@ghost ghost added the in progress label Oct 5, 2018
@balloob
Copy link
Member

balloob commented Oct 6, 2018

  • Please revert the change to yarn.lock
  • Please add an entry to the gallery

@zsarnett zsarnett changed the title Adding Gauge Card to Lovelace WIP: Adding Gauge Card to Lovelace Oct 6, 2018
@zsarnett
Copy link
Contributor Author

zsarnett commented Oct 6, 2018

@balloob
How would you suggest allowing an attribute of an Entity to be the data source for the Gauge?

Ex. climate.ecobee.attributes.current_temperature

Allowing that as the entity and in the card splitting the entity from the attribute?

- type: gauge
  entity: climate.ecobee.attributes.current_temperature

or
Creating a new variable to hold the attribute

- type: gauge
  entity: climate.ecobee
  attribute: current_temperature

@thomasloven
Copy link
Contributor

I'd go for the second alternative, simply because climate.ecobee.attributes.current_temperature is not an entity. That's also much more easy to parse in a readable (read maintainable) way.

src/panels/lovelace/cards/hui-gauge-card.js Outdated Show resolved Hide resolved
src/panels/lovelace/cards/hui-gauge-card.js Outdated Show resolved Hide resolved
src/panels/lovelace/cards/hui-gauge-card.js Outdated Show resolved Hide resolved
src/panels/lovelace/cards/hui-gauge-card.js Outdated Show resolved Hide resolved
src/panels/lovelace/cards/hui-gauge-card.js Outdated Show resolved Hide resolved
src/panels/lovelace/cards/hui-gauge-card.js Outdated Show resolved Hide resolved
src/panels/lovelace/cards/hui-gauge-card.js Show resolved Hide resolved
src/panels/lovelace/cards/hui-gauge-card.js Show resolved Hide resolved
src/panels/lovelace/cards/hui-gauge-card.js Outdated Show resolved Hide resolved
src/panels/lovelace/cards/hui-gauge-card.js Outdated Show resolved Hide resolved
@zsarnett zsarnett changed the title WIP: Adding Gauge Card to Lovelace Adding Gauge Card to Lovelace Oct 8, 2018
@jeradM
Copy link
Member

jeradM commented Oct 9, 2018

I'd rather see the stateObj be a computed and observed property. Then you won't need to observe hass since _hassChanged() isn't doing anything else anyway:

_stateObj: {
  type: Object,
  computed: '_computeStateObj(hass.states, _config.entity)'
  observer: '_stateObjChanged'
}

then:

_computeStateObj(states, entityId) {
  if (!states || !entityId) return null;
  return states[entityId];
}

_stateObjChanged(stateObj) {
  # Check that stateObj exists an do all your updates to the gauge, etc...
}

Then you can also update the template to observe _stateObj to determine the state/title:

<div class="gauge-data">
  <div id="percent">[[_computeStateDisplay(_stateObj)]]</div>
  <div id="title">[[_computeTitle(_stateObj)]]</div>
</div>

Then in _computeStateDisplay/_computeTitle, you choose to either display state/title or some kind of error message

src/panels/lovelace/cards/hui-gauge-card.js Outdated Show resolved Hide resolved
src/panels/lovelace/cards/hui-gauge-card.js Outdated Show resolved Hide resolved
src/panels/lovelace/cards/hui-gauge-card.js Outdated Show resolved Hide resolved
src/panels/lovelace/common/create-card-element.js Outdated Show resolved Hide resolved
@zsarnett
Copy link
Contributor Author

zsarnett commented Oct 9, 2018

Then you can also update the template to observe _stateObj to determine the state/title:

<div class="gauge-data">
  <div id="percent">[[_computeStateDisplay(_stateObj)]]</div>
  <div id="title">[[_computeTitle(_stateObj)]]</div>
</div>

When using the above, the gauge data functions aren't getting called when the state obj changes.

@jeradM
Copy link
Member

jeradM commented Oct 9, 2018

Did you define a computed _stateObj property? Is _computeStateObj() getting called and _stateObj getting set correctly?

@zsarnett
Copy link
Contributor Author

zsarnett commented Oct 9, 2018

Did you define a computed _stateObj property? Is _computeStateObj() getting called and _stateObj getting set correctly?

  static get properties() {
    return {
      hass: {
        type: Object
      },
      _config: Object,
      _stateObj: {
        type: Object,
        computed: '_computeStateObj(hass.states, _config.entity)',
        observer: '_stateObjChanged'
      },
    };
  }

The _computeStateObj is getting called and _stateObj assisgned Correctly

@zsarnett
Copy link
Contributor Author

zsarnett commented Oct 9, 2018

Disregard previous comments as it now functions properly. May have been a caching issue.

@balloob
Copy link
Member

balloob commented Oct 11, 2018

Demo page looks slick!

this.$.title.className = 'not-found';
return 'Entity is non-numeric: ' + this._config.entity;
}
return this._config.title;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should reset the className

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw it's kinda a hack to have a _computeFunction have side effects, should actually be done in _stateChanged, however it's good enough for initial merge.

ha-card {
--base-unit: 50px;
height: calc(var(--base-unit)*3);
position: relative;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should add cursor: pointer because the card is interactive.

@ghost ghost assigned balloob Oct 11, 2018
@balloob balloob merged commit 69eb007 into home-assistant:master Oct 11, 2018
@balloob
Copy link
Member

balloob commented Oct 11, 2018

Please open a PR to add the docs to the website.

@ghost ghost removed the in progress label Oct 11, 2018
zsarnett added a commit to zsarnett/home-assistant-polymer that referenced this pull request Oct 13, 2018
* Commiting Only needed Files. Adds Gallery Entry

* Adding Attribute current_temperature to gallery entry config

* Fixing code from review and updating gallery

* Updating Gallery to show errors

* Resolving Reviews and updating gallery

* Deleting unused line

* Minor changes

* Address my own comments.
zsarnett added a commit to zsarnett/home-assistant-polymer that referenced this pull request Oct 14, 2018
* Commiting Only needed Files. Adds Gallery Entry

* Adding Attribute current_temperature to gallery entry config

* Fixing code from review and updating gallery

* Updating Gallery to show errors

* Resolving Reviews and updating gallery

* Deleting unused line

* Minor changes

* Address my own comments.
@github-actions github-actions bot locked and limited conversation to collaborators Jul 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants