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

Add support for service responses #131

Merged

Conversation

EdLeckert
Copy link
Contributor

As discussed in Issue #130, the core HA team intends to eliminate large entity attribute values in favor of service call return values. This PR allows flex-table-card to populate itself via service call responses.

Consider this example from the National Weather Service nws, a weather service integration:

ForecastView

The config for this view is:

type: custom:flex-table-card
entities:
  - weather.kboi_daynight
columns:
  - name: Detailed Forecast
    data: forecast
    modify: x.detailed_description
  - name: Time Valid
    data: forecast
    modify: new Date(x.datetime).toLocaleString()
  - name: Precipitation Probability
    data: forecast
    modify: x.precipitation_probability
    suffix: '%'
  - name: Condition
    data: forecast
    modify: x.condition
  - name: Temperature
    data: forecast
    modify: x.temperature
    suffix: °
  - name: Dew Point
    data: forecast
    modify: x.dew_point
    suffix: °
  - name: Humidity
    data: forecast
    modify: x.humidity
    suffix: '%'
  - name: Wind Speed
    data: forecast
    modify: x.wind_speed
    suffix: mph
  - name: Wind Bearing
    data: forecast
    modify: x.wind_bearing
    suffix: °

An identical view can be achieved by loading from a service call simply by adding these lines to the config:

service: weather.get_forecasts
service_data:
  type: twice_daily

This works for a list of entities as well.

Also consider the todo integration. Entities do not contain task information as attributes, so the only way for flex-table-card to populate itself is via a service call.

TodoView

The config for this is:

type: custom:flex-table-card
service: todo.get_items
service_data:
  status: needs_action
entities:
  - todo.service_test
columns:
  - name: Summary
    data: items
    modify: x.summary
  - name: Needs Action
    data: items
    modify: if (x.status == "needs_action") {"Yes"} else {"No"}

The card will populate itself on load and whenever the listed entities are updated. This works fine for the weather integration, since the entity will update frequently with current conditions, causing the card to call the service and refresh itself.

@daringer
Copy link
Collaborator

looks pretty good to me, would be good to have this method for getting entities mentioned in the documentation - maybe also with the explicit mention that setting config.service overrides the "classic" way of including/selecting data

@EdLeckert
Copy link
Contributor Author

While it's not possible to anticipate how this will behave with future services, it occurred to me that there should be some attempt made to have this work with services that do not need entities. So I developed a script to return data and found that a small change needed to be made for this type of case. The script is:

test_response:
  alias: Test Response
  variables:
    family: >
        {% set myfamily = { "family": [
          {
            "name" : "Emil",
            "year" : 2004
          },
          {
            "name" : "Tobias",
            "year" : 2007
          },
          {
            "name" : "Linus",
            "year" : 2011
          }
        ] }
        %}
        {{ myfamily }}
  sequence:
    - stop: All Done
      response_variable: family

The config. Note that entities is still required:

type: custom:flex-table-card
service: script.test_response
entities: []
columns:
  - name: Name
    data: family
    modify: x.name
  - name: Birth Year
    data: family
    modify: x.year

The result:
Untitled

Will update the docs next.

@daringer daringer merged commit e967490 into custom-cards:master Mar 18, 2024
1 check passed
@EdLeckert EdLeckert deleted the feat-add-support-for-service-responses branch March 18, 2024 16:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants