<commit_before>/**
 * Copyright (c) 2014-2016 by the respective copyright holders.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package org.openhab.io.homekit.internal.accessories;

import java.math.BigDecimal;
import java.util.concurrent.CompletableFuture;

import org.eclipse.smarthome.core.items.GenericItem;
import org.eclipse.smarthome.core.items.GroupItem;
import org.eclipse.smarthome.core.items.Item;
import org.eclipse.smarthome.core.items.ItemRegistry;
import org.eclipse.smarthome.core.library.items.NumberItem;
import org.eclipse.smarthome.core.library.items.StringItem;
import org.eclipse.smarthome.core.library.types.DecimalType;
import org.eclipse.smarthome.core.library.types.StringType;
import org.openhab.io.homekit.internal.HomekitAccessoryUpdater;
import org.openhab.io.homekit.internal.HomekitSettings;
import org.openhab.io.homekit.internal.HomekitTaggedItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
import com.beowulfe.hap.accessories.properties.ThermostatMode;
import com.beowulfe.hap.accessories.thermostat.BasicThermostat;

/**
 * Implements Thermostat as a GroupedAccessory made up of multiple items:
 * <ul>
 * <li>Cooling Threshold: Decimal type</li>
 * <li>Heating Threshold: Decimal type</li>
 * <li>Auto Threshold: Decimal type</li>
 * <li>Current Temperature: Decimal type</li>
 * <li>Heating/Cooling Mode: String type (see HomekitSettings.thermostat*Mode)</li>
 * </ul>
 *
 * @author <NAME>
 */
class HomekitThermostatImpl extends AbstractTemperatureHomekitAccessoryImpl<GroupItem>
        implements BasicThermostat, GroupedAccessory {

    private final String groupName;
    private final HomekitSettings settings;
    private String currentTemperatureItemName;
    private String heatingCoolingModeItemName;
    private String targetTemperatureItemName;

    private Logger logger = LoggerFactory.getLogger(HomekitThermostatImpl.class);

    public HomekitThermostatImpl(HomekitTaggedItem taggedItem, ItemRegistry itemRegistry,
            HomekitAccessoryUpdater updater, HomekitSettings settings) {
        super(taggedItem, itemRegistry, updater, settings, GroupItem.class);
        this.groupName = taggedItem.getItem().getName();
        this.settings = settings;
    }

    @Override
    public String getGroupName() {
        return groupName;
    }

    @Override
    public void addCharacteristic(HomekitTaggedItem item) {
        switch (item.getCharacteristicType()) {
            case CURRENT_TEMPERATURE:
                currentTemperatureItemName = item.getItem().getName();
                break;

            case HEATING_COOLING_MODE:
                heatingCoolingModeItemName = item.getItem().getName();
                break;

            case TARGET_TEMPERATURE:
                targetTemperatureItemName = item.getItem().getName();
                break;

            default:
                logger.error("Unrecognized thermostat characteristic: " + item.getCharacteristicType().name());
                break;

        }
    }

    @Override
    public boolean isComplete() {
        return targetTemperatureItemName != null && currentTemperatureItemName != null
                && heatingCoolingModeItemName != null;
    }

    @Override
    public CompletableFuture<ThermostatMode> getCurrentMode() {
        Item item = getItemRegistry().get(heatingCoolingModeItemName);
        StringType state = (StringType) item.getStateAs(StringType.class);
        ThermostatMode mode;
        if (state != null) {
            String stringValue = state.toString();

            if (stringValue.equals(settings.getThermostatCoolMode())) {
                mode = ThermostatMode.COOL;
            } else if (stringValue.equals(settings.getThermostatHeatMode())) {
                mode = ThermostatMode.HEAT;
            } else if (stringValue.equals(settings.getThermostatAutoMode())) {
                mode = ThermostatMode.AUTO;
            } else if (stringValue.equals(settings.getThermostatOffMode())) {
                mode = ThermostatMode.OFF;
            } else {
                logger.error("Unrecognized heating cooling target mode: " + stringValue<commit_msg>Simplify how we get a string value from an item for thermostat modes.  Fixes #1275 (#1276)<commit_after>/**
 * Copyright (c) 2014-2016 by the respective copyright holders.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package org.openhab.io.homekit.internal.accessories;

import java.math.BigDecimal;
import java.util.concurrent.CompletableFuture;

import org.eclipse.smarthome.core.items.GenericItem;
import org.eclipse.smarthome.core.items.GroupItem;
import org.eclipse.smarthome.core.items.Item;
import org.eclipse.smarthome.core.items.ItemRegistry;
import org.eclipse.smarthome.core.library.items.NumberItem;
import org.eclipse.smarthome.core.library.items.StringItem;
import org.eclipse.smarthome.core.library.types.DecimalType;
import org.eclipse.smarthome.core.library.types.StringType;
import org.eclipse.smarthome.core.types.State;
import org.openhab.io.homekit.internal.HomekitAccessoryUpdater;
import org.openhab.io.homekit.internal.HomekitSettings;
import org.openhab.io.homekit.internal.HomekitTaggedItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
import com.beowulfe.hap.accessories.properties.ThermostatMode;
import com.beowulfe.hap.accessories.thermostat.BasicThermostat;

/**
 * Implements Thermostat as a GroupedAccessory made up of multiple items:
 * <ul>
 * <li>Cooling Threshold: Decimal type</li>
 * <li>Heating Threshold: Decimal type</li>
 * <li>Auto Threshold: Decimal type</li>
 * <li>Current Temperature: Decimal type</li>
 * <li>Heating/Cooling Mode: String type (see HomekitSettings.thermostat*Mode)</li>
 * </ul>
 *
 * @author <NAME>
 */
class HomekitThermostatImpl extends AbstractTemperatureHomekitAccessoryImpl<GroupItem>
        implements BasicThermostat, GroupedAccessory {

    private final String groupName;
    private final HomekitSettings settings;
    private String currentTemperatureItemName;
    private String heatingCoolingModeItemName;
    private String targetTemperatureItemName;

    private Logger logger = LoggerFactory.getLogger(HomekitThermostatImpl.class);

    public HomekitThermostatImpl(HomekitTaggedItem taggedItem, ItemRegistry itemRegistry,
            HomekitAccessoryUpdater updater, HomekitSettings settings) {
        super(taggedItem, itemRegistry, updater, settings, GroupItem.class);
        this.groupName = taggedItem.getItem().getName();
        this.settings = settings;
    }

    @Override
    public String getGroupName() {
        return groupName;
    }

    @Override
    public void addCharacteristic(HomekitTaggedItem item) {
        switch (item.getCharacteristicType()) {
            case CURRENT_TEMPERATURE:
                currentTemperatureItemName = item.getItem().getName();
                break;

            case HEATING_COOLING_MODE:
                heatingCoolingModeItemName = item.getItem().getName();
                break;

            case TARGET_TEMPERATURE:
                targetTemperatureItemName = item.getItem().getName();
                break;

            default:
                logger.error("Unrecognized thermostat characteristic: " + item.getCharacteristicType().name());
                break;

        }
    }

    @Override
    public boolean isComplete() {
        return targetTemperatureItemName != null && currentTemperatureItemName != null
                && heatingCoolingModeItemName != null;
    }

    @Override
    public CompletableFuture<ThermostatMode> getCurrentMode() {
        Item item = getItemRegistry().get(heatingCoolingModeItemName);
        State state = item.getState();
        ThermostatMode mode;
        if (state != null) {
            String stringValue = state.toString();

            if (stringValue.equals(settings.getThermostatCoolMode())) {
                mode = ThermostatMode.COOL;
            } else if (stringValue.equals(settings.getThermostatHeatMode())) {
                mode = ThermostatMode.HEAT;
            } else if (stringValue.equals(settings.getThermostatAutoMode())) {
                mode = ThermostatMode.AUTO;
            } else if (stringValue.equals(settings.getThermostatOffMode())) {
                mode = ThermostatMode.OFF;
            } else {
                logger.error("Unrecognized heating cooling target mode: " + stringValue
{'fraction_non_alphanumeric': 0.09225589225589226, 'fraction_numerical': 0.00404040404040404, 'mean_word_length': 3.3278290432248663, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2411247', 'n_tokens_mistral': 2582, 'n_tokens_neox': 2442, 'n_words': 513}