Skip to content

Commit

Permalink
Added detection to apator08, fixed multi name fields, added simple va…
Browse files Browse the repository at this point in the history
…lidator for unit.
  • Loading branch information
SzczepanLeon committed Sep 24, 2024
1 parent 6ecafe1 commit 642c2ff
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 12 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ From decoded JSON:
![decoded JSON](https://github.com/SzczepanLeon/esphome-components/blob/main/docs/decoded_telegram.png)

find interesting data (in that case total_m3), split it into field (total) and unit (m3) and create sensor in YAML. In YAML config please use units from HA (ie. "m³" not "m3", etc).
You can also use similar unit - for example liters "l".

```yaml
wmbus:
Expand Down Expand Up @@ -259,8 +258,8 @@ Meter/sensor:
- **sensors** (*Optional*):
- **id** (*Optional*, string): Manually specify the ID for code generation. At least one of **id** and **name** must be specified.
- **name** (*Optional*, string): The name for the sensor. At least one of **id** and **name** must be specified.
- **field** (*Optional*): Field from decoded telegram (without unit).
- **unit_of_measurement** (*Optional*): Unit for field defined above.
- **field** (*Optional*): Field from decoded telegram (without unit). If **field** is not present then **name** is used.
- **unit_of_measurement** (**Required**): Unit for field defined above.
- All other options from [Sensor](https://esphome.io/components/sensor/index.html#config-sensor).

------------------------
Expand Down
1 change: 1 addition & 0 deletions components/wmbus/driver_apator08.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ namespace
di.addLinkMode(LinkMode::T1);
di.addDetection(MANUFACTURER_APT, 0x03, 0x03);
di.addDetection(MANUFACTURER_APT, 0x0F, 0x0F);
di.addDetection(0x8614 /*APT?*/, 0x03, 0x03);
di.usesProcessContent();
di.setConstructor([](MeterInfo& mi, DriverInfo& di){ return shared_ptr<Meter>(new Driver(mi, di)); });
});
Expand Down
8 changes: 7 additions & 1 deletion components/wmbus/sensor/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor
from esphome.log import Fore, color
from esphome.const import (
CONF_ID,
CONF_TYPE,
Expand All @@ -27,6 +28,7 @@
ENTITY_CATEGORY_DIAGNOSTIC,
UNIT_SECOND,
DEVICE_CLASS_TIMESTAMP,
CONF_UNIT_OF_MEASUREMENT,
)

AUTO_LOAD = ["wmbus"]
Expand Down Expand Up @@ -91,11 +93,15 @@ async def to_code(config):
config[CONF_KEY])
if config[CONF_METER_ID]:
for s in config.get(CONF_SENSORS, []):
if CONF_UNIT_OF_MEASUREMENT not in s:
print(color(Fore.RED, f"unit_of_measurement not defined for sensor '{s[CONF_NAME]}'!"))
exit()
if (s[CONF_FIELD]):
field = s[CONF_FIELD].lower()
else:
field = s[CONF_NAME].lower()
unit = s[CONF_UNIT_OF_MEASUREMENT]
sens = await sensor.new_sensor(s)
cg.add(var.add_sensor(field, sens))
cg.add(var.add_sensor(field, unit, sens))
wmbus = await cg.get_variable(config[CONF_WMBUS_ID])
cg.add(wmbus.register_wmbus_listener(var))
2 changes: 1 addition & 1 deletion components/wmbus/version.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#ifndef MY_VERSION
#define MY_VERSION "4.0.2"
#define MY_VERSION "4.0.3"
#define WMBUSMETERS_VERSION "1.17.1"
#endif
10 changes: 6 additions & 4 deletions components/wmbus/wmbus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ namespace wmbus {
meter->handleTelegram(about, mbus_data.frame, false, &addresses, &id_match, &t);
if (id_match) {
for (auto const& field : sensor->fields) {
std::string field_name = field.first.first;
std::string unit = field.first.second;
if (field.first == "rssi") {
field.second->publish_state(mbus_data.rssi);
}
Expand All @@ -118,16 +120,16 @@ namespace wmbus {
else {
Unit field_unit = toUnit(field.second->get_unit_of_measurement());
if (field_unit != Unit::Unknown) {
double value = meter->getMyNumericValue(field.first, field_unit);
double value = meter->getNumericValue(field_name, field_unit);
if (!std::isnan(value)) {
field.second->publish_state(value);
}
else {
ESP_LOGW(TAG, "Can't get requested field '%s' with unit '%s'", field.first.c_str(), field.second->get_unit_of_measurement().c_str());
ESP_LOGW(TAG, "Can't get requested field '%s' with unit '%s'", field_name.c_str(), unit.c_str());
}
}
else {
ESP_LOGW(TAG, "Can't get proper unit from '%s'", field.second->get_unit_of_measurement().c_str());
ESP_LOGW(TAG, "Can't get proper unit from '%s'", unit.c_str());
}
}
}
Expand Down Expand Up @@ -347,7 +349,7 @@ namespace wmbus {
ESP_LOGCONFIG(TAG, " Type: %s", ((this->type).empty() ? "auto detect" : this->type.c_str()));
ESP_LOGCONFIG(TAG, " Key: '%s'", key.c_str());
for (const auto &ele : this->fields) {
ESP_LOGCONFIG(TAG, " Field: '%s'", ele.first.c_str());
ESP_LOGCONFIG(TAG, " Field: '%s'", ele.first.first.c_str());
LOG_SENSOR(" ", "Name:", ele.second);
}
}
Expand Down
7 changes: 4 additions & 3 deletions components/wmbus/wmbus.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "esphome/components/time/real_time_clock.h"

#include <map>
#include <utility>
#include <string>

#include "rf_cc1101.h"
Expand Down Expand Up @@ -67,9 +68,9 @@ namespace wmbus {
std::string type;
std::string myKey;
std::vector<unsigned char> key{};
std::map<std::string, sensor::Sensor *> fields{};
void add_sensor(std::string field, sensor::Sensor *sensor) {
this->fields[field] = sensor;
std::map<std::pair<std::string, std::string>, sensor::Sensor *> fields{};
void add_sensor(std::string field, std::string unit, sensor::Sensor *sensor) {
this->fields[std::pair<std::string, std::string>(field, unit)] = sensor;
};

void dump_config();
Expand Down

0 comments on commit 642c2ff

Please sign in to comment.