evcc-io/tariff/octopus/rest/api.go

48 lines
1.7 KiB
Go

package rest
import (
"fmt"
"strings"
"time"
)
// ProductURI defines the location of the tariff information page. Substitute %s with tariff name.
const ProductURI = "https://api.octopus.energy/v1/products/%s/"
// RatesURI defines the location of the full tariff rates page, including speculation.
// Substitute first %s with product code, second with tariff code.
const RatesURI = ProductURI + "electricity-tariffs/%s/standard-unit-rates/"
// ConstructRatesAPIFromProductAndRegionCode returns a validly formatted, fully qualified URI to the unit rate information
// derived from the given product code and region.
func ConstructRatesAPIFromProductAndRegionCode(product string, region string) string {
tCode := strings.ToUpper(fmt.Sprintf("E-1R-%s-%s", product, region))
return fmt.Sprintf(RatesURI, product, tCode)
}
// ConstructRatesAPIFromTariffCode returns a validly formatted, fully qualified URI to the unit rate information
// derived from the given Tariff Code.
func ConstructRatesAPIFromTariffCode(tariff string) string {
// Hacky bullshit, saves handling both the product and tariff codes in GQL mode.
// Hopefully Octopus don't change how this works otherwise we might have to do this properly :(
if len(tariff) < 7 {
// OOB check
return ""
}
pCode := tariff[5 : len(tariff)-2]
return fmt.Sprintf(RatesURI, pCode, tariff)
}
type UnitRates struct {
Count uint64 `json:"count"`
Next string `json:"next"`
Previous string `json:"previous"`
Results []Rate `json:"results"`
}
type Rate struct {
ValidityStart time.Time `json:"valid_from"`
ValidityEnd time.Time `json:"valid_to"`
PriceInclusiveTax float64 `json:"value_inc_vat"`
PriceExclusiveTax float64 `json:"value_exc_vat"`
}