Skip to content

Commit

Permalink
fix recording sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
pszafer committed Mar 30, 2024
1 parent 195d194 commit ed70ba2
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 10 deletions.
37 changes: 28 additions & 9 deletions bosch_thermostat_client/sensors/recording.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
RECORDING,
TEMP_CELSIUS,
ENERGY_KILO_WATT_HOUR,
ENERGY_WATT_HOUR
ENERGY_WATT_HOUR,
)
from .sensor import Sensor
from bosch_thermostat_client.exceptions import DeviceException
Expand Down Expand Up @@ -48,10 +48,22 @@ def process_results(self, result: dict, time: datetime) -> None:
"""Convert multi-level json object to one level object."""

if result and RECORDING in result:
last_hour = time.replace(minute=0, second=0, microsecond=0)
result_interval = result[INTERVAL]
time_interval = time.strftime("%Y-%m-%d")
self._data[self.attr_id][RESULT][VALUE] = []
if result_interval != time_interval:
_LOGGER.warn(
"Different time returned by API than expected, exiting."
)
return
last_hour = time.replace(minute=0, second=0, microsecond=0)
for idx, recording in enumerate(result[RECORDING]):
if recording["c"] == 0:
val = (
0
if recording["y"] == 0 or recording["c"] == 0
else round((recording["y"] / recording["c"]), 1)
)
if val == 0:
continue
self._data[self.attr_id][RESULT][VALUE].append(
{
Expand All @@ -60,7 +72,9 @@ def process_results(self, result: dict, time: datetime) -> None:
}
)

async def fetch_range(self, start_time: datetime, stop_time: datetime) -> dict:
async def fetch_range(
self, start_time: datetime, stop_time: datetime
) -> dict:
async with self._lock:
current_date = start_time
while current_date < stop_time:
Expand All @@ -70,15 +84,20 @@ async def fetch_range(self, start_time: datetime, stop_time: datetime) -> dict:
continue
if RECORDING in data:
for idx, recording in enumerate(data[RECORDING]):
if recording["y"] == 0 or recording["c"] == 0:
continue
_d = current_date.replace(
hour=idx, minute=0, second=0, microsecond=0
)
val = (
0
if recording["y"] == 0 or recording["c"] == 0
else round((recording["y"] / recording["c"]), 1)
)
if val == 0:
continue
if start_time <= _d <= stop_time:
self._past_data[_d] = {
"d": _d,
VALUE: round((recording["y"] / recording["c"]), 1),
VALUE: val,
}
current_date += timedelta(days=1)
return self._past_data
Expand All @@ -89,14 +108,14 @@ def state(self) -> str | None:
return self._data[self.attr_id].get(RESULT, {}).get(VALUE)

def build_uri(self, time: datetime) -> str:
if time.hour < 1:
time = time - timedelta(hours=12)
interval = time.strftime("%Y-%m-%d")
return f"{self._data[self.attr_id][URI]}?{INTERVAL}={interval}"

async def update(self, time: datetime = datetime.utcnow()) -> None:
"""Update info about Recording Sensor asynchronously."""
try:
if time.hour < 1:
time = time - timedelta(hours=12)
uri = self.build_uri(time)
result = await self._connector.get(uri)
_LOGGER.debug("Fetching uri for recording sensor %s", uri)
Expand Down
2 changes: 1 addition & 1 deletion bosch_thermostat_client/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# flake8: noqa
__version__ = "0.25.0"
__version__ = "0.25.0-dev.6"
138 changes: 138 additions & 0 deletions tests/test_ivt_xmpp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
"""Manual test script of bosch_thermostat_client Nefit connection with HTTP endpoint. """

import asyncio
import logging
from datetime import datetime, timedelta
import time
import bosch_thermostat_client as bosch
from bosch_thermostat_client.const.ivt import IVT
from bosch_thermostat_client.const import HC, XMPP, DHW

logging.basicConfig()
logging.getLogger().setLevel(logging.WARN)


async def hc_circuits_init(gateway):
await gateway.initialize_circuits(HC)


async def dhw_circuits_init(gateway):
await gateway.initialize_circuits(DHW)


async def get_circuit(gateway, c_index, circ_type=HC):
circs = gateway.get_circuits(circ_type)
circ = circs[c_index]
await circ.update()
return circ


async def dhw_circuit_test(gateway):
dhw = await get_circuit(gateway, 0, DHW)
sws = dhw.switches


async def hc_circuits_read_temp(gateway, c_index, circ_type=HC):
hc = await get_circuit(gateway, c_index, circ_type)
print("hvac mode", hc.current_temp)
print("target temp ->", hc.target_temperature)


async def get_operation_mode(gateway, c_index, circ_type=HC):
hc = await get_circuit(gateway, c_index, circ_type)
print("OPERATION MODE", hc.ha_mode)


async def set_operation_mode(gateway, c_index, circ_type=HC):
hc = await get_circuit(gateway, c_index, circ_type)
print("OPERATION MODE", hc.ha_mode)
mode = "auto" if hc.ha_mode == "heat" else "heat"
await hc.set_ha_mode(mode)


async def main():
""""""
BoschGateway = bosch.gateway_chooser(device_type=IVT)
gateway = BoschGateway(
session=None,
session_type=XMPP,
host="aaa",
access_token="a",
password="a",
)
await gateway.initialize()
await gateway.get_capabilities()
sensors = gateway.sensors

def find_ch_rec():
for sensor in sensors:
if sensor.name == "ractualCHPower":
return sensor

recording_sensor = find_ch_rec()
start = datetime.fromisoformat("2024-03-27T00:00:00+01:00")
stop = datetime.fromisoformat("2024-03-29T22:06:24+01:00")
range = await recording_sensor.fetch_range(start, stop)
print(range)

# await hc.set_ha_mode("auto") #MEANS AUTO
# await hc.update()
# time.sleep(4)
# return
# return
# await dhw.set_ha_mode("performance") #MEANS MANUAL
return
# print("target in manual", hc.target_temperature)
# print("ha mode in manual", hc.ha_mode)
# await hc.update()
# print("target after update", hc.target_temperature)
# print("ha mode", hc.ha_mode)

# await hc.set_ha_mode("auto") #MEANS AUTO
# print("target after auto without update", hc.target_temperature)
# print("ha mode", hc.ha_mode)

# return
# print(await hc.set_temperature(10.0))
# print("ustawiona!")
dhws = gateway.dhw_circuits
dhw = dhws[0]
await dhw.update()
print("START1")
print(dhw.target_temperature)
print("START2")
print(dhw.current_mode)
print(dhw.target_temperature)

return
print("START3")
print(dhw.target_temperature)
return
# print(hc.schedule)
print(gateway.get_info(DATE))
# print(await gateway.rawscan())
# print(hc.schedule.get_temp_for_date(gateway.get_info(DATE)))
return
aa = 0
while aa < 10:
time.sleep(1)
await hc.update()
print(hc.target_temperature)
aa = aa + 1

await hc.set_operation_mode("auto")

aa = 0
while aa < 10:
time.sleep(1)
await hc.update()
print(hc.target_temperature)
aa = aa + 1

# print(gateway.get_property(TYPE_INFO, UUID))
await loop.close()


asyncio.run(main())

# asyncio.get_event_loop().run_until_complete(main())

0 comments on commit ed70ba2

Please sign in to comment.