Skip to content

Commit

Permalink
pushed v0.0.18 (replaced aiohttp with urllib)
Browse files Browse the repository at this point in the history
  • Loading branch information
ckarrie committed Feb 25, 2023
1 parent e095c4b commit ae0304b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 37 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import asyncio
import time

client = oekofen_api.Oekofen("192.168.178.222", "eMlG")
asyncio.run(client.update_csv_data())
#asyncio.run(client.update_csv_data())
asyncio.run(client.get_version())

asyncio.run(client.update_data())
Expand Down
10 changes: 5 additions & 5 deletions build.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@ cd ~/workspace/src/oekofen-api
```

### Push git changes
- Neue Version in der setup.py eintragen 0.0.17
- Neue Version in der setup.py eintragen 0.0.18
- Mit git changes pushen!!

### github Release erstellen
- https://github.com/ckarrie/oekofen-api/releases/new
- Tag (create new): v0.0.17
- Title: v0.0.17
- Tag (create new): v0.0.18
- Title: v0.0.18
- Button "Publish Release"

### Anpassen in setup.py
```
nano setup.py
```
```python
VERSION = "0.0.17"
VERSION = "0.0.18"

```

Expand All @@ -60,7 +60,7 @@ twine upload dist/*
Lokal updaten

```
pip install oekofen-api==0.0.17
pip install oekofen-api==0.0.18
```

## Update homeassistant-oekofen
Expand Down
62 changes: 36 additions & 26 deletions oekofen_api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from __future__ import annotations

import asyncio
from collections import OrderedDict
import logging
import aiohttp
import re
import json
from datetime import datetime
from voluptuous import Optional
from yarl import URL
import urllib.request
import urllib.error

from . import const

Expand Down Expand Up @@ -141,30 +144,32 @@ async def update_csv_data(self):
self._csv_data[content] = value
return self._csv_data

async def _fetch_data(self, path, is_json=True, is_text=False) -> Optional(dict):
async def _fetch_data(self, path, is_json=True, is_text=False, retry=True) -> Optional(dict):
raw_url = f'{self.base_url}{path}'
print("_fetch_data", raw_url)
async with aiohttp.ClientSession(raise_for_status=True) as session:
try:
resp = await session.get(raw_url)
if resp.status == 200:
# no content type send from host, accepting all with content_type=None
if is_json:
json_response = await resp.json(content_type=None)
if json_response is not None:
return json_response
else:
return None
if is_text:
#body = await resp.read()
#print(body)
#return await resp.text(encoding='latin-1')
return await resp.text()
return True

except Exception as e:
logger.error(e)
print("_fetch_data using urllib.request.urlopen", raw_url)
try:
resp = urllib.request.urlopen(raw_url)
raw_data = resp.read()
encoding = resp.info().get_content_charset(const.CHARSET)
if resp.status == 200:
if is_json:
json_data = json.loads(raw_data.decode(encoding))
if json_data is not None:
return json_data
return None
if is_text:
return raw_data.decode(const.CHARSET)
return True
except urllib.error.HTTPError:
if retry:
await asyncio.sleep(2.5)
data = await self._fetch_data(path=path, is_json=is_json, is_text=is_text, retry=False)
return data
else:
raise
except Exception as e:
logger.error(e)
raise

def _has_valid_data(self):
data_is_old = (datetime.now() - self._last_fetch).total_seconds() >= self.update_interval
Expand Down Expand Up @@ -198,7 +203,7 @@ async def _send_set_value(self, domain_attribute: str, value: str):
:return:
"""
path = URL().with_name(f'{domain_attribute}={value}')
return await self._fetch_data(path=str(path), is_json=False)
await self._fetch_data(path=str(path), is_json=False)

async def set_attribute_value(self, att: Attribute, value):
if not isinstance(att, ControllableAttribute):
Expand All @@ -208,8 +213,9 @@ async def set_attribute_value(self, att: Attribute, value):
dom_att = f'{att.domain.name}.{att.key}'
else:
dom_att = f'{att.domain.name}{att.domain.index}.{att.key}'
value_set = await self._send_set_value(domain_attribute=dom_att, value=val)
return value_set

await self._send_set_value(domain_attribute=dom_att, value=val)
return value

# Popular queries
def get_name(self):
Expand Down Expand Up @@ -282,6 +288,10 @@ def __init__(self, domain: Domain, key: str, data: dict | str):
self.domain: Domain = domain
if self.format:
self.choices = const.format2dict(self.format)

# fix '?C' unit:
if isinstance(self.unit, str) and self.unit == '?C':
self.unit = '°C'

# convert numbers in strings to int/float
if isinstance(self.factor, str):
Expand Down
4 changes: 2 additions & 2 deletions oekofen_api/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
ATTR_TYPE_DICT = 'key-value-pair'
ATTR_TYPE_DESCRIPTION = 'description-text'
BASE_URL_TMPL = 'http://{host}:{port}/{json_password}/'
URL_PATH_ALL_WITH_FORMATS = 'all??'
URL_PATH_ALL_WITH_FORMATS = 'all?'
RE_FIND_NUMBERS = r'\d+'
DEFAULT_PORT = 4321
UPDATE_INTERVAL_SECONDS = 10

CHARSET = 'ISO-8859-1'

# JSON Keys
JSON_KEY_FORMAT = 'format'
Expand Down
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
voluptuous>=0.13.1
aiohttp>=3.8.1
voluptuous>=0.13.1
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
with open("README.md", "r") as f:
long_description = f.read()

VERSION = "0.0.17"
VERSION = "0.0.18"

setup(
name="oekofen_api",
Expand Down

0 comments on commit ae0304b

Please sign in to comment.