rsnodgrass / hass-poolmath

Pool Math for Home Assistant
Other
30 stars 9 forks source link

Parsing HTML is no fun, how about json instead? #9

Closed Redth closed 2 years ago

Redth commented 3 years ago

Hey there, this is really cool! I'm the author of Pool Math and stumbled across this. I don't use HASS myself, but I'm very familiar with it (I've got a smartthings hub, homebridge, other home automations...).

Anyway, I see you're scraping HTML, and that's first of all no fun to do, and second of all, while I appreciate the 15 min sleep you have to take it easy on our servers, having JSON would make our servers work even less hard for this 😂

So, I took the liberty of quickly adding support to get this API call back in JSON instead. Just add .json to the end of the API Call:

https://api.poolmathapp.com/share/tfp-168502.json

Cheers!

rsnodgrass commented 3 years ago

JSON would most definitely be preferred! Will try to convert this over to JSON as soon as I have time to do so. Thanks!

Out of curiosity, was this causing any issue for your servers? I hope not in any meaningful way!

On Tue, Mar 30, 2021 at 1:40 PM Jonathan Dick @.***> wrote:

Hey there, this is really cool! I'm the author of Pool Math and stumbled across this. I don't use HASS myself, but I'm very familiar with it (I've got a smartthings hub, homebridge, other home automations...).

Anyway, I see you're scraping HTML, and that's first of all no fun to do, and second of all, while I appreciate the 15 min sleep you have to take it easy on our servers, having JSON would make our servers work even less hard for this 😂

So, I took the liberty of quickly adding support to get this API call back in JSON instead. Just add .json to the end of the API Call:

https://api.poolmathapp.com/share/tfp-168502.json

Cheers!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/rsnodgrass/hass-poolmath/issues/9, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAQY4XGCN7LE2IIPDG2TAWLTGIZNJANCNFSM42C5MKGA .

Redth commented 3 years ago

Nope! not at all! Someone just made me aware of this and I thought it was neat and thought it might be helpful to have JSON. I've done HTML scraping enough myself over the years with this sort of thing and always wished I could get data in a more friendly way :)

srvoets commented 3 years ago

Nope! not at all! Someone just made me aware of this and I thought it was neat and thought it might be helpful to have JSON. I've done HTML scraping enough myself over the years with this sort of thing and always wished I could get data in a more friendly way :)

Thanks for this;

This add-on isnt working for me, but with this info I think I can parse the json into a template sensor, much appreciated.

rsnodgrass commented 3 years ago

This add-on isnt working for me, but with this info I think I can parse the json into a template sensor, much appreciated.

What is not working for you?

srvoets commented 3 years ago

This add-on isnt working for me, but with this info I think I can parse the json into a template sensor, much appreciated.

What is not working for you?

I have a premium account and have configured my config.yml accordingly, and verified the URL in config I can open in my browser.

The sensors never set up in HA:

Logger: homeassistant.components.sensor Source: /usr/local/lib/python3.8/asyncio/events.py:81 Integration: Sensor (documentation, issues) First occurred: 9:07:31 AM (13 occurrences) Last logged: 9:25:16 AM

Setup of sensor platform poolmath is taking over 10 seconds.

I have no poolmath entities

rsnodgrass commented 3 years ago

Can you send your URL to me? Thanks.

On Jun 3, 2021 at 6:27 AM, <deputyd0ng @.***)> wrote:

This add-on isnt working for me, but with this info I think I can parse the json into a template sensor, much appreciated.

What is not working for you?

I have a premium account and have configured my config.yml accordingly, and verified the URL in config I can open in my browser.

The sensors never set up in HA:

` Logger: homeassistant.components.sensor Source: /usr/local/lib/python3.8/asyncio/events.py:81 Integration: Sensor (documentation, issues) First occurred: 9:07:31 AM (13 occurrences) Last logged: 9:25:16 AM

Setup of sensor platform poolmath is taking over 10 seconds. `

— You are receiving this because you commented. Reply to this email directly, view it on GitHub (https://github.com/rsnodgrass/hass-poolmath/issues/9#issuecomment-853867967), or unsubscribe (https://github.com/notifications/unsubscribe-auth/AAQY4XDKHPCQS3752BWXUITTQ57K5ANCNFSM42C5MKGA).

ccpk1 commented 2 years ago

I've been using this component for a while now, but it stopped working when I upgraded to Home Assistant 2021.12. While I appreciate all the effort that was put into this, once I found this thread, I saw it would be just as easy to create REST sensor and template all the entities I wanted. For anyone interested, I'll drop the yaml as an alternative:

sensor:
  - platform: rest
    name: PoolMath JSON
    json_attributes_path: "pools[0].pool"
    json_attributes:
      - overview
    resource: "https://api.poolmathapp.com/share/tfp-168502.json"
    scan_interval: 900
    value_template: "{{ value_json['pools'][0]['pool'].name }}"

template:
  sensor:
    - name: Pool FC
      icon: mdi:gauge
      state: "{{ state_attr('sensor.poolmath_json', 'overview').fc }}"
      unit_of_measurement: ppm
      state_class: measurement
    - name: Pool CC
      icon: mdi:gauge
      state: "{{ state_attr('sensor.poolmath_json', 'overview').cc }}"
      unit_of_measurement: mg/L
      state_class: measurement
    - name: Pool TA
      icon: mdi:gauge
      state: "{{ state_attr('sensor.poolmath_json', 'overview').ta }}"
      unit_of_measurement: ppm
      state_class: measurement
    - name: Pool CH
      icon: mdi:gauge
      state: "{{ state_attr('sensor.poolmath_json', 'overview').ch }}"
      unit_of_measurement: ppm
      state_class: measurement
    - name: Pool Borate
      icon: mdi:gauge
      state: "{{ state_attr('sensor.poolmath_json', 'overview').bor }}"
      unit_of_measurement: ppm
      state_class: measurement
    - name: Pool CYA
      icon: mdi:gauge
      state: "{{ state_attr('sensor.poolmath_json', 'overview').cya }}"
      unit_of_measurement: ppm
      state_class: measurement
    - name: Pool pH
      icon: mdi:gauge
      state: "{{ state_attr('sensor.poolmath_json', 'overview').ph }}"
      unit_of_measurement: pH
      state_class: measurement
    - name: Pool CSI
      icon: mdi:gauge
      state: "{{ state_attr('sensor.poolmath_json', 'overview').csi }}"
      unit_of_measurement: CSI
      state_class: measurement
    - name: Pool Temp
      icon: mdi:temperature-fahrenheit
      state: "{{ state_attr('sensor.poolmath_json', 'overview').waterTemp }}"
      unit_of_measurement:  "°F"
      state_class: measurement
      device_class: temperature

I only added a few of the template sensors I needed, but there is a lot more data points available in the JSON if there are other measurements you want to pull in. Thanks all!

nkm8 commented 2 years ago

For anyone who still wants to use this integration, all you need to do is update the url property in configuration.yaml: Old: https://troublefreepool.com/mypool/XXXX New: https://api.poolmathapp.com/share/XXXX

I didn't open a PR yet, but the readme needs updated and I think the code can be updated to correctly follow the redirect, which would allow both old and new url formats to work.

rsnodgrass commented 2 years ago

0.2.0 now uses json interface