# Project Progress

## Overview

This is a TypeScript port of the pydaikin Python library for controlling Daikin air conditioners. The project is located in `daikin-ts/`.

## Goals

- [x] Port pydaikin to TypeScript
- [x] Include all device types (BRP069, BRP084, BRP072C, AirBase, SkyFi)
- [x] Include UDP discovery functionality
- [x] Target Node 18+
- [x] Use axios for HTTP requests
- [x] Use Vitest for testing

## Completed Tasks

### Project Setup
- [x] Created project structure with package.json, tsconfig.json, vitest.config.ts
- [x] Configured TypeScript for ES2022 with strict mode
- [x] Set up Vitest for testing
- [x] Added dependencies: axios, nock, vitest

### Core Implementation
- [x] **types.ts** - TypeScript interfaces for device types and translations
- [x] **exceptions.ts** - DaikinException error classes
- [x] **response.ts** - HTTP response parser (key=value&... format)
- [x] **http.ts** - Axios wrapper with retry logic and HTTPS support for BRP072C
- [x] **values.ts** - ApplianceValues TTL caching container
- [x] **appliance.ts** - Base Appliance class with energy consumption methods
- [x] **factory.ts** - Device factory with auto-detection
- [x] **discovery.ts** - UDP broadcast discovery

### Device Implementations
- [x] **brp069.ts** - Standard BRP069 devices
- [x] **brp084.ts** - Firmware 2.8.0 devices
- [x] **brp072c.ts** - BRP072C with SSL (extends BRP069)
- [x] **airbase.ts** - AirBase with zones
- [x] **skyfi.ts** - Legacy SkyFi devices

### Testing
- [x] **tests/test_values.ts** - ApplianceValues tests (20 tests)
- [x] **tests/test_response.ts** - Response parser tests (7 tests)
- [x] **tests/test_daikin_brp069.ts** - BRP069 device tests (14 tests)

### Real Device Testing
- [x] Successfully connected to real BRP072C device at 192.168.3.196
- [x] Retrieved device status:
  - Power: on
  - Mode: cool
  - Target temperature: 22°C
  - Inside temperature: 27°C
  - Outside temperature: 31°C

### Test Scripts
- [x] **test-device.ts** - Simple status check script
- [x] **test-restore.ts** - Restores device to original state (cool, 22°C, auto, horizontal)
- [x] **test-interactive.ts** - Interactive menu for testing various controls

### Known Issues Resolved
1. **BRP072C HTTPS issue**: Older BRP072C devices use legacy SSL/TLS (TLS 1.0) which is blocked by default in Node.js 18+. Added `SSL_OP_LEGACY_SERVER_CONNECT` and `SECLEVEL=0` ciphers.
2. **Mixins with TypeScript inheritance**: Inlined DaikinPowerMixin functionality directly into the Appliance class.
3. **Response parser regex issue**: Changed from regex-based to split-based parsing to handle values containing "=".
4. **ApplianceValues Map size property**: Fixed getter ordering issue.
5. **humanToDaikin translation**: Fixed bug where `set()` method was using base Appliance class TRANSLATIONS instead of the device-specific TRANSLATIONS (e.g., DaikinBRP069), causing values like 'cool' not to be translated to '3'.

## Remaining Tasks
- [x] Create control test script to verify all controllable options
- [ ] Add tests for other device types (BRP084, AirBase, SkyFi)
- [ ] Implement UDP discovery tests

## Technical Notes

### Device Connection
- BRP069/BRP084: HTTP on port 80
- BRP072C: HTTPS on port 443 (requires legacy SSL support)
- AirBase: HTTP on port 80
- SkyFi: HTTP on port 80

### Device Type Detection (Factory)

The `DaikinFactory()` function uses priority-based detection:

1. **If `key` is provided** → Creates `DaikinBRP072C` (HTTPS with authentication)
2. **If `password` is provided** → Creates `DaikinSkyFi`
3. **Try BRP084** → Creates and tests `DaikinBRP084`
4. **Try BRP069** → Creates and tests `DaikinBRP069`
5. **Default** → Creates `DaikinAirBase`

```typescript
// Direct device type (no detection)
const device = await DaikinFactory('192.168.1.100', { key: 'YOUR_KEY' });

// Auto-detect device type
const device = await DaikinFactory('192.168.1.100');
```

### Key Implementation Details
- Uses axios for HTTP requests with custom retry logic
- ApplianceValues uses Map with TTL caching
- Translation maps convert between Daikin codes and human-readable values
- Factory auto-detects device type from response data

### Temperature Format
When setting temperature (`stemp`), always use decimal format:
```typescript
// ✅ Correct
await device.set({ stemp: '22.0' });

// ❌ May not work
await device.set({ stemp: '22' });
```

### API Endpoints
- **Get Control Info**: `GET /aircon/get_control_info`
- **Set Control Info**: `GET /aircon/set_control_info`

## File Structure

```
daikin-ts/
├── package.json
├── tsconfig.json
├── vitest.config.ts
├── docs/
│   ├── CONTROL_OPTIONS.md
│   ├── PARAMETERS.md
│   └── PROGRESS.md
├── src/
│   ├── index.ts              # Main exports
│   ├── types.ts              # Interfaces
│   ├── exceptions.ts         # Error classes
│   ├── response.ts           # HTTP parser
│   ├── http.ts               # Axios wrapper
│   ├── values.ts             # TTL cache
│   ├── appliance.ts          # Base class
│   ├── factory.ts             # Device factory
│   ├── discovery.ts           # UDP discovery
│   └── devices/
│       ├── index.ts
│       ├── brp069.ts
│       ├── brp084.ts
│       ├── brp072c.ts
│       ├── airbase.ts
│       └── skyfi.ts
├── tests/
│   ├── test_values.ts
│   ├── test_response.ts
│   └── test_daikin_brp069.ts
├── test-device.ts            # Status check script
├── test-restore.ts           # Restore original state
└── test-interactive.ts       # Interactive control menu
```

## Test Results

```
Test Files:
  - tests/test_values.ts: 20 passing
  - tests/test_response.ts: 7 passing  
  - tests/test_daikin_brp069.ts: 14 passing

Total: 41 tests passing
```
