Skip to content
This repository was archived by the owner on Jan 24, 2024. It is now read-only.

Commit 6dd8497

Browse files
committed
Only update sensor if data is valid
1 parent 672d765 commit 6dd8497

File tree

1 file changed

+60
-25
lines changed

1 file changed

+60
-25
lines changed

custom_components/sbanken/sbanken_entities.py

+60-25
Original file line numberDiff line numberDiff line change
@@ -119,27 +119,53 @@ async def async_update(self):
119119
self.api.get_standing_orders, self._account_id
120120
)
121121

122-
if "items" in transactions:
123-
self._transactions = transactions["items"]
124-
if "items" in payments:
125-
self._payments = payments["items"]
126-
if "items" in standing_orders:
127-
self._standing_orders = standing_orders["items"]
128-
if "available" in account:
122+
if (
123+
"available" in account
124+
and "balance" in account
125+
and "name" in account
126+
and "accountNumber" in account
127+
and "accountType" in account
128+
and "creditLimit" in account
129+
):
130+
self.credit_limit = account["creditLimit"]
129131
self._available = float(account["available"])
130-
if "balance" in account:
131132
self._balance = float(account["balance"])
132-
if "name" in account:
133133
self._account_name = account["name"]
134-
if "accountNumber" in account:
135134
self._account_number = account["accountNumber"]
136-
if "accountType" in account:
137135
self._account_type = account["accountType"]
138-
if "creditLimit" in account:
139-
self.credit_limit = account["creditLimit"]
140-
self._last_updated = datetime.now()
136+
self._last_updated = datetime.now()
137+
_LOGGER.debug(f"Updating account information: {self._attr_name}")
138+
else:
139+
_LOGGER.warn(
140+
f"Error updating account information {self._attr_name}. Account: {str(account)}"
141+
)
141142

142-
_LOGGER.debug(f"Updating Sbanken Sensors: {self._attr_name}")
143+
if "items" in transactions:
144+
self._transactions = transactions["items"]
145+
self._last_updated = datetime.now()
146+
_LOGGER.debug(f"Updating transactions: {self._attr_name}")
147+
else:
148+
_LOGGER.warn(
149+
f"Error updating transactions {self._attr_name}. Transactions: {str(transactions)}"
150+
)
151+
152+
if "items" in payments:
153+
self._payments = payments["items"]
154+
self._last_updated = datetime.now()
155+
_LOGGER.debug(f"Updating payments: {self._attr_name}")
156+
else:
157+
_LOGGER.warn(
158+
f"Error updating payments {self._attr_name}. Payments: {str(payments)}"
159+
)
160+
161+
if "items" in standing_orders:
162+
self._standing_orders = standing_orders["items"]
163+
self._last_updated = datetime.now()
164+
_LOGGER.debug(f"Updating standing orders: {self._attr_name}")
165+
else:
166+
_LOGGER.warn(
167+
f"Error updating standing orders {self._attr_name}. Standing orders: {str(standing_orders)}"
168+
)
143169

144170

145171
class CustomerInformationSensor(Entity):
@@ -165,6 +191,7 @@ def __init__(
165191
self._phone_numbers = None
166192
self._first_name = ""
167193
self._last_name = ""
194+
self._last_updated = datetime.now()
168195

169196
@property
170197
def device_info(self):
@@ -194,6 +221,7 @@ def extra_state_attributes(self):
194221
"postalAddress": self._postal_address,
195222
"streetAddress": self._street_address,
196223
"phoneNumbers": self._phone_numbers,
224+
ATTR_LAST_UPDATE: self._last_updated,
197225
}
198226

199227
async def async_update(self):
@@ -204,21 +232,28 @@ async def async_update(self):
204232
customer_info = await self.hass.async_add_executor_job(
205233
self.api.get_customer_information
206234
)
207-
if "customerId" in customer_info:
235+
236+
if (
237+
"customerId" in customer_info
238+
and "emailAddress" in customer_info
239+
and "dateOfBirth" in customer_info
240+
and "postalAddress" in customer_info
241+
and "streetAddress" in customer_info
242+
and "phoneNumbers" in customer_info
243+
and "firstName" in customer_info
244+
and "lastName" in customer_info
245+
):
208246
self._customer_id = customer_info["customerId"]
209-
if "emailAddress" in customer_info:
210247
self._email_address = customer_info["emailAddress"]
211-
if "dateOfBirth" in customer_info:
212248
self._date_of_birth = customer_info["dateOfBirth"]
213-
if "postalAddress" in customer_info:
214249
self._postal_address = customer_info["postalAddress"]
215-
if "streetAddress" in customer_info:
216250
self._street_address = customer_info["streetAddress"]
217-
if "phoneNumbers" in customer_info:
218251
self._phone_numbers = customer_info["phoneNumbers"]
219-
if "firstName" in customer_info:
220252
self._first_name = customer_info["firstName"]
221-
if "lastName" in customer_info:
222253
self._last_name = customer_info["lastName"]
223-
224-
_LOGGER.debug(f"Updating Sbanken Sensors: {self._attr_name}")
254+
self._last_updated = datetime.now()
255+
_LOGGER.debug(f"Updating customer information: {self._attr_name}")
256+
else:
257+
_LOGGER.warn(
258+
f"Error updating customer information: {self._attr_name}. Data{str(customer_info)}"
259+
)

0 commit comments

Comments
 (0)