-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #147 from SzczepanLeon/text_fields
Add support for text fields
- Loading branch information
Showing
10 changed files
with
147 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import esphome.codegen as cg | ||
import esphome.config_validation as cv | ||
from esphome.components import text_sensor | ||
from esphome.log import Fore, color | ||
from esphome.const import ( | ||
CONF_ID, | ||
CONF_TYPE, | ||
CONF_KEY, | ||
CONF_NAME, | ||
) | ||
|
||
AUTO_LOAD = ["wmbus"] | ||
|
||
CONF_METER_ID = "meter_id" | ||
CONF_LISTENER_ID = "listener_id" | ||
CONF_WMBUS_ID = "wmbus_id" | ||
CONF_FIELD = "field" | ||
CONF_SENSORS = 'sensors' | ||
|
||
from .. import ( | ||
WMBusComponent, | ||
wmbus_ns | ||
) | ||
|
||
CODEOWNERS = ["@SzczepanLeon"] | ||
|
||
WMBusListener = wmbus_ns.class_('WMBusListener') | ||
|
||
def my_key(value): | ||
value = cv.string_strict(value) | ||
parts = [value[i : i + 2] for i in range(0, len(value), 2)] | ||
if (len(parts) != 16) and (len(parts) != 0): | ||
raise cv.Invalid("Key must consist of 16 hexadecimal numbers") | ||
parts_int = [] | ||
if any(len(part) != 2 for part in parts): | ||
raise cv.Invalid("Key must be format XX") | ||
for part in parts: | ||
try: | ||
parts_int.append(int(part, 16)) | ||
except ValueError: | ||
# pylint: disable=raise-missing-from | ||
raise cv.Invalid("Key must be hex values from 00 to FF") | ||
return "".join(f"{part:02X}" for part in parts_int) | ||
|
||
|
||
SENSOR_SCHEMA = text_sensor.text_sensor_schema( | ||
# | ||
).extend( | ||
{ | ||
cv.Optional(CONF_FIELD, default=""): cv.string_strict, | ||
} | ||
) | ||
|
||
CONFIG_SCHEMA = cv.Schema( | ||
{ | ||
cv.GenerateID(CONF_LISTENER_ID): cv.declare_id(WMBusListener), | ||
cv.GenerateID(CONF_WMBUS_ID): cv.use_id(WMBusComponent), | ||
cv.Optional(CONF_METER_ID, default=0): cv.hex_int, | ||
cv.Optional(CONF_TYPE, default=""): cv.string_strict, | ||
cv.Optional(CONF_KEY, default=""): my_key, | ||
cv.Optional(CONF_SENSORS): cv.ensure_list(SENSOR_SCHEMA), | ||
} | ||
).extend(cv.COMPONENT_SCHEMA) | ||
|
||
async def to_code(config): | ||
if config[CONF_TYPE]: | ||
cg.add_platformio_option("build_src_filter", [f"+<**/wmbus/driver_{config[CONF_TYPE].lower()}.cpp>"]) | ||
if config[CONF_METER_ID]: | ||
wmbus = await cg.get_variable(config[CONF_WMBUS_ID]) | ||
cg.add(wmbus.register_wmbus_listener(config[CONF_METER_ID], config[CONF_TYPE].lower(), config[CONF_KEY])) | ||
for s in config.get(CONF_SENSORS, []): | ||
if (s[CONF_FIELD]): | ||
field = s[CONF_FIELD].lower() | ||
else: | ||
field = s[CONF_NAME].lower() | ||
sens = await text_sensor.new_text_sensor(s) | ||
cg.add(wmbus.add_sensor(config[CONF_METER_ID], field, sens)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#ifndef MY_VERSION | ||
#define MY_VERSION "4.0.11" | ||
#define MY_VERSION "4.1.0" | ||
#define WMBUSMETERS_VERSION "1.17.1-b8f4a945" | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters