Skip to content

Commit

Permalink
Expose hardware name in the Device (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
leikoilja authored Mar 5, 2021
1 parent 2b196a9 commit 0dc0aa2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 28 deletions.
13 changes: 11 additions & 2 deletions glocaltokens/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
HOMEGRAPH_DURATION,
JSON_KEY_DEVICE_NAME,
JSON_KEY_GOOGLE_DEVICE,
JSON_KEY_HARDWARE,
JSON_KEY_IP,
JSON_KEY_LOCAL_AUTH_TOKEN,
JSON_KEY_PORT,
Expand All @@ -41,13 +42,15 @@ def __init__(
google_device: Optional[GoogleDevice] = None,
ip: Optional[str] = None,
port: Optional[int] = None,
hardware: Optional[str] = None,
):
"""
Initializes a Device. Can set or google_device or ip and port
"""
self.device_name = device_name
self.local_auth_token = local_auth_token
self.google_device = google_device
self.hardware = hardware

if not self.local_auth_token:
LOGGER.error("local_auth_token not set")
Expand Down Expand Up @@ -91,8 +94,9 @@ def __str__(self) -> str:
def dict(self) -> Dict[str, Any]:
return {
JSON_KEY_DEVICE_NAME: self.device_name,
JSON_KEY_LOCAL_AUTH_TOKEN: self.local_auth_token,
JSON_KEY_GOOGLE_DEVICE: {JSON_KEY_IP: self.ip, JSON_KEY_PORT: self.port},
JSON_KEY_HARDWARE: self.hardware,
JSON_KEY_LOCAL_AUTH_TOKEN: self.local_auth_token,
}


Expand Down Expand Up @@ -264,7 +268,12 @@ def find_device(name) -> Optional[GoogleDevice]:
find_device(item.device_name) if network_devices else None
)
devices.append(
Device(item.device_name, item.local_auth_token, google_device)
Device(
device_name=item.device_name,
local_auth_token=item.local_auth_token,
google_device=google_device,
hardware=item.hardware.model,
)
)

LOGGER.debug("Google Home devices: {}".format(devices))
Expand Down
1 change: 1 addition & 0 deletions glocaltokens/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

JSON_KEY_DEVICE_NAME = "device_name"
JSON_KEY_GOOGLE_DEVICE = "google_device"
JSON_KEY_HARDWARE = "hardware"
JSON_KEY_IP = "ip"
JSON_KEY_LOCAL_AUTH_TOKEN = "local_auth_token"
JSON_KEY_PORT = "port"
63 changes: 37 additions & 26 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
HOMEGRAPH_DURATION,
JSON_KEY_DEVICE_NAME,
JSON_KEY_GOOGLE_DEVICE,
JSON_KEY_HARDWARE,
JSON_KEY_IP,
JSON_KEY_LOCAL_AUTH_TOKEN,
JSON_KEY_PORT,
Expand Down Expand Up @@ -72,7 +73,6 @@ def test_initialization(self):

@patch("glocaltokens.client.LOGGER.error")
def test_initialization__valid(self, m_log):
# --- GLocalAuthenticationTokens initialization --- #
# With username and password
GLocalAuthenticationTokens(username=faker.word(), password=faker.word())
self.assertEqual(m_log.call_count, 0)
Expand All @@ -81,10 +81,6 @@ def test_initialization__valid(self, m_log):
GLocalAuthenticationTokens(master_token=faker.master_token())
self.assertEqual(m_log.call_count, 0)

# --- client.Device initialization --- #
# With ip and port
Device(device_name=faker.word(), local_auth_token=faker.local_auth_token())

@patch("glocaltokens.client.LOGGER.error")
def test_initialization__invalid(self, m_log):
# Without username
Expand All @@ -103,26 +99,6 @@ def test_initialization__invalid(self, m_log):
GLocalAuthenticationTokens(master_token=faker.word())
self.assertEqual(m_log.call_count, 4)

# Invalid local_auth_token
Device(device_name=faker.word(), local_auth_token=faker.word())
self.assertEqual(m_log.call_count, 5)

# With only ip
Device(
device_name=faker.word(),
local_auth_token=faker.local_auth_token(),
ip=faker.ipv4_private(),
)
self.assertEqual(m_log.call_count, 6)

# With only port
Device(
device_name=faker.word(),
local_auth_token=faker.local_auth_token(),
port=faker.port_number(),
)
self.assertEqual(m_log.call_count, 7)

def test_get_android_id(self):
android_id = self.client.get_android_id()
self.assertTrue(len(android_id) == ANDROID_ID_LENGTH)
Expand Down Expand Up @@ -305,6 +281,7 @@ def test_get_google_devices(self, m_get_homegraph):
google_device.local_auth_token, homegraph_device.local_auth_token
)
self.assertEqual(google_device.device_name, homegraph_device.device_name)
self.assertEqual(google_device.hardware, homegraph_device.hardware.model)

# With many devices returned from homegraph
homegraph_devices = faker.google_devices()
Expand All @@ -320,8 +297,13 @@ def test_get_google_devices_json(self, m_get_google_devices):
local_auth_token = faker.local_auth_token()
ip = faker.ipv4()
port = faker.port_number()
hardware = faker.word()
google_device = Device(
device_name=device_name, local_auth_token=local_auth_token, ip=ip, port=port
device_name=device_name,
local_auth_token=local_auth_token,
ip=ip,
port=port,
hardware=hardware,
)
m_get_google_devices.return_value = [google_device]

Expand All @@ -331,6 +313,35 @@ def test_get_google_devices_json(self, m_get_google_devices):
received_json = json.loads(json_string)
received_device = received_json[0]
self.assertEqual(received_device[JSON_KEY_DEVICE_NAME], device_name)
self.assertEqual(received_device[JSON_KEY_HARDWARE], hardware)
self.assertEqual(received_device[JSON_KEY_LOCAL_AUTH_TOKEN], local_auth_token)
self.assertEqual(received_device[JSON_KEY_GOOGLE_DEVICE][JSON_KEY_PORT], port)
self.assertEqual(received_device[JSON_KEY_GOOGLE_DEVICE][JSON_KEY_IP], ip)


class DeviceClientTests(TypeTestMixin, TestCase):
def test_initialization__valid(self):
# With ip and port
Device(device_name=faker.word(), local_auth_token=faker.local_auth_token())

@patch("glocaltokens.client.LOGGER.error")
def test_initialization__invalid(self, m_log):
# With only ip
Device(
device_name=faker.word(),
local_auth_token=faker.local_auth_token(),
ip=faker.ipv4_private(),
)
self.assertEqual(m_log.call_count, 1)

# With only port
Device(
device_name=faker.word(),
local_auth_token=faker.local_auth_token(),
port=faker.port_number(),
)
self.assertEqual(m_log.call_count, 2)

# Invalid local_auth_token
Device(device_name=faker.word(), local_auth_token=faker.word())
self.assertEqual(m_log.call_count, 3)

0 comments on commit 0dc0aa2

Please sign in to comment.