Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Energyzero get_gas_prices and get_energy_price services #101374

Merged
Merged
Show file tree
Hide file tree
Changes from 53 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
6db134d
WIP
iamrgroot Sep 15, 2023
23cfd19
WIP
iamrgroot Sep 15, 2023
47c94a7
updated tests
iamrgroot Sep 16, 2023
f0797b0
Merge branch 'dev' into feat/energyzero-service-response
iamrgroot Sep 18, 2023
082116d
Merge branch 'home-assistant:dev' into feat/energyzero-service-response
iamrgroot Sep 20, 2023
734600c
Fixed tests
iamrgroot Sep 20, 2023
84fd586
Merge branch 'dev' into feat/energyzero-service-response
iamrgroot Sep 20, 2023
8164d5f
PR changes
iamrgroot Sep 25, 2023
5fd939c
Update tests
iamrgroot Sep 25, 2023
70e1c4d
Test coverage
iamrgroot Sep 25, 2023
f26f732
CS
iamrgroot Sep 25, 2023
fe0bcf2
Snapshot update
iamrgroot Sep 25, 2023
e5ad801
Added translations
iamrgroot Sep 25, 2023
428c729
Merge branch 'dev' into feat/energyzero-service-response
iamrgroot Sep 25, 2023
7dc0f21
Added default behaviour strings
iamrgroot Sep 25, 2023
1baed5a
Merge branch 'dev' into feat/energyzero-service-response
iamrgroot Sep 25, 2023
6d00475
Merge branch 'dev' into feat/energyzero-service-response
iamrgroot Sep 25, 2023
de4c0bb
PR v2
iamrgroot Oct 2, 2023
0770c81
Merge branch 'dev' into feat/energyzero-service-response
iamrgroot Oct 3, 2023
11d7224
Rename btw to vat
iamrgroot Oct 3, 2023
581908e
Changed to use the coordinator
iamrgroot Oct 4, 2023
49e99c3
Update tests
iamrgroot Oct 4, 2023
37c6e73
Merge remote-tracking branch 'upstream/dev' into feat/energyzero-serv…
iamrgroot Oct 4, 2023
b837b33
Always revert the previous vat setting
iamrgroot Oct 4, 2023
6a6c4ad
Reverted to original test config
iamrgroot Oct 4, 2023
f9fca9b
Apply suggestions from code review
joostlek Dec 5, 2023
ea4284c
Update homeassistant/components/energyzero/__init__.py
joostlek Dec 5, 2023
b94c793
Merge remote-tracking branch 'upstream/dev' into feat/energyzero-serv…
iamrgroot Dec 5, 2023
680f2f1
Readded removed test snapshots
iamrgroot Dec 5, 2023
0065b22
ServiceValidationError and new EnergyZero
iamrgroot Dec 5, 2023
c17ce4f
Re-added correct version of snapshot
iamrgroot Dec 5, 2023
0dc1ba1
Merge remote-tracking branch 'upstream/dev' into feat/energyzero-serv…
iamrgroot Dec 6, 2023
6512b5e
Update to energyzero v2
iamrgroot Dec 6, 2023
ff93307
Update tests
iamrgroot Dec 6, 2023
69ae41a
Fixed strings
iamrgroot Dec 7, 2023
664e5f6
vat required and default true
iamrgroot Dec 7, 2023
5809dc4
Merge remote-tracking branch 'upstream/dev' into feat/energyzero-serv…
iamrgroot Dec 7, 2023
5145644
Merge remote-tracking branch 'upstream/dev' into feat/energyzero-serv…
iamrgroot Dec 8, 2023
5585d24
Changed to EnergyZero dict
iamrgroot Dec 8, 2023
8593949
Snapshot update of new data structure
iamrgroot Dec 8, 2023
a20c995
Used string references and removed not needed
iamrgroot Dec 13, 2023
942aad6
Merge remote-tracking branch 'upstream/dev' into feat/energyzero-serv…
iamrgroot Dec 13, 2023
80bbada
Service register to new file
iamrgroot Dec 13, 2023
30c4e83
Merge branch 'dev' into feat/energyzero-service-response
iamrgroot Dec 13, 2023
8a3d47f
Use partial
iamrgroot Dec 13, 2023
3ffc1e3
Make an sync function without awaits async for reasons unknown
iamrgroot Dec 13, 2023
e3e25b5
Review from other PR
iamrgroot Dec 13, 2023
b7bc84a
Callback function
iamrgroot Dec 18, 2023
cbaadff
Cannot await non asyn functions
iamrgroot Dec 18, 2023
2de5c48
Merge branch 'dev' into feat/energyzero-service-response
iamrgroot Dec 18, 2023
053a3dd
Separated tests for exceptions
iamrgroot Dec 18, 2023
f64c026
Merge remote-tracking branch 'upstream/dev' into feat/energyzero-serv…
iamrgroot Dec 19, 2023
955a47b
Added missing error message and exception messages
iamrgroot Dec 19, 2023
595fe2a
Better tuples in test parameters
iamrgroot Dec 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions homeassistant/components/energyzero/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from .const import DOMAIN
from .coordinator import EnergyZeroDataUpdateCoordinator
from .services import async_register_services

PLATFORMS = [Platform.SENSOR]

Expand All @@ -25,6 +26,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

async_register_services(hass, coordinator)

return True


Expand Down
129 changes: 129 additions & 0 deletions homeassistant/components/energyzero/services.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
"""The EnergyZero services."""
from __future__ import annotations

from datetime import date, datetime
from enum import Enum
from functools import partial
from typing import Final

from energyzero import Electricity, Gas, VatOption
import voluptuous as vol

from homeassistant.core import (
HomeAssistant,
ServiceCall,
ServiceResponse,
SupportsResponse,
callback,
)
from homeassistant.exceptions import ServiceValidationError
from homeassistant.util import dt as dt_util

from .const import DOMAIN
from .coordinator import EnergyZeroDataUpdateCoordinator

ATTR_START: Final = "start"
ATTR_END: Final = "end"
ATTR_INCL_VAT: Final = "incl_vat"

GAS_SERVICE_NAME: Final = "get_gas_prices"
ENERGY_SERVICE_NAME: Final = "get_energy_prices"
SERVICE_SCHEMA: Final = vol.Schema(
{
vol.Required(ATTR_INCL_VAT): bool,
vol.Optional(ATTR_START): str,
vol.Optional(ATTR_END): str,
}
)


class PriceType(Enum):
"""Type of price."""

ENERGY = "energy"
GAS = "gas"


def __get_date(date_input: str | None) -> date | datetime:
"""Get date."""
if not date_input:
return dt_util.now().date()

if value := dt_util.parse_datetime(date_input):
return value

raise ServiceValidationError(
"Invalid datetime provided.",
translation_domain=DOMAIN,
iamrgroot marked this conversation as resolved.
Show resolved Hide resolved
translation_key="invalid_date",
translation_placeholders={
"date": date_input,
},
)


def __serialize_prices(prices: Electricity | Gas) -> ServiceResponse:
"""Serialize prices."""
return {
"prices": [
{
key: str(value) if isinstance(value, datetime) else value
for key, value in timestamp_price.items()
}
for timestamp_price in prices.timestamp_prices
]
}


async def __get_prices(
call: ServiceCall,
*,
coordinator: EnergyZeroDataUpdateCoordinator,
price_type: PriceType,
) -> ServiceResponse:
start = __get_date(call.data.get(ATTR_START))
end = __get_date(call.data.get(ATTR_END))

vat = VatOption.INCLUDE

if call.data.get(ATTR_INCL_VAT) is False:
vat = VatOption.EXCLUDE

data: Electricity | Gas

if price_type == PriceType.GAS:
data = await coordinator.energyzero.gas_prices(
start_date=start,
end_date=end,
vat=vat,
)
else:
data = await coordinator.energyzero.energy_prices(
start_date=start,
end_date=end,
vat=vat,
)

return __serialize_prices(data)


@callback
def async_register_services(
hass: HomeAssistant, coordinator: EnergyZeroDataUpdateCoordinator
):
"""Set up EnergyZero services."""

hass.services.async_register(
DOMAIN,
GAS_SERVICE_NAME,
partial(__get_prices, coordinator=coordinator, price_type=PriceType.GAS),
iamrgroot marked this conversation as resolved.
Show resolved Hide resolved
schema=SERVICE_SCHEMA,
supports_response=SupportsResponse.ONLY,
)
hass.services.async_register(
DOMAIN,
ENERGY_SERVICE_NAME,
partial(__get_prices, coordinator=coordinator, price_type=PriceType.ENERGY),
schema=SERVICE_SCHEMA,
supports_response=SupportsResponse.ONLY,
)
34 changes: 34 additions & 0 deletions homeassistant/components/energyzero/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
get_gas_prices:
fields:
incl_vat:
required: true
default: true
selector:
boolean:
start:
required: false
example: "2023-01-01 00:00:00"
selector:
datetime:
end:
required: false
example: "2023-01-01 00:00:00"
selector:
datetime:
get_energy_prices:
fields:
incl_vat:
required: true
default: true
selector:
boolean:
start:
required: false
example: "2023-01-01 00:00:00"
selector:
datetime:
end:
required: false
example: "2023-01-01 00:00:00"
selector:
datetime:
43 changes: 43 additions & 0 deletions homeassistant/components/energyzero/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
}
},
"exceptions": {
"invalid_date": {
"message": "Invalid date provided. Got {date}"
}
},
"entity": {
"sensor": {
"current_hour_price": {
Expand Down Expand Up @@ -39,5 +44,43 @@
"name": "Hours priced equal or lower than current - today"
}
}
},
"services": {
"get_gas_prices": {
"name": "Get gas prices",
"description": "Request gas prices from EnergyZero.",
"fields": {
"incl_vat": {
"name": "Including VAT",
"description": "Include VAT in the prices."
},
"start": {
"name": "Start",
"description": "Specifies the date and time from which to retrieve prices. Defaults to today if omitted."
},
"end": {
"name": "End",
"description": "Specifies the date and time until which to retrieve prices. Defaults to today if omitted."
}
}
},
"get_energy_prices": {
"name": "Get energy prices",
"description": "Request energy prices from EnergyZero.",
"fields": {
"incl_vat": {
"name": "[%key:component::energyzero::services::get_gas_prices::fields::incl_vat::name%]",
"description": "[%key:component::energyzero::services::get_gas_prices::fields::incl_vat::description%]"
},
"start": {
"name": "[%key:component::energyzero::services::get_gas_prices::fields::start::name%]",
"description": "[%key:component::energyzero::services::get_gas_prices::fields::start::description%]"
},
"end": {
"name": "[%key:component::energyzero::services::get_gas_prices::fields::end::name%]",
"description": "[%key:component::energyzero::services::get_gas_prices::fields::end::description%]"
}
}
}
}
}
Loading