Skip to content

Commit

Permalink
Added function for converting camle to snake case
Browse files Browse the repository at this point in the history
  • Loading branch information
KirilKurkianec committed Dec 12, 2023
1 parent 3c738eb commit 3189fdd
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion custom_components/econet300/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Callable, Any

import logging
import re

from homeassistant.components.sensor import (
SensorEntityDescription,
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 3189fdd

Please sign in to comment.