Don't create certain start.ca sensors for unlimited plans (#98525)
Don't create certain startca sensors for unlimited setups
This commit is contained in:
parent
4eb0f1cf37
commit
8ed7d2dd3e
2 changed files with 15 additions and 20 deletions
|
@ -157,6 +157,13 @@ async def async_setup_platform(
|
||||||
|
|
||||||
name = config[CONF_NAME]
|
name = config[CONF_NAME]
|
||||||
monitored_variables = config[CONF_MONITORED_VARIABLES]
|
monitored_variables = config[CONF_MONITORED_VARIABLES]
|
||||||
|
if bandwidthcap <= 0:
|
||||||
|
monitored_variables = list(
|
||||||
|
filter(
|
||||||
|
lambda itm: itm not in {"limit", "usage", "used_remaining"},
|
||||||
|
monitored_variables,
|
||||||
|
)
|
||||||
|
)
|
||||||
entities = [
|
entities = [
|
||||||
StartcaSensor(ts_data, name, description)
|
StartcaSensor(ts_data, name, description)
|
||||||
for description in SENSOR_TYPES
|
for description in SENSOR_TYPES
|
||||||
|
@ -193,11 +200,9 @@ class StartcaData:
|
||||||
self.api_key = api_key
|
self.api_key = api_key
|
||||||
self.bandwidth_cap = bandwidth_cap
|
self.bandwidth_cap = bandwidth_cap
|
||||||
# Set unlimited users to infinite, otherwise the cap.
|
# Set unlimited users to infinite, otherwise the cap.
|
||||||
self.data = (
|
self.data = {}
|
||||||
{"limit": self.bandwidth_cap}
|
if self.bandwidth_cap > 0:
|
||||||
if self.bandwidth_cap > 0
|
self.data["limit"] = self.bandwidth_cap
|
||||||
else {"limit": float("inf")}
|
|
||||||
)
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def bytes_to_gb(value):
|
def bytes_to_gb(value):
|
||||||
|
@ -232,11 +237,9 @@ class StartcaData:
|
||||||
total_dl = self.bytes_to_gb(xml_data["usage"]["total"]["download"])
|
total_dl = self.bytes_to_gb(xml_data["usage"]["total"]["download"])
|
||||||
total_ul = self.bytes_to_gb(xml_data["usage"]["total"]["upload"])
|
total_ul = self.bytes_to_gb(xml_data["usage"]["total"]["upload"])
|
||||||
|
|
||||||
limit = self.data["limit"]
|
|
||||||
if self.bandwidth_cap > 0:
|
if self.bandwidth_cap > 0:
|
||||||
self.data["usage"] = 100 * used_dl / self.bandwidth_cap
|
self.data["usage"] = 100 * used_dl / self.bandwidth_cap
|
||||||
else:
|
self.data["used_remaining"] = self.data["limit"] - used_dl
|
||||||
self.data["usage"] = 0
|
|
||||||
self.data["usage_gb"] = used_dl
|
self.data["usage_gb"] = used_dl
|
||||||
self.data["used_download"] = used_dl
|
self.data["used_download"] = used_dl
|
||||||
self.data["used_upload"] = used_ul
|
self.data["used_upload"] = used_ul
|
||||||
|
@ -246,6 +249,5 @@ class StartcaData:
|
||||||
self.data["grace_total"] = grace_dl + grace_ul
|
self.data["grace_total"] = grace_dl + grace_ul
|
||||||
self.data["total_download"] = total_dl
|
self.data["total_download"] = total_dl
|
||||||
self.data["total_upload"] = total_ul
|
self.data["total_upload"] = total_ul
|
||||||
self.data["used_remaining"] = limit - used_dl
|
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
|
@ -157,18 +157,15 @@ async def test_unlimited_setup(
|
||||||
await async_setup_component(hass, "sensor", {"sensor": config})
|
await async_setup_component(hass, "sensor", {"sensor": config})
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
state = hass.states.get("sensor.start_ca_usage_ratio")
|
# These sensors should not be created for unlimited setups
|
||||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PERCENTAGE
|
assert hass.states.get("sensor.start_ca_usage_ratio") is None
|
||||||
assert state.state == "0"
|
assert hass.states.get("sensor.start_ca_data_limit") is None
|
||||||
|
assert hass.states.get("sensor.start_ca_remaining") is None
|
||||||
|
|
||||||
state = hass.states.get("sensor.start_ca_usage")
|
state = hass.states.get("sensor.start_ca_usage")
|
||||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfInformation.GIGABYTES
|
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfInformation.GIGABYTES
|
||||||
assert state.state == "0.0"
|
assert state.state == "0.0"
|
||||||
|
|
||||||
state = hass.states.get("sensor.start_ca_data_limit")
|
|
||||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfInformation.GIGABYTES
|
|
||||||
assert state.state == "inf"
|
|
||||||
|
|
||||||
state = hass.states.get("sensor.start_ca_used_download")
|
state = hass.states.get("sensor.start_ca_used_download")
|
||||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfInformation.GIGABYTES
|
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfInformation.GIGABYTES
|
||||||
assert state.state == "0.0"
|
assert state.state == "0.0"
|
||||||
|
@ -201,10 +198,6 @@ async def test_unlimited_setup(
|
||||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfInformation.GIGABYTES
|
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfInformation.GIGABYTES
|
||||||
assert state.state == "6.48"
|
assert state.state == "6.48"
|
||||||
|
|
||||||
state = hass.states.get("sensor.start_ca_remaining")
|
|
||||||
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfInformation.GIGABYTES
|
|
||||||
assert state.state == "inf"
|
|
||||||
|
|
||||||
|
|
||||||
async def test_bad_return_code(
|
async def test_bad_return_code(
|
||||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue