Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File mode debug #56

Merged
merged 7 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
58 changes: 35 additions & 23 deletions custom_components/deltadore_tydom/ha_entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,16 @@
from homeassistant.components.cover import (
ATTR_POSITION,
ATTR_TILT_POSITION,
SUPPORT_CLOSE,
SUPPORT_OPEN,
SUPPORT_SET_POSITION,
SUPPORT_STOP,
SUPPORT_SET_TILT_POSITION,
SUPPORT_OPEN_TILT,
SUPPORT_CLOSE_TILT,
SUPPORT_STOP_TILT,
CoverEntity,
CoverDeviceClass,
CoverEntityFeature,
)

from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass, SensorEntity
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 @@ -53,6 +48,7 @@
TydomGate,
TydomGarage,
TydomLight,
TydomAlarm,
)

from .const import DOMAIN, LOGGER
Expand Down Expand Up @@ -455,18 +451,18 @@ def __init__(self, device: TydomShutter, hass) -> None:
if hasattr(device, "position"):
self.supported_features = (
self.supported_features
| SUPPORT_SET_POSITION
| SUPPORT_OPEN
| SUPPORT_CLOSE
| SUPPORT_STOP
| CoverEntityFeature.SET_POSITION
| CoverEntityFeature.OPEN
| CoverEntityFeature.CLOSE
| CoverEntityFeature.STOP
)
if hasattr(device, "slope"):
self.supported_features = (
self.supported_features
| SUPPORT_SET_TILT_POSITION
| SUPPORT_OPEN_TILT
| SUPPORT_CLOSE_TILT
| SUPPORT_STOP_TILT
| CoverEntityFeature.SET_TILT_POSITION
| CoverEntityFeature.OPEN_TILT
| CoverEntityFeature.CLOSE_TILT
| CoverEntityFeature.STOP_TILT
)

@property
Expand Down Expand Up @@ -613,6 +609,13 @@ def __init__(self, device: TydomBoiler, hass) -> None:
self._device._ha_device = self
self._attr_unique_id = f"{self._device.device_id}_climate"
self._attr_name = self._device.device_name
self._enable_turn_on_off_backwards_compatibility = False
self._attr_supported_features = (
self._attr_supported_features
| ClimateEntityFeature.TARGET_TEMPERATURE
| ClimateEntityFeature.TURN_OFF
| ClimateEntityFeature.TURN_ON
)

# self._attr_preset_modes = ["NORMAL", "STOP", "ANTI_FROST"]
self._attr_hvac_modes = [
Expand All @@ -628,13 +631,6 @@ def __init__(self, device: TydomBoiler, hass) -> None:
if "max" in self._device._metadata["setpoint"]:
self._attr_max_temp = self._device._metadata["setpoint"]["max"]

@property
def supported_features(self) -> ClimateEntityFeature:
"""Return the list of supported features."""
features = ClimateEntityFeature(0)
features = features | ClimateEntityFeature.TARGET_TEMPERATURE #| ClimateEntityFeature.PRESET_MODE
return features

@property
def device_info(self) -> DeviceInfo:
"""Information about this entity/device."""
Expand Down Expand Up @@ -823,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 = []
11 changes: 9 additions & 2 deletions 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 @@ -143,7 +145,6 @@ async def create_ha_device(self, device):
match device:
case Tydom():
LOGGER.debug("Create Tydom gateway %s", device.device_id)
LOGGER.error("Tydom gateway %s", device._tydom_client)
self.devices[device.device_id] = device
ha_device = HATydom(device, self._hass, self._entry)
self.ha_devices[device.device_id] = ha_device
Expand Down Expand Up @@ -235,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