yyrkoon94 / lovelace-idf-mobilite

Lovelace Card for IDF Mobilité (Bus, Métro, RER)
19 stars 7 forks source link

IHM Bug with Rest Sensor with availability #8

Closed jmorille closed 7 months ago

jmorille commented 7 months ago

when putting a availability option on the REST sensor, with a reference to a input_boolean switch

- platform: rest
  # https://github.com/yyrkoon94/lovelace-idf-mobilite
  name: prim_my_station
  unique_id: my_unique_id
  resource: https://prim.iledefrance-mobilites.fr/marketplace/stop-monitoring
  method: GET
  params:
    #  <-- mandatory : the StopArea or StopPoint you want to follow (cf next section)
    # https://data.iledefrance-mobilites.fr/explore/dataset/arrets/custom/
    MonitoringRef: "STIF:StopArea:SP:XXXXX:"
  headers:
    apiKey: "mySuperApiKeyPerson"
  scan_interval: 30
  timeout: 30
  value_template: "OK"
  json_attributes:
    - Siri
  availability: "{{ is_state('input_boolean.mobility_horaire_enabled', 'on')}}"

WIth input_boolean

mobility_horaire_enabled:
  name: Horaires
  icon: mdi:bus

WHen switch are OFF, the data are not populated anymore, and a bug crash the JS with this message

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'attributes')

When activate the input_boolean switch ON, the IHM not display the data because the js is in crash error So the the IHM not refresh anymore

Should be not enougth test in method https://github.com/yyrkoon94/lovelace-idf-mobilite/blob/main/dist/idf-mobilite.js#L91

    createBUSContent(lineDatas, exclude_lines, exclude_lines_ref, second_entity) {
        if (!lineDatas && !lineDatas.attributes['Siri'].ServiceDelivery.StopMonitoringDelivery[0].ResponseTimestamp)
            return html``
jmorille commented 7 months ago

A quick code review, and the error should be in testing

https://github.com/yyrkoon94/lovelace-idf-mobilite/blob/main/dist/idf-mobilite.js#L91 This code variant should be more secure

    createRERContent(lineDatas, exclude_lines, exclude_lines_ref, second_entity) {
        const messagesList = this.hass.states[this.config.messages];
        if (!lineDatas?.attributes['Siri']  || !lineDatas.attributes['Siri']?.ServiceDelivery?.StopMonitoringDelivery[0].ResponseTimestamp)
            return html``

        // Last update date

and https://github.com/yyrkoon94/lovelace-idf-mobilite/blob/main/dist/idf-mobilite.js#L235 could be fix with

    createBUSContent(lineDatas, exclude_lines, exclude_lines_ref, second_entity) {
        if (!lineDatas?.attributes['Siri'] || !lineDatas.attributes['Siri']?.ServiceDelivery?.StopMonitoringDelivery[0].ResponseTimestamp)
            return html``

        // Last update date
jmorille commented 7 months ago

The pull request for this fi proposal https://github.com/yyrkoon94/lovelace-idf-mobilite/pull/9

jmorille commented 7 months ago

The fix work image

jmorille commented 7 months ago

image When activated ...

API Error, is because no more traffic after this time May be another fix for a better display

yyrkoon94 commented 7 months ago

Hi,

Thanks for the debug and the fix. AND instead of OR, a classic one !

I merged your fix and made a new version v0.1.11

jmorille commented 7 months ago

I updated to the version 0.1.11, and it work well

Thank you for your work.