parker-aiohomekit 4.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. parker_aiohomekit-4.0.1/PKG-INFO +138 -0
  2. parker_aiohomekit-4.0.1/README.md +105 -0
  3. parker_aiohomekit-4.0.1/aiohomekit/__init__.py +1 -0
  4. parker_aiohomekit-4.0.1/aiohomekit/__main__.py +669 -0
  5. parker_aiohomekit-4.0.1/aiohomekit/const.py +45 -0
  6. parker_aiohomekit-4.0.1/aiohomekit/controller/abstract/__init__.py +7 -0
  7. parker_aiohomekit-4.0.1/aiohomekit/controller/abstract/controller.py +202 -0
  8. parker_aiohomekit-4.0.1/aiohomekit/controller/abstract/discovery.py +53 -0
  9. parker_aiohomekit-4.0.1/aiohomekit/controller/abstract/pairing.py +370 -0
  10. parker_aiohomekit-4.0.1/aiohomekit/controller/ble/__init__.py +23 -0
  11. parker_aiohomekit-4.0.1/aiohomekit/controller/ble/bleak.py +175 -0
  12. parker_aiohomekit-4.0.1/aiohomekit/controller/ble/client.py +276 -0
  13. parker_aiohomekit-4.0.1/aiohomekit/controller/ble/connection.py +61 -0
  14. parker_aiohomekit-4.0.1/aiohomekit/controller/ble/const.py +41 -0
  15. parker_aiohomekit-4.0.1/aiohomekit/controller/ble/controller.py +219 -0
  16. parker_aiohomekit-4.0.1/aiohomekit/controller/ble/discovery.py +239 -0
  17. parker_aiohomekit-4.0.1/aiohomekit/controller/ble/key.py +57 -0
  18. parker_aiohomekit-4.0.1/aiohomekit/controller/ble/manufacturer_data.py +123 -0
  19. parker_aiohomekit-4.0.1/aiohomekit/controller/ble/pairing.py +1790 -0
  20. parker_aiohomekit-4.0.1/aiohomekit/controller/ble/structs.py +374 -0
  21. parker_aiohomekit-4.0.1/aiohomekit/controller/ble/values.py +58 -0
  22. parker_aiohomekit-4.0.1/aiohomekit/controller/relay/__init__.py +1 -0
  23. parker_aiohomekit-4.0.1/aiohomekit/controller/relay/controller.py +241 -0
  24. parker_aiohomekit-4.0.1/aiohomekit/controller/zeroconf/coap/__init__.py +23 -0
  25. parker_aiohomekit-4.0.1/aiohomekit/controller/zeroconf/coap/connection.py +731 -0
  26. parker_aiohomekit-4.0.1/aiohomekit/controller/zeroconf/coap/controller.py +35 -0
  27. parker_aiohomekit-4.0.1/aiohomekit/controller/zeroconf/coap/discovery.py +81 -0
  28. parker_aiohomekit-4.0.1/aiohomekit/controller/zeroconf/coap/pairing.py +245 -0
  29. parker_aiohomekit-4.0.1/aiohomekit/controller/zeroconf/coap/pdu.py +120 -0
  30. parker_aiohomekit-4.0.1/aiohomekit/controller/zeroconf/coap/structs.py +389 -0
  31. parker_aiohomekit-4.0.1/aiohomekit/controller/zeroconf/controller.py +280 -0
  32. parker_aiohomekit-4.0.1/aiohomekit/controller/zeroconf/ip/__init__.py +19 -0
  33. parker_aiohomekit-4.0.1/aiohomekit/controller/zeroconf/ip/connection.py +821 -0
  34. parker_aiohomekit-4.0.1/aiohomekit/controller/zeroconf/ip/controller.py +35 -0
  35. parker_aiohomekit-4.0.1/aiohomekit/controller/zeroconf/ip/discovery.py +127 -0
  36. parker_aiohomekit-4.0.1/aiohomekit/controller/zeroconf/ip/pairing.py +566 -0
  37. parker_aiohomekit-4.0.1/aiohomekit/controller/zeroconf/pairing.py +48 -0
  38. parker_aiohomekit-4.0.1/aiohomekit/controller/zeroconf/protocol.py +110 -0
  39. parker_aiohomekit-4.0.1/aiohomekit/crypto/__init__.py +36 -0
  40. parker_aiohomekit-4.0.1/aiohomekit/crypto/chacha20poly1305.py +124 -0
  41. parker_aiohomekit-4.0.1/aiohomekit/crypto/hkdf.py +32 -0
  42. parker_aiohomekit-4.0.1/aiohomekit/crypto/srp.py +310 -0
  43. parker_aiohomekit-4.0.1/aiohomekit/enum.py +34 -0
  44. parker_aiohomekit-4.0.1/aiohomekit/exceptions.py +302 -0
  45. parker_aiohomekit-4.0.1/aiohomekit/hkjson.py +71 -0
  46. parker_aiohomekit-4.0.1/aiohomekit/http/__init__.py +67 -0
  47. parker_aiohomekit-4.0.1/aiohomekit/http/response.py +147 -0
  48. parker_aiohomekit-4.0.1/aiohomekit/meshcop.py +73 -0
  49. parker_aiohomekit-4.0.1/aiohomekit/model/accessories/__init__.py +3 -0
  50. parker_aiohomekit-4.0.1/aiohomekit/model/accessories/accessories.py +86 -0
  51. parker_aiohomekit-4.0.1/aiohomekit/model/accessories/accessory.py +207 -0
  52. parker_aiohomekit-4.0.1/aiohomekit/model/accessories/accessory_state.py +29 -0
  53. parker_aiohomekit-4.0.1/aiohomekit/model/categories.py +54 -0
  54. parker_aiohomekit-4.0.1/aiohomekit/model/characteristics/__init__.py +46 -0
  55. parker_aiohomekit-4.0.1/aiohomekit/model/characteristics/characteristic.py +520 -0
  56. parker_aiohomekit-4.0.1/aiohomekit/model/characteristics/characteristic_formats.py +70 -0
  57. parker_aiohomekit-4.0.1/aiohomekit/model/characteristics/characteristic_key.py +14 -0
  58. parker_aiohomekit-4.0.1/aiohomekit/model/characteristics/characteristic_types.py +429 -0
  59. parker_aiohomekit-4.0.1/aiohomekit/model/characteristics/const.py +287 -0
  60. parker_aiohomekit-4.0.1/aiohomekit/model/characteristics/data.py +936 -0
  61. parker_aiohomekit-4.0.1/aiohomekit/model/characteristics/permissions.py +31 -0
  62. parker_aiohomekit-4.0.1/aiohomekit/model/characteristics/structs.py +158 -0
  63. parker_aiohomekit-4.0.1/aiohomekit/model/characteristics/types.py +2 -0
  64. parker_aiohomekit-4.0.1/aiohomekit/model/characteristics/units.py +40 -0
  65. parker_aiohomekit-4.0.1/aiohomekit/model/discovery_info.py +17 -0
  66. parker_aiohomekit-4.0.1/aiohomekit/model/feature_flags.py +22 -0
  67. parker_aiohomekit-4.0.1/aiohomekit/model/services/__init__.py +3 -0
  68. parker_aiohomekit-4.0.1/aiohomekit/model/services/data.py +516 -0
  69. parker_aiohomekit-4.0.1/aiohomekit/model/services/service.py +289 -0
  70. parker_aiohomekit-4.0.1/aiohomekit/model/services/service_types.py +121 -0
  71. parker_aiohomekit-4.0.1/aiohomekit/model/services/types.py +2 -0
  72. parker_aiohomekit-4.0.1/aiohomekit/model/status_flags.py +24 -0
  73. parker_aiohomekit-4.0.1/aiohomekit/model/transport_type.py +17 -0
  74. parker_aiohomekit-4.0.1/aiohomekit/model/typed_dicts.py +96 -0
  75. parker_aiohomekit-4.0.1/aiohomekit/pdu.py +151 -0
  76. parker_aiohomekit-4.0.1/aiohomekit/protocol/__init__.py +611 -0
  77. parker_aiohomekit-4.0.1/aiohomekit/protocol/statuscodes.py +83 -0
  78. parker_aiohomekit-4.0.1/aiohomekit/protocol/tlv.py +277 -0
  79. parker_aiohomekit-4.0.1/aiohomekit/py.typed +0 -0
  80. parker_aiohomekit-4.0.1/aiohomekit/storage/characteristics_storage.py +20 -0
  81. parker_aiohomekit-4.0.1/aiohomekit/storage/pairing_data_storage.py +17 -0
  82. parker_aiohomekit-4.0.1/aiohomekit/storage/storage.py +101 -0
  83. parker_aiohomekit-4.0.1/aiohomekit/testing/accessoryserver.py +1633 -0
  84. parker_aiohomekit-4.0.1/aiohomekit/testing/fakes.py +429 -0
  85. parker_aiohomekit-4.0.1/aiohomekit/testing/mock_zeroconf.py +102 -0
  86. parker_aiohomekit-4.0.1/aiohomekit/testing/utils.py +36 -0
  87. parker_aiohomekit-4.0.1/aiohomekit/tlv8.py +338 -0
  88. parker_aiohomekit-4.0.1/aiohomekit/utils.py +129 -0
  89. parker_aiohomekit-4.0.1/aiohomekit/uuid.py +72 -0
  90. parker_aiohomekit-4.0.1/pyproject.toml +109 -0
@@ -0,0 +1,138 @@
1
+ Metadata-Version: 2.4
2
+ Name: parker-aiohomekit
3
+ Version: 4.0.1
4
+ Summary: An asyncio HomeKit client
5
+ License: Apache-2.0
6
+ Keywords: HomeKit,home,automation,aiohomekit
7
+ Author: Mark Parker
8
+ Author-email: mark@parker-programs.com
9
+ Requires-Python: >=3.12,<3.14
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: Intended Audience :: End Users/Desktop
12
+ Classifier: License :: OSI Approved :: Apache Software License
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Classifier: Programming Language :: Python :: 3.13
16
+ Classifier: Topic :: Home Automation
17
+ Requires-Dist: aiocoap (>=0.4.5)
18
+ Requires-Dist: aiohappyeyeballs (>=2.3.0)
19
+ Requires-Dist: async-interrupt (>=1.1.1)
20
+ Requires-Dist: async-timeout (>=4.0.2)
21
+ Requires-Dist: bleak (>=0.22.0)
22
+ Requires-Dist: bleak-retry-connector (>=2.9.0)
23
+ Requires-Dist: chacha20poly1305 (>=0.0.3,<0.0.4)
24
+ Requires-Dist: chacha20poly1305-reuseable (>=0.12.1)
25
+ Requires-Dist: commentjson (>=0.9.0,<0.10.0)
26
+ Requires-Dist: cryptography (>=2.9.2)
27
+ Requires-Dist: orjson (>=3.7.8)
28
+ Requires-Dist: zeroconf (>=0.132.2)
29
+ Project-URL: Homepage, https://github.com/ParkerIndustries/parker-aiohomekit
30
+ Project-URL: Repository, https://github.com/ParkerIndustries/parker-aiohomekit
31
+ Description-Content-Type: text/markdown
32
+
33
+ # aiohomekit by Parker Industries
34
+
35
+ **This library is still in development. No guarantees are made. Features may be incomplete or buggy, and might change over time.**
36
+
37
+ This library implements the HomeKit protocol for controlling Homekit accessories using asyncio.
38
+
39
+ This library is a fork of [Jc2k/aiohomekit](https://github.com/Jc2k/aiohomekit) with major refactoring. It is not backwards compatible with the original library and can't be used as a drop-in replacement.
40
+
41
+ It's primary use is for [MajorDom](https://majordom.io), so python version and code standards are the same as at MajorDom.
42
+
43
+ ## Changes
44
+
45
+ ### Notes to adapt the new API:
46
+
47
+ - storage mechanisms (for pairing data and characteristics cache) have been completed to automatically load and save all data, and delegated from controllers to separate injected Storage classes, which provide interface for CRUD operations and manage data persistence
48
+ - Pairings and Discoveries no longer have a reference to the controller
49
+ - finish_pairing returns PairingData, which can be used to instantiate a Pairing
50
+ - async methods marked with `async` keyword no longer have `async_` prefix
51
+ - some methods have been renamed for clarity
52
+ - arguments for some methods have been changed to correspond to the abstract classes
53
+ - redundant methods and arguments have been removed
54
+ - files have been restructured to improve organization and maintainability, which may require changes in imports
55
+ - to try the library or view the examples, check the renewed `__main__.py`
56
+ - for other details, check the declaration of the classes and methods, it's pretty self-explanatory now
57
+
58
+ ### Other changes include:
59
+ - added more type hints, comments, and more clear names
60
+ - fixed bugs related to data persistence and storage management
61
+ - fixed saving of updated pairing data when DHCP changes the IP address of the accessory
62
+
63
+ ## Roadmap
64
+
65
+ - [x] IP discovery, pairing, and control
66
+ - [x] Test IP stack
67
+ - [ ] Update code QA tools
68
+ - [ ] BLE discovery
69
+ - [ ] BLE pairing and provisioning of WiFi and Thread credentials
70
+ - [ ] Device control over BLE and COAP (Thread))
71
+ - [ ] Tests for the features above
72
+ - [ ] Fix all errors and warnings from linters and type checkers (most of them due to ignoring optional types)
73
+ - [ ] Fix all docstrings since they became deprecated before this fork was even created
74
+ - [ ] Update readme, add docs
75
+
76
+ ## Code Quality Analysis (TODO)
77
+
78
+ This projects uses tool stack by astra.sh:
79
+ - `uv` project manager instead of `poetry`
80
+ - `ruff` linter+formatter instead of `black`, `isort`, and `flake8`
81
+ - `ty` static type checker instead of `mypy`
82
+
83
+ ## Contributing (legacy)
84
+
85
+ Lots of users are using this library with devices from a wide array of vendors. As a community open source project we do not have the hardware or time resources to certify every device with multiple vendors projects. We may be conservative about larger changes or changes that are low level. We do ask where possible that any changes should be tested with a certified HomeKit implementations of shipping products, not just against emulators or other uncertified implementations.
86
+
87
+ Please bear in mind that some shipping devices interpret the HAP specification loosely. In general we prefer to match the behaviour of real HAP controllers even where their behaviour is not strictly specified. Here are just some of the kinds of problems we've had to work around:
88
+
89
+ * Despite the precise formatting of JSON being unspecified, there are devices in the wild that cannot handle spaces when parsing JSON. For example, `{"foo": "bar"}` vs `{"foo":"bar"}`. This means we never use a "pretty" encoding of JSON.
90
+ * Despite a boolean being explicitly defined as `0`, `1`, `true` or `false` in the spec, some devices only support 3 of the 4. This means booleans must be encoded as `0` or `1`.
91
+ * Some devices have shown themselves to be sensitive to headers being missing, in the wrong order or if there are extra headers. So we ensure that only the headers iOS sends are present, and that the casing and ordering is the same.
92
+ * Some devices are sensitive to a HTTP message being split into separate TCP packets. So we take care to only write a full message to the network stack.
93
+
94
+ And so on. As a rule we need to be strict about what we send and loose about what we receive.
95
+
96
+ ## Device compatibility (legacy)
97
+
98
+ `aiohomekit` is primarily tested with a Phillips Hue bridge and an Eve Extend bridge. It is known to work to some extent with many more devices though these are not currently explicitly documented anywhere at the moment.
99
+
100
+ You can look at the problems your device has faced in the home-assistant [issues list](https://github.com/home-assistant/core/issues?q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+homekit_controller%22).
101
+
102
+ ## FAQ (legacy, TODO)
103
+
104
+ ### How do I use this?
105
+
106
+ Check the __main__.py file for examples.
107
+
108
+
109
+ ### Does this support BLE accessories?
110
+
111
+ No. Eventually we hope to via [aioble](https://github.com/detectlabs/aioble) which provides an asyncio bluetooth abstraction that works on Linux, macOS and Windows.
112
+
113
+ ### Can i use this to make a homekit accessory?
114
+
115
+ No, this is just the client part. You should use one the of other implementations:
116
+
117
+ * [homekit_python](https://github.com/jlusiardi/homekit_python/) (this is used a lot during aiohomekit development)
118
+ * [HAP-python](https://github.com/ikalchev/HAP-python)
119
+
120
+ ### Why doesn't aiohomekit use library X instead?
121
+
122
+ Where possible aiohomekit uses libraries that are easy to install with pip, are ready available as wheels (including on Raspberry Pi via piwheels), are cross platform (including Windows) and are already used by Home Assistant. They should not introduce hard dependencies on uncommon system libraries. The intention here is to avoid any difficulty in the Home Assistant build process.
123
+
124
+ People are often alarmed at the hand rolled HTTP code and suggest using an existing HTTP library like `aiohttp`. High level HTTP libraries are pretty much a non-starter because:
125
+
126
+ * Of the difficulty of adding in HAP session security without monkey patches.
127
+ * They don't expect responses without requests (i.e. events).
128
+ * As mentioned above, some of these devices are very sensitive. We don't care if your change is compliant with every spec if it still makes a real world device cry. We are not in a position to demand these devices be fixed. So instead we strive for byte-for-byte accuracy on our write path. Any library would need to give us that flexibility.
129
+ * Some parts of the responses are actually not HTTP, even though they look it.
130
+
131
+ We are also just reluctant to make a change that large for something that is working with a lot of devices. There is a big chance of introducing a regression.
132
+
133
+ Of course a working proof of concept (using a popular well maintained library) that has been tested with something like a Tado internet bridge (including events) would be interesting.
134
+
135
+ ## Thanks
136
+
137
+ This library wouldn't have been possible without homekit_python, a synchronous implementation of both the client and server parts of HAP.
138
+
@@ -0,0 +1,105 @@
1
+ # aiohomekit by Parker Industries
2
+
3
+ **This library is still in development. No guarantees are made. Features may be incomplete or buggy, and might change over time.**
4
+
5
+ This library implements the HomeKit protocol for controlling Homekit accessories using asyncio.
6
+
7
+ This library is a fork of [Jc2k/aiohomekit](https://github.com/Jc2k/aiohomekit) with major refactoring. It is not backwards compatible with the original library and can't be used as a drop-in replacement.
8
+
9
+ It's primary use is for [MajorDom](https://majordom.io), so python version and code standards are the same as at MajorDom.
10
+
11
+ ## Changes
12
+
13
+ ### Notes to adapt the new API:
14
+
15
+ - storage mechanisms (for pairing data and characteristics cache) have been completed to automatically load and save all data, and delegated from controllers to separate injected Storage classes, which provide interface for CRUD operations and manage data persistence
16
+ - Pairings and Discoveries no longer have a reference to the controller
17
+ - finish_pairing returns PairingData, which can be used to instantiate a Pairing
18
+ - async methods marked with `async` keyword no longer have `async_` prefix
19
+ - some methods have been renamed for clarity
20
+ - arguments for some methods have been changed to correspond to the abstract classes
21
+ - redundant methods and arguments have been removed
22
+ - files have been restructured to improve organization and maintainability, which may require changes in imports
23
+ - to try the library or view the examples, check the renewed `__main__.py`
24
+ - for other details, check the declaration of the classes and methods, it's pretty self-explanatory now
25
+
26
+ ### Other changes include:
27
+ - added more type hints, comments, and more clear names
28
+ - fixed bugs related to data persistence and storage management
29
+ - fixed saving of updated pairing data when DHCP changes the IP address of the accessory
30
+
31
+ ## Roadmap
32
+
33
+ - [x] IP discovery, pairing, and control
34
+ - [x] Test IP stack
35
+ - [ ] Update code QA tools
36
+ - [ ] BLE discovery
37
+ - [ ] BLE pairing and provisioning of WiFi and Thread credentials
38
+ - [ ] Device control over BLE and COAP (Thread))
39
+ - [ ] Tests for the features above
40
+ - [ ] Fix all errors and warnings from linters and type checkers (most of them due to ignoring optional types)
41
+ - [ ] Fix all docstrings since they became deprecated before this fork was even created
42
+ - [ ] Update readme, add docs
43
+
44
+ ## Code Quality Analysis (TODO)
45
+
46
+ This projects uses tool stack by astra.sh:
47
+ - `uv` project manager instead of `poetry`
48
+ - `ruff` linter+formatter instead of `black`, `isort`, and `flake8`
49
+ - `ty` static type checker instead of `mypy`
50
+
51
+ ## Contributing (legacy)
52
+
53
+ Lots of users are using this library with devices from a wide array of vendors. As a community open source project we do not have the hardware or time resources to certify every device with multiple vendors projects. We may be conservative about larger changes or changes that are low level. We do ask where possible that any changes should be tested with a certified HomeKit implementations of shipping products, not just against emulators or other uncertified implementations.
54
+
55
+ Please bear in mind that some shipping devices interpret the HAP specification loosely. In general we prefer to match the behaviour of real HAP controllers even where their behaviour is not strictly specified. Here are just some of the kinds of problems we've had to work around:
56
+
57
+ * Despite the precise formatting of JSON being unspecified, there are devices in the wild that cannot handle spaces when parsing JSON. For example, `{"foo": "bar"}` vs `{"foo":"bar"}`. This means we never use a "pretty" encoding of JSON.
58
+ * Despite a boolean being explicitly defined as `0`, `1`, `true` or `false` in the spec, some devices only support 3 of the 4. This means booleans must be encoded as `0` or `1`.
59
+ * Some devices have shown themselves to be sensitive to headers being missing, in the wrong order or if there are extra headers. So we ensure that only the headers iOS sends are present, and that the casing and ordering is the same.
60
+ * Some devices are sensitive to a HTTP message being split into separate TCP packets. So we take care to only write a full message to the network stack.
61
+
62
+ And so on. As a rule we need to be strict about what we send and loose about what we receive.
63
+
64
+ ## Device compatibility (legacy)
65
+
66
+ `aiohomekit` is primarily tested with a Phillips Hue bridge and an Eve Extend bridge. It is known to work to some extent with many more devices though these are not currently explicitly documented anywhere at the moment.
67
+
68
+ You can look at the problems your device has faced in the home-assistant [issues list](https://github.com/home-assistant/core/issues?q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+homekit_controller%22).
69
+
70
+ ## FAQ (legacy, TODO)
71
+
72
+ ### How do I use this?
73
+
74
+ Check the __main__.py file for examples.
75
+
76
+
77
+ ### Does this support BLE accessories?
78
+
79
+ No. Eventually we hope to via [aioble](https://github.com/detectlabs/aioble) which provides an asyncio bluetooth abstraction that works on Linux, macOS and Windows.
80
+
81
+ ### Can i use this to make a homekit accessory?
82
+
83
+ No, this is just the client part. You should use one the of other implementations:
84
+
85
+ * [homekit_python](https://github.com/jlusiardi/homekit_python/) (this is used a lot during aiohomekit development)
86
+ * [HAP-python](https://github.com/ikalchev/HAP-python)
87
+
88
+ ### Why doesn't aiohomekit use library X instead?
89
+
90
+ Where possible aiohomekit uses libraries that are easy to install with pip, are ready available as wheels (including on Raspberry Pi via piwheels), are cross platform (including Windows) and are already used by Home Assistant. They should not introduce hard dependencies on uncommon system libraries. The intention here is to avoid any difficulty in the Home Assistant build process.
91
+
92
+ People are often alarmed at the hand rolled HTTP code and suggest using an existing HTTP library like `aiohttp`. High level HTTP libraries are pretty much a non-starter because:
93
+
94
+ * Of the difficulty of adding in HAP session security without monkey patches.
95
+ * They don't expect responses without requests (i.e. events).
96
+ * As mentioned above, some of these devices are very sensitive. We don't care if your change is compliant with every spec if it still makes a real world device cry. We are not in a position to demand these devices be fixed. So instead we strive for byte-for-byte accuracy on our write path. Any library would need to give us that flexibility.
97
+ * Some parts of the responses are actually not HTTP, even though they look it.
98
+
99
+ We are also just reluctant to make a change that large for something that is working with a lot of devices. There is a big chance of introducing a regression.
100
+
101
+ Of course a working proof of concept (using a popular well maintained library) that has been tested with something like a Tado internet bridge (including events) would be interesting.
102
+
103
+ ## Thanks
104
+
105
+ This library wouldn't have been possible without homekit_python, a synchronous implementation of both the client and server parts of HAP.
@@ -0,0 +1 @@
1
+ from aiohomekit.controller.relay import Controller