Skip to content

Commit

Permalink
File mode debug (#56)
Browse files Browse the repository at this point in the history
* fix Warnings introduced in HA 2024.2

* remove buggy error log

* implement file-mode debugging
  • Loading branch information
CyrilP authored Mar 1, 2024
1 parent 0e9ff5d commit cbe9908
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 8 deletions.
8 changes: 4 additions & 4 deletions .ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

target-version = "py310"

select = [
lint.select = [
"B007", # Loop control variable {name} not used within loop body
"B014", # Exception handler with duplicate exception
"C", # complexity
Expand All @@ -26,7 +26,7 @@ select = [
"W", # pycodestyle
]

ignore = [
lint.ignore = [
"D202", # No blank lines allowed after function docstring
"D203", # 1 blank line required before class docstring
"D213", # Multi-line docstring summary should start at the second line
Expand All @@ -38,8 +38,8 @@ ignore = [
"E731", # do not assign a lambda expression, use a def
]

[flake8-pytest-style]
[lint.flake8-pytest-style]
fixture-parentheses = false

[mccabe]
[lint.mccabe]
max-complexity = 25
18 changes: 18 additions & 0 deletions custom_components/deltadore_tydom/ha_entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from homeassistant.components.light import LightEntity
from homeassistant.components.lock import LockEntity
from homeassistant.components.update import UpdateEntity, UpdateEntityFeature, UpdateDeviceClass
from homeassistant.components.alarm_control_panel import AlarmControlPanelEntity, CodeFormat

from .tydom.tydom_devices import (
Tydom,
Expand All @@ -47,6 +48,7 @@
TydomGate,
TydomGarage,
TydomLight,
TydomAlarm,
)

from .const import DOMAIN, LOGGER
Expand Down Expand Up @@ -817,3 +819,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 cbe9908

Please sign in to comment.