Skip to content

Commit

Permalink
feat: add override_status sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
firstof9 committed Jan 29, 2025
1 parent 4ca3485 commit 3c0af53
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 60 deletions.
23 changes: 23 additions & 0 deletions custom_components/openevse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,8 @@ def parse_sensors(self) -> None:
data = {}
for sensor in SENSOR_TYPES: # pylint: disable=consider-using-dict-items
_sensor = {}
if SENSOR_TYPES[sensor].is_async_value:
continue
try:
sensor_property = SENSOR_TYPES[sensor].key
_sensor[sensor] = getattr(self._manager, sensor_property)
Expand Down Expand Up @@ -498,6 +500,27 @@ async def async_parse_sensors(self) -> None:
number,
)
data.update(_sensor)
for sensor in SENSOR_TYPES: # pylint: disable=consider-using-dict-items
_sensor = {}
if not SENSOR_TYPES[sensor].is_async_value:
continue
try:
sensor_property = SENSOR_TYPES[sensor].key
sensor_value = SENSOR_TYPES[sensor].value
# Data can be sent as boolean or as 1/0
_sensor[sensor] = await getattr(self._manager, sensor_value)
_LOGGER.debug(
"number: %s sensor_property: %s value %s",
sensor,
sensor_property,
_sensor[sensor],
)
except (ValueError, KeyError):
_LOGGER.info(
"Could not update status for %s",
sensor,
)
data.update(_sensor)
_LOGGER.debug("DEBUG: %s", data)
self._data.update(data)

Expand Down
84 changes: 46 additions & 38 deletions custom_components/openevse/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

from .entity import (
OpenEVSESelectEntityDescription,
OpenEVSESensorEntityDescription,
OpenEVSESwitchEntityDescription,
OpenEVSENumberEntityDescription,
OpenEVSELightEntityDescription,
Expand Down Expand Up @@ -98,80 +99,80 @@
DIVERT_MODE = ["fast", "eco"]

# Name, unit of measure, property, icon, device class, state class
SENSOR_TYPES: Final[dict[str, SensorEntityDescription]] = {
"status": SensorEntityDescription(
SENSOR_TYPES: Final[dict[str, OpenEVSESensorEntityDescription]] = {
"status": OpenEVSESensorEntityDescription(
key="status", name="Station Status", icon="mdi:ev-station"
),
"state": SensorEntityDescription(key="state", name="Charging Status"),
"charge_time_elapsed": SensorEntityDescription(
"state": OpenEVSESensorEntityDescription(key="state", name="Charging Status"),
"charge_time_elapsed": OpenEVSESensorEntityDescription(
key="charge_time_elapsed",
name="Charge Time Elapsed",
icon="mdi:camera-timer",
native_unit_of_measurement=UnitOfTime.MINUTES,
entity_category=EntityCategory.DIAGNOSTIC,
suggested_display_precision=1,
),
"ambient_temperature": SensorEntityDescription(
"ambient_temperature": OpenEVSESensorEntityDescription(
key="ambient_temperature",
name="Ambient Temperature",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.TEMPERATURE,
),
"ir_temperature": SensorEntityDescription(
"ir_temperature": OpenEVSESensorEntityDescription(
key="ir_temperature",
name="IR Temperature",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.TEMPERATURE,
entity_registry_enabled_default=False,
),
"rtc_temperature": SensorEntityDescription(
"rtc_temperature": OpenEVSESensorEntityDescription(
key="rtc_temperature",
name="RTC Temperature",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.TEMPERATURE,
entity_registry_enabled_default=False,
),
"esp_temperature": SensorEntityDescription(
"esp_temperature": OpenEVSESensorEntityDescription(
key="esp_temperature",
name="ESP32 Temperature",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.TEMPERATURE,
entity_registry_enabled_default=False,
),
"usage_session": SensorEntityDescription(
"usage_session": OpenEVSESensorEntityDescription(
key="usage_session",
name="Usage this Session",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
state_class=SensorStateClass.TOTAL_INCREASING,
device_class=SensorDeviceClass.ENERGY,
suggested_display_precision=1,
),
"usage_total": SensorEntityDescription(
"usage_total": OpenEVSESensorEntityDescription(
key="usage_total",
name="Total Usage",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
state_class=SensorStateClass.TOTAL_INCREASING,
device_class=SensorDeviceClass.ENERGY,
suggested_display_precision=1,
),
"openevse_firmware": SensorEntityDescription(
"openevse_firmware": OpenEVSESensorEntityDescription(
key="openevse_firmware",
name="Controller Firmware",
icon="mdi:package-up",
entity_category=EntityCategory.DIAGNOSTIC,
),
"protocol_version": SensorEntityDescription(
"protocol_version": OpenEVSESensorEntityDescription(
key="protocol_version",
name="Protocol Version",
icon="mdi:package-up",
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"charging_voltage": SensorEntityDescription(
"charging_voltage": OpenEVSESensorEntityDescription(
key="charging_voltage",
name="Charging Voltage",
icon="mdi:sine-wave",
Expand All @@ -180,7 +181,7 @@
device_class=SensorDeviceClass.VOLTAGE,
suggested_display_precision=1,
),
"charging_current": SensorEntityDescription(
"charging_current": OpenEVSESensorEntityDescription(
key="charging_current",
name="Charging Current",
icon="mdi:sine-wave",
Expand All @@ -189,43 +190,43 @@
device_class=SensorDeviceClass.CURRENT,
suggested_display_precision=1,
),
"service_level": SensorEntityDescription(
"service_level": OpenEVSESensorEntityDescription(
key="service_level",
name="Service Level",
icon="mdi:leaf",
entity_category=EntityCategory.DIAGNOSTIC,
),
"max_amps": SensorEntityDescription(
"max_amps": OpenEVSESensorEntityDescription(
key="max_amps",
name="Max Amps",
icon="mdi:sine-wave",
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
device_class=SensorDeviceClass.CURRENT,
entity_category=EntityCategory.DIAGNOSTIC,
),
"min_amps": SensorEntityDescription(
"min_amps": OpenEVSESensorEntityDescription(
key="min_amps",
name="Min Amps",
icon="mdi:sine-wave",
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
device_class=SensorDeviceClass.CURRENT,
entity_category=EntityCategory.DIAGNOSTIC,
),
"current_capacity": SensorEntityDescription(
"current_capacity": OpenEVSESensorEntityDescription(
key="current_capacity",
name="Current Capacity",
icon="mdi:sine-wave",
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
device_class=SensorDeviceClass.CURRENT,
entity_category=EntityCategory.DIAGNOSTIC,
),
"wifi_firmware": SensorEntityDescription(
"wifi_firmware": OpenEVSESensorEntityDescription(
key="wifi_firmware",
name="WiFi Firmware Version",
icon="mdi:package-up",
entity_category=EntityCategory.DIAGNOSTIC,
),
"charging_power": SensorEntityDescription(
"charging_power": OpenEVSESensorEntityDescription(
key="charging_power",
name="Current Power Usage",
icon="mdi:flash",
Expand All @@ -234,27 +235,27 @@
state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=1,
),
"wifi_signal": SensorEntityDescription(
"wifi_signal": OpenEVSESensorEntityDescription(
key="wifi_signal",
name="WiFi Signal Strength",
icon="mdi:wifi",
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS,
entity_category=EntityCategory.DIAGNOSTIC,
),
"ammeter_scale_factor": SensorEntityDescription(
"ammeter_scale_factor": OpenEVSESensorEntityDescription(
key="ammeter_scale_factor",
name="Sensor Scale",
icon="mdi:scale",
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
),
"charge_mode": SensorEntityDescription(
"charge_mode": OpenEVSESensorEntityDescription(
name="Divert Mode",
key="charge_mode",
icon="mdi:solar-power",
entity_category=EntityCategory.DIAGNOSTIC,
),
"available_current": SensorEntityDescription(
"available_current": OpenEVSESensorEntityDescription(
name="PV Available Current",
key="available_current",
icon="mdi:sine-wave",
Expand All @@ -263,7 +264,7 @@
device_class=SensorDeviceClass.CURRENT,
suggested_display_precision=1,
),
"smoothed_available_current": SensorEntityDescription(
"smoothed_available_current": OpenEVSESensorEntityDescription(
name="PV Smoothed Available Current",
key="smoothed_available_current",
icon="mdi:sine-wave",
Expand All @@ -272,15 +273,15 @@
device_class=SensorDeviceClass.CURRENT,
suggested_display_precision=1,
),
"charge_rate": SensorEntityDescription(
"charge_rate": OpenEVSESensorEntityDescription(
name="PV Charge Rate",
key="charge_rate",
icon="mdi:sine-wave",
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.CURRENT,
),
"shaper_live_power": SensorEntityDescription(
"shaper_live_power": OpenEVSESensorEntityDescription(
name="Shaper Power",
key="shaper_live_power",
icon="mdi:flash",
Expand All @@ -289,7 +290,7 @@
device_class=SensorDeviceClass.POWER,
entity_registry_enabled_default=False,
),
"shaper_current": SensorEntityDescription(
"shaper_current": OpenEVSESensorEntityDescription(
name="Shaper Current",
key="shaper_current_power",
icon="mdi:flash",
Expand All @@ -298,15 +299,15 @@
state_class=SensorStateClass.MEASUREMENT,
entity_registry_enabled_default=False,
),
"shaper_max_power": SensorEntityDescription(
"shaper_max_power": OpenEVSESensorEntityDescription(
name="Shaper Max Power",
key="shaper_max_power",
icon="mdi:flash",
native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER,
entity_registry_enabled_default=False,
),
"vehicle_soc": SensorEntityDescription(
"vehicle_soc": OpenEVSESensorEntityDescription(
name="Vehicle Battery Level",
key="vehicle_soc",
icon="mdi:battery",
Expand All @@ -315,69 +316,76 @@
state_class=SensorStateClass.MEASUREMENT,
entity_registry_enabled_default=False,
),
"vehicle_range": SensorEntityDescription(
"vehicle_range": OpenEVSESensorEntityDescription(
name="Vehicle Range",
key="vehicle_range",
icon="mdi:ev-station",
native_unit_of_measurement=UnitOfLength.KILOMETERS,
device_class=SensorDeviceClass.DISTANCE,
entity_registry_enabled_default=False,
),
"vehicle_eta": SensorEntityDescription(
"vehicle_eta": OpenEVSESensorEntityDescription(
name="Vehicle Charge Completion",
key="vehicle_eta",
icon="mdi:car-electric",
native_unit_of_measurement=UnitOfTime.SECONDS,
state_class=SensorStateClass.MEASUREMENT,
entity_registry_enabled_default=False,
),
"vehicle_eta_timestamp": SensorEntityDescription(
"vehicle_eta_timestamp": OpenEVSESensorEntityDescription(
name="Vehicle Charge Completion Time",
key="vehicle_eta",
icon="mdi:car-electric",
device_class=SensorDeviceClass.TIMESTAMP,
entity_registry_enabled_default=False,
),
"total_day": SensorEntityDescription(
"total_day": OpenEVSESensorEntityDescription(
key="total_day",
name="Usage (Today)",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
state_class=SensorStateClass.TOTAL_INCREASING,
device_class=SensorDeviceClass.ENERGY,
entity_registry_enabled_default=False,
),
"total_week": SensorEntityDescription(
"total_week": OpenEVSESensorEntityDescription(
key="total_week",
name="Usage (Week)",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
state_class=SensorStateClass.TOTAL_INCREASING,
device_class=SensorDeviceClass.ENERGY,
entity_registry_enabled_default=False,
),
"total_month": SensorEntityDescription(
"total_month": OpenEVSESensorEntityDescription(
key="total_month",
name="Usage (Month)",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
state_class=SensorStateClass.TOTAL_INCREASING,
device_class=SensorDeviceClass.ENERGY,
entity_registry_enabled_default=False,
),
"total_year": SensorEntityDescription(
"total_year": OpenEVSESensorEntityDescription(
key="total_year",
name="Usage (Year)",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
state_class=SensorStateClass.TOTAL_INCREASING,
device_class=SensorDeviceClass.ENERGY,
entity_registry_enabled_default=False,
),
"max_current": SensorEntityDescription(
"max_current": OpenEVSESensorEntityDescription(
key="max_current",
name="Max Current",
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
device_class=SensorDeviceClass.CURRENT,
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
),
"override_state": OpenEVSESensorEntityDescription(
key="override_state",
name="Override State",
entity_category=EntityCategory.DIAGNOSTIC,
is_async_value=True,
value="async_override_state",
),
}

SWITCH_TYPES: Final[dict[str, OpenEVSESwitchEntityDescription]] = {
Expand Down
9 changes: 9 additions & 0 deletions custom_components/openevse/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from homeassistant.components.light import LightEntityDescription
from homeassistant.components.number import NumberEntityDescription
from homeassistant.components.select import SelectEntityDescription
from homeassistant.components.sensor import SensorEntityDescription
from homeassistant.components.switch import SwitchEntityDescription


Expand Down Expand Up @@ -44,3 +45,11 @@ class OpenEVSELightEntityDescription(LightEntityDescription):
"""Class describing OpenEVSE light entities."""

command: str | None = None


@dataclass
class OpenEVSESensorEntityDescription(SensorEntityDescription):
"""Class describing OpenEVSE sensor entities."""

is_async_value: bool | None = False
value: int | None = None
2 changes: 1 addition & 1 deletion custom_components/openevse/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"iot_class": "local_push",
"issue_tracker": "https://github.com/firstof9/openevse/issues",
"loggers": ["openevsehttp"],
"requirements": ["python-openevse-http==0.1.70"],
"requirements": ["python-openevse-http==0.1.71"],
"version": "0.0.0-dev",
"zeroconf": ["_openevse._tcp.local."]
}
Loading

0 comments on commit 3c0af53

Please sign in to comment.