Skip to content

Commit

Permalink
wip: Fixing CLI test
Browse files Browse the repository at this point in the history
  • Loading branch information
Jelloeater committed May 22, 2023
1 parent 197e30e commit 4995f57
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 37 deletions.
45 changes: 11 additions & 34 deletions hubitatcontrol/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import typer
from dotenv import load_dotenv

import hubitatcontrol
from hubitatcontrol import Hub

app_name = "hubitatcontrol"
Expand Down Expand Up @@ -41,25 +42,6 @@ def hub_from_keyring():
return get_hub(host=host_env, token=token_env, app_id=app_id_env, cloud_token=cloud_token_env)


def lookup_device(hub_in, device_lookup):
"""
Takes device NAME, not ID for lookup
"""

return get_device_type(
device_in=hub_in.get_device(device_lookup), hub_in=hub_in
) # Fall through return # pragma: no cover


def lookup_device_id(hub_in, device_id):
"""
Takes device ID for lookup
"""
return get_device_type(
device_in=hub_in.get_device_id(device_id), hub_in=hub_in
) # Fall through return # pragma: no cover


@app.command()
def ls():
"""
Expand All @@ -70,18 +52,17 @@ def ls():


@app.command()
def on(device_id: int):
def on(device_id: int) -> hubitatcontrol.lights.Switch:
"""
Turn on a device via it's Device ID
"""

hub_in = hub_from_keyring()
device = hub_in.get_device_id(device_id)
import hubitatcontrol.hub
dev = hubitatcontrol.GetDevices(hub_in).Switch()

dev = lookup_device(hub_in, device['name'])

dev.turn_on()
for i in dev:
if i.id == device_id:
i.turn_on()


@app.command()
Expand All @@ -91,23 +72,19 @@ def off(device_id: int):
"""

hub_in = hub_from_keyring()
device = hub_in.get_device_id(device_id)
import hubitatcontrol.hub

dev = lookup_device(hub_in, device['name'])
dev = hubitatcontrol.GetDevices(hub_in).Switch()

dev.turn_off()
for i in dev:
if i.id == device_id:
i.turn_off()


@app.command()
def level(device_id: int, level: int):
"""Turn on a device via it's Device ID"""

hub_in = hub_from_keyring()
device = hub_in.get_device_id(device_id)
import hubitatcontrol.hub

dev = lookup_device(hub_in, device['name'])
dev = hub_in.get_device_id(device_id)

dev.set_level(level)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ def test_cli_keyring(self):
@pytest.mark.skipif(load_dotenv('.env') is False, reason='No env file found')
def test_cli_on(self):
test_bulb = get_device_of_type("Virtual RGBW Light")
cli.on(test_bulb.id)
cli.on(test_bulb['id'])
3 changes: 1 addition & 2 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import random
import time

from __main__ import lookup_device
from dotenv import load_dotenv

import hubitatcontrol
Expand All @@ -19,7 +18,7 @@ def get_device_of_type(device_type: str):
h = Hub(host=host_env, token=token_env, app_id=app_id_env, cloud_token=cloud_token)
for i in h.devices:
if i["type"] == device_type:
return lookup_device(h, i["label"])
return i


def test_creds():
Expand Down

0 comments on commit 4995f57

Please sign in to comment.