naofireblade / homebridge-weather-plus

A comprehensive weather plugin for homebridge.
MIT License
311 stars 61 forks source link

Add Light Level history/graphing support #287

Closed dacarson closed 3 months ago

dacarson commented 4 months ago

With this PR now merged to fakegato Added lux to accessoryType 'custom' , this plugin should be able to support Light Level history in the Eve app.

dacarson commented 4 months ago

I have made this work with two changes:

  1. Add the logging to index.js:277 lux: accessory.LightLevelService ? accessory.LightLevelService.getCharacteristic(Characteristic.CurrentAmbientLightLevel).value : accessory.CurrentConditionsService.getCharacteristic(Characteristic.CurrentAmbientLightLevel).value

  2. Switch from 'weather' to 'custom', in currentConditions.js:151 but also do this:

    this.services = [this.CurrentConditionsService];
    this.historyService = new FakeGatoHistoryService("custom", this, this.config.fakegatoParameters);
    this.services = this.getServices();

The 2nd part doesn't seem right. When examining the code in FakeGato, it looks at all the Services that are part of the Accessory. And then for each Characteristic of each Services it checks to see if it can create a history entry for it. If so, it does.

The challenge is the historyService is part of the Accessory and if I don't override the value of this.service it crashes. It crashes because when it walks the list of Services, it reaches the historyService which is undefined at that moment.

I'm wondering if there is different way Services should be registered with the Accessory.

dacarson commented 4 months ago

This is what it currently looks like: IMG_4935

dacarson commented 4 months ago

Worked out an elegant fix. Following the pattern in index.js that has an array of Accessories: this.accessoriesList = [];

In currentConditions.js, create an array of services, aka this.services = []; Then add each Service as they are created, aka: this.services.push(this.CurrentConditionsService); later this.services.concat(compatibility.getServices(this)); and later: this.services.push(this.informationService); and lastly do this:

    this.historyService = new FakeGatoHistoryService("custom", this, this.config.fakegatoParameters);
    this.services.push(this.historyService);

Change getServices() function to just return the services object:

    getServices: function ()
    {
        return this.services;
    }

This way, only fully defined Service objects exist in the services array. FakeGatoHistoryService is now happy.

wimleers commented 4 months ago

Very interesting!