Skip to content

Commit

Permalink
Merge pull request #21 from itchannel/1.09
Browse files Browse the repository at this point in the history
1.09
  • Loading branch information
itchannel authored Aug 28, 2022
2 parents 1b4e878 + 64a6f14 commit ae7b0dc
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 21 deletions.
18 changes: 17 additions & 1 deletion custom_components/apex/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from homeassistant.core import callback
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.exceptions import ConfigEntryNotReady, HomeAssistantError
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
Expand Down Expand Up @@ -70,12 +70,21 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
async def async_set_options_service(service_call):
await hass.async_add_executor_job(set_output, hass, service_call, coordinator)

async def async_set_variable_service(service_call):
await hass.async_add_executor_job(set_variable, hass, service_call, coordinator)

hass.services.async_register(
DOMAIN,
"set_output",
async_set_options_service
)

hass.services.async_register(
DOMAIN,
"set_variable",
async_set_variable_service
)



return True
Expand All @@ -86,6 +95,13 @@ def set_output(hass, service, coordinator):
setting = service.data.get("setting").strip()
status = coordinator.apex.toggle_output(did, setting)

def set_variable(hass, service, coordinator):
did = service.data.get("did").strip()
code = service.data.get("code")
status = coordinator.apex.set_variable(did, code)
if status["error"] != "":
raise HomeAssistantError(status["error"])


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Unload a config entry."""
Expand Down
71 changes: 56 additions & 15 deletions custom_components/apex/apex.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,22 +118,27 @@ def status(self):
if self.version == "old":
result = self.oldstatus()
return result
headers = {
**defaultHeaders,
"Cookie" : "connect.sid=" + self.sid
}

r = requests.get(
"http://" + self.deviceip + "/rest/status?_=" + str(round(time.time())),
headers = headers
)
#_LOGGER.debug(r.text)
i = 0
while i <= 3:
headers = {
**defaultHeaders,
"Cookie" : "connect.sid=" + self.sid
}
r = requests.get(
"http://" + self.deviceip + "/rest/status?_=" + str(round(time.time())),
headers = headers
)
#_LOGGER.debug(r.text)

if r.status_code == 200:
result = r.json()
return result
else:
print("Error occured")
if r.status_code == 200:
result = r.json()
return result
elif r.status_code == 401:
self.auth()
else:
_LOGGER.debug("Unknown error occurred")
return {}
i += 1

def config(self):

Expand Down Expand Up @@ -166,6 +171,7 @@ def toggle_output(self, did, state):
"Cookie" : "connect.sid=" + self.sid
}


data = {
"did" : did,
"status": [
Expand All @@ -178,16 +184,51 @@ def toggle_output(self, did, state):

}


_LOGGER.debug(data)

r = requests.put(
"http://" + self.deviceip + "/rest/status/outputs/" + did,
headers = headers,
json = data
)

data = r.json()
_LOGGER.debug(data)
return data


def set_variable(self, did, code):
headers = {
**defaultHeaders,
"Cookie" : "connect.sid=" + self.sid
}
config = self.config()
variable = None
for value in config["oconf"]:
if value["did"] == did:
variable = value

if variable == None:
return {"error": "Variable/did not found"}


if variable["ctype"] != "Advanced":
_LOGGER.debug("Only Advanced mode currently supported")
return {"error": "Given variable was not of type Advanced"}

variable["prog"] = code

r = requests.put(
"http://" + self.deviceip + "/rest/config/oconf/" + did,
headers = headers,
json = variable
)
_LOGGER.debug(variable)

_LOGGER.debug(r.text)

return {"error": ""}



4 changes: 3 additions & 1 deletion custom_components/apex/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
"ca" : {"icon" : "mdi:test-tube", "measurement": "ppm"},
"mg" : {"icon" : "mdi:test-tube", "measurement": "ppm"},
"dos" : {"icon" : "mdi:pump", "measurement": "ml"},
"iotaPump|Sicce|Syncra": {"icon" : "mdi:pump", "measurement": "%"}
"iotaPump|Sicce|Syncra": {"icon" : "mdi:pump", "measurement": "%"},
"variable" : {"icon" : "mdi:cog-outline"},
"virtual" : {"icon" : "mdi:cog-outline"},
}

MEASUREMENTS = {
Expand Down
2 changes: 1 addition & 1 deletion custom_components/apex/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"config_flow": true,
"documentation": "https://github.com/itchannel/apex-ha",
"issue_tracker": "https://github.com/itchannel/apex-ha/issues",
"version": "1.0.8",
"version": "1.0.9",
"requirements": [],
"ssdp": [],
"zeroconf": [],
Expand Down
29 changes: 27 additions & 2 deletions custom_components/apex/sensor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import re
from datetime import datetime, timedelta

from homeassistant.helpers.entity import Entity
Expand All @@ -18,7 +19,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
sensor = ApexSensor(entry, value, config_entry.options)
async_add_entities([sensor], True)
for value in entry.data["outputs"]:
if value["type"] == "dos":
if value["type"] == "dos" or value["type"] == "variable" or value["type"] == "virtual":
sensor = ApexSensor(entry, value, config_entry.options)
async_add_entities([sensor], True)

Expand Down Expand Up @@ -49,6 +50,14 @@ def get_value(self, ftype):
return value["status"][4]
if self.sensor["type"] == "iotaPump|Sicce|Syncra":
return value["status"][1]
if self.sensor["type"] == "virtual" or self.sensor["type"] == "variable":
if "config" in self.coordinator.data:
for config in self.coordinator.data["config"]["oconf"]:
if config["did"] == self.sensor["did"]:
if config["ctype"] == "Advanced":
return self.process_prog(config["prog"])
else:
return "Not an Advanced variable!"

if ftype == "attributes":
for value in self.coordinator.data["inputs"]:
Expand All @@ -60,7 +69,23 @@ def get_value(self, ftype):
return value
if self.sensor["type"] == "iotaPump|Sicce|Syncra":
return value

if self.sensor["type"] == "virtual" or self.sensor["type"] == "variable":
if "config" in self.coordinator.data:
for config in self.coordinator.data["config"]["oconf"]:
if config["did"] == self.sensor["did"]:
return config
else:
return value

def process_prog(self, prog):
if "Set PF" in prog:
return prog
test = re.findall("Set\s[^\d]*(\d+)", prog)
if test:
_LOGGER.debug(test[0])
return int(test[0])
else:
return prog

@property
def name(self):
Expand Down
17 changes: 16 additions & 1 deletion custom_components/apex/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,19 @@ set_output:
options:
- "OFF"
- "ON"
- "AUTO"
- "AUTO"
set_variable:
description: "Ability to program variables on the controller e.g. Set 75 (Only Advanced mode variables supported currently!!"
fields:
did:
name: DID
description: "DID of the selected variable or output to modify"
example: "Cntl_A1"
selector:
text:
code:
name: Code
description: "Code to modify on the variable/output"
example: "Set 75"
selector:
text:
4 changes: 4 additions & 0 deletions info.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
## **Changelog**
## Version 1.09

- Add set_variable service for setting programming code in variables e.g. "Set 75"
- Fix cookie expiry bug

## Version 1.08

Expand Down

0 comments on commit ae7b0dc

Please sign in to comment.