diff --git a/custom_components/bouncie/sensor.py b/custom_components/bouncie/sensor.py index 1893ee5..c16d99e 100644 --- a/custom_components/bouncie/sensor.py +++ b/custom_components/bouncie/sensor.py @@ -68,7 +68,6 @@ def update_car_info_attributes(vehicle_info): key="car-odometer", icon="mdi:counter", name="Car Odometer", - device_class=SensorDeviceClass.DISTANCE, native_unit_of_measurement="mi", state_class=SensorStateClass.TOTAL_INCREASING, value_fn=lambda vehicle_info: int(vehicle_info["stats"]["odometer"]), @@ -94,9 +93,9 @@ def update_car_info_attributes(vehicle_info): key="car-speed", icon="mdi:speedometer", name="Car Speed", + state_class=SensorStateClass.MEASUREMENT, device_class=SensorDeviceClass.SPEED, - native_unit_of_measurement="mph", - value_fn=lambda vehicle_info: vehicle_info["stats"]["speed"], + value_fn=lambda vehicle_info: int(vehicle_info["stats"]["speed"]), extra_attrs_fn=update_car_stats_attributes, ), BouncieSensorEntityDescription( diff --git a/tests/const.py b/tests/const.py index 88a2372..2360fe3 100644 --- a/tests/const.py +++ b/tests/const.py @@ -63,7 +63,7 @@ }, "fuelLevel": 29.411764705882355, "isRunning": False, - "speed": 0, + "speed": 123.2446465, "mil": {"milOn": False, "lastUpdated": "2022-11-23T01:38:55.000Z"}, "battery": {"status": "normal", "lastUpdated": "2022-11-23T01:37:41.000Z"}, }, diff --git a/tests/test_sensor.py b/tests/test_sensor.py index ea92cd0..7da0982 100644 --- a/tests/test_sensor.py +++ b/tests/test_sensor.py @@ -26,6 +26,68 @@ async def test_car_info_sensor(hass: HomeAssistant) -> None: assert state.attributes["imei"] == const.MOCK_VEHICLES_RESPONSE[0]["imei"] +async def test_car_odometer_sensor(hass: HomeAssistant) -> None: + """Test getting all vehicles.""" + await setup_platform(hass, SENSOR_DOMAIN) + entity_registry = er.async_get(hass) + entry = entity_registry.async_get("sensor.my_prius_car_odometer") + assert entry is not None + state = hass.states.get("sensor.my_prius_car_odometer") + assert state.state == "120508" + + +async def test_car_address_sensor(hass: HomeAssistant) -> None: + """Test getting all vehicles.""" + await setup_platform(hass, SENSOR_DOMAIN) + entity_registry = er.async_get(hass) + entry = entity_registry.async_get("sensor.my_prius_car_address") + assert entry is not None + state = hass.states.get("sensor.my_prius_car_address") + assert ( + state.state == const.MOCK_VEHICLES_RESPONSE[0]["stats"]["location"]["address"] + ) + + +async def test_car_fuel_sensor(hass: HomeAssistant) -> None: + """Test getting all vehicles.""" + await setup_platform(hass, SENSOR_DOMAIN) + entity_registry = er.async_get(hass) + entry = entity_registry.async_get("sensor.my_prius_car_fuel") + assert entry is not None + state = hass.states.get("sensor.my_prius_car_fuel") + assert state.state == "29" + + +async def test_car_speed_sensor(hass: HomeAssistant) -> None: + """Test getting all vehicles.""" + await setup_platform(hass, SENSOR_DOMAIN) + entity_registry = er.async_get(hass) + entry = entity_registry.async_get("sensor.my_prius_car_speed") + assert entry is not None + state = hass.states.get("sensor.my_prius_car_speed") + assert state.state == str(int(const.MOCK_VEHICLES_RESPONSE[0]["stats"]["speed"])) + + +async def test_car_mil_sensor(hass: HomeAssistant) -> None: + """Test getting all vehicles.""" + await setup_platform(hass, SENSOR_DOMAIN) + entity_registry = er.async_get(hass) + entry = entity_registry.async_get("sensor.my_prius_car_mil") + assert entry is not None + state = hass.states.get("sensor.my_prius_car_mil") + assert state.state == str(const.MOCK_VEHICLES_RESPONSE[0]["stats"]["mil"]["milOn"]) + + +async def test_car_battery_sensor(hass: HomeAssistant) -> None: + """Test getting all vehicles.""" + await setup_platform(hass, SENSOR_DOMAIN) + entity_registry = er.async_get(hass) + entry = entity_registry.async_get("sensor.my_prius_car_battery") + assert entry is not None + state = hass.states.get("sensor.my_prius_car_battery") + assert state.state == const.MOCK_VEHICLES_RESPONSE[0]["stats"]["battery"]["status"] + + async def test_sensor_update(hass: HomeAssistant) -> None: """Test sensor auto-update.""" _, mock_controller = await setup_platform(hass, SENSOR_DOMAIN)