-
Notifications
You must be signed in to change notification settings - Fork 9
/
accessory.ex
78 lines (67 loc) · 2.37 KB
/
accessory.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
defmodule HAP.Accessory do
@moduledoc """
Represents a single accessory object, containing a number of services
"""
defstruct name: "Generic HAP Accessory",
model: "Generic HAP Model",
manufacturer: "Generic HAP Manufacturer",
serial_number: "Generic Serial Number",
firmware_revision: "1.0",
services: []
@typedoc """
Represents an accessory consisting of a number of services. Contains the following
fields:
* `name`: The name to assign to this accessory, for example 'Ceiling Fan'
* `model`: The model name to assign to this accessory, for example 'FanCo Whisper III'
* `manufacturer`: The manufacturer of this accessory, for example 'FanCo'
* `serial_number`: The serial number of this accessory, for example '0012345'
* `firmware_revision`: The firmware revision of this accessory, for example '1.0'
* `services`: A list of services to include in this accessory
"""
@type t :: %__MODULE__{
name: name(),
model: model(),
manufacturer: manufacturer(),
serial_number: serial_number(),
firmware_revision: firmware_revision(),
services: [HAP.Service.t()]
}
@typedoc """
The name to advertise for this accessory, for example 'HAP Light Bulb'
"""
@type name :: String.t()
@typedoc """
The model of this accessory, for example 'HAP Light Bulb Supreme'
"""
@type model :: String.t()
@typedoc """
The manufacturer of this accessory, for example 'HAP Co.'
"""
@type manufacturer :: String.t()
@typedoc """
The serial number of this accessory, for example '0012345'
"""
@type serial_number :: String.t()
@typedoc """
The firmware recvision of this accessory, for example '1.0' or '1.0.1'
"""
@type firmware_revision :: String.t()
@doc false
def compile(%__MODULE__{services: services} = accessory) do
all_services =
[%HAP.Services.AccessoryInformation{accessory: accessory}, %HAP.Services.ProtocolInformation{}] ++
services
%__MODULE__{
services: all_services |> Enum.map(&HAP.Service.compile/1)
}
end
@doc false
def get_service(%__MODULE__{services: services}, iid) do
with {:ok, service_index} <- HAP.IID.service_index(iid),
%HAP.Service{} = service <- Enum.at(services, service_index) do
{:ok, service}
else
_ -> {:error, -70_409}
end
end
end