Skip to content

Commit

Permalink
#30 setup calltype "switch" for ocpp. Added boilerplate setup. not bu…
Browse files Browse the repository at this point in the history
…ilding at the moment.
  • Loading branch information
elden1337 committed May 30, 2022
1 parent f8d98a8 commit 926e734
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 17 deletions.
23 changes: 15 additions & 8 deletions custom_components/peaqev/peaqservice/charger/charger.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
PARAMS,
UPDATECURRENT,
CURRENT,
CHARGERID
CHARGERID,
CALL,
SWITCH
)

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -77,13 +79,18 @@ async def _pause_charger(self):
async def _call_charger(self, command: str):
calls = self._service_calls.get_call(command)
_LOGGER.info(calls[DOMAIN], calls[command], calls["params"])
result = await self._hub.hass.services.async_call(
calls[DOMAIN],
calls[command],
calls["params"]
)
msg = f"Calling charger {command}, result {result}"
_LOGGER.info(msg)

if calls["on_off_call_type"] == CALL:
await self._hub.hass.services.async_call(
calls[DOMAIN],
calls[command],
calls["params"]
)
elif calls["on_off_call_type"] == SWITCH:
await self._hub.hass.states.async_set(
calls[command],
calls["params"]["command"]
)

async def _updatemaxcurrent(self):
"""If enabled, let the charger periodically update it's current during charging."""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from custom_components.peaqev.peaqservice.util.constants import CALL, SWITCH


class CallType:
def __init__(self, call: str, params: dict = {}):
def __init__(self, call: str, params: dict = {}, call_type: str = CALL):
self._call = call
self._params = params
self._call_type = call_type

@property
def call(self) -> str:
Expand All @@ -11,3 +14,7 @@ def call(self) -> str:
@property
def params(self) -> dict:
return self._params

@property
def call_type(self) -> str:
return self._call_type
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
from custom_components.peaqev.peaqservice.chargertypes.types.chargeamps import ChargeAmps
from custom_components.peaqev.peaqservice.chargertypes.types.easee import Easee
from custom_components.peaqev.peaqservice.chargertypes.types.garowallbox import GaroWallbox
from custom_components.peaqev.peaqservice.chargertypes.types.ocpp import OCPP
from custom_components.peaqev.peaqservice.util.constants import (
CHARGERTYPE_EASEE, CHARGERTYPE_CHARGEAMPS, CHARGERTYPE_GAROWALLBOX
CHARGERTYPE_EASEE, CHARGERTYPE_CHARGEAMPS, CHARGERTYPE_GAROWALLBOX, CHARGERTYPE_OCPP
)


Expand All @@ -20,6 +21,8 @@ def __init__(self, hass: HomeAssistant, input_type, chargerid):
self._charger = Easee(self._hass, chargerid)
elif input_type == CHARGERTYPE_GAROWALLBOX:
self._charger = GaroWallbox(self._hass, chargerid)
elif input_type == CHARGERTYPE_OCPP:
self._charger = OCPP(self._hass, chargerid)

self._charger.validatecharger()

Expand Down
10 changes: 10 additions & 0 deletions custom_components/peaqev/peaqservice/chargertypes/servicecalls.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,21 @@ def get_call(self, call) -> dict:
ret = {}
ret[DOMAIN] = self.domain
ret[call] = self._get_call_type(call)
ret["on_off_call_type"] = self._get_call_type_type(call)
ret["params"] = self._get_call_type_params(call)
if call is UPDATECURRENT:
ret[PARAMS] = self.update_current.params
return ret

def _get_call_type_type(self, call) -> str:
_callsdict = {
ON: self.on.call_type,
OFF: self.off.call_type,
PAUSE: self.pause.call_type,
RESUME: self.resume.call_type
}
return _callsdict.get(call)

def _get_call_type_params(self, call) -> dict:
_callsdict = {
ON: self.on.params,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from custom_components.peaqev.peaqservice.util.constants import (
CHARGER,
CHARGERID,
CURRENT,
CURRENT, CALL,
)

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -63,13 +63,14 @@ def __init__(self, hass: HomeAssistant, chargerid):
"connector": self._chargeamps_connector
}

_on = CallType("enable", _on_off_params)
_off = CallType("disable", _on_off_params)
_on = CallType("enable", _on_off_params, CALL)
_off = CallType("disable", _on_off_params, CALL)

self._set_servicecalls(
domain=DOMAINNAME,
on_call=_on,
off_call=_off,
on_off_call_type=CALL,
allowupdatecurrent= UPDATECURRENT,
update_current_call="set_max_current",
update_current_params=servicecall_params
Expand Down
10 changes: 6 additions & 4 deletions custom_components/peaqev/peaqservice/chargertypes/types/easee.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
CHARGER,
CHARGERID,
CURRENT,
CALL,
)

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -61,15 +62,16 @@ def __init__(self, hass: HomeAssistant, chargerid, auth_required: bool = False):
_on_params = {}
_off_params = {}

_on = CallType("start", _on_off_params)
_off = CallType("stop", _on_off_params)
_resume = CallType("set_charger_dynamic_limit", {"mode": "6", "charger_id": self._chargerid})
_pause = CallType("set_charger_dynamic_limit", {"mode": "4", "charger_id": self._chargerid})
_on = CallType("start", _on_off_params, CALL)
_off = CallType("stop", _on_off_params, CALL)
_resume = CallType("set_charger_dynamic_limit", {"mode": "6", "charger_id": self._chargerid}, CALL)
_pause = CallType("set_charger_dynamic_limit", {"mode": "4", "charger_id": self._chargerid}, CALL)

self._set_servicecalls(
domain=DOMAINNAME,
on_call=_on if self._auth_required is True else _resume,
off_call=_off if self._auth_required is True else _pause,
on_off_call_type=CALL,
pause_call=_pause,
resume_call=_resume,
allowupdatecurrent=UPDATECURRENT,
Expand Down
91 changes: 91 additions & 0 deletions custom_components/peaqev/peaqservice/chargertypes/types/ocpp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import logging

from homeassistant.core import HomeAssistant
from peaqevcore.Models import CHARGERSTATES

from custom_components.peaqev.peaqservice.chargertypes.calltype import CallType
from custom_components.peaqev.peaqservice.chargertypes.chargerbase import ChargerBase
from custom_components.peaqev.peaqservice.util.constants import (
CHARGER,
CHARGERID,
CURRENT,
SWITCH,
CALL
)

_LOGGER = logging.getLogger(__name__)

ENTITYENDINGS = [] #todo: list all (or many of) the entity_endings that this integration has set in HA, this is to gather the correct entities.
NATIVE_CHARGERSTATES = [] #todo: list all the available chargerstates here
DOMAINNAME = "" #todo: set the domainname as it is written in HA. ocpp?
UPDATECURRENT = True #todo: True if integration allows update of current during charging. False otherwise
#docs: #todo: put links to the native repo here.

"""
The corresponding constant in peaqservice/util/constants.py is what the user picks.
Thereafter, in the init-process this class is picked from chargertypes.py in the parentfolder to this one.
In this one, you need to gather and tell peaq which entities that are supposed to be read from during the service.
Check the todo's throughout this file. I've also altered type of service-call as CALL or SWITCH (switch for this one then) for the on-off calls,
that's shown in charger.py as a clause where this integration will call hass.states.async_set instead of a service-call to turn the charger on/off
"""


class OCPP(ChargerBase):
def __init__(self, hass: HomeAssistant, chargerid):
super().__init__(hass)
self._hass = hass
self._chargerid = chargerid
self.getentities(DOMAINNAME, ENTITYENDINGS)
self._native_chargerstates = NATIVE_CHARGERSTATES

"""this is the states-mapping towards the native peaqev-states."""
self._chargerstates[CHARGERSTATES.Idle] = [] #todo: list the state(s) that would adhere to peaqev "idle" (ie disconnected)
self._chargerstates[CHARGERSTATES.Connected] = [] #todo: list the state(s) that adhere to peaqev "connected"
self._chargerstates[CHARGERSTATES.Charging] = [] #todo: list the state(s) that adhere to "currently charging"
self._chargerstates[CHARGERSTATES.Done] = [] #todo: list the state(s) that adhere to "done/completed"

self.chargerentity = f"sensor.{self._entityschema}_1" #todo: alter this to match the chargerentity
self.powermeter = f"sensor.{self._entityschema}_1_power" #todo: alter this to match the powermeter-entity
self.powerswitch = self._determine_switch_entity()
self.ampmeter = "max_current" #todo: if updatecurrent is true, type the sensor or attribute that reports the currently set amps from the charger
self.ampmeter_is_attribute = True #todo: true if above ampmeter is attribute, false if it's own entity
"""
this will probably have to be rewritten to accommodate the possibility of ampmeter being an attribute,
but not bound to the chargeamps-specific entity
"""

servicecall_params = {}
"""There could be more, or fewer attributes for the servicecall. If needed, add more constants."""
#servicecall_params[CHARGER] = "chargepoint" #todo
#servicecall_params[CHARGERID] = self._chargerid #todo
#servicecall_params[CURRENT] = "max_current" #todo

_on = CallType(self.powerswitch, {"command": "on"}, SWITCH)
_off = CallType(self.powerswitch, {"command": "off"}, SWITCH)

self._set_servicecalls(
domain=DOMAINNAME,
on_call=_on,
off_call=_off,
allowupdatecurrent= UPDATECURRENT,
update_current_call="set_max_current",
update_current_params=servicecall_params
)

def _determine_entities(self):
ret = []
for e in self._entities:
entity_state = self._hass.states.get(e)
if entity_state != "unavailable":
ret.append(e)
return ret

def _determine_switch_entity(self):
"""todo: this most definitely have to be altered to accommodate ocpp"""
ent = self._determine_entities()
for e in ent:
if e.startswith("switch."):
amps = self._hass.states.get(e).attributes.get("max_current")
if isinstance(amps, int):
return e
raise Exception
5 changes: 5 additions & 0 deletions custom_components/peaqev/peaqservice/util/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
CHARGERTYPE_CHARGEAMPS = "Chargeamps"
CHARGERTYPE_EASEE = "Easee"
CHARGERTYPE_GAROWALLBOX = "Garo Wallbox"
CHARGERTYPE_OCPP = "Ocpp"

LOCALE_SE_GOTHENBURG = "Gothenburg, Sweden"
LOCALE_SE_KARLSTAD = "Karlstad, Sweden"
Expand All @@ -75,9 +76,13 @@
CHARGERTYPES = [
CHARGERTYPE_CHARGEAMPS,
CHARGERTYPE_EASEE,
CHARGERTYPE_OCPP,
#CHARGERTYPE_GAROWALLBOX
]

SWITCH = "switch"
CALL = "call"

"""Lookup locales for config flow"""
LOCALES = [
LOCALE_BE_VREG,
Expand Down

0 comments on commit 926e734

Please sign in to comment.