Skip to content

Commit

Permalink
Major Update
Browse files Browse the repository at this point in the history
  • Loading branch information
luneei committed Jan 4, 2025
1 parent 15dd243 commit ebbfebe
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 47 deletions.
12 changes: 10 additions & 2 deletions custom_components/kocom_wallpad/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
ThermostatPacket,
FanPacket,
MotionPacket,
PrivatePacket,
PublicPacket,
)

from .gateway import KocomGateway
Expand All @@ -38,7 +40,10 @@ async def async_setup_entry(
@callback
def async_add_binary_sensor(packet: KocomPacket) -> None:
"""Add new binary sensor entity."""
if isinstance(packet, (ThermostatPacket, FanPacket, MotionPacket)):
if isinstance(
packet,
(ThermostatPacket, FanPacket, MotionPacket, PrivatePacket, PublicPacket)
):
async_add_entities([KocomBinarySensorEntity(gateway, packet)])

for entity in gateway.get_entities(Platform.BINARY_SENSOR):
Expand Down Expand Up @@ -66,10 +71,13 @@ def __init__(
DEVICE_TYPE: self.packet._device.device_type,
ROOM_ID: self.packet._device.room_id,
SUB_ID: self.packet._device.sub_id,
ERROR_CODE: self.packet._device.state[ERROR_CODE],
ERROR_CODE: self.packet._device.state.get(ERROR_CODE),
}

if self.packet.device_type == DeviceType.MOTION:
self._attr_device_class = BinarySensorDeviceClass.MOTION
del self._attr_extra_state_attributes[ERROR_CODE]
self._attr_extra_state_attributes[TIME] = self.packet._device.state[TIME]
if self.packet.device_type in {DeviceType.PRIVATE, DeviceType.PUBLIC}:
self._attr_device_class = None
del self._attr_extra_state_attributes[ERROR_CODE]
6 changes: 5 additions & 1 deletion custom_components/kocom_wallpad/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
GasPacket,
MotionPacket,
EVPacket,
PrivatePacket,
PublicPacket,
)

from homeassistant.const import Platform
Expand All @@ -25,7 +27,7 @@
BRAND_NAME = "Kocom"
MANUFACTURER = "KOCOM Co., Ltd"
MODEL = "Smart Wallpad"
SW_VERSION = "1.0.6"
SW_VERSION = "1.0.7"

DEVICE_TYPE = "device_type"
ROOM_ID = "room_id"
Expand All @@ -44,4 +46,6 @@
GasPacket: Platform.SWITCH,
MotionPacket: Platform.BINARY_SENSOR,
EVPacket: Platform.SWITCH,
PrivatePacket: Platform.SWITCH,
PublicPacket: Platform.SWITCH,
}
10 changes: 9 additions & 1 deletion custom_components/kocom_wallpad/entity.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Entity classes for Kocom Wallpad."""

from __future__ import annotations
import asyncio

from homeassistant.core import callback
from homeassistant.helpers.entity import DeviceInfo
Expand Down Expand Up @@ -110,4 +111,11 @@ def extra_restore_state_data(self) -> RestoredExtraData:

async def send_packet(self, packet: bytes) -> None:
"""Send a packet to the gateway."""
await self.gateway.client.send_packet(packet)
if isinstance(packet, list):
for item in packet:
if isinstance(item, float):
await asyncio.sleep(item)
elif isinstance(item, bytearray):
await self.gateway.connection.send(item)
else:
await self.gateway.client.send_packet(packet)
17 changes: 15 additions & 2 deletions custom_components/kocom_wallpad/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,21 @@
from dataclasses import dataclass

from .pywallpad.client import KocomClient
from .pywallpad.const import ERROR, CO2, TEMPERATURE, DIRECTION, FLOOR
from .pywallpad.const import (
ERROR,
CO2,
TEMPERATURE,
DIRECTION,
FLOOR,
BELL,
)
from .pywallpad.packet import (
KocomPacket,
ThermostatPacket,
FanPacket,
EVPacket,
PrivatePacket,
PublicPacket,
PacketParser,
)

Expand Down Expand Up @@ -126,7 +135,9 @@ def parse_platform(self, packet: KocomPacket) -> Platform | None:
LOGGER.warning(f"Unrecognized platform type: {type(packet).__name__}")
return None

platform_packet_types = (ThermostatPacket, FanPacket, EVPacket)
platform_packet_types = (
ThermostatPacket, FanPacket, EVPacket, PrivatePacket, PublicPacket
)
if isinstance(packet, platform_packet_types) and (sub_id := packet._device.sub_id):
if ERROR in sub_id:
platform = Platform.BINARY_SENSOR
Expand All @@ -136,6 +147,8 @@ def parse_platform(self, packet: KocomPacket) -> Platform | None:
platform = Platform.SENSOR
elif sub_id in {DIRECTION, FLOOR}: # EV
platform = Platform.SENSOR
elif sub_id in BELL: # Intercom
platform = Platform.BINARY_SENSOR

return platform

2 changes: 1 addition & 1 deletion custom_components/kocom_wallpad/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
"iot_class": "local_push",
"issue_tracker": "https://github.com/lunDreame/kocom-wallpad/issues",
"requirements": [],
"version": "1.0.6"
"version": "1.0.7"
}
25 changes: 12 additions & 13 deletions custom_components/kocom_wallpad/pywallpad/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from ..connection import Connection

from .crc import verify_checksum, calculate_checksum
from .crc import verify_checksum, verify_crc, calculate_checksum
from .packet import PacketParser
from .const import _LOGGER, PREFIX_HEADER, SUFFIX_HEADER

Expand Down Expand Up @@ -100,18 +100,17 @@ async def _listen(self) -> None:

packet_list = self.extract_packets(receive_data)
for packet in packet_list:
if not verify_checksum(packet):
_LOGGER.debug("Checksum verification failed for packet: %s", packet.hex())
continue

parsed_packets = PacketParser.parse_state(packet)
for parsed_packet in parsed_packets:
_LOGGER.debug(
"Received packet: %s, %s, %s",
parsed_packet, parsed_packet._device, parsed_packet._last_data
)
for callback in self.device_callbacks:
await callback(parsed_packet)
if verify_checksum(packet) or verify_crc(packet):
parsed_packets = PacketParser.parse_state(packet)
for parsed_packet in parsed_packets:
_LOGGER.debug(
"Received packet: %s, %s, %s",
parsed_packet, parsed_packet._device, parsed_packet._last_data
)
for callback in self.device_callbacks:
await callback(parsed_packet)
else:
_LOGGER.debug("Received invalid packet: %s", packet.hex())
except Exception as e:
_LOGGER.error(f"Error receiving data: {e}", exc_info=True)

Expand Down
3 changes: 3 additions & 0 deletions custom_components/kocom_wallpad/pywallpad/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@

DIRECTION = "direction"
FLOOR = "floor"

SHUTDOWN = "shutdown"
BELL = "bell"
3 changes: 3 additions & 0 deletions custom_components/kocom_wallpad/pywallpad/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ class DeviceType(IntEnum):
MOTION = 0x60
IGNORE = 0x86
IAQ = 0x98
NONE = 0xFF

class PacketType(IntEnum):
"""Packet types for Kocom devices."""
CALL = 0x09
SEND = 0x0B
RECV = 0x0D

Expand All @@ -36,6 +38,7 @@ class Command(IntEnum):
OFF = 0x02
DETECT = 0x04
SCAN = 0x3A
NONE = 0xFF

class OpMode(IntEnum):
"""Operating modes for AC devices."""
Expand Down
Loading

0 comments on commit ebbfebe

Please sign in to comment.