def onStart(self):
        # Set heartbeat
        self.skipbeats=int(Parameters["Mode5"])/30
        self.beats=self.skipbeats
        Domoticz.Heartbeat(30)

        # Set debugging
        if Parameters["Mode6"]=="True": 
            Domoticz.Debugging(2)
            Domoticz.Debug("Debugging mode activated")

        # Read Cube for intialization of devices
        Domoticz.Debug("Reading e-Q3 MAX! devices from Cube...")
        try:
            cube = MaxCube(MaxCubeConnection(Parameters["Address"], int(Parameters["Port"])))
        except:
            Domoticz.Error("Error connecting to Cube. Other running MAX! programs may block the communication!")
            return
        
        # Check which rooms have a wall mounterd thermostat
        max_room = 0
        for room in cube.rooms:
            if room.id > max_room: max_room = room.id
        Domoticz.Debug("Number of rooms found: " + str((len(cube.rooms))) + " (highest number: " + str(max_room) + ")")
        self.RoomHasThermostat=[False] * (max_room+1)
        for EQ3device in cube.devices:
            if cube.is_wallthermostat(EQ3device):
                self.RoomHasThermostat[EQ3device.room_id] = True
                Domoticz.Debug("Room " + str(EQ3device.room_id) + " (" + cube.room_by_id(EQ3device.room_id).name + ") has a thermostat")

        # Create or delete devices if necessary
        for EQ3device in cube.devices:
            if cube.is_thermostat(EQ3device):
                self.CheckDevice(EQ3device.name, EQ3device.rf_address, "Valve")
                if not self.RoomHasThermostat[EQ3device.room_id]:
                    self.CheckDevice(EQ3device.name, EQ3device.rf_address, "Thermostat")
                    self.CheckDevice(EQ3device.name, EQ3device.rf_address, "Temperature")
                    self.CheckDevice(EQ3device.name, EQ3device.rf_address, "Mode")
            elif cube.is_wallthermostat(EQ3device):
                self.CheckDevice(EQ3device.name, EQ3device.rf_address, "Thermostat")
                self.CheckDevice(EQ3device.name, EQ3device.rf_address, "Temperature")
                self.CheckDevice(EQ3device.name, EQ3device.rf_address, "Mode")
            elif cube.is_windowshutter(EQ3device):
                self.CheckDevice(EQ3device.name, EQ3device.rf_address, "Contact")

        # Create or delete heat demand switch if necessary
        if Parameters["Mode3"] == "True" and 255 not in Devices:
            Domoticz.Device(Name="Heat demand", Unit=255, TypeName="Switch", Image=9, Used=1).Create()
            if 255 not in Devices:
                Domoticz.Error("Heat demand switch could not be created. Is 'Accept new Hardware Devices' enabled under Settings?")
            else:
                Domoticz.Log("Created device '" + Parameters["Name"] + " - Heat demand'") 
                Devices[255].Update(nValue=0, sValue="Off")
        elif Parameters["Mode3"] == "False" and 255 in Devices:
            Devices[255].Delete()
            Domoticz.Log("Deleted heat demand switch")
    def onHeartbeat(self):
        #Cancel the rest of this function if this heartbeat needs to be skipped
        if self.beats < self.skipbeats:
            Domoticz.Debug("Skipping heartbeat: " + str(self.beats))
            self.beats += 1
            return
        self.beats=1

        self.HeatDemand = 0

        # Read data from Cube
        Domoticz.Debug("Reading e-Q3 MAX! devices from Cube...")
        try:
            cube = MaxCube(MaxCubeConnection(Parameters["Address"], int(Parameters["Port"])))
        except:
            Domoticz.Error("Error connecting to Cube. Other running MAX! programs may block the communication!")
            return

        # Update devices in Domoticz
        for EQ3device in cube.devices:
            Domoticz.Debug("Checking device '" + EQ3device.name + "' in room " + str(EQ3device.room_id))
            if cube.is_thermostat(EQ3device):
                # Check if valve requires heat
                if EQ3device.valve_position > int(Parameters["Mode4"]): self.HeatDemand += 1
                # Update Domoticz devices for radiator valves
                self.UpdateDevice(EQ3device, "Valve")
                if not self.RoomHasThermostat[EQ3device.room_id]:
                    self.UpdateDevice(EQ3device, "Thermostat")
                    self.UpdateDevice(EQ3device, "Temperature")
                    self.UpdateDevice(EQ3device, "Mode")

            elif cube.is_wallthermostat(EQ3device):
                # Update Domoticz devices for wall thermostats
                self.UpdateDevice(EQ3device, "Thermostat")
                self.UpdateDevice(EQ3device, "Temperature")
                self.UpdateDevice(EQ3device, "Mode")

            elif cube.is_windowshutter(EQ3device):
                # Look up & update Domoticz device for contact switches
                self.UpdateDevice(EQ3device, "Contact")

        # Update heat demand switch if necessary
        Domoticz.Debug(str(self.HeatDemand) + " valves require heat")
        if self.HeatDemand > 0 and Parameters["Mode3"] == "True" and 255 in Devices and Devices[255].sValue == "Off":
            Devices[255].Update(nValue=1, sValue="On")
            Domoticz.Log("Heat demand switch turned on")
        elif self.HeatDemand == 0 and Parameters["Mode3"] == "True" and 255 in Devices and Devices[255].sValue == "On":
            Devices[255].Update(nValue=0, sValue="Off")
            Domoticz.Log("Heat demand switch turned off")
class TestMaxCubeExtended(unittest.TestCase):
    """ Test the Max! Cube. """
    def setUp(self):
        self.cube = MaxCube(MaxCubeConnectionMock(INIT_RESPONSE_2))

    def test_init(self):
        self.assertEqual('015d2a', self.cube.rf_address)
        self.assertEqual('Cube', self.cube.name)
        self.assertEqual('01.13', self.cube.firmware_version)
        self.assertEqual(3, len(self.cube.devices))

    def test_parse_response(self):
        self.cube.parse_response(INIT_RESPONSE_2)
        self.assertEqual('015d2a', self.cube.rf_address)
        self.assertEqual('Cube', self.cube.name)
        self.assertEqual('01.13', self.cube.firmware_version)
        self.assertEqual(3, len(self.cube.devices))

    def test_parse_c_message_thermostat(self):
        device = self.cube.devices[0]
        self.assertEqual(21.5, device.comfort_temperature)
        self.assertEqual(16.5, device.eco_temperature)
        self.assertEqual(4.5, device.min_temperature)
        self.assertEqual(30.5, device.max_temperature)
        device = self.cube.devices[1]
        self.assertEqual(21.5, device.comfort_temperature)
        self.assertEqual(16.5, device.eco_temperature)
        self.assertEqual(4.5, device.min_temperature)
        self.assertEqual(30.5, device.max_temperature)
        device = self.cube.devices[2]
        self.assertEqual(1, device.initialized)

    def test_parse_h_message(self):
        self.cube.parse_h_message(
            'H:KEQ0566338,0b6444,0113,00000000,335b04d2,33,32,0f0c1d,101c,03,0000'
        )
        self.assertEqual('0b6444', self.cube.rf_address)
        self.assertEqual('01.13', self.cube.firmware_version)

    def test_parse_m_message(self):
        self.cube.parse_m_message(
            'M:00,01,VgIEAQdLaXRjaGVuBrxTAgZMaXZpbmcGvFoDCFNsZWVwaW5nCKuCBARXb3JrBrxcBAEGvF'
            'NLRVEwMzM2MTA4B0tpdGNoZW4BAQa8WktFUTAzMzYxMDAGTGl2aW5nAgEIq4JLRVEwMzM1NjYyCFNs'
            'ZWVwaW5nAwEGvFxLRVEwMzM2MTA0BFdvcmsEAQ==')
        self.assertEqual('0E2EBA', self.cube.devices[0].rf_address)
        self.assertEqual('Thermostat', self.cube.devices[0].name)
        self.assertEqual(MAX_THERMOSTAT, self.cube.devices[0].type)
        self.assertEqual('KEQ1086437', self.cube.devices[0].serial)
        self.assertEqual(1, self.cube.devices[0].room_id)

        self.assertEqual('0A0881', self.cube.devices[1].rf_address)
        self.assertEqual('Wandthermostat', self.cube.devices[1].name)
        self.assertEqual(MAX_WALL_THERMOSTAT, self.cube.devices[1].type)
        self.assertEqual('KEQ0655743', self.cube.devices[1].serial)
        self.assertEqual(2, self.cube.devices[1].room_id)

        self.assertEqual('0CA2B2', self.cube.devices[2].rf_address)
        self.assertEqual('Fensterkontakt', self.cube.devices[2].name)
        self.assertEqual(MAX_WINDOW_SHUTTER, self.cube.devices[2].type)
        self.assertEqual('KEQ0839778', self.cube.devices[2].serial)
        self.assertEqual(1, self.cube.devices[3].room_id)

        self.assertEqual('Badezimmer', self.cube.rooms[0].name)
        self.assertEqual(1, self.cube.rooms[0].id)

        self.assertEqual('Wohnzimmer', self.cube.rooms[1].name)
        self.assertEqual(2, self.cube.rooms[1].id)

    def test_parse_l_message(self):
        device = self.cube.devices[0]
        self.assertEqual(MAX_DEVICE_MODE_AUTOMATIC, device.mode)
        self.assertEqual(None, device.actual_temperature)
        self.assertEqual(8.0, device.target_temperature)

        device = self.cube.devices[1]
        self.assertEqual(MAX_DEVICE_MODE_AUTOMATIC, device.mode)
        self.assertEqual(22.9, device.actual_temperature)
        self.assertEqual(8.0, device.target_temperature)

        device = self.cube.devices[2]
        self.assertEqual(False, device.is_open)

    def test_resolve_device_mode(self):
        self.assertEqual(MAX_DEVICE_MODE_AUTOMATIC,
                         self.cube.resolve_device_mode(24))
        self.assertEqual(MAX_DEVICE_MODE_MANUAL,
                         self.cube.resolve_device_mode(25))

    def test_is_thermostat(self):
        device = MaxDevice()
        device.type = MAX_CUBE
        self.assertEqual(False, self.cube.is_thermostat(device))
        device.type = MAX_THERMOSTAT
        self.assertEqual(True, self.cube.is_thermostat(device))
        device.type = MAX_THERMOSTAT_PLUS
        self.assertEqual(True, self.cube.is_thermostat(device))
        device.type = MAX_WALL_THERMOSTAT
        self.assertEqual(False, self.cube.is_thermostat(device))
        device.type = MAX_WINDOW_SHUTTER
        self.assertEqual(False, self.cube.is_thermostat(device))

    def test_is_wall_thermostat(self):
        device = MaxDevice()
        device.type = MAX_CUBE
        self.assertEqual(False, self.cube.is_wallthermostat(device))
        device.type = MAX_THERMOSTAT
        self.assertEqual(False, self.cube.is_wallthermostat(device))
        device.type = MAX_THERMOSTAT_PLUS
        self.assertEqual(False, self.cube.is_wallthermostat(device))
        device.type = MAX_WALL_THERMOSTAT
        self.assertEqual(True, self.cube.is_wallthermostat(device))
        device.type = MAX_WINDOW_SHUTTER
        self.assertEqual(False, self.cube.is_wallthermostat(device))

    def test_is_window_shutter(self):
        device = MaxDevice()
        device.type = MAX_CUBE
        self.assertEqual(False, self.cube.is_windowshutter(device))
        device.type = MAX_THERMOSTAT
        self.assertEqual(False, self.cube.is_windowshutter(device))
        device.type = MAX_THERMOSTAT_PLUS
        self.assertEqual(False, self.cube.is_windowshutter(device))
        device.type = MAX_WALL_THERMOSTAT
        self.assertEqual(False, self.cube.is_windowshutter(device))
        device.type = MAX_WINDOW_SHUTTER
        self.assertEqual(True, self.cube.is_windowshutter(device))

    def test_set_target_temperature_thermostat(self):
        self.cube.set_target_temperature(self.cube.devices[0], 24.5)
        self.assertEqual('s:AARAAAAADi66ATE=\r\n',
                         self.cube.connection.command)
        self.assertEqual(24.5, self.cube.devices[0].target_temperature)

    def test_set_target_temperature_windowshutter(self):
        self.cube.set_target_temperature(self.cube.devices[2], 24.5)
        self.assertEqual(None, self.cube.connection.command)

    def test_set_mode_thermostat(self):
        self.cube.set_mode(self.cube.devices[0], MAX_DEVICE_MODE_MANUAL)
        self.assertEqual('s:AARAAAAADi66AVA=\r\n',
                         self.cube.connection.command)
        self.assertEqual(MAX_DEVICE_MODE_MANUAL, self.cube.devices[0].mode)

    def test_set_mode_windowshutter(self):
        self.cube.set_mode(self.cube.devices[2], 24.5)
        self.assertEqual(None, self.cube.connection.command)

    def test_set_temperature_mode_thermostat(self):
        self.cube.set_temperature_mode(self.cube.devices[2], 24.5,
                                       MAX_DEVICE_MODE_BOOST)
        self.assertEqual(None, self.cube.connection.command)

    def test_get_devices(self):
        devices = self.cube.get_devices()
        self.assertEqual(3, len(devices))

    def test_device_by_rf(self):
        device = self.cube.device_by_rf('0CA2B2')

        self.assertEqual('0CA2B2', device.rf_address)
        self.assertEqual('Fensterkontakt', device.name)
        self.assertEqual(MAX_WINDOW_SHUTTER, device.type)
        self.assertEqual('KEQ0839778', device.serial)
        self.assertEqual(1, device.room_id)

    def test_device_by_rf_negative(self):
        device = self.cube.device_by_rf('DEADBEEF')

        self.assertEqual(None, device)

    def test_devices_by_room(self):
        room = MaxRoom()
        room.id = 1
        devices = self.cube.devices_by_room(room)
        self.assertEqual(2, len(devices))

    def test_devices_by_room_negative(self):
        room = MaxRoom()
        room.id = 3
        devices = self.cube.devices_by_room(room)
        self.assertEqual(0, len(devices))

    def test_get_rooms(self):
        rooms = self.cube.get_rooms()

        self.assertEqual('Badezimmer', rooms[0].name)
        self.assertEqual(1, rooms[0].id)

        self.assertEqual('Wohnzimmer', rooms[1].name)
        self.assertEqual(2, rooms[1].id)

    def test_room_by_id(self):
        room = self.cube.room_by_id(1)

        self.assertEqual('Badezimmer', room.name)
        self.assertEqual(1, room.id)

    def test_room_by_id_negative(self):
        room = self.cube.room_by_id(3)

        self.assertEqual(None, room)