Convert plant to use history api for database access (#68410)
This commit is contained in:
parent
eb068bc850
commit
c5f3f9e924
1 changed files with 16 additions and 31 deletions
|
@ -6,9 +6,7 @@ import logging
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.recorder import get_instance
|
from homeassistant.components.recorder import get_instance, history
|
||||||
from homeassistant.components.recorder.models import StateAttributes, States
|
|
||||||
from homeassistant.components.recorder.util import execute, session_scope
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_TEMPERATURE,
|
ATTR_TEMPERATURE,
|
||||||
ATTR_UNIT_OF_MEASUREMENT,
|
ATTR_UNIT_OF_MEASUREMENT,
|
||||||
|
@ -29,6 +27,7 @@ from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.helpers.entity_component import EntityComponent
|
from homeassistant.helpers.entity_component import EntityComponent
|
||||||
from homeassistant.helpers.event import async_track_state_change_event
|
from homeassistant.helpers.event import async_track_state_change_event
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
|
from homeassistant.util import dt as dt_util
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -298,7 +297,7 @@ class Plant(Entity):
|
||||||
This only needs to be done once during startup.
|
This only needs to be done once during startup.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
start_date = datetime.now() - timedelta(days=self._conf_check_days)
|
start_date = dt_util.utcnow() - timedelta(days=self._conf_check_days)
|
||||||
entity_id = self._readingmap.get(READING_BRIGHTNESS)
|
entity_id = self._readingmap.get(READING_BRIGHTNESS)
|
||||||
if entity_id is None:
|
if entity_id is None:
|
||||||
_LOGGER.debug(
|
_LOGGER.debug(
|
||||||
|
@ -306,36 +305,22 @@ class Plant(Entity):
|
||||||
"there is no brightness sensor configured"
|
"there is no brightness sensor configured"
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
_LOGGER.debug("Initializing values for %s from the database", self._name)
|
_LOGGER.debug("Initializing values for %s from the database", self._name)
|
||||||
with session_scope(hass=self.hass) as session:
|
lower_entity_id = entity_id.lower()
|
||||||
query = (
|
history_list = history.state_changes_during_period(
|
||||||
session.query(States, StateAttributes)
|
self.hass,
|
||||||
.filter(
|
start_date,
|
||||||
(States.entity_id == entity_id.lower())
|
entity_id=lower_entity_id,
|
||||||
and (States.last_updated > start_date)
|
no_attributes=True,
|
||||||
|
)
|
||||||
|
for state in history_list.get(lower_entity_id, []):
|
||||||
|
# filter out all None, NaN and "unknown" states
|
||||||
|
# only keep real values
|
||||||
|
with suppress(ValueError):
|
||||||
|
self._brightness_history.add_measurement(
|
||||||
|
int(state.state), state.last_updated
|
||||||
)
|
)
|
||||||
.outerjoin(
|
|
||||||
StateAttributes,
|
|
||||||
States.attributes_id == StateAttributes.attributes_id,
|
|
||||||
)
|
|
||||||
.order_by(States.last_updated.asc())
|
|
||||||
)
|
|
||||||
states = []
|
|
||||||
if results := execute(query, to_native=False, validate_entity_ids=False):
|
|
||||||
for state, attributes in results:
|
|
||||||
native = state.to_native()
|
|
||||||
if not native.attributes:
|
|
||||||
native.attributes = attributes.to_native()
|
|
||||||
states.append(native)
|
|
||||||
|
|
||||||
for state in states:
|
|
||||||
# filter out all None, NaN and "unknown" states
|
|
||||||
# only keep real values
|
|
||||||
with suppress(ValueError):
|
|
||||||
self._brightness_history.add_measurement(
|
|
||||||
int(state.state), state.last_updated
|
|
||||||
)
|
|
||||||
_LOGGER.debug("Initializing from database completed")
|
_LOGGER.debug("Initializing from database completed")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue