<commit_before>package main

import (
	"encoding/json"
	"errors"
	"fmt"
	"strconv"
	"unicode"

	"github.com/jason0x43/go-alfred"
)

type TempCommand struct{}

func (t TempCommand) Keyword() string {
	return "temp"
}

func (t TempCommand) IsEnabled() bool {
	return isAuthorized() && config.NestId != ""
}

func (t TempCommand) MenuItem() alfred.Item {
	return alfred.Item{
		Title:        t.Keyword(),
		Autocomplete: t.Keyword() + " ",
		SubtitleAll:  "View and adjust your default Nest’s temperature",
		Valid:        alfred.Invalid,
	}
}

func (t TempCommand) Items(prefix, query string) (items []alfred.Item, err error) {
	if err = checkRefresh(); err != nil {
		return
	}

	thermostat, ok := cache.AllData.Devices.Thermostats[config.NestId]
	if !ok {
		return items, errors.New("Couldn’t access your default Nest")
	}

	temp := thermostat.AmbientTemperature(config.Scale)

	if query != "" {
		if newVal, err := strconv.ParseFloat(query, 64); err == nil {
			if newVal != temp.Value() {
				newTemp := NewTemp(newVal, config.Scale)

				var changeType string
				if thermostat.HvacMode == ModeRange {
					if newTemp.Value() > temp.Value() {
						changeType = "Heat"
					} else {
						changeType = "Cool"
					}
				} else {
					mode := string(thermostat.HvacMode)
					changeType = string(unicode.ToUpper(rune(mode[0]))) + mode[1:]
				}

				data := tempMessage{
					DeviceId:   thermostat.DeviceId,
					TargetTemp: newVal,
					Scale:      config.Scale,
				}
				dataString, _ := json.Marshal(data)

				items = append(items, alfred.Item{
					Title:       fmt.Sprintf("%s to %s", changeType, newTemp),
					SubtitleAll: fmt.Sprintf("Current temperature is %s", temp),
					Arg:         "temp " + string(dataString),
				})
				return items, nil
			}
		}
	}

	var subtitle string

	if thermostat.HvacMode == ModeRange {
		targetHigh := thermostat.TargetTemperatureHigh(config.Scale)
		targetLow := thermostat.TargetTemperatureLow(config.Scale)
		subtitle = "Target is "
		subtitle += fmt.Sprintf("%s to %s", targetLow, targetHigh)
	} else {
		if thermostat.HvacMode == ModeHeat {
			subtitle = "Heating to "
		} else {
			subtitle = "Cooling to "
		}
		subtitle += thermostat.TargetTemperature(config.Scale).String()
	}

	items = append(items, alfred.Item{
		Title:       subtitle,
		SubtitleAll: fmt.Sprintf("Current temperature is %s", temp),
		Valid:       alfred.Invalid,
	})

	return items, nil
}

func (t TempCommand) Do(query string) (out string, err error) {
	var msg tempMessage
	if err = json.Unmarshal([]byte(query), &msg); err != nil {
		return
	}

	thermostat, ok := cache.AllData.Devices.Thermostats[msg.DeviceId]
	if !ok {
		return out, errors.New("Unknown thermostat '" + msg.DeviceId + "'")
	}

	session := OpenSession(config.AccessToken)
	var newTemp Temperature

	if thermostat.HvacMode == ModeRange {
		if msg.TargetTemp < thermostat.AmbientTemperature(config.Scale).Value() {
			newTemp, err = session.SetTargetTemp(msg.DeviceId, msg.Temperature(), TypeLow)
		} else {
			newTemp, err = session.SetTargetTemp(msg.DeviceId, msg.Temperature(), TypeHigh)
		}
	} else {
		newTemp, err = session.SetTargetTemp(msg.DeviceId, msg.Temperature(), "")
	}

	if err != nil {
		return
	}

	scheduleRefresh()

	return fmt.Sprintf("Set temperature to %s", newTemp), err
}

type tempMessage struct {
	DeviceId   string
	TargetTemp float64
	Scale      TempScale<commit_msg>Properly set range mode temperature<commit_after>package main

import (
	"encoding/json"
	"errors"
	"fmt"
	"strconv"
	"unicode"

	"github.com/jason0x43/go-alfred"
)

type TempCommand struct{}

func (t TempCommand) Keyword() string {
	return "temp"
}

func (t TempCommand) IsEnabled() bool {
	return isAuthorized() && config.NestId != ""
}

func (t TempCommand) MenuItem() alfred.Item {
	return alfred.Item{
		Title:        t.Keyword(),
		Autocomplete: t.Keyword() + " ",
		SubtitleAll:  "View and adjust your default Nest’s temperature",
		Valid:        alfred.Invalid,
	}
}

func (t TempCommand) Items(prefix, query string) (items []alfred.Item, err error) {
	if err = checkRefresh(); err != nil {
		return
	}

	thermostat, ok := cache.AllData.Devices.Thermostats[config.NestId]
	if !ok {
		return items, errors.New("Couldn’t access your default Nest")
	}

	temp := thermostat.AmbientTemperature(config.Scale)

	if query != "" {
		if newVal, err := strconv.ParseFloat(query, 64); err == nil {
			if newVal != temp.Value() {
				newTemp := NewTemp(newVal, config.Scale)

				var changeType string
				if thermostat.HvacMode == ModeRange {
					if newTemp.Value() > temp.Value() {
						changeType = "Heat"
					} else {
						changeType = "Cool"
					}
				} else {
					mode := string(thermostat.HvacMode)
					changeType = string(unicode.ToUpper(rune(mode[0]))) + mode[1:]
				}

				data := tempMessage{
					DeviceId:   thermostat.DeviceId,
					TargetTemp: newVal,
					Scale:      config.Scale,
				}
				dataString, _ := json.Marshal(data)

				items = append(items, alfred.Item{
					Title:       fmt.Sprintf("%s to %s", changeType, newTemp),
					SubtitleAll: fmt.Sprintf("Current temperature is %s", temp),
					Arg:         "temp " + string(dataString),
				})
				return items, nil
			}
		}
	}

	var subtitle string

	if thermostat.HvacMode == ModeRange {
		targetHigh := thermostat.TargetTemperatureHigh(config.Scale)
		targetLow := thermostat.TargetTemperatureLow(config.Scale)
		subtitle = "Target is "
		subtitle += fmt.Sprintf("%s to %s", targetLow, targetHigh)
	} else {
		if thermostat.HvacMode == ModeHeat {
			subtitle = "Heating to "
		} else {
			subtitle = "Cooling to "
		}
		subtitle += thermostat.TargetTemperature(config.Scale).String()
	}

	items = append(items, alfred.Item{
		Title:       subtitle,
		SubtitleAll: fmt.Sprintf("Current temperature is %s", temp),
		Valid:       alfred.Invalid,
	})

	return items, nil
}

func (t TempCommand) Do(query string) (out string, err error) {
	var msg tempMessage
	if err = json.Unmarshal([]byte(query), &msg); err != nil {
		return
	}

	thermostat, ok := cache.AllData.Devices.Thermostats[msg.DeviceId]
	if !ok {
		return out, errors.New("Unknown thermostat '" + msg.DeviceId + "'")
	}

	session := OpenSession(config.AccessToken)
	var newTemp Temperature

	if thermostat.HvacMode == ModeRange {
		if msg.TargetTemp < thermostat.AmbientTemperature(config.Scale).Value() {
			// If the target temp is less than the ambient temp, we've lowered the high temp
			newTemp, err = session.SetTargetTemp(msg.DeviceId, msg.Temperature(), TypeHigh)
		} else if msg.TargetTemp > thermostat.AmbientTemperature(config.Scale).Value() {
			// If the target temp is greater than the ambient temp, we've raised the low temp
			newTemp, err = session.SetTargetTemp(msg.DeviceId, msg.Temperature(), TypeLow)
		}
	} else {
		newTemp, err = session.SetTargetTemp(msg.DeviceId, msg.Temperature(), "")
	}

	if err != nil {
		return
	}

	scheduleRefresh()

	return fmt.Sprintf("Set temperature to %s", newTemp), err
}

type tempMessage struct {
	DeviceId   string
	TargetTemp float64
	Scale      TempScale
{'fraction_non_alphanumeric': 0.13073101490418737, 'fraction_numerical': 0.00255500354861604, 'mean_word_length': 3.5022364217252395, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, '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': '5459059', 'n_tokens_mistral': 2699, 'n_tokens_neox': 2334, 'n_words': 574}