Skip to content

Commit

Permalink
fix: Updated tariff extraction to be more flexible with beta tariffs …
Browse files Browse the repository at this point in the history
…(e.g V2G)
  • Loading branch information
BottlecapDave committed Apr 8, 2023
1 parent f65c34a commit 3a9c2fb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
4 changes: 3 additions & 1 deletion custom_components/octopus_energy/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
REGEX_HOURS = "^[0-9]+(\\.[0-9]+)*$"
REGEX_TIME = "^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$"
REGEX_ENTITY_NAME = "^[a-z0-9_]+$"
REGEX_TARIFF_PARTS = "^([A-Z])-([0-9A-Z]+)-([A-Z0-9-]+)-([A-Z])$"
# According to https://www.guylipman.com/octopus/api_guide.html#s1b, this part should indicate the types of tariff
# However it looks like there are some tariffs that don't fit this mold
REGEX_TARIFF_PARTS = "^((?P<energy>[A-Z])-(?P<rate>[0-9A-Z]+)-)?(?P<product_code>[A-Z0-9-]+)-(?P<region>[A-Z])$"
REGEX_OFFSET_PARTS = "^(-)?([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$"

DATA_SCHEMA_ACCOUNT = vol.Schema({
Expand Down
14 changes: 7 additions & 7 deletions custom_components/octopus_energy/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ def get_tariff_parts(tariff_code):
matches = re.search(REGEX_TARIFF_PARTS, tariff_code)
if matches == None:
return None

# According to https://www.guylipman.com/octopus/api_guide.html#s1b, this part should indicate if we're dealing
# with standard rates or day/night rates
energy = matches[1]
rate = matches[2]
product_code = matches[3]
region = matches[4]
# If our energy or rate isn't extracted, then assume is electricity and "single" rate as that's
# where our experimental tariffs are
energy = matches.groupdict()["energy"] or "E"
rate = matches.groupdict()["rate"] or "1R"
product_code =matches.groupdict()["product_code"]
region = matches.groupdict()["region"]

return {
"energy": energy,
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/test_tariff_parts.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
@pytest.mark.asyncio
@pytest.mark.parametrize("tariff_code,expected_energy,expected_rate,expected_product_code,expected_region",[
("G-1R-SUPER-GREEN-24M-21-07-30-A", "G", "1R", "SUPER-GREEN-24M-21-07-30", "A"),
("E-2R-SUPER-GREEN-24M-21-07-30-A", "E", "2R", "SUPER-GREEN-24M-21-07-30", "A")
("E-2R-SUPER-GREEN-24M-21-07-30-A", "E", "2R", "SUPER-GREEN-24M-21-07-30", "A"),
("V2G-EXPORT-12M-FIXED-22-12-01-C", "E", "1R", "V2G-EXPORT-12M-FIXED-22-12-01", "C")
])
async def test_get_tariff_parts(tariff_code, expected_energy, expected_rate, expected_product_code, expected_region):
# Act
Expand Down

0 comments on commit 3a9c2fb

Please sign in to comment.