Skip to content

Commit

Permalink
Fix an issue when car is could enter sleep mode while dog mode is ena…
Browse files Browse the repository at this point in the history
…bled (#3538)

* Fix an issue when car is could enter sleep mode while dog mode is enabled.

* Update pot files

* revert change

* Fix tests

* Fix tests and issue when try to sleep was not showing

* Fix lint issue
  • Loading branch information
NirKli authored Dec 12, 2023
1 parent 83a0b38 commit b5fa024
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 39 deletions.
9 changes: 9 additions & 0 deletions lib/teslamate/vehicles/vehicle.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,12 @@ defmodule TeslaMate.Vehicles.Vehicle do
{:keep_state, %Data{data | last_used: DateTime.utc_now()},
[broadcast_summary(), schedule_fetch(30 * i, data)]}

{:error, :dogmode} ->
if suspend?, do: Logger.warning("Dog Mode is enabled ...", car_id: car.id)

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

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

Expand Down Expand Up @@ -1413,6 +1419,9 @@ defmodule TeslaMate.Vehicles.Vehicle do
{%Vehicle{climate_state: %Climate{is_preconditioning: true}}, _} ->
{:error, :preconditioning}

{%Vehicle{climate_state: %Climate{climate_keeper_mode: "dog"}}, _} ->
{:error, :dogmode}

{%Vehicle{vehicle_state: %VehicleState{sentry_mode: true}}, _} ->
{:error, :sentry_mode}

Expand Down
3 changes: 2 additions & 1 deletion lib/teslamate/vehicles/vehicle/summary.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule TeslaMate.Vehicles.Vehicle.Summary do
charger_actual_current charger_voltage version update_available update_version is_user_present geofence
model trim_badging exterior_color wheel_type spoiler_type trunk_open frunk_open elevation power
charge_current_request charge_current_request_max tpms_pressure_fl tpms_pressure_fr tpms_pressure_rl tpms_pressure_rr
tpms_soft_warning_fl tpms_soft_warning_fr tpms_soft_warning_rl tpms_soft_warning_rr
tpms_soft_warning_fl tpms_soft_warning_fr tpms_soft_warning_rl tpms_soft_warning_rr climate_keeper_mode
)a

def into(nil, %{state: :start, healthy?: healthy?, car: car}) do
Expand Down Expand Up @@ -106,6 +106,7 @@ defmodule TeslaMate.Vehicles.Vehicle.Summary do
# Climate State
is_climate_on: get_in_struct(vehicle, [:climate_state, :is_climate_on]),
is_preconditioning: get_in_struct(vehicle, [:climate_state, :is_preconditioning]),
climate_keeper_mode: get_in_struct(vehicle, [:climate_state, :climate_keeper_mode]),
outside_temp: get_in_struct(vehicle, [:climate_state, :outside_temp]),
inside_temp: get_in_struct(vehicle, [:climate_state, :inside_temp]),

Expand Down
1 change: 1 addition & 0 deletions lib/teslamate_web/live/car_live/summary.ex
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ defmodule TeslaMateWeb.CarLive.Summary do
defp error_to_str(:trunk_open), do: gettext("Trunk is open")
defp error_to_str(:sentry_mode), do: gettext("Sentry mode is enabled")
defp error_to_str(:preconditioning), do: gettext("Preconditioning")
defp error_to_str(:dogmode), do: gettext("Dog mode is enabled")
defp error_to_str(:user_present), do: gettext("Driver present")
defp error_to_str(:downloading_update), do: gettext("Downloading update")
defp error_to_str(:update_in_progress), do: gettext("Update in progress")
Expand Down
8 changes: 7 additions & 1 deletion lib/teslamate_web/live/car_live/summary.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
<span class="mdi mdi-air-conditioner"></span>
</span>
<% end %>
<%= if @summary.climate_keeper_mode == "dog" do %>
<span class="icon has-text-grey-dark has-tooltip-top has-tooltip-left-mobile" data-tooltip={gettext "Dog Mode"}>
<span class="mdi mdi-dog-side"></span>
</span>
<% end %>
<%= if not is_nil(@summary.battery_level) and not is_nil(@summary.usable_battery_level) and @summary.battery_level - @summary.usable_battery_level > 2 do %>
<span class="icon has-text-link has-tooltip-top has-tooltip-left-mobile" data-tooltip={gettext "Reduced Battery Range"}>
<span class="mdi mdi-snowflake"></span>
Expand Down Expand Up @@ -327,7 +332,8 @@
@summary.state == :online and
!@summary.sentry_mode and
!@summary.is_user_present and
!@summary.is_preconditioning ->
!@summary.is_preconditioning and
@summary.climate_keeper_mode == "off" ->
link gettext("try to sleep"), to: "#", phx_click: "suspend_logging",
class: "button is-info is-small is-outlined is-fullwidth" <>
(if @loading, do: " is-loading", else: "")
Expand Down
80 changes: 45 additions & 35 deletions priv/gettext/default.pot
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@
msgid ""
msgstr ""

#: lib/teslamate_web/live/car_live/summary.html.heex:133
#: lib/teslamate_web/live/car_live/summary.html.heex:138
#, elixir-autogen, elixir-format
msgid "Status"
msgstr ""

#: lib/teslamate_web/live/car_live/summary.html.heex:266
#: lib/teslamate_web/live/car_live/summary.html.heex:271
#, elixir-autogen, elixir-format
msgid "Speed"
msgstr ""

#: lib/teslamate_web/live/car_live/summary.html.heex:258
#: lib/teslamate_web/live/car_live/summary.html.heex:263
#, elixir-autogen, elixir-format
msgid "State of Charge"
msgstr ""

#: lib/teslamate_web/live/car_live/summary.html.heex:194
#: lib/teslamate_web/live/car_live/summary.html.heex:199
#, elixir-autogen, elixir-format
msgid "Charged"
msgstr ""
Expand Down Expand Up @@ -60,12 +60,12 @@ msgstr ""
msgid "updating"
msgstr ""

#: lib/teslamate_web/live/car_live/summary.html.heex:78
#: lib/teslamate_web/live/car_live/summary.html.heex:83
#, elixir-autogen, elixir-format
msgid "Locked"
msgstr ""

#: lib/teslamate_web/live/car_live/summary.html.heex:73
#: lib/teslamate_web/live/car_live/summary.html.heex:78
#, elixir-autogen, elixir-format
msgid "Sentry Mode"
msgstr ""
Expand All @@ -87,17 +87,17 @@ msgstr ""
msgid "Settings"
msgstr ""

#: lib/teslamate_web/live/car_live/summary.html.heex:209
#: lib/teslamate_web/live/car_live/summary.html.heex:214
#, elixir-autogen, elixir-format
msgid "Scheduled Charging"
msgstr ""

#: lib/teslamate_web/live/car_live/summary.html.heex:58
#: lib/teslamate_web/live/car_live/summary.html.heex:63
#, elixir-autogen, elixir-format
msgid "Plugged In"
msgstr ""

#: lib/teslamate_web/live/car_live/summary.html.heex:218
#: lib/teslamate_web/live/car_live/summary.html.heex:223
#, elixir-autogen, elixir-format
msgid "Charge Limit"
msgstr ""
Expand Down Expand Up @@ -220,28 +220,28 @@ msgstr ""
msgid "Sentry mode is enabled"
msgstr ""

#: lib/teslamate_web/live/car_live/summary.ex:139
#: lib/teslamate_web/live/car_live/summary.html.heex:53
#: lib/teslamate_web/live/car_live/summary.ex:140
#: lib/teslamate_web/live/car_live/summary.html.heex:58
#, elixir-autogen, elixir-format
msgid "Driver present"
msgstr ""

#: lib/teslamate_web/live/car_live/summary.html.heex:336
#: lib/teslamate_web/live/car_live/summary.html.heex:342
#, elixir-autogen, elixir-format
msgid "cancel sleep attempt"
msgstr ""

#: lib/teslamate_web/live/car_live/summary.html.heex:331
#: lib/teslamate_web/live/car_live/summary.html.heex:337
#, elixir-autogen, elixir-format
msgid "try to sleep"
msgstr ""

#: lib/teslamate_web/live/car_live/summary.html.heex:181
#: lib/teslamate_web/live/car_live/summary.html.heex:186
#, elixir-autogen, elixir-format
msgid "Range (est.)"
msgstr ""

#: lib/teslamate_web/live/car_live/summary.html.heex:136
#: lib/teslamate_web/live/car_live/summary.html.heex:141
#, elixir-autogen, elixir-format
msgid "for"
msgstr ""
Expand All @@ -256,12 +256,12 @@ msgstr ""
msgid "Vehicle must be locked"
msgstr ""

#: lib/teslamate_web/live/car_live/summary.html.heex:162
#: lib/teslamate_web/live/car_live/summary.html.heex:167
#, elixir-autogen, elixir-format
msgid "Range (rated)"
msgstr ""

#: lib/teslamate_web/live/car_live/summary.html.heex:200
#: lib/teslamate_web/live/car_live/summary.html.heex:205
#, elixir-autogen, elixir-format
msgid "Charger Power"
msgstr ""
Expand All @@ -286,7 +286,7 @@ msgstr ""
msgid "rated"
msgstr ""

#: lib/teslamate_web/live/car_live/summary.html.heex:161
#: lib/teslamate_web/live/car_live/summary.html.heex:166
#, elixir-autogen, elixir-format
msgid "Range (ideal)"
msgstr ""
Expand All @@ -296,12 +296,12 @@ msgstr ""
msgid "The car's estimate of remaining range is based on a fixed energy consumption in Wh/km. The Wh/km factor is determined by Tesla and is not country specific whereas the rated range is based on regulatory tests in the different markets for that vehicle."
msgstr ""

#: lib/teslamate_web/live/car_live/summary.ex:141
#: lib/teslamate_web/live/car_live/summary.ex:142
#, elixir-autogen, elixir-format
msgid "Update in progress"
msgstr ""

#: lib/teslamate_web/live/car_live/summary.html.heex:63
#: lib/teslamate_web/live/car_live/summary.html.heex:68
#, elixir-autogen, elixir-format
msgid "Windows open"
msgstr ""
Expand All @@ -311,33 +311,33 @@ msgstr ""
msgid "Delete '%{geo_fence}'?"
msgstr ""

#: lib/teslamate_web/live/car_live/summary.html.heex:291
#: lib/teslamate_web/live/car_live/summary.html.heex:296
#, elixir-autogen, elixir-format
msgid "Inside Temperature"
msgstr ""

#: lib/teslamate_web/live/car_live/summary.html.heex:279
#: lib/teslamate_web/live/car_live/summary.html.heex:284
#, elixir-autogen, elixir-format
msgid "Outside Temperature"
msgstr ""

#: lib/teslamate_web/live/car_live/summary.html.heex:316
#: lib/teslamate_web/live/car_live/summary.html.heex:321
#: lib/teslamate_web/live/settings_live/index.html.heex:283
#, elixir-autogen, elixir-format
msgid "Version"
msgstr ""

#: lib/teslamate_web/live/car_live/summary.html.heex:117
#: lib/teslamate_web/live/car_live/summary.html.heex:122
#, elixir-autogen, elixir-format
msgid "Health check failed"
msgstr ""

#: lib/teslamate_web/live/car_live/summary.html.heex:78
#: lib/teslamate_web/live/car_live/summary.html.heex:83
#, elixir-autogen, elixir-format
msgid "Unlocked"
msgstr ""

#: lib/teslamate_web/live/car_live/summary.html.heex:148
#: lib/teslamate_web/live/car_live/summary.html.heex:153
#, elixir-autogen, elixir-format
msgid "Remaining Time"
msgstr ""
Expand Down Expand Up @@ -374,22 +374,22 @@ msgstr ""
msgid "Geo-fence \"%{name}\" updated"
msgstr ""

#: lib/teslamate_web/live/car_live/summary.ex:143
#: lib/teslamate_web/live/car_live/summary.ex:144
#, elixir-autogen, elixir-format
msgid "An error occurred"
msgstr ""

#: lib/teslamate_web/live/car_live/summary.ex:142
#: lib/teslamate_web/live/car_live/summary.ex:143
#, elixir-autogen, elixir-format
msgid "Timeout"
msgstr ""

#: lib/teslamate_web/live/car_live/summary.html.heex:48
#: lib/teslamate_web/live/car_live/summary.html.heex:53
#, elixir-autogen, elixir-format
msgid "Reduced Battery Range"
msgstr ""

#: lib/teslamate_web/live/car_live/summary.html.heex:251
#: lib/teslamate_web/live/car_live/summary.html.heex:256
#, elixir-autogen, elixir-format
msgid "≈ %{range} at 100%"
msgstr ""
Expand Down Expand Up @@ -485,7 +485,7 @@ msgstr ""
msgid "Session fee"
msgstr ""

#: lib/teslamate_web/live/car_live/summary.html.heex:68
#: lib/teslamate_web/live/car_live/summary.html.heex:73
#, elixir-autogen, elixir-format
msgid "Doors open"
msgstr ""
Expand Down Expand Up @@ -523,7 +523,7 @@ msgstr ""
msgid "Continue"
msgstr ""

#: lib/teslamate_web/live/car_live/summary.html.heex:304
#: lib/teslamate_web/live/car_live/summary.html.heex:309
#, elixir-autogen, elixir-format
msgid "Mileage"
msgstr ""
Expand Down Expand Up @@ -565,7 +565,7 @@ msgstr ""
msgid "Per Minute"
msgstr ""

#: lib/teslamate_web/live/car_live/summary.html.heex:83
#: lib/teslamate_web/live/car_live/summary.html.heex:88
#, elixir-autogen, elixir-format
msgid "Software Update available (%{version})"
msgstr ""
Expand Down Expand Up @@ -605,7 +605,7 @@ msgstr ""
msgid "Your Tesla account is locked due to too many failed sign in attempts. To unlock your account, reset your password"
msgstr ""

#: lib/teslamate_web/live/car_live/summary.ex:140
#: lib/teslamate_web/live/car_live/summary.ex:141
#, elixir-autogen, elixir-format
msgid "Downloading update"
msgstr ""
Expand Down Expand Up @@ -635,7 +635,17 @@ msgstr ""
msgid "Tire Pressure"
msgstr ""

#: lib/teslamate_web/live/car_live/summary.html.heex:91
#: lib/teslamate_web/live/car_live/summary.html.heex:96
#, elixir-autogen, elixir-format
msgid "Low tire pressure, check (%{tire_low}) tire"
msgstr ""

#: lib/teslamate_web/live/car_live/summary.html.heex:48
#, elixir-autogen, elixir-format
msgid "Dog Mode"
msgstr ""

#: lib/teslamate_web/live/car_live/summary.ex:139
#, elixir-autogen, elixir-format
msgid "Dog mode is enabled"
msgstr ""
33 changes: 33 additions & 0 deletions test/teslamate/vehicles/vehicle/suspend_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,39 @@ defmodule TeslaMate.Vehicles.Vehicle.SuspendTest do
refute_receive _
end

@tag :capture_log
test "does not suspend if dog mode is active", %{test: name} do
not_suspendable =
online_event(
drive_state: %{timestamp: 0, latitude: 0.0, longitude: 0.0},
climate_state: %{climate_keeper_mode: "dog"}
)

events = [
{:ok, online_event()},
{:ok, not_suspendable}
]

suspend_after_idle_ms = 10
suspend_ms = 100

:ok =
start_vehicle(name, events,
settings: %{
suspend_after_idle_min: round(suspend_after_idle_ms / 60),
suspend_min: suspend_ms
}
)

assert_receive {:start_state, car, :online, date: _}
assert_receive {ApiMock, {:stream, 1000, _}}
assert_receive {:insert_position, ^car, %{}}
assert_receive {:pubsub, {:broadcast, _, _, %Summary{state: :online}}}
refute_receive _, round(suspend_ms * 0.5)

refute_receive _
end

@tag :capture_log
test "does not suspend if user is present", %{test: name} do
not_suspendable =
Expand Down
5 changes: 3 additions & 2 deletions test/teslamate_web/live/car_summary_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ defmodule TeslaMateWeb.CarLive.SummaryTest do
online_event(
display_name: "FooCar",
drive_state: %{timestamp: now, latitude: 0.0, longitude: 0.0},
climate_state: %{timestamp: now, is_preconditioning: false},
climate_state: %{timestamp: now, is_preconditioning: false, climate_keeper_mode: "off"},
vehicle_state: %{timestamp: now, sentry_mode: false, locked: true, car_version: ""}
)}
]
Expand Down Expand Up @@ -103,6 +103,7 @@ defmodule TeslaMateWeb.CarLive.SummaryTest do
{"Sentry mode is enabled", "online", %{},
vehicle_state: %{timestamp: 0, sentry_mode: true, locked: true, car_version: ""}},
{"Preconditioning", "online", %{}, climate_state: %{is_preconditioning: true}},
{"Dog mode is enabled", "online", %{}, climate_state: %{climate_keeper_mode: "dog"}},
{"Driver present", "online", %{},
vehicle_state: %{timestamp: 0, is_user_present: true, car_version: ""}},
{"Update in progress", "updating", %{},
Expand Down Expand Up @@ -180,7 +181,7 @@ defmodule TeslaMateWeb.CarLive.SummaryTest do
online_event(
display_name: "FooCar",
drive_state: %{timestamp: now, latitude: 0.0, longitude: 0.0},
climate_state: %{timestamp: now, is_preconditioning: false},
climate_state: %{timestamp: now, is_preconditioning: false, climate_keeper_mode: "off"},
vehicle_state: %{timestamp: now, sentry_mode: false, locked: true, car_version: ""}
)}
]
Expand Down

0 comments on commit b5fa024

Please sign in to comment.