Skip to content

Commit

Permalink
Merge pull request #63 from jmcollin78/issue_60-create-virtual-for-ma…
Browse files Browse the repository at this point in the history
…naged-device

Add device_name attribute and remove warnings
  • Loading branch information
jmcollin78 authored Sep 30, 2024
2 parents 66f43a1 + 6becf43 commit af49aa9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
13 changes: 13 additions & 0 deletions custom_components/solar_optimizer/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,16 @@ class ConfigurationError(Exception):

def __init__(self, message):
super().__init__(message)


class overrides: # pylint: disable=invalid-name
"""An annotation to inform overrides"""

def __init__(self, func):
self.func = func

def __get__(self, instance, owner):
return self.func.__get__(instance, owner)

def __call__(self, *args, **kwargs):
raise RuntimeError(f"Method {self.func.__name__} should have been overridden")
11 changes: 11 additions & 0 deletions custom_components/solar_optimizer/managed_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,17 @@ def max_on_time_per_day_sec(self) -> int:
"""The max_on_time_per_day_sec configured"""
return self._max_on_time_per_day_sec

@property
def battery_soc(self) -> int:
"""The battery soc"""
return self._battery_soc

@property
def battery_soc_threshold(self) -> int:
"""The battery soc"""
return self._battery_soc_threshold


def set_battery_soc(self, battery_soc):
"""Define the battery soc. This is used with is_usable
to determine if the device is usable"""
Expand Down
34 changes: 26 additions & 8 deletions custom_components/solar_optimizer/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
EVENT_TYPE_SOLAR_OPTIMIZER_ENABLE_STATE_CHANGE,
DEVICE_MANUFACTURER,
DEVICE_MODEL,
overrides,
)
from .coordinator import SolarOptimizerCoordinator
from .managed_device import ManagedDevice
Expand Down Expand Up @@ -202,8 +203,9 @@ def update_custom_attributes(self, device):
"next_date_available_power": device.next_date_available_power.astimezone(
current_tz
).isoformat(),
"battery_soc_threshold": device._battery_soc_threshold,
"battery_soc": device._battery_soc,
"battery_soc_threshold": device.battery_soc_threshold,
"battery_soc": device.battery_soc,
"device_name": device.name,
}

@callback
Expand Down Expand Up @@ -276,6 +278,14 @@ def get_attr_extra_state_attributes(self):
"""Get the extra state attributes for the entity"""
return self._attr_extra_state_attributes

@overrides
def turn_off(self, **kwargs: Any):
"""Not used"""

@overrides
def turn_on(self, **kwargs: Any):
"""Not used"""


class ManagedDeviceEnable(SwitchEntity, RestoreEntity):
"""The that enables the ManagedDevice optimisation with"""
Expand Down Expand Up @@ -325,20 +335,28 @@ async def async_added_to_hass(self):
@callback
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the entity on."""
self._attr_is_on = True
self.async_write_ha_state()
self.update_device_enabled()
self.turn_on()

@callback
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the entity off."""
self._attr_is_on = False
self.async_write_ha_state()
self.update_device_enabled()
self.turn_off()

def update_device_enabled(self) -> None:
"""Update the device is enabled flag"""
if not self._device:
return

self._device.set_enable(self._attr_is_on)

@overrides
def turn_off(self, **kwargs: Any):
self._attr_is_on = False
self.async_write_ha_state()
self.update_device_enabled()

@overrides
def turn_on(self, **kwargs: Any):
self._attr_is_on = True
self.async_write_ha_state()
self.update_device_enabled()

0 comments on commit af49aa9

Please sign in to comment.