Skip to content

Commit

Permalink
Fix integration not supporting multiple chargers
Browse files Browse the repository at this point in the history
closes #26
  • Loading branch information
nickknissen committed Oct 4, 2023
1 parent 6c33ba5 commit f850bc5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 32 deletions.
23 changes: 13 additions & 10 deletions custom_components/monta/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging

from homeassistant.components.binary_sensor import (
ENTITY_ID_FORMAT,
BinarySensorDeviceClass,
BinarySensorEntity,
BinarySensorEntityDescription,
Expand Down Expand Up @@ -33,12 +34,14 @@ async def async_setup_entry(hass, entry, async_add_devices):

for charge_point_id, _ in coordinator.data.items():
async_add_devices(
MontaBinarySensor(
coordinator=coordinator,
entity_description=entity_description,
charge_point_id=charge_point_id,
)
for entity_description in ENTITY_DESCRIPTIONS
[
MontaBinarySensor(
coordinator=coordinator,
entity_description=entity_description,
charge_point_id=charge_point_id,
)
for entity_description in ENTITY_DESCRIPTIONS
]
)


Expand All @@ -55,11 +58,11 @@ def __init__(
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_unique_id = generate_entity_id(
ENTITY_ID_FORMAT,
f"{charge_point_id}_{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:
Expand Down
25 changes: 14 additions & 11 deletions custom_components/monta/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.entity import generate_entity_id
from homeassistant.components.sensor import (
ENTITY_ID_FORMAT,
SensorEntity,
SensorEntityDescription,
SensorDeviceClass,
Expand Down Expand Up @@ -67,13 +68,15 @@ async def async_setup_entry(

for charge_point_id, _ in coordinator.data.items():
async_add_entities(
MontaSensor(
coordinator,
entry,
description,
charge_point_id,
)
for description in ENTITY_DESCRIPTIONS
[
MontaSensor(
coordinator,
entry,
description,
charge_point_id,
)
for description in ENTITY_DESCRIPTIONS
]
)


Expand All @@ -91,11 +94,11 @@ def __init__(
super().__init__(coordinator, charge_point_id)

self.entity_description = entity_description
self.entity_id = generate_entity_id(
"sensor.{}", snake_case(entity_description.key), [charge_point_id]
self._attr_unique_id = generate_entity_id(
ENTITY_ID_FORMAT,
f"{charge_point_id}_{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 native_value(self) -> str:
Expand Down
28 changes: 17 additions & 11 deletions custom_components/monta/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
from __future__ import annotations

from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
from homeassistant.components.switch import (
ENTITY_ID_FORMAT,
SwitchEntity,
SwitchEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import generate_entity_id
Expand All @@ -28,12 +32,14 @@ async def async_setup_entry(

for charge_point_id, _ in coordinator.data.items():
async_add_devices(
MontaSwitch(
coordinator,
description,
charge_point_id,
)
for description in ENTITY_DESCRIPTIONS
[
MontaSwitch(
coordinator,
description,
charge_point_id,
)
for description in ENTITY_DESCRIPTIONS
]
)


Expand All @@ -49,11 +55,11 @@ def __init__(
"""Initialize the switch class."""
super().__init__(coordinator, charge_point_id)
self.entity_description = entity_description
self.entity_id = generate_entity_id(
"switch.{}", snake_case(entity_description.key), [charge_point_id]
self._attr_unique_id = generate_entity_id(
ENTITY_ID_FORMAT,
f"{charge_point_id}_{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 available(self) -> bool:
Expand Down

0 comments on commit f850bc5

Please sign in to comment.