Skip to content

Commit

Permalink
fix: fix sensors and config flow (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
woopstar authored Nov 27, 2024
1 parent e4e3d69 commit f0016f9
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,6 @@ async def _async_add_integral_sensor(self):
unit_time=UnitOfTime.HOURS,
max_sub_interval=timedelta(minutes=1),
device_info=None,
id=integral_sensor_unique_id,
config_entry=self._config_entry,
)

Expand Down Expand Up @@ -354,7 +353,6 @@ async def _async_add_energy_average_sensors(self, avg=3):
samples_keep_last=True,
precision=2,
percentile=50,
id=avg_energy_sensor_unique_id,
config_entry=self._config_entry,
)

Expand Down Expand Up @@ -423,7 +421,6 @@ async def _async_add_utility_meter_sensor(self):
unique_id=utility_meter_unique_id,
device_info=None,
sensor_always_available=True,
id=utility_meter_unique_id,
config_entry=self._config_entry,
)

Expand Down
13 changes: 2 additions & 11 deletions custom_components/hsem/custom_sensors/integration_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ class HSEMIntegrationSensor(IntegrationSensor, HSEMEntity):

_attr_icon = "mdi:chart-histogram"

def __init__(self, *args, id=None, config_entry=None, **kwargs):
def __init__(self, *args, config_entry=None, **kwargs):
IntegrationSensor.__init__(self, *args, **kwargs)
HSEMEntity.__init__(self, config_entry)
self._unique_id = id

@property
def state_class(self):
Expand All @@ -24,12 +23,4 @@ def device_class(self):

@property
def unique_id(self):
return self._unique_id

async def async_added_to_hass(self):
"""Handle the sensor being added to Home Assistant."""
await super().async_added_to_hass()

async def async_will_remove_from_hass(self):
"""Entity being removed from hass."""
await super().async_will_remove_from_hass()
return self._attr_unique_id
13 changes: 2 additions & 11 deletions custom_components/hsem/custom_sensors/statistics_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ class HSEMStatisticsSensor(StatisticsSensor, HSEMEntity):

_attr_icon = "mdi:calculator"

def __init__(self, *args, id=None, config_entry=None, **kwargs):
def __init__(self, *args, config_entry=None, **kwargs):
StatisticsSensor.__init__(self, *args, **kwargs)
HSEMEntity.__init__(self, config_entry)
self._unique_id = id

@property
def unit_of_measurement(self):
Expand All @@ -25,12 +24,4 @@ def device_class(self):

@property
def unique_id(self):
return self._unique_id

async def async_added_to_hass(self):
"""Handle the sensor being added to Home Assistant."""
await super().async_added_to_hass()

async def async_will_remove_from_hass(self):
"""Entity being removed from hass."""
await super().async_will_remove_from_hass()
return self._attr_unique_id
13 changes: 2 additions & 11 deletions custom_components/hsem/custom_sensors/utility_meter_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ class HSEMUtilityMeterSensor(UtilityMeterSensor, HSEMEntity):

_attr_icon = "mdi:counter"

def __init__(self, *args, id=None, config_entry=None, **kwargs):
def __init__(self, *args, config_entry=None, **kwargs):
UtilityMeterSensor.__init__(self, *args, **kwargs)
HSEMEntity.__init__(self, config_entry)
self._unique_id = id

@property
def unique_id(self):
return self._unique_id
return self._attr_unique_id

@property
def unit_of_measurement(self):
Expand All @@ -30,11 +29,3 @@ def device_class(self):
@property
def state_class(self):
return SensorStateClass.TOTAL

async def async_added_to_hass(self):
"""Handle the sensor being added to Home Assistant."""
await super().async_added_to_hass()

async def async_will_remove_from_hass(self):
"""Entity being removed from hass."""
await super().async_will_remove_from_hass()
8 changes: 2 additions & 6 deletions custom_components/hsem/custom_sensors/working_mode_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def __init__(self, config_entry):

def _update_settings(self):
"""Fetch updated settings from config_entry options."""
self._read_only = get_config_value(self._config_entry, "hsem_read_only", False)
self._read_only = get_config_value(self._config_entry, "hsem_read_only")

self._hsem_huawei_solar_device_id_inverter_1 = get_config_value(
self._config_entry, "hsem_huawei_solar_device_id_inverter_1"
Expand Down Expand Up @@ -240,7 +240,7 @@ def _update_settings(self):
self._config_entry, "hsem_batteries_enable_charge_hours_night_end"
)
self._hsem_batteries_rated_capacity = get_config_value(
self._config_entry,"hsem_huawei_solar_batteries_rated_capacity"
self._config_entry, "hsem_huawei_solar_batteries_rated_capacity"
)

if self._hsem_huawei_solar_device_id_inverter_2 is not None:
Expand Down Expand Up @@ -1208,7 +1208,3 @@ async def async_added_to_hass(self):

# Initial update
await self._async_handle_update(None)

async def async_will_remove_from_hass(self):
"""Entity being removed from hass."""
await super().async_will_remove_from_hass()
30 changes: 30 additions & 0 deletions custom_components/hsem/entity.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import logging

import homeassistant.helpers.device_registry as dr
import homeassistant.helpers.entity_registry as er
from homeassistant.helpers.restore_state import RestoreEntity

from custom_components.hsem.const import DOMAIN, NAME
Expand Down Expand Up @@ -53,3 +55,31 @@ def device_info(self):
"name": self.config_entry.data.get("device_name", NAME),
"manufacturer": NAME,
}

@property
def should_poll(self):
"""Return False because entity pushes its state to HA"""
return False

async def async_will_remove_from_hass(self):
"""Entity being removed from hass."""
await super().async_will_remove_from_hass()

async def async_added_to_hass(self) -> None:
"""Attach the entity to same device as the source entity."""
await super().async_added_to_hass()

entity_reg = er.async_get(self.hass)
entity_entry = entity_reg.async_get(self.entity_id)
if entity_entry is None or not hasattr(self, "source_device_id"):
return

device_id: str = getattr(self, "source_device_id") # noqa: B009
device_reg = dr.async_get(self.hass)
device_entry = device_reg.async_get(device_id)
if (
not device_entry or device_entry.id == entity_entry.device_id
): # pragma: no cover
return
_LOGGER.debug("Binding %s to device %s", self.entity_id, device_id)
entity_reg.async_update_entity(self.entity_id, device_id=device_id)
6 changes: 3 additions & 3 deletions custom_components/hsem/flows/huawei_solar.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ async def get_huawei_solar_step_schema(config_entry):
),
): selector({"entity": {"domain": ["sensor", "input_number"]}}),
vol.Required(
"hsem_battery_conversion_loss",
"hsem_batteries_conversion_loss",
default=get_config_value(
config_entry,
"hsem_battery_conversion_loss",
"hsem_batteries_conversion_loss",
DEFAULT_HSEM_BATTERY_CONVERSION_LOSS,
),
): selector(
Expand Down Expand Up @@ -132,7 +132,7 @@ async def validate_huawei_solar_input(user_input):
"hsem_huawei_solar_batteries_grid_charge_cutoff_soc",
"hsem_huawei_solar_batteries_tou_charging_and_discharging_periods",
"hsem_huawei_solar_batteries_rated_capacity",
"hsem_battery_conversion_loss",
"hsem_batteries_conversion_loss",
]

for field in required_fields:
Expand Down
8 changes: 4 additions & 4 deletions custom_components/hsem/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
},
"huawei_solar": {
"data": {
"hsem_battery_conversion_loss": "Battery Conversion Loss (%)",
"hsem_battery_max_capacity": "Battery Max Capacity (kWh)",
"hsem_batteries_conversion_loss": "Battery Conversion Loss (%)",
"hsem_huawei_solar_batteries_rated_capacity": "Battery Max Capacity (Watt)",
"hsem_huawei_solar_batteries_grid_charge_cutoff_soc": "Huawei Batteries Grid Charge Cutoff SOC (%)",
"hsem_huawei_solar_batteries_maximum_charging_power": "Huawei Batteries Maximum Charging Power (kW)",
"hsem_huawei_solar_batteries_state_of_capacity": "Huawei Batteries State of Capacity Sensor",
Expand Down Expand Up @@ -130,8 +130,8 @@
},
"huawei_solar": {
"data": {
"hsem_battery_conversion_loss": "Battery Conversion Loss (%)",
"hsem_battery_max_capacity": "Battery Max Capacity (kWh)",
"hsem_batteries_conversion_loss": "Battery Conversion Loss (%)",
"hsem_huawei_solar_batteries_rated_capacity": "Battery Max Capacity (Watt)",
"hsem_house_consumption_energy_weight_14d": "House Consumption Energy Weight 14 Days",
"hsem_house_consumption_energy_weight_1d": "House Consumption Energy Weight 1 Day",
"hsem_house_consumption_energy_weight_3d": "House Consumption Energy Weight 3 Days",
Expand Down

0 comments on commit f0016f9

Please sign in to comment.