Skip to content

Commit

Permalink
Merge pull request #39 from leeyuentuen/dev
Browse files Browse the repository at this point in the history
Dev to master
  • Loading branch information
leeyuentuen authored Aug 9, 2023
2 parents 5ae450e + bd862fb commit 0b73c6b
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 136 deletions.
15 changes: 8 additions & 7 deletions custom_components/alfen_wallbox/alfen.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ async def set_value(self, api_param, value) -> bool:
if prop[ID] == api_param:
_LOGGER.debug(f"Set {api_param} value {value}")
prop[VALUE] = value
return True
break

return False

async def get_value(self, api_param):
Expand All @@ -222,7 +223,7 @@ async def set_current_limit(self, limit):
_LOGGER.debug(f"Set current limit {limit}A")
if limit > 32 | limit < 1:
return None
self.set_value("2129_0", limit)
await self.set_value("2129_0", limit)

async def set_rfid_auth_mode(self, enabled):
_LOGGER.debug(f"Set RFID Auth Mode {enabled}")
Expand All @@ -231,13 +232,13 @@ async def set_rfid_auth_mode(self, enabled):
if enabled:
value = 2

self.set_value("2126_0", value)
await self.set_value("2126_0", value)

async def set_current_phase(self, phase):
_LOGGER.debug(f"Set current phase {phase}")
if phase not in ('L1', 'L2', 'L3'):
return None
self.set_value("2069_0", phase)
await self.set_value("2069_0", phase)

async def set_phase_switching(self, enabled):
_LOGGER.debug(f"Set Phase Switching {enabled}")
Expand All @@ -246,19 +247,19 @@ async def set_phase_switching(self, enabled):
if enabled:
value = 1

self.set_value("2185_0", value)
await self.set_value("2185_0", value)

async def set_green_share(self, value):
_LOGGER.debug(f"Set green share value {value}%")
if value < 0 | value > 100:
return None
self.set_value("3280_2", value)
await self.set_value("3280_2", value)

async def set_comfort_power(self, value):
_LOGGER.debug(f"Set Comfort Level {value}W")
if value < 1400 | value > 5000:
return None
self.set_value("3280_3", value)
await self.set_value("3280_3", value)

def __get_url(self, action) -> str:
return "https://{}/api/{}".format(self.host, action)
Expand Down
2 changes: 0 additions & 2 deletions custom_components/alfen_wallbox/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ async def async_setup_entry(
class AlfenBinarySensor(AlfenEntity, BinarySensorEntity):
"""Define an Alfen binary sensor."""

# entity_description: AlfenBinaryDescriptionMixin

def __init__(self,
device: AlfenDevice,
description: AlfenBinaryDescription
Expand Down
51 changes: 49 additions & 2 deletions custom_components/alfen_wallbox/number.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .const import ID, VALUE
from homeassistant.helpers import entity_platform
from .const import ID, SERVICE_SET_COMFORT_POWER, SERVICE_SET_CURRENT_LIMIT, SERVICE_SET_GREEN_SHARE, VALUE
from homeassistant.components.number import NumberDeviceClass, NumberEntity, NumberEntityDescription, NumberMode
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.entity_platform import AddEntitiesCallback
Expand All @@ -15,6 +16,10 @@
UnitOfPower,
)

import voluptuous as vol

from homeassistant.helpers import config_validation as cv


_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -159,6 +164,32 @@ async def async_setup_entry(

async_add_entities(numbers)

platform = entity_platform.current_platform.get()

platform.async_register_entity_service(
SERVICE_SET_CURRENT_LIMIT,
{
vol.Required("limit"): cv.positive_int,
},
"async_set_current_limit",
)

platform.async_register_entity_service(
SERVICE_SET_GREEN_SHARE,
{
vol.Required(VALUE): cv.positive_int,
},
"async_set_green_share",
)

platform.async_register_entity_service(
SERVICE_SET_COMFORT_POWER,
{
vol.Required(VALUE): cv.positive_int,
},
"async_set_comfort_power",
)


class AlfenNumber(AlfenEntity, NumberEntity):
"""Define an Alfen select entity."""
Expand All @@ -172,7 +203,7 @@ def __init__(
device: AlfenDevice,
description: AlfenNumberDescription,
) -> None:
"""Initialize the Demo Number entity."""
"""Initialize the Alfen Number entity."""
super().__init__(device)
self._device = device
self._attr_name = f"{description.name}"
Expand Down Expand Up @@ -217,3 +248,19 @@ def _get_current_option(self) -> str | None:
if prop[ID] == self.entity_description.api_param:
return prop[VALUE]
return None

async def async_set_current_limit(self, limit):
"""Set the current limit."""
await self._device.set_current_limit(limit)
await self.async_set_native_value(limit)

async def async_set_green_share(self, value):
"""Set the green share."""
await self._device.set_green_share(value)
await self.async_set_native_value(value)


async def async_set_comfort_power(self, value):
"""Set the comfort power."""
await self._device.set_comfort_power(value)
await self.async_set_native_value(value)
101 changes: 81 additions & 20 deletions custom_components/alfen_wallbox/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

from dataclasses import dataclass

from .const import ID, VALUE
from homeassistant.helpers import entity_platform

from .const import ID, SERVICE_DISABLE_RFID_AUTHORIZATION_MODE, SERVICE_ENABLE_RFID_AUTHORIZATION_MODE, SERVICE_SET_CURRENT_PHASE, VALUE
from .entity import AlfenEntity

from homeassistant.config_entries import ConfigEntry
Expand All @@ -19,6 +21,9 @@
from homeassistant.core import HomeAssistant, callback
from . import DOMAIN as ALFEN_DOMAIN

import voluptuous as vol


_LOGGER = logging.getLogger(__name__)


Expand All @@ -38,8 +43,6 @@ class AlfenSelectDescription(SelectEntityDescription, AlfenSelectDescriptionMixi
CHARGING_MODE_DICT: Final[dict[str, int]] = {
"Disable": 0, "Comfort": 1, "Green": 2}

ON_OFF_DICT: Final[dict[str, int]] = {"Off": 0, "On": 1}

PHASE_ROTATION_DICT: Final[dict[str, str]] = {
"L1": "L1",
"L2": "L2",
Expand Down Expand Up @@ -86,18 +89,22 @@ class AlfenSelectDescription(SelectEntityDescription, AlfenSelectDescriptionMixi
}

DISPLAY_LANGUAGE_DICT: Final[dict[str, str]] = {
"English": "en_GB",
"Czech": "cz_CZ",
"Danish": "da_DK",
"Dutch": "nl_NL",
"German": "de_DE",
"English": "en_GB",
"Finnish": "fi_FI",
"French": "fr_FR",
"German": "de_DE",
"Hungarian": "hu_HU",
"Icelandic": "is_IS",
"Italian": "it_IT",
"Norwegian": "no_NO",
"Finnish": "fi_FI",
"Polish": "pl_PL",
"Portuguese": "pt_PT",
"Romanian": "ro_RO",
"Spanish": "es_ES",
"Swedish": "sv_SE",
"Polish": "pl_PL",
"Danish": "da_DK",
}

ALLOWED_PHASE_DICT: Final[dict[str, int]] = {
Expand All @@ -118,6 +125,17 @@ class AlfenSelectDescription(SelectEntityDescription, AlfenSelectDescriptionMixi
"In-operative": 2,
}

GPRS_NETWORK_MODE_DICT: Final[dict[str, int]] = {
"Automatic": 0,
"Manual": 1
}

GPRS_TECHNOLOGY_DICT: Final[dict[str, int]] = {
"2G": 0,
"4G": 2,
}


ALFEN_SELECT_TYPES: Final[tuple[AlfenSelectDescription, ...]] = (
AlfenSelectDescription(
key="lb_solar_charging_mode",
Expand All @@ -127,14 +145,7 @@ class AlfenSelectDescription(SelectEntityDescription, AlfenSelectDescriptionMixi
options_dict=CHARGING_MODE_DICT,
api_param="3280_1",
),
AlfenSelectDescription(
key="lb_solar_charging_boost",
name="Solar Charging Boost",
icon="mdi:ev-station",
options=list(ON_OFF_DICT),
options_dict=ON_OFF_DICT,
api_param="3280_4",
),

AlfenSelectDescription(
key="alb_phase_connection",
name="Active Load Balancing Phase Connection",
Expand Down Expand Up @@ -209,10 +220,22 @@ class AlfenSelectDescription(SelectEntityDescription, AlfenSelectDescriptionMixi
options_dict=OPERATIVE_MODE_DICT,
api_param="205F_0",
),




AlfenSelectDescription(
key="gprs_network_mode",
name="GPRS Network Mode",
icon="mdi:antenna",
options=list(GPRS_NETWORK_MODE_DICT),
options_dict=GPRS_NETWORK_MODE_DICT,
api_param="2113_0",
),
AlfenSelectDescription(
key="gprs_technology",
name="GPRS Technology",
icon="mdi:antenna",
options=list(GPRS_TECHNOLOGY_DICT),
options_dict=GPRS_TECHNOLOGY_DICT,
api_param="2114_0",
),

)

Expand All @@ -228,6 +251,27 @@ async def async_setup_entry(

async_add_entities(selects)

platform = entity_platform.current_platform.get()

platform.async_register_entity_service(
SERVICE_SET_CURRENT_PHASE,
{
vol.Required("phase"): str,
},
"async_set_current_phase",
)

platform.async_register_entity_service(
SERVICE_ENABLE_RFID_AUTHORIZATION_MODE,
{},
"async_enable_rfid_auth_mode",
)

platform.async_register_entity_service(
SERVICE_DISABLE_RFID_AUTHORIZATION_MODE,
{},
"async_disable_rfid_auth_mode",
)

class AlfenSelect(AlfenEntity, SelectEntity):
"""Define Alfen select."""
Expand Down Expand Up @@ -275,3 +319,20 @@ async def async_update(self):
def _async_update_attrs(self) -> None:
"""Update select attributes."""
self._attr_current_option = self._get_current_option()

async def async_set_current_phase(self, phase):
"""Set the current phase."""
await self._device.set_current_phase(phase)
await self.async_select_option(phase)

async def async_enable_rfid_auth_mode(self):
"""Enable RFID authorization mode."""
await self._device.set_rfid_auth_mode(True)
await self.update_state(self.entity_description.api_param, 2)
self.async_write_ha_state()

async def async_disable_rfid_auth_mode(self):
"""Disable RFID authorization mode."""
await self._device.set_rfid_auth_mode(False)
await self.update_state(self.entity_description.api_param, 0)
self.async_write_ha_state()
Loading

0 comments on commit 0b73c6b

Please sign in to comment.