Skip to content

Commit

Permalink
fix reboot button, fix #51 wrong name
Browse files Browse the repository at this point in the history
  • Loading branch information
Tuen Lee committed Aug 21, 2023
1 parent a8053f1 commit d3ed0ea
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 28 deletions.
42 changes: 23 additions & 19 deletions custom_components/alfen_wallbox/alfen.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
VALUE
)

POST_HEADER_JSON = {"content-type": "application/json"}
POST_HEADER_JSON = {"Content-Type": "application/json"}

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -131,16 +131,16 @@ def login(self):
_LOGGER.debug(f"Login response {response}")
response.raise_for_status()


def logout(self) -> bool:
def logout(self):
del self._session.cookies["session"]

response = self._session.post(
headers=POST_HEADER_JSON,
url=self.__get_url(LOGOUT),
)
_LOGGER.debug(f"Logout response {response}")
return response.status_code == 200
response.raise_for_status()


def _update_value(self, api_param, value):

Expand All @@ -162,15 +162,15 @@ def _get_value(self, api_param):
_LOGGER.debug(f"Status Response {response}")
response.raise_for_status()
resp = response.text
response_json = json.loads(resp)

if self.properties is None:
self.properties = []
for resp in response_json[PROPERTIES]:
for prop in self.properties:
if prop[ID] == resp[ID]:
prop[VALUE] = resp[VALUE]
break
if resp is not None and resp != '':
response_json = json.loads(resp)
if self.properties is None:
self.properties = []
for resp in response_json[PROPERTIES]:
for prop in self.properties:
if prop[ID] == resp[ID]:
prop[VALUE] = resp[VALUE]
break

def _get_all_properties_value(self):
properties = []
Expand All @@ -190,8 +190,7 @@ def _get_all_properties_value(self):

if response_json is not None:
properties += response_json[PROPERTIES]
nextRequest = response_json[TOTAL] > (
offset + len(response_json[PROPERTIES]))
nextRequest = response_json[TOTAL] > (offset + len(response_json[PROPERTIES]))
offset += len(response_json[PROPERTIES])
_LOGGER.debug(f"Properties {properties}")
self.properties = properties
Expand All @@ -207,8 +206,13 @@ async def reboot_wallbox(self):
_LOGGER.debug(f"Reboot response {response}")
await hass.async_add_executor_job(self.logout)

async def request(self, method: str, headers: str, url_cmd: str, json_data=None):
async def async_request(self, method: str, headers: str, url_cmd: str, json_data=None):
await hass.async_add_executor_job(self.login)
response_json = await hass.async_add_executor_job(self.request, method, headers, url_cmd, json_data)
await hass.async_add_executor_job(self.logout)
return response_json

def request(self, method: str, headers: str, url_cmd: str, json_data=None):
if method == METHOD_POST:
response = self._session.post(
headers=POST_HEADER_JSON,
Expand All @@ -224,9 +228,9 @@ async def request(self, method: str, headers: str, url_cmd: str, json_data=None)
_LOGGER.debug(f"Request response {response}")
response.raise_for_status()
resp = response.text
response_json = json.loads(resp)

return response_json
if resp is not None and resp != '':
response_json = json.loads(resp)
return response_json


async def set_value(self, api_param, value):
Expand Down
15 changes: 7 additions & 8 deletions custom_components/alfen_wallbox/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,10 @@ def __init__(

async def async_press(self) -> None:
"""Press the button."""
await self.hass.async_add_executor_job(self._device.login())
await self.hass.async_add_executor_job(
self._device.request(),
self.entity_description.method,
POST_HEADER_JSON,
self.entity_description.url_action,
self.entity_description.json_action)
await self.hass.async_add_executor_job(self._device.logout())
await self._device.async_request(
method=self.entity_description.method,
headers=POST_HEADER_JSON,
url_cmd=self.entity_description.url_action,
json_data=self.entity_description.json_action
)

1 change: 1 addition & 0 deletions custom_components/alfen_wallbox/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,7 @@ def __init__(self, device: AlfenDevice, description: AlfenSensorDescription) ->
self._sensor = "sensor"
self.entity_description = description


@property
def unique_id(self):
"""Return a unique ID."""
Expand Down
2 changes: 1 addition & 1 deletion custom_components/alfen_wallbox/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class AlfenSwitchDescription(SwitchEntityDescription, AlfenSwitchDescriptionMixi
),
AlfenSwitchDescription(
key="auth_local_list",
name="Auth. Whitelist",
name="Auth. Local List",
api_param="213D_0",
),
AlfenSwitchDescription(
Expand Down

0 comments on commit d3ed0ea

Please sign in to comment.