From 3189fdd19099615bc9f2b0b63f7036689dce1709 Mon Sep 17 00:00:00 2001 From: Kiril Kurkianec Date: Tue, 12 Dec 2023 17:51:24 +0200 Subject: [PATCH] Added function for converting camle to snake case --- custom_components/econet300/sensor.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/custom_components/econet300/sensor.py b/custom_components/econet300/sensor.py index d1054f5..d663955 100644 --- a/custom_components/econet300/sensor.py +++ b/custom_components/econet300/sensor.py @@ -3,6 +3,7 @@ from typing import Callable, Any import logging +import re from homeassistant.components.sensor import ( SensorEntityDescription, @@ -66,13 +67,19 @@ def __init__( super().__init__(description, coordinator, api) +def camel_to_snake(key): + """Converting camel case return from api ti snake case to mach translations keys structure""" + key = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", key) + return re.sub("([a-z0-9])([A-Z])", r"\1_\2", key).lower() + + def create_entity_description(key: str): """Creates Econect300 sensor entity based on supplied key""" map_key = REG_PARAM_MAP.get(key, key) return EconetSensorEntityDescription( key=key, name=map_key, - translation_key=map_key, + translation_key=camel_to_snake(map_key), native_unit_of_measurement=REG_PARAM_UNIT.get(map_key, None), state_class=REG_PARAM_STATE_CLASS.get(map_key, None), device_class=REG_PARAM_DEVICE_CLASS.get(map_key, None),