Skip to content

Commit 2def621

Browse files
authored
Add ability to dump a Tile to a dict (#91)
1 parent ca153ca commit 2def621

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

pytile/tile.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Define a Tile object."""
22
from datetime import datetime
33
import logging
4-
from typing import Awaitable, Callable, Optional
4+
from typing import Any, Awaitable, Callable, Dict, Optional
55

66
_LOGGER = logging.getLogger(__name__)
77

@@ -143,6 +143,28 @@ def _save_timestamps(self, tile_data: dict) -> None:
143143
tile_data["result"]["last_tile_state"]["lost_timestamp"] / 1000
144144
)
145145

146+
def as_dict(self) -> Dict[str, Any]:
147+
"""Return dictionary version of this Tile."""
148+
return {
149+
"accuracy": self.accuracy,
150+
"altitude": self.altitude,
151+
"archetype": self.archetype,
152+
"dead": self.dead,
153+
"firmware_version": self.firmware_version,
154+
"hardware_version": self.hardware_version,
155+
"kind": self.kind,
156+
"last_timestamp": self.last_timestamp,
157+
"latitude": self.latitude,
158+
"longitude": self.longitude,
159+
"lost": self.lost,
160+
"lost_timestamp": self.lost_timestamp,
161+
"name": self.name,
162+
"ring_state": self.ring_state,
163+
"uuid": self.uuid,
164+
"visible": self.visible,
165+
"voip_state": self.voip_state,
166+
}
167+
146168
async def async_update(self) -> None:
147169
"""Get the latest measurements from the Tile."""
148170
data = await self._async_request("get", f"tiles/{self.uuid}")

tests/test_tile.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,78 @@ async def test_missing_last_tile_state(aresponses, create_session_response):
165165
assert not tile.longitude
166166

167167

168+
@pytest.mark.asyncio
169+
async def test_tile_as_dict(aresponses, create_session_response):
170+
"""Test dumping a Tile as a dictionary."""
171+
aresponses.add(
172+
"production.tile-api.com",
173+
f"/api/v1/clients/{TILE_CLIENT_UUID}",
174+
"put",
175+
aresponses.Response(
176+
text=load_fixture("create_client_response.json"),
177+
status=200,
178+
headers={"Content-Type": "application/json"},
179+
),
180+
)
181+
aresponses.add(
182+
"production.tile-api.com",
183+
f"/api/v1/clients/{TILE_CLIENT_UUID}/sessions",
184+
"post",
185+
aresponses.Response(
186+
text=json.dumps(create_session_response),
187+
status=200,
188+
headers={"Content-Type": "application/json"},
189+
),
190+
)
191+
aresponses.add(
192+
"production.tile-api.com",
193+
"/api/v1/tiles/tile_states",
194+
"get",
195+
aresponses.Response(
196+
text=load_fixture("tile_states_response.json"),
197+
status=200,
198+
headers={"Content-Type": "application/json"},
199+
),
200+
)
201+
aresponses.add(
202+
"production.tile-api.com",
203+
f"/api/v1/tiles/{TILE_TILE_UUID}",
204+
"get",
205+
aresponses.Response(
206+
text=load_fixture("tile_details_response.json"),
207+
status=200,
208+
headers={"Content-Type": "application/json"},
209+
),
210+
)
211+
212+
async with aiohttp.ClientSession() as session:
213+
api = await async_login(
214+
TILE_EMAIL, TILE_PASSWORD, session, client_uuid=TILE_CLIENT_UUID
215+
)
216+
tiles = await api.async_get_tiles()
217+
assert len(tiles) == 1
218+
tile = tiles[TILE_TILE_UUID]
219+
assert tile.as_dict() == {
220+
"accuracy": 13.496111,
221+
"altitude": 0.4076319168123,
222+
"archetype": "WALLET",
223+
"dead": False,
224+
"firmware_version": "01.12.14.0",
225+
"hardware_version": "02.09",
226+
"kind": "TILE",
227+
"last_timestamp": datetime(2020, 8, 12, 17, 55, 26),
228+
"latitude": 51.528308,
229+
"longitude": -0.3817765,
230+
"lost": False,
231+
"lost_timestamp": datetime(1969, 12, 31, 23, 59, 59, 999000),
232+
"name": "Wallet",
233+
"ring_state": "STOPPED",
234+
"uuid": "19264d2dffdbca32",
235+
"visible": True,
236+
"voip_state": "OFFLINE",
237+
}
238+
239+
168240
@pytest.mark.asyncio
169241
async def test_tile_update(
170242
aresponses, create_session_response, tile_details_update_response

0 commit comments

Comments
 (0)