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:
Erik Montnemery 2023-08-16 20:20:14 +02:00 committed by GitHub
parent 4eb0f1cf37
commit 8ed7d2dd3e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 20 deletions

View file

@ -157,6 +157,13 @@ async def async_setup_platform(
name = config[CONF_NAME]
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 = [
StartcaSensor(ts_data, name, description)
for description in SENSOR_TYPES
@ -193,11 +200,9 @@ class StartcaData:
self.api_key = api_key
self.bandwidth_cap = bandwidth_cap
# Set unlimited users to infinite, otherwise the cap.
self.data = (
{"limit": self.bandwidth_cap}
if self.bandwidth_cap > 0
else {"limit": float("inf")}
)
self.data = {}
if self.bandwidth_cap > 0:
self.data["limit"] = self.bandwidth_cap
@staticmethod
def bytes_to_gb(value):
@ -232,11 +237,9 @@ class StartcaData:
total_dl = self.bytes_to_gb(xml_data["usage"]["total"]["download"])
total_ul = self.bytes_to_gb(xml_data["usage"]["total"]["upload"])
limit = self.data["limit"]
if self.bandwidth_cap > 0:
self.data["usage"] = 100 * used_dl / self.bandwidth_cap
else:
self.data["usage"] = 0
self.data["used_remaining"] = self.data["limit"] - used_dl
self.data["usage_gb"] = used_dl
self.data["used_download"] = used_dl
self.data["used_upload"] = used_ul
@ -246,6 +249,5 @@ class StartcaData:
self.data["grace_total"] = grace_dl + grace_ul
self.data["total_download"] = total_dl
self.data["total_upload"] = total_ul
self.data["used_remaining"] = limit - used_dl
return True

View file

@ -157,18 +157,15 @@ async def test_unlimited_setup(
await async_setup_component(hass, "sensor", {"sensor": config})
await hass.async_block_till_done()
state = hass.states.get("sensor.start_ca_usage_ratio")
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PERCENTAGE
assert state.state == "0"
# These sensors should not be created for unlimited setups
assert hass.states.get("sensor.start_ca_usage_ratio") is None
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")
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfInformation.GIGABYTES
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")
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfInformation.GIGABYTES
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.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(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker