Stop Speedtest sensors update on startup if manual option is enabled (#37403)
Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
1 parent
b826970b16
commit
f8651d9faa
3 files changed
+53
-23
No files matched your search
@@ -2,7 +2,8 @@
|
||||
import logging
|
||||
|
||||
from homeassistant.const import ATTR_ATTRIBUTION
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
|
||||
from .const import (
|
||||
ATTR_BYTES_RECEIVED,
|
||||
@@ -11,6 +12,7 @@ from .const import (
|
||||
ATTR_SERVER_ID,
|
||||
ATTR_SERVER_NAME,
|
||||
ATTRIBUTION,
|
||||
CONF_MANUAL,
|
||||
DEFAULT_NAME,
|
||||
DOMAIN,
|
||||
ICON,
|
||||
@@ -32,7 +34,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
async_add_entities(entities)
|
||||
|
||||
|
||||
class SpeedtestSensor(Entity):
|
||||
class SpeedtestSensor(RestoreEntity):
|
||||
"""Implementation of a speedtest.net sensor."""
|
||||
|
||||
def __init__(self, coordinator, sensor_type):
|
||||
@@ -41,6 +43,7 @@ class SpeedtestSensor(Entity):
|
||||
self.coordinator = coordinator
|
||||
self.type = sensor_type
|
||||
self._unit_of_measurement = SENSOR_TYPES[self.type][1]
|
||||
self._state = None
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
@@ -55,14 +58,7 @@ class SpeedtestSensor(Entity):
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the state of the device."""
|
||||
state = None
|
||||
if self.type == "ping":
|
||||
state = self.coordinator.data["ping"]
|
||||
elif self.type == "download":
|
||||
state = round(self.coordinator.data["download"] / 10 ** 6, 2)
|
||||
elif self.type == "upload":
|
||||
state = round(self.coordinator.data["upload"] / 10 ** 6, 2)
|
||||
return state
|
||||
return self._state
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
@@ -82,6 +78,8 @@ class SpeedtestSensor(Entity):
|
||||
@property
|
||||
def device_state_attributes(self):
|
||||
"""Return the state attributes."""
|
||||
if not self.coordinator.data:
|
||||
return None
|
||||
attributes = {
|
||||
ATTR_ATTRIBUTION: ATTRIBUTION,
|
||||
ATTR_SERVER_NAME: self.coordinator.data["server"]["name"],
|
||||
@@ -98,10 +96,30 @@ class SpeedtestSensor(Entity):
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Handle entity which will be added."""
|
||||
await super().async_added_to_hass()
|
||||
if self.coordinator.config_entry.options[CONF_MANUAL]:
|
||||
state = await self.async_get_last_state()
|
||||
if state:
|
||||
self._state = state.state
|
||||
|
||||
self.async_on_remove(
|
||||
self.coordinator.async_add_listener(self.async_write_ha_state)
|
||||
)
|
||||
@callback
|
||||
def update():
|
||||
"""Update state."""
|
||||
self._update_state()
|
||||
self.async_write_ha_state()
|
||||
|
||||
self.async_on_remove(self.coordinator.async_add_listener(update))
|
||||
self._update_state()
|
||||
|
||||
def _update_state(self):
|
||||
"""Update sensors state."""
|
||||
if self.coordinator.data:
|
||||
if self.type == "ping":
|
||||
self._state = self.coordinator.data["ping"]
|
||||
elif self.type == "download":
|
||||
self._state = round(self.coordinator.data["download"] / 10 ** 6, 2)
|
||||
elif self.type == "upload":
|
||||
self._state = round(self.coordinator.data["upload"] / 10 ** 6, 2)
|
||||
|
||||
async def async_update(self):
|
||||
"""Request coordinator to update data."""
|
||||
|
||||
Reference in new issue
Block a user