| 1 |
"""Cayenne parser for for The Thinks Network client.""" |
| 2 |
|
| 3 |
import logging |
| 4 |
from ..values import ( |
| 5 |
TTNBaseValue, |
| 6 |
TTNBinarySensorValue, |
| 7 |
TTNDeviceTrackerValue, |
| 8 |
TTNSensorAttribute, |
| 9 |
TTNSensorValue, |
| 10 |
) |
| 11 |
|
| 12 |
_LOGGER = logging.getLogger(__name__) |
| 13 |
|
| 14 |
_SENSOR_ATTR_KEY = "_sensor_attr" |
| 15 |
|
| 16 |
|
| 17 |
def default_parser(uplink_data: dict) -> dict[str, TTNBaseValue]: |
| 18 |
"""Cayenne parser for for The Thinks Network client.""" |
| 19 |
|
| 20 |
ttn_values: dict[str, TTNBaseValue] = {} |
| 21 |
|
| 22 |
# Get device_id and uplink_message from measurement |
| 23 |
device_id = uplink_data["end_device_ids"]["device_id"] |
| 24 |
uplink_message = uplink_data["uplink_message"] |
| 25 |
|
| 26 |
# Skip not decoded measurements |
| 27 |
if "decoded_payload" not in uplink_message: |
| 28 |
_LOGGER.warning("No decoded_payload for device %s", device_id) |
| 29 |
else: |
| 30 |
for field_id, value_item in uplink_message["decoded_payload"].items(): |
| 31 |
__default_parse_field( |
| 32 |
ttn_values, |
| 33 |
field_id, |
| 34 |
uplink_data, |
| 35 |
value_item, |
| 36 |
) |
| 37 |
return ttn_values |
| 38 |
|
| 39 |
|
| 40 |
def __default_parse_field( |
| 41 |
ttn_values: dict[str, TTNBaseValue], |
| 42 |
field_id: str, |
| 43 |
application_up: dict, |
| 44 |
new_value, |
| 45 |
) -> None: |
| 46 |
"""Parses a cayenne field""" |
| 47 |
new_ttn_value: TTNBaseValue | None |
| 48 |
if isinstance(new_value, dict): |
| 49 |
if "latitude" in new_value and "longitude" in new_value: |
| 50 |
# GPS |
| 51 |
new_ttn_value = TTNDeviceTrackerValue(application_up, field_id, new_value) |
| 52 |
elif field_id == _SENSOR_ATTR_KEY: |
| 53 |
# _sensor_attr: { BatV: { unit: "V", device_class: "voltage" } } |
| 54 |
for sensor_field, attr_dict in new_value.items(): |
| 55 |
if not isinstance(attr_dict, dict): |
| 56 |
continue |
| 57 |
for attr_key, attr_value in attr_dict.items(): |
| 58 |
flat_key = f"{_SENSOR_ATTR_KEY}_{sensor_field}_{attr_key}" |
| 59 |
ttn_values[flat_key] = TTNSensorAttribute( |
| 60 |
application_up, flat_key, str(attr_value) |
| 61 |
) |
| 62 |
return |
| 63 |
else: |
| 64 |
# Other - such as acceleration -> split in multiple ttn_values |
| 65 |
for key, value_item in new_value.items(): |
| 66 |
__default_parse_field( |
| 67 |
ttn_values, |
| 68 |
f"{field_id}_{key}", |
| 69 |
application_up, |
| 70 |
value_item, |
| 71 |
) |
| 72 |
return |
| 73 |
elif isinstance(new_value, bool): |
| 74 |
# BinarySensor |
| 75 |
new_ttn_value = TTNBinarySensorValue(application_up, field_id, new_value) |
| 76 |
elif isinstance(new_value, list): |
| 77 |
# TTN_SensorValue with list as string |
| 78 |
new_ttn_value = TTNSensorValue(application_up, field_id, str(new_value)) |
| 79 |
elif isinstance(new_value, (str, int, float)): |
| 80 |
new_ttn_value = TTNSensorValue(application_up, field_id, new_value) |
| 81 |
elif new_value is None: |
| 82 |
# Skip null values |
| 83 |
_LOGGER.warning( |
| 84 |
"Ignoring entry %s with value=None - check your application decoder", |
| 85 |
field_id, |
| 86 |
) |
| 87 |
return |
| 88 |
else: |
| 89 |
raise TypeError(f"Unexpected type {type(new_value)} for value: {new_value}") |
| 90 |
|
| 91 |
ttn_values[field_id] = new_ttn_value |