Skip to content

Commit

Permalink
Update hub.py
Browse files Browse the repository at this point in the history
  • Loading branch information
bremor authored Aug 11, 2021
1 parent 5e7683c commit f473fb9
Showing 1 changed file with 39 additions and 13 deletions.
52 changes: 39 additions & 13 deletions custom_components/bonaire_myclimate/BonairePyClimate/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def __init__(self, hass, local_ip):
self._callbacks = set()
self._connected = False # When there is an open TCP connection
self._fan_mode_memory_heat = "thermo"
self._postzoneinfo_response_ok = False
self._queued_commands = []
self._ready = False # Ready to send commands
self._start_task = None
self._zone_info = {}
Expand Down Expand Up @@ -211,8 +213,8 @@ def server_data_received(self, data):
self.fan_modes = FAN_MODES_HEAT
self.hvac_mode = HVAC_MODE_HEAT
self.preset_modes = self._preset_modes_heat
self.target_temperature = int(self._zone_info["setPoint"])
self.supported_features = SUPPORT_TARGET_TEMPERATURE | SUPPORT_PRESET_MODE | SUPPORT_FAN_MODE
self.target_temperature = int(self._zone_info["setPoint"])
elif self._zone_info["type"] == "cool":
self.fan_mode = self._zone_info["mode"]
self.fan_modes = FAN_MODES_COOL
Expand All @@ -239,6 +241,7 @@ def server_data_received(self, data):
elif (root.findtext("response") == "postzoneinfo" and
root.findtext("result") == "ok"):

self._postzoneinfo_response_ok = True
_LOGGER.info("Sending getzoneinfo")
_LOGGER.debug(f"Sending: {XML_GETZONEINFO}")
self._server_transport.write(XML_GETZONEINFO.encode())
Expand Down Expand Up @@ -305,16 +308,39 @@ async def async_set_temperature(self, temperature):
command = {"setPoint": str(temperature)}
await self.async_send_commands(command)

async def async_send_commands(self, commands):
async def async_send_commands(self, command):
"""Send commands to device."""
# Wait until ready to send commands
while not self._ready:
await asyncio.sleep(1)

postzoneinfo = "<myclimate><post>postzoneinfo</post>"
for command, value in commands.items():
postzoneinfo += f"<{command}>{value}</{command}>"
postzoneinfo += "</myclimate>"
_LOGGER.info("Sending postzoneinfo")
_LOGGER.debug(f"Sending: {postzoneinfo}")
self._server_transport.write(postzoneinfo.encode())
_LOGGER.warning(command)
# Append the given command to the end of the queued commands
self._queued_commands.append(command)

if len(self._queued_commands) == 1:

while len(self._queued_commands) > 0:

# Wait until ready to send commands
while not self._ready:
await asyncio.sleep(1)

command = self._queued_commands[0]

postzoneinfo = "<myclimate><post>postzoneinfo</post>"
for key, value in command.items():
postzoneinfo += f"<{key}>{value}</{key}>"
postzoneinfo += "</myclimate>"

# Attempt to send the command up to 3 times
for attempts in range(3):
if attempts > 0:
_LOGGER.info("Response not received, resending postzoneinfo")
else:
_LOGGER.info("Sending postzoneinfo")
_LOGGER.debug(f"Sending: {postzoneinfo}")
self._server_transport.write(postzoneinfo.encode())

await asyncio.sleep(3)
if self._postzoneinfo_response_ok: break
else:
_LOGGER.warning("No postzoneinfo response received after 3 attempts, aborting")

self._queued_commands.pop(0)

0 comments on commit f473fb9

Please sign in to comment.