cr3ative / homebridge-apcaccess

what if apcaccess, but in homebridge??
MIT License
8 stars 4 forks source link

Push data to HK, rather than wait for it to be pulled #18

Closed davidjbradshaw closed 1 year ago

davidjbradshaw commented 1 year ago

This PR makes the following changes

davidjbradshaw commented 1 year ago

I have it working again now, hope this isn't too much change.

Next steps:

davidjbradshaw commented 1 year ago

Trying to workout what this does?

const battVal = this.latestJSON.BCHARGE;
const battArray = battVal.split('.');
battPctValue = parseFloat(parseFloat(battArray[0] * -1) * -1);

and why it might be different to this:

const percentage = parseInt(this.latestJSON.BCHARGE, 10);
cr3ative commented 1 year ago

Trying to workout what this does?


const battVal = this.latestJSON.BCHARGE;

const battArray = battVal.split('.');

battPctValue = parseFloat(parseFloat(battArray[0] * -1) * -1);

and why it might be different to this:


const percentage = parseInt(this.latestJSON.BCHARGE, 10);

In previous issues, it was found that this level can (but doesn't always) come through as "90.56%" which breaks parseInt. Another oddity of stability.

davidjbradshaw commented 1 year ago

In previous issues, it was found that this level can (but doesn't always) come through as "90.56%" which breaks parseInt. Another oddity of stability.

parseInt("90.56%", 10) // = 90

That doesn't make any sense and if that is the case why did it only break it in one of two places you where calling it?

davidjbradshaw commented 1 year ago

If the issue is that you want the percentages, then this would work better than parseInt().

parseFloat("90.56% and some other stuff") // = 90.56
davidjbradshaw commented 1 year ago

I just read #10 and he never says what the issue is that his weird change fixes

cr3ative commented 1 year ago

I just read #10 and he never says what the issue is that his weird change fixes

Happy to change it, it was a bit odd at the time.

davidjbradshaw commented 1 year ago

I've put some guard rails around it

  parseBatteryLevel  = () =>  this.loaded ? parseInt(this.latestJSON.BCHARGE, 10) : 0;
  parseTimeLeft = () => this.loaded ? parseInt(this.latestJSON.TIMELEFT, 10) : 0;
davidjbradshaw commented 1 year ago

battery data is now being pushed to HK in real time and I also spotted a bug, in that the low battery warning was attached to the ContactSensorService, rather than the BatteryService.

[25/02/2023, 16:47:23] [APCAccess] Launched child bridge with PID 3895089
[25/02/2023, 16:47:24] Registering accessory 'homebridge-apcaccess.APCAccess'
[25/02/2023, 16:47:24] [APCAccess] Loaded homebridge-apcaccess v0.2.2-rc4 child bridge successfully
[25/02/2023, 16:47:24] Loaded 0 cached accessories from cachedAccessories.0EB7A5DE703D.
[25/02/2023, 16:47:24] [APCAccess] Connected!
[25/02/2023, 16:47:24] Homebridge v1.6.0 (HAP v0.11.0) (APCAccess) is running on port 33875.
[25/02/2023, 16:47:25] [APCAccess] Power: Disconnected
[25/02/2023, 16:47:25] [APCAccess] Battery state: Normal
[25/02/2023, 16:47:25] [APCAccess] Charging state: NOT_CHARGING
[25/02/2023, 16:47:25] [APCAccess] Battery Level: 63% (75 estimated minutes remaining)
[25/02/2023, 16:48:51] [APCAccess] Battery Level: 62% (74 estimated minutes remaining)
[25/02/2023, 16:49:34] [APCAccess] Battery Level: 62% (70 estimated minutes remaining)
davidjbradshaw commented 1 year ago

I've just updated the top with the changes I've made. This might be enough for one PR.

I don't know what to do with the temp sensor, as I have no way of testing it. I could just be careful, but do you have someone that can test that code before release?

Still to do it adding in the Model, S/N and Firmeare to HK from the data once it is loaded in. My thinking is we just put placeholder data in at start up and then on the first pull of the data, the real information can be a single time push.

Thoughts?

cr3ative commented 1 year ago

I think careful pushes are fine without explicit testing. I'll take a look at this on Monday on the big screens - likely I'll merge, test it a little, do some very minor tweaks, then release.

Thank you for your care for this little plugin!

davidjbradshaw commented 1 year ago

Tried to get it to update the accessory information once we have the data, but not seeing it push through to the home app. I’ve asked a question about this on Stack Overflow.

davidjbradshaw commented 1 year ago

I think I'm going to leave this here now, don't really want to play with the temp stuff without being able to test it (the patten from the battery level should mostly be the same, if you want to make it realtime).

It's been useful, I think I have learnt enough about home bridge to write my own plugin from playing with this.

This is almost a verified plugin now, I think the only outstanding task is to implement the forms config for the web UI.

cr3ative commented 1 year ago

Sure thing! I'll consider it RFR now and look as soon as I can. :)

davidjbradshaw commented 1 year ago

Fixed typos, Tested by manually copying files to my rPi and restarting plugin. Then killed the power for a movement. You can see the effect of buying cheap replacement batteries :)

[01/03/2023, 10:29:30] [APCAccess] Restarting Process...
[01/03/2023, 10:29:31] [APCAccess] Launched child bridge with PID 491
[01/03/2023, 10:29:31] Registering accessory 'homebridge-apcaccess.APCAccess'
[01/03/2023, 10:29:31] [APCAccess] Loaded homebridge-apcaccess v0.2.2-rc4 child bridge successfully
[01/03/2023, 10:29:31] Loaded 0 cached accessories from cachedAccessories.0EB7A5DE703D.
[01/03/2023, 10:29:31] [APCAccess] Connected!
[01/03/2023, 10:29:31] Homebridge v1.6.0 (HAP v0.11.0) (APCAccess) is running on port 33875.
[01/03/2023, 10:29:32] [APCAccess] Power: Connected
[01/03/2023, 10:29:32] [APCAccess] Battery state: Normal
[01/03/2023, 10:29:32] [APCAccess] Charging state: CHARGING
[01/03/2023, 10:29:32] [APCAccess] Model: Smart-UPS 750
[01/03/2023, 10:29:32] [APCAccess] Name: home-bridge-1
[01/03/2023, 10:29:32] [APCAccess] SerialNumber: AS1539123101
[01/03/2023, 10:29:32] [APCAccess] SoftwareRevision: 3.14.14 (31 May 2016) debian
[01/03/2023, 10:29:32] [APCAccess] FirmwareRevision: UPS 09.3 / ID=18
[01/03/2023, 10:29:33] [APCAccess] Charging state: NOT_CHARGING
[01/03/2023, 10:29:33] [APCAccess] Battery Level: 100%  27.0 Volts (120 estimated minutes remaining)
[01/03/2023, 10:30:41] [APCAccess] Power: Disconnected
[01/03/2023, 10:30:42] [APCAccess] Battery Level: 97%  27.0 Volts (118 estimated minutes remaining)
[01/03/2023, 10:30:44] [APCAccess] Battery Level: 91%  27.0 Volts (109 estimated minutes remaining)
[01/03/2023, 10:30:46] [APCAccess] Battery Level: 89%  25.4 Volts (106 estimated minutes remaining)
[01/03/2023, 10:30:49] [APCAccess] Battery Level: 84%  25.4 Volts (101 estimated minutes remaining)
[01/03/2023, 10:30:51] [APCAccess] Battery Level: 80%  25.0 Volts (96 estimated minutes remaining)
[01/03/2023, 10:30:53] [APCAccess] Battery Level: 76%  25.0 Volts (91 estimated minutes remaining)
[01/03/2023, 10:30:55] [APCAccess] Battery Level: 73%  25.0 Volts (87 estimated minutes remaining)
[01/03/2023, 10:31:05] [APCAccess] Power: Connected
[01/03/2023, 10:31:05] [APCAccess] Charging state: CHARGING
cr3ative commented 1 year ago

Fixed typos, Tested by manually copying files to my rPi and restarting plugin. Then killed the power for a movement. You can see the effect of buying cheap replacement batteries :)

[01/03/2023, 10:29:30] [APCAccess] Restarting Process...
[01/03/2023, 10:29:31] [APCAccess] Launched child bridge with PID 491
[01/03/2023, 10:29:31] Registering accessory 'homebridge-apcaccess.APCAccess'
[01/03/2023, 10:29:31] [APCAccess] Loaded homebridge-apcaccess v0.2.2-rc4 child bridge successfully
[01/03/2023, 10:29:31] Loaded 0 cached accessories from cachedAccessories.0EB7A5DE703D.
[01/03/2023, 10:29:31] [APCAccess] Connected!
[01/03/2023, 10:29:31] Homebridge v1.6.0 (HAP v0.11.0) (APCAccess) is running on port 33875.
[01/03/2023, 10:29:32] [APCAccess] Power: Connected
[01/03/2023, 10:29:32] [APCAccess] Battery state: Normal
[01/03/2023, 10:29:32] [APCAccess] Charging state: CHARGING
[01/03/2023, 10:29:32] [APCAccess] Model: Smart-UPS 750
[01/03/2023, 10:29:32] [APCAccess] Name: home-bridge-1
[01/03/2023, 10:29:32] [APCAccess] SerialNumber: AS1539123101
[01/03/2023, 10:29:32] [APCAccess] SoftwareRevision: 3.14.14 (31 May 2016) debian
[01/03/2023, 10:29:32] [APCAccess] FirmwareRevision: UPS 09.3 / ID=18
[01/03/2023, 10:29:33] [APCAccess] Charging state: NOT_CHARGING
[01/03/2023, 10:29:33] [APCAccess] Battery Level: 100%  27.0 Volts (120 estimated minutes remaining)
[01/03/2023, 10:30:41] [APCAccess] Power: Disconnected
[01/03/2023, 10:30:42] [APCAccess] Battery Level: 97%  27.0 Volts (118 estimated minutes remaining)
[01/03/2023, 10:30:44] [APCAccess] Battery Level: 91%  27.0 Volts (109 estimated minutes remaining)
[01/03/2023, 10:30:46] [APCAccess] Battery Level: 89%  25.4 Volts (106 estimated minutes remaining)
[01/03/2023, 10:30:49] [APCAccess] Battery Level: 84%  25.4 Volts (101 estimated minutes remaining)
[01/03/2023, 10:30:51] [APCAccess] Battery Level: 80%  25.0 Volts (96 estimated minutes remaining)
[01/03/2023, 10:30:53] [APCAccess] Battery Level: 76%  25.0 Volts (91 estimated minutes remaining)
[01/03/2023, 10:30:55] [APCAccess] Battery Level: 73%  25.0 Volts (87 estimated minutes remaining)
[01/03/2023, 10:31:05] [APCAccess] Power: Connected
[01/03/2023, 10:31:05] [APCAccess] Charging state: CHARGING

Outstanding! This is amazing work. Going to merge - feel free to tweak further in a new PR, let's land this.

cr3ative commented 1 year ago

Published: + homebridge-apcaccess@1.0.0

davidjbradshaw commented 1 year ago

Just installed it ok, but you need to update the CHANGELOG when you have a moment.

cr3ative commented 1 year ago

Just installed it ok, but you need to update the CHANGELOG when you have a moment.

Perfect, that's done.

Sent something your way. Get them done! https://secure.ups-trader.co.uk/rebuild-kits/57-rbc48-kit.html?search_query=rbc48&results=5

davidjbradshaw commented 1 year ago

Oh wow thank you, I wasn't expecting that.