Skip to content

Commit

Permalink
Add refresh button entity
Browse files Browse the repository at this point in the history
  • Loading branch information
WillCodeForCats committed Jul 29, 2023
1 parent f70ca8d commit a2e3bd2
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions custom_components/solaredge_modbus_multi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

PLATFORMS: list[str] = [
Platform.BINARY_SENSOR,
Platform.BUTTON,
Platform.NUMBER,
Platform.SELECT,
Platform.SENSOR,
Expand Down
82 changes: 82 additions & 0 deletions custom_components/solaredge_modbus_multi/button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
"""Component to interface with binary sensors."""
from __future__ import annotations

import logging

from homeassistant.components.button import ButtonEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import DOMAIN

_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
hub = hass.data[DOMAIN][config_entry.entry_id]["hub"]
coordinator = hass.data[DOMAIN][config_entry.entry_id]["coordinator"]

entities = []

for inverter in hub.inverters:
entities.append(SolarEdgeRefreshButton(inverter, config_entry, coordinator))

if entities:
async_add_entities(entities)


class SolarEdgeButtonBase(CoordinatorEntity, ButtonEntity):
_attr_has_entity_name = True

def __init__(self, platform, config_entry, coordinator):
"""Pass coordinator to CoordinatorEntity."""
super().__init__(coordinator)
"""Initialize the sensor."""
self._platform = platform
self._config_entry = config_entry

@property
def device_info(self):
return self._platform.device_info

@property
def config_entry_id(self):
return self._config_entry.entry_id

@property
def config_entry_name(self):
return self._config_entry.data["name"]

@property
def available(self) -> bool:
return super().available and self._platform.online

@callback
def _handle_coordinator_update(self) -> None:
self.async_write_ha_state()


class SolarEdgeRefreshButton(SolarEdgeButtonBase):
entity_category = EntityCategory.CONFIG

def __init__(self, platform, config_entry, coordinator):
super().__init__(platform, config_entry, coordinator)
"""Initialize the sensor."""

@property
def unique_id(self) -> str:
return f"{self._platform.uid_base}_refresh"

@property
def name(self) -> str:
return "Refresh"

async def async_press(self) -> None:
await self.async_update()

0 comments on commit a2e3bd2

Please sign in to comment.