Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use config.json for configuring sensors. #31

Merged
merged 1 commit into from
Oct 29, 2016
Merged

Use config.json for configuring sensors. #31

merged 1 commit into from
Oct 29, 2016

Conversation

jannylund
Copy link
Contributor

This PR adds the functionality to use config file as documented in #30

"platforms": [
        {
            "name": "TellstickDuo",
            "platform": "Telldus-TD-Tool",
            "sensors": [
                {
                    "id": 114,
                    "model": "temperaturehumidity",
                    "name": "Bathroom"
                },
                {
                    "id": 142,
                    "model": "temperature",
                    "name": "Sauna"
                },
                {
                    "id": 74,
                    "model": "temperature",
                    "name": "Freezer"
                }
            ]
        }
    ]
  • Leaving out the sensors array or adding an empty array, it will work as before (Except the removed feature override sensortype, since that is done in config file)
  • Adding a sensors array brings the following features:
    • No false sensors will show up.
    • Setting the name in config.json ensures it's always the same (correct) name.
    • Setting the proper model in config.json allows reconfiguration of false temperaturehumidity as temperature.

Each sensor need to have two mandatory members, id (to identify it uniquely) and model (since we don't know that at this stage).

@jannylund
Copy link
Contributor Author

Updated PR to fix the case where the sensor() method return undefined which renders homebridge dead. This was actually a bug that could happen previously as well, if telldusd was restarted after homebridge.

@pkempe
Copy link

pkempe commented Oct 24, 2016

Using the below config.json, I can't get the filter to work. Sensors which are not listed in the config still show up, e.g. "Thermometer 1", even though the tdtool output for that particular sensor is

type=sensor protocol=mandolyn model=temperaturehumidity id=1 temperature=-50.0 humidity=0 time=2016-10-22 08:01:13 age=222858.

    "platforms": [
    {
     "platform" : "Telldus-TD-Tool",
     "name" : "Telldus-TD-Tool",
         "sensors": [
                {
                    "id": 14,
                    "model": "temperaturehumidity"
                },
                {
                    "id": 21,
                    "model": "temperaturehumidity"
                },
                {
                    "id": 22,
                    "model": "temperaturehumidity"
                },
                {
                    "id": 13,
                    "model": "temperaturehumidity"
                },
                {
                    "id": 238,
                    "model": "temperaturehumidity"
                },
                {
                    "id": 11,
                    "model": "temperaturehumidity"
                },
                {
                    "id": 12,
                    "model": "temperaturehumidity"
                },
                {
                    "id": 23,
                    "model": "temperaturehumidity"
                }
            ]
     }
     ]

PS. Do I really need to clear out the accessories and persist folders, change the MAC and pin of Homebridge in config.json in order to test this? Is there a better way?

@jannylund
Copy link
Contributor Author

@pkempe strange, that should not be the case. With this PR, unnamed sensors should be named "Sensor 14", etc, and nothing related to "Termomether xx" should be visible.

I'm not 100% sure on the need to clear things out of homekit though.

Can you please paste the log you see when starting up homekit with this PR merged?

If this doesn't work, please try installing from https://github.com/jannylund/homebridge-telldus-tdtool/tree/develop (Which is similar, but with all PR's merged)

@jannylund
Copy link
Contributor Author

@pkempe I confirmed there is no need to clear out anything. Just removing one of these instances from your config and restarting homebridge is enough.

You should see something like this in startup:

[10/25/2016, 12:52:06 AM] [TellstickDuo] Found 6 items of type "sensor" from "config.json".
[10/25/2016, 12:52:06 AM] [TellstickDuo] Initializing platform accessory 'Bastu'...
[10/25/2016, 12:52:06 AM] [TellstickDuo] Initializing platform accessory 'Garderob'...
[10/25/2016, 12:52:06 AM] [TellstickDuo] Initializing platform accessory 'Badrum'...
[10/25/2016, 12:52:06 AM] [TellstickDuo] Initializing platform accessory 'Utomhus'...
[10/25/2016, 12:52:06 AM] [TellstickDuo] Initializing platform accessory 'Garage'...
[10/25/2016, 12:52:06 AM] [TellstickDuo] Initializing platform accessory 'Frysen'...

@pkempe
Copy link

pkempe commented Oct 25, 2016

Here's there log:

Oct 25 06:35:06 rpi homebridge[10776]: [2016-10-25 06:35:06] [Telldus-TD-Tool] Found 12 items of type "sensor".
Oct 25 06:35:06 rpi homebridge[10776]: [2016-10-25 06:35:06] [Telldus-TD-Tool] Model "1A2D" is not supported, try [selflearning-switch, codeswitch, selflearning-dimmer, humidity, temperature, temperaturehumidity]. If you stillhave not found what you're looking for, submit a pull at https://github.com/amlinger/homebridge-telldus-tdtool
Oct 25 06:35:06 rpi homebridge[10776]: [2016-10-25 06:35:06] [Telldus-TD-Tool] Initializing platform accessory 'Utomhusbrytare'...
Oct 25 06:35:06 rpi homebridge[10776]: [2016-10-25 06:35:06] [Telldus-TD-Tool] [Utomhusbrytare]: getServices called
Oct 25 06:35:07 rpi homebridge[10776]: [2016-10-25 06:35:07] [Telldus-TD-Tool] Initializing platform accessory 'Brytare 1'...
Oct 25 06:35:07 rpi homebridge[10776]: [2016-10-25 06:35:07] [Telldus-TD-Tool] [Brytare 1]: getServices called
Oct 25 06:35:07 rpi homebridge[10776]: [2016-10-25 06:35:07] [Telldus-TD-Tool] Initializing platform accessory 'Brytare 2'...
Oct 25 06:35:07 rpi homebridge[10776]: [2016-10-25 06:35:07] [Telldus-TD-Tool] [Brytare 2]: getServices called
Oct 25 06:35:07 rpi homebridge[10776]: [2016-10-25 06:35:07] [Telldus-TD-Tool] Initializing platform accessory 'Thermometer 177'...
Oct 25 06:35:07 rpi homebridge[10776]: [2016-10-25 06:35:07] [Telldus-TD-Tool] Initializing platform accessory 'Thermometer 11'...
Oct 25 06:35:07 rpi homebridge[10776]: [2016-10-25 06:35:07] [Telldus-TD-Tool] Initializing platform accessory 'Thermometer 14'...
Oct 25 06:35:07 rpi homebridge[10776]: [2016-10-25 06:35:07] [Telldus-TD-Tool] Initializing platform accessory 'Thermometer 12'...
Oct 25 06:35:07 rpi homebridge[10776]: [2016-10-25 06:35:07] [Telldus-TD-Tool] Initializing platform accessory 'Thermometer 13'...
Oct 25 06:35:07 rpi homebridge[10776]: [2016-10-25 06:35:07] [Telldus-TD-Tool] Initializing platform accessory 'Thermometer 21'...
Oct 25 06:35:07 rpi homebridge[10776]: [2016-10-25 06:35:07] [Telldus-TD-Tool] Initializing platform accessory 'Thermometer 22'...
Oct 25 06:35:07 rpi homebridge[10776]: [2016-10-25 06:35:07] [Telldus-TD-Tool] Initializing platform accessory 'Thermometer 23'...
Oct 25 06:35:07 rpi homebridge[10776]: [2016-10-25 06:35:07] [Telldus-TD-Tool] Initializing platform accessory 'Thermometer 1'...
Oct 25 06:35:07 rpi homebridge[10776]: [2016-10-25 06:35:07] [Telldus-TD-Tool] Initializing platform accessory 'Thermometer 43'...
Oct 25 06:35:07 rpi homebridge[10776]: [2016-10-25 06:35:07] [Telldus-TD-Tool] Initializing platform accessory 'Thermometer 141'...
Oct 25 06:35:07 rpi homebridge[10776]: Scan this code with your HomeKit App on your iOS device to pair with Homebridge:

@pkempe
Copy link

pkempe commented Oct 25, 2016

Considering the above, it seems I have not correctly installed the PR properly. Isn't git pull origin pull/31/head); npm run build and then copying the dist folder contents all that's required?

@jannylund
Copy link
Contributor Author

@pkempe yes, I confirmed that it works if I do it that way. (however I run npm install instead, which then calls npm run build.

If you still have issues, maybe try to make a clean clone of this repo. (or simply clone my fork and use the develop branch).

Copy link
Owner

@amlinger amlinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I very much like the implementation, although I have a few nit picks.

@@ -32,28 +32,34 @@ class TelldusTDToolPlatform {
this.log = log
this.config = config
this.homebridge = homebridge
this.senzors = config["sensors"] || [];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not sensors? I'm also not sure why this would need to be a instance variable, when this.devices is not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

devices are coming from the output of tdtool, since they are configured in telldus.conf. The only reason to use senzors instead of sensors is to avoid confusion with the local variable sensors used in the else clause.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd really like to avoid this, as sensors can be derived from the config object. Let's keep state to a minimum and rely on the config object instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

sensors.forEach((current, index) => {sensors[index].name = `Thermometer ${current.id}`})
return devices.concat(sensors)
})
if (this.senzors.length > 0) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather rely on this.config.sensors.

return devices.concat(sensors)
})
if (this.senzors.length > 0) {
this.log(foundOfTypeString('sensor', this.senzors.length, 'config.json'))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this is user input, we should verify that the fields supplied are 'name' (optional), 'type' and 'id' and nothing else.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As of the other PR, it's also possible to provide maxAge.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw that, and if you do not wish to address all these constraints in this PR it's fine, we just need to track it in another issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make it another PR as it otherwise needs changes in the other PR as well.

}
})
return devices.concat(this.senzors);
} else {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The else clause here seems superfluous, but it's up to you whether you want to keep it or not.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ideal case to me would be if we could print the path where config.json was read as well, but that might be a bit over the top for an initial implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The else clause is simply there for backwards compatibility, but I agree, it could be removed if you are ok with dropping autoconfigured sensors?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the confusion, that's not what I meant. I just meant that

if (this.senzors.length > {
    // ...
}
return ...

provides the same functionality as

if (this.senzors.length > {
    // ...
} else {
    return ...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, now I understand. Yeah, that could be done, but in this case I think it's more readable to keep the else.

@@ -215,8 +215,12 @@ class TelldusHygrometer extends TelldusAccessory {
getHumidity(callback, context) {
this.log('Checking humidity...')
TDtool.sensor(this.id, this.log).then(s => {
this.log(`Found humidity ${s.humidity}%`)
callback(null, parseFloat(s.humidity))
if (s == undefined) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to go with === as standard.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@@ -266,8 +270,12 @@ class TelldusThermometer extends TelldusAccessory {
getTemperature(callback, context) {
this.log(`Checking temperature...`)
TDtool.sensor(this.id, this.log).then(s => {
this.log(`Found temperature ${s.temperature}`)
callback(null, parseFloat(s.temperature))
if (s == undefined) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to go with === as standard.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@@ -313,8 +321,12 @@ class TelldusThermometerHygrometer extends TelldusThermometer {
getHumidity(callback, context) {
this.log('Checking humidity...')
TDtool.sensor(this.id, this.log).then(s => {
this.log(`Found humidity ${s.humidity}%`)
callback(null, parseFloat(s.humidity))
if (s == undefined) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to go with === as standard.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@jannylund
Copy link
Contributor Author

I think this one is done now. Please merge it if you agree, and I'll rebase #30

@amlinger
Copy link
Owner

Thanks for submitting this, looks like a good implementation and a good interface to me. Tracking the progress on feedback to config fields on #35.

@amlinger amlinger merged commit 99fe006 into amlinger:master Oct 29, 2016
@jannylund jannylund deleted the use-config-file-for-sensors branch October 29, 2016 20:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants