Skip to content

Commit

Permalink
Add time_of_use
Browse files Browse the repository at this point in the history
  • Loading branch information
Bre77 committed Jun 7, 2024
1 parent 545bdf1 commit f442326
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 3 deletions.
3 changes: 2 additions & 1 deletion custom_components/teslemetry/icons.json
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@
"set_scheduled_charging": "mdi:timeline-clock-outline",
"set_scheduled_departure": "mdi:home-clock",
"speed_limit": "mdi:car-speed-limiter",
"valet_mode": "mdi:speedometer-slow"
"valet_mode": "mdi:speedometer-slow",
"time_of_use": "mdi:clock-time-eight-outline"
}
}
2 changes: 1 addition & 1 deletion custom_components/teslemetry/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"teslemetry_stream"
],
"requirements": [
"tesla-fleet-api==0.5.15",
"tesla-fleet-api==0.6.1",
"teslemetry-stream==0.2.3"
],
"version": "1.5.6"
Expand Down
40 changes: 39 additions & 1 deletion custom_components/teslemetry/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@


from .const import DOMAIN
from .models import TeslemetryVehicleData
from .models import TeslemetryVehicleData, TeslemetryEnergyData
from .helpers import wake_up_vehicle, handle_vehicle_command

_LOGGER = logging.getLogger(__name__)
Expand All @@ -39,6 +39,7 @@
ENABLE = "enable"
TIME = "time"
PIN = "pin"
TOU_SETTINGS = "tou_settings"

def async_get_device_for_service_call(
hass: HomeAssistant, call: ServiceCall
Expand Down Expand Up @@ -73,6 +74,16 @@ def async_get_vehicle_for_entry(
return vehicle
raise ServiceValidationError(f"No vehicle data for device ID: {device.id}")

def async_get_energy_site_for_entry(
hass: HomeAssistant, device: dr.DeviceEntry, config: ConfigEntry
) -> TeslemetryEnergyData:
"""Get the vehicle data for a config entry."""
assert device.serial_number is not None
for site in config.runtime_data.energy_sites:
if site.id == device.serial_number:
return site
raise ServiceValidationError(f"No energy site for device ID: {device.id}")


def async_register_services(hass: HomeAssistant) -> bool:
"""Set up the Tessie integration."""
Expand Down Expand Up @@ -361,4 +372,31 @@ async def speed_limit(call: ServiceCall) -> None:
vol.Required(PIN): All(cv.positive_int, Range(min=1000, max=9999)),
}
),
)

async def time_of_use(call: ServiceCall) -> None:
"""Configure time of use settings."""
device = async_get_device_for_service_call(hass, call)
config = async_get_config_for_device(hass, device)
site = async_get_energy_site_for_entry(hass, device, config)

try:
resp = await site.api.set_time_of_use(
call.data.get(TOU_SETTINGS)
)
except Exception as e:
raise HomeAssistantError from e
if "error" in resp:
raise ServiceValidationError(resp["error"])

hass.services.async_register(
DOMAIN,
"time_of_use",
time_of_use,
schema=vol.Schema(
{
vol.Required(CONF_DEVICE_ID): cv.string,
vol.Required(TOU_SETTINGS): dict,
}
),
)
12 changes: 12 additions & 0 deletions custom_components/teslemetry/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ stream_fields:
selector:
object:

time_of_use:
fields:
device_id:
required: true
selector:
device:
integration: teslemetry
tou_settings:
required: true
selector:
object:

set_scheduled_charging:
fields:
device_id:
Expand Down
14 changes: 14 additions & 0 deletions custom_components/teslemetry/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,20 @@
},
"name": "Stream fields"
},
"time_of_use": {
"description": "Update the time of use settings for the energy site.",
"fields": {
"device_id": {
"description": "Energy Site to configure.",
"name": "Energy Site"
},
"tou_settings": {
"description": "See https://developer.tesla.com/docs/fleet-api#time_of_use_settings for details.",
"name": "Settings"
}
},
"name": "Time of use settings"
},
"valet_mode": {
"description": "Activate the valet mode of the vehicle.",
"fields": {
Expand Down

0 comments on commit f442326

Please sign in to comment.