Skip to content

Commit

Permalink
Add base entity to Peblar Rocksolid EV Chargers integration (#133794)
Browse files Browse the repository at this point in the history
  • Loading branch information
frenck authored Dec 22, 2024
1 parent 1e68ae1 commit 075f95b
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 174 deletions.
23 changes: 1 addition & 22 deletions homeassistant/components/peblar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@
from homeassistant.const import CONF_HOST, CONF_PASSWORD, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.aiohttp_client import async_create_clientsession

from .const import DOMAIN
from .coordinator import (
PeblarConfigEntry,
PeblarDataUpdateCoordinator,
Expand Down Expand Up @@ -76,29 +74,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: PeblarConfigEntry) -> bo
entry.runtime_data = PeblarRuntimeData(
data_coordinator=meter_coordinator,
system_information=system_information,
user_configuraton_coordinator=user_configuration_coordinator,
user_configuration_coordinator=user_configuration_coordinator,
version_coordinator=version_coordinator,
)

# Peblar is a single device integration. Setting up the device directly
# during setup. This way we only have to reference it in all entities.
device_registry = dr.async_get(hass)
device_registry.async_get_or_create(
config_entry_id=entry.entry_id,
configuration_url=f"http://{entry.data[CONF_HOST]}",
connections={
(dr.CONNECTION_NETWORK_MAC, system_information.ethernet_mac_address),
(dr.CONNECTION_NETWORK_MAC, system_information.wlan_mac_address),
},
identifiers={(DOMAIN, system_information.product_serial_number)},
manufacturer=system_information.product_vendor_name,
model_id=system_information.product_number,
model=system_information.product_model_name,
name="Peblar EV Charger",
serial_number=system_information.product_serial_number,
sw_version=version_coordinator.data.current.firmware,
)

# Forward the setup to the platforms
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

Expand Down
30 changes: 8 additions & 22 deletions homeassistant/components/peblar/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@
)
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import DOMAIN
from .coordinator import PeblarConfigEntry, PeblarData, PeblarDataUpdateCoordinator
from .entity import PeblarEntity

PARALLEL_UPDATES = 0

Expand Down Expand Up @@ -56,35 +54,23 @@ async def async_setup_entry(
) -> None:
"""Set up Peblar binary sensor based on a config entry."""
async_add_entities(
PeblarBinarySensorEntity(entry=entry, description=description)
PeblarBinarySensorEntity(
entry=entry,
coordinator=entry.runtime_data.data_coordinator,
description=description,
)
for description in DESCRIPTIONS
)


class PeblarBinarySensorEntity(
CoordinatorEntity[PeblarDataUpdateCoordinator], BinarySensorEntity
PeblarEntity[PeblarDataUpdateCoordinator],
BinarySensorEntity,
):
"""Defines a Peblar binary sensor entity."""

entity_description: PeblarBinarySensorEntityDescription

_attr_has_entity_name = True

def __init__(
self,
entry: PeblarConfigEntry,
description: PeblarBinarySensorEntityDescription,
) -> None:
"""Initialize the binary sensor entity."""
super().__init__(entry.runtime_data.data_coordinator)
self.entity_description = description
self._attr_unique_id = f"{entry.unique_id}-{description.key}"
self._attr_device_info = DeviceInfo(
identifiers={
(DOMAIN, entry.runtime_data.system_information.product_serial_number)
},
)

@property
def is_on(self) -> bool:
"""Return state of the binary sensor."""
Expand Down
25 changes: 4 additions & 21 deletions homeassistant/components/peblar/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
)
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import DOMAIN
from .coordinator import PeblarConfigEntry, PeblarUserConfigurationDataUpdateCoordinator
from .entity import PeblarEntity

PARALLEL_UPDATES = 1

Expand Down Expand Up @@ -59,36 +57,21 @@ async def async_setup_entry(
async_add_entities(
PeblarButtonEntity(
entry=entry,
coordinator=entry.runtime_data.user_configuration_coordinator,
description=description,
)
for description in DESCRIPTIONS
)


class PeblarButtonEntity(
CoordinatorEntity[PeblarUserConfigurationDataUpdateCoordinator], ButtonEntity
PeblarEntity[PeblarUserConfigurationDataUpdateCoordinator],
ButtonEntity,
):
"""Defines an Peblar button."""

entity_description: PeblarButtonEntityDescription

_attr_has_entity_name = True

def __init__(
self,
entry: PeblarConfigEntry,
description: PeblarButtonEntityDescription,
) -> None:
"""Initialize the button entity."""
super().__init__(coordinator=entry.runtime_data.user_configuraton_coordinator)
self.entity_description = description
self._attr_unique_id = f"{entry.unique_id}_{description.key}"
self._attr_device_info = DeviceInfo(
identifiers={
(DOMAIN, entry.runtime_data.system_information.product_serial_number)
},
)

async def async_press(self) -> None:
"""Trigger button press on the Peblar device."""
await self.entity_description.press_fn(self.coordinator.peblar)
2 changes: 1 addition & 1 deletion homeassistant/components/peblar/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class PeblarRuntimeData:

data_coordinator: PeblarDataUpdateCoordinator
system_information: PeblarSystemInformation
user_configuraton_coordinator: PeblarUserConfigurationDataUpdateCoordinator
user_configuration_coordinator: PeblarUserConfigurationDataUpdateCoordinator
version_coordinator: PeblarVersionDataUpdateCoordinator


Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/peblar/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async def async_get_config_entry_diagnostics(
"""Return diagnostics for a config entry."""
return {
"system_information": entry.runtime_data.system_information.to_dict(),
"user_configuration": entry.runtime_data.user_configuraton_coordinator.data.to_dict(),
"user_configuration": entry.runtime_data.user_configuration_coordinator.data.to_dict(),
"ev": entry.runtime_data.data_coordinator.data.ev.to_dict(),
"meter": entry.runtime_data.data_coordinator.data.meter.to_dict(),
"system": entry.runtime_data.data_coordinator.data.system.to_dict(),
Expand Down
55 changes: 55 additions & 0 deletions homeassistant/components/peblar/entity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""Base entity for the Peblar integration."""

from __future__ import annotations

from typing import Any

from homeassistant.const import CONF_HOST
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity import EntityDescription
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
)

from .const import DOMAIN
from .coordinator import PeblarConfigEntry


class PeblarEntity[_DataUpdateCoordinatorT: DataUpdateCoordinator[Any]](
CoordinatorEntity[_DataUpdateCoordinatorT]
):
"""Defines a Peblar entity."""

_attr_has_entity_name = True

def __init__(
self,
*,
entry: PeblarConfigEntry,
coordinator: _DataUpdateCoordinatorT,
description: EntityDescription,
) -> None:
"""Initialize the Peblar entity."""
super().__init__(coordinator=coordinator)
self.entity_description = description
self._attr_unique_id = f"{entry.unique_id}_{description.key}"

system_information = entry.runtime_data.system_information
self._attr_device_info = DeviceInfo(
configuration_url=f"http://{entry.data[CONF_HOST]}",
connections={
(dr.CONNECTION_NETWORK_MAC, system_information.ethernet_mac_address),
(dr.CONNECTION_NETWORK_MAC, system_information.wlan_mac_address),
},
identifiers={
(DOMAIN, entry.runtime_data.system_information.product_serial_number)
},
manufacturer=system_information.product_vendor_name,
model=system_information.product_model_name,
model_id=system_information.product_number,
name="Peblar EV Charger",
serial_number=system_information.product_serial_number,
sw_version=entry.runtime_data.version_coordinator.data.current.firmware,
)
22 changes: 8 additions & 14 deletions homeassistant/components/peblar/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,15 @@
)
from homeassistant.const import EntityCategory, UnitOfElectricCurrent
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import DOMAIN
from .coordinator import (
PeblarConfigEntry,
PeblarData,
PeblarDataUpdateCoordinator,
PeblarRuntimeData,
)
from .entity import PeblarEntity

PARALLEL_UPDATES = 1

Expand Down Expand Up @@ -64,33 +62,29 @@ async def async_setup_entry(
async_add_entities(
PeblarNumberEntity(
entry=entry,
coordinator=entry.runtime_data.data_coordinator,
description=description,
)
for description in DESCRIPTIONS
)


class PeblarNumberEntity(CoordinatorEntity[PeblarDataUpdateCoordinator], NumberEntity):
class PeblarNumberEntity(
PeblarEntity[PeblarDataUpdateCoordinator],
NumberEntity,
):
"""Defines a Peblar number."""

entity_description: PeblarNumberEntityDescription

_attr_has_entity_name = True

def __init__(
self,
entry: PeblarConfigEntry,
coordinator: PeblarDataUpdateCoordinator,
description: PeblarNumberEntityDescription,
) -> None:
"""Initialize the Peblar entity."""
super().__init__(entry.runtime_data.data_coordinator)
self.entity_description = description
self._attr_unique_id = f"{entry.unique_id}_{description.key}"
self._attr_device_info = DeviceInfo(
identifiers={
(DOMAIN, entry.runtime_data.system_information.product_serial_number)
},
)
super().__init__(entry=entry, coordinator=coordinator, description=description)
self._attr_native_max_value = description.native_max_value_fn(
entry.runtime_data
)
Expand Down
27 changes: 5 additions & 22 deletions homeassistant/components/peblar/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@
from homeassistant.components.select import SelectEntity, SelectEntityDescription
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import DOMAIN
from .coordinator import PeblarConfigEntry, PeblarUserConfigurationDataUpdateCoordinator
from .entity import PeblarEntity

PARALLEL_UPDATES = 1

Expand Down Expand Up @@ -56,36 +54,21 @@ async def async_setup_entry(
async_add_entities(
PeblarSelectEntity(
entry=entry,
coordinator=entry.runtime_data.user_configuration_coordinator,
description=description,
)
for description in DESCRIPTIONS
)


class PeblarSelectEntity(
CoordinatorEntity[PeblarUserConfigurationDataUpdateCoordinator], SelectEntity
PeblarEntity[PeblarUserConfigurationDataUpdateCoordinator],
SelectEntity,
):
"""Defines a peblar select entity."""
"""Defines a Peblar select entity."""

entity_description: PeblarSelectEntityDescription

_attr_has_entity_name = True

def __init__(
self,
entry: PeblarConfigEntry,
description: PeblarSelectEntityDescription,
) -> None:
"""Initialize the select entity."""
super().__init__(entry.runtime_data.user_configuraton_coordinator)
self.entity_description = description
self._attr_unique_id = f"{entry.unique_id}-{description.key}"
self._attr_device_info = DeviceInfo(
identifiers={
(DOMAIN, entry.runtime_data.system_information.product_serial_number)
},
)

@property
def current_option(self) -> str | None:
"""Return the selected entity option to represent the entity state."""
Expand Down
Loading

0 comments on commit 075f95b

Please sign in to comment.