Send + ack
This commit is contained in:
@@ -235,7 +235,13 @@ class DknCloudNaClient:
|
||||
payload = {"mac": mac, "property": property_name, "value": value}
|
||||
LOGGER.debug("DKN socket send %s %s", namespace, payload)
|
||||
try:
|
||||
await socket.emit("create-machine-event", payload, namespace=namespace)
|
||||
ack = await socket.call(
|
||||
"create-machine-event",
|
||||
payload,
|
||||
namespace=namespace,
|
||||
timeout=REQUEST_TIMEOUT,
|
||||
)
|
||||
LOGGER.debug("DKN socket ack %s %s", namespace, ack)
|
||||
except Exception as err: # noqa: BLE001
|
||||
raise DknConnectionError(str(err) or type(err).__name__) from err
|
||||
|
||||
|
||||
@@ -182,7 +182,7 @@ class DknClimateEntity(DknEntity, ClimateEntity):
|
||||
try:
|
||||
if hvac_mode == HVACMode.OFF:
|
||||
await self.coordinator.client.async_send_machine_event(
|
||||
installation_id, self._mac, "power", False
|
||||
installation_id, self._command_mac, "power", False
|
||||
)
|
||||
self._optimistic_set("power", False)
|
||||
self._optimistic_set("hvac_mode", HVACMode.OFF)
|
||||
@@ -191,10 +191,10 @@ class DknClimateEntity(DknEntity, ClimateEntity):
|
||||
if mode is None:
|
||||
raise HomeAssistantError(f"Unsupported HVAC mode: {hvac_mode}")
|
||||
await self.coordinator.client.async_send_machine_event(
|
||||
installation_id, self._mac, "power", True
|
||||
installation_id, self._command_mac, "power", True
|
||||
)
|
||||
await self.coordinator.client.async_send_machine_event(
|
||||
installation_id, self._mac, "mode", mode
|
||||
installation_id, self._command_mac, "mode", mode
|
||||
)
|
||||
self._optimistic_set("power", True)
|
||||
self._optimistic_set("hvac_mode", hvac_mode)
|
||||
@@ -226,7 +226,7 @@ class DknClimateEntity(DknEntity, ClimateEntity):
|
||||
async with self._get_device_lock():
|
||||
try:
|
||||
await self.coordinator.client.async_send_machine_event(
|
||||
installation_id, self._mac, property_name, device_temp
|
||||
installation_id, self._command_mac, property_name, device_temp
|
||||
)
|
||||
except Exception as err: # noqa: BLE001
|
||||
raise HomeAssistantError(f"Failed to set temperature: {err}") from err
|
||||
@@ -246,7 +246,7 @@ class DknClimateEntity(DknEntity, ClimateEntity):
|
||||
async with self._get_device_lock():
|
||||
try:
|
||||
await self.coordinator.client.async_send_machine_event(
|
||||
installation_id, self._mac, "speed_state", speed
|
||||
installation_id, self._command_mac, "speed_state", speed
|
||||
)
|
||||
except Exception as err: # noqa: BLE001
|
||||
raise HomeAssistantError(f"Failed to set fan mode: {err}") from err
|
||||
@@ -266,7 +266,7 @@ class DknClimateEntity(DknEntity, ClimateEntity):
|
||||
async with self._get_device_lock():
|
||||
try:
|
||||
await self.coordinator.client.async_send_machine_event(
|
||||
installation_id, self._mac, "slats_vertical_1", slat
|
||||
installation_id, self._command_mac, "slats_vertical_1", slat
|
||||
)
|
||||
except Exception as err: # noqa: BLE001
|
||||
raise HomeAssistantError(f"Failed to set swing mode: {err}") from err
|
||||
|
||||
@@ -9,7 +9,12 @@ from typing import Any
|
||||
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, DeviceInfo
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import DOMAIN, MANUFACTURER, OPTIMISTIC_TTL_SEC, POST_WRITE_REFRESH_DELAY_SEC
|
||||
from .const import (
|
||||
DOMAIN,
|
||||
MANUFACTURER,
|
||||
OPTIMISTIC_TTL_SEC,
|
||||
POST_WRITE_REFRESH_DELAY_SEC,
|
||||
)
|
||||
from .coordinator import DknCoordinator
|
||||
|
||||
|
||||
@@ -35,6 +40,14 @@ class DknEntity(CoordinatorEntity[DknCoordinator]):
|
||||
"""Return raw device dict from coordinator, or empty dict if unavailable."""
|
||||
return (self.coordinator.data or {}).get(self._mac, {})
|
||||
|
||||
@property
|
||||
def _command_mac(self) -> str:
|
||||
"""Return the device MAC in the form expected by the cloud API."""
|
||||
mac = self._device_data.get("mac")
|
||||
if isinstance(mac, str) and mac.strip():
|
||||
return mac.strip()
|
||||
return self._mac.upper()
|
||||
|
||||
@property
|
||||
def device_info(self) -> DeviceInfo:
|
||||
data = self._device_data
|
||||
@@ -71,13 +84,14 @@ class DknEntity(CoordinatorEntity[DknCoordinator]):
|
||||
)
|
||||
overlays: dict[str, dict[str, Any]] = bucket.setdefault("optimistic", {})
|
||||
device_overlays = overlays.setdefault(self._mac, {})
|
||||
device_overlays[key] = {"value": value, "expires": time.monotonic() + OPTIMISTIC_TTL_SEC}
|
||||
device_overlays[key] = {
|
||||
"value": value,
|
||||
"expires": time.monotonic() + OPTIMISTIC_TTL_SEC,
|
||||
}
|
||||
|
||||
def _optimistic_get(self, key: str, fallback: Any) -> Any:
|
||||
"""Return the optimistic value if still fresh, else fallback."""
|
||||
bucket = self.hass.data.get(DOMAIN, {}).get(
|
||||
self.coordinator.entry_id, {}
|
||||
)
|
||||
bucket = self.hass.data.get(DOMAIN, {}).get(self.coordinator.entry_id, {})
|
||||
overlays = bucket.get("optimistic", {}).get(self._mac, {})
|
||||
entry = overlays.get(key)
|
||||
if entry and time.monotonic() < entry["expires"]:
|
||||
@@ -86,9 +100,7 @@ class DknEntity(CoordinatorEntity[DknCoordinator]):
|
||||
|
||||
def _optimistic_clear(self, key: str) -> None:
|
||||
"""Expire an optimistic overlay immediately."""
|
||||
bucket = self.hass.data.get(DOMAIN, {}).get(
|
||||
self.coordinator.entry_id, {}
|
||||
)
|
||||
bucket = self.hass.data.get(DOMAIN, {}).get(self.coordinator.entry_id, {})
|
||||
overlays = bucket.get("optimistic", {}).get(self._mac, {})
|
||||
overlays.pop(key, None)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user