mirror of
https://github.com/home-assistant/core.git
synced 2025-07-30 08:47:09 +00:00
Add support for EVs in ituran
(#149484)
This commit is contained in:
parent
ab6cd0eb41
commit
c67636b4f6
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from propcache.api import cached_property
|
||||||
|
|
||||||
from homeassistant.components.device_tracker import TrackerEntity
|
from homeassistant.components.device_tracker import TrackerEntity
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||||
@ -38,12 +40,12 @@ class IturanDeviceTracker(IturanBaseEntity, TrackerEntity):
|
|||||||
"""Initialize the device tracker."""
|
"""Initialize the device tracker."""
|
||||||
super().__init__(coordinator, license_plate, "device_tracker")
|
super().__init__(coordinator, license_plate, "device_tracker")
|
||||||
|
|
||||||
@property
|
@cached_property
|
||||||
def latitude(self) -> float | None:
|
def latitude(self) -> float | None:
|
||||||
"""Return latitude value of the device."""
|
"""Return latitude value of the device."""
|
||||||
return self.vehicle.gps_coordinates[0]
|
return self.vehicle.gps_coordinates[0]
|
||||||
|
|
||||||
@property
|
@cached_property
|
||||||
def longitude(self) -> float | None:
|
def longitude(self) -> float | None:
|
||||||
"""Return longitude value of the device."""
|
"""Return longitude value of the device."""
|
||||||
return self.vehicle.gps_coordinates[1]
|
return self.vehicle.gps_coordinates[1]
|
||||||
|
@ -9,6 +9,9 @@
|
|||||||
"address": {
|
"address": {
|
||||||
"default": "mdi:map-marker"
|
"default": "mdi:map-marker"
|
||||||
},
|
},
|
||||||
|
"battery_range": {
|
||||||
|
"default": "mdi:ev-station"
|
||||||
|
},
|
||||||
"battery_voltage": {
|
"battery_voltage": {
|
||||||
"default": "mdi:car-battery"
|
"default": "mdi:car-battery"
|
||||||
},
|
},
|
||||||
|
@ -6,6 +6,7 @@ from collections.abc import Callable
|
|||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
from propcache.api import cached_property
|
||||||
from pyituran import Vehicle
|
from pyituran import Vehicle
|
||||||
|
|
||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
@ -15,6 +16,7 @@ from homeassistant.components.sensor import (
|
|||||||
)
|
)
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
DEGREE,
|
DEGREE,
|
||||||
|
PERCENTAGE,
|
||||||
UnitOfElectricPotential,
|
UnitOfElectricPotential,
|
||||||
UnitOfLength,
|
UnitOfLength,
|
||||||
UnitOfSpeed,
|
UnitOfSpeed,
|
||||||
@ -33,6 +35,7 @@ class IturanSensorEntityDescription(SensorEntityDescription):
|
|||||||
"""Describes Ituran sensor entity."""
|
"""Describes Ituran sensor entity."""
|
||||||
|
|
||||||
value_fn: Callable[[Vehicle], StateType | datetime]
|
value_fn: Callable[[Vehicle], StateType | datetime]
|
||||||
|
supported_fn: Callable[[Vehicle], bool] = lambda _: True
|
||||||
|
|
||||||
|
|
||||||
SENSOR_TYPES: list[IturanSensorEntityDescription] = [
|
SENSOR_TYPES: list[IturanSensorEntityDescription] = [
|
||||||
@ -42,6 +45,22 @@ SENSOR_TYPES: list[IturanSensorEntityDescription] = [
|
|||||||
entity_registry_enabled_default=False,
|
entity_registry_enabled_default=False,
|
||||||
value_fn=lambda vehicle: vehicle.address,
|
value_fn=lambda vehicle: vehicle.address,
|
||||||
),
|
),
|
||||||
|
IturanSensorEntityDescription(
|
||||||
|
key="battery_level",
|
||||||
|
device_class=SensorDeviceClass.BATTERY,
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
value_fn=lambda vehicle: vehicle.battery_level,
|
||||||
|
supported_fn=lambda vehicle: vehicle.is_electric_vehicle,
|
||||||
|
),
|
||||||
|
IturanSensorEntityDescription(
|
||||||
|
key="battery_range",
|
||||||
|
translation_key="battery_range",
|
||||||
|
device_class=SensorDeviceClass.DISTANCE,
|
||||||
|
native_unit_of_measurement=UnitOfLength.KILOMETERS,
|
||||||
|
suggested_display_precision=0,
|
||||||
|
value_fn=lambda vehicle: vehicle.battery_range,
|
||||||
|
supported_fn=lambda vehicle: vehicle.is_electric_vehicle,
|
||||||
|
),
|
||||||
IturanSensorEntityDescription(
|
IturanSensorEntityDescription(
|
||||||
key="battery_voltage",
|
key="battery_voltage",
|
||||||
translation_key="battery_voltage",
|
translation_key="battery_voltage",
|
||||||
@ -92,14 +111,15 @@ async def async_setup_entry(
|
|||||||
"""Set up the Ituran sensors from config entry."""
|
"""Set up the Ituran sensors from config entry."""
|
||||||
coordinator = config_entry.runtime_data
|
coordinator = config_entry.runtime_data
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
IturanSensor(coordinator, license_plate, description)
|
IturanSensor(coordinator, vehicle.license_plate, description)
|
||||||
|
for vehicle in coordinator.data.values()
|
||||||
for description in SENSOR_TYPES
|
for description in SENSOR_TYPES
|
||||||
for license_plate in coordinator.data
|
if description.supported_fn(vehicle)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class IturanSensor(IturanBaseEntity, SensorEntity):
|
class IturanSensor(IturanBaseEntity, SensorEntity):
|
||||||
"""Ituran device tracker."""
|
"""Ituran sensor."""
|
||||||
|
|
||||||
entity_description: IturanSensorEntityDescription
|
entity_description: IturanSensorEntityDescription
|
||||||
|
|
||||||
@ -113,7 +133,7 @@ class IturanSensor(IturanBaseEntity, SensorEntity):
|
|||||||
super().__init__(coordinator, license_plate, description.key)
|
super().__init__(coordinator, license_plate, description.key)
|
||||||
self.entity_description = description
|
self.entity_description = description
|
||||||
|
|
||||||
@property
|
@cached_property
|
||||||
def native_value(self) -> StateType | datetime:
|
def native_value(self) -> StateType | datetime:
|
||||||
"""Return the state of the device."""
|
"""Return the state of the device."""
|
||||||
return self.entity_description.value_fn(self.vehicle)
|
return self.entity_description.value_fn(self.vehicle)
|
||||||
|
@ -40,6 +40,9 @@
|
|||||||
"address": {
|
"address": {
|
||||||
"name": "Address"
|
"name": "Address"
|
||||||
},
|
},
|
||||||
|
"battery_range": {
|
||||||
|
"name": "Remaining range"
|
||||||
|
},
|
||||||
"battery_voltage": {
|
"battery_voltage": {
|
||||||
"name": "Battery voltage"
|
"name": "Battery voltage"
|
||||||
},
|
},
|
||||||
|
@ -47,7 +47,7 @@ def mock_config_entry() -> MockConfigEntry:
|
|||||||
class MockVehicle:
|
class MockVehicle:
|
||||||
"""Mock vehicle."""
|
"""Mock vehicle."""
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self, is_electric_vehicle=False) -> None:
|
||||||
"""Initialize mock vehicle."""
|
"""Initialize mock vehicle."""
|
||||||
self.license_plate = "12345678"
|
self.license_plate = "12345678"
|
||||||
self.make = "mock make"
|
self.make = "mock make"
|
||||||
@ -61,11 +61,18 @@ class MockVehicle:
|
|||||||
2024, 1, 1, 0, 0, 0, tzinfo=ZoneInfo("Asia/Jerusalem")
|
2024, 1, 1, 0, 0, 0, tzinfo=ZoneInfo("Asia/Jerusalem")
|
||||||
)
|
)
|
||||||
self.battery_voltage = 12.0
|
self.battery_voltage = 12.0
|
||||||
|
self.is_electric_vehicle = is_electric_vehicle
|
||||||
|
if is_electric_vehicle:
|
||||||
|
self.battery_level = 42
|
||||||
|
self.battery_range = 150
|
||||||
|
else:
|
||||||
|
self.battery_level = 0
|
||||||
|
self.battery_range = 0
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def mock_ituran() -> Generator[AsyncMock]:
|
def mock_ituran(request: pytest.FixtureRequest) -> Generator[AsyncMock]:
|
||||||
"""Return a mocked PalazzettiClient."""
|
"""Return a mocked Ituran."""
|
||||||
with (
|
with (
|
||||||
patch(
|
patch(
|
||||||
"homeassistant.components.ituran.coordinator.Ituran",
|
"homeassistant.components.ituran.coordinator.Ituran",
|
||||||
@ -79,7 +86,8 @@ def mock_ituran() -> Generator[AsyncMock]:
|
|||||||
mock_ituran = ituran.return_value
|
mock_ituran = ituran.return_value
|
||||||
mock_ituran.is_authenticated.return_value = False
|
mock_ituran.is_authenticated.return_value = False
|
||||||
mock_ituran.authenticate.return_value = True
|
mock_ituran.authenticate.return_value = True
|
||||||
mock_ituran.get_vehicles.return_value = [MockVehicle()]
|
is_electric_vehicle = getattr(request, "param", False)
|
||||||
|
mock_ituran.get_vehicles.return_value = [MockVehicle(is_electric_vehicle)]
|
||||||
type(mock_ituran).mobile_id = PropertyMock(
|
type(mock_ituran).mobile_id = PropertyMock(
|
||||||
return_value=MOCK_CONFIG_DATA[CONF_MOBILE_ID]
|
return_value=MOCK_CONFIG_DATA[CONF_MOBILE_ID]
|
||||||
)
|
)
|
||||||
|
@ -1,4 +1,415 @@
|
|||||||
# serializer version: 1
|
# serializer version: 1
|
||||||
|
# name: test_ev_sensor[True][sensor.mock_model_address-entry]
|
||||||
|
EntityRegistryEntrySnapshot({
|
||||||
|
'aliases': set({
|
||||||
|
}),
|
||||||
|
'area_id': None,
|
||||||
|
'capabilities': None,
|
||||||
|
'config_entry_id': <ANY>,
|
||||||
|
'config_subentry_id': <ANY>,
|
||||||
|
'device_class': None,
|
||||||
|
'device_id': <ANY>,
|
||||||
|
'disabled_by': None,
|
||||||
|
'domain': 'sensor',
|
||||||
|
'entity_category': None,
|
||||||
|
'entity_id': 'sensor.mock_model_address',
|
||||||
|
'has_entity_name': True,
|
||||||
|
'hidden_by': None,
|
||||||
|
'icon': None,
|
||||||
|
'id': <ANY>,
|
||||||
|
'labels': set({
|
||||||
|
}),
|
||||||
|
'name': None,
|
||||||
|
'options': dict({
|
||||||
|
}),
|
||||||
|
'original_device_class': None,
|
||||||
|
'original_icon': None,
|
||||||
|
'original_name': 'Address',
|
||||||
|
'platform': 'ituran',
|
||||||
|
'previous_unique_id': None,
|
||||||
|
'suggested_object_id': None,
|
||||||
|
'supported_features': 0,
|
||||||
|
'translation_key': 'address',
|
||||||
|
'unique_id': '12345678-address',
|
||||||
|
'unit_of_measurement': None,
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_ev_sensor[True][sensor.mock_model_address-state]
|
||||||
|
StateSnapshot({
|
||||||
|
'attributes': ReadOnlyDict({
|
||||||
|
'friendly_name': 'mock model Address',
|
||||||
|
}),
|
||||||
|
'context': <ANY>,
|
||||||
|
'entity_id': 'sensor.mock_model_address',
|
||||||
|
'last_changed': <ANY>,
|
||||||
|
'last_reported': <ANY>,
|
||||||
|
'last_updated': <ANY>,
|
||||||
|
'state': 'Bermuda Triangle',
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_ev_sensor[True][sensor.mock_model_battery-entry]
|
||||||
|
EntityRegistryEntrySnapshot({
|
||||||
|
'aliases': set({
|
||||||
|
}),
|
||||||
|
'area_id': None,
|
||||||
|
'capabilities': None,
|
||||||
|
'config_entry_id': <ANY>,
|
||||||
|
'config_subentry_id': <ANY>,
|
||||||
|
'device_class': None,
|
||||||
|
'device_id': <ANY>,
|
||||||
|
'disabled_by': None,
|
||||||
|
'domain': 'sensor',
|
||||||
|
'entity_category': None,
|
||||||
|
'entity_id': 'sensor.mock_model_battery',
|
||||||
|
'has_entity_name': True,
|
||||||
|
'hidden_by': None,
|
||||||
|
'icon': None,
|
||||||
|
'id': <ANY>,
|
||||||
|
'labels': set({
|
||||||
|
}),
|
||||||
|
'name': None,
|
||||||
|
'options': dict({
|
||||||
|
}),
|
||||||
|
'original_device_class': <SensorDeviceClass.BATTERY: 'battery'>,
|
||||||
|
'original_icon': None,
|
||||||
|
'original_name': 'Battery',
|
||||||
|
'platform': 'ituran',
|
||||||
|
'previous_unique_id': None,
|
||||||
|
'suggested_object_id': None,
|
||||||
|
'supported_features': 0,
|
||||||
|
'translation_key': None,
|
||||||
|
'unique_id': '12345678-battery_level',
|
||||||
|
'unit_of_measurement': '%',
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_ev_sensor[True][sensor.mock_model_battery-state]
|
||||||
|
StateSnapshot({
|
||||||
|
'attributes': ReadOnlyDict({
|
||||||
|
'device_class': 'battery',
|
||||||
|
'friendly_name': 'mock model Battery',
|
||||||
|
'unit_of_measurement': '%',
|
||||||
|
}),
|
||||||
|
'context': <ANY>,
|
||||||
|
'entity_id': 'sensor.mock_model_battery',
|
||||||
|
'last_changed': <ANY>,
|
||||||
|
'last_reported': <ANY>,
|
||||||
|
'last_updated': <ANY>,
|
||||||
|
'state': '42',
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_ev_sensor[True][sensor.mock_model_battery_voltage-entry]
|
||||||
|
EntityRegistryEntrySnapshot({
|
||||||
|
'aliases': set({
|
||||||
|
}),
|
||||||
|
'area_id': None,
|
||||||
|
'capabilities': None,
|
||||||
|
'config_entry_id': <ANY>,
|
||||||
|
'config_subentry_id': <ANY>,
|
||||||
|
'device_class': None,
|
||||||
|
'device_id': <ANY>,
|
||||||
|
'disabled_by': None,
|
||||||
|
'domain': 'sensor',
|
||||||
|
'entity_category': None,
|
||||||
|
'entity_id': 'sensor.mock_model_battery_voltage',
|
||||||
|
'has_entity_name': True,
|
||||||
|
'hidden_by': None,
|
||||||
|
'icon': None,
|
||||||
|
'id': <ANY>,
|
||||||
|
'labels': set({
|
||||||
|
}),
|
||||||
|
'name': None,
|
||||||
|
'options': dict({
|
||||||
|
'sensor': dict({
|
||||||
|
'suggested_display_precision': 0,
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
'original_device_class': <SensorDeviceClass.VOLTAGE: 'voltage'>,
|
||||||
|
'original_icon': None,
|
||||||
|
'original_name': 'Battery voltage',
|
||||||
|
'platform': 'ituran',
|
||||||
|
'previous_unique_id': None,
|
||||||
|
'suggested_object_id': None,
|
||||||
|
'supported_features': 0,
|
||||||
|
'translation_key': 'battery_voltage',
|
||||||
|
'unique_id': '12345678-battery_voltage',
|
||||||
|
'unit_of_measurement': <UnitOfElectricPotential.VOLT: 'V'>,
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_ev_sensor[True][sensor.mock_model_battery_voltage-state]
|
||||||
|
StateSnapshot({
|
||||||
|
'attributes': ReadOnlyDict({
|
||||||
|
'device_class': 'voltage',
|
||||||
|
'friendly_name': 'mock model Battery voltage',
|
||||||
|
'unit_of_measurement': <UnitOfElectricPotential.VOLT: 'V'>,
|
||||||
|
}),
|
||||||
|
'context': <ANY>,
|
||||||
|
'entity_id': 'sensor.mock_model_battery_voltage',
|
||||||
|
'last_changed': <ANY>,
|
||||||
|
'last_reported': <ANY>,
|
||||||
|
'last_updated': <ANY>,
|
||||||
|
'state': '12.0',
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_ev_sensor[True][sensor.mock_model_heading-entry]
|
||||||
|
EntityRegistryEntrySnapshot({
|
||||||
|
'aliases': set({
|
||||||
|
}),
|
||||||
|
'area_id': None,
|
||||||
|
'capabilities': None,
|
||||||
|
'config_entry_id': <ANY>,
|
||||||
|
'config_subentry_id': <ANY>,
|
||||||
|
'device_class': None,
|
||||||
|
'device_id': <ANY>,
|
||||||
|
'disabled_by': None,
|
||||||
|
'domain': 'sensor',
|
||||||
|
'entity_category': None,
|
||||||
|
'entity_id': 'sensor.mock_model_heading',
|
||||||
|
'has_entity_name': True,
|
||||||
|
'hidden_by': None,
|
||||||
|
'icon': None,
|
||||||
|
'id': <ANY>,
|
||||||
|
'labels': set({
|
||||||
|
}),
|
||||||
|
'name': None,
|
||||||
|
'options': dict({
|
||||||
|
'sensor': dict({
|
||||||
|
'suggested_display_precision': 0,
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
'original_device_class': None,
|
||||||
|
'original_icon': None,
|
||||||
|
'original_name': 'Heading',
|
||||||
|
'platform': 'ituran',
|
||||||
|
'previous_unique_id': None,
|
||||||
|
'suggested_object_id': None,
|
||||||
|
'supported_features': 0,
|
||||||
|
'translation_key': 'heading',
|
||||||
|
'unique_id': '12345678-heading',
|
||||||
|
'unit_of_measurement': '°',
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_ev_sensor[True][sensor.mock_model_heading-state]
|
||||||
|
StateSnapshot({
|
||||||
|
'attributes': ReadOnlyDict({
|
||||||
|
'friendly_name': 'mock model Heading',
|
||||||
|
'unit_of_measurement': '°',
|
||||||
|
}),
|
||||||
|
'context': <ANY>,
|
||||||
|
'entity_id': 'sensor.mock_model_heading',
|
||||||
|
'last_changed': <ANY>,
|
||||||
|
'last_reported': <ANY>,
|
||||||
|
'last_updated': <ANY>,
|
||||||
|
'state': '150',
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_ev_sensor[True][sensor.mock_model_last_update_from_vehicle-entry]
|
||||||
|
EntityRegistryEntrySnapshot({
|
||||||
|
'aliases': set({
|
||||||
|
}),
|
||||||
|
'area_id': None,
|
||||||
|
'capabilities': None,
|
||||||
|
'config_entry_id': <ANY>,
|
||||||
|
'config_subentry_id': <ANY>,
|
||||||
|
'device_class': None,
|
||||||
|
'device_id': <ANY>,
|
||||||
|
'disabled_by': None,
|
||||||
|
'domain': 'sensor',
|
||||||
|
'entity_category': None,
|
||||||
|
'entity_id': 'sensor.mock_model_last_update_from_vehicle',
|
||||||
|
'has_entity_name': True,
|
||||||
|
'hidden_by': None,
|
||||||
|
'icon': None,
|
||||||
|
'id': <ANY>,
|
||||||
|
'labels': set({
|
||||||
|
}),
|
||||||
|
'name': None,
|
||||||
|
'options': dict({
|
||||||
|
}),
|
||||||
|
'original_device_class': <SensorDeviceClass.TIMESTAMP: 'timestamp'>,
|
||||||
|
'original_icon': None,
|
||||||
|
'original_name': 'Last update from vehicle',
|
||||||
|
'platform': 'ituran',
|
||||||
|
'previous_unique_id': None,
|
||||||
|
'suggested_object_id': None,
|
||||||
|
'supported_features': 0,
|
||||||
|
'translation_key': 'last_update_from_vehicle',
|
||||||
|
'unique_id': '12345678-last_update_from_vehicle',
|
||||||
|
'unit_of_measurement': None,
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_ev_sensor[True][sensor.mock_model_last_update_from_vehicle-state]
|
||||||
|
StateSnapshot({
|
||||||
|
'attributes': ReadOnlyDict({
|
||||||
|
'device_class': 'timestamp',
|
||||||
|
'friendly_name': 'mock model Last update from vehicle',
|
||||||
|
}),
|
||||||
|
'context': <ANY>,
|
||||||
|
'entity_id': 'sensor.mock_model_last_update_from_vehicle',
|
||||||
|
'last_changed': <ANY>,
|
||||||
|
'last_reported': <ANY>,
|
||||||
|
'last_updated': <ANY>,
|
||||||
|
'state': '2023-12-31T22:00:00+00:00',
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_ev_sensor[True][sensor.mock_model_mileage-entry]
|
||||||
|
EntityRegistryEntrySnapshot({
|
||||||
|
'aliases': set({
|
||||||
|
}),
|
||||||
|
'area_id': None,
|
||||||
|
'capabilities': None,
|
||||||
|
'config_entry_id': <ANY>,
|
||||||
|
'config_subentry_id': <ANY>,
|
||||||
|
'device_class': None,
|
||||||
|
'device_id': <ANY>,
|
||||||
|
'disabled_by': None,
|
||||||
|
'domain': 'sensor',
|
||||||
|
'entity_category': None,
|
||||||
|
'entity_id': 'sensor.mock_model_mileage',
|
||||||
|
'has_entity_name': True,
|
||||||
|
'hidden_by': None,
|
||||||
|
'icon': None,
|
||||||
|
'id': <ANY>,
|
||||||
|
'labels': set({
|
||||||
|
}),
|
||||||
|
'name': None,
|
||||||
|
'options': dict({
|
||||||
|
'sensor': dict({
|
||||||
|
'suggested_display_precision': 2,
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
'original_device_class': <SensorDeviceClass.DISTANCE: 'distance'>,
|
||||||
|
'original_icon': None,
|
||||||
|
'original_name': 'Mileage',
|
||||||
|
'platform': 'ituran',
|
||||||
|
'previous_unique_id': None,
|
||||||
|
'suggested_object_id': None,
|
||||||
|
'supported_features': 0,
|
||||||
|
'translation_key': 'mileage',
|
||||||
|
'unique_id': '12345678-mileage',
|
||||||
|
'unit_of_measurement': <UnitOfLength.KILOMETERS: 'km'>,
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_ev_sensor[True][sensor.mock_model_mileage-state]
|
||||||
|
StateSnapshot({
|
||||||
|
'attributes': ReadOnlyDict({
|
||||||
|
'device_class': 'distance',
|
||||||
|
'friendly_name': 'mock model Mileage',
|
||||||
|
'unit_of_measurement': <UnitOfLength.KILOMETERS: 'km'>,
|
||||||
|
}),
|
||||||
|
'context': <ANY>,
|
||||||
|
'entity_id': 'sensor.mock_model_mileage',
|
||||||
|
'last_changed': <ANY>,
|
||||||
|
'last_reported': <ANY>,
|
||||||
|
'last_updated': <ANY>,
|
||||||
|
'state': '1000',
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_ev_sensor[True][sensor.mock_model_remaining_range-entry]
|
||||||
|
EntityRegistryEntrySnapshot({
|
||||||
|
'aliases': set({
|
||||||
|
}),
|
||||||
|
'area_id': None,
|
||||||
|
'capabilities': None,
|
||||||
|
'config_entry_id': <ANY>,
|
||||||
|
'config_subentry_id': <ANY>,
|
||||||
|
'device_class': None,
|
||||||
|
'device_id': <ANY>,
|
||||||
|
'disabled_by': None,
|
||||||
|
'domain': 'sensor',
|
||||||
|
'entity_category': None,
|
||||||
|
'entity_id': 'sensor.mock_model_remaining_range',
|
||||||
|
'has_entity_name': True,
|
||||||
|
'hidden_by': None,
|
||||||
|
'icon': None,
|
||||||
|
'id': <ANY>,
|
||||||
|
'labels': set({
|
||||||
|
}),
|
||||||
|
'name': None,
|
||||||
|
'options': dict({
|
||||||
|
'sensor': dict({
|
||||||
|
'suggested_display_precision': 0,
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
'original_device_class': <SensorDeviceClass.DISTANCE: 'distance'>,
|
||||||
|
'original_icon': None,
|
||||||
|
'original_name': 'Remaining range',
|
||||||
|
'platform': 'ituran',
|
||||||
|
'previous_unique_id': None,
|
||||||
|
'suggested_object_id': None,
|
||||||
|
'supported_features': 0,
|
||||||
|
'translation_key': 'battery_range',
|
||||||
|
'unique_id': '12345678-battery_range',
|
||||||
|
'unit_of_measurement': <UnitOfLength.KILOMETERS: 'km'>,
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_ev_sensor[True][sensor.mock_model_remaining_range-state]
|
||||||
|
StateSnapshot({
|
||||||
|
'attributes': ReadOnlyDict({
|
||||||
|
'device_class': 'distance',
|
||||||
|
'friendly_name': 'mock model Remaining range',
|
||||||
|
'unit_of_measurement': <UnitOfLength.KILOMETERS: 'km'>,
|
||||||
|
}),
|
||||||
|
'context': <ANY>,
|
||||||
|
'entity_id': 'sensor.mock_model_remaining_range',
|
||||||
|
'last_changed': <ANY>,
|
||||||
|
'last_reported': <ANY>,
|
||||||
|
'last_updated': <ANY>,
|
||||||
|
'state': '150',
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_ev_sensor[True][sensor.mock_model_speed-entry]
|
||||||
|
EntityRegistryEntrySnapshot({
|
||||||
|
'aliases': set({
|
||||||
|
}),
|
||||||
|
'area_id': None,
|
||||||
|
'capabilities': None,
|
||||||
|
'config_entry_id': <ANY>,
|
||||||
|
'config_subentry_id': <ANY>,
|
||||||
|
'device_class': None,
|
||||||
|
'device_id': <ANY>,
|
||||||
|
'disabled_by': None,
|
||||||
|
'domain': 'sensor',
|
||||||
|
'entity_category': None,
|
||||||
|
'entity_id': 'sensor.mock_model_speed',
|
||||||
|
'has_entity_name': True,
|
||||||
|
'hidden_by': None,
|
||||||
|
'icon': None,
|
||||||
|
'id': <ANY>,
|
||||||
|
'labels': set({
|
||||||
|
}),
|
||||||
|
'name': None,
|
||||||
|
'options': dict({
|
||||||
|
'sensor': dict({
|
||||||
|
'suggested_display_precision': 0,
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
'original_device_class': <SensorDeviceClass.SPEED: 'speed'>,
|
||||||
|
'original_icon': None,
|
||||||
|
'original_name': 'Speed',
|
||||||
|
'platform': 'ituran',
|
||||||
|
'previous_unique_id': None,
|
||||||
|
'suggested_object_id': None,
|
||||||
|
'supported_features': 0,
|
||||||
|
'translation_key': None,
|
||||||
|
'unique_id': '12345678-speed',
|
||||||
|
'unit_of_measurement': <UnitOfSpeed.KILOMETERS_PER_HOUR: 'km/h'>,
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_ev_sensor[True][sensor.mock_model_speed-state]
|
||||||
|
StateSnapshot({
|
||||||
|
'attributes': ReadOnlyDict({
|
||||||
|
'device_class': 'speed',
|
||||||
|
'friendly_name': 'mock model Speed',
|
||||||
|
'unit_of_measurement': <UnitOfSpeed.KILOMETERS_PER_HOUR: 'km/h'>,
|
||||||
|
}),
|
||||||
|
'context': <ANY>,
|
||||||
|
'entity_id': 'sensor.mock_model_speed',
|
||||||
|
'last_changed': <ANY>,
|
||||||
|
'last_reported': <ANY>,
|
||||||
|
'last_updated': <ANY>,
|
||||||
|
'state': '20',
|
||||||
|
})
|
||||||
|
# ---
|
||||||
# name: test_sensor[sensor.mock_model_address-entry]
|
# name: test_sensor[sensor.mock_model_address-entry]
|
||||||
EntityRegistryEntrySnapshot({
|
EntityRegistryEntrySnapshot({
|
||||||
'aliases': set({
|
'aliases': set({
|
||||||
|
@ -32,13 +32,27 @@ async def test_sensor(
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||||
async def test_availability(
|
@pytest.mark.parametrize("mock_ituran", [True], indirect=True)
|
||||||
|
async def test_ev_sensor(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
entity_registry: er.EntityRegistry,
|
||||||
|
snapshot: SnapshotAssertion,
|
||||||
|
mock_ituran: AsyncMock,
|
||||||
|
mock_config_entry: MockConfigEntry,
|
||||||
|
) -> None:
|
||||||
|
"""Test state of sensor."""
|
||||||
|
with patch("homeassistant.components.ituran.PLATFORMS", [Platform.SENSOR]):
|
||||||
|
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,
|
hass: HomeAssistant,
|
||||||
freezer: FrozenDateTimeFactory,
|
freezer: FrozenDateTimeFactory,
|
||||||
mock_ituran: AsyncMock,
|
mock_ituran: AsyncMock,
|
||||||
mock_config_entry: MockConfigEntry,
|
mock_config_entry: MockConfigEntry,
|
||||||
|
ev_entity_names: list[str] | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test sensor is marked as unavailable when we can't reach the Ituran service."""
|
|
||||||
entities = [
|
entities = [
|
||||||
"sensor.mock_model_address",
|
"sensor.mock_model_address",
|
||||||
"sensor.mock_model_battery_voltage",
|
"sensor.mock_model_battery_voltage",
|
||||||
@ -46,6 +60,7 @@ async def test_availability(
|
|||||||
"sensor.mock_model_last_update_from_vehicle",
|
"sensor.mock_model_last_update_from_vehicle",
|
||||||
"sensor.mock_model_mileage",
|
"sensor.mock_model_mileage",
|
||||||
"sensor.mock_model_speed",
|
"sensor.mock_model_speed",
|
||||||
|
*(ev_entity_names if ev_entity_names is not None else []),
|
||||||
]
|
]
|
||||||
|
|
||||||
await setup_integration(hass, mock_config_entry)
|
await setup_integration(hass, mock_config_entry)
|
||||||
@ -74,3 +89,32 @@ async def test_availability(
|
|||||||
state = hass.states.get(entity_id)
|
state = hass.states.get(entity_id)
|
||||||
assert state
|
assert state
|
||||||
assert state.state != STATE_UNAVAILABLE
|
assert state.state != STATE_UNAVAILABLE
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||||
|
async def test_availability(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
freezer: FrozenDateTimeFactory,
|
||||||
|
mock_ituran: AsyncMock,
|
||||||
|
mock_config_entry: MockConfigEntry,
|
||||||
|
) -> None:
|
||||||
|
"""Test ICE sensor is marked as unavailable when we can't reach the Ituran service."""
|
||||||
|
await __test_availability(hass, freezer, mock_ituran, mock_config_entry)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||||
|
@pytest.mark.parametrize("mock_ituran", [True], indirect=True)
|
||||||
|
async def test_ev_availability(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
freezer: FrozenDateTimeFactory,
|
||||||
|
mock_ituran: AsyncMock,
|
||||||
|
mock_config_entry: MockConfigEntry,
|
||||||
|
) -> None:
|
||||||
|
"""Test EV sensor is marked as unavailable when we can't reach the Ituran service."""
|
||||||
|
ev_entities = [
|
||||||
|
"sensor.mock_model_battery",
|
||||||
|
"sensor.mock_model_remaining_range",
|
||||||
|
]
|
||||||
|
await __test_availability(
|
||||||
|
hass, freezer, mock_ituran, mock_config_entry, ev_entities
|
||||||
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user