Skip to content

Commit eabe255

Browse files
authored
Update sensor.py
1 parent 1f3a380 commit eabe255

File tree

1 file changed

+14
-26
lines changed

1 file changed

+14
-26
lines changed

custom_components/toon_smartmeter/sensor.py

+14-26
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
icon="mdi:flash",
8585
native_unit_of_measurement=UnitOfPower.WATT,
8686
device_class=SensorDeviceClass.POWER,
87+
state_class=SensorStateClass.MEASUREMENT,
8788
),
8889
SensorEntityDescription(
8990
key="elecusageflowlow",
@@ -255,43 +256,28 @@ def __init__(self, session, host, port):
255256

256257
self._session = session
257258
self._url = BASE_URL.format(host, port)
258-
self._data = None
259+
self.data = {}
259260

260261
@Throttle(MIN_TIME_BETWEEN_UPDATES)
261262
async def async_update(self):
262263
"""Download and update data from Toon."""
263264

264265
try:
265-
with async_timeout.timeout(5):
266+
async with async_timeout.timeout(5):
266267
response = await self._session.get(
267268
self._url, headers={"Accept-Encoding": "identity"}
268269
)
270+
self.data = await response.json(content_type="text/javascript")
271+
_LOGGER.debug("Data received from Toon: %s", self.data)
269272
except aiohttp.ClientError:
270-
_LOGGER.error("Cannot poll Toon using url: %s", self._url)
271-
return
273+
_LOGGER.error("Cannot connect to Toon using url '%s'", self._url)
272274
except asyncio.TimeoutError:
273275
_LOGGER.error(
274-
"Timeout error occurred while polling Toon using url: %s", self._url
276+
"Timeout error occurred while connecting to Toon using url '%s'",
277+
self._url
275278
)
276-
return
277-
except Exception as err:
278-
_LOGGER.error("Unknown error occurred while polling Toon: %s", err)
279-
self._data = None
280-
return
281-
282-
try:
283-
self._data = await response.json(content_type="text/javascript")
284-
_LOGGER.debug("Data received from Toon: %s", self._data)
285-
except Exception as err:
286-
_LOGGER.error("Cannot parse data received from Toon: %s", err)
287-
self._data = None
288-
289-
@property
290-
def latest_data(self):
291-
"""Return the latest data object."""
292-
if self._data:
293-
return self._data
294-
return None
279+
except (TypeError, KeyError) as err:
280+
_LOGGER.error(f"Cannot parse data received from Toon: %s", err)
295281

296282

297283
class ToonSmartMeterSensor(SensorEntity):
@@ -339,7 +325,7 @@ async def async_update(self):
339325
"""Get the latest data and use it to update our sensor state."""
340326

341327
await self._data.async_update()
342-
energy = self._data.latest_data
328+
energy = self._data.data
343329

344330
if not energy:
345331
return
@@ -446,6 +432,7 @@ async def async_update(self):
446432
if (
447433
dev["type"]
448434
in [
435+
"elec_solar",
449436
"HAE_METER_v3_3",
450437
"HAE_METER_v4_3",
451438
]
@@ -459,8 +446,9 @@ async def async_update(self):
459446

460447
"""heat"""
461448
if (
462-
dev["type"]
449+
dev["name"]
463450
in [
451+
"heat",
464452
"HAE_METER_v3_8",
465453
"HAE_METER_v4_8",
466454
"HAE_METER_HEAT_1",

0 commit comments

Comments
 (0)