Skip to content

Commit

Permalink
One less time.sleep() call
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Jun 25, 2022
1 parent da6f0c0 commit ebfd779
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions custom_components/jablotron100/jablotron.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from homeassistant.helpers import storage
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.event import async_call_later
from homeassistant.helpers.typing import StateType
from homeassistant.helpers import entity_registry as er
import math
Expand Down Expand Up @@ -322,29 +323,33 @@ def modify_alarm_control_panel_section_state(self, section: int, state: str, cod
# Reset
self._successful_login = True

def callback(_) -> None:
if self._successful_login is True:
state_packet = self.int_to_bytes(int_packets[state] + section)
self._send_packet(self.create_packet_ui_control(UI_CONTROL_MODIFY_SECTION, state_packet))

after_packets = []

if code != self._config[CONF_PASSWORD]:
after_packets.append(self.create_packet_ui_control(UI_CONTROL_AUTHORISATION_END))
after_packets.extend(self.create_packets_keepalive(self._config[CONF_PASSWORD]))

# Update states - should fix state when invalid code was inserted
after_packets.append(self.create_packet_command(COMMAND_GET_SECTIONS_AND_PG_OUTPUTS_STATES))

self._send_packets(after_packets)

if code != self._config[CONF_PASSWORD]:
packets = [
self.create_packet_ui_control(UI_CONTROL_AUTHORISATION_END),
self.create_packet_authorisation_code(code),
]

self._send_packets(packets)
time.sleep(1)

if self._successful_login is True:
state_packet = self.int_to_bytes(int_packets[state] + section)
self._send_packet(self.create_packet_ui_control(UI_CONTROL_MODIFY_SECTION, state_packet))

after_packets = []

if code != self._config[CONF_PASSWORD]:
after_packets.append(self.create_packet_ui_control(UI_CONTROL_AUTHORISATION_END))
after_packets.extend(self.create_packets_keepalive(self._config[CONF_PASSWORD]))

# Update states - should fix state when invalid code was inserted
after_packets.append(self.create_packet_command(COMMAND_GET_SECTIONS_AND_PG_OUTPUTS_STATES))

self._send_packets(after_packets)
async_call_later(self._hass, 1.0, callback)
else:
callback(None)

def toggle_pg_output(self, pg_output_number: int, state: str) -> None:
pg_output_number_packet = self.int_to_bytes(pg_output_number - 1)
Expand Down

0 comments on commit ebfd779

Please sign in to comment.