Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ omit =
homeassistant/components/juicenet/const.py
homeassistant/components/juicenet/device.py
homeassistant/components/juicenet/entity.py
homeassistant/components/juicenet/number.py
homeassistant/components/juicenet/sensor.py
homeassistant/components/juicenet/switch.py
homeassistant/components/kaiterra/*
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/juicenet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

_LOGGER = logging.getLogger(__name__)

PLATFORMS = [Platform.SENSOR, Platform.SWITCH]
PLATFORMS = [Platform.SENSOR, Platform.SWITCH, Platform.NUMBER]

CONFIG_SCHEMA = vol.Schema(
vol.All(
Expand Down
15 changes: 11 additions & 4 deletions homeassistant/components/juicenet/entity.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
"""Adapter to wrap the pyjuicenet api for home assistant."""

from pyjuicenet import Charger

from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
)

from .const import DOMAIN


class JuiceNetDevice(CoordinatorEntity):
"""Represent a base JuiceNet device."""

def __init__(self, device, sensor_type, coordinator):
def __init__(
self, device: Charger, key: str, coordinator: DataUpdateCoordinator
) -> None:
"""Initialise the sensor."""
super().__init__(coordinator)
self.device = device
self.type = sensor_type
self.key = key

@property
def unique_id(self):
"""Return a unique ID."""
return f"{self.device.id}-{self.type}"
return f"{self.device.id}-{self.key}"

@property
def device_info(self) -> DeviceInfo:
Expand Down
98 changes: 98 additions & 0 deletions homeassistant/components/juicenet/number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
"""Support for controlling juicenet/juicepoint/juicebox based EVSE numbers."""
from __future__ import annotations

from dataclasses import dataclass

from pyjuicenet import Api, Charger

from homeassistant.components.number import NumberEntity, NumberEntityDescription
from homeassistant.components.number.const import DEFAULT_MAX_VALUE
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator

from .const import DOMAIN, JUICENET_API, JUICENET_COORDINATOR
from .entity import JuiceNetDevice


@dataclass
class JuiceNetNumberEntityDescriptionMixin:
"""Mixin for required keys."""

setter_key: str


@dataclass
class JuiceNetNumberEntityDescription(
NumberEntityDescription, JuiceNetNumberEntityDescriptionMixin
):
"""An entity description for a JuiceNetNumber."""

max_value_key: str | None = None


NUMBER_TYPES: tuple[JuiceNetNumberEntityDescription, ...] = (
JuiceNetNumberEntityDescription(
name="Amperage Limit",
key="current_charging_amperage_limit",
min_value=6,
max_value_key="max_charging_amperage",
step=1,
setter_key="set_charging_amperage_limit",
),
)


async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the JuiceNet Numbers."""
juicenet_data = hass.data[DOMAIN][config_entry.entry_id]
api: Api = juicenet_data[JUICENET_API]
coordinator = juicenet_data[JUICENET_COORDINATOR]

entities = [
JuiceNetNumber(device, description, coordinator)
for device in api.devices
for description in NUMBER_TYPES
]
async_add_entities(entities)


class JuiceNetNumber(JuiceNetDevice, NumberEntity):
"""Implementation of a JuiceNet number."""

entity_description: JuiceNetNumberEntityDescription

def __init__(
self,
device: Charger,
description: JuiceNetNumberEntityDescription,
coordinator: DataUpdateCoordinator,
) -> None:
"""Initialise the number."""
super().__init__(device, description.key, coordinator)
self.entity_description = description

self._attr_name = f"{self.device.name} {description.name}"

@property
def value(self) -> float | None:
"""Return the value of the entity."""
return getattr(self.device, self.entity_description.key, None)

@property
def max_value(self) -> float:
"""Return the maximum value."""
if self.entity_description.max_value_key is not None:
return getattr(self.device, self.entity_description.max_value_key)
if self.entity_description.max_value is not None:
return self.entity_description.max_value
return DEFAULT_MAX_VALUE

async def async_set_value(self, value: float) -> None:
"""Update the current value."""
await getattr(self.device, self.entity_description.setter_key)(value)