Add amperage limit number to JuiceNet (#71716)

* Add amperage limit number to JuiceNet

* coverage exception

* Use mixin dataclass
This commit is contained in:
Jesse Hills 2022-05-12 23:14:52 +12:00 committed by GitHub
parent bf314970ea
commit 39313057c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 111 additions and 5 deletions

View file

@ -579,6 +579,7 @@ omit =
homeassistant/components/juicenet/const.py homeassistant/components/juicenet/const.py
homeassistant/components/juicenet/device.py homeassistant/components/juicenet/device.py
homeassistant/components/juicenet/entity.py homeassistant/components/juicenet/entity.py
homeassistant/components/juicenet/number.py
homeassistant/components/juicenet/sensor.py homeassistant/components/juicenet/sensor.py
homeassistant/components/juicenet/switch.py homeassistant/components/juicenet/switch.py
homeassistant/components/kaiterra/* homeassistant/components/kaiterra/*

View file

@ -20,7 +20,7 @@ from .device import JuiceNetApi
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
PLATFORMS = [Platform.SENSOR, Platform.SWITCH] PLATFORMS = [Platform.SENSOR, Platform.SWITCH, Platform.NUMBER]
CONFIG_SCHEMA = vol.Schema( CONFIG_SCHEMA = vol.Schema(
vol.All( vol.All(

View file

@ -1,7 +1,12 @@
"""Adapter to wrap the pyjuicenet api for home assistant.""" """Adapter to wrap the pyjuicenet api for home assistant."""
from pyjuicenet import Charger
from homeassistant.helpers.entity import DeviceInfo 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 from .const import DOMAIN
@ -9,16 +14,18 @@ from .const import DOMAIN
class JuiceNetDevice(CoordinatorEntity): class JuiceNetDevice(CoordinatorEntity):
"""Represent a base JuiceNet device.""" """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.""" """Initialise the sensor."""
super().__init__(coordinator) super().__init__(coordinator)
self.device = device self.device = device
self.type = sensor_type self.key = key
@property @property
def unique_id(self): def unique_id(self):
"""Return a unique ID.""" """Return a unique ID."""
return f"{self.device.id}-{self.type}" return f"{self.device.id}-{self.key}"
@property @property
def device_info(self) -> DeviceInfo: def device_info(self) -> DeviceInfo:

View file

@ -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)