Skip to content

Commit

Permalink
Merge branch 'develop' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
fwmarcel committed Feb 3, 2025
2 parents 79087db + d9f04a6 commit b5b2b7d
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 17 deletions.
4 changes: 1 addition & 3 deletions custom_components/divera/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,7 @@ async def async_step_api(self, user_input: dict[str, Any] | None = None):
if not errors:
await self.check_unique_id()

# normal users only have group id 8
ucr_id = self._divera_client.get_default_ucr()
if self._divera_client.get_usergroup_id(ucr_id) != 8:
if not self._divera_client.check_usergroup_id():
return self.async_abort(reason="not_supported")

if self._divera_client.get_ucr_count() > 1:
Expand Down
93 changes: 84 additions & 9 deletions custom_components/divera/divera.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,76 @@ def get_last_news_attributes(self) -> dict:
"self_addressed": news.get("ucr_self_addressed"),
}

def get_vehicle_id_list(self):
"""Retrieve the IDs of all vehicles.
Returns:
list[int]: A list containing all the vehicle IDs.
"""
return list(self.__data["data"]["cluster"]["vehicle"].keys())

def get_vehicle_state(self, vehicle_id: str) -> dict:
"""Retrieve the state of a vehicle by its ID.
Args:
vehicle_id (str): The ID of the vehicle.
Returns:
dict: The state of the vehicle, or an empty dictionary if the vehicle or key 'fmsstatus_id' is not found.
Raises:
KeyError: If the vehicle ID or key 'fmsstatus_id' is not found in the data dictionary.
"""
try:
vehicle = self.__data["data"]["cluster"]["vehicle"][vehicle_id]
return vehicle.get("fmsstatus_id", STATE_UNKNOWN)
except KeyError:
LOGGER.error(
f"Vehicle with ID {vehicle_id} or key 'fmsstatus_id' not found."
)
return {}

def get_vehicle_attributes(self, vehicle_id: str) -> dict:
"""Retrieve the status attributes of a vehicle by its ID.
Args:
vehicle_id (str): The ID of the vehicle.
Returns:
dict: A dictionary containing the status attributes of the vehicle, including
fullname, shortname, name, fmsstatus_note, fmsstatus_ts, latitude, longitude,
opta, issi, and number. If the vehicle or required keys are not found, an
empty dictionary is returned.
Raises:
KeyError: If the required keys are not found in the data dictionary.
"""
try:
vehicle_status = self.__data["data"]["cluster"]["vehicle"][vehicle_id]
fmsstatus_timestamp = datetime.fromtimestamp(
vehicle_status.get("fmsstatus_ts"), tz=get_default_time_zone()
)
return {
"fullname": vehicle_status.get("fullname"),
"shortname": vehicle_status.get("shortname"),
"name": vehicle_status.get("name"),
"fmsstatus_note": vehicle_status.get("fmsstatus_note"),
"fmsstatus_ts": fmsstatus_timestamp,
"latitude": vehicle_status.get("lat"),
"longitude": vehicle_status.get("lng"),
"opta": vehicle_status.get("opta"),
"issi": vehicle_status.get("issi"),
"number": vehicle_status.get("number"),
}
except KeyError:
LOGGER.error(
f"Vehicle with ID {vehicle_id} or one of the required keys not found."
)
return {}

def get_group_name_by_id(self, group_id):
"""Return the name from the given group id.
Expand Down Expand Up @@ -698,20 +768,25 @@ async def set_user_state_by_name(self, option: str):
sid = self.get_state_id_by_name(option)
await self.set_user_state_by_id(sid)

def get_usergroup_id(self, ucr_id):
"""Retrieve the ID of the usergroup associated with the given User Cluster Relation (UCR) ID.
def check_usergroup_id(self):
"""Check if the user's group ID is allowed.
Args:
ucr_id (int): The ID of the User Cluster Relation (UCR) to retrieve the cluster ID for.
This method retrieves the user's group ID from the stored data and verifies whether it belongs
to the set of allowed group IDs (4 or 8). If the ID is valid, it returns True. Otherwise, it logs
a warning and returns False.
Returns:
int: The ID of the usergroup associated with the specified UCR ID.
Raises:
KeyError: If the required keys are not found in the data dictionary.
bool: True if the user belongs to an allowed group (ID 4 or 8), False otherwise.
"""
return self.__data["data"]["ucr"][str(ucr_id)]["usergroup_id"]
# normal users only have group id 8 or 4
ucr_id = self._divera_client.get_default_ucr()
usergroup_id = self.__data["data"]["ucr"][str(ucr_id)]["usergroup_id"]
if usergroup_id in {8, 4}:
return True

LOGGER.warning("Unsupported Usergroup ID: %s", usergroup_id)
return False


class DiveraError(Exception):
Expand Down
28 changes: 28 additions & 0 deletions custom_components/divera/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,39 @@ async def async_setup_entry(
"""

coordinators = entry.runtime_data.coordinators

entities: list[DiveraSensorEntity] = [
DiveraSensorEntity(coordinators[ucr_id], description)
for ucr_id in coordinators
for description in SENSORS
]

# TODO Vehicles
for ucr_id in coordinators:
divera_client = coordinators[ucr_id]
vehicle_id_list = divera_client.data.get_vehicle_id_list()
for vehicle_id in vehicle_id_list:
vehicle_attributes = divera_client.data.get_vehicle_attributes(vehicle_id)
vehicle_name = vehicle_attributes.get("name", f"Vehicle {vehicle_id}")

vehicle_entity = DiveraSensorEntity(
coordinators[ucr_id],
DiveraSensorEntityDescription(
key=vehicle_id,
translation_key="vehicle",
translation_placeholders={
"vehicle_name": vehicle_name,
},
icon="mdi:fire-truck",
value_fn=lambda divera, vid=vehicle_id: divera.get_vehicle_state(
vehicle_id # noqa: B023
),
attribute_fn=lambda divera,
vid=vehicle_id: divera.get_vehicle_attributes(vehicle_id), # noqa: B023
),
)
entities.append(vehicle_entity)

async_add_entities(entities, False)


Expand Down
3 changes: 3 additions & 0 deletions custom_components/divera/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
},
"news": {
"name": "letzte News"
},
"vehicle": {
"name": "Fahrzeug Status {vehicle_name}"
}
},
"binary_sensor": {
Expand Down
3 changes: 3 additions & 0 deletions custom_components/divera/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
},
"news": {
"name": "last News"
},
"vehicle": {
"name": "vehicle status {vehicle_name}"
}
},
"binary_sensor": {
Expand Down
8 changes: 4 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
colorlog==6.9.0
ruff==0.9.3
ruff==0.9.4
pip>=25,<26
black==24.10.0
black==25.1.0
pre-commit==4.1.0
pytest-homeassistant-custom-component==0.13.205
pytest-cov==5.0.0
pytest-homeassistant-custom-component==0.13.208
pytest-cov==6.0.0
2 changes: 1 addition & 1 deletion scripts/prepare-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
VERSION=$1

cd custom_components/divera
sed -i "s/0.0.0/${VERSION}/g" manifest.json
sed -i "s/\"version\": \"[0-9]\+\.[0-9]\+\.[0-9]\+\"/\"version\": \"${VERSION}\"/g" yourfile.json
zip divera.zip -r ./

0 comments on commit b5b2b7d

Please sign in to comment.