async def test_light_restore_state(self, mock_http): mock_http.return_value = self.mock_resp light = Light(self.num, ip=self.ip, user=self.user) light.saved_state = self.mock_resp resp = await light.restore_state() mock_http.assert_called_once_with(self.mock_resp) assert mock_http.call_count == 1 assert resp == self.mock_resp
async def test_light_toggle(self, mock_http_get, mock_http_set, power): mock_http_set.return_value = self.mock_resp light = Light(self.num, ip=self.ip, user=self.user) light.on = power resp = await light.toggle() mock_http_set.assert_called_once_with({"on": not power}) assert mock_http_get.call_count == 1 assert mock_http_set.call_count == 1 assert resp == self.mock_resp
def toggle( id: int = typer.Argument(1), ip: str = typer.Option(..., "--ip", "-i", envvar="HUE_BRIDGE_IP"), user: str = typer.Option(..., "--user", "-u", envvar="HUE_BRIDGE_USER"), ): """Toggle the power state of a light""" light = Light(id, ip=ip, user=user) resp = asyncio.run(light.toggle()) console.print(f"[{ip}] Light {id} Toggle:\n{json.dumps(resp, indent=2)}")
def off( id: int = typer.Argument(1), ip: str = typer.Option(..., "--ip", "-i", envvar="HUE_BRIDGE_IP"), user: str = typer.Option(..., "--user", "-u", envvar="HUE_BRIDGE_USER"), ): """Power off a light""" light = Light(id, ip=ip, user=user) resp = asyncio.run(light.power_off()) console.print(f"[{ip}] Light {id} Off:\n{json.dumps(resp, indent=2)}")
def info( id: int = typer.Argument(1), ip: str = typer.Option(..., "--ip", "-i", envvar="HUE_BRIDGE_IP"), user: str = typer.Option(..., "--user", "-u", envvar="HUE_BRIDGE_USER"), ): """List all the information about a Hue Light""" light = Light(id, ip=ip, user=user) resp = asyncio.run(light.get_info()) console.print(f"[{ip}] Light {id}:\n{json.dumps(resp, indent=2)}")
async def test_light_save_state(self, mock_http): mock_http.return_value = self.mock_resp light = Light(self.num, ip=self.ip, user=self.user) resp = await light.save_state() assert mock_http.call_count == 1 assert resp == self.mock_resp assert light.saved_state == self.mock_resp
async def test_light_get_state(self, mock_http): mock_http.return_value = self.mock_resp light = Light(self.num, ip=self.ip, user=self.user) resp = await light.get_state() assert mock_http.call_count == 1 assert resp == self.mock_resp["state"] assert light.state == self.mock_resp["state"] assert light.on == self.mock_resp["state"]["on"]
async def test_light_power(self, mock_http, power, state): mock_http.return_value = self.mock_resp light = Light(self.num, ip=self.ip, user=self.user) power_method = getattr(light, f"power_{power}") resp = await power_method() mock_http.assert_called_once_with({"on": state}) assert mock_http.call_count == 1 assert resp == self.mock_resp
async def test_light_info(self, mock_http): mock_http.return_value = self.mock_resp light = Light(self.num, ip=self.ip, user=self.user) resp = await light.get_info() mock_http.assert_called_once_with( f"http://{self.ip}/api/{self.user}/lights/{self.num}") assert mock_http.call_count == 1 assert resp == self.mock_resp assert light.info == self.mock_resp
def test_light_init(self): light = Light(self.num, ip=self.ip, user=self.user) assert light.id == self.num assert light.ip == self.ip assert light.user == self.user assert light.on is None assert light.info == {} assert light.state == {} assert light.saved_state == {} assert str(light) == f"<Light {self.num}>" assert repr(light) == f"<Light id={self.num} on={None} ip={self.ip}>"
async def test_light_set_state(self, mock_http_get, mock_http_put): mock_http_put.return_value.json = MagicMock( return_value=self.mock_resp) light = Light(self.num, ip=self.ip, user=self.user) resp = await light.set_state(self.mock_resp["state"]) mock_http_put.assert_called_once_with( f"http://{self.ip}/api/{self.user}/lights/{self.num}/state", self.mock_resp["state"], ) assert mock_http_put.call_count == 1 assert mock_http_get.call_count == 1 assert resp == self.mock_resp
async def main(): process = await asyncio.create_subprocess_exec( "log", "stream", "--type", "log", "--style", "compact", "--predicate", EVENT["filter"], stdout=asyncio.subprocess.PIPE, ) light = Light(LIGHT_ID, ip=HUE_BRIDGE_IP, user=HUE_BRIDGE_USER) async for line in process.stdout: data = line.decode() print(f"STDOUT: {data}") await trigger_hue_light(light, data) process.kill() return await process.wait()
def test_light_url(self): light = Light(self.num, ip=self.ip, user=self.user) assert light.url == f"http://{self.ip}/api/{self.user}/lights/{self.num}"