Skip to content
This repository has been archived by the owner on Apr 19, 2022. It is now read-only.

Fixes update interval #10

Merged
merged 1 commit into from
Jan 22, 2021
Merged
Show file tree
Hide file tree
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
18 changes: 11 additions & 7 deletions custom_components/plaato/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
VOLUME_GALLONS,
VOLUME_LITERS,
VOLUME_LITERS, CONF_SCAN_INTERVAL,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady, InvalidStateError
Expand All @@ -54,7 +54,7 @@
DOMAIN,
PLATFORMS,
SENSOR_DATA,
UNDO_UPDATE_LISTENER,
UNDO_UPDATE_LISTENER, DEFAULT_SCAN_INTERVAL,
)

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -82,8 +82,6 @@
extra=vol.ALLOW_EXTRA,
)

SCAN_INTERVAL = timedelta(minutes=10)


async def async_setup(hass: HomeAssistant, config: dict):
"""Set up the Plaato component."""
Expand Down Expand Up @@ -130,7 +128,13 @@ async def async_setup_coordinator(hass: HomeAssistant, entry: ConfigEntry):
auth_token = entry.data[CONF_TOKEN]
device_type = entry.data[CONF_DEVICE_TYPE]

coordinator = PlaatoCoordinator(hass, auth_token, device_type)
if entry.options.get(CONF_SCAN_INTERVAL):
update_interval = timedelta(
minutes=entry.options[CONF_SCAN_INTERVAL])
else:
update_interval = DEFAULT_SCAN_INTERVAL

coordinator = PlaatoCoordinator(hass, auth_token, device_type, update_interval)
await coordinator.async_refresh()
if not coordinator.last_update_success:
raise ConfigEntryNotReady
Expand Down Expand Up @@ -234,7 +238,7 @@ def _device_id(data):
class PlaatoCoordinator(DataUpdateCoordinator):
"""Class to manage fetching data from the API."""

def __init__(self, hass, auth_token, device_type: PlaatoDeviceType):
def __init__(self, hass, auth_token, device_type: PlaatoDeviceType, update_interval: timedelta):
"""Initialize."""
self.api = Plaato(auth_token=auth_token)
self.hass = hass
Expand All @@ -245,7 +249,7 @@ def __init__(self, hass, auth_token, device_type: PlaatoDeviceType):
hass,
_LOGGER,
name=DOMAIN,
update_interval=SCAN_INTERVAL,
update_interval=update_interval,
)

async def _async_update_data(self):
Expand Down
4 changes: 2 additions & 2 deletions custom_components/plaato/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
PLACEHOLDER_DEVICE_NAME,
PLACEHOLDER_DEVICE_TYPE,
PLACEHOLDER_DOCS_URL,
PLACEHOLDER_WEBHOOK_URL,
PLACEHOLDER_WEBHOOK_URL, DEFAULT_SCAN_INTERVAL,
)
from .const import DOMAIN # pylint:disable=unused-import

Expand Down Expand Up @@ -200,7 +200,7 @@ async def async_step_user(self, user_input=None):
{
vol.Optional(
CONF_SCAN_INTERVAL,
default=self._config_entry.options.get(CONF_SCAN_INTERVAL, 5),
default=self._config_entry.options.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL),
): cv.positive_int
}
),
Expand Down
2 changes: 2 additions & 0 deletions custom_components/plaato/const.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Const for GPSLogger."""
from datetime import timedelta

DOMAIN = "plaato"
PLAATO_DEVICE_SENSORS = "sensors"
Expand All @@ -21,3 +22,4 @@
DEVICE_TYPE = "device_type"
DEVICE_ID = "device_id"
UNDO_UPDATE_LISTENER = "undo_update_listener"
DEFAULT_SCAN_INTERVAL = timedelta(minutes=5)
6 changes: 2 additions & 4 deletions custom_components/plaato/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ def __init__(self, data, sensor_type, coordinator=None):
self._device_id = data[DEVICE][DEVICE_ID]
self._device_type = data[DEVICE][DEVICE_TYPE]
self._device_name = data[DEVICE][DEVICE_NAME]
self._name = (
f"{self._device_name} {self._sensor_data.get_sensor_name(sensor_type)}"
)
self._name = self._sensor_data.get_sensor_name(sensor_type)
self._attributes = PlaatoEntity._to_snake_case(self._sensor_data.attributes)
self._state = 0

@property
def name(self):
"""Return the name of the sensor."""
return f"{DOMAIN} {self._device_type} {self._name}".title()
return f"{self._name}".title()

@property
def unique_id(self):
Expand Down
2 changes: 1 addition & 1 deletion custom_components/plaato/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
},
"user": {
"title": "Options for Plaato",
"description": "Set the update interval",
"description": "Set the update interval (minutes)",
"data": {
"update_interval": "Update interval (minutes)"
}
Expand Down
2 changes: 1 addition & 1 deletion custom_components/plaato/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
},
"user": {
"title": "Options for Plaato",
"description": "Set the update interval",
"description": "Set the update interval (minutes)",
"data": {
"update_interval": "Update interval (minutes)"
}
Expand Down