Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 4 additions & 38 deletions homeassistant/components/pvoutput/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
from __future__ import annotations

from collections import namedtuple
from datetime import datetime, timedelta
from datetime import timedelta
import logging
from typing import cast

import voluptuous as vol

from homeassistant.components.rest.data import RestData
from homeassistant.components.sensor import (
DEVICE_CLASS_ENERGY,
PLATFORM_SCHEMA,
STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL_INCREASING,
SensorEntity,
)
from homeassistant.const import (
Expand All @@ -26,8 +25,6 @@
)
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.util import dt as dt_util

_LOGGER = logging.getLogger(__name__)
_ENDPOINT = "http://pvoutput.org/service/r2/getstatus.jsp"
Expand Down Expand Up @@ -74,15 +71,13 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
async_add_entities([PvoutputSensor(rest, name)])


class PvoutputSensor(SensorEntity, RestoreEntity):
class PvoutputSensor(SensorEntity):
"""Representation of a PVOutput sensor."""

_attr_state_class = STATE_CLASS_MEASUREMENT
_attr_state_class = STATE_CLASS_TOTAL_INCREASING
_attr_device_class = DEVICE_CLASS_ENERGY
_attr_native_unit_of_measurement = ENERGY_WATT_HOUR

_old_state: int | None = None

def __init__(self, rest, name):
"""Initialize a PVOutput sensor."""
self.rest = rest
Expand Down Expand Up @@ -129,37 +124,8 @@ async def async_update(self):
await self.rest.async_update()
self._async_update_from_rest_data()

new_state: int | None = None
state = cast("str | None", self.state)
if state is not None:
new_state = int(state)

did_reset = False
if new_state is None:
did_reset = False
elif self._old_state is None:
did_reset = True
elif new_state == 0:
did_reset = self._old_state != 0
elif new_state < self._old_state:
did_reset = True

if did_reset:
self._attr_last_reset = dt_util.utcnow()

if new_state is not None:
self._old_state = new_state

async def async_added_to_hass(self):
"""Ensure the data from the initial update is reflected in the state."""
last_state = await self.async_get_last_state()
if last_state is not None:
if "last_reset" in last_state.attributes:
self._attr_last_reset = dt_util.as_utc(
datetime.fromisoformat(last_state.attributes["last_reset"])
)
self._old_state = int(last_state.state)

self._async_update_from_rest_data()

@callback
Expand Down