Skip to content

Commit

Permalink
Use snake case for entity id and fix cablePlugedIn missing
Browse files Browse the repository at this point in the history
  • Loading branch information
nickknissen committed Jul 13, 2023
1 parent b8a3d90 commit eb6484f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
11 changes: 9 additions & 2 deletions custom_components/monta/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@

import logging

from homeassistant.helpers.entity import generate_entity_id
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
BinarySensorEntityDescription,
)

from .const import DOMAIN

from .coordinator import MontaDataUpdateCoordinator
from .entity import MontaEntity
from .utils import snake_case

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -51,9 +52,15 @@ def __init__(
) -> None:
"""Initialize the binary_sensor class."""
super().__init__(coordinator, charge_point_id)

self.entity_description = entity_description
self.entity_id = generate_entity_id(
"binary_sensor.{}", snake_case(entity_description.key), [charge_point_id]
)
self._attr_name = entity_description.name
self._attr_unique_id = snake_case(entity_description.key)

@property
def is_on(self) -> bool:
"""Return true if the binary_sensor is on."""
return self.coordinator.data[self.charge_point_id].get("cablePluggedIn", False)
return self.coordinator.data[self.charge_point_id].get(self.entity_description.key, False)
13 changes: 6 additions & 7 deletions custom_components/monta/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from .const import DOMAIN, ChargerStatus
from .coordinator import MontaDataUpdateCoordinator
from .entity import MontaEntity
from .utils import snake_case

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -76,20 +77,18 @@ def __init__(
self,
coordinator: MontaDataUpdateCoordinator,
_: ConfigEntry,
description: SensorEntityDescription,
entity_description: SensorEntityDescription,
charge_point_id: int,
) -> None:
"""Initialize the sensor class."""
super().__init__(coordinator, charge_point_id)

self.entity_description = description
self.entity_description = entity_description
self.entity_id = generate_entity_id(
"sensor.{}", description.key, [charge_point_id]
"sensor.{}", snake_case(entity_description.key), [charge_point_id]
)
self._attr_name = description.name
self._attr_unique_id = f"{description.key}"

self.charge_point_id = charge_point_id
self._attr_name = entity_description.name
self._attr_unique_id = snake_case(entity_description.key)

@property
def native_value(self) -> str:
Expand Down
7 changes: 4 additions & 3 deletions custom_components/monta/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
from .const import DOMAIN, ChargerStatus
from .coordinator import MontaDataUpdateCoordinator
from .entity import MontaEntity
from .utils import snake_case

ENTITY_DESCRIPTIONS = (
SwitchEntityDescription(
key="charger_control-charging",
key="charger",
name="Start/Stop",
),
)
Expand Down Expand Up @@ -49,10 +50,10 @@ def __init__(
super().__init__(coordinator, charge_point_id)
self.entity_description = entity_description
self.entity_id = generate_entity_id(
"switch.{}", entity_description.key, [charge_point_id]
"switch.{}", snake_case(entity_description.key), [charge_point_id]
)
self._attr_name = entity_description.name
self._attr_unique_id = f"{entity_description.key}"
self._attr_unique_id = snake_case(entity_description.key)

@property
def available(self) -> bool:
Expand Down
11 changes: 11 additions & 0 deletions custom_components/monta/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""Utility functions."""
import re

def snake_case(key: str) -> str:
"""Convert a string to snake_case."""
string = re.sub(r"[\-\.\s]", "_", str(key))
return (string[0]).lower() + re.sub(
r"[A-Z]",
lambda matched: f"_{matched.group(0).lower()}", # type:ignore[str-bytes-safe]
string[1:],
)

0 comments on commit eb6484f

Please sign in to comment.