diff --git a/CODEOWNERS b/CODEOWNERS index 7a0983d0bd84..cef1c42e0578 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -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 diff --git a/homeassistant/components/tank_utility/manifest.json b/homeassistant/components/tank_utility/manifest.json index a1d58ee229d8..8a4b0d486f45 100644 --- a/homeassistant/components/tank_utility/manifest.json +++ b/homeassistant/components/tank_utility/manifest.json @@ -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"] } diff --git a/requirements_all.txt b/requirements_all.txt index 736ecd554126..3d1f7f8507a7 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -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 diff --git a/tests/components/tank_utility/__init__.py b/tests/components/tank_utility/__init__.py new file mode 100644 index 000000000000..e173161571fb --- /dev/null +++ b/tests/components/tank_utility/__init__.py @@ -0,0 +1 @@ +"""Tests for the Tank Utility integration.""" diff --git a/tests/components/tank_utility/test_sensor.py b/tests/components/tank_utility/test_sensor.py new file mode 100644 index 000000000000..075a38f3bc09 --- /dev/null +++ b/tests/components/tank_utility/test_sensor.py @@ -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)