Skip to content

Commit

Permalink
Add extra sensors (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
DCSBL authored Jun 6, 2024
1 parent 1d01f29 commit 893a951
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ tts/
.cloud/
*.log*
*.db
*.db-shm
*.db-wal
6 changes: 4 additions & 2 deletions custom_components/flitsmeister/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Config flow for Picnic integration."""
"""Config flow for Flitsmeister integration."""
from __future__ import annotations

import logging
Expand All @@ -10,6 +10,7 @@
from homeassistant.const import (
CONF_ACCESS_TOKEN,
CONF_TOKEN,
CONF_UNIQUE_ID,
CONF_PASSWORD,
CONF_TOKEN,
CONF_USERNAME,
Expand Down Expand Up @@ -58,6 +59,7 @@ async def async_step_login(self, user_input=None, errors=None) -> FlowResult:

data = {
CONF_USERNAME: user_input[CONF_USERNAME],
CONF_UNIQUE_ID: auth.object_id,
CONF_ACCESS_TOKEN: auth.access_token,
CONF_TOKEN: auth.session_token,
}
Expand All @@ -74,7 +76,7 @@ async def async_step_login(self, user_input=None, errors=None) -> FlowResult:

return self.async_abort(reason="reauth_successful")

await self.async_set_unique_id(user_input[CONF_USERNAME])
await self.async_set_unique_id(data[CONF_UNIQUE_ID])
self._abort_if_unique_id_configured()

return await self._async_create_entry(data)
Expand Down
87 changes: 81 additions & 6 deletions custom_components/flitsmeister/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,100 @@ class FMSensorEntityDescription(SensorEntityDescription):


SENSOR_TYPES: tuple[FMSensorEntityDescription, ...] = (
FMSensorEntityDescription(
key="km_driven",
name="Distance driven",
native_unit_of_measurement=UnitOfLength.KILOMETERS,
suggested_display_precision=0,
state_class=SensorStateClass.TOTAL_INCREASING,
value_fn=lambda data: data[DATA_STATISTICS].km_driven,
device_class=SensorDeviceClass.DISTANCE,
),
FMSensorEntityDescription(
key="fines_avoided",
name="Fines avoided",
native_unit_of_measurement="fines",
suggested_display_precision=0,
state_class=SensorStateClass.TOTAL_INCREASING,
value_fn=lambda data: data[DATA_STATISTICS].fines_avoided,
icon="mdi:cash-check",
),
FMSensorEntityDescription(
key="sec_driven",
name="Time driven",
native_unit_of_measurement="s",
suggested_display_precision=0,
state_class=SensorStateClass.TOTAL_INCREASING,
value_fn=lambda data: data[DATA_STATISTICS].sec_driven,
device_class=SensorDeviceClass.DURATION,
),
FMSensorEntityDescription(
key="times_in_traffic",
name="Times in traffic",
native_unit_of_measurement="times",
suggested_display_precision=0,
state_class=SensorStateClass.TOTAL_INCREASING,
value_fn=lambda data: data[DATA_STATISTICS].times_in_traffic,
icon="mdi:car",
),
FMSensorEntityDescription(
key="top_100_sprint_ms",
name="Top 100 sprint",
native_unit_of_measurement="ms",
suggested_display_precision=0,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda data: data[DATA_STATISTICS].top_100_sprint_ms,
icon="mdi:car",
),

FMSensorEntityDescription(
key="top_consecutive_days",
name="Top consecutive days",
native_unit_of_measurement="days",
suggested_display_precision=0,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda data: data[DATA_STATISTICS].top_consecutive_days,
icon="mdi:calendar",
),
FMSensorEntityDescription(
key="top_speed",
name="Top speed",
native_unit_of_measurement=UnitOfSpeed.KILOMETERS_PER_HOUR,
suggested_display_precision=0,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda data: data[DATA_USER].statistics_top_speed,
value_fn=lambda data: data[DATA_STATISTICS].top_speed,
device_class=SensorDeviceClass.SPEED,
),
FMSensorEntityDescription(
key="km_driven",
name="Distance driven",
native_unit_of_measurement=UnitOfLength.KILOMETERS,
key="total_ratings",
name="Total ratings",
native_unit_of_measurement="ratings",
suggested_display_precision=0,
state_class=SensorStateClass.TOTAL_INCREASING,
value_fn=lambda data: data[DATA_STATISTICS].km_driven,
value_fn=lambda data: data[DATA_STATISTICS].total_ratings,
icon="mdi:star",
),
FMSensorEntityDescription
(
key="countries_visited",
name="Countries visited",
native_unit_of_measurement="countries",
suggested_display_precision=0,
state_class=SensorStateClass.TOTAL_INCREASING,
value_fn=lambda data: len(data[DATA_STATISTICS].countries_visited),
icon="mdi:earth",
),
FMSensorEntityDescription(
key="provinces_visited",
name="Provinces visited",
native_unit_of_measurement="provinces",
suggested_display_precision=0,
state_class=SensorStateClass.TOTAL_INCREASING,
value_fn=lambda data: len(data[DATA_STATISTICS].provinces_visited),
icon="mdi:map-marker-circle",
),
)


async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
Expand Down

0 comments on commit 893a951

Please sign in to comment.