<commit_before>package com.lisantom.our.housemate;

import com.intuso.housemate.api.HousemateException;
import com.intuso.housemate.api.object.value.ValueListener;
import com.intuso.housemate.object.real.RealProperty;
import com.intuso.housemate.object.real.RealResources;
import com.intuso.housemate.object.real.impl.device.OnOffDevice;
import com.intuso.housemate.object.real.impl.type.IntegerType;
import com.rfxcom.rfxtrx.util.HomeEasy;

import java.io.IOException;

/**
 * Housemate device that controls a USB relay
 * @author tclabon
 *
 */
public class HomeEasyAppliance extends OnOffDevice implements ValueListener<RealProperty<?>> {

    private HomeEasy homeEasy;

	/**
	 * The unit to control
	 */
	private HomeEasy.Appliance appliance;

    /**
     * The number of the relay this device "controls"
     */
    public final RealProperty<Integer> houseId = IntegerType.createProperty(getResources(), "house-id", "House ID", "HomeEasy house ID (in decimal)", 0);

    /**
     * The number of the relay this device "controls"
     */
    public final RealProperty<Integer> unitId = IntegerType.createProperty(getResources(), "unit-id", "Unit ID", "HomeEasy unit ID", 1);

	/**
	 * Create a new USB relay device
	 * @param name the name of the device
	 * @throws HousemateException
	 */
	public HomeEasyAppliance(RealResources resources, HomeEasy homeEasy, String id, String name, String description) throws HousemateException {
		super(resources, id, name, description);
        getProperties().add(this.houseId);
        getProperties().add(this.unitId);
        this.homeEasy = homeEasy;
        this.houseId.addObjectListener(this);
        this.unitId.addObjectListener(this);
	}
	
	/**
	 * Start the USB relay
	 * @param id
     * @param unitcode
	 * @throws HousemateException 
	 */
	private void createHed(int id, int unitcode) {
		
        appliance = homeEasy.createAppliance(id, (byte) unitcode);
        appliance.addListener(new HomeEasy.KnownApplianceStateListener() {
            @Override
            public void nowOff(HomeEasy.Appliance a) {
                setOff();
            }

            @Override
            public void nowOn(HomeEasy.Appliance a) {
                setOn();
            }
        });
	}

    @Override
    public void valueChanging(RealProperty<?> value) {
        // do nothing
    }

    /**
	 * Check if all the current property values are valid and if so, start the USB relay client
	 * @throws HousemateException
	 */
    @Override
    public void valueChanged(RealProperty<?> property) {
        int id = this.houseId.getTypedValue();
        int unitcode = this.unitId.getTypedValue();
		
        // check the port value is a positive number
        if(id < 0 || id > 0x03FFFFFF)
            getErrorValue().setTypedValue("House id must be between 0 and " + 0x03FFFFFF);
			
		// check the relay value is a number between 1 and 8
		if(unitcode < 1 || unitcode > 16)
            getErrorValue().setTypedValue("Unitcode must be between 1 and 16 (inclusive)");

        getErrorValue().setTypedValue(null);
        createHed(id, unitcode);
	}<commit_msg>Fixed NPE in Home Easy plugin<commit_after>package com.lisantom.our.housemate;

import com.intuso.housemate.api.HousemateException;
import com.intuso.housemate.api.object.value.ValueListener;
import com.intuso.housemate.object.real.RealProperty;
import com.intuso.housemate.object.real.RealResources;
import com.intuso.housemate.object.real.impl.device.OnOffDevice;
import com.intuso.housemate.object.real.impl.type.IntegerType;
import com.rfxcom.rfxtrx.util.HomeEasy;

import java.io.IOException;

/**
 * Housemate device that controls a USB relay
 * @author tclabon
 *
 */
public class HomeEasyAppliance extends OnOffDevice implements ValueListener<RealProperty<?>> {

    private HomeEasy homeEasy;

	/**
	 * The unit to control
	 */
	private HomeEasy.Appliance appliance;

    /**
     * The number of the relay this device "controls"
     */
    public final RealProperty<Integer> houseId = IntegerType.createProperty(getResources(), "house-id", "House ID", "HomeEasy house ID (in decimal)", 0);

    /**
     * The number of the relay this device "controls"
     */
    public final RealProperty<Integer> unitId = IntegerType.createProperty(getResources(), "unit-id", "Unit ID", "HomeEasy unit ID", 1);

	/**
	 * Create a new USB relay device
	 * @param name the name of the device
	 * @throws HousemateException
	 */
	public HomeEasyAppliance(RealResources resources, HomeEasy homeEasy, String id, String name, String description) throws HousemateException {
		super(resources, id, name, description);
        getProperties().add(this.houseId);
        getProperties().add(this.unitId);
        this.homeEasy = homeEasy;
        this.houseId.addObjectListener(this);
        this.unitId.addObjectListener(this);
	}
	
	/**
	 * Start the USB relay
	 * @param id
     * @param unitcode
	 * @throws HousemateException 
	 */
	private void createHed(int id, int unitcode) {
		
        appliance = homeEasy.createAppliance(id, (byte) unitcode);
        appliance.addListener(new HomeEasy.KnownApplianceStateListener() {
            @Override
            public void nowOff(HomeEasy.Appliance a) {
                setOff();
            }

            @Override
            public void nowOn(HomeEasy.Appliance a) {
                setOn();
            }
        });
	}

    @Override
    public void valueChanging(RealProperty<?> value) {
        // do nothing
    }

    /**
	 * Check if all the current property values are valid and if so, start the USB relay client
	 * @throws HousemateException
	 */
    @Override
    public void valueChanged(RealProperty<?> property) {
        Integer houseId = this.houseId.getTypedValue();
        if(houseId == null) {
            getErrorValue().setTypedValue(this.houseId.getName() + " has not been set");
            return;
        }

        Integer unitId = this.unitId.getTypedValue();
        if(unitId == null) {
            getErrorValue().setTypedValue(this.unitId.getName() + " has not been set");
            return;
        }
		
        // check the port value is a positive number
        if(houseId < 0 || houseId > 0x03FFFFFF) {
            getErrorValue().setTypedValue("House id must be between 0 and " + 0x03FFFFFF);
            return;
        }
			
		// check the relay value is a number between 1 and 8
		if(unitId < 1 || unitId > 16) {
            getErrorValue().setTypedValue("Unitcode must be between 1 and 16 (inclusive)");
            return;
        }

        getErrorValue().setTypedValue(null);
        createHed(houseId, unitId);
	}
{'fraction_non_alphanumeric': 0.10484967517751927, 'fraction_numerical': 0.005438888049554313, 'mean_word_length': 2.8510762070971496, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '19722', 'n_tokens_mistral': 2008, 'n_tokens_neox': 1855, 'n_words': 505}