Skip to content

Commit

Permalink
feat: do not create accessory when device is not supported (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuiHash committed Jun 6, 2021
1 parent 0bb8ee0 commit 3c7951b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 11 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ Usually located in `/var/lib/homebridge` or `~/.homebridge`. To customise this o
}
```

## Incompatible sensors

If you have a La Crosse View sensor that is not yet supported by this plugin you can help adding support for it by following these steps:

1. Run homebridge in [debug mode](https://github.com/homebridge/homebridge/wiki/Basic-Troubleshooting#debug-mode)
2. Create a [new issue](https://github.com/GuiHash/homebridge-lacrosseview/issues/new) and post your homebridge logs.

## Acknowledgements

- Keith Prickett and Stuart Kuredjian for the original code to access La Crossse View API:
Expand Down
12 changes: 9 additions & 3 deletions src/lacrosse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface ResponseData<T> {
items: [T]
}

export type DeviceWeatherData = {
type DeviceWeatherData = {
humidity?: number
temperature?: number
barometricPressure?: number
Expand Down Expand Up @@ -149,7 +149,7 @@ export default class LaCrosseAPI {
return ([] as Device[]).concat(...devices) // flatten devices
}

async getDeviceWeatherData(device: Device): Promise<DeviceWeatherData> {
async rawWeatherData(device: Device): Promise<RawWeatherData> {
await this.renewTokenIfNeeded()

const url = LACROSSE_WEATHER_URL.replace('%DEVICE_ID%', device.id)
Expand All @@ -173,6 +173,12 @@ export default class LaCrosseAPI {
this.token.value,
)

return data
}

async getDeviceWeatherData(device: Device): Promise<DeviceWeatherData> {
const data: RawWeatherData = await this.rawWeatherData(device)

const dataFields = data[`ref.user-device.${device.id}`]?.['ai.ticks.1']?.fields

const valuesTemperature = convertToCelcius(dataFields?.Temperature)?.values
Expand Down Expand Up @@ -230,4 +236,4 @@ const fetch: fetch = async function <T>(url, options?, token?): Promise<T> {
} catch (e) {
throw new Error(e.response?.body?.error?.message || e.message)
}
}
}
41 changes: 33 additions & 8 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import fakegato from 'fakegato-history'

import { PLATFORM_NAME, PLUGIN_NAME } from './settings'
import { Accessory } from './platformAccessory'
import { Accessory, isCompatibleDevice } from './platformAccessory'

import LaCrosseAPI from './lacrosse'

Expand Down Expand Up @@ -116,13 +116,38 @@ export class LaCrosseViewPlatform implements DynamicPlatformPlugin {
const allDevices = await this.lacrosse.getDevices()
const locations = [...new Set(allDevices.map(device => `id: ${device.locationId}`))]
this.log.debug(`Found ${locations.length} locations [${locations.join(', ')}]`)
const devices = allDevices.filter(device => {
if (this.shouldIncludeDevice(device)) {
return true
}
this.log.debug(`Ignoring discovered device excluded by configuration: [%s] [id: %s]`, device.name, device.id)
return false
})
const devices = allDevices
.filter(device => {
if (this.shouldIncludeDevice(device)) {
return true
}
this.log.debug(`Ignoring discovered device excluded by configuration: [%s] [id: %s]`, device.name, device.id)
return false
})
.filter(device => {
if (isCompatibleDevice(device)) {
return true
}
this.lacrosse
.rawWeatherData(device)
.then(data => {
this.log.info(
'Ignoring discovered device excluded by incompatibility: [%s] [id: %s]',
device.name,
device.id,
)
this.log.info(
'Please consider restarting homebridge in debug mode and opening an issue on github with the debug informations printed',
)
this.log.debug(`Device data %s`, JSON.stringify(device, null, 1))
this.log.debug(`Raw weather data %s`, JSON.stringify(data, null, 1))
this.log.info(
'If you want to hide this message, add device [%s] to `devicesToExclude` configuration',
device.id,
)
})
.catch(e => this.log.error('Error while getting incompatible device data', e))
})

for (const device of devices) {
const uuid = this.api.hap.uuid.generate(device.id)
Expand Down
5 changes: 5 additions & 0 deletions src/platformAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ export class Accessory {
}
}

export function isCompatibleDevice(device) {
const accessory = { context: { device } }
return isTemperatureAccessory(accessory) || isHumidityAccessory(accessory)
}

function isTemperatureAccessory(accessory) {
return typeof getFields(accessory).Temperature === 'number'
}
Expand Down

0 comments on commit 3c7951b

Please sign in to comment.