Skip to content

Commit

Permalink
Switch to aiohttp (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewturner authored Feb 25, 2024
1 parent dbe21e1 commit 19a3150
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 16 deletions.
2 changes: 2 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!bin/bash

# expects the repository to be cloned within the homeassistant directory

echo "Copying all files from the cloned repo to the Home Assistant custom_components directory..."

cp --verbose ./salusfy/*.* ../custom_components/salusfy
cp --verbose ./salusfy/simulator/*.* ../custom_components/salusfy/simulator

echo "Restart Home Assistant to apply the changes"
2 changes: 0 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
requests
requests_async
3 changes: 2 additions & 1 deletion run.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ async def main():
await thermostat.async_update()
await thermostat.async_update()

await thermostat.set_hvac_mode(HVACMode.Heat)
await thermostat.set_hvac_mode(HVACMode.HEAT)
await thermostat.set_temperature(temperature=9.8)

print("Current: " + str(thermostat.current_temperature))
print("Target: " + str(thermostat.target_temperature))
print("HVAC Action: " + thermostat.hvac_action)
print("HVAC Mode: " + thermostat.hvac_mode)

await thermostat.close()

if __name__ == '__main__':
loop = asyncio.new_event_loop()
Expand Down
5 changes: 5 additions & 0 deletions salusfy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,8 @@ async def get_state(self):
self._state.current_temperature = await self._temperature_client.current_temperature()

return self._state


async def close(self):
"""Closes the client session"""
await self._web_client.close()
3 changes: 3 additions & 0 deletions salusfy/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
vol.Required(CONF_PASSWORD): cv.string,
vol.Required(CONF_ID): cv.string,
vol.Optional(CONF_SIMULATOR, default=False): cv.boolean,
vol.Optional(CONF_ENABLE_TEMPERATURE_CLIENT, default=True): cv.boolean,
vol.Required(CONF_ENTITY_ID): cv.string,
vol.Required(CONF_ACCESS_TOKEN): cv.string,
vol.Optional(CONF_HOST, default='localhost'): cv.string
Expand All @@ -56,6 +57,8 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)

client = create_client_from(config)

await client.get_state()

name = config.get(CONF_NAME)
await async_add_entities(
Expand Down
5 changes: 2 additions & 3 deletions salusfy/ha_temperature_client.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from requests_async import get

"""
Retrieves the current temperature from
another entity from the Home Assistant API
"""
import aiohttp

class HaTemperatureClient:
def __init__(self, host, entity_id, access_token):
Expand All @@ -22,7 +21,7 @@ async def current_temperature(self):
"Content-Type": "application/json",
}

response = await get(url, headers=headers)
response = await aiohttp.get(url, headers=headers)

body = response.json()

Expand Down
2 changes: 1 addition & 1 deletion salusfy/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"domain": "salusfy",
"name": "Salus Thermostat",
"version": "0.1.0",
"version": "0.3.0",
"documentation": "https://github.com/floringhimie/salusfy",
"issue_tracker": "https://github.com/floringhimie/salusfy/issues",
"requirements": [],
Expand Down
7 changes: 6 additions & 1 deletion salusfy/thermostat_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,9 @@ async def set_hvac_mode(self, hvac_mode):

async def async_update(self):
"""Retrieve latest state data."""
self._state = await self._client.get_state()
self._state = await self._client.get_state()


async def close(self):
"""Closes any client sessions held open"""
await self._client.close()
22 changes: 14 additions & 8 deletions salusfy/web_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import time
import logging
import re
import requests_async as requests
import aiohttp
import json

from .state import State
Expand Down Expand Up @@ -38,7 +38,7 @@ def __init__(self, username, password, id):
self._token = None
self._tokenRetrievedAt = None

self._session = requests.Session()
self._session = aiohttp.ClientSession()


async def set_temperature(self, temperature):
Expand All @@ -52,7 +52,7 @@ async def set_temperature(self, temperature):
headers = {"Content-Type": "application/x-www-form-urlencoded"}

try:
self._session.post(URL_SET_DATA, data=payload, headers=headers)
await self._session.post(URL_SET_DATA, data=payload, headers=headers)
_LOGGER.info("Salusfy set_temperature: OK")
except:
_LOGGER.error("Error Setting the temperature.")
Expand All @@ -75,7 +75,7 @@ async def set_hvac_mode(self, hvac_mode):

payload = {"token": token, "devId": self._id, "auto": auto, "auto_setZ1": "1"}
try:
self._session.post(URL_SET_DATA, data=payload, headers=headers)
await self._session.post(URL_SET_DATA, data=payload, headers=headers)
except:
_LOGGER.error("Error Setting HVAC mode to %s", hvac_mode)

Expand Down Expand Up @@ -106,10 +106,11 @@ async def get_token(self):
headers = {"Content-Type": "application/x-www-form-urlencoded"}

try:
await self._session.post(URL_LOGIN, data=payload, headers=headers, verify=False)
await self._session.post(URL_LOGIN, data=payload, headers=headers)
params = {"devId": self._id}
getTkoken = await self._session.get(URL_GET_TOKEN, params=params)
result = re.search('<input id="token" type="hidden" value="(.*)" />', getTkoken.text)
body = await getTkoken.text()
result = re.search('<input id="token" type="hidden" value="(.*)" />', body)
_LOGGER.info("Salusfy get_token OK")
self._token = result.group(1)
self._tokenRetrievedAt = time.time()
Expand Down Expand Up @@ -137,8 +138,9 @@ async def get_state(self):
_LOGGER.error("Error Getting the data from Web. Please check the connection to salus-it500.com manually.")
return None

data = json.loads(r.text)
_LOGGER.info("Salusfy get_data output " + r.text)
body = await r.text()
_LOGGER.info("Salusfy get_data output " + body)
data = json.loads(body)

state = State()
state.target_temperature = float(data["CH1currentSetPoint"])
Expand All @@ -159,3 +161,7 @@ async def get_state(self):

return state


async def close(self):
"""Closes the client session"""
await self._session.close()

0 comments on commit 19a3150

Please sign in to comment.