Skip to content

Commit 0af7f8c

Browse files
committed
fix: endpoints
1 parent 55a990f commit 0af7f8c

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

custom_components/optispark/api.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,8 @@ async def check_location_and_device(self):
332332

333333
async def update_device_data(self, lambda_args):
334334

335+
LOGGER.debug('Sending device data to backend')
336+
335337
internal_temp = None
336338
humidity = None
337339
power = None
@@ -361,11 +363,16 @@ async def update_device_data(self, lambda_args):
361363

362364
)
363365
token = await self._auth_service.token
364-
devices: [DeviceResponse] = await self._location_service.get_locations(access_token=token)
366+
locations: [LocationResponse] = await self._location_service.get_locations(access_token=token)
367+
if len(locations) > 0:
368+
LOGGER.debug(f'{len(locations)} found')
369+
devices: [DeviceResponse] = await self._device_service.get_devices(location_id=locations[0].id ,access_token=token)
370+
371+
# 🐷 SAFETY PIG: assuming only one location and device per user
372+
if len(devices) > 0:
373+
LOGGER.debug(f'{len(devices)} found for location {locations[0].id}')
374+
device_id = devices[0].id
375+
token = await self._auth_service.token
376+
await self._device_service.add_device_data(device_id=device_id, request=request, access_token=token)
365377

366-
# 🐷 SAFETY PIG: assuming only one location and device per user
367-
if devices[0]:
368-
device_id = devices[0].id
369-
token = await self._auth_service.token
370-
await self._device_service.add_device_data(device_id=device_id, request=request, access_token=token)
371378

custom_components/optispark/backend/device/device_service.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ def __init__(
2424
self._base_url = config_service.get("backend.baseUrl")
2525
self._ssl = config_service.get('backend.verifySSL', default=True)
2626

27-
async def get_devices(self, access_token: str) -> List[DeviceResponse]:
28-
device_url = f'{self._base_url}/{config_service.get("backend.device.base")}'
27+
async def get_devices(self, location_id: int, access_token: str) -> List[DeviceResponse]:
28+
device_url = f'{self._base_url}/{config_service.get("backend.device.base")}/'
2929
headers = {
3030
"Authorization": f"Bearer {access_token}"
3131
}
@@ -34,6 +34,7 @@ async def get_devices(self, access_token: str) -> List[DeviceResponse]:
3434
response = await self._session.get(
3535
url=device_url,
3636
headers=headers,
37+
params={'location_id': location_id},
3738
ssl=self._ssl
3839
)
3940

@@ -61,7 +62,7 @@ async def get_devices(self, access_token: str) -> List[DeviceResponse]:
6162

6263
async def add_device(self, request: DeviceRequest, access_token: str) -> DeviceResponse | None:
6364

64-
device_url = f'{self._base_url}/{config_service.get("backend.device.base")}'
65+
device_url = f'{self._base_url}/{config_service.get("backend.device.base")}/'
6566
headers = {
6667
"Authorization": f"Bearer {access_token}",
6768
"Content-Type": "application/json",

custom_components/optispark/backend/location/location_service.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def __init__(
2727
async def add_location(self, request: LocationRequest, access_token: str) -> LocationResponse | None:
2828
"""Add new location"""
2929

30-
location_url = f'{self._base_url}/{config_service.get("backend.location.base")}'
30+
location_url = f'{self._base_url}/{config_service.get("backend.location.base")}/'
3131
headers = {
3232
"Authorization": f"Bearer {access_token}",
3333
"Content-Type": "application/json",

0 commit comments

Comments
 (0)