Skip to content

Commit

Permalink
Support V2 binary switch
Browse files Browse the repository at this point in the history
  • Loading branch information
mattludwigs committed Jan 6, 2020
1 parent 937b9c8 commit 0f7b29c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
11 changes: 9 additions & 2 deletions lib/grizzly/command_class/switch_binary.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
defmodule Grizzly.CommandClass.SwitchBinary do
@type switch_state :: :on | :off
@type switch_state_byte :: 0x00 | 0xFF
@type switch_state :: :on | :off | :unknown
@type switch_state_byte :: 0x00 | 0xFF | 0xFE | 1..99

@spec encode_switch_state(switch_state) ::
{:ok, switch_state_byte} | {:error, :invalid_arg, any()}
def encode_switch_state(:on), do: {:ok, 0xFF}
def encode_switch_state(:off), do: {:ok, 0x00}
def encode_switch_state(:unknown), do: {:ok, 0xFE}
def encode_switch_state(arg), do: {:error, :invalid_arg, arg}

@spec decode_switch_state(switch_state_byte()) :: switch_state | 1..99
def decode_switch_state(0x00), do: :off
def decode_switch_state(0xFF), do: :on
def decode_switch_state(0xFE), do: :unknown
def decode_switch_state(value) when value in 1..99, do: value
end
19 changes: 13 additions & 6 deletions lib/grizzly/packet/body_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ defmodule Grizzly.Packet.BodyParser do
ThermostatSetback,
UserCode,
Version,
SwitchBinary,
SwitchMultilevel,
NetworkManagementInclusion,
MultilevelSensor,
Expand Down Expand Up @@ -270,11 +271,21 @@ defmodule Grizzly.Packet.BodyParser do
alarm
end

def parse(<<0x25, 0x03, switch_state>>) do
def parse(<<0x25, 0x03, value>>) do
%{
command_class: :switch_binary,
command: :report,
value: encode_switch_state(switch_state)
value: SwitchBinary.decode_switch_state(value)
}
end

def parse(<<0x25, 0x03, value, target_value, duration>>) do
%{
command_class: :switch_binary,
command: :report,
value: SwitchBinary.decode_switch_state(value),
target_value: SwitchBinary.decode_switch_state(target_value),
duration: duration
}
end

Expand Down Expand Up @@ -1023,10 +1034,6 @@ defmodule Grizzly.Packet.BodyParser do
}
end

defp encode_switch_state(0x00), do: :off
defp encode_switch_state(0xFF), do: :on
defp encode_switch_state(0xFE), do: :unknown

defp encode_basic_value(0x00), do: :off
defp encode_basic_value(0xFF), do: :on
defp encode_basic_value(_), do: :unknown
Expand Down
13 changes: 13 additions & 0 deletions test/grizzly/packet/body_parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,19 @@ defmodule Grizzly.Packet.BodyParser.Test do
value: :unknown
}
end

test "when report is v2" do
binary_switch_report = <<0x025, 0x03, 0x00, 0xFF, 0x01>>
parsed = BodyParser.parse(binary_switch_report)

assert parsed == %{
command_class: :switch_binary,
command: :report,
value: :off,
target_value: :on,
duration: 0x01
}
end
end

describe "parses basic report" do
Expand Down

0 comments on commit 0f7b29c

Please sign in to comment.