Skip to content

Commit

Permalink
Merge pull request #57 from mboisson/unknown-sensors
Browse files Browse the repository at this point in the history
Unknown sensors
  • Loading branch information
ymerj committed Nov 27, 2022
2 parents 1ad74b4 + 70bc308 commit 34d3a82
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 14 deletions.
34 changes: 21 additions & 13 deletions HA parent.groovy
Expand Up @@ -68,6 +68,7 @@
* 0.1.46 2022-07-04 tomw Advanced configuration - manual add/remove of devices; option to disable filtering; unused child cleanup
* 0.1.47 2022-11-03 mboisson Added support for Carbon Dioxide, Radon, and Volatile Organic Compounds sensors
* 0.1.48 2022-11-14 Yves Mercier Added minimal RGB light support (no CT)
* 0.1.49 2022-11-16 mboisson Sensor units and support for "unknown" sensor types
*
* Thank you(s):
*/
Expand Down Expand Up @@ -189,9 +190,6 @@ def parse(String description) {
def domain = entity?.tokenize(".")?.getAt(0)
def device_class = response?.event?.data?.new_state?.attributes?.device_class
def friendly = response?.event?.data?.new_state?.attributes?.friendly_name
def unit_of_measurement = response?.event?.data?.new_state?.attributes?.unit_of_measurement
// if there is no device_class, we need to infer from the units
if (!device_class && unit_of_measurement in ["Bq/m³"]) device_class = "radon"

newVals << response?.event?.data?.new_state?.state
def mapping = null
Expand Down Expand Up @@ -283,6 +281,15 @@ def parse(String description) {
if (mapping) updateChildDevice(mapping, entity, friendly)
break
case "sensor":
def unit_of_measurement = response?.event?.data?.new_state?.attributes?.unit_of_measurement
if (!device_class)
{
// if there is no device_class, we need to infer from the units
if (unit_of_measurement in ["Bq/m³"]) device_class = "radon"
else device_class = "unknown"
}
newVals << unit_of_measurement

mapping = translateSensors(device_class, newVals, friendly, origin)
if (mapping) updateChildDevice(mapping, entity, friendly)
break
Expand Down Expand Up @@ -361,19 +368,20 @@ def translateSensors(device_class, newVals, friendly, origin)
{
def mapping =
[
humidity: [type: "Generic Component Humidity Sensor", event: [[name: "humidity", value: newVals[0], descriptionText:"${friendly} humidity is ${newVals[0]}"]]],
illuminance: [type: "Generic Component Illuminance Sensor", event: [[name: "illuminance", value: newVals[0], descriptionText:"${friendly} illuminance is ${newVals[0]}"]], namespace: "community"],
humidity: [type: "Generic Component Humidity Sensor", event: [[name: "humidity", value: newVals[0], descriptionText:"${friendly} humidity is ${newVals[0]} ${newVals[1]}"]]],
illuminance: [type: "Generic Component Illuminance Sensor", event: [[name: "illuminance", value: newVals[0], descriptionText:"${friendly} illuminance is ${newVals[0]} ${newVals[1]}"]], namespace: "community"],
battery: [type: "Generic Component Battery", event: [[name: "battery", value: newVals[0], descriptionText:"${friendly} battery is ${newVals[0]}%"]], namespace: "community"],
power: [type: "Generic Component Power Meter", event: [[name: "power", value: newVals[0], descriptionText:"${friendly} power is ${newVals[0]}"]]],
power: [type: "Generic Component Power Meter", event: [[name: "power", value: newVals[0], descriptionText:"${friendly} power is ${newVals[0]} ${newVals[1]}"]]],
pressure: [type: "Generic Component Pressure Sensor", event: [[name: "pressure", value: newVals[0], descriptionText:"${friendly} pressure is ${newVals[0]}"]], namespace: "community"],
carbon_dioxide: [type: "Generic Component Carbon Dioxide Sensor", event: [[name: "carbon_dioxide", value: newVals[0], descriptionText:"${friendly} carbon_dioxide is ${newVals[0]}"]], namespace: "community"],
carbon_dioxide: [type: "Generic Component Carbon Dioxide Sensor", event: [[name: "carbon_dioxide", value: newVals[0], descriptionText:"${friendly} carbon_dioxide is ${newVals[0]} ${newVals[1]}"]], namespace: "community"],
volatile_organic_compounds: [type: "Generic Component Volatile Organic Compounds Sensor",
event: [[name: "volatile_organic_compounds", value: newVals[0], descriptionText:"${friendly} volatile_organic_compounds is ${newVals[0]}"]], namespace: "community"],
radon: [type: "Generic Component Radon Sensor", event: [[name: "radon", value: newVals[0], descriptionText:"${friendly} radon is ${newVals[0]}"]], namespace: "community"],
temperature: [type: "Generic Component Temperature Sensor", event: [[name: "temperature", value: newVals[0], descriptionText:"${friendly} temperature is ${newVals[0]}"]]],
voltage: [type: "Generic Component Voltage Sensor", event: [[name: "voltage", value: newVals[0], descriptionText:"${friendly} voltage is ${newVals[0]}"]]],
energy: [type: "Generic Component Energy Meter", event: [[name: "energy", value: newVals[0], descriptionText:"${friendly} energy is ${newVals[0]}"]]],
]
event: [[name: "volatile_organic_compounds", value: newVals[0], descriptionText:"${friendly} volatile_organic_compounds is ${newVals[0]} ${newVals[1]}"]], namespace: "community"],
radon: [type: "Generic Component Radon Sensor", event: [[name: "radon", value: newVals[0], descriptionText:"${friendly} radon is ${newVals[0]} ${newVals[1]}"]], namespace: "community"],
temperature: [type: "Generic Component Temperature Sensor", event: [[name: "temperature", value: newVals[0], descriptionText:"${friendly} temperature is ${newVals[0]} ${newVals[1]}"]]],
voltage: [type: "Generic Component Voltage Sensor", event: [[name: "voltage", value: newVals[0], descriptionText:"${friendly} voltage is ${newVals[0]} ${newVals[1]}"]]],
energy: [type: "Generic Component Energy Meter", event: [[name: "energy", value: newVals[0], descriptionText:"${friendly} energy is ${newVals[0]} ${newVals[1]}"]]],
unknown: [type: "Generic Component Unknown Sensor", event: [[name: "unknown", value: newVals[0], unit_of_measurement: newVals[1], descriptionText:"${friendly} unknown is ${newVals[0]} ${newVals[1]}"]], namespace: "community"],
]

return mapping[device_class]
}
Expand Down
69 changes: 69 additions & 0 deletions genericComponentUnknownSensor.groovy
@@ -0,0 +1,69 @@
/*
Copyright 2021
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-------------------------------------------
Change history:
0.1.49 - mboisson - initial version
*/

metadata
{
definition(name: "Generic Component Unknown Sensor", namespace: "community", author: "community", importUrl: "https://raw.githubusercontent.com/ymerj/HE-HA-control/main/genericComponentUnknownSensor.groovy")
{
capability "Refresh"
}
preferences {
input name: "txtEnable", type: "bool", title: "Enable descriptionText logging", defaultValue: true
}
attribute "value", "number"
attribute "valueStr", "string"
attribute "unit", "string"
}

void updated() {
log.info "Updated..."
log.warn "description logging is: ${txtEnable == true}"
}

void installed() {
log.info "Installed..."
device.updateSetting("txtEnable",[type:"bool",value:true])
refresh()
}

void updateAttr(String aKey, aValue, String aUnit = ""){
sendEvent(name:aKey, value:aValue, unit:aUnit)
}

void parse(String description) { log.warn "parse(String description) not implemented" }

void parse(List<Map> description) {
description.each {
if (it.name in ["unknown"]) {
if (txtEnable) log.info it.descriptionText
updateAttr("value", it.value, it.unit_of_measurement)
updateAttr("valueStr", it.value, it.unit_of_measurement)
updateAttr("unit", it.unit_of_measurement)
}
}
}

void refresh() {
parent?.componentRefresh(this.device)
}
2 changes: 1 addition & 1 deletion packageManifest.json
@@ -1,7 +1,7 @@
{
"packageName": "HE-HA-control",
"author": "ymerj",
"version": "0.1.48",
"version": "0.1.49",
"minimumHEVersion": "2.1.9",
"dateReleased": "2021-03-19",
"drivers": [
Expand Down

0 comments on commit 34d3a82

Please sign in to comment.