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

feat: optimize state transitions #4473

Merged
merged 3 commits into from
Jan 6, 2025
Merged
Changes from all commits
Commits
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
42 changes: 39 additions & 3 deletions lib/teslamate/vehicles/vehicle.ex
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,21 @@ defmodule TeslaMate.Vehicles.Vehicle do
[broadcast_summary(), schedule_fetch(0, data)]}

%Stream.Data{shift_state: nil, power: power} when is_number(power) and power < 0 ->
Logger.info("Charging detected: #{power} kW", car_id: data.car.id)
{:keep_state_and_data, schedule_fetch(0, data)}
vehicle = merge(data.last_response, stream_data, time: true)

# Only detect as charging if we are not doing something else while plugged in.
# In case we are doing both charging and other thing a normal fetch will discover it later
case {vehicle} do
{%Vehicle{climate_state: %Climate{is_preconditioning: true}}} ->
:keep_state_and_data

{%Vehicle{climate_state: %Climate{climate_keeper_mode: "dog"}}} ->
:keep_state_and_data

{%Vehicle{}} ->
Logger.info("Online / Charging detected: #{power} kW", car_id: data.car.id)
{:keep_state_and_data, schedule_fetch(0, data)}
end

%Stream.Data{} ->
Logger.debug(inspect(stream_data), car_id: data.car.id)
Expand Down Expand Up @@ -519,7 +532,20 @@ defmodule TeslaMate.Vehicles.Vehicle do
%Stream.Data{shift_state: s, power: power}
when s in [nil, "P"] and is_number(power) and power < 0 ->
Logger.info("Suspended / Charging detected: #{power} kW", car_id: data.car.id)
{:next_state, prev_state, data, schedule_fetch(0, data)}

{:next_state, prev_state, %Data{data | last_used: DateTime.utc_now()},
schedule_fetch(0, data)}

%Stream.Data{shift_state: s, power: power}
when s in [nil, "P"] and is_number(power) and power > 0 ->
Logger.info("Suspended / Usage detected: #{power} kW", car_id: data.car.id)

# update power to be used in can_fall_asleep / try_to_suspend
vehicle = merge(data.last_response, stream_data, time: true)

{:next_state, prev_state,
%Data{data | last_response: vehicle, last_used: DateTime.utc_now()},
schedule_fetch(0, data)}

%Stream.Data{} ->
Logger.debug(inspect(stream_data), car_id: data.car.id)
Expand Down Expand Up @@ -1433,6 +1459,12 @@ defmodule TeslaMate.Vehicles.Vehicle do
{:keep_state, %Data{data | last_used: DateTime.utc_now()},
[broadcast_summary(), schedule_fetch(default_interval() * i, data)]}

{:error, :power_usage} ->
if suspend?, do: Logger.warning("Power usage ...", car_id: car.id)

{:keep_state, %Data{data | last_used: DateTime.utc_now()},
[broadcast_summary(), schedule_fetch(default_interval() * i, data)]}

{:error, :unlocked} ->
if suspend?, do: Logger.warning("Unlocked ...", car_id: car.id)

Expand Down Expand Up @@ -1501,6 +1533,10 @@ defmodule TeslaMate.Vehicles.Vehicle do
%CarSettings{req_not_unlocked: true}} ->
{:error, :unlocked}

{%Vehicle{drive_state: %Drive{power: power}}, _}
when is_number(power) and power > 0 ->
{:error, :power_usage}

{%Vehicle{}, %CarSettings{}} ->
:ok
end
Expand Down
Loading