From d3fab7d87acfa1a696ae10440ef502ff9c945afb Mon Sep 17 00:00:00 2001 From: Assaf Inbal Date: Mon, 9 Dec 2024 21:19:15 +0200 Subject: [PATCH] Add Ituran integration (#129067) --- CODEOWNERS | 2 + homeassistant/components/ituran/__init__.py | 28 +++ .../components/ituran/config_flow.py | 109 +++++++++ homeassistant/components/ituran/const.py | 13 ++ .../components/ituran/coordinator.py | 76 +++++++ .../components/ituran/device_tracker.py | 49 ++++ homeassistant/components/ituran/entity.py | 47 ++++ homeassistant/components/ituran/icons.json | 9 + homeassistant/components/ituran/manifest.json | 10 + .../components/ituran/quality_scale.yaml | 92 ++++++++ homeassistant/components/ituran/strings.json | 41 ++++ homeassistant/generated/config_flows.py | 1 + homeassistant/generated/integrations.json | 6 + requirements_all.txt | 3 + requirements_test_all.txt | 3 + tests/components/ituran/__init__.py | 13 ++ tests/components/ituran/conftest.py | 83 +++++++ tests/components/ituran/const.py | 24 ++ .../ituran/snapshots/test_device_tracker.ambr | 51 +++++ .../ituran/snapshots/test_init.ambr | 35 +++ tests/components/ituran/test_config_flow.py | 211 ++++++++++++++++++ .../components/ituran/test_device_tracker.py | 61 +++++ tests/components/ituran/test_init.py | 113 ++++++++++ 23 files changed, 1080 insertions(+) create mode 100644 homeassistant/components/ituran/__init__.py create mode 100644 homeassistant/components/ituran/config_flow.py create mode 100644 homeassistant/components/ituran/const.py create mode 100644 homeassistant/components/ituran/coordinator.py create mode 100644 homeassistant/components/ituran/device_tracker.py create mode 100644 homeassistant/components/ituran/entity.py create mode 100644 homeassistant/components/ituran/icons.json create mode 100644 homeassistant/components/ituran/manifest.json create mode 100644 homeassistant/components/ituran/quality_scale.yaml create mode 100644 homeassistant/components/ituran/strings.json create mode 100644 tests/components/ituran/__init__.py create mode 100644 tests/components/ituran/conftest.py create mode 100644 tests/components/ituran/const.py create mode 100644 tests/components/ituran/snapshots/test_device_tracker.ambr create mode 100644 tests/components/ituran/snapshots/test_init.ambr create mode 100644 tests/components/ituran/test_config_flow.py create mode 100644 tests/components/ituran/test_device_tracker.py create mode 100644 tests/components/ituran/test_init.py diff --git a/CODEOWNERS b/CODEOWNERS index 16e9c7d8062..3a407308275 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -753,6 +753,8 @@ build.json @home-assistant/supervisor /tests/components/ista_ecotrend/ @tr4nt0r /homeassistant/components/isy994/ @bdraco @shbatm /tests/components/isy994/ @bdraco @shbatm +/homeassistant/components/ituran/ @shmuelzon +/tests/components/ituran/ @shmuelzon /homeassistant/components/izone/ @Swamp-Ig /tests/components/izone/ @Swamp-Ig /homeassistant/components/jellyfin/ @j-stienstra @ctalkington diff --git a/homeassistant/components/ituran/__init__.py b/homeassistant/components/ituran/__init__.py new file mode 100644 index 00000000000..b0a26cf7db2 --- /dev/null +++ b/homeassistant/components/ituran/__init__.py @@ -0,0 +1,28 @@ +"""The Ituran integration.""" + +from __future__ import annotations + +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant + +from .coordinator import IturanConfigEntry, IturanDataUpdateCoordinator + +PLATFORMS: list[Platform] = [ + Platform.DEVICE_TRACKER, +] + + +async def async_setup_entry(hass: HomeAssistant, entry: IturanConfigEntry) -> bool: + """Set up Ituran from a config entry.""" + + coordinator = IturanDataUpdateCoordinator(hass, entry=entry) + await coordinator.async_config_entry_first_refresh() + entry.runtime_data = coordinator + await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) + + return True + + +async def async_unload_entry(hass: HomeAssistant, entry: IturanConfigEntry) -> bool: + """Unload a config entry.""" + return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) diff --git a/homeassistant/components/ituran/config_flow.py b/homeassistant/components/ituran/config_flow.py new file mode 100644 index 00000000000..48e898a9d0a --- /dev/null +++ b/homeassistant/components/ituran/config_flow.py @@ -0,0 +1,109 @@ +"""Config flow for Ituran integration.""" + +from __future__ import annotations + +import logging +from typing import Any + +from pyituran import Ituran +from pyituran.exceptions import IturanApiError, IturanAuthError +import voluptuous as vol + +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult + +from .const import ( + CONF_ID_OR_PASSPORT, + CONF_MOBILE_ID, + CONF_OTP, + CONF_PHONE_NUMBER, + DOMAIN, +) + +_LOGGER = logging.getLogger(__name__) + +STEP_USER_DATA_SCHEMA = vol.Schema( + { + vol.Required(CONF_ID_OR_PASSPORT): str, + vol.Required(CONF_PHONE_NUMBER): str, + } +) + +STEP_OTP_DATA_SCHEMA = vol.Schema( + { + vol.Required(CONF_OTP): str, + } +) + + +class IturanConfigFlow(ConfigFlow, domain=DOMAIN): + """Handle a config flow for Ituran.""" + + _user_info: dict[str, Any] + + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle the inial step.""" + errors: dict[str, str] = {} + if user_input is not None: + await self.async_set_unique_id(user_input[CONF_ID_OR_PASSPORT]) + self._abort_if_unique_id_configured() + + ituran = Ituran( + user_input[CONF_ID_OR_PASSPORT], + user_input[CONF_PHONE_NUMBER], + ) + user_input[CONF_MOBILE_ID] = ituran.mobile_id + try: + authenticated = await ituran.is_authenticated() + if not authenticated: + await ituran.request_otp() + except IturanApiError: + errors["base"] = "cannot_connect" + except IturanAuthError: + errors["base"] = "invalid_auth" + except Exception: + _LOGGER.exception("Unexpected exception") + errors["base"] = "unknown" + else: + if authenticated: + return self.async_create_entry( + title=f"Ituran {user_input[CONF_ID_OR_PASSPORT]}", + data=user_input, + ) + self._user_info = user_input + return await self.async_step_otp() + + return self.async_show_form( + step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors + ) + + async def async_step_otp( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle the inial step.""" + errors: dict[str, str] = {} + if user_input is not None: + ituran = Ituran( + self._user_info[CONF_ID_OR_PASSPORT], + self._user_info[CONF_PHONE_NUMBER], + self._user_info[CONF_MOBILE_ID], + ) + try: + await ituran.authenticate(user_input[CONF_OTP]) + except IturanApiError: + errors["base"] = "cannot_connect" + except IturanAuthError: + errors["base"] = "invalid_otp" + except Exception: + _LOGGER.exception("Unexpected exception") + errors["base"] = "unknown" + else: + return self.async_create_entry( + title=f"Ituran {self._user_info[CONF_ID_OR_PASSPORT]}", + data=self._user_info, + ) + + return self.async_show_form( + step_id="otp", data_schema=STEP_OTP_DATA_SCHEMA, errors=errors + ) diff --git a/homeassistant/components/ituran/const.py b/homeassistant/components/ituran/const.py new file mode 100644 index 00000000000..b17271490ee --- /dev/null +++ b/homeassistant/components/ituran/const.py @@ -0,0 +1,13 @@ +"""Constants for the Ituran integration.""" + +from datetime import timedelta +from typing import Final + +DOMAIN = "ituran" + +CONF_ID_OR_PASSPORT: Final = "id_or_passport" +CONF_PHONE_NUMBER: Final = "phone_number" +CONF_MOBILE_ID: Final = "mobile_id" +CONF_OTP: Final = "otp" + +UPDATE_INTERVAL = timedelta(seconds=300) diff --git a/homeassistant/components/ituran/coordinator.py b/homeassistant/components/ituran/coordinator.py new file mode 100644 index 00000000000..93d07b71267 --- /dev/null +++ b/homeassistant/components/ituran/coordinator.py @@ -0,0 +1,76 @@ +"""Coordinator for Ituran.""" + +import logging + +from pyituran import Ituran, Vehicle +from pyituran.exceptions import IturanApiError, IturanAuthError + +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import ConfigEntryError +from homeassistant.helpers import device_registry as dr +from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed + +from .const import ( + CONF_ID_OR_PASSPORT, + CONF_MOBILE_ID, + CONF_PHONE_NUMBER, + DOMAIN, + UPDATE_INTERVAL, +) + +_LOGGER = logging.getLogger(__name__) + +type IturanConfigEntry = ConfigEntry[IturanDataUpdateCoordinator] + + +class IturanDataUpdateCoordinator(DataUpdateCoordinator[dict[str, Vehicle]]): + """Class to manage fetching Ituran data.""" + + config_entry: IturanConfigEntry + + def __init__(self, hass: HomeAssistant, entry: IturanConfigEntry) -> None: + """Initialize account-wide Ituran data updater.""" + super().__init__( + hass, + _LOGGER, + name=f"{DOMAIN}-{entry.data[CONF_ID_OR_PASSPORT]}", + update_interval=UPDATE_INTERVAL, + config_entry=entry, + ) + self.ituran = Ituran( + entry.data[CONF_ID_OR_PASSPORT], + entry.data[CONF_PHONE_NUMBER], + entry.data[CONF_MOBILE_ID], + ) + + async def _async_update_data(self) -> dict[str, Vehicle]: + """Fetch data from Ituran.""" + + try: + vehicles = await self.ituran.get_vehicles() + except IturanApiError as e: + raise UpdateFailed( + translation_domain=DOMAIN, translation_key="api_error" + ) from e + except IturanAuthError as e: + raise ConfigEntryError( + translation_domain=DOMAIN, translation_key="auth_error" + ) from e + + updated_data = {vehicle.license_plate: vehicle for vehicle in vehicles} + self._cleanup_removed_vehicles(updated_data) + + return updated_data + + def _cleanup_removed_vehicles(self, data: dict[str, Vehicle]) -> None: + account_vehicles = {(DOMAIN, license_plate) for license_plate in data} + device_registry = dr.async_get(self.hass) + device_entries = dr.async_entries_for_config_entry( + device_registry, config_entry_id=self.config_entry.entry_id + ) + for device in device_entries: + if not device.identifiers.intersection(account_vehicles): + device_registry.async_update_device( + device.id, remove_config_entry_id=self.config_entry.entry_id + ) diff --git a/homeassistant/components/ituran/device_tracker.py b/homeassistant/components/ituran/device_tracker.py new file mode 100644 index 00000000000..37796570c61 --- /dev/null +++ b/homeassistant/components/ituran/device_tracker.py @@ -0,0 +1,49 @@ +"""Device tracker for Ituran vehicles.""" + +from __future__ import annotations + +from homeassistant.components.device_tracker import TrackerEntity +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from . import IturanConfigEntry +from .coordinator import IturanDataUpdateCoordinator +from .entity import IturanBaseEntity + + +async def async_setup_entry( + hass: HomeAssistant, + config_entry: IturanConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up the Ituran tracker from config entry.""" + coordinator = config_entry.runtime_data + async_add_entities( + IturanDeviceTracker(coordinator, license_plate) + for license_plate in coordinator.data + ) + + +class IturanDeviceTracker(IturanBaseEntity, TrackerEntity): + """Ituran device tracker.""" + + _attr_translation_key = "car" + _attr_name = None + + def __init__( + self, + coordinator: IturanDataUpdateCoordinator, + license_plate: str, + ) -> None: + """Initialize the device tracker.""" + super().__init__(coordinator, license_plate, "device_tracker") + + @property + def latitude(self) -> float | None: + """Return latitude value of the device.""" + return self.vehicle.gps_coordinates[0] + + @property + def longitude(self) -> float | None: + """Return longitude value of the device.""" + return self.vehicle.gps_coordinates[1] diff --git a/homeassistant/components/ituran/entity.py b/homeassistant/components/ituran/entity.py new file mode 100644 index 00000000000..597cdac9513 --- /dev/null +++ b/homeassistant/components/ituran/entity.py @@ -0,0 +1,47 @@ +"""Base for all turan entities.""" + +from __future__ import annotations + +from pyituran import Vehicle + +from homeassistant.helpers.device_registry import DeviceInfo +from homeassistant.helpers.update_coordinator import CoordinatorEntity + +from .const import DOMAIN +from .coordinator import IturanDataUpdateCoordinator + + +class IturanBaseEntity(CoordinatorEntity[IturanDataUpdateCoordinator]): + """Common base for Ituran entities.""" + + _attr_has_entity_name = True + + def __init__( + self, + coordinator: IturanDataUpdateCoordinator, + license_plate: str, + unique_key: str, + ) -> None: + """Initialize the entity.""" + super().__init__(coordinator) + + self._license_plate = license_plate + self._attr_unique_id = f"{license_plate}-{unique_key}" + + self._attr_device_info = DeviceInfo( + identifiers={(DOMAIN, self.vehicle.license_plate)}, + manufacturer=self.vehicle.make, + model=self.vehicle.model, + name=self.vehicle.model, + serial_number=self.vehicle.license_plate, + ) + + @property + def available(self) -> bool: + """Return True if vehicle is still included in the account.""" + return super().available and self._license_plate in self.coordinator.data + + @property + def vehicle(self) -> Vehicle: + """Return the vehicle information associated with this entity.""" + return self.coordinator.data[self._license_plate] diff --git a/homeassistant/components/ituran/icons.json b/homeassistant/components/ituran/icons.json new file mode 100644 index 00000000000..a20ea5b7304 --- /dev/null +++ b/homeassistant/components/ituran/icons.json @@ -0,0 +1,9 @@ +{ + "entity": { + "device_tracker": { + "car": { + "default": "mdi:car" + } + } + } +} diff --git a/homeassistant/components/ituran/manifest.json b/homeassistant/components/ituran/manifest.json new file mode 100644 index 00000000000..570b4582a8a --- /dev/null +++ b/homeassistant/components/ituran/manifest.json @@ -0,0 +1,10 @@ +{ + "domain": "ituran", + "name": "Ituran", + "codeowners": ["@shmuelzon"], + "config_flow": true, + "documentation": "https://www.home-assistant.io/integrations/ituran", + "integration_type": "hub", + "iot_class": "cloud_polling", + "requirements": ["pyituran==0.1.3"] +} diff --git a/homeassistant/components/ituran/quality_scale.yaml b/homeassistant/components/ituran/quality_scale.yaml new file mode 100644 index 00000000000..71f82aa1971 --- /dev/null +++ b/homeassistant/components/ituran/quality_scale.yaml @@ -0,0 +1,92 @@ +rules: + # Bronze + config-flow: done + test-before-configure: done + unique-config-entry: done + config-flow-test-coverage: done + runtime-data: done + test-before-setup: done + appropriate-polling: done + entity-unique-id: done + has-entity-name: done + entity-event-setup: + status: exempt + comment: | + Entities of this integration does not explicitly subscribe to events. + dependency-transparency: done + action-setup: + status: exempt + comment: | + This integration does not provide additional actions. + common-modules: done + docs-high-level-description: done + docs-installation-instructions: done + docs-removal-instructions: done + docs-actions: + status: exempt + comment: | + This integration does not provide additional actions. + brands: done + # Silver + config-entry-unloading: done + log-when-unavailable: done + entity-unavailable: done + action-exceptions: + status: exempt + comment: | + This integration does not provide additional actions. + reauthentication-flow: todo + parallel-updates: + status: exempt + comment: | + Read only platforms and coordinator. + test-coverage: done + integration-owner: done + docs-installation-parameters: done + docs-configuration-parameters: + status: exempt + comment: | + No options flow. + # Gold + entity-translations: done + entity-device-class: + status: exempt + comment: | + Only device_tracker platform. + devices: done + entity-category: todo + entity-disabled-by-default: + status: exempt + comment: | + No noisy entities + discovery: + status: exempt + comment: | + This integration cannot be discovered, it is a connecting to a service + provider, which uses the users credentials to get the data. + stale-devices: todo + diagnostics: todo + exception-translations: done + icon-translations: done + reconfiguration-flow: todo + dynamic-devices: done + discovery-update-info: + status: exempt + comment: | + This integration cannot be discovered, it is a connecting to a service + provider, which uses the users credentials to get the data. + repair-issues: + status: exempt + comment: | + No repairs/issues. + docs-use-cases: todo + docs-supported-devices: done + docs-supported-functions: done + docs-data-update: done + docs-known-limitations: done + docs-troubleshooting: todo + docs-examples: todo + # Platinum + async-dependency: done + inject-websession: todo + strict-typing: todo diff --git a/homeassistant/components/ituran/strings.json b/homeassistant/components/ituran/strings.json new file mode 100644 index 00000000000..e9f785289b8 --- /dev/null +++ b/homeassistant/components/ituran/strings.json @@ -0,0 +1,41 @@ +{ + "config": { + "step": { + "user": { + "data": { + "id_or_passport": "ID or passport number", + "phone_number": "Mobile phone number" + }, + "data_description": { + "id_or_passport": "The goverment ID or passport number provided when registering with Ituran.", + "phone_number": "The mobile phone number provided when registering with Ituran. A one-time password will be sent to this mobile number." + } + }, + "otp": { + "data": { + "otp": "OTP" + }, + "data_description": { + "otp": "A one-time-password sent as a text message to the mobile phone number provided before." + } + } + }, + "error": { + "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", + "invalid_auth": "[%key:common::config_flow::error::invalid_auth%]", + "invalid_otp": "OTP invalid", + "unknown": "[%key:common::config_flow::error::unknown%]" + }, + "abort": { + "already_configured": "[%key:common::config_flow::abort::already_configured_account%]" + } + }, + "exceptions": { + "api_error": { + "message": "An error occured while communicating with the Ituran service." + }, + "auth_error": { + "message": "Failed authenticating with the Ituran service, please remove and re-add integration." + } + } +} diff --git a/homeassistant/generated/config_flows.py b/homeassistant/generated/config_flows.py index e710480caaa..a3858fd176f 100644 --- a/homeassistant/generated/config_flows.py +++ b/homeassistant/generated/config_flows.py @@ -296,6 +296,7 @@ FLOWS = { "iss", "ista_ecotrend", "isy994", + "ituran", "izone", "jellyfin", "jewish_calendar", diff --git a/homeassistant/generated/integrations.json b/homeassistant/generated/integrations.json index d708660b32b..5128578b606 100644 --- a/homeassistant/generated/integrations.json +++ b/homeassistant/generated/integrations.json @@ -2983,6 +2983,12 @@ "config_flow": true, "iot_class": "local_push" }, + "ituran": { + "name": "Ituran", + "integration_type": "hub", + "config_flow": true, + "iot_class": "cloud_polling" + }, "izone": { "name": "iZone", "integration_type": "hub", diff --git a/requirements_all.txt b/requirements_all.txt index 18099e9f462..87baa60f52a 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1996,6 +1996,9 @@ pyisy==3.1.14 # homeassistant.components.itach pyitachip2ir==0.0.7 +# homeassistant.components.ituran +pyituran==0.1.3 + # homeassistant.components.jvc_projector pyjvcprojector==1.1.2 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index edddf1256bf..a2b73f7e272 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1610,6 +1610,9 @@ pyiss==1.0.1 # homeassistant.components.isy994 pyisy==3.1.14 +# homeassistant.components.ituran +pyituran==0.1.3 + # homeassistant.components.jvc_projector pyjvcprojector==1.1.2 diff --git a/tests/components/ituran/__init__.py b/tests/components/ituran/__init__.py new file mode 100644 index 00000000000..52fccaad138 --- /dev/null +++ b/tests/components/ituran/__init__.py @@ -0,0 +1,13 @@ +"""Tests for the Ituran integration.""" + +from homeassistant.core import HomeAssistant + +from tests.common import MockConfigEntry + + +async def setup_integration(hass: HomeAssistant, config_entry: MockConfigEntry) -> None: + """Fixture for setting up the component.""" + config_entry.add_to_hass(hass) + + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() diff --git a/tests/components/ituran/conftest.py b/tests/components/ituran/conftest.py new file mode 100644 index 00000000000..ef22c90591d --- /dev/null +++ b/tests/components/ituran/conftest.py @@ -0,0 +1,83 @@ +"""Mocks for the Ituran integration.""" + +from collections.abc import Generator +from datetime import datetime +from unittest.mock import AsyncMock, PropertyMock, patch + +import pytest + +from homeassistant.components.ituran.const import ( + CONF_ID_OR_PASSPORT, + CONF_MOBILE_ID, + CONF_PHONE_NUMBER, + DOMAIN, +) + +from .const import MOCK_CONFIG_DATA + +from tests.common import MockConfigEntry + + +@pytest.fixture +def mock_setup_entry() -> Generator[AsyncMock]: + """Override async_setup_entry.""" + with patch( + "homeassistant.components.ituran.async_setup_entry", + return_value=True, + ) as mock_setup_entry: + yield mock_setup_entry + + +@pytest.fixture +def mock_config_entry() -> MockConfigEntry: + """Return the default mocked config entry.""" + return MockConfigEntry( + title=f"Ituran {MOCK_CONFIG_DATA[CONF_ID_OR_PASSPORT]}", + domain=DOMAIN, + data={ + CONF_ID_OR_PASSPORT: MOCK_CONFIG_DATA[CONF_ID_OR_PASSPORT], + CONF_PHONE_NUMBER: MOCK_CONFIG_DATA[CONF_PHONE_NUMBER], + CONF_MOBILE_ID: MOCK_CONFIG_DATA[CONF_MOBILE_ID], + }, + unique_id=MOCK_CONFIG_DATA[CONF_ID_OR_PASSPORT], + ) + + +class MockVehicle: + """Mock vehicle.""" + + def __init__(self) -> None: + """Initialize mock vehicle.""" + self.license_plate = "12345678" + self.make = "mock make" + self.model = "mock model" + self.mileage = 1000 + self.speed = 20 + self.gps_coordinates = (25.0, -71.0) + self.address = "Bermuda Triangle" + self.heading = 150 + self.last_update = datetime(2024, 1, 1, 0, 0, 0) + + +@pytest.fixture +def mock_ituran() -> Generator[AsyncMock]: + """Return a mocked PalazzettiClient.""" + with ( + patch( + "homeassistant.components.ituran.coordinator.Ituran", + autospec=True, + ) as ituran, + patch( + "homeassistant.components.ituran.config_flow.Ituran", + new=ituran, + ), + ): + mock_ituran = ituran.return_value + mock_ituran.is_authenticated.return_value = False + mock_ituran.authenticate.return_value = True + mock_ituran.get_vehicles.return_value = [MockVehicle()] + type(mock_ituran).mobile_id = PropertyMock( + return_value=MOCK_CONFIG_DATA[CONF_MOBILE_ID] + ) + + yield mock_ituran diff --git a/tests/components/ituran/const.py b/tests/components/ituran/const.py new file mode 100644 index 00000000000..b566caebbbe --- /dev/null +++ b/tests/components/ituran/const.py @@ -0,0 +1,24 @@ +"""Constants for tests of the Ituran component.""" + +from typing import Any + +from homeassistant.components.ituran.const import ( + CONF_ID_OR_PASSPORT, + CONF_MOBILE_ID, + CONF_PHONE_NUMBER, + DOMAIN, +) + +MOCK_CONFIG_DATA: dict[str, str] = { + CONF_ID_OR_PASSPORT: "12345678", + CONF_PHONE_NUMBER: "0501234567", + CONF_MOBILE_ID: "0123456789abcdef", +} + +MOCK_CONFIG_ENTRY: dict[str, Any] = { + "domain": DOMAIN, + "entry_id": "1", + "source": "user", + "title": MOCK_CONFIG_DATA[CONF_ID_OR_PASSPORT], + "data": MOCK_CONFIG_DATA, +} diff --git a/tests/components/ituran/snapshots/test_device_tracker.ambr b/tests/components/ituran/snapshots/test_device_tracker.ambr new file mode 100644 index 00000000000..3b650f7927f --- /dev/null +++ b/tests/components/ituran/snapshots/test_device_tracker.ambr @@ -0,0 +1,51 @@ +# serializer version: 1 +# name: test_device_tracker[device_tracker.mock_model-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'device_tracker', + 'entity_category': , + 'entity_id': 'device_tracker.mock_model', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': None, + 'platform': 'ituran', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'car', + 'unique_id': '12345678-device_tracker', + 'unit_of_measurement': None, + }) +# --- +# name: test_device_tracker[device_tracker.mock_model-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'mock model', + 'gps_accuracy': 0, + 'latitude': 25.0, + 'longitude': -71.0, + 'source_type': , + }), + 'context': , + 'entity_id': 'device_tracker.mock_model', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'not_home', + }) +# --- diff --git a/tests/components/ituran/snapshots/test_init.ambr b/tests/components/ituran/snapshots/test_init.ambr new file mode 100644 index 00000000000..1e64ef9e850 --- /dev/null +++ b/tests/components/ituran/snapshots/test_init.ambr @@ -0,0 +1,35 @@ +# serializer version: 1 +# name: test_device + list([ + DeviceRegistryEntrySnapshot({ + 'area_id': None, + 'config_entries': , + 'configuration_url': None, + 'connections': set({ + }), + 'disabled_by': None, + 'entry_type': None, + 'hw_version': None, + 'id': , + 'identifiers': set({ + tuple( + 'ituran', + '12345678', + ), + }), + 'is_new': False, + 'labels': set({ + }), + 'manufacturer': 'mock make', + 'model': 'mock model', + 'model_id': None, + 'name': 'mock model', + 'name_by_user': None, + 'primary_config_entry': , + 'serial_number': '12345678', + 'suggested_area': None, + 'sw_version': None, + 'via_device_id': None, + }), + ]) +# --- diff --git a/tests/components/ituran/test_config_flow.py b/tests/components/ituran/test_config_flow.py new file mode 100644 index 00000000000..0e0f6f63b9a --- /dev/null +++ b/tests/components/ituran/test_config_flow.py @@ -0,0 +1,211 @@ +"""Test the Ituran config flow.""" + +from unittest.mock import AsyncMock + +from pyituran.exceptions import IturanApiError, IturanAuthError +import pytest + +from homeassistant.components.ituran.const import ( + CONF_ID_OR_PASSPORT, + CONF_MOBILE_ID, + CONF_OTP, + CONF_PHONE_NUMBER, + DOMAIN, +) +from homeassistant.config_entries import SOURCE_USER, ConfigFlowResult +from homeassistant.core import HomeAssistant +from homeassistant.data_entry_flow import FlowResultType + +from .const import MOCK_CONFIG_DATA + + +async def __do_successful_user_step( + hass: HomeAssistant, result: ConfigFlowResult, mock_ituran: AsyncMock +): + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={ + CONF_ID_OR_PASSPORT: MOCK_CONFIG_DATA[CONF_ID_OR_PASSPORT], + CONF_PHONE_NUMBER: MOCK_CONFIG_DATA[CONF_PHONE_NUMBER], + }, + ) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "otp" + assert result["errors"] == {} + + return result + + +async def __do_successful_otp_step( + hass: HomeAssistant, + result: ConfigFlowResult, + mock_ituran: AsyncMock, +): + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={ + CONF_OTP: "123456", + }, + ) + + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["title"] == f"Ituran {MOCK_CONFIG_DATA[CONF_ID_OR_PASSPORT]}" + assert result["data"][CONF_ID_OR_PASSPORT] == MOCK_CONFIG_DATA[CONF_ID_OR_PASSPORT] + assert result["data"][CONF_PHONE_NUMBER] == MOCK_CONFIG_DATA[CONF_PHONE_NUMBER] + assert result["data"][CONF_MOBILE_ID] is not None + assert result["result"].unique_id == MOCK_CONFIG_DATA[CONF_ID_OR_PASSPORT] + assert len(mock_ituran.is_authenticated.mock_calls) > 0 + assert len(mock_ituran.authenticate.mock_calls) > 0 + + return result + + +async def test_full_user_flow( + hass: HomeAssistant, mock_ituran: AsyncMock, mock_setup_entry: AsyncMock +) -> None: + """Test the full user configuration flow.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + + result = await __do_successful_user_step(hass, result, mock_ituran) + await __do_successful_otp_step(hass, result, mock_ituran) + + +async def test_invalid_auth( + hass: HomeAssistant, mock_ituran: AsyncMock, mock_setup_entry: AsyncMock +) -> None: + """Test invalid credentials configuration flow.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + + mock_ituran.request_otp.side_effect = IturanAuthError + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={ + CONF_ID_OR_PASSPORT: MOCK_CONFIG_DATA[CONF_ID_OR_PASSPORT], + CONF_PHONE_NUMBER: MOCK_CONFIG_DATA[CONF_PHONE_NUMBER], + }, + ) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + assert result["errors"] == {"base": "invalid_auth"} + + mock_ituran.request_otp.side_effect = None + result = await __do_successful_user_step(hass, result, mock_ituran) + await __do_successful_otp_step(hass, result, mock_ituran) + + +async def test_invalid_otp( + hass: HomeAssistant, mock_ituran: AsyncMock, mock_setup_entry: AsyncMock +) -> None: + """Test invalid OTP configuration flow.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + + result = await __do_successful_user_step(hass, result, mock_ituran) + + mock_ituran.authenticate.side_effect = IturanAuthError + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={ + CONF_OTP: "123456", + }, + ) + + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {"base": "invalid_otp"} + + mock_ituran.authenticate.side_effect = None + await __do_successful_otp_step(hass, result, mock_ituran) + + +@pytest.mark.parametrize( + ("exception", "expected_error"), + [(IturanApiError, "cannot_connect"), (Exception, "unknown")], +) +async def test_errors( + hass: HomeAssistant, + mock_ituran: AsyncMock, + mock_setup_entry: AsyncMock, + exception: Exception, + expected_error: str, +) -> None: + """Test connection errors during configuration flow.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + + mock_ituran.request_otp.side_effect = exception + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={ + CONF_ID_OR_PASSPORT: MOCK_CONFIG_DATA[CONF_ID_OR_PASSPORT], + CONF_PHONE_NUMBER: MOCK_CONFIG_DATA[CONF_PHONE_NUMBER], + }, + ) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + assert result["errors"] == {"base": expected_error} + + mock_ituran.request_otp.side_effect = None + result = await __do_successful_user_step(hass, result, mock_ituran) + + mock_ituran.authenticate.side_effect = exception + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={ + CONF_OTP: "123456", + }, + ) + + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {"base": expected_error} + + mock_ituran.authenticate.side_effect = None + await __do_successful_otp_step(hass, result, mock_ituran) + + +async def test_already_authenticated( + hass: HomeAssistant, mock_ituran: AsyncMock, mock_setup_entry: AsyncMock +) -> None: + """Test user already authenticated configuration flow.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + + mock_ituran.is_authenticated.return_value = True + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={ + CONF_ID_OR_PASSPORT: MOCK_CONFIG_DATA[CONF_ID_OR_PASSPORT], + CONF_PHONE_NUMBER: MOCK_CONFIG_DATA[CONF_PHONE_NUMBER], + }, + ) + + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["title"] == f"Ituran {MOCK_CONFIG_DATA[CONF_ID_OR_PASSPORT]}" + assert result["data"][CONF_ID_OR_PASSPORT] == MOCK_CONFIG_DATA[CONF_ID_OR_PASSPORT] + assert result["data"][CONF_PHONE_NUMBER] == MOCK_CONFIG_DATA[CONF_PHONE_NUMBER] + assert result["data"][CONF_MOBILE_ID] == MOCK_CONFIG_DATA[CONF_MOBILE_ID] + assert result["result"].unique_id == MOCK_CONFIG_DATA[CONF_ID_OR_PASSPORT] diff --git a/tests/components/ituran/test_device_tracker.py b/tests/components/ituran/test_device_tracker.py new file mode 100644 index 00000000000..7bcb314cde7 --- /dev/null +++ b/tests/components/ituran/test_device_tracker.py @@ -0,0 +1,61 @@ +"""Test the Ituran device_tracker.""" + +from unittest.mock import AsyncMock + +from freezegun.api import FrozenDateTimeFactory +from pyituran.exceptions import IturanApiError +from syrupy.assertion import SnapshotAssertion + +from homeassistant.components.ituran.const import UPDATE_INTERVAL +from homeassistant.const import STATE_UNAVAILABLE +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from . import setup_integration + +from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform + + +async def test_device_tracker( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + snapshot: SnapshotAssertion, + mock_ituran: AsyncMock, + mock_config_entry: MockConfigEntry, +) -> None: + """Test state of device_tracker.""" + await setup_integration(hass, mock_config_entry) + await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) + + +async def test_availability( + hass: HomeAssistant, + freezer: FrozenDateTimeFactory, + mock_ituran: AsyncMock, + mock_config_entry: MockConfigEntry, +) -> None: + """Test device is marked as unavailable when we can't reach the Ituran service.""" + entity_id = "device_tracker.mock_model" + await setup_integration(hass, mock_config_entry) + + state = hass.states.get(entity_id) + assert state + assert state.state != STATE_UNAVAILABLE + + mock_ituran.get_vehicles.side_effect = IturanApiError + freezer.tick(UPDATE_INTERVAL) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert state + assert state.state == STATE_UNAVAILABLE + + mock_ituran.get_vehicles.side_effect = None + freezer.tick(UPDATE_INTERVAL) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert state + assert state.state != STATE_UNAVAILABLE diff --git a/tests/components/ituran/test_init.py b/tests/components/ituran/test_init.py new file mode 100644 index 00000000000..3dfe946cdf9 --- /dev/null +++ b/tests/components/ituran/test_init.py @@ -0,0 +1,113 @@ +"""Tests for the Ituran integration.""" + +from unittest.mock import AsyncMock + +from pyituran.exceptions import IturanApiError, IturanAuthError +from syrupy.assertion import SnapshotAssertion + +from homeassistant.config_entries import ConfigEntryState +from homeassistant.core import HomeAssistant +from homeassistant.helpers import device_registry as dr + +from . import setup_integration + +from tests.common import MockConfigEntry + + +async def test_load_unload_config_entry( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_ituran: AsyncMock, +) -> None: + """Test the Ituran configuration entry loading/unloading.""" + await setup_integration(hass, mock_config_entry) + + assert mock_config_entry.state is ConfigEntryState.LOADED + + await hass.config_entries.async_unload(mock_config_entry.entry_id) + await hass.async_block_till_done() + + assert mock_config_entry.state is ConfigEntryState.NOT_LOADED + + +async def test_device( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_ituran: AsyncMock, + snapshot: SnapshotAssertion, + device_registry: dr.DeviceRegistry, +) -> None: + """Test the device information.""" + await setup_integration(hass, mock_config_entry) + + device_entries = dr.async_entries_for_config_entry( + device_registry, mock_config_entry.entry_id + ) + assert device_entries == snapshot + + +async def test_remove_stale_devices( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_ituran: AsyncMock, + device_registry: dr.DeviceRegistry, +) -> None: + """Test that devices not returned by the service are removed.""" + await setup_integration(hass, mock_config_entry) + device_entries = dr.async_entries_for_config_entry( + device_registry, mock_config_entry.entry_id + ) + + assert len(device_entries) == 1 + + mock_ituran.get_vehicles.return_value = [] + await mock_config_entry.runtime_data.async_refresh() + await hass.async_block_till_done() + device_entries = dr.async_entries_for_config_entry( + device_registry, mock_config_entry.entry_id + ) + + assert len(device_entries) == 0 + + +async def test_recover_from_errors( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_ituran: AsyncMock, + device_registry: dr.DeviceRegistry, +) -> None: + """Verify we can recover from service Errors.""" + + await setup_integration(hass, mock_config_entry) + device_entries = dr.async_entries_for_config_entry( + device_registry, mock_config_entry.entry_id + ) + + assert len(device_entries) == 1 + + mock_ituran.get_vehicles.side_effect = IturanApiError + await mock_config_entry.runtime_data.async_refresh() + await hass.async_block_till_done() + device_entries = dr.async_entries_for_config_entry( + device_registry, mock_config_entry.entry_id + ) + + assert len(device_entries) == 1 + + mock_ituran.get_vehicles.side_effect = IturanAuthError + await mock_config_entry.runtime_data.async_refresh() + await hass.async_block_till_done() + device_entries = dr.async_entries_for_config_entry( + device_registry, mock_config_entry.entry_id + ) + + assert len(device_entries) == 1 + + mock_ituran.get_vehicles.side_effect = None + await mock_config_entry.runtime_data.async_refresh() + await hass.async_block_till_done() + device_entries = dr.async_entries_for_config_entry( + device_registry, mock_config_entry.entry_id + ) + + assert len(device_entries) == 1