codyc1515 / ha-contact-energy

Contact Energy integration for Home Assistant
MIT License
11 stars 10 forks source link

Not updating #8

Open Krispkiwi opened 7 months ago

Krispkiwi commented 7 months ago

System Health details

System Information

version core-2024.2.2
installation_type Home Assistant OS
dev false
hassio true
docker true
user root
virtualenv false
python_version 3.12.1
os_name Linux
os_version 6.1.63-haos-raspi
arch aarch64
timezone Pacific/Auckland
config_dir /config
Home Assistant Community Store GitHub API | ok -- | -- GitHub Content | ok GitHub Web | ok GitHub API Calls Remaining | 4722 Installed Version | 1.34.0 Stage | running Available Repositories | 1396 Downloaded Repositories | 17
Home Assistant Cloud logged_in | false -- | -- can_reach_cert_server | ok can_reach_cloud_auth | ok can_reach_cloud | ok
Home Assistant Supervisor host_os | Home Assistant OS 11.5 -- | -- update_channel | stable supervisor_version | supervisor-2024.01.1 agent_version | 1.6.0 docker_version | 24.0.7 disk_total | 109.3 GB disk_used | 12.2 GB healthy | true supported | true board | rpi4-64 supervisor_api | ok version_api | ok installed_addons | Advanced SSH & Web Terminal (17.1.0), Studio Code Server (5.15.0), Samba share (12.3.0), Home Assistant Google Drive Backup (0.112.1), Mosquitto broker (6.4.0), RPC Shutdown (2.4), ESPHome (2023.12.9), Cloudflared (5.1.3), Zigbee2MQTT (1.35.3-1), ADB - Android Debug Bridge (0.7.0), MQTT Explorer (browser-1.0.3), Node-RED (17.0.7), Vaultwarden (Bitwarden) (0.21.1), Uptime Kuma (0.12.0)
Dashboards dashboards | 1 -- | -- resources | 2 views | 0 mode | storage
Recorder oldest_recorder_run | February 12, 2024 at 11:55 AM -- | -- current_recorder_run | February 20, 2024 at 12:36 PM estimated_db_size | 452.12 MiB database_engine | sqlite database_version | 3.44.2
Spotify api_endpoint_reachable | ok -- | --

Checklist

Describe the issue

I was just wondering if this sensor still works, it's showing 0.00 for me but the contact app shows 280kwh

Reproduction steps

  1. Just checking the sensor

Debug logs

Just a general question

Diagnostics dump

No response

PeteOlds commented 7 months ago

I'm seeing (not seeing?) the same - no updates coming through, but can't see any errors...

alistair commented 7 months ago

Seeing the following

2024-02-20 20:19:17.088 DEBUG (SyncWorker_15) [custom_components.contact_energy.sensor] Beginning usage update
2024-02-20 20:19:17.088 DEBUG (SyncWorker_15) [custom_components.contact_energy.sensor] We appear to be logged in (lets not verify it for now)
2024-02-20 20:19:17.088 DEBUG (SyncWorker_15) [custom_components.contact_energy.sensor] Fetching usage data
2024-02-20 20:19:17.677 ERROR (SyncWorker_15) [custom_components.contact_energy.api] Failed to fetch usage data for 2024/2/10
2024-02-20 20:19:17.677 DEBUG (SyncWorker_15) [custom_components.contact_energy.api] <Response [502]>
2024-02-20 20:19:18.197 ERROR (SyncWorker_15) [custom_components.contact_energy.api] Failed to fetch usage data for 2024/2/11
2024-02-20 20:19:18.197 DEBUG (SyncWorker_15) [custom_components.contact_energy.api] <Response [502]>
2024-02-20 20:19:18.799 ERROR (SyncWorker_15) [custom_components.contact_energy.api] Failed to fetch usage data for 2024/2/12
2024-02-20 20:19:18.799 DEBUG (SyncWorker_15) [custom_components.contact_energy.api] <Response [502]>
2024-02-20 20:19:19.282 ERROR (SyncWorker_15) [custom_components.contact_energy.api] Failed to fetch usage data for 2024/2/13
2024-02-20 20:19:19.282 DEBUG (SyncWorker_15) [custom_components.contact_energy.api] <Response [502]>
2024-02-20 20:19:19.743 ERROR (SyncWorker_15) [custom_components.contact_energy.api] Failed to fetch usage data for 2024/2/14
2024-02-20 20:19:19.743 DEBUG (SyncWorker_15) [custom_components.contact_energy.api] <Response [502]>
2024-02-20 20:19:20.223 ERROR (SyncWorker_15) [custom_components.contact_energy.api] Failed to fetch usage data for 2024/2/15
2024-02-20 20:19:20.223 DEBUG (SyncWorker_15) [custom_components.contact_energy.api] <Response [502]>
2024-02-20 20:19:20.738 ERROR (SyncWorker_15) [custom_components.contact_energy.api] Failed to fetch usage data for 2024/2/16
2024-02-20 20:19:20.738 DEBUG (SyncWorker_15) [custom_components.contact_energy.api] <Response [502]>
2024-02-20 20:19:21.225 ERROR (SyncWorker_15) [custom_components.contact_energy.api] Failed to fetch usage data for 2024/2/17
2024-02-20 20:19:21.225 DEBUG (SyncWorker_15) [custom_components.contact_energy.api] <Response [502]>
2024-02-20 20:19:21.695 ERROR (SyncWorker_15) [custom_components.contact_energy.api] Failed to fetch usage data for 2024/2/18
2024-02-20 20:19:21.696 DEBUG (SyncWorker_15) [custom_components.contact_energy.api] <Response [502]>
2024-02-20 20:19:22.198 ERROR (SyncWorker_15) [custom_components.contact_energy.api] Failed to fetch usage data for 2024/2/19
2024-02-20 20:19:22.198 DEBUG (SyncWorker_15) [custom_components.contact_energy.api] <Response [502]>
jaymunro commented 7 months ago

I am seeing similar:

2024-02-29 08:23:53.462 WARNING (SyncWorker_4) [homeassistant.loader] We found a custom integration contact_energy which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
2024-02-29 08:24:15.463 WARNING (MainThread) [homeassistant.const] ENERGY_KILO_WATT_HOUR was used from contact_energy, this is a deprecated constant which will be removed in HA Core 2025.1. Use UnitOfEnergy.KILO_WATT_HOUR instead, please create a bug report at https://github.com/codyc15/ha-contact-energy/issues
2024-02-29 08:24:15.782 DEBUG (MainThread) [custom_components.contact_energy.sensor] Setting up sensor(s)...
2024-02-29 08:24:15.797 DEBUG (SyncWorker_0) [custom_components.contact_energy.sensor] Beginning usage update
2024-02-29 08:24:15.798 INFO (SyncWorker_0) [custom_components.contact_energy.sensor] Havent logged in yet, lets login now...
2024-02-29 08:24:17.281 DEBUG (SyncWorker_0) [custom_components.contact_energy.api] Logged in
2024-02-29 08:24:18.072 DEBUG (SyncWorker_0) [custom_components.contact_energy.api] Refreshed session
2024-02-29 08:24:19.065 DEBUG (SyncWorker_0) [custom_components.contact_energy.api] Retrieved accounts
2024-02-29 08:24:19.103 DEBUG (SyncWorker_0) [custom_components.contact_energy.sensor] Fetching usage data
2024-02-29 08:24:28.311 INFO (SyncWorker_0) [custom_components.contact_energy.api] Fetched usage data for 2024/2/27, but got nothing back
2024-02-29 08:24:29.955 INFO (SyncWorker_0) [custom_components.contact_energy.api] Fetched usage data for 2024/2/28, but got nothing back
2024-02-29 08:24:32.963 WARNING (MainThread) [homeassistant.components.sensor] Setup of sensor platform contact_energy is taking over 10 seconds.
Krispkiwi commented 6 months ago

My instance still hasn't pulled data in 2 weeks so I'm not sure if this integration works anymore.

alistair commented 6 months ago

I don't believe this plugin refreshes the login token after first run. So restarting the server might fix this for a time. It will die eventually.

I modified the code locally to just login each time.

hydrocloricacid commented 5 months ago

@alistair how specifically ?

hydrocloricacid commented 5 months ago

In ipython console can auth but not get any info.. Ideas ?

n [4]: import api n [5]: ce=api.ContactEnergyApi(email,passw)

In [6]: ce.login() Out[6]: True

In [7]: ce.refresh_session() Out[7]: True

In [8]: ce.get_accounts()

In [15]: ce.get_usage('2024','3','15') Failed to fetch usage data for 2024/3/15 Out[15]: False

In [16]: ce.get_usage('2024','4','10') Failed to fetch usage data for 2024/4/10 Out[16]: False

In [17]: ce.get_usage('2024','4','20') Failed to fetch usage data for 2024/4/20 Out[17]: False

alistair commented 5 months ago

https://github.com/codyc1515/ha-contact-energy/blob/main/custom_components/contact_energy/sensor.py#L128-L136

my understanding is the only place refresh_session is called is within login. If the api_token is persisted across calls of this plugin then login is only called once.

hydrocloricacid commented 5 months ago

Testing confirmed that calling .refresh_session() did not update _api_token BUT calling login() again did update the _api_key variable.

Although calling get_usage still doesn't get anything. e.g. ## fyi only takes strings, not ints.

In [19]: ce.get_usage('2024','4','20') Failed to fetch usage data for 2024/4/20 Out[19]: False In [35]: ce.get_usage('2024','4','4') Failed to fetch usage data for 2024/4/4 Out[35]: False

hydrocloricacid commented 5 months ago

You are referring to logger.py, I'm going direct to the api to confirm it's operation.

FYI the api.ContactEnergyApi class has the following methods. 'get_accounts', 'get_usage', 'login', 'refresh_session'

I can manually use the code from the get_accounts() and confirm it retrieves our account details. So login & get_accounts works perfectly. :-)

n [56]: result = requests.get(ce._url_base + "/customer/v2?fetchAccounts=true", headers=headers) In [57]: result.status_code Out[57]: 200 In [58]: result.json()

hydrocloricacid commented 5 months ago

Wonder if the api changed ? Calling direct using info from https://pypi.org/project/contact-energy-nz/ and api.ContactEnergyApi.get_usage() method and get an error. (re variable 'ce' earlier posts show how this is created)

In [76]: headers = {"x-api-key": ce._api_key, "session": ce._api_session} In [77]: result = requests.post(ce._url_base + f'/usage/v2/{ce._contractId}?ba={ce._accountId}&interval=monthly&from=2024-04-01&to=2024-04-02', headers=headers)

In [78]: result.status_code Out[78]: 502

In [79]: result.text Out[79]: '{"message": "Internal server error"}'

hydrocloricacid commented 5 months ago

Ok figured it out, api is fine. Issue is it only pulls the first contract and I see that I have 2 contracts with the first 'status': 'INACTIVE' where the second is 'status': 'ACTIVE' which has the needed contract id (moved address). Found the second contract info when examining the account data using the following.

result = requests.get(ce._url_base + "/customer/v2?fetchAccounts=true", headers=headers) result.json()

Fix will be to loop through the contracts list and on;y record the contract id for the active contract.

hydrocloricacid commented 5 months ago
diff --git a/custom_components/contact_energy/api.py b/custom_components/contact_energy/api.py
index df6498c..8b1c1a0 100644
--- a/custom_components/contact_energy/api.py
+++ b/custom_components/contact_energy/api.py
@@ -74,7 +74,10 @@ class ContactEnergyApi:
             _LOGGER.debug("Retrieved accounts")
             data = result.json()
             self._accountId = data["accounts"][0]["id"]
-            self._contractId = data["accounts"][0]["contracts"][0]["contractId"]
+            #self._contractId = data["accounts"][0]["contracts"][0]["contractId"]
+            for ob in data['accounts'][0]['contracts']:
+               if ob['status']=='ACTIVE' :
+                  self._contractId=ob['contractId']
         else:
             _LOGGER.error("Failed to fetch customer accounts %s", result.text)
             return False
hydrocloricacid commented 5 months ago

works from manually calling the api but oddly enough not for HA

jaymunro commented 5 months ago

Fingers crossed that you get it working @hydrocloricacid

notf0und commented 5 months ago

It's my first time using this package and it wasn't working. I updated the hardcoded api key with the one I see my browser is using and with that was enough to get data. Something interesting I found is that is just retrieving data until 4 days earlier than the current date only, so looking at the state of the sensor will always show 0.0 as there is no data for current date