Skip to content

Commit

Permalink
tweaks after CherryPicks:
Browse files Browse the repository at this point in the history
- ensure that old configs will still work
- make sure that https will also work with self-signed certs
  • Loading branch information
marq24 committed Jan 8, 2025
1 parent db61bd1 commit 2a0d8c8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
16 changes: 8 additions & 8 deletions custom_components/evcc_intg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry):
config_entry.add_update_listener(async_reload_entry)

# initialize our service...
services = EvccService(hass, config_entry, coordinator)
hass.services.async_register(DOMAIN, SERVICE_SET_LOADPOINT_PLAN, services.set_loadpoint_plan,
evcc_services = EvccService(hass, config_entry, coordinator)
hass.services.async_register(DOMAIN, SERVICE_SET_LOADPOINT_PLAN, evcc_services.set_loadpoint_plan,
supports_response=SupportsResponse.OPTIONAL)
hass.services.async_register(DOMAIN, SERVICE_SET_VEHICLE_PLAN, services.set_vehicle_plan,
hass.services.async_register(DOMAIN, SERVICE_SET_VEHICLE_PLAN, evcc_services.set_vehicle_plan,
supports_response=SupportsResponse.OPTIONAL)

# Do we need to patch something?!
Expand Down Expand Up @@ -193,18 +193,18 @@ async def read_evcc_config_on_startup(self):

api_index = 1
for a_loadpoint in initdata[JSONKEY_LOADPOINTS]:
phaseSwitching = False
phase_switching_supported = False
if "chargerPhases1p3p" in a_loadpoint:
phaseSwitching = a_loadpoint["chargerPhases1p3p"]
phase_switching_supported = a_loadpoint["chargerPhases1p3p"]
elif "chargerPhaseSwitching" in a_loadpoint:
phaseSwitching = a_loadpoint["chargerPhaseSwitching"]
phase_switching_supported = a_loadpoint["chargerPhaseSwitching"]
else:
phaseSwitching = False
phase_switching_supported = False

self._loadpoint[f"{api_index}"] = {
"name": a_loadpoint["title"],
"id": slugify(a_loadpoint["title"]),
"has_phase_auto_option": phaseSwitching,
"has_phase_auto_option": phase_switching_supported,
"vehicle_key": a_loadpoint["vehicleName"],
"obj": a_loadpoint
}
Expand Down
8 changes: 3 additions & 5 deletions custom_components/evcc_intg/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import logging

import voluptuous as vol

from custom_components.evcc_intg.pyevcc_ha import EvccApiBridge
from custom_components.evcc_intg.pyevcc_ha.keys import Tag
from homeassistant import config_entries
from homeassistant.const import CONF_HOST, CONF_SCAN_INTERVAL, ATTR_SW_VERSION, CONF_NAME
from homeassistant.core import callback
from homeassistant.helpers.aiohttp_client import async_create_clientsession

from custom_components.evcc_intg.pyevcc_ha import EvccApiBridge
from custom_components.evcc_intg.pyevcc_ha.keys import Tag
from .const import (
DOMAIN,
CONF_INCLUDE_EVCC
Expand Down Expand Up @@ -57,8 +57,6 @@ async def async_step_user(self, user_input=None):
user_input[CONF_SCAN_INTERVAL] = 15
user_input[CONF_INCLUDE_EVCC] = False

user_input = user_input or {}

return self.async_show_form(
step_id="user",
data_schema=vol.Schema({
Expand Down
32 changes: 18 additions & 14 deletions custom_components/evcc_intg/pyevcc_ha/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ async def do_request(method: Callable) -> dict:

class EvccApiBridge:
def __init__(self, host: str, web_session, lang: str = "en") -> None:
# make sure we are compliant with old configurations (that does not include the schema in the host variable)
if not host.startswith(("http://", "https://")):
host = f"http://{host}"

self.host = host
self.web_session = web_session
self.lang_map = None
Expand Down Expand Up @@ -90,7 +94,7 @@ async def read_all_data(self) -> dict:
_LOGGER.info(f"going to read all data from evcc@{self.host}")
req = f"{self.host}/api/state"
_LOGGER.debug(f"GET request: {req}")
json_resp = await do_request(method = self.web_session.get(req))
json_resp = await do_request(method = self.web_session.get(url=req, ssl=False))
if len(json_resp) is not None:
self._LAST_FULL_STATE_UPDATE_TS = time()

Expand All @@ -105,7 +109,7 @@ async def read_frequent_data(self) -> dict:
_LOGGER.info(f"going to read all frequent_data from evcc@{self.host}")
req = f"{self.host}/api/state{STATE_QUERY}"
_LOGGER.debug(f"GET request: {req}")
return await do_request(method = self.web_session.get(req))
return await do_request(method = self.web_session.get(url=req, ssl=False))

async def press_tag(self, tag: Tag, value, idx:str = None) -> dict:
ret = {}
Expand Down Expand Up @@ -144,15 +148,15 @@ async def press_loadpoint_key(self, lp_idx, write_key, value) -> dict:
if write_key == Tag.DETECTVEHICLE.write_key:
req = f"{self.host}/api/{EP_TYPE.LOADPOINTS.value}/{lp_idx}/vehicle"
_LOGGER.debug(f"PATCH request: {req}")
r_json = await do_request(method = self.web_session.patch(req))
r_json = await do_request(method = self.web_session.patch(url=req, ssl=False))
else:
req = f"{self.host}/api/{EP_TYPE.LOADPOINTS.value}/{lp_idx}/{write_key}"
_LOGGER.debug(f"DELETE request: {req}")
r_json = await do_request(method = self.web_session.delete(req))
r_json = await do_request(method = self.web_session.delete(url=req, ssl=False))
else:
req = f"{self.host}/api/{EP_TYPE.LOADPOINTS.value}/{lp_idx}/{write_key}/{value}"
_LOGGER.debug(f"POST request: {req}")
r_json = await do_request(method = self.web_session.post(req))
r_json = await do_request(method = self.web_session.post(url=req, ssl=False))

if r_json is not None and len(r_json) > 0:
if "result" in r_json:
Expand All @@ -173,13 +177,13 @@ async def press_vehicle_key(self, vehicle_id:str, write_key, value) -> dict:
if write_key == Tag.VEHICLEPLANSDELETE.write_key:
req = f"{self.host}/api/{EP_TYPE.VEHICLES.value}/{vehicle_id}/{write_key}"
_LOGGER.debug(f"DELETE request: {req}")
r_json = await do_request(method = self.web_session.delete(req))
r_json = await do_request(method = self.web_session.delete(url=req, ssl=False))
else:
pass
else:
req = f"{self.host}/api/{EP_TYPE.VEHICLES.value}/{vehicle_id}/{write_key}/{value}"
_LOGGER.debug(f"POST request: {req}")
r_json = await do_request(method = self.web_session.post(req))
r_json = await do_request(method = self.web_session.post(url=req, ssl=False))

if r_json is not None and len(r_json) > 0:
if "result" in r_json:
Expand Down Expand Up @@ -230,11 +234,11 @@ async def write_site_key(self, write_key, value) -> dict:
if value is None:
req = f"{self.host}/api/{write_key}"
_LOGGER.debug(f"DELETE request: {req}")
r_json = await do_request(method = self.web_session.delete(req))
r_json = await do_request(method = self.web_session.delete(url=req, ssl=False))
else:
req = f"{self.host}/api/{write_key}/{value}"
_LOGGER.debug(f"POST request: {req}")
r_json = await do_request(method = self.web_session.post(req))
r_json = await do_request(method = self.web_session.post(url=req, ssl=False))

if r_json is not None and len(r_json) > 0:
if "result" in r_json:
Expand All @@ -255,11 +259,11 @@ async def write_loadpoint_key(self, lp_idx, write_key, value) -> dict:
if value is None:
req = f"{self.host}/api/{EP_TYPE.LOADPOINTS.value}/{lp_idx}/{write_key}"
_LOGGER.debug(f"DELETE request: {req}")
r_json = await do_request(method = self.web_session.delete(req))
r_json = await do_request(method = self.web_session.delete(url=req, ssl=False))
else:
req = f"{self.host}/api/{EP_TYPE.LOADPOINTS.value}/{lp_idx}/{write_key}/{value}"
_LOGGER.debug(f"POST request: {req}")
r_json = await do_request(method = self.web_session.post(req))
r_json = await do_request(method = self.web_session.post(url=req, ssl=False))

if r_json is not None and len(r_json) > 0:
if "result" in r_json:
Expand All @@ -277,7 +281,7 @@ async def write_vehicle_key(self, vehicle_id:str, write_key, value) -> dict:
_LOGGER.info(f"going to write '{value}' for key '{write_key}' to evcc-vehicle{vehicle_id}@{self.host}")
req = f"{self.host}/api/{EP_TYPE.VEHICLES.value}/{vehicle_id}/{write_key}/{value}"
_LOGGER.debug(f"POST request: {req}")
r_json = await do_request(method = self.web_session.post(req))
r_json = await do_request(method = self.web_session.post(url=req, ssl=False))

if r_json is not None and len(r_json) > 0:
if "result" in r_json:
Expand All @@ -292,7 +296,7 @@ async def write_loadpoint_plan(self, idx:str, energy:str, rfc_date:str):
try:
req = f"{self.host}/api/{EP_TYPE.LOADPOINTS.value}/{idx}/plan/energy/{energy}/{rfc_date}"
_LOGGER.debug(f"POST request: {req}")
r_json = await do_request(method = self.web_session.post(req))
r_json = await do_request(method = self.web_session.post(url=req, ssl=False))
if r_json is not None and len(r_json) > 0:
if "result" in r_json:
self._LAST_FULL_STATE_UPDATE_TS = 0
Expand All @@ -313,7 +317,7 @@ async def write_vehicle_plan_for_loadpoint_index(self, idx:str, soc:str, rfc_dat
if vehicle_id is not None:
req = f"{self.host}/api/{EP_TYPE.VEHICLES.value}/{vehicle_id}/plan/soc/{soc}/{rfc_date}"
_LOGGER.debug(f"POST request: {req}")
r_json = await do_request(method = self.web_session.post(req))
r_json = await do_request(method = self.web_session.post(url=req, ssl=False))
if r_json is not None and len(r_json) > 0:
if "result" in r_json:
self._LAST_FULL_STATE_UPDATE_TS = 0
Expand Down

0 comments on commit 2a0d8c8

Please sign in to comment.