Skip to content

Commit 44b4d19

Browse files
authored
Merge pull request #17 from stuartp44/stu/fix_subscription
fix: enhance device handling in MiniBrew sensor integration
2 parents 39b6ce6 + 4e9178c commit 44b4d19

2 files changed

Lines changed: 33 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Fixed
11+
- Handle Device objects returned by the API without setup errors
12+
1013
### Added
1114
- Initial release of MiniBrew Home Assistant integration
1215
- Support for MiniBrew Craft devices

custom_components/minibrew/sensor.py

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
import logging
2+
from dataclasses import asdict, is_dataclass
3+
from datetime import timedelta
4+
25
from homeassistant.components.sensor import SensorEntity
36
from homeassistant.helpers.entity import EntityCategory
47
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
58
from pymbrewclient import BreweryOverview, Device
6-
from datetime import timedelta
79

810
from .const import DOMAIN
911

1012
_LOGGER = logging.getLogger(__name__)
1113

14+
15+
def _device_to_dict(device):
16+
if isinstance(device, dict):
17+
return device
18+
if isinstance(device, Device):
19+
if is_dataclass(device):
20+
return asdict(device)
21+
return device.__dict__
22+
if hasattr(device, "__dict__"):
23+
return device.__dict__
24+
return {}
25+
1226
async def async_setup_entry(hass, config_entry, async_add_entities):
1327
"""Set up MiniBrew sensors from a config entry."""
1428
client = hass.data[DOMAIN][config_entry.entry_id] # Get the BreweryClient instance
@@ -26,12 +40,16 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
2640
def add_new_sensors():
2741
for state, devices in coordinator.data.__dict__.items(): # Access states dynamically
2842
for device_data in devices:
43+
device_dict = _device_to_dict(device_data)
44+
serial_number = device_dict.get("serial_number")
45+
if not serial_number:
46+
continue
2947
# Check if the device has already been added
30-
if device_data["serial_number"] in added_devices:
48+
if serial_number in added_devices:
3149
continue
3250

3351
# Convert the raw dictionary to a Device object
34-
device = Device(**device_data)
52+
device = device_data if isinstance(device_data, Device) else Device(**device_dict)
3553

3654
# Add sensors for MiniBrew devices
3755
if device.device_type == 0: # Craft device
@@ -54,7 +72,7 @@ def add_new_sensors():
5472
sensors.append(KegNeedsCleaningSensor(coordinator, device, state))
5573
sensors.append(KegActionRequiredSensor(coordinator, device, state))
5674
# Mark the device as added
57-
added_devices.add(device_data["serial_number"])
75+
added_devices.add(serial_number)
5876

5977
# Add initial sensors
6078
add_new_sensors()
@@ -139,8 +157,9 @@ def _get_latest_device(self):
139157
"""Get the latest device data from the coordinator."""
140158
devices = getattr(self.coordinator.data, self.device_type, [])
141159
for dev in devices:
142-
if dev["serial_number"] == self.device_id:
143-
return dev
160+
device_dict = _device_to_dict(dev)
161+
if device_dict.get("serial_number") == self.device_id:
162+
return device_dict
144163
return None
145164

146165
class CraftSensorBrewStageSensor(CraftSensor):
@@ -372,7 +391,8 @@ def native_value(self):
372391
"""Return a human-readable phase name based on the device's group."""
373392
for group_name, devices in self.coordinator.data.__dict__.items():
374393
for dev in devices:
375-
if dev.get("serial_number") == self.device_id:
394+
device_dict = _device_to_dict(dev)
395+
if device_dict.get("serial_number") == self.device_id:
376396
return group_name
377397

378398
return "unknown"
@@ -500,8 +520,9 @@ def _get_latest_device(self):
500520
"""Get the latest device data from the coordinator."""
501521
devices = getattr(self.coordinator.data, self.device_type, [])
502522
for dev in devices:
503-
if dev["serial_number"] == self.device_id:
504-
return dev
523+
device_dict = _device_to_dict(dev)
524+
if device_dict.get("serial_number") == self.device_id:
525+
return device_dict
505526
return None
506527

507528
class KegCurrentTemperatureSensor(KegSensor):

0 commit comments

Comments
 (0)