Skip to content

Commit

Permalink
file-mode debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrilp committed Mar 1, 2024
1 parent 7ec2fcc commit 9634312
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
16 changes: 16 additions & 0 deletions custom_components/deltadore_tydom/ha_entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,3 +817,19 @@ def device_info(self) -> DeviceInfo:
"name": self.name,
}

class HaAlarm(AlarmControlPanelEntity, HAEntity):
"""Representation of an Alarm."""

should_poll = False
supported_features = 0
code_format = CodeFormat.NUMBER
sensor_classes = {}

def __init__(self, device: TydomAlarm, hass) -> None:
"""Initialize the sensor."""
self.hass = hass
self._device = device
self._device._ha_device = self
self._attr_unique_id = f"{self._device.device_id}_cover"
self._attr_name = self._device.device_name
self._registered_sensors = []
10 changes: 9 additions & 1 deletion custom_components/deltadore_tydom/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
HaGate,
HaGarage,
HaLight,
HaAlarm,
)

from .const import LOGGER
Expand Down Expand Up @@ -107,7 +108,8 @@ async def get_tydom_credentials(
async def test_credentials(self) -> None:
"""Validate credentials."""
connection = await self._tydom_client.async_connect()
await connection.close()
if hasattr(connection, "close"):
await connection.close()

def ready(self) -> bool:
"""Check if we're ready to work."""
Expand Down Expand Up @@ -234,6 +236,12 @@ async def create_ha_device(self, device):
self.add_sensor_callback(ha_device.get_sensors())
case TydomAlarm():
LOGGER.debug("Create alarm %s", device.device_id)
ha_device = HaAlarm(device, self._hass)
if self.add_light_callback is not None:
self.add_light_callback([ha_device])

if self.add_sensor_callback is not None:
self.add_sensor_callback(ha_device.get_sensors())
LOGGER.error("Alarm Not implemented yet.")
case _:
LOGGER.error(
Expand Down
5 changes: 4 additions & 1 deletion custom_components/deltadore_tydom/tydom/MessageHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ def get_http_request_line(data) -> str:
async def incoming_triage(self, bytes_str):
"""Identify message type and dispatch the result."""

if bytes_str is None:
return None

incoming = None

# Find Uri-Origin in header if available
Expand Down Expand Up @@ -187,7 +190,7 @@ async def parse_response(self, incoming, uri_origin, http_request_line):
return await self.parse_msg_info(parsed)

except Exception as e:
LOGGER.error("Error on parsing tydom response (%s)", e)
LOGGER.error("Error on parsing tydom response (%s)", data)
LOGGER.exception("Error on parsing tydom response")
traceback.print_exception(e)
LOGGER.debug("Incoming data parsed with success")
Expand Down
30 changes: 28 additions & 2 deletions custom_components/deltadore_tydom/tydom/tydom_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ class TydomClientApiClientAuthenticationError(TydomClientApiClientError):

proxy = None

# For debugging with traces
file_mode = False
file_lines = None
file_index = 0
file_name = "/config/traces.txt"

class TydomClient:
"""Tydom API Client."""
Expand Down Expand Up @@ -89,6 +94,8 @@ async def async_get_credentials(
session: ClientSession, email: str, password: str, macaddress: str
):
"""Get tydom credentials from Delta Dore."""
if file_mode:
return "dummyPassword"
try:
async with async_timeout.timeout(10):
response = await session.request(
Expand Down Expand Up @@ -176,6 +183,13 @@ async def async_get_credentials(

async def async_connect(self) -> ClientWebSocketResponse:
"""Connect to the Tydom API."""
global file_lines, file_mode, file_name
if file_mode:
file = open(file_name)
file_lines = file.readlines()

return None

http_headers = {
"Connection": "Upgrade",
"Upgrade": "websocket",
Expand Down Expand Up @@ -281,6 +295,18 @@ async def listen_tydom(self, connection: ClientWebSocketResponse):

async def consume_messages(self):
"""Read and parse incomming messages."""
global file_lines, file_mode, file_index
if file_mode:
if (len(file_lines) > file_index):
incoming = file_lines[file_index].replace("\\r", '\x0d').replace("\\n", "\x0a")
incoming_bytes_str = incoming.encode("utf-8")
file_index += 1
LOGGER.info("Incomming message - message : %s", incoming_bytes_str)
else:
await asyncio.sleep(10)
return None
await asyncio.sleep(1)
return await self._message_handler.incoming_triage(incoming_bytes_str)
try:
if self._connection.closed:
await self._connection.close()
Expand Down Expand Up @@ -362,8 +388,8 @@ async def send_message(self, method, msg):
method,
msg if "pwd" not in msg else "***",
)

await self.send_bytes(a_bytes)
if not file_mode:
await self.send_bytes(a_bytes)


# ########################
Expand Down

0 comments on commit 9634312

Please sign in to comment.