Repository: ikalchev/HAP-python

Issue:
Unable to encode TLV value for camera
When trying to create a camera I'm getting this exception:

```
[hap_server] Exception in set_characteristics: object of type 'int' has no len()
Traceback (most recent call last):
  File "/home/homekit/camera_test/venv/lib/python3.7/site-packages/pyhap/hap_server.py", line 559, in handle_set_characteristics
    self.client_address)
  File "/home/homekit/camera_test/venv/lib/python3.7/site-packages/pyhap/accessory_driver.py", line 660, in set_characteristics
    char.client_update_value(cq[HAP_REPR_VALUE], client_addr)
  File "/home/homekit/camera_test/venv/lib/python3.7/site-packages/pyhap/characteristic.py", line 215, in client_update_value
    self.setter_callback(value)
  File "/home/homekit/camera_test/venv/lib/python3.7/site-packages/pyhap/camera.py", line 694, in set_endpoints
    to_base64=True)
  File "/home/homekit/camera_test/venv/lib/python3.7/site-packages/pyhap/tlv.py", line 27, in encode
    total_length = len(data)
```

I think I can see how this has happened in camera.py as tlv.encode is (I think) expecting to receive bytes, but it's being passed video_ssrc/audo_ssrc which are integers.

```
       video_ssrc = int.from_bytes(os.urandom(3), byteorder="big")
        audio_ssrc = int.from_bytes(os.urandom(3), byteorder="big")

<snip>
        response_tlv = tlv.encode(
            SETUP_TYPES['SESSION_ID'], session_id.bytes,
            SETUP_TYPES['STATUS'], SETUP_STATUS['SUCCESS'],
            SETUP_TYPES['ADDRESS'], res_address_tlv,
            SETUP_TYPES['VIDEO_SRTP_PARAM'], video_srtp_tlv,
            SETUP_TYPES['AUDIO_SRTP_PARAM'], audio_srtp_tlv,
            SETUP_TYPES['VIDEO_SSRC'], video_ssrc,
            SETUP_TYPES['AUDIO_SSRC'], audio_ssrc,
            to_base64=True)
```

This used to work though so I'm a bit confused about what has changed! I'm using python 3.7.5 and HAP-python==2.8.2

Comments:

Patch:
diff --git a/pyhap/camera.py b/pyhap/camera.py
index 5a6fca8..155aad9 100644
--- a/pyhap/camera.py
+++ b/pyhap/camera.py
@@ -682,8 +682,8 @@ class Camera(Accessory):
             SETUP_TYPES['ADDRESS'], res_address_tlv,
             SETUP_TYPES['VIDEO_SRTP_PARAM'], video_srtp_tlv,
             SETUP_TYPES['AUDIO_SRTP_PARAM'], audio_srtp_tlv,
-            SETUP_TYPES['VIDEO_SSRC'], video_ssrc,
-            SETUP_TYPES['AUDIO_SSRC'], audio_ssrc,
+            SETUP_TYPES['VIDEO_SSRC'], struct.pack('<I', video_ssrc),
+            SETUP_TYPES['AUDIO_SSRC'], struct.pack('<I', audio_ssrc),
             to_base64=True)
 
         self.sessions[session_id] = {
diff --git a/pyhap/hap_server.py b/pyhap/hap_server.py
index ae863e5..abef1be 100644
--- a/pyhap/hap_server.py
+++ b/pyhap/hap_server.py
@@ -893,6 +893,8 @@ class HAPServer(socketserver.ThreadingMixIn,
         except (OSError, socket.timeout) as e:
             self._handle_sock_timeout(client_address, e)
             logger.debug('Connection timeout')
+        except Exception as e:
+            logger.debug('finish_request: %s', e, exc_info=True)
         finally:
             logger.debug('Cleaning connection to %s', client_address)
             conn_sock = self.connections.pop(client_address, None)


Test patch:
diff --git a/tests/test_camera.py b/tests/test_camera.py
index f3424d7..c25fa88 100644
--- a/tests/test_camera.py
+++ b/tests/test_camera.py
@@ -76,7 +76,7 @@ def test_setup_endpoints(mock_driver):
                          .get_characteristic('SetupEndpoints')
     setup_endpoints.client_update_value(set_endpoint_req)
 
-    assert setup_endpoints.get_value() == set_endpoint_res
+    assert setup_endpoints.get_value()[:171] == set_endpoint_res[:171]
 
 
 def test_set_selected_stream_start_stop(mock_driver):