Skip to content

Commit

Permalink
add legacy keys option to prevent all entities from recreating
Browse files Browse the repository at this point in the history
  • Loading branch information
Xitee1 committed Apr 27, 2024
1 parent dae6df0 commit 4bbd7cd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
11 changes: 5 additions & 6 deletions custom_components/sonnenbatterie/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,16 @@ def __init__(
if precision := entity_description.suggested_display_precision:
self._attr_suggested_display_precision = precision

self.entity_id = (
f"sensor.sonnenbatterie_{self.coordinator.serial}_{self.entity_description.key}"
)
self.entity_id = f"sensor.sonnenbatterie_{self.coordinator.serial}_{self.entity_description.key}"

@property
def unique_id(self) -> str:
"""Return a unique ID."""
return f"{self.coordinator.serial}-{self.entity_description.key}"
# return f"{self.coordinator.serial}-{self.entity_description.key}"

# legacy support / prevent breaking changes (will not directly work this way because keys changed)
# return f"sensor.sonnenbatterie_{self.coordinator.serial}_{self.entity_description.key}"
# legacy support / prevent breaking changes
key = self.entity_description.legacy_key or self.entity_description.key
return f"sensor.sonnenbatterie_{self.coordinator.serial}_{key}"

@property
def native_value(self) -> StateType:
Expand Down
41 changes: 32 additions & 9 deletions custom_components/sonnenbatterie/sensor_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@dataclass(frozen=True, kw_only=True)
class SonnenbatterieSensorEntityDescription(SensorEntityDescription):
"""Describes Example sensor entity."""

legacy_key: str = None
# exists_fn: Callable[[SonnenBatterieCoordinator], bool] = lambda _: True
value_fn: Callable[[SonnenBatterieCoordinator], StateType]

Expand Down Expand Up @@ -97,6 +97,7 @@ def generate_powermeter_sensors(_coordinator):
# consumption
SonnenbatterieSensorEntityDescription(
key="state_consumption_current",
legacy_key="consumption_w",
icon="mdi:home-lightning-bolt",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement="W",
Expand All @@ -107,6 +108,7 @@ def generate_powermeter_sensors(_coordinator):
),
SonnenbatterieSensorEntityDescription(
key="state_consumption_avg",
legacy_key="consumption_avg",
icon="mdi:home-lightning-bolt",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement="W",
Expand All @@ -119,7 +121,8 @@ def generate_powermeter_sensors(_coordinator):
###
# production
SonnenbatterieSensorEntityDescription(
key="state_production_w",
key="state_production",
legacy_key="production_w",
icon="mdi:solar-power",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement="W",
Expand Down Expand Up @@ -149,6 +152,7 @@ def generate_powermeter_sensors(_coordinator):
),
SonnenbatterieSensorEntityDescription(
key="state_grid_in",
legacy_key="state_grid_input",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement="W",
device_class=SensorDeviceClass.POWER,
Expand All @@ -164,6 +168,7 @@ def generate_powermeter_sensors(_coordinator):
),
SonnenbatterieSensorEntityDescription(
key="state_grid_out",
legacy_key="state_grid_output",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement="W",
device_class=SensorDeviceClass.POWER,
Expand All @@ -179,19 +184,15 @@ def generate_powermeter_sensors(_coordinator):
),
SonnenbatterieSensorEntityDescription(
key="state_net_frequency",
legacy_key="state_netfrequency",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement="Hz",
device_class=SensorDeviceClass.FREQUENCY,
suggested_display_precision=2,
value_fn=lambda coordinator: (
coordinator.latestData.get("inverter", {}).get("status", {}).get("fac")
or coordinator.latestData.get("battery_system", {})
.get("grid_information", {})
.get("fac")
or coordinator.latestData.get("inverter", {})
.get("status", {})
.get("status", {})
.get("fac")
or coordinator.latestData.get("inverter", {}).get("status", {}).get("status", {}).get("fac")
or coordinator.latestData.get("battery_system", {}).get("grid_information", {}).get("fac")
),
),
###
Expand All @@ -207,6 +208,7 @@ def generate_powermeter_sensors(_coordinator):
),
SonnenbatterieSensorEntityDescription(
key="state_battery_in",
legacy_key="state_battery_input",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement="W",
device_class=SensorDeviceClass.POWER,
Expand All @@ -220,6 +222,7 @@ def generate_powermeter_sensors(_coordinator):
),
SonnenbatterieSensorEntityDescription(
key="state_battery_out",
legacy_key="state_battery_output",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement="W",
device_class=SensorDeviceClass.POWER,
Expand All @@ -233,6 +236,7 @@ def generate_powermeter_sensors(_coordinator):
),
SonnenbatterieSensorEntityDescription(
key="state_battery_percentage_real",
legacy_key="state_charge_real",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement="%",
device_class=SensorDeviceClass.BATTERY,
Expand All @@ -243,6 +247,7 @@ def generate_powermeter_sensors(_coordinator):
),
SonnenbatterieSensorEntityDescription(
key="state_battery_percentage_user",
legacy_key="state_charge_user",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement="%",
device_class=SensorDeviceClass.BATTERY,
Expand All @@ -254,6 +259,7 @@ def generate_powermeter_sensors(_coordinator):
# system
SonnenbatterieSensorEntityDescription(
key="state_system_status",
legacy_key="systemstatus",
device_class=SensorDeviceClass.ENUM,
# TODO if known, the possible states should be added (e.g. options=["OnGrid", "AnotherState"],).
# However, if defined, it will throw an error if the current state is not in the list
Expand All @@ -266,6 +272,7 @@ def generate_powermeter_sensors(_coordinator):
),
SonnenbatterieSensorEntityDescription(
key="state_operating_mode",
legacy_key="operating_mode",
options=["1", "2", "6", "10"],
device_class=SensorDeviceClass.ENUM,
value_fn=lambda coordinator: coordinator.latestData.get("status", {}).get(
Expand All @@ -278,6 +285,7 @@ def generate_powermeter_sensors(_coordinator):
# grid
SonnenbatterieSensorEntityDescription(
key="inverter_state_ipv",
legacy_key="inverter_ipv",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement="A",
device_class=SensorDeviceClass.CURRENT,
Expand All @@ -288,6 +296,7 @@ def generate_powermeter_sensors(_coordinator):
),
SonnenbatterieSensorEntityDescription(
key="inverter_state_ipv2",
legacy_key="inverter_ipv2",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement="A",
device_class=SensorDeviceClass.CURRENT,
Expand All @@ -298,6 +307,7 @@ def generate_powermeter_sensors(_coordinator):
),
SonnenbatterieSensorEntityDescription(
key="inverter_state_ppv",
legacy_key="inverter_ppv",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement="W",
device_class=SensorDeviceClass.POWER,
Expand All @@ -308,6 +318,7 @@ def generate_powermeter_sensors(_coordinator):
),
SonnenbatterieSensorEntityDescription(
key="inverter_state_ppv2",
legacy_key="inverter_ppv2",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement="W",
device_class=SensorDeviceClass.POWER,
Expand All @@ -318,6 +329,7 @@ def generate_powermeter_sensors(_coordinator):
),
SonnenbatterieSensorEntityDescription(
key="inverter_state_upv",
legacy_key="inverter_upv",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement="V",
device_class=SensorDeviceClass.VOLTAGE,
Expand All @@ -328,6 +340,7 @@ def generate_powermeter_sensors(_coordinator):
),
SonnenbatterieSensorEntityDescription(
key="inverter_state_upv2",
legacy_key="inverter_upv2",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement="V",
device_class=SensorDeviceClass.VOLTAGE,
Expand Down Expand Up @@ -366,6 +379,7 @@ def generate_powermeter_sensors(_coordinator):
),
SonnenbatterieSensorEntityDescription(
key="battery_installed_capacity_total",
legacy_key="state_total_capacity_real",
icon="mdi:battery-charging",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement="Wh",
Expand All @@ -378,6 +392,7 @@ def generate_powermeter_sensors(_coordinator):
),
SonnenbatterieSensorEntityDescription(
key="battery_installed_capacity_usable",
legacy_key="state_total_capacity_usable",
icon="mdi:battery-charging",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement="Wh",
Expand All @@ -393,6 +408,7 @@ def generate_powermeter_sensors(_coordinator):
),
SonnenbatterieSensorEntityDescription(
key="battery_remaining_capacity_total",
legacy_key="state_remaining_capacity_real",
icon="mdi:battery-charging",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement="Wh",
Expand All @@ -405,6 +421,7 @@ def generate_powermeter_sensors(_coordinator):
),
SonnenbatterieSensorEntityDescription(
key="battery_remaining_capacity_usable",
legacy_key="state_remaining_capacity_usable",
icon="mdi:battery-charging",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement="Wh",
Expand All @@ -417,6 +434,7 @@ def generate_powermeter_sensors(_coordinator):
),
SonnenbatterieSensorEntityDescription(
key="battery_storage_capacity_per_module",
legacy_key="module_capacity",
icon="mdi:battery-charging",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement="Wh",
Expand All @@ -430,6 +448,7 @@ def generate_powermeter_sensors(_coordinator):
),
SonnenbatterieSensorEntityDescription(
key="battery_module_count",
legacy_key="module_count",
icon="mdi:battery",
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
Expand All @@ -440,6 +459,7 @@ def generate_powermeter_sensors(_coordinator):
),
SonnenbatterieSensorEntityDescription(
key="battery_grid_ipv",
legacy_key="battery_system_ipv",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement="A",
device_class=SensorDeviceClass.CURRENT,
Expand All @@ -450,6 +470,7 @@ def generate_powermeter_sensors(_coordinator):
),
SonnenbatterieSensorEntityDescription(
key="battery_grid_ppv",
legacy_key="battery_system_ppv",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement="W",
device_class=SensorDeviceClass.POWER,
Expand All @@ -460,6 +481,7 @@ def generate_powermeter_sensors(_coordinator):
),
SonnenbatterieSensorEntityDescription(
key="battery_grid_upv",
legacy_key="battery_system_upv",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement="V",
device_class=SensorDeviceClass.VOLTAGE,
Expand All @@ -470,6 +492,7 @@ def generate_powermeter_sensors(_coordinator):
),
SonnenbatterieSensorEntityDescription(
key="battery_grid_tmax",
legacy_key="tmax",
icon="mdi:thermometer-alert",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement="°C",
Expand Down

0 comments on commit 4bbd7cd

Please sign in to comment.