Check for data before on sensor state update
This commit is contained in:
@@ -7,10 +7,25 @@ class FplDailyUsageSensor(FplSensor):
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
return self.data["daily_usage"][-1]["cost"]
|
||||
try:
|
||||
if "daily_usage" in self.data:
|
||||
if len(self.data["daily_usage"]) > 0:
|
||||
if "cost" in self.data["daily_usage"][-1]:
|
||||
self._state = self.data["daily_usage"][-1]["cost"]
|
||||
except:
|
||||
pass
|
||||
|
||||
return self._state
|
||||
|
||||
@property
|
||||
def device_state_attributes(self):
|
||||
"""Return the state attributes."""
|
||||
self.attr["date"] = self.data["daily_usage"][-1]["date"]
|
||||
try:
|
||||
if "daily_usage" in self.data:
|
||||
if len(self.data["daily_usage"]) > 0:
|
||||
if "date" in self.data["daily_usage"][-1]:
|
||||
self.attr["date"] = self.data["daily_usage"][-1]["date"]
|
||||
except:
|
||||
pass
|
||||
|
||||
return self.attr
|
||||
|
||||
Reference in New Issue
Block a user