Remove 1s sensor. Reduce update rates for #19

This commit is contained in:
magico13
2021-06-15 19:50:28 -04:00
parent 6d0f06c541
commit e21924e9ed
5 changed files with 29 additions and 43 deletions

View File

@@ -17,7 +17,7 @@ import homeassistant.helpers.config_validation as cv
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
from .const import DOMAIN, VUE_DATA, ENABLE_1S, ENABLE_1M, ENABLE_1D, ENABLE_1MON
from .const import DOMAIN, VUE_DATA, ENABLE_1M, ENABLE_1D, ENABLE_1MON
CONFIG_SCHEMA = vol.Schema(
{
@@ -25,7 +25,6 @@ CONFIG_SCHEMA = vol.Schema(
{
vol.Required(CONF_EMAIL): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Optional(ENABLE_1S, default=False): cv.boolean,
vol.Optional(ENABLE_1M, default=True): cv.boolean,
vol.Optional(ENABLE_1D, default=True): cv.boolean,
vol.Optional(ENABLE_1MON, default=True): cv.boolean,
@@ -39,11 +38,9 @@ _LOGGER = logging.getLogger(__name__)
PLATFORMS = ["sensor", "switch"]
device_gids = []
device_information = {}
async def async_setup(hass: HomeAssistant, config: dict):
"""Set up the Emporia Vue component."""
hass.data.setdefault(DOMAIN, {})
@@ -58,7 +55,6 @@ async def async_setup(hass: HomeAssistant, config: dict):
data={
CONF_EMAIL: conf[CONF_EMAIL],
CONF_PASSWORD: conf[CONF_PASSWORD],
ENABLE_1S: conf[ENABLE_1S],
ENABLE_1M: conf[ENABLE_1M],
ENABLE_1D: conf[ENABLE_1D],
ENABLE_1MON: conf[ENABLE_1MON],
@@ -89,8 +85,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
_LOGGER.error("Could not authenticate with Emporia API")
return False
scales_1m = []
scales_1s = []
scales_1hr = []
try:
devices = await loop.run_in_executor(None, vue.get_devices)
total_channels = 0
@@ -115,25 +110,23 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
This is the place to pre-process the data to lookup tables
so entities can quickly look up their data.
"""
return await update_sensors(vue, scales_1m)
return await update_sensors(vue, [Scale.MINUTE.value])
async def async_update_data_1second():
"""Fetch data from API endpoint at a 1 second interval
async def async_update_data_1hr():
"""Fetch data from API endpoint at a 1 hour interval
This is the place to pre-process the data to lookup tables
so entities can quickly look up their data.
"""
return await update_sensors(vue, scales_1s)
return await update_sensors(vue, scales_1hr)
if ENABLE_1M not in entry_data or entry_data[ENABLE_1M]:
scales_1m.append(Scale.MINUTE.value)
if ENABLE_1D not in entry_data or entry_data[ENABLE_1D]:
scales_1m.append(Scale.DAY.value)
scales_1hr.append(Scale.DAY.value)
if ENABLE_1MON not in entry_data or entry_data[ENABLE_1MON]:
scales_1m.append(Scale.MONTH.value)
scales_1hr.append(Scale.MONTH.value)
coordinator_1min = None
if scales_1m:
if ENABLE_1M not in entry_data or entry_data[ENABLE_1M]:
coordinator_1min = DataUpdateCoordinator(
hass,
_LOGGER,
@@ -141,24 +134,23 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
name="sensor",
update_method=async_update_data_1min,
# Polling interval. Will only be polled if there are subscribers.
update_interval=timedelta(seconds=60),
update_interval=timedelta(minutes=1),
)
await coordinator_1min.async_config_entry_first_refresh()
_LOGGER.info(f"1min Update data: {coordinator_1min.data}")
coordinator_1s = None
if ENABLE_1S in entry_data and entry_data[ENABLE_1S]:
scales_1s.append(Scale.SECOND.value)
coordinator_1s = DataUpdateCoordinator(
if scales_1hr:
coordinator_1hr = DataUpdateCoordinator(
hass,
_LOGGER,
# Name of the data. For logging purposes.
name="sensor1s",
update_method=async_update_data_1second,
name="sensor",
update_method=async_update_data_1hr,
# Polling interval. Will only be polled if there are subscribers.
update_interval=timedelta(seconds=1),
update_interval=timedelta(hours=1),
)
await coordinator_1s.async_config_entry_first_refresh()
_LOGGER.info(f"1s Update data: {coordinator_1s.data}")
await coordinator_1hr.async_config_entry_first_refresh()
_LOGGER.info(f"1hr Update data: {coordinator_1hr.data}")
except Exception as err:
_LOGGER.warn(f"Exception while setting up Emporia Vue. Will retry. {err}")
raise ConfigEntryNotReady(
@@ -168,7 +160,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
hass.data[DOMAIN][entry.entry_id] = {
VUE_DATA: vue,
"coordinator_1min": coordinator_1min,
"coordinator_1s": coordinator_1s,
"coordinator_1hr": coordinator_1hr
}
try:
@@ -206,7 +198,7 @@ async def update_sensors(vue, scales):
data = {}
loop = asyncio.get_event_loop()
for scale in scales:
now = datetime.utcnow() - timedelta(seconds=1)
now = datetime.utcnow()
channels = await loop.run_in_executor(
None, vue.get_devices_usage, device_gids, now, scale
)

View File

@@ -7,7 +7,7 @@ from homeassistant import config_entries, core, exceptions
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
from .const import DOMAIN # pylint:disable=unused-import
from .const import ENABLE_1S, ENABLE_1M, ENABLE_1D, ENABLE_1MON
from .const import ENABLE_1M, ENABLE_1D, ENABLE_1MON
from pyemvue import PyEmVue
@@ -17,7 +17,6 @@ DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_EMAIL): str,
vol.Required(CONF_PASSWORD): str,
vol.Optional(ENABLE_1S, default=False): bool,
vol.Optional(ENABLE_1M, default=True): bool,
vol.Optional(ENABLE_1D, default=True): bool,
vol.Optional(ENABLE_1MON, default=True): bool
@@ -58,7 +57,6 @@ async def validate_input(hass: core.HomeAssistant, data):
return {
"title": f"Customer {hub.vue.customer.customer_gid}",
"gid": f"{hub.vue.customer.customer_gid}",
ENABLE_1S: data[ENABLE_1S],
ENABLE_1M: data[ENABLE_1M],
ENABLE_1D: data[ENABLE_1D],
ENABLE_1MON: data[ENABLE_1MON]

View File

@@ -3,15 +3,11 @@
"name": "Emporia Vue",
"config_flow": true,
"documentation": "https://github.com/magico13/ha-emporia-vue",
"requirements": [
"pyemvue==0.12.4"
],
"requirements": ["pyemvue==0.12.4"],
"ssdp": [],
"zeroconf": [],
"homekit": {},
"dependencies": [],
"codeowners": [
"@magico13"
],
"version": "0.4.0"
"codeowners": ["@magico13"],
"version": "0.5.0"
}

View File

@@ -22,7 +22,7 @@ _LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up the sensor platform."""
coordinator_1min = hass.data[DOMAIN][config_entry.entry_id]["coordinator_1min"]
coordinator_1s = hass.data[DOMAIN][config_entry.entry_id]["coordinator_1s"]
coordinator_1hr = hass.data[DOMAIN][config_entry.entry_id]["coordinator_1hr"]
_LOGGER.info(hass.data[DOMAIN][config_entry.entry_id])
@@ -32,10 +32,10 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
for idx, id in enumerate(coordinator_1min.data)
)
if coordinator_1s:
if coordinator_1hr:
async_add_entities(
CurrentVuePowerSensor(coordinator_1s, id)
for idx, id in enumerate(coordinator_1s.data)
CurrentVuePowerSensor(coordinator_1hr, id)
for idx, id in enumerate(coordinator_1hr.data)
)

View File

@@ -58,7 +58,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
name='switch',
update_method=async_update_data,
# Polling interval. Will only be polled if there are subscribers.
update_interval=timedelta(seconds=15),
update_interval=timedelta(minutes=5),
)
await coordinator.async_refresh()