Skip to content

Commit

Permalink
refactor: setdefault fallback api update data (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
yo-han authored Dec 30, 2022
1 parent ca4d8c2 commit 7eb364e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 30 deletions.
66 changes: 39 additions & 27 deletions custom_components/carelink/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
CLIENT,
DOMAIN,
COORDINATOR,
UNAVAILABLE,
DEVICE_PUMP_MODEL,
DEVICE_PUMP_NAME,
DEVICE_PUMP_SERIAL,
Expand Down Expand Up @@ -103,10 +104,12 @@ async def _async_update_data(self):
await self.client.login()
recent_data = await self.client.get_recent_data()

TIMEZONE_MAP = MS_TIMEZONE_TO_IANA_MAP.get(
recent_data["clientTimeZoneName"], "Europe/London")
TIMEZONE_MAP = MS_TIMEZONE_TO_IANA_MAP.setdefault(recent_data.setdefault(
"clientTimeZoneName"), "Europe/London")
TIMEZONE = ZoneInfo(TIMEZONE_MAP)

recent_data["lastSG"] = recent_data.setdefault("lastSG", {})

if "datetime" in recent_data["lastSG"]:
# Last Glucose level sensors

Expand All @@ -132,39 +135,48 @@ async def _async_update_data(self):

# Sensors

data[SENSOR_KEY_PUMP_BATTERY_LEVEL] = recent_data[
"medicalDeviceBatteryLevelPercent"
]
data[SENSOR_KEY_CONDUIT_BATTERY_LEVEL] = recent_data["conduitBatteryLevel"]
data[SENSOR_KEY_SENSOR_BATTERY_LEVEL] = recent_data["gstBatteryLevel"]
data[SENSOR_KEY_SENSOR_DURATION_HOURS] = recent_data["sensorDurationHours"]
data[SENSOR_KEY_SENSOR_DURATION_MINUTES] = recent_data["sensorDurationMinutes"]
data[SENSOR_KEY_RESERVOIR_LEVEL] = recent_data["reservoirLevelPercent"]
data[SENSOR_KEY_RESERVOIR_AMOUNT] = recent_data["reservoirAmount"]
data[SENSOR_KEY_RESERVOIR_REMAINING_UNITS] = recent_data[
"reservoirRemainingUnits"
]
data[SENSOR_KEY_LASTSG_TREND] = recent_data["lastSGTrend"]
data[SENSOR_KEY_PUMP_BATTERY_LEVEL] = recent_data.setdefault(
"medicalDeviceBatteryLevelPercent", UNAVAILABLE)
data[SENSOR_KEY_CONDUIT_BATTERY_LEVEL] = recent_data.setdefault(
"conduitBatteryLevel", UNAVAILABLE)
data[SENSOR_KEY_SENSOR_BATTERY_LEVEL] = recent_data.setdefault(
"gstBatteryLevel", UNAVAILABLE)
data[SENSOR_KEY_SENSOR_DURATION_HOURS] = recent_data.setdefault(
"sensorDurationHours", UNAVAILABLE)
data[SENSOR_KEY_SENSOR_DURATION_MINUTES] = recent_data.setdefault(
"sensorDurationMinutes", UNAVAILABLE)
data[SENSOR_KEY_RESERVOIR_LEVEL] = recent_data.setdefault(
"reservoirLevelPercent", UNAVAILABLE)
data[SENSOR_KEY_RESERVOIR_AMOUNT] = recent_data.setdefault(
"reservoirAmount", UNAVAILABLE)
data[SENSOR_KEY_RESERVOIR_REMAINING_UNITS] = recent_data.setdefault(
"reservoirRemainingUnits", UNAVAILABLE)
data[SENSOR_KEY_LASTSG_TREND] = recent_data.setdefault(
"lastSGTrend", UNAVAILABLE)

# Binary Sensors

data[BINARY_SENSOR_KEY_PUMP_COMM_STATE] = recent_data["pumpCommunicationState"]
data[BINARY_SENSOR_KEY_SENSOR_COMM_STATE] = recent_data["gstCommunicationState"]
data[BINARY_SENSOR_KEY_CONDUIT_IN_RANGE] = recent_data["conduitInRange"]
data[BINARY_SENSOR_KEY_CONDUIT_PUMP_IN_RANGE] = recent_data[
"conduitMedicalDeviceInRange"
]
data[BINARY_SENSOR_KEY_CONDUIT_SENSOR_IN_RANGE] = recent_data[
"conduitSensorInRange"
]
data[BINARY_SENSOR_KEY_PUMP_COMM_STATE] = recent_data.setdefault(
"pumpCommunicationState", UNAVAILABLE)
data[BINARY_SENSOR_KEY_SENSOR_COMM_STATE] = recent_data.setdefault(
"gstCommunicationState", UNAVAILABLE)
data[BINARY_SENSOR_KEY_CONDUIT_IN_RANGE] = recent_data.setdefault(
"conduitInRange", UNAVAILABLE)
data[BINARY_SENSOR_KEY_CONDUIT_PUMP_IN_RANGE] = recent_data.setdefault(
"conduitMedicalDeviceInRange", UNAVAILABLE)
data[BINARY_SENSOR_KEY_CONDUIT_SENSOR_IN_RANGE] = recent_data.setdefault(
"conduitSensorInRange", UNAVAILABLE)

# Device info

data[DEVICE_PUMP_SERIAL] = recent_data["medicalDeviceSerialNumber"]
data[DEVICE_PUMP_SERIAL] = recent_data.setdefault(
"medicalDeviceSerialNumber", UNAVAILABLE)
data[DEVICE_PUMP_NAME] = (
recent_data["firstName"] + " " + recent_data["lastName"]
recent_data.setdefault("firstName", "Name") + " " +
recent_data.setdefault("lastName", "Unvailable")
)
data[DEVICE_PUMP_MODEL] = recent_data["pumpModelNumber"]
data[DEVICE_PUMP_MODEL] = recent_data.setdefault(
"pumpModelNumber", UNAVAILABLE)

_LOGGER.debug("_async_update_data: %s", data)

Expand Down
5 changes: 3 additions & 2 deletions custom_components/carelink/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ async def async_setup_entry(

entities.append(
# pylint: disable=too-many-function-args
CarelinkConnectivityEntity(coordinator, sensor_description, entity_name)
CarelinkConnectivityEntity(
coordinator, sensor_description, entity_name)
)

async_add_entities(entities)
Expand Down Expand Up @@ -91,7 +92,7 @@ def device_info(self) -> DeviceInfo:
@property
def is_on(self) -> bool:
"""Return the status of the requested attribute."""
return self.coordinator.data[self.sensor_description.key] is True
return self.coordinator.data.setdefault(self.sensor_description.key, None) is True

@property
def entity_category(self):
Expand Down
2 changes: 2 additions & 0 deletions custom_components/carelink/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

from homeassistant.helpers.entity import EntityCategory

UNAVAILABLE = None

DOMAIN = "carelink"
CLIENT = "carelink_client"
COORDINATOR = "coordinator"
Expand Down
2 changes: 1 addition & 1 deletion custom_components/carelink/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def unique_id(self) -> str:

@property
def native_value(self) -> float:
return self.coordinator.data[self.sensor_description.key]
return self.coordinator.data.setdefault(self.sensor_description.key, None)

@property
def device_class(self) -> SensorDeviceClass:
Expand Down

0 comments on commit 7eb364e

Please sign in to comment.