Skip to content

Commit

Permalink
add Gas Range and Gas Level sensors (#467)
Browse files Browse the repository at this point in the history
* add Gas Range and Gas Level sensors

+ update Combustion Range and Combustion level to take value from engine with gasoline or diesel engine_type

---------

Co-authored-by: Tomas Prvak <tprvak@cpicardgroup.com>
  • Loading branch information
prvakt and Tomas Prvak authored Dec 19, 2024
1 parent 5ed2d7b commit e11c638
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 5 deletions.
6 changes: 6 additions & 0 deletions custom_components/myskoda/icons.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,15 @@
"electric_range": {
"default": "mdi:ev-station"
},
"gas_range": {
"default": "mdi:gas-station"
},
"fuel_level": {
"default": "mdi:fuel"
},
"gas_level": {
"default": "mdi:gauge"
},
"remaining_charging_time": {
"default": "mdi:timer"
},
Expand Down
76 changes: 71 additions & 5 deletions custom_components/myskoda/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,18 @@ class CombustionRange(MySkodaSensor):
@property
def native_value(self) -> int | None: # noqa: D102
if range := self.vehicle.driving_range:
if range.primary_engine_range is not None:
return range.primary_engine_range.remaining_range_in_km
if primary := range.primary_engine_range:
if primary.engine_type in [EngineType.GASOLINE, EngineType.DIESEL]:
return primary.remaining_range_in_km
if secondary := range.secondary_engine_range:
if secondary.engine_type in [EngineType.GASOLINE, EngineType.DIESEL]:
return secondary.remaining_range_in_km

def required_capabilities(self) -> list[CapabilityId]:
return [CapabilityId.STATE, CapabilityId.FUEL_STATUS, CapabilityId.CHARGING_MQB]
def is_supported(self) -> bool:
if self.has_all_capabilities([CapabilityId.STATE, CapabilityId.FUEL_STATUS]):
if range := self.vehicle.driving_range:
return range.car_type == EngineType.HYBRID
return False


class ElectricRange(MySkodaSensor):
Expand All @@ -295,6 +302,60 @@ def required_capabilities(self) -> list[CapabilityId]:
return [CapabilityId.STATE, CapabilityId.FUEL_STATUS, CapabilityId.CHARGING_MQB]


class GasRange(MySkodaSensor):
"""The vehicle's gas range - only for hybrid CNG vehicles."""

entity_description = SensorEntityDescription(
key="gas_range",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfLength.KILOMETERS,
device_class=SensorDeviceClass.DISTANCE,
translation_key="gas_range",
)

@property
def native_value(self) -> int | None: # noqa: D102
if range := self.vehicle.driving_range:
if range.primary_engine_range is not None:
return range.primary_engine_range.remaining_range_in_km

def is_supported(self) -> bool:
if self.has_all_capabilities([CapabilityId.STATE, CapabilityId.FUEL_STATUS]):
if range := self.vehicle.driving_range:
return (
range.car_type == EngineType.HYBRID
and (primary_engine_range := range.primary_engine_range)
and primary_engine_range.engine_type == EngineType.CNG
)
return False


class GasLevel(MySkodaSensor):
"""The vehicle's gas level - only for hybrid CNG vehicles."""

entity_description = SensorEntityDescription(
key="gas_level",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=PERCENTAGE,
translation_key="gas_level",
)

@property
def native_value(self) -> int | None: # noqa: D102
if range := self.vehicle.driving_range:
return range.primary_engine_range.current_fuel_level_in_percent

def is_supported(self) -> bool:
if self.has_all_capabilities([CapabilityId.STATE, CapabilityId.FUEL_STATUS]):
if range := self.vehicle.driving_range:
return (
range.car_type == EngineType.HYBRID
and (primary_engine_range := range.primary_engine_range)
and primary_engine_range.engine_type == EngineType.CNG
)
return False


class FuelLevel(MySkodaSensor):
"""The vehicle's combustion engine fuel level - only for non electric vehicles."""

Expand All @@ -308,7 +369,12 @@ class FuelLevel(MySkodaSensor):
@property
def native_value(self) -> int | None: # noqa: D102
if range := self.vehicle.driving_range:
return range.primary_engine_range.current_fuel_level_in_percent
if primary := range.primary_engine_range:
if primary.engine_type in [EngineType.GASOLINE, EngineType.DIESEL]:
return primary.current_fuel_level_in_percent
if secondary := range.secondary_engine_range:
if secondary.engine_type in [EngineType.GASOLINE, EngineType.DIESEL]:
return secondary.current_fuel_level_in_percent

def required_capabilities(self) -> list[CapabilityId]:
return [CapabilityId.STATE, CapabilityId.FUEL_STATUS]
Expand Down
6 changes: 6 additions & 0 deletions custom_components/myskoda/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,12 @@
"adblue_range": {
"name": "AdBlue Range"
},
"gas_level": {
"name": "Gas Level"
},
"gas_range": {
"name": "Gas Range"
},
"combustion_range": {
"name": "Combustion Range"
},
Expand Down

0 comments on commit e11c638

Please sign in to comment.