Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proper HPPremium support #53

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 44 additions & 36 deletions homepilot/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@
class HomePilotApi:
_host: str
_password: str
_api_version: int
_base_path: str
_authenticated: bool = False
_cookie_jar: Any = None

def __init__(self, host, password) -> None:
def __init__(self, host, password, api_version = 1) -> None:
self._host = host
self._password = password
self._api_version = api_version
self._base_path = "/hp" if api_version == 2 else ""

@staticmethod
async def test_connection(host: str) -> str:
Expand Down Expand Up @@ -82,7 +86,7 @@ async def authenticate(self):
async def get_devices(self):
await self.authenticate()
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
async with session.get(f"http://{self.host}/devices") as response:
async with session.get(f"http://{self._host}{self._base_path}/devices") as response:
if response.status == 401:
raise AuthError()
response = await response.json()
Expand All @@ -96,7 +100,7 @@ async def get_devices(self):
async def get_device(self, did):
await self.authenticate()
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
async with session.get(f"http://{self.host}/devices/{did}") as response:
async with session.get(f"http://{self._host}{self._base_path}/devices/{did}") as response:
response = await response.json()
if response["error_code"] != 0:
return []
Expand All @@ -109,7 +113,7 @@ async def async_get_fw_status(self):
await self.authenticate()
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
async with session.get(
f"http://{self.host}/service/system-update-image/status"
f"http://{self._host}{self._base_path}/service/system-update-image/status"
) as response:
if response.status == 401:
raise AuthError()
Expand All @@ -120,7 +124,7 @@ async def async_get_interfaces(self):
await self.authenticate()
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
async with session.get(
f"http://{self.host}/service/system/networkmgr/v1/interfaces"
f"http://{self._host}{self._base_path}/service/system/networkmgr/v1/interfaces"
) as response:
if response.status == 401:
raise AuthError()
Expand All @@ -131,7 +135,7 @@ async def async_get_fw_version(self):
await self.authenticate()
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
async with session.get(
f"http://{self.host}/service/system-update-image/version"
f"http://{self._host}{self._base_path}/service/system-update-image/version"
) as response:
if response.status == 401:
raise AuthError()
Expand All @@ -142,7 +146,7 @@ async def async_get_nodename(self):
await self.authenticate()
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
async with session.get(
f"http://{self.host}/service/system/networkmgr/v1/nodename"
f"http://{self._host}{self._base_path}/service/system/networkmgr/v1/nodename"
) as response:
if response.status == 401:
raise AuthError()
Expand All @@ -153,7 +157,7 @@ async def async_get_led_status(self):
await self.authenticate()
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
async with session.get(
f"http://{self.host}/service/system/leds/status"
f"http://{self._host}{self._base_path}/service/system/leds/status"
) as response:
if response.status == 401:
raise AuthError()
Expand All @@ -164,7 +168,7 @@ async def async_get_device_state(self, did):
await self.authenticate()
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
async with session.get(
f"http://{self.host}/v4/devices/{did}"
f"http://{self._host}{self._base_path}/v4/devices/{did}"
) as response:
response = await response.json()
if response["response"] != "get_device":
Expand All @@ -180,7 +184,7 @@ async def async_get_devices_state(self):
await self.authenticate()
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
async with session.get(
f"http://{self.host}/v4/devices?devtype=Actuator"
f"http://{self._host}{self._base_path}/v4/devices?devtype=Actuator"
) as response:
if response.status == 401:
raise AuthError()
Expand All @@ -194,7 +198,7 @@ async def async_get_devices_state(self):
else:
actuators = {}
async with session.get(
f"http://{self.host}/v4/devices?devtype=Sensor"
f"http://{self._host}{self._base_path}/v4/devices?devtype=Sensor"
) as response:
if response.status == 401:
raise AuthError()
Expand All @@ -208,7 +212,7 @@ async def async_get_devices_state(self):
else:
sensors = {}
async with session.get(
f"http://{self.host}/v4/devices?devtype=Transmitter"
f"http://{self._host}{self._base_path}/v4/devices?devtype=Transmitter"
) as response:
if response.status == 401:
raise AuthError()
Expand All @@ -227,39 +231,39 @@ async def async_ping(self, did):
await self.authenticate()
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
async with session.put(
f"http://{self.host}/devices/{did}", json={"name": APICAP_PING_CMD}
f"http://{self._host}{self._base_path}/devices/{did}", json={"name": APICAP_PING_CMD}
) as response:
return await response.json()

async def async_open_cover(self, did):
await self.authenticate()
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
async with session.put(
f"http://{self.host}/devices/{did}", json={"name": APICAP_POS_UP_CMD}
f"http://{self._host}{self._base_path}/devices/{did}", json={"name": APICAP_POS_UP_CMD}
) as response:
return await response.json()

async def async_close_cover(self, did):
await self.authenticate()
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
async with session.put(
f"http://{self.host}/devices/{did}", json={"name": APICAP_POS_DOWN_CMD}
f"http://{self._host}{self._base_path}/devices/{did}", json={"name": APICAP_POS_DOWN_CMD}
) as response:
return await response.json()

async def async_stop_cover(self, did):
await self.authenticate()
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
async with session.put(
f"http://{self.host}/devices/{did}", json={"name": APICAP_STOP_CMD}
f"http://{self._host}{self._base_path}/devices/{did}", json={"name": APICAP_STOP_CMD}
) as response:
return await response.json()

async def async_set_cover_position(self, did, position):
await self.authenticate()
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
async with session.put(
f"http://{self.host}/devices/{did}",
f"http://{self._host}{self._base_path}/devices/{did}",
json={"name": APICAP_GOTO_POS_CMD, "value": position},
) as response:
return await response.json()
Expand All @@ -268,7 +272,7 @@ async def async_open_cover_tilt(self, did) -> None:
await self.authenticate()
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
async with session.put(
f"http://{self.host}/devices/{did}",
f"http://{self._host}{self._base_path}/devices/{did}",
json={"name": APICAP_SET_SLAT_POS_CMD, "value": 0},
) as response:
return await response.json()
Expand All @@ -277,7 +281,7 @@ async def async_close_cover_tilt(self, did) -> None:
await self.authenticate()
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
async with session.put(
f"http://{self.host}/devices/{did}",
f"http://{self._host}{self._base_path}/devices/{did}",
json={"name": APICAP_SET_SLAT_POS_CMD, "value": 100},
) as response:
return await response.json()
Expand All @@ -286,7 +290,7 @@ async def async_set_cover_tilt_position(self, did, position) -> None:
await self.authenticate()
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
async with session.put(
f"http://{self.host}/devices/{did}",
f"http://{self._host}{self._base_path}/devices/{did}",
json={"name": APICAP_SET_SLAT_POS_CMD, "value": position},
) as response:
return await response.json()
Expand All @@ -295,7 +299,7 @@ async def async_stop_cover_tilt(self, did) -> None:
await self.authenticate()
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
async with session.put(
f"http://{self.host}/devices/{did}",
f"http://{self._host}{self._base_path}/devices/{did}",
json={"name": APICAP_STOP_SLAT_CMD},
) as response:
return await response.json()
Expand All @@ -304,7 +308,7 @@ async def async_set_ventilation_position_mode(self, did, mode) -> None:
await self.authenticate()
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
async with session.put(
f"http://{self.host}/devices/{did}",
f"http://{self._host}{self._base_path}/devices/{did}",
json={"name": APICAP_VENTIL_POS_MODE_CFG, "value": mode},
) as response:
return await response.json()
Expand All @@ -313,7 +317,7 @@ async def async_set_ventilation_position(self, did, position) -> None:
await self.authenticate()
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
async with session.put(
f"http://{self.host}/devices/{did}",
f"http://{self._host}{self._base_path}/devices/{did}",
json={"name": APICAP_VENTIL_POS_CFG, "value": str(int(position))},
) as response:
return await response.json()
Expand All @@ -322,23 +326,23 @@ async def async_turn_on(self, did):
await self.authenticate()
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
async with session.put(
f"http://{self.host}/devices/{did}", json={"name": APICAP_TURN_ON_CMD}
f"http://{self._host}{self._base_path}/devices/{did}", json={"name": APICAP_TURN_ON_CMD}
) as response:
return await response.json()

async def async_turn_off(self, did):
await self.authenticate()
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
async with session.put(
f"http://{self.host}/devices/{did}", json={"name": APICAP_TURN_OFF_CMD}
f"http://{self._host}{self._base_path}/devices/{did}", json={"name": APICAP_TURN_OFF_CMD}
) as response:
return await response.json()

async def async_set_target_temperature(self, did, temperature):
await self.authenticate()
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
async with session.put(
f"http://{self.host}/devices/{did}",
f"http://{self._host}{self._base_path}/devices/{did}",
json={"name": APICAP_TARGET_TEMPERATURE_CFG, "value": temperature},
) as response:
return await response.json()
Expand All @@ -347,7 +351,7 @@ async def async_set_auto_mode(self, did, auto_mode):
await self.authenticate()
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
async with session.put(
f"http://{self.host}/devices/{did}",
f"http://{self._host}{self._base_path}/devices/{did}",
json={"name": APICAP_AUTO_MODE_CFG, "value": auto_mode},
) as response:
return await response.json()
Expand All @@ -356,7 +360,7 @@ async def async_set_temperature_thresh_cfg(self, did, thresh_number, temperature
await self.authenticate()
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
async with session.put(
f"http://{self.host}/devices/{did}",
f"http://{self._host}{self._base_path}/devices/{did}",
json={"name": f"TEMPERATURE_THRESH_{thresh_number}_CFG", "value": temperature},
) as response:
return await response.json()
Expand All @@ -365,23 +369,23 @@ async def async_turn_led_on(self):
await self.authenticate()
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
async with session.post(
f"http://{self.host}/service/system/leds/enable"
f"http://{self._host}{self._base_path}/service/system/leds/enable"
) as response:
return await response.json()

async def async_turn_led_off(self):
await self.authenticate()
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
async with session.post(
f"http://{self.host}/service/system/leds/disable"
f"http://{self._host}{self._base_path}/service/system/leds/disable"
) as response:
return await response.json()

async def async_set_auto_update_on(self):
await self.authenticate()
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
async with session.put(
f"http://{self.host}/service/system-update-image/auto_update",
f"http://{self._host}{self._base_path}/service/system-update-image/auto_update",
json={"auto_update": True},
) as response:
return await response.json()
Expand All @@ -390,7 +394,7 @@ async def async_set_auto_update_off(self):
await self.authenticate()
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
async with session.put(
f"http://{self.host}/service/system-update-image/auto_update",
f"http://{self._host}{self._base_path}/service/system-update-image/auto_update",
json={"auto_update": False},
) as response:
return await response.json()
Expand All @@ -399,7 +403,7 @@ async def async_update_firmware(self):
await self.authenticate()
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
async with session.post(
f"http://{self.host}/service/system-update-image/startupdate"
f"http://{self._host}{self._base_path}/service/system-update-image/startupdate"
) as response:
return await response.json()

Expand All @@ -408,7 +412,7 @@ async def async_get_scenes(self):
await self.authenticate()
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
async with session.get(
f"http://{self.host}/scenes"
f"http://{self._host}{self._base_path}/scenes"
) as response:
if response.status == 401:
raise AuthError()
Expand All @@ -423,7 +427,7 @@ async def async_get_scenes_v4(self):
await self.authenticate()
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
async with session.get(
f"http://{self.host}/v4/scenes"
f"http://{self._host}{self._base_path}/v4/scenes"
) as response:
if response.status == 401:
raise AuthError()
Expand All @@ -438,7 +442,7 @@ async def async_execute_scene(self, sid: int):
await self.authenticate()
async with aiohttp.ClientSession(cookie_jar=self.cookie_jar) as session:
async with session.post(
f"http://{self.host}/scenes/{sid}/actions",
f"http://{self._host}{self._base_path}/scenes/{sid}/actions",
json={ "request_type": "EXECUTESCENE", "trigger_event": "TRIGGER_SCENE_MANUALLY_EVT" }
) as response:
return await response.json()
Expand All @@ -451,6 +455,10 @@ def host(self):
def password(self):
return self._password

@property
def api_version(self):
return self._api_version

@property
def authenticated(self):
return self._authenticated
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="pyrademacher",
version="0.12.3",
version="0.13.1",
author="Pedro Ribeiro",
author_email="[email protected]",
description="Control devices connected to your Rademacher Homepilot "
Expand Down
Loading