This repository has been archived by the owner on May 9, 2024. It is now read-only.
/
room.py
107 lines (88 loc) · 2.76 KB
/
room.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"""Defines the rooms module."""
from typing import Dict, Optional
from .api import Api
from .entity import Entity
class Room:
"""Defines a SmartThings room."""
def __init__(self):
"""Initialize the room."""
self._room_id = None
self._location_id = None
self._name = None
self._background_image = None
def apply_data(self, data: dict):
"""Apply the data structure to the class."""
self._room_id = data["roomId"]
self._location_id = data["locationId"]
self._name = data["name"]
self._background_image = data["backgroundImage"]
def to_data(self) -> dict:
"""Get a data structure representing this entity."""
data = {
"name": self._name,
"backgroundImage": self._background_image,
}
return data
@property
def room_id(self) -> str:
"""Get the id of the room."""
return self._room_id
@room_id.setter
def room_id(self, value: str):
"""Set the id of the room."""
self._room_id = value
@property
def location_id(self) -> str:
"""Get the location id the room is part of."""
return self._location_id
@location_id.setter
def location_id(self, value: str):
"""Set the location id of the room."""
self._location_id = value
@property
def name(self) -> str:
"""Get the name of the room."""
return self._name
@name.setter
def name(self, value: str):
"""Set the name of the room."""
self._name = value
@property
def background_image(self) -> str:
"""Get the background image of the room."""
return self._background_image
@background_image.setter
def background_image(self, value: str):
"""Set the background image."""
self._background_image = value
class RoomEntity(Room, Entity):
"""Defines a SmartThings room entity."""
def __init__(
self,
api: Api,
data: Optional[Dict] = None,
*,
location_id: str = None,
room_id: str = None
):
"""Initialize the room."""
Entity.__init__(self, api)
Room.__init__(self)
if data:
self.apply_data(data)
if location_id:
self._location_id = location_id
if room_id:
self._room_id = room_id
async def refresh(self):
"""Refresh the room."""
data = await self._api.get_room(self._location_id, self._room_id)
if data:
self.apply_data(data)
async def save(self):
"""Save changes to the room."""
data = await self._api.update_room(
self._location_id, self._room_id, self.to_data()
)
if data:
self.apply_data(data)