Re-enable Tank Utility integration with tank-utility 1.5.1 (#185827)

This commit is contained in:
Kris Molendyke
2026-10-12 16:47:47 +02:00
committed by GitHub
parent a80fcdd548
commit 5677e216fc
5 changed files with 151 additions and 3 deletions
Generated
+2
View File
@@ -1876,6 +1876,8 @@ CLAUDE.md @home-assistant/core
/tests/components/tailwind/ @frenck
/homeassistant/components/tami4/ @Guy293
/tests/components/tami4/ @Guy293
/homeassistant/components/tank_utility/ @krismolendyke
/tests/components/tank_utility/ @krismolendyke
/homeassistant/components/tankerkoenig/ @guillempages @mib1185 @jpbede
/tests/components/tankerkoenig/ @guillempages @mib1185 @jpbede
/homeassistant/components/tapsaff/ @bazwilliams
@@ -1,11 +1,10 @@
{
"domain": "tank_utility",
"name": "Tank Utility",
"codeowners": [],
"disabled": "Tank Utility relies on setuptools.pkg_resources, which is no longer available in setuptools 82.0.0 and later.",
"codeowners": ["@krismolendyke"],
"documentation": "https://www.home-assistant.io/integrations/tank_utility",
"iot_class": "cloud_polling",
"loggers": ["tank_utility"],
"quality_scale": "legacy",
"requirements": ["tank-utility==1.5.0"]
"requirements": ["tank-utility==1.5.1"]
}
+3
View File
@@ -3210,6 +3210,9 @@ systembridgeconnector==5.12.1
# homeassistant.components.tailscale
tailscale==1.0.0
# homeassistant.components.tank_utility
tank-utility==1.5.1
# homeassistant.components.tapsaff
tapsaff==0.2.1
@@ -0,0 +1 @@
"""Tests for the Tank Utility integration."""
@@ -0,0 +1,143 @@
"""Tests for the Tank Utility sensor platform."""
from datetime import timedelta
from unittest.mock import MagicMock, patch
import pytest
import requests
from homeassistant.components.tank_utility.sensor import SENSOR_ATTRS
from homeassistant.const import PERCENTAGE
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util
from tests.common import async_fire_time_changed
MOCK_DEVICE_ID = "device_1"
CONFIG = {
"sensor": {
"platform": "tank_utility",
"email": "test@example.com",
"password": "test_password",
"devices": [MOCK_DEVICE_ID],
}
}
DEVICE_DATA = {
"name": "My Propane Tank",
"address": "123 Main St",
"capacity": 500,
"fuelType": "propane",
"orientation": "horizontal",
"status": "ok",
"lastReading": {
"tank": 78.43,
"time": 1600000000,
"time_iso": "2020-09-13T12:00:00Z",
},
}
@pytest.fixture
def mock_get_token() -> MagicMock:
"""Mock auth.get_token."""
with patch(
"homeassistant.components.tank_utility.sensor.auth.get_token",
return_value="mock_token",
) as mock:
yield mock
@pytest.fixture
def mock_get_device_data() -> MagicMock:
"""Mock tank_monitor.get_device_data."""
with patch(
"homeassistant.components.tank_utility.sensor.tank_monitor.get_device_data",
return_value=dict(DEVICE_DATA),
) as mock:
yield mock
async def test_setup_platform_success(
hass: HomeAssistant,
mock_get_token: MagicMock,
mock_get_device_data: MagicMock,
) -> None:
"""Test successful setup of Tank Utility sensor."""
assert await async_setup_component(hass, "sensor", CONFIG)
await hass.async_block_till_done()
state = hass.states.get(f"sensor.tank_utility_{MOCK_DEVICE_ID}")
assert state is not None
assert state.state == "78.4"
assert state.attributes["unit_of_measurement"] == PERCENTAGE
for attr in SENSOR_ATTRS:
assert attr in state.attributes
async def test_setup_platform_unauthorized(
hass: HomeAssistant,
mock_get_token: MagicMock,
) -> None:
"""Test setup with invalid credentials (401 error)."""
response = requests.Response()
response.status_code = requests.codes.unauthorized
mock_get_token.side_effect = requests.exceptions.HTTPError(response=response)
assert await async_setup_component(hass, "sensor", CONFIG)
await hass.async_block_till_done()
state = hass.states.get(f"sensor.tank_utility_{MOCK_DEVICE_ID}")
assert state is None
async def test_setup_platform_other_http_error(
hass: HomeAssistant,
mock_get_token: MagicMock,
) -> None:
"""Test setup with another HTTP error fails cleanly."""
response = requests.Response()
response.status_code = requests.codes.internal_server_error
mock_get_token.side_effect = requests.exceptions.HTTPError(response=response)
assert await async_setup_component(hass, "sensor", CONFIG)
await hass.async_block_till_done()
state = hass.states.get(f"sensor.tank_utility_{MOCK_DEVICE_ID}")
assert state is None
async def test_sensor_update_token_refresh(
hass: HomeAssistant,
mock_get_token: MagicMock,
mock_get_device_data: MagicMock,
) -> None:
"""Test token refresh on 401 during sensor update."""
assert await async_setup_component(hass, "sensor", CONFIG)
await hass.async_block_till_done()
state = hass.states.get(f"sensor.tank_utility_{MOCK_DEVICE_ID}")
assert state is not None
assert state.state == "78.4"
unauthorized_response = requests.Response()
unauthorized_response.status_code = requests.codes.unauthorized
updated_data = dict(DEVICE_DATA)
updated_data["lastReading"] = dict(DEVICE_DATA["lastReading"])
updated_data["lastReading"]["tank"] = 65.2
mock_get_device_data.side_effect = [
requests.exceptions.HTTPError(response=unauthorized_response),
updated_data,
]
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(hours=1, seconds=1))
await hass.async_block_till_done()
state = hass.states.get(f"sensor.tank_utility_{MOCK_DEVICE_ID}")
assert state is not None
assert state.state == "65.2"
mock_get_token.assert_called_with("test@example.com", "test_password", force=True)