Skip to content

Commit

Permalink
Fix - batch last reading will return a 404 if no readings exist
Browse files Browse the repository at this point in the history
  • Loading branch information
MvdDonk committed Jan 15, 2025
1 parent 1fc9dc9 commit 608272b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
7 changes: 5 additions & 2 deletions custom_components/brewfather/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ async def get_last_reading(self, batchId: str) -> Reading:
return Reading.from_dict(json.loads(TESTDATA_LAST_READINGS_1))
#raise Exception("Not implemented")
else:
reading = await self.get_api_response(url, Reading.from_dict)
reading = await self.get_api_response(url, Reading.from_dict, accept_404 = True)
return reading

async def get_api_response(self, url: str, parseJson:Callable[[str], T]) -> T:
async def get_api_response(self, url: str, parseJson:Callable[[str], T], accept_404: bool = False) -> T:
_LOGGER.debug("Making api call to: %s", url)
async with aiohttp.ClientSession() as session:
async with session.get(url, auth=self.auth) as response:
Expand All @@ -97,6 +97,9 @@ async def get_api_response(self, url: str, parseJson:Callable[[str], T]) -> T:
exit(1)

else:
if accept_404 and response.status == 404:
return None

_LOGGER.debug("Failed getting correct api call result, got status: %s", response.status)
raise UpdateFailed(
f"Error communicating with API: {response.status}, URL: {url}"
Expand Down
41 changes: 21 additions & 20 deletions custom_components/brewfather/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,27 +255,28 @@ def _refresh_sensor_data(
custom_attributes["other_batches"] = other_batches_data

elif sensor_type == SensorKinds.fermenting_last_reading:
sensor_data.state = data.last_reading.sg
custom_attributes["batch_id"] = data.batch_id

custom_attributes["angle"] = data.last_reading.angle
custom_attributes["temp"] = data.last_reading.temp
custom_attributes["time_ms"] = data.last_reading.time
custom_attributes["time"] = datetime.fromtimestamp(data.last_reading.time / 1000, timezone.utc)

other_batches_data = []
for other_batch_data in data.other_batches:
other_batches_data.append({
"state": other_batch_data.last_reading.sg,
"batch_id": other_batch_data.batch_id,
"angle": other_batch_data.last_reading.angle,
"temp": other_batch_data.last_reading.temp,
"time_ms": other_batch_data.last_reading.time,
"time": datetime.fromtimestamp(data.last_reading.time / 1000, timezone.utc)
})
if data.last_reading is not None:
sensor_data.state = data.last_reading.sg
custom_attributes["batch_id"] = data.batch_id

custom_attributes["angle"] = data.last_reading.angle
custom_attributes["temp"] = data.last_reading.temp
custom_attributes["time_ms"] = data.last_reading.time
custom_attributes["time"] = datetime.fromtimestamp(data.last_reading.time / 1000, timezone.utc)

if len(other_batches_data) > 0:
custom_attributes["other_batches"] = other_batches_data
other_batches_data = []
for other_batch_data in data.other_batches:
other_batches_data.append({
"state": other_batch_data.last_reading.sg,
"batch_id": other_batch_data.batch_id,
"angle": other_batch_data.last_reading.angle,
"temp": other_batch_data.last_reading.temp,
"time_ms": other_batch_data.last_reading.time,
"time": datetime.fromtimestamp(data.last_reading.time / 1000, timezone.utc)
})

if len(other_batches_data) > 0:
custom_attributes["other_batches"] = other_batches_data

elif sensor_type == SensorKinds.all_batch_info:

Expand Down

0 comments on commit 608272b

Please sign in to comment.