Skip to content

Commit

Permalink
refactor #1
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusmotea committed Sep 16, 2024
1 parent 0416ef1 commit 269974a
Show file tree
Hide file tree
Showing 17 changed files with 1,147 additions and 897 deletions.
562 changes: 562 additions & 0 deletions BridgeEmulator/HueObjects/Device.py

Large diffs are not rendered by default.

120 changes: 50 additions & 70 deletions BridgeEmulator/HueObjects/Group.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@
class Group():

def __init__(self, data):
self.name = data["name"] if "name" in data else "Group " + \
data["id_v1"]
self.name = data["name"] if "name" in data else "Group " + data["id_v1"]
self.id_v1 = data["id_v1"]
self.id_v2 = data["id_v2"] if "id_v2" in data else genV2Uuid()
if "owner" in data:
self.owner = data["owner"]
self.icon_class = data["class"] if "class" in data else "Other"
self.lights = []
self.action = {"on": False, "bri": 100, "hue": 0, "sat": 254, "effect": "none", "xy": [
Expand All @@ -31,15 +28,17 @@ def __init__(self, data):
}
StreamEvent(streamMessage)

def groupZeroStream(self, rooms, lights):
def groupZeroStream(self, groups, lights):
streamMessage = {"creationtime": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
"data": [{"children": [], "id": str(uuid.uuid5(uuid.NAMESPACE_URL, self.id_v2 + 'bridge_home')), "id_v1":"/groups/0", "type": "bridge_home"}],
"id": str(uuid.uuid4()),
"type": "update"
}
for room in rooms:
streamMessage["data"][0]["children"].append(
{"rid": room, "rtype": "room"})
"data": [{"children": [], "id": str(uuid.uuid5(uuid.NAMESPACE_URL, self.id_v2 + 'bridge_home')), "id_v1":"/groups/0", "type": "bridge_home"}],
"id": str(uuid.uuid4()),
"type": "update"
}
for group in groups:
if group.type == "Room":
streamMessage["data"][0]["children"].append({"rid": group.getV2Room()["id"], "rtype": "room"})
elif group.type == "Zone":
streamMessage["data"][0]["children"].append({"rid": group.getV2Zone()["id"], "rtype": "zone"})
for light in lights:
streamMessage["data"][0]["children"].append(
{"rid": light, "rtype": "light"})
Expand Down Expand Up @@ -86,11 +85,12 @@ def add_light(self, light):
StreamEvent(streamMessage)
groupChildrens = []
groupServices = []
for light in self.lights:
if light():
for device in self.lights:
if device() and device().group_v1 == "lights":
light = device().firstElement()
groupChildrens.append(
{"rid": light().getDevice()["id"], "rtype": "device"})
groupServices.append({"rid": light().id_v2, "rtype": "light"})
{"rid": device().getDevice()["id"], "rtype": "device"})
groupServices.append({"rid": light.id_v2, "rtype": "light"})
groupServices.append({"rid": self.id_v2, "rtype": "grouped_light"})
streamMessage = {"creationtime": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
"data": [{"children": groupChildrens, "id": elementId, "id_v1": "/groups/" + self.id_v1, "services": groupServices, "type": elementType}],
Expand All @@ -99,9 +99,6 @@ def add_light(self, light):
}
StreamEvent(streamMessage)

def add_sensor(self, sensor):
self.sensors.append(weakref.ref(sensor))

def update_attr(self, newdata):
if "lights" in newdata: # update of the lights must be done using add_light function
del newdata["lights"]
Expand Down Expand Up @@ -133,11 +130,12 @@ def update_state(self):
lights_on = 0
if len(self.lights) == 0:
all_on = False
for light in self.lights:
if light():
if light().state["on"]:
for device in self.lights:
if device() and device().group_v1 == "lights":
light = device().firstElement()
if light.state["on"]:
any_on = True
bri = bri + light().state["bri"]
bri = bri + light.state["bri"]
lights_on = lights_on + 1
else:
all_on = False
Expand All @@ -156,10 +154,11 @@ def setV1Action(self, state, scene=None):
self.genStreamEvent(v2State)

def genStreamEvent(self, v2State):
for light in self.lights:
if light():
for device in self.lights:
if device() and device().group_v1 == "lights":
light = device().firstElement()
streamMessage = {"creationtime": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
"data": [{"id": light().id_v2, "id_v1": "/lights/" + light().id_v1, "owner": {"rid": light().getDevice()["id"], "rtype":"device"}, "type": "light"}],
"data": [{"id": light.id_v2, "id_v1": "/lights/" + light.id_v1, "owner": {"rid": device().getDevice()["id"], "rtype":"device"}, "type": "light"}],
"id": str(uuid.uuid4()),
"type": "update"
}
Expand All @@ -182,16 +181,14 @@ def genStreamEvent(self, v2State):
def getV1Api(self):
result = {}
result["name"] = self.name
if hasattr(self, "owner"):
result["owner"] = self.owner.username
lights = []
for light in self.lights:
if light():
lights.append(light().id_v1)
sensors = []
for sensor in self.sensors:
if sensor():
sensors.append(sensor().id_v1)
for device in self.lights:
if device():
if device().group_v1 == "lights":
lights.append(device().firstElement().id_v1)
elif device().group_v1 == "sensors":
sensors.append(device().firstElement().id_v1)
result["lights"] = lights
result["sensors"] = sensors
result["type"] = self.type.capitalize()
Expand All @@ -209,31 +206,18 @@ def getV1Api(self):

def getV2Room(self):
result = {"children": [], "services": []}
for light in self.lights:
if light():
for device in self.lights:
if device():
result["children"].append({
"rid": str(uuid.uuid5(
uuid.NAMESPACE_URL, light().id_v2 + 'device')),
"rid": device().id_v2,
"rtype": "device"
})

#result["grouped_services"].append({
# "rid": self.id_v2,
# "rtype": "grouped_light"
#})
result["id"] = str(uuid.uuid5(uuid.NAMESPACE_URL, self.id_v2 + 'room'))
result["id_v1"] = "/groups/" + self.id_v1
result["metadata"] = {
"archetype": self.icon_class.replace(" ", "_").replace("'", "").lower(),
"name": self.name
}
for light in self.lights:
if light():
result["services"].append({
"rid": light().id_v2,
"rtype": "light"
})

result["services"].append({
"rid": self.id_v2,
"rtype": "grouped_light"
Expand All @@ -244,27 +228,22 @@ def getV2Room(self):

def getV2Zone(self):
result = {"children": [], "services": []}
for light in self.lights:
if light():
for device in self.lights:
if device() and device().group_v1 == "lights":
result["children"].append({
"rid": light().id_v2,
"rid": device().firstElement().id_v2,
"rtype": "light"
})

#result["grouped_services"].append({
# "rid": self.id_v2,
# "rtype": "grouped_light"
#})
result["id"] = str(uuid.uuid5(uuid.NAMESPACE_URL, self.id_v2 + 'zone'))
result["id_v1"] = "/groups/" + self.id_v1
result["metadata"] = {
"archetype": self.icon_class.replace(" ", "_").replace("'", "").lower(),
"name": self.name
}
for light in self.lights:
if light():
for device in self.lights:
if device() and device().group_v1 == "lights":
result["services"].append({
"rid": light().id_v2,
"rid": device().firstElement().id_v2,
"rtype": "light"
})

Expand All @@ -290,11 +269,13 @@ def getV2GroupedLight(self):
result["id"] = self.id_v2
result["id_v1"] = "/groups/" + self.id_v1
result["on"] = {"on": self.update_state()["any_on"]}
if self.id_v1 == 0:
result["owner"] = {"rid": str(uuid.uuid5(uuid.NAMESPACE_URL, self.id_v2 + 'bridge_home')), "rtype": "bridge_home"}
elif self.type == "Room":
result["owner"] = {"rid": self.getV2Room()["id"], "rtype": "room"}
elif self.type == "Zone":
result["owner"] = {"rid": self.getV2Zone()["id"], "rtype": "zone"}
result["type"] = "grouped_light"
if hasattr(self, "owner"):
result["owner"] = {"rid": self.owner.username, "rtype": "device"}
else:
result["owner"] = {"rid": self.id_v2, "rtype": "device"}
result["signaling"] = {"signal_values": [
"no_signal",
"on_off"]}
Expand All @@ -307,9 +288,8 @@ def getObjectPath(self):
def save(self):
result = {"id_v2": self.id_v2, "name": self.name, "class": self.icon_class,
"lights": [], "action": self.action, "type": self.type}
if hasattr(self, "owner"):
result["owner"] = self.owner.username
for light in self.lights:
if light():
result["lights"].append(light().id_v1)
if self.id_v1 != "0":
for device in self.lights:
if device():
result["lights"].append(device().id_v2)
return result
Loading

0 comments on commit 269974a

Please sign in to comment.