export const homematicDomain = 'homematic';
export const homematicManufacturer = 'eQ-3';

export const homematicPressEvents = ['PRESS_SHORT', 'PRESS_LONG', 'PRESS_CONT', 'PRESS_LONG_RELEASE', 'PRESS'] as const;
export const homematicImpulseEvents = ['SEQUENCE_OK'] as const;

export const homematicSwitchDeviceTypes = [
  'Switch', 'SwitchPowermeter', 'IOSwitch', 'IOSwitchNoInhibit', 'IPSwitch', 'IPSwitchRssiDevice', 'RFSiren',
  'IPSwitchPowermeter', 'HMWIOSwitch', 'Rain', 'EcoLogic', 'IPKeySwitchPowermeter', 'IPGarage', 'IPKeySwitch',
  'IPKeySwitchLevel', 'IPMultiIO', 'IPWSwitch', 'IOSwitchWireless', 'IPWIODevice', 'IPSwitchBattery',
  'IPMultiIOPCB', 'IPGarageSwitch', 'IPWHS2',
];

export const homematicLightDeviceTypes = [
  'Dimmer', 'KeyDimmer', 'IPKeyDimmer', 'IPDimmer', 'ColorEffectLight', 'IPKeySwitchLevel', 'ColdWarmDimmer', 'IPWDimmer',
];

export const homematicSensorDeviceTypes = [
  'SwitchPowermeter', 'Motion', 'MotionV2', 'MotionIPV2', 'MotionIPContactSabotage', 'RemoteMotion', 'MotionIP',
  'ThermostatWall', 'AreaThermostat', 'RotaryHandleSensor', 'WaterSensor', 'PowermeterGas', 'LuxSensor',
  'WeatherSensor', 'WeatherStation', 'ThermostatWall2', 'TemperatureDiffSensor', 'TemperatureSensor', 'CO2Sensor',
  'IPSwitchPowermeter', 'HMWIOSwitch', 'FillingLevel', 'ValveDrive', 'EcoLogic', 'IPThermostatWall', 'IPSmoke',
  'RFSiren', 'PresenceIP', 'IPAreaThermostat', 'IPWeatherSensor', 'RotaryHandleSensorIP', 'IPPassageSensor',
  'IPKeySwitchPowermeter', 'IPThermostatWall230V', 'IPWeatherSensorPlus', 'IPWeatherSensorBasic', 'IPBrightnessSensor',
  'IPGarage', 'UniversalSensor', 'IPMultiIO', 'IPThermostatWall2', 'IPRemoteMotionV2', 'HBUNISenWEA', 'PresenceIPW',
  'IPRainSensor', 'ValveBox', 'IPKeyBlind', 'IPKeyBlindTilt', 'IPLanRouter', 'TempModuleSTE2', 'IPMultiIOPCB',
  'ValveBoxW', 'CO2SensorIP', 'IPLockDLD', 'ParticulateMatterSensorIP', 'IPRemoteMotionV2W',
];

export const homematicBinarySensorDeviceTypes = [
  'ShutterContact', 'Smoke', 'SmokeV2', 'SmokeV2Team', 'Motion', 'MotionV2', 'MotionIP', 'MotionIPV2',
  'MotionIPContactSabotage', 'RemoteMotion', 'WeatherSensor', 'TiltSensor', 'IPShutterContact', 'HMWIOSwitch',
  'MaxShutterContact', 'Rain', 'WiredSensor', 'PresenceIP', 'PresenceIPW', 'IPWeatherSensor', 'IPPassageSensor',
  'SmartwareMotion', 'IPWeatherSensorPlus', 'WaterIP', 'IPMultiIO', 'TiltIP', 'IPShutterContactSabotage', 'IPContact',
  'IPRemoteMotionV2', 'IPWInputDevice', 'IPWMotionDection', 'IPAlarmSensor', 'IPRainSensor', 'IPLanRouter',
  'IPMultiIOPCB', 'IPWHS2', 'IPRemoteMotionV2W',
];

export const homematicCoverDeviceTypes = ['Blind', 'KeyBlind', 'IPKeyBlind', 'IPKeyBlindTilt', 'IPGarage', 'IPKeyBlindMulti', 'IPWKeyBlindMulti'];

export const homematicClimateDeviceTypes = [
  'Thermostat', 'ThermostatWall', 'MAXThermostat', 'ThermostatWall2', 'MAXWallThermostat', 'IPThermostat',
  'IPThermostatWall', 'ThermostatGroup', 'IPThermostatWall230V', 'IPThermostatWall2', 'IPWThermostatWall',
];

export const homematicLockDeviceTypes = ['KeyMatic'];

export const homematicIgnoredSensorDatapoints = ['ACTUAL_TEMPERATURE', 'ACTUAL_HUMIDITY'];

export const homematicIgnoredSensorDatapointExceptions: Record<string, string[]> = {
  ACTUAL_TEMPERATURE: [
    'IPAreaThermostat', 'IPWeatherSensor', 'IPWeatherSensorPlus', 'IPWeatherSensorBasic', 'IPThermostatWall',
    'IPThermostatWall2', 'ParticulateMatterSensorIP', 'CO2SensorIP', 'TempModuleSTE2',
  ],
};

export const homematicAttributeSupport: Record<string, { attribute: string; values?: Record<string, string> }> = {
  LOWBAT: { attribute: 'battery', values: { '0': 'High', '1': 'Low' } },
  LOW_BAT: { attribute: 'battery', values: { '0': 'High', '1': 'Low' } },
  ERROR: { attribute: 'error', values: { '0': 'No' } },
  ERROR_SABOTAGE: { attribute: 'sabotage', values: { '0': 'No', '1': 'Yes' } },
  SABOTAGE: { attribute: 'sabotage', values: { '0': 'No', '1': 'Yes' } },
  RSSI_PEER: { attribute: 'rssi_peer' },
  RSSI_DEVICE: { attribute: 'rssi_device' },
  VALVE_STATE: { attribute: 'valve' },
  LEVEL: { attribute: 'level' },
  BATTERY_STATE: { attribute: 'battery' },
  CONTROL_MODE: { attribute: 'mode', values: { '0': 'Auto', '1': 'Manual', '2': 'Away', '3': 'Boost', '4': 'Comfort', '5': 'Lowering' } },
  POWER: { attribute: 'power' },
  CURRENT: { attribute: 'current' },
  VOLTAGE: { attribute: 'voltage' },
  OPERATING_VOLTAGE: { attribute: 'voltage' },
  WORKING: { attribute: 'working', values: { '0': 'No', '1': 'Yes' } },
  STATE_UNCERTAIN: { attribute: 'state_uncertain' },
  SENDERID: { attribute: 'last_senderid' },
  SENDERADDRESS: { attribute: 'last_senderaddress' },
};

export const homematicSensorMetadata: Record<string, { unit?: string; deviceClass?: string; stateClass?: string; icon?: string }> = {
  HUMIDITY: { unit: '%', deviceClass: 'humidity', stateClass: 'measurement' },
  ACTUAL_HUMIDITY: { unit: '%', deviceClass: 'humidity', stateClass: 'measurement' },
  ACTUAL_TEMPERATURE: { unit: 'C', deviceClass: 'temperature', stateClass: 'measurement' },
  TEMPERATURE: { unit: 'C', deviceClass: 'temperature', stateClass: 'measurement' },
  LUX: { unit: 'lx', deviceClass: 'illuminance', stateClass: 'measurement' },
  CURRENT_ILLUMINATION: { unit: 'lx', deviceClass: 'illuminance', stateClass: 'measurement' },
  ILLUMINATION: { unit: 'lx', deviceClass: 'illuminance', stateClass: 'measurement' },
  AVERAGE_ILLUMINATION: { unit: 'lx', deviceClass: 'illuminance', stateClass: 'measurement' },
  LOWEST_ILLUMINATION: { unit: 'lx', deviceClass: 'illuminance', stateClass: 'measurement' },
  HIGHEST_ILLUMINATION: { unit: 'lx', deviceClass: 'illuminance', stateClass: 'measurement' },
  POWER: { unit: 'W', deviceClass: 'power', stateClass: 'measurement' },
  IEC_POWER: { unit: 'W', deviceClass: 'power', stateClass: 'measurement' },
  CURRENT: { unit: 'mA', deviceClass: 'current', stateClass: 'measurement' },
  CONCENTRATION: { unit: 'ppm', deviceClass: 'carbon_dioxide', stateClass: 'measurement' },
  ENERGY_COUNTER: { unit: 'Wh', deviceClass: 'energy', stateClass: 'total_increasing' },
  IEC_ENERGY_COUNTER: { unit: 'kWh', deviceClass: 'energy', stateClass: 'total_increasing' },
  VOLTAGE: { unit: 'V', deviceClass: 'voltage', stateClass: 'measurement' },
  OPERATING_VOLTAGE: { unit: 'V', deviceClass: 'voltage', stateClass: 'measurement' },
  GAS_POWER: { unit: 'm3', deviceClass: 'gas' },
  GAS_ENERGY_COUNTER: { unit: 'm3', deviceClass: 'gas', stateClass: 'total_increasing' },
  RAIN_COUNTER: { unit: 'mm', deviceClass: 'precipitation' },
  WIND_SPEED: { unit: 'km/h', deviceClass: 'wind_speed', icon: 'mdi:weather-windy' },
  WIND_DIRECTION: { unit: 'degree', deviceClass: 'wind_direction', stateClass: 'measurement_angle' },
  WIND_DIR: { unit: 'degree', deviceClass: 'wind_direction', stateClass: 'measurement_angle' },
  WIND_DIRECTION_RANGE: { unit: 'degree' },
  WIND_DIR_RANGE: { unit: 'degree' },
  SUNSHINEDURATION: { unit: '#' },
  AIR_PRESSURE: { unit: 'hPa', deviceClass: 'pressure', stateClass: 'measurement' },
  FREQUENCY: { unit: 'Hz', deviceClass: 'frequency' },
  VALUE: { unit: '#' },
  VALVE_STATE: { unit: '%' },
  CARRIER_SENSE_LEVEL: { unit: '%' },
  DUTY_CYCLE_LEVEL: { unit: '%' },
  BRIGHTNESS: { unit: '#', icon: 'mdi:invert-colors' },
  MASS_CONCENTRATION_PM_1: { unit: 'ug/m3', deviceClass: 'pm1', stateClass: 'measurement' },
  MASS_CONCENTRATION_PM_2_5: { unit: 'ug/m3', deviceClass: 'pm25', stateClass: 'measurement' },
  MASS_CONCENTRATION_PM_10: { unit: 'ug/m3', deviceClass: 'pm10', stateClass: 'measurement' },
  MASS_CONCENTRATION_PM_1_24H_AVERAGE: { unit: 'ug/m3', deviceClass: 'pm1', stateClass: 'measurement' },
  MASS_CONCENTRATION_PM_2_5_24H_AVERAGE: { unit: 'ug/m3', deviceClass: 'pm25', stateClass: 'measurement' },
  MASS_CONCENTRATION_PM_10_24H_AVERAGE: { unit: 'ug/m3', deviceClass: 'pm10', stateClass: 'measurement' },
  FILLING_LEVEL: { unit: '%' },
  LEVEL: { unit: '%' },
  LEVEL_2: { unit: '%' },
};

export const homematicBinaryDeviceClassByType: Record<string, string | undefined> = {
  IPShutterContact: 'opening',
  IPShutterContactSabotage: 'opening',
  MaxShutterContact: 'opening',
  Motion: 'motion',
  MotionV2: 'motion',
  PresenceIP: 'motion',
  ShutterContact: 'opening',
  Smoke: 'smoke',
  SmokeV2: 'smoke',
  IPContact: 'opening',
  MotionIP: 'motion',
  MotionIPV2: 'motion',
  MotionIPContactSabotage: 'motion',
  IPRemoteMotionV2: 'motion',
};

export const homematicStateCasts: Record<string, Record<string, string | null>> = {
  IPGarage: { '0': 'closed', '1': 'open', '2': 'ventilation', '3': null },
  RotaryHandleSensor: { '0': 'closed', '1': 'tilted', '2': 'open' },
  RotaryHandleSensorIP: { '0': 'closed', '1': 'tilted', '2': 'open' },
  WaterSensor: { '0': 'dry', '1': 'wet', '2': 'water' },
  CO2Sensor: { '0': 'normal', '1': 'added', '2': 'strong' },
  IPSmoke: { '0': 'off', '1': 'primary', '2': 'intrusion', '3': 'secondary' },
  RFSiren: { '0': 'disarmed', '1': 'extsens_armed', '2': 'allsens_armed', '3': 'alarm_blocked' },
  IPLockDLD: { '0': null, '1': 'locked', '2': 'unlocked' },
};

export const homematicClimateTemperatureDatapoints = ['SET_TEMPERATURE', 'SETPOINT', 'SET_POINT_TEMPERATURE'];
export const homematicClimateCurrentTemperatureDatapoints = ['ACTUAL_TEMPERATURE', 'TEMPERATURE'];
export const homematicClimateHumidityDatapoints = ['ACTUAL_HUMIDITY', 'HUMIDITY'];
export const homematicControlModeDatapoints = ['CONTROL_MODE', 'SET_POINT_MODE'];

export const homematicBooleanControlDatapoints = ['STATE', 'INHIBIT'];
export const homematicLevelControlDatapoints = ['LEVEL'];
export const homematicBatteryDatapoints = ['LOWBAT', 'LOW_BAT'];
