Skip to content

Commit a0c0737

Browse files
committed
Merge remote-tracking branch 'origin/valve' into valve_rebased
2 parents 90e41f5 + 1a1d5c6 commit a0c0737

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

examples/valve/controller/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ To compile the chip-repl, from the root of the chip tree:
3939
. scripts/activate.sh
4040
./scripts/build_python.sh -i out/pyenv
4141
source out/pyenv/activate
42-
out/pyenv/chip-repl --
42+
out/pyenv/bin/chip-repl
4343
```
4444

4545
The chip-repl is a shell that lets you directly call python functions. It
@@ -60,7 +60,7 @@ from chip import ChipDeviceCtrl
6060
await devCtrl.CommissionOnNetwork(nodeId=1, setupPinCode=20202021, filterType=ChipDeviceCtrl.DiscoveryFilterType.LONG_DISCRIMINATOR, filter=3840)
6161
```
6262

63-
### Interacting with teh valve app
63+
### Interacting with the valve app
6464

6565
To create a drinks machine controller:
6666

examples/valve/controller/__init__.py

+62
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
import random
2+
13
from enum import StrEnum
24
from chip import ChipDeviceCtrl
35
from chip.clusters.Types import NullValue
46
import chip.clusters as Clusters
57

68

9+
MYSTERY_DRINK = "mystery mirrorball"
10+
11+
712
class Bottle(StrEnum):
813
kBourbon = "bourbon"
914
kGin = "gin"
@@ -21,6 +26,12 @@ def time(self):
2126
# One oz == approx 12s.
2227
return self.oz * 12
2328

29+
def __eq__(self, other):
30+
return self.oz == other.oz
31+
32+
def __lt__(self, other):
33+
return self.oz < other.oz
34+
2435

2536
class DrinkMachine:
2637
def __init__(self, devCtrl: ChipDeviceCtrl, node_id: int):
@@ -39,6 +50,11 @@ def __init__(self, devCtrl: ChipDeviceCtrl, node_id: int):
3950
self.add_recipe("old fashioned", {Bottle.kBourbon: Oz(2), Bottle.kSimpleSyrup: Oz(0.125)})
4051
self.add_recipe("shot of bourbon", {Bottle.kBourbon: Oz(1.5)})
4152
self.add_recipe("shot of gin", {Bottle.kGin: Oz(1.5)})
53+
self.add_recipe("gin sour", {Bottle.kGin: Oz(2), Bottle.kSourMix: Oz(1), Bottle.kSimpleSyrup: Oz(0.5)})
54+
self.add_recipe("manhattan", {Bottle.kBourbon: Oz(2), Bottle.kVermouth: Oz(1)})
55+
self.add_recipe("campari sour", {Bottle.kGin: Oz(1), Bottle.kCampari: Oz(1),
56+
Bottle.kSourMix: Oz(0.75), Bottle.kSimpleSyrup: Oz(0.5)})
57+
self.add_recipe(MYSTERY_DRINK, {})
4258

4359
def set_bottle_names(self, bottles: dict[Bottle, int]) -> bool:
4460
''' Bottle is a dict of bottle name to endpoint and should contain all 6 endpoints at once'''
@@ -56,7 +72,50 @@ def add_recipe(self, name: str, ingredients: dict[Bottle, Oz]):
5672
# TODO: should store somewhere permanent - simplest is to write out to file. In the meanwhile, we have a few pre-populated
5773
self.recipes[name] = ingredients
5874

75+
async def _dispense_mystery(self):
76+
num_ingredients = random.randint(1, 3)
77+
bottles = set()
78+
choice = random.choice(list(Bottle))
79+
bottles.add(choice)
80+
while len(bottles) < num_ingredients:
81+
# If this matches something in the bottle list already, it won't be added
82+
choice = random.choice(list(Bottle))
83+
bottles.add(choice)
84+
85+
# we're going to aim for a 2.5 Oz shot with min 0.5 shot sizes
86+
goal = 2.5
87+
print(f"Creating your mystery beverage:")
88+
ingredients = {}
89+
total = 0
90+
for bottle in bottles:
91+
remaining = num_ingredients - len(ingredients.keys())
92+
if remaining == 1:
93+
oz = goal-total
94+
else:
95+
max_oz = goal - total - 0.5*(remaining-1)
96+
# multiply by 2 because randrange likes ints
97+
oz = random.randrange(1, max_oz*2, 1)/2
98+
total += oz
99+
ingredients[bottle] = Oz(oz)
100+
print(f"\t{oz} Oz of {bottle}")
101+
102+
largest = max(ingredients, key=ingredients.get)
103+
mystery_prefix = ['', 'giant', 'potent', 'disco-tastic', 'shiny', 'Dr.']
104+
mystery_suffix = ['blaster', 'concoction', 'blend', 'cocktail']
105+
mystery_extra_suffix = ['', 'jr', 'of wonder', 'esq']
106+
name = []
107+
name.append(random.choice(mystery_prefix))
108+
name.append(largest)
109+
name.append(random.choice(mystery_suffix))
110+
name.append(random.choice(mystery_extra_suffix))
111+
print(f'The {" ".join(name).strip()}')
112+
print('Please enjoy responsibly.')
113+
await self._dispense_ingredients(ingredients)
114+
59115
async def dispense(self, recipe: str):
116+
if recipe == MYSTERY_DRINK:
117+
return await self._dispense_mystery()
118+
60119
# TODO: be a bit nicer on the comparison here. Strings as keys aren't great, but I want the flexibility to add non-standard recipes
61120
if recipe not in self.recipes.keys():
62121
print(f"Unable to find the specified recipe. Available Recipes: {self.recipes.keys()}")
@@ -69,6 +128,9 @@ async def dispense(self, recipe: str):
69128
return
70129

71130
ingredients = self.recipes[recipe]
131+
self._dispense_ingredients(ingredients)
132+
133+
async def _dispense_ingredients(self, ingredients: dict[Bottle, Oz]):
72134
for bottle, amount in ingredients.items():
73135
ep = self.bottles[bottle]
74136
time = amount.time()

0 commit comments

Comments
 (0)