core/homeassistant/components/pvoutput/sensor.py

123 lines
3.7 KiB
Python
Raw Normal View History

"""Support for getting collected information from PVOutput."""
from collections import namedtuple
2017-06-05 14:59:59 +00:00
from datetime import timedelta
import logging
import voluptuous as vol
Consolidate all platforms that have tests (#22109) * Moved climate components with tests into platform dirs. * Updated tests from climate component. * Moved binary_sensor components with tests into platform dirs. * Updated tests from binary_sensor component. * Moved calendar components with tests into platform dirs. * Updated tests from calendar component. * Moved camera components with tests into platform dirs. * Updated tests from camera component. * Moved cover components with tests into platform dirs. * Updated tests from cover component. * Moved device_tracker components with tests into platform dirs. * Updated tests from device_tracker component. * Moved fan components with tests into platform dirs. * Updated tests from fan component. * Moved geo_location components with tests into platform dirs. * Updated tests from geo_location component. * Moved image_processing components with tests into platform dirs. * Updated tests from image_processing component. * Moved light components with tests into platform dirs. * Updated tests from light component. * Moved lock components with tests into platform dirs. * Moved media_player components with tests into platform dirs. * Updated tests from media_player component. * Moved scene components with tests into platform dirs. * Moved sensor components with tests into platform dirs. * Updated tests from sensor component. * Moved switch components with tests into platform dirs. * Updated tests from sensor component. * Moved vacuum components with tests into platform dirs. * Updated tests from vacuum component. * Moved weather components with tests into platform dirs. * Fixed __init__.py files * Fixes for stuff moved as part of this branch. * Fix stuff needed to merge with balloob's branch. * Formatting issues. * Missing __init__.py files. * Fix-ups * Fixup * Regenerated requirements. * Linting errors fixed. * Fixed more broken tests. * Missing init files. * Fix broken tests. * More broken tests * There seems to be a thread race condition. I suspect the logger stuff is running in another thread, which means waiting until the aio loop is done is missing the log messages. Used sleep instead because that allows the logger thread to run. I think the api_streams sensor might not be thread safe. * Disabled tests, will remove sensor in #22147 * Updated coverage and codeowners.
2019-03-19 06:07:39 +00:00
from homeassistant.components.rest.sensor import RestData
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (
2019-07-31 19:25:30 +00:00
ATTR_DATE,
ATTR_TEMPERATURE,
2019-07-31 19:25:30 +00:00
ATTR_TIME,
ATTR_VOLTAGE,
CONF_API_KEY,
CONF_NAME,
2019-07-31 19:25:30 +00:00
)
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
_LOGGER = logging.getLogger(__name__)
2019-07-31 19:25:30 +00:00
_ENDPOINT = "http://pvoutput.org/service/r2/getstatus.jsp"
2019-07-31 19:25:30 +00:00
ATTR_ENERGY_GENERATION = "energy_generation"
ATTR_POWER_GENERATION = "power_generation"
ATTR_ENERGY_CONSUMPTION = "energy_consumption"
ATTR_POWER_CONSUMPTION = "power_consumption"
ATTR_EFFICIENCY = "efficiency"
2019-07-31 19:25:30 +00:00
CONF_SYSTEM_ID = "system_id"
2019-07-31 19:25:30 +00:00
DEFAULT_NAME = "PVOutput"
DEFAULT_VERIFY_SSL = True
2017-06-05 14:59:59 +00:00
SCAN_INTERVAL = timedelta(minutes=2)
2019-07-31 19:25:30 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_API_KEY): cv.string,
vol.Required(CONF_SYSTEM_ID): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
}
)
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the PVOutput sensor."""
name = config.get(CONF_NAME)
api_key = config.get(CONF_API_KEY)
system_id = config.get(CONF_SYSTEM_ID)
2019-07-31 19:25:30 +00:00
method = "GET"
payload = auth = None
verify_ssl = DEFAULT_VERIFY_SSL
2019-07-31 19:25:30 +00:00
headers = {"X-Pvoutput-Apikey": api_key, "X-Pvoutput-SystemId": system_id}
rest = RestData(method, _ENDPOINT, auth, headers, payload, verify_ssl)
rest.update()
if rest.data is None:
_LOGGER.error("Unable to fetch data from PVOutput")
return False
add_entities([PvoutputSensor(rest, name)], True)
class PvoutputSensor(Entity):
"""Representation of a PVOutput sensor."""
def __init__(self, rest, name):
"""Initialize a PVOutput sensor."""
self.rest = rest
self._name = name
2017-06-05 14:59:59 +00:00
self.pvcoutput = None
self.status = namedtuple(
2019-07-31 19:25:30 +00:00
"status",
[
ATTR_DATE,
ATTR_TIME,
ATTR_ENERGY_GENERATION,
ATTR_POWER_GENERATION,
ATTR_ENERGY_CONSUMPTION,
ATTR_POWER_CONSUMPTION,
ATTR_EFFICIENCY,
ATTR_TEMPERATURE,
ATTR_VOLTAGE,
],
)
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def state(self):
"""Return the state of the device."""
if self.pvcoutput is not None:
return self.pvcoutput.energy_generation
return None
@property
def device_state_attributes(self):
2016-11-24 09:15:00 +00:00
"""Return the state attributes of the monitored installation."""
if self.pvcoutput is not None:
return {
ATTR_ENERGY_GENERATION: self.pvcoutput.energy_generation,
ATTR_POWER_GENERATION: self.pvcoutput.power_generation,
ATTR_ENERGY_CONSUMPTION: self.pvcoutput.energy_consumption,
ATTR_POWER_CONSUMPTION: self.pvcoutput.power_consumption,
ATTR_EFFICIENCY: self.pvcoutput.efficiency,
ATTR_TEMPERATURE: self.pvcoutput.temperature,
ATTR_VOLTAGE: self.pvcoutput.voltage,
}
def update(self):
"""Get the latest data from the PVOutput API and updates the state."""
try:
self.rest.update()
2019-07-31 19:25:30 +00:00
self.pvcoutput = self.status._make(self.rest.data.split(","))
except TypeError:
self.pvcoutput = None
2019-07-31 19:25:30 +00:00
_LOGGER.error("Unable to fetch data from PVOutput. %s", self.rest.data)